You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "andygrove (via GitHub)" <gi...@apache.org> on 2025/12/27 19:05:36 UTC

[PR] perf: Optimize `contains` for scalar search arg [datafusion]

andygrove opened a new pull request, #19514:
URL: https://github.com/apache/datafusion/pull/19514

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   - Closes #.
   
   ## Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   `ContainsFunc` used `make_scalar_function`, which expands scalar arguments into arrays before calling Arrow's `contains` function, defeating Arrow's built-in scalar optimization.
   
   | Case                            | Before      | After       | Speedup       |
   |---------------------------------|-------------|-------------|---------------|
   | StringArray + scalar search     | 259 µs      | 41 µs       | 6.3x          |
   | StringViewArray + scalar search | 339 µs      | 41 µs       | 8.3x          |
   | Array + array search            | ~260-300 µs | ~265-305 µs | No regression |
   
   ## What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   Rewrote invoke_with_args to:
   1. Handle the four cases directly (scalar/scalar, array/scalar, scalar/array, array/array)
   2. Use Arrow's Scalar<ArrayRef> wrapper when passing scalar arguments
   3. Let Arrow's contains function use its optimized op_scalar path when the second argument is a scalar
   
   
   ## Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   ## Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Optimize `contains` for scalar search arg [datafusion]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #19514:
URL: https://github.com/apache/datafusion/pull/19514#issuecomment-3694864572

   > > Thanks @andygrove the numbers make huge sense to me, what prob concerns is DF using `make_scalar_function` in lots of places so the same problem can be relevant for others.
   > > Would you mind adding more details what is the exact reason and what scalar/array combinations are the most trouble?
   > > this info might be important how DF treats bultin functions now
   > 
   > Basically, the `make_scalar_function` functions (there are three implementations, two of which are identical and one with slightly different behavior) convert scalar arguments into arrays. This adds unnecessary overhead in some cases because Arrow has specialized implementations of some kernels with fast paths for scalar arguments. For example, for `contains`, Arrow has `pub fn contains(left: &dyn Datum, right: &dyn Datum)` and `Datum` is implemented for arrays and scalars. The implementation has special handling for scalars.
   > 
   > The three implementations of `make_scalar_function` can be found in these files:
   > 
   >     * `datafusion/functions/src/utils.rs`
   > 
   >     * `datafusion/functions-nested/src/utils.rs`
   > 
   >     * `datafusion/spark/src/function/functions_nested_utils.rs`
   
   The issue is that `make_scalar_function` expected ArrayRefs:
   
   ```
   F: Fn(&[ArrayRef]) -> Result<ArrayRef>,                                                                                                                                                      
   ```
   
   It would perhaps be better if it accepted `ColumnarValue`, which supports both arrays and scalars. This is probably a large refactor.
   
   ```
   F: Fn(&[ColumnarValue]) -> Result<ArrayRef>,   
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Optimize `contains` for scalar search arg [datafusion]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #19514:
URL: https://github.com/apache/datafusion/pull/19514#issuecomment-3694893516

   @comphead I created an alternate PR that creates a new version of `make_scalar_function` that does not expand scalars: https://github.com/apache/datafusion/pull/19529


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Optimize `contains` for scalar search arg [datafusion]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #19514:
URL: https://github.com/apache/datafusion/pull/19514#issuecomment-3694848340

   > Thanks @andygrove the numbers make huge sense to me, what prob concerns is DF using `make_scalar_function` in lots of places so the same problem can be relevant for others.
   > 
   > Would you mind adding more details what is the exact reason and what scalar/array combinations are the most trouble?
   > 
   > this info might be important how DF treats bultin functions now
   
   Basically, the `make_scalar_function` functions (there are three implementations, two of which are identical and one with slightly different behavior) convert scalar arguments into arrays. This adds unnecessary overhead in some cases because Arrow has specialized implementations of some kernels with fast paths for scalar arguments. For example, for `contains`, Arrow has `pub fn contains(left: &dyn Datum, right: &dyn Datum)` and `Datum` is implemented for arrays and scalars. The implementation has special handling for scalars.
   
   The three implementations of `make_scalar_function` can be found in these files:
   
   - `datafusion/functions/src/utils.rs`
   - `datafusion/functions-nested/src/utils.rs`
   - `datafusion/spark/src/function/functions_nested_utils.rs`
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] perf: Optimize `contains` for scalar search arg [datafusion]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove closed pull request #19514: perf: Optimize `contains` for scalar search arg
URL: https://github.com/apache/datafusion/pull/19514


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org