-
Notifications
You must be signed in to change notification settings - Fork 5.5k
feat(analyzer): Add support for SELECT alias references in HAVING clause #27199
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -308,7 +308,32 @@ public void testReferenceToOutputColumnFromOrderByAggregation() | |
| @Test | ||
| public void testHavingReferencesOutputAlias() | ||
| { | ||
| assertFails(MISSING_ATTRIBUTE, "SELECT sum(a) x FROM t1 HAVING x > 5"); | ||
| // HAVING now support referencing SELECT aliases for improved SQL compatibility | ||
| analyze("SELECT sum(a) x FROM t1 HAVING x > 5"); | ||
| analyze("SELECT sum(a) AS total FROM t1 GROUP BY b HAVING total > 10"); | ||
| analyze("SELECT count(*) AS cnt, sum(a) AS total FROM t1 GROUP BY b HAVING cnt > 5 AND total > 100"); | ||
| analyze("SELECT sum(a) as sum_a FROM t1 GROUP BY b HAVING sum_a > 1"); | ||
|
Comment on lines
+313
to
+315
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Add a negative test for HAVING referencing a non-existent or non-select alias With the new positive cases, we should also keep a negative one where HAVING references a non-existent, non-resolvable name (not a SELECT alias), e.g.: assertFails(MISSING_ATTRIBUTE, "SELECT sum(a) AS total FROM t1 HAVING unknown_alias > 5");This ensures unresolved references in HAVING still produce the expected semantic error.
Comment on lines
+314
to
+315
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Add tests to ensure HAVING cannot indirectly introduce window functions via aliases Since HAVING now rewrites expressions using SELECT aliases, please add a test that window functions are still rejected when referenced via an alias, e.g.: assertFails(INVALID_WINDOW_FUNCTION,
"SELECT row_number() OVER () AS rn FROM t1 GROUP BY b HAVING rn > 1");(or using the same error code used elsewhere in |
||
| } | ||
|
|
||
| @Test | ||
| public void testHavingAmbiguousAlias() | ||
| { | ||
| // Ambiguous alias referenced in HAVING should throw appropriate error | ||
| assertFails(AMBIGUOUS_ATTRIBUTE, "SELECT sum(a) AS x, count(b) AS x FROM t1 GROUP BY c HAVING x > 5"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testHavingNonExistentAlias() | ||
| { | ||
| // Non-existent alias in HAVING should fail with MISSING_ATTRIBUTE | ||
| assertFails(MISSING_ATTRIBUTE, "SELECT sum(a) AS total FROM t1 GROUP BY b HAVING unknown_alias > 5"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testHavingWindowFunctionViaAlias() | ||
| { | ||
| // Window functions are not allowed in HAVING, even when referenced via alias | ||
| assertFails(NESTED_WINDOW, "SELECT row_number() OVER () AS rn FROM t1 GROUP BY b HAVING rn > 1"); | ||
| } | ||
|
|
||
| @Test | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (testing): Add negative tests for ambiguous alias references in HAVING
Since HAVING now supports SELECT aliases, please also add negative tests for ambiguous aliases, similar to ORDER BY. For instance:
This ensures multiple definitions of the same alias in the SELECT list cause the expected ambiguity error when referenced from HAVING, and validates the new alias resolution logic and error message for this case.