-
Notifications
You must be signed in to change notification settings - Fork 839
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
Support comparison between dictionary array and binary array #2645
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -2175,6 +2175,50 @@ macro_rules! typed_dict_string_array_cmp { | |
}}; | ||
} | ||
|
||
#[cfg(feature = "dyn_cmp_dict")] | ||
macro_rules! typed_dict_binary_array_cmp { | ||
($LEFT: expr, $RIGHT: expr, $LEFT_KEY_TYPE: expr, $RIGHT_TYPE: tt, $OP: expr) => {{ | ||
match $LEFT_KEY_TYPE { | ||
DataType::Int8 => { | ||
let left = as_dictionary_array::<Int8Type>($LEFT); | ||
cmp_dict_binary_array::<_, $RIGHT_TYPE, _>(left, $RIGHT, $OP) | ||
} | ||
DataType::Int16 => { | ||
let left = as_dictionary_array::<Int16Type>($LEFT); | ||
cmp_dict_binary_array::<_, $RIGHT_TYPE, _>(left, $RIGHT, $OP) | ||
} | ||
DataType::Int32 => { | ||
let left = as_dictionary_array::<Int32Type>($LEFT); | ||
cmp_dict_binary_array::<_, $RIGHT_TYPE, _>(left, $RIGHT, $OP) | ||
} | ||
DataType::Int64 => { | ||
let left = as_dictionary_array::<Int64Type>($LEFT); | ||
cmp_dict_binary_array::<_, $RIGHT_TYPE, _>(left, $RIGHT, $OP) | ||
} | ||
DataType::UInt8 => { | ||
let left = as_dictionary_array::<UInt8Type>($LEFT); | ||
cmp_dict_binary_array::<_, $RIGHT_TYPE, _>(left, $RIGHT, $OP) | ||
} | ||
DataType::UInt16 => { | ||
let left = as_dictionary_array::<UInt16Type>($LEFT); | ||
cmp_dict_binary_array::<_, $RIGHT_TYPE, _>(left, $RIGHT, $OP) | ||
} | ||
DataType::UInt32 => { | ||
let left = as_dictionary_array::<UInt32Type>($LEFT); | ||
cmp_dict_binary_array::<_, $RIGHT_TYPE, _>(left, $RIGHT, $OP) | ||
} | ||
DataType::UInt64 => { | ||
let left = as_dictionary_array::<UInt64Type>($LEFT); | ||
cmp_dict_binary_array::<_, $RIGHT_TYPE, _>(left, $RIGHT, $OP) | ||
} | ||
t => Err(ArrowError::NotYetImplemented(format!( | ||
"Cannot compare dictionary array of key type {}", | ||
t | ||
))), | ||
} | ||
}}; | ||
} | ||
|
||
#[cfg(feature = "dyn_cmp_dict")] | ||
macro_rules! typed_dict_boolean_array_cmp { | ||
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. Also removed this macro and use downcast_dictionary_array instead. |
||
($LEFT: expr, $RIGHT: expr, $LEFT_KEY_TYPE: expr, $OP: expr) => {{ | ||
|
@@ -2264,6 +2308,12 @@ macro_rules! typed_cmp_dict_non_dict { | |
(DataType::LargeUtf8, DataType::LargeUtf8) => { | ||
typed_dict_string_array_cmp!($LEFT, $RIGHT, left_key_type.as_ref(), i64, $OP) | ||
} | ||
(DataType::Binary, DataType::Binary) => { | ||
typed_dict_binary_array_cmp!($LEFT, $RIGHT, left_key_type.as_ref(), i32, $OP) | ||
} | ||
(DataType::LargeBinary, DataType::LargeBinary) => { | ||
typed_dict_binary_array_cmp!($LEFT, $RIGHT, left_key_type.as_ref(), i64, $OP) | ||
} | ||
(t1, t2) if t1 == t2 => Err(ArrowError::NotYetImplemented(format!( | ||
"Comparing dictionary array of type {} with array of type {} is not yet implemented", | ||
t1, t2 | ||
|
@@ -2675,6 +2725,29 @@ where | |
) | ||
} | ||
|
||
/// Perform given operation on `DictionaryArray` and `GenericBinaryArray`. The value | ||
/// type of `DictionaryArray` is same as `GenericBinaryArray`'s type. | ||
#[cfg(feature = "dyn_cmp_dict")] | ||
fn cmp_dict_binary_array<K, OffsetSize: OffsetSizeTrait, F>( | ||
left: &DictionaryArray<K>, | ||
right: &dyn Array, | ||
op: F, | ||
) -> Result<BooleanArray> | ||
where | ||
K: ArrowNumericType, | ||
F: Fn(&[u8], &[u8]) -> bool, | ||
{ | ||
compare_op( | ||
left.downcast_dict::<GenericBinaryArray<OffsetSize>>() | ||
.unwrap(), | ||
right | ||
.as_any() | ||
.downcast_ref::<GenericBinaryArray<OffsetSize>>() | ||
.unwrap(), | ||
op, | ||
) | ||
} | ||
|
||
/// Perform given operation on two `DictionaryArray`s which value type is | ||
/// primitive type. Returns an error if the two arrays have different value | ||
/// type | ||
|
@@ -6152,6 +6225,112 @@ mod tests { | |
); | ||
} | ||
|
||
#[test] | ||
#[cfg(feature = "dyn_cmp_dict")] | ||
fn test_eq_dyn_neq_dyn_dictionary_to_binary_array() { | ||
let values: BinaryArray = ["hello", "", "parquet"] | ||
.into_iter() | ||
.map(|b| Some(b.as_bytes())) | ||
.collect(); | ||
|
||
let keys = UInt64Array::from(vec![Some(0_u64), None, Some(2), Some(2)]); | ||
let dict_array = DictionaryArray::<UInt64Type>::try_new(&keys, &values).unwrap(); | ||
|
||
let array: BinaryArray = ["hello", "", "parquet", "test"] | ||
.into_iter() | ||
.map(|b| Some(b.as_bytes())) | ||
.collect(); | ||
|
||
let result = eq_dyn(&dict_array, &array); | ||
assert_eq!( | ||
result.unwrap(), | ||
BooleanArray::from(vec![Some(true), None, Some(true), Some(false)]) | ||
); | ||
|
||
let result = eq_dyn(&array, &dict_array); | ||
assert_eq!( | ||
result.unwrap(), | ||
BooleanArray::from(vec![Some(true), None, Some(true), Some(false)]) | ||
); | ||
|
||
let result = neq_dyn(&dict_array, &array); | ||
assert_eq!( | ||
result.unwrap(), | ||
BooleanArray::from(vec![Some(false), None, Some(false), Some(true)]) | ||
); | ||
|
||
let result = neq_dyn(&array, &dict_array); | ||
assert_eq!( | ||
result.unwrap(), | ||
BooleanArray::from(vec![Some(false), None, Some(false), Some(true)]) | ||
); | ||
} | ||
|
||
#[test] | ||
#[cfg(feature = "dyn_cmp_dict")] | ||
fn test_lt_dyn_lt_eq_dyn_gt_dyn_gt_eq_dyn_dictionary_to_binary_array() { | ||
let values: BinaryArray = ["hello", "", "parquet"] | ||
.into_iter() | ||
.map(|b| Some(b.as_bytes())) | ||
.collect(); | ||
|
||
let keys = UInt64Array::from(vec![Some(0_u64), None, Some(2), Some(2)]); | ||
let dict_array = DictionaryArray::<UInt64Type>::try_new(&keys, &values).unwrap(); | ||
|
||
let array: BinaryArray = ["hello", "", "parquet", "test"] | ||
.into_iter() | ||
.map(|b| Some(b.as_bytes())) | ||
.collect(); | ||
|
||
let result = lt_dyn(&dict_array, &array); | ||
assert_eq!( | ||
result.unwrap(), | ||
BooleanArray::from(vec![Some(false), None, Some(false), Some(true)]) | ||
); | ||
|
||
let result = lt_dyn(&array, &dict_array); | ||
assert_eq!( | ||
result.unwrap(), | ||
BooleanArray::from(vec![Some(false), None, Some(false), Some(false)]) | ||
); | ||
|
||
let result = lt_eq_dyn(&dict_array, &array); | ||
assert_eq!( | ||
result.unwrap(), | ||
BooleanArray::from(vec![Some(true), None, Some(true), Some(true)]) | ||
); | ||
|
||
let result = lt_eq_dyn(&array, &dict_array); | ||
assert_eq!( | ||
result.unwrap(), | ||
BooleanArray::from(vec![Some(true), None, Some(true), Some(false)]) | ||
); | ||
|
||
let result = gt_dyn(&dict_array, &array); | ||
assert_eq!( | ||
result.unwrap(), | ||
BooleanArray::from(vec![Some(false), None, Some(false), Some(false)]) | ||
); | ||
|
||
let result = gt_dyn(&array, &dict_array); | ||
assert_eq!( | ||
result.unwrap(), | ||
BooleanArray::from(vec![Some(false), None, Some(false), Some(true)]) | ||
); | ||
|
||
let result = gt_eq_dyn(&dict_array, &array); | ||
assert_eq!( | ||
result.unwrap(), | ||
BooleanArray::from(vec![Some(true), None, Some(true), Some(false)]) | ||
); | ||
|
||
let result = gt_eq_dyn(&array, &dict_array); | ||
assert_eq!( | ||
result.unwrap(), | ||
BooleanArray::from(vec![Some(true), None, Some(true), Some(true)]) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_dict_nlike_kernels() { | ||
let data = | ||
|
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.
I wonder if we could use downcast_dictionary_array here?
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.
Yea, nice catch. We could use it here and save some code.