rohityadav1993 opened a new pull request, #20314:
URL: https://github.com/apache/pinot/pull/20314

   ### Problem
   
   On the multi-stage engine, `CROSS JOIN UNNEST` over a column produced by an 
aggregate fails at plan time:
   
   ```sql
   SELECT f.col1, u.v
   FROM (SELECT col1, ARRAY_AGG(col3, 'INT') AS r FROM a GROUP BY col1) f
   CROSS JOIN UNNEST(f.r) AS u(v)
   -- Error composing query plan ...: Unsupported RexNode type with SqlKind: 
FIELD_ACCESS
   ```
   
   ### Root cause
   
   After optimization, the correlate's left input is the aggregate itself, and 
its output fields carry generated names (`agg#0`, `$f1`, ...) instead of the 
alias `r`:
   
   ```text
   LogicalCorrelate(correlation=[$cor0], requiredColumns=[{1}])
     PinotLogicalAggregate(group=[{0}], agg#0=[ARRAY_AGG(...)])
     Uncollect
       LogicalProject(r=[$cor0.r])
   ```
   
   `RelToPlanNodeConverter.resolveInputRefFromCorrel` looked up `$cor0.r` by 
field name in the left input's row type. The lookup missed, and the fallback 
`RexExpressionUtils.fromRexNode(RexFieldAccess)` threw. Unnesting a column of a 
plain scan works only because there the left input keeps the alias.
   
   ### Fix
   
   Resolve the correlated reference by its field index, which is how Calcite 
defines it: the index into the correlation variable's row type, i.e. the left 
input's row type. `PlanNodeToRelConverter` already builds the reference this 
way. The index is used only when the correlation variable's row type still 
lines up with the left input (same field count, same type at that index, 
ignoring nullability); otherwise the existing name match is used as before.
   
   ### Compatibility
   
   This is a planner-only change. There are no config, wire format, or plan 
serialization changes. Queries that planned before resolve to the same column.
   
   ### Validation
   
   Run on JDK 25:
   
   - New `UnnestSqlPlannerTest.testUnnestOverAggregateOutput` cases (plain, 
`WITH ORDINALITY` with a filter on the element, and an array alias that is also 
a source column name) fail with `FIELD_ACCESS` without the fix and pass with 
it; the array resolves to `InputRef(1)`.
   - New `queries/UnnestOverAggregate.json` cases in `ResourceBasedQueriesTest` 
fail with the same error without the fix (2 errors) and return the expected 
rows with it.
   - `UnnestSqlPlannerTest`, `RelToPlanNodeConverterTest`, 
`QueryCompilationTest`, `PlanNodeToRelConverterTest`, `UnnestOperatorTest` and 
the full `ResourceBasedQueriesTest` pass.
   - About 40 additional planner shapes were checked for the resolved column: 
multiple arrays in either order, arrays at different positions, expressions 
over the array, joins with duplicate column names, window and `UNION ALL` 
inputs, column pruning, chained unnests. No changes for shapes that planned 
before.
   - `./mvnw -pl pinot-query-planner,pinot-query-runtime spotless:check 
checkstyle:check license:check` passes.
   
   


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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to