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

Cleanup uses of Array::data_ref (#3880) #3918

Merged
merged 2 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
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
54 changes: 9 additions & 45 deletions arrow-arith/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,13 @@ fn math_checked_divide_op_on_iters<T, F>(
left: impl Iterator<Item = Option<T::Native>>,
right: impl Iterator<Item = Option<T::Native>>,
op: F,
len: usize,
null_bit_buffer: Option<arrow_buffer::Buffer>,
nulls: Option<arrow_buffer::NullBuffer>,
) -> Result<PrimitiveArray<T>, ArrowError>
where
T: ArrowNumericType,
F: Fn(T::Native, T::Native) -> Result<T::Native, ArrowError>,
{
let buffer = if null_bit_buffer.is_some() {
let buffer = if nulls.is_some() {
let values = left.zip(right).map(|(left, right)| {
if let (Some(l), Some(r)) = (left, right) {
op(l, r)
Expand All @@ -130,18 +129,7 @@ where
unsafe { arrow_buffer::Buffer::try_from_trusted_len_iter(values) }
}?;

let data = unsafe {
arrow_data::ArrayData::new_unchecked(
T::DATA_TYPE,
len,
None,
null_bit_buffer,
0,
vec![buffer],
vec![],
)
};
Ok(PrimitiveArray::<T>::from(data))
Ok(PrimitiveArray::new(T::DATA_TYPE, buffer.into(), nulls))
}

/// Calculates the modulus operation `left % right` on two SIMD inputs.
Expand Down Expand Up @@ -284,20 +272,16 @@ where
}

// Create the combined `Bitmap`
let null_bit_buffer = arrow_data::bit_mask::combine_option_bitmap(
&[left.data_ref(), right.data_ref()],
left.len(),
);
let nulls = arrow_buffer::NullBuffer::union(left.nulls(), right.nulls());

let lanes = T::lanes();
let buffer_size = left.len() * std::mem::size_of::<T::Native>();
let mut result =
arrow_buffer::MutableBuffer::new(buffer_size).with_bitset(buffer_size, false);

match &null_bit_buffer {
match &nulls {
Some(b) => {
// combine_option_bitmap returns a slice or new buffer starting at 0
let valid_chunks = b.bit_chunks(0, left.len());
let valid_chunks = b.inner().bit_chunks();

// process data in chunks of 64 elements since we also get 64 bits of validity information at a time

Expand Down Expand Up @@ -372,18 +356,7 @@ where
}
}

let data = unsafe {
arrow_data::ArrayData::new_unchecked(
T::DATA_TYPE,
left.len(),
None,
null_bit_buffer,
0,
vec![result.into()],
vec![],
)
};
Ok(PrimitiveArray::<T>::from(data))
Ok(PrimitiveArray::new(T::DATA_TYPE, result.into(), nulls))
}

/// Applies $OP to $LEFT and $RIGHT which are two dictionaries which have (the same) key type $KT
Expand Down Expand Up @@ -628,10 +601,7 @@ where
)));
}

let null_bit_buffer = arrow_data::bit_mask::combine_option_bitmap(
&[left.data_ref(), right.data_ref()],
left.len(),
);
let nulls = arrow_buffer::NullBuffer::union(left.nulls(), right.nulls());

// Safety justification: Since the inputs are valid Arrow arrays, all values are
// valid indexes into the dictionary (which is verified during construction)
Expand All @@ -653,13 +623,7 @@ where
.take_iter_unchecked(right.keys_iter())
};

math_checked_divide_op_on_iters(
left_iter,
right_iter,
op,
left.len(),
null_bit_buffer,
)
math_checked_divide_op_on_iters(left_iter, right_iter, op, nulls)
}

#[cfg(feature = "dyn_arith_dict")]
Expand Down
Loading