Skip to content

Commit bfc012e

Browse files
authored
bench: Add IN list benchmarks for non-constant list expressions (#20444)
## 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. --> - Relates to #20427 . ## 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. --> The existing `in_list` benchmarks only cover the static filter path (constant literal lists), which uses HashSet lookup. There are no benchmarks for the dynamic evaluation path, triggered when the IN list contains non-constant expressions such as column references (e.g., `a IN (b, c, d)`). Adding these benchmarks establishes a baseline for measuring the impact upcoming optimizations to the dynamic path. (see #20428). ## 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. --> Add criterion benchmarks for the dynamic IN list evaluation path: - `bench_dynamic_int32`: Int32 column references, list sizes [3, 8, 28] × match rates [0%, 50%, 100%] × null rates [0%, 20%] - `bench_dynamic_utf8`: Utf8 column references, list sizes [3, 8, 28] × match rates [0%, 50%, 100%] ## 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)? --> Yes. The benchmarks compile and run correctly. No implementation code is changed. ## 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. -->
1 parent c1ad863 commit bfc012e

1 file changed

Lines changed: 166 additions & 0 deletions

File tree

datafusion/physical-expr/benches/in_list.rs

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use arrow::datatypes::{Field, Schema};
2323
use arrow::record_batch::RecordBatch;
2424
use criterion::{Criterion, criterion_group, criterion_main};
2525
use datafusion_common::ScalarValue;
26+
use datafusion_physical_expr::PhysicalExpr;
2627
use datafusion_physical_expr::expressions::{col, in_list, lit};
2728
use rand::distr::Alphanumeric;
2829
use rand::prelude::*;
@@ -50,7 +51,9 @@ fn random_string(rng: &mut StdRng, len: usize) -> String {
5051
}
5152

5253
const IN_LIST_LENGTHS: [usize; 4] = [3, 8, 28, 100];
54+
const LIST_WITH_COLUMNS_LENGTHS: [usize; 3] = [3, 8, 28];
5355
const NULL_PERCENTS: [f64; 2] = [0., 0.2];
56+
const MATCH_PERCENTS: [f64; 3] = [0.0, 0.5, 1.0];
5457
const STRING_LENGTHS: [usize; 3] = [3, 12, 100];
5558
const ARRAY_LENGTH: usize = 8192;
5659

@@ -219,6 +222,165 @@ fn bench_realistic_mixed_strings<A>(
219222
}
220223
}
221224

