[CALCITE-7744] ORDER BY agg(col) on a query where an alias shadows col produces an invalid plan containing an aggregate call in a Project - #5225
Conversation
…l produces an invalid plan containing an aggregate call in a Project Signed-off-by: 1fanwang <1fannnw@gmail.com>
|
SELECT max(sal) AS sal, deptno, job FROM emp GROUP BY deptno, job ORDER BY max(sal); postgresql can return result, instead of throw error |
|
Thanks for raising this @iwanttobepowerful PostgreSQL and Calcite resolve this query differently. PostgreSQL only recognizes an output alias when it appears by itself in Calcite allows SELECT aliases inside Right now this PR proposes to keep Calcite's current resolution rules. It replaces the later planner failure with a clear validation error. I think matching PostgreSQL would require a separate conformance change that's beyond the scope of this ticket/PR |



Jira Link
CALCITE-7744
Changes Proposed
SELECT max(sal) AS sal, deptno, job FROM emp GROUP BY deptno, job ORDER BY max(sal)passes validation but fails during execution withUnable to implement EnumerableCalc.The ORDER BY alias expansion turns the expression into
max(max(sal))after the original expression has already been validated. The inner aggregate then leaks into aProject. Validate expanded non-measure ORDER BY expressions before SQL-to-rel conversion. The query now returns Calcite's existingAggregate expressions cannot be nestedvalidation error instead of building an illegal plan. Measure aliases retain their existing scope and conversion path.Verification
JAVA_HOME=$(/usr/libexec/java_home -v 21) ./gradlew :core:test \ --tests org.apache.calcite.test.SqlValidatorTest \ --tests org.apache.calcite.test.JdbcTest --no-daemonThe JDBC regression uses
CalciteAssert.Config.SCOTTand the query above. The same test ran against production source fromupstream/mainand from this branch.Raw logs