You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "joroKr21 (via GitHub)" <gi...@apache.org> on 2025/04/02 15:53:10 UTC

[PR] Respect ignore_nulls in array_agg [datafusion]

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

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   - Closes #13742
   - Part of #9924
   
   ## Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   See the linked issues.
   
   ## What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   Take into account the `ignore_nulls` flag from `AccumulatorArgs`.
   
   ## Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   Yes, added SLT tests.
   
   ## Are there any user-facing changes?
   
   `ArrayAggAccumulator` constructors take an `ignore_nulls` flag.
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   


-- 
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] Respect ignore_nulls in array_agg [datafusion]

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

   This missed the v47 train. Anything else needed to merge?


-- 
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] Respect ignore_nulls in array_agg [datafusion]

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


##########
datafusion/functions-aggregate/src/array_agg.rs:
##########
@@ -288,10 +300,23 @@ impl Accumulator for ArrayAggAccumulator {
             return internal_err!("expects single batch");
         }
 
-        let val = Arc::clone(&values[0]);
+        let val = &values[0];
+        let nulls = if self.ignore_nulls {
+            val.logical_nulls()
+        } else {
+            None
+        };
+
+        let val = match nulls {
+            Some(nulls) if nulls.null_count() >= val.len() => return Ok(()),

Review Comment:
   how could the null count be greater than the length? or is the `>=` a defensive coding mechanism?



-- 
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] Respect ignore_nulls in array_agg [datafusion]

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


##########
datafusion/sqllogictest/test_files/aggregate.slt:
##########
@@ -3194,6 +3196,28 @@ select array_agg(column1) from t;
 statement ok
 drop table t;
 
+# array_agg_ignore_nulls
+statement ok
+create table t as values (NULL, ''), (1, 'c'), (2, 'a'), (NULL, 'b'), (4, NULL), (NULL, NULL), (5, 'a');
+
+query ?
+select array_agg(column1) ignore nulls as c1 from t;
+----
+[1, 2, 4, 5]
+
+query II
+select count(*), array_length(array_agg(distinct column2) ignore nulls) from t;
+----
+7 4
+
+query ?
+select array_agg(column2 order by column1) ignore nulls from t;
+----
+[c, a, a, , b]
+
+statement ok
+drop table t;
+

Review Comment:
   For the sake of completeness, how about adding this other test that mixes DISTINCT with ORDER BY:
   
   ```sql
   query ?
   select array_agg(DISTINCT column2 order by column2) ignore nulls from t;
   ----
   [, a, b, c]
   ```



-- 
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] Respect ignore_nulls in array_agg [datafusion]

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


##########
datafusion/functions-aggregate/src/array_agg.rs:
##########
@@ -500,11 +548,30 @@ impl Accumulator for OrderSensitiveArrayAggAccumulator {
             return Ok(());
         }
 
-        let n_row = values[0].len();
-        for index in 0..n_row {
-            let row = get_row_at_idx(values, index)?;
-            self.values.push(row[0].clone());
-            self.ordering_values.push(row[1..].to_vec());
+        let val = &values[0];
+        let ord = &values[1..];
+        let nulls = if self.ignore_nulls {
+            val.logical_nulls()
+        } else {
+            None
+        };
+
+        match nulls {
+            Some(nulls) if nulls.null_count() >= val.len() => (),
+            Some(nulls) => {
+                for i in 0..val.len() {
+                    if nulls.is_valid(i) {
+                        self.values.push(ScalarValue::try_from_array(val, i)?);
+                        self.ordering_values.push(get_row_at_idx(ord, i)?)
+                    }
+                }
+            }
+            None => {
+                for i in 0..val.len() {
+                    self.values.push(ScalarValue::try_from_array(val, i)?);
+                    self.ordering_values.push(get_row_at_idx(ord, i)?)
+                }
+            }
         }

Review Comment:
   Sure, we could do that. I'm not sure what is better - keep the tight loop separate or rely on branch prediction to get it right when there are no nulls.



-- 
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] Respect ignore_nulls in array_agg [datafusion]

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


##########
datafusion/functions-aggregate/src/array_agg.rs:
##########
@@ -288,10 +300,23 @@ impl Accumulator for ArrayAggAccumulator {
             return internal_err!("expects single batch");
         }
 
-        let val = Arc::clone(&values[0]);
+        let val = &values[0];
+        let nulls = if self.ignore_nulls {
+            val.logical_nulls()
+        } else {
+            None
+        };
+
+        let val = match nulls {
+            Some(nulls) if nulls.null_count() >= val.len() => return Ok(()),

Review Comment:
   It shouldn't be - I guess we can use `==` but does it make a difference?



-- 
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] Respect ignore_nulls in array_agg [datafusion]

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


