Skip to content

Let HAVING use an alias for a computed column to close #2769 - #3003

Open
mateuscmtropical wants to merge 2 commits into
AlaSQL:developfrom
mateuscmtropical:fix-2769-having-alias-expression
Open

mateuscmtropical wants to merge 2 commits into
AlaSQL:developfrom
mateuscmtropical:fix-2769-having-alias-expression

Conversation

@mateuscmtropical

@mateuscmtropical mateuscmtropical commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2769

What was wrong

resolveHavingAliases pointed 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 like g * 10 AS z is only built later on the output row, so g['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 x has 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 > 50 still finds no rows. In 4.x GROUP BY c0 groups by the alias (#2710), so the source column c0 is not in the group row. That case needs the 5.0 change of #2710.

Tests

test/test2769.js covers 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 z and GROUP 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 on develop.

yarn test-only gives 2558 pass. The only failures are the 5 Test 418 cases that load files from the internet.

@mathiasrw

Copy link
Copy Markdown
Member

Thank you for this @mateuscmtropical

HAVING on an alias of a computed column now works

SELECT g * 10 AS z FROM t GROUP BY g HAVING z > 15. 

But

When the query also groups by that alias or by the same expression, HAVING now returns nothing, instead of the current behavour:

SELECT g*10 AS z FROM t GROUP BY z HAVING z > 15        -- currently [{z:20}], PR []
SELECT g*10 AS z FROM t GROUP BY g*10 HAVING z > 15     -- currently [{z:20}], PR []
SELECT ABS(g) AS z FROM t GROUP BY z HAVING z > 1       -- currently [{z:2}], PR []

Also, the SELECT column node is now shared with HAVING, so a function behind the alias runs twice. With RANDOM() AS x ... HAVING x < 0.5, rows with x of 0.5 or more come back. Could you read the alias from the already computed group value instead, and add these as tests?

@mateuscmtropical

Copy link
Copy Markdown
Contributor Author

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.

@mateuscmtropical

Copy link
Copy Markdown
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HAVING with an alias for an expression finds no rows: SELECT g * 10 AS z ... HAVING z > 15

2 participants