-
Notifications
You must be signed in to change notification settings - Fork 884
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
Use take for dictionary like comparisons #3313
Merged
Merged
Changes from 1 commit
Commits
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
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 |
---|---|---|
|
@@ -21,6 +21,7 @@ use arrow_array::*; | |
use arrow_data::bit_mask::combine_option_bitmap; | ||
use arrow_data::ArrayData; | ||
use arrow_schema::*; | ||
use arrow_select::take::take; | ||
use regex::Regex; | ||
use std::collections::HashMap; | ||
|
||
|
@@ -214,7 +215,10 @@ pub fn like_utf8_scalar_dyn( | |
DataType::Dictionary(_, _) => { | ||
downcast_dictionary_array!( | ||
left => { | ||
like_dict_scalar(left, right) | ||
let dict_comparison = like_utf8_scalar_dyn(left.values().as_ref(), right)?; | ||
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. The duplication is slightly unfortunate, but will hopefully get cleaned up as part of #3296 |
||
// TODO: Use take_boolean (#2967) | ||
let array = take(&dict_comparison, left.keys(), None)?; | ||
Ok(BooleanArray::from(array.data().clone())) | ||
} | ||
t => Err(ArrowError::ComputeError(format!( | ||
"Should be DictionaryArray but got: {}", t | ||
|
@@ -240,31 +244,6 @@ pub fn like_utf8_scalar<OffsetSize: OffsetSizeTrait>( | |
like_scalar(left, right) | ||
} | ||
|
||
/// Perform SQL `left LIKE right` operation on [`DictionaryArray`] with values | ||
/// [`StringArray`]/[`LargeStringArray`] and a scalar. | ||
/// | ||
/// See the documentation on [`like_utf8`] for more details. | ||
fn like_dict_scalar<K: ArrowPrimitiveType>( | ||
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. 👍 |
||
left: &DictionaryArray<K>, | ||
right: &str, | ||
) -> Result<BooleanArray, ArrowError> { | ||
match left.value_type() { | ||
DataType::Utf8 => { | ||
let left = left.downcast_dict::<GenericStringArray<i32>>().unwrap(); | ||
like_scalar(left, right) | ||
} | ||
DataType::LargeUtf8 => { | ||
let left = left.downcast_dict::<GenericStringArray<i64>>().unwrap(); | ||
like_scalar(left, right) | ||
} | ||
_ => { | ||
Err(ArrowError::ComputeError( | ||
"like_dict_scalar only supports DictionaryArray with Utf8 or LargeUtf8 values".to_string(), | ||
)) | ||
} | ||
} | ||
} | ||
|
||
/// Transforms a like `pattern` to a regex compatible pattern. To achieve that, it does: | ||
/// | ||
/// 1. Replace like wildcards for regex expressions as the pattern will be evaluated using regex match: `%` => `.*` and `_` => `.` | ||
|
@@ -431,7 +410,10 @@ pub fn nlike_utf8_scalar_dyn( | |
DataType::Dictionary(_, _) => { | ||
downcast_dictionary_array!( | ||
left => { | ||
nlike_dict_scalar(left, right) | ||
let dict_comparison = nlike_utf8_scalar_dyn(left.values().as_ref(), right)?; | ||
// TODO: Use take_boolean (#2967) | ||
let array = take(&dict_comparison, left.keys(), None)?; | ||
Ok(BooleanArray::from(array.data().clone())) | ||
} | ||
t => Err(ArrowError::ComputeError(format!( | ||
"Should be DictionaryArray but got: {}", t | ||
|
@@ -457,31 +439,6 @@ pub fn nlike_utf8_scalar<OffsetSize: OffsetSizeTrait>( | |
nlike_scalar(left, right) | ||
} | ||
|
||
/// Perform SQL `left NOT LIKE right` operation on [`DictionaryArray`] with values | ||
/// [`StringArray`]/[`LargeStringArray`] and a scalar. | ||
/// | ||
/// See the documentation on [`like_utf8`] for more details. | ||
fn nlike_dict_scalar<K: ArrowPrimitiveType>( | ||
left: &DictionaryArray<K>, | ||
right: &str, | ||
) -> Result<BooleanArray, ArrowError> { | ||
match left.value_type() { | ||
DataType::Utf8 => { | ||
let left = left.downcast_dict::<GenericStringArray<i32>>().unwrap(); | ||
nlike_scalar(left, right) | ||
} | ||
DataType::LargeUtf8 => { | ||
let left = left.downcast_dict::<GenericStringArray<i64>>().unwrap(); | ||
nlike_scalar(left, right) | ||
} | ||
_ => { | ||
Err(ArrowError::ComputeError( | ||
"nlike_dict_scalar only supports DictionaryArray with Utf8 or LargeUtf8 values".to_string(), | ||
)) | ||
} | ||
} | ||
} | ||
|
||
/// Perform SQL `left ILIKE right` operation on [`StringArray`] / | ||
/// [`LargeStringArray`]. | ||
/// | ||
|
@@ -663,7 +620,10 @@ pub fn ilike_utf8_scalar_dyn( | |
DataType::Dictionary(_, _) => { | ||
downcast_dictionary_array!( | ||
left => { | ||
ilike_dict_scalar(left, right) | ||
let dict_comparison = ilike_utf8_scalar_dyn(left.values().as_ref(), right)?; | ||
// TODO: Use take_boolean (#2967) | ||
let array = take(&dict_comparison, left.keys(), None)?; | ||
Ok(BooleanArray::from(array.data().clone())) | ||
} | ||
t => Err(ArrowError::ComputeError(format!( | ||
"Should be DictionaryArray but got: {}", t | ||
|
@@ -689,31 +649,6 @@ pub fn ilike_utf8_scalar<OffsetSize: OffsetSizeTrait>( | |
ilike_scalar(left, right) | ||
} | ||
|
||
/// Perform SQL `left ILIKE right` operation on [`DictionaryArray`] with values | ||
/// [`StringArray`]/[`LargeStringArray`] and a scalar. | ||
/// | ||
/// See the documentation on [`like_utf8`] for more details. | ||
fn ilike_dict_scalar<K: ArrowPrimitiveType>( | ||
left: &DictionaryArray<K>, | ||
right: &str, | ||
) -> Result<BooleanArray, ArrowError> { | ||
match left.value_type() { | ||
DataType::Utf8 => { | ||
let left = left.downcast_dict::<GenericStringArray<i32>>().unwrap(); | ||
ilike_scalar(left, right) | ||
} | ||
DataType::LargeUtf8 => { | ||
let left = left.downcast_dict::<GenericStringArray<i64>>().unwrap(); | ||
ilike_scalar(left, right) | ||
} | ||
_ => { | ||
Err(ArrowError::ComputeError( | ||
"ilike_dict_scalar only supports DictionaryArray with Utf8 or LargeUtf8 values".to_string(), | ||
)) | ||
} | ||
} | ||
} | ||
|
||
/// Perform SQL `left NOT ILIKE right` operation on [`StringArray`] / | ||
/// [`LargeStringArray`]. | ||
/// | ||
|
@@ -843,7 +778,10 @@ pub fn nilike_utf8_scalar_dyn( | |
DataType::Dictionary(_, _) => { | ||
downcast_dictionary_array!( | ||
left => { | ||
nilike_dict_scalar(left, right) | ||
let dict_comparison = nilike_utf8_scalar_dyn(left.values().as_ref(), right)?; | ||
// TODO: Use take_boolean (#2967) | ||
let array = take(&dict_comparison, left.keys(), None)?; | ||
Ok(BooleanArray::from(array.data().clone())) | ||
} | ||
t => Err(ArrowError::ComputeError(format!( | ||
"Should be DictionaryArray but got: {}", t | ||
|
@@ -869,31 +807,6 @@ pub fn nilike_utf8_scalar<OffsetSize: OffsetSizeTrait>( | |
nilike_scalar(left, right) | ||
} | ||
|
||
/// Perform SQL `left NOT ILIKE right` operation on [`DictionaryArray`] with values | ||
/// [`StringArray`]/[`LargeStringArray`] and a scalar. | ||
/// | ||
/// See the documentation on [`like_utf8`] for more details. | ||
fn nilike_dict_scalar<K: ArrowPrimitiveType>( | ||
left: &DictionaryArray<K>, | ||
right: &str, | ||
) -> Result<BooleanArray, ArrowError> { | ||
match left.value_type() { | ||
DataType::Utf8 => { | ||
let left = left.downcast_dict::<GenericStringArray<i32>>().unwrap(); | ||
nilike_scalar(left, right) | ||
} | ||
DataType::LargeUtf8 => { | ||
let left = left.downcast_dict::<GenericStringArray<i64>>().unwrap(); | ||
nilike_scalar(left, right) | ||
} | ||
_ => { | ||
Err(ArrowError::ComputeError( | ||
"nilike_dict_scalar only supports DictionaryArray with Utf8 or LargeUtf8 values".to_string(), | ||
)) | ||
} | ||
} | ||
} | ||
|
||
fn is_like_pattern(c: char) -> bool { | ||
c == '%' || c == '_' | ||
} | ||
|
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
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 is somewhat unfortunate, but arrow-select is fairly foundational so I'm not too worried