##########
datafusion/sqllogictest/test_files/aggregate.slt:
##########
@@ -3194,6 +3196,28 @@ select array_agg(column1) from t;
 statement ok
 drop table t;
 
+# array_agg_ignore_nulls
+statement ok
+create table t as values (NULL, ''), (1, 'c'), (2, 'a'), (NULL, 'b'), (4, NULL), (NULL, NULL), (5, 'a');
+
+query ?
+select array_agg(column1) ignore nulls as c1 from t;
+----
+[1, 2, 4, 5]
+
+query II
+select count(*), array_length(array_agg(distinct column2) ignore nulls) from t;
+----
+7 4
+
+query ?
+select array_agg(column2 order by column1) ignore nulls from t;
+----
+[c, a, a, , b]
+
+statement ok
+drop table t;
+

Review Comment:
   For the shake of completeness, how about adding this other test that mixes DISTINCT with ORDER BY:
   
   ```sql
   query ?
   select array_agg(DISTINCT column2 order by column2) ignore nulls from t;
   ----
   [, a, b, c]
   ```



##########
datafusion/functions-aggregate/src/array_agg.rs:
##########
@@ -500,11 +548,30 @@ impl Accumulator for OrderSensitiveArrayAggAccumulator {
             return Ok(());
         }
 
-        let n_row = values[0].len();
-        for index in 0..n_row {
-            let row = get_row_at_idx(values, index)?;
-            self.values.push(row[0].clone());
-            self.ordering_values.push(row[1..].to_vec());
+        let val = &values[0];
+        let ord = &values[1..];
+        let nulls = if self.ignore_nulls {
+            val.logical_nulls()
+        } else {
+            None
+        };
+
+        match nulls {
+            Some(nulls) if nulls.null_count() >= val.len() => (),
+            Some(nulls) => {
+                for i in 0..val.len() {
+                    if nulls.is_valid(i) {
+                        self.values.push(ScalarValue::try_from_array(val, i)?);
+                        self.ordering_values.push(get_row_at_idx(ord, i)?)
+                    }
+                }
+            }
+            None => {
+                for i in 0..val.len() {
+                    self.values.push(ScalarValue::try_from_array(val, i)?);
+                    self.ordering_values.push(get_row_at_idx(ord, i)?)
+                }
+            }
         }

Review Comment:
   🤔 I wonder if there is a way of avoiding some of the repetition here, I can think of something like 
   ```rust
           for i in 0..val.len() {
               if nulls.as_ref().is_none_or(|n| n.is_valid(i)) {
                   self.values.push(ScalarValue::try_from_array(val, i)?);
                   self.ordering_values.push(get_row_at_idx(ord, i)?)
               }
           }
   ```
   
   but you'd lose the `ulls.null_count() >= val.len()` case optimization. 



-- 
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] Respect ignore_nulls in array_agg [datafusion]

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


-- 
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] Respect ignore_nulls in array_agg [datafusion]

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


##########
datafusion/functions-aggregate/src/array_agg.rs:
##########
@@ -500,11 +548,30 @@ impl Accumulator for OrderSensitiveArrayAggAccumulator {
             return Ok(());
         }
 
-        let n_row = values[0].len();
-        for index in 0..n_row {
-            let row = get_row_at_idx(values, index)?;
-            self.values.push(row[0].clone());
-            self.ordering_values.push(row[1..].to_vec());
+        let val = &values[0];
+        let ord = &values[1..];
+        let nulls = if self.ignore_nulls {
+            val.logical_nulls()
+        } else {
+            None
+        };
+
+        match nulls {
+            Some(nulls) if nulls.null_count() >= val.len() => (),
+            Some(nulls) => {
+                for i in 0..val.len() {
+                    if nulls.is_valid(i) {
+                        self.values.push(ScalarValue::try_from_array(val, i)?);
+                        self.ordering_values.push(get_row_at_idx(ord, i)?)
+                    }
+                }
+            }
+            None => {
+                for i in 0..val.len() {
+                    self.values.push(ScalarValue::try_from_array(val, i)?);
+                    self.ordering_values.push(get_row_at_idx(ord, i)?)
+                }
+            }
         }

Review Comment:
   Usually there is some overhead of generating specialized code, although I think in this case the overhead of `ScalarValue` etc. will be bigger to matter much.



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