Repository navigation
Let HAVING use an alias for a computed column to close #2769 - #3003
mateuscmtropical wants to merge 2 commits into
Conversation
|
Thank you for this @mateuscmtropical HAVING on an alias of a computed column now works But When the query also groups by that alias or by the same expression, HAVING now returns nothing, instead of the current behavour: Also, the SELECT column node is now shared with HAVING, so a function behind the alias runs twice. With |
|
Done. HAVING now reads a computed alias from the row that the SELECT list already built for the group, so the expression runs once. When HAVING does not use such an alias, the row is still built only for the groups that pass. Your three queries are test E in test2769.js and the RANDOM() case is test F. Both failed on the previous commit. Test G covers a compiled statement that runs again after a schema change. |
|
Hi @mathiasrw, friendly ping on my open PRs from Nov 11. All of them are up to date with develop and have no conflicts. Happy to change anything, or to close the ones you would rather not take. |
Closes #2769
What was wrong
resolveHavingAliasespointed an alias at the nick of its SELECT column. That works for a bare column or aggregate, because the group row holds a value under that nick. A computed column likeg * 10 AS zis only built later on the output row, sog['g * 10']was undefined and HAVING never matched.What changed
When the alias names a SELECT expression that is not a bare column or aggregate, HAVING reads the value from the row that the SELECT list computed for the group. The expression runs once, so
RANDOM() AS xhas the same value in HAVING and in the result. The row is built before HAVING only when HAVING uses such an alias. A source column of the same name still wins over the alias, as before.Not covered
SELECT c0 + 100 AS c0 FROM t GROUP BY c0 HAVING c0 > 50still finds no rows. In 4.xGROUP BY c0groups by the alias (#2710), so the source columnc0is not in the group row. That case needs the 5.0 change of #2710.Tests
test/test2769.jscovers the queries from the issue table: arithmetic, a function and an expression with an aggregate behind an alias, the alias used as the GROUP BY key (GROUP BY zandGROUP BY g*10),RANDOM(), a compiled statement that runs again after a schema change, and the bare cases that already worked. The first three fail ondevelop.yarn test-onlygives 2558 pass. The only failures are the 5Test 418cases that load files from the internet.