Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Replaced un-needed unsafe #755

Merged
merged 1 commit into from
Jan 11, 2022
Merged
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
100 changes: 54 additions & 46 deletions src/compute/aggregate/min_max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub trait SimdOrd<T> {
fn new_max() -> Self;
}

/// Helper macro to perform min/max of binarys.
/// Helper to compute min/max of [`BinaryArray`]
fn min_max_binary<O: Offset, F: Fn(&[u8], &[u8]) -> bool>(
array: &BinaryArray<O>,
cmp: F,
Expand All @@ -40,33 +40,37 @@ fn min_max_binary<O: Offset, F: Fn(&[u8], &[u8]) -> bool>(
if null_count == array.len() || array.len() == 0 {
return None;
}
let mut n;
if let Some(validity) = array.validity() {
n = "".as_bytes();
let mut has_value = false;

for i in 0..array.len() {
let item = array.value(i);
if validity.get_bit(i) && (!has_value || cmp(n, item)) {
has_value = true;
n = item;
let value = if array.validity().is_some() {
array.iter().fold(None, |mut acc: Option<&[u8]>, v| {
if let Some(item) = v {
if let Some(acc) = acc.as_mut() {
if cmp(acc, item) {
*acc = item
}
} else {
acc = Some(item)
}
}
}
acc
})
} else {
// array.len() == 0 checked above
n = unsafe { array.value_unchecked(0) };
for i in 1..array.len() {
// loop is up to `len`.
let item = unsafe { array.value_unchecked(i) };
if cmp(n, item) {
n = item;
}
}
}
Some(n)
array
.values_iter()
.fold(None, |mut acc: Option<&[u8]>, item| {
if let Some(acc) = acc.as_mut() {
if cmp(acc, item) {
*acc = item
}
} else {
acc = Some(item)
}
acc
})
};
value
}

/// Helper macro to perform min/max of strings
/// Helper to compute min/max of [`Utf8Array`]
fn min_max_string<O: Offset, F: Fn(&str, &str) -> bool>(
array: &Utf8Array<O>,
cmp: F,
Expand All @@ -76,30 +80,34 @@ fn min_max_string<O: Offset, F: Fn(&str, &str) -> bool>(
if null_count == array.len() || array.len() == 0 {
return None;
}
let mut n;
if let Some(validity) = array.validity() {
n = "";
let mut has_value = false;

for i in 0..array.len() {
let item = array.value(i);
if validity.get_bit(i) && (!has_value || cmp(n, item)) {
has_value = true;
n = item;
let value = if array.validity().is_some() {
array.iter().fold(None, |mut acc: Option<&str>, v| {
if let Some(item) = v {
if let Some(acc) = acc.as_mut() {
if cmp(acc, item) {
*acc = item
}
} else {
acc = Some(item)
}
}
}
acc
})
} else {
// array.len() == 0 checked above
n = unsafe { array.value_unchecked(0) };
for i in 1..array.len() {
// loop is up to `len`.
let item = unsafe { array.value_unchecked(i) };
if cmp(n, item) {
n = item;
}
}
}
Some(n)
array
.values_iter()
.fold(None, |mut acc: Option<&str>, item| {
if let Some(acc) = acc.as_mut() {
if cmp(acc, item) {
*acc = item
}
} else {
acc = Some(item)
}
acc
})
};
value
}

fn nonnull_min_primitive<T>(values: &[T]) -> T
Expand Down