You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2026/09/09 17:43:07 UTC
(datafusion) branch gh-readonly-queue/main/pr-24976-9f21155c48faac3f1658853ad6a55bd314a322ed created (now 4084d4c57b)
This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a change to branch gh-readonly-queue/main/pr-24976-9f21155c48faac3f1658853ad6a55bd314a322ed
in repository https://gitbox.apache.org/repos/asf/datafusion.git
at 4084d4c57b fix(physical-plan): report Final emission for LeftSemi hash and neste… (#24976)
This branch includes the following new commits:
new ba57f8a5db Support INSERT OVERWRITE for MemTable (#24969)
new 790b110df7 bench(predicate_eval): add expensive-first and wide-column shapes (#25032)
new cc61eeaa5e perf: emit unmatched build rows in batch_size chunks in HashJoinExec (#25028)
new 9f21155c48 perf: reduce invariant-check code generation (#25073)
new 4084d4c57b fix(physical-plan): report Final emission for LeftSemi hash and neste… (#24976)
The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org
(datafusion) 04/05: perf: reduce invariant-check code generation (#25073)
Posted by gi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-24976-9f21155c48faac3f1658853ad6a55bd314a322ed
in repository https://gitbox.apache.org/repos/asf/datafusion.git
commit 9f21155c48faac3f1658853ad6a55bd314a322ed
Author: Kumar Ujjawal <uj...@gmail.com>
AuthorDate: Wed Sep 9 14:19:15 2026 +0000
perf: reduce invariant-check code generation (#25073)
## Which issue does this PR close?
- Part of #13815.
## Rationale for this change
`check_default_invariants` is generic over each `ExecutionPlan`
implementation. The compiler therefore generated many copies of its
dynamic-expression and input-distribution validation logic.
These checks depend only on erased values such as the plan name,
children, and dynamic expressions. Sharing the heavier validation paths
reduces generated code without changing invariant semantics or the
public API.
Local release measurements against `upstream/main` at `92746a993`:
| Measurement | `upstream/main` | This PR | Change |
|---|---:|---:|---:|
| Targeted invariant-check LLVM IR lines | 68,856 | 47,082 | -31.6% |
| Total physical-plan LLVM IR lines | 2,560,976 | 2,513,101 | -1.87% |
| Release `rlib` size | 56,223,208 bytes | 55,689,856 bytes | -0.95% |
| Linked `partial_ordering` benchmark binary | 5,882,960 bytes |
5,882,960 bytes | No change |
The linked benchmark binary was unchanged because its linker removed
unused code. This PR does not claim a measured end-to-end WASM binary
reduction.
The added Criterion benchmark showed no runtime regression:
| Case | `upstream/main` | This PR |
|---|---:|---:|
| Leaf plan | 13.016–13.030 ns | 12.939–12.951 ns |
| Four-child plan | 150.24–151.02 ns | 142.75–145.68 ns |
## What changes are included in this PR?
- Move dynamic-expression ID validation behind a non-generic helper.
- Make input-distribution invariant validation non-generic.
- Keep the inexpensive per-plan length checks inline.
- Skip dynamic-expression validation when a plan produces no dynamic
expressions.
- Preserve the public `check_default_invariants` signature and existing
error behavior.
- Add unit tests for malformed vectors, dynamic-expression IDs,
co-partitioning requirements, and trait-object callers.
- Add a Criterion benchmark for leaf and four-child plans.
## What is the testing strategy for this PR?
The new unit tests cover:
- incorrect invariant-vector lengths and their error messages;
- missing and duplicate dynamic-expression IDs;
- malformed and valid co-partitioning requirements;
- calls through concrete plans and `dyn ExecutionPlan`.
The following checks pass:
```shell
cargo fmt --all
cargo clippy --all-targets --all-features -- -D warnings
RUST_BACKTRACE=1 cargo test --profile ci \
--exclude datafusion-examples \
--exclude datafusion-benchmarks \
--exclude datafusion-cli \
--workspace --lib --tests --bins \
--features avro,json,backtrace,extended_tests,recursive_protection,parquet_encryption
```
The code-size comparison and runtime measurements use the new
`invariant_check` Criterion benchmark and release builds of
`datafusion-physical-plan`.
## Are there any user-facing changes?
No.
---
datafusion/physical-plan/Cargo.toml | 4 +
.../physical-plan/benches/invariant_check.rs | 61 ++++++
.../physical-plan/src/distribution_requirements.rs | 41 +++-
datafusion/physical-plan/src/execution_plan.rs | 234 ++++++++++++++++++---
4 files changed, 306 insertions(+), 34 deletions(-)
diff --git a/datafusion/physical-plan/Cargo.toml b/datafusion/physical-plan/Cargo.toml
index 0aa22653b4..534cea8ea9 100644
--- a/datafusion/physical-plan/Cargo.toml
+++ b/datafusion/physical-plan/Cargo.toml
@@ -109,6 +109,10 @@ tokio = { workspace = true, features = [
harness = false
name = "partial_ordering"
+[[bench]]
+harness = false
+name = "invariant_check"
+
[[bench]]
harness = false
name = "union_schema"
diff --git a/datafusion/physical-plan/benches/invariant_check.rs b/datafusion/physical-plan/benches/invariant_check.rs
new file mode 100644
index 0000000000..65d9c3f23b
--- /dev/null
+++ b/datafusion/physical-plan/benches/invariant_check.rs
@@ -0,0 +1,61 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::hint::black_box;
+use std::sync::Arc;
+
+use arrow::datatypes::{Schema, SchemaRef};
+use criterion::{Criterion, criterion_group, criterion_main};
+use datafusion_physical_plan::empty::EmptyExec;
+use datafusion_physical_plan::execution_plan::{
+ ExecutionPlan, InvariantLevel, check_default_invariants,
+};
+use datafusion_physical_plan::union::UnionExec;
+
+fn empty_exec(schema: &SchemaRef) -> Arc<dyn ExecutionPlan> {
+ Arc::new(EmptyExec::new(Arc::clone(schema)))
+}
+
+fn bench_invariant_checks(c: &mut Criterion) {
+ let schema = Arc::new(Schema::empty());
+ let leaf = EmptyExec::new(Arc::clone(&schema));
+ let union =
+ UnionExec::try_new((0..4).map(|_| empty_exec(&schema)).collect::<Vec<_>>())
+ .unwrap();
+
+ let mut group = c.benchmark_group("check_default_invariants");
+ group.bench_function("leaf", |b| {
+ b.iter(|| {
+ black_box(check_default_invariants(
+ black_box(&leaf),
+ InvariantLevel::Always,
+ ))
+ });
+ });
+ group.bench_function("four_children", |b| {
+ b.iter(|| {
+ black_box(check_default_invariants(
+ black_box(union.as_ref()),
+ InvariantLevel::Always,
+ ))
+ });
+ });
+ group.finish();
+}
+
+criterion_group!(benches, bench_invariant_checks);
+criterion_main!(benches);
diff --git a/datafusion/physical-plan/src/distribution_requirements.rs b/datafusion/physical-plan/src/distribution_requirements.rs
index 119b2357c0..f7a17ddc7b 100644
--- a/datafusion/physical-plan/src/distribution_requirements.rs
+++ b/datafusion/physical-plan/src/distribution_requirements.rs
@@ -17,6 +17,8 @@
//! Input distribution requirements for physical execution plans.
+use std::sync::Arc;
+
use datafusion_common::{Result, internal_err};
use datafusion_physical_expr::{Distribution, Partitioning, PartitioningSatisfaction};
@@ -205,25 +207,46 @@ impl InputDistributionRequirements {
}
/// Validate the requirements against a plan's children.
- pub(crate) fn check_invariants<P: ExecutionPlan + ?Sized>(
+ #[inline]
+ pub(crate) fn check_invariants(
&self,
- plan: &P,
+ plan_name: &str,
+ children: Vec<&Arc<dyn ExecutionPlan>>,
check: InvariantLevel,
) -> Result<()> {
- let children = plan.children();
- self.validate_shape(plan.name(), children.len())?;
+ let children_len = children.len();
+ if self.children.len() != children_len {
+ return self.validate_shape(plan_name, children_len);
+ }
+
+ if let Some(co_partitioned) = &self.co_partitioned {
+ self.validate_shape(plan_name, children_len)?;
+ if matches!(check, InvariantLevel::Executable) {
+ return self.check_co_partitioning_invariant(
+ plan_name,
+ co_partitioned,
+ children,
+ );
+ }
+ }
+ Ok(())
+ }
+
+ fn check_co_partitioning_invariant(
+ &self,
+ plan_name: &str,
+ co_partitioned: &[usize],
+ children: Vec<&Arc<dyn ExecutionPlan>>,
+ ) -> Result<()> {
let children = children
.into_iter()
.map(|child| child.as_ref())
.collect::<Vec<_>>();
- if matches!(check, InvariantLevel::Executable)
- && let Some(co_partitioned) = &self.co_partitioned
- && !self.co_partitioning_satisfied(co_partitioned, &children)
- {
+ if !self.co_partitioning_satisfied(co_partitioned, &children) {
return internal_err!(
"{} requires children {:?} to be co-partitioned",
- plan.name(),
+ plan_name,
co_partitioned
);
}
diff --git a/datafusion/physical-plan/src/execution_plan.rs b/datafusion/physical-plan/src/execution_plan.rs
index 6cb3eeef8c..978059b9fa 100644
--- a/datafusion/physical-plan/src/execution_plan.rs
+++ b/datafusion/physical-plan/src/execution_plan.rs
@@ -1601,6 +1601,39 @@ impl PlanProperties {
}
}
+#[derive(Debug)]
+struct DefaultInvariantChecker<'a> {
+ plan_name: &'a str,
+}
+
+impl<'a> DefaultInvariantChecker<'a> {
+ fn new(plan_name: &'a str) -> Self {
+ Self { plan_name }
+ }
+
+ /// All dynamic expressions must have an expression id.
+ fn check_dynamic_expression_invariants(
+ &self,
+ dynamic_expressions: Vec<Arc<dyn PhysicalExpr>>,
+ ) -> Result<()> {
+ let mut produced_ids = HashSet::new();
+ for expr in dynamic_expressions {
+ let Some(expression_id) = expr.expression_id() else {
+ return internal_err!(
+ "{}::dynamic_expressions_produced returned an expression without an expression ID",
+ self.plan_name
+ );
+ };
+ assert_or_internal_err!(
+ produced_ids.insert(expression_id),
+ "{}::dynamic_expressions_produced returned duplicate expression ID {expression_id}",
+ self.plan_name
+ );
+ }
+ Ok(())
+ }
+}
+
macro_rules! check_len {
($target:expr, $func_name:ident, $expected_len:expr) => {
let actual_len = $target.$func_name().len();
@@ -1616,27 +1649,6 @@ macro_rules! check_len {
};
}
-/// All dynamic expressions must have an expression id.
-fn check_dynamic_expression_invariants<P: ExecutionPlan + ?Sized>(
- plan: &P,
-) -> Result<()> {
- let mut produced_ids = HashSet::new();
- for expr in plan.dynamic_expressions_produced() {
- let Some(expression_id) = expr.expression_id() else {
- return internal_err!(
- "{}::dynamic_expressions_produced returned an expression without an expression ID",
- plan.name()
- );
- };
- assert_or_internal_err!(
- produced_ids.insert(expression_id),
- "{}::dynamic_expressions_produced returned duplicate expression ID {expression_id}",
- plan.name()
- );
- }
- Ok(())
-}
-
/// Checks a set of invariants that apply to all ExecutionPlan implementations.
/// Returns an error if the given node does not conform.
pub fn check_default_invariants<P: ExecutionPlan + ?Sized>(
@@ -1648,9 +1660,19 @@ pub fn check_default_invariants<P: ExecutionPlan + ?Sized>(
check_len!(plan, maintains_input_order, children_len);
check_len!(plan, required_input_ordering, children_len);
check_len!(plan, benefits_from_input_partitioning, children_len);
- plan.input_distribution_requirements()
- .check_invariants(plan, check)?;
- check_dynamic_expression_invariants(plan)?;
+
+ let input_distribution_requirements = plan.input_distribution_requirements();
+ let children = plan.children();
+ let checker = DefaultInvariantChecker::new(plan.name());
+ input_distribution_requirements.check_invariants(
+ checker.plan_name,
+ children,
+ check,
+ )?;
+ let dynamic_expressions = plan.dynamic_expressions_produced();
+ if !dynamic_expressions.is_empty() {
+ checker.check_dynamic_expression_invariants(dynamic_expressions)?;
+ }
Ok(())
}
@@ -2094,16 +2116,31 @@ mod tests {
#[derive(Debug)]
pub struct EmptyExec {
+ children: Vec<Arc<dyn ExecutionPlan>>,
dynamic_expressions: Vec<Arc<dyn PhysicalExpr>>,
+ maintains_input_order_len: Option<usize>,
+ required_input_ordering_len: Option<usize>,
+ benefits_from_input_partitioning_len: Option<usize>,
+ input_distribution_requirements: Option<InputDistributionRequirements>,
}
impl EmptyExec {
pub fn new(_schema: SchemaRef) -> Self {
Self {
+ children: vec![],
dynamic_expressions: vec![],
+ maintains_input_order_len: None,
+ required_input_ordering_len: None,
+ benefits_from_input_partitioning_len: None,
+ input_distribution_requirements: None,
}
}
+ fn with_child(mut self, child: Arc<dyn ExecutionPlan>) -> Self {
+ self.children.push(child);
+ self
+ }
+
fn with_dynamic_expressions(
mut self,
dynamic_expressions: Vec<Arc<dyn PhysicalExpr>>,
@@ -2111,6 +2148,37 @@ mod tests {
self.dynamic_expressions = dynamic_expressions;
self
}
+
+ fn with_maintains_input_order_len(mut self, len: usize) -> Self {
+ self.maintains_input_order_len = Some(len);
+ self
+ }
+
+ fn with_required_input_ordering_len(mut self, len: usize) -> Self {
+ self.required_input_ordering_len = Some(len);
+ self
+ }
+
+ fn with_benefits_from_input_partitioning_len(mut self, len: usize) -> Self {
+ self.benefits_from_input_partitioning_len = Some(len);
+ self
+ }
+
+ fn with_input_distribution_requirements_len(mut self, len: usize) -> Self {
+ self.input_distribution_requirements =
+ Some(InputDistributionRequirements::new(
+ vec![Distribution::UnspecifiedDistribution; len],
+ ));
+ self
+ }
+
+ fn with_input_distribution_requirements(
+ mut self,
+ requirements: InputDistributionRequirements,
+ ) -> Self {
+ self.input_distribution_requirements = Some(requirements);
+ self
+ }
}
impl DisplayAs for EmptyExec {
@@ -2133,7 +2201,42 @@ mod tests {
}
fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {
- vec![]
+ self.children.iter().collect()
+ }
+
+ fn maintains_input_order(&self) -> Vec<bool> {
+ vec![
+ false;
+ self.maintains_input_order_len
+ .unwrap_or(self.children.len())
+ ]
+ }
+
+ fn required_input_ordering(&self) -> Vec<Option<OrderingRequirements>> {
+ vec![
+ None;
+ self.required_input_ordering_len
+ .unwrap_or(self.children.len())
+ ]
+ }
+
+ fn benefits_from_input_partitioning(&self) -> Vec<bool> {
+ vec![
+ true;
+ self.benefits_from_input_partitioning_len
+ .unwrap_or(self.children.len())
+ ]
+ }
+
+ fn input_distribution_requirements(&self) -> InputDistributionRequirements {
+ self.input_distribution_requirements
+ .clone()
+ .unwrap_or_else(|| {
+ InputDistributionRequirements::new(vec![
+ Distribution::UnspecifiedDistribution;
+ self.children.len()
+ ])
+ })
}
fn replace_children(
@@ -2208,6 +2311,87 @@ mod tests {
Ok(())
}
+ #[test]
+ fn test_default_invariant_lengths() -> Result<()> {
+ let schema = Arc::new(Schema::empty());
+ let child: Arc<dyn ExecutionPlan> = Arc::new(EmptyExec::new(Arc::clone(&schema)));
+ let make_plan =
+ || EmptyExec::new(Arc::clone(&schema)).with_child(Arc::clone(&child));
+
+ check_default_invariants(&make_plan(), InvariantLevel::Always)?;
+
+ let cases = [
+ (
+ make_plan().with_maintains_input_order_len(0),
+ "Internal error: Assertion failed: actual_len == children_len (left: 0, right: 1): EmptyExec::maintains_input_order returned Vec with incorrect size: 0 != 1",
+ ),
+ (
+ make_plan().with_required_input_ordering_len(0),
+ "Internal error: Assertion failed: actual_len == children_len (left: 0, right: 1): EmptyExec::required_input_ordering returned Vec with incorrect size: 0 != 1",
+ ),
+ (
+ make_plan().with_benefits_from_input_partitioning_len(0),
+ "Internal error: Assertion failed: actual_len == children_len (left: 0, right: 1): EmptyExec::benefits_from_input_partitioning returned Vec with incorrect size: 0 != 1",
+ ),
+ (
+ make_plan().with_input_distribution_requirements_len(0),
+ "Internal error: EmptyExec::input_distribution_requirements returned incorrect child count: 0 != 1",
+ ),
+ ];
+
+ for (plan, expected) in cases {
+ let error = check_default_invariants(&plan, InvariantLevel::Always)
+ .unwrap_err()
+ .strip_backtrace();
+ assert!(error.starts_with(expected), "{error}");
+ }
+
+ let invalid_co_partitioning = make_plan()
+ .with_child(child)
+ .with_input_distribution_requirements(
+ InputDistributionRequirements::co_partitioned(vec![
+ Distribution::UnspecifiedDistribution,
+ Distribution::UnspecifiedDistribution,
+ ]),
+ );
+ let error =
+ check_default_invariants(&invalid_co_partitioning, InvariantLevel::Always)
+ .unwrap_err()
+ .strip_backtrace();
+ assert!(
+ error.starts_with(
+ "Internal error: EmptyExec has invalid co-partitioning requirement: child 0 has unspecified distribution"
+ ),
+ "{error}"
+ );
+
+ let left: Arc<dyn ExecutionPlan> =
+ Arc::new(crate::empty::EmptyExec::new(Arc::clone(&schema)));
+ let right: Arc<dyn ExecutionPlan> =
+ Arc::new(crate::empty::EmptyExec::new(Arc::clone(&schema)));
+ let valid_co_partitioning = EmptyExec::new(schema)
+ .with_child(left)
+ .with_child(right)
+ .with_input_distribution_requirements(
+ InputDistributionRequirements::co_partitioned(vec![
+ Distribution::SinglePartition,
+ Distribution::SinglePartition,
+ ]),
+ );
+ check_default_invariants(&valid_co_partitioning, InvariantLevel::Executable)?;
+
+ Ok(())
+ }
+
+ #[test]
+ fn test_default_invariants_accept_trait_object() -> Result<()> {
+ let plan = EmptyExec::new(Arc::new(Schema::empty()));
+ let plan: &dyn ExecutionPlan = &plan;
+
+ check_default_invariants(plan, InvariantLevel::Always)?;
+ plan.check_invariants(InvariantLevel::Always)
+ }
+
#[derive(Debug)]
pub struct RenamedEmptyExec;
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org
(datafusion) 01/05: Support INSERT OVERWRITE for MemTable (#24969)
Posted by gi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-24976-9f21155c48faac3f1658853ad6a55bd314a322ed
in repository https://gitbox.apache.org/repos/asf/datafusion.git
commit ba57f8a5db742fa40503ba649796ca870c4d601e
Author: Artem Osipov <59...@users.noreply.github.com>
AuthorDate: Wed Sep 9 10:14:55 2026 +0000
Support INSERT OVERWRITE for MemTable (#24969)
## Which issue does this PR close?
- Part of #19617.
## Rationale for this change
DataFusion parses and plans `INSERT OVERWRITE`, but `MemTable` rejects
every insert operation except append. Users therefore cannot replace the
contents of an in-memory table with the standard overwrite operation.
## What changes are included in this PR?
- allow `InsertOp::Overwrite` in `MemTable::insert_into`
- let `MemSink` replace each target partition after the input stream
completes successfully
- continue rejecting the distinct `InsertOp::Replace` operation
## What is the testing strategy for this PR?
Three unit tests verify that overwrite replaces existing rows,
distributes multiple replacement batches across multiple target
partitions, and clears the table for empty input. A separate test
confirms that `InsertOp::Replace` remains unsupported.
Validated with:
- `cargo test -p datafusion --lib test_insert_overwrite -- --nocapture`
- `cargo clippy -p datafusion -p datafusion-datasource -p
datafusion-catalog --all-targets --all-features -- -D warnings`
- `cargo fmt --all -- --check`
## Are there any user-facing changes?
Yes. `INSERT OVERWRITE` now replaces all existing data in a `MemTable`.
There are no breaking public API changes.
---
datafusion/catalog/src/memory/table.rs | 5 +-
datafusion/core/src/datasource/memory_test.rs | 130 +++++++++++++++++++++++++-
datafusion/datasource/src/memory.rs | 24 ++++-
3 files changed, 151 insertions(+), 8 deletions(-)
diff --git a/datafusion/catalog/src/memory/table.rs b/datafusion/catalog/src/memory/table.rs
index d817aa8b77..1cc7287c32 100644
--- a/datafusion/catalog/src/memory/table.rs
+++ b/datafusion/catalog/src/memory/table.rs
@@ -349,10 +349,11 @@ impl MemTable {
self.schema()
.logically_equivalent_names_and_types(&input.schema())?;
- if insert_op != InsertOp::Append {
+ if insert_op == InsertOp::Replace {
return not_impl_err!("{insert_op} not implemented for MemoryTable yet");
}
- let sink = MemSink::try_new(self.batches.clone(), Arc::clone(&self.schema))?;
+ let sink = MemSink::try_new(self.batches.clone(), Arc::clone(&self.schema))?
+ .with_overwrite(insert_op == InsertOp::Overwrite);
Ok(Arc::new(DataSinkExec::new(input, Arc::new(sink), None)))
}
diff --git a/datafusion/core/src/datasource/memory_test.rs b/datafusion/core/src/datasource/memory_test.rs
index cc5ad539da..033a8036f5 100644
--- a/datafusion/core/src/datasource/memory_test.rs
+++ b/datafusion/core/src/datasource/memory_test.rs
@@ -23,7 +23,7 @@ mod tests {
use crate::physical_plan::collect;
use crate::prelude::SessionContext;
use arrow::array::{AsArray, Int32Array};
- use arrow::datatypes::{DataType, Field, Schema, UInt64Type};
+ use arrow::datatypes::{DataType, Field, Int32Type, Schema, UInt64Type};
use arrow::error::ArrowError;
use arrow::record_batch::RecordBatch;
use arrow_schema::SchemaRef;
@@ -318,6 +318,16 @@ mod tests {
schema: SchemaRef,
initial_data: Vec<Vec<RecordBatch>>,
inserted_data: Vec<Vec<RecordBatch>>,
+ ) -> Result<Vec<Vec<RecordBatch>>> {
+ experiment_with_insert_op(schema, initial_data, inserted_data, InsertOp::Append)
+ .await
+ }
+
+ async fn experiment_with_insert_op(
+ schema: SchemaRef,
+ initial_data: Vec<Vec<RecordBatch>>,
+ inserted_data: Vec<Vec<RecordBatch>>,
+ insert_op: InsertOp,
) -> Result<Vec<Vec<RecordBatch>>> {
let expected_count: u64 = inserted_data
.iter()
@@ -339,7 +349,7 @@ mod tests {
let scan_plan = LogicalPlanBuilder::scan("source", source, None)?.build()?;
// Create an insert plan to insert the source data into the initial table
let insert_into_table =
- LogicalPlanBuilder::insert_into(scan_plan, "t", target, InsertOp::Append)?
+ LogicalPlanBuilder::insert_into(scan_plan, "t", target, insert_op)?
.build()?;
// Create a physical plan from the insert plan
let plan = session_ctx
@@ -479,6 +489,122 @@ mod tests {
Ok(())
}
+ #[tokio::test]
+ async fn test_insert_overwrite_replaces_existing_data() -> Result<()> {
+ let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)]));
+ let initial_batch = RecordBatch::try_new(
+ Arc::clone(&schema),
+ vec![Arc::new(Int32Array::from(vec![1, 2, 3]))],
+ )?;
+ let replacement_batch = RecordBatch::try_new(
+ Arc::clone(&schema),
+ vec![Arc::new(Int32Array::from(vec![4, 5]))],
+ )?;
+
+ let resulting_data = experiment_with_insert_op(
+ schema,
+ vec![vec![initial_batch]],
+ vec![vec![replacement_batch]],
+ InsertOp::Overwrite,
+ )
+ .await?;
+
+ assert_eq!(resulting_data[0].len(), 1);
+ assert_eq!(
+ resulting_data[0][0]
+ .column(0)
+ .as_primitive::<Int32Type>()
+ .values(),
+ &[4, 5]
+ );
+ Ok(())
+ }
+
+ #[tokio::test]
+ async fn test_insert_overwrite_replaces_multiple_partitions() -> Result<()> {
+ let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)]));
+ let batch = |values| {
+ RecordBatch::try_new(
+ Arc::clone(&schema),
+ vec![Arc::new(Int32Array::from(values))],
+ )
+ };
+
+ let resulting_data = experiment_with_insert_op(
+ Arc::clone(&schema),
+ vec![vec![batch(vec![1])?], vec![batch(vec![2])?]],
+ vec![vec![
+ batch(vec![10])?,
+ batch(vec![20])?,
+ batch(vec![30])?,
+ batch(vec![40])?,
+ ]],
+ InsertOp::Overwrite,
+ )
+ .await?;
+
+ assert_eq!(resulting_data.len(), 2);
+ for (partition, expected) in resulting_data.iter().zip([[10, 30], [20, 40]]) {
+ let actual = partition
+ .iter()
+ .flat_map(|batch| {
+ batch
+ .column(0)
+ .as_primitive::<Int32Type>()
+ .values()
+ .iter()
+ .copied()
+ })
+ .collect::<Vec<_>>();
+ assert_eq!(actual, expected);
+ }
+ Ok(())
+ }
+
+ #[tokio::test]
+ async fn test_insert_overwrite_with_empty_input_clears_table() -> Result<()> {
+ let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)]));
+ let initial_batch = RecordBatch::try_new(
+ Arc::clone(&schema),
+ vec![Arc::new(Int32Array::from(vec![1, 2, 3]))],
+ )?;
+
+ let resulting_data = experiment_with_insert_op(
+ schema,
+ vec![vec![initial_batch]],
+ vec![vec![]],
+ InsertOp::Overwrite,
+ )
+ .await?;
+
+ assert!(resulting_data[0].is_empty());
+ Ok(())
+ }
+
+ #[tokio::test]
+ async fn test_insert_replace_remains_unsupported() -> Result<()> {
+ let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Int32, false)]));
+ let batch = RecordBatch::try_new(
+ Arc::clone(&schema),
+ vec![Arc::new(Int32Array::from(vec![1, 2, 3]))],
+ )?;
+
+ let error = experiment_with_insert_op(
+ schema,
+ vec![vec![batch.clone()]],
+ vec![vec![batch]],
+ InsertOp::Replace,
+ )
+ .await
+ .unwrap_err();
+
+ assert_eq!(
+ error.strip_backtrace(),
+ "This feature is not implemented: Replace Into not implemented for MemoryTable yet"
+ );
+ Ok(())
+ }
+
// Test inserting a batch into a MemTable without any partitions
#[tokio::test]
async fn test_insert_into_zero_partition() -> Result<()> {
diff --git a/datafusion/datasource/src/memory.rs b/datafusion/datasource/src/memory.rs
index 4c79cf4a98..0d0d02a37f 100644
--- a/datafusion/datasource/src/memory.rs
+++ b/datafusion/datasource/src/memory.rs
@@ -920,6 +920,7 @@ pub struct MemSink {
/// Target locations for writing data
batches: Vec<PartitionData>,
schema: SchemaRef,
+ overwrite: bool,
}
impl Debug for MemSink {
@@ -953,7 +954,17 @@ impl MemSink {
if batches.is_empty() {
return plan_err!("Cannot insert into MemTable with zero partitions");
}
- Ok(Self { batches, schema })
+ Ok(Self {
+ batches,
+ schema,
+ overwrite: false,
+ })
+ }
+
+ /// Configures whether writes replace the existing data instead of appending to it.
+ pub fn with_overwrite(mut self, overwrite: bool) -> Self {
+ self.overwrite = overwrite;
+ self
}
}
@@ -981,10 +992,15 @@ impl DataSink for MemSink {
i = (i + 1) % num_partitions;
}
- // write the outputs into the batches
+ // Modify the table only after the input stream has completed successfully.
for (target, mut batches) in self.batches.iter().zip(new_batches) {
- // Append all the new batches in one go to minimize locking overhead
- target.write().await.append(&mut batches);
+ let mut target = target.write().await;
+ if self.overwrite {
+ *target = batches;
+ } else {
+ // Append all the new batches in one go to minimize locking overhead
+ target.append(&mut batches);
+ }
}
Ok(row_count as u64)
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org
(datafusion) 02/05: bench(predicate_eval): add expensive-first and wide-column shapes (#25032)
Posted by gi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-24976-9f21155c48faac3f1658853ad6a55bd314a322ed
in repository https://gitbox.apache.org/repos/asf/datafusion.git
commit 790b110df7e02ccfdcf152a49e18d87622aa982d
Author: Adrian Garcia Badaracco <17...@users.noreply.github.com>
AuthorDate: Wed Sep 9 12:28:00 2026 +0000
bench(predicate_eval): add expensive-first and wide-column shapes (#25032)
## Which issue does this PR close?
- Part of #22883
- Related to #11262
- Prequel split out of #22698: these are the benchmark-suite additions
from that PR, carried on their own so they can land (and be run)
independently of the adaptive-filter work.
## Rationale for this change
The `predicate_eval` suite (the conjunctive-filter microbenchmarks added
for adaptive predicate ordering in #11262) was missing a few shapes that
distinguish evaluation strategies for AND chains, and it had no result
checking at all:
1. **An expensive-selective conjunct written *before* a
cheap-unselective one.** The suite already had the reverse (`costsel`
q03: cheap `c0 < 90` at ~90% first, expensive `regexp_like(s, 'rare')`
at ~0.1% second). Without the mirror shape there is no case in the suite
where the as-written order is already the optimal one, so the suite
cannot tell "reordered well" apart from "reordered at all". It also
exercises a distinct code path: measuring a *cheap* conjunct on the
small survivor batch left after pre-selection by an expensive one,
rather than measuring an expensive conjunct on a full batch.
2. **A many-column table.** Every existing `cardinality` query runs over
the 16-column `ints` dataset, so the cost of materializing a filtered
batch per level is small and roughly constant across the suite.
Re-running the k = 8 predicate over a 64-column table separates the cost
of *evaluating* the conjuncts from the cost of *filtering the batch* at
each level, which is what per-level batch filtering actually pays for on
wide inputs.
3. **No result validation.** The suite's template had no `result`
directive, so `--result-mode validate` verified nothing on it. A
reordering under test must not change *which* rows survive the filter,
and every query here is a `count(*)` whose value is fixed by the
generated data — a checked-in expected count is therefore a free
correctness check on top of the timing.
4. **Nullable conjuncts.** Every predicate in the suite was
non-nullable, so the suite never exercised the one case where writing
the selective conjunct first buys nothing: `BinaryExpr`'s AND
pre-selection bails out when the left-hand boolean array contains any
NULL.
5. **Drift that a warm-up cannot absorb.** The existing `drift` dataset
flipped which conjunct is selective late enough that a one-shot warm-up
already lands on the post-flip order and is right for most of the scan.
There was no shape where a decide-once strategy is wrong for a large
part of the scan, and none at all where the *right* order differs
between concurrently running partitions rather than over time.
## What changes are included in this PR?
Everything is under `benchmarks/` (plus the suite's own docs); there are
no Rust changes, and the suite discovers new `.benchmark` files
automatically.
- **Result validation.** The shared template gains `result
sql_benchmarks/predicate_eval/results/${NAME}.csv`, and every
benchmark's expected `count(*)` is checked in under
`predicate_eval/results/`, so `--result-mode validate` now checks that a
reordering under test still returns the same rows. The counts were
persisted at the suite defaults (`PRED_ROWS=1000000`, `PRED_FILL=30`),
so validation assumes those; the `scale` and `width` subgroups pin their
own values as template parameters (which win over the environment) and
validate at any setting. The template's old `count(*) > 0` assert is
dropped as redundant with this.
- **`costsel` q04** — the mirror of q03: expensive-selective
`regexp_like(s, 'rare')` written before cheap-unselective `c0 < 90`, so
the as-written order is already the best one. q03/q04 bracket a
reorderer when cost and selectivity point the same way.
- **`cardinality` q34 and `load/ints_wide.sql`** — q32's k = 8 predicate
over a 64-column table. `c0..c15` reuse `ints.sql`'s multipliers (all
coprime to 100), so the hidden selectivities are identical to q32's and
only the batch width changes.
- **`nulls` subgroup (q90, q91).** `ints.sql` gains `c_sel`, NULL on 10%
of rows and uniform on `[0,100)` elsewhere, so `c_sel < 5` is true on
exactly 4% of rows. `check_short_circuit` returns
`ShortCircuitStrategy::None` as soon as the left-hand boolean array has
`null_count() > 0`, so the nullable selective conjunct gates nothing
wherever it is written — q90 writes it first, q91 last. No `ints` query
references `c_sel`, so their counts are unchanged.
- **`drift` down to three cases.** q80 keeps the in-memory dataset, with
the flip moved to 2% of the rows so it lands inside the pooled 8-batch
warm-up (~65k rows at the default size). The mirrored-order twin was
dropped as the same measurement. q82 and q83 share
`load/drift_split.sql`: 16 Parquet files of `PRED_ROWS / 16` rows in a
gitignored `predicate_eval/scratch/`, `f00..f07` with `a_sel = 0`
selective (~0.1%) against `b_sel = 0` at ~50% and `f08..f15` the mirror.
`FileGroup::split_files` sorts a group's files by path, so
`target_partitions = 1` (q82) reads them in order and meets the flip
halfway through one stream, and `= 16` (q83) gives every stream one
whole file and one fixed profile — the case a decide-once or
shared-decision strategy cannot get right. `repartition_file_scans =
false` lives in the shared load file (it stops the groups being
re-derived as byte ranges); `target_partitions` differs per query, so
each `.benchmark` sets its own in an `init` step. The file names are
fixed rather than written by one `COPY ... PARTITIONED BY`, because a
directory-target `COPY` names its output with a random write id and
Parquet has no overwrite, so the second load of the shared dataset would
add files instead of replacing them.
- **Shared queries for the knob sweeps.** The template takes a `QUERY`
parameter (the path stem under `queries/`), so the `scale` and `width`
stubs point at `costsel/q03` and `costsel/q01` rather than keeping seven
verbatim copies of two WHERE clauses.
- **Docs.** The `predicate_eval` row in
`benchmarks/sql_benchmarks/README.md` carries the subgroup list, the
result-validation behaviour with its defaults caveat, and the note that
the suite sets no engine config of its own; `bench.sh` and the `.suite`
description match.
## What is the testing strategy for this PR?
No unit tests: these are benchmark definitions, not library code.
Verification is that the suite parses, every query runs, and every
query's result matches its checked-in expected count.
A full run passes on a release build: `benchmark_runner predicate_eval
-i 1 --result-mode validate` runs all 31 benchmarks across the ten
subgroups and exits 0, and repeats cleanly (the drift `COPY`s overwrite
their files rather than accumulating).
Validation was checked to actually be checking: overwriting one expected
count (`nulls_q90` 20000 → 999999) fails the run with
```
Error: Execution error: Error in result on row 1, column 1 running query "": expected value "999999" but got value "20000" in row: ["20000"]
```
and restoring the file makes it pass again; the same is true of
`drift_q83_per_partition_skew` at 991 instead of 992.
The drift plans are as intended on both sides (`EXPLAIN`, release build,
file list capped at five entries):
```
-- q82, target_partitions = 1
FilterExec: a_sel@0 = 0 AND b_sel@1 = 0, projection=[]
DataSourceExec: file_groups={1 group: [[.../drift_split/f00.parquet, .../f01.parquet, .../f02.parquet, .../f03.parquet, .../f04.parquet, ...]]}, projection=[a_sel, b_sel], file_type=parquet, predicate=a_sel@1 = 0 AND b_sel@2 = 0, ...
-- q83, target_partitions = 16
FilterExec: a_sel@0 = 0 AND b_sel@1 = 0, projection=[]
DataSourceExec: file_groups={16 groups: [[.../drift_split/f00.parquet], [.../f01.parquet], [.../f02.parquet], [.../f03.parquet], [.../f04.parquet], ...]}, projection=[a_sel, b_sel], file_type=parquet, predicate=a_sel@1 = 0 AND b_sel@2 = 0, ...
```
Both settings were checked to be load-bearing by dropping them: without
q82's `init` the plan no longer contains `file_groups={1 group`, and
with `repartition_file_scans` left on, q83 plans as `file_groups={12
groups: [[.../f00.parquet:0..224901, .../f01.parquet:0..74967], ...]}` —
both profiles in one stream. That the read order is deterministic is
what q82 relies on, and it is: `FileGroup::split_files` sorts a group's
files by path before chunking them
([`file_groups.rs`](https://github.com/apache/datafusion/blob/main/datafusion/datasource/src/file_groups.rs#L459-L462),
whose comment says why: `ObjectStore::list` gives no order), and
`FileStream` scans a group's files in order.
The datasets were verified directly too: `c_sel` is NULL on exactly
100000 rows and `< 5` on 40000 at `PRED_ROWS=1000000`; the split dataset
has 62500 rows per file, `a_sel = 0` on 62 of them and `b_sel = 0` on
31250 in `f00..f07`, mirrored in `f08..f15`, and 992 rows matching both
over the whole table. q34 and q32 have identical selectivities (both
`count(*) = 5000` at `PRED_ROWS=100000`).
## Are there any user-facing changes?
No. Benchmark definitions only; nothing in this PR is part of any public
API or affects query execution.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Fable 5.1 <no...@anthropic.com>
---
benchmarks/bench.sh | 10 ++-
benchmarks/sql_benchmarks/README.md | 2 +-
.../benchmarks/cardinality/q30.benchmark | 3 +-
.../benchmarks/cardinality/q31.benchmark | 3 +-
.../benchmarks/cardinality/q32.benchmark | 3 +-
.../benchmarks/cardinality/q33.benchmark | 3 +-
.../cardinality/{q32.benchmark => q34.benchmark} | 7 +-
.../benchmarks/correlation/q70.benchmark | 3 +-
.../benchmarks/correlation/q71.benchmark | 3 +-
.../benchmarks/correlation/q72.benchmark | 3 +-
.../benchmarks/correlation/q73.benchmark | 3 +-
.../predicate_eval/benchmarks/cost/q10.benchmark | 3 +-
.../predicate_eval/benchmarks/cost/q11.benchmark | 3 +-
.../benchmarks/costsel/q01.benchmark | 3 +-
.../benchmarks/costsel/q02.benchmark | 3 +-
.../benchmarks/costsel/q03.benchmark | 3 +-
.../costsel/{q03.benchmark => q04.benchmark} | 5 +-
.../predicate_eval/benchmarks/drift/q80.benchmark | 3 +-
.../predicate_eval/benchmarks/drift/q81.benchmark | 7 --
.../predicate_eval/benchmarks/drift/q82.benchmark | 10 +++
.../predicate_eval/benchmarks/drift/q83.benchmark | 10 +++
.../benchmarks/neutral/q60.benchmark | 3 +-
.../benchmarks/neutral/q61.benchmark | 3 +-
.../{neutral/q60.benchmark => nulls/q90.benchmark} | 7 +-
.../{neutral/q60.benchmark => nulls/q91.benchmark} | 7 +-
.../predicate_eval/benchmarks/scale/q50.benchmark | 3 +-
.../predicate_eval/benchmarks/scale/q51.benchmark | 3 +-
.../predicate_eval/benchmarks/scale/q52.benchmark | 3 +-
.../predicate_eval/benchmarks/scale/q53.benchmark | 3 +-
.../benchmarks/selectivity/q20.benchmark | 3 +-
.../benchmarks/selectivity/q21.benchmark | 3 +-
.../predicate_eval/benchmarks/width/q40.benchmark | 3 +-
.../predicate_eval/benchmarks/width/q41.benchmark | 3 +-
.../predicate_eval/benchmarks/width/q42.benchmark | 3 +-
.../sql_benchmarks/predicate_eval/load/corr.sql | 15 +---
.../predicate_eval/load/corrproxy.sql | 34 +++-----
.../sql_benchmarks/predicate_eval/load/drift.sql | 19 ++---
.../predicate_eval/load/drift_split.sql | 96 ++++++++++++++++++++++
.../sql_benchmarks/predicate_eval/load/ints.sql | 13 +--
.../predicate_eval/load/ints_wide.sql | 71 ++++++++++++++++
.../sql_benchmarks/predicate_eval/load/markers.sql | 17 ++--
.../sql_benchmarks/predicate_eval/load/mixed.sql | 16 +---
.../predicate_eval.benchmark.template | 37 +++------
.../predicate_eval/predicate_eval.suite | 2 +-
.../predicate_eval/queries/cardinality/q30.sql | 4 +-
.../predicate_eval/queries/cardinality/q31.sql | 2 +-
.../predicate_eval/queries/cardinality/q32.sql | 2 +-
.../predicate_eval/queries/cardinality/q33.sql | 2 +-
.../queries/cardinality/{q32.sql => q34.sql} | 2 +-
.../predicate_eval/queries/correlation/q70.sql | 4 +-
.../predicate_eval/queries/correlation/q71.sql | 4 +-
.../predicate_eval/queries/correlation/q72.sql | 4 +-
.../predicate_eval/queries/correlation/q73.sql | 10 +--
.../predicate_eval/queries/cost/q10.sql | 4 +-
.../predicate_eval/queries/cost/q11.sql | 3 +-
.../predicate_eval/queries/costsel/q01.sql | 6 +-
.../predicate_eval/queries/costsel/q02.sql | 3 +-
.../predicate_eval/queries/costsel/q03.sql | 4 +-
.../predicate_eval/queries/costsel/q04.sql | 4 +
.../predicate_eval/queries/drift/q80.sql | 6 +-
.../predicate_eval/queries/drift/q81.sql | 5 --
.../predicate_eval/queries/drift/q82.sql | 5 ++
.../predicate_eval/queries/drift/q83.sql | 5 ++
.../predicate_eval/queries/neutral/q60.sql | 3 +-
.../predicate_eval/queries/neutral/q61.sql | 4 +-
.../predicate_eval/queries/nulls/q90.sql | 6 ++
.../predicate_eval/queries/nulls/q91.sql | 5 ++
.../predicate_eval/queries/scale/q50.sql | 6 --
.../predicate_eval/queries/scale/q51.sql | 4 -
.../predicate_eval/queries/scale/q52.sql | 4 -
.../predicate_eval/queries/scale/q53.sql | 4 -
.../predicate_eval/queries/selectivity/q20.sql | 5 +-
.../predicate_eval/queries/selectivity/q21.sql | 3 +-
.../predicate_eval/queries/width/q40.sql | 9 --
.../predicate_eval/queries/width/q41.sql | 7 --
.../predicate_eval/queries/width/q42.sql | 7 --
.../predicate_eval/results/cardinality_q30_k2.csv | 2 +
.../predicate_eval/results/cardinality_q31_k4.csv | 2 +
.../predicate_eval/results/cardinality_q32_k8.csv | 2 +
.../predicate_eval/results/cardinality_q33_k16.csv | 2 +
.../results/cardinality_q34_k8_wide64.csv | 2 +
.../results/correlation_q70_independent.csv | 2 +
.../results/correlation_q71_positive.csv | 2 +
.../results/correlation_q72_anti.csv | 2 +
.../results/correlation_q73_redundant_proxy.csv | 2 +
.../results/cost_q10_expensive_first.csv | 2 +
.../results/cost_q11_cheap_first.csv | 2 +
.../results/costsel_q01_regexp_selective_last.csv | 2 +
.../results/costsel_q02_regexp_selective_first.csv | 2 +
..._cheap_unselective_then_expensive_selective.csv | 2 +
..._expensive_selective_then_cheap_unselective.csv | 2 +
.../predicate_eval/results/drift_q80_a_then_b.csv | 2 +
.../predicate_eval/results/drift_q82_late_flip.csv | 2 +
.../results/drift_q83_per_partition_skew.csv | 2 +
.../results/neutral_q60_cheap_uniform.csv | 2 +
.../results/neutral_q61_expensive_uniform.csv | 2 +
.../results/nulls_q90_nullable_selective_first.csv | 2 +
.../results/nulls_q91_nullable_selective_last.csv | 2 +
.../predicate_eval/results/scale_q50_5k.csv | 2 +
.../predicate_eval/results/scale_q51_100k.csv | 2 +
.../predicate_eval/results/scale_q52_5m.csv | 2 +
.../predicate_eval/results/scale_q53_50m.csv | 2 +
.../results/selectivity_q20_unselective_first.csv | 2 +
.../results/selectivity_q21_selective_first.csv | 2 +
.../predicate_eval/results/width_q40_narrow.csv | 2 +
.../predicate_eval/results/width_q41_wide.csv | 2 +
.../predicate_eval/results/width_q42_xwide.csv | 2 +
.../predicate_eval/scratch/.gitignore | 1 +
108 files changed, 393 insertions(+), 276 deletions(-)
diff --git a/benchmarks/bench.sh b/benchmarks/bench.sh
index 419fd5be3a..df33d2aec7 100755
--- a/benchmarks/bench.sh
+++ b/benchmarks/bench.sh
@@ -106,7 +106,7 @@ wide_schema: Small-projection queries on a wide synthetic dataset (10
(runs both 'wide' and 'narrow' subgroups: narrow is an internal baseline; the wide-vs-narrow ratio is the signal)
predicate_eval: Conjunctive (AND) filter-evaluation micro-benchmarks; each subgroup is a different predicate pattern, to test how an
adaptive predicate-ordering system behaves across them (see https://github.com/apache/datafusion/issues/11262)
- (subgroups via BENCH_SUBGROUP: costsel, cost, selectivity, cardinality, width, scale, neutral, correlation, drift)
+ (subgroups via BENCH_SUBGROUP: costsel, cost, selectivity, cardinality, width, scale, neutral, correlation, drift, nulls)
(toggle a system under test with its native DATAFUSION_* env var; size data with PRED_ROWS, string width with PRED_FILL)
parquet_row_filter_skip: Per-RG fully-matched RowFilter skip on Parquet (apache/datafusion#23696); clustered string key + low-selectivity
range filter + pushdown, so most row groups are fully matched and the per-row RowFilter is skipped on them
@@ -876,7 +876,11 @@ run_push_down_topk() {
# micro-benchmarks where each subgroup is a different predicate pattern, used to
# test how an adaptive predicate-ordering system behaves across them (see
# https://github.com/apache/datafusion/issues/11262). Data is generated inline
-# by the suite's load SQL, so there is no data step.
+# by the suite's load SQL, so there is no data step (drift q82 and q83 share 16
+# small Parquet files written into sql_benchmarks/predicate_eval/scratch/, which
+# is gitignored; q82 reads them with target_partitions=1, so the selectivity flip
+# lands halfway through one stream, and q83 with 16, so each stream gets one whole
+# file and a fixed profile).
#
# By default the suite measures DataFusion's built-in left-deep AND short-circuit
# and sets no engine config of its own. To evaluate a system under test, export
@@ -885,7 +889,7 @@ run_push_down_topk() {
# DATAFUSION_EXECUTION_ADAPTIVE_FILTER_REORDERING=true ./bench.sh run predicate_eval
# Suite-specific knobs (string-substituted into the load SQL, not engine config):
# BENCH_SUBGROUP run one subgroup (costsel, cost, selectivity, cardinality,
-# width, scale, neutral, correlation, drift)
+# width, scale, neutral, correlation, drift, nulls)
# PRED_ROWS synthetic row count (default 1_000_000; the scale subgroup
# overrides this per query)
# PRED_FILL filler chars per marker = string-column width knob
diff --git a/benchmarks/sql_benchmarks/README.md b/benchmarks/sql_benchmarks/README.md
index 1ce1fa488c..b8bf8adede 100644
--- a/benchmarks/sql_benchmarks/README.md
+++ b/benchmarks/sql_benchmarks/README.md
@@ -43,7 +43,7 @@ in the community:
| `tpcds` | TPC‑DS queries |
| `tpch` | TPC‑H queries |
| `wide_schema` | Small-projection queries on a wide (1024-col, 256-file) synthetic dataset; runs `wide` + `narrow` subgroups for comparison |
-| `predicate_eval` | Conjunctive (AND) filter-evaluation micro-benchmarks; each subgroup is a different predicate pattern, to test how an adaptive predicate-ordering system behaves across them ([#11262](https://github.com/apache/datafusion/issues/11262)). Subgroups (`--subgroup`): `costsel`, `cost`, `selectivity`, `cardinality`, `width`, `scale`, `neutral`, `correlation`, `drift`. Configure the system under test through its DataFusion settings. |
+| `predicate_eval` | Conjunctive (AND) filter-evaluation micro-benchmarks; each subgroup is a different predicate pattern, to test how an adaptive predicate-ordering system behaves across them ([#11262](https://github.com/apache/datafusion/issues/11262)). Subgroups (`--subgroup`): `costsel`, `cost`, `selectivity`, `cardinality`, `width`, `scale`, `neutral`, `correlation`, `drift`, `nulls`. The suite sets no engine config of its own, so by default it measures DataFusion's built-in le [...]
| `parquet_row_filter_skip` | Micro-benchmark for the per-row-group fully-matched RowFilter skip on Parquet scans ([#23696](https://github.com/apache/datafusion/issues/23696)). Subgroups (`--subgroup`): `skip` (clustered key, most row groups fully matched by statistics so the per-row filter is skipped), `control` (scrambled key, no row group is ever fully matched). Size the data with `PRED_ROWS` and the row-group size with `RG_SIZE`. |
# Running Benchmarks
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q30.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q30.benchmark
index 760ea2ca90..d404a193d4 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q30.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q30.benchmark
@@ -1,7 +1,6 @@
subgroup cardinality
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=cardinality
-QPAD=30
+QUERY=cardinality/q30
DATASET=ints
NAME=cardinality_q30_k2
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q31.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q31.benchmark
index 74f22715d1..54a25337bd 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q31.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q31.benchmark
@@ -1,7 +1,6 @@
subgroup cardinality
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=cardinality
-QPAD=31
+QUERY=cardinality/q31
DATASET=ints
NAME=cardinality_q31_k4
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q32.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q32.benchmark
index b6b69c3852..aff0173e3d 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q32.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q32.benchmark
@@ -1,7 +1,6 @@
subgroup cardinality
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=cardinality
-QPAD=32
+QUERY=cardinality/q32
DATASET=ints
NAME=cardinality_q32_k8
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q33.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q33.benchmark
index 1260e68137..bb1464d200 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q33.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q33.benchmark
@@ -1,7 +1,6 @@
subgroup cardinality
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=cardinality
-QPAD=33
+QUERY=cardinality/q33
DATASET=ints
NAME=cardinality_q33_k16
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q32.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q34.benchmark
similarity index 57%
copy from benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q32.benchmark
copy to benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q34.benchmark
index b6b69c3852..c1851e34ad 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q32.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cardinality/q34.benchmark
@@ -1,7 +1,6 @@
subgroup cardinality
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=cardinality
-QPAD=32
-DATASET=ints
-NAME=cardinality_q32_k8
+QUERY=cardinality/q34
+DATASET=ints_wide
+NAME=cardinality_q34_k8_wide64
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/correlation/q70.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/correlation/q70.benchmark
index ef20f7dc49..2084ddab97 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/correlation/q70.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/correlation/q70.benchmark
@@ -1,7 +1,6 @@
subgroup correlation
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=correlation
-QPAD=70
+QUERY=correlation/q70
DATASET=corr
NAME=correlation_q70_independent
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/correlation/q71.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/correlation/q71.benchmark
index 8875f6c44e..cd5e63aa95 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/correlation/q71.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/correlation/q71.benchmark
@@ -1,7 +1,6 @@
subgroup correlation
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=correlation
-QPAD=71
+QUERY=correlation/q71
DATASET=corr
NAME=correlation_q71_positive
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/correlation/q72.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/correlation/q72.benchmark
index 8109f1439a..e1dfc3fdaf 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/correlation/q72.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/correlation/q72.benchmark
@@ -1,7 +1,6 @@
subgroup correlation
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=correlation
-QPAD=72
+QUERY=correlation/q72
DATASET=corr
NAME=correlation_q72_anti
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/correlation/q73.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/correlation/q73.benchmark
index cc3f7bcf54..c9a89b1da8 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/correlation/q73.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/correlation/q73.benchmark
@@ -1,7 +1,6 @@
subgroup correlation
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=correlation
-QPAD=73
+QUERY=correlation/q73
DATASET=corrproxy
NAME=correlation_q73_redundant_proxy
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cost/q10.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cost/q10.benchmark
index 9b864b8594..57c7034573 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cost/q10.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cost/q10.benchmark
@@ -1,7 +1,6 @@
subgroup cost
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=cost
-QPAD=10
+QUERY=cost/q10
DATASET=mixed
NAME=cost_q10_expensive_first
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cost/q11.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cost/q11.benchmark
index 296ea443b3..30398925cb 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cost/q11.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/cost/q11.benchmark
@@ -1,7 +1,6 @@
subgroup cost
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=cost
-QPAD=11
+QUERY=cost/q11
DATASET=mixed
NAME=cost_q11_cheap_first
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/costsel/q01.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/costsel/q01.benchmark
index abedd1d580..123839a2de 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/costsel/q01.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/costsel/q01.benchmark
@@ -1,7 +1,6 @@
subgroup costsel
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=costsel
-QPAD=01
+QUERY=costsel/q01
DATASET=markers
NAME=costsel_q01_regexp_selective_last
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/costsel/q02.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/costsel/q02.benchmark
index f50aab6642..304e67189d 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/costsel/q02.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/costsel/q02.benchmark
@@ -1,7 +1,6 @@
subgroup costsel
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=costsel
-QPAD=02
+QUERY=costsel/q02
DATASET=markers
NAME=costsel_q02_regexp_selective_first
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/costsel/q03.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/costsel/q03.benchmark
index 10c4ce184e..d039e6e28d 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/costsel/q03.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/costsel/q03.benchmark
@@ -1,7 +1,6 @@
subgroup costsel
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=costsel
-QPAD=03
+QUERY=costsel/q03
DATASET=mixed
NAME=costsel_q03_cheap_unselective_then_expensive_selective
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/costsel/q03.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/costsel/q04.benchmark
similarity index 55%
copy from benchmarks/sql_benchmarks/predicate_eval/benchmarks/costsel/q03.benchmark
copy to benchmarks/sql_benchmarks/predicate_eval/benchmarks/costsel/q04.benchmark
index 10c4ce184e..811e5d642a 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/costsel/q03.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/costsel/q04.benchmark
@@ -1,7 +1,6 @@
subgroup costsel
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=costsel
-QPAD=03
+QUERY=costsel/q04
DATASET=mixed
-NAME=costsel_q03_cheap_unselective_then_expensive_selective
+NAME=costsel_q04_expensive_selective_then_cheap_unselective
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/drift/q80.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/drift/q80.benchmark
index 970adc53f8..fcbd32b0bf 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/drift/q80.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/drift/q80.benchmark
@@ -1,7 +1,6 @@
subgroup drift
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=drift
-QPAD=80
+QUERY=drift/q80
DATASET=drift
NAME=drift_q80_a_then_b
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/drift/q81.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/drift/q81.benchmark
deleted file mode 100644
index 93cde75ffe..0000000000
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/drift/q81.benchmark
+++ /dev/null
@@ -1,7 +0,0 @@
-subgroup drift
-
-template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=drift
-QPAD=81
-DATASET=drift
-NAME=drift_q81_b_then_a
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/drift/q82.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/drift/q82.benchmark
new file mode 100644
index 0000000000..8ec68bd5c1
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/drift/q82.benchmark
@@ -0,0 +1,10 @@
+subgroup drift
+
+template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
+QUERY=drift/q82
+DATASET=drift_split
+NAME=drift_q82_late_flip
+
+# One stream over the whole directory: the flip lands mid-scan.
+init
+set datafusion.execution.target_partitions = 1;
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/drift/q83.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/drift/q83.benchmark
new file mode 100644
index 0000000000..be25d56612
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/drift/q83.benchmark
@@ -0,0 +1,10 @@
+subgroup drift
+
+template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
+QUERY=drift/q83
+DATASET=drift_split
+NAME=drift_q83_per_partition_skew
+
+# One file per stream: each stream sees a single profile.
+init
+set datafusion.execution.target_partitions = 16;
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/neutral/q60.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/neutral/q60.benchmark
index 039fee622b..5d39993e98 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/neutral/q60.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/neutral/q60.benchmark
@@ -1,7 +1,6 @@
subgroup neutral
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=neutral
-QPAD=60
+QUERY=neutral/q60
DATASET=ints
NAME=neutral_q60_cheap_uniform
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/neutral/q61.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/neutral/q61.benchmark
index edaf89b471..ec66cdfc39 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/neutral/q61.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/neutral/q61.benchmark
@@ -1,7 +1,6 @@
subgroup neutral
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=neutral
-QPAD=61
+QUERY=neutral/q61
DATASET=markers
NAME=neutral_q61_expensive_uniform
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/neutral/q60.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/nulls/q90.benchmark
similarity index 54%
copy from benchmarks/sql_benchmarks/predicate_eval/benchmarks/neutral/q60.benchmark
copy to benchmarks/sql_benchmarks/predicate_eval/benchmarks/nulls/q90.benchmark
index 039fee622b..4e82661a56 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/neutral/q60.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/nulls/q90.benchmark
@@ -1,7 +1,6 @@
-subgroup neutral
+subgroup nulls
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=neutral
-QPAD=60
+QUERY=nulls/q90
DATASET=ints
-NAME=neutral_q60_cheap_uniform
+NAME=nulls_q90_nullable_selective_first
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/neutral/q60.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/nulls/q91.benchmark
similarity index 54%
copy from benchmarks/sql_benchmarks/predicate_eval/benchmarks/neutral/q60.benchmark
copy to benchmarks/sql_benchmarks/predicate_eval/benchmarks/nulls/q91.benchmark
index 039fee622b..377b139bf0 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/neutral/q60.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/nulls/q91.benchmark
@@ -1,7 +1,6 @@
-subgroup neutral
+subgroup nulls
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=neutral
-QPAD=60
+QUERY=nulls/q91
DATASET=ints
-NAME=neutral_q60_cheap_uniform
+NAME=nulls_q91_nullable_selective_last
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/scale/q50.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/scale/q50.benchmark
index 0bef31e14f..1c8f2c148f 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/scale/q50.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/scale/q50.benchmark
@@ -1,8 +1,7 @@
subgroup scale
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=scale
-QPAD=50
+QUERY=costsel/q03
DATASET=mixed
PRED_ROWS=5000
NAME=scale_q50_5k
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/scale/q51.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/scale/q51.benchmark
index 8f1315fb11..52bd2a0fd0 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/scale/q51.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/scale/q51.benchmark
@@ -1,8 +1,7 @@
subgroup scale
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=scale
-QPAD=51
+QUERY=costsel/q03
DATASET=mixed
PRED_ROWS=100000
NAME=scale_q51_100k
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/scale/q52.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/scale/q52.benchmark
index 7ddbfc19b4..9b37a48627 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/scale/q52.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/scale/q52.benchmark
@@ -1,8 +1,7 @@
subgroup scale
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=scale
-QPAD=52
+QUERY=costsel/q03
DATASET=mixed
PRED_ROWS=5000000
NAME=scale_q52_5m
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/scale/q53.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/scale/q53.benchmark
index 6cea5c44a1..9839289113 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/scale/q53.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/scale/q53.benchmark
@@ -1,8 +1,7 @@
subgroup scale
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=scale
-QPAD=53
+QUERY=costsel/q03
DATASET=mixed
PRED_ROWS=50000000
NAME=scale_q53_50m
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/selectivity/q20.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/selectivity/q20.benchmark
index 077a62650d..f587dc50d9 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/selectivity/q20.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/selectivity/q20.benchmark
@@ -1,7 +1,6 @@
subgroup selectivity
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=selectivity
-QPAD=20
+QUERY=selectivity/q20
DATASET=ints
NAME=selectivity_q20_unselective_first
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/selectivity/q21.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/selectivity/q21.benchmark
index 24fc6ef4cd..655b067211 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/selectivity/q21.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/selectivity/q21.benchmark
@@ -1,7 +1,6 @@
subgroup selectivity
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=selectivity
-QPAD=21
+QUERY=selectivity/q21
DATASET=ints
NAME=selectivity_q21_selective_first
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/width/q40.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/width/q40.benchmark
index df66cf16a3..186fafc40b 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/width/q40.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/width/q40.benchmark
@@ -1,8 +1,7 @@
subgroup width
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=width
-QPAD=40
+QUERY=costsel/q01
DATASET=markers
PRED_FILL=2
NAME=width_q40_narrow
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/width/q41.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/width/q41.benchmark
index c260dc9985..16967c4d87 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/width/q41.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/width/q41.benchmark
@@ -1,8 +1,7 @@
subgroup width
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=width
-QPAD=41
+QUERY=costsel/q01
DATASET=markers
PRED_FILL=30
NAME=width_q41_wide
diff --git a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/width/q42.benchmark b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/width/q42.benchmark
index 988ff59c70..44ebe3137d 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/benchmarks/width/q42.benchmark
+++ b/benchmarks/sql_benchmarks/predicate_eval/benchmarks/width/q42.benchmark
@@ -1,8 +1,7 @@
subgroup width
template sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
-SUBGROUP=width
-QPAD=42
+QUERY=costsel/q01
DATASET=markers
PRED_FILL=170
NAME=width_q42_xwide
diff --git a/benchmarks/sql_benchmarks/predicate_eval/load/corr.sql b/benchmarks/sql_benchmarks/predicate_eval/load/corr.sql
index 2d7ceb73e6..946a999ceb 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/load/corr.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/load/corr.sql
@@ -1,15 +1,6 @@
--- Correlation dataset: a base column plus derived columns that control the
--- *conditional* selectivity of one predicate given another (its selectivity
--- among the rows that already passed the other).
---
--- x uniform [0,100)
--- x_pos = x (perfectly positively correlated: `x<k AND x_pos<k`
--- passes ~k%, not the ~k%^2 an independence assumption
--- predicts)
--- x_anti = 99 - x (anti-correlated: `x<k AND x_anti<k` is empty for k<=50)
--- ind independent control column, uniform [0,100)
---
--- PRED_ROWS sizes the table.
+-- Correlation dataset: `x` uniform on [0,100); `x_pos` = x (perfectly positively
+-- correlated); `x_anti` = 99 - x (anti-correlated); `ind` uniform on [0,100) and
+-- independent of x. PRED_ROWS sizes the table.
CREATE TABLE t AS
SELECT
(value * 7) % 100 AS x,
diff --git a/benchmarks/sql_benchmarks/predicate_eval/load/corrproxy.sql b/benchmarks/sql_benchmarks/predicate_eval/load/corrproxy.sql
index f06e68d38c..e17d0d4925 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/load/corrproxy.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/load/corrproxy.sql
@@ -1,30 +1,20 @@
--- Correlated-proxy dataset: a cheap integer predicate that is a perfect proxy
--- for three string predicates, plus one independent string predicate.
---
+-- Correlated-proxy dataset:
-- c0 = 1 for ~30% of rows (cheap proxy)
--- s1, s2, s3 each contain a marker exactly where c0 = 1 (correlated)
--- s4 contains a marker for an independent ~30% (independent)
---
--- The four string columns are deliberately *identical in shape*: same width,
--- the same single marker at the same offset, each matched by an equally cheap
--- regex with the same ~30% marginal selectivity. Marginally the four regex
--- predicates are therefore indistinguishable -- same cost, same selectivity, in
--- every position -- so neither a marginal cost/selectivity estimator nor
--- runtime timing can prefer one over another. Only their *conditional*
--- behaviour behind the proxy differs: after `c0 = 1`, the s1/s2/s3 regexes keep
--- every survivor (each re-tests the proxy's condition) while the s4 regex still
--- discards ~70%. Only joint statistics can see that; an independence assumption
--- prices all four regexes identically in every position.
+-- s1, s2, s3 each carry a marker exactly where c0 = 1 (correlated)
+-- s4 carries a marker for an independent ~30% (independent)
+-- PRED_FILL sets the filler width each side of the marker, PRED_ROWS the row count.
--
--- PRED_FILL sets the filler width on each side of the marker (a non-matching
--- `regexp_like` must scan the whole value), and PRED_ROWS sizes the table.
+-- The four string columns are identical in shape -- same width, same marker
+-- offset, same regex cost, same ~30% marginal selectivity -- so marginally they
+-- are indistinguishable in every position. Only their joint distribution with the
+-- proxy differs: after `c0 = 1` the s1/s2/s3 regexes keep every survivor while s4
+-- still discards ~70%. Ranking them therefore takes joint statistics; an
+-- independence assumption prices all four regexes identically everywhere.
CREATE TABLE t AS
WITH base AS (
SELECT
- -- The cheap proxy and the independent control share one definition each, so
- -- the perfect-proxy / independence invariants can't drift apart silently.
- (value * 7) % 100 < 30 AS proxy, -- ~30%, drives c0 and s1/s2/s3
- (value * 13) % 100 < 30 AS indep -- ~30%, independent of proxy, drives s4
+ (value * 7) % 100 < 30 AS proxy,
+ (value * 13) % 100 < 30 AS indep
FROM generate_series(1, ${PRED_ROWS:-1000000})
)
SELECT
diff --git a/benchmarks/sql_benchmarks/predicate_eval/load/drift.sql b/benchmarks/sql_benchmarks/predicate_eval/load/drift.sql
index d5635d91dd..0345abc3f2 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/load/drift.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/load/drift.sql
@@ -1,16 +1,11 @@
--- Drift dataset: two predicates whose *relative* selectivity flips partway
--- through the scan, so whole-table selectivity differs from per-batch
--- selectivity. Rows are emitted in `seq` order, so batches observe the drift in
--- order.
---
--- a_sel = 0 is selective (~0.1%) in the first 10% of rows, unselective
--- (~50%) afterwards.
--- b_sel = 0 is the mirror: unselective early, selective late.
---
--- PRED_ROWS sizes the table.
+-- Drift dataset: rows are emitted in `seq` order and which predicate is more
+-- selective flips partway through, so whole-table selectivity differs from
+-- per-batch selectivity. `a_sel = 0` matches ~0.1% over the first 2% of rows and
+-- ~50% after; `b_sel = 0` is the mirror. At the default PRED_ROWS, which sizes the
+-- table, that flip lands inside the pooled 8-batch warm-up (~65k rows).
CREATE TABLE t AS
SELECT
value AS seq,
- CASE WHEN value < ${PRED_ROWS:-1000000} / 10 THEN value % 1000 ELSE value % 2 END AS a_sel,
- CASE WHEN value < ${PRED_ROWS:-1000000} / 10 THEN value % 2 ELSE value % 1000 END AS b_sel
+ CASE WHEN value < ${PRED_ROWS:-1000000} / 50 THEN value % 1000 ELSE value % 2 END AS a_sel,
+ CASE WHEN value < ${PRED_ROWS:-1000000} / 50 THEN value % 2 ELSE value % 1000 END AS b_sel
FROM generate_series(1, ${PRED_ROWS:-1000000});
diff --git a/benchmarks/sql_benchmarks/predicate_eval/load/drift_split.sql b/benchmarks/sql_benchmarks/predicate_eval/load/drift_split.sql
new file mode 100644
index 0000000000..b36a0d122f
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/load/drift_split.sql
@@ -0,0 +1,96 @@
+-- Split drift dataset, shared by q82 and q83: 16 Parquet files of PRED_ROWS / 16
+-- rows each, at the q80 rates -- f00..f07 carry profile A (`a_sel = 0` selective
+-- at ~0.1%, `b_sel = 0` unselective at ~50%) and f08..f15 the mirror.
+--
+-- One `COPY` per file, because a directory-target `COPY` names its output with a
+-- random write id and Parquet has no overwrite, so a second load would add files;
+-- the names are zero-padded so they sort numerically. `FileGroup::split_files`
+-- sorts a group's files by path, so `target_partitions = 1` (q82) reads f00..f15
+-- in order and `= 16` (q83) gives each stream one whole file; each query sets it.
+-- `repartition_file_scans` stays off, or both profiles land in one stream again.
+
+COPY (SELECT value AS seq, value % 1000 AS a_sel, value % 2 AS b_sel
+ FROM generate_series(1, ${PRED_ROWS:-1000000} / 16) ORDER BY value)
+TO 'sql_benchmarks/predicate_eval/scratch/drift_split/f00.parquet'
+STORED AS PARQUET;
+
+COPY (SELECT value AS seq, value % 1000 AS a_sel, value % 2 AS b_sel
+ FROM generate_series(1, ${PRED_ROWS:-1000000} / 16) ORDER BY value)
+TO 'sql_benchmarks/predicate_eval/scratch/drift_split/f01.parquet'
+STORED AS PARQUET;
+
+COPY (SELECT value AS seq, value % 1000 AS a_sel, value % 2 AS b_sel
+ FROM generate_series(1, ${PRED_ROWS:-1000000} / 16) ORDER BY value)
+TO 'sql_benchmarks/predicate_eval/scratch/drift_split/f02.parquet'
+STORED AS PARQUET;
+
+COPY (SELECT value AS seq, value % 1000 AS a_sel, value % 2 AS b_sel
+ FROM generate_series(1, ${PRED_ROWS:-1000000} / 16) ORDER BY value)
+TO 'sql_benchmarks/predicate_eval/scratch/drift_split/f03.parquet'
+STORED AS PARQUET;
+
+COPY (SELECT value AS seq, value % 1000 AS a_sel, value % 2 AS b_sel
+ FROM generate_series(1, ${PRED_ROWS:-1000000} / 16) ORDER BY value)
+TO 'sql_benchmarks/predicate_eval/scratch/drift_split/f04.parquet'
+STORED AS PARQUET;
+
+COPY (SELECT value AS seq, value % 1000 AS a_sel, value % 2 AS b_sel
+ FROM generate_series(1, ${PRED_ROWS:-1000000} / 16) ORDER BY value)
+TO 'sql_benchmarks/predicate_eval/scratch/drift_split/f05.parquet'
+STORED AS PARQUET;
+
+COPY (SELECT value AS seq, value % 1000 AS a_sel, value % 2 AS b_sel
+ FROM generate_series(1, ${PRED_ROWS:-1000000} / 16) ORDER BY value)
+TO 'sql_benchmarks/predicate_eval/scratch/drift_split/f06.parquet'
+STORED AS PARQUET;
+
+COPY (SELECT value AS seq, value % 1000 AS a_sel, value % 2 AS b_sel
+ FROM generate_series(1, ${PRED_ROWS:-1000000} / 16) ORDER BY value)
+TO 'sql_benchmarks/predicate_eval/scratch/drift_split/f07.parquet'
+STORED AS PARQUET;
+
+COPY (SELECT value AS seq, value % 2 AS a_sel, value % 1000 AS b_sel
+ FROM generate_series(1, ${PRED_ROWS:-1000000} / 16) ORDER BY value)
+TO 'sql_benchmarks/predicate_eval/scratch/drift_split/f08.parquet'
+STORED AS PARQUET;
+
+COPY (SELECT value AS seq, value % 2 AS a_sel, value % 1000 AS b_sel
+ FROM generate_series(1, ${PRED_ROWS:-1000000} / 16) ORDER BY value)
+TO 'sql_benchmarks/predicate_eval/scratch/drift_split/f09.parquet'
+STORED AS PARQUET;
+
+COPY (SELECT value AS seq, value % 2 AS a_sel, value % 1000 AS b_sel
+ FROM generate_series(1, ${PRED_ROWS:-1000000} / 16) ORDER BY value)
+TO 'sql_benchmarks/predicate_eval/scratch/drift_split/f10.parquet'
+STORED AS PARQUET;
+
+COPY (SELECT value AS seq, value % 2 AS a_sel, value % 1000 AS b_sel
+ FROM generate_series(1, ${PRED_ROWS:-1000000} / 16) ORDER BY value)
+TO 'sql_benchmarks/predicate_eval/scratch/drift_split/f11.parquet'
+STORED AS PARQUET;
+
+COPY (SELECT value AS seq, value % 2 AS a_sel, value % 1000 AS b_sel
+ FROM generate_series(1, ${PRED_ROWS:-1000000} / 16) ORDER BY value)
+TO 'sql_benchmarks/predicate_eval/scratch/drift_split/f12.parquet'
+STORED AS PARQUET;
+
+COPY (SELECT value AS seq, value % 2 AS a_sel, value % 1000 AS b_sel
+ FROM generate_series(1, ${PRED_ROWS:-1000000} / 16) ORDER BY value)
+TO 'sql_benchmarks/predicate_eval/scratch/drift_split/f13.parquet'
+STORED AS PARQUET;
+
+COPY (SELECT value AS seq, value % 2 AS a_sel, value % 1000 AS b_sel
+ FROM generate_series(1, ${PRED_ROWS:-1000000} / 16) ORDER BY value)
+TO 'sql_benchmarks/predicate_eval/scratch/drift_split/f14.parquet'
+STORED AS PARQUET;
+
+COPY (SELECT value AS seq, value % 2 AS a_sel, value % 1000 AS b_sel
+ FROM generate_series(1, ${PRED_ROWS:-1000000} / 16) ORDER BY value)
+TO 'sql_benchmarks/predicate_eval/scratch/drift_split/f15.parquet'
+STORED AS PARQUET;
+
+set datafusion.optimizer.repartition_file_scans = false;
+
+CREATE EXTERNAL TABLE t
+STORED AS PARQUET
+LOCATION 'sql_benchmarks/predicate_eval/scratch/drift_split/';
diff --git a/benchmarks/sql_benchmarks/predicate_eval/load/ints.sql b/benchmarks/sql_benchmarks/predicate_eval/load/ints.sql
index 1d3f030915..f36b283c2a 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/load/ints.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/load/ints.sql
@@ -1,8 +1,8 @@
--- Sixteen independent integer columns, each uniform on [0,100). The predicate
--- `cN < k` therefore has selectivity ~k%. All columns are equally cheap to
--- evaluate, so only selectivity (not cost) distinguishes orderings here. The
--- multipliers are all coprime to 100, which keeps the residues uniform and the
--- columns mutually decorrelated. PRED_ROWS sizes the table.
+-- Seventeen integer columns: c0..c15 uniform on [0,100), so `cN < k` has
+-- selectivity ~k%, all equally cheap and mutually decorrelated (the multipliers
+-- are coprime to 100); plus `c_sel`, NULL on 10% of rows and uniform on [0,100)
+-- elsewhere, so `c_sel < 5` is true on 4% of rows and NULL on 10% -- a selective
+-- conjunct that is not NULL-free. PRED_ROWS sizes the table.
CREATE TABLE t AS
SELECT
(value * 1) % 100 AS c0,
@@ -20,5 +20,6 @@ SELECT
(value * 31) % 100 AS c12,
(value * 33) % 100 AS c13,
(value * 37) % 100 AS c14,
- (value * 39) % 100 AS c15
+ (value * 39) % 100 AS c15,
+ CASE WHEN value % 10 = 0 THEN NULL ELSE (value * 11) % 100 END AS c_sel
FROM generate_series(1, ${PRED_ROWS:-1000000});
diff --git a/benchmarks/sql_benchmarks/predicate_eval/load/ints_wide.sql b/benchmarks/sql_benchmarks/predicate_eval/load/ints_wide.sql
new file mode 100644
index 0000000000..cddce6f8e5
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/load/ints_wide.sql
@@ -0,0 +1,71 @@
+-- `ints.sql` widened to 64 integer columns, each uniform on [0,100), so `cN < k`
+-- again has selectivity ~k%. c0..c15 reuse the same multipliers, so a predicate
+-- over them has exactly the same selectivities as on `ints` and only the width of
+-- the batches flowing through the filter changes. PRED_ROWS sizes the table.
+CREATE TABLE t AS
+SELECT
+ (value * 1) % 100 AS c0,
+ (value * 3) % 100 AS c1,
+ (value * 7) % 100 AS c2,
+ (value * 9) % 100 AS c3,
+ (value * 11) % 100 AS c4,
+ (value * 13) % 100 AS c5,
+ (value * 17) % 100 AS c6,
+ (value * 19) % 100 AS c7,
+ (value * 21) % 100 AS c8,
+ (value * 23) % 100 AS c9,
+ (value * 27) % 100 AS c10,
+ (value * 29) % 100 AS c11,
+ (value * 31) % 100 AS c12,
+ (value * 33) % 100 AS c13,
+ (value * 37) % 100 AS c14,
+ (value * 39) % 100 AS c15,
+ (value * 41) % 100 AS c16,
+ (value * 43) % 100 AS c17,
+ (value * 47) % 100 AS c18,
+ (value * 49) % 100 AS c19,
+ (value * 51) % 100 AS c20,
+ (value * 53) % 100 AS c21,
+ (value * 57) % 100 AS c22,
+ (value * 59) % 100 AS c23,
+ (value * 61) % 100 AS c24,
+ (value * 63) % 100 AS c25,
+ (value * 67) % 100 AS c26,
+ (value * 69) % 100 AS c27,
+ (value * 71) % 100 AS c28,
+ (value * 73) % 100 AS c29,
+ (value * 77) % 100 AS c30,
+ (value * 79) % 100 AS c31,
+ (value * 81) % 100 AS c32,
+ (value * 83) % 100 AS c33,
+ (value * 87) % 100 AS c34,
+ (value * 89) % 100 AS c35,
+ (value * 91) % 100 AS c36,
+ (value * 93) % 100 AS c37,
+ (value * 97) % 100 AS c38,
+ (value * 99) % 100 AS c39,
+ (value * 101) % 100 AS c40,
+ (value * 103) % 100 AS c41,
+ (value * 107) % 100 AS c42,
+ (value * 109) % 100 AS c43,
+ (value * 111) % 100 AS c44,
+ (value * 113) % 100 AS c45,
+ (value * 117) % 100 AS c46,
+ (value * 119) % 100 AS c47,
+ (value * 121) % 100 AS c48,
+ (value * 123) % 100 AS c49,
+ (value * 127) % 100 AS c50,
+ (value * 129) % 100 AS c51,
+ (value * 131) % 100 AS c52,
+ (value * 133) % 100 AS c53,
+ (value * 137) % 100 AS c54,
+ (value * 139) % 100 AS c55,
+ (value * 141) % 100 AS c56,
+ (value * 143) % 100 AS c57,
+ (value * 147) % 100 AS c58,
+ (value * 149) % 100 AS c59,
+ (value * 151) % 100 AS c60,
+ (value * 153) % 100 AS c61,
+ (value * 157) % 100 AS c62,
+ (value * 159) % 100 AS c63
+FROM generate_series(1, ${PRED_ROWS:-1000000});
diff --git a/benchmarks/sql_benchmarks/predicate_eval/load/markers.sql b/benchmarks/sql_benchmarks/predicate_eval/load/markers.sql
index 6b8fff9385..d1c818fe0f 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/load/markers.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/load/markers.sql
@@ -1,15 +1,8 @@
--- Wide-string dataset: five markers embedded in `PRED_FILL`-wide filler so that a
--- non-matching `regexp_like` must scan the whole value (every string predicate
--- is "expensive"). Selectivities are coprime so the predicates are independent:
---
--- 'aaa' present in ~90% of rows (value % 10 <> 0)
--- 'bbb' present in ~86% of rows (value % 7 <> 0)
--- 'ccc' present in ~80% of rows (value % 5 <> 0)
--- 'ddd' present in ~75% of rows (value % 4 <> 0)
--- 'rare' present in ~0.1% of rows (value % 1009 = 5) <- the selective one
---
--- PRED_FILL sets the filler width per marker (the string-column width knob: ~6*PRED_FILL
--- chars per row), and PRED_ROWS sizes the table.
+-- Wide-string dataset: one column `s` holding five markers in PRED_FILL-wide
+-- filler, so a non-matching `regexp_like` must scan the whole value. 'aaa' ~90%,
+-- 'bbb' ~86%, 'ccc' ~80%, 'ddd' ~75%, 'rare' ~0.1%; the moduli are coprime, so the
+-- markers are independent. PRED_FILL is the string-width knob (~6*PRED_FILL chars
+-- per row) and PRED_ROWS sizes the table.
CREATE TABLE t AS
SELECT
repeat('q', ${PRED_FILL:-30})
diff --git a/benchmarks/sql_benchmarks/predicate_eval/load/mixed.sql b/benchmarks/sql_benchmarks/predicate_eval/load/mixed.sql
index a51c1040da..bc931e8a40 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/load/mixed.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/load/mixed.sql
@@ -1,15 +1,7 @@
--- Mixed-cost dataset: cheap integer columns (`cN < k` ~ k% selectivity)
--- alongside one wide string column carrying three markers matched by expensive
--- `regexp_like`:
---
--- 'rare' present in ~0.1% of rows (value % 1009 = 5)
--- 'ten' present in ~10% of rows (value % 10 = 0)
--- 'aaa' present in ~90% of rows (value % 10 <> 0)
---
--- This lets a single table mix cheap integer compares with expensive regexp
--- scans at independently chosen selectivities (e.g. a cheap, unselective compare
--- next to an expensive, selective regexp). PRED_FILL is the string-width knob;
--- PRED_ROWS sizes the table.
+-- Mixed-cost dataset: cheap integer columns c0..c3 uniform on [0,100) (`cN < k`
+-- ~k%) alongside one wide string column `s` carrying three markers matched by an
+-- expensive `regexp_like`: 'rare' ~0.1%, 'ten' ~10%, 'aaa' ~90%. PRED_FILL is the
+-- string-width knob and PRED_ROWS sizes the table.
CREATE TABLE t AS
SELECT
(value * 1) % 100 AS c0,
diff --git a/benchmarks/sql_benchmarks/predicate_eval/predicate_eval.benchmark.template b/benchmarks/sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
index e2e3d6175e..766bd20270 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
+++ b/benchmarks/sql_benchmarks/predicate_eval/predicate_eval.benchmark.template
@@ -1,35 +1,22 @@
-# Shared template for every predicate_eval benchmark. Each qNN.benchmark sets
-# its `subgroup` directive and then includes this template with parameters:
-# SUBGROUP subgroup name, also the query sub-directory (e.g. costsel)
-# QPAD zero-padded query id, also the query file stem (e.g. 01)
-# DATASET load script stem under load/ (e.g. markers)
-# NAME criterion display name (e.g. costsel_q01_regexp_selective_last)
-# Optional (consumed by the load scripts via ${...:-default}):
-# PRED_ROWS synthetic row count (default 1_000_000)
-# PRED_FILL filler chars per marker = string-column width knob (default 30)
-#
-# The run SQL lives in queries/${SUBGROUP}/q${QPAD}.sql so the WHERE clause is
-# readable on its own. The table is always named `t`, so the assert and cleanup
-# are uniform across datasets.
-#
-# The suite is implementation-agnostic and sets no engine config of its own: it
-# measures DataFusion's built-in left-deep `AND` short-circuit by default. To
-# evaluate a predicate-ordering system under test, set its native config via the
-# environment (the bench harness builds its SessionContext with
-# SessionConfig::from_env), e.g.
-# DATAFUSION_EXECUTION_ADAPTIVE_FILTER_REORDERING=true
+# Shared template for every predicate_eval benchmark. Each qNN.benchmark sets its
+# `subgroup` directive and includes this template with parameters:
+# QUERY query file stem under queries/ (e.g. costsel/q01)
+# DATASET load script stem under load/ (e.g. markers)
+# NAME criterion display name (e.g. costsel_q01_regexp_selective_last)
+# Optional, read by the load scripts as ${...:-default}:
+# PRED_ROWS synthetic row count (default 1000000)
+# PRED_FILL filler chars per marker, the string-width knob (default 30)
+# The knob sweeps reuse another subgroup's query file through QUERY, and the table
+# is always named `t`, so cleanup is uniform across datasets.
load sql_benchmarks/predicate_eval/load/${DATASET}.sql
name ${NAME}
group predicate_eval
-assert I
-SELECT count(*) > 0 FROM t;
-----
-true
+run sql_benchmarks/predicate_eval/queries/${QUERY}.sql
-run sql_benchmarks/predicate_eval/queries/${SUBGROUP}/q${QPAD}.sql
+result sql_benchmarks/predicate_eval/results/${NAME}.csv
result sql_benchmarks/predicate_eval/results/${NAME}.csv
diff --git a/benchmarks/sql_benchmarks/predicate_eval/predicate_eval.suite b/benchmarks/sql_benchmarks/predicate_eval/predicate_eval.suite
index af1a326cd8..4ee33994d3 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/predicate_eval.suite
+++ b/benchmarks/sql_benchmarks/predicate_eval/predicate_eval.suite
@@ -1,4 +1,4 @@
-description = "Conjunctive filter evaluation micro-benchmarks covering predicate cost, selectivity, cardinality, width, scale, correlation, and drift"
+description = "Conjunctive filter evaluation micro-benchmarks covering predicate cost, selectivity, cardinality, width, scale, correlation, drift, and nullable predicates"
query_pattern = "q{QUERY_ID_PADDED}.benchmark"
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q30.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q30.sql
index 3be840e917..dbd410f957 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q30.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q30.sql
@@ -1,6 +1,4 @@
--- Hidden: cheap integer compares; `c1 < 5` matches ~5%, the `c0 < 90` family
--- ~90%. k = 2 here. q30..q33 sweep k = 2/4/8/16 with one ~5% predicate written
--- last among ~90% ones.
+-- k = 2: one ~90% compare then one ~5% compare (q30..q33 sweep k = 2/4/8/16).
SELECT count(*) FROM t
WHERE c0 < 90
AND c1 < 5;
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q31.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q31.sql
index 4ba84f8124..d21dd6273e 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q31.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q31.sql
@@ -1,4 +1,4 @@
--- k = 4: three ~90% compares followed by one ~5% compare. See q30.
+-- k = 4: three ~90% compares then one ~5% compare.
SELECT count(*) FROM t
WHERE c0 < 90
AND c1 < 90
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q32.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q32.sql
index d9e920cc62..83037bdfc5 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q32.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q32.sql
@@ -1,4 +1,4 @@
--- k = 8: seven ~90% compares followed by one ~5% compare. See q30.
+-- k = 8: seven ~90% compares then one ~5% compare.
SELECT count(*) FROM t
WHERE c0 < 90
AND c1 < 90
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q33.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q33.sql
index 2408427ab7..890ff4205d 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q33.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q33.sql
@@ -1,4 +1,4 @@
--- k = 16: fifteen ~90% compares followed by one ~5% compare. See q30.
+-- k = 16: fifteen ~90% compares then one ~5% compare.
SELECT count(*) FROM t
WHERE c0 < 90
AND c1 < 90
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q32.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q34.sql
similarity index 66%
copy from benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q32.sql
copy to benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q34.sql
index d9e920cc62..2c5b586e54 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q32.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/cardinality/q34.sql
@@ -1,4 +1,4 @@
--- k = 8: seven ~90% compares followed by one ~5% compare. See q30.
+-- k = 8 as in q32, but over the 64-column `ints_wide` table.
SELECT count(*) FROM t
WHERE c0 < 90
AND c1 < 90
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/correlation/q70.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/correlation/q70.sql
index 86e33534c7..f9d7017e61 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/correlation/q70.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/correlation/q70.sql
@@ -1,6 +1,4 @@
--- Hidden: `x` and `ind` are independent, each ~20%, so the conjunction matches
--- ~4% and the second predicate is just as selective among the first's survivors
--- as on its own. Baseline for the correlation sweep. cf. q71, q72.
+-- Independent: `x` and `ind` are uncorrelated, each ~20%, so the pair matches ~4%.
SELECT count(*) FROM t
WHERE x < 20
AND ind < 20;
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/correlation/q71.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/correlation/q71.sql
index eda61cc289..4b5925e80a 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/correlation/q71.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/correlation/q71.sql
@@ -1,6 +1,4 @@
--- Hidden: `x_pos` is a copy of `x`, so `x < 20 AND x_pos < 20` still matches
--- ~20% (not the ~4% independence would imply) -- the second predicate removes
--- none of the first's survivors. cf. q70.
+-- Positively correlated: `x_pos` is a copy of `x`, so the pair still matches ~20%.
SELECT count(*) FROM t
WHERE x < 20
AND x_pos < 20;
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/correlation/q72.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/correlation/q72.sql
index ff987524da..63ec8d57de 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/correlation/q72.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/correlation/q72.sql
@@ -1,6 +1,4 @@
--- Hidden: `x_anti` is `99 - x`, so `x < 50 AND x_anti < 50` is empty -- the
--- second predicate removes all of the first's survivors, though each matches
--- ~50% alone. cf. q70.
+-- Anti-correlated: `x_anti` is `99 - x`, so the pair is empty though each matches ~50%.
SELECT count(*) FROM t
WHERE x < 50
AND x_anti < 50;
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/correlation/q73.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/correlation/q73.sql
index 5e1e822e92..6dc76a8cab 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/correlation/q73.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/correlation/q73.sql
@@ -1,11 +1,5 @@
--- Hidden: `c0 = 1` is a perfect proxy for the s1/s2/s3 regexes -- after the
--- cheap proxy, each of those keeps every survivor while the equally selective
--- (~30%) s4 regex still discards ~70%. The optimal order is [c0, s4, s1/s2/s3]
--- (one informative regex on 30% of rows, the three redundant ones on 9%), but
--- the four regexes are marginally identical -- same width, same marker offset,
--- same cost, same selectivity -- so ranking them takes their *joint*
--- distribution with the proxy. Written with the redundant regexes first,
--- grouped with their proxy, as an author naturally would.
+-- Redundant proxy: `c0 = 1` implies the s1/s2/s3 regexes, while the marginally
+-- identical s4 regex is independent of it. Written proxy-first, regexes grouped.
SELECT count(*) FROM t
WHERE c0 = 1
AND regexp_like(s1, 'a.a')
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/cost/q10.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/cost/q10.sql
index b089ebc7a1..cbe0e937ef 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/cost/q10.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/cost/q10.sql
@@ -1,6 +1,4 @@
--- Hidden: both predicates match ~10%, but `regexp_like(s, 'ten')` scans the
--- string (expensive) while `c0 < 10` is a cheap compare. Equal selectivity,
--- unequal cost; expensive one written first. cf. q11 (opposite order).
+-- Equal selectivity (~10%), unequal cost: the expensive regexp written first. cf. q11.
SELECT count(*) FROM t
WHERE regexp_like(s, 'ten')
AND c0 < 10;
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/cost/q11.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/cost/q11.sql
index 82d748c93b..54167ff194 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/cost/q11.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/cost/q11.sql
@@ -1,5 +1,4 @@
--- Same two predicates as q10 (both ~10%; regexp expensive, compare cheap),
--- opposite written order. cf. q10.
+-- q10 with the cheap compare written first.
SELECT count(*) FROM t
WHERE c0 < 10
AND regexp_like(s, 'ten');
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/costsel/q01.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/costsel/q01.sql
index bc029ed5d8..1618c54814 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/costsel/q01.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/costsel/q01.sql
@@ -1,7 +1,5 @@
--- Hidden in the data: the five markers have very different selectivities --
--- 'aaa' ~90%, 'bbb' ~86%, 'ccc' ~80%, 'ddd' ~75%, 'rare' ~0.1% -- while every
--- regexp_like costs about the same. 'rare' (most selective) is written last.
--- cf. q02 (most selective written first).
+-- Five equally expensive regexps of very different selectivity ('rare' ~0.1%, the
+-- rest 75-90%), the selective one written last. cf. q02.
SELECT count(*) FROM t
WHERE regexp_like(s, 'aaa')
AND regexp_like(s, 'bbb')
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/costsel/q02.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/costsel/q02.sql
index 7f7fc61831..dc57aa8d88 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/costsel/q02.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/costsel/q02.sql
@@ -1,5 +1,4 @@
--- Same predicates and hidden selectivities as q01 ('rare' ~0.1% is the
--- selective one, the rest 75-90%), but with 'rare' written first. cf. q01.
+-- q01 with the selective regexp written first.
SELECT count(*) FROM t
WHERE regexp_like(s, 'rare')
AND regexp_like(s, 'aaa')
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/costsel/q03.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/costsel/q03.sql
index a583a498b2..a9d110c65a 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/costsel/q03.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/costsel/q03.sql
@@ -1,6 +1,4 @@
--- Hidden: `c0 < 90` matches ~90% (cheap integer compare); `regexp_like(s,
--- 'rare')` matches ~0.1% (scans the wide string). The cheaper predicate is the
--- less selective one.
+-- Cheap unselective compare (~90%) then expensive selective regexp (~0.1%). cf. q04.
SELECT count(*) FROM t
WHERE c0 < 90
AND regexp_like(s, 'rare');
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/costsel/q04.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/costsel/q04.sql
new file mode 100644
index 0000000000..2c5a253664
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/costsel/q04.sql
@@ -0,0 +1,4 @@
+-- q03 the other way round, so the as-written order is already the best one.
+SELECT count(*) FROM t
+WHERE regexp_like(s, 'rare')
+ AND c0 < 90;
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/drift/q80.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/drift/q80.sql
index b8cb61e85a..63e55ce473 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/drift/q80.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/drift/q80.sql
@@ -1,7 +1,5 @@
--- The non-obvious property: selectivity changes across the scan. Rows arrive in
--- `seq` order; `a_sel = 0` matches ~0.1% in the first 10% of rows and ~50%
--- after, `b_sel = 0` is the mirror -- so which predicate is more selective flips
--- partway through. cf. q81 (opposite order).
+-- Selectivity drifts across the scan: `a_sel = 0` matches ~0.1% over the first 2%
+-- of rows and ~50% after, and `b_sel = 0` is the mirror.
SELECT count(*) FROM t
WHERE a_sel = 0
AND b_sel = 0;
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/drift/q81.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/drift/q81.sql
deleted file mode 100644
index d65ef475cc..0000000000
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/drift/q81.sql
+++ /dev/null
@@ -1,5 +0,0 @@
--- Same drifting predicates as q80 (a_sel/b_sel flip which is more selective
--- partway through the scan), opposite written order. cf. q80.
-SELECT count(*) FROM t
-WHERE b_sel = 0
- AND a_sel = 0;
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/drift/q82.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/drift/q82.sql
new file mode 100644
index 0000000000..57bb65d216
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/drift/q82.sql
@@ -0,0 +1,5 @@
+-- Late flip: one stream reads f=00..f=15 in order, so the drift lands halfway
+-- through the scan and a warm-up-and-freeze decision is wrong for half the rows.
+SELECT count(*) FROM t
+WHERE a_sel = 0
+ AND b_sel = 0;
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/drift/q83.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/drift/q83.sql
new file mode 100644
index 0000000000..7ecc0fb1c5
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/drift/q83.sql
@@ -0,0 +1,5 @@
+-- Per-partition skew: one whole file per stream, so each stream sees a single fixed
+-- profile and one pooled decision is backwards for half of them.
+SELECT count(*) FROM t
+WHERE a_sel = 0
+ AND b_sel = 0;
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/neutral/q60.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/neutral/q60.sql
index b217f56953..76f5a154da 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/neutral/q60.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/neutral/q60.sql
@@ -1,5 +1,4 @@
--- Hidden: four integer compares of equal cost, each ~50% selective. Nothing is
--- selective and the costs are equal, so the predicates are interchangeable.
+-- Four equally cheap compares, each ~50%: nothing to reorder.
SELECT count(*) FROM t
WHERE c0 < 50
AND c1 < 50
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/neutral/q61.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/neutral/q61.sql
index 7029a3d9f8..43225f2e48 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/neutral/q61.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/neutral/q61.sql
@@ -1,6 +1,4 @@
--- Hidden: four regexp scans of about equal cost, all unselective ('aaa' ~90%,
--- 'bbb' ~86%, 'ccc' ~80%, 'ddd' ~75%). Like q60 the predicates are
--- interchangeable, but here each one is expensive.
+-- Four equally expensive regexps, all unselective (75-90%): nothing to reorder.
SELECT count(*) FROM t
WHERE regexp_like(s, 'aaa')
AND regexp_like(s, 'bbb')
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/nulls/q90.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/nulls/q90.sql
new file mode 100644
index 0000000000..ce4e40d9ac
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/nulls/q90.sql
@@ -0,0 +1,6 @@
+-- The selective conjunct (~4%) is nullable, so AND pre-selection is disabled and
+-- writing it first gates nothing. cf. q91.
+SELECT count(*) FROM t
+WHERE c_sel < 5
+ AND c0 < 90
+ AND c1 < 90;
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/nulls/q91.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/nulls/q91.sql
new file mode 100644
index 0000000000..abca8bb1f9
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/nulls/q91.sql
@@ -0,0 +1,5 @@
+-- q90 with the nullable selective conjunct written last, which is the same work.
+SELECT count(*) FROM t
+WHERE c0 < 90
+ AND c1 < 90
+ AND c_sel < 5;
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/scale/q50.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/scale/q50.sql
deleted file mode 100644
index 03a0f1c0db..0000000000
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/scale/q50.sql
+++ /dev/null
@@ -1,6 +0,0 @@
--- Same predicates as costsel/q03 (`c0 < 90` ~90% cheap, `regexp_like(s, 'rare')`
--- ~0.1% expensive). q50..q53 sweep table size; here PRED_ROWS=5_000, roughly a
--- single batch.
-SELECT count(*) FROM t
-WHERE c0 < 90
- AND regexp_like(s, 'rare');
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/scale/q51.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/scale/q51.sql
deleted file mode 100644
index 28174a5df4..0000000000
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/scale/q51.sql
+++ /dev/null
@@ -1,4 +0,0 @@
--- q50 at PRED_ROWS=100_000 (~12 batches). See q50.
-SELECT count(*) FROM t
-WHERE c0 < 90
- AND regexp_like(s, 'rare');
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/scale/q52.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/scale/q52.sql
deleted file mode 100644
index 74938c4634..0000000000
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/scale/q52.sql
+++ /dev/null
@@ -1,4 +0,0 @@
--- q50 at PRED_ROWS=5_000_000 (~610 batches). See q50.
-SELECT count(*) FROM t
-WHERE c0 < 90
- AND regexp_like(s, 'rare');
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/scale/q53.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/scale/q53.sql
deleted file mode 100644
index 8edb4d4a05..0000000000
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/scale/q53.sql
+++ /dev/null
@@ -1,4 +0,0 @@
--- q50 at PRED_ROWS=50_000_000 (~6100 batches); builds a ~9 GB table. See q50.
-SELECT count(*) FROM t
-WHERE c0 < 90
- AND regexp_like(s, 'rare');
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/selectivity/q20.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/selectivity/q20.sql
index 3638f757a7..b834a4bd7b 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/selectivity/q20.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/selectivity/q20.sql
@@ -1,6 +1,5 @@
--- Hidden: two equally cheap integer compares of unequal selectivity -- `c4 < 95`
--- matches ~95%, `c0 < 5` matches ~5%. Less selective one written first.
--- cf. q21 (opposite order).
+-- Two equally cheap compares of unequal selectivity (~95%, ~5%), less selective
+-- first. cf. q21.
SELECT count(*) FROM t
WHERE c4 < 95
AND c0 < 5;
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/selectivity/q21.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/selectivity/q21.sql
index 5181faf387..1f8531a510 100644
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/selectivity/q21.sql
+++ b/benchmarks/sql_benchmarks/predicate_eval/queries/selectivity/q21.sql
@@ -1,5 +1,4 @@
--- Same two equally-cheap compares as q20 (`c4 < 95` ~95%, `c0 < 5` ~5%),
--- opposite written order. cf. q20.
+-- q20 with the selective compare written first.
SELECT count(*) FROM t
WHERE c0 < 5
AND c4 < 95;
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/width/q40.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/width/q40.sql
deleted file mode 100644
index 1b3df3e937..0000000000
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/width/q40.sql
+++ /dev/null
@@ -1,9 +0,0 @@
--- Same predicate set and hidden selectivities as costsel/q01 ('rare' ~0.1%, the
--- rest 75-90%); only the string-column width differs across q40/q41/q42. Narrow:
--- PRED_FILL=2, ~12 chars/row.
-SELECT count(*) FROM t
-WHERE regexp_like(s, 'aaa')
- AND regexp_like(s, 'bbb')
- AND regexp_like(s, 'ccc')
- AND regexp_like(s, 'ddd')
- AND regexp_like(s, 'rare');
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/width/q41.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/width/q41.sql
deleted file mode 100644
index a03b576d9c..0000000000
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/width/q41.sql
+++ /dev/null
@@ -1,7 +0,0 @@
--- q40 with wide strings: PRED_FILL=30, ~186 chars/row. See q40.
-SELECT count(*) FROM t
-WHERE regexp_like(s, 'aaa')
- AND regexp_like(s, 'bbb')
- AND regexp_like(s, 'ccc')
- AND regexp_like(s, 'ddd')
- AND regexp_like(s, 'rare');
diff --git a/benchmarks/sql_benchmarks/predicate_eval/queries/width/q42.sql b/benchmarks/sql_benchmarks/predicate_eval/queries/width/q42.sql
deleted file mode 100644
index cb55d828e3..0000000000
--- a/benchmarks/sql_benchmarks/predicate_eval/queries/width/q42.sql
+++ /dev/null
@@ -1,7 +0,0 @@
--- q40 with extra-wide strings: PRED_FILL=170, ~1KB/row. See q40.
-SELECT count(*) FROM t
-WHERE regexp_like(s, 'aaa')
- AND regexp_like(s, 'bbb')
- AND regexp_like(s, 'ccc')
- AND regexp_like(s, 'ddd')
- AND regexp_like(s, 'rare');
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/cardinality_q30_k2.csv b/benchmarks/sql_benchmarks/predicate_eval/results/cardinality_q30_k2.csv
new file mode 100644
index 0000000000..23d7344bc8
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/cardinality_q30_k2.csv
@@ -0,0 +1,2 @@
+count(*)
+50000
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/cardinality_q31_k4.csv b/benchmarks/sql_benchmarks/predicate_eval/results/cardinality_q31_k4.csv
new file mode 100644
index 0000000000..94f68af265
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/cardinality_q31_k4.csv
@@ -0,0 +1,2 @@
+count(*)
+40000
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/cardinality_q32_k8.csv b/benchmarks/sql_benchmarks/predicate_eval/results/cardinality_q32_k8.csv
new file mode 100644
index 0000000000..23d7344bc8
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/cardinality_q32_k8.csv
@@ -0,0 +1,2 @@
+count(*)
+50000
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/cardinality_q33_k16.csv b/benchmarks/sql_benchmarks/predicate_eval/results/cardinality_q33_k16.csv
new file mode 100644
index 0000000000..6291042419
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/cardinality_q33_k16.csv
@@ -0,0 +1,2 @@
+count(*)
+10000
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/cardinality_q34_k8_wide64.csv b/benchmarks/sql_benchmarks/predicate_eval/results/cardinality_q34_k8_wide64.csv
new file mode 100644
index 0000000000..23d7344bc8
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/cardinality_q34_k8_wide64.csv
@@ -0,0 +1,2 @@
+count(*)
+50000
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/correlation_q70_independent.csv b/benchmarks/sql_benchmarks/predicate_eval/results/correlation_q70_independent.csv
new file mode 100644
index 0000000000..23d7344bc8
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/correlation_q70_independent.csv
@@ -0,0 +1,2 @@
+count(*)
+50000
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/correlation_q71_positive.csv b/benchmarks/sql_benchmarks/predicate_eval/results/correlation_q71_positive.csv
new file mode 100644
index 0000000000..e3795c1d30
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/correlation_q71_positive.csv
@@ -0,0 +1,2 @@
+count(*)
+200000
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/correlation_q72_anti.csv b/benchmarks/sql_benchmarks/predicate_eval/results/correlation_q72_anti.csv
new file mode 100644
index 0000000000..4303b90458
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/correlation_q72_anti.csv
@@ -0,0 +1,2 @@
+count(*)
+0
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/correlation_q73_redundant_proxy.csv b/benchmarks/sql_benchmarks/predicate_eval/results/correlation_q73_redundant_proxy.csv
new file mode 100644
index 0000000000..1cabb81336
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/correlation_q73_redundant_proxy.csv
@@ -0,0 +1,2 @@
+count(*)
+90000
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/cost_q10_expensive_first.csv b/benchmarks/sql_benchmarks/predicate_eval/results/cost_q10_expensive_first.csv
new file mode 100644
index 0000000000..6291042419
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/cost_q10_expensive_first.csv
@@ -0,0 +1,2 @@
+count(*)
+10000
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/cost_q11_cheap_first.csv b/benchmarks/sql_benchmarks/predicate_eval/results/cost_q11_cheap_first.csv
new file mode 100644
index 0000000000..6291042419
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/cost_q11_cheap_first.csv
@@ -0,0 +1,2 @@
+count(*)
+10000
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/costsel_q01_regexp_selective_last.csv b/benchmarks/sql_benchmarks/predicate_eval/results/costsel_q01_regexp_selective_last.csv
new file mode 100644
index 0000000000..5eb0b761fa
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/costsel_q01_regexp_selective_last.csv
@@ -0,0 +1,2 @@
+count(*)
+508
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/costsel_q02_regexp_selective_first.csv b/benchmarks/sql_benchmarks/predicate_eval/results/costsel_q02_regexp_selective_first.csv
new file mode 100644
index 0000000000..5eb0b761fa
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/costsel_q02_regexp_selective_first.csv
@@ -0,0 +1,2 @@
+count(*)
+508
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/costsel_q03_cheap_unselective_then_expensive_selective.csv b/benchmarks/sql_benchmarks/predicate_eval/results/costsel_q03_cheap_unselective_then_expensive_selective.csv
new file mode 100644
index 0000000000..1c82d80b6f
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/costsel_q03_cheap_unselective_then_expensive_selective.csv
@@ -0,0 +1,2 @@
+count(*)
+893
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/costsel_q04_expensive_selective_then_cheap_unselective.csv b/benchmarks/sql_benchmarks/predicate_eval/results/costsel_q04_expensive_selective_then_cheap_unselective.csv
new file mode 100644
index 0000000000..1c82d80b6f
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/costsel_q04_expensive_selective_then_cheap_unselective.csv
@@ -0,0 +1,2 @@
+count(*)
+893
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/drift_q80_a_then_b.csv b/benchmarks/sql_benchmarks/predicate_eval/results/drift_q80_a_then_b.csv
new file mode 100644
index 0000000000..c543899cb1
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/drift_q80_a_then_b.csv
@@ -0,0 +1,2 @@
+count(*)
+1000
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/drift_q82_late_flip.csv b/benchmarks/sql_benchmarks/predicate_eval/results/drift_q82_late_flip.csv
new file mode 100644
index 0000000000..571ea61b27
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/drift_q82_late_flip.csv
@@ -0,0 +1,2 @@
+count(*)
+992
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/drift_q83_per_partition_skew.csv b/benchmarks/sql_benchmarks/predicate_eval/results/drift_q83_per_partition_skew.csv
new file mode 100644
index 0000000000..571ea61b27
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/drift_q83_per_partition_skew.csv
@@ -0,0 +1,2 @@
+count(*)
+992
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/neutral_q60_cheap_uniform.csv b/benchmarks/sql_benchmarks/predicate_eval/results/neutral_q60_cheap_uniform.csv
new file mode 100644
index 0000000000..a1c39233b5
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/neutral_q60_cheap_uniform.csv
@@ -0,0 +1,2 @@
+count(*)
+150000
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/neutral_q61_expensive_uniform.csv b/benchmarks/sql_benchmarks/predicate_eval/results/neutral_q61_expensive_uniform.csv
new file mode 100644
index 0000000000..5d7624926c
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/neutral_q61_expensive_uniform.csv
@@ -0,0 +1,2 @@
+count(*)
+514286
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/nulls_q90_nullable_selective_first.csv b/benchmarks/sql_benchmarks/predicate_eval/results/nulls_q90_nullable_selective_first.csv
new file mode 100644
index 0000000000..ce62ca3d04
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/nulls_q90_nullable_selective_first.csv
@@ -0,0 +1,2 @@
+count(*)
+20000
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/nulls_q91_nullable_selective_last.csv b/benchmarks/sql_benchmarks/predicate_eval/results/nulls_q91_nullable_selective_last.csv
new file mode 100644
index 0000000000..ce62ca3d04
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/nulls_q91_nullable_selective_last.csv
@@ -0,0 +1,2 @@
+count(*)
+20000
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/scale_q50_5k.csv b/benchmarks/sql_benchmarks/predicate_eval/results/scale_q50_5k.csv
new file mode 100644
index 0000000000..8cedd33498
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/scale_q50_5k.csv
@@ -0,0 +1,2 @@
+count(*)
+5
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/scale_q51_100k.csv b/benchmarks/sql_benchmarks/predicate_eval/results/scale_q51_100k.csv
new file mode 100644
index 0000000000..5066201ce8
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/scale_q51_100k.csv
@@ -0,0 +1,2 @@
+count(*)
+90
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/scale_q52_5m.csv b/benchmarks/sql_benchmarks/predicate_eval/results/scale_q52_5m.csv
new file mode 100644
index 0000000000..41c4f54ec1
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/scale_q52_5m.csv
@@ -0,0 +1,2 @@
+count(*)
+4461
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/scale_q53_50m.csv b/benchmarks/sql_benchmarks/predicate_eval/results/scale_q53_50m.csv
new file mode 100644
index 0000000000..defd47ee70
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/scale_q53_50m.csv
@@ -0,0 +1,2 @@
+count(*)
+44600
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/selectivity_q20_unselective_first.csv b/benchmarks/sql_benchmarks/predicate_eval/results/selectivity_q20_unselective_first.csv
new file mode 100644
index 0000000000..23d7344bc8
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/selectivity_q20_unselective_first.csv
@@ -0,0 +1,2 @@
+count(*)
+50000
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/selectivity_q21_selective_first.csv b/benchmarks/sql_benchmarks/predicate_eval/results/selectivity_q21_selective_first.csv
new file mode 100644
index 0000000000..23d7344bc8
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/selectivity_q21_selective_first.csv
@@ -0,0 +1,2 @@
+count(*)
+50000
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/width_q40_narrow.csv b/benchmarks/sql_benchmarks/predicate_eval/results/width_q40_narrow.csv
new file mode 100644
index 0000000000..5eb0b761fa
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/width_q40_narrow.csv
@@ -0,0 +1,2 @@
+count(*)
+508
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/width_q41_wide.csv b/benchmarks/sql_benchmarks/predicate_eval/results/width_q41_wide.csv
new file mode 100644
index 0000000000..5eb0b761fa
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/width_q41_wide.csv
@@ -0,0 +1,2 @@
+count(*)
+508
diff --git a/benchmarks/sql_benchmarks/predicate_eval/results/width_q42_xwide.csv b/benchmarks/sql_benchmarks/predicate_eval/results/width_q42_xwide.csv
new file mode 100644
index 0000000000..5eb0b761fa
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/results/width_q42_xwide.csv
@@ -0,0 +1,2 @@
+count(*)
+508
diff --git a/benchmarks/sql_benchmarks/predicate_eval/scratch/.gitignore b/benchmarks/sql_benchmarks/predicate_eval/scratch/.gitignore
new file mode 100644
index 0000000000..4bed5da93f
--- /dev/null
+++ b/benchmarks/sql_benchmarks/predicate_eval/scratch/.gitignore
@@ -0,0 +1 @@
+*.parquet
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org
(datafusion) 03/05: perf: emit unmatched build rows in batch_size chunks in HashJoinExec (#25028)
Posted by gi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-24976-9f21155c48faac3f1658853ad6a55bd314a322ed
in repository https://gitbox.apache.org/repos/asf/datafusion.git
commit cc61eeaa5eb775258e1d520dfbb6622c436aaf42
Author: Jay Zhan <ja...@gmail.com>
AuthorDate: Wed Sep 9 14:07:17 2026 +0000
perf: emit unmatched build rows in batch_size chunks in HashJoinExec (#25028)
## Which issue does this PR close?
- Related to #24768 (bounded-memory hash join) and #18942 (hash join
operator optimizations); no dedicated issue.
## Rationale for this change
For join types that emit build-side rows after the probe side is
exhausted (`Left`, `Full`, `LeftAnti`, `LeftSemi`, `LeftMark`),
`HashJoinExec` computed the final indices over the *whole* build side
and materialized them as **one** `RecordBatch` before handing it to the
output coalescer.
That batch is not bounded by `batch_size`, and it is not covered by the
memory reservation. Worse, `LimitedBatchCoalescer` configures arrow's
`BatchCoalescer` with `biggest_coalesce_batch_size = batch_size / 2`,
which passes any larger batch through untouched. So a `LEFT ANTI` join
over a 10M-row build side with few matches emitted a single ~10M-row
batch downstream, regardless of `datafusion.execution.batch_size`.
`NestedLoopJoinExec` already emits its unmatched build rows in
`batch_size` chunks; this PR brings `HashJoinExec` in line.
## What changes are included in this PR?
- `HashJoinStream` gets a new state, `EmitUnmatchedBuildRows`, entered
from `ExhaustedProbeSide` by the last probe partition. It holds a
`BooleanBuffer` snapshot of the visited bitmap (taken once, after every
partition reported completion, so the lock is not held while emitting)
and a cursor.
- `next_final_indices_chunk` scans the snapshot from the cursor and
returns at most `batch_size` final indices per call (`LeftMark` emits
every row, so its chunks are plain ranges). Null-aware
`LeftAnti`/`LeftMark` post-processing and `fetch` handling are unchanged
and now run per chunk.
- `input_batches` is still bumped once for the final phase and
`input_rows` once per chunk, so metric values are identical to before.
- `get_final_indices_from_bit_map` /
`get_final_indices_from_shared_bitmap` in `joins/utils.rs` had no other
callers and are removed.
- New benchmark cases in `hash_join_semi_anti.rs` with a 1M-row build
side and a 100K-row probe side (`left_semi_build1m_h10`,
`left_anti_build1m_h10`, `left_build1m_h10`).
Benchmark (this branch vs. `main`, Apple Silicon, `cargo bench --bench
hash_join_semi_anti -- build1m`):
| case | main | this PR | change |
|---|---|---|---|
| left_semi_build1m_h10 | 2.05 ms | 1.97 ms | -4% |
| left_anti_build1m_h10 | 6.21 ms | 4.75 ms | -24% |
| left_build1m_h10 | 7.37 ms | 6.79 ms | -8% |
Peak RSS of the `left_anti_build1m_h10` bench binary: ~369 MB on `main`
vs ~160 MB on this branch (`/usr/bin/time -l`, 1M build rows, 900K
unmatched).
## Are these changes tested?
- New `join_emits_final_build_rows_in_batch_size_chunks` test
(Left/Full/LeftAnti/LeftSemi/LeftMark × batch sizes 1/7/8192 ×
perfect-hash-join on/off) asserts the output rows and that every output
batch respects `batch_size`. On `main` 14 of its 30 cases fail the
batch-size assertion.
- New `join_fetch_stops_final_build_rows_mid_chunk` test checks a
`fetch` that is reached in the middle of the final rows.
- Existing hash join unit tests (493), join sqllogictests (`joins`,
`join_limit_pushdown`, `join_disable_repartition_joins`, `subquery`),
and the core join fuzz tests pass.
## Are there any user-facing changes?
No result changes. Output batches of the affected join types are now
bounded by `batch_size` instead of arriving as one batch holding every
unmatched build row.
---
.../physical-plan/benches/hash_join_semi_anti.rs | 59 +++++++
.../physical-plan/src/joins/hash_join/exec.rs | 162 ++++++++++++++++++-
.../physical-plan/src/joins/hash_join/stream.rs | 174 ++++++++++++++++++---
datafusion/physical-plan/src/joins/utils.rs | 56 -------
4 files changed, 374 insertions(+), 77 deletions(-)
diff --git a/datafusion/physical-plan/benches/hash_join_semi_anti.rs b/datafusion/physical-plan/benches/hash_join_semi_anti.rs
index 1e11da36be..40ba41272c 100644
--- a/datafusion/physical-plan/benches/hash_join_semi_anti.rs
+++ b/datafusion/physical-plan/benches/hash_join_semi_anti.rs
@@ -380,6 +380,65 @@ fn bench_hash_join_semi_anti(c: &mut Criterion) {
});
}
+ // =========================================================================
+ // Build-side output benchmarks (LeftSemi / LeftAnti / Left)
+ // =========================================================================
+ //
+ // These join types emit build-side rows only after the probe side is
+ // exhausted, so they exercise the final "unmatched build rows" emission.
+ // Build side: 1M rows, Probe side: 100K rows, so most build rows stay
+ // unmatched and the final emission dominates.
+ let large_build_rows = 1_000_000;
+ let small_probe_rows = 100_000;
+
+ // LeftSemi - 10% of build rows matched -> 100K output rows
+ {
+ let left_batches = build_batches(large_build_rows, large_build_rows, 0, &s);
+ let right_batches = build_batches(small_probe_rows, small_probe_rows, 0, &s);
+ group.bench_function(
+ BenchmarkId::new("left_semi_build1m_h10", small_probe_rows),
+ |b| {
+ b.iter(|| {
+ let left = make_exec(&left_batches, &s);
+ let right = make_exec(&right_batches, &s);
+ do_hash_join(left, right, JoinType::LeftSemi, &rt)
+ })
+ },
+ );
+ }
+
+ // LeftAnti - 10% of build rows matched -> 900K output rows
+ {
+ let left_batches = build_batches(large_build_rows, large_build_rows, 0, &s);
+ let right_batches = build_batches(small_probe_rows, small_probe_rows, 0, &s);
+ group.bench_function(
+ BenchmarkId::new("left_anti_build1m_h10", small_probe_rows),
+ |b| {
+ b.iter(|| {
+ let left = make_exec(&left_batches, &s);
+ let right = make_exec(&right_batches, &s);
+ do_hash_join(left, right, JoinType::LeftAnti, &rt)
+ })
+ },
+ );
+ }
+
+ // Left - 10% of build rows matched -> 100K matched + 900K unmatched output rows
+ {
+ let left_batches = build_batches(large_build_rows, large_build_rows, 0, &s);
+ let right_batches = build_batches(small_probe_rows, small_probe_rows, 0, &s);
+ group.bench_function(
+ BenchmarkId::new("left_build1m_h10", small_probe_rows),
+ |b| {
+ b.iter(|| {
+ let left = make_exec(&left_batches, &s);
+ let right = make_exec(&right_batches, &s);
+ do_hash_join(left, right, JoinType::Left, &rt)
+ })
+ },
+ );
+ }
+
group.finish();
}
diff --git a/datafusion/physical-plan/src/joins/hash_join/exec.rs b/datafusion/physical-plan/src/joins/hash_join/exec.rs
index 5ead46ed63..01dec6570b 100644
--- a/datafusion/physical-plan/src/joins/hash_join/exec.rs
+++ b/datafusion/physical-plan/src/joins/hash_join/exec.rs
@@ -3041,11 +3041,11 @@ mod tests {
};
use arrow::array::{
- Array, ArrayRef, Date32Array, DictionaryArray, Int32Array, Int64Array,
+ Array, ArrayRef, AsArray, Date32Array, DictionaryArray, Int32Array, Int64Array,
StructArray, UInt32Array, UInt64Array,
};
use arrow::buffer::NullBuffer;
- use arrow::datatypes::{DataType, Field};
+ use arrow::datatypes::{DataType, Field, Int32Type};
use datafusion_common::hash_utils::create_hashes;
use datafusion_common::test_util::{batches_to_sort_string, batches_to_string};
use datafusion_common::{
@@ -4127,6 +4127,164 @@ mod tests {
return Ok(());
}
+ /// Build side keyed `0..num_build_rows`, probe side holding only
+ /// `matched_keys`, joined on the key column.
+ fn final_build_rows_inputs(
+ num_build_rows: i32,
+ matched_keys: &[i32],
+ ) -> (Arc<dyn ExecutionPlan>, Arc<dyn ExecutionPlan>, JoinOn) {
+ let build_keys: Vec<i32> = (0..num_build_rows).collect();
+ let probe_keys = matched_keys.to_vec();
+ let left = build_table(
+ ("a1", &build_keys),
+ ("b1", &build_keys),
+ ("c1", &build_keys),
+ );
+ let right = build_table(
+ ("a2", &probe_keys),
+ ("b2", &probe_keys),
+ ("c2", &probe_keys),
+ );
+ let on = vec![(
+ Arc::new(Column::new_with_schema("b1", &left.schema()).unwrap()) as _,
+ Arc::new(Column::new_with_schema("b2", &right.schema()).unwrap()) as _,
+ )];
+ (left, right, on)
+ }
+
+ /// Returns the sorted `(a1, matched)` pairs of the joined output, where
+ /// `matched` is read from the mark column for `LeftMark`, from the
+ /// presence of probe-side data for `Left`/`Full`, and is `None` for
+ /// existence joins whose output carries no match information.
+ fn build_rows_with_match_flag(batches: &[RecordBatch]) -> Vec<(i32, Option<bool>)> {
+ let mut rows = vec![];
+ for batch in batches {
+ let a1 = batch
+ .column_by_name("a1")
+ .unwrap()
+ .as_primitive::<Int32Type>();
+ let matched: Vec<Option<bool>> = match batch.column_by_name("mark") {
+ Some(mark) => mark.as_boolean().iter().collect(),
+ None => match batch.column_by_name("a2") {
+ Some(a2) => (0..a2.len()).map(|i| Some(a2.is_valid(i))).collect(),
+ None => vec![None; batch.num_rows()],
+ },
+ };
+ rows.extend(a1.values().iter().copied().zip(matched));
+ }
+ rows.sort_unstable();
+ rows
+ }
+
+ /// The `(a1, matched)` pairs a join of [`final_build_rows_inputs`] must
+ /// produce, in the format of [`build_rows_with_match_flag`].
+ fn expected_final_build_rows(
+ join_type: JoinType,
+ num_build_rows: i32,
+ matched_keys: &[i32],
+ ) -> Vec<(i32, Option<bool>)> {
+ (0..num_build_rows)
+ .filter_map(|key| {
+ let matched = matched_keys.contains(&key);
+ match join_type {
+ JoinType::LeftSemi => matched.then_some((key, None)),
+ JoinType::LeftAnti => (!matched).then_some((key, None)),
+ _ => Some((key, Some(matched))),
+ }
+ })
+ .collect()
+ }
+
+ /// The final build-side rows (unmatched rows, or matched rows for
+ /// `LeftSemi`) must be emitted in chunks bounded by `batch_size` instead
+ /// of one batch over the whole build side.
+ #[rstest]
+ #[tokio::test]
+ async fn join_emits_final_build_rows_in_batch_size_chunks(
+ #[values(
+ JoinType::Left,
+ JoinType::Full,
+ JoinType::LeftAnti,
+ JoinType::LeftSemi,
+ JoinType::LeftMark
+ )]
+ join_type: JoinType,
+ #[values(1, 7, 8192)] batch_size: usize,
+ #[values(true, false)] use_perfect_hash_join_as_possible: bool,
+ ) -> Result<()> {
+ let task_ctx = prepare_task_ctx(batch_size, use_perfect_hash_join_as_possible);
+ let num_build_rows = 100;
+ let matched_keys = [3, 50, 97];
+ let (left, right, on) = final_build_rows_inputs(num_build_rows, &matched_keys);
+
+ let (_, batches, metrics) = join_collect(
+ left,
+ right,
+ on,
+ &join_type,
+ NullEquality::NullEqualsNothing,
+ task_ctx,
+ )
+ .await?;
+ assert_phj_used(&metrics, use_perfect_hash_join_as_possible);
+
+ let expected =
+ expected_final_build_rows(join_type, num_build_rows, &matched_keys);
+ assert_eq!(build_rows_with_match_flag(&batches), expected);
+
+ for batch in &batches {
+ assert!(
+ batch.num_rows() <= batch_size,
+ "{join_type} join emitted a batch of {} rows with batch_size {batch_size}",
+ batch.num_rows()
+ );
+ }
+ assert!(
+ batches.len() >= expected.len().div_ceil(batch_size),
+ "{join_type} join emitted {} batches for {} rows with batch_size {batch_size}",
+ batches.len(),
+ expected.len()
+ );
+
+ Ok(())
+ }
+
+ /// A `fetch` limit that is reached in the middle of the final build-side
+ /// rows stops the emission at exactly `fetch` rows.
+ #[rstest]
+ #[tokio::test]
+ async fn join_fetch_stops_final_build_rows_mid_chunk(
+ #[values(JoinType::Left, JoinType::LeftAnti, JoinType::LeftMark)]
+ join_type: JoinType,
+ ) -> Result<()> {
+ let batch_size = 7;
+ let fetch = 20;
+ let num_build_rows = 100;
+ let matched_keys = [3, 50, 97];
+ let task_ctx = prepare_task_ctx(batch_size, false);
+ let (left, right, on) = final_build_rows_inputs(num_build_rows, &matched_keys);
+
+ let join = HashJoinExecBuilder::new(left, right, on, join_type)
+ .with_partition_mode(PartitionMode::CollectLeft)
+ .with_fetch(Some(fetch))
+ .build()?;
+ let batches = common::collect(join.execute(0, task_ctx)?).await?;
+
+ let num_rows: usize = batches.iter().map(|b| b.num_rows()).sum();
+ assert_eq!(num_rows, fetch);
+ for batch in &batches {
+ assert!(batch.num_rows() <= batch_size);
+ }
+ // Every emitted row is a genuine join result row.
+ let expected =
+ expected_final_build_rows(join_type, num_build_rows, &matched_keys);
+ for row in build_rows_with_match_flag(&batches) {
+ assert!(expected.contains(&row), "unexpected output row {row:?}");
+ }
+
+ Ok(())
+ }
+
#[apply(hash_join_exec_configs)]
#[tokio::test]
async fn join_full_multi_batch(
diff --git a/datafusion/physical-plan/src/joins/hash_join/stream.rs b/datafusion/physical-plan/src/joins/hash_join/stream.rs
index ff08828e94..2c1ad94460 100644
--- a/datafusion/physical-plan/src/joins/hash_join/stream.rs
+++ b/datafusion/physical-plan/src/joins/hash_join/stream.rs
@@ -32,9 +32,7 @@ use crate::joins::hash_join::exec::{JoinLeftData, NullAwareMode};
use crate::joins::hash_join::shared_bounds::{
PartitionBounds, PartitionBuildData, SharedBuildAccumulator,
};
-use crate::joins::utils::{
- OnceFut, equal_rows_arr, get_final_indices_from_shared_bitmap, matchable_join_keys,
-};
+use crate::joins::utils::{OnceFut, equal_rows_arr, matchable_join_keys};
use crate::stream::EmptyRecordBatchStream;
use crate::{
RecordBatchStream, SendableRecordBatchStream, handle_state,
@@ -49,7 +47,7 @@ use crate::{
};
use arrow::array::{Array, ArrayRef, UInt32Array, UInt64Array};
-use arrow::buffer::NullBuffer;
+use arrow::buffer::{BooleanBuffer, NullBuffer};
use arrow::datatypes::{Schema, SchemaRef};
use arrow::record_batch::RecordBatch;
use datafusion_common::{
@@ -119,11 +117,18 @@ impl BuildSide {
/// WaitBuildSide
/// │
/// ▼
-/// ┌─► FetchProbeBatch ───► ExhaustedProbeSide ───► Completed
-/// │ │
-/// │ ▼
-/// └─ ProcessProbeBatch
+/// ┌─► FetchProbeBatch ───► ExhaustedProbeSide ──────────► Completed
+/// │ │ │ ▲
+/// │ ▼ ▼ │
+/// └─ ProcessProbeBatch ┌─► EmitUnmatchedBuildRows ───────┘
+/// └──────────┘
/// ```
+///
+/// `ExhaustedProbeSide` moves to `EmitUnmatchedBuildRows` only for join types
+/// that emit build-side rows after the probe side is exhausted (see
+/// [`need_produce_result_in_final`]), and only in the partition that finished
+/// probing last. That state re-enters itself once per emitted chunk of at most
+/// `batch_size` rows.
#[derive(Debug, Clone)]
pub(super) enum HashJoinStreamState {
/// Initial state for HashJoinStream indicating that build-side data not collected yet
@@ -136,6 +141,10 @@ pub(super) enum HashJoinStreamState {
ProcessProbeBatch(ProcessProbeBatchState),
/// Indicates that probe-side has been fully processed
ExhaustedProbeSide,
+ /// Indicates that the probe side has been fully processed by every
+ /// partition, and this stream is emitting the final build-side rows
+ /// (unmatched rows, or matched rows for `LeftSemi`) in chunks
+ EmitUnmatchedBuildRows(EmitUnmatchedBuildRowsState),
/// Indicates that HashJoinStream execution is completed
Completed,
}
@@ -149,6 +158,19 @@ impl HashJoinStreamState {
_ => internal_err!("Expected hash join stream in ProcessProbeBatch state"),
}
}
+
+ /// Tries to extract EmitUnmatchedBuildRowsState from HashJoinStreamState enum.
+ /// Returns an error if state is not EmitUnmatchedBuildRows.
+ fn try_as_emit_unmatched_build_rows_mut(
+ &mut self,
+ ) -> Result<&mut EmitUnmatchedBuildRowsState> {
+ match self {
+ HashJoinStreamState::EmitUnmatchedBuildRows(state) => Ok(state),
+ _ => {
+ internal_err!("Expected hash join stream in EmitUnmatchedBuildRows state")
+ }
+ }
+ }
}
/// Container for HashJoinStreamState::ProcessProbeBatch related data
@@ -177,6 +199,19 @@ impl ProcessProbeBatchState {
}
}
+/// Container for HashJoinStreamState::EmitUnmatchedBuildRows related data
+#[derive(Debug, Clone)]
+pub(super) struct EmitUnmatchedBuildRowsState {
+ /// Snapshot of the build-side visited bitmap, one bit per build row.
+ ///
+ /// Taken once every probe partition has reported completion, so no
+ /// further updates to the shared bitmap are possible and the snapshot
+ /// can be scanned without holding its lock.
+ visited: BooleanBuffer,
+ /// Index of the next build row to examine
+ cursor: usize,
+}
+
/// Lifecycle of this partition's build-data report to the shared coordinator.
///
/// `Scheduled` means the reporting `OnceFut` has been constructed but is lazy:
@@ -638,7 +673,10 @@ impl HashJoinStream {
handle_state!(self.process_probe_batch())
}
HashJoinStreamState::ExhaustedProbeSide => {
- handle_state!(self.process_unmatched_build_batch())
+ handle_state!(self.prepare_unmatched_build_rows())
+ }
+ HashJoinStreamState::EmitUnmatchedBuildRows(_) => {
+ handle_state!(self.emit_unmatched_build_rows())
}
HashJoinStreamState::Completed if !self.output_buffer.is_empty() => {
// Flush any remaining buffered data
@@ -971,10 +1009,14 @@ impl HashJoinStream {
Ok(StatefulStreamResult::Continue)
}
- /// Processes unmatched build-side rows for certain join types and produces output batch
+ /// Decides whether this stream emits the final build-side rows (unmatched
+ /// rows, or matched rows for `LeftSemi`) once the probe side is exhausted.
///
- /// Updates state to `Completed`
- fn process_unmatched_build_batch(
+ /// Only the last partition to finish probing emits them, since the shared
+ /// visited bitmap is complete only at that point. Updates state to
+ /// `EmitUnmatchedBuildRows` when there are rows to emit, and to `Completed`
+ /// otherwise.
+ fn prepare_unmatched_build_rows(
&mut self,
) -> Result<StatefulStreamResult<Option<RecordBatch>>> {
let timer = self.join_metrics.join_time.timer();
@@ -1006,11 +1048,56 @@ impl HashJoinStream {
return Ok(StatefulStreamResult::Continue);
}
+ // Every probe partition has finished, so the bitmap is final: snapshot
+ // it once and release the lock for the whole emission phase.
+ let visited = build_side
+ .left_data
+ .visited_indices_bitmap()
+ .lock()
+ .finish_cloned();
+
+ // The final build rows count as one logical input batch; the rows are
+ // added chunk by chunk in `emit_unmatched_build_rows`.
+ self.join_metrics.input_batches.add(1);
+
+ timer.done();
+ self.state =
+ HashJoinStreamState::EmitUnmatchedBuildRows(EmitUnmatchedBuildRowsState {
+ visited,
+ cursor: 0,
+ });
+
+ Ok(StatefulStreamResult::Continue)
+ }
+
+ /// Emits the next chunk of at most `batch_size` final build-side rows and
+ /// stays in `EmitUnmatchedBuildRows` until every build row has been
+ /// examined, then updates state to `Completed`.
+ ///
+ /// Emitting in chunks instead of one batch over the whole build side keeps
+ /// the output within `batch_size` and bounds the memory materialized at
+ /// once, which otherwise grows with the number of unmatched build rows
+ /// (e.g. a highly selective `LeftAnti` join over a large build side).
+ fn emit_unmatched_build_rows(
+ &mut self,
+ ) -> Result<StatefulStreamResult<Option<RecordBatch>>> {
+ let timer = self.join_metrics.join_time.timer();
+
+ let state = self.state.try_as_emit_unmatched_build_rows_mut()?;
+ if state.cursor >= state.visited.len() {
+ timer.done();
+ self.state = HashJoinStreamState::Completed;
+ return Ok(StatefulStreamResult::Continue);
+ }
+
+ let build_side = self.build_side.try_as_ready()?;
+
// use the global left bitmap to produce the left indices and right indices
- let (left_side, right_side) = get_final_indices_from_shared_bitmap(
- build_side.left_data.visited_indices_bitmap(),
+ let (left_side, right_side) = next_final_indices_chunk(
+ &state.visited,
+ &mut state.cursor,
self.join_type,
- true,
+ self.batch_size,
);
// Null-aware joins post-process the build rows under SQL three-valued
@@ -1035,10 +1122,9 @@ impl HashJoinStream {
_ => (left_side, right_side, None),
};
- self.join_metrics.input_batches.add(1);
self.join_metrics.input_rows.add(left_side.len());
- // Push final unmatched indices to output buffer
+ // Push this chunk of final indices to output buffer
if !left_side.is_empty() {
let empty_right_batch = RecordBatch::new_empty(self.right.schema());
let batch = build_batch_from_indices(
@@ -1054,19 +1140,69 @@ impl HashJoinStream {
)?;
let push_status = self.output_buffer.push_batch(batch)?;
- // If limit reached, finish the coalescer
+ // If limit reached, finish the coalescer and stop emitting
if push_status == PushBatchStatus::LimitReached {
self.output_buffer.finish()?;
+ self.state = HashJoinStreamState::Completed;
}
}
timer.done();
- self.state = HashJoinStreamState::Completed;
Ok(StatefulStreamResult::Continue)
}
}
+/// Returns the next chunk of final build-side indices for join types that
+/// produce build rows once the probe side is exhausted, starting at `cursor`
+/// and holding at most `batch_size` rows. Advances `cursor` past the build
+/// rows examined; `cursor == visited.len()` means every row has been examined.
+///
+/// The build indices are always valid. The probe indices are NULL for every
+/// row (`Left`, `LeftAnti`, `Full`, `LeftSemi`), except for `LeftMark`, where
+/// every build row is emitted and a NULL probe index marks an unmatched row.
+///
+/// For example, with `visited = [true, false, true, true, false]`:
+/// - `Left`: build `[1, 4]`, probe `[null, null]`
+/// - `LeftSemi`: build `[0, 2, 3]`, probe `[null, null, null]`
+/// - `LeftMark`: build `[0, 1, 2, 3, 4]`, probe `[0, null, 0, 0, null]`
+fn next_final_indices_chunk(
+ visited: &BooleanBuffer,
+ cursor: &mut usize,
+ join_type: JoinType,
+ batch_size: usize,
+) -> (UInt64Array, UInt32Array) {
+ let num_rows = visited.len();
+ let start = *cursor;
+
+ if join_type == JoinType::LeftMark {
+ // Every build row is emitted, so a chunk is a plain range of rows.
+ let end = (start + batch_size).min(num_rows);
+ let build_indices = (start as u64..end as u64).collect::<UInt64Array>();
+ let probe_indices = (start..end)
+ .map(|idx| visited.value(idx).then_some(0))
+ .collect::<UInt32Array>();
+ *cursor = end;
+ return (build_indices, probe_indices);
+ }
+
+ // `LeftSemi` emits the matched build rows; `Left`, `LeftAnti` and `Full`
+ // emit the unmatched ones.
+ let emit_visited = join_type == JoinType::LeftSemi;
+ let mut build_indices = Vec::with_capacity(batch_size.min(num_rows - start));
+ let mut idx = start;
+ while idx < num_rows && build_indices.len() < batch_size {
+ if visited.value(idx) == emit_visited {
+ build_indices.push(idx as u64);
+ }
+ idx += 1;
+ }
+ *cursor = idx;
+
+ let probe_indices = UInt32Array::new_null(build_indices.len());
+ (UInt64Array::from(build_indices), probe_indices)
+}
+
/// Applies the pre-lookup bookkeeping of a null-aware join to a probe batch
/// and returns `true` if the batch cannot contribute any output row.
///
diff --git a/datafusion/physical-plan/src/joins/utils.rs b/datafusion/physical-plan/src/joins/utils.rs
index 04ff9e30bf..4e01588e5a 100644
--- a/datafusion/physical-plan/src/joins/utils.rs
+++ b/datafusion/physical-plan/src/joins/utils.rs
@@ -26,7 +26,6 @@ use std::ops::Range;
use std::sync::Arc;
use std::task::{Context, Poll};
-use crate::joins::SharedBitmapBuilder;
use crate::metrics::{
self, BaselineMetrics, ExecutionPlanMetricsSet, MetricBuilder, MetricCategory,
MetricType,
@@ -1217,61 +1216,6 @@ pub(crate) fn is_existence_join(join_type: JoinType) -> bool {
)
}
-pub(crate) fn get_final_indices_from_shared_bitmap(
- shared_bitmap: &SharedBitmapBuilder,
- join_type: JoinType,
- piecewise: bool,
-) -> (UInt64Array, UInt32Array) {
- let bitmap = shared_bitmap.lock();
- get_final_indices_from_bit_map(&bitmap, join_type, piecewise)
-}
-
-/// In the end of join execution, need to use bit map of the matched
-/// indices to generate the final left and right indices.
-///
-/// For example:
-///
-/// 1. left_bit_map: `[true, false, true, true, false]`
-/// 2. join_type: `Left`
-///
-/// The result is: `([1,4], [null, null])`
-pub(crate) fn get_final_indices_from_bit_map(
- left_bit_map: &BooleanBufferBuilder,
- join_type: JoinType,
- // We add a flag for whether this is being passed from the `PiecewiseMergeJoin`
- // because the bitmap can be for left + right `JoinType`s
- piecewise: bool,
-) -> (UInt64Array, UInt32Array) {
- let left_size = left_bit_map.len();
- if join_type == JoinType::LeftMark || (join_type == JoinType::RightMark && piecewise)
- {
- let left_indices = (0..left_size as u64).collect::<UInt64Array>();
- let right_indices = (0..left_size)
- .map(|idx| left_bit_map.get_bit(idx).then_some(0))
- .collect::<UInt32Array>();
- return (left_indices, right_indices);
- }
- let left_indices = if join_type == JoinType::LeftSemi
- || (join_type == JoinType::RightSemi && piecewise)
- {
- (0..left_size)
- .filter_map(|idx| (left_bit_map.get_bit(idx)).then_some(idx as u64))
- .collect::<UInt64Array>()
- } else {
- // just for `Left`, `LeftAnti` and `Full` join
- // `LeftAnti`, `Left` and `Full` will produce the unmatched left row finally
- (0..left_size)
- .filter_map(|idx| (!left_bit_map.get_bit(idx)).then_some(idx as u64))
- .collect::<UInt64Array>()
- };
- // right_indices
- // all the element in the right side is None
- let mut builder = UInt32Builder::with_capacity(left_indices.len());
- builder.append_nulls(left_indices.len());
- let right_indices = builder.finish();
- (left_indices, right_indices)
-}
-
#[expect(clippy::too_many_arguments)]
pub(crate) fn apply_join_filter_to_indices(
build_input_buffer: &RecordBatch,
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org
(datafusion) 05/05: fix(physical-plan): report Final emission for LeftSemi hash and neste… (#24976)
Posted by gi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-24976-9f21155c48faac3f1658853ad6a55bd314a322ed
in repository https://gitbox.apache.org/repos/asf/datafusion.git
commit 4084d4c57bc886d8a645ec7643748d09b21397db
Author: Tiny_Murky <mu...@gmail.com>
AuthorDate: Wed Sep 9 14:52:23 2026 +0000
fix(physical-plan): report Final emission for LeftSemi hash and neste… (#24976)
## Which issue does this PR close?
- #24962
## Rationale for this change
`HashJoinExec` and `NestedLoopJoinExec` report `LeftSemi` joins as
emitting incrementally, but they only emit matched build-side rows after
the probe
side is exhausted.
Reporting `EmissionType::Final` reflects their actual behavior and
allows `SanityCheckPlan` to reject pipelines with an unbounded probe
side that cannot produce output.
## What changes are included in this PR?
- Classify `LeftSemi` joins as `EmissionType::Final` in both operators,
regardless of the probe input's emission type.
- Added unit tests verifying the emission type of LeftSemi joins in both
`HashJoinExec` and `NestedLoopJoinExec`.
- Add an integration test covering input swapping for an unbounded left
input, rejection of an unbounded probe input, and successful planning
when
both inputs are bounded.
## Are these changes tested?
Added unit tests verifying the emission type of LeftSemi joins in both
`HashJoinExec` and `NestedLoopJoinExec`.
Added an integration test covering:
- Unbounded left and bounded right: planning succeeds after swapping to
RightSemi.
- Bounded left and unbounded right: planning is rejected.
- Both inputs bounded: planning succeeds.
Following test commands have been executed and passed
- `cargo test -p datafusion`
- `cargo test --profile=ci --test sqllogictests`
- `cargo test -p datafusion`
- `cargo test -p datafusion-cli`
## Are there any user-facing changes?
Yes.
`LeftSemi` hash and nested loop joins now report Final emission.
Plans with a bounded build side and an unbounded probe side are rejected
instead of being accepted despite being unable to produce output.
Results for bounded inputs are unchanged.
---
.../tests/physical_optimizer/sanity_checker.rs | 39 ++++++++++++++
.../physical-plan/src/joins/hash_join/exec.rs | 52 ++++++++++++++-----
.../physical-plan/src/joins/nested_loop_join.rs | 59 ++++++++++++++--------
3 files changed, 116 insertions(+), 34 deletions(-)
diff --git a/datafusion/core/tests/physical_optimizer/sanity_checker.rs b/datafusion/core/tests/physical_optimizer/sanity_checker.rs
index 184125dcbe..32f02d0f6a 100644
--- a/datafusion/core/tests/physical_optimizer/sanity_checker.rs
+++ b/datafusion/core/tests/physical_optimizer/sanity_checker.rs
@@ -186,6 +186,45 @@ async fn test_hash_left_join_swap() -> Result<()> {
Ok(())
}
+#[tokio::test]
+async fn test_hash_left_semi_join() -> Result<()> {
+ // LeftSemi hash joins emit matched build-side rows only after probing
+ // completes, so their emission type is Final.
+
+ let test1 = BinaryTestCase {
+ // The optimizer swaps the inputs and changes LeftSemi to RightSemi.
+ // The bounded right table becomes the build side, allowing matched
+ // rows from the unbounded left table to be emitted incrementally.
+ source_types: (SourceType::Unbounded, SourceType::Bounded),
+ expect_fail: false,
+ };
+
+ let test2 = BinaryTestCase {
+ // LeftSemi waits for the unbounded probe side to finish before
+ // emitting matched build-side rows. SanityCheckPlan must reject
+ // this pipeline because it cannot produce output.
+ source_types: (SourceType::Bounded, SourceType::Unbounded),
+ expect_fail: true,
+ };
+
+ let test3 = BinaryTestCase {
+ // Both inputs are bounded, so probing can finish and final output
+ // can be emitted.
+ source_types: (SourceType::Bounded, SourceType::Bounded),
+ expect_fail: false,
+ };
+
+ let case = QueryCase {
+ sql: "SELECT l.c1 FROM left AS l LEFT SEMI JOIN right AS r ON l.c1 = r.c1"
+ .to_string(),
+ cases: vec![Arc::new(test1), Arc::new(test2), Arc::new(test3)],
+ error_operator: "operator: HashJoinExec".to_string(),
+ };
+
+ case.run().await?;
+ Ok(())
+}
+
#[tokio::test]
async fn test_hash_right_join_swap() -> Result<()> {
let test1 = BinaryTestCase {
diff --git a/datafusion/physical-plan/src/joins/hash_join/exec.rs b/datafusion/physical-plan/src/joins/hash_join/exec.rs
index 01dec6570b..5f22741662 100644
--- a/datafusion/physical-plan/src/joins/hash_join/exec.rs
+++ b/datafusion/physical-plan/src/joins/hash_join/exec.rs
@@ -1218,19 +1218,22 @@ impl HashJoinExec {
}
};
- let emission_type = if left.boundedness().is_unbounded() {
- EmissionType::Final
- } else if right.pipeline_behavior() == EmissionType::Incremental {
- // Unmatched build-side rows can only be emitted once the probe side
- // is exhausted; everything else is emitted incrementally.
- if emits_unmatched_left_rows(join_type) {
- EmissionType::Both
+ let emission_type =
+ // LeftSemi does not emit rows during probing. It records matching build-side
+ // rows in a bitmap and can only emit them after the probe side is exhausted.
+ if left.boundedness().is_unbounded() || join_type == JoinType::LeftSemi {
+ EmissionType::Final
+ } else if right.pipeline_behavior() == EmissionType::Incremental {
+ // Unmatched build-side rows can only be emitted once the probe side
+ // is exhausted; everything else is emitted incrementally.
+ if emits_unmatched_left_rows(join_type) {
+ EmissionType::Both
+ } else {
+ EmissionType::Incremental
+ }
} else {
- EmissionType::Incremental
- }
- } else {
- right.pipeline_behavior()
- };
+ right.pipeline_behavior()
+ };
// If contains projection, update the PlanProperties.
if let Some(projection) = projection {
@@ -4732,6 +4735,31 @@ mod tests {
)
}
+ #[tokio::test]
+ async fn test_semi_left_join_reports_final_emission() -> Result<()> {
+ let (left_schema, right_schema, on) = build_schema_and_on()?;
+
+ let left = TestMemoryExec::try_new_exec(&[vec![]], left_schema, None)?;
+
+ let right = TestMemoryExec::try_new_exec(&[vec![]], right_schema, None)?;
+
+ let join = HashJoinExec::try_new(
+ left,
+ right,
+ on,
+ None,
+ &JoinType::LeftSemi,
+ None,
+ PartitionMode::CollectLeft,
+ NullEquality::NullEqualsNothing,
+ false,
+ )?;
+
+ assert_eq!(join.properties().emission_type, EmissionType::Final);
+
+ Ok(())
+ }
+
#[apply(hash_join_exec_configs)]
#[tokio::test]
async fn join_left_semi(
diff --git a/datafusion/physical-plan/src/joins/nested_loop_join.rs b/datafusion/physical-plan/src/joins/nested_loop_join.rs
index fd2e0ae21c..9f0abd92a0 100644
--- a/datafusion/physical-plan/src/joins/nested_loop_join.rs
+++ b/datafusion/physical-plan/src/joins/nested_loop_join.rs
@@ -390,28 +390,31 @@ impl NestedLoopJoinExec {
let mut output_partitioning =
asymmetric_join_output_partitioning(left, right, &join_type)?;
- let emission_type = if left.boundedness().is_unbounded() {
- EmissionType::Final
- } else if right.pipeline_behavior() == EmissionType::Incremental {
- match join_type {
- // If we only need to generate matched rows from the probe side,
- // we can emit rows incrementally.
- JoinType::Inner
- | JoinType::LeftSemi
- | JoinType::RightSemi
- | JoinType::Right
- | JoinType::RightAnti
- | JoinType::RightMark => EmissionType::Incremental,
- // If we need to generate unmatched rows from the *build side*,
- // we need to emit them at the end.
- JoinType::Left
- | JoinType::LeftAnti
- | JoinType::LeftMark
- | JoinType::Full => EmissionType::Both,
- }
- } else {
- right.pipeline_behavior()
- };
+ let emission_type =
+ // LeftSemi does not emit rows during probing. It records matching build-side
+ // rows in a bitmap and can only emit them after the probe side is exhausted.
+ if left.boundedness().is_unbounded() || join_type == JoinType::LeftSemi {
+ EmissionType::Final
+ } else if right.pipeline_behavior() == EmissionType::Incremental {
+ match join_type {
+ // If we only need to generate matched rows from the probe side,
+ // we can emit rows incrementally.
+ JoinType::Inner
+ | JoinType::LeftSemi
+ | JoinType::RightSemi
+ | JoinType::Right
+ | JoinType::RightAnti
+ | JoinType::RightMark => EmissionType::Incremental,
+ // If we need to generate unmatched rows from the *build side*,
+ // we need to emit them at the end.
+ JoinType::Left
+ | JoinType::LeftAnti
+ | JoinType::LeftMark
+ | JoinType::Full => EmissionType::Both,
+ }
+ } else {
+ right.pipeline_behavior()
+ };
if let Some(projection) = projection {
// construct a map from the input expressions to the output expression of the Projection
@@ -4660,6 +4663,18 @@ pub(crate) mod tests {
Ok(())
}
+ #[tokio::test]
+ async fn test_left_semi_join_reports_final_emission() -> Result<()> {
+ let left = build_left_table();
+ let right = build_right_table();
+ let join =
+ NestedLoopJoinExec::try_new(left, right, None, &JoinType::LeftSemi, None)?;
+
+ assert_eq!(join.properties().emission_type, EmissionType::Final);
+
+ Ok(())
+ }
+
#[rstest]
#[tokio::test]
async fn join_left_semi_with_filter(
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org