@@ -24,7 +24,9 @@ use arrow_buffer::{ArrowNativeType, NullBuffer};
2424use arrow_data:: bit_iterator:: try_for_each_valid_idx;
2525use arrow_schema:: * ;
2626use std:: borrow:: BorrowMut ;
27+ use std:: cmp:: { self , Ordering } ;
2728use std:: ops:: { BitAnd , BitOr , BitXor } ;
29+ use types:: ByteViewType ;
2830
2931/// An accumulator for primitive numeric values.
3032trait NumericAccumulator < T : ArrowNativeTypeOp > : Copy + Default {
@@ -425,14 +427,55 @@ where
425427 }
426428}
427429
430+ /// Helper to compute min/max of [`GenericByteViewArray<T>`].
431+ /// The specialized min/max leverages the inlined values to compare the byte views.
432+ /// `swap_cond` is the condition to swap current min/max with the new value.
433+ /// For example, `Ordering::Greater` for max and `Ordering::Less` for min.
434+ fn min_max_view_helper < T : ByteViewType > (
435+ array : & GenericByteViewArray < T > ,
436+ swap_cond : cmp:: Ordering ,
437+ ) -> Option < & T :: Native > {
438+ let null_count = array. null_count ( ) ;
439+ if null_count == array. len ( ) {
440+ None
441+ } else if null_count == 0 {
442+ let target_idx = ( 0 ..array. len ( ) ) . reduce ( |acc, item| {
443+ // SAFETY: array's length is correct so item is within bounds
444+ let cmp = unsafe { GenericByteViewArray :: compare_unchecked ( array, item, array, acc) } ;
445+ if cmp == swap_cond {
446+ item
447+ } else {
448+ acc
449+ }
450+ } ) ;
451+ // SAFETY: idx came from valid range `0..array.len()`
452+ unsafe { target_idx. map ( |idx| array. value_unchecked ( idx) ) }
453+ } else {
454+ let nulls = array. nulls ( ) . unwrap ( ) ;
455+
456+ let target_idx = nulls. valid_indices ( ) . reduce ( |acc_idx, idx| {
457+ let cmp =
458+ unsafe { GenericByteViewArray :: compare_unchecked ( array, idx, array, acc_idx) } ;
459+ if cmp == swap_cond {
460+ idx
461+ } else {
462+ acc_idx
463+ }
464+ } ) ;
465+
466+ // SAFETY: idx came from valid range `0..array.len()`
467+ unsafe { target_idx. map ( |idx| array. value_unchecked ( idx) ) }
468+ }
469+ }
470+
428471/// Returns the maximum value in the binary array, according to the natural order.
429472pub fn max_binary < T : OffsetSizeTrait > ( array : & GenericBinaryArray < T > ) -> Option < & [ u8 ] > {
430473 min_max_helper :: < & [ u8 ] , _ , _ > ( array, |a, b| * a < * b)
431474}
432475
433476/// Returns the maximum value in the binary view array, according to the natural order.
434477pub fn max_binary_view ( array : & BinaryViewArray ) -> Option < & [ u8 ] > {
435- min_max_helper :: < & [ u8 ] , _ , _ > ( array, |a , b| * a < * b )
478+ min_max_view_helper ( array, Ordering :: Greater )
436479}
437480
438481/// Returns the minimum value in the binary array, according to the natural order.
@@ -442,7 +485,7 @@ pub fn min_binary<T: OffsetSizeTrait>(array: &GenericBinaryArray<T>) -> Option<&
442485
443486/// Returns the minimum value in the binary view array, according to the natural order.
444487pub fn min_binary_view ( array : & BinaryViewArray ) -> Option < & [ u8 ] > {
445- min_max_helper :: < & [ u8 ] , _ , _ > ( array, |a , b| * a > * b )
488+ min_max_view_helper ( array, Ordering :: Less )
446489}
447490
448491/// Returns the maximum value in the string array, according to the natural order.
@@ -452,7 +495,7 @@ pub fn max_string<T: OffsetSizeTrait>(array: &GenericStringArray<T>) -> Option<&
452495
453496/// Returns the maximum value in the string view array, according to the natural order.
454497pub fn max_string_view ( array : & StringViewArray ) -> Option < & str > {
455- min_max_helper :: < & str , _ , _ > ( array, |a , b| * a < * b )
498+ min_max_view_helper ( array, Ordering :: Greater )
456499}
457500
458501/// Returns the minimum value in the string array, according to the natural order.
@@ -462,7 +505,7 @@ pub fn min_string<T: OffsetSizeTrait>(array: &GenericStringArray<T>) -> Option<&
462505
463506/// Returns the minimum value in the string view array, according to the natural order.
464507pub fn min_string_view ( array : & StringViewArray ) -> Option < & str > {
465- min_max_helper :: < & str , _ , _ > ( array, |a , b| * a > * b )
508+ min_max_view_helper ( array, Ordering :: Less )
466509}
467510
468511/// Returns the sum of values in the array.
0 commit comments