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

   ## Problem
   
   
`PinotAggregateExchangeNodeInsertRule.createPlanWithExchangeDirectAggregation` 
(the `is_skip_leaf_stage_group_by` / direct-aggregation path) casts the result 
of `generateProjectUnderAggregate` directly to `Aggregate`:
   
   ```java
   aggRel = (Aggregate) generateProjectUnderAggregate(call, aggRel);
   ```
   
   But `generateProjectUnderAggregate` rebuilds the aggregate with 
`RelBuilder`, which **deduplicates identical aggregate calls** and, when it 
does, adds a top `Project` to restore the original output layout (a column 
referenced more than once is projected twice). In that case the helper returns 
a `Project`-over-`Aggregate`, and the cast throws:
   
   ```
   java.lang.ClassCastException: class 
org.apache.calcite.rel.logical.LogicalProject
     cannot be cast to class org.apache.calcite.rel.core.Aggregate
   ```
   
   This is reachable whenever an aggregate carrying 
`is_skip_leaf_stage_group_by` ends up with duplicate aggregate calls — e.g. 
after an `AVG` reduction reuses an existing `SUM`, leaving two identical `SUM` 
calls that `RelBuilder` then deduplicates.
   
   ## Fix
   
   Detect the layout-restoring top `Project`, extract the `Aggregate` beneath 
it for the direct-aggregation rewrite, and **re-apply the `Project`** (same 
projects and row type) over the resulting direct aggregate so the output 
columns are preserved. `createPlan` / `createPlanWithExchangeDirectAggregation` 
now return `RelNode`.
   
   ## Testing
   
   New `PinotAggregateExchangeNodeInsertRuleTest`:
   - `skipLeafWithDuplicateAggregateCallsPreservesLayoutProject` — the 
duplicate-agg-call case that previously threw; asserts the plan is a 
layout-restoring `Project` over a `PinotLogicalAggregate`.
   - `skipLeafWithoutDuplicatesProducesDirectAggregate` — the common single-agg 
case still produces a bare direct aggregate.
   
   Existing `QueryCompilationTest` (213 tests) passes unchanged.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)


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