Codball
Converting a nested keyword list into a keyword map
I have a function
def list_store_params do
#query = Ecto.Query.from(s in Store, select: [s.state, s.city, s.name], distinct: true)
IO.inspect(Repo.all(query()))
query()
|> Repo.all()
|> Enum.group_by(&Enum.take(&1, 2))
|> Enum.map(fn {key, value} ->
key ++ Enum.flat_map(value, &Enum.drop(&1, 2))
end)
|> Enum.chunk_every(2)
|> IO.inspect
end
def query,
do:
from(s in Store,
order_by: [asc: s.state, asc: s.city, asc: s.name],
select: %{
a: s.state,
b: s.city,
c: s.name
}
)
that converts
[
%{a: "Idaho", b: "Boise", c: "Test"},
%{a: "Colorado", b: "Denver", c: "Eagle"},
%{a: "Colorado", b: "Denver", c: "Jelly Beans"},
%{a: "Colorado", b: "Denver", c: "Name"}
]
into
[
[
[a: "Idaho", b: "Boise", c: "Test"],
[a: "Colorado", b: "Denver", c: "Eagle", c: "Jelly Beans", c: "Name"]
]
]
which I need to convert to a keyword map {a: “Idaho”, b: “Boise”, c: “Test”}. I’m having trouble understanding Enum.into and Map.new, does anyone have any direction/sources here?
Marked As Solved
Eiji
@Codball: Sorry for delay …
First of all for that you do not need any arrays in a, b and c key. You only need to iterate by all records from database.
If for some reason you still want to have a nested struct then I guess that you would need something like:
%{
"Colorado" => %{"Denver" => ["Eagle", "Jelly Beans", "Name"]},
"Idaho" => %{"Boise" => ["Test"]}
}
To achieve that in pure SQL query we would need to use lateral joins and some jsonb functions. Here is example code:
defmodule Example do
alias Ecto.Query
alias Example.{Repo, Store}
require Query
def sample do
query =
Store
|> Query.from(as: :store1)
|> Query.join(
:inner_lateral,
[store1: store1],
store2 in fragment(
"""
select jsonb_object_agg(store2.city, store3.data) as data
from stores store2
inner join lateral (
select array_agg(name) as data
from stores store3
where store2.state = store3.state and store2.city = store3.city
) store3 on true
where store2.state = ?
""",
store1.state
),
as: :store2
)
|> Query.select(
[store1: store1, store2: store2],
fragment(
"jsonb_object_agg(?, ?)",
store1.state,
store2.data
)
)
Repo.one!(query)
end
end
Here is alternative version for inner join:
defmodule Example do
alias Ecto.Query
alias Example.{Repo, Store}
require Query
def sample do
store3_query =
Store
|> Query.from(as: :store3)
|> Query.group_by([store3: store3], [store3.city, store3.state])
|> Query.select([store3: store3], %{
city: store3.city,
data: fragment("array_agg(?)", store3.name),
state: store3.state
})
|> Query.subquery()
store2_query =
Store
|> Query.from(as: :store2)
|> Query.group_by([store2: store2], store2.state)
|> Query.join(
:inner,
[store2: store2],
store3 in ^store3_query,
as: :store3,
on: store2.city == store3.city and store2.state == store3.state
)
|> Query.select(
[store2: store2, store3: store3],
%{data: fragment("jsonb_object_agg(?, ?)", store2.city, store3.data), state: store2.state}
)
|> Query.subquery()
query =
Store
|> Query.from(as: :store1)
|> Query.join(
:inner,
[store1: store1],
store2 in ^store2_query,
as: :store2,
on: store1.state == store2.state
)
|> Query.select(
[store1: store1, store2: store2],
fragment(
"jsonb_object_agg(?, ?)",
store1.state,
store2.data
)
)
Repo.one!(query)
end
end
I’m not sure, but it should be slower in benchmarks than first one, but at least looks much more nice.
Running Example.sample would return previously pasted sample output.
Here is special version if you do not want to do it on database level (due to ecto problems with lateral joins where we need to use big fragments blobs).
defmodule Example do
def sample(input), do: Enum.reduce(input, %{}, &do_sample/2)
defp do_sample(%{a: a, b: b, c: c}, acc),
do: update_in(acc, [Access.key(a, %{}), Access.key(b, [])], &[c | &1 || []])
end
input = [
%{a: "Idaho", b: "Boise", c: "Test"},
%{a: "Colorado", b: "Denver", c: "Eagle"},
%{a: "Colorado", b: "Denver", c: "Jelly Beans"},
%{a: "Colorado", b: "Denver", c: "Name"}
]
Example.sample(input)
%{
"Colorado" => %{"Denver" => ["Name", "Jelly Beans", "Eagle"]},
"Idaho" => %{"Boise" => ["Test"]}
}
In order to use any of those function returns in template you would need to:
<%= for {state, cities} <- @data do %>
<%= for {city, names} <- cities do %>
<%= for name <- names do %>
<%= state %>
<%= cite %>
<%= name %>
<% end %>
<% end %>
<% end %>
Also Liked
Codball
I think I should probably be accomplishing what I need through the query in the first place rather than doing math on the whole database client side. The idea is to group c in b and b in a.
I want to show a distinct list of states, with a distinct list of cities with a list of all entries of c.
it should look like diagram
Eiji
Yeah, I also think that’s best solution.
I do not have repository to test this code, but it should look like:
alias Ecto.Query
require Query
Store
|> Query.from(as: :store)
|> Query.group_by([store: store], [store.state, store.city])
|> Query.select([store: store], %{
a: store.state,
b: store.city,
c: fragment("array_agg(?)", store.name)
})
Eiji
right, it should be List - not Tuple









