notfilippo opened a new issue, #24143:
URL: https://github.com/apache/datafusion/issues/24143

   ### Describe the bug
   
   `common_sub_expression_eliminate` drops the internal `__grouping_id` column 
when deduplicating aggregate expressions in an aggregate using `GROUPING SETS`.
   
   A parent node referencing `__grouping_id` then fails schema resolution.
   
   ### To Reproduce
   
   Add this test to `datafusion/optimizer/src/common_subexpr_eliminate.rs`:
   
   ```rust
   #[test]
   fn common_aggregate_grouping_set_loses_internal_id() -> Result<()> {
       let plan = LogicalPlanBuilder::from(test_table_scan()?)
           .aggregate(
               vec![grouping_set(vec![vec![col("a")]])],
               vec![
                   avg(col("b")).alias("first"),
                   avg(col("b")).alias("second"),
               ],
           )?
           .filter(col(Aggregate::INTERNAL_GROUPING_ID).eq(lit(0_u8)))?
           .build()?;
   
       CommonSubexprEliminate::new().rewrite(plan, &OptimizerContext::new())?;
       Ok(())
   }
   ```
   
   Run:
   
   ```shell
   cargo test -p datafusion-optimizer 
common_aggregate_grouping_set_loses_internal_id
   ```
   
   It fails with:
   
   ```text
   Schema error: No field named __grouping_id.
   Valid fields are test.a, first, second.
   ```
   
   ### Expected behavior
   
   The recovery projection must preserve the aggregate's complete output 
schema, including `__grouping_id`.
   
   ### Additional context
   
   _No response_


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