-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Improve performance of find_in_set
function
#14020
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Tai Le Manh <[email protected]>
.split(',') | ||
.position(|s| s == string) | ||
.map_or(0, |idx| idx + 1); | ||
builder.append_value(T::Native::from_usize(position).unwrap()); |
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.
Probably faster if create Vec
first and use PrimitiveArray::from()
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.
@jayzhan211 Thanks for reviewing,
Probably faster if create Vec first and use PrimitiveArray::from()
I have tried implementing it this way, it seems to have similar performance to this PR.
Here is the code I have tested:
pub fn find_in_set_general<'a, T, V>(
string_array: V,
str_list_array: V,
) -> Result<ArrayRef>
where
T: ArrowPrimitiveType,
T::Native: OffsetSizeTrait,
V: StringArrayType<'a, Item = &'a str>,
PrimitiveArray<T>: From<Vec<Option<<T as ArrowPrimitiveType>::Native>>>,
{
let string_iter = ArrayIter::new(string_array);
let str_list_iter = ArrayIter::new(str_list_array);
let mut result: Vec<Option<T::Native>> = Vec::with_capacity(string_iter.len());
string_iter
.zip(str_list_iter)
.for_each(
|(string_opt, str_list_opt)| match (string_opt, str_list_opt) {
(Some(string), Some(str_list)) => {
let position = str_list
.split(',')
.position(|s| s == string)
.map_or(0, |idx| idx + 1);
let value = T::Native::from_usize(position).unwrap();
result.push(Some(value));
}
_ => result.push(None),
},
);
let array = PrimitiveArray::<T>::from(result);
Ok(Arc::new(array) as ArrayRef)
}
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 think it is a good PR the way it is. One thing comes to my mind which probably relevant for other string functions as well.
So we do split(',')
for every search pattern value, but most likely that argument will be literal like SELECT FIND_IN_SET(a, "s,q,l");
so we can do split only once.
I think we need to have a check a literal or column somewhere and we can optimize even further having this info, WDYT @tlm365 ?
Which issue does this PR close?
Closes #.
Rationale for this change
Improve performance of
find_in_set
functionWhat changes are included in this PR?
Are these changes tested?
Yes. Added unit tests and benchmarks.
Are there any user-facing changes?
No.
*BENCHMARK RESULT