1fanwang commented on code in PR #5225:
URL: https://github.com/apache/calcite/pull/5225#discussion_r4139696624
##########
core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java:
##########
@@ -5484,6 +5484,7 @@ private void validateOrderItem(SqlSelect select, SqlNode
orderItem) {
final RelDataType type = deriveType(scope, orderExpr2);
setValidatedNodeType(orderExpr2, type);
if (!type.isMeasure()) {
+ orderExpr2.validate(this, getSelectScope(select));
Review Comment:
Moved the check to `validateOrderItem` so `expandOrderExpr` remains an AST
expansion without re-validating during plan conversion. Added inline comments
explaining the `getSelectScope(select)` validation context and why measure
expressions are excluded.
##########
core/src/test/java/org/apache/calcite/test/JdbcTest.java:
##########
@@ -3391,6 +3391,14 @@ void testJoinWithTrimmingConfigs(boolean
enableTrimmingByConfig,
+ "store_id=0; grocery_sqft=null\n");
}
+ @Test void testOrderByAggregateAliasShadowing() {
+ CalciteAssert.that()
+ .with(CalciteAssert.Config.SCOTT)
+ .query("SELECT max(sal) AS sal, deptno, job "
+ + "FROM emp GROUP BY deptno, job ORDER BY max(sal)")
+ .throws_("Aggregate expressions cannot be nested");
Review Comment:
Updated `JdbcTest` with the Jira javadoc link and renamed to
`testOrderByAggregateAliasShadowingFailsWithNestedAggError`. When expanding
aliases in `OrderExpressionExpander`, we now reposition the cloned subtree to
the order item position so the nested aggregate error points directly to
`^max(sal)^`.
##########
core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java:
##########
@@ -5067,6 +5067,8 @@ private void checkNegWindow(String s, String msg) {
// various kinds of measure expressions
sql("select deptno, empno + 1 as measure e1 from emp").ok();
sql("select *, empno + 1 as measure e1 from emp").ok();
+ sql("select deptno + 1 as d1, d1 + 2 as measure d3\n"
Review Comment:
Added the comment clarifying the measure guard in `testAsMeasure`, and added
`testOrderByAggregateAliasShadowing` with the requested negative and positive
position tests (`order by ^max(sal)^`, `desc`, `sal + max(sal)`, `filter`, and
the non-shadowed positive query).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]