Codball

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

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

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

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

Eiji

right, it should be List - not Tuple

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

We're in Beta

About us Mission Statement