-
Notifications
You must be signed in to change notification settings - Fork 811
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
arrow-ord: lt
and eq
for nested list
#5408
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
119e23d
first draft
jayzhan211 76d960f
build_compare for non-nested
jayzhan211 1cd6c7c
move out compare list
jayzhan211 4dfa79b
change to ord
jayzhan211 ac7e789
remove duplicate node
jayzhan211 b86c096
dyn compare
jayzhan211 934528e
cleanup
jayzhan211 f8ba661
fmt
jayzhan211 51c2c1e
clippy
jayzhan211 bf93c86
dyncompare without null
jayzhan211 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,10 +31,13 @@ use arrow_array::{ | |
}; | ||
use arrow_buffer::bit_util::ceil; | ||
use arrow_buffer::{BooleanBuffer, MutableBuffer, NullBuffer}; | ||
use arrow_schema::ArrowError; | ||
use arrow_schema::{ArrowError, DataType}; | ||
use arrow_select::take::take; | ||
use std::cmp::Ordering; | ||
use std::ops::Not; | ||
|
||
use crate::ord::build_compare; | ||
|
||
#[derive(Debug, Copy, Clone)] | ||
enum Op { | ||
Equal, | ||
|
@@ -166,6 +169,48 @@ pub fn not_distinct(lhs: &dyn Datum, rhs: &dyn Datum) -> Result<BooleanArray, Ar | |
compare_op(Op::NotDistinct, lhs, rhs) | ||
} | ||
|
||
fn process_nested( | ||
l: &dyn Array, | ||
r: &dyn Array, | ||
op: Op, | ||
l_t: &DataType, | ||
r_t: &DataType, | ||
len: usize, | ||
) -> Result<Option<BooleanArray>, ArrowError> { | ||
use arrow_schema::DataType::*; | ||
if let (List(_), List(_)) = (l_t, r_t) { | ||
fn process_ordering( | ||
l: &dyn Array, | ||
r: &dyn Array, | ||
target_ord: Ordering, | ||
len: usize, | ||
) -> Result<BooleanArray, ArrowError> { | ||
let cmp = build_compare(l, r)?; | ||
let mut values = BooleanArray::builder(len); | ||
for i in 0..len { | ||
let ord = cmp(i, i); | ||
values.append_value(ord == target_ord); | ||
} | ||
Ok(values.finish()) | ||
} | ||
|
||
// Process nested data types | ||
match op { | ||
Op::Less => Ok(Some(process_ordering(l, r, Ordering::Less, len)?)), | ||
Op::Equal => Ok(Some(process_ordering(l, r, Ordering::Equal, len)?)), | ||
_ => Err(ArrowError::NotYetImplemented(format!( | ||
"Comparison for {op} is NYI" | ||
))), | ||
} | ||
} else if l_t.is_nested() { | ||
Err(ArrowError::NotYetImplemented(format!( | ||
"Comparison for {l_t} is NYI" | ||
))) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
|
||
/// Perform `op` on the provided `Datum` | ||
#[inline(never)] | ||
fn compare_op(op: Op, lhs: &dyn Datum, rhs: &dyn Datum) -> Result<BooleanArray, ArrowError> { | ||
|
@@ -198,12 +243,16 @@ fn compare_op(op: Op, lhs: &dyn Datum, rhs: &dyn Datum) -> Result<BooleanArray, | |
let r = r_v.map(|x| x.values().as_ref()).unwrap_or(r); | ||
let r_t = r.data_type(); | ||
|
||
if l_t != r_t || l_t.is_nested() { | ||
if l_t != r_t { | ||
return Err(ArrowError::InvalidArgumentError(format!( | ||
"Invalid comparison operation: {l_t} {op} {r_t}" | ||
))); | ||
} | ||
|
||
if let Some(values) = process_nested(l, r, op, l_t, r_t, len)? { | ||
return Ok(values); | ||
} | ||
|
||
// Defer computation as may not be necessary | ||
let values = || -> BooleanBuffer { | ||
let d = downcast_primitive_array! { | ||
|
@@ -544,7 +593,11 @@ impl<'a> ArrayOrd for &'a FixedSizeBinaryArray { | |
mod tests { | ||
use std::sync::Arc; | ||
|
||
use arrow_array::{DictionaryArray, Int32Array, Scalar, StringArray}; | ||
use arrow_array::{ | ||
types::Int32Type, ArrayRef, DictionaryArray, Int32Array, ListArray, Scalar, StringArray, | ||
}; | ||
use arrow_buffer::OffsetBuffer; | ||
use arrow_schema::Field; | ||
|
||
use super::*; | ||
|
||
|
@@ -702,4 +755,216 @@ mod tests { | |
|
||
neq(&col.slice(0, col.len() - 1), &col.slice(1, col.len() - 1)).unwrap(); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to see some tests of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure |
||
#[test] | ||
fn test_list_lt() { | ||
let l1 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(0), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4), Some(5)]), | ||
]); | ||
let l2 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(1), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4)]), | ||
]); | ||
let res = lt(&l1, &l2).unwrap(); | ||
assert_eq!(res, BooleanArray::from(vec![true, false, false])); | ||
|
||
let l1 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(0), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4), Some(5)]), | ||
]); | ||
let l2 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(0), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4)]), | ||
]); | ||
let res = lt(&l1, &l2).unwrap(); | ||
assert_eq!(res, BooleanArray::from(vec![false, false, false])); | ||
|
||
let l1 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(0), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4), Some(5)]), | ||
]); | ||
let l2 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(0), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4), Some(5), Some(7)]), | ||
]); | ||
let res = lt(&l1, &l2).unwrap(); | ||
assert_eq!(res, BooleanArray::from(vec![false, false, true])); | ||
} | ||
|
||
fn array_into_list_array(arr: ArrayRef) -> ListArray { | ||
let offsets = OffsetBuffer::from_lengths([arr.len()]); | ||
ListArray::new( | ||
Arc::new(Field::new("item", arr.data_type().to_owned(), true)), | ||
offsets, | ||
arr, | ||
None, | ||
) | ||
} | ||
|
||
#[test] | ||
fn test_nested_list_lt() { | ||
let l1 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(0), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4), Some(5)]), | ||
]); | ||
let l1 = array_into_list_array(Arc::new(l1)); | ||
let l2 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(1), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4)]), | ||
]); | ||
let l2 = array_into_list_array(Arc::new(l2)); | ||
|
||
let res = lt(&l1, &l2).unwrap(); | ||
assert_eq!(res, BooleanArray::from(vec![true])); | ||
|
||
let l1 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(1), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4), Some(5)]), | ||
]); | ||
let l1 = array_into_list_array(Arc::new(l1)); | ||
let l2 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(1), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4)]), | ||
]); | ||
let l2 = array_into_list_array(Arc::new(l2)); | ||
|
||
let res = lt(&l1, &l2).unwrap(); | ||
assert_eq!(res, BooleanArray::from(vec![false])); | ||
|
||
let l1 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(0), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4), Some(5)]), | ||
]); | ||
let l1 = array_into_list_array(Arc::new(l1)); | ||
let l2 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(0), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4), Some(5), Some(7)]), | ||
]); | ||
let l2 = array_into_list_array(Arc::new(l2)); | ||
|
||
let res = lt(&l1, &l2).unwrap(); | ||
assert_eq!(res, BooleanArray::from(vec![true])); | ||
} | ||
|
||
#[test] | ||
fn test_list_eq() { | ||
let l1 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(0), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4), Some(5)]), | ||
]); | ||
let l2 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(1), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4)]), | ||
]); | ||
let res = eq(&l1, &l2).unwrap(); | ||
assert_eq!(res, BooleanArray::from(vec![false, true, false])); | ||
|
||
let l1 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(0), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4), Some(5)]), | ||
]); | ||
let l2 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(0), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4)]), | ||
]); | ||
let res = eq(&l1, &l2).unwrap(); | ||
assert_eq!(res, BooleanArray::from(vec![true, true, false])); | ||
|
||
let l1 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(0), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4), Some(5)]), | ||
]); | ||
let l2 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(0), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4), Some(5), Some(7)]), | ||
]); | ||
let res = eq(&l1, &l2).unwrap(); | ||
assert_eq!(res, BooleanArray::from(vec![true, true, false])); | ||
} | ||
|
||
#[test] | ||
fn test_nested_list_eq() { | ||
let l1 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(0), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4), Some(5)]), | ||
]); | ||
let l1 = array_into_list_array(Arc::new(l1)); | ||
let l2 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(1), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4)]), | ||
]); | ||
let l2 = array_into_list_array(Arc::new(l2)); | ||
|
||
let res = eq(&l1, &l2).unwrap(); | ||
assert_eq!(res, BooleanArray::from(vec![false])); | ||
|
||
let l1 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(1), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4), Some(5)]), | ||
]); | ||
let l1 = array_into_list_array(Arc::new(l1)); | ||
let l2 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(1), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4)]), | ||
]); | ||
let l2 = array_into_list_array(Arc::new(l2)); | ||
|
||
let res = eq(&l1, &l2).unwrap(); | ||
assert_eq!(res, BooleanArray::from(vec![false])); | ||
|
||
let l1 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(0), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4), Some(5)]), | ||
]); | ||
let l1 = array_into_list_array(Arc::new(l1)); | ||
let l2 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(0), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4), Some(5), Some(7)]), | ||
]); | ||
let l2 = array_into_list_array(Arc::new(l2)); | ||
|
||
let res = eq(&l1, &l2).unwrap(); | ||
assert_eq!(res, BooleanArray::from(vec![false])); | ||
|
||
let l1 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(0), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4), Some(5)]), | ||
]); | ||
let l1 = array_into_list_array(Arc::new(l1)); | ||
let l2 = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![ | ||
Some(vec![Some(0), Some(1), Some(2)]), | ||
None, | ||
Some(vec![Some(3), Some(4), Some(5)]), | ||
]); | ||
let l2 = array_into_list_array(Arc::new(l2)); | ||
|
||
let res = eq(&l1, &l2).unwrap(); | ||
assert_eq!(res, BooleanArray::from(vec![true])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This now allows hitting unreachable in the below code block
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I return an error in
process_nested
, so the nested type that is NYI will not go down there