You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "davidlghellin (via GitHub)" <gi...@apache.org> on 2026/01/10 22:23:14 UTC

[PR] fix: null in array_agg with DISTINCT and IGNORE [datafusion]

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

   ## Which issue does this PR close?
   
   - Closes https://github.com/apache/datafusion/issues/19735.
   
   ## Rationale for this change
   
   The `SingleDistinctToGroupBy` optimizer rewrites aggregate functions with `DISTINCT` into a `GROUP BY` operation for better performance. However, during this rewrite, it was discarding important aggregate function parameters: `null_treatment`, `filter`, and `order_by`.
   
   This caused queries like `ARRAY_AGG(DISTINCT x IGNORE NULLS)` to include NULL values in the result because the `IGNORE NULLS` clause (stored as null_treatment) was being lost during optimization.
   
   ## What changes are included in this PR?
   
   Preserve aggregate parameters in optimizer: Modified `SingleDistinctToGroupBy` to extract and preserve `null_treatment`, `filter`, and `order_by` from the original aggregate function when creating the rewritten version.
   
   Add regression test: Added SQL logic test to verify that `ARRAY_AGG(DISTINCT x IGNORE NULLS)` correctly filters out NULL values.
   
   Files changed:
   
   **datafusion/optimizer/src/single_distinct_to_groupby.rs**: Extract and pass through filter, order_by, and null_treatment parameters
   **datafusion/sqllogictest/test_files/aggregate.slt**: Add test case for ARRAY_AGG(DISTINCT ... IGNORE NULLS)
   
   ## Are these changes tested?
   
   Yes:
   New SQL logic test in aggregate.slt verifies the fix works end-to-end
   Existing optimizer tests continue to pass (19 tests in `single_distinct_to_groupby`)
   Existing aggregate tests continue to pass (20 tests in `array_agg`)
   
   ## Are there any user-facing changes?
   
   Bug fix - Users can now correctly use `IGNORE NULLS` (and `FILTER` / `ORDER BY`) with `DISTINCT` aggregates:
   
   Before (broken):
   
   ```sql
   SELECT ARRAY_AGG(DISTINCT x IGNORE NULLS) 
   FROM (VALUES (1), (2), (NULL), (2), (1)) AS t(x);
   -- Result: [2, NULL, 1]  ❌ NULL incorrectly included
   ```
   
   After (fixed):
   ```sql
   SELECT ARRAY_AGG(DISTINCT x IGNORE NULLS) 
   FROM (VALUES (1), (2), (NULL), (2), (1)) AS t(x);
   -- Result: [1, 2]  ✅ NULLs correctly filtered
   ```


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: null in array_agg with DISTINCT and IGNORE [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on PR #19736:
URL: https://github.com/apache/datafusion/pull/19736#issuecomment-3775987047

   Thanks @davidlghellin 


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: null in array_agg with DISTINCT and IGNORE [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey merged PR #19736:
URL: https://github.com/apache/datafusion/pull/19736


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: null in array_agg with DISTINCT and IGNORE [datafusion]

Posted by "davidlghellin (via GitHub)" <gi...@apache.org>.
davidlghellin commented on PR #19736:
URL: https://github.com/apache/datafusion/pull/19736#issuecomment-3734557674

   > Is it possible to find a test case for the 2-phase rewrite cases? 🤔
   
   If I understood correctly, I’ve added a couple of tests covering those cases.


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: null in array_agg with DISTINCT and IGNORE [datafusion]

Posted by "davidlghellin (via GitHub)" <gi...@apache.org>.
davidlghellin commented on PR #19736:
URL: https://github.com/apache/datafusion/pull/19736#issuecomment-3733646808

   take


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: null in array_agg with DISTINCT and IGNORE [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on PR #19736:
URL: https://github.com/apache/datafusion/pull/19736#issuecomment-3736572470

   Looking into it some more, it looks like the two-phase aggregation related changes aren't really needed as they don't affect anything 🤔
   
   The introduced tests don't fail on main, and it seems its because the only supported aggregates for the two-phase aggregation branch are min/max/sum:
   
   https://github.com/apache/datafusion/blob/f9697c14e29babc961c074eaec008e747495a636/datafusion/optimizer/src/single_distinct_to_groupby.rs#L85-L94
   
   - See how we bail if we find a non-distinct function that isn't sum/min/max
   
   I guess it doesn't hurt to keep the fix but they won't actually affect anything (since `ignore nulls` doesn't affect sum/min/max), and we bail out of the rule if we have a filter or order_by in any aggregate:
   
   https://github.com/apache/datafusion/blob/f9697c14e29babc961c074eaec008e747495a636/datafusion/optimizer/src/single_distinct_to_groupby.rs#L81-L83


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: null in array_agg with DISTINCT and IGNORE [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #19736:
URL: https://github.com/apache/datafusion/pull/19736#discussion_r2679439613


##########
datafusion/optimizer/src/single_distinct_to_groupby.rs:
##########


Review Comment:
   There's another case here



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix: null in array_agg with DISTINCT and IGNORE [datafusion]

Posted by "davidlghellin (via GitHub)" <gi...@apache.org>.
davidlghellin commented on code in PR #19736:
URL: https://github.com/apache/datafusion/pull/19736#discussion_r2679516244


##########
datafusion/optimizer/src/single_distinct_to_groupby.rs:
##########


Review Comment:
   I’ll look into whether we can add a minimal test case covering the 2-phase rewrite scenario.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org