-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Aggregate by count is broken when used with distinct clause and select with struct value #4568
Comments
Thanks for the report. I believe this is working as expected but it's probably not obvious enough why. If you use The reason for this restriction is because when you select a map in a subquery, you turn it into something like this:
Where
rather than
If we didn't do it this way then the database would return all of the fields in the underlying table even if they weren't in the schema. TLDR: I believe this is expected and can't be changed. But we can probably update the docs to make this clearer. |
@greg-rychlewski in this case though, since we are not aggregating over a particular column, we could nuke the select? Or have it as select: 1? |
I think something like The only hesitation I have is there are databases out there like clickhouse and others that are supporting more non-standard functions. I don't know for sure if there is any aggregate function out there that can operate on 0 args and might be affected. A couple of ways I can see this theoretically breaking something, but can't find an example for:
The chance of breaking seems low to me based on what I know. But I'm not sure how much I don't know :). So it's probably a choice between being extra cautious and improving the docs or taking a tiny chance it breaks an unusual case and we have to revert. I slightly prefer the status quo just because it's less manipulation of user input and I don't see a strong reason to allow aggregating over a query that Ecto would not let you run anyways. But I am ok changing it too if you think that will help more people. |
I think the confusion is exactly that the query they pass actually run, it doesn't run because we move it to a subquery behind the scenes. I think we could:
|
It works well for the standard use cases I am aware of. For number 2 in particular I am not sure if there exist some non standard use cases where the properties of the row are important without needing any particular value for the aggregation. As a contrived example if there was a count_not_all_null convenience function in some database that checks if all columns are null or not. Then us changing the value will not be good. I don’t use this function though and the use case for it is not super clear to me. If it’s meant as a convenience for the user to give it a query they are using somewhere else in their app and leave it to Ecto to make it work with aggregations then what you said is probably best and we just don’t support uncommon cases that don’t fit our assumptions |
You are right. I believe our best option then is to improve the docs. I will push something soon. |
Elixir version
1.17.1
Database and Version
PostgreSQL 16.4
Ecto Versions
main
Database Adapter and Versions (postgrex, myxql, etc)
postgrex
Current behavior
Aggregating by count fails if the query includes a distinct clause, and a select clause that would not be allowed in a subquery.
PR with failing test, for reference: #4567
Expected behavior
1
The text was updated successfully, but these errors were encountered: