hauleth

hauleth

Proposal: Alias for current query select in Ecto queries

Currently such query:

from foo in Foo,
 group_by: fragment("date_part(?, ?)", "day", foo.inserted_at),
 select: %{for: fragment("date_part(?, ?)", "day", foo.inserted_at), count: count(foo.id)}

Is invalid (at least in PostgreSQL) with very confusing error (at least for the newcomers):

column "p0.inserted_at" must appear in the GROUP BY clause or be used in aggregate function

Everything is because what DB sees is something like:

SELECT
  date_part(foo.inserted_at, $1) AS for,
  COUNT(foo.id) AS count
FROM foos foo
GROUP BY
    date_part(foo.inserted_at, $2)

And while we knows that value of $1 and $2 is the same the query planner cannot assume that, because there is not enough data. What we can do currently is:

from foo in Foo,
 group_by: fragment(~s["for"]),
 select: %{for: fragment("date_part(?, ?)", "day", foo.inserted_at), count: count(foo.id)}

Which will use our alias for for field, however this is sub perfect experience, what I would like to see instead is something like:

from foo in Foo,
 group_by: self.for,
 select: %{for: fragment("date_part(?, ?)", "day", foo.inserted_at), count: count(foo.id)}

Or similar (no need to use exactly self word). This would allow to have better experience when working with SQL functions and to reduce duplication in some OLAP queries.

Not that we cannot use foo as the foo.for as we can have the same field names in select as there are in foo and we need to distinguish between them.

Where Next?

Popular in Discussions Top

jeramyRR
This is an interesting article to read. Elixir’s performance, like usual, is excellent. However, it seems like the high CPU usage is co...
New
New
axelson
Decided against including more info in the title, but the gist is that Plataformatec sponsored projects will continue with the assets bei...
New
arcanemachine
https://nitter.net/josevalim/status/1744395345872683471 https://twitter.com/josevalim/status/1744395345872683471
New
Ankhers
Just a little information upfront. Generally speaking, if I feel like I need to either break a pipe chain or use an anonymous function in...
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
acrolink
How does the two languages compare when it comes to server side application development? Any experiences or ideas? Thank you.
New
scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Other popular topics Top

mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
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

We're in Beta

About us Mission Statement