You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "zhangxffff (via GitHub)" <gi...@apache.org> on 2026/02/19 08:48:26 UTC

[PR] perf: Optimize dynamic IN list evaluation with vectorized Arrow eq kernel [datafusion]

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

   ## 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 #20427 .
   
   ## Rationale for this change
   
   The dynamic IN list evaluation path in `InListExpr::evaluate()` is triggered when the list contains non-constant expressions such as column references (e.g., `a IN (b, c, d)`). It currently compares values row-by-row via `make_comparator` bypassing Arrow's vectorized SIMD kernels. This makes it orders of magnitude slower than the static filter path (HashSet) used for constant literals.
   
   <!--
    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.  
   -->
   
   ## 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.
   -->
   
     In the dynamic IN list path (`None =>` branch in `InListExpr::evaluate()`):
   
     1. **Vectorized comparison**: Use `arrow::compute::kernels::cmp::eq` instead of per-row `make_comparator` for non-nested types (primitives, strings, binary, dictionary). For nested types (Struct, List, Map), fall back to `make_comparator` since `arrow_eq` intentionally rejects them due to ambiguous null semantics.
     2. **Short-circuit with `break`**: Replace `try_fold` with an explicit `for` loop. Check `found.true_count() == num_rows` before each iteration and `break` immediately — skipping both `evaluate()` and comparison for remaining list items.
   
   
   ## Are these changes tested?
    Yes. All 37 existing `in_list` unit tests pass, including nested-type tests (`in_list_struct_with_exprs_not_array`, etc.). New criterion benchmarks (`bench_dynamic_int32`, `bench_dynamic_utf8`) are added for the dynamic path.
   
   Benchmarked with `cargo bench --bench in_list -- "in_list_dynamic"` on 8192-row batches. Int32 improved 11–320x, Utf8 improved 2–10x, with the largest gains on high match rates due to early termination.
   
   ```
   (zhangxffff) zhangxffff@95d3d60664da ~/W/datafusion (feat/opt_dynamic_in)> critcmp after before
   group                                                 after                                  before
   -----                                                 -----                                  ------
   in_list_dynamic/Int32/list=28/match=0%/nulls=0%       1.00     92.8±0.87µs        ? ?/sec    11.80 1094.4±21.14µs        ? ?/sec
   in_list_dynamic/Int32/list=28/match=0%/nulls=20%      1.00    107.9±0.90µs        ? ?/sec    16.57 1788.5±12.38µs        ? ?/sec
   in_list_dynamic/Int32/list=28/match=100%/nulls=0%     1.00      3.5±0.04µs        ? ?/sec    319.51 1116.0±21.26µs        ? ?/sec
   in_list_dynamic/Int32/list=28/match=100%/nulls=20%    1.00    107.3±1.97µs        ? ?/sec    16.63 1783.9±15.31µs        ? ?/sec
   in_list_dynamic/Int32/list=28/match=50%/nulls=0%      1.00     49.6±0.31µs        ? ?/sec    32.46 1610.0±27.97µs        ? ?/sec
   in_list_dynamic/Int32/list=28/match=50%/nulls=20%     1.00    107.2±1.15µs        ? ?/sec    19.54     2.1±0.01ms        ? ?/sec
   in_list_dynamic/Int32/list=3/match=0%/nulls=0%        1.00     10.1±0.07µs        ? ?/sec    11.61   116.9±1.61µs        ? ?/sec
   in_list_dynamic/Int32/list=3/match=0%/nulls=20%       1.00     11.4±0.11µs        ? ?/sec    15.09   172.6±0.97µs        ? ?/sec
   in_list_dynamic/Int32/list=3/match=100%/nulls=0%      1.00      3.5±0.02µs        ? ?/sec    33.76   119.7±2.75µs        ? ?/sec
   in_list_dynamic/Int32/list=3/match=100%/nulls=20%     1.00     11.4±0.16µs        ? ?/sec    15.12   173.1±0.97µs        ? ?/sec
   in_list_dynamic/Int32/list=3/match=50%/nulls=0%       1.00     10.0±0.08µs        ? ?/sec    16.92   170.0±4.09µs        ? ?/sec
   in_list_dynamic/Int32/list=3/match=50%/nulls=20%      1.00     11.6±0.19µs        ? ?/sec    17.49   202.5±2.60µs        ? ?/sec
   in_list_dynamic/Int32/list=8/match=0%/nulls=0%        1.00     26.6±0.45µs        ? ?/sec    11.73   312.5±2.88µs        ? ?/sec
   in_list_dynamic/Int32/list=8/match=0%/nulls=20%       1.00     30.4±0.43µs        ? ?/sec    16.23   493.6±3.45µs        ? ?/sec
   in_list_dynamic/Int32/list=8/match=100%/nulls=0%      1.00      3.5±0.03µs        ? ?/sec    90.16   313.6±6.51µs        ? ?/sec
   in_list_dynamic/Int32/list=8/match=100%/nulls=20%     1.00     31.2±0.23µs        ? ?/sec    16.04   499.9±4.44µs        ? ?/sec
   in_list_dynamic/Int32/list=8/match=50%/nulls=0%       1.00     26.6±0.17µs        ? ?/sec    17.07   453.4±8.14µs        ? ?/sec
   in_list_dynamic/Int32/list=8/match=50%/nulls=20%      1.00     30.8±0.37µs        ? ?/sec    19.16   590.6±2.55µs        ? ?/sec
   in_list_dynamic/Utf8/list=28/match=0%                 1.00    149.9±2.70µs        ? ?/sec    10.51  1574.8±9.97µs        ? ?/sec
   in_list_dynamic/Utf8/list=28/match=100%               1.00    725.9±9.09µs        ? ?/sec    2.13   1548.3±7.20µs        ? ?/sec
   in_list_dynamic/Utf8/list=28/match=50%                1.00   1070.0±7.65µs        ? ?/sec    2.93      3.1±0.02ms        ? ?/sec
   in_list_dynamic/Utf8/list=3/match=0%                  1.00     16.1±0.17µs        ? ?/sec    10.07   162.4±1.62µs        ? ?/sec
   in_list_dynamic/Utf8/list=3/match=100%                1.00     65.8±1.22µs        ? ?/sec    2.39    157.1±1.79µs        ? ?/sec
   in_list_dynamic/Utf8/list=3/match=50%                 1.00     93.4±2.15µs        ? ?/sec    3.32    309.6±1.23µs        ? ?/sec
   in_list_dynamic/Utf8/list=8/match=0%                  1.00     43.0±0.29µs        ? ?/sec    10.37   445.6±3.09µs        ? ?/sec
   in_list_dynamic/Utf8/list=8/match=100%                1.00    197.5±0.93µs        ? ?/sec    2.21    436.9±2.10µs        ? ?/sec
   in_list_dynamic/Utf8/list=8/match=50%                 1.00    296.8±2.36µs        ? ?/sec    2.97    880.5±6.52µs        ? ?/sec
   
   ```
   
   <!--
   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?
   No.
   <!--
   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 dynamic IN list evaluation with vectorized Arrow eq kernel [datafusion]

