vlsi commented on code in PR #5225:
URL: https://github.com/apache/calcite/pull/5225#discussion_r4097640726


##########
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() {

Review Comment:
   This is the only negative test, and it runs end to end, but the fix is in 
the validator. Please add the rejection to `SqlValidatorTest` with a `^...^` 
position and `ERR_NESTED_AGG` (see my comment there). If this JDBC test stays, 
please give it the `/** Test case for <a 
href="https://issues.apache.org/jira/browse/CALCITE-7744";>[CALCITE-7744]</a> 
... */` comment that the neighboring regression tests carry, and a name that 
states the outcome as well as the scenario.



##########
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:
   `expandOrderExpr` is not only a validator method. `SqlToRelConverter` calls 
it twice per ORDER BY item, from `convertOrderItem` and from the extra 
projections in `convertSelectList`, and `SqlAbstractGroupFunction.validateCall` 
calls it as well. With this line, every ORDER BY expression is validated again 
during SQL-to-rel conversion. The Javadoc of `SqlValidator#expandOrderExpr` 
describes an expansion only and does not mention that the method can throw a 
validation error. Could the check move to the validation phase, for example 
`validateOrderItem` or `OrderByScope.validateExpr`?
   
   Wherever the check ends up, it needs a comment on two choices a reader 
cannot infer from the code: why it validates in `getSelectScope(select)` rather 
than the ORDER BY scope, and why it skips the measure branch. I checked the 
second one: with `validate` before the `isMeasure()` check, `select deptno + 1 
as d1, d1 + 2 as measure d3 from emp order by d3` fails with `Column 'D1' not 
found in any table`.



##########
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:
   This line guards the measure exclusion in `expandOrderExpr`, but nothing 
marks it as a guard, and inside `testAsMeasure` it looks like one more measure 
example. Please add a comment that says what it guards, or move it to a test 
whose name says so.
   
   Please also add validator-level tests for the rejection itself, with 
positions:
   
   - the query from the issue: `select max(sal) as sal, deptno, job from emp 
group by deptno, job order by ^max(sal)^`
   - `order by max(sal) desc`
   - `order by sal + max(sal)`
   - `order by count(*) filter (where sal > 1000)`, which now fails with 
`FILTER must not contain aggregate expression`
   
   On `main`, the last two also fail in Enumerable code generation, so this 
change affects them too. Please also add a case that must still pass: the same 
aggregate in ORDER BY without the shadowing alias, for example `select max(sal) 
as m, deptno, job from emp group by deptno, job order by max(sal)`.



##########
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:
   Following up on the position question: at the validator level the error is 
reported at `line 1 col 12 thru line 2 col 38`, which is `max(^sal) as sal, ... 
order by max(sal)^`. The span starts inside the SELECT-list aggregate and runs 
across FROM and GROUP BY. The ORDER BY item as written, `max(sal)`, contains no 
nested aggregate, so `Aggregate expressions cannot be nested` does not tell the 
user what to fix. Please point the error at the ORDER BY item and say that 
`SAL` resolved to the select-list alias of an aggregate. A new resource message 
would do it.



-- 
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]

Reply via email to