mohitgurav20 opened a new pull request, #27657:
URL: https://github.com/apache/datafusion/pull/27657

   # Which issue does this PR close?
   
   Follow-up split from #26000 per review feedback from @jayzhan211.
   
   # Rationale for this change
   
   On `main`, single-distinct queries combining a distinct aggregate with 
non-distinct companion aggregates (such as `count(*)`) check 
`rewrite_pays_for_count` to decide whether to rewrite to two-phase `GROUP BY`.
   
   For aggregates declaring `DistinctHandling::Unsupported` (such as `stddev`), 
`groups_accumulator_supported_for_types` returns `None` rather than 
`Some(false)`. As a result, `rewrite_pays_for_count` concludes the rewrite does 
not pay and leaves the query untouched. At execution time, the engine fails 
with an error because the accumulator cannot handle DISTINCT natively:
   
   ```sql
   SELECT g, count(*), stddev(DISTINCT x) FROM t GROUP BY g;
   -- Fails on main: STDDEV_POP(DISTINCT) aggregations are not available
   
   SELECT count(*), stddev(DISTINCT x) FROM t;
   -- Fails with same error
   ```
   
   # What changes are included in this PR?
   
   - Adds an early check in `SingleDistinctToGroupBy::is_single_distinct_agg` 
to ensure aggregates with `DistinctHandling::Unsupported` are always rewritten 
into the two-phase `GROUP BY` plan, where deduplication happens in the inner 
aggregation and the outer function runs non-distinct.
   - Adds an optimizer unit test covering the rewrite.
   - Adds regression tests for grouped and un-grouped `stddev(DISTINCT)` with 
companion `count(*)` in `single_distinct_to_groupby.slt`.
   
   # Are these changes tested?
   
   Yes:
   - Unit test in `datafusion/optimizer/src/single_distinct_to_groupby.rs`.
   - End-to-end queries in `single_distinct_to_groupby.slt`.


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