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

   ### Is your feature request related to a problem or challenge?
   
   `COUNT(non_nullable_column)` counts every input row, but DataFusion still 
evaluates the column as an aggregate argument. This can prevent pruning the 
column and cause unnecessary I/O and expression evaluation.
   
   This optimization was identified while reviewing #25849 and should be 
considered separately from optimizing constant aggregate arguments.
   
   ### Describe the solution you'd like
   
   During logical expression simplification, rewrite a safe, non-`DISTINCT` 
count over arguments that are provably non-null based on the input schema:
   
   ```sql
   COUNT(non_nullable_column) -> COUNT(1)
   ```
   
   The rewrite should preserve the original output name and schema. Nullable 
columns, `DISTINCT` counts, and expressions that are not provably non-null 
should remain unchanged.
   
   For multiple-argument `COUNT`, the same rewrite could apply when every 
argument is provably non-null.
   
   ### Describe alternatives you've considered
   
   - Rewrite to nullary `COUNT()` as proposed in #25849. This requires nullary 
aggregate execution support and causes broader API, `EXPLAIN`, serialization, 
and unparser changes.
   - Cache arrays expanded from constant aggregate arguments. This is 
complementary: after rewriting to `COUNT(1)`, caching can avoid rebuilding the 
constant array and can also benefit aggregates such as `string_agg(value, 
constant_separator)` and `nth_value(value, constant_n)`.
   - Optimize only in the physical planner. A logical rewrite is preferable 
because it can also enable column pruning before physical planning.
   
   ### Additional context
   
   This issue follows the suggestion in 
https://github.com/apache/datafusion/pull/25849#issuecomment-5914339814 to 
split the nullability-based rewrite into a separate change.
   


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