Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use const generic for computing min/max #2

Draft
wants to merge 1 commit into
base: min-max-better-view
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions arrow-arith/src/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use arrow_buffer::{ArrowNativeType, NullBuffer};
use arrow_data::bit_iterator::try_for_each_valid_idx;
use arrow_schema::*;
use std::borrow::BorrowMut;
use std::cmp::{self, Ordering};
use std::cmp::Ordering;
use std::ops::{BitAnd, BitOr, BitXor};
use types::ByteViewType;

Expand Down Expand Up @@ -414,12 +414,17 @@ where

/// Helper to compute min/max of [`GenericByteViewArray<T>`].
/// The specialized min/max leverages the inlined values to compare the byte views.
/// `swap_cond` is the condition to swap current min/max with the new value.
/// For example, `Ordering::Greater` for max and `Ordering::Less` for min.
fn min_max_view_helper<T: ByteViewType>(
/// if `GREATER` is true, compares the values using `Ordering::Greater` (max)
/// if `GREATER` is false, compares the values using `Ordering::Less` (min)
fn min_max_view_helper<T: ByteViewType, const GREATER: bool>(
array: &GenericByteViewArray<T>,
swap_cond: cmp::Ordering,
) -> Option<&T::Native> {
let swap_cond = if GREATER {
Ordering::Greater
} else {
Ordering::Less
};

let null_count = array.null_count();
if null_count == array.len() {
None
Expand Down Expand Up @@ -460,7 +465,7 @@ pub fn max_binary<T: OffsetSizeTrait>(array: &GenericBinaryArray<T>) -> Option<&

/// Returns the maximum value in the binary view array, according to the natural order.
pub fn max_binary_view(array: &BinaryViewArray) -> Option<&[u8]> {
min_max_view_helper(array, Ordering::Greater)
min_max_view_helper::<_, true>(array)
}

/// Returns the minimum value in the binary array, according to the natural order.
Expand All @@ -470,7 +475,7 @@ pub fn min_binary<T: OffsetSizeTrait>(array: &GenericBinaryArray<T>) -> Option<&

/// Returns the minimum value in the binary view array, according to the natural order.
pub fn min_binary_view(array: &BinaryViewArray) -> Option<&[u8]> {
min_max_view_helper(array, Ordering::Less)
min_max_view_helper::<_, false>(array)
}

/// Returns the maximum value in the string array, according to the natural order.
Expand All @@ -480,7 +485,7 @@ pub fn max_string<T: OffsetSizeTrait>(array: &GenericStringArray<T>) -> Option<&

/// Returns the maximum value in the string view array, according to the natural order.
pub fn max_string_view(array: &StringViewArray) -> Option<&str> {
min_max_view_helper(array, Ordering::Greater)
min_max_view_helper::<_, true>(array)
}

/// Returns the minimum value in the string array, according to the natural order.
Expand All @@ -490,7 +495,7 @@ pub fn min_string<T: OffsetSizeTrait>(array: &GenericStringArray<T>) -> Option<&

/// Returns the minimum value in the string view array, according to the natural order.
pub fn min_string_view(array: &StringViewArray) -> Option<&str> {
min_max_view_helper(array, Ordering::Less)
min_max_view_helper::<_, false>(array)
}

/// Returns the sum of values in the array.
Expand Down