-
Notifications
You must be signed in to change notification settings - Fork 21.7k
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
#count doesn't honor distinct attributes in select clause #5554
Comments
FYI, as a workaround you can use the :select option to replace the "*" with the columns, &c. of your choosing.
|
I'm seeing the same behavior with that workaround as well:
|
Ah you're right. Unfortunately I don't have a record of what I originally ran that set me astray. |
It seems Kaminari is working around this bug, with massive performance consequences for us. I've written a post describing the issue here: http://devblog.agworld.com.au/post/22194500063/distinctly-uncountable I've replicated the issue here: https://github.com/agworld/distinctly_uncountable It seems that BTW, this may be related to #1003. |
Nice writeup of the issue and performance in different cases. It seems that for now, if you need an accurate count/sum/etc. and there is a distinct clause on the query, you have to instantiate the AR object array. Not very performant, but that's better than getting inaccurate results. 1003 is definitely related. |
Hi, i think this is expected as you see here. If you remove that validation, you will get:
/cc @carlosantoniodasilva @rafaelfranca I'm not sure about this, but i'd prefer to raise an error to avoid this kind of issues. |
Something like:
What do you think? |
Yes, raising an exception is much more preferable to returning an incorrect result. |
Related with #6865 |
Is this still happening? I might have fixed this by working on a different issue. Let me know if it's still a problem on |
I've updated https://github.com/agworld/distinctly_uncountable to work with rails master, and the tests still fail, so would be great if you can fix this @senny. You should also note that this bug affects everything in |
I'll take a look |
not every database supports counting on multiple distinct columns. The fallback is to use subqueries. The code that silently falls back to |
@mtalcott, would you mind to show an example of an AR object array that would work around this issue? Following is generating correct count for me: However, since I need this in a model scope it wont work since this returns an array instead of an AR relation. |
I submitted a PR to solve this issue: #10710 |
Calling
count
on an ActiveRecord::Relation with more than 1 distinct attribute in the select clause doesn't honor the select conditions. For example, this works:However, the distinct clause disappears from the count SQL when another distinct attribute is specified:
Same thing with
distinct users.*
:That means for a relation with multiple distinct columns,
relation.count
is not necessarily the same asrelation.all.count
.For
distinct users.*
,count(:distinct => true)
works (it generatesSELECT COUNT(DISTINCT "users"."id") FROM "users"
), but that won't work fordistinct username, email
.I've verified this behavior on Postgres and SQLite.
The text was updated successfully, but these errors were encountered: