Sanjibukai

Sanjibukai

How to apply SQL array aggregation function in Ecto

Hi everybody,

Edit for TL;DR:
How I can use SQL array_agg aggregate function with an Ecto fragment.
Particularly how I can nest the selects..

Let me start with some little explanations of my problem (because it might be irrelevant in real world use case..)

Say I have Posts and Users and I want to fetch all users and also all posts associated to each user.
The association I have is only defined in the Post schema with a belongs_to :user, User
So the following give me what I want but with the User duplicated in every row:

iex> Repo.all from p in Post,
         join: u in assoc(p, :user),
         select: {u.name, p.title}
[
  {"José", "Post 1"},
  {"José", "Post 2"},
  {"Joe", "Post A"},
  {"Joe", "post B"}
]

Now what I’m wanting is to get the following:

[
  {"José", ["Post 1", "Post 2"]},
  {"Joe", ["Post A", "Post B"]}
]

I can achieve this result using Enum.reduce with a Map.update in Elixir side:

Enum.reduce(map, %{}, fn {k, v}, acc -> Map.update(acc, k, [v], &[v|&1]) end)

All of the above are examples using select and ending with results having basic typed values (like strings here). And in fact name here is to be considered unique or maybe adding the id as a key, but the point here is that I can always achieve what I want in Elixir side.

And I also know that having has_many :post on the User Schema and then using preload could do the trick but only with having the elixir Structs. I mean I didn’t managed to use select as above to just grab the user name and the video title.

Note: I know that this question might seem to mix concerns like domain and views.
In real world scenario, I would have loaded the Structs and then grab according fields within a view, etc..
But I’m asking for learning purpose.

So I wanted to know if it’s somehow possible to achieve that using more elaborated SQL queries..
I tried many naive code combinations without success..
For example, I tried to use group_by: :user but then I got an error because of the lack of aggregating function.
Then I discovered array_agg aggregate function in SQL (and postgres). I guess I need to use it somehow with fragment but I don’t have any clue how to use it (besides simple example I saw in the documentation)?

So does anyone can give me some hints..

Marked As Solved

hauleth

hauleth

from p in Post,
  join: u in assoc(p, :user),
  group_by: u.name,
  select: {u.name, fragment("array_agg(?)", p.title)}

You can clean it a little by using macro:

defmacro array_agg(field) do
  quote do: fragment("array_agg(?)", unquote(field))

from p in Post,
  join: u in assoc(p, :user),
  group_by: u.name,
  select: {u.name, array_agg(p.title)}

Or if you use many of such custom functions then I have created ecto_function which provides helper functions for defining such macros. So the above will be:

defqueryfunc array_agg(field)

from p in Post,
  join: u in assoc(p, :user),
  group_by: u.name,
  select: {u.name, array_agg(p.title)}
16
Post #2

Also Liked

Sanjibukai

Sanjibukai

Thanks for your clear answer. I appreciated how you gradually put things, finishing by presenting your package.

I took a look and it’s definitely a good idea!

rameramwe

rameramwe

Thank you very much! i was stuck for multiple hours on this :smiley:
You’re awesome!

Last Post!

rameramwe

rameramwe

Thank you very much! i was stuck for multiple hours on this :smiley:
You’re awesome!

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
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

We're in Beta

About us Mission Statement