nevi-me commented on a change in pull request #8280:
URL: https://github.com/apache/arrow/pull/8280#discussion_r495435647
##########
File path: rust/arrow/src/compute/kernels/comparison.rs
##########
@@ -555,11 +555,159 @@ where
compare_op_scalar!(left, right, |a, b| a >= b)
}
+/// Checks if a `GenericListArray` contains a value in the `PrimitiveArray`
+pub fn contains<T, OffsetSize>(
+ left: &PrimitiveArray<T>,
+ right: &GenericListArray<OffsetSize>,
+) -> Result<BooleanArray>
+where
+ T: ArrowNumericType,
+ OffsetSize: OffsetSizeTrait,
+{
+ if left.len() != right.len() {
+ return Err(ArrowError::ComputeError(
+ "Cannot perform comparison operation on arrays of different length"
+ .to_string(),
+ ));
+ }
+
+ let not_both_null_bit_buffer =
+ match compare_option_bitmap(left.data_ref(), right.data_ref(),
left.len())? {
+ Some(buff) => buff,
+ None => new_all_set_buffer(left.len()),
+ };
+ let not_both_null_bitmap = not_both_null_bit_buffer.data();
+
+ let left_data = left.data();
+ let left_null_bitmap = match left_data.null_bitmap() {
+ Some(bitmap) => bitmap.clone().into_buffer(),
+ _ => new_all_set_buffer(left.len()),
+ };
+ let left_null_bitmap = left_null_bitmap.data();
+
+ let mut result = BooleanBufferBuilder::new(left.len());
+
+ for i in 0..left.len() {
+ let mut is_in = false;
+
+ // contains(null, null) = false
Review comment:
I think the test is
```sql
SELECT
'foo' = ANY(ARRAY['foo','bar']::text[]) AS non_null,
'foo' = ANY(ARRAY[]::text[]) AS empty,
NULL::text = ANY(ARRAY[NULL, 'foo']::text[]) AS null_value,
'foo' = ANY(NULL::text[]) AS null_array
```
but nonetheless it still yields the same result.
I copied this from another PR mostly-verbatim, so I'm only scrutinising the
code today myself.
I would prefer aligning with the SQL behaviour, that a null value can't be
contained in an array if the array has nulls.
If users want to find out if an array has nulls, they can use the below or
something better:
```rust
let array: ListArray<i32> = ...;
let ceil = bit_util::ceil(array.len(), 8);
let mut bools = MutableBuffer::new(ceil).
for i in -..array.len() {
if array.is_valid(i) && array.value(i).null_count() > 0 {
bools.set_bit(i);
}
}
// create bool array from bools
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]