This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch 
gh-readonly-queue/main/pr-11650-df2f8f454e8a29446813bc72cf613f9185d261a1
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git

commit 88b5c5a3952bc715fa8d13d76e45c407717b12b6
Author: Kyon <[email protected]>
AuthorDate: Sun Oct 18 09:31:09 2026 +0000

    Respect dictionary value nulls in aggregates (#11650)
    
    # Which issue does this PR close?
    
    - Closes #11596
    
    # Rationale for this change
    
    Dictionary values can be null even when their keys are valid. Min/max
    currently include the underlying payload of those null values, and both
    sum APIs return a zero result when all referenced values are null
    instead of returning `None`.
    
    # What changes are included in this PR?
    
    Use the logical null mask for min/max and return a dictionary sum only
    after encountering a non-null row. Preserve the existing zero-seeded
    wrapping and checked accumulation.
    
    # Are these changes tested?
    
    A regression test compares all four aggregate APIs with equivalent
    primitive-array rows, covering mixed key/value nulls, entirely null
    results, unused null dictionary values, and empty input.
    
    AI assistance was used to analyze the aggregate paths and draft the
    code, regression test, and this description.
    
    # Are there any significant user-facing changes?
    
    No public API changes.
---
 arrow-arith/src/aggregate.rs | 45 ++++++++++++++++++++++++++++++++++++++------
 1 file changed, 39 insertions(+), 6 deletions(-)

diff --git a/arrow-arith/src/aggregate.rs b/arrow-arith/src/aggregate.rs
index 99615f7601..16f1401a21 100644
--- a/arrow-arith/src/aggregate.rs
+++ b/arrow-arith/src/aggregate.rs
@@ -457,7 +457,8 @@ fn min_max_helper<T, A: ArrayAccessor<Item = T>, F>(array: 
A, cmp: F) -> Option<
 where
     F: Fn(&T, &T) -> bool,
 {
-    let null_count = array.null_count();
+    let nulls = array.logical_nulls();
+    let null_count = nulls.as_ref().map_or(0, NullBuffer::null_count);
     if null_count == array.len() {
         None
     } else if null_count == 0 {
@@ -468,7 +469,7 @@ where
             .map(|i| unsafe { array.value_unchecked(i) })
             .reduce(|acc, item| if cmp(&acc, &item) { item } else { acc })
     } else {
-        let nulls = array.nulls().unwrap();
+        let nulls = nulls.unwrap();
         unsafe {
             let idx = nulls.valid_indices().reduce(|acc_idx, idx| {
                 let acc = array.value_unchecked(acc_idx);
@@ -572,9 +573,9 @@ pub fn sum_array<T: ArrowNumericType, A: ArrayAccessor<Item 
= T::Native>>(
 ) -> Option<T::Native> {
     match array.data_type() {
         DataType::Dictionary(_, _) => {
-            let null_count = array.null_count();
+            let logical_null_count = array.logical_null_count();
 
-            if null_count == array.len() {
+            if logical_null_count == array.len() {
                 return None;
             }
 
@@ -612,9 +613,9 @@ pub fn sum_array_checked<T: ArrowNumericType, A: 
ArrayAccessor<Item = T::Native>
 ) -> Result<Option<T::Native>, ArrowError> {
     match array.data_type() {
         DataType::Dictionary(_, _) => {
-            let null_count = array.null_count();
+            let logical_null_count = array.logical_null_count();
 
-            if null_count == array.len() {
+            if logical_null_count == array.len() {
                 return Ok(None);
             }
 
@@ -1814,6 +1815,38 @@ mod tests {
         assert_eq!(Some(false), max_boolean(&with_nulls));
     }
 
+    #[test]
+    fn test_dictionary_aggregates_with_null_values() {
+        let values = Arc::new(Int32Array::new(
+            vec![5, -100, 100].into(),
+            Some(vec![true, false, false].into()),
+        ));
+        for (keys, expected) in [
+            (
+                vec![Some(0_i8), Some(1), Some(0)],
+                vec![Some(5), None, Some(5)],
+            ),
+            (vec![Some(1), Some(2)], vec![None, None]),
+            (vec![None, Some(1), Some(0)], vec![None, None, Some(5)]),
+            (vec![Some(0), Some(0)], vec![Some(5), Some(5)]),
+            (vec![None, None], vec![None, None]),
+            (vec![], vec![]),
+        ] {
+            let dictionary =
+                DictionaryArray::<Int8Type>::try_new(Int8Array::from(keys), 
values.clone())
+                    .unwrap();
+            let array = dictionary.downcast_dict::<Int32Array>().unwrap();
+            let expected = Int32Array::from(expected);
+            assert_eq!(min_array::<Int32Type, _>(array), min(&expected));
+            assert_eq!(max_array::<Int32Type, _>(array), max(&expected));
+            assert_eq!(sum_array::<Int32Type, _>(array), sum(&expected));
+            assert_eq!(
+                sum_array_checked::<Int32Type, _>(array).unwrap(),
+                sum_checked(&expected).unwrap()
+            );
+        }
+    }
+
     #[test]
     fn test_sum_dyn() {
         let values = Int8Array::from_iter_values([10_i8, 11, 12, 13, 14, 15, 
16, 17]);

Reply via email to