Posted by "Dandandan (via GitHub)" <gi...@apache.org>.
Dandandan commented on code in PR #20428:
URL: https://github.com/apache/datafusion/pull/20428#discussion_r2831852295


##########
datafusion/physical-expr/src/expressions/in_list.rs:
##########
@@ -773,11 +774,23 @@ impl PhysicalExpr for InListExpr {
             None => {
                 // No static filter: iterate through each expression, compare, and OR results
                 let value = value.into_array(num_rows)?;
-                let found = self.list.iter().map(|expr| expr.evaluate(batch)).try_fold(
-                    BooleanArray::new(BooleanBuffer::new_unset(num_rows), None),
-                    |result, expr| -> Result<BooleanArray> {
-                        let rhs = match expr? {
-                            ColumnarValue::Array(array) => {
+                let use_arrow_eq = !value.data_type().is_nested();
+                let mut found =

Review Comment:
   Instead of setting allocating the first to all -false (i.e. extra allocation / memset), it could initialize the first separately.



-- 
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 dynamic IN list evaluation [datafusion]

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

   @adriangb Sorry for the delay. I've opened a focused PR #20528 to replace `make_comparator` with Arrow's vectorized eq kernel. Please review it at your convenience.
   
   After #20528 is merged, I'll submit a follow-up PR for short-circuit early termination.


-- 
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 IN list with column references evaluation [datafusion]

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

   This PR split into three PR:
   - PR1: benchmarks (#20444, merged)
   - PR2: Arrow vectorized eq kernel (#20528, merged)
   - PR3 : short-circuit, collect_bool, and first-expr initialization (#20694 )
   Would close after #20694 merged.


-- 
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 IN list with column references evaluation [datafusion]

Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb closed pull request #20428: perf: Optimize IN list with column references evaluation
URL: https://github.com/apache/datafusion/pull/20428


-- 
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 dynamic IN list evaluation [datafusion]

Posted by "zhangxffff (via GitHub)" <gi...@apache.org>.
zhangxffff commented on code in PR #20428:
URL: https://github.com/apache/datafusion/pull/20428#discussion_r2848128998


##########
datafusion/physical-expr/src/expressions/in_list.rs:
##########
@@ -773,11 +774,23 @@ impl PhysicalExpr for InListExpr {
             None => {
                 // No static filter: iterate through each expression, compare, and OR results
                 let value = value.into_array(num_rows)?;
-                let found = self.list.iter().map(|expr| expr.evaluate(batch)).try_fold(
-                    BooleanArray::new(BooleanBuffer::new_unset(num_rows), None),
-                    |result, expr| -> Result<BooleanArray> {
-                        let rhs = match expr? {
-                            ColumnarValue::Array(array) => {
+                let use_arrow_eq = !value.data_type().is_nested();
+                let mut found =

Review Comment:
   Thank you for your comments! I've split the vectorized eq optimization and early break into two separate PRs. The vectorized eq optimization is in #20528, and the early break PR will be submitted after #20528 is merged. I'll also address your suggestions (initialize first vector and BooleanBuffer::collect_bool optimization) in the follow-up PR.



-- 
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 dynamic IN list evaluation with vectorized Arrow eq kernel [datafusion]

Posted by "Dandandan (via GitHub)" <gi...@apache.org>.
Dandandan commented on code in PR #20428:
URL: https://github.com/apache/datafusion/pull/20428#discussion_r2831854746


##########
datafusion/physical-expr/src/expressions/in_list.rs:
##########
@@ -792,35 +805,40 @@ impl PhysicalExpr for InListExpr {
                                     })
                                     .collect::<BooleanArray>()
                             }
-                            ColumnarValue::Scalar(scalar) => {
-                                // Check if scalar is null once, before the loop
-                                if scalar.is_null() {
-                                    // If scalar is null, all comparisons return null
-                                    BooleanArray::from(vec![None; num_rows])
-                                } else {
-                                    // Convert scalar to 1-element array
-                                    let array = scalar.to_array()?;
-                                    let cmp = make_comparator(
-                                        value.as_ref(),
-                                        array.as_ref(),
-                                        SortOptions::default(),
-                                    )?;
-                                    // Compare each row of value with the single scalar element
-                                    (0..num_rows)
-                                        .map(|i| {
-                                            if value.is_null(i) {
-                                                None
-                                            } else {
-                                                Some(cmp(i, 0).is_eq())
-                                            }
-                                        })
-                                        .collect::<BooleanArray>()
-                                }
+                        }
+                        ColumnarValue::Scalar(scalar) => {
+                            if scalar.is_null() {
+                                // null compared to anything is null
+                                BooleanArray::new(
+                                    BooleanBuffer::new_unset(num_rows),
+                                    Some(NullBuffer::new_null(num_rows)),
+                                )
+                            } else if use_arrow_eq {
+                                // Vectorized scalar comparison
+                                let scalar_datum = scalar.to_scalar()?;
+                                arrow_eq(&value, &scalar_datum)?
+                            } else {
+                                // Row-by-row comparator for nested types
+                                let array = scalar.to_array()?;
+                                let cmp = make_comparator(
+                                    value.as_ref(),
+                                    array.as_ref(),
+                                    SortOptions::default(),
+                                )?;
+                                (0..num_rows)

Review Comment:
   BooleanBuffer::collect_bool + cloning the original null buffer is much faster.



-- 
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 dynamic IN list evaluation with vectorized Arrow eq kernel [datafusion]

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

   > Yes. Add 7 testcase to cover dynamic in path. Also add New criterion benchmarks (bench_dynamic_int32, bench_dynamic_utf8).
   
   Could you make a PR adding just the benchmarks first? That way we can merge that and then show a clear improvement on this PR


-- 
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 dynamic IN list evaluation with vectorized Arrow eq kernel [datafusion]

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

   It would also be nice to see these seemingly unrelated changes (use kernel and break early) as separate PRs


-- 
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 dynamic IN list evaluation with vectorized Arrow eq kernel [datafusion]

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

   > > Yes. Add 7 testcase to cover dynamic in path. Also add New criterion benchmarks (bench_dynamic_int32, bench_dynamic_utf8).
   > 
   > Could you make a PR adding just the benchmarks first? That way we can merge that and then show a clear improvement on this PR
   
   Thank you so much for your review!
   
   I’ve opened a separate PR (#20444) focused on adding the benchmarks first, and would greatly appreciate it if you could take a look at this code when you have some time.
   
   I will follow up with further optimizations separately after #20444 is merged.
   


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