Fix HAVING clause to accept SELECT aliases (#887) - #2558
Open
harsh-thakkar7 wants to merge 1 commit into
Open
harsh-thakkar7 wants to merge 1 commit into
harsh-thakkar7 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Restores support for referencing SELECT aliases inside the HAVING clause, which the wiki documents and which regressed.
Given:
the query used to return the groups with more than two rows; it currently returns
[]because the HAVING expressioncnt > 2compiles tog['cnt']while groups accumulate under the aggregate's key (COUNT(*)), so the comparison always evaluates againstundefined.Root cause
HAVINGis evaluated per group (insrc/38query.js) before the SELECT list is materialized, so output aliases are not directly visible there.compileHaving(src/425having.js) compiles the raw expression against the group row without resolving alias references.Fix
yy.Select.prototype.compileHavingnow rewrites unqualified column references in the HAVING expression to the corresponding group-accumulation key usingquery.groupColumns(the same alias → nick mapping used to build the SELECT output). The rewrite clones the expression nodes, so the shared AST is untouched and column-reference behaviour elsewhere is unchanged.Resolves #887
Changes
src/425having.js— resolve SELECT aliases referenced inHAVING(resolveHavingAliases).test/test1277.js— new tests:HAVINGwithCOUNT(*) AS cntaliasHAVINGalias in aSELECT *, ...query (the exact case from the issue)HAVINGwith aSUM() AS popsumaliasHAVING COUNT(*) > 2still worksVerification
bun test ./test/test1277.js— 4/4 passing.bun test ./test/test[0-9]*.js ./test/test-*.js— 2240 pass, 5 fail. The 5 failures (Test 147 / 220 / 334 / 404, all WITH/CTE-related) are pre-existing and order-dependent — identical on a cleandevelopcheckout and unrelated to this change.yarn test-formatpasses.