225+
/// Benchmarks the column-reference evaluation path (no static filter) by including
226+
/// a column reference in the IN list, which prevents static filter creation.
227+
///
228+
/// This simulates SQL like:
229+
/// ```sql
230+
/// CREATE TABLE t (a INT, b0 INT, b1 INT, b2 INT);
231+
/// SELECT * FROM t WHERE a IN (b0, b1, b2);
232+
/// ```
233+
///
234+
/// - `values`: the "needle" column (`a`)
235+
/// - `list_cols`: the "haystack" columns (`b0`, `b1`, …)
236+
fn do_bench_with_columns(
237+
c: &mut Criterion,
238+
name: &str,
239+
values: ArrayRef,
240+
list_cols: &[ArrayRef],
241+
) {
242+
let mut fields = vec![Field::new("a", values.data_type().clone(), true)];
243+
let mut columns: Vec<ArrayRef> = vec![values];
244+
245+
// Build list expressions: column refs (forces non-constant evaluation path)
246+
let schema_fields: Vec<Field> = list_cols
247+
.iter()
248+
.enumerate()
249+
.map(|(i, col_arr)| {
250+
let name = format!("b{i}");
251+
fields.push(Field::new(&name, col_arr.data_type().clone(), true));
252+
columns.push(Arc::clone(col_arr));
253+
Field::new(&name, col_arr.data_type().clone(), true)
254+
})
255+
.collect();
256+
257+
let schema = Schema::new(fields);
258+
let list_exprs: Vec<Arc<dyn PhysicalExpr>> = schema_fields
259+
.iter()
260+
.map(|f| col(f.name(), &schema).unwrap())
261+
.collect();
262+
263+
let expr = in_list(col("a", &schema).unwrap(), list_exprs, &false, &schema).unwrap();
264+
let batch = RecordBatch::try_new(Arc::new(schema), columns).unwrap();
265+
266+
c.bench_function(name, |b| {
267+
b.iter(|| black_box(expr.evaluate(black_box(&batch)).unwrap()))
268+
});
269+
}
270+
271+
/// Benchmarks the IN list path with column references for Int32 arrays.
272+
///
273+
/// Equivalent SQL:
274+
/// ```sql
275+
/// CREATE TABLE t (a INT, b0 INT, b1 INT, ...);
276+
/// SELECT * FROM t WHERE a IN (b0, b1, ...);
277+
/// ```
278+
fn bench_with_columns_int32(c: &mut Criterion) {
279+
let mut rng = StdRng::seed_from_u64(42);
280+
281+
for list_size in LIST_WITH_COLUMNS_LENGTHS {
282+
for match_percent in MATCH_PERCENTS {
283+
for null_percent in NULL_PERCENTS {
284+
// Generate the "needle" column
285+
let values: Int32Array = (0..ARRAY_LENGTH)
286+
.map(|_| {
287+
rng.random_bool(1.0 - null_percent)
288+
.then(|| rng.random_range(0..1000))
289+
})
290+
.collect();
291+
292+
// Generate list columns with controlled match rate
293+
let list_cols: Vec<ArrayRef> = (0..list_size)
294+
.map(|_| {
295+
let col: Int32Array = (0..ARRAY_LENGTH)
296+
.map(|row| {
297+
if rng.random_bool(1.0 - null_percent) {
298+
if rng.random_bool(match_percent) {
299+
// Copy from values to create a match
300+
if values.is_null(row) {
301+
Some(rng.random_range(0..1000))
302+
} else {
303+
Some(values.value(row))
304+
}
305+
} else {
306+
// Random value (unlikely to match)
307+
Some(rng.random_range(1000..2000))
308+
}
309+
} else {
310+
None
311+
}
312+
})
313+
.collect();
314+
Arc::new(col) as ArrayRef
315+
})
316+
.collect();
317+
318+
do_bench_with_columns(
319+
c,
320+
&format!(
321+
"in_list_cols/Int32/list={}/match={}%/nulls={}%",
322+
list_size,
323+
(match_percent * 100.0) as u32,
324+
(null_percent * 100.0) as u32
325+
),
326+
Arc::new(values),
327+
&list_cols,
328+
);
329+
}
330+
}
331+
}
332+
}
333+
334+
/// Benchmarks the IN list path with column references for Utf8 arrays.
335+
///
336+
/// Equivalent SQL:
337+
/// ```sql
338+
/// CREATE TABLE t (a VARCHAR, b0 VARCHAR, b1 VARCHAR, ...);
339+
/// SELECT * FROM t WHERE a IN (b0, b1, ...);
340+
/// ```
341+
fn bench_with_columns_utf8(c: &mut Criterion) {
342+
let mut rng = StdRng::seed_from_u64(99);
343+
344+
for list_size in LIST_WITH_COLUMNS_LENGTHS {
345+
for match_percent in MATCH_PERCENTS {
346+
// Generate the "needle" column
347+
let value_strings: Vec<Option<String>> = (0..ARRAY_LENGTH)
348+
.map(|_| rng.random_bool(0.8).then(|| random_string(&mut rng, 12)))
349+
.collect();
350+
let values: StringArray =
351+
value_strings.iter().map(|s| s.as_deref()).collect();
352+
353+
// Generate list columns with controlled match rate
354+
let list_cols: Vec<ArrayRef> = (0..list_size)
355+
.map(|_| {
356+
let col: StringArray = (0..ARRAY_LENGTH)
357+
.map(|row| {
358+
if rng.random_bool(match_percent) {
359+
// Copy from values to create a match
360+
value_strings[row].as_deref()
361+
} else {
362+
Some("no_match_value_xyz")
363+
}
364+
})
365+
.collect();
366+
Arc::new(col) as ArrayRef
367+
})
368+
.collect();
369+
370+
do_bench_with_columns(
371+
c,
372+
&format!(
373+
"in_list_cols/Utf8/list={}/match={}%",
374+
list_size,
375+
(match_percent * 100.0) as u32,
376+
),
377+
Arc::new(values),
378+
&list_cols,
379+
);
380+
}
381+
}
382+
}
383+
222384
/// Entry point: registers in_list benchmarks for string and numeric array types.
223385
fn criterion_benchmark(c: &mut Criterion) {
224386
let mut rng = StdRng::seed_from_u64(120320);
@@ -266,6 +428,10 @@ fn criterion_benchmark(c: &mut Criterion) {
266428
|rng| rng.random(),
267429
|v| ScalarValue::TimestampNanosecond(Some(v), None),
268430
);
431+
432+
// Column-reference path benchmarks (non-constant list expressions)
433+
bench_with_columns_int32(c);
434+
bench_with_columns_utf8(c);
269435
}
270436

271437
criterion_group! {

0 commit comments

Comments
 (0)