Skip to content
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

Merged
merged 3 commits into from
Jan 12, 2025
Merged

Conversation

tlm365
Copy link
Contributor

@tlm365 tlm365 commented Jan 6, 2025

Which issue does this PR close?

Closes #.

Rationale for this change

Improve performance of find_in_set function

What 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

Compiling datafusion-functions v44.0.0 (/home/tailm/repos/github/datafusion/datafusion/functions)
    Finished `bench` profile [optimized] target(s) in 1m 02s
     Running benches/find_in_set.rs (target/release/deps/find_in_set-c80fb04778cbe610)
Gnuplot not found, using plotters backend
find_in_set/string_len_8
                        time:   [414.89 µs 415.67 µs 416.41 µs]
                        change: [-64.610% -64.526% -64.440%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 2 outliers among 50 measurements (4.00%)
  2 (4.00%) low mild
find_in_set/string_view_len_8
                        time:   [440.65 µs 441.02 µs 441.50 µs]
                        change: [-63.746% -63.607% -63.494%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 2 outliers among 50 measurements (4.00%)
  1 (2.00%) high mild
  1 (2.00%) high severe

find_in_set/string_len_32
                        time:   [474.24 µs 474.97 µs 475.79 µs]
                        change: [-61.751% -61.586% -61.447%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 2 outliers among 50 measurements (4.00%)
  2 (4.00%) high mild
find_in_set/string_view_len_32
                        time:   [493.30 µs 494.79 µs 496.58 µs]
                        change: [-61.565% -61.365% -61.171%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 4 outliers among 50 measurements (8.00%)
  1 (2.00%) high mild
  3 (6.00%) high severe

find_in_set/string_len_1024
                        time:   [4.8152 ms 4.8243 ms 4.8367 ms]
                        change: [-12.694% -12.465% -12.233%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 3 outliers among 50 measurements (6.00%)
  2 (4.00%) high mild
  1 (2.00%) high severe
find_in_set/string_view_len_1024
                        time:   [4.8690 ms 4.8755 ms 4.8825 ms]
                        change: [-13.472% -13.274% -13.072%] (p = 0.00 < 0.05)
                        Performance has improved.

@tlm365 tlm365 marked this pull request as ready for review January 6, 2025 11:41
.split(',')
.position(|s| s == string)
.map_or(0, |idx| idx + 1);
builder.append_value(T::Native::from_usize(position).unwrap());
Copy link
Contributor

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()

Copy link
Contributor Author

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)
}

Copy link
Contributor

@comphead comphead left a 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 ?

@tlm365
Copy link
Contributor Author

tlm365 commented Jan 8, 2025

@comphead Thanks for reviewing,

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 ?

Oh yes, your suggestion is very reasonable. I will try to implement it. TYSM ❤

@tlm365 tlm365 marked this pull request as draft January 8, 2025 13:23
@tlm365
Copy link
Contributor Author

tlm365 commented Jan 8, 2025

I just pushed some updates to support scalar args. Could you please take a look? @jayzhan211 @comphead

@tlm365 tlm365 marked this pull request as ready for review January 8, 2025 17:56
@comphead
Copy link
Contributor

comphead commented Jan 8, 2025

Oh with literal support I think the code becomes much more tricky.
Wondering if the performance benefit still worthy such complications. @tlm365 can we a criterion to check find_in_set with literal vs original optimization?

@tlm365
Copy link
Contributor Author

tlm365 commented Jan 10, 2025

Oh with literal support I think the code becomes much more tricky.
Wondering if the performance benefit still worthy such complications. @tlm365 can we a criterion to check find_in_set with literal vs original optimization?

@comphead I have added benchmark for case find_in_set(str, str_list) - str_list is literal. And the benchmark result compare the original PR vs literal support commit (faster) here:

find_in_set_scalar/string_len_8
                        time:   [57.247 µs 57.355 µs 57.477 µs]
                        change: [-77.622% -77.503% -77.415%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 13 outliers among 100 measurements (13.00%)
  7 (7.00%) high mild
  6 (6.00%) high severe

find_in_set_scalar/string_len_32
                        time:   [57.872 µs 58.447 µs 59.215 µs]
                        change: [-77.737% -77.460% -77.182%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 16 outliers among 100 measurements (16.00%)
  16 (16.00%) high severe

find_in_set_scalar/string_len_1024
                        time:   [58.024 µs 58.462 µs 58.993 µs]
                        change: [-77.049% -76.867% -76.660%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 18 outliers among 100 measurements (18.00%)
  3 (3.00%) high mild
  15 (15.00%) high severe

NOTE:
One notable thing I want to point out here. find_in_set(str, str_list) doesn't work if str or str_list is Scalar::Utf8View (string view literal), which is true for both the main branch and the original version of this PR (unit tests for these cases have been added in this PR, seems like a bug?! 🤔)

@comphead
Copy link
Contributor

NOTE:
One notable thing I want to point out here. find_in_set(str, str_list) doesn't work if str or str_list is Scalar::Utf8View (string view literal), which is true for both the main branch and the original version of this PR (unit tests for these cases have been added in this PR, seems like a bug?! 🤔)

Is possible to provide a test case so we can file a ticket if this is undesired behavior?

The performance is definitely better, thanks @tlm365 I'm going to merge the PR

],
Int32Array::from(vec![0, 0, 1, 2, 3])
);
test_find_in_set!(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is possible to provide a test case so we can file a ticket if this is undesired behavior?

@comphead This is the test case I mentioned before. (test_find_in_set_with_scalar_args_2/3/4)

@alamb
Copy link
Contributor

alamb commented Jan 12, 2025

Looks like this PR is ready to go so I'll merge it in. Let's handle any follow on work with subsequent PRs.

Thanks @tlm365 @comphead and @jayzhan211

@alamb alamb merged commit 2e5c6b2 into apache:main Jan 12, 2025
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants