You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by al...@apache.org on 2025/06/20 13:14:32 UTC

(arrow-rs) branch main updated: feat: add min max aggregate support for FixedSizeBinary (#7675)

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

alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/main by this push:
     new 75008eb580 feat: add min max aggregate support for FixedSizeBinary (#7675)
75008eb580 is described below

commit 75008eb580c0fb52ae21de0ce4db602bb771336f
Author: Alex Wilcoxson <al...@relativity.com>
AuthorDate: Fri Jun 20 08:14:27 2025 -0500

    feat: add min max aggregate support for FixedSizeBinary (#7675)
    
    # Which issue does this PR close?
    
    Closes #7674.
    
    # Rationale for this change
    
    Adding support for these min/max functions [so DataFusion can utilize
    them](https://github.com/apache/datafusion/blob/dd936cb1b25cb685e0e146f297950eb00048c64c/datafusion/functions-aggregate/src/min_max.rs#L600)
    
    # What changes are included in this PR?
    
    Added new min and max functions for fixed size binary and updated
    existing tests.
    
    # Are there any user-facing changes?
    
    Yes new functions `min_fixed_size_binary` and `max_fixed_size_binary`
    added.
---
 arrow-arith/src/aggregate.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/arrow-arith/src/aggregate.rs b/arrow-arith/src/aggregate.rs
index ef0fddeb0b..9a19b5d8a1 100644
--- a/arrow-arith/src/aggregate.rs
+++ b/arrow-arith/src/aggregate.rs
@@ -513,6 +513,11 @@ pub fn max_binary_view(array: &BinaryViewArray) -> Option<&[u8]> {
     min_max_view_helper(array, Ordering::Greater)
 }
 
+/// Returns the maximum value in the fixed size binary array, according to the natural order.
+pub fn max_fixed_size_binary(array: &FixedSizeBinaryArray) -> Option<&[u8]> {
+    min_max_helper::<&[u8], _, _>(array, |a, b| *a < *b)
+}
+
 /// Returns the minimum value in the binary array, according to the natural order.
 pub fn min_binary<T: OffsetSizeTrait>(array: &GenericBinaryArray<T>) -> Option<&[u8]> {
     min_max_helper::<&[u8], _, _>(array, |a, b| *a > *b)
@@ -523,6 +528,11 @@ pub fn min_binary_view(array: &BinaryViewArray) -> Option<&[u8]> {
     min_max_view_helper(array, Ordering::Less)
 }
 
+/// Returns the minimum value in the fixed size binary array, according to the natural order.
+pub fn min_fixed_size_binary(array: &FixedSizeBinaryArray) -> Option<&[u8]> {
+    min_max_helper::<&[u8], _, _>(array, |a, b| *a > *b)
+}
+
 /// Returns the maximum value in the string array, according to the natural order.
 pub fn max_string<T: OffsetSizeTrait>(array: &GenericStringArray<T>) -> Option<&str> {
     min_max_helper::<&str, _, _>(array, |a, b| *a < *b)
@@ -1240,6 +1250,41 @@ mod tests {
         assert!(max(&a).unwrap().is_nan());
     }
 
+    fn pad_inputs_and_test_fixed_size_binary(
+        input: Vec<Option<&[u8]>>,
+        expected_min: Option<&[u8]>,
+        expected_max: Option<&[u8]>,
+    ) {
+        fn pad_slice(slice: &[u8], len: usize) -> Vec<u8> {
+            let mut padded = vec![0; len];
+            padded[..slice.len()].copy_from_slice(slice);
+            padded
+        }
+
+        let max_len = input
+            .iter()
+            .filter_map(|x| x.as_ref().map(|b| b.len()))
+            .max()
+            .unwrap_or(0);
+        let padded_input = input
+            .iter()
+            .map(|x| x.as_ref().map(|b| pad_slice(b, max_len)));
+        let input_arr =
+            FixedSizeBinaryArray::try_from_sparse_iter_with_size(padded_input, max_len as i32)
+                .unwrap();
+        let padded_expected_min = expected_min.map(|b| pad_slice(b, max_len));
+        let padded_expected_max = expected_max.map(|b| pad_slice(b, max_len));
+
+        assert_eq!(
+            padded_expected_min.as_deref(),
+            min_fixed_size_binary(&input_arr)
+        );
+        assert_eq!(
+            padded_expected_max.as_deref(),
+            max_fixed_size_binary(&input_arr)
+        );
+    }
+
     macro_rules! test_binary {
         ($NAME:ident, $ARRAY:expr, $EXPECTED_MIN:expr, $EXPECTED_MAX: expr) => {
             #[test]
@@ -1255,6 +1300,8 @@ mod tests {
                 let binary_view = BinaryViewArray::from($ARRAY);
                 assert_eq!($EXPECTED_MIN, min_binary_view(&binary_view));
                 assert_eq!($EXPECTED_MAX, max_binary_view(&binary_view));
+
+                pad_inputs_and_test_fixed_size_binary($ARRAY, $EXPECTED_MIN, $EXPECTED_MAX);
             }
         };
     }