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

   ## Which issue does this PR close?
   
   - Closes #24038.
   - Part of #8227.
   
   ## Rationale for this change
   
   `AggregateExec` reported a row count of `Exact(0)` whenever its input had no 
rows, even when the
   group-by contained an empty grouping set. `GROUPING SETS(())`, `ROLLUP` and 
`CUBE` each emit a
   grand-total row for such an input, so the row count contradicted execution. 
The count was claimed
   as `Exact`, so rules that answer a query from statistics rather than by 
executing it returned a
   value that no row of the result holds:
   
   ```sql
   SELECT COUNT(*) FROM (SELECT SUM(v1) FROM t WHERE false GROUP BY ROLLUP(v1));
   -- 0, while the inner aggregate emits one row
   ```
   
   This is a correctness fix, not an estimation improvement.
   
   ## What changes are included in this PR?
   
   - `estimate_num_rows` counts the empty grouping sets instead of reusing the 
child's row count of
     zero. Partial aggregation emits the grand-total row from every output 
partition, so the count
     goes through the per-partition scaling already applied to aggregates 
without grouping
     expressions, now factored into `scale_logical_rows`.
   - The grouping columns of such an output are reported as NULL: typed null 
bounds, a distinct
     count of zero, and a null count equal to the row count.
   
   ## Are these changes tested?
   
   Yes.
   
   - SQL logic tests in `grouping.slt` for both folds: an outer `COUNT(*)` over 
`ROLLUP`, `CUBE` and
     `GROUPING SETS`, and an outer `MIN`/`MAX` over the grand-total row.
   - Unit tests for the reported statistics: zero rows for a plain `GROUP BY`, 
one row for
     `GROUPING SETS((a), ())`, two rows for `GROUPING SETS((a), (), ())`, the 
partition scaling of a
     partial aggregate, and the NULL grouping-column statistics.
   
   Each test was checked to fail without the corresponding change.
   
   ## Are there any user-facing changes?
   
   Yes. Queries such as the one above now return the correct result. There are 
no API changes.
   
   ----
   
   Disclaimer: I used AI to assist in the code generation, I have manually 
reviewed the output and it matches my intention and understanding.
   


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