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
Allow for alias omission in aggregate expressions #300
Conversation
While I agree with this patch I find that passing cc @tenderlove |
IMO this is rather back-to-front... the alias shouldn't be part of the aggregate to begin with: it's properly part of the 'project' production, not the function call. And contrariwise, our motivation for applying the alias there (predictability of the output column name, I guess?) should presumably apply equally to any non-aggregate complex expression. |
@matthewd Agreed, the alias inclusion felt odd to begin with, particularly because Arel makes affordances for explicit aliases. But it seemed to have been there since nearly the beginning of time, so I was gun-shy on touching it. I could remove the alias and ensure the tests still pass, and maybe @tenderlove as the original author of that would be able to hit the brakes if that seems to violate the original motivation? |
OK, I had some more time this morning, and I've removed the default aggregate aliases altogether. There were some The only other thing I can think of is that there are occasions I'm unaware of where generating an alias is necessary on aggregate functions, but Googling around turns nothing up there. (It's worth noting that this change could very-well break existing code that relies on the aliases that are generated.) |
@rafaelfranca I know you're quite busy with 4.2 betas (thanks and kudos!), but I'm curious if there's any more I can do to help this one across the finish line? |
I'll defer to @matthewd |
I think it's fair to treat the name as undefined here, given that DBs have differing opinions on the default; if you need a particular value, you should specify an explicit alias. (Or just access the column by position, as we do in Rails.) |
Allow for alias omission in aggregate expressions
This reverts commit 9b92af7. beta2 is out, and we've fixed the issue that this caused in Rails
This fix should be backwards-compatible, but I'm not confident it's being applied at the appropriate level.
When using aggregate expressions in a
HAVING
claus, an alias was forced, causing a syntax error in Postgres. The following Arel:Produces:
Which leads to the following error:
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near "AS"
because Postgres (and maybe others?) disallows aliases inHAVING
clauses.This fix allows these aggregate functions to take an optional argument that can either override the
avg_id
default string or be nil to skip the alias altogether, while maintaining the default behavior.Now we could do:
To yield:
And avoid syntax errors.