From 1789406dad27bfa100c10f0b5337856f3ea0965a Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Mon, 2 Nov 2026 13:07:41 -0600 Subject: [PATCH 1/9] perf: replace the routed hash join dynamic filter with one global filter (InList or bloom filter) Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + datafusion/common/src/config.rs | 37 + .../physical_optimizer/filter_pushdown.rs | 18 +- datafusion/physical-plan/Cargo.toml | 1 + .../src/joins/hash_join/bloom_filter.rs | 673 ++++++++++ .../src/joins/hash_join/build.rs | 9 +- .../physical-plan/src/joins/hash_join/exec.rs | 237 ++-- .../src/joins/hash_join/exec/prepared.rs | 1 + .../physical-plan/src/joins/hash_join/mod.rs | 8 + .../src/joins/hash_join/shared_bounds.rs | 1150 +++++++++-------- .../src/joins/hash_join/stream.rs | 11 +- datafusion/physical-plan/src/joins/mod.rs | 2 +- datafusion/pruning/src/pruning_predicate.rs | 40 +- .../test_files/dynamic_row_group_pruning.slt | 62 +- .../test_files/information_schema.slt | 8 + .../test_files/push_down_filter_parquet.slt | 14 +- docs/source/user-guide/configs.md | 324 ++--- 17 files changed, 1695 insertions(+), 901 deletions(-) create mode 100644 datafusion/physical-plan/src/joins/hash_join/bloom_filter.rs diff --git a/Cargo.lock b/Cargo.lock index d9da77fa4b988..56d78545fa498 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2604,6 +2604,7 @@ dependencies = [ "loom", "num-traits", "parking_lot 0.13.0", + "parquet", "pin-project-lite", "rand 0.9.4", "rstest", diff --git a/datafusion/common/src/config.rs b/datafusion/common/src/config.rs index cdcecfe6196b4..a6b19388d3ff1 100644 --- a/datafusion/common/src/config.rs +++ b/datafusion/common/src/config.rs @@ -1890,6 +1890,43 @@ config_namespace! { /// See: pub hash_join_inlist_pushdown_max_distinct_values: usize, default = 150 + /// Maximum number of distinct build side keys, over all partitions of a hash join, + /// for the global dynamic filter to test membership with one InList of all keys. + /// Above this limit the dynamic filter uses a bloom filter + /// (see `hash_join_bloom_filter_fpp`). + /// + /// Every partition must also use an InList by itself + /// (`hash_join_inlist_pushdown_max_distinct_values` and `hash_join_inlist_pushdown_max_size`). + /// For larger sets, a bloom filter is cheaper per probe row than an InList. + pub hash_join_dynamic_filter_inlist_max_distinct_values: usize, default = 20 + + /// Target false positive probability of the bloom filter in the dynamic + /// filter of a hash join. + /// + /// When the build side of a hash join has more distinct keys than + /// `hash_join_inlist_pushdown_max_distinct_values` (over all partitions), + /// the dynamic filter tests probe rows against one bloom filter of all + /// build side keys. Each build partition fills a filter of its keys, sized + /// for the estimated number of distinct keys of all partitions at this + /// probability. Then one task ORs the partition filters and folds the + /// result to this probability. + pub hash_join_bloom_filter_fpp: f64, default = 0.01 + + /// Maximum size, in bytes, of the bloom filter in the dynamic filter of a + /// hash join (and of the filter of each build partition). A build side that needs a larger + /// filter at `hash_join_bloom_filter_fpp` gets no bloom filter: such a + /// large build side is rarely selective, so the filter is too big to be + /// useful. The dynamic filter then tests only the min/max bounds (and + /// keeps key range pruning). + pub hash_join_bloom_filter_max_bytes: usize, default = 8 * 1024 * 1024 + + /// The bloom filter of a hash join dynamic filter is not used when its + /// estimated false positive probability is above this value (the filter + /// is saturated, a safety net). Then the dynamic filter tests only the + /// min/max bounds of the build keys, because a saturated filter removes few rows and costs + /// CPU time for each probe row. + pub hash_join_bloom_filter_max_fpp: f64, default = 0.15 + /// The default filter selectivity used by Filter Statistics /// when an exact selectivity cannot be determined. Valid values are /// between 0 (no selectivity) and 100 (all rows are selected). diff --git a/datafusion/core/tests/physical_optimizer/filter_pushdown.rs b/datafusion/core/tests/physical_optimizer/filter_pushdown.rs index 987dca9fdf581..b65b5704a98a1 100644 --- a/datafusion/core/tests/physical_optimizer/filter_pushdown.rs +++ b/datafusion/core/tests/physical_optimizer/filter_pushdown.rs @@ -1149,7 +1149,7 @@ async fn test_hashjoin_dynamic_filter_pushdown_partitioned() { #[cfg(not(feature = "force_hash_collisions"))] insta::assert_snapshot!( format_plan_for_test(&plan), - @r" + @" - SortExec: expr=[a@0 DESC NULLS LAST], preserve_partitioning=[false] - CoalescePartitionsExec - HashJoinExec: mode=Partitioned, join_type=Inner, on=[(a@0, a@0), (b@1, b@1)] @@ -1365,7 +1365,7 @@ async fn test_hashjoin_dynamic_filter_pushdown_range_partitioned() { // Now check what our filter looks like insta::assert_snapshot!( format_plan_for_test(&plan), - @r" + @" - SortExec: expr=[a@0 DESC NULLS LAST], preserve_partitioning=[false] - CoalescePartitionsExec - HashJoinExec: mode=Partitioned, join_type=Inner, on=[(a@0, a@0), (b@1, b@1)] @@ -3932,11 +3932,11 @@ async fn test_hashjoin_hash_table_pushdown_partitioned() { config.optimizer.enable_dynamic_filter_pushdown = true; let (plan, batches) = optimize_and_collect_pushdown_plan(plan, config).await; - // Verify that hash_lookup is used instead of IN (SET) + // Verify that a bloom filter is used instead of IN (SET) let plan_str = format_plan_for_test(&plan).to_string(); assert!( - plan_str.contains("hash_lookup"), - "Expected hash_lookup in plan but got: {plan_str}" + plan_str.contains("bloom_filter"), + "Expected bloom_filter in plan but got: {plan_str}" ); assert!( !plan_str.contains("IN (SET)"), @@ -3966,7 +3966,7 @@ async fn test_hashjoin_hash_table_pushdown_partitioned() { // Ported to push_down_filter_parquet.slt (`hl_build`/`hl_probe` fixture). // Rust version retained only because the slt port cannot hand-wire the -// RepartitionExec-above-probe shape this test uses; the hash_lookup vs +// RepartitionExec-above-probe shape this test uses; the bloom_filter vs // IN (SET) invariant is captured in the slt port. #[tokio::test] async fn test_hashjoin_hash_table_pushdown_collect_left() { @@ -4033,11 +4033,11 @@ async fn test_hashjoin_hash_table_pushdown_collect_left() { config.optimizer.enable_dynamic_filter_pushdown = true; let (plan, batches) = optimize_and_collect_pushdown_plan(plan, config).await; - // Verify that hash_lookup is used instead of IN (SET) + // Verify that a bloom filter is used instead of IN (SET) let plan_str = format_plan_for_test(&plan).to_string(); assert!( - plan_str.contains("hash_lookup"), - "Expected hash_lookup in plan but got: {plan_str}" + plan_str.contains("bloom_filter"), + "Expected bloom_filter in plan but got: {plan_str}" ); assert!( !plan_str.contains("IN (SET)"), diff --git a/datafusion/physical-plan/Cargo.toml b/datafusion/physical-plan/Cargo.toml index 25f8e437407a1..66307ec7f3d5c 100644 --- a/datafusion/physical-plan/Cargo.toml +++ b/datafusion/physical-plan/Cargo.toml @@ -74,6 +74,7 @@ itertools = { workspace = true, features = ["use_std"] } log = { workspace = true } num-traits = { workspace = true } parking_lot = { workspace = true } +parquet = { workspace = true } pin-project-lite = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } diff --git a/datafusion/physical-plan/src/joins/hash_join/bloom_filter.rs b/datafusion/physical-plan/src/joins/hash_join/bloom_filter.rs new file mode 100644 index 0000000000000..7eaad50db5f76 --- /dev/null +++ b/datafusion/physical-plan/src/joins/hash_join/bloom_filter.rs @@ -0,0 +1,673 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership +// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements + +//! Bloom filter membership test over the build side keys of a hash join, for +//! the join's dynamic filter. +//! +//! Each build partition inserts the hash join hashes of its keys into its own +//! bloom filter ([`partition_filter`]), in its own build task. Once every +//! partition is done, one task ORs the partition filters into one filter +//! ([`Sbbf::union`]) and folds it to the target false positive probability. +//! Folding and union commute, so this is the filter of all build keys. + +use std::fmt::{self, Display}; +use std::hash::Hash; +use std::sync::Arc; + +use arrow::array::{Array, ArrayRef, BooleanArray}; +use arrow::buffer::NullBuffer; +use arrow::datatypes::{DataType, Schema}; +use arrow::record_batch::RecordBatch; +use datafusion_common::hash_utils::create_hashes; +use datafusion_common::{DataFusionError, Result, ScalarValue}; +use datafusion_execution::memory_pool::{MemoryConsumer, MemoryPool, MemoryReservation}; +use datafusion_expr::ColumnarValue; +use datafusion_expr_common::dyn_eq::DynHash; +use datafusion_physical_expr_common::physical_expr::{PhysicalExpr, PhysicalExprRef}; +use parquet::bloom_filter::Sbbf; + +use crate::joins::hash_join::exec::HASH_JOIN_SEED; +use crate::joins::key_range_bitmap::KeyRangeBitmap; + +/// Sizing options of a [`BloomFilterExpr`]. +#[derive(Debug, Clone, Copy)] +pub(crate) struct BloomFilterOptions { + /// Target false positive probability. + pub(crate) fpp: f64, + /// Largest filter, in bytes. A build side that needs a larger filter at + /// `fpp` gets no bloom filter. + pub(crate) max_bytes: usize, + /// Do not use the filter when its estimated false positive probability is + /// above this value. + pub(crate) max_fpp: f64, +} + +impl BloomFilterOptions { + /// Bytes of a filter for `num_distinct` keys at `fpp`, or `None` when + /// that is more than `max_bytes`. + fn bytes(&self, num_distinct: usize) -> Option { + let bits = -8.0 * num_distinct as f64 / (1.0 - self.fpp.powf(1.0 / 8.0)).ln(); + let bytes = (bits / 8.0) as usize; + (bytes <= self.max_bytes).then_some(bytes) + } +} + +fn parquet_err(e: parquet::errors::ParquetError) -> DataFusionError { + DataFusionError::External(Box::new(e)) +} + +/// Rows hashed at a time when filling a filter. +const HASH_BATCH_SIZE: usize = 8192; + +/// Hashes `keys` like the hash join and calls `f` with the hashes of each +/// batch of rows. A row with a NULL key gets no hash. +fn for_each_hash_batch( + keys: &[ArrayRef], + mut f: impl FnMut(&[u64]) -> Result<()>, +) -> Result<()> { + let num_rows = keys.first().map_or(0, |k| k.len()); + let mut hashes = Vec::with_capacity(HASH_BATCH_SIZE.min(num_rows)); + for offset in (0..num_rows).step_by(HASH_BATCH_SIZE) { + let len = HASH_BATCH_SIZE.min(num_rows - offset); + let batch: Vec = keys.iter().map(|k| k.slice(offset, len)).collect(); + hashes.clear(); + hashes.resize(len, 0); + create_hashes(&batch, HASH_JOIN_SEED.random_state(), &mut hashes)?; + if let Some(valid) = valid_rows(&batch) { + let mut num_valid = 0; + for i in valid.valid_indices() { + hashes[num_valid] = hashes[i]; + num_valid += 1; + } + hashes.truncate(num_valid); + } + f(&hashes)?; + } + Ok(()) +} + +/// The rows where no key is NULL, or `None` when no key has NULLs. +fn valid_rows(keys: &[ArrayRef]) -> Option { + let nulls: Vec<_> = keys.iter().map(|k| k.logical_nulls()).collect(); + NullBuffer::union_many(nulls.iter().map(Option::as_ref)) +} + +/// The bloom filter of the keys of one build partition, sized for +/// `num_distinct` keys (the estimated number of distinct keys of all +/// partitions, so that the partition filters can be ORed). `None` when that +/// filter would be larger than `options.max_bytes`. +/// +/// The filter memory is charged to `reservation`. `None` also when it cannot +/// grow. +pub(crate) fn partition_filter( + keys: &[ArrayRef], + num_distinct: usize, + options: BloomFilterOptions, + reservation: &MemoryReservation, +) -> Result> { + let Some(bytes) = options.bytes(num_distinct) else { + return Ok(None); + }; + // The size that `Sbbf::new_with_num_of_bytes` allocates + if reservation + .try_grow(bytes.max(32).next_power_of_two()) + .is_err() + { + return Ok(None); + } + let mut filter = Sbbf::new_with_num_of_bytes(bytes); + for_each_hash_batch(keys, |hashes| { + for &hash in hashes { + filter.insert_hash(hash); + } + Ok(()) + })?; + Ok(Some(filter)) +} + +/// The bloom filter of a [`BloomFilterExpr`] and the reservation for its +/// memory, which is released when the filter is dropped. +struct Filter { + filter: Sbbf, + _reservation: MemoryReservation, +} + +/// ORs the filters of all build partitions and folds the result. Returns +/// `None` (no bloom filter) when `pool` cannot hold the filter or it is +/// saturated. +fn merge( + partitions: &[&Sbbf], + options: BloomFilterOptions, + pool: &Arc, +) -> Result> { + // Partitions estimate the number of keys of all partitions from their own, + // so their filters can differ in size: fold the larger ones into the + // smallest. + let Some((smallest, first)) = partitions + .iter() + .enumerate() + .min_by_key(|(_, f)| f.num_blocks()) + .map(|(i, f)| (i, *f)) + else { + return Ok(None); + }; + let reservation = MemoryConsumer::new("HashJoinBloomFilter").register(pool); + if reservation.try_grow(first.num_blocks() * 32).is_err() { + return Ok(None); + } + let mut filter = first.clone(); + for (i, partition) in partitions.iter().enumerate() { + if i != smallest { + filter.union(partition).map_err(parquet_err)?; + } + } + filter.fold_to_target_fpp(options.fpp); + // A saturated filter costs CPU time and removes almost no rows + if filter.estimated_fpp() > options.max_fpp { + return Ok(None); + } + reservation.resize(filter.num_blocks() * 32); + Ok(Some(Filter { + filter, + _reservation: reservation, + })) +} + +/// Physical expression that tests join keys against a bloom filter of the +/// build side keys of a hash join. +/// +/// The result is `true` for a row whose keys may be on the build side, and +/// `false` for a row whose keys are definitely not on the build side (or are +/// NULL). The filter holds the hash join hashes of the keys (all key columns +/// together), so it supports every key type that the hash join supports. +pub struct BloomFilterExpr { + /// Probe side join key expressions + on_columns: Vec, + /// `None` when no bloom filter was built + filter: Option>, + /// Which parts of the build side's key range hold keys, for container pruning + pruning_bitmap: Option>, +} + +impl BloomFilterExpr { + /// ORs the bloom filters of every build partition ([`partition_filter`]) + /// into one filter, charged to `pool`. + /// + /// When no useful filter is possible (`partitions` is `None` because a + /// partition has no filter, `pool` cannot hold the filter, or the filter + /// is saturated), the expression has no filter: it is `true` for every + /// row and only carries `pruning_bitmap` (see [`Self::has_filter`]). + pub(crate) fn try_new( + on_columns: Vec, + partitions: Option<&[&Sbbf]>, + options: BloomFilterOptions, + pool: &Arc, + pruning_bitmap: Option>, + ) -> Result { + let filter = match partitions { + Some(partitions) => merge(partitions, options, pool)?.map(Arc::new), + None => None, + }; + Ok(Self { + on_columns, + filter, + pruning_bitmap, + }) + } + + /// Returns `true` if the expression tests rows against a bloom filter. + /// Without a filter it only carries the pruning bitmap. + pub fn has_filter(&self) -> bool { + self.filter.is_some() + } + + /// Which parts of the build side's key range hold keys, for container pruning. + pub fn pruning_bitmap(&self) -> Option<&Arc> { + self.pruning_bitmap.as_ref() + } + + /// Bytes of the bloom filter, 0 without a filter. + #[cfg(test)] + pub(crate) fn filter_bytes(&self) -> usize { + self.filter + .as_ref() + .map_or(0, |filter| filter.filter.num_blocks() * 32) + } + + /// The probe side key expressions. + pub fn on_columns(&self) -> &[PhysicalExprRef] { + &self.on_columns + } +} + +impl fmt::Debug for BloomFilterExpr { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let cols = self + .on_columns + .iter() + .map(|e| e.to_string()) + .collect::>() + .join(", "); + write!(f, "bloom_filter({cols})") + } +} + +impl Hash for BloomFilterExpr { + fn hash(&self, state: &mut H) { + self.on_columns.dyn_hash(state); + self.filter.as_ref().map(Arc::as_ptr).hash(state); + } +} + +impl PartialEq for BloomFilterExpr { + fn eq(&self, other: &Self) -> bool { + self.on_columns == other.on_columns + && match (&self.filter, &other.filter) { + (Some(a), Some(b)) => Arc::ptr_eq(a, b), + (None, None) => true, + _ => false, + } + } +} + +impl Eq for BloomFilterExpr {} + +impl Display for BloomFilterExpr { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.has_filter() { + write!(f, "bloom_filter") + } else { + write!(f, "key_range_pruning") + } + } +} + +impl PhysicalExpr for BloomFilterExpr { + fn children(&self) -> Vec<&Arc> { + self.on_columns.iter().collect() + } + + fn with_new_children( + self: Arc, + children: Vec>, + ) -> Result> { + Ok(Arc::new(Self { + on_columns: children, + filter: self.filter.clone(), + pruning_bitmap: self.pruning_bitmap.clone(), + })) + } + + fn data_type(&self, _input_schema: &Schema) -> Result { + Ok(DataType::Boolean) + } + + fn nullable(&self, _input_schema: &Schema) -> Result { + Ok(false) + } + + fn evaluate(&self, batch: &RecordBatch) -> Result { + let Some(filter) = &self.filter else { + // No filter: only the pruning bitmap, which is not tested per row + return Ok(ColumnarValue::Scalar(ScalarValue::Boolean(Some(true)))); + }; + let num_rows = batch.num_rows(); + let keys = self + .on_columns + .iter() + .map(|c| c.evaluate(batch)?.into_array(num_rows)) + .collect::>>()?; + let mut hashes = vec![0; num_rows]; + create_hashes(&keys, HASH_JOIN_SEED.random_state(), &mut hashes)?; + let matches = filter.filter.check_hashes(&hashes); + // A NULL key matches no build key + let matches = match valid_rows(&keys) { + Some(valid) => &matches & valid.inner(), + None => matches, + }; + Ok(ColumnarValue::Array(Arc::new(BooleanArray::new( + matches, None, + )))) + } + + #[cfg(feature = "proto")] + fn try_to_proto( + &self, + _ctx: &datafusion_physical_expr_common::physical_expr::proto_encode::PhysicalExprEncodeCtx<'_>, + ) -> Result> { + use datafusion_proto_models::protobuf; + use datafusion_proto_models::protobuf::physical_expr_node::ExprType; + + // Like `HashTableLookupExpr`, the expression holds runtime build side + // state that is not serialized. It is replaced with lit(true), which + // keeps every row: the dynamic filter is only an optimization. A plan + // that is executed again calls reset_state(), and the join builds a new + // dynamic filter at runtime. + let Self { + on_columns: _, + filter: _, + pruning_bitmap: _, + } = self; + let value = datafusion_proto_common::ScalarValue { + value: Some(datafusion_proto_common::scalar_value::Value::BoolValue( + true, + )), + }; + Ok(Some(protobuf::PhysicalExprNode { + expr_id: None, + expr_type: Some(ExprType::Literal(value)), + })) + } + + fn fmt_sql(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{self}") + } +} + +#[cfg(test)] +mod tests { + use super::*; + use arrow::array::{Float64Array, Int64Array, StringArray}; + use arrow::datatypes::Field; + use datafusion_execution::memory_pool::{GreedyMemoryPool, UnboundedMemoryPool}; + use datafusion_physical_expr::expressions::Column; + + const OPTIONS: BloomFilterOptions = BloomFilterOptions { + fpp: 0.01, + max_bytes: 8 * 1024 * 1024, + max_fpp: 0.15, + }; + + fn unbounded() -> Arc { + Arc::new(UnboundedMemoryPool::default()) + } + + /// The filters of `partitions`, each sized for the keys of all partitions. + fn partition_filters( + partitions: &[Vec], + options: BloomFilterOptions, + ) -> Option> { + let num_distinct = partitions.iter().map(|p| p[0].len()).sum(); + partitions + .iter() + .map(|keys| filter_of(keys, num_distinct, options)) + .collect() + } + + fn filter_of( + keys: &[ArrayRef], + num_distinct: usize, + options: BloomFilterOptions, + ) -> Option { + let reservation = MemoryConsumer::new("test").register(&unbounded()); + partition_filter(keys, num_distinct, options, &reservation).unwrap() + } + + fn expr_with( + columns: Vec, + partitions: &[Vec], + options: BloomFilterOptions, + pool: &Arc, + ) -> Option { + let filters = partition_filters(partitions, options); + let refs = filters.as_ref().map(|f| f.iter().collect::>()); + let expr = + BloomFilterExpr::try_new(columns, refs.as_deref(), options, pool, None) + .unwrap(); + expr.has_filter().then_some(expr) + } + + fn int_keys(keys: Vec) -> Vec { + vec![Arc::new(Int64Array::from(keys))] + } + + fn int_expr_with( + partitions: Vec>, + options: BloomFilterOptions, + pool: &Arc, + ) -> Option { + let partitions: Vec<_> = partitions.into_iter().map(int_keys).collect(); + expr_with( + vec![Arc::new(Column::new("k", 0))], + &partitions, + options, + pool, + ) + } + + fn int_expr(partitions: Vec>) -> BloomFilterExpr { + int_expr_with(partitions, OPTIONS, &unbounded()).unwrap() + } + + fn int_batch(keys: Vec>) -> RecordBatch { + let schema = Schema::new(vec![Field::new("k", DataType::Int64, true)]); + RecordBatch::try_new(Arc::new(schema), vec![Arc::new(Int64Array::from(keys))]) + .unwrap() + } + + fn eval(expr: &BloomFilterExpr, batch: &RecordBatch) -> Vec { + let result = expr + .evaluate(batch) + .unwrap() + .into_array(batch.num_rows()) + .unwrap(); + let result = result.as_any().downcast_ref::().unwrap(); + result.iter().map(|v| v.unwrap()).collect() + } + + #[test] + fn finds_build_keys_and_rejects_others() { + let expr = int_expr(vec![(0..1000).collect(), (5000..6000).collect()]); + let batch = int_batch((0..10_000).map(Some).collect()); + let result = eval(&expr, &batch); + // No false negatives + for k in (0..1000).chain(5000..6000) { + assert!(result[k], "key {k} dropped"); + } + // Few false positives + let false_positives = result.iter().filter(|v| **v).count() - 2000; + assert!(false_positives < 200, "{false_positives} false positives"); + } + + #[test] + fn null_keys_do_not_match() { + // A NULL build key is not inserted, a NULL probe key does not match + let build: ArrayRef = Arc::new(Int64Array::from(vec![Some(1), None, Some(3)])); + let expr = expr_with( + vec![Arc::new(Column::new("k", 0))], + &[vec![build]], + OPTIONS, + &unbounded(), + ) + .unwrap(); + let batch = int_batch(vec![Some(1), None, Some(3)]); + assert_eq!(eval(&expr, &batch), vec![true, false, true]); + } + + /// ORing the partition filters gives the filter of all keys. + #[test] + fn keys_with_null_buffers_but_no_nulls() { + // A null buffer can have no NULLs, e.g. after slicing off the NULL rows + let key: ArrayRef = + Arc::new(Int64Array::from(vec![None, Some(1), Some(2)]).slice(1, 2)); + assert!(key.logical_nulls().is_some()); + assert_eq!(valid_rows(&[Arc::clone(&key), Arc::clone(&key)]), None); + } + + #[test] + fn merged_filter_matches_one_filter_of_all_keys() { + let partitions: Vec> = (0..8) + .map(|p| (0..10_000).map(|i| (i * 7919 + p) % 1_000_000).collect()) + .collect(); + let expr = int_expr(partitions.clone()); + let all: Vec = partitions.into_iter().flatten().collect(); + let mut expected = filter_of(&int_keys(all.clone()), all.len(), OPTIONS).unwrap(); + expected.fold_to_target_fpp(OPTIONS.fpp); + let (mut a, mut b) = (vec![], vec![]); + expected.write_bitset(&mut a).unwrap(); + let filter = &expr.filter.as_ref().unwrap().filter; + filter.write_bitset(&mut b).unwrap(); + assert_eq!(a, b); + } + + /// Partition filters of different sizes (from different estimates of the + /// number of keys) are folded into the smallest. + #[test] + fn partition_filters_of_different_sizes() { + let small = filter_of(&int_keys((0..100).collect()), 1000, OPTIONS).unwrap(); + let large = + filter_of(&int_keys((1000..1100).collect()), 100_000, OPTIONS).unwrap(); + assert!(large.num_blocks() > small.num_blocks()); + let expr = BloomFilterExpr::try_new( + vec![Arc::new(Column::new("k", 0))], + Some(&[&large, &small]), + OPTIONS, + &unbounded(), + None, + ) + .unwrap(); + let result = eval(&expr, &int_batch((0..1100).map(Some).collect())); + assert!(result[..100].iter().all(|v| *v)); + assert!(result[1000..].iter().all(|v| *v)); + } + + /// A build side that needs a filter larger than `max_bytes` at the target + /// fpp gets no bloom filter: it is too big to be useful. + #[test] + fn too_big_build_side_has_no_filter() { + let options = BloomFilterOptions { + max_bytes: 1024, + ..OPTIONS + }; + assert!( + int_expr_with(vec![(0..10_000).collect()], options, &unbounded()).is_none() + ); + assert!(int_expr_with(vec![(0..100).collect()], options, &unbounded()).is_some()); + } + + /// A saturated filter is not used. + #[test] + fn saturated_filter_is_not_used() { + // Sized for 10 keys, filled with 10k keys + let keys = int_keys((0..10_000).collect()); + let filter = filter_of(&keys, 10, OPTIONS).unwrap(); + let expr = BloomFilterExpr::try_new( + vec![Arc::new(Column::new("k", 0))], + Some(&[&filter]), + OPTIONS, + &unbounded(), + None, + ) + .unwrap(); + assert!(!expr.has_filter()); + assert_eq!(expr.to_string(), "key_range_pruning"); + let batch = int_batch(vec![Some(-1)]); + assert_eq!(eval(&expr, &batch), vec![true]); + } + + /// The filter memory is charged to the pool and released on drop. + #[test] + fn memory_is_reserved_and_released() { + let pool: Arc = Arc::new(GreedyMemoryPool::new(64 << 20)); + let expr = int_expr_with(vec![(0..100_000).collect()], OPTIONS, &pool).unwrap(); + let built = &expr.filter.as_ref().unwrap().filter; + assert_eq!(pool.reserved(), built.num_blocks() * 32); + drop(expr); + assert_eq!(pool.reserved(), 0); + } + + /// When the pool cannot hold the filter, no filter is built. The query + /// does not fail. + #[test] + fn small_memory_pool_skips_the_filter() { + let pool: Arc = Arc::new(GreedyMemoryPool::new(1024)); + assert!(int_expr_with(vec![(0..100_000).collect()], OPTIONS, &pool).is_none()); + assert_eq!(pool.reserved(), 0); + } + + #[test] + fn multi_column_keys() { + let keys = vec![ + Arc::new(Int64Array::from(vec![1, 2])) as ArrayRef, + Arc::new(StringArray::from(vec!["a", "b"])) as ArrayRef, + ]; + let expr = expr_with( + vec![Arc::new(Column::new("k", 0)), Arc::new(Column::new("s", 1))], + &[keys], + OPTIONS, + &unbounded(), + ) + .unwrap(); + let schema = Schema::new(vec![ + Field::new("k", DataType::Int64, true), + Field::new("s", DataType::Utf8, true), + ]); + let batch = RecordBatch::try_new( + Arc::new(schema), + vec![ + Arc::new(Int64Array::from(vec![1, 2, 1, 99])), + Arc::new(StringArray::from(vec!["a", "b", "zzz", "a"])), + ], + ) + .unwrap(); + assert_eq!(eval(&expr, &batch), vec![true, true, false, false]); + } + + /// Any key type the hash join hashes is supported, floats included. + #[test] + fn float_keys() { + let keys: ArrayRef = Arc::new(Float64Array::from(vec![1.5, 2.5])); + let expr = expr_with( + vec![Arc::new(Column::new("f", 0))], + &[vec![keys]], + OPTIONS, + &unbounded(), + ) + .unwrap(); + let schema = Schema::new(vec![Field::new("f", DataType::Float64, true)]); + let batch = RecordBatch::try_new( + Arc::new(schema), + vec![Arc::new(Float64Array::from(vec![1.5, 2.5, 3.5]))], + ) + .unwrap(); + assert_eq!(eval(&expr, &batch), vec![true, true, false]); + } + + /// The filter holds runtime build side state, so it serializes as lit(true). + #[cfg(feature = "proto")] + #[test] + fn serializes_as_literal_true() { + use datafusion_common::internal_datafusion_err; + use datafusion_physical_expr_common::physical_expr::proto_encode::{ + PhysicalExprEncode, PhysicalExprEncodeCtx, + }; + use datafusion_proto_models::protobuf; + + struct TestEncoder; + impl PhysicalExprEncode for TestEncoder { + fn encode( + &self, + expr: &Arc, + ) -> Result { + let ctx = PhysicalExprEncodeCtx::new(self); + expr.try_to_proto(&ctx)? + .ok_or_else(|| internal_datafusion_err!("cannot encode {expr:?}")) + } + } + + let expr: Arc = Arc::new(int_expr(vec![(0..1000).collect()])); + let proto = TestEncoder.encode(&expr).unwrap(); + let Some(protobuf::physical_expr_node::ExprType::Literal(value)) = + proto.expr_type + else { + panic!("expected a literal, got {proto:?}"); + }; + assert_eq!( + value.value, + Some(datafusion_proto_common::scalar_value::Value::BoolValue( + true + )) + ); + } +} diff --git a/datafusion/physical-plan/src/joins/hash_join/build.rs b/datafusion/physical-plan/src/joins/hash_join/build.rs index 47a807fbc673a..1c40722198dc1 100644 --- a/datafusion/physical-plan/src/joins/hash_join/build.rs +++ b/datafusion/physical-plan/src/joins/hash_join/build.rs @@ -19,7 +19,7 @@ use parking_lot::Mutex; use super::exec::JoinBuildData; use crate::joins::PartitionMode; use crate::joins::hash_join::shared_bounds::{ - PartitionBounds, PartitionBuildData, SharedBuildAccumulator, + BuildKeys, PartitionBounds, PartitionBuildData, SharedBuildAccumulator, }; use crate::joins::utils::{BuildProbeJoinMetrics, OnceFut}; @@ -145,17 +145,24 @@ impl HashJoinBuild { .values .iter() .any(|array| array.logical_null_count() > 0); + let build_keys = BuildKeys { + keys: data.values.to_vec(), + bloom_filter: data.bloom_filter.clone(), + num_distinct: data.map.num_of_distinct_key(), + }; let report = match self.mode { PartitionMode::Partitioned => PartitionBuildData::Partitioned { partition_id: self.partition, pushdown, bounds, keys_have_null, + build_keys, }, PartitionMode::CollectLeft => PartitionBuildData::CollectLeft { pushdown, bounds, keys_have_null, + build_keys, }, PartitionMode::Auto => { return internal_err!( diff --git a/datafusion/physical-plan/src/joins/hash_join/exec.rs b/datafusion/physical-plan/src/joins/hash_join/exec.rs index dd2f172b28c53..18daa87027489 100644 --- a/datafusion/physical-plan/src/joins/hash_join/exec.rs +++ b/datafusion/physical-plan/src/joins/hash_join/exec.rs @@ -21,12 +21,14 @@ use crate::joins::array_map::ArrayMap; use crate::joins::hash_join::batched_join_processor::{ BatchedJoinProcessorConfig, RegularJoinKind, }; +use crate::joins::hash_join::bloom_filter; use crate::joins::hash_join::build::{HashJoinBuild, RegularJoinLeftData}; use crate::joins::hash_join::inlist_builder::build_struct_inlist_values; use crate::joins::hash_join::not_in::{NotIn, NotInBuild}; use crate::joins::hash_join::regular_join::RegularHashJoinStream; use crate::joins::hash_join::shared_bounds::{ - ColumnBounds, PartitionBounds, PushdownStrategy, SharedBuildAccumulator, + ColumnBounds, GlobalFilterOptions, PartitionBounds, PushdownStrategy, + SharedBuildAccumulator, }; use crate::joins::hash_join::stream::{ BuildSide, BuildSideInitialState, HashJoinStream, HashJoinStreamState, @@ -63,6 +65,7 @@ use crate::{ }, metrics::{ExecutionPlanMetricsSet, MetricsSet}, }; +use parquet::bloom_filter::Sbbf; use arrow::array::{Array, ArrayData, ArrayRef, BooleanBufferBuilder}; use arrow::compute::concat_batches; @@ -264,6 +267,9 @@ pub(super) struct JoinBuildData { /// Used for pre-filter probing, similar to [`Self::bounds`], but in different /// representations: IN-list values or a hash-table reference. pub(super) membership: PushdownStrategy, + /// Bloom filter of the build keys, for the global dynamic filter. `None` + /// when no bloom filter is possible for this build. + pub(super) bloom_filter: Option>, // Keep the reservation after the allocations it accounts for. reservation: MemoryReservation, } @@ -365,6 +371,11 @@ impl JoinLeftData { &self.build.membership } + /// Bloom filter of the build keys, for the dynamic filter. + pub(super) fn bloom_filter(&self) -> Option<&Arc> { + self.build.bloom_filter.as_ref() + } + /// Borrow bounds without sharing a consuming join's dynamic-filter state. pub(super) fn bounds(&self) -> Option<&PartitionBounds> { self.build.bounds.as_ref() @@ -1797,7 +1808,8 @@ impl ExecutionPlan for HashJoinExec { repartition_random_state, self.null_equality, not_in, - &context.session_config().options().optimizer, + GlobalFilterOptions::new(context.session_config().options()) + .with_memory_pool(Arc::clone(context.memory_pool())), )) }))) }) @@ -1821,6 +1833,10 @@ impl ExecutionPlan for HashJoinExec { // Both paths share collection, then create their own left-side data inside // the once-only future before sibling probes can access it. + let num_build_partitions = match self.mode { + PartitionMode::CollectLeft => 1, + _ => self.left.output_partitioning().partition_count(), + }; let collect = |build_partition, probe_partitions| { let left_stream = self.left.execute(build_partition, Arc::clone(&context))?; let name = match self.mode { @@ -1842,6 +1858,7 @@ impl ExecutionPlan for HashJoinExec { not_in, array_map_created_count.clone(), BuildMode::Ordinary, + num_build_partitions, )) }; @@ -3239,6 +3256,7 @@ async fn collect_left_input( not_in: Option, array_map_created_count: Count, mode: BuildMode, + num_build_partitions: usize, ) -> Result { let schema = left_stream.schema(); let prepared = mode == BuildMode::Prepared; @@ -3332,6 +3350,7 @@ async fn collect_left_input( not_in, &array_map_created_count, prepared, + num_build_partitions, ) } @@ -3363,6 +3382,7 @@ fn build_in_memory( not_in: Option, array_map_created_count: &Count, prepared: bool, + num_build_partitions: usize, ) -> Result { let is_phj_candidate = is_perfect_hash_join_candidate(on_left, schema)?; @@ -3561,6 +3581,29 @@ fn build_in_memory( bounds = None; } + // Fill the bloom filter of this partition's keys here, in its build task, + // so the finalizing task only ORs the partition filters. Partitions hold + // disjoint keys of similar counts, so the filter is sized for the keys of + // all partitions estimated from this one. + let bloom_filter = if !should_compute_dynamic_filters + || matches!(membership, PushdownStrategy::Empty) + || left_values.is_empty() + { + None + } else { + let filter = bloom_filter::partition_filter( + &left_values, + map.num_of_distinct_key() + .saturating_mul(num_build_partitions), + GlobalFilterOptions::new(config).bloom_filter, + &reservation, + )?; + if let Some(filter) = &filter { + metrics.build_mem_used.add(filter.num_blocks() * 32); + } + filter.map(Arc::new) + }; + if prepared { batches.clear(); // Prepared keys are direct columns. IN-list arrays share these batch @@ -3581,6 +3624,7 @@ fn build_in_memory( values: normalized_values, bounds, membership, + bloom_filter, reservation, }), not_in, @@ -3812,148 +3856,51 @@ mod tests { Ok(()) } - /// The dynamic filter shares the build map and can outlive the join's - /// stream and plan, so the map stays reserved until the last holder drops - /// it, not only until the partition's stream ends. - #[rstest] - #[tokio::test] - async fn build_map_stays_reserved_with_dynamic_filter( - #[values(PartitionMode::CollectLeft, PartitionMode::Partitioned)] - mode: PartitionMode, - ) -> Result<()> { - let SparseKeyJoinInputs { build, probe, on } = sparse_key_join_inputs()?; - let (join, filter) = hash_join_with_dynamic_filter_and_mode( - build, - probe, - on, - JoinType::Inner, - mode, - )?; - let join: Arc = Arc::new(join); - - let pool: Arc = Arc::new(GreedyMemoryPool::new(1 << 20)); - let runtime = RuntimeEnvBuilder::new() - .with_memory_pool(Arc::clone(&pool)) - .build_arc()?; - let mut config = SessionConfig::new(); - config - .options_mut() - .optimizer - .hash_join_inlist_pushdown_max_size = 0; - let task_ctx = Arc::new( - TaskContext::default() - .with_runtime(runtime) - .with_session_config(config), - ); - let batches = common::collect(join.execute(0, task_ctx)?).await?; - assert_eq!(batches.iter().map(|b| b.num_rows()).sum::(), 3); - - let build_mem_used = join - .metrics() - .unwrap() - .sum_by_name("build_mem_used") - .unwrap() - .as_usize(); - let keys = sparse_keys(); - let map_bytes = - estimate_memory_size::<(u32, u64)>(keys.len(), size_of::())? - + keys.len() * size_of::(); - let keys_array: ArrayRef = Arc::new(Int64Array::from(keys.clone())); - let bitmap_bytes = KeyRangeBitmap::try_new( - &keys_array, - &ScalarValue::Int64(Some(0)), - &ScalarValue::Int64(Some(keys[keys.len() - 1])), - keys.len(), - ) - .map_or(0, |bitmap| bitmap.size()); - let expected = match mode { - // The stream is gone; only the map and its bitmap are still held. - PartitionMode::Partitioned => map_bytes + bitmap_bytes, - PartitionMode::CollectLeft => build_mem_used, - PartitionMode::Auto => unreachable!(), - }; - assert_eq!(pool.reserved(), expected); - - // The filter is the last holder. - drop(join); - assert_eq!(pool.reserved(), map_bytes + bitmap_bytes); - drop(filter); - assert_eq!(pool.reserved(), 0); - Ok(()) + /// Bytes of the bloom filters in `expr`. + fn bloom_filter_bytes(expr: &Arc) -> Result { + let mut bytes = 0; + expr.apply(|e| { + if let Some(bloom) = e.downcast_ref::() { + bytes += bloom.filter_bytes(); + } + Ok(TreeNodeRecursion::Continue) + })?; + Ok(bytes) } - /// Dense keys build an `ArrayMap`, which is charged to the map's own - /// reservation in `array_map_key_range` rather than the build's. + /// The dynamic filter can outlive the join's streams and plan. It holds + /// its own bloom filter, not the build map, so after the plan is dropped + /// the pool holds only the bloom filter, until the filter is dropped. #[rstest] + #[case::collect_left(PartitionMode::CollectLeft, 1)] + #[case::partitioned(PartitionMode::Partitioned, 1)] + #[case::two_hash_partitions(PartitionMode::Partitioned, 2)] #[tokio::test] - async fn array_map_stays_reserved_with_dynamic_filter( - #[values(PartitionMode::CollectLeft, PartitionMode::Partitioned)] - mode: PartitionMode, + async fn dynamic_filter_reserves_only_its_bloom_filter( + #[case] mode: PartitionMode, + #[case] partitions: usize, ) -> Result<()> { - let schema = Arc::new(Schema::new(vec![Field::new("k", DataType::Int64, false)])); - let batch = |keys: Vec| { - let column = Arc::new(Int64Array::from(keys)) as ArrayRef; - let batch = RecordBatch::try_new(Arc::clone(&schema), vec![column])?; - TestMemoryExec::try_new_exec(&[vec![batch]], Arc::clone(&schema), None) - }; - let on = vec![( - Arc::new(Column::new_with_schema("k", &schema)?) as _, - Arc::new(Column::new_with_schema("k", &schema)?) as _, - )]; - let (join, filter) = hash_join_with_dynamic_filter_and_mode( - batch((0..20_000).collect())?, - batch(vec![0, 5, 15])?, + let SparseKeyJoinInputs { + mut build, + mut probe, on, - JoinType::Inner, - mode, - )?; - let join: Arc = Arc::new(join); - - let pool: Arc = Arc::new(GreedyMemoryPool::new(1 << 22)); - let runtime = RuntimeEnvBuilder::new() - .with_memory_pool(Arc::clone(&pool)) - .build_arc()?; - let mut config = SessionConfig::new(); - config - .options_mut() - .optimizer - .hash_join_inlist_pushdown_max_size = 0; - let task_ctx = Arc::new( - TaskContext::default() - .with_runtime(runtime) - .with_session_config(config), - ); - let batches = common::collect(join.execute(0, task_ctx)?).await?; - assert_eq!(batches.iter().map(|b| b.num_rows()).sum::(), 3); - assert_phj_used(&join.metrics().unwrap(), true); - - drop(join); - assert_eq!( - pool.reserved(), - ArrayMap::estimate_memory_size(0, 19_999, 20_000) - ); - drop(filter); - assert_eq!(pool.reserved(), 0); - Ok(()) - } - - /// With rows in more than one build partition, the filter is a routed lookup - /// that holds every partition's map, so the maps stay reserved until it is - /// dropped. - #[cfg(not(feature = "force_hash_collisions"))] - #[tokio::test] - async fn routed_maps_stay_reserved_with_dynamic_filter() -> Result<()> { - let SparseKeyJoinInputs { build, probe, on } = sparse_key_join_inputs()?; - let repartition = |input: Arc, key: &PhysicalExprRef| { - let partitioning = Partitioning::Hash(vec![Arc::clone(key)], 2); - RepartitionExec::try_new(input, partitioning) - }; + } = sparse_key_join_inputs()?; + if partitions > 1 { + let repartition = |input, key: &PhysicalExprRef| { + let partitioning = Partitioning::Hash(vec![Arc::clone(key)], partitions); + Ok::<_, datafusion_common::DataFusionError>(Arc::new( + RepartitionExec::try_new(input, partitioning)?, + ) as _) + }; + build = repartition(build, &on[0].0)?; + probe = repartition(probe, &on[0].1)?; + } let (join, filter) = hash_join_with_dynamic_filter_and_mode( - Arc::new(repartition(build, &on[0].0)?), - Arc::new(repartition(probe, &on[0].1)?), + build, + probe, on, JoinType::Inner, - PartitionMode::Partitioned, + mode, )?; let join: Arc = Arc::new(join); @@ -3973,14 +3920,11 @@ mod tests { ); let batches = crate::execution_plan::collect(Arc::clone(&join), task_ctx).await?; assert_eq!(batches.iter().map(|b| b.num_rows()).sum::(), 3); - assert!(format!("{:?}", filter.current()?).contains("routed by")); - // Every stream is gone; only the maps and their bitmaps are still held. - let reserved = pool.reserved(); - assert!(reserved > 0); - // The filter is the last holder. + let bloom_bytes = bloom_filter_bytes(&filter.current()?)?; + assert!(bloom_bytes > 0); drop(join); - assert_eq!(pool.reserved(), reserved); + assert_eq!(pool.reserved(), bloom_bytes); drop(filter); assert_eq!(pool.reserved(), 0); Ok(()) @@ -4658,6 +4602,7 @@ mod tests { None, Count::new(), BuildMode::Ordinary, + 1, ) .await? .into_legacy_left_data(); @@ -10713,9 +10658,7 @@ mod tests { /// /// Here the build-side logical NULL lives in exactly one build partition, so /// the `keys_have_null` flag has to survive the per-partition aggregation in - /// `build_partitioned_filter` and widen the *routed* filter as a whole: the - /// probe NULL is routed to a single partition, and OR-ing `IS NULL` into - /// that one partition's check would still drop it for every other route. + /// `build_partitioned_filter` and widen the global filter as a whole. #[tokio::test] async fn test_partitioned_null_equal_dynamic_filter_keeps_probe_nulls_for_build_logical_null() -> Result<()> { @@ -10794,11 +10737,11 @@ mod tests { dynamic_filter.wait_complete().await; let filter = dynamic_filter.current()?.to_string(); - // The aggregated build-side NULL must widen the whole merged filter once, - // at the top: `c2 IS NULL OR `. + // The aggregated build-side NULL must widen the global filter once, at the + // top: `c2 IS NULL OR `. assert!( filter.starts_with("c2@0 IS NULL OR"), - "expected the IS NULL disjunct to wrap the whole routed filter, got: {filter}" + "expected the IS NULL disjunct to wrap the whole filter, got: {filter}" ); // The NULL-NULL row (d1 = 200, d2 = 20) is only produced if the probe diff --git a/datafusion/physical-plan/src/joins/hash_join/exec/prepared.rs b/datafusion/physical-plan/src/joins/hash_join/exec/prepared.rs index 7b580f09d3866..4d1fe1e18458a 100644 --- a/datafusion/physical-plan/src/joins/hash_join/exec/prepared.rs +++ b/datafusion/physical-plan/src/joins/hash_join/exec/prepared.rs @@ -189,6 +189,7 @@ impl HashJoinExec { None, count, BuildMode::Prepared, + 1, ) .await?; Ok(Arc::new(PreparedHashJoinBuild { diff --git a/datafusion/physical-plan/src/joins/hash_join/mod.rs b/datafusion/physical-plan/src/joins/hash_join/mod.rs index e5a10cfd3e6cf..e6b22864d246c 100644 --- a/datafusion/physical-plan/src/joins/hash_join/mod.rs +++ b/datafusion/physical-plan/src/joins/hash_join/mod.rs @@ -18,6 +18,13 @@ //! [`BatchedJoinProcessor`]: batched_join_processor::BatchedJoinProcessor //! [`Map`]: crate::joins::Map +/// # Public Only for Internal Use: +/// +/// This is not a public API and is for internal use only; see [API policy] for details. +/// +/// [API policy]: https://datafusion.apache.org/contributor-guide/api-health.html#internal-public-apis +#[doc(hidden)] +pub use bloom_filter::BloomFilterExpr; pub use exec::{HashJoinExec, HashJoinExecBuilder, PreparedHashJoinBuild}; /// # Public Only for Internal Use: /// @@ -29,6 +36,7 @@ pub use partitioned_hash_eval::HashTableLookupExpr; pub use partitioned_hash_eval::{HashExpr, SeededRandomState}; mod batched_join_processor; +mod bloom_filter; mod build; mod exec; mod inlist_builder; diff --git a/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs b/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs index 091cc3a34fa8a..8c6409a865b50 100644 --- a/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs +++ b/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs @@ -5,33 +5,32 @@ //! Utilities for shared build-side information. Used in dynamic filter pushdown in Hash Joins. // TODO: include the link to the Dynamic Filter blog post. +use std::collections::HashSet; use std::fmt; use std::sync::Arc; use crate::ExecutionPlan; use crate::ExecutionPlanProperties; use crate::Partitioning; -use crate::joins::Map; use crate::joins::PartitionMode; use crate::joins::ReservedMap; +use crate::joins::hash_join::bloom_filter::{BloomFilterExpr, BloomFilterOptions}; use crate::joins::hash_join::exec::HASH_JOIN_SEED; use crate::joins::hash_join::inlist_builder::build_struct_fields; use crate::joins::hash_join::not_in::NotIn; use crate::joins::hash_join::partitioned_hash_eval::{ - HashTableLookupExpr, PartitionRouter, SeededRandomState, + HashTableLookupExpr, SeededRandomState, }; -use crate::joins::join_hash_map::{JoinHashMapType, JoinHashMapU32}; use crate::joins::key_range_bitmap::{KeyRangeBitmap, KeyRangePart}; -use crate::repartition::RangeExpr; -use arrow::array::{ArrayRef, AsArray}; -use arrow::compute::concat; +use arrow::array::{Array, ArrayRef, UInt32Array}; use arrow::datatypes::{DataType, Field, Schema}; -use datafusion_common::config::{ConfigOptions, OptimizerOptions}; -use datafusion_common::hash_utils::create_hashes; +use arrow::row::{RowConverter, SortField}; +use datafusion_common::config::ConfigOptions; use datafusion_common::{ DataFusionError, NullEquality, Result, ScalarValue, SharedResult, - assert_or_internal_err, internal_err, + assert_or_internal_err, }; +use datafusion_execution::memory_pool::{MemoryPool, UnboundedMemoryPool}; use datafusion_expr::Operator; use datafusion_functions::core::r#struct as struct_func; use datafusion_physical_expr::expressions::{ @@ -40,17 +39,35 @@ use datafusion_physical_expr::expressions::{ use datafusion_physical_expr::{ PhysicalExpr, PhysicalExprRef, RangePartitioning, ScalarFunctionExpr, }; +use parquet::bloom_filter::Sbbf; use parking_lot::Mutex; use tokio::sync::Notify; +/// The distinct values of `values`, in the order of their first occurrence. +/// +/// The InList of a partition holds every build row, so the concatenated lists +/// of all partitions can repeat few keys many times. +fn distinct_values(values: &ArrayRef) -> Result { + let converter = RowConverter::new(vec![SortField::new(values.data_type().clone())])?; + let rows = converter.convert_columns(std::slice::from_ref(values))?; + let mut seen = HashSet::with_capacity(rows.num_rows()); + let indices: UInt32Array = (0..rows.num_rows() as u32) + .filter(|&i| seen.insert(rows.row(i as usize))) + .collect(); + if indices.len() == values.len() { + return Ok(Arc::clone(values)); + } + Ok(arrow::compute::take(values.as_ref(), &indices, None)?) +} + /// Represents the minimum and maximum values for a specific column. /// Used in dynamic filter pushdown to establish value boundaries. #[derive(Debug, Clone, PartialEq)] pub(crate) struct ColumnBounds { /// The minimum value observed for this column pub(crate) min: ScalarValue, - /// The maximum value observed for this column + /// The maximum value observed for this column pub(crate) max: ScalarValue, } @@ -78,35 +95,92 @@ impl PartitionBounds { self.column_bounds.get(index) } - /// Bounds covering both `self` and `other`, which must describe the same - /// columns. A NULL bound means the partition holds no non-null key in that - /// column, so it never widens the other side's bound. - fn union(self, other: &PartitionBounds) -> PartitionBounds { - // Every non-empty partition of a join reports one bound per join key. - debug_assert_eq!( - self.column_bounds.len(), - other.column_bounds.len(), - "partitions report bounds for different numbers of columns" - ); - let column_bounds = self - .column_bounds - .into_iter() - .zip(&other.column_bounds) - .map(|(a, b)| { - let min = if !b.min.is_null() && (a.min.is_null() || b.min < a.min) { - b.min.clone() - } else { - a.min - }; - let max = if !b.max.is_null() && (a.max.is_null() || b.max > a.max) { - b.max.clone() - } else { - a.max - }; - ColumnBounds::new(min, max) - }) - .collect(); - PartitionBounds::new(column_bounds) + /// Bounds that hold the keys of every partition: the smallest minimum and the + /// largest maximum of each column. + /// + /// `None` when a partition has no bounds or a column's values cannot be + /// compared. Then a bounds predicate is not possible. + fn merge<'a>( + mut partitions: impl Iterator, + ) -> Option { + let mut merged = partitions.next()?.clone(); + if merged.column_bounds.is_empty() { + return None; + } + for bounds in partitions { + if bounds.column_bounds.len() != merged.column_bounds.len() { + return None; + } + for (acc, b) in merged.column_bounds.iter_mut().zip(&bounds.column_bounds) { + // A NULL bound comes from a partition with no non-NULL key in this + // column. It must not widen (or, as a NULL comparison, disable) the + // bounds of the keys of other partitions. + if acc.min.is_null() + || (!b.min.is_null() && b.min.partial_cmp(&acc.min)?.is_lt()) + { + acc.min = b.min.clone(); + } + if acc.max.is_null() + || (!b.max.is_null() && b.max.partial_cmp(&acc.max)?.is_gt()) + { + acc.max = b.max.clone(); + } + } + } + Some(merged) + } +} + +/// The build side key columns of one partition, for the global dynamic filter. +#[derive(Clone, Default)] +pub(crate) struct BuildKeys { + /// One array per join key + pub(crate) keys: Vec, + /// Bloom filter of the keys. `None` when this partition has no filter, + /// then the global filter has no bloom filter. + pub(crate) bloom_filter: Option>, + /// Number of distinct key tuples + pub(crate) num_distinct: usize, +} + +/// Options for the global dynamic filter of a hash join, from the session +/// configuration. +#[derive(Debug, Clone)] +pub(crate) struct GlobalFilterOptions { + /// Use an InList when all partitions together have at most this many + /// distinct keys (`hash_join_dynamic_filter_inlist_max_distinct_values`). + pub(crate) inlist_max_distinct_values: usize, + pub(crate) bloom_filter: BloomFilterOptions, + /// Pool that the bloom filter memory is charged to. `None` (tests only) + /// does not track the memory. + pub(crate) memory_pool: Option>, +} + +impl GlobalFilterOptions { + pub(crate) fn new(config: &ConfigOptions) -> Self { + let optimizer = &config.optimizer; + Self { + inlist_max_distinct_values: optimizer + .hash_join_dynamic_filter_inlist_max_distinct_values, + bloom_filter: BloomFilterOptions { + fpp: optimizer.hash_join_bloom_filter_fpp, + max_bytes: optimizer.hash_join_bloom_filter_max_bytes, + max_fpp: optimizer.hash_join_bloom_filter_max_fpp, + }, + memory_pool: None, + } + } + + /// Charge the bloom filter memory to `pool`. + pub(crate) fn with_memory_pool(mut self, pool: Arc) -> Self { + self.memory_pool = Some(pool); + self + } +} + +impl Default for GlobalFilterOptions { + fn default() -> Self { + Self::new(&ConfigOptions::default()) } } @@ -257,9 +331,12 @@ fn combine_membership_and_bounds( /// /// ## Hash Map vs Bounds /// -/// - **Hash Maps (Partitioned mode)**: Collects Arc references to hash tables from each partition. -/// Creates a `PartitionedHashLookupPhysicalExpr` that routes rows to the correct partition's hash table. -/// - **Bounds (CollectLeft mode)**: Collects min/max bounds and creates range predicates. +/// - **Global filter (default)**: merges the bounds and keys of all partitions +/// into one unrouted filter: global min/max bounds AND an InList (few keys) or +/// a bloom filter of all build keys. See `build_global_filter`. +/// - **Routed filter (fallback)**: when a partition was canceled or the key type +/// has no bloom filter support, a `CASE` on the repartition hash (or range) +/// routes each probe row to its partition's bounds and InList / hash table. /// /// ## Partition Counting /// @@ -287,10 +364,12 @@ pub(crate) struct SharedBuildAccumulator { on_right: Vec, /// Random state for partitioning (RepartitionExec's hash function with 0,0,0,0 seeds) /// Used for PartitionedHashLookupPhysicalExpr + #[expect(dead_code)] repartition_random_state: SeededRandomState, /// Schema of the probe (right) side for evaluating filter expressions probe_schema: Arc, /// Probe-side Range routing metadata for partitioned dynamic filters. + #[cfg_attr(not(test), expect(dead_code))] probe_range_partitioning: Option, /// Null equality of the join. Under `NullEqualsNull` a probe-side NULL can match a /// build-side NULL, so the pushed filter must keep NULL rows here too. @@ -298,12 +377,8 @@ pub(crate) struct SharedBuildAccumulator { /// Null-aware anti join (`NOT IN`). A probe-side NULL must reach the join so its /// three-valued logic can collapse the result, so the pushed filter keeps NULL rows. not_in: Option, - /// Largest merged IN list, in bytes, that partitioned filters keep as an IN - /// list: `hash_join_inlist_pushdown_max_size`, as for a single build. - in_list_max_size: usize, - /// Largest merged IN list, in values, that partitioned filters keep as an IN - /// list: `hash_join_inlist_pushdown_max_distinct_values`, as for a single build. - in_list_max_values: usize, + /// Options for the global (unrouted) filter + global_filter_options: GlobalFilterOptions, } /// Strategy for filter pushdown (decided at collection time) @@ -325,11 +400,13 @@ pub(crate) enum PartitionBuildData { pushdown: PushdownStrategy, bounds: PartitionBounds, keys_have_null: bool, + build_keys: BuildKeys, }, CollectLeft { pushdown: PushdownStrategy, bounds: PartitionBounds, keys_have_null: bool, + build_keys: BuildKeys, }, } @@ -341,6 +418,8 @@ struct PartitionData { /// Whether any build key of this partition is NULL. Decides whether the pushed /// filter must keep probe-side NULL rows for a null-equal join to match them. keys_have_null: bool, + /// The build side keys, for the global filter + build_keys: BuildKeys, } /// Build-side data organized by partition mode @@ -427,7 +506,7 @@ impl SharedBuildAccumulator { repartition_random_state: SeededRandomState, null_equality: NullEquality, not_in: Option, - optimizer: &OptimizerOptions, + global_filter_options: GlobalFilterOptions, ) -> Self { // Troubleshooting: If partition counts are incorrect, verify this logic matches // the actual execution pattern in collect_build_side() @@ -485,8 +564,7 @@ impl SharedBuildAccumulator { probe_range_partitioning, null_equality, not_in, - in_list_max_size: optimizer.hash_join_inlist_pushdown_max_size, - in_list_max_values: optimizer.hash_join_inlist_pushdown_max_distinct_values, + global_filter_options, } } @@ -545,6 +623,7 @@ impl SharedBuildAccumulator { pushdown, bounds, keys_have_null, + build_keys, }, AccumulatedBuildData::Partitioned { partitions, @@ -558,6 +637,7 @@ impl SharedBuildAccumulator { bounds, pushdown, keys_have_null, + build_keys, }); } ( @@ -565,6 +645,7 @@ impl SharedBuildAccumulator { pushdown, bounds, keys_have_null, + build_keys, }, AccumulatedBuildData::CollectLeft { data, @@ -577,6 +658,7 @@ impl SharedBuildAccumulator { bounds, pushdown, keys_have_null, + build_keys, }); } *reported_count += 1; @@ -669,18 +751,27 @@ impl SharedBuildAccumulator { self.build_collect_left_filter(partition) } FinalizeInput::Partitioned(partitions) => { - self.build_partitioned_filter(partitions) + self.build_partitioned_filter(&partitions) } } } /// Builds the single global filter used by a collect-left join. fn build_collect_left_filter(&self, partition: PartitionStatus) -> Result<()> { + // An empty build side keeps the filter unchanged, as before. + if let PartitionStatus::Reported(data) = &partition + && !matches!(data.pushdown, PushdownStrategy::Empty) + { + return self + .dynamic_filter + .update(self.build_global_filter(&[data])?); + } match partition { PartitionStatus::Reported(PartitionData { bounds, pushdown, keys_have_null, + build_keys: _, }) => { let membership_expr = create_membership_predicate( &self.on_right, @@ -710,198 +801,143 @@ impl SharedBuildAccumulator { /// Builds one probe-side filter from finalized partitioned build data. /// Empty and canceled partitions contribute no key. - fn build_partitioned_filter(&self, partitions: Vec) -> Result<()> { - let num_partitions = partitions.len(); - let mut members = Vec::with_capacity(num_partitions); - let mut merged_bounds: Option = None; - let mut keys_have_null = false; - - for (partition_id, partition) in partitions.into_iter().enumerate() { + fn build_partitioned_filter(&self, partitions: &[PartitionStatus]) -> Result<()> { + let mut reported = Vec::with_capacity(partitions.len()); + for partition in partitions { match partition { + PartitionStatus::Reported(data) => reported.push(data), // A canceled partition's stream never probes, so no key of its // build side can matter. - PartitionStatus::CanceledUnknown - | PartitionStatus::Reported(PartitionData { - pushdown: PushdownStrategy::Empty, - .. - }) => {} - PartitionStatus::Reported(PartitionData { - bounds, - pushdown, - keys_have_null: partition_keys_have_null, - }) => { - keys_have_null |= partition_keys_have_null; - merged_bounds = Some(match merged_bounds { - None => bounds.clone(), - Some(merged) => merged.union(&bounds), - }); - members.push((partition_id, pushdown, bounds)); - } + PartitionStatus::CanceledUnknown => {} PartitionStatus::Pending => { - return internal_err!( + return datafusion_common::internal_err!( "attempted to finalize dynamic filter with pending partition" ); } } } - - let filter_expr = match members.as_slice() { - // No build row exists, so no probe row can match. - [] => lit(false), - // Only one build partition holds rows, so its own filter covers every - // probe match without routing, and an IN list keeps its pruning. - [(_, pushdown, bounds)] => combine_membership_and_bounds( - create_membership_predicate( - &self.on_right, - pushdown.clone(), - &HASH_JOIN_SEED, - self.probe_schema.as_ref(), - )?, - create_bounds_predicate(&self.on_right, bounds), - ) - .expect("membership predicate is always present"), - _ => { - self.merged_membership_filter(&members, merged_bounds, num_partitions)? - } - }; self.dynamic_filter - .update(self.preserve_probe_nulls(filter_expr, keys_have_null)?) + .update(self.build_global_filter(&reported)?) } - /// `members` holds each non-empty partition's id, strategy (an IN list, a - /// hash table or an array map) and bounds, out of `num_partitions` partitions. - fn merged_membership_filter( + /// Builds one filter for all probe rows from the build data of every partition, + /// with no routing to partitions: + /// + /// ```text + /// [key IS NULL OR] key >= min AND key <= max AND membership(key) + /// ``` + /// + /// `min` and `max` are the global bounds. `membership` is an InList of the + /// keys of all partitions when they have at most + /// `hash_join_dynamic_filter_inlist_max_distinct_values` distinct keys together, + /// and a [`BloomFilterExpr`] otherwise. The filter is true for every build + /// key, whatever the partitioning of the build and probe sides, so it is + /// correct for hash, range and collect-left joins. + fn build_global_filter( &self, - members: &[(usize, PushdownStrategy, PartitionBounds)], - merged_bounds: Option, - num_partitions: usize, + partitions: &[&PartitionData], ) -> Result> { - let lists = members + let non_empty: Vec<&PartitionData> = partitions + .iter() + .copied() + .filter(|p| !matches!(p.pushdown, PushdownStrategy::Empty)) + .collect(); + if non_empty.is_empty() { + // No build row exists, so no probe row can match. + return Ok(lit(false)); + } + let keys_have_null = non_empty.iter().any(|p| p.keys_have_null); + let bounds = PartitionBounds::merge(non_empty.iter().map(|p| &p.bounds)); + let num_distinct: usize = + non_empty.iter().map(|p| p.build_keys.num_distinct).sum(); + + let in_lists = non_empty .iter() - .filter_map(|(_, pushdown, _)| match pushdown { - PushdownStrategy::InList(values) => Some(values), + .map(|p| match &p.pushdown { + PushdownStrategy::InList(values) => Some(Arc::clone(values)), _ => None, }) - .collect::>(); - - // Every partition is an IN list, and together they still fit the limits a - // single build uses to pick an IN list: one combined list keeps the - // statistics and bloom filter pruning an IN list allows. Past those - // limits, a hash lookup is cheaper per row and carries a pruning bitmap. - let combined_in_list = lists.len() == members.len() && { - let values = lists.iter().map(|list| list.len()).sum::(); - let bytes = lists - .iter() - .map(|list| list.get_array_memory_size()) - .sum::(); - values <= self.in_list_max_values && bytes <= self.in_list_max_size - }; - let membership = if combined_in_list { - let values = concat(&lists.iter().map(|l| l.as_ref()).collect::>())?; - create_membership_predicate( - &self.on_right, - PushdownStrategy::InList(values), - &HASH_JOIN_SEED, - self.probe_schema.as_ref(), - )? - .expect("an IN list always has a membership predicate") - } else { - // Each probe row is looked up in the table of the partition its key - // routes to, so the partitions' tables are shared as they are: no - // merged table to build while every probe stream waits on the filter. - let mut maps = vec![None; num_partitions]; - for (partition_id, pushdown, _) in members { - maps[*partition_id] = Some(match pushdown { - PushdownStrategy::Map(map, _) => Arc::clone(map), - PushdownStrategy::InList(values) => self.in_list_hash_map(values)?, - PushdownStrategy::Empty => { - return internal_err!("empty partitions have no members"); - } - }); + .collect::>>(); + let membership_expr = match in_lists { + // The partitions hold disjoint keys, so the sum of their distinct + // counts is the number of distinct keys of the build side. + Some(in_lists) + if num_distinct + <= self.global_filter_options.inlist_max_distinct_values => + { + let arrays: Vec<&dyn Array> = + in_lists.iter().map(|a| a.as_ref()).collect(); + let values = distinct_values(&arrow::compute::concat(&arrays)?)?; + create_membership_predicate( + &self.on_right, + PushdownStrategy::InList(values), + &HASH_JOIN_SEED, + self.probe_schema.as_ref(), + )? } - Arc::new(HashTableLookupExpr::new_reserved( - self.on_right.clone(), - HASH_JOIN_SEED, - Some(self.partition_router(num_partitions)?), - maps, - String::from("hash_lookup"), - self.merged_pruning_bitmap(members, merged_bounds.as_ref()), - )?) - }; - - let bounds = merged_bounds - .and_then(|bounds| create_bounds_predicate(&self.on_right, &bounds)); - Ok(combine_membership_and_bounds(Some(membership), bounds) - .expect("membership predicate is always present")) - } - - /// How probe rows find their build partition: as the probe side was - /// partitioned. - fn partition_router(&self, num_partitions: usize) -> Result { - Ok(match &self.probe_range_partitioning { - Some(range_partitioning) => { - assert_or_internal_err!( - num_partitions == range_partitioning.partition_count(), - "Dynamic filter partition count {} does not match Range partition count {}", - num_partitions, - range_partitioning.partition_count() - ); - PartitionRouter::Range(Arc::new(RangeExpr::try_new_with_schema( + _ => { + let pruning_bitmap = + self.merge_pruning_bitmaps(&non_empty, bounds.as_ref()); + // Every partition must have its filter. A partition has none + // when the build side is too big for a bloom filter, or its + // memory reservation failed. + let filters = non_empty + .iter() + .map(|p| p.build_keys.bloom_filter.as_deref()) + .collect::>>(); + let pool = self + .global_filter_options + .memory_pool + .clone() + .unwrap_or_else(|| Arc::new(UnboundedMemoryPool::default())); + let bloom = BloomFilterExpr::try_new( self.on_right.clone(), - range_partitioning, - &self.probe_schema, - )?)) + filters.as_deref(), + self.global_filter_options.bloom_filter, + &pool, + pruning_bitmap, + )?; + // Without a bloom filter, keep the expression only for its + // pruning bitmap + (bloom.has_filter() || bloom.pruning_bitmap().is_some()) + .then(|| Arc::new(bloom) as Arc) } - None => PartitionRouter::Hash(self.repartition_random_state.clone()), - }) - } - - /// A hash table of an IN list partition's keys, hashed as the probe side - /// hashes its join keys: one column per join key. - /// - /// The table is not reserved, same as the IN list it is built from. Its - /// size follows that list, which `hash_join_inlist_pushdown_max_size` caps. - fn in_list_hash_map(&self, values: &ArrayRef) -> Result> { - let columns = match values.as_struct_opt() { - Some(columns) if self.on_right.len() > 1 => columns.columns(), - _ => std::slice::from_ref(values), }; - let mut hashes = vec![0; values.len()]; - create_hashes(columns, HASH_JOIN_SEED.random_state(), &mut hashes)?; - let mut map = JoinHashMapU32::with_capacity(hashes.len()); - map.update_from_iter(Box::new(hashes.iter().enumerate()), 0); - let map = Arc::new(Map::HashMap(Box::new(map))); - Ok(Arc::new(ReservedMap::unreserved(map))) - } - - /// Container pruning reads one bitmap, so merge the partitions' key ranges: - /// a hash table's bitmap, an IN list's keys, or else a partition's whole - /// bounds. `None` when nothing can be excluded. - fn merged_pruning_bitmap( + let bounds_expr = bounds + .as_ref() + .and_then(|b| create_bounds_predicate(&self.on_right, b)); + let filter_expr = combine_membership_and_bounds(membership_expr, bounds_expr) + .unwrap_or_else(|| lit(true)); + self.preserve_probe_nulls(filter_expr, keys_have_null) + } + + /// One [`KeyRangeBitmap`] for the keys of all partitions, so that scans can + /// prune containers (for example parquet row groups and pages) with the + /// global filter. Only for a single integer key. + fn merge_pruning_bitmaps( &self, - members: &[(usize, PushdownStrategy, PartitionBounds)], - merged_bounds: Option<&PartitionBounds>, + partitions: &[&PartitionData], + bounds: Option<&PartitionBounds>, ) -> Option> { if self.on_right.len() != 1 { return None; } - let merged = merged_bounds?.get_column_bounds(0)?; - let parts = members + let column_bounds = bounds?.get_column_bounds(0)?; + let parts = partitions .iter() - .map(|(_, pushdown, bounds)| match pushdown { + .map(|p| match &p.pushdown { PushdownStrategy::Map(_, Some(bitmap)) => { - Some(KeyRangePart::Bitmap(bitmap)) + Some(KeyRangePart::Bitmap(bitmap.as_ref())) } - PushdownStrategy::InList(keys) => Some(KeyRangePart::Keys(keys.as_ref())), - _ => bounds - .get_column_bounds(0) - .map(|bounds| KeyRangePart::Range { - min: &bounds.min, - max: &bounds.max, - }), + _ => p + .build_keys + .keys + .first() + .map(|keys| KeyRangePart::Keys(keys.as_ref())), }) .collect::>>()?; - KeyRangeBitmap::union(&merged.min, &merged.max, &parts).map(Arc::new) + KeyRangeBitmap::union(&column_bounds.min, &column_bounds.max, &parts) + .map(Arc::new) } /// Keeps probe rows with a NULL key when the join semantics need them. @@ -1000,8 +1036,7 @@ pub(super) fn make_partitioned_accumulator_for_test( probe_range_partitioning: None, null_equality: NullEquality::NullEqualsNothing, not_in: None, - in_list_max_size: 128 * 1024, - in_list_max_values: 150, + global_filter_options: GlobalFilterOptions::default(), } } @@ -1021,15 +1056,32 @@ pub(super) fn completed_partitions_for_test(acc: &SharedBuildAccumulator) -> usi #[cfg(test)] mod tests { use super::*; + use crate::joins::Map; use crate::joins::hash_join::not_in::{NotInOutput, NotInScope}; - use crate::joins::join_hash_map::JoinHashMapU32; - use arrow::array::{ArrayRef, Int32Array}; - use datafusion_physical_expr::expressions::{Column, Literal}; - #[cfg(not(feature = "force_hash_collisions"))] - use { - arrow::array::BooleanArray, arrow::record_batch::RecordBatch, - datafusion_common::SplitPoint, datafusion_physical_expr::PhysicalSortExpr, + /// Adds the bloom filter of `build_keys.keys`, sized for 4 times its + /// keys (the keys of a few partitions). + fn test_build_keys(mut build_keys: BuildKeys) -> BuildKeys { + let reservation = datafusion_execution::memory_pool::MemoryConsumer::new("test") + .register(&(Arc::new(UnboundedMemoryPool::default()) as Arc)); + build_keys.bloom_filter = + crate::joins::hash_join::bloom_filter::partition_filter( + &build_keys.keys, + build_keys.num_distinct * 4, + GlobalFilterOptions::default().bloom_filter, + &reservation, + ) + .unwrap() + .map(Arc::new); + build_keys + } + + use arrow::array::{ArrayRef, BooleanArray, Float64Array, Int32Array}; + use arrow::record_batch::RecordBatch; + use datafusion_common::SplitPoint; + use datafusion_physical_expr::{ + PhysicalSortExpr, + expressions::{Column, Literal}, }; fn test_on_right() -> Vec { @@ -1068,8 +1120,7 @@ mod tests { probe_range_partitioning: None, null_equality: NullEquality::NullEqualsNothing, not_in: None, - in_list_max_size: 128 * 1024, - in_list_max_values: 150, + global_filter_options: GlobalFilterOptions::default(), } } @@ -1116,6 +1167,7 @@ mod tests { pushdown, bounds, keys_have_null: false, + build_keys: BuildKeys::default(), }) } @@ -1188,421 +1240,392 @@ mod tests { (partitions.clone(), *completed_partitions) } - #[test] - fn collect_left_updates_with_membership_only() { - let acc = make_collect_left_accumulator_for_test(); - - acc.build_filter(FinalizeInput::CollectLeft(reported( - in_list(&[1, 2, 3]), - no_bounds(), - ))) - .unwrap(); - - let expr = current_expr(&acc); - assert_in_list_column_values(&expr, "probe_key", 0, &[1, 2, 3]); - } - - #[test] - fn collect_left_updates_with_bounds_only() { - let acc = make_collect_left_accumulator_for_test(); - - acc.build_filter(FinalizeInput::CollectLeft(reported( - PushdownStrategy::Empty, - bounds(10, 20), - ))) - .unwrap(); - - let expr = current_expr(&acc); - assert_top_binary_op(&expr, Operator::And); + /// A partition that reports a hash table (more keys than the InList limit). + fn reported_map(keys: Vec) -> PartitionStatus { + let (min, max) = (*keys.iter().min().unwrap(), *keys.iter().max().unwrap()); + let num_distinct = keys.len(); + PartitionStatus::Reported(PartitionData { + pushdown: PushdownStrategy::Map( + Arc::new(ReservedMap::unreserved(Arc::new(Map::HashMap(Box::new( + crate::joins::join_hash_map::JoinHashMapU32::with_capacity(0), + ))))), + None, + ), + bounds: bounds(min, max), + keys_have_null: false, + build_keys: test_build_keys(BuildKeys { + bloom_filter: None, + keys: vec![Arc::new(Int32Array::from(keys))], + num_distinct, + }), + }) } - #[test] - fn collect_left_empty_build_data_does_not_update_filter() { - let acc = make_collect_left_accumulator_for_test(); - let initial_generation = acc.dynamic_filter.snapshot_generation(); - - acc.build_filter(FinalizeInput::CollectLeft(reported( - PushdownStrategy::Empty, - no_bounds(), - ))) + fn eval_bool(expr: &PhysicalExprRef, keys: Vec) -> Vec { + let batch = RecordBatch::try_new( + test_probe_schema(), + vec![Arc::new(Int32Array::from(keys))], + ) .unwrap(); - - assert_eq!( - acc.dynamic_filter.snapshot_generation(), - initial_generation, - "empty CollectLeft input must not update with a no-op filter" - ); - let expr = current_expr(&acc); - assert_literal_bool(&expr, true); - } - - fn hash_map() -> PushdownStrategy { - let map = Arc::new(Map::HashMap(Box::new(JoinHashMapU32::with_capacity(1)))); - PushdownStrategy::Map(Arc::new(ReservedMap::unreserved(map)), None) + let result = expr + .evaluate(&batch) + .unwrap() + .into_array(batch.num_rows()) + .unwrap(); + let result = result.as_any().downcast_ref::().unwrap(); + result.iter().map(|v| v.unwrap_or(false)).collect() } + /// Hash partitions with many keys get one unrouted filter: + /// `key >= global_min AND key <= global_max AND bloom_filter`. #[test] - fn partitioned_hash_map_partitions_merge_into_one_lookup() { + fn partitioned_global_bloom_filter() { let acc = make_partitioned_expr_accumulator_for_test(3); - acc.build_filter(FinalizeInput::Partitioned(vec![ - reported(hash_map(), bounds(5, 10)), + reported_map((0..1000).map(|k| k * 3).collect()), reported(PushdownStrategy::Empty, no_bounds()), - reported(hash_map(), bounds(1, 7)), + reported_map((0..1000).map(|k| k * 3 + 10_000).collect()), ])) .unwrap(); let expr = current_expr(&acc); + let text = format!("{expr}"); assert_eq!( - expr.to_string(), - "probe_key@0 >= 1 AND probe_key@0 <= 10 AND hash_lookup" + text, "probe_key@0 >= 0 AND probe_key@0 <= 12997 AND bloom_filter", + "{text}" ); + assert!(!text.contains("CASE")); + + let probe: Vec = (-10..13_010).collect(); + let result = eval_bool(&expr, probe.clone()); + let mut false_positives = 0; + for (k, pass) in probe.iter().zip(result) { + let is_key = (*k >= 0 && *k < 3000 && k % 3 == 0) + || (*k >= 10_000 && *k < 13_000 && (k - 10_000) % 3 == 0); + assert!(pass || !is_key, "build key {k} dropped"); + false_positives += (pass && !is_key) as usize; + } + // Bounds remove everything outside [0, 12997]; the bloom filter removes + // most other keys + assert!(false_positives < 300, "{false_positives} false positives"); } - /// A hash table holding `keys`, hashed as the build side hashes them. - #[cfg(not(feature = "force_hash_collisions"))] - fn hash_map_of(keys: &[i32]) -> PushdownStrategy { - use crate::joins::join_hash_map::JoinHashMapType; - let keys: ArrayRef = Arc::new(Int32Array::from(keys.to_vec())); - let mut hashes = vec![0; keys.len()]; - create_hashes([&keys], HASH_JOIN_SEED.random_state(), &mut hashes).unwrap(); - let mut map = JoinHashMapU32::with_capacity(keys.len()); - map.update_from_iter(Box::new(hashes.iter().enumerate()), 0); - let map = Arc::new(Map::HashMap(Box::new(map))); - PushdownStrategy::Map(Arc::new(ReservedMap::unreserved(map)), None) - } - - /// Splits `keys` across `num_partitions` build partitions as hash - /// repartitioning would, reporting each non-empty partition with the - /// strategy `strategy` picks for it. - #[cfg(not(feature = "force_hash_collisions"))] - fn routed_partitions( - acc: &SharedBuildAccumulator, - num_partitions: usize, - keys: &[i32], - strategy: impl Fn(usize, &[i32]) -> PushdownStrategy, - ) -> Vec { - let array: ArrayRef = Arc::new(Int32Array::from(keys.to_vec())); - let mut hashes = vec![0; keys.len()]; - create_hashes( - [&array], - acc.repartition_random_state.random_state(), - &mut hashes, - ) - .unwrap(); - (0..num_partitions) - .map(|partition| { - let held: Vec = keys - .iter() - .zip(&hashes) - .filter(|(_, hash)| *hash % num_partitions as u64 == partition as u64) - .map(|(key, _)| *key) - .collect(); - if held.is_empty() { - reported(PushdownStrategy::Empty, no_bounds()) - } else { - reported(strategy(partition, &held), no_bounds()) - } - }) - .collect() - } - - /// Partitions that chose an IN list and partitions that chose a hash table - /// merge into one lookup: a probe row passes exactly when - /// the partition its key routes to holds the key. - #[test] - #[cfg(not(feature = "force_hash_collisions"))] - fn partitioned_in_list_and_hash_map_partitions_merge() { - let acc = make_partitioned_expr_accumulator_for_test(4); - let build: Vec = (0..40).step_by(2).collect(); - - acc.build_filter(FinalizeInput::Partitioned(routed_partitions( - &acc, - 4, - &build, - |partition, keys| { - if partition % 2 == 0 { - hash_map_of(keys) - } else { - in_list(keys) - } - }, - ))) - .unwrap(); - - let probe: Vec = (0..40).collect(); - let expected: Vec = probe.iter().map(|key| key % 2 == 0).collect(); - assert_matches(&acc, &probe, &expected); - } - - /// IN list partitions whose combined list passes the IN list limits merge - /// into a hash lookup rather than one oversized IN list, which must still - /// accept every partition's keys. + /// Partitions that each fit an InList but together exceed the InList limit get + /// a bloom filter of all their keys. #[test] - #[cfg(not(feature = "force_hash_collisions"))] - fn partitioned_in_lists_past_limit_merge_into_hash_lookup() { - let mut acc = make_partitioned_expr_accumulator_for_test(3); - acc.in_list_max_values = 2; - let build: Vec = (1..=9).collect(); - - acc.build_filter(FinalizeInput::Partitioned(routed_partitions( - &acc, - 3, - &build, - |_, keys| in_list(keys), - ))) + fn partitioned_inlists_over_global_limit_use_bloom_filter() { + let mut acc = make_partitioned_expr_accumulator_for_test(2); + acc.global_filter_options.inlist_max_distinct_values = 3; + let in_list_partition = |keys: Vec| { + PartitionStatus::Reported(PartitionData { + pushdown: in_list(&keys), + bounds: bounds(*keys.iter().min().unwrap(), *keys.iter().max().unwrap()), + keys_have_null: false, + build_keys: test_build_keys(BuildKeys { + bloom_filter: None, + num_distinct: keys.len(), + keys: vec![Arc::new(Int32Array::from(keys))], + }), + }) + }; + acc.build_filter(FinalizeInput::Partitioned(vec![ + in_list_partition(vec![1, 3]), + in_list_partition(vec![5, 7]), + ])) .unwrap(); - let expr = current_expr(&acc); - assert!( - expr.downcast_ref::().is_none(), - "expected a hash lookup, got: {expr}" - ); - let probe: Vec = (0..20).collect(); - let expected: Vec = probe.iter().map(|key| (1..=9).contains(key)).collect(); - assert_matches(&acc, &probe, &expected); - } + assert!(format!("{expr}").ends_with("AND bloom_filter"), "{expr}"); + let result = eval_bool(&expr, (0..10).collect()); + for k in [1, 3, 5, 7] { + assert!(result[k], "build key {k} dropped"); + } + assert!(!result[0] && !result[8] && !result[9]); - /// Evaluates the current filter over probe keys `keys`. - #[cfg(not(feature = "force_hash_collisions"))] - fn assert_matches(acc: &SharedBuildAccumulator, keys: &[i32], expected: &[bool]) { - let probe = RecordBatch::try_new( - test_probe_schema(), - vec![Arc::new(Int32Array::from(keys.to_vec()))], - ) + // At the limit: one InList with the keys of both partitions + let mut acc = make_partitioned_expr_accumulator_for_test(2); + acc.global_filter_options.inlist_max_distinct_values = 4; + acc.build_filter(FinalizeInput::Partitioned(vec![ + in_list_partition(vec![1, 3]), + in_list_partition(vec![5, 7]), + ])) .unwrap(); - let matched = current_expr(acc) - .evaluate(&probe) - .unwrap() - .into_array(keys.len()) - .unwrap(); - assert_eq!(matched.as_boolean(), &BooleanArray::from(expected.to_vec())); + let expr = current_expr(&acc); + let and = binary_expr(&expr); + assert_in_list_column_values(and.right(), "probe_key", 0, &[1, 3, 5, 7]); } - /// An array map holding `keys`, as a dense build side gets. - fn array_map_of(keys: &[i32]) -> PushdownStrategy { - use crate::joins::array_map::ArrayMap; - - let bound = |key: Option<&i32>| { - ArrayMap::key_to_u64(&ScalarValue::Int32(key.copied())).unwrap() + /// A partition InList holds every build row; the global InList holds each + /// key once. + #[test] + fn global_inlist_has_distinct_keys() { + let acc = make_partitioned_expr_accumulator_for_test(2); + let partition = |rows: Vec, num_distinct: usize| { + PartitionStatus::Reported(PartitionData { + pushdown: in_list(&rows), + bounds: bounds(*rows.iter().min().unwrap(), *rows.iter().max().unwrap()), + keys_have_null: false, + build_keys: test_build_keys(BuildKeys { + bloom_filter: None, + num_distinct, + keys: vec![Arc::new(Int32Array::from(rows))], + }), + }) }; - let min = bound(keys.iter().min()); - let max = bound(keys.iter().max()); - let keys: ArrayRef = Arc::new(Int32Array::from(keys.to_vec())); - let map = ArrayMap::try_new(&keys, min, max).unwrap(); - let map = Arc::new(Map::ArrayMap(map)); - PushdownStrategy::Map(Arc::new(ReservedMap::unreserved(map)), None) + acc.build_filter(FinalizeInput::Partitioned(vec![ + partition(vec![1, 1, 3, 1, 3], 2), + partition(vec![5, 5, 5], 1), + ])) + .unwrap(); + let expr = current_expr(&acc); + let and = binary_expr(&expr); + assert_in_list_column_values(and.right(), "probe_key", 0, &[1, 3, 5]); } - /// Array map partitions merge into one lookup with hash table partitions. + /// A partition whose only key is NULL has NULL bounds. The merged bounds + /// ignore them, so the filter keeps the non-NULL keys of the other partitions. #[test] - #[cfg(not(feature = "force_hash_collisions"))] - fn partitioned_array_map_and_hash_map_partitions_merge() { - let acc = make_partitioned_expr_accumulator_for_test(4); - let build: Vec = (0..40).step_by(2).collect(); - - acc.build_filter(FinalizeInput::Partitioned(routed_partitions( - &acc, - 4, - &build, - |partition, keys| { - if partition % 2 == 0 { - array_map_of(keys) - } else { - hash_map_of(keys) - } - }, - ))) + fn merged_bounds_ignore_null_bounds() { + let null_bounds = PartitionBounds::new(vec![ColumnBounds::new( + ScalarValue::Int32(None), + ScalarValue::Int32(None), + )]); + let merged = PartitionBounds::merge( + [&null_bounds, &bounds(11, 11), &null_bounds].into_iter(), + ) .unwrap(); - - let probe: Vec = (0..40).collect(); - let expected: Vec = probe.iter().map(|key| key % 2 == 0).collect(); - assert_matches(&acc, &probe, &expected); + assert_eq!( + merged.get_column_bounds(0).unwrap(), + &ColumnBounds::new( + ScalarValue::Int32(Some(11)), + ScalarValue::Int32(Some(11)) + ) + ); + let merged = + PartitionBounds::merge([&bounds(5, 6), &null_bounds].into_iter()).unwrap(); + assert_eq!( + merged.get_column_bounds(0).unwrap(), + &ColumnBounds::new(ScalarValue::Int32(Some(5)), ScalarValue::Int32(Some(6))) + ); } - /// Array map partitions merge under Range routing too. + /// Hash and range partitioning both use the same global filter: it holds every + /// build key, so it does not depend on the routing. #[test] - #[cfg(not(feature = "force_hash_collisions"))] - fn partitioned_range_array_map_partitions_merge() -> Result<()> { - let mut acc = make_partitioned_expr_accumulator_for_test(4); + fn range_partitioned_uses_global_filter() -> Result<()> { + let mut acc = make_partitioned_expr_accumulator_for_test(2); acc.probe_range_partitioning = Some(RangePartitioning::try_new( [PhysicalSortExpr::new( Arc::clone(&acc.on_right[0]), Default::default(), )] .into(), - vec![ - SplitPoint::new(vec![ScalarValue::Int32(Some(10))]), - SplitPoint::new(vec![ScalarValue::Int32(Some(20))]), - SplitPoint::new(vec![ScalarValue::Int32(Some(30))]), - ], + vec![SplitPoint::new(vec![ScalarValue::Int32(Some(1000))])], )?); - acc.build_filter(FinalizeInput::Partitioned(vec![ - reported(array_map_of(&[7, 8, 9]), bounds(7, 9)), - reported(PushdownStrategy::Empty, no_bounds()), - reported(array_map_of(&[27, 28, 29]), bounds(27, 29)), - reported(array_map_of(&[37, 38, 39]), bounds(37, 39)), + reported_map((0..500).map(|k| k * 2).collect()), + reported_map((0..500).map(|k| 1000 + k * 2).collect()), ]))?; - - let probe: Vec = (0..50).collect(); - let expected: Vec = probe - .iter() - .map(|key| matches!(key, 7..=9 | 27..=29 | 37..=39)) - .collect(); - assert_matches(&acc, &probe, &expected); + let expr = current_expr(&acc); + assert!(!format!("{expr}").contains("CASE"), "{expr}"); + let result = eval_bool(&expr, (0..2000).collect()); + for k in (0..2000).step_by(2) { + assert!(result[k], "build key {k} dropped"); + } Ok(()) } - /// Partitions without a bitmap still leave the gaps between them prunable. + /// A canceled partition's keys are unknown, so the global filter is not + /// possible: the routed filter keeps every row of the canceled route. + /// A canceled partition contributes no key, so the other partitions still + /// get the global bloom filter. #[test] - fn disjoint_array_map_partitions_keep_gap_pruning() { + fn partitioned_canceled_partition_contributes_no_key() { let acc = make_partitioned_expr_accumulator_for_test(2); - let members = [ - (0, array_map_of(&[7, 8, 9]), bounds(7, 9)), - (1, array_map_of(&[37, 38, 39]), bounds(37, 39)), - ]; - - let bitmap = acc - .merged_pruning_bitmap(&members, Some(&bounds(7, 39))) - .expect("the gap between the partitions can be pruned"); - - assert!(bitmap.may_contain_range(7, 9)); - assert!(!bitmap.may_contain_range(10, 36)); + acc.build_filter(FinalizeInput::Partitioned(vec![ + reported_map((0..1000).collect()), + PartitionStatus::CanceledUnknown, + ])) + .unwrap(); + let expr = current_expr(&acc); + assert!(format!("{expr}").contains("bloom_filter"), "{expr}"); } + /// Float keys get the global bloom filter too. #[test] - fn partitioned_one_real_partition_with_rest_empty_keeps_its_in_list() { - let acc = make_partitioned_expr_accumulator_for_test(3); - + fn float_keys_use_global_bloom_filter() { + let mut acc = make_partitioned_expr_accumulator_for_test(2); + acc.probe_schema = Arc::new(Schema::new(vec![Field::new( + "probe_key", + DataType::Float64, + false, + )])); + let float_partition = |keys: Vec| { + PartitionStatus::Reported(PartitionData { + pushdown: PushdownStrategy::Map( + Arc::new(ReservedMap::unreserved(Arc::new(Map::HashMap(Box::new( + crate::joins::join_hash_map::JoinHashMapU32::with_capacity(0), + ))))), + None, + ), + bounds: no_bounds(), + keys_have_null: false, + build_keys: test_build_keys(BuildKeys { + bloom_filter: None, + num_distinct: keys.len(), + keys: vec![Arc::new(Float64Array::from(keys))], + }), + }) + }; acc.build_filter(FinalizeInput::Partitioned(vec![ - reported(PushdownStrategy::Empty, no_bounds()), - reported(in_list(&[2]), no_bounds()), - reported(PushdownStrategy::Empty, no_bounds()), + float_partition((0..1000).map(f64::from).collect()), + float_partition((1000..2000).map(f64::from).collect()), ])) .unwrap(); + let expr = current_expr(&acc); + assert_eq!(format!("{expr}"), "bloom_filter"); + } + /// With a memory pool too small for the bloom filter, the global filter + /// keeps every build key: it falls back to the bounds (and the pruning + /// bitmap), and the join results stay correct. + #[test] + fn small_memory_pool_falls_back_to_bounds() { + use datafusion_execution::memory_pool::GreedyMemoryPool; + let mut acc = make_partitioned_expr_accumulator_for_test(2); + let pool: Arc = Arc::new(GreedyMemoryPool::new(1024)); + acc.global_filter_options.memory_pool = Some(Arc::clone(&pool)); + acc.build_filter(FinalizeInput::Partitioned(vec![ + reported_map((0..1000).map(|k| k * 4).collect()), + reported_map((0..1000).map(|k| k * 4 + 1).collect()), + ])) + .unwrap(); let expr = current_expr(&acc); - in_list_expr(&expr); + let result = eval_bool(&expr, (-10..4010).collect()); + for (k, pass) in (-10..4010).zip(result) { + // Bounds only: every key in [0, 3997] passes, nothing outside + assert_eq!(pass, (0..=3997).contains(&k), "key {k}"); + } + assert_eq!(pool.reserved(), 0); } - /// One non-empty partition keeps its own filter: no routing, and an IN list - /// stays an IN list even when its duplicate rows exceed the merged list limit. + /// A partition without hashes (too big alone, or its memory reservation + /// failed) means no bloom filter: the filter keeps only the bounds and the + /// pruning bitmap. #[test] - fn partitioned_one_non_empty_partition_keeps_its_own_filter() { - let empty = || reported(PushdownStrategy::Empty, no_bounds()); - let mut acc = make_partitioned_expr_accumulator_for_test(3); - acc.in_list_max_values = 150; + fn partition_without_hashes_skips_bloom_filter() { + let acc = make_partitioned_expr_accumulator_for_test(2); + let mut no_hashes = reported_map((1_000_000..1_000_100).collect()); + if let PartitionStatus::Reported(data) = &mut no_hashes { + data.build_keys.bloom_filter = None; + } acc.build_filter(FinalizeInput::Partitioned(vec![ - empty(), - reported(in_list(&[2; 1000]), no_bounds()), - PartitionStatus::CanceledUnknown, + reported_map((0..100).collect()), + no_hashes, ])) .unwrap(); - in_list_expr(¤t_expr(&acc)); + let expr = current_expr(&acc); + assert_eq!( + expr.to_string(), + "probe_key@0 >= 0 AND probe_key@0 <= 1000099 AND key_range_pruning" + ); + } - let acc = make_partitioned_expr_accumulator_for_test(3); + /// The bloom filter carries one key-range bitmap for all partitions, so scans + /// can prune containers with it. + #[test] + fn global_bloom_filter_has_merged_pruning_bitmap() { + let acc = make_partitioned_expr_accumulator_for_test(2); acc.build_filter(FinalizeInput::Partitioned(vec![ - empty(), - reported(hash_map(), bounds(1, 7)), - PartitionStatus::CanceledUnknown, + reported_map((0..100).collect()), + reported_map((1_000_000..1_000_100).collect()), ])) .unwrap(); - let expr = format!("{:?}", current_expr(&acc)); - assert!(!expr.contains("routed by"), "{expr}"); + let expr = current_expr(&acc); + let bloom = binary_expr(&expr) + .right() + .downcast_ref::() + .expect("expected a bloom filter"); + let bitmap = bloom.pruning_bitmap().expect("expected a pruning bitmap"); + let may = |lo: i32, hi: i32| bitmap.may_contain_range(lo as u64, hi as u64); + assert!(may(50, 60)); + assert!(may(1_000_050, 1_000_060)); + assert!(!may(400_000, 600_000)); } - /// A canceled partition rejects the rows routed to it, and only those: the - /// live partitions keep their matches. - #[cfg(not(feature = "force_hash_collisions"))] #[test] - fn partitioned_canceled_partition_rejects_only_its_routed_rows() { - let acc = make_partitioned_expr_accumulator_for_test(4); - let keys: Vec = (0..40).collect(); - let mut partitions = - routed_partitions(&acc, 4, &keys, |_, held| hash_map_of(held)); - partitions[0] = PartitionStatus::CanceledUnknown; - acc.build_filter(FinalizeInput::Partitioned(partitions)) - .unwrap(); + fn collect_left_updates_with_membership_only() { + let acc = make_collect_left_accumulator_for_test(); - let array: ArrayRef = Arc::new(Int32Array::from(keys.clone())); - let mut hashes = vec![0; keys.len()]; - create_hashes( - [&array], - acc.repartition_random_state.random_state(), - &mut hashes, - ) + acc.build_filter(FinalizeInput::CollectLeft(reported( + in_list(&[1, 2, 3]), + no_bounds(), + ))) .unwrap(); - let expected: Vec = hashes.iter().map(|hash| *hash % 4 != 0).collect(); - assert!(expected.contains(&true) && expected.contains(&false)); - assert_matches(&acc, &keys, &expected); + + let expr = current_expr(&acc); + assert_in_list_column_values(&expr, "probe_key", 0, &[1, 2, 3]); } #[test] - fn partitioned_canceled_and_empty_partitions_reject_every_row() { - let acc = make_partitioned_expr_accumulator_for_test(2); + fn collect_left_updates_with_bounds_only() { + let acc = make_collect_left_accumulator_for_test(); - acc.build_filter(FinalizeInput::Partitioned(vec![ - PartitionStatus::CanceledUnknown, - reported(PushdownStrategy::Empty, no_bounds()), - ])) + acc.build_filter(FinalizeInput::CollectLeft(reported( + PushdownStrategy::Empty, + bounds(10, 20), + ))) .unwrap(); - assert_literal_bool(¤t_expr(&acc), false); + let expr = current_expr(&acc); + assert_top_binary_op(&expr, Operator::And); } - // The routed hash lookup matches on the hash alone, so forced collisions make - // every key match its partition's table. - #[cfg(not(feature = "force_hash_collisions"))] #[test] - fn partitioned_range_dynamic_filter_routes_with_range_expr() -> Result<()> { - let mut acc = make_partitioned_expr_accumulator_for_test(4); - acc.probe_range_partitioning = Some(RangePartitioning::try_new( - [PhysicalSortExpr::new( - Arc::clone(&acc.on_right[0]), - Default::default(), - )] - .into(), - vec![ - SplitPoint::new(vec![ScalarValue::Int32(Some(10))]), - SplitPoint::new(vec![ScalarValue::Int32(Some(20))]), - SplitPoint::new(vec![ScalarValue::Int32(Some(30))]), - ], - )?); + fn collect_left_empty_build_data_does_not_update_filter() { + let acc = make_collect_left_accumulator_for_test(); + let initial_generation = acc.dynamic_filter.snapshot_generation(); + + acc.build_filter(FinalizeInput::CollectLeft(reported( + PushdownStrategy::Empty, + no_bounds(), + ))) + .unwrap(); - // No combined IN list, so each probe row is routed to its partition. - acc.in_list_max_values = 0; + assert_eq!( + acc.dynamic_filter.snapshot_generation(), + initial_generation, + "empty CollectLeft input must not update with a no-op filter" + ); + let expr = current_expr(&acc); + assert_literal_bool(&expr, true); + } + + #[test] + fn partitioned_one_real_partition_with_rest_empty_keeps_its_in_list() { + let acc = make_partitioned_expr_accumulator_for_test(3); acc.build_filter(FinalizeInput::Partitioned(vec![ reported(PushdownStrategy::Empty, no_bounds()), - reported(in_list(&[10, 19]), no_bounds()), - reported(in_list(&[20, 29]), no_bounds()), - reported(in_list(&[30]), no_bounds()), - ]))?; + reported(in_list(&[2]), no_bounds()), + reported(PushdownStrategy::Empty, no_bounds()), + ])) + .unwrap(); let expr = current_expr(&acc); - assert!( - format!("{expr:?}").contains("routed by range over 4 partitions"), - "{expr:?}" - ); + in_list_expr(&expr); + } - let batch = RecordBatch::try_new( - test_probe_schema(), - vec![Arc::new(Int32Array::from(vec![ - 9, 10, 19, 20, 21, 29, 30, 31, - ]))], - )?; - let result = expr.evaluate(&batch)?.into_array(batch.num_rows())?; - let result = result - .as_any() - .downcast_ref::() - .expect("dynamic filter should evaluate to BooleanArray"); - assert_eq!( - result, - &BooleanArray::from(vec![false, true, true, true, false, true, true, false,]) - ); + #[test] + fn partitioned_canceled_and_empty_partitions_reject_every_row() { + let acc = make_partitioned_expr_accumulator_for_test(2); - Ok(()) + acc.build_filter(FinalizeInput::Partitioned(vec![ + PartitionStatus::CanceledUnknown, + reported(PushdownStrategy::Empty, no_bounds()), + ])) + .unwrap(); + + assert_literal_bool(¤t_expr(&acc), false); } // Regression guard for the build-report lifecycle fix: on `Drop`, a stream @@ -1626,6 +1649,7 @@ mod tests { pushdown: PushdownStrategy::Empty, bounds: PartitionBounds::new(vec![]), keys_have_null: false, + build_keys: BuildKeys::default(), }, ) .unwrap(); @@ -1688,8 +1712,7 @@ mod tests { scope: NotInScope::Uncorrelated, output: NotInOutput::Rows, }), - in_list_max_size: 128 * 1024, - in_list_max_values: 150, + global_filter_options: GlobalFilterOptions::default(), } } @@ -1713,6 +1736,7 @@ mod tests { pushdown, bounds, keys_have_null: true, + build_keys: BuildKeys::default(), }) } @@ -1737,11 +1761,10 @@ mod tests { acc } - /// The IS NULL disjunct must wrap the merged filter as a whole. A probe NULL - /// belongs to exactly one partition, so widening only the filter of the - /// partition that reported the NULL would still drop it everywhere else. + /// The IS NULL disjunct must wrap the global filter as a whole when any + /// partition reports a NULL key. #[test] - fn partitioned_null_keys_in_one_partition_widen_whole_routed_filter() { + fn partitioned_null_keys_in_one_partition_widen_whole_global_filter() { let acc = null_equal_partitioned_accumulator(2); acc.build_filter(FinalizeInput::Partitioned(vec![ @@ -1758,11 +1781,7 @@ mod tests { "expected the IS NULL disjunct first, got: {expr}" ); assert_in_list_column_values(widened.right(), "probe_key", 0, &[1, 2]); - assert_eq!( - format!("{expr}").matches("IS NULL").count(), - 1, - "the routed filter must be widened once, not per branch" - ); + assert_eq!(format!("{expr}").matches("IS NULL").count(), 1); } /// A canceled partition holds no key, so it cannot add a NULL key that @@ -1899,8 +1918,7 @@ mod tests { scope: NotInScope::Correlated { value_keys }, output: NotInOutput::Mark, }), - in_list_max_size: 128 * 1024, - in_list_max_values: 150, + global_filter_options: GlobalFilterOptions::default(), }; let bounds = PartitionBounds::new( diff --git a/datafusion/physical-plan/src/joins/hash_join/stream.rs b/datafusion/physical-plan/src/joins/hash_join/stream.rs index 390492fc00bd7..ba2f4ec3351e6 100644 --- a/datafusion/physical-plan/src/joins/hash_join/stream.rs +++ b/datafusion/physical-plan/src/joins/hash_join/stream.rs @@ -30,7 +30,7 @@ use crate::joins::PartitionMode; use crate::joins::hash_join::exec::JoinLeftData; use crate::joins::hash_join::not_in::NotInProbe; use crate::joins::hash_join::shared_bounds::{ - PartitionBounds, PartitionBuildData, SharedBuildAccumulator, + BuildKeys, PartitionBounds, PartitionBuildData, SharedBuildAccumulator, }; use crate::joins::join_hash_map::ProbeOffset; use crate::joins::join_hash_map::ProbeOffset::AtRow; @@ -677,17 +677,25 @@ impl HashJoinStream { .iter() .any(|array| array.logical_null_count() > 0); + let build_keys = BuildKeys { + keys: left_data.values().to_vec(), + bloom_filter: left_data.bloom_filter().cloned(), + num_distinct: left_data.map().num_of_distinct_key(), + }; + let build_data = match self.mode { PartitionMode::Partitioned => PartitionBuildData::Partitioned { partition_id: self.partition, pushdown, bounds, keys_have_null, + build_keys, }, PartitionMode::CollectLeft => PartitionBuildData::CollectLeft { pushdown, bounds, keys_have_null, + build_keys, }, PartitionMode::Auto => unreachable!( "PartitionMode::Auto should not be present at execution time. This is a bug in DataFusion, please report it!" @@ -1277,6 +1285,7 @@ mod tests { pushdown: PushdownStrategy::Empty, bounds: PartitionBounds::new(vec![]), keys_have_null: false, + build_keys: Default::default(), } } diff --git a/datafusion/physical-plan/src/joins/mod.rs b/datafusion/physical-plan/src/joins/mod.rs index c4549d847f554..64aec6b8b16ea 100644 --- a/datafusion/physical-plan/src/joins/mod.rs +++ b/datafusion/physical-plan/src/joins/mod.rs @@ -21,7 +21,7 @@ use datafusion_physical_expr::PhysicalExprRef; /// /// [API policy]: https://datafusion.apache.org/contributor-guide/api-health.html#internal-public-apis #[doc(hidden)] -pub use hash_join::HashTableLookupExpr; +pub use hash_join::{BloomFilterExpr, HashTableLookupExpr}; pub use hash_join::{ HashExpr, HashJoinExec, HashJoinExecBuilder, PreparedHashJoinBuild, SeededRandomState, }; diff --git a/datafusion/pruning/src/pruning_predicate.rs b/datafusion/pruning/src/pruning_predicate.rs index b7325d90d5672..82de276873c11 100644 --- a/datafusion/pruning/src/pruning_predicate.rs +++ b/datafusion/pruning/src/pruning_predicate.rs @@ -38,7 +38,8 @@ use datafusion_expr_common::operator::Operator; use datafusion_physical_expr::utils::{Guarantee, LiteralGuarantee}; use datafusion_physical_expr::{PhysicalExprRef, expressions as phys_expr}; use datafusion_physical_expr_common::physical_expr::snapshot_physical_expr_opt; -use datafusion_physical_plan::joins::HashTableLookupExpr; +use datafusion_physical_plan::joins::key_range_bitmap::KeyRangeBitmap; +use datafusion_physical_plan::joins::{BloomFilterExpr, HashTableLookupExpr}; use datafusion_physical_plan::{ColumnarValue, PhysicalExpr}; /// Used to prove that arbitrary predicates (boolean expression) can not @@ -1530,18 +1531,19 @@ fn min_max_non_null_exprs( statistics } -/// Builds a "may match" expression for a [`HashTableLookupExpr`] (a large join -/// build side pushed down as an opaque hash-table lookup): tests container +/// Builds a "may match" expression for a [`HashTableLookupExpr`] or a +/// [`BloomFilterExpr`] (a large join build side pushed down as an opaque +/// membership test) with key-range bitmap `bitmap`: tests container /// min/max stats against the build side's key-range bitmap, which is fixed size /// and built once with the join's hash table. fn build_hash_lookup_pruning_expr( - lookup: &HashTableLookupExpr, + bitmap: &Arc, + on_columns: &[&Arc], schema: &Schema, required_columns: &mut RequiredColumns, ) -> Option> { - let bitmap = Arc::clone(lookup.pruning_bitmap()?); - let on_columns = lookup.children(); - let [column_expr] = on_columns[..] else { + let bitmap = Arc::clone(bitmap); + let [column_expr] = on_columns else { return None; }; // The schema adapter often wraps the column in a same-type or widening cast @@ -1849,7 +1851,29 @@ fn build_predicate_expression( } } if let Some(lookup) = expr.downcast_ref::() { - return build_hash_lookup_pruning_expr(lookup, schema, required_columns) + return lookup + .pruning_bitmap() + .and_then(|bitmap| { + build_hash_lookup_pruning_expr( + bitmap, + &lookup.children(), + schema, + required_columns, + ) + }) + .unwrap_or_else(|| unhandled_hook.handle(expr)); + } + if let Some(bloom) = expr.downcast_ref::() { + return bloom + .pruning_bitmap() + .and_then(|bitmap| { + build_hash_lookup_pruning_expr( + bitmap, + &bloom.children(), + schema, + required_columns, + ) + }) .unwrap_or_else(|| unhandled_hook.handle(expr)); } // A partitioned hash join hides its per-partition filters under a `CASE` on the diff --git a/datafusion/sqllogictest/test_files/dynamic_row_group_pruning.slt b/datafusion/sqllogictest/test_files/dynamic_row_group_pruning.slt index 208914325ea48..7d9bc58bb49d0 100644 --- a/datafusion/sqllogictest/test_files/dynamic_row_group_pruning.slt +++ b/datafusion/sqllogictest/test_files/dynamic_row_group_pruning.slt @@ -327,7 +327,7 @@ explain analyze select rgsel.b from dim join rgsel on dim.column1 = rgsel.b; Plan with Metrics 01)HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(column1@0, b@0)], projection=[b@1], metrics=[output_rows=3, elapsed_compute=, output_bytes=, output_batches=1, build_mem_used=, array_map_created_count=0, build_input_batches=1, build_input_rows=3, input_batches=2, input_rows=3, build_time=, join_time=, avg_fanout=100% (3/3), output_rows_skew=0%, probe_hit_rate=100% (3/3)] 02)--DataSourceExec: partitions=1, partition_sizes=[1], metrics=[] -03)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/dynamic_row_group_pruning/rgsel.parquet]]}, projection=[b], file_type=parquet, predicate=DynamicFilter [ b@1 >= 1000 AND b@1 <= 3050 AND hash_lookup ], dynamic_rg_pruning=eligible, pruning_predicate=b_null_count@1 != row_count@2 AND b_max@0 >= 1000 AND b_null_count@1 != row_count@2 AND b_min@3 <= 3050 AND b_null_count@1 != row_count@2 AND KEY_RANGE_BITMAP(b_min@3, b_max@0, 3/4096 buckets), required_guarantees=[], metrics=[output_rows=3, elapsed_compute=, output_bytes=, output_batches=2, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=4 total → 2 matched, page_index_pages_pruned=20 total → 3 matched, page_index_rows_pruned=200 total → 30 matched, limit_pruned_row_groups=0 total → 0 matched, batches_split=0, bytes_processed=, bytes_scanned=, file_open_errors=0, file_scan_errors=0, files_opened=1, files_processed=1, num_predicate_creation_errors=0, predicate_evaluation_errors=0, pushdown_rows_matched=3, pushdown_rows_pruned=27, row_groups_pruned_dynamic_filter=0, predicate_cache_inner_records=200, predicate_cache_records=3, bloom_filter_eval_time=, metadata_load_time=, page_index_eval_time=, row_pushdown_eval_time=, statistics_eval_time=, time_elapsed_opening=, time_elapsed_processing=, time_elapsed_scanning_total=, time_elapsed_scanning_until_data=, output_rows_skew=, scan_efficiency_ratio=] +03)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/dynamic_row_group_pruning/rgsel.parquet]]}, projection=[b], file_type=parquet, predicate=DynamicFilter [ b@1 >= 1000 AND b@1 <= 3050 AND bloom_filter ], dynamic_rg_pruning=eligible, pruning_predicate=b_null_count@1 != row_count@2 AND b_max@0 >= 1000 AND b_null_count@1 != row_count@2 AND b_min@3 <= 3050 AND b_null_count@1 != row_count@2 AND KEY_RANGE_BITMAP(b_min@3, b_max@0, 3/4096 buckets), required_guarantees=[], metrics=[output_rows=3, elapsed_compute=, output_bytes=, output_batches=2, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=4 total → 2 matched, page_index_pages_pruned=20 total → 3 matched, page_index_rows_pruned=200 total → 30 matched, limit_pruned_row_groups=0 total → 0 matched, batches_split=0, bytes_processed=, bytes_scanned=, file_open_errors=0, file_scan_errors=0, files_opened=1, files_processed=1, num_predicate_creation_errors=0, predicate_evaluation_errors=0, pushdown_rows_matched=3, pushdown_rows_pruned=27, row_groups_pruned_dynamic_filter=0, predicate_cache_inner_records=200, predicate_cache_records=3, bloom_filter_eval_time=, metadata_load_time=, page_index_eval_time=, row_pushdown_eval_time=, statistics_eval_time=, time_elapsed_opening=, time_elapsed_processing=, time_elapsed_scanning_total=, time_elapsed_scanning_until_data=, output_rows_skew=, scan_efficiency_ratio=] statement ok RESET datafusion.optimizer.hash_join_inlist_pushdown_max_distinct_values; @@ -403,5 +403,65 @@ set datafusion.execution.target_partitions = 4; statement ok RESET datafusion.explain.analyze_level; +# Partitioned hash join with filter pushdown off. The dynamic filter is one +# global filter (no `CASE hash_repartition % N` routing). The probe scan uses it +# only for statistics pruning, so the bloom filter is never built, but the +# key-range bitmap merged from all build partitions prunes row groups 1 and 2, +# which the [5, 390] bounds alone cannot prune. +statement ok +set datafusion.execution.parquet.pushdown_filters = false; + +statement ok +set datafusion.optimizer.hash_join_single_partition_threshold = 0; + +statement ok +set datafusion.optimizer.hash_join_single_partition_threshold_rows = 0; + +statement ok +set datafusion.optimizer.hash_join_inlist_pushdown_max_distinct_values = 0; + +statement ok +COPY (SELECT CAST(i AS BIGINT) AS k FROM generate_series(0, 399) AS t(i)) +TO 'test_files/scratch/dynamic_row_group_pruning/pdoff_probe.parquet' +STORED AS PARQUET +OPTIONS ('format.max_row_group_size' '100'); + +statement ok +CREATE EXTERNAL TABLE pdoff_probe STORED AS PARQUET +LOCATION 'test_files/scratch/dynamic_row_group_pruning/pdoff_probe.parquet'; + +statement ok +CREATE TABLE pdoff_build AS SELECT CAST(column1 AS BIGINT) AS k FROM (VALUES (5), (17), (390)); + +query TT +explain analyze select pdoff_probe.k from pdoff_build join pdoff_probe on pdoff_build.k = pdoff_probe.k; +---- +Plan with Metrics +01)HashJoinExec: mode=Partitioned, join_type=Inner, on=[(k@0, k@0)], projection=[k@1], metrics=[output_rows=3, elapsed_compute=, output_bytes=, output_batches=1, build_mem_used=, array_map_created_count=1, build_input_batches=1, build_input_rows=3, input_batches=2, input_rows=200, build_time=, join_time=, avg_fanout=100% (3/3), output_rows_skew=, probe_hit_rate=1.5% (3/200)] +02)--DataSourceExec: partitions=1, partition_sizes=[1], metrics=[] +03)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/dynamic_row_group_pruning/pdoff_probe.parquet]]}, projection=[k], file_type=parquet, predicate=DynamicFilter [ k@0 >= 5 AND k@0 <= 390 AND bloom_filter ], dynamic_rg_pruning=eligible, pruning_predicate=k_null_count@1 != row_count@2 AND k_max@0 >= 5 AND k_null_count@1 != row_count@2 AND k_min@3 <= 390 AND k_null_count@1 != row_count@2 AND KEY_RANGE_BITMAP(k_min@3, k_max@0, 3/512 buckets), required_guarantees=[], metrics=[output_rows=200, elapsed_compute=, output_bytes=, output_batches=2, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=4 total → 2 matched, page_index_pages_pruned=2 total → 2 matched, page_index_rows_pruned=200 total → 200 matched, limit_pruned_row_groups=0 total → 0 matched, batches_split=0, bytes_processed=, bytes_scanned=, file_open_errors=0, file_scan_errors=0, files_opened=1, files_processed=1, num_predicate_creation_errors=0, predicate_evaluation_errors=0, pushdown_rows_matched=0, pushdown_rows_pruned=0, row_groups_pruned_dynamic_filter=0, predicate_cache_inner_records=0, predicate_cache_records=0, bloom_filter_eval_time=, metadata_load_time=, page_index_eval_time=, row_pushdown_eval_time=, statistics_eval_time=, time_elapsed_opening=, time_elapsed_processing=, time_elapsed_scanning_total=, time_elapsed_scanning_until_data=, output_rows_skew=, scan_efficiency_ratio=] + +query I rowsort +select pdoff_probe.k from pdoff_build join pdoff_probe on pdoff_build.k = pdoff_probe.k; +---- +17 +390 +5 + +statement ok +drop table pdoff_build; + +statement ok +drop table pdoff_probe; + +statement ok +RESET datafusion.optimizer.hash_join_inlist_pushdown_max_distinct_values; + +statement ok +RESET datafusion.optimizer.hash_join_single_partition_threshold; + +statement ok +RESET datafusion.optimizer.hash_join_single_partition_threshold_rows; + statement ok RESET datafusion.execution.parquet.pushdown_filters; diff --git a/datafusion/sqllogictest/test_files/information_schema.slt b/datafusion/sqllogictest/test_files/information_schema.slt index e761260a82c5a..4cce77659cefd 100644 --- a/datafusion/sqllogictest/test_files/information_schema.slt +++ b/datafusion/sqllogictest/test_files/information_schema.slt @@ -323,6 +323,10 @@ datafusion.optimizer.enable_window_limits true datafusion.optimizer.enable_window_topn true datafusion.optimizer.expand_views_at_output false datafusion.optimizer.filter_null_join_keys false +datafusion.optimizer.hash_join_bloom_filter_fpp 0.01 +datafusion.optimizer.hash_join_bloom_filter_max_bytes 8388608 +datafusion.optimizer.hash_join_bloom_filter_max_fpp 0.15 +datafusion.optimizer.hash_join_dynamic_filter_inlist_max_distinct_values 20 datafusion.optimizer.hash_join_inlist_pushdown_max_distinct_values 150 datafusion.optimizer.hash_join_inlist_pushdown_max_size 131072 datafusion.optimizer.hash_join_single_partition_threshold 4194304 @@ -494,6 +498,10 @@ datafusion.optimizer.enable_window_limits true When set to true, the optimizer w datafusion.optimizer.enable_window_topn true When set to true, the optimizer will replace Filter(rn<=K) → Window(ROW_NUMBER, RANK or DENSE_RANK) → Sort patterns with a PartitionedTopKExec that keeps only the top K rows of each window partition, avoiding a full sort of the input. datafusion.optimizer.expand_views_at_output false When set to true, if the returned type is a view type then the output will be coerced to a non-view. Coerces `Utf8View` to `LargeUtf8`, and `BinaryView` to `LargeBinary`. datafusion.optimizer.filter_null_join_keys false When set to true, the optimizer will insert filters before a join between a nullable and non-nullable column to filter out nulls on the nullable side. This filter can add additional overhead when the file format does not fully support predicate push down. +datafusion.optimizer.hash_join_bloom_filter_fpp 0.01 Target false positive probability of the bloom filter in the dynamic filter of a hash join. When the build side of a hash join has more distinct keys than `hash_join_inlist_pushdown_max_distinct_values` (over all partitions), the dynamic filter tests probe rows against one bloom filter of all build side keys. Each build partition fills a filter of its keys, sized for the estimated number of distinct keys of all partitions at this probability. Then one task ORs the partition filters and folds the result to this probability. +datafusion.optimizer.hash_join_bloom_filter_max_bytes 8388608 Maximum size, in bytes, of the bloom filter in the dynamic filter of a hash join (and of the filter of each build partition). A build side that needs a larger filter at `hash_join_bloom_filter_fpp` gets no bloom filter: such a large build side is rarely selective, so the filter is too big to be useful. The dynamic filter then tests only the min/max bounds (and keeps key range pruning). +datafusion.optimizer.hash_join_bloom_filter_max_fpp 0.15 The bloom filter of a hash join dynamic filter is not used when its estimated false positive probability is above this value (the filter is saturated, a safety net). Then the dynamic filter tests only the min/max bounds of the build keys, because a saturated filter removes few rows and costs CPU time for each probe row. +datafusion.optimizer.hash_join_dynamic_filter_inlist_max_distinct_values 20 Maximum number of distinct build side keys, over all partitions of a hash join, for the global dynamic filter to test membership with one InList of all keys. Above this limit the dynamic filter uses a bloom filter (see `hash_join_bloom_filter_fpp`). Every partition must also use an InList by itself (`hash_join_inlist_pushdown_max_distinct_values` and `hash_join_inlist_pushdown_max_size`). For larger sets, a bloom filter is cheaper per probe row than an InList. datafusion.optimizer.hash_join_inlist_pushdown_max_distinct_values 150 Maximum number of distinct values (rows) in the build side of a hash join to be pushed down as an InList expression for dynamic filtering. Build sides with more rows than this will use hash table lookups instead. Set to 0 to always use hash table lookups. This provides an additional limit beyond `hash_join_inlist_pushdown_max_size` to prevent very large IN lists that might not provide much benefit over hash table lookups. This uses the deduplicated row count once the build side has been evaluated. The default is 150 values per partition. This is inspired by Trino's `max-filter-keys-per-column` setting. See: datafusion.optimizer.hash_join_inlist_pushdown_max_size 131072 Maximum size in bytes for the build side of a hash join to be pushed down as an InList expression for dynamic filtering. Build sides larger than this will use hash table lookups instead. Set to 0 to always use hash table lookups. InList pushdown can be more efficient for small build sides because it can result in better statistics pruning as well as use any bloom filters present on the scan side. InList expressions are also more transparent and easier to serialize over the network in distributed uses of DataFusion. On the other hand InList pushdown requires making a copy of the data and thus adds some overhead to the build side and uses more memory. This setting is per-partition, so we may end up using `hash_join_inlist_pushdown_max_size` * `target_partitions` memory. The default is 128kB per partition. This should allow point lookup joins (e.g. joining on a unique primary key) to use InList pushdown in most cases but avoids excessive memory usage or overhead for larger joins. datafusion.optimizer.hash_join_single_partition_threshold 4194304 The maximum estimated size in bytes for one input side of a HashJoin will be collected into a single partition. Partitioned joins also use it: an input below it is not used as the co-partitioning reference for an input above it. diff --git a/datafusion/sqllogictest/test_files/push_down_filter_parquet.slt b/datafusion/sqllogictest/test_files/push_down_filter_parquet.slt index bbf72983ce2e0..2a78b2b049dd5 100644 --- a/datafusion/sqllogictest/test_files/push_down_filter_parquet.slt +++ b/datafusion/sqllogictest/test_files/push_down_filter_parquet.slt @@ -478,7 +478,7 @@ Plan with Metrics 02)--HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(a@0, b@0)], metrics=[output_rows=2, output_batches=1, array_map_created_count=0, build_input_batches=1, build_input_rows=2, input_batches=1, input_rows=2, avg_fanout=100% (2/2), probe_hit_rate=100% (2/2)] 03)----DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/push_down_filter_parquet/nested_t1.parquet]]}, projection=[a, x], file_type=parquet, metrics=[output_rows=2, output_batches=1, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=1 total → 1 matched, page_index_pages_pruned=0 total → 0 matched, page_index_rows_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, batches_split=0, file_open_errors=0, file_scan_errors=0, files_opened=1, files_processed=1, num_predicate_creation_errors=0, predicate_evaluation_errors=0, pushdown_rows_matched=0, pushdown_rows_pruned=0, predicate_cache_inner_records=0, predicate_cache_records=0, scan_efficiency_ratio=18.13% (132/728)] 04)----DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/push_down_filter_parquet/nested_t2.parquet]]}, projection=[b, c, y], file_type=parquet, predicate=DynamicFilter [ b@0 >= aa AND b@0 <= ab AND b@0 IN (SET) ([aa, ab]) ], dynamic_rg_pruning=eligible, pruning_predicate=b_null_count@1 != row_count@2 AND b_max@0 >= aa AND b_null_count@1 != row_count@2 AND b_min@3 <= ab AND (b_null_count@1 != row_count@2 AND b_min@3 <= aa AND aa <= b_max@0 OR b_null_count@1 != row_count@2 AND b_min@3 <= ab AND ab <= b_max@0), required_guarantees=[b in (aa, ab)], metrics=[output_rows=2, output_batches=1, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=1 total → 1 matched, page_index_pages_pruned=1 total → 1 matched, page_index_rows_pruned=5 total → 5 matched, limit_pruned_row_groups=0 total → 0 matched, batches_split=0, file_open_errors=0, file_scan_errors=0, files_opened=1, files_processed=1, num_predicate_creation_errors=0, predicate_evaluation_errors=0, pushdown_rows_matched=2, pushdown_rows_pruned=3, predicate_cache_inner_records=5, predicate_cache_records=2, scan_efficiency_ratio=23.19% (234/1.01 K)] -05)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/push_down_filter_parquet/nested_t3.parquet]]}, projection=[d, z], file_type=parquet, predicate=DynamicFilter [ d@0 >= ca AND d@0 <= cb AND hash_lookup ], dynamic_rg_pruning=eligible, pruning_predicate=d_null_count@1 != row_count@2 AND d_max@0 >= ca AND d_null_count@1 != row_count@2 AND d_min@3 <= cb, required_guarantees=[], metrics=[output_rows=2, output_batches=1, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=1 total → 1 matched, page_index_pages_pruned=1 total → 1 matched, page_index_rows_pruned=8 total → 8 matched, limit_pruned_row_groups=0 total → 0 matched, batches_split=0, file_open_errors=0, file_scan_errors=0, files_opened=1, files_processed=1, num_predicate_creation_errors=0, predicate_evaluation_errors=0, pushdown_rows_matched=2, pushdown_rows_pruned=6, predicate_cache_inner_records=8, predicate_cache_records=2, scan_efficiency_ratio=22.34% (172/770)] +05)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/push_down_filter_parquet/nested_t3.parquet]]}, projection=[d, z], file_type=parquet, predicate=DynamicFilter [ d@0 >= ca AND d@0 <= cb AND bloom_filter ], dynamic_rg_pruning=eligible, pruning_predicate=d_null_count@1 != row_count@2 AND d_max@0 >= ca AND d_null_count@1 != row_count@2 AND d_min@3 <= cb, required_guarantees=[], metrics=[output_rows=2, output_batches=1, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=1 total → 1 matched, page_index_pages_pruned=1 total → 1 matched, page_index_rows_pruned=8 total → 8 matched, limit_pruned_row_groups=0 total → 0 matched, batches_split=0, file_open_errors=0, file_scan_errors=0, files_opened=1, files_processed=1, num_predicate_creation_errors=0, predicate_evaluation_errors=0, pushdown_rows_matched=2, pushdown_rows_pruned=6, predicate_cache_inner_records=8, predicate_cache_records=2, scan_efficiency_ratio=22.34% (172/770)] statement ok reset datafusion.explain.analyze_categories; @@ -910,7 +910,7 @@ drop table lj_probe; # test_hashjoin_hash_table_pushdown_integer_keys # # With `hash_join_inlist_pushdown_max_size = 1`, the HashJoin dynamic filter -# should use the HashTable strategy (`hash_lookup`) instead of the InList +# should use the bloom filter strategy (`bloom_filter`) instead of the InList # strategy (`IN (SET) ([...])`), even when the build side is tiny. ######## @@ -952,7 +952,7 @@ LOCATION 'test_files/scratch/push_down_filter_parquet/hl_probe.parquet'; statement ok set datafusion.explain.analyze_categories = 'rows'; -# probe scan filter should contain `hash_lookup`, not `IN (SET)`. +# probe scan filter should contain `bloom_filter`, not `IN (SET)`. query TT EXPLAIN ANALYZE SELECT p.a, p.b, build.c, p.e @@ -962,7 +962,7 @@ FROM hl_probe p INNER JOIN hl_build AS build Plan with Metrics 01)HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(a@0, a@0), (b@1, b@1)], projection=[a@3, b@4, c@2, e@5], metrics=[output_rows=2, output_batches=1, array_map_created_count=0, build_input_batches=1, build_input_rows=2, input_batches=1, input_rows=2, avg_fanout=100% (2/2), probe_hit_rate=100% (2/2)] 02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/push_down_filter_parquet/hl_build.parquet]]}, projection=[a, b, c], file_type=parquet, metrics=[output_rows=2, output_batches=1, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=1 total → 1 matched, page_index_pages_pruned=0 total → 0 matched, page_index_rows_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, batches_split=0, file_open_errors=0, file_scan_errors=0, files_opened=1, files_processed=1, num_predicate_creation_errors=0, predicate_evaluation_errors=0, pushdown_rows_matched=0, pushdown_rows_pruned=0, predicate_cache_inner_records=0, predicate_cache_records=0, scan_efficiency_ratio=20.25% (196/968)] -03)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/push_down_filter_parquet/hl_probe.parquet]]}, projection=[a, b, e], file_type=parquet, predicate=DynamicFilter [ a@0 >= aa AND a@0 <= ab AND b@1 >= ba AND b@1 <= bb AND hash_lookup ], dynamic_rg_pruning=eligible, pruning_predicate=a_null_count@1 != row_count@2 AND a_max@0 >= aa AND a_null_count@1 != row_count@2 AND a_min@3 <= ab AND b_null_count@5 != row_count@2 AND b_max@4 >= ba AND b_null_count@5 != row_count@2 AND b_min@6 <= bb, required_guarantees=[], metrics=[output_rows=2, output_batches=1, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=1 total → 1 matched, page_index_pages_pruned=0 total → 0 matched, page_index_rows_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, batches_split=0, file_open_errors=0, file_scan_errors=0, files_opened=1, files_processed=1, num_predicate_creation_errors=0, predicate_evaluation_errors=0, pushdown_rows_matched=2, pushdown_rows_pruned=2, predicate_cache_inner_records=8, predicate_cache_records=4, scan_efficiency_ratio=22.78% (228/1.00 K)] +03)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/push_down_filter_parquet/hl_probe.parquet]]}, projection=[a, b, e], file_type=parquet, predicate=DynamicFilter [ a@0 >= aa AND a@0 <= ab AND b@1 >= ba AND b@1 <= bb AND bloom_filter ], dynamic_rg_pruning=eligible, pruning_predicate=a_null_count@1 != row_count@2 AND a_max@0 >= aa AND a_null_count@1 != row_count@2 AND a_min@3 <= ab AND b_null_count@5 != row_count@2 AND b_max@4 >= ba AND b_null_count@5 != row_count@2 AND b_min@6 <= bb, required_guarantees=[], metrics=[output_rows=2, output_batches=1, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=1 total → 1 matched, page_index_pages_pruned=0 total → 0 matched, page_index_rows_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, batches_split=0, file_open_errors=0, file_scan_errors=0, files_opened=1, files_processed=1, num_predicate_creation_errors=0, predicate_evaluation_errors=0, pushdown_rows_matched=2, pushdown_rows_pruned=2, predicate_cache_inner_records=8, predicate_cache_records=4, scan_efficiency_ratio=22.78% (228/1.00 K)] statement ok drop table hl_build; @@ -1011,7 +1011,7 @@ FROM int_build b INNER JOIN int_probe p Plan with Metrics 01)HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(id1@0, id1@0), (id2@1, id2@1)], projection=[id1@0, id2@1, value@2, data@5], metrics=[output_rows=2, output_batches=1, array_map_created_count=0, build_input_batches=1, build_input_rows=2, input_batches=1, input_rows=2, avg_fanout=100% (2/2), probe_hit_rate=100% (2/2)] 02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/push_down_filter_parquet/int_build.parquet]]}, projection=[id1, id2, value], file_type=parquet, metrics=[output_rows=2, output_batches=1, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=1 total → 1 matched, page_index_pages_pruned=0 total → 0 matched, page_index_rows_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, batches_split=0, file_open_errors=0, file_scan_errors=0, files_opened=1, files_processed=1, num_predicate_creation_errors=0, predicate_evaluation_errors=0, pushdown_rows_matched=0, pushdown_rows_pruned=0, predicate_cache_inner_records=0, predicate_cache_records=0, scan_efficiency_ratio=18.78% (204/1.09 K)] -03)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/push_down_filter_parquet/int_probe.parquet]]}, projection=[id1, id2, data], file_type=parquet, predicate=DynamicFilter [ id1@0 >= 1 AND id1@0 <= 2 AND id2@1 >= 10 AND id2@1 <= 20 AND hash_lookup ], dynamic_rg_pruning=eligible, pruning_predicate=id1_null_count@1 != row_count@2 AND id1_max@0 >= 1 AND id1_null_count@1 != row_count@2 AND id1_min@3 <= 2 AND id2_null_count@5 != row_count@2 AND id2_max@4 >= 10 AND id2_null_count@5 != row_count@2 AND id2_min@6 <= 20, required_guarantees=[], metrics=[output_rows=2, output_batches=1, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=1 total → 1 matched, page_index_pages_pruned=0 total → 0 matched, page_index_rows_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, batches_split=0, file_open_errors=0, file_scan_errors=0, files_opened=1, files_processed=1, num_predicate_creation_errors=0, predicate_evaluation_errors=0, pushdown_rows_matched=2, pushdown_rows_pruned=2, predicate_cache_inner_records=8, predicate_cache_records=4, scan_efficiency_ratio=21.03% (221/1.05 K)] +03)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/push_down_filter_parquet/int_probe.parquet]]}, projection=[id1, id2, data], file_type=parquet, predicate=DynamicFilter [ id1@0 >= 1 AND id1@0 <= 2 AND id2@1 >= 10 AND id2@1 <= 20 AND bloom_filter ], dynamic_rg_pruning=eligible, pruning_predicate=id1_null_count@1 != row_count@2 AND id1_max@0 >= 1 AND id1_null_count@1 != row_count@2 AND id1_min@3 <= 2 AND id2_null_count@5 != row_count@2 AND id2_max@4 >= 10 AND id2_null_count@5 != row_count@2 AND id2_min@6 <= 20, required_guarantees=[], metrics=[output_rows=2, output_batches=1, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=1 total → 1 matched, page_index_pages_pruned=0 total → 0 matched, page_index_rows_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, batches_split=0, file_open_errors=0, file_scan_errors=0, files_opened=1, files_processed=1, num_predicate_creation_errors=0, predicate_evaluation_errors=0, pushdown_rows_matched=2, pushdown_rows_pruned=2, predicate_cache_inner_records=8, predicate_cache_records=4, scan_efficiency_ratio=21.03% (221/1.05 K)] statement ok reset datafusion.explain.analyze_categories; @@ -1200,8 +1200,8 @@ statement ok drop table nnp_probe; -# Partitioned mode: the per-partition CASE filter gets the same IS NULL widening, so a -# probe NULL routed to a pruning branch still reaches the join. +# Partitioned mode: the global filter gets the same IS NULL widening, so a +# probe NULL still reaches the join. statement ok set datafusion.execution.target_partitions = 4; diff --git a/docs/source/user-guide/configs.md b/docs/source/user-guide/configs.md index 47c196b21b250..f22ce39eb10ef 100644 --- a/docs/source/user-guide/configs.md +++ b/docs/source/user-guide/configs.md @@ -50,166 +50,170 @@ SET datafusion.execution.target_partitions = '1'; The following configuration settings are available: -| key | default | description | -| ----------------------------------------------------------------------- | ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| datafusion.catalog.create_default_catalog_and_schema | true | Whether the default catalog and schema should be created automatically. | -| datafusion.catalog.default_catalog | datafusion | The default catalog name - this impacts what SQL queries use if not specified | -| datafusion.catalog.default_schema | public | The default schema name - this impacts what SQL queries use if not specified | -| datafusion.catalog.information_schema | false | Should DataFusion provide access to `information_schema` virtual tables for displaying schema information | -| datafusion.catalog.partition_information_schema | false | When set together with `information_schema`, the `information_schema` of each catalog only describes the objects in that catalog. When not set, the `information_schema` of each catalog describes all catalogs. | -| datafusion.catalog.location | NULL | Location scanned to load tables for `default` schema | -| datafusion.catalog.format | NULL | Type of `TableProvider` to use when loading `default` schema | -| datafusion.catalog.has_header | true | Default value for `format.has_header` for `CREATE EXTERNAL TABLE` if not specified explicitly in the statement. | -| datafusion.catalog.newlines_in_values | false | Specifies whether newlines in (quoted) CSV values are supported. This is the default value for `format.newlines_in_values` for `CREATE EXTERNAL TABLE` if not specified explicitly in the statement. Parsing newlines in quoted values may be affected by execution behaviour such as parallel file scanning. Setting this to `true` ensures that newlines in values are parsed successfully, which may reduce performance. | -| datafusion.execution.batch_size | 8192 | Default batch size while creating new batches, it's especially useful for buffer-in-memory batches since creating tiny batches would result in too much metadata memory consumption | -| datafusion.execution.perfect_hash_join_small_build_threshold | 1024 | A perfect hash join (see `HashJoinExec` for more details) will be considered if the range of keys (max - min) on the build side is < this threshold. This provides a fast path for joins with very small key ranges, bypassing the density check. Currently only supports cases where build_side.num_rows() < u32::MAX. Support for build_side.num_rows() >= u32::MAX will be added in the future. | -| datafusion.execution.perfect_hash_join_min_key_density | 0.15 | The minimum required density of join keys on the build side to consider a perfect hash join (see `HashJoinExec` for more details). Density is calculated as: `(number of rows) / (max_key - min_key + 1)`. A perfect hash join may be used if the actual key density > this value. Currently only supports cases where build_side.num_rows() < u32::MAX. Support for build_side.num_rows() >= u32::MAX will be added in the future. | -| datafusion.execution.coalesce_batches | true | When set to true, record batches will be examined between each operator and small batches will be coalesced into larger batches. This is helpful when there are highly selective filters or joins that could produce tiny output batches. The target batch size is determined by the configuration setting | -| datafusion.execution.collect_statistics | true | Should DataFusion collect statistics when first creating a table. Has no effect after the table is created. Defaults to true. | -| datafusion.execution.target_partitions | 0 | Number of partitions for query execution. Increasing partitions can increase concurrency. Defaults to the number of CPU cores on the system | -| datafusion.execution.time_zone | NULL | The default time zone Some functions, e.g. `now`, return timestamps in this time zone. This is also used to interpret timezone-naive timestamps in comparisons and subtraction with timezone-aware timestamps. | -| datafusion.execution.parquet.enable_page_index | true | (reading) If true, reads the Parquet data page level metadata (the Page Index), if present, to reduce the I/O and number of rows decoded. | -| datafusion.execution.parquet.pruning | true | (reading) If true, the parquet reader attempts to skip entire row groups based on the predicate in the query and the metadata (min/max values) stored in the parquet file | -| datafusion.execution.parquet.skip_metadata | true | (reading) If true, the parquet reader skip the optional embedded metadata that may be in the file Schema. This setting can help avoid schema conflicts when querying multiple parquet files with schemas containing compatible types but different metadata. Arrow extension types (the `ARROW:extension:name` and `ARROW:extension:metadata` field metadata) are kept, because they are part of the type of a column | -| datafusion.execution.parquet.metadata_size_hint | 524288 | (reading) If specified, the parquet reader will try and fetch the last `size_hint` bytes of the parquet file optimistically. If not specified, two reads are required: One read to fetch the 8-byte parquet footer and another to fetch the metadata length encoded in the footer Default setting to 512 KiB, which should be sufficient for most parquet files, it can reduce one I/O operation per parquet file. If the metadata is larger than the hint, two reads will still be performed. | -| datafusion.execution.parquet.pushdown_filters | false | (reading) If true, filter expressions are be applied during the parquet decoding operation to reduce the number of rows decoded. This optimization is sometimes called "late materialization". | -| datafusion.execution.parquet.reorder_filters | false | (reading) If true, filter expressions evaluated during the parquet decoding operation will be reordered heuristically to minimize the cost of evaluation. If false, the filters are applied in the same order as written in the query | -| datafusion.execution.parquet.force_filter_selections | false | (reading) Force the use of RowSelections for filter results, when pushdown_filters is enabled. If false, the reader will automatically choose between a RowSelection and a Bitmap based on the number and pattern of selected rows. | -| datafusion.execution.parquet.enable_exact_reverse_scan | false | (reading) If true, a scan whose requested ordering is the reverse of the files' declared ordering reads each row group into memory and emits its rows in reverse, so the scan is exactly ordered and the `SortExec` above it can be removed. Peak memory per scanned file is one decoded row group (after projection and filtering). If false, only the row group order is reversed and the sort is kept. The value is captured when the table is created, so a later `SET` does not affect existing tables. | -| datafusion.execution.parquet.schema_force_view_types | true | (reading) If true, parquet reader will read columns of `Utf8/Utf8Large` with `Utf8View`, and `Binary/BinaryLarge` with `BinaryView`. The parquet reader is optimized for reading `Utf8View` and `BinaryView`, so such queries are significantly faster than reading `Utf8`/`Binary` and then casting to the view types. | -| datafusion.execution.parquet.binary_as_string | false | (reading) If true, parquet reader will read columns of `Binary/LargeBinary` with `Utf8`, and `BinaryView` with `Utf8View`. Parquet files generated by some legacy writers do not correctly set the UTF8 flag for strings, causing string columns to be loaded as BLOB instead. The parquet reader has special optimizations for `Utf8` validation, so reading such columns as strings is significantly faster than reading them as binary and then casting to string. | -| datafusion.execution.parquet.coerce_int96 | NULL | (reading) If set, parquet reader will read columns of physical type int96 as originating from this resolution instead of nanosecond. Valid values are: ns, us, ms, and s. This is useful for reading data from systems like Spark which store microsecond resolution timestamps in an int96, allowing them to write values with a larger date range than 64-bit timestamps with nanosecond resolution. | -| datafusion.execution.parquet.coerce_int96_tz | NULL | (reading) Optional timezone applied to INT96 columns when `coerce_int96` is set. When `Some`, INT96 columns coerce to `Timestamp(, Some())` instead of the default `Timestamp(, None)`. Spark and other systems write INT96 values as UTC-adjusted instants, so callers that need the resulting Arrow type to be timezone-aware (e.g. for Spark `TimestampType` semantics) should set this to `"UTC"`. No effect when `coerce_int96` is `None`, but the value is still validated when it is set. | -| datafusion.execution.parquet.bloom_filter_on_read | true | (reading) Use any available bloom filters when reading parquet files | -| datafusion.execution.parquet.max_predicate_cache_size | NULL | (reading) The maximum predicate cache size, in bytes. When `pushdown_filters` is enabled, sets the maximum memory used to cache the results of predicate evaluation between filter evaluation and output generation. Decreasing this value will reduce memory usage, but may increase IO and CPU usage. None means use the default parquet reader setting. 0 means no caching. | -| datafusion.execution.parquet.max_in_list_size | 20 | Maximum number of input values in an `IN (...)` list eligible for min/max pruning. Lists above this cap, or a cap of 0, skip this rewrite; other predicates and Bloom-filter pruning remain available. Within the cap, nonempty lists of at most 20 values use the existing per-value rewrite. Larger literal lists use a compact representation when the column type is string, variable-length binary, integer, decimal, date, time, timestamp, or duration. This applies to both `IN` and `NOT IN`, including lists with NULL members. `NOT IN` with NULL and all-NULL `IN` lists cannot match any rows. Compact lists containing NULL do not use the fully-matched-row-group optimization. Floating-point and other lists retain the existing per-value rewrite, so raising the cap can make those predicates expensive to build and evaluate. Defaults to 20. | -| datafusion.execution.parquet.enable_rle_to_dictionary | false | (reading) If true, top-level string and binary Parquet columns with dictionary pages are inferred and scanned as `Dictionary` / `Dictionary` instead of their plain value type. This applies only when DataFusion infers the table schema. Tables with a user-supplied schema are not promoted because the Parquet footer is not read at DDL time, so dictionary pages cannot be detected per column. Setting `dictionary_read_max_distinct_values` also enables dictionary reads. When both options are set, the cap controls which plain columns are promoted. See | -| datafusion.execution.parquet.dictionary_read_max_distinct_values | NULL | (reading) If set, promote top-level plain string and binary columns to dictionaries when every data page is dictionary encoded and the per-file sum of row-group cardinality upper bounds is within this cap. Files without page encoding statistics, such as those written by parquet-rs before 55.0.0 (DataFusion 46 and earlier) or DuckDB 1.1, are not promoted. Missing NDVs use the non-null row count when available and the row count otherwise. Values repeated across row groups may be counted more than once. The cap does not demote a dictionary declared by the same file; mixed dictionary/plain schemas across files still merge as plain. Computed during schema inference. Large row-group dictionaries can be slower because each output batch can retain the whole dictionary. The crossover is workload-dependent: in one 20-million-row group-by benchmark with 40-byte strings and five row groups per file, files with a per-file sum of about 500,000 (100,000 per row group) were faster than plain reads, while files with about 1,460,000 (about 295,000 per row group) were slower. Use a conservative cap and benchmark representative queries. | -| datafusion.execution.parquet.row_group_range_assignment | `start_offset` | (reading) Which byte range of a split file reads each row group. `start_offset` picks the range containing the row group's start. `midpoint` picks the range containing its midpoint, as Spark does, which spreads large row groups more evenly across ranges. | -| datafusion.execution.parquet.data_pagesize_limit | 1048576 | (writing) Sets best effort maximum size of data page in bytes | -| datafusion.execution.parquet.write_batch_size | 1024 | (writing) Sets write_batch_size in rows | -| datafusion.execution.parquet.writer_version | 1.0 | (writing) Sets parquet writer version valid values are "1.0" and "2.0" | -| datafusion.execution.parquet.skip_arrow_metadata | false | (writing) Skip encoding the embedded arrow metadata in the KV_meta This is analogous to the `ArrowWriterOptions::with_skip_arrow_metadata`. Refer to | -| datafusion.execution.parquet.compression | zstd(3) | (writing) Sets default parquet compression codec. Valid values are: uncompressed, snappy, gzip(level), brotli(level), lz4, zstd(level), and lz4_raw. These values are not case sensitive. If NULL, uses default parquet writer setting Note that this default setting is not the same as the default parquet writer setting. | -| datafusion.execution.parquet.dictionary_enabled | true | (writing) Sets if dictionary encoding is enabled. If NULL, uses default parquet writer setting | -| datafusion.execution.parquet.dictionary_page_size_limit | 1048576 | (writing) Sets best effort maximum dictionary page size, in bytes | -| datafusion.execution.parquet.statistics_enabled | page | (writing) Sets if statistics are enabled for any column Valid values are: "none", "chunk", and "page" These values are not case sensitive. If NULL, uses default parquet writer setting | -| datafusion.execution.parquet.max_row_group_size | 1048576 | (writing) Target maximum number of rows in each row group (defaults to 1M rows). Writing larger row groups requires more memory to write, but can get better compression and be faster to read. When `max_row_group_bytes` is also set, the writer flushes a row group when either limit is reached, whichever comes first. | -| datafusion.execution.parquet.max_row_group_bytes | NULL | (writing) Target maximum size of each row group in bytes. When set, the writer flushes whenever either this limit or `max_row_group_size` is reached, whichever comes first. Useful for bounding writer memory on wide schemas where a row-count limit can map to very different byte sizes. Matches the behavior of `parquet.block.size` in parquet-mr. If `None` (the default), only the row-count limit applies. Currently only honored when `allow_single_file_parallelism` is `false`; by default the parallel file writer ignores this limit. | -| datafusion.execution.parquet.created_by | datafusion version 57.0.0-dev | (writing) Sets "created by" property | -| datafusion.execution.parquet.column_index_truncate_length | 64 | (writing) Sets column index truncate length | -| datafusion.execution.parquet.statistics_truncate_length | 64 | (writing) Sets statistics truncate length. If NULL, uses default parquet writer setting | -| datafusion.execution.parquet.data_page_row_count_limit | 20000 | (writing) Sets best effort maximum number of rows in data page | -| datafusion.execution.parquet.encoding | NULL | (writing) Sets default encoding for any column. Valid values are: plain, plain_dictionary, rle, bit_packed, delta_binary_packed, delta_length_byte_array, delta_byte_array, rle_dictionary, byte_stream_split, and alp. These values are not case sensitive. If NULL, uses default parquet writer setting | -| datafusion.execution.parquet.bloom_filter_on_write | false | (writing) Write bloom filters for all columns when creating parquet files | -| datafusion.execution.parquet.bloom_filter_fpp | NULL | (writing) Sets bloom filter false positive probability. If NULL, uses default parquet writer setting | -| datafusion.execution.parquet.bloom_filter_ndv | NULL | (writing) Sets bloom filter number of distinct values. If NULL, uses default parquet writer setting | -| datafusion.execution.parquet.write_row_group_number_distinct_values | false | (writing) Write the number of distinct values (NDV) for each column in row group statistics when creating parquet files. Enabling this improves NDV-based query optimizations at the cost of hashing every non-null value during write. | -| datafusion.execution.parquet.allow_single_file_parallelism | true | (writing) Controls whether DataFusion will attempt to speed up writing parquet files by serializing them in parallel. Each column in each row group in each output file are serialized in parallel leveraging a maximum possible core count of n_files\*n_row_groups\*n_columns. | -| datafusion.execution.parquet.maximum_parallel_row_group_writers | 1 | (writing) By default parallel parquet writer is tuned for minimum memory usage in a streaming execution plan. You may see a performance benefit when writing large parquet files by increasing maximum_parallel_row_group_writers and maximum_buffered_record_batches_per_stream if your system has idle cores and can tolerate additional memory usage. Boosting these values is likely worthwhile when writing out already in-memory data, such as from a cached data frame. | -| datafusion.execution.parquet.maximum_buffered_record_batches_per_stream | 2 | (writing) By default parallel parquet writer is tuned for minimum memory usage in a streaming execution plan. You may see a performance benefit when writing large parquet files by increasing maximum_parallel_row_group_writers and maximum_buffered_record_batches_per_stream if your system has idle cores and can tolerate additional memory usage. Boosting these values is likely worthwhile when writing out already in-memory data, such as from a cached data frame. | -| datafusion.execution.parquet.content_defined_chunking.enabled | false | (writing) EXPERIMENTAL: Enable content-defined chunking (CDC) when writing parquet files. When enabled, parallel writing is automatically disabled since the chunker state must persist across row groups. | -| datafusion.execution.parquet.content_defined_chunking.min_chunk_size | 262144 | Minimum chunk size in bytes. The rolling hash will not trigger a split until this many bytes have been accumulated. Default is 256 KiB. | -| datafusion.execution.parquet.content_defined_chunking.max_chunk_size | 1048576 | Maximum chunk size in bytes. A split is forced when the accumulated size exceeds this value. Default is 1 MiB. | -| datafusion.execution.parquet.content_defined_chunking.norm_level | 0 | Normalization level. Increasing this improves deduplication ratio but increases fragmentation. Recommended range is [-3, 3], default is 0. | -| datafusion.execution.planning_concurrency | 0 | Fan-out during initial physical planning. This is mostly use to plan `UNION` children in parallel. Defaults to the number of CPU cores on the system | -| datafusion.execution.skip_physical_aggregate_schema_check | false | When set to true, skips verifying that the schema produced by planning the input of `LogicalPlan::Aggregate` exactly matches the schema of the input plan. When set to false, if the schema does not match exactly (including nullability and metadata), a planning error will be raised. This is used to workaround bugs in the planner that are now caught by the new schema verification step. | -| datafusion.execution.enable_migration_hash_join | true | Whether inner, left outer, right outer, and full outer hash joins use the refactored probe implementation. When set to `false`, these joins use the previous probe implementation. The fallback exists only as a workaround for bugs in the new implementation and will be removed, together with this option, once the migration is complete. See for details. | -| datafusion.execution.spill_compression | uncompressed | Sets the compression codec used when spilling data to disk. Since datafusion writes spill files using the Arrow IPC Stream format, only codecs supported by the Arrow IPC Stream Writer are allowed. Valid values are: uncompressed, lz4_frame, zstd. Note: lz4_frame offers faster (de)compression, but typically results in larger spill files. In contrast, zstd achieves higher compression ratios at the cost of slower (de)compression speed. | -| datafusion.execution.sort_spill_reservation_bytes | 10485760 | Specifies the reserved memory for each spillable sort operation to facilitate an in-memory merge. When a sort operation spills to disk, the in-memory data must be sorted and merged before being written to a file. This setting reserves a specific amount of memory for that in-memory sort/merge process. Note: This setting is irrelevant if the sort operation cannot spill (i.e., if there's no `DiskManager` configured). | -| datafusion.execution.sort_in_place_threshold_bytes | 1048576 | When sorting, below what size should data be concatenated and sorted in a single RecordBatch rather than sorted in batches and merged. | -| datafusion.execution.sort_pushdown_buffer_capacity | 1073741824 | Maximum buffer capacity (in bytes) per partition for BufferExec inserted during sort pushdown optimization. When PushdownSort eliminates a SortExec under SortPreservingMergeExec, a BufferExec is inserted to replace SortExec's buffering role. This prevents I/O stalls by allowing the scan to run ahead of the merge. This uses strictly less memory than the SortExec it replaces (which buffers the entire partition). The buffer respects the global memory pool limit. Setting this to a large value is safe — actual memory usage is bounded by partition size and global memory limits. | -| datafusion.execution.max_spill_file_size_bytes | 134217728 | Maximum size in bytes for individual spill files before rotating to a new file. When operators spill data to disk (e.g., RepartitionExec), they write multiple batches to the same file until this size limit is reached, then rotate to a new file. This reduces syscall overhead compared to one-file-per-batch while preventing files from growing too large. A larger value reduces file creation overhead but may hold more disk space. A smaller value creates more files but allows finer-grained space reclamation as files can be deleted once fully consumed. Now only `RepartitionExec` supports this spill file rotation feature, other spilling operators may create spill files larger than the limit. Default: 128 MB | -| datafusion.execution.enable_nlj_coordinated_fallback | true | Enables the memory-limited fallback for `NestedLoopJoinExec` join types that emit unmatched left rows in the final output (LEFT, LEFT SEMI, LEFT ANTI, LEFT MARK, FULL) when the right side has multiple partitions. This fallback shares left-side state (the current left chunk, the visited bitmap and the probe-thread counter) across all right-side partitions, which assumes every partition runs in the same process. Distributed engines that execute each output partition as an independent task (e.g. Ballista, datafusion-distributed) give each task its own copy of this state and poll only one partition, so the cross-partition counter never reaches zero and the fallback would stall. Such engines should set this to `false`: the coordinated fallback is then disabled for left-emitting multi-partition joins, which instead fail with a resource-exhaustion error under memory pressure rather than deadlocking. Single-partition and non-left-emitting joins are unaffected and always keep the fallback. | -| datafusion.execution.meta_fetch_concurrency | 32 | Number of files to read in parallel when inferring schema and statistics | -| datafusion.execution.minimum_parallel_output_files | 4 | Guarantees a minimum level of output files running in parallel. RecordBatches will be distributed in round robin fashion to each parallel writer. Each writer is closed and a new file opened once soft_max_rows_per_output_file or soft_max_bytes_per_output_file is reached. | -| datafusion.execution.soft_max_rows_per_output_file | 50000000 | Target number of rows in output files when writing multiple. This is a soft max, so it can be exceeded slightly. There also will be one file smaller than the limit if the total number of rows written is not roughly divisible by the soft max | -| datafusion.execution.soft_max_bytes_per_output_file | 4294967295 | Target encoded size in bytes of output files when writing multiple. Writers asynchronously report the cumulative encoded size as they process RecordBatches. The final file size may exceed this limit due to batches buffered before the limit is observed, the size of a batch, and file metadata written when the file is finalized. | -| datafusion.execution.max_buffered_batches_per_output_file | 2 | This is the maximum number of RecordBatches buffered for each output file being worked. Higher values can potentially give faster write performance at the cost of higher peak memory consumption. This budget is split evenly between two independent points in the write pipeline (see the demuxer diagram in #7791): how many files can be in flight from the demuxer to a writer task, and how many RecordBatches are buffered for a single file's writer. Must be at least 2 so each half gets at least 1 unit of buffering - 0 or 1 would leave one side with a zero-capacity channel and panic at write time. | -| datafusion.execution.listing_table_ignore_subdirectory | true | Should sub directories be ignored when scanning directories for data files. Defaults to true (ignores subdirectories), consistent with Hive. Note that this setting does not affect reading partitioned tables (e.g. `/table/year=2021/month=01/data.parquet`). | -| datafusion.execution.listing_table_factory_infer_partitions | true | Should a `ListingTable` created through the `ListingTableFactory` infer table partitions from Hive compliant directories. Defaults to true (partition columns are inferred and will be represented in the table schema). | -| datafusion.execution.listing_table_hive_default_partition_name | `__HIVE_DEFAULT_PARTITION__` | The directory name Hive compliant writers use in place of a null partition value, e.g. `/table/date=__HIVE_DEFAULT_PARTITION__/data.parquet`. Partition values equal to this name are read as NULL, as Hive, Spark and Impala do, and partitioned writes reject them. Corresponds to Hive's `hive.exec.default.partition.name`. Set this to the empty string to read and write such values as the literal directory name instead. | -| datafusion.execution.enable_recursive_ctes | true | Should DataFusion support recursive CTEs | -| datafusion.execution.split_file_groups_by_statistics | false | Attempt to eliminate sorts by packing & sorting files with non-overlapping statistics into the same file groups. Currently experimental | -| datafusion.execution.keep_partition_by_columns | false | Should DataFusion keep the columns used for partition_by in the output RecordBatches | -| datafusion.execution.enable_file_stream_work_stealing | true | When `true` (the default), DataFusion's built-in file scans dynamically rebalance files across partitions at query execution time: a partition that goes idle reads files (or byte-range morsels) originally assigned to a sibling partition, which keeps all partitions busy in a single process. Executors that depend on the plan-time partition assignment — such as Ballista and datafusion-distributed, which run each partition as an isolated task and never poll the siblings — should set this to `false` so each partition reads only its own file group and no runtime reassignment occurs. | -| datafusion.execution.skip_partial_aggregation_probe_ratio_threshold | 0.8 | Aggregation ratio (number of groups the partial aggregation creates / number of input rows) threshold for skipping partial aggregation. A key that returns after an early emit under memory pressure counts again. If the value is greater then partial aggregation will skip aggregation for further input | -| datafusion.execution.skip_partial_aggregation_probe_rows_threshold | 100000 | Number of input rows partial aggregation partition should process, before aggregation ratio check and trying to switch to skipping aggregation mode | -| datafusion.execution.use_row_number_estimates_to_optimize_partitioning | false | Should DataFusion use row number estimates at the input to decide whether increasing parallelism is beneficial or not. By default, only exact row numbers (not estimates) are used for this decision. Setting this flag to `true` will likely produce better plans. if the source of statistics is accurate. We plan to make this the default in the future. | -| datafusion.execution.enforce_batch_size_in_joins | false | Should DataFusion enforce batch size in joins or not. By default, DataFusion will not enforce batch size in joins. Enforcing batch size in joins can reduce memory usage when joining large tables with a highly-selective join filter, but is also slightly slower. Note: this option currently only applies to the symmetric hash join. | -| datafusion.execution.objectstore_writer_buffer_size | 10485760 | Size (bytes) of data buffer DataFusion uses when writing output files. This affects the size of the data chunks that are uploaded to remote object stores (e.g. AWS S3). If very large (>= 100 GiB) output files are being written, it may be necessary to increase this size to avoid errors from the remote end point. | -| datafusion.execution.enable_ansi_mode | false | Whether to enable ANSI SQL mode. The flag is experimental and relevant only for DataFusion Spark built-in functions When `enable_ansi_mode` is set to `true`, the query engine follows ANSI SQL semantics for expressions, casting, and error handling. This means: - **Strict type coercion rules:** implicit casts between incompatible types are disallowed. - **Standard SQL arithmetic behavior:** operations such as division by zero, numeric overflow, or invalid casts raise runtime errors rather than returning `NULL` or adjusted values. - **Consistent ANSI behavior** for string concatenation, comparisons, and `NULL` handling. When `enable_ansi_mode` is `false` (the default), the engine uses a more permissive, non-ANSI mode designed for user convenience and backward compatibility. In this mode: - Implicit casts between types are allowed (e.g., string to integer when possible). - Arithmetic operations are more lenient — for example, `abs()` on the minimum representable integer value returns the input value instead of raising overflow. - Division by zero or invalid casts may return `NULL` instead of failing. # Default `false` — ANSI SQL mode is disabled by default. | -| datafusion.execution.hash_join_buffering_capacity | 0 | How many bytes to buffer in the probe side of hash joins while the build side is concurrently being built. Without this, hash joins will wait until the full materialization of the build side before polling the probe side. This is useful in scenarios where the query is not completely CPU bounded, allowing to do some early work concurrently and reducing the latency of the query. Note that when hash join buffering is enabled, the probe side will start eagerly polling data, not giving time for the producer side of dynamic filters to produce any meaningful predicate. Queries with dynamic filters might see performance degradation. Disabled by default, set to a number greater than 0 for enabling it. | -| datafusion.optimizer.enable_distinct_aggregation_soft_limit | true | When set to true, the optimizer will push a limit operation into grouped aggregations which have no aggregate expressions, as a soft limit, emitting groups once the limit is reached, before all rows in the group are read. | -| datafusion.optimizer.enable_round_robin_repartition | true | When set to true, the physical plan optimizer will try to add round robin repartitioning to increase parallelism to leverage more CPU cores | -| datafusion.optimizer.enable_topk_aggregation | true | When set to true, the optimizer will attempt to perform limit operations during aggregations, if possible | -| datafusion.optimizer.enable_window_limits | true | When set to true, the optimizer will attempt to push limit operations past window functions, if possible | -| datafusion.optimizer.enable_window_topn | true | When set to true, the optimizer will replace Filter(rn<=K) → Window(ROW_NUMBER, RANK or DENSE_RANK) → Sort patterns with a PartitionedTopKExec that keeps only the top K rows of each window partition, avoiding a full sort of the input. | -| datafusion.optimizer.enable_row_number_to_aggregate | false | When set to true, the optimizer will rewrite a "top-1 per group" pattern of the form `Filter(row_number() = 1)` over a `PARTITION BY` window into an `Aggregate(first_value(... ORDER BY ...) GROUP BY partition)`. This avoids buffering / fully sorting the input, which is especially beneficial on wide payloads. Disabled by default because on nested/wide value types the aggregate only becomes memory efficient once the columnar nested-type `GroupsAccumulator` fast path is available; enabling it without that support can route wide payloads onto the slow per-group accumulator path. | -| datafusion.optimizer.enable_topk_repartition | true | When set to true, the optimizer will push TopK (Sort with fetch) below hash repartition when the partition key is a prefix of the sort key, reducing data volume before the shuffle. | -| datafusion.optimizer.enable_topk_dynamic_filter_pushdown | true | When set to true, the optimizer will attempt to push down TopK dynamic filters into the file scan phase. | -| datafusion.optimizer.enable_physical_uncorrelated_scalar_subquery | true | When set to true, uncorrelated scalar subqueries are left in the logical plan and executed by `ScalarSubqueryExec` during physical execution. When set to false, all scalar subqueries (including uncorrelated ones) are rewritten to left joins by the `ScalarSubqueryToJoin` optimizer rule. Note disabling this option is not recommended. It restores pre behavior, which silently produces incorrect results for multi-row subqueries and does not support scalar subqueries in ORDER BY / JOIN ON / aggregate-function arguments. This option is intended as a temporary escape hatch for distributed execution frameworks and is planned to be removed in a future DataFusion release. | -| datafusion.optimizer.enable_join_dynamic_filter_pushdown | true | When set to true, the optimizer will attempt to push down Join dynamic filters into the file scan phase. | -| datafusion.optimizer.enable_aggregate_dynamic_filter_pushdown | true | When set to true, the optimizer will attempt to push down Aggregate dynamic filters into the file scan phase. | -| datafusion.optimizer.enable_dynamic_filter_pushdown | true | When set to true attempts to push down dynamic filters generated by operators (TopK, Join & Aggregate) into the file scan phase. For example, for a query such as `SELECT * FROM t ORDER BY timestamp DESC LIMIT 10`, the optimizer will attempt to push down the current top 10 timestamps that the TopK operator references into the file scans. This means that if we already have 10 timestamps in the year 2025 any files that only have timestamps in the year 2024 can be skipped / pruned at various stages in the scan. The config will suppress `enable_join_dynamic_filter_pushdown`, `enable_topk_dynamic_filter_pushdown` & `enable_aggregate_dynamic_filter_pushdown` So if you disable `enable_topk_dynamic_filter_pushdown`, then enable `enable_dynamic_filter_pushdown`, the `enable_topk_dynamic_filter_pushdown` will be overridden. | -| datafusion.optimizer.filter_null_join_keys | false | When set to true, the optimizer will insert filters before a join between a nullable and non-nullable column to filter out nulls on the nullable side. This filter can add additional overhead when the file format does not fully support predicate push down. | -| datafusion.optimizer.repartition_aggregations | true | Should DataFusion repartition data using the aggregate keys to execute aggregates in parallel using the provided `target_partitions` level | -| datafusion.optimizer.repartition_file_min_size | 1048576 | Minimum total file size in bytes for file-group byte-range splitting to fire. Files (or merged file groups) smaller than this stay as one partition. Lower values produce more, smaller partitions — better at filling `target_partitions` worth of cores when files are modestly sized, at the cost of slightly more per-partition open / metadata-load overhead. | -| datafusion.optimizer.repartition_joins | true | Should DataFusion repartition data using the join keys to execute joins in parallel using the provided `target_partitions` level | -| datafusion.optimizer.allow_symmetric_joins_without_pruning | true | Should DataFusion allow symmetric hash joins for unbounded data sources even when its inputs do not have any ordering or filtering If the flag is not enabled, the SymmetricHashJoin operator will be unable to prune its internal buffers, resulting in certain join types - such as Full, Left, LeftAnti, LeftSemi, Right, RightAnti, and RightSemi - being produced only at the end of the execution. This is not typical in stream processing. Additionally, without proper design for long runner execution, all types of joins may encounter out-of-memory errors. | -| datafusion.optimizer.repartition_file_scans | true | When set to `true`, datasource partitions will be repartitioned to achieve maximum parallelism. This applies to both in-memory partitions and FileSource's file groups (1 group is 1 partition). For FileSources, only Parquet and CSV formats are currently supported. If set to `true` for a FileSource, all files will be repartitioned evenly (i.e., a single large file might be partitioned into smaller chunks) for parallel scanning. If set to `false` for a FileSource, different files will be read in parallel, but repartitioning won't happen within a single file. If set to `true` for an in-memory source, all memtable's partitions will have their batches repartitioned evenly to the desired number of `target_partitions`. Repartitioning can change the total number of partitions and batches per partition, but does not slice the initial record tables provided to the MemTable on creation. | -| datafusion.optimizer.preserve_file_partitions | 0 | Minimum number of distinct partition values required to group files by their Hive partition column values (enabling output partitioning declaration). How the option is used: - preserve_file_partitions=0: Disable it. - preserve_file_partitions=1: Always enable it. - preserve_file_partitions=N, actual file partitions=M: Only enable when M >= N. This threshold preserves I/O parallelism when file partitioning is below it. Note: This may reduce parallelism, rooting from the I/O level, if the number of distinct partitions is less than the target_partitions. | -| datafusion.optimizer.repartition_windows | true | Should DataFusion repartition data using the partitions keys to execute window functions in parallel using the provided `target_partitions` level | -| datafusion.optimizer.repartition_sorts | true | Should DataFusion execute sorts in a per-partition fashion and merge afterwards instead of coalescing first and sorting globally. With this flag is enabled, plans in the form below `text "SortExec: [a@0 ASC]", " CoalescePartitionsExec", " RepartitionExec: partitioning=RoundRobinBatch(8), input_partitions=1", ` would turn into the plan below which performs better in multithreaded environments `text "SortPreservingMergeExec: [a@0 ASC]", " SortExec: [a@0 ASC]", " RepartitionExec: partitioning=RoundRobinBatch(8), input_partitions=1", ` | -| datafusion.optimizer.subset_repartition_threshold | 4 | Partition count threshold for subset satisfaction optimization. When the current partition count is >= this threshold, DataFusion will skip repartitioning if the required partitioning expression is a subset of the current partition expression such as Hash(a) satisfies Hash(a, b). When the current partition count is < this threshold, DataFusion will repartition to increase parallelism even when subset satisfaction applies. Set to 0 to always repartition (disable subset satisfaction optimization). Set to a high value to always use subset satisfaction. Example (subset_repartition_threshold = 4): `text Hash([a]) satisfies Hash([a, b]) because (Hash([a, b]) is subset of Hash([a]) If current partitions (3) < threshold (4), repartition: AggregateExec: mode=FinalPartitioned, gby=[a, b], aggr=[SUM(x)] RepartitionExec: partitioning=Hash([a, b], 8), input_partitions=3 AggregateExec: mode=Partial, gby=[a, b], aggr=[SUM(x)] DataSourceExec: file_groups={...}, output_partitioning=Hash([a], 3) If current partitions (8) >= threshold (4), use subset satisfaction: AggregateExec: mode=SinglePartitioned, gby=[a, b], aggr=[SUM(x)] DataSourceExec: file_groups={...}, output_partitioning=Hash([a], 8) ` | -| datafusion.optimizer.prefer_existing_sort | false | When true, DataFusion will opportunistically remove sorts when the data is already sorted, (i.e. setting `preserve_order` to true on `RepartitionExec` and using `SortPreservingMergeExec`) When false, DataFusion will maximize plan parallelism using `RepartitionExec` even if this requires subsequently resorting data using a `SortExec`. | -| datafusion.optimizer.skip_failed_rules | false | When set to true, the logical plan optimizer will produce warning messages if any optimization rules produce errors and then proceed to the next rule. When set to false, any rules that produce errors will cause the query to fail | -| datafusion.optimizer.max_passes | 3 | The logical optimizer applies rules in order for up to `max_passes` passes, stopping early if a pass leaves the plan unchanged or repeats an earlier plan. The physical optimizer always runs in one pass. | -| datafusion.optimizer.top_down_join_key_reordering | true | When set to true, the physical plan optimizer will run a top down process to reorder the join keys | -| datafusion.optimizer.join_reordering | true | When set to true, the physical plan optimizer may swap join inputs based on statistics. When set to false, statistics-driven join input reordering is disabled and the original join order in the query is used. | -| datafusion.optimizer.use_statistics_registry | false | (Deprecated) Ignored: the physical plan optimizer always consults the session's pluggable `StatisticsRegistry` (register providers on the `SessionState`; with none it is a no-op). | -| datafusion.optimizer.prefer_hash_join | true | When set to true, the physical plan optimizer will prefer HashJoin over SortMergeJoin. HashJoin can work more efficiently than SortMergeJoin but consumes more memory | -| datafusion.optimizer.enable_piecewise_merge_join | false | When set to true, piecewise merge join is enabled. PiecewiseMergeJoin is currently experimental. Physical planner will opt for PiecewiseMergeJoin when there is only one range filter. | -| datafusion.optimizer.hash_join_single_partition_threshold | 4194304 | The maximum estimated size in bytes for one input side of a HashJoin will be collected into a single partition. Partitioned joins also use it: an input below it is not used as the co-partitioning reference for an input above it. | -| datafusion.optimizer.hash_join_single_partition_threshold_rows | 131072 | The maximum estimated size in rows for one input side of a HashJoin will be collected into a single partition. Partitioned joins also use it: an input below it is not used as the co-partitioning reference for an input above it. | -| datafusion.optimizer.hash_join_inlist_pushdown_max_size | 131072 | Maximum size in bytes for the build side of a hash join to be pushed down as an InList expression for dynamic filtering. Build sides larger than this will use hash table lookups instead. Set to 0 to always use hash table lookups. InList pushdown can be more efficient for small build sides because it can result in better statistics pruning as well as use any bloom filters present on the scan side. InList expressions are also more transparent and easier to serialize over the network in distributed uses of DataFusion. On the other hand InList pushdown requires making a copy of the data and thus adds some overhead to the build side and uses more memory. This setting is per-partition, so we may end up using `hash_join_inlist_pushdown_max_size` * `target_partitions` memory. The default is 128kB per partition. This should allow point lookup joins (e.g. joining on a unique primary key) to use InList pushdown in most cases but avoids excessive memory usage or overhead for larger joins. | -| datafusion.optimizer.hash_join_inlist_pushdown_max_distinct_values | 150 | Maximum number of distinct values (rows) in the build side of a hash join to be pushed down as an InList expression for dynamic filtering. Build sides with more rows than this will use hash table lookups instead. Set to 0 to always use hash table lookups. This provides an additional limit beyond `hash_join_inlist_pushdown_max_size` to prevent very large IN lists that might not provide much benefit over hash table lookups. This uses the deduplicated row count once the build side has been evaluated. The default is 150 values per partition. This is inspired by Trino's `max-filter-keys-per-column` setting. See: | -| datafusion.optimizer.default_filter_selectivity | 20 | The default filter selectivity used by Filter Statistics when an exact selectivity cannot be determined. Valid values are between 0 (no selectivity) and 100 (all rows are selected). | -| datafusion.optimizer.prefer_existing_union | false | When set to true, the optimizer will not attempt to convert Union to Interleave | -| datafusion.optimizer.expand_views_at_output | false | When set to true, if the returned type is a view type then the output will be coerced to a non-view. Coerces `Utf8View` to `LargeUtf8`, and `BinaryView` to `LargeBinary`. | -| datafusion.optimizer.enable_sort_pushdown | true | Enable sort pushdown optimization. When enabled, attempts to push sort requirements down to data sources that can natively handle them (e.g., by reversing file/row group read order). Returns **inexact ordering**: Sort operator is kept for correctness, but optimized input enables early termination for TopK queries (ORDER BY ... LIMIT N), providing significant speedup. Memory: No additional overhead (only changes read order). Future: Will add option to detect perfectly sorted data and eliminate Sort completely. Default: true | -| datafusion.optimizer.enable_leaf_expression_pushdown | true | When set to true, the optimizer will extract leaf expressions (such as `get_field`) from filter/sort/join nodes into projections closer to the leaf table scans, and push those projections down towards the leaf nodes. | -| datafusion.optimizer.enable_unions_to_filter | false | When set to true, the logical optimizer will rewrite `UNION DISTINCT` branches that read from the same source and differ only by filter predicates into a single branch with a combined filter. This optimization is conservative and only applies when the branches share the same source and compatible wrapper nodes such as identical projections or aliases. | -| datafusion.optimizer.single_distinct_prefer_native | false | Prefer native grouped DISTINCT aggregation when target_partitions is 1 and every aggregate explicitly supports a specialized GroupsAccumulator for its DISTINCT argument types. Defaults to false, preserving the two-phase GROUP BY rewrite. The native plan can avoid materializing distinct (group, value) pairs, but can be slower for many groups with duplicate values. It must fit each group's distinct values in memory, so queries that succeed by spilling with the rewrite may fail with this option enabled. One target partition can still have multiple input partitions and merge native states. | -| datafusion.explain.logical_plan_only | false | When set to true, the explain statement will only print logical plans | -| datafusion.explain.physical_plan_only | false | When set to true, the explain statement will only print physical plans | -| datafusion.explain.show_statistics | false | When set to true, the explain statement will print operator statistics for physical plans | -| datafusion.explain.show_sizes | true | When set to true, the explain statement will print the partition sizes | -| datafusion.explain.show_schema | false | When set to true, the explain statement will print schema information | -| datafusion.explain.show_equivalences | false | When set to true, the explain statement will print the equivalence properties (orderings, equivalent expressions, and constants) of each physical plan node | -| datafusion.explain.format | indent | Display format of explain. Default is "indent". When set to "tree", it will print the plan in a tree-rendered format. | -| datafusion.explain.tree_maximum_render_width | 240 | (format=tree only) Maximum total width of the rendered tree. When set to 0, the tree will have no width limit. | -| datafusion.explain.analyze_level | dev | Verbosity level for "EXPLAIN ANALYZE". Default is "dev" "summary" shows common metrics for high-level insights. "dev" provides deep operator-level introspection for developers. | -| datafusion.explain.analyze_categories | all | Which metric categories to include in "EXPLAIN ANALYZE" output. Comma-separated list of: "rows", "bytes", "timing", "uncategorized". Use "none" to show plan structure only, or "all" (default) to show everything. Metrics without a declared category are treated as "uncategorized". | -| datafusion.sql_parser.parse_float_as_decimal | false | When set to true, SQL parser will parse float as decimal type | -| datafusion.sql_parser.enable_ident_normalization | true | When set to true, SQL parser will normalize ident (convert ident to lowercase when not quoted) | -| datafusion.sql_parser.enable_options_value_normalization | false | When set to true, SQL parser will normalize options value (convert value to lowercase). Note that this option is ignored and will be removed in the future. All case-insensitive values are normalized automatically. | -| datafusion.sql_parser.dialect | generic | Configure the SQL dialect used by DataFusion's parser; supported values include: Generic, MySQL, PostgreSQL, Hive, SQLite, Snowflake, Redshift, MsSQL, ClickHouse, BigQuery, Ansi, DuckDB, Databricks, Spark. | -| datafusion.sql_parser.support_varchar_with_length | true | If true, permit lengths for `VARCHAR` such as `VARCHAR(20)`, but ignore the length. If false, error if a `VARCHAR` with a length is specified. The Arrow type system does not have a notion of maximum string length and thus DataFusion can not enforce such limits. | -| datafusion.sql_parser.map_string_types_to_utf8view | true | If true, string types (VARCHAR, CHAR, Text, and String) are mapped to `Utf8View` during SQL planning. If false, they are mapped to `Utf8`. Default is true. | -| datafusion.sql_parser.collect_spans | false | When set to true, the source locations relative to the original SQL query (i.e. [`Span`](https://docs.rs/sqlparser/latest/sqlparser/tokenizer/struct.Span.html)) will be collected and recorded in the logical plan nodes. | -| datafusion.sql_parser.recursion_limit | 51 | Specifies the recursion depth limit when parsing complex SQL Queries | -| datafusion.sql_parser.default_null_ordering | `nulls_max` | Specifies the default null ordering for query results. There are 4 options: - `nulls_max`: Nulls appear last in ascending order. - `nulls_min`: Nulls appear first in ascending order. - `nulls_first`: Nulls always be first in any order. - `nulls_last`: Nulls always be last in any order. By default, `nulls_max` is used to follow Postgres's behavior. postgres rule: | -| datafusion.sql_parser.enable_subquery_sort_elimination | true | When set to true, DataFusion may remove `ORDER BY` clauses from subqueries or CTEs during SQL planning when their ordering cannot affect the result, such as when no `LIMIT` or other order-sensitive operator depends on them. Disable this option to preserve explicit subquery ordering in the planned query. | -| datafusion.format.safe | true | If set to `true` any formatting errors will be written to the output instead of being converted into a [`std::fmt::Error`] | -| datafusion.format.null | | Format string for nulls | -| datafusion.format.date_format | %Y-%m-%d | Date format for date arrays | -| datafusion.format.datetime_format | %Y-%m-%dT%H:%M:%S%.f | Format for DateTime arrays | -| datafusion.format.timestamp_format | %Y-%m-%dT%H:%M:%S%.f | Timestamp format for timestamp arrays | -| datafusion.format.timestamp_tz_format | NULL | Timestamp format for timestamp with timezone arrays. When `None`, ISO 8601 format is used. | -| datafusion.format.time_format | %H:%M:%S%.f | Time format for time arrays | -| datafusion.format.duration_format | pretty | Duration format. Can be either `"pretty"` or `"ISO8601"` | -| datafusion.format.types_info | false | Show types in visual representation batches | +| key | default | description | +| ------------------------------------------------------------------------ | ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| datafusion.catalog.create_default_catalog_and_schema | true | Whether the default catalog and schema should be created automatically. | +| datafusion.catalog.default_catalog | datafusion | The default catalog name - this impacts what SQL queries use if not specified | +| datafusion.catalog.default_schema | public | The default schema name - this impacts what SQL queries use if not specified | +| datafusion.catalog.information_schema | false | Should DataFusion provide access to `information_schema` virtual tables for displaying schema information | +| datafusion.catalog.partition_information_schema | false | When set together with `information_schema`, the `information_schema` of each catalog only describes the objects in that catalog. When not set, the `information_schema` of each catalog describes all catalogs. | +| datafusion.catalog.location | NULL | Location scanned to load tables for `default` schema | +| datafusion.catalog.format | NULL | Type of `TableProvider` to use when loading `default` schema | +| datafusion.catalog.has_header | true | Default value for `format.has_header` for `CREATE EXTERNAL TABLE` if not specified explicitly in the statement. | +| datafusion.catalog.newlines_in_values | false | Specifies whether newlines in (quoted) CSV values are supported. This is the default value for `format.newlines_in_values` for `CREATE EXTERNAL TABLE` if not specified explicitly in the statement. Parsing newlines in quoted values may be affected by execution behaviour such as parallel file scanning. Setting this to `true` ensures that newlines in values are parsed successfully, which may reduce performance. | +| datafusion.execution.batch_size | 8192 | Default batch size while creating new batches, it's especially useful for buffer-in-memory batches since creating tiny batches would result in too much metadata memory consumption | +| datafusion.execution.perfect_hash_join_small_build_threshold | 1024 | A perfect hash join (see `HashJoinExec` for more details) will be considered if the range of keys (max - min) on the build side is < this threshold. This provides a fast path for joins with very small key ranges, bypassing the density check. Currently only supports cases where build_side.num_rows() < u32::MAX. Support for build_side.num_rows() >= u32::MAX will be added in the future. | +| datafusion.execution.perfect_hash_join_min_key_density | 0.15 | The minimum required density of join keys on the build side to consider a perfect hash join (see `HashJoinExec` for more details). Density is calculated as: `(number of rows) / (max_key - min_key + 1)`. A perfect hash join may be used if the actual key density > this value. Currently only supports cases where build_side.num_rows() < u32::MAX. Support for build_side.num_rows() >= u32::MAX will be added in the future. | +| datafusion.execution.coalesce_batches | true | When set to true, record batches will be examined between each operator and small batches will be coalesced into larger batches. This is helpful when there are highly selective filters or joins that could produce tiny output batches. The target batch size is determined by the configuration setting | +| datafusion.execution.collect_statistics | true | Should DataFusion collect statistics when first creating a table. Has no effect after the table is created. Defaults to true. | +| datafusion.execution.target_partitions | 0 | Number of partitions for query execution. Increasing partitions can increase concurrency. Defaults to the number of CPU cores on the system | +| datafusion.execution.time_zone | NULL | The default time zone Some functions, e.g. `now`, return timestamps in this time zone. This is also used to interpret timezone-naive timestamps in comparisons and subtraction with timezone-aware timestamps. | +| datafusion.execution.parquet.enable_page_index | true | (reading) If true, reads the Parquet data page level metadata (the Page Index), if present, to reduce the I/O and number of rows decoded. | +| datafusion.execution.parquet.pruning | true | (reading) If true, the parquet reader attempts to skip entire row groups based on the predicate in the query and the metadata (min/max values) stored in the parquet file | +| datafusion.execution.parquet.skip_metadata | true | (reading) If true, the parquet reader skip the optional embedded metadata that may be in the file Schema. This setting can help avoid schema conflicts when querying multiple parquet files with schemas containing compatible types but different metadata. Arrow extension types (the `ARROW:extension:name` and `ARROW:extension:metadata` field metadata) are kept, because they are part of the type of a column | +| datafusion.execution.parquet.metadata_size_hint | 524288 | (reading) If specified, the parquet reader will try and fetch the last `size_hint` bytes of the parquet file optimistically. If not specified, two reads are required: One read to fetch the 8-byte parquet footer and another to fetch the metadata length encoded in the footer Default setting to 512 KiB, which should be sufficient for most parquet files, it can reduce one I/O operation per parquet file. If the metadata is larger than the hint, two reads will still be performed. | +| datafusion.execution.parquet.pushdown_filters | false | (reading) If true, filter expressions are be applied during the parquet decoding operation to reduce the number of rows decoded. This optimization is sometimes called "late materialization". | +| datafusion.execution.parquet.reorder_filters | false | (reading) If true, filter expressions evaluated during the parquet decoding operation will be reordered heuristically to minimize the cost of evaluation. If false, the filters are applied in the same order as written in the query | +| datafusion.execution.parquet.force_filter_selections | false | (reading) Force the use of RowSelections for filter results, when pushdown_filters is enabled. If false, the reader will automatically choose between a RowSelection and a Bitmap based on the number and pattern of selected rows. | +| datafusion.execution.parquet.enable_exact_reverse_scan | false | (reading) If true, a scan whose requested ordering is the reverse of the files' declared ordering reads each row group into memory and emits its rows in reverse, so the scan is exactly ordered and the `SortExec` above it can be removed. Peak memory per scanned file is one decoded row group (after projection and filtering). If false, only the row group order is reversed and the sort is kept. The value is captured when the table is created, so a later `SET` does not affect existing tables. | +| datafusion.execution.parquet.schema_force_view_types | true | (reading) If true, parquet reader will read columns of `Utf8/Utf8Large` with `Utf8View`, and `Binary/BinaryLarge` with `BinaryView`. The parquet reader is optimized for reading `Utf8View` and `BinaryView`, so such queries are significantly faster than reading `Utf8`/`Binary` and then casting to the view types. | +| datafusion.execution.parquet.binary_as_string | false | (reading) If true, parquet reader will read columns of `Binary/LargeBinary` with `Utf8`, and `BinaryView` with `Utf8View`. Parquet files generated by some legacy writers do not correctly set the UTF8 flag for strings, causing string columns to be loaded as BLOB instead. The parquet reader has special optimizations for `Utf8` validation, so reading such columns as strings is significantly faster than reading them as binary and then casting to string. | +| datafusion.execution.parquet.coerce_int96 | NULL | (reading) If set, parquet reader will read columns of physical type int96 as originating from this resolution instead of nanosecond. Valid values are: ns, us, ms, and s. This is useful for reading data from systems like Spark which store microsecond resolution timestamps in an int96, allowing them to write values with a larger date range than 64-bit timestamps with nanosecond resolution. | +| datafusion.execution.parquet.coerce_int96_tz | NULL | (reading) Optional timezone applied to INT96 columns when `coerce_int96` is set. When `Some`, INT96 columns coerce to `Timestamp(, Some())` instead of the default `Timestamp(, None)`. Spark and other systems write INT96 values as UTC-adjusted instants, so callers that need the resulting Arrow type to be timezone-aware (e.g. for Spark `TimestampType` semantics) should set this to `"UTC"`. No effect when `coerce_int96` is `None`, but the value is still validated when it is set. | +| datafusion.execution.parquet.bloom_filter_on_read | true | (reading) Use any available bloom filters when reading parquet files | +| datafusion.execution.parquet.max_predicate_cache_size | NULL | (reading) The maximum predicate cache size, in bytes. When `pushdown_filters` is enabled, sets the maximum memory used to cache the results of predicate evaluation between filter evaluation and output generation. Decreasing this value will reduce memory usage, but may increase IO and CPU usage. None means use the default parquet reader setting. 0 means no caching. | +| datafusion.execution.parquet.max_in_list_size | 20 | Maximum number of input values in an `IN (...)` list eligible for min/max pruning. Lists above this cap, or a cap of 0, skip this rewrite; other predicates and Bloom-filter pruning remain available. Within the cap, nonempty lists of at most 20 values use the existing per-value rewrite. Larger literal lists use a compact representation when the column type is string, variable-length binary, integer, decimal, date, time, timestamp, or duration. This applies to both `IN` and `NOT IN`, including lists with NULL members. `NOT IN` with NULL and all-NULL `IN` lists cannot match any rows. Compact lists containing NULL do not use the fully-matched-row-group optimization. Floating-point and other lists retain the existing per-value rewrite, so raising the cap can make those predicates expensive to build and evaluate. Defaults to 20. | +| datafusion.execution.parquet.enable_rle_to_dictionary | false | (reading) If true, top-level string and binary Parquet columns with dictionary pages are inferred and scanned as `Dictionary` / `Dictionary` instead of their plain value type. This applies only when DataFusion infers the table schema. Tables with a user-supplied schema are not promoted because the Parquet footer is not read at DDL time, so dictionary pages cannot be detected per column. Setting `dictionary_read_max_distinct_values` also enables dictionary reads. When both options are set, the cap controls which plain columns are promoted. See | +| datafusion.execution.parquet.dictionary_read_max_distinct_values | NULL | (reading) If set, promote top-level plain string and binary columns to dictionaries when every data page is dictionary encoded and the per-file sum of row-group cardinality upper bounds is within this cap. Files without page encoding statistics, such as those written by parquet-rs before 55.0.0 (DataFusion 46 and earlier) or DuckDB 1.1, are not promoted. Missing NDVs use the non-null row count when available and the row count otherwise. Values repeated across row groups may be counted more than once. The cap does not demote a dictionary declared by the same file; mixed dictionary/plain schemas across files still merge as plain. Computed during schema inference. Large row-group dictionaries can be slower because each output batch can retain the whole dictionary. The crossover is workload-dependent: in one 20-million-row group-by benchmark with 40-byte strings and five row groups per file, files with a per-file sum of about 500,000 (100,000 per row group) were faster than plain reads, while files with about 1,460,000 (about 295,000 per row group) were slower. Use a conservative cap and benchmark representative queries. | +| datafusion.execution.parquet.row_group_range_assignment | `start_offset` | (reading) Which byte range of a split file reads each row group. `start_offset` picks the range containing the row group's start. `midpoint` picks the range containing its midpoint, as Spark does, which spreads large row groups more evenly across ranges. | +| datafusion.execution.parquet.data_pagesize_limit | 1048576 | (writing) Sets best effort maximum size of data page in bytes | +| datafusion.execution.parquet.write_batch_size | 1024 | (writing) Sets write_batch_size in rows | +| datafusion.execution.parquet.writer_version | 1.0 | (writing) Sets parquet writer version valid values are "1.0" and "2.0" | +| datafusion.execution.parquet.skip_arrow_metadata | false | (writing) Skip encoding the embedded arrow metadata in the KV_meta This is analogous to the `ArrowWriterOptions::with_skip_arrow_metadata`. Refer to | +| datafusion.execution.parquet.compression | zstd(3) | (writing) Sets default parquet compression codec. Valid values are: uncompressed, snappy, gzip(level), brotli(level), lz4, zstd(level), and lz4_raw. These values are not case sensitive. If NULL, uses default parquet writer setting Note that this default setting is not the same as the default parquet writer setting. | +| datafusion.execution.parquet.dictionary_enabled | true | (writing) Sets if dictionary encoding is enabled. If NULL, uses default parquet writer setting | +| datafusion.execution.parquet.dictionary_page_size_limit | 1048576 | (writing) Sets best effort maximum dictionary page size, in bytes | +| datafusion.execution.parquet.statistics_enabled | page | (writing) Sets if statistics are enabled for any column Valid values are: "none", "chunk", and "page" These values are not case sensitive. If NULL, uses default parquet writer setting | +| datafusion.execution.parquet.max_row_group_size | 1048576 | (writing) Target maximum number of rows in each row group (defaults to 1M rows). Writing larger row groups requires more memory to write, but can get better compression and be faster to read. When `max_row_group_bytes` is also set, the writer flushes a row group when either limit is reached, whichever comes first. | +| datafusion.execution.parquet.max_row_group_bytes | NULL | (writing) Target maximum size of each row group in bytes. When set, the writer flushes whenever either this limit or `max_row_group_size` is reached, whichever comes first. Useful for bounding writer memory on wide schemas where a row-count limit can map to very different byte sizes. Matches the behavior of `parquet.block.size` in parquet-mr. If `None` (the default), only the row-count limit applies. Currently only honored when `allow_single_file_parallelism` is `false`; by default the parallel file writer ignores this limit. | +| datafusion.execution.parquet.created_by | datafusion version 57.0.0-dev | (writing) Sets "created by" property | +| datafusion.execution.parquet.column_index_truncate_length | 64 | (writing) Sets column index truncate length | +| datafusion.execution.parquet.statistics_truncate_length | 64 | (writing) Sets statistics truncate length. If NULL, uses default parquet writer setting | +| datafusion.execution.parquet.data_page_row_count_limit | 20000 | (writing) Sets best effort maximum number of rows in data page | +| datafusion.execution.parquet.encoding | NULL | (writing) Sets default encoding for any column. Valid values are: plain, plain_dictionary, rle, bit_packed, delta_binary_packed, delta_length_byte_array, delta_byte_array, rle_dictionary, byte_stream_split, and alp. These values are not case sensitive. If NULL, uses default parquet writer setting | +| datafusion.execution.parquet.bloom_filter_on_write | false | (writing) Write bloom filters for all columns when creating parquet files | +| datafusion.execution.parquet.bloom_filter_fpp | NULL | (writing) Sets bloom filter false positive probability. If NULL, uses default parquet writer setting | +| datafusion.execution.parquet.bloom_filter_ndv | NULL | (writing) Sets bloom filter number of distinct values. If NULL, uses default parquet writer setting | +| datafusion.execution.parquet.write_row_group_number_distinct_values | false | (writing) Write the number of distinct values (NDV) for each column in row group statistics when creating parquet files. Enabling this improves NDV-based query optimizations at the cost of hashing every non-null value during write. | +| datafusion.execution.parquet.allow_single_file_parallelism | true | (writing) Controls whether DataFusion will attempt to speed up writing parquet files by serializing them in parallel. Each column in each row group in each output file are serialized in parallel leveraging a maximum possible core count of n_files\*n_row_groups\*n_columns. | +| datafusion.execution.parquet.maximum_parallel_row_group_writers | 1 | (writing) By default parallel parquet writer is tuned for minimum memory usage in a streaming execution plan. You may see a performance benefit when writing large parquet files by increasing maximum_parallel_row_group_writers and maximum_buffered_record_batches_per_stream if your system has idle cores and can tolerate additional memory usage. Boosting these values is likely worthwhile when writing out already in-memory data, such as from a cached data frame. | +| datafusion.execution.parquet.maximum_buffered_record_batches_per_stream | 2 | (writing) By default parallel parquet writer is tuned for minimum memory usage in a streaming execution plan. You may see a performance benefit when writing large parquet files by increasing maximum_parallel_row_group_writers and maximum_buffered_record_batches_per_stream if your system has idle cores and can tolerate additional memory usage. Boosting these values is likely worthwhile when writing out already in-memory data, such as from a cached data frame. | +| datafusion.execution.parquet.content_defined_chunking.enabled | false | (writing) EXPERIMENTAL: Enable content-defined chunking (CDC) when writing parquet files. When enabled, parallel writing is automatically disabled since the chunker state must persist across row groups. | +| datafusion.execution.parquet.content_defined_chunking.min_chunk_size | 262144 | Minimum chunk size in bytes. The rolling hash will not trigger a split until this many bytes have been accumulated. Default is 256 KiB. | +| datafusion.execution.parquet.content_defined_chunking.max_chunk_size | 1048576 | Maximum chunk size in bytes. A split is forced when the accumulated size exceeds this value. Default is 1 MiB. | +| datafusion.execution.parquet.content_defined_chunking.norm_level | 0 | Normalization level. Increasing this improves deduplication ratio but increases fragmentation. Recommended range is [-3, 3], default is 0. | +| datafusion.execution.planning_concurrency | 0 | Fan-out during initial physical planning. This is mostly use to plan `UNION` children in parallel. Defaults to the number of CPU cores on the system | +| datafusion.execution.skip_physical_aggregate_schema_check | false | When set to true, skips verifying that the schema produced by planning the input of `LogicalPlan::Aggregate` exactly matches the schema of the input plan. When set to false, if the schema does not match exactly (including nullability and metadata), a planning error will be raised. This is used to workaround bugs in the planner that are now caught by the new schema verification step. | +| datafusion.execution.enable_migration_hash_join | true | Whether inner, left outer, right outer, and full outer hash joins use the refactored probe implementation. When set to `false`, these joins use the previous probe implementation. The fallback exists only as a workaround for bugs in the new implementation and will be removed, together with this option, once the migration is complete. See for details. | +| datafusion.execution.spill_compression | uncompressed | Sets the compression codec used when spilling data to disk. Since datafusion writes spill files using the Arrow IPC Stream format, only codecs supported by the Arrow IPC Stream Writer are allowed. Valid values are: uncompressed, lz4_frame, zstd. Note: lz4_frame offers faster (de)compression, but typically results in larger spill files. In contrast, zstd achieves higher compression ratios at the cost of slower (de)compression speed. | +| datafusion.execution.sort_spill_reservation_bytes | 10485760 | Specifies the reserved memory for each spillable sort operation to facilitate an in-memory merge. When a sort operation spills to disk, the in-memory data must be sorted and merged before being written to a file. This setting reserves a specific amount of memory for that in-memory sort/merge process. Note: This setting is irrelevant if the sort operation cannot spill (i.e., if there's no `DiskManager` configured). | +| datafusion.execution.sort_in_place_threshold_bytes | 1048576 | When sorting, below what size should data be concatenated and sorted in a single RecordBatch rather than sorted in batches and merged. | +| datafusion.execution.sort_pushdown_buffer_capacity | 1073741824 | Maximum buffer capacity (in bytes) per partition for BufferExec inserted during sort pushdown optimization. When PushdownSort eliminates a SortExec under SortPreservingMergeExec, a BufferExec is inserted to replace SortExec's buffering role. This prevents I/O stalls by allowing the scan to run ahead of the merge. This uses strictly less memory than the SortExec it replaces (which buffers the entire partition). The buffer respects the global memory pool limit. Setting this to a large value is safe — actual memory usage is bounded by partition size and global memory limits. | +| datafusion.execution.max_spill_file_size_bytes | 134217728 | Maximum size in bytes for individual spill files before rotating to a new file. When operators spill data to disk (e.g., RepartitionExec), they write multiple batches to the same file until this size limit is reached, then rotate to a new file. This reduces syscall overhead compared to one-file-per-batch while preventing files from growing too large. A larger value reduces file creation overhead but may hold more disk space. A smaller value creates more files but allows finer-grained space reclamation as files can be deleted once fully consumed. Now only `RepartitionExec` supports this spill file rotation feature, other spilling operators may create spill files larger than the limit. Default: 128 MB | +| datafusion.execution.enable_nlj_coordinated_fallback | true | Enables the memory-limited fallback for `NestedLoopJoinExec` join types that emit unmatched left rows in the final output (LEFT, LEFT SEMI, LEFT ANTI, LEFT MARK, FULL) when the right side has multiple partitions. This fallback shares left-side state (the current left chunk, the visited bitmap and the probe-thread counter) across all right-side partitions, which assumes every partition runs in the same process. Distributed engines that execute each output partition as an independent task (e.g. Ballista, datafusion-distributed) give each task its own copy of this state and poll only one partition, so the cross-partition counter never reaches zero and the fallback would stall. Such engines should set this to `false`: the coordinated fallback is then disabled for left-emitting multi-partition joins, which instead fail with a resource-exhaustion error under memory pressure rather than deadlocking. Single-partition and non-left-emitting joins are unaffected and always keep the fallback. | +| datafusion.execution.meta_fetch_concurrency | 32 | Number of files to read in parallel when inferring schema and statistics | +| datafusion.execution.minimum_parallel_output_files | 4 | Guarantees a minimum level of output files running in parallel. RecordBatches will be distributed in round robin fashion to each parallel writer. Each writer is closed and a new file opened once soft_max_rows_per_output_file or soft_max_bytes_per_output_file is reached. | +| datafusion.execution.soft_max_rows_per_output_file | 50000000 | Target number of rows in output files when writing multiple. This is a soft max, so it can be exceeded slightly. There also will be one file smaller than the limit if the total number of rows written is not roughly divisible by the soft max | +| datafusion.execution.soft_max_bytes_per_output_file | 4294967295 | Target encoded size in bytes of output files when writing multiple. Writers asynchronously report the cumulative encoded size as they process RecordBatches. The final file size may exceed this limit due to batches buffered before the limit is observed, the size of a batch, and file metadata written when the file is finalized. | +| datafusion.execution.max_buffered_batches_per_output_file | 2 | This is the maximum number of RecordBatches buffered for each output file being worked. Higher values can potentially give faster write performance at the cost of higher peak memory consumption. This budget is split evenly between two independent points in the write pipeline (see the demuxer diagram in #7791): how many files can be in flight from the demuxer to a writer task, and how many RecordBatches are buffered for a single file's writer. Must be at least 2 so each half gets at least 1 unit of buffering - 0 or 1 would leave one side with a zero-capacity channel and panic at write time. | +| datafusion.execution.listing_table_ignore_subdirectory | true | Should sub directories be ignored when scanning directories for data files. Defaults to true (ignores subdirectories), consistent with Hive. Note that this setting does not affect reading partitioned tables (e.g. `/table/year=2021/month=01/data.parquet`). | +| datafusion.execution.listing_table_factory_infer_partitions | true | Should a `ListingTable` created through the `ListingTableFactory` infer table partitions from Hive compliant directories. Defaults to true (partition columns are inferred and will be represented in the table schema). | +| datafusion.execution.listing_table_hive_default_partition_name | `__HIVE_DEFAULT_PARTITION__` | The directory name Hive compliant writers use in place of a null partition value, e.g. `/table/date=__HIVE_DEFAULT_PARTITION__/data.parquet`. Partition values equal to this name are read as NULL, as Hive, Spark and Impala do, and partitioned writes reject them. Corresponds to Hive's `hive.exec.default.partition.name`. Set this to the empty string to read and write such values as the literal directory name instead. | +| datafusion.execution.enable_recursive_ctes | true | Should DataFusion support recursive CTEs | +| datafusion.execution.split_file_groups_by_statistics | false | Attempt to eliminate sorts by packing & sorting files with non-overlapping statistics into the same file groups. Currently experimental | +| datafusion.execution.keep_partition_by_columns | false | Should DataFusion keep the columns used for partition_by in the output RecordBatches | +| datafusion.execution.enable_file_stream_work_stealing | true | When `true` (the default), DataFusion's built-in file scans dynamically rebalance files across partitions at query execution time: a partition that goes idle reads files (or byte-range morsels) originally assigned to a sibling partition, which keeps all partitions busy in a single process. Executors that depend on the plan-time partition assignment — such as Ballista and datafusion-distributed, which run each partition as an isolated task and never poll the siblings — should set this to `false` so each partition reads only its own file group and no runtime reassignment occurs. | +| datafusion.execution.skip_partial_aggregation_probe_ratio_threshold | 0.8 | Aggregation ratio (number of groups the partial aggregation creates / number of input rows) threshold for skipping partial aggregation. A key that returns after an early emit under memory pressure counts again. If the value is greater then partial aggregation will skip aggregation for further input | +| datafusion.execution.skip_partial_aggregation_probe_rows_threshold | 100000 | Number of input rows partial aggregation partition should process, before aggregation ratio check and trying to switch to skipping aggregation mode | +| datafusion.execution.use_row_number_estimates_to_optimize_partitioning | false | Should DataFusion use row number estimates at the input to decide whether increasing parallelism is beneficial or not. By default, only exact row numbers (not estimates) are used for this decision. Setting this flag to `true` will likely produce better plans. if the source of statistics is accurate. We plan to make this the default in the future. | +| datafusion.execution.enforce_batch_size_in_joins | false | Should DataFusion enforce batch size in joins or not. By default, DataFusion will not enforce batch size in joins. Enforcing batch size in joins can reduce memory usage when joining large tables with a highly-selective join filter, but is also slightly slower. Note: this option currently only applies to the symmetric hash join. | +| datafusion.execution.objectstore_writer_buffer_size | 10485760 | Size (bytes) of data buffer DataFusion uses when writing output files. This affects the size of the data chunks that are uploaded to remote object stores (e.g. AWS S3). If very large (>= 100 GiB) output files are being written, it may be necessary to increase this size to avoid errors from the remote end point. | +| datafusion.execution.enable_ansi_mode | false | Whether to enable ANSI SQL mode. The flag is experimental and relevant only for DataFusion Spark built-in functions When `enable_ansi_mode` is set to `true`, the query engine follows ANSI SQL semantics for expressions, casting, and error handling. This means: - **Strict type coercion rules:** implicit casts between incompatible types are disallowed. - **Standard SQL arithmetic behavior:** operations such as division by zero, numeric overflow, or invalid casts raise runtime errors rather than returning `NULL` or adjusted values. - **Consistent ANSI behavior** for string concatenation, comparisons, and `NULL` handling. When `enable_ansi_mode` is `false` (the default), the engine uses a more permissive, non-ANSI mode designed for user convenience and backward compatibility. In this mode: - Implicit casts between types are allowed (e.g., string to integer when possible). - Arithmetic operations are more lenient — for example, `abs()` on the minimum representable integer value returns the input value instead of raising overflow. - Division by zero or invalid casts may return `NULL` instead of failing. # Default `false` — ANSI SQL mode is disabled by default. | +| datafusion.execution.hash_join_buffering_capacity | 0 | How many bytes to buffer in the probe side of hash joins while the build side is concurrently being built. Without this, hash joins will wait until the full materialization of the build side before polling the probe side. This is useful in scenarios where the query is not completely CPU bounded, allowing to do some early work concurrently and reducing the latency of the query. Note that when hash join buffering is enabled, the probe side will start eagerly polling data, not giving time for the producer side of dynamic filters to produce any meaningful predicate. Queries with dynamic filters might see performance degradation. Disabled by default, set to a number greater than 0 for enabling it. | +| datafusion.optimizer.enable_distinct_aggregation_soft_limit | true | When set to true, the optimizer will push a limit operation into grouped aggregations which have no aggregate expressions, as a soft limit, emitting groups once the limit is reached, before all rows in the group are read. | +| datafusion.optimizer.enable_round_robin_repartition | true | When set to true, the physical plan optimizer will try to add round robin repartitioning to increase parallelism to leverage more CPU cores | +| datafusion.optimizer.enable_topk_aggregation | true | When set to true, the optimizer will attempt to perform limit operations during aggregations, if possible | +| datafusion.optimizer.enable_window_limits | true | When set to true, the optimizer will attempt to push limit operations past window functions, if possible | +| datafusion.optimizer.enable_window_topn | true | When set to true, the optimizer will replace Filter(rn<=K) → Window(ROW_NUMBER, RANK or DENSE_RANK) → Sort patterns with a PartitionedTopKExec that keeps only the top K rows of each window partition, avoiding a full sort of the input. | +| datafusion.optimizer.enable_row_number_to_aggregate | false | When set to true, the optimizer will rewrite a "top-1 per group" pattern of the form `Filter(row_number() = 1)` over a `PARTITION BY` window into an `Aggregate(first_value(... ORDER BY ...) GROUP BY partition)`. This avoids buffering / fully sorting the input, which is especially beneficial on wide payloads. Disabled by default because on nested/wide value types the aggregate only becomes memory efficient once the columnar nested-type `GroupsAccumulator` fast path is available; enabling it without that support can route wide payloads onto the slow per-group accumulator path. | +| datafusion.optimizer.enable_topk_repartition | true | When set to true, the optimizer will push TopK (Sort with fetch) below hash repartition when the partition key is a prefix of the sort key, reducing data volume before the shuffle. | +| datafusion.optimizer.enable_topk_dynamic_filter_pushdown | true | When set to true, the optimizer will attempt to push down TopK dynamic filters into the file scan phase. | +| datafusion.optimizer.enable_physical_uncorrelated_scalar_subquery | true | When set to true, uncorrelated scalar subqueries are left in the logical plan and executed by `ScalarSubqueryExec` during physical execution. When set to false, all scalar subqueries (including uncorrelated ones) are rewritten to left joins by the `ScalarSubqueryToJoin` optimizer rule. Note disabling this option is not recommended. It restores pre behavior, which silently produces incorrect results for multi-row subqueries and does not support scalar subqueries in ORDER BY / JOIN ON / aggregate-function arguments. This option is intended as a temporary escape hatch for distributed execution frameworks and is planned to be removed in a future DataFusion release. | +| datafusion.optimizer.enable_join_dynamic_filter_pushdown | true | When set to true, the optimizer will attempt to push down Join dynamic filters into the file scan phase. | +| datafusion.optimizer.enable_aggregate_dynamic_filter_pushdown | true | When set to true, the optimizer will attempt to push down Aggregate dynamic filters into the file scan phase. | +| datafusion.optimizer.enable_dynamic_filter_pushdown | true | When set to true attempts to push down dynamic filters generated by operators (TopK, Join & Aggregate) into the file scan phase. For example, for a query such as `SELECT * FROM t ORDER BY timestamp DESC LIMIT 10`, the optimizer will attempt to push down the current top 10 timestamps that the TopK operator references into the file scans. This means that if we already have 10 timestamps in the year 2025 any files that only have timestamps in the year 2024 can be skipped / pruned at various stages in the scan. The config will suppress `enable_join_dynamic_filter_pushdown`, `enable_topk_dynamic_filter_pushdown` & `enable_aggregate_dynamic_filter_pushdown` So if you disable `enable_topk_dynamic_filter_pushdown`, then enable `enable_dynamic_filter_pushdown`, the `enable_topk_dynamic_filter_pushdown` will be overridden. | +| datafusion.optimizer.filter_null_join_keys | false | When set to true, the optimizer will insert filters before a join between a nullable and non-nullable column to filter out nulls on the nullable side. This filter can add additional overhead when the file format does not fully support predicate push down. | +| datafusion.optimizer.repartition_aggregations | true | Should DataFusion repartition data using the aggregate keys to execute aggregates in parallel using the provided `target_partitions` level | +| datafusion.optimizer.repartition_file_min_size | 1048576 | Minimum total file size in bytes for file-group byte-range splitting to fire. Files (or merged file groups) smaller than this stay as one partition. Lower values produce more, smaller partitions — better at filling `target_partitions` worth of cores when files are modestly sized, at the cost of slightly more per-partition open / metadata-load overhead. | +| datafusion.optimizer.repartition_joins | true | Should DataFusion repartition data using the join keys to execute joins in parallel using the provided `target_partitions` level | +| datafusion.optimizer.allow_symmetric_joins_without_pruning | true | Should DataFusion allow symmetric hash joins for unbounded data sources even when its inputs do not have any ordering or filtering If the flag is not enabled, the SymmetricHashJoin operator will be unable to prune its internal buffers, resulting in certain join types - such as Full, Left, LeftAnti, LeftSemi, Right, RightAnti, and RightSemi - being produced only at the end of the execution. This is not typical in stream processing. Additionally, without proper design for long runner execution, all types of joins may encounter out-of-memory errors. | +| datafusion.optimizer.repartition_file_scans | true | When set to `true`, datasource partitions will be repartitioned to achieve maximum parallelism. This applies to both in-memory partitions and FileSource's file groups (1 group is 1 partition). For FileSources, only Parquet and CSV formats are currently supported. If set to `true` for a FileSource, all files will be repartitioned evenly (i.e., a single large file might be partitioned into smaller chunks) for parallel scanning. If set to `false` for a FileSource, different files will be read in parallel, but repartitioning won't happen within a single file. If set to `true` for an in-memory source, all memtable's partitions will have their batches repartitioned evenly to the desired number of `target_partitions`. Repartitioning can change the total number of partitions and batches per partition, but does not slice the initial record tables provided to the MemTable on creation. | +| datafusion.optimizer.preserve_file_partitions | 0 | Minimum number of distinct partition values required to group files by their Hive partition column values (enabling output partitioning declaration). How the option is used: - preserve_file_partitions=0: Disable it. - preserve_file_partitions=1: Always enable it. - preserve_file_partitions=N, actual file partitions=M: Only enable when M >= N. This threshold preserves I/O parallelism when file partitioning is below it. Note: This may reduce parallelism, rooting from the I/O level, if the number of distinct partitions is less than the target_partitions. | +| datafusion.optimizer.repartition_windows | true | Should DataFusion repartition data using the partitions keys to execute window functions in parallel using the provided `target_partitions` level | +| datafusion.optimizer.repartition_sorts | true | Should DataFusion execute sorts in a per-partition fashion and merge afterwards instead of coalescing first and sorting globally. With this flag is enabled, plans in the form below `text "SortExec: [a@0 ASC]", " CoalescePartitionsExec", " RepartitionExec: partitioning=RoundRobinBatch(8), input_partitions=1", ` would turn into the plan below which performs better in multithreaded environments `text "SortPreservingMergeExec: [a@0 ASC]", " SortExec: [a@0 ASC]", " RepartitionExec: partitioning=RoundRobinBatch(8), input_partitions=1", ` | +| datafusion.optimizer.subset_repartition_threshold | 4 | Partition count threshold for subset satisfaction optimization. When the current partition count is >= this threshold, DataFusion will skip repartitioning if the required partitioning expression is a subset of the current partition expression such as Hash(a) satisfies Hash(a, b). When the current partition count is < this threshold, DataFusion will repartition to increase parallelism even when subset satisfaction applies. Set to 0 to always repartition (disable subset satisfaction optimization). Set to a high value to always use subset satisfaction. Example (subset_repartition_threshold = 4): `text Hash([a]) satisfies Hash([a, b]) because (Hash([a, b]) is subset of Hash([a]) If current partitions (3) < threshold (4), repartition: AggregateExec: mode=FinalPartitioned, gby=[a, b], aggr=[SUM(x)] RepartitionExec: partitioning=Hash([a, b], 8), input_partitions=3 AggregateExec: mode=Partial, gby=[a, b], aggr=[SUM(x)] DataSourceExec: file_groups={...}, output_partitioning=Hash([a], 3) If current partitions (8) >= threshold (4), use subset satisfaction: AggregateExec: mode=SinglePartitioned, gby=[a, b], aggr=[SUM(x)] DataSourceExec: file_groups={...}, output_partitioning=Hash([a], 8) ` | +| datafusion.optimizer.prefer_existing_sort | false | When true, DataFusion will opportunistically remove sorts when the data is already sorted, (i.e. setting `preserve_order` to true on `RepartitionExec` and using `SortPreservingMergeExec`) When false, DataFusion will maximize plan parallelism using `RepartitionExec` even if this requires subsequently resorting data using a `SortExec`. | +| datafusion.optimizer.skip_failed_rules | false | When set to true, the logical plan optimizer will produce warning messages if any optimization rules produce errors and then proceed to the next rule. When set to false, any rules that produce errors will cause the query to fail | +| datafusion.optimizer.max_passes | 3 | The logical optimizer applies rules in order for up to `max_passes` passes, stopping early if a pass leaves the plan unchanged or repeats an earlier plan. The physical optimizer always runs in one pass. | +| datafusion.optimizer.top_down_join_key_reordering | true | When set to true, the physical plan optimizer will run a top down process to reorder the join keys | +| datafusion.optimizer.join_reordering | true | When set to true, the physical plan optimizer may swap join inputs based on statistics. When set to false, statistics-driven join input reordering is disabled and the original join order in the query is used. | +| datafusion.optimizer.use_statistics_registry | false | (Deprecated) Ignored: the physical plan optimizer always consults the session's pluggable `StatisticsRegistry` (register providers on the `SessionState`; with none it is a no-op). | +| datafusion.optimizer.prefer_hash_join | true | When set to true, the physical plan optimizer will prefer HashJoin over SortMergeJoin. HashJoin can work more efficiently than SortMergeJoin but consumes more memory | +| datafusion.optimizer.enable_piecewise_merge_join | false | When set to true, piecewise merge join is enabled. PiecewiseMergeJoin is currently experimental. Physical planner will opt for PiecewiseMergeJoin when there is only one range filter. | +| datafusion.optimizer.hash_join_single_partition_threshold | 4194304 | The maximum estimated size in bytes for one input side of a HashJoin will be collected into a single partition. Partitioned joins also use it: an input below it is not used as the co-partitioning reference for an input above it. | +| datafusion.optimizer.hash_join_single_partition_threshold_rows | 131072 | The maximum estimated size in rows for one input side of a HashJoin will be collected into a single partition. Partitioned joins also use it: an input below it is not used as the co-partitioning reference for an input above it. | +| datafusion.optimizer.hash_join_inlist_pushdown_max_size | 131072 | Maximum size in bytes for the build side of a hash join to be pushed down as an InList expression for dynamic filtering. Build sides larger than this will use hash table lookups instead. Set to 0 to always use hash table lookups. InList pushdown can be more efficient for small build sides because it can result in better statistics pruning as well as use any bloom filters present on the scan side. InList expressions are also more transparent and easier to serialize over the network in distributed uses of DataFusion. On the other hand InList pushdown requires making a copy of the data and thus adds some overhead to the build side and uses more memory. This setting is per-partition, so we may end up using `hash_join_inlist_pushdown_max_size` * `target_partitions` memory. The default is 128kB per partition. This should allow point lookup joins (e.g. joining on a unique primary key) to use InList pushdown in most cases but avoids excessive memory usage or overhead for larger joins. | +| datafusion.optimizer.hash_join_inlist_pushdown_max_distinct_values | 150 | Maximum number of distinct values (rows) in the build side of a hash join to be pushed down as an InList expression for dynamic filtering. Build sides with more rows than this will use hash table lookups instead. Set to 0 to always use hash table lookups. This provides an additional limit beyond `hash_join_inlist_pushdown_max_size` to prevent very large IN lists that might not provide much benefit over hash table lookups. This uses the deduplicated row count once the build side has been evaluated. The default is 150 values per partition. This is inspired by Trino's `max-filter-keys-per-column` setting. See: | +| datafusion.optimizer.hash_join_dynamic_filter_inlist_max_distinct_values | 20 | Maximum number of distinct build side keys, over all partitions of a hash join, for the global dynamic filter to test membership with one InList of all keys. Above this limit the dynamic filter uses a bloom filter (see `hash_join_bloom_filter_fpp`). Every partition must also use an InList by itself (`hash_join_inlist_pushdown_max_distinct_values` and `hash_join_inlist_pushdown_max_size`). For larger sets, a bloom filter is cheaper per probe row than an InList. | +| datafusion.optimizer.hash_join_bloom_filter_fpp | 0.01 | Target false positive probability of the bloom filter in the dynamic filter of a hash join. When the build side of a hash join has more distinct keys than `hash_join_inlist_pushdown_max_distinct_values` (over all partitions), the dynamic filter tests probe rows against one bloom filter of all build side keys. Each build partition fills a filter of its keys, sized for the estimated number of distinct keys of all partitions at this probability. Then one task ORs the partition filters and folds the result to this probability. | +| datafusion.optimizer.hash_join_bloom_filter_max_bytes | 8388608 | Maximum size, in bytes, of the bloom filter in the dynamic filter of a hash join (and of the filter of each build partition). A build side that needs a larger filter at `hash_join_bloom_filter_fpp` gets no bloom filter: such a large build side is rarely selective, so the filter is too big to be useful. The dynamic filter then tests only the min/max bounds (and keeps key range pruning). | +| datafusion.optimizer.hash_join_bloom_filter_max_fpp | 0.15 | The bloom filter of a hash join dynamic filter is not used when its estimated false positive probability is above this value (the filter is saturated, a safety net). Then the dynamic filter tests only the min/max bounds of the build keys, because a saturated filter removes few rows and costs CPU time for each probe row. | +| datafusion.optimizer.default_filter_selectivity | 20 | The default filter selectivity used by Filter Statistics when an exact selectivity cannot be determined. Valid values are between 0 (no selectivity) and 100 (all rows are selected). | +| datafusion.optimizer.prefer_existing_union | false | When set to true, the optimizer will not attempt to convert Union to Interleave | +| datafusion.optimizer.expand_views_at_output | false | When set to true, if the returned type is a view type then the output will be coerced to a non-view. Coerces `Utf8View` to `LargeUtf8`, and `BinaryView` to `LargeBinary`. | +| datafusion.optimizer.enable_sort_pushdown | true | Enable sort pushdown optimization. When enabled, attempts to push sort requirements down to data sources that can natively handle them (e.g., by reversing file/row group read order). Returns **inexact ordering**: Sort operator is kept for correctness, but optimized input enables early termination for TopK queries (ORDER BY ... LIMIT N), providing significant speedup. Memory: No additional overhead (only changes read order). Future: Will add option to detect perfectly sorted data and eliminate Sort completely. Default: true | +| datafusion.optimizer.enable_leaf_expression_pushdown | true | When set to true, the optimizer will extract leaf expressions (such as `get_field`) from filter/sort/join nodes into projections closer to the leaf table scans, and push those projections down towards the leaf nodes. | +| datafusion.optimizer.enable_unions_to_filter | false | When set to true, the logical optimizer will rewrite `UNION DISTINCT` branches that read from the same source and differ only by filter predicates into a single branch with a combined filter. This optimization is conservative and only applies when the branches share the same source and compatible wrapper nodes such as identical projections or aliases. | +| datafusion.optimizer.single_distinct_prefer_native | false | Prefer native grouped DISTINCT aggregation when target_partitions is 1 and every aggregate explicitly supports a specialized GroupsAccumulator for its DISTINCT argument types. Defaults to false, preserving the two-phase GROUP BY rewrite. The native plan can avoid materializing distinct (group, value) pairs, but can be slower for many groups with duplicate values. It must fit each group's distinct values in memory, so queries that succeed by spilling with the rewrite may fail with this option enabled. One target partition can still have multiple input partitions and merge native states. | +| datafusion.explain.logical_plan_only | false | When set to true, the explain statement will only print logical plans | +| datafusion.explain.physical_plan_only | false | When set to true, the explain statement will only print physical plans | +| datafusion.explain.show_statistics | false | When set to true, the explain statement will print operator statistics for physical plans | +| datafusion.explain.show_sizes | true | When set to true, the explain statement will print the partition sizes | +| datafusion.explain.show_schema | false | When set to true, the explain statement will print schema information | +| datafusion.explain.show_equivalences | false | When set to true, the explain statement will print the equivalence properties (orderings, equivalent expressions, and constants) of each physical plan node | +| datafusion.explain.format | indent | Display format of explain. Default is "indent". When set to "tree", it will print the plan in a tree-rendered format. | +| datafusion.explain.tree_maximum_render_width | 240 | (format=tree only) Maximum total width of the rendered tree. When set to 0, the tree will have no width limit. | +| datafusion.explain.analyze_level | dev | Verbosity level for "EXPLAIN ANALYZE". Default is "dev" "summary" shows common metrics for high-level insights. "dev" provides deep operator-level introspection for developers. | +| datafusion.explain.analyze_categories | all | Which metric categories to include in "EXPLAIN ANALYZE" output. Comma-separated list of: "rows", "bytes", "timing", "uncategorized". Use "none" to show plan structure only, or "all" (default) to show everything. Metrics without a declared category are treated as "uncategorized". | +| datafusion.sql_parser.parse_float_as_decimal | false | When set to true, SQL parser will parse float as decimal type | +| datafusion.sql_parser.enable_ident_normalization | true | When set to true, SQL parser will normalize ident (convert ident to lowercase when not quoted) | +| datafusion.sql_parser.enable_options_value_normalization | false | When set to true, SQL parser will normalize options value (convert value to lowercase). Note that this option is ignored and will be removed in the future. All case-insensitive values are normalized automatically. | +| datafusion.sql_parser.dialect | generic | Configure the SQL dialect used by DataFusion's parser; supported values include: Generic, MySQL, PostgreSQL, Hive, SQLite, Snowflake, Redshift, MsSQL, ClickHouse, BigQuery, Ansi, DuckDB, Databricks, Spark. | +| datafusion.sql_parser.support_varchar_with_length | true | If true, permit lengths for `VARCHAR` such as `VARCHAR(20)`, but ignore the length. If false, error if a `VARCHAR` with a length is specified. The Arrow type system does not have a notion of maximum string length and thus DataFusion can not enforce such limits. | +| datafusion.sql_parser.map_string_types_to_utf8view | true | If true, string types (VARCHAR, CHAR, Text, and String) are mapped to `Utf8View` during SQL planning. If false, they are mapped to `Utf8`. Default is true. | +| datafusion.sql_parser.collect_spans | false | When set to true, the source locations relative to the original SQL query (i.e. [`Span`](https://docs.rs/sqlparser/latest/sqlparser/tokenizer/struct.Span.html)) will be collected and recorded in the logical plan nodes. | +| datafusion.sql_parser.recursion_limit | 51 | Specifies the recursion depth limit when parsing complex SQL Queries | +| datafusion.sql_parser.default_null_ordering | `nulls_max` | Specifies the default null ordering for query results. There are 4 options: - `nulls_max`: Nulls appear last in ascending order. - `nulls_min`: Nulls appear first in ascending order. - `nulls_first`: Nulls always be first in any order. - `nulls_last`: Nulls always be last in any order. By default, `nulls_max` is used to follow Postgres's behavior. postgres rule: | +| datafusion.sql_parser.enable_subquery_sort_elimination | true | When set to true, DataFusion may remove `ORDER BY` clauses from subqueries or CTEs during SQL planning when their ordering cannot affect the result, such as when no `LIMIT` or other order-sensitive operator depends on them. Disable this option to preserve explicit subquery ordering in the planned query. | +| datafusion.format.safe | true | If set to `true` any formatting errors will be written to the output instead of being converted into a [`std::fmt::Error`] | +| datafusion.format.null | | Format string for nulls | +| datafusion.format.date_format | %Y-%m-%d | Date format for date arrays | +| datafusion.format.datetime_format | %Y-%m-%dT%H:%M:%S%.f | Format for DateTime arrays | +| datafusion.format.timestamp_format | %Y-%m-%dT%H:%M:%S%.f | Timestamp format for timestamp arrays | +| datafusion.format.timestamp_tz_format | NULL | Timestamp format for timestamp with timezone arrays. When `None`, ISO 8601 format is used. | +| datafusion.format.time_format | %H:%M:%S%.f | Time format for time arrays | +| datafusion.format.duration_format | pretty | Duration format. Can be either `"pretty"` or `"ISO8601"` | +| datafusion.format.types_info | false | Show types in visual representation batches | You can also reset configuration options to default settings via SQL using the `RESET` command. For example, to set and reset `datafusion.execution.batch_size`: From 92745096c7873d0fbbad2fa99a7c5fd9a91dc8b3 Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Wed, 4 Nov 2026 06:02:11 -0300 Subject: [PATCH 2/9] refactor: drop the routed hash table lookup dynamic filter The hash join dynamic filter is the global filter of all build partitions: bounds AND an InList or a bloom filter, with a key-range pruning bitmap. Both join modes now build only that filter, so the routed hash table lookup has no user left. A canceled partition contributes no key to the filter, as on main since https://github.com/apache/datafusion/pull/27092. Its stream never probes, so no row that only its build side matches can reach the output. This commit: - Removes the routed `CASE` / `RangeExpr` filter, `HashTableLookupExpr` and its re-exports, the crate-private `RangeExpr::route_partition_ids`, the `HashTableLookupExpr` pruning branch and its proto test. - Replaces `PushdownStrategy::Map(Arc, Option>)` with `PushdownStrategy::Large(Option>)`. The join keeps its own hash map. - Builds the collect-left filter only from the global filter. - Rewrites the `CASE` pruning test with range branches, and drops the unused `rstest` dev-dependency of `datafusion-pruning`. `HashExpr`, `SeededRandomState` and `RangeExpr` stay public with their proto support. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/extended.yml | 4 - Cargo.lock | 1 - .../src/joins/hash_join/bloom_filter.rs | 2 +- .../physical-plan/src/joins/hash_join/exec.rs | 9 +- .../joins/hash_join/exec/prepared/tests.rs | 6 +- .../physical-plan/src/joins/hash_join/mod.rs | 7 - .../joins/hash_join/partitioned_hash_eval.rs | 839 +----------------- .../src/joins/hash_join/shared_bounds.rs | 372 +++----- datafusion/physical-plan/src/joins/mod.rs | 30 +- .../physical-plan/src/repartition/mod.rs | 14 - datafusion/proto/tests/cases/plans/exprs.rs | 52 -- datafusion/pruning/Cargo.toml | 1 - datafusion/pruning/src/pruning_predicate.rs | 176 +--- .../test_files/dynamic_row_group_pruning.slt | 2 +- docs/source/contributor-guide/api-health.md | 2 +- xtask/src/ci_steps.rs | 64 +- 16 files changed, 199 insertions(+), 1382 deletions(-) diff --git a/.github/workflows/extended.yml b/.github/workflows/extended.yml index 5085ad49846f1..d03da2f7bb7f3 100644 --- a/.github/workflows/extended.yml +++ b/.github/workflows/extended.yml @@ -78,10 +78,6 @@ jobs: run: cargo xtask ci step test extended - name: Run fuzz tests run: cargo xtask ci step test fuzz - # Migrate it to the above unified fuzz runner over time - # Tracker: https://github.com/apache/datafusion/issues/27082 - - name: Run unmigrated fuzzers with more iterations - run: cargo xtask ci step test fuzz-legacy - parallel: - name: Verify Working Directory Clean run: git diff --exit-code diff --git a/Cargo.lock b/Cargo.lock index 56d78545fa498..76993ff15a3df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2694,7 +2694,6 @@ dependencies = [ "insta", "itertools 0.15.0", "log", - "rstest", ] [[package]] diff --git a/datafusion/physical-plan/src/joins/hash_join/bloom_filter.rs b/datafusion/physical-plan/src/joins/hash_join/bloom_filter.rs index 7eaad50db5f76..5d3e807428030 100644 --- a/datafusion/physical-plan/src/joins/hash_join/bloom_filter.rs +++ b/datafusion/physical-plan/src/joins/hash_join/bloom_filter.rs @@ -339,7 +339,7 @@ impl PhysicalExpr for BloomFilterExpr { use datafusion_proto_models::protobuf; use datafusion_proto_models::protobuf::physical_expr_node::ExprType; - // Like `HashTableLookupExpr`, the expression holds runtime build side + // The expression holds runtime build side // state that is not serialized. It is replaced with lit(true), which // keeps every row: the dynamic filter is only an optimization. A plan // that is executed again calls reset_state(), and the join builds a new diff --git a/datafusion/physical-plan/src/joins/hash_join/exec.rs b/datafusion/physical-plan/src/joins/hash_join/exec.rs index 18daa87027489..5b7df73d26add 100644 --- a/datafusion/physical-plan/src/joins/hash_join/exec.rs +++ b/datafusion/physical-plan/src/joins/hash_join/exec.rs @@ -47,7 +47,6 @@ use crate::projection::{ EmbeddedProjection, JoinData, ProjectionExec, try_embed_projection, try_pushdown_through_join_with_column_indices, }; -use crate::repartition::REPARTITION_RANDOM_STATE; use crate::statistics::{ChildStats, StatisticsArgs, with_per_partition_fetch}; use crate::{ ChildrenPropertiesMode, ExecutionPlanProperties, ReplaceChildrenOptions, @@ -1787,8 +1786,6 @@ impl ExecutionPlan for HashJoinExec { .counter(ARRAY_MAP_CREATED_COUNT_METRIC_NAME, partition); // Initialize build_accumulator lazily with runtime partition counts (only if enabled) - // Use RepartitionExec's random state (seeds: 0,0,0,0) for partition routing - let repartition_random_state = REPARTITION_RANDOM_STATE; let build_accumulator = enable_dynamic_filter_pushdown .then(|| { self.dynamic_filter.as_ref().map(|df| { @@ -1805,7 +1802,6 @@ impl ExecutionPlan for HashJoinExec { self.right.as_ref(), filter, on_right, - repartition_random_state, self.null_equality, not_in, GlobalFilterOptions::new(context.session_config().options()) @@ -3529,7 +3525,6 @@ fn build_in_memory( PushdownStrategy::Empty } else { // If the build side is small enough we can use IN list pushdown. - // If it's too big we fall back to pushing down a reference to the hash table. // See `PushdownStrategy` for more details. let estimated_size = left_values .iter() @@ -3573,7 +3568,7 @@ fn build_in_memory( } ok }); - PushdownStrategy::Map(Arc::clone(&map), pruning_bitmap) + PushdownStrategy::Large(pruning_bitmap) } }; @@ -10711,7 +10706,7 @@ mod tests { // The FilterExec sits above the repartition so each probe partition is only // read after the join has waited for every partition's build report, which - // makes the finalized (routed and widened) filter the one actually applied. + // makes the finalized (widened) filter the one actually applied. let dynamic_filter = HashJoinExec::create_dynamic_filter(&on); let consumer: Arc = Arc::clone(&dynamic_filter) as _; let right_filtered: Arc = diff --git a/datafusion/physical-plan/src/joins/hash_join/exec/prepared/tests.rs b/datafusion/physical-plan/src/joins/hash_join/exec/prepared/tests.rs index 5d3bd9c5921d6..c3b3d4616a161 100644 --- a/datafusion/physical-plan/src/joins/hash_join/exec/prepared/tests.rs +++ b/datafusion/physical-plan/src/joins/hash_join/exec/prepared/tests.rs @@ -847,8 +847,8 @@ async fn prepared_inlist_shares_accounted_build_buffers() -> Result<()> { Arc::new(config), ) .await?; - let PushdownStrategy::Map(_, bitmap) = &map.build.membership else { - panic!("expected hash-map membership"); + let PushdownStrategy::Large(bitmap) = &map.build.membership else { + panic!("expected large membership"); }; let bitmap_bytes = bitmap.as_ref().map_or(0, |bitmap| bitmap.size()); assert_eq!(map.reserved_bytes(), bytes + bitmap_bytes); @@ -943,7 +943,7 @@ async fn prepared_byte_keys_use_hash_membership() -> Result<()> { assert!(prepared.build.bounds.is_none()); assert!(matches!( &prepared.build.membership, - PushdownStrategy::Map(_, _) + PushdownStrategy::Large(_) )); let task = with_probe_filter(base)? .builder() diff --git a/datafusion/physical-plan/src/joins/hash_join/mod.rs b/datafusion/physical-plan/src/joins/hash_join/mod.rs index e6b22864d246c..c765583236036 100644 --- a/datafusion/physical-plan/src/joins/hash_join/mod.rs +++ b/datafusion/physical-plan/src/joins/hash_join/mod.rs @@ -26,13 +26,6 @@ #[doc(hidden)] pub use bloom_filter::BloomFilterExpr; pub use exec::{HashJoinExec, HashJoinExecBuilder, PreparedHashJoinBuild}; -/// # Public Only for Internal Use: -/// -/// This is not a public API and is for internal use only; see [API policy] for details. -/// -/// [API policy]: https://datafusion.apache.org/contributor-guide/api-health.html#internal-public-apis -#[doc(hidden)] -pub use partitioned_hash_eval::HashTableLookupExpr; pub use partitioned_hash_eval::{HashExpr, SeededRandomState}; mod batched_join_processor; diff --git a/datafusion/physical-plan/src/joins/hash_join/partitioned_hash_eval.rs b/datafusion/physical-plan/src/joins/hash_join/partitioned_hash_eval.rs index eabc1bc86f633..cae5af937a469 100644 --- a/datafusion/physical-plan/src/joins/hash_join/partitioned_hash_eval.rs +++ b/datafusion/physical-plan/src/joins/hash_join/partitioned_hash_eval.rs @@ -2,28 +2,22 @@ // SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership // SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements -//! Hash computation and hash table lookup expressions for dynamic filtering +//! Hash computation expression for dynamic filtering -use std::{fmt::Display, hash::Hash, ops::Range, sync::Arc}; +use std::{fmt::Display, hash::Hash, sync::Arc}; use arrow::{ - array::{ArrayRef, BooleanArray, BooleanBufferBuilder, UInt32Array, UInt64Array}, - compute::take_arrays, + array::{ArrayRef, UInt64Array}, datatypes::{DataType, Schema}, record_batch::RecordBatch, }; +use datafusion_common::Result; use datafusion_common::hash_utils::RandomState; -use datafusion_common::hash_utils::{create_hashes, with_hashes}; -use datafusion_common::{Result, internal_err}; +use datafusion_common::hash_utils::create_hashes; use datafusion_expr::ColumnarValue; use datafusion_expr_common::dyn_eq::DynHash; use datafusion_physical_expr_common::physical_expr::{PhysicalExpr, PhysicalExprRef}; -use crate::joins::array_map::ArrayMap; -use crate::joins::key_range_bitmap::KeyRangeBitmap; -use crate::joins::{Map, ReservedMap}; -use crate::repartition::RangeExpr; - /// RandomState wrapper that preserves the seed used to create it. /// /// This is needed because `RandomState` doesn't expose its seed after creation, @@ -240,7 +234,9 @@ impl HashExpr { let Some(protobuf::physical_expr_node::ExprType::HashExpr(hash_expr)) = &node.expr_type else { - return internal_err!("PhysicalExprNode is not a HashExpr"); + return datafusion_common::internal_err!( + "PhysicalExprNode is not a HashExpr" + ); }; // Destructure exhaustively (no `..`) so that a newly added proto field // is a compile error here instead of being silently ignored. @@ -259,441 +255,6 @@ impl HashExpr { } } -/// Physical expression that checks join keys in a [`Map`] (hash table or array map). -/// -/// Returns a [`BooleanArray`] indicating if join keys (from `on_columns`) exist in the map. -// TODO: rename to MapLookupExpr -pub struct HashTableLookupExpr { - /// Columns in the ON clause used to compute the join key for lookups - on_columns: Vec, - /// Random state for hashing (with seeds preserved for serialization) - random_state: SeededRandomState, - /// Map(s) to check against, with their reservations - lookup: Lookup, - /// Description for display - description: String, - /// Shared pruning-only view of the build side's key range - pruning_bitmap: Option>, -} - -/// The build side a [`HashTableLookupExpr`] checks probe keys against. -#[derive(Clone)] -enum Lookup { - /// One map holding every build key, probed directly with no routing. - Single(Arc), - /// One map (a hash table or an array map) per build partition of a - /// `Partitioned` join, `None` where the partition is empty. A key is only - /// ever built into the partition its key routes to, so each probe row is - /// looked up in that partition's map alone: no `CASE` over per-partition - /// filters, and no merged map. - Routed { - router: PartitionRouter, - maps: Arc<[Option>]>, - }, -} - -/// Assigns probe rows to the build partition their key was repartitioned to. -/// -/// # Public Only for Internal Use: -/// Public only because [`HashTableLookupExpr::new`] takes it; not part of the -/// supported public API. -#[doc(hidden)] -#[derive(Clone)] -pub enum PartitionRouter { - /// `hash(keys) % partitions`, as hash repartitioning routes rows. - Hash(SeededRandomState), - /// The probe side's range partitioning. - Range(Arc), -} - -impl PartitionRouter { - /// Writes the partition of each row of `keys` to `partition_ids`, which - /// holds one slot per row. - fn route( - &self, - keys: &[ArrayRef], - partitions: usize, - partition_ids: &mut Vec, - ) -> Result<()> { - match self { - PartitionRouter::Hash(random_state) => { - partition_ids.resize(keys[0].len(), 0); - create_hashes(keys, random_state.random_state(), partition_ids)?; - let partitions = partitions as u64; - for id in partition_ids.iter_mut() { - *id %= partitions; - } - Ok(()) - } - PartitionRouter::Range(range) => { - range.route_partition_ids(keys, partition_ids) - } - } - } - - fn seed(&self) -> Option { - match self { - PartitionRouter::Hash(random_state) => Some(random_state.seed()), - PartitionRouter::Range(_) => None, - } - } -} - -impl HashTableLookupExpr { - /// Create a new HashTableLookupExpr that looks each probe row up in the - /// map of the build partition `router` assigns it to. - /// - /// # Arguments - /// * `on_columns` - Columns in the ON clause used to compute the join key - /// * `random_state` - SeededRandomState for hashing - /// * `router` - Routes each probe row to the partition its key was - /// repartitioned to; `None` when `maps` is a single map - /// * `maps` - One entry per build partition, `None` for an empty partition - /// * `description` - Description for debugging - /// * `pruning_bitmap` - key-range summary covering every partition's keys - /// for pruning only, or `None` - /// - /// When `maps` has exactly one entry, that map is probed directly without - /// routing and `router` is ignored. Otherwise `router` is required, even if - /// only one entry is non-empty. - /// - /// # Public Only for Internal Use: - /// `datafusion-proto` tests require this constructor, but it is not part of - /// the supported public API. - #[doc(hidden)] - pub fn new( - on_columns: Vec, - random_state: SeededRandomState, - router: Option, - maps: Vec>>, - description: String, - pruning_bitmap: Option>, - ) -> Result { - let maps = maps - .into_iter() - .map(|map| map.map(ReservedMap::unreserved).map(Arc::new)) - .collect(); - Self::new_reserved( - on_columns, - random_state, - router, - maps, - description, - pruning_bitmap, - ) - } - - /// [`Self::new`] for a join's maps, whose memory stays reserved while the - /// lookup holds them. - pub(crate) fn new_reserved( - on_columns: Vec, - random_state: SeededRandomState, - router: Option, - maps: Vec>>, - description: String, - pruning_bitmap: Option>, - ) -> Result { - let lookup = match (router, maps.as_slice()) { - // Every row routes to the only partition, so skip routing. - (_, [Some(map)]) => Lookup::Single(Arc::clone(map)), - (None, _) => { - return internal_err!( - "a lookup over anything but one non-empty map needs a partition router" - ); - } - (Some(router), _) => Lookup::Routed { - router, - maps: maps.into(), - }, - }; - Ok(Self { - on_columns, - random_state, - lookup, - description, - pruning_bitmap, - }) - } - - /// Which parts of this build side's key range hold keys, for container pruning. - pub fn pruning_bitmap(&self) -> Option<&Arc> { - self.pruning_bitmap.as_ref() - } - - /// Membership of each row of `keys` in the map of its routed partition. - fn evaluate_routed( - &self, - keys: &[ArrayRef], - router: &PartitionRouter, - maps: &[Option>], - ) -> Result { - let num_rows = keys[0].len(); - let mut partition_ids = Vec::with_capacity(num_rows); - router.route(keys, maps.len(), &mut partition_ids)?; - - let (starts, rows) = sort_rows_by_partition(&partition_ids, maps.len()); - let probe_array_map = |map: &ArrayMap, run: Range| { - // An array map is keyed by value, not hash, so it probes the - // run's own key values. - let indices = UInt32Array::from(rows[run].to_vec()); - map.contain_keys(&take_arrays(keys, &indices, None)?) - }; - - // `Some` only when every map is an array map. Only a hash map probes - // by hash, so this path skips hashing the keys. - let array_maps: Option>> = maps - .iter() - .map(|map| match map.as_deref().map(|map| -> &Map { map }) { - None => Some(None), - Some(Map::ArrayMap(map)) => Some(Some(map)), - Some(Map::HashMap(_)) => None, - }) - .collect(); - if let Some(array_maps) = array_maps { - return probe_runs(&array_maps, &starts, &rows, |map, run| { - probe_array_map(map, run) - }); - } - - with_hashes(keys, self.random_state.random_state(), |hashes| { - let sorted_hashes: Vec = - rows.iter().map(|&row| hashes[row as usize]).collect(); - let maps: Vec> = maps - .iter() - .map(|map| map.as_deref().map(|map| -> &Map { map })) - .collect(); - probe_runs(&maps, &starts, &rows, |map, run| match map { - Map::HashMap(map) => Ok(map.contain_hashes(&sorted_hashes[run])), - Map::ArrayMap(map) => probe_array_map(map, run), - }) - }) - } -} - -/// Counting sort of the row indices by partition, so each table is probed once -/// per batch over a contiguous run of its rows. -/// -/// Returns `(starts, rows)`. The rows of partition `p` are -/// `rows[starts[p]..starts[p + 1]]`. -fn sort_rows_by_partition( - partition_ids: &[u64], - num_partitions: usize, -) -> (Vec, Vec) { - let mut starts = vec![0usize; num_partitions + 1]; - for &id in partition_ids { - starts[id as usize + 1] += 1; - } - for i in 0..num_partitions { - starts[i + 1] += starts[i]; - } - let mut next = starts.clone(); - let mut rows = vec![0u32; partition_ids.len()]; - for (row, &id) in partition_ids.iter().enumerate() { - let slot = &mut next[id as usize]; - rows[*slot] = row as u32; - *slot += 1; - } - (starts, rows) -} - -/// Probes each partition's map over its run of sorted rows and scatters the -/// hits back to the original row order. -/// -/// `probe` returns one membership bit per row of the run it receives. -fn probe_runs( - maps: &[Option], - starts: &[usize], - rows: &[u32], - mut probe: impl FnMut(M, Range) -> Result, -) -> Result { - let mut matched = BooleanBufferBuilder::new(rows.len()); - matched.append_n(rows.len(), false); - for (partition, map) in maps.iter().enumerate() { - // An empty partition holds no key, so its rows stay unmatched. - let Some(map) = *map else { continue }; - let run = starts[partition]..starts[partition + 1]; - if run.is_empty() { - continue; - } - let hits = probe(map, run.clone())?; - for hit in hits.values().set_indices() { - matched.set_bit(rows[run.start + hit] as usize, true); - } - } - Ok(BooleanArray::new(matched.finish(), None)) -} - -impl std::fmt::Debug for HashTableLookupExpr { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let cols = self - .on_columns - .iter() - .map(|e| e.to_string()) - .collect::>() - .join(", "); - let seed = self.random_state.seed(); - match &self.lookup { - Lookup::Single(_) => write!(f, "{}({cols}, [{seed}])", self.description), - Lookup::Routed { router, maps } => { - let router = match router.seed() { - Some(routing_seed) => format!("hash [{routing_seed}]"), - None => "range".to_string(), - }; - write!( - f, - "{}({cols}, [{seed}], routed by {router} over {} partitions)", - self.description, - maps.len() - ) - } - } - } -} - -impl Hash for HashTableLookupExpr { - fn hash(&self, state: &mut H) { - self.on_columns.dyn_hash(state); - self.description.hash(state); - self.random_state.seed().hash(state); - // Hash immutable map identity, consistently with pointer equality below. - self.lookup.identity().hash(state); - } -} - -impl PartialEq for HashTableLookupExpr { - fn eq(&self, other: &Self) -> bool { - // Sharing an immutable map implies equal contents, including across - // prepared-build consumers. Distinct maps need no expensive deep comparison. - self.on_columns == other.on_columns - && self.description == other.description - && self.random_state.seed() == other.random_state.seed() - && self.lookup.identity() == other.lookup.identity() - } -} - -impl Eq for HashTableLookupExpr {} - -impl Lookup { - /// Identity of the immutable build-side maps. A routed lookup's router is - /// created together with its maps, so the maps identify it as well. - fn identity(&self) -> *const () { - match self { - Lookup::Single(map) => std::ptr::from_ref::(map).cast(), - Lookup::Routed { maps, .. } => Arc::as_ptr(maps).cast(), - } - } -} - -impl Display for HashTableLookupExpr { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.description) - } -} - -impl PhysicalExpr for HashTableLookupExpr { - fn children(&self) -> Vec<&Arc> { - self.on_columns.iter().collect() - } - - fn with_new_children( - self: Arc, - children: Vec>, - ) -> Result> { - Ok(Arc::new(HashTableLookupExpr { - on_columns: children, - random_state: self.random_state.clone(), - lookup: self.lookup.clone(), - description: self.description.clone(), - pruning_bitmap: self.pruning_bitmap.clone(), - })) - } - - fn data_type(&self, _input_schema: &Schema) -> Result { - Ok(DataType::Boolean) - } - - fn nullable(&self, _input_schema: &Schema) -> Result { - Ok(false) - } - - fn evaluate(&self, batch: &RecordBatch) -> Result { - // Evaluate columns - let join_keys = evaluate_columns(&self.on_columns, batch)?; - - match &self.lookup { - Lookup::Single(map) => { - let map: &Map = map; - match map { - Map::HashMap(map) => with_hashes( - &join_keys, - self.random_state.random_state(), - |hashes| { - let array = map.contain_hashes(hashes); - Ok(ColumnarValue::Array(Arc::new(array))) - }, - ), - Map::ArrayMap(map) => { - let array = map.contain_keys(&join_keys)?; - Ok(ColumnarValue::Array(Arc::new(array))) - } - } - } - Lookup::Routed { router, maps } => { - let array = self.evaluate_routed(&join_keys, router, maps)?; - Ok(ColumnarValue::Array(Arc::new(array))) - } - } - } - #[cfg(feature = "proto")] - fn try_to_proto( - &self, - _ctx: &datafusion_physical_expr_common::physical_expr::proto_encode::PhysicalExprEncodeCtx<'_>, - ) -> Result> { - use datafusion_proto_models::protobuf; - use datafusion_proto_models::protobuf::physical_expr_node::ExprType; - - // Destructure exhaustively (no `..`) so that a newly added field is a - // compile error here, forcing a decision about whether the lit(true) - // replacement below is still the right thing to emit. - let Self { - // deliberately not serialized: the whole expression is replaced - // with lit(true), see the comment below - on_columns: _, - random_state: _, - lookup: _, - description: _, - pruning_bitmap: _, - } = self; - - // HashTableLookupExpr holds a runtime Arc (the build-side hash - // table) that cannot be serialized, so it is replaced with lit(true). - // - // Dynamic filtering is a performance optimisation only — replacing the - // lookup with lit(true) preserves correctness by allowing all rows - // through. - // - // If a plan is serialized before execution, HashTableLookupExpr is not - // yet present in the dynamic filter expression. - // - // If a plan is serialized after execution, any runtime-created - // HashTableLookupExpr is replaced during serialization. Re-executing - // the plan requires reset_state(), after which HashJoinExec rebuilds - // fresh dynamic filters at runtime. - let value = datafusion_proto_common::ScalarValue { - value: Some(datafusion_proto_common::scalar_value::Value::BoolValue( - true, - )), - }; - Ok(Some(protobuf::PhysicalExprNode { - expr_id: None, - expr_type: Some(ExprType::Literal(value)), - })) - } - fn fmt_sql(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.description) - } -} - fn evaluate_columns( columns: &[PhysicalExprRef], batch: &RecordBatch, @@ -708,10 +269,6 @@ fn evaluate_columns( #[cfg(test)] mod tests { use super::*; - use crate::joins::array_map::ArrayMap; - use crate::joins::join_hash_map::JoinHashMapU32; - use arrow::array::AsArray; - use datafusion_common::ScalarValue; use datafusion_physical_expr::expressions::Column; use std::collections::hash_map::DefaultHasher; use std::hash::Hasher; @@ -722,42 +279,6 @@ mod tests { hasher.finish() } - /// The pruning bitmap must survive `with_new_children`, which a scan calls - /// once per file. - #[test] - fn test_pruning_bitmap_shared_with_derived_children() -> Result<()> { - let keys: ArrayRef = Arc::new(arrow::array::Int64Array::from(vec![0, 1_000_000])); - let bitmap = Arc::new( - KeyRangeBitmap::try_new( - &keys, - &ScalarValue::Int64(Some(0)), - &ScalarValue::Int64(Some(1_000_000)), - 2, - ) - .ok_or_else(|| { - datafusion_common::internal_datafusion_err!( - "two keys a million apart leave prunable gaps" - ) - })?, - ); - let expr = Arc::new(HashTableLookupExpr::new( - vec![Arc::new(Column::new("a", 0))], - SeededRandomState::with_seed(1), - None, - vec![Some(Arc::new(Map::HashMap(Box::new( - JoinHashMapU32::with_capacity(10), - ))))], - "hash_lookup".to_string(), - Some(Arc::clone(&bitmap)), - )?); - - let derived = expr.with_new_children(vec![Arc::new(Column::new("a", 7))])?; - let derived = derived.downcast_ref::().unwrap(); - assert_eq!(derived.children()[0].to_string(), "a@7"); - assert!(Arc::ptr_eq(&bitmap, derived.pruning_bitmap().unwrap())); - Ok(()) - } - #[test] fn test_hash_expr_eq_same() { let col_a: PhysicalExprRef = Arc::new(Column::new("a", 0)); @@ -899,7 +420,9 @@ mod tests { Some(protobuf::physical_expr_node::ExprType::Column(_)) => { Column::try_from_proto(node, &ctx) } - _ => internal_err!("test decoder cannot decode {node:?}"), + _ => datafusion_common::internal_err!( + "test decoder cannot decode {node:?}" + ), } } } @@ -1024,344 +547,4 @@ mod tests { ); } } - - #[test] - fn test_hash_table_lookup_expr_eq_same() -> Result<()> { - let col_a: PhysicalExprRef = Arc::new(Column::new("a", 0)); - let hash_map = - Arc::new(Map::HashMap(Box::new(JoinHashMapU32::with_capacity(10)))); - - let expr1 = HashTableLookupExpr::new( - vec![Arc::clone(&col_a)], - SeededRandomState::with_seed(1), - None, - vec![Some(Arc::clone(&hash_map))], - "lookup".to_string(), - None, - )?; - - let expr2 = HashTableLookupExpr::new( - vec![Arc::clone(&col_a)], - SeededRandomState::with_seed(1), - None, - vec![Some(Arc::clone(&hash_map))], - "lookup".to_string(), - None, - )?; - - assert_eq!(expr1, expr2); - Ok(()) - } - - #[test] - fn test_hash_table_lookup_expr_eq_different_columns() -> Result<()> { - let col_a: PhysicalExprRef = Arc::new(Column::new("a", 0)); - let col_b: PhysicalExprRef = Arc::new(Column::new("b", 1)); - - let hash_map = - Arc::new(Map::HashMap(Box::new(JoinHashMapU32::with_capacity(10)))); - - let expr1 = HashTableLookupExpr::new( - vec![Arc::clone(&col_a)], - SeededRandomState::with_seed(1), - None, - vec![Some(Arc::clone(&hash_map))], - "lookup".to_string(), - None, - )?; - - let expr2 = HashTableLookupExpr::new( - vec![Arc::clone(&col_b)], - SeededRandomState::with_seed(1), - None, - vec![Some(Arc::clone(&hash_map))], - "lookup".to_string(), - None, - )?; - - assert_ne!(expr1, expr2); - Ok(()) - } - - #[test] - fn test_hash_table_lookup_expr_eq_different_description() -> Result<()> { - let col_a: PhysicalExprRef = Arc::new(Column::new("a", 0)); - let hash_map = - Arc::new(Map::HashMap(Box::new(JoinHashMapU32::with_capacity(10)))); - - let expr1 = HashTableLookupExpr::new( - vec![Arc::clone(&col_a)], - SeededRandomState::with_seed(1), - None, - vec![Some(Arc::clone(&hash_map))], - "lookup_one".to_string(), - None, - )?; - - let expr2 = HashTableLookupExpr::new( - vec![Arc::clone(&col_a)], - SeededRandomState::with_seed(1), - None, - vec![Some(Arc::clone(&hash_map))], - "lookup_two".to_string(), - None, - )?; - - assert_ne!(expr1, expr2); - Ok(()) - } - - #[test] - fn test_hash_table_lookup_expr_eq_different_hash_map() -> Result<()> { - let col_a: PhysicalExprRef = Arc::new(Column::new("a", 0)); - - // Two different Arc pointers (even with same content) should not be equal - let hash_map1 = - Arc::new(Map::HashMap(Box::new(JoinHashMapU32::with_capacity(10)))); - let hash_map2 = - Arc::new(Map::HashMap(Box::new(JoinHashMapU32::with_capacity(10)))); - let expr1 = HashTableLookupExpr::new( - vec![Arc::clone(&col_a)], - SeededRandomState::with_seed(1), - None, - vec![Some(hash_map1)], - "lookup".to_string(), - None, - )?; - - let expr2 = HashTableLookupExpr::new( - vec![Arc::clone(&col_a)], - SeededRandomState::with_seed(1), - None, - vec![Some(hash_map2)], - "lookup".to_string(), - None, - )?; - - // Different Arc pointers means not equal (uses Arc::ptr_eq) - assert_ne!(expr1, expr2); - Ok(()) - } - - #[test] - fn test_hash_table_lookup_expr_hash_consistency() -> Result<()> { - let col_a: PhysicalExprRef = Arc::new(Column::new("a", 0)); - let hash_map = - Arc::new(Map::HashMap(Box::new(JoinHashMapU32::with_capacity(10)))); - - let expr1 = HashTableLookupExpr::new( - vec![Arc::clone(&col_a)], - SeededRandomState::with_seed(1), - None, - vec![Some(Arc::clone(&hash_map))], - "lookup".to_string(), - None, - )?; - - let expr2 = HashTableLookupExpr::new( - vec![Arc::clone(&col_a)], - SeededRandomState::with_seed(1), - None, - vec![Some(Arc::clone(&hash_map))], - "lookup".to_string(), - None, - )?; - - // Equal expressions should have equal hashes - assert_eq!(expr1, expr2); - assert_eq!(compute_hash(&expr1), compute_hash(&expr2)); - Ok(()) - } - - /// Probes `probe` against `expr`, one row per key of column `a`. - fn lookup(expr: &HashTableLookupExpr, probe: &ArrayRef) -> Result { - let schema = Arc::new(Schema::new(vec![arrow::datatypes::Field::new( - "a", - DataType::Int32, - true, - )])); - let batch = RecordBatch::try_new(schema, vec![Arc::clone(probe)])?; - let matched = expr.evaluate(&batch)?.into_array(batch.num_rows())?; - Ok(matched.as_boolean().clone()) - } - - /// A hash table of `keys`, hashed with `random_state` as the build side - /// hashes them. - fn hash_table(keys: &ArrayRef, random_state: &SeededRandomState) -> Result> { - use crate::joins::join_hash_map::JoinHashMapType; - let mut hashes = vec![0; keys.len()]; - create_hashes([keys], random_state.random_state(), &mut hashes)?; - let mut map = JoinHashMapU32::with_capacity(keys.len()); - map.update_from_iter(Box::new(hashes.iter().enumerate()), 0); - Ok(Arc::new(Map::HashMap(Box::new(map)))) - } - - /// An array map of `keys`, as a dense build side gets. - fn array_map(keys: &ArrayRef) -> Result> { - let values = keys.as_primitive::(); - let key = |value: Option| { - ArrayMap::key_to_u64(&ScalarValue::Int32(value)).expect("Int32 key") - }; - let min = key(arrow::compute::min(values)); - let max = key(arrow::compute::max(values)); - Ok(Arc::new(Map::ArrayMap(ArrayMap::try_new(keys, min, max)?))) - } - - /// With every partition built as an array map, the routed lookup skips - /// hashing and still matches each probe row against its own partition. - #[test] - fn routed_lookup_all_array_maps() -> Result<()> { - use crate::repartition::{BatchPartitioner, REPARTITION_RANDOM_STATE}; - - let column: PhysicalExprRef = Arc::new(Column::new("a", 0)); - let schema = Arc::new(Schema::new(vec![arrow::datatypes::Field::new( - "a", - DataType::Int32, - false, - )])); - let build: ArrayRef = - Arc::new(arrow::array::Int32Array::from(vec![1, 2, 3, 5, 8, 13, 21])); - let num_partitions = 4; - let mut maps = vec![None; num_partitions]; - BatchPartitioner::new_hash_partitioner( - vec![Arc::clone(&column)], - num_partitions, - crate::metrics::Time::new(), - )? - .partition( - RecordBatch::try_new(schema, vec![build])?, - |partition, batch| { - maps[partition] = Some(array_map(batch.column(0))?); - Ok(()) - }, - )?; - let routed = HashTableLookupExpr::new( - vec![column], - SeededRandomState::with_seed(1), - Some(PartitionRouter::Hash(REPARTITION_RANDOM_STATE)), - maps, - "routed".to_string(), - None, - )?; - - let probe: ArrayRef = Arc::new(arrow::array::Int32Array::from(vec![ - Some(0), - Some(1), - None, - Some(4), - Some(5), - Some(13), - Some(20), - Some(21), - ])); - assert_eq!( - lookup(&routed, &probe)?, - BooleanArray::from(vec![false, true, false, false, true, true, false, true]) - ); - Ok(()) - } - - /// One randomly generated scenario, see [`routed_lookup_fuzz`]. - fn run_routed_lookup_scenario(seed: u64) -> Result<()> { - use crate::repartition::{BatchPartitioner, REPARTITION_RANDOM_STATE}; - use rand::rngs::StdRng; - use rand::{Rng, SeedableRng}; - - let mut rng = StdRng::seed_from_u64(seed); - let num_partitions = rng.random_range(1..8usize); - // A key domain near the row counts, so probes both hit and miss. - let build: Vec = (0..rng.random_range(0..128)) - .map(|_| rng.random_range(-64..64)) - .collect(); - let probe: Vec> = (0..rng.random_range(0..128)) - .map(|_| rng.random_bool(0.9).then(|| rng.random_range(-80..80))) - .collect(); - let context = format!( - "seed {seed}: {num_partitions} partitions, {} build rows, {} probe rows", - build.len(), - probe.len() - ); - - let column: PhysicalExprRef = Arc::new(Column::new("a", 0)); - let schema = Arc::new(Schema::new(vec![arrow::datatypes::Field::new( - "a", - DataType::Int32, - false, - )])); - let build: ArrayRef = Arc::new(arrow::array::Int32Array::from(build)); - let probe: ArrayRef = Arc::new(arrow::array::Int32Array::from(probe)); - let random_state = SeededRandomState::with_seed(1); - - // Split the build side exactly as hash repartitioning does. - let mut maps = vec![None; num_partitions]; - BatchPartitioner::new_hash_partitioner( - vec![Arc::clone(&column)], - num_partitions, - crate::metrics::Time::new(), - )? - .partition( - RecordBatch::try_new(schema, vec![Arc::clone(&build)])?, - |partition, batch| { - // Half the partitions build an array map, as dense keys would. Under - // forced collisions a hash table matches every key, so an array - // map would disagree with the single-table reference below. - let dense = - cfg!(not(feature = "force_hash_collisions")) && rng.random_bool(0.5); - maps[partition] = Some(if dense { - array_map(batch.column(0))? - } else { - hash_table(batch.column(0), &random_state)? - }); - Ok(()) - }, - )?; - let routed = HashTableLookupExpr::new( - vec![Arc::clone(&column)], - random_state.clone(), - Some(PartitionRouter::Hash(REPARTITION_RANDOM_STATE)), - maps, - "routed".to_string(), - None, - )?; - // Reference: the single-table lookup over the whole build side. - let single = HashTableLookupExpr::new( - vec![column], - random_state.clone(), - None, - vec![Some(hash_table(&build, &random_state)?)], - "single".to_string(), - None, - )?; - - assert_eq!( - lookup(&routed, &probe)?, - lookup(&single, &probe)?, - "{context}" - ); - Ok(()) - } - - /// Routing each probe row to its build partition's table matches one table - /// over the whole build side, when the build side is split the way hash - /// repartitioning splits it. - /// - /// `DATAFUSION_ROUTED_LOOKUP_FUZZ_ITERATIONS` and - /// `DATAFUSION_ROUTED_LOOKUP_FUZZ_SEED` select how many scenarios run and - /// from which seed; a failure names its seed so it can be replayed. - #[test] - fn routed_lookup_fuzz() -> Result<()> { - let env_u64 = |name: &str, default: u64| { - std::env::var(name) - .ok() - .and_then(|v| v.parse().ok()) - .unwrap_or(default) - }; - let first_seed = env_u64("DATAFUSION_ROUTED_LOOKUP_FUZZ_SEED", 0); - let iterations = env_u64("DATAFUSION_ROUTED_LOOKUP_FUZZ_ITERATIONS", 256); - // `saturating_add` so a replay seed near `u64::MAX` cannot overflow. - for seed in first_seed..first_seed.saturating_add(iterations) { - run_routed_lookup_scenario(seed)?; - } - Ok(()) - } } diff --git a/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs b/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs index 8c6409a865b50..a81af507c4f4a 100644 --- a/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs +++ b/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs @@ -11,16 +11,10 @@ use std::sync::Arc; use crate::ExecutionPlan; use crate::ExecutionPlanProperties; -use crate::Partitioning; use crate::joins::PartitionMode; -use crate::joins::ReservedMap; use crate::joins::hash_join::bloom_filter::{BloomFilterExpr, BloomFilterOptions}; -use crate::joins::hash_join::exec::HASH_JOIN_SEED; use crate::joins::hash_join::inlist_builder::build_struct_fields; use crate::joins::hash_join::not_in::NotIn; -use crate::joins::hash_join::partitioned_hash_eval::{ - HashTableLookupExpr, SeededRandomState, -}; use crate::joins::key_range_bitmap::{KeyRangeBitmap, KeyRangePart}; use arrow::array::{Array, ArrayRef, UInt32Array}; use arrow::datatypes::{DataType, Field, Schema}; @@ -36,9 +30,7 @@ use datafusion_functions::core::r#struct as struct_func; use datafusion_physical_expr::expressions::{ BinaryExpr, DynamicFilterPhysicalExpr, InListExpr, IsNullExpr, lit, }; -use datafusion_physical_expr::{ - PhysicalExpr, PhysicalExprRef, RangePartitioning, ScalarFunctionExpr, -}; +use datafusion_physical_expr::{PhysicalExpr, PhysicalExprRef, ScalarFunctionExpr}; use parquet::bloom_filter::Sbbf; use parking_lot::Mutex; @@ -184,69 +176,43 @@ impl Default for GlobalFilterOptions { } } -/// Creates a membership predicate for filter pushdown. -/// -/// If `inlist_values` is provided (for small build sides), creates an InList expression. -/// Otherwise, creates a HashTableLookup expression (for large build sides). -/// -/// Supports both single-column and multi-column joins using struct expressions. -fn create_membership_predicate( +/// Creates `key IN (values)` for a single key, or +/// `struct(keys) IN (values)` for several keys. +fn create_in_list_predicate( on_right: &[PhysicalExprRef], - pushdown: PushdownStrategy, - random_state: &SeededRandomState, + in_list_array: ArrayRef, schema: &Schema, -) -> Result>> { - match pushdown { - // Use InList expression for small build sides - PushdownStrategy::InList(in_list_array) => { - // Build the expression to compare against - let expr = if on_right.len() == 1 { - // Single column: col IN (val1, val2, ...) - Arc::clone(&on_right[0]) - } else { - let fields = build_struct_fields( - on_right - .iter() - .map(|r| r.data_type(schema)) - .collect::>>()? - .as_ref(), - )?; - - // The return field name and the function field name don't really matter here. - let return_field = - Arc::new(Field::new("struct", DataType::Struct(fields), true)); - - Arc::new(ScalarFunctionExpr::new( - "struct", - struct_func(), - on_right.to_vec(), - return_field, - Arc::new(ConfigOptions::default()), - )) as Arc - }; +) -> Result> { + let expr = if on_right.len() == 1 { + Arc::clone(&on_right[0]) + } else { + let fields = build_struct_fields( + on_right + .iter() + .map(|r| r.data_type(schema)) + .collect::>>()? + .as_ref(), + )?; + + // The return field name and the function field name don't really matter here. + let return_field = Arc::new(Field::new("struct", DataType::Struct(fields), true)); + + Arc::new(ScalarFunctionExpr::new( + "struct", + struct_func(), + on_right.to_vec(), + return_field, + Arc::new(ConfigOptions::default()), + )) as Arc + }; - // Use InListExpr::try_new_from_array() to build an InList with static_filter optimization (hash-based lookup) - Ok(Some(Arc::new(InListExpr::try_new_from_array( - expr, - in_list_array, - false, - schema, - )?))) - } - // Use hash table lookup for large build sides - PushdownStrategy::Map(hash_map, pruning_bitmap) => { - Ok(Some(Arc::new(HashTableLookupExpr::new_reserved( - on_right.to_vec(), - random_state.clone(), - None, - vec![Some(hash_map)], - "hash_lookup".to_string(), - pruning_bitmap, - )?) as Arc)) - } - // Empty partition - should not create a filter for this - PushdownStrategy::Empty => Ok(None), - } + // `try_new_from_array` builds the InList with its hash-based static filter. + Ok(Arc::new(InListExpr::try_new_from_array( + expr, + in_list_array, + false, + schema, + )?)) } /// Creates a bounds predicate from partition bounds. @@ -295,8 +261,7 @@ fn create_bounds_predicate( /// Combines a membership predicate and a bounds predicate with logical AND. /// -/// Returns `None` when neither is available; callers decide the fallback (e.g. -/// skip updating the filter or use `lit(true)`). +/// Returns `None` when neither is available. fn combine_membership_and_bounds( membership_expr: Option>, bounds_expr: Option>, @@ -314,14 +279,14 @@ fn combine_membership_and_bounds( /// Coordinates build-side information collection across multiple partitions /// -/// This structure collects information from the build side (hash tables and/or bounds) and +/// This structure collects information from the build side (keys and bounds) and /// ensures that dynamic filters are built with complete information from all relevant /// partitions before being applied to probe-side scans. Incomplete filters would /// incorrectly eliminate valid join results. /// /// ## Synchronization Strategy /// -/// 1. Each partition computes information from its build-side data (hash maps and/or bounds) +/// 1. Each partition computes information from its build-side data (keys and bounds) /// 2. Information is stored in the shared state, which tracks how many partitions have reported /// 3. When the last partition reports, one waiter is elected as the finalizer; it merges the /// collected information, updates the dynamic filter exactly once, and publishes the @@ -329,14 +294,12 @@ fn combine_membership_and_bounds( /// 4. A [`tokio::sync::Notify`] wakes any other partitions parked in `wait_for_completion`, /// which then observe the `Ready` state under the mutex and return immediately /// -/// ## Hash Map vs Bounds +/// ## Filter /// -/// - **Global filter (default)**: merges the bounds and keys of all partitions -/// into one unrouted filter: global min/max bounds AND an InList (few keys) or -/// a bloom filter of all build keys. See `build_global_filter`. -/// - **Routed filter (fallback)**: when a partition was canceled or the key type -/// has no bloom filter support, a `CASE` on the repartition hash (or range) -/// routes each probe row to its partition's bounds and InList / hash table. +/// The filter merges the bounds and keys of all partitions into one global +/// filter: global min/max bounds AND an InList (few keys) or a bloom filter of +/// the build keys. See `build_global_filter`. A canceled partition contributes +/// no key. /// /// ## Partition Counting /// @@ -362,22 +325,15 @@ pub(crate) struct SharedBuildAccumulator { dynamic_filter: Arc, /// Right side join expressions needed for creating filter expressions on_right: Vec, - /// Random state for partitioning (RepartitionExec's hash function with 0,0,0,0 seeds) - /// Used for PartitionedHashLookupPhysicalExpr - #[expect(dead_code)] - repartition_random_state: SeededRandomState, /// Schema of the probe (right) side for evaluating filter expressions probe_schema: Arc, - /// Probe-side Range routing metadata for partitioned dynamic filters. - #[cfg_attr(not(test), expect(dead_code))] - probe_range_partitioning: Option, /// Null equality of the join. Under `NullEqualsNull` a probe-side NULL can match a /// build-side NULL, so the pushed filter must keep NULL rows here too. null_equality: NullEquality, /// Null-aware anti join (`NOT IN`). A probe-side NULL must reach the join so its /// three-valued logic can collapse the result, so the pushed filter keeps NULL rows. not_in: Option, - /// Options for the global (unrouted) filter + /// Options for the global filter global_filter_options: GlobalFilterOptions, } @@ -386,9 +342,10 @@ pub(crate) struct SharedBuildAccumulator { pub(crate) enum PushdownStrategy { /// Use InList for small build sides (< 128MB) InList(ArrayRef), - /// Use map lookup for large build sides. If `Some` the second field - /// represents the build side's keys using a bucket bitmap. - Map(Arc, Option>), + /// The build side has too many keys for an InList. The global filter + /// uses the bloom filter of [`BuildKeys`]. `Some` holds a bucket bitmap of + /// the keys, for pruning. + Large(Option>), /// There was no data in this partition, do not build a dynamic filter for it Empty, } @@ -503,7 +460,6 @@ impl SharedBuildAccumulator { right_child: &dyn ExecutionPlan, dynamic_filter: Arc, on_right: Vec, - repartition_random_state: SeededRandomState, null_equality: NullEquality, not_in: Option, global_filter_options: GlobalFilterOptions, @@ -543,14 +499,6 @@ impl SharedBuildAccumulator { ), }; - let probe_range_partitioning = - match (partition_mode, right_child.output_partitioning()) { - (PartitionMode::Partitioned, Partitioning::Range(range)) => { - Some(range.clone()) - } - _ => None, - }; - Self { inner: Mutex::new(AccumulatorState { data: mode_data, @@ -559,9 +507,7 @@ impl SharedBuildAccumulator { completion_notify: Notify::new(), dynamic_filter, on_right, - repartition_random_state, probe_schema: right_child.schema(), - probe_range_partitioning, null_equality, not_in, global_filter_options, @@ -572,11 +518,10 @@ impl SharedBuildAccumulator { /// /// This unified method handles both CollectLeft and Partitioned modes. When all partitions /// have reported (barrier wait), the leader builds the appropriate filter expression: - /// - CollectLeft: Simple conjunction of bounds and membership check - /// - Partitioned: one lookup that routes each probe row to its partition's keys + /// the global filter of all partitions that are not canceled. /// /// # Arguments - /// * `data` - Build data including hash map, pushdown strategy, and bounds + /// * `data` - Build data including keys, pushdown strategy, and bounds /// /// # Returns /// * `Result<()>` - Ok if successful, Err if filter update failed or mode mismatch @@ -758,38 +703,15 @@ impl SharedBuildAccumulator { /// Builds the single global filter used by a collect-left join. fn build_collect_left_filter(&self, partition: PartitionStatus) -> Result<()> { - // An empty build side keeps the filter unchanged, as before. - if let PartitionStatus::Reported(data) = &partition - && !matches!(data.pushdown, PushdownStrategy::Empty) - { - return self - .dynamic_filter - .update(self.build_global_filter(&[data])?); - } match partition { + // An empty build side keeps the filter unchanged. PartitionStatus::Reported(PartitionData { - bounds, - pushdown, - keys_have_null, - build_keys: _, - }) => { - let membership_expr = create_membership_predicate( - &self.on_right, - pushdown, - &HASH_JOIN_SEED, - self.probe_schema.as_ref(), - )?; - let bounds_expr = create_bounds_predicate(&self.on_right, &bounds); - - if let Some(filter_expr) = - combine_membership_and_bounds(membership_expr, bounds_expr) - { - self.dynamic_filter.update( - self.preserve_probe_nulls(filter_expr, keys_have_null)?, - )?; - } - Ok(()) - } + pushdown: PushdownStrategy::Empty, + .. + }) => Ok(()), + PartitionStatus::Reported(data) => self + .dynamic_filter + .update(self.build_global_filter(&[&data])?), PartitionStatus::Pending => datafusion_common::internal_err!( "attempted to finalize collect-left dynamic filter without reported build data" ), @@ -799,7 +721,8 @@ impl SharedBuildAccumulator { } } - /// Builds one probe-side filter from finalized partitioned build data. + /// Builds the global filter from finalized partitioned build data. + /// /// Empty and canceled partitions contribute no key. fn build_partitioned_filter(&self, partitions: &[PartitionStatus]) -> Result<()> { let mut reported = Vec::with_capacity(partitions.len()); @@ -868,12 +791,11 @@ impl SharedBuildAccumulator { let arrays: Vec<&dyn Array> = in_lists.iter().map(|a| a.as_ref()).collect(); let values = distinct_values(&arrow::compute::concat(&arrays)?)?; - create_membership_predicate( + Some(create_in_list_predicate( &self.on_right, - PushdownStrategy::InList(values), - &HASH_JOIN_SEED, + values, self.probe_schema.as_ref(), - )? + )?) } _ => { let pruning_bitmap = @@ -926,7 +848,7 @@ impl SharedBuildAccumulator { let parts = partitions .iter() .map(|p| match &p.pushdown { - PushdownStrategy::Map(_, Some(bitmap)) => { + PushdownStrategy::Large(Some(bitmap)) => { Some(KeyRangePart::Bitmap(bitmap.as_ref())) } _ => p @@ -1031,9 +953,7 @@ pub(super) fn make_partitioned_accumulator_for_test( completion_notify: Notify::new(), dynamic_filter, on_right: vec![], - repartition_random_state: SeededRandomState::with_seed(1), probe_schema, - probe_range_partitioning: None, null_equality: NullEquality::NullEqualsNothing, not_in: None, global_filter_options: GlobalFilterOptions::default(), @@ -1056,7 +976,6 @@ pub(super) fn completed_partitions_for_test(acc: &SharedBuildAccumulator) -> usi #[cfg(test)] mod tests { use super::*; - use crate::joins::Map; use crate::joins::hash_join::not_in::{NotInOutput, NotInScope}; /// Adds the bloom filter of `build_keys.keys`, sized for 4 times its @@ -1078,11 +997,7 @@ mod tests { use arrow::array::{ArrayRef, BooleanArray, Float64Array, Int32Array}; use arrow::record_batch::RecordBatch; - use datafusion_common::SplitPoint; - use datafusion_physical_expr::{ - PhysicalSortExpr, - expressions::{Column, Literal}, - }; + use datafusion_physical_expr::expressions::{Column, Literal}; fn test_on_right() -> Vec { vec![Arc::new(Column::new("probe_key", 0))] @@ -1115,9 +1030,7 @@ mod tests { completion_notify: Notify::new(), dynamic_filter, on_right, - repartition_random_state: SeededRandomState::with_seed(1), probe_schema: test_probe_schema(), - probe_range_partitioning: None, null_equality: NullEquality::NullEqualsNothing, not_in: None, global_filter_options: GlobalFilterOptions::default(), @@ -1240,17 +1153,12 @@ mod tests { (partitions.clone(), *completed_partitions) } - /// A partition that reports a hash table (more keys than the InList limit). + /// A partition with more keys than the InList limit. fn reported_map(keys: Vec) -> PartitionStatus { let (min, max) = (*keys.iter().min().unwrap(), *keys.iter().max().unwrap()); let num_distinct = keys.len(); PartitionStatus::Reported(PartitionData { - pushdown: PushdownStrategy::Map( - Arc::new(ReservedMap::unreserved(Arc::new(Map::HashMap(Box::new( - crate::joins::join_hash_map::JoinHashMapU32::with_capacity(0), - ))))), - None, - ), + pushdown: PushdownStrategy::Large(None), bounds: bounds(min, max), keys_have_null: false, build_keys: test_build_keys(BuildKeys { @@ -1408,46 +1316,47 @@ mod tests { ); } - /// Hash and range partitioning both use the same global filter: it holds every - /// build key, so it does not depend on the routing. + /// A canceled partition does not widen the filter. #[test] - fn range_partitioned_uses_global_filter() -> Result<()> { - let mut acc = make_partitioned_expr_accumulator_for_test(2); - acc.probe_range_partitioning = Some(RangePartitioning::try_new( - [PhysicalSortExpr::new( - Arc::clone(&acc.on_right[0]), - Default::default(), - )] - .into(), - vec![SplitPoint::new(vec![ScalarValue::Int32(Some(1000))])], - )?); + fn partitioned_canceled_partition_does_not_widen_filter() { + let acc = make_partitioned_expr_accumulator_for_test(3); acc.build_filter(FinalizeInput::Partitioned(vec![ - reported_map((0..500).map(|k| k * 2).collect()), - reported_map((0..500).map(|k| 1000 + k * 2).collect()), - ]))?; + reported_map((0..1000).collect()), + PartitionStatus::CanceledUnknown, + reported(PushdownStrategy::Empty, no_bounds()), + ])) + .unwrap(); let expr = current_expr(&acc); - assert!(!format!("{expr}").contains("CASE"), "{expr}"); - let result = eval_bool(&expr, (0..2000).collect()); - for k in (0..2000).step_by(2) { - assert!(result[k], "build key {k} dropped"); - } - Ok(()) + assert_eq!( + expr.to_string(), + "probe_key@0 >= 0 AND probe_key@0 <= 999 AND bloom_filter" + ); + assert_eq!(eval_bool(&expr, (0..1000).collect()), vec![true; 1000]); + assert_eq!(eval_bool(&expr, vec![-1, 1000]), vec![false, false]); } - /// A canceled partition's keys are unknown, so the global filter is not - /// possible: the routed filter keeps every row of the canceled route. - /// A canceled partition contributes no key, so the other partitions still - /// get the global bloom filter. + /// Only canceled partitions: no stream probes, so the filter rejects every row. #[test] - fn partitioned_canceled_partition_contributes_no_key() { + fn partitioned_all_canceled_partitions_set_filter_to_false() { let acc = make_partitioned_expr_accumulator_for_test(2); acc.build_filter(FinalizeInput::Partitioned(vec![ - reported_map((0..1000).collect()), + PartitionStatus::CanceledUnknown, PartitionStatus::CanceledUnknown, ])) .unwrap(); - let expr = current_expr(&acc); - assert!(format!("{expr}").contains("bloom_filter"), "{expr}"); + assert_eq!(current_expr(&acc).to_string(), "false"); + } + + /// All partitions empty: no probe row can match. + #[test] + fn partitioned_all_empty_partitions_set_filter_to_false() { + let acc = make_partitioned_expr_accumulator_for_test(2); + acc.build_filter(FinalizeInput::Partitioned(vec![ + reported(PushdownStrategy::Empty, no_bounds()), + reported(PushdownStrategy::Empty, no_bounds()), + ])) + .unwrap(); + assert_eq!(current_expr(&acc).to_string(), "false"); } /// Float keys get the global bloom filter too. @@ -1461,12 +1370,7 @@ mod tests { )])); let float_partition = |keys: Vec| { PartitionStatus::Reported(PartitionData { - pushdown: PushdownStrategy::Map( - Arc::new(ReservedMap::unreserved(Arc::new(Map::HashMap(Box::new( - crate::joins::join_hash_map::JoinHashMapU32::with_capacity(0), - ))))), - None, - ), + pushdown: PushdownStrategy::Large(None), bounds: no_bounds(), keys_have_null: false, build_keys: test_build_keys(BuildKeys { @@ -1566,20 +1470,6 @@ mod tests { assert_in_list_column_values(&expr, "probe_key", 0, &[1, 2, 3]); } - #[test] - fn collect_left_updates_with_bounds_only() { - let acc = make_collect_left_accumulator_for_test(); - - acc.build_filter(FinalizeInput::CollectLeft(reported( - PushdownStrategy::Empty, - bounds(10, 20), - ))) - .unwrap(); - - let expr = current_expr(&acc); - assert_top_binary_op(&expr, Operator::And); - } - #[test] fn collect_left_empty_build_data_does_not_update_filter() { let acc = make_collect_left_accumulator_for_test(); @@ -1601,7 +1491,7 @@ mod tests { } #[test] - fn partitioned_one_real_partition_with_rest_empty_keeps_its_in_list() { + fn partitioned_one_real_partition_with_rest_empty_uses_its_in_list() { let acc = make_partitioned_expr_accumulator_for_test(3); acc.build_filter(FinalizeInput::Partitioned(vec![ @@ -1612,20 +1502,7 @@ mod tests { .unwrap(); let expr = current_expr(&acc); - in_list_expr(&expr); - } - - #[test] - fn partitioned_canceled_and_empty_partitions_reject_every_row() { - let acc = make_partitioned_expr_accumulator_for_test(2); - - acc.build_filter(FinalizeInput::Partitioned(vec![ - PartitionStatus::CanceledUnknown, - reported(PushdownStrategy::Empty, no_bounds()), - ])) - .unwrap(); - - assert_literal_bool(¤t_expr(&acc), false); + assert_in_list_column_values(&expr, "probe_key", 0, &[2]); } // Regression guard for the build-report lifecycle fix: on `Drop`, a stream @@ -1687,6 +1564,37 @@ mod tests { assert_eq!(completed, 1); } + /// A partition that waits for the filter completes when another partition + /// is canceled, and the filter holds the keys of the waiting partition. + #[tokio::test] + async fn canceled_partition_completes_waiting_partition() { + let acc = make_partitioned_expr_accumulator_for_test(2); + let report = acc.report_build_data(PartitionBuildData::Partitioned { + partition_id: 0, + pushdown: in_list(&[1, 2]), + bounds: bounds(1, 2), + keys_have_null: false, + build_keys: BuildKeys::default(), + }); + let mut report = std::pin::pin!(report); + let mut cx = std::task::Context::from_waker(futures::task::noop_waker_ref()); + assert!(report.as_mut().poll(&mut cx).is_pending()); + + acc.report_canceled_partition(1); + + tokio::time::timeout(std::time::Duration::from_secs(5), report) + .await + .expect("the waiting partition must complete") + .unwrap(); + assert_eq!( + current_expr(&acc).to_string(), + "probe_key@0 >= 1 AND probe_key@0 <= 2 AND probe_key@0 IN (SET) ([1, 2])" + ); + assert!( + futures::poll!(std::pin::pin!(acc.dynamic_filter.wait_complete())).is_ready() + ); + } + fn null_semantics_accumulator( probe_schema: Arc, on_right: Vec, @@ -1704,9 +1612,7 @@ mod tests { completion_notify: Notify::new(), dynamic_filter: Arc::new(DynamicFilterPhysicalExpr::new(vec![], lit(true))), on_right, - repartition_random_state: SeededRandomState::with_seed(1), probe_schema, - probe_range_partitioning: None, null_equality, not_in: null_aware.then_some(NotIn::BuildRows { scope: NotInScope::Uncorrelated, @@ -1784,20 +1690,22 @@ mod tests { assert_eq!(format!("{expr}").matches("IS NULL").count(), 1); } - /// A canceled partition holds no key, so it cannot add a NULL key that - /// would make a null-equal join keep probe-side NULL rows. + /// In a null-equal join, the NULL keys of a live partition still widen the + /// filter when another partition is canceled. #[test] - fn partitioned_canceled_partition_does_not_widen_filter() { + fn partitioned_canceled_partition_keeps_null_keys_of_live_partition() { let acc = null_equal_partitioned_accumulator(2); acc.build_filter(FinalizeInput::Partitioned(vec![ PartitionStatus::CanceledUnknown, - reported(in_list(&[2]), no_bounds()), + reported_with_null_keys(in_list(&[2]), no_bounds()), ])) .unwrap(); - let expr = current_expr(&acc); - assert_eq!(format!("{expr}").matches("IS NULL").count(), 0, "{expr}"); + assert_eq!( + current_expr(&acc).to_string(), + "probe_key@0 IS NULL OR probe_key@0 IN (SET) ([2])" + ); } #[test] @@ -1910,9 +1818,7 @@ mod tests { completion_notify: Notify::new(), dynamic_filter, on_right, - repartition_random_state: SeededRandomState::with_seed(1), probe_schema: Arc::new(Schema::new(fields)), - probe_range_partitioning: None, null_equality: NullEquality::NullEqualsNothing, not_in: Some(NotIn::BuildRows { scope: NotInScope::Correlated { value_keys }, @@ -1932,7 +1838,7 @@ mod tests { .collect(), ); acc.build_filter(FinalizeInput::CollectLeft(reported( - PushdownStrategy::Empty, + PushdownStrategy::Large(None), bounds, ))) .unwrap(); diff --git a/datafusion/physical-plan/src/joins/mod.rs b/datafusion/physical-plan/src/joins/mod.rs index 64aec6b8b16ea..dfd7b299b8dd1 100644 --- a/datafusion/physical-plan/src/joins/mod.rs +++ b/datafusion/physical-plan/src/joins/mod.rs @@ -12,7 +12,7 @@ use std::sync::Arc; use arrow::array::BooleanBufferBuilder; pub use asof_join::{AsOfJoinExec, AsOfMatchExpr}; pub use cross_join::CrossJoinExec; -use datafusion_common::{Result, internal_err}; +use datafusion_common::Result; use datafusion_execution::memory_pool::MemoryReservation; use datafusion_physical_expr::PhysicalExprRef; /// # Public Only for Internal Use: @@ -21,7 +21,7 @@ use datafusion_physical_expr::PhysicalExprRef; /// /// [API policy]: https://datafusion.apache.org/contributor-guide/api-health.html#internal-public-apis #[doc(hidden)] -pub use hash_join::{BloomFilterExpr, HashTableLookupExpr}; +pub use hash_join::BloomFilterExpr; pub use hash_join::{ HashExpr, HashJoinExec, HashJoinExecBuilder, PreparedHashJoinBuild, SeededRandomState, }; @@ -98,16 +98,13 @@ impl Map { } } -/// A [`Map`] and the reservation for its memory, which the dynamic filter -/// shares for as long as the plan lives. Held together, the memory stays -/// reserved until the last holder drops the map. +/// A [`Map`] and the reservation for its memory. Held together, the memory +/// stays reserved until the last holder drops the map. pub(crate) struct ReservedMap { - /// Shared, as [`HashTableLookupExpr::new`] takes one and compares by it. map: Arc, /// Also charged for data kept with the map, such as the pruning bitmap. - /// `None` for a map built outside a join; see [`HashTableLookupExpr::new`]. // Keep the reservation after the map it accounts for. - reservation: Option, + reservation: MemoryReservation, } impl ReservedMap { @@ -115,28 +112,17 @@ impl ReservedMap { pub(crate) fn new(map: Map, reservation: MemoryReservation) -> Self { Self { map: Arc::new(map), - reservation: Some(reservation), - } - } - - /// Wraps a map that was not charged to a pool. - pub(crate) fn unreserved(map: Arc) -> Self { - Self { - map, - reservation: None, + reservation, } } /// Charges `bytes` for data kept with the map. pub(crate) fn try_grow(&self, bytes: usize) -> Result<()> { - match &self.reservation { - Some(reservation) => reservation.try_grow(bytes), - None => internal_err!("ReservedMap has no reservation to grow"), - } + self.reservation.try_grow(bytes) } pub(crate) fn reserved_bytes(&self) -> usize { - self.reservation.as_ref().map_or(0, MemoryReservation::size) + self.reservation.size() } } diff --git a/datafusion/physical-plan/src/repartition/mod.rs b/datafusion/physical-plan/src/repartition/mod.rs index 98c70093a9daf..ad394193c6c36 100644 --- a/datafusion/physical-plan/src/repartition/mod.rs +++ b/datafusion/physical-plan/src/repartition/mod.rs @@ -783,20 +783,6 @@ impl RangeExpr { pub fn sort_options(&self) -> &[SortOptions] { self.router.sort_options() } - - /// Appends the partition of each row of `arrays`, the evaluated - /// `on_columns`, to `partition_ids`. - pub(crate) fn route_partition_ids( - &self, - arrays: &[arrow::array::ArrayRef], - partition_ids: &mut Vec, - ) -> Result<()> { - if self.router.split_points().is_empty() { - partition_ids.resize(partition_ids.len() + arrays[0].len(), 0); - return Ok(()); - } - self.router.route_partition_ids(arrays, partition_ids) - } } impl Display for RangeExpr { diff --git a/datafusion/proto/tests/cases/plans/exprs.rs b/datafusion/proto/tests/cases/plans/exprs.rs index 8c193283ad917..386d6c8764312 100644 --- a/datafusion/proto/tests/cases/plans/exprs.rs +++ b/datafusion/proto/tests/cases/plans/exprs.rs @@ -426,58 +426,6 @@ fn roundtrip_like() -> Result<()> { roundtrip_test(plan) } -/// Test that HashTableLookupExpr serializes to lit(true) -/// -/// HashTableLookupExpr contains a runtime hash table that cannot be serialized. -/// The serialization code replaces it with lit(true) which is safe because -/// it's a performance optimization filter, not a correctness requirement. -#[test] -fn roundtrip_hash_table_lookup_expr_to_lit() -> Result<()> { - use datafusion::physical_plan::joins::join_hash_map::JoinHashMapU32; - use datafusion::physical_plan::joins::{HashTableLookupExpr, Map}; - - // Create a simple schema and input plan - let schema = Arc::new(Schema::new(vec![Field::new("col", DataType::Int64, false)])); - let input = Arc::new(EmptyExec::new(schema.clone())); - - // Create a HashTableLookupExpr - it will be replaced with lit(true) during serialization - let hash_map = Arc::new(Map::HashMap(Box::new(JoinHashMapU32::with_capacity(0)))); - let on_columns = vec![col("col", &schema)?]; - let lookup_expr: Arc = Arc::new(HashTableLookupExpr::new( - on_columns, - datafusion::physical_plan::joins::SeededRandomState::with_seed(0), - None, - vec![Some(hash_map)], - "test_lookup".to_string(), - None, - )?); - - // Create a filter with the lookup expression - let filter = Arc::new(FilterExec::try_new(lookup_expr, input)?); - - // Serialize - let ctx = SessionContext::new(); - let codec = DefaultPhysicalExtensionCodec {}; - - let proto: PhysicalPlanNode = - PhysicalPlanNode::try_from_physical_plan(filter.clone(), &codec) - .expect("serialization should succeed"); - - // Deserialize - let result: Arc = proto - .try_into_physical_plan(&ctx.task_ctx(), &codec) - .expect("deserialization should succeed"); - - // The deserialized plan should have lit(true) instead of HashTableLookupExpr - // Verify the filter predicate is a Literal(true) - let result_filter = result.downcast_ref::().unwrap(); - let predicate = result_filter.predicate(); - let literal = predicate.downcast_ref::().unwrap(); - assert_eq!(*literal.value(), ScalarValue::Boolean(Some(true))); - - Ok(()) -} - #[test] fn roundtrip_hash_expr() -> Result<()> { use datafusion::physical_plan::joins::{HashExpr, SeededRandomState}; diff --git a/datafusion/pruning/Cargo.toml b/datafusion/pruning/Cargo.toml index 0d6e6e49c01d0..08b72f5932a1c 100644 --- a/datafusion/pruning/Cargo.toml +++ b/datafusion/pruning/Cargo.toml @@ -35,7 +35,6 @@ datafusion-expr = { workspace = true } datafusion-functions-nested = { workspace = true } insta = { workspace = true } itertools = { workspace = true } -rstest = { workspace = true } [[bench]] harness = false diff --git a/datafusion/pruning/src/pruning_predicate.rs b/datafusion/pruning/src/pruning_predicate.rs index 82de276873c11..612d89c9410e8 100644 --- a/datafusion/pruning/src/pruning_predicate.rs +++ b/datafusion/pruning/src/pruning_predicate.rs @@ -38,8 +38,8 @@ use datafusion_expr_common::operator::Operator; use datafusion_physical_expr::utils::{Guarantee, LiteralGuarantee}; use datafusion_physical_expr::{PhysicalExprRef, expressions as phys_expr}; use datafusion_physical_expr_common::physical_expr::snapshot_physical_expr_opt; +use datafusion_physical_plan::joins::BloomFilterExpr; use datafusion_physical_plan::joins::key_range_bitmap::KeyRangeBitmap; -use datafusion_physical_plan::joins::{BloomFilterExpr, HashTableLookupExpr}; use datafusion_physical_plan::{ColumnarValue, PhysicalExpr}; /// Used to prove that arbitrary predicates (boolean expression) can not @@ -1531,12 +1531,11 @@ fn min_max_non_null_exprs( statistics } -/// Builds a "may match" expression for a [`HashTableLookupExpr`] or a -/// [`BloomFilterExpr`] (a large join build side pushed down as an opaque -/// membership test) with key-range bitmap `bitmap`: tests container -/// min/max stats against the build side's key-range bitmap, which is fixed size -/// and built once with the join's hash table. -fn build_hash_lookup_pruning_expr( +/// Builds a "may match" expression for a [`BloomFilterExpr`] (a large join +/// build side pushed down as an opaque membership test) with key-range bitmap +/// `bitmap`: tests container min/max stats against the build side's key-range +/// bitmap, which is fixed size and built once with the join's build side. +fn build_key_range_bitmap_pruning_expr( bitmap: &Arc, on_columns: &[&Arc], schema: &Schema, @@ -1850,24 +1849,11 @@ fn build_predicate_expression( return unhandled_hook.handle(expr); } } - if let Some(lookup) = expr.downcast_ref::() { - return lookup - .pruning_bitmap() - .and_then(|bitmap| { - build_hash_lookup_pruning_expr( - bitmap, - &lookup.children(), - schema, - required_columns, - ) - }) - .unwrap_or_else(|| unhandled_hook.handle(expr)); - } if let Some(bloom) = expr.downcast_ref::() { return bloom .pruning_bitmap() .and_then(|bitmap| { - build_hash_lookup_pruning_expr( + build_key_range_bitmap_pruning_expr( bitmap, &bloom.children(), schema, @@ -1876,10 +1862,9 @@ fn build_predicate_expression( }) .unwrap_or_else(|| unhandled_hook.handle(expr)); } - // A partitioned hash join hides its per-partition filters under a `CASE` on the - // repartition hash. A row takes exactly one branch, so a container may match - // only if some branch may: the branches' disjunction is a sound relaxation, and - // the `WHEN`s (a hash, which no statistics describe) can be dropped. + // A row takes exactly one branch of a `CASE`, so a container may match only + // if some branch may: the branches' disjunction is a sound relaxation, and + // the `WHEN`s can be dropped. if let Some(case) = expr.downcast_ref::() { // Only a Boolean `CASE` is a predicate; anything else is a value for // whatever compares it to handle. @@ -2502,11 +2487,7 @@ mod tests { self as phys_expr, DynamicFilterPhysicalExpr, }; use datafusion_physical_expr::planner::logical2physical; - use datafusion_physical_plan::joins::join_hash_map::JoinHashMapU32; - use datafusion_physical_plan::joins::key_range_bitmap::KeyRangeBitmap; - use datafusion_physical_plan::joins::{Map, SeededRandomState}; use itertools::Itertools; - use rstest::rstest; #[derive(Debug, Default)] /// Mock statistic provider for tests @@ -7570,129 +7551,34 @@ mod tests { assert_eq!(res.to_string(), expected); } - /// A hash-table lookup over `column` carrying a bitmap over `keys`. - fn hash_lookup( - column: &Arc, - keys: &[i64], - ) -> Result> { - let array: ArrayRef = Arc::new(Int64Array::from(keys.to_vec())); - let bitmap = KeyRangeBitmap::try_new( - &array, - &ScalarValue::Int64(Some(*keys.iter().min().unwrap())), - &ScalarValue::Int64(Some(*keys.iter().max().unwrap())), - keys.len(), - ); - Ok(Arc::new(HashTableLookupExpr::new( - vec![Arc::clone(column)], - SeededRandomState::with_seed(1), - None, - vec![Some(Arc::new(Map::HashMap(Box::new( - JoinHashMapU32::with_capacity(1), - ))))], - "hash_lookup".to_string(), - bitmap.map(Arc::new), - )?)) - } - #[test] - fn test_hash_lookup_pruning_via_min_max() -> Result<()> { - let schema = Arc::new(Schema::new(vec![Field::new("b", DataType::Int64, true)])); - let column: Arc = Arc::new(phys_expr::Column::new("b", 0)); - // Gapped keys: 10,20,30 then a far-away 100000, so the bitmap is sparse. - let lookup = hash_lookup(&column, &[10, 20, 30, 100_000])?; - - let predicate = PruningPredicateBuilder::new() - .with_file_schema(Arc::clone(&schema)) - .try_build(lookup)?; - - // An opaque lookup yields no literal guarantees, so `contained()` is never - // consulted and every exclusion below comes from the min/max rewrite. - assert!(predicate.literal_guarantees().is_empty()); - - let statistics = TestStatistics::new().with( - "b", - ContainerStats::new_i64( - vec![Some(5000), Some(15), Some(50000)], - vec![Some(8000), Some(25), Some(60000)], - ), - ); - - let result = predicate.prune(&statistics)?; - // Containers 0 and 2 sit in the gap between 30 and 100000; container 1 - // holds 20. The bitmap excludes the first and third. - assert_eq!(result, vec![false, true, false]); - Ok(()) - } - - #[rstest] - #[case::widening_i32_to_i64(DataType::Int32, DataType::Int64, true)] - #[case::widening_u32_to_i64(DataType::UInt32, DataType::Int64, true)] - #[case::widening_u16_to_i64(DataType::UInt16, DataType::Int64, true)] - #[case::same_type_i64_to_i64(DataType::Int64, DataType::Int64, true)] - #[case::narrowing_i64_to_i32(DataType::Int64, DataType::Int32, false)] - #[case::narrowing_u32_to_i16(DataType::UInt32, DataType::Int16, false)] - fn test_hash_lookup_pruning_through_cast( - #[case] file_type: DataType, - #[case] cast_type: DataType, - #[case] pruned: bool, - ) -> Result<()> { - let schema = - Arc::new(Schema::new(vec![Field::new("b", file_type.clone(), true)])); - let column: Arc = Arc::new(phys_expr::Column::new("b", 0)); - let cast_column: Arc = - Arc::new(phys_expr::CastExpr::new(column, cast_type, None)); - let lookup = hash_lookup(&cast_column, &[10, 20, 30, 100_000])?; - - let predicate = PruningPredicateBuilder::new() - .with_file_schema(Arc::clone(&schema)) - .try_build(lookup)?; - - let to_file_type = |values: Vec>| -> ArrayRef { - let values: ArrayRef = Arc::new(values.into_iter().collect::()); - arrow::compute::cast(&values, &file_type).unwrap() - }; - let statistics = TestStatistics::new().with( - "b", - ContainerStats::new() - .with_min(to_file_type(vec![Some(5000), Some(15), Some(50000)])) - .with_max(to_file_type(vec![Some(8000), Some(25), Some(60000)])), - ); - - let result = predicate.prune(&statistics)?; - // Containers 0 and 2 sit in the gap between 30 and 100000; container 1 - // holds 20. A working cast excludes the first and third; a declined one - // falls back to keeping every container. - let expected = if pruned { - vec![false, true, false] - } else { - vec![true, true, true] - }; - assert_eq!(result, expected); - Ok(()) - } - - #[test] - fn test_partition_routed_hash_lookup_pruning() -> Result<()> { + fn test_case_pruning_uses_disjunction_of_branches() -> Result<()> { let schema = Arc::new(Schema::new(vec![Field::new("b", DataType::Int32, true)])); let column: Arc = Arc::new(phys_expr::Column::new("b", 0)); let literal = |value: ScalarValue| -> Arc { Arc::new(phys_expr::Literal::new(value)) }; - // One branch per build partition, each holding its own slice of the build - // side, as `build_partitioned_filter` produces them. + let between = |min: i32, max: i32| -> Arc { + Arc::new(phys_expr::BinaryExpr::new( + Arc::new(phys_expr::BinaryExpr::new( + Arc::clone(&column), + Operator::GtEq, + literal(ScalarValue::Int32(Some(min))), + )), + Operator::And, + Arc::new(phys_expr::BinaryExpr::new( + Arc::clone(&column), + Operator::LtEq, + literal(ScalarValue::Int32(Some(max))), + )), + )) + }; let case: Arc = Arc::new(phys_expr::CaseExpr::try_new( Some(literal(ScalarValue::UInt64(Some(0)))), vec![ - ( - literal(ScalarValue::UInt64(Some(0))), - hash_lookup(&column, &[10, 20, 30])?, - ), - ( - literal(ScalarValue::UInt64(Some(1))), - hash_lookup(&column, &[40, 50, 60])?, - ), + (literal(ScalarValue::UInt64(Some(0))), between(18, 22)), + (literal(ScalarValue::UInt64(Some(1))), between(48, 52)), ], - // Partitions with no build rows reject everything routed to them. Some(literal(ScalarValue::Boolean(Some(false)))), )?); @@ -7709,9 +7595,9 @@ mod tests { ); let result = predicate.prune(&statistics)?; - // Container 1 ([15,25]) holds 20 from the first branch and container 2 - // ([45,55]) holds 50 from the second, so a branch may match in each. The - // other two intersect neither branch, nor the `ELSE`. + // Container 1 ([15,25]) overlaps the first branch and container 2 + // ([45,55]) overlaps the second. The other two overlap neither branch, + // nor the `ELSE`. assert_eq!(result, vec![false, true, true, false]); Ok(()) } diff --git a/datafusion/sqllogictest/test_files/dynamic_row_group_pruning.slt b/datafusion/sqllogictest/test_files/dynamic_row_group_pruning.slt index 7d9bc58bb49d0..7fe2c7bf2122a 100644 --- a/datafusion/sqllogictest/test_files/dynamic_row_group_pruning.slt +++ b/datafusion/sqllogictest/test_files/dynamic_row_group_pruning.slt @@ -404,7 +404,7 @@ statement ok RESET datafusion.explain.analyze_level; # Partitioned hash join with filter pushdown off. The dynamic filter is one -# global filter (no `CASE hash_repartition % N` routing). The probe scan uses it +# global filter. The probe scan uses it # only for statistics pruning, so the bloom filter is never built, but the # key-range bitmap merged from all build partitions prunes row groups 1 and 2, # which the [5, 390] bounds alone cannot prune. diff --git a/docs/source/contributor-guide/api-health.md b/docs/source/contributor-guide/api-health.md index d26f25abaeb34..ecd78df99f33a 100644 --- a/docs/source/contributor-guide/api-health.md +++ b/docs/source/contributor-guide/api-health.md @@ -54,7 +54,7 @@ For APIs intended only for internal use, add `#[doc(hidden)]` and use the following doc comment format, including the API policy link: ```txt -impl HashTableLookupExpr { +impl MyInternalExpr { /// ... /// /// # Public Only for Internal Use: diff --git a/xtask/src/ci_steps.rs b/xtask/src/ci_steps.rs index 54c56daf56e27..436065ed73fe0 100644 --- a/xtask/src/ci_steps.rs +++ b/xtask/src/ci_steps.rs @@ -15,9 +15,6 @@ type StepRunner = fn(&StepContext, &[String]) -> StepResult; const CI_COMMAND: &str = "cargo xtask ci step"; const CI_SHORTCUT: &str = "cargo ci-step"; const TPCH_DATA_DIR: &str = "datafusion/sqllogictest/test_files/tpch/data"; -/// Physical-plan fuzzers awaiting migration to the centralized driver. -const LEGACY_FUZZ_TESTS: &[&str] = - &["joins::hash_join::partitioned_hash_eval::tests::routed_lookup_fuzz"]; /// Metadata and implementation for one CI step. /// @@ -41,7 +38,7 @@ static CI_STEPS: &[StepInfo] = &[ }, StepInfo { command: "test", - help_usage: "test [--explain]", + help_usage: "test [--explain]", help_examples: &["test cli", "test workspace"], help_description: "Run a CI test suite", runner: StepContext::run_test, @@ -500,25 +497,6 @@ impl StepContext { ]) .env("RUST_BACKTRACE", "1"); } - "fuzz-legacy" => { - cmd_builder - .doc("Run unmigrated physical-plan fuzz tests with more iterations."); - cmd_builder - .program("cargo") - .args([ - "test", - "--profile", - "ci", - "-p", - "datafusion-physical-plan", - "--lib", - "--", - "--exact", - ]) - .args(LEGACY_FUZZ_TESTS) - .env("RUST_BACKTRACE", "1") - .env("DATAFUSION_ROUTED_LOOKUP_FUZZ_ITERATIONS", "10000"); - } "hash-collisions" => { cmd_builder.doc( "Check that results stay correct when hash values collide.\n\ @@ -598,7 +576,7 @@ mod tests { use super::*; use std::ffi::OsStr; - const TEST_USAGE: &str = "usage: cargo xtask ci step test [--explain]"; + const TEST_USAGE: &str = "usage: cargo xtask ci step test [--explain]"; fn args(args: &[&str]) -> Vec { args.iter().map(|arg| (*arg).to_string()).collect() @@ -647,27 +625,6 @@ mod tests { assert_eq!(error, TEST_USAGE); } - /// A filter that matches no test passes without running anything, so a - /// renamed or moved fuzzer would silently stop running in CI. - #[test] - fn legacy_fuzz_tests_exist() { - for path in LEGACY_FUZZ_TESTS { - let (module, test) = path - .rsplit_once("::tests::") - .expect("test path has a `tests` module"); - let file = Path::new(env!("CARGO_MANIFEST_DIR")) - .join("../datafusion/physical-plan/src") - .join(format!("{}.rs", module.replace("::", "/"))); - let source = std::fs::read_to_string(&file) - .unwrap_or_else(|e| panic!("cannot read {}: {e}", file.display())); - assert!( - source.contains("mod tests {") && source.contains(&format!("fn {test}(")), - "{} has no test `{test}` in `mod tests`; update LEGACY_FUZZ_TESTS", - file.display() - ); - } - } - /// Extended workflows explicitly select targets gated by Cargo's /// `required-features`. A test hidden behind a `cfg` in another target /// would never run in those workflows. @@ -1013,23 +970,6 @@ cargo nextest run \ --features avro,backtrace,extended_tests,parquet_encryption \ --no-fail-fast \ --status-level pass -") - }, - }, - Cmd { - command: &["test", "fuzz-legacy"], - expected: |actual| { - insta::assert_snapshot!(actual, @r" -# Run unmigrated physical-plan fuzz tests with more iterations. - -cd /workspace && \ -DATAFUSION_ROUTED_LOOKUP_FUZZ_ITERATIONS=10000 \ -RUST_BACKTRACE=1 \ -cargo test \ ---profile ci \ --p datafusion-physical-plan \ ---lib \ --- --exact joins::hash_join::partitioned_hash_eval::tests::routed_lookup_fuzz ") }, }, From 15402bfad8f6a6b620ac1184f5cac56ac89c89db Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Tue, 10 Nov 2026 00:28:08 -0700 Subject: [PATCH 3/9] refactor: allow the hash join dynamic filter for any input partitioning The routed filter checked a probe row only against the build keys of the partition that the router assigned the row to. It was correct only when the probe side was split the way the router assumed, so Partitioned joins needed co-partitioned Hash or Range inputs, and Hash inputs under `preserve_file_partitions` were rejected because file groups do not follow the hash router. The global filter holds the keys of every build partition whose stream can probe. It passes every row that a stream can join, whatever the partitioning. This commit removes `has_partitioned_dynamic_filter_routing` and the `preserve_file_partitions` Hash guard, and replaces the two partitioning tests with one that checks the filter is allowed for every partitioning. This enables dynamic filters for `preserve_file_partitions` scans (https://github.com/apache/datafusion/issues/20195). Co-Authored-By: Claude Opus 5.5 --- .../physical-plan/src/joins/hash_join/exec.rs | 110 +++++------------- 1 file changed, 26 insertions(+), 84 deletions(-) diff --git a/datafusion/physical-plan/src/joins/hash_join/exec.rs b/datafusion/physical-plan/src/joins/hash_join/exec.rs index 5b7df73d26add..b61402f3c09ca 100644 --- a/datafusion/physical-plan/src/joins/hash_join/exec.rs +++ b/datafusion/physical-plan/src/joins/hash_join/exec.rs @@ -1164,57 +1164,12 @@ impl HashJoinExec { return false; } - // Under `preserve_file_partitions`, file groups can report Hash without following - // the dynamic filter hash router and look like a real hash repartition, so reject - // Hash. Range inputs with matching split points are safe. - // Follow-up work: enable dynamic filtering for preserve_file_partitioned scans (issue #20195). - // https://github.com/apache/datafusion/issues/20195 - if config.optimizer.preserve_file_partitions > 0 - && self.mode == PartitionMode::Partitioned - && matches!( - ( - self.left.output_partitioning(), - self.right.output_partitioning() - ), - (Partitioning::Hash(_, _), Partitioning::Hash(_, _)) - ) - { - return false; - } - - if self.mode == PartitionMode::Partitioned - && !self.has_partitioned_dynamic_filter_routing() - { - return false; - } - + // The partitioning of the inputs does not matter. The filter holds the + // keys of every build partition whose stream can probe, so it passes + // every row that a stream can join. true } - fn has_partitioned_dynamic_filter_routing(&self) -> bool { - match ( - self.left.output_partitioning(), - self.right.output_partitioning(), - ) { - ( - Partitioning::Hash(_, left_partition_count), - Partitioning::Hash(_, right_partition_count), - ) => left_partition_count == right_partition_count, - (Partitioning::Range(_), Partitioning::Range(_)) => { - let children = [self.left.as_ref(), self.right.as_ref()]; - matches!( - self.input_distribution_requirements() - .unsatisfied_co_partitioned_children(self.name(), &children), - Ok(unsatisfied) if unsatisfied.is_empty() - ) - } - (left_partitioning, right_partitioning) => { - left_partitioning.partition_count() == 1 - && right_partitioning.partition_count() == 1 - } - } - } - /// left (build) side which gets hashed pub fn left(&self) -> &Arc { &self.left @@ -9480,7 +9435,7 @@ mod tests { &child_right_schema, )?) as _, )]; - let (child_join, _child_dynamic_filter) = hash_join_with_dynamic_filter_and_mode( + let (child_join, child_dynamic_filter) = hash_join_with_dynamic_filter_and_mode( child_left, child_right, child_on, @@ -9524,6 +9479,13 @@ mod tests { &batches ); + // The parent drops child partitions 1 and 3, so the filter holds only + // the keys of partitions 0 and 2. + assert_eq!( + child_dynamic_filter.current()?.to_string(), + "child_right_key@1 >= 0 AND child_right_key@1 <= 2 AND child_right_key@1 IN (SET) ([0, 2])" + ); + Ok(()) } @@ -12414,52 +12376,32 @@ mod tests { .build() } + /// The filter holds the keys of every build partition whose stream can + /// probe, so it does not depend on how the inputs are partitioned. #[test] - fn test_partitioned_dynamic_filter_pushdown_allows_supported_partitioning() - -> Result<()> { - let (range_join, on) = range_partitioned_dynamic_filter_test_join(10, 10)?; - let hash_join = with_hash_partitioned_children(&range_join, &on)?; - let mut session_config = SessionConfig::default(); - session_config - .options_mut() - .optimizer - .enable_join_dynamic_filter_pushdown = true; - - assert!(range_join.allow_join_dynamic_filter_pushdown(session_config.options())); - assert!(hash_join.allow_join_dynamic_filter_pushdown(session_config.options())); - - session_config - .options_mut() - .optimizer - .preserve_file_partitions = 1; - assert!(range_join.allow_join_dynamic_filter_pushdown(session_config.options())); - - Ok(()) - } - - #[test] - fn test_partitioned_dynamic_filter_pushdown_rejects_unsupported_partitioning() - -> Result<()> { + fn test_partitioned_dynamic_filter_pushdown_allows_any_partitioning() -> Result<()> { let (range_join, on) = range_partitioned_dynamic_filter_test_join(10, 10)?; let hash_join = with_hash_partitioned_children(&range_join, &on)?; let (mismatched_range_join, _) = range_partitioned_dynamic_filter_test_join(10, 11)?; + let joins = [&range_join, &hash_join, &mismatched_range_join]; let mut session_config = SessionConfig::default(); session_config .options_mut() .optimizer .enable_join_dynamic_filter_pushdown = true; - assert!( - !mismatched_range_join - .allow_join_dynamic_filter_pushdown(session_config.options()) - ); - - session_config - .options_mut() - .optimizer - .preserve_file_partitions = 1; - assert!(!hash_join.allow_join_dynamic_filter_pushdown(session_config.options())); + for preserve_file_partitions in [0, 1] { + session_config + .options_mut() + .optimizer + .preserve_file_partitions = preserve_file_partitions; + for join in joins { + assert!( + join.allow_join_dynamic_filter_pushdown(session_config.options()) + ); + } + } Ok(()) } From f117fc9bc72d8f808f07ea8c717dc7f738591859 Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Wed, 18 Nov 2026 11:03:48 -0600 Subject: [PATCH 4/9] perf: fill the hash join bloom filter from distinct keys only The build-side bloom fill hashed every build row and inserted each hash, although rows with the same key set the same bits. For a build of 60M rows with 100K distinct keys, that is 600x the needed inserts. The fill now inserts one hash per distinct key. A hash map build inserts the hash of each map entry, with no rehashing: the map hashes with `HASH_JOIN_SEED`, the seed the filter is probed with. An array map build hashes one build row per set slot. Co-Authored-By: Claude Opus 5.5 --- .../physical-plan/src/joins/array_map.rs | 26 ++++++++++++- .../src/joins/hash_join/bloom_filter.rs | 39 ++++++++++++++----- .../physical-plan/src/joins/hash_join/exec.rs | 38 ++++++++++++++---- .../physical-plan/src/joins/join_hash_map.rs | 11 ++++++ .../src/joins/stream_join_utils.rs | 4 ++ 5 files changed, 100 insertions(+), 18 deletions(-) diff --git a/datafusion/physical-plan/src/joins/array_map.rs b/datafusion/physical-plan/src/joins/array_map.rs index 932524705e9af..b9ab97e9801d8 100644 --- a/datafusion/physical-plan/src/joins/array_map.rs +++ b/datafusion/physical-plan/src/joins/array_map.rs @@ -9,7 +9,7 @@ use std::mem::size_of; use crate::joins::chain::traverse_chain; use crate::joins::join_hash_map::ProbeOffset; use crate::joins::join_hash_map::ProbeOffset::{AtRow, MidChain}; -use arrow::array::{Array, ArrayRef, AsArray, BooleanArray}; +use arrow::array::{Array, ArrayRef, AsArray, BooleanArray, UInt32Array}; use arrow::buffer::BooleanBuffer; use arrow::datatypes::ArrowNumericType; use datafusion_common::{Result, ScalarValue, internal_err}; @@ -223,6 +223,15 @@ impl ArrayMap { Ok(()) } + /// The index of one build row for each distinct key. + pub(crate) fn distinct_rows(&self) -> UInt32Array { + self.data + .iter() + .filter(|&&row| row != 0) + .map(|&row| row - 1) + .collect() + } + pub fn num_of_distinct_key(&self) -> usize { self.num_of_distinct_key } @@ -421,6 +430,21 @@ mod tests { use arrow::array::UInt64Array; use std::sync::Arc; + #[test] + fn distinct_rows_has_one_row_per_key() -> Result<()> { + let build: ArrayRef = Arc::new(Int32Array::from(vec![ + Some(3), + Some(1), + None, + Some(3), + Some(1), + ])); + let map = ArrayMap::try_new(&build, 1, 3)?; + // Each key keeps its first row, in key order. + assert_eq!(map.distinct_rows().values(), &[1, 0]); + Ok(()) + } + #[test] fn test_array_map_limit_offset_duplicate_elements() -> Result<()> { let build: ArrayRef = Arc::new(Int32Array::from(vec![1, 1, 2])); diff --git a/datafusion/physical-plan/src/joins/hash_join/bloom_filter.rs b/datafusion/physical-plan/src/joins/hash_join/bloom_filter.rs index 5d3e807428030..fb456294d8731 100644 --- a/datafusion/physical-plan/src/joins/hash_join/bloom_filter.rs +++ b/datafusion/physical-plan/src/joins/hash_join/bloom_filter.rs @@ -106,17 +106,10 @@ pub(crate) fn partition_filter( options: BloomFilterOptions, reservation: &MemoryReservation, ) -> Result> { - let Some(bytes) = options.bytes(num_distinct) else { + let Some(mut filter) = new_partition_filter(num_distinct, options, reservation) + else { return Ok(None); }; - // The size that `Sbbf::new_with_num_of_bytes` allocates - if reservation - .try_grow(bytes.max(32).next_power_of_two()) - .is_err() - { - return Ok(None); - } - let mut filter = Sbbf::new_with_num_of_bytes(bytes); for_each_hash_batch(keys, |hashes| { for &hash in hashes { filter.insert_hash(hash); @@ -126,6 +119,34 @@ pub(crate) fn partition_filter( Ok(Some(filter)) } +/// Like [`partition_filter`], but fills the filter from key hashes that the +/// caller already computed with `HASH_JOIN_SEED`. +pub(crate) fn partition_filter_from_hashes( + for_each_hash: impl FnOnce(&mut dyn FnMut(u64)), + num_distinct: usize, + options: BloomFilterOptions, + reservation: &MemoryReservation, +) -> Option { + let mut filter = new_partition_filter(num_distinct, options, reservation)?; + for_each_hash(&mut |hash| filter.insert_hash(hash)); + Some(filter) +} + +/// An empty filter sized for `num_distinct` keys, with its memory charged to +/// `reservation`. +fn new_partition_filter( + num_distinct: usize, + options: BloomFilterOptions, + reservation: &MemoryReservation, +) -> Option { + let bytes = options.bytes(num_distinct)?; + // The size that `Sbbf::new_with_num_of_bytes` allocates + reservation + .try_grow(bytes.max(32).next_power_of_two()) + .ok()?; + Some(Sbbf::new_with_num_of_bytes(bytes)) +} + /// The bloom filter of a [`BloomFilterExpr`] and the reservation for its /// memory, which is released when the filter is dropped. struct Filter { diff --git a/datafusion/physical-plan/src/joins/hash_join/exec.rs b/datafusion/physical-plan/src/joins/hash_join/exec.rs index b61402f3c09ca..2d3c456e6bc8b 100644 --- a/datafusion/physical-plan/src/joins/hash_join/exec.rs +++ b/datafusion/physical-plan/src/joins/hash_join/exec.rs @@ -67,7 +67,7 @@ use crate::{ use parquet::bloom_filter::Sbbf; use arrow::array::{Array, ArrayData, ArrayRef, BooleanBufferBuilder}; -use arrow::compute::concat_batches; +use arrow::compute::{concat_batches, take}; use arrow::datatypes::SchemaRef; use arrow::record_batch::RecordBatch; use arrow::util::bit_util; @@ -3541,13 +3541,35 @@ fn build_in_memory( { None } else { - let filter = bloom_filter::partition_filter( - &left_values, - map.num_of_distinct_key() - .saturating_mul(num_build_partitions), - GlobalFilterOptions::new(config).bloom_filter, - &reservation, - )?; + let num_distinct = map + .num_of_distinct_key() + .saturating_mul(num_build_partitions); + let options = GlobalFilterOptions::new(config).bloom_filter; + // Insert one hash per distinct key, not one per build row. + let filter = match &**map { + // The map hashes the keys with `random_state`, which is + // `HASH_JOIN_SEED`, the seed the filter is probed with. Keys whose + // hashes collide share an entry, and also share their filter bits. + Map::HashMap(hash_map) => bloom_filter::partition_filter_from_hashes( + |f| hash_map.for_each_entry_hash(f), + num_distinct, + options, + &reservation, + ), + Map::ArrayMap(array_map) => { + let rows = array_map.distinct_rows(); + let keys = left_values + .iter() + .map(|values| take(values.as_ref(), &rows, None)) + .collect::, _>>()?; + bloom_filter::partition_filter( + &keys, + num_distinct, + options, + &reservation, + )? + } + }; if let Some(filter) = &filter { metrics.build_mem_used.add(filter.num_blocks() * 32); } diff --git a/datafusion/physical-plan/src/joins/join_hash_map.rs b/datafusion/physical-plan/src/joins/join_hash_map.rs index 6fc3a363769f4..9c644509ccca6 100644 --- a/datafusion/physical-plan/src/joins/join_hash_map.rs +++ b/datafusion/physical-plan/src/joins/join_hash_map.rs @@ -152,6 +152,9 @@ pub trait JoinHashMapType: Send + Sync { /// Returns the number of entries in the join hash map. fn len(&self) -> usize; + + /// Calls `f` with the hash of each entry: one hash per distinct key hash. + fn for_each_entry_hash(&self, f: &mut dyn FnMut(u64)); } pub struct JoinHashMapU32 { @@ -186,6 +189,10 @@ impl Debug for JoinHashMapU32 { } impl JoinHashMapType for JoinHashMapU32 { + fn for_each_entry_hash(&self, f: &mut dyn FnMut(u64)) { + self.map.iter().for_each(|&(hash, _)| f(hash)); + } + fn extend_zero(&mut self, _: usize) {} fn update_from_iter<'a>( @@ -272,6 +279,10 @@ impl Debug for JoinHashMapU64 { } impl JoinHashMapType for JoinHashMapU64 { + fn for_each_entry_hash(&self, f: &mut dyn FnMut(u64)) { + self.map.iter().for_each(|&(hash, _)| f(hash)); + } + fn extend_zero(&mut self, _: usize) {} fn update_from_iter<'a>( diff --git a/datafusion/physical-plan/src/joins/stream_join_utils.rs b/datafusion/physical-plan/src/joins/stream_join_utils.rs index 7b0b236f6729d..4683e74d83003 100644 --- a/datafusion/physical-plan/src/joins/stream_join_utils.rs +++ b/datafusion/physical-plan/src/joins/stream_join_utils.rs @@ -42,6 +42,10 @@ use hashbrown::HashTable; /// Implementation of `JoinHashMapType` for `PruningJoinHashMap`. impl JoinHashMapType for PruningJoinHashMap { + fn for_each_entry_hash(&self, f: &mut dyn FnMut(u64)) { + self.map.iter().for_each(|&(hash, _)| f(hash)); + } + // Extend with zero fn extend_zero(&mut self, len: usize) { self.next.resize(self.next.len() + len, 0) From 39e606d990658034553a85ecc36eae93bb98e113 Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:59:07 -0600 Subject: [PATCH 5/9] test: tie the hash map bloom fill to the probe seed The hash map path fills the bloom filter from the hashes stored in the build map, and the filter is probed with HASH_JOIN_SEED. A build map with another seed would make the filter drop matching rows. The new test joins on string keys, which take the hash map path, and checks that the filter keeps every build key. Co-Authored-By: Claude Opus 5.5 --- .../physical-plan/src/joins/hash_join/exec.rs | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/datafusion/physical-plan/src/joins/hash_join/exec.rs b/datafusion/physical-plan/src/joins/hash_join/exec.rs index 2d3c456e6bc8b..0405f2bdc25e9 100644 --- a/datafusion/physical-plan/src/joins/hash_join/exec.rs +++ b/datafusion/physical-plan/src/joins/hash_join/exec.rs @@ -3902,6 +3902,46 @@ mod tests { Ok(()) } + /// A hash map build fills the bloom filter from the hashes stored in the + /// map, and the filter is probed with `HASH_JOIN_SEED`. String keys take + /// the hash map path, so this fails if the build map uses another seed. + #[tokio::test] + async fn bloom_filter_from_hash_map_keeps_every_build_key() -> Result<()> { + let schema = Arc::new(Schema::new(vec![Field::new("k", DataType::Utf8, false)])); + let keys: ArrayRef = Arc::new(StringArray::from_iter_values( + (0..1000).map(|i| format!("key-{i}")), + )); + let batch = RecordBatch::try_new(Arc::clone(&schema), vec![Arc::clone(&keys)])?; + let exec = || { + TestMemoryExec::try_new_exec( + &[vec![batch.clone()]], + Arc::clone(&schema), + None, + ) + }; + let on = vec![( + Arc::new(Column::new_with_schema("k", &schema)?) as _, + Arc::new(Column::new_with_schema("k", &schema)?) as _, + )]; + let (join, filter) = + hash_join_with_dynamic_filter(exec()?, exec()?, on, JoinType::Inner)?; + + let mut config = SessionConfig::new(); + config + .options_mut() + .optimizer + .hash_join_inlist_pushdown_max_size = 0; + let task_ctx = Arc::new(TaskContext::default().with_session_config(config)); + let batches = crate::execution_plan::collect(Arc::new(join), task_ctx).await?; + assert_eq!(batches.iter().map(|b| b.num_rows()).sum::(), 1000); + + let current = filter.current()?; + assert!(bloom_filter_bytes(¤t)? > 0); + let kept = current.evaluate(&batch)?.into_array(batch.num_rows())?; + assert_eq!(kept.as_boolean().true_count(), 1000); + Ok(()) + } + #[derive(Debug)] struct PartitionedTestExec { cache: Arc, From b31e2d9a70c605905f7718b2bd8420740dbc77ab Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Fri, 20 Nov 2026 15:03:33 -0600 Subject: [PATCH 6/9] fix: keep the global bloom filter for many small hash join partitions Each build partition sizes its bloom filter for the keys of all partitions, estimated as its own distinct count times the number of partitions. With few keys per partition the counts vary a lot: in TPC-H Q17 at 128 partitions they range from 4 to 30. The merge folds every filter into the smallest one, which then holds all 2044 keys at a false positive rate of 0.30, above max_fpp, so the join published no bloom filter at all and the scan under the aggregate kept 59.9M rows instead of 61K. Partitions with an IN list hold their keys. When every partition has one but together they exceed the IN list limit, the accumulator now builds one bloom filter of all IN list values, sized for the real distinct count. Co-Authored-By: Claude Opus 5.5 --- .../src/joins/hash_join/shared_bounds.rs | 143 +++++++++++++++--- 1 file changed, 125 insertions(+), 18 deletions(-) diff --git a/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs b/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs index a81af507c4f4a..25e79c66886ac 100644 --- a/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs +++ b/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs @@ -12,11 +12,11 @@ use std::sync::Arc; use crate::ExecutionPlan; use crate::ExecutionPlanProperties; use crate::joins::PartitionMode; -use crate::joins::hash_join::bloom_filter::{BloomFilterExpr, BloomFilterOptions}; +use crate::joins::hash_join::bloom_filter::{self, BloomFilterExpr, BloomFilterOptions}; use crate::joins::hash_join::inlist_builder::build_struct_fields; use crate::joins::hash_join::not_in::NotIn; use crate::joins::key_range_bitmap::{KeyRangeBitmap, KeyRangePart}; -use arrow::array::{Array, ArrayRef, UInt32Array}; +use arrow::array::{Array, ArrayRef, AsArray, UInt32Array}; use arrow::datatypes::{DataType, Field, Schema}; use arrow::row::{RowConverter, SortField}; use datafusion_common::config::ConfigOptions; @@ -24,7 +24,9 @@ use datafusion_common::{ DataFusionError, NullEquality, Result, ScalarValue, SharedResult, assert_or_internal_err, }; -use datafusion_execution::memory_pool::{MemoryPool, UnboundedMemoryPool}; +use datafusion_execution::memory_pool::{ + MemoryConsumer, MemoryPool, UnboundedMemoryPool, +}; use datafusion_expr::Operator; use datafusion_functions::core::r#struct as struct_func; use datafusion_physical_expr::expressions::{ @@ -781,7 +783,7 @@ impl SharedBuildAccumulator { _ => None, }) .collect::>>(); - let membership_expr = match in_lists { + let membership_expr = match &in_lists { // The partitions hold disjoint keys, so the sum of their distinct // counts is the number of distinct keys of the build side. Some(in_lists) @@ -803,15 +805,31 @@ impl SharedBuildAccumulator { // Every partition must have its filter. A partition has none // when the build side is too big for a bloom filter, or its // memory reservation failed. - let filters = non_empty - .iter() - .map(|p| p.build_keys.bloom_filter.as_deref()) - .collect::>>(); let pool = self .global_filter_options .memory_pool .clone() .unwrap_or_else(|| Arc::new(UnboundedMemoryPool::default())); + // Each partition sizes its filter for the keys of all partitions, + // estimated from its own count. With few keys per partition the + // counts vary a lot, and the merge folds into the smallest + // filter, which then cannot hold every key. Partitions with an + // IN list hold their keys, so build one filter of all of them, + // sized for the real count. + let from_in_lists = in_lists + .as_deref() + .map(|in_lists| { + self.filter_from_in_lists(in_lists, num_distinct, &pool) + }) + .transpose()? + .flatten(); + let filters = match &from_in_lists { + Some(filter) => Some(vec![filter]), + None => non_empty + .iter() + .map(|p| p.build_keys.bloom_filter.as_deref()) + .collect::>>(), + }; let bloom = BloomFilterExpr::try_new( self.on_right.clone(), filters.as_deref(), @@ -833,6 +851,34 @@ impl SharedBuildAccumulator { self.preserve_probe_nulls(filter_expr, keys_have_null) } + /// A bloom filter of the IN list values of all partitions, sized for + /// `num_distinct` keys. `None` when that filter is too large or `pool` + /// cannot hold it. + fn filter_from_in_lists( + &self, + in_lists: &[ArrayRef], + num_distinct: usize, + pool: &Arc, + ) -> Result> { + let arrays: Vec<&dyn Array> = in_lists.iter().map(|a| a.as_ref()).collect(); + let values = arrow::compute::concat(&arrays)?; + // Multi-column keys are a struct of the key columns. + let keys = if self.on_right.len() == 1 { + vec![values] + } else { + values.as_struct().columns().to_vec() + }; + // Only for the build: the merge copies the filter into its own + // reservation. + let reservation = MemoryConsumer::new("HashJoinBloomFilterBuild").register(pool); + bloom_filter::partition_filter( + &keys, + num_distinct, + self.global_filter_options.bloom_filter, + &reservation, + ) + } + /// One [`KeyRangeBitmap`] for the keys of all partitions, so that scans can /// prune containers (for example parquet row groups and pages) with the /// global filter. Only for a single integer key. @@ -981,17 +1027,16 @@ mod tests { /// Adds the bloom filter of `build_keys.keys`, sized for 4 times its /// keys (the keys of a few partitions). fn test_build_keys(mut build_keys: BuildKeys) -> BuildKeys { - let reservation = datafusion_execution::memory_pool::MemoryConsumer::new("test") + let reservation = MemoryConsumer::new("test") .register(&(Arc::new(UnboundedMemoryPool::default()) as Arc)); - build_keys.bloom_filter = - crate::joins::hash_join::bloom_filter::partition_filter( - &build_keys.keys, - build_keys.num_distinct * 4, - GlobalFilterOptions::default().bloom_filter, - &reservation, - ) - .unwrap() - .map(Arc::new); + build_keys.bloom_filter = bloom_filter::partition_filter( + &build_keys.keys, + build_keys.num_distinct * 4, + GlobalFilterOptions::default().bloom_filter, + &reservation, + ) + .unwrap() + .map(Arc::new); build_keys } @@ -1218,6 +1263,68 @@ mod tests { assert!(false_positives < 300, "{false_positives} false positives"); } + /// Many partitions with few keys each have uneven key counts, so the + /// filter that the smallest partition sizes for all partitions is too + /// small for every key. The IN lists still give a bloom filter. + #[test] + fn many_small_inlist_partitions_keep_bloom_filter() { + const PARTITIONS: usize = 128; + let partition_keys = |p: usize| -> Vec { + let n = if p == 0 { 4 } else { 16 }; + (0..n).map(|i| (p * 100 + i) as i32).collect() + }; + let partitions = (0..PARTITIONS) + .map(|p| { + let keys = partition_keys(p); + let values: ArrayRef = Arc::new(Int32Array::from(keys.clone())); + // Sized as the build sizes it: for this partition's keys times + // the number of partitions. + let reservation = MemoryConsumer::new("test").register( + &(Arc::new(UnboundedMemoryPool::default()) as Arc), + ); + let bloom_filter = bloom_filter::partition_filter( + std::slice::from_ref(&values), + keys.len() * PARTITIONS, + GlobalFilterOptions::default().bloom_filter, + &reservation, + ) + .unwrap() + .map(Arc::new); + PartitionStatus::Reported(PartitionData { + pushdown: PushdownStrategy::InList(Arc::clone(&values)), + bounds: bounds(keys[0], *keys.last().unwrap()), + keys_have_null: false, + build_keys: BuildKeys { + bloom_filter, + num_distinct: keys.len(), + keys: vec![values], + }, + }) + }) + .collect(); + let acc = make_partitioned_expr_accumulator_for_test(PARTITIONS); + acc.build_filter(FinalizeInput::Partitioned(partitions)) + .unwrap(); + + let expr = current_expr(&acc); + assert!(format!("{expr}").ends_with("AND bloom_filter"), "{expr}"); + let build_keys: Vec = (0..PARTITIONS).flat_map(partition_keys).collect(); + let num_keys = build_keys.len(); + assert!(eval_bool(&expr, build_keys).into_iter().all(|kept| kept)); + // Keys between the partitions pass the bounds but not the bloom filter + let others: Vec = (0..PARTITIONS as i32) + .flat_map(|p| p * 100 + 50..p * 100 + 100) + .collect(); + let false_positives = eval_bool(&expr, others) + .into_iter() + .filter(|&kept| kept) + .count(); + assert!( + false_positives < num_keys / 10, + "{false_positives} false positives" + ); + } + /// Partitions that each fit an InList but together exceed the InList limit get /// a bloom filter of all their keys. #[test] From 9d6caf2c7ece6f3df7dc5012dbcf463462eff773 Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Tue, 1 Dec 2026 16:52:20 -0800 Subject: [PATCH 7/9] fix: address review of the global hash join filter Review of apache/datafusion#26442 found cases where the global filter lost keys, kept memory, or pruned wrongly. This commit: - gives prepared builds a bloom filter and counts it in their reservation, - uses the bloom filter when dictionary InLists overflow the limit together, - builds the filter from the latest collect-left report, - releases the build data once the filter is built, - prunes containers with the key range bitmap only through widening casts, - iterates the distinct rows of an `ArrayMap` without a copy. A sqllogictest checks that the filter prunes files of a range-partitioned probe side. Co-Authored-By: Claude Opus 5.5 --- .../physical-plan/src/joins/array_map.rs | 7 +- .../src/joins/hash_join/bloom_filter.rs | 345 +++++++++++---- .../src/joins/hash_join/build.rs | 3 +- .../physical-plan/src/joins/hash_join/exec.rs | 155 ++++--- .../src/joins/hash_join/exec/prepared.rs | 4 +- .../joins/hash_join/exec/prepared/tests.rs | 2 + .../src/joins/hash_join/shared_bounds.rs | 401 ++++++++++-------- .../src/joins/hash_join/stream.rs | 3 +- datafusion/pruning/src/pruning_predicate.rs | 71 ++++ .../test_files/preserve_file_partitioning.slt | 27 ++ 10 files changed, 711 insertions(+), 307 deletions(-) diff --git a/datafusion/physical-plan/src/joins/array_map.rs b/datafusion/physical-plan/src/joins/array_map.rs index b9ab97e9801d8..613af8f7a003e 100644 --- a/datafusion/physical-plan/src/joins/array_map.rs +++ b/datafusion/physical-plan/src/joins/array_map.rs @@ -9,7 +9,7 @@ use std::mem::size_of; use crate::joins::chain::traverse_chain; use crate::joins::join_hash_map::ProbeOffset; use crate::joins::join_hash_map::ProbeOffset::{AtRow, MidChain}; -use arrow::array::{Array, ArrayRef, AsArray, BooleanArray, UInt32Array}; +use arrow::array::{Array, ArrayRef, AsArray, BooleanArray}; use arrow::buffer::BooleanBuffer; use arrow::datatypes::ArrowNumericType; use datafusion_common::{Result, ScalarValue, internal_err}; @@ -224,12 +224,11 @@ impl ArrayMap { } /// The index of one build row for each distinct key. - pub(crate) fn distinct_rows(&self) -> UInt32Array { + pub(crate) fn distinct_rows(&self) -> impl Iterator + '_ { self.data .iter() .filter(|&&row| row != 0) .map(|&row| row - 1) - .collect() } pub fn num_of_distinct_key(&self) -> usize { @@ -441,7 +440,7 @@ mod tests { ])); let map = ArrayMap::try_new(&build, 1, 3)?; // Each key keeps its first row, in key order. - assert_eq!(map.distinct_rows().values(), &[1, 0]); + assert_eq!(map.distinct_rows().collect::>(), [1, 0]); Ok(()) } diff --git a/datafusion/physical-plan/src/joins/hash_join/bloom_filter.rs b/datafusion/physical-plan/src/joins/hash_join/bloom_filter.rs index fb456294d8731..92267558913eb 100644 --- a/datafusion/physical-plan/src/joins/hash_join/bloom_filter.rs +++ b/datafusion/physical-plan/src/joins/hash_join/bloom_filter.rs @@ -20,7 +20,7 @@ use arrow::buffer::NullBuffer; use arrow::datatypes::{DataType, Schema}; use arrow::record_batch::RecordBatch; use datafusion_common::hash_utils::create_hashes; -use datafusion_common::{DataFusionError, Result, ScalarValue}; +use datafusion_common::{DataFusionError, Result, ScalarValue, internal_err}; use datafusion_execution::memory_pool::{MemoryConsumer, MemoryPool, MemoryReservation}; use datafusion_expr::ColumnarValue; use datafusion_expr_common::dyn_eq::DynHash; @@ -44,11 +44,13 @@ pub(crate) struct BloomFilterOptions { } impl BloomFilterOptions { - /// Bytes of a filter for `num_distinct` keys at `fpp`, or `None` when - /// that is more than `max_bytes`. + /// Bytes that [`Sbbf::new_with_num_of_bytes`] allocates for `num_distinct` + /// keys at `fpp`, or `None` when that is more than `max_bytes`. fn bytes(&self, num_distinct: usize) -> Option { let bits = -8.0 * num_distinct as f64 / (1.0 - self.fpp.powf(1.0 / 8.0)).ln(); - let bytes = (bits / 8.0) as usize; + let bytes = ((bits / 8.0) as usize) + .max(32) + .checked_next_power_of_two()?; (bytes <= self.max_bytes).then_some(bytes) } } @@ -58,7 +60,7 @@ fn parquet_err(e: parquet::errors::ParquetError) -> DataFusionError { } /// Rows hashed at a time when filling a filter. -const HASH_BATCH_SIZE: usize = 8192; +pub(crate) const HASH_BATCH_SIZE: usize = 8192; /// Hashes `keys` like the hash join and calls `f` with the hashes of each /// batch of rows. A row with a NULL key gets no hash. @@ -93,57 +95,88 @@ fn valid_rows(keys: &[ArrayRef]) -> Option { NullBuffer::union_many(nulls.iter().map(Option::as_ref)) } -/// The bloom filter of the keys of one build partition, sized for -/// `num_distinct` keys (the estimated number of distinct keys of all -/// partitions, so that the partition filters can be ORed). `None` when that -/// filter would be larger than `options.max_bytes`. -/// -/// The filter memory is charged to `reservation`. `None` also when it cannot -/// grow. -pub(crate) fn partition_filter( - keys: &[ArrayRef], - num_distinct: usize, - options: BloomFilterOptions, - reservation: &MemoryReservation, -) -> Result> { - let Some(mut filter) = new_partition_filter(num_distinct, options, reservation) - else { - return Ok(None); - }; - for_each_hash_batch(keys, |hashes| { - for &hash in hashes { - filter.insert_hash(hash); - } - Ok(()) - })?; - Ok(Some(filter)) +/// A build partition with at most this many distinct keys reports their hashes +/// instead of a bloom filter. A filter sized from so few keys is a poor estimate +/// of the whole build side, and one such partition would shrink the merged +/// filter until it is saturated. +const MAX_PARTITION_HASHES: usize = 1024; + +/// What one build partition contributes to the global bloom filter. It owns the +/// reservation of its memory. +#[derive(Debug)] +pub(crate) struct PartitionFilter { + keys: PartitionKeys, + reservation: MemoryReservation, } -/// Like [`partition_filter`], but fills the filter from key hashes that the -/// caller already computed with `HASH_JOIN_SEED`. -pub(crate) fn partition_filter_from_hashes( - for_each_hash: impl FnOnce(&mut dyn FnMut(u64)), - num_distinct: usize, - options: BloomFilterOptions, - reservation: &MemoryReservation, -) -> Option { - let mut filter = new_partition_filter(num_distinct, options, reservation)?; - for_each_hash(&mut |hash| filter.insert_hash(hash)); - Some(filter) +#[derive(Debug)] +enum PartitionKeys { + /// The hashes of the distinct keys + Hashes(Vec), + /// Sized for the keys of all partitions, estimated from this one, so that + /// the partition filters can be ORed. + Bloom(Sbbf), +} + +impl PartitionFilter { + /// An empty filter for a partition with `num_distinct` keys, one of + /// `num_partitions`. `None` when the filter would be larger than + /// `options.max_bytes`, or `reservation` cannot grow. + pub(crate) fn try_new( + num_distinct: usize, + num_partitions: usize, + options: BloomFilterOptions, + reservation: MemoryReservation, + ) -> Option { + let keys = if num_distinct <= MAX_PARTITION_HASHES { + options.bytes(num_distinct)?; + reservation.try_grow(num_distinct * size_of::()).ok()?; + PartitionKeys::Hashes(Vec::with_capacity(num_distinct)) + } else { + let num_distinct = num_distinct.saturating_mul(num_partitions); + PartitionKeys::Bloom(new_filter(num_distinct, options, &reservation)?) + }; + Some(Self { keys, reservation }) + } + + #[inline] + pub(crate) fn insert_hash(&mut self, hash: u64) { + match &mut self.keys { + PartitionKeys::Hashes(hashes) => hashes.push(hash), + PartitionKeys::Bloom(filter) => filter.insert_hash(hash), + } + } + + /// Hashes `keys` like the hash join and inserts each row without a NULL key. + pub(crate) fn insert_keys(&mut self, keys: &[ArrayRef]) -> Result<()> { + for_each_hash_batch(keys, |hashes| { + for &hash in hashes { + self.insert_hash(hash); + } + Ok(()) + }) + } + + /// Bytes charged to the memory pool. + pub(crate) fn reserved_bytes(&self) -> usize { + self.reservation.size() + } + + #[cfg(test)] + pub(crate) fn has_hashes(&self) -> bool { + matches!(self.keys, PartitionKeys::Hashes(_)) + } } /// An empty filter sized for `num_distinct` keys, with its memory charged to /// `reservation`. -fn new_partition_filter( +fn new_filter( num_distinct: usize, options: BloomFilterOptions, reservation: &MemoryReservation, ) -> Option { let bytes = options.bytes(num_distinct)?; - // The size that `Sbbf::new_with_num_of_bytes` allocates - reservation - .try_grow(bytes.max(32).next_power_of_two()) - .ok()?; + reservation.try_grow(bytes).ok()?; Some(Sbbf::new_with_num_of_bytes(bytes)) } @@ -154,33 +187,72 @@ struct Filter { _reservation: MemoryReservation, } -/// ORs the filters of all build partitions and folds the result. Returns -/// `None` (no bloom filter) when `pool` cannot hold the filter or it is -/// saturated. +/// ORs the filters of all build partitions, inserts the keys of the partitions +/// that reported hashes, and folds the result. Returns `None` (no bloom filter) +/// when `pool` cannot hold the filter or it is saturated. +/// +/// Reuses the memory of a partition filter that nothing else holds. fn merge( - partitions: &[&Sbbf], + partitions: Vec>, + num_distinct: usize, options: BloomFilterOptions, pool: &Arc, ) -> Result> { + let mut blooms = Vec::with_capacity(partitions.len()); + let mut hash_lists = vec![]; + for partition in partitions { + match &partition.keys { + PartitionKeys::Bloom(filter) => { + blooms.push((filter.num_blocks(), partition)); + } + PartitionKeys::Hashes(_) => hash_lists.push(partition), + } + } // Partitions estimate the number of keys of all partitions from their own, // so their filters can differ in size: fold the larger ones into the // smallest. - let Some((smallest, first)) = partitions + let smallest = blooms .iter() .enumerate() - .min_by_key(|(_, f)| f.num_blocks()) - .map(|(i, f)| (i, *f)) - else { - return Ok(None); + .min_by_key(|(_, (blocks, _))| *blocks) + .map(|(i, _)| i); + let new_reservation = || MemoryConsumer::new("HashJoinBloomFilter").register(pool); + let (mut filter, reservation) = match smallest.map(|i| blooms.swap_remove(i).1) { + Some(partition) => match Arc::try_unwrap(partition) { + Ok(PartitionFilter { + keys: PartitionKeys::Bloom(filter), + reservation, + }) => (filter, reservation), + Ok(_) => return internal_err!("expected a bloom filter"), + Err(shared) => { + let PartitionKeys::Bloom(filter) = &shared.keys else { + return internal_err!("expected a bloom filter"); + }; + let reservation = new_reservation(); + if reservation.try_grow(filter.num_blocks() * 32).is_err() { + return Ok(None); + } + (filter.clone(), reservation) + } + }, + None => { + let reservation = new_reservation(); + let Some(filter) = new_filter(num_distinct, options, &reservation) else { + return Ok(None); + }; + (filter, reservation) + } }; - let reservation = MemoryConsumer::new("HashJoinBloomFilter").register(pool); - if reservation.try_grow(first.num_blocks() * 32).is_err() { - return Ok(None); + for (_, partition) in blooms { + if let PartitionKeys::Bloom(other) = &partition.keys { + filter.union(other).map_err(parquet_err)?; + } } - let mut filter = first.clone(); - for (i, partition) in partitions.iter().enumerate() { - if i != smallest { - filter.union(partition).map_err(parquet_err)?; + for partition in hash_lists { + if let PartitionKeys::Hashes(hashes) = &partition.keys { + for &hash in hashes { + filter.insert_hash(hash); + } } } filter.fold_to_target_fpp(options.fpp); @@ -212,8 +284,8 @@ pub struct BloomFilterExpr { } impl BloomFilterExpr { - /// ORs the bloom filters of every build partition ([`partition_filter`]) - /// into one filter, charged to `pool`. + /// Merges the [`PartitionFilter`]s of every build partition, with + /// `num_distinct` keys together, into one filter charged to `pool`. /// /// When no useful filter is possible (`partitions` is `None` because a /// partition has no filter, `pool` cannot hold the filter, or the filter @@ -221,13 +293,16 @@ impl BloomFilterExpr { /// row and only carries `pruning_bitmap` (see [`Self::has_filter`]). pub(crate) fn try_new( on_columns: Vec, - partitions: Option<&[&Sbbf]>, + partitions: Option>>, + num_distinct: usize, options: BloomFilterOptions, pool: &Arc, pruning_bitmap: Option>, ) -> Result { let filter = match partitions { - Some(partitions) => merge(partitions, options, pool)?.map(Arc::new), + Some(partitions) => { + merge(partitions, num_distinct, options, pool)?.map(Arc::new) + } None => None, }; Ok(Self { @@ -408,11 +483,12 @@ mod tests { fn partition_filters( partitions: &[Vec], options: BloomFilterOptions, - ) -> Option> { + pool: &Arc, + ) -> Option>> { let num_distinct = partitions.iter().map(|p| p[0].len()).sum(); partitions .iter() - .map(|keys| filter_of(keys, num_distinct, options)) + .map(|keys| filter_in(keys, num_distinct, options, pool)) .collect() } @@ -420,9 +496,41 @@ mod tests { keys: &[ArrayRef], num_distinct: usize, options: BloomFilterOptions, - ) -> Option { + ) -> Option> { + filter_in(keys, num_distinct, options, &unbounded()) + } + + fn filter_in( + keys: &[ArrayRef], + num_distinct: usize, + options: BloomFilterOptions, + pool: &Arc, + ) -> Option> { + let reservation = MemoryConsumer::new("test").register(pool); + let filter = new_filter(num_distinct, options, &reservation)?; + let mut filter = PartitionFilter { + keys: PartitionKeys::Bloom(filter), + reservation, + }; + filter.insert_keys(keys).unwrap(); + Some(Arc::new(filter)) + } + + /// The hashes of `keys`, as a partition with few keys reports them. + fn hashes_of(keys: &[ArrayRef]) -> Arc { let reservation = MemoryConsumer::new("test").register(&unbounded()); - partition_filter(keys, num_distinct, options, &reservation).unwrap() + let mut filter = + PartitionFilter::try_new(keys[0].len(), 8, OPTIONS, reservation).unwrap(); + filter.insert_keys(keys).unwrap(); + assert!(filter.has_hashes()); + Arc::new(filter) + } + + fn sbbf(filter: &PartitionFilter) -> &Sbbf { + match &filter.keys { + PartitionKeys::Bloom(filter) => filter, + PartitionKeys::Hashes(_) => panic!("expected a bloom filter"), + } } fn expr_with( @@ -431,10 +539,10 @@ mod tests { options: BloomFilterOptions, pool: &Arc, ) -> Option { - let filters = partition_filters(partitions, options); - let refs = filters.as_ref().map(|f| f.iter().collect::>()); + let filters = partition_filters(partitions, options, pool); + let num_distinct = partitions.iter().map(|p| p[0].len()).sum(); let expr = - BloomFilterExpr::try_new(columns, refs.as_deref(), options, pool, None) + BloomFilterExpr::try_new(columns, filters, num_distinct, options, pool, None) .unwrap(); expr.has_filter().then_some(expr) } @@ -486,6 +594,9 @@ mod tests { for k in (0..1000).chain(5000..6000) { assert!(result[k], "key {k} dropped"); } + if cfg!(feature = "force_hash_collisions") { + return; + } // Few false positives let false_positives = result.iter().filter(|v| **v).count() - 2000; assert!(false_positives < 200, "{false_positives} false positives"); @@ -523,7 +634,8 @@ mod tests { .collect(); let expr = int_expr(partitions.clone()); let all: Vec = partitions.into_iter().flatten().collect(); - let mut expected = filter_of(&int_keys(all.clone()), all.len(), OPTIONS).unwrap(); + let expected = filter_of(&int_keys(all.clone()), all.len(), OPTIONS).unwrap(); + let mut expected = sbbf(&expected).clone(); expected.fold_to_target_fpp(OPTIONS.fpp); let (mut a, mut b) = (vec![], vec![]); expected.write_bitset(&mut a).unwrap(); @@ -539,10 +651,11 @@ mod tests { let small = filter_of(&int_keys((0..100).collect()), 1000, OPTIONS).unwrap(); let large = filter_of(&int_keys((1000..1100).collect()), 100_000, OPTIONS).unwrap(); - assert!(large.num_blocks() > small.num_blocks()); + assert!(sbbf(&large).num_blocks() > sbbf(&small).num_blocks()); let expr = BloomFilterExpr::try_new( vec![Arc::new(Column::new("k", 0))], - Some(&[&large, &small]), + Some(vec![large, small]), + 200, OPTIONS, &unbounded(), None, @@ -553,6 +666,78 @@ mod tests { assert!(result[1000..].iter().all(|v| *v)); } + /// A partition that reports hashes does not shrink the merged filter. + #[test] + fn hashes_merge_into_partition_filters() { + let large: Vec = (0..100_000).collect(); + let hashes = hashes_of(&int_keys(vec![1_000_001, 1_000_002, 1_000_003])); + let large = filter_of(&int_keys(large.clone()), large.len(), OPTIONS).unwrap(); + let expr = BloomFilterExpr::try_new( + vec![Arc::new(Column::new("k", 0))], + Some(vec![hashes, large]), + 100_003, + OPTIONS, + &unbounded(), + None, + ) + .unwrap(); + assert!(expr.has_filter()); + let keys = [0, 50_000, 99_999, 1_000_001, 1_000_002, 1_000_003]; + let result = eval(&expr, &int_batch(keys.map(Some).to_vec())); + assert_eq!(result, vec![true; keys.len()]); + } + + /// The limit applies to the allocated size, which is a power of two. + #[test] + fn rounded_size_respects_the_limit() { + let options = |max_bytes| BloomFilterOptions { + max_bytes, + ..OPTIONS + }; + assert_eq!(options(1000).bytes(800), None); + assert_eq!(options(1024).bytes(800), Some(1024)); + } + + /// The merged filter takes the memory of an unshared partition filter, and + /// leaves a shared one to its other holders. + #[test] + fn merge_reuses_unshared_memory() { + let pool: Arc = Arc::new(GreedyMemoryPool::new(64 << 20)); + let partition = || { + let reservation = MemoryConsumer::new("test").register(&pool); + let mut filter = + PartitionFilter::try_new(10_000, 1, OPTIONS, reservation).unwrap(); + filter + .insert_keys(&int_keys((0..10_000).collect())) + .unwrap(); + Arc::new(filter) + }; + let expr = |partition| { + BloomFilterExpr::try_new( + vec![Arc::new(Column::new("k", 0))], + Some(vec![partition]), + 10_000, + OPTIONS, + &pool, + None, + ) + .unwrap() + }; + let unshared = partition(); + let bytes = unshared.reserved_bytes(); + let merged = expr(unshared); + assert_eq!(pool.reserved(), merged.filter_bytes()); + drop(merged); + + let shared = partition(); + let merged = expr(Arc::clone(&shared)); + assert_eq!(pool.reserved(), bytes + merged.filter_bytes()); + drop(merged); + assert_eq!(pool.reserved(), bytes); + drop(shared); + assert_eq!(pool.reserved(), 0); + } + /// A build side that needs a filter larger than `max_bytes` at the target /// fpp gets no bloom filter: it is too big to be useful. #[test] @@ -568,6 +753,7 @@ mod tests { } /// A saturated filter is not used. + #[cfg(not(feature = "force_hash_collisions"))] #[test] fn saturated_filter_is_not_used() { // Sized for 10 keys, filled with 10k keys @@ -575,7 +761,8 @@ mod tests { let filter = filter_of(&keys, 10, OPTIONS).unwrap(); let expr = BloomFilterExpr::try_new( vec![Arc::new(Column::new("k", 0))], - Some(&[&filter]), + Some(vec![filter]), + 10, OPTIONS, &unbounded(), None, @@ -632,7 +819,8 @@ mod tests { ], ) .unwrap(); - assert_eq!(eval(&expr, &batch), vec![true, true, false, false]); + let miss = cfg!(feature = "force_hash_collisions"); + assert_eq!(eval(&expr, &batch), vec![true, true, miss, miss]); } /// Any key type the hash join hashes is supported, floats included. @@ -652,7 +840,8 @@ mod tests { vec![Arc::new(Float64Array::from(vec![1.5, 2.5, 3.5]))], ) .unwrap(); - assert_eq!(eval(&expr, &batch), vec![true, true, false]); + let miss = cfg!(feature = "force_hash_collisions"); + assert_eq!(eval(&expr, &batch), vec![true, true, miss]); } /// The filter holds runtime build side state, so it serializes as lit(true). diff --git a/datafusion/physical-plan/src/joins/hash_join/build.rs b/datafusion/physical-plan/src/joins/hash_join/build.rs index 1c40722198dc1..281f20f0a8f63 100644 --- a/datafusion/physical-plan/src/joins/hash_join/build.rs +++ b/datafusion/physical-plan/src/joins/hash_join/build.rs @@ -146,8 +146,7 @@ impl HashJoinBuild { .iter() .any(|array| array.logical_null_count() > 0); let build_keys = BuildKeys { - keys: data.values.to_vec(), - bloom_filter: data.bloom_filter.clone(), + filter: data.report_partition_filter(), num_distinct: data.map.num_of_distinct_key(), }; let report = match self.mode { diff --git a/datafusion/physical-plan/src/joins/hash_join/exec.rs b/datafusion/physical-plan/src/joins/hash_join/exec.rs index 0405f2bdc25e9..c90fd8a6db5ca 100644 --- a/datafusion/physical-plan/src/joins/hash_join/exec.rs +++ b/datafusion/physical-plan/src/joins/hash_join/exec.rs @@ -21,7 +21,9 @@ use crate::joins::array_map::ArrayMap; use crate::joins::hash_join::batched_join_processor::{ BatchedJoinProcessorConfig, RegularJoinKind, }; -use crate::joins::hash_join::bloom_filter; +use crate::joins::hash_join::bloom_filter::{ + BloomFilterOptions, HASH_BATCH_SIZE, PartitionFilter, +}; use crate::joins::hash_join::build::{HashJoinBuild, RegularJoinLeftData}; use crate::joins::hash_join::inlist_builder::build_struct_inlist_values; use crate::joins::hash_join::not_in::{NotIn, NotInBuild}; @@ -64,9 +66,8 @@ use crate::{ }, metrics::{ExecutionPlanMetricsSet, MetricsSet}, }; -use parquet::bloom_filter::Sbbf; -use arrow::array::{Array, ArrayData, ArrayRef, BooleanBufferBuilder}; +use arrow::array::{Array, ArrayData, ArrayRef, BooleanBufferBuilder, UInt32Array}; use arrow::compute::{concat_batches, take}; use arrow::datatypes::SchemaRef; use arrow::record_batch::RecordBatch; @@ -242,6 +243,35 @@ fn array_map_key_range( Ok(Some((min_val, max_val))) } +/// The [`PartitionFilter`] of a build, or `None` when the build has none. +enum BuildFilter { + /// Only one execution uses the build. The first report takes the filter, so + /// the probe phase does not hold its memory. + Once(Mutex>>), + /// A prepared build serves several executions, which all need the filter. + Shared(Option>), +} + +impl JoinBuildData { + /// The filter to report to the dynamic filter, see [`BuildFilter`]. + pub(super) fn report_partition_filter(&self) -> Option> { + match &self.partition_filter { + BuildFilter::Once(filter) => filter.lock().take(), + BuildFilter::Shared(filter) => filter.clone(), + } + } + + fn partition_filter_bytes(&self) -> usize { + let bytes = |f: &Option>| { + f.as_ref().map_or(0, |f| f.reserved_bytes()) + }; + match &self.partition_filter { + BuildFilter::Once(filter) => bytes(&filter.lock()), + BuildFilter::Shared(filter) => bytes(filter), + } + } +} + /// Immutable build buffers and their durable reservation. pub(super) struct JoinBuildData { /// Hash table with row indices into `batch`, charged to its own @@ -266,9 +296,8 @@ pub(super) struct JoinBuildData { /// Used for pre-filter probing, similar to [`Self::bounds`], but in different /// representations: IN-list values or a hash-table reference. pub(super) membership: PushdownStrategy, - /// Bloom filter of the build keys, for the global dynamic filter. `None` - /// when no bloom filter is possible for this build. - pub(super) bloom_filter: Option>, + /// The build keys, for the bloom filter of the global dynamic filter. + partition_filter: BuildFilter, // Keep the reservation after the allocations it accounts for. reservation: MemoryReservation, } @@ -370,9 +399,9 @@ impl JoinLeftData { &self.build.membership } - /// Bloom filter of the build keys, for the dynamic filter. - pub(super) fn bloom_filter(&self) -> Option<&Arc> { - self.build.bloom_filter.as_ref() + /// The filter to report to the dynamic filter, see [`BuildFilter`]. + pub(super) fn report_partition_filter(&self) -> Option> { + self.build.report_partition_filter() } /// Borrow bounds without sharing a consuming join's dynamic-filter state. @@ -3531,49 +3560,28 @@ fn build_in_memory( bounds = None; } - // Fill the bloom filter of this partition's keys here, in its build task, - // so the finalizing task only ORs the partition filters. Partitions hold - // disjoint keys of similar counts, so the filter is sized for the keys of - // all partitions estimated from this one. - let bloom_filter = if !should_compute_dynamic_filters - || matches!(membership, PushdownStrategy::Empty) - || left_values.is_empty() - { + // Collect this partition's key hashes here, in its build task, so the + // finalizing task only merges them. Partitions hold disjoint keys. + // A prepared build needs them even when it computes no bounds. + let filter = if matches!(membership, PushdownStrategy::Empty) { None } else { - let num_distinct = map - .num_of_distinct_key() - .saturating_mul(num_build_partitions); - let options = GlobalFilterOptions::new(config).bloom_filter; - // Insert one hash per distinct key, not one per build row. - let filter = match &**map { - // The map hashes the keys with `random_state`, which is - // `HASH_JOIN_SEED`, the seed the filter is probed with. Keys whose - // hashes collide share an entry, and also share their filter bits. - Map::HashMap(hash_map) => bloom_filter::partition_filter_from_hashes( - |f| hash_map.for_each_entry_hash(f), - num_distinct, - options, - &reservation, - ), - Map::ArrayMap(array_map) => { - let rows = array_map.distinct_rows(); - let keys = left_values - .iter() - .map(|values| take(values.as_ref(), &rows, None)) - .collect::, _>>()?; - bloom_filter::partition_filter( - &keys, - num_distinct, - options, - &reservation, - )? - } - }; - if let Some(filter) = &filter { - metrics.build_mem_used.add(filter.num_blocks() * 32); - } - filter.map(Arc::new) + build_partition_filter( + &map, + &left_values, + num_build_partitions, + GlobalFilterOptions::new(config).bloom_filter, + // A separate reservation, released when the filter is merged + reservation.new_empty(), + )? + }; + if let Some(filter) = &filter { + metrics.build_mem_used.add(filter.reserved_bytes()); + } + let partition_filter = if prepared { + BuildFilter::Shared(filter.map(Arc::new)) + } else { + BuildFilter::Once(Mutex::new(filter.map(Arc::new))) }; if prepared { @@ -3596,7 +3604,7 @@ fn build_in_memory( values: normalized_values, bounds, membership, - bloom_filter, + partition_filter, reservation, }), not_in, @@ -3605,6 +3613,47 @@ fn build_in_memory( }) } +/// The [`PartitionFilter`] of the distinct keys of `map`. `None` when the filter +/// is too large or `reservation` cannot grow. +fn build_partition_filter( + map: &Map, + keys: &[ArrayRef], + num_build_partitions: usize, + options: BloomFilterOptions, + reservation: MemoryReservation, +) -> Result> { + let Some(mut filter) = PartitionFilter::try_new( + map.num_of_distinct_key(), + num_build_partitions, + options, + reservation, + ) else { + return Ok(None); + }; + match map { + // The map hashes the keys with `random_state`, which is + // `HASH_JOIN_SEED`, the seed the filter is probed with. Keys whose + // hashes collide share an entry, and also share their filter bits. + Map::HashMap(map) => map.for_each_entry_hash(&mut |h| filter.insert_hash(h)), + Map::ArrayMap(map) => { + let mut rows = map.distinct_rows(); + loop { + // In chunks, because the copied keys are not reserved + let chunk: UInt32Array = rows.by_ref().take(HASH_BATCH_SIZE).collect(); + if chunk.is_empty() { + break; + } + let chunk = keys + .iter() + .map(|key| take(key.as_ref(), &chunk, None)) + .collect::, _>>()?; + filter.insert_keys(&chunk)?; + } + } + } + Ok(Some(filter)) +} + #[cfg(test)] mod tests { use super::*; @@ -3832,7 +3881,9 @@ mod tests { fn bloom_filter_bytes(expr: &Arc) -> Result { let mut bytes = 0; expr.apply(|e| { - if let Some(bloom) = e.downcast_ref::() { + if let Some(bloom) = + e.downcast_ref::() + { bytes += bloom.filter_bytes(); } Ok(TreeNodeRecursion::Continue) diff --git a/datafusion/physical-plan/src/joins/hash_join/exec/prepared.rs b/datafusion/physical-plan/src/joins/hash_join/exec/prepared.rs index 4d1fe1e18458a..bdc256103d408 100644 --- a/datafusion/physical-plan/src/joins/hash_join/exec/prepared.rs +++ b/datafusion/physical-plan/src/joins/hash_join/exec/prepared.rs @@ -45,7 +45,9 @@ impl fmt::Debug for PreparedHashJoinBuild { impl PreparedHashJoinBuild { /// Return the retained build reservation, excluding all per-probe state. pub fn reserved_bytes(&self) -> usize { - self.build.reservation.size() + self.build.map.reserved_bytes() + self.build.reservation.size() + + self.build.map.reserved_bytes() + + self.build.partition_filter_bytes() } /// Return the complete build row count, including duplicate and null keys. diff --git a/datafusion/physical-plan/src/joins/hash_join/exec/prepared/tests.rs b/datafusion/physical-plan/src/joins/hash_join/exec/prepared/tests.rs index c3b3d4616a161..a7bf30d1fc472 100644 --- a/datafusion/physical-plan/src/joins/hash_join/exec/prepared/tests.rs +++ b/datafusion/physical-plan/src/joins/hash_join/exec/prepared/tests.rs @@ -953,6 +953,8 @@ async fn prepared_byte_keys_use_hash_membership() -> Result<()> { assert_eq!(output.iter().map(RecordBatch::num_rows).sum::(), 1); let filter = &task.dynamic_filter.as_ref().unwrap().filter; assert!(futures::poll!(Box::pin(filter.wait_complete())).is_ready()); + let current = filter.current()?.to_string(); + assert_eq!(current, "bloom_filter"); drop(task); assert_eq!(pool.reserved(), 0); } diff --git a/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs b/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs index 25e79c66886ac..caa4d9250c268 100644 --- a/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs +++ b/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs @@ -12,28 +12,28 @@ use std::sync::Arc; use crate::ExecutionPlan; use crate::ExecutionPlanProperties; use crate::joins::PartitionMode; -use crate::joins::hash_join::bloom_filter::{self, BloomFilterExpr, BloomFilterOptions}; +use crate::joins::hash_join::bloom_filter::{ + BloomFilterExpr, BloomFilterOptions, PartitionFilter, +}; use crate::joins::hash_join::inlist_builder::build_struct_fields; use crate::joins::hash_join::not_in::NotIn; use crate::joins::key_range_bitmap::{KeyRangeBitmap, KeyRangePart}; -use arrow::array::{Array, ArrayRef, AsArray, UInt32Array}; +use arrow::array::{Array, ArrayRef, UInt32Array}; use arrow::datatypes::{DataType, Field, Schema}; +use arrow::error::ArrowError; use arrow::row::{RowConverter, SortField}; use datafusion_common::config::ConfigOptions; use datafusion_common::{ DataFusionError, NullEquality, Result, ScalarValue, SharedResult, assert_or_internal_err, }; -use datafusion_execution::memory_pool::{ - MemoryConsumer, MemoryPool, UnboundedMemoryPool, -}; +use datafusion_execution::memory_pool::{MemoryPool, UnboundedMemoryPool}; use datafusion_expr::Operator; use datafusion_functions::core::r#struct as struct_func; use datafusion_physical_expr::expressions::{ BinaryExpr, DynamicFilterPhysicalExpr, InListExpr, IsNullExpr, lit, }; use datafusion_physical_expr::{PhysicalExpr, PhysicalExprRef, ScalarFunctionExpr}; -use parquet::bloom_filter::Sbbf; use parking_lot::Mutex; use tokio::sync::Notify; @@ -55,6 +55,17 @@ fn distinct_values(values: &ArrayRef) -> Result { Ok(arrow::compute::take(values.as_ref(), &indices, None)?) } +/// The IN lists of all partitions in one array, or `None` when their dictionaries +/// together have more values than the key type of the result can index. +fn concat_in_lists(in_lists: &[ArrayRef]) -> Result> { + let arrays: Vec<&dyn Array> = in_lists.iter().map(|a| a.as_ref()).collect(); + match arrow::compute::concat(&arrays) { + Ok(values) => Ok(Some(values)), + Err(ArrowError::DictionaryKeyOverflowError) => Ok(None), + Err(e) => Err(e.into()), + } +} + /// Represents the minimum and maximum values for a specific column. /// Used in dynamic filter pushdown to establish value boundaries. #[derive(Debug, Clone, PartialEq)] @@ -125,14 +136,12 @@ impl PartitionBounds { } } -/// The build side key columns of one partition, for the global dynamic filter. +/// The build side keys of one partition, for the global dynamic filter. #[derive(Clone, Default)] pub(crate) struct BuildKeys { - /// One array per join key - pub(crate) keys: Vec, - /// Bloom filter of the keys. `None` when this partition has no filter, - /// then the global filter has no bloom filter. - pub(crate) bloom_filter: Option>, + /// `None` when this partition has no filter, then the global filter has no + /// bloom filter. + pub(crate) filter: Option>, /// Number of distinct key tuples pub(crate) num_distinct: usize, } @@ -345,7 +354,7 @@ pub(crate) enum PushdownStrategy { /// Use InList for small build sides (< 128MB) InList(ArrayRef), /// The build side has too many keys for an InList. The global filter - /// uses the bloom filter of [`BuildKeys`]. `Some` holds a bucket bitmap of + /// uses the filter of [`BuildKeys`]. `Some` holds a bucket bitmap of /// the keys, for pruning. Large(Option>), /// There was no data in this partition, do not build a dynamic filter for it @@ -563,6 +572,10 @@ impl SharedBuildAccumulator { guard: &mut AccumulatorState, data: PartitionBuildData, ) -> Result<()> { + // The filter is already built. Keep no build data for it. + if !matches!(guard.completion, CompletionState::Pending) { + return Ok(()); + } match (data, &mut guard.data) { ( PartitionBuildData::Partitioned { @@ -600,7 +613,13 @@ impl SharedBuildAccumulator { .. }, ) => { - if matches!(data, PartitionStatus::Pending) { + // Only one of the streams that share the build gets its filter, + // and it may not be the first to report. + if let PartitionStatus::Reported(data) = data { + if let Some(filter) = build_keys.filter { + data.build_keys.filter = Some(filter); + } + } else { *data = PartitionStatus::Reported(PartitionData { bounds, pushdown, @@ -624,10 +643,11 @@ impl SharedBuildAccumulator { guard: &mut AccumulatorState, partition_id: usize, ) { - if let AccumulatedBuildData::Partitioned { - partitions, - completed_partitions, - } = &mut guard.data + if matches!(guard.completion, CompletionState::Pending) + && let AccumulatedBuildData::Partitioned { + partitions, + completed_partitions, + } = &mut guard.data && matches!(partitions[partition_id], PartitionStatus::Pending) { partitions[partition_id] = PartitionStatus::CanceledUnknown; @@ -643,19 +663,23 @@ impl SharedBuildAccumulator { return None; } - let finalize_input = match &guard.data { + // Move the build data out, so it does not outlive the filter it becomes. + let finalize_input = match &mut guard.data { AccumulatedBuildData::Partitioned { partitions, completed_partitions, } if *completed_partitions == partitions.len() => { - Some(FinalizeInput::Partitioned(partitions.clone())) + Some(FinalizeInput::Partitioned(std::mem::take(partitions))) } AccumulatedBuildData::CollectLeft { data, reported_count, expected_reports, } if *reported_count == *expected_reports => { - Some(FinalizeInput::CollectLeft(data.clone())) + Some(FinalizeInput::CollectLeft(std::mem::replace( + data, + PartitionStatus::Pending, + ))) } _ => None, }?; @@ -698,7 +722,7 @@ impl SharedBuildAccumulator { self.build_collect_left_filter(partition) } FinalizeInput::Partitioned(partitions) => { - self.build_partitioned_filter(&partitions) + self.build_partitioned_filter(partitions) } } } @@ -713,7 +737,7 @@ impl SharedBuildAccumulator { }) => Ok(()), PartitionStatus::Reported(data) => self .dynamic_filter - .update(self.build_global_filter(&[&data])?), + .update(self.build_global_filter(vec![data])?), PartitionStatus::Pending => datafusion_common::internal_err!( "attempted to finalize collect-left dynamic filter without reported build data" ), @@ -726,7 +750,7 @@ impl SharedBuildAccumulator { /// Builds the global filter from finalized partitioned build data. /// /// Empty and canceled partitions contribute no key. - fn build_partitioned_filter(&self, partitions: &[PartitionStatus]) -> Result<()> { + fn build_partitioned_filter(&self, partitions: Vec) -> Result<()> { let mut reported = Vec::with_capacity(partitions.len()); for partition in partitions { match partition { @@ -742,7 +766,7 @@ impl SharedBuildAccumulator { } } self.dynamic_filter - .update(self.build_global_filter(&reported)?) + .update(self.build_global_filter(reported)?) } /// Builds one filter for all probe rows from the build data of every partition, @@ -760,11 +784,10 @@ impl SharedBuildAccumulator { /// correct for hash, range and collect-left joins. fn build_global_filter( &self, - partitions: &[&PartitionData], + partitions: Vec, ) -> Result> { - let non_empty: Vec<&PartitionData> = partitions - .iter() - .copied() + let non_empty: Vec = partitions + .into_iter() .filter(|p| !matches!(p.pushdown, PushdownStrategy::Empty)) .collect(); if non_empty.is_empty() { @@ -783,65 +806,49 @@ impl SharedBuildAccumulator { _ => None, }) .collect::>>(); - let membership_expr = match &in_lists { - // The partitions hold disjoint keys, so the sum of their distinct - // counts is the number of distinct keys of the build side. + // The partitions hold disjoint keys, so the sum of their distinct counts + // is the number of distinct keys of the build side. + let in_list_values = match &in_lists { Some(in_lists) if num_distinct <= self.global_filter_options.inlist_max_distinct_values => { - let arrays: Vec<&dyn Array> = - in_lists.iter().map(|a| a.as_ref()).collect(); - let values = distinct_values(&arrow::compute::concat(&arrays)?)?; - Some(create_in_list_predicate( - &self.on_right, - values, - self.probe_schema.as_ref(), - )?) - } - _ => { - let pruning_bitmap = - self.merge_pruning_bitmaps(&non_empty, bounds.as_ref()); - // Every partition must have its filter. A partition has none - // when the build side is too big for a bloom filter, or its - // memory reservation failed. - let pool = self - .global_filter_options - .memory_pool - .clone() - .unwrap_or_else(|| Arc::new(UnboundedMemoryPool::default())); - // Each partition sizes its filter for the keys of all partitions, - // estimated from its own count. With few keys per partition the - // counts vary a lot, and the merge folds into the smallest - // filter, which then cannot hold every key. Partitions with an - // IN list hold their keys, so build one filter of all of them, - // sized for the real count. - let from_in_lists = in_lists - .as_deref() - .map(|in_lists| { - self.filter_from_in_lists(in_lists, num_distinct, &pool) - }) - .transpose()? - .flatten(); - let filters = match &from_in_lists { - Some(filter) => Some(vec![filter]), - None => non_empty - .iter() - .map(|p| p.build_keys.bloom_filter.as_deref()) - .collect::>>(), - }; - let bloom = BloomFilterExpr::try_new( - self.on_right.clone(), - filters.as_deref(), - self.global_filter_options.bloom_filter, - &pool, - pruning_bitmap, - )?; - // Without a bloom filter, keep the expression only for its - // pruning bitmap - (bloom.has_filter() || bloom.pruning_bitmap().is_some()) - .then(|| Arc::new(bloom) as Arc) + concat_in_lists(in_lists)? } + _ => None, + }; + let membership_expr = if let Some(values) = in_list_values { + Some(create_in_list_predicate( + &self.on_right, + distinct_values(&values)?, + self.probe_schema.as_ref(), + )?) + } else { + let pruning_bitmap = self.merge_pruning_bitmaps(&non_empty, bounds.as_ref()); + let pool = self + .global_filter_options + .memory_pool + .clone() + .unwrap_or_else(|| Arc::new(UnboundedMemoryPool::default())); + // Every partition must have its filter. A partition has none when + // the build side is too big for a bloom filter, or its memory + // reservation failed. + let filters = non_empty + .into_iter() + .map(|p| p.build_keys.filter) + .collect::>>(); + let bloom = BloomFilterExpr::try_new( + self.on_right.clone(), + filters, + num_distinct, + self.global_filter_options.bloom_filter, + &pool, + pruning_bitmap, + )?; + // Without a bloom filter, keep the expression only for its + // pruning bitmap + (bloom.has_filter() || bloom.pruning_bitmap().is_some()) + .then(|| Arc::new(bloom) as Arc) }; let bounds_expr = bounds .as_ref() @@ -851,40 +858,12 @@ impl SharedBuildAccumulator { self.preserve_probe_nulls(filter_expr, keys_have_null) } - /// A bloom filter of the IN list values of all partitions, sized for - /// `num_distinct` keys. `None` when that filter is too large or `pool` - /// cannot hold it. - fn filter_from_in_lists( - &self, - in_lists: &[ArrayRef], - num_distinct: usize, - pool: &Arc, - ) -> Result> { - let arrays: Vec<&dyn Array> = in_lists.iter().map(|a| a.as_ref()).collect(); - let values = arrow::compute::concat(&arrays)?; - // Multi-column keys are a struct of the key columns. - let keys = if self.on_right.len() == 1 { - vec![values] - } else { - values.as_struct().columns().to_vec() - }; - // Only for the build: the merge copies the filter into its own - // reservation. - let reservation = MemoryConsumer::new("HashJoinBloomFilterBuild").register(pool); - bloom_filter::partition_filter( - &keys, - num_distinct, - self.global_filter_options.bloom_filter, - &reservation, - ) - } - /// One [`KeyRangeBitmap`] for the keys of all partitions, so that scans can /// prune containers (for example parquet row groups and pages) with the /// global filter. Only for a single integer key. fn merge_pruning_bitmaps( &self, - partitions: &[&PartitionData], + partitions: &[PartitionData], bounds: Option<&PartitionBounds>, ) -> Option> { if self.on_right.len() != 1 { @@ -897,11 +876,14 @@ impl SharedBuildAccumulator { PushdownStrategy::Large(Some(bitmap)) => { Some(KeyRangePart::Bitmap(bitmap.as_ref())) } + PushdownStrategy::InList(keys) => Some(KeyRangePart::Keys(keys.as_ref())), _ => p - .build_keys - .keys - .first() - .map(|keys| KeyRangePart::Keys(keys.as_ref())), + .bounds + .get_column_bounds(0) + .map(|bounds| KeyRangePart::Range { + min: &bounds.min, + max: &bounds.max, + }), }) .collect::>>()?; KeyRangeBitmap::union(&column_bounds.min, &column_bounds.max, &parts) @@ -1024,24 +1006,27 @@ mod tests { use super::*; use crate::joins::hash_join::not_in::{NotInOutput, NotInScope}; - /// Adds the bloom filter of `build_keys.keys`, sized for 4 times its - /// keys (the keys of a few partitions). - fn test_build_keys(mut build_keys: BuildKeys) -> BuildKeys { + /// The build keys of one of 4 partitions, with `num_distinct` distinct keys. + fn test_build_keys(num_distinct: usize, keys: ArrayRef) -> BuildKeys { let reservation = MemoryConsumer::new("test") .register(&(Arc::new(UnboundedMemoryPool::default()) as Arc)); - build_keys.bloom_filter = bloom_filter::partition_filter( - &build_keys.keys, - build_keys.num_distinct * 4, + let mut filter = PartitionFilter::try_new( + num_distinct, + 4, GlobalFilterOptions::default().bloom_filter, - &reservation, + reservation, ) - .unwrap() - .map(Arc::new); - build_keys + .unwrap(); + filter.insert_keys(&[keys]).unwrap(); + BuildKeys { + filter: Some(Arc::new(filter)), + num_distinct, + } } use arrow::array::{ArrayRef, BooleanArray, Float64Array, Int32Array}; use arrow::record_batch::RecordBatch; + use datafusion_execution::memory_pool::MemoryConsumer; use datafusion_physical_expr::expressions::{Column, Literal}; fn test_on_right() -> Vec { @@ -1206,11 +1191,7 @@ mod tests { pushdown: PushdownStrategy::Large(None), bounds: bounds(min, max), keys_have_null: false, - build_keys: test_build_keys(BuildKeys { - bloom_filter: None, - keys: vec![Arc::new(Int32Array::from(keys))], - num_distinct, - }), + build_keys: test_build_keys(num_distinct, Arc::new(Int32Array::from(keys))), }) } @@ -1260,7 +1241,9 @@ mod tests { } // Bounds remove everything outside [0, 12997]; the bloom filter removes // most other keys - assert!(false_positives < 300, "{false_positives} false positives"); + if !cfg!(feature = "force_hash_collisions") { + assert!(false_positives < 300, "{false_positives} false positives"); + } } /// Many partitions with few keys each have uneven key counts, so the @@ -1277,28 +1260,11 @@ mod tests { .map(|p| { let keys = partition_keys(p); let values: ArrayRef = Arc::new(Int32Array::from(keys.clone())); - // Sized as the build sizes it: for this partition's keys times - // the number of partitions. - let reservation = MemoryConsumer::new("test").register( - &(Arc::new(UnboundedMemoryPool::default()) as Arc), - ); - let bloom_filter = bloom_filter::partition_filter( - std::slice::from_ref(&values), - keys.len() * PARTITIONS, - GlobalFilterOptions::default().bloom_filter, - &reservation, - ) - .unwrap() - .map(Arc::new); PartitionStatus::Reported(PartitionData { pushdown: PushdownStrategy::InList(Arc::clone(&values)), bounds: bounds(keys[0], *keys.last().unwrap()), keys_have_null: false, - build_keys: BuildKeys { - bloom_filter, - num_distinct: keys.len(), - keys: vec![values], - }, + build_keys: test_build_keys(keys.len(), values), }) }) .collect(); @@ -1319,10 +1285,115 @@ mod tests { .into_iter() .filter(|&kept| kept) .count(); - assert!( - false_positives < num_keys / 10, - "{false_positives} false positives" + if !cfg!(feature = "force_hash_collisions") { + assert!( + false_positives < num_keys / 10, + "{false_positives} false positives" + ); + } + } + + /// Two partitions whose dictionaries do not fit one `Int8` dictionary. + fn int8_dictionary_partitions() -> Vec { + use arrow::array::{DictionaryArray, Int8Array, StringArray}; + (0..2) + .map(|p| { + let values = + StringArray::from_iter_values((0..80).map(|i| format!("{p}-{i}"))); + let keys: ArrayRef = Arc::new(DictionaryArray::new( + Int8Array::from_iter_values(0..80), + Arc::new(values) as ArrayRef, + )); + PartitionStatus::Reported(PartitionData { + pushdown: PushdownStrategy::InList(Arc::clone(&keys)), + bounds: no_bounds(), + keys_have_null: false, + build_keys: test_build_keys(80, keys), + }) + }) + .collect() + } + + #[test] + fn dictionaries_that_overflow_together_use_the_bloom_filter() { + // Above the limit of the global InList, and below it + for limit in [20, 1000] { + let mut acc = make_partitioned_expr_accumulator_for_test(2); + acc.global_filter_options.inlist_max_distinct_values = limit; + acc.build_filter(FinalizeInput::Partitioned(int8_dictionary_partitions())) + .unwrap(); + assert_eq!(current_expr(&acc).to_string(), "bloom_filter"); + } + } + + /// One tiny partition next to large ones does not remove the bloom filter. + #[test] + fn skewed_partitions_keep_bloom_filter() { + let acc = make_partitioned_expr_accumulator_for_test(4); + let mut partitions = vec![reported_map(vec![1, 2, 3])]; + partitions + .extend((1..4).map(|p| reported_map((p * 100_000..).take(20_000).collect()))); + acc.build_filter(FinalizeInput::Partitioned(partitions)) + .unwrap(); + let expr = current_expr(&acc).to_string(); + assert!(expr.contains("AND bloom_filter"), "{expr}"); + } + + /// The accumulator keeps no build data after it builds the filter. + #[tokio::test] + async fn finalized_accumulator_releases_build_data() { + let acc = make_partitioned_expr_accumulator_for_test(1); + let values: ArrayRef = Arc::new(Int32Array::from_iter_values(0..2000)); + let build_keys = test_build_keys(2000, Arc::clone(&values)); + let filter = Arc::downgrade(build_keys.filter.as_ref().unwrap()); + let report = |build_keys| PartitionBuildData::Partitioned { + partition_id: 0, + pushdown: PushdownStrategy::InList(Arc::clone(&values)), + bounds: bounds(0, 1999), + keys_have_null: false, + build_keys, + }; + acc.report_build_data(report(build_keys)).await.unwrap(); + assert_eq!(filter.strong_count(), 0); + assert_eq!(Arc::strong_count(&values), 1); + // A late report or cancellation changes nothing + acc.report_build_data(report(BuildKeys::default())) + .await + .unwrap(); + acc.report_canceled_partition(0); + assert_eq!(Arc::strong_count(&values), 1); + } + + /// The stream that takes the filter of a shared build may report last. + #[tokio::test] + async fn collect_left_uses_filter_of_later_report() { + let mut acc = make_collect_left_accumulator_for_test(); + let AccumulatedBuildData::CollectLeft { + expected_reports, .. + } = &mut acc.inner.get_mut().data + else { + panic!("expected collect-left"); + }; + *expected_reports = 2; + let values: ArrayRef = Arc::new(Int32Array::from_iter_values(0..2000)); + let report = |build_keys| PartitionBuildData::CollectLeft { + pushdown: PushdownStrategy::Large(None), + bounds: bounds(0, 1999), + keys_have_null: false, + build_keys, + }; + let without_filter = BuildKeys { + filter: None, + num_distinct: 2000, + }; + let (a, b) = futures::join!( + acc.report_build_data(report(without_filter)), + acc.report_build_data(report(test_build_keys(2000, values))), ); + a.unwrap(); + b.unwrap(); + let expr = current_expr(&acc).to_string(); + assert!(expr.ends_with("AND bloom_filter"), "{expr}"); } /// Partitions that each fit an InList but together exceed the InList limit get @@ -1336,11 +1407,7 @@ mod tests { pushdown: in_list(&keys), bounds: bounds(*keys.iter().min().unwrap(), *keys.iter().max().unwrap()), keys_have_null: false, - build_keys: test_build_keys(BuildKeys { - bloom_filter: None, - num_distinct: keys.len(), - keys: vec![Arc::new(Int32Array::from(keys))], - }), + build_keys: test_build_keys(keys.len(), Arc::new(Int32Array::from(keys))), }) }; acc.build_filter(FinalizeInput::Partitioned(vec![ @@ -1379,11 +1446,10 @@ mod tests { pushdown: in_list(&rows), bounds: bounds(*rows.iter().min().unwrap(), *rows.iter().max().unwrap()), keys_have_null: false, - build_keys: test_build_keys(BuildKeys { - bloom_filter: None, + build_keys: test_build_keys( num_distinct, - keys: vec![Arc::new(Int32Array::from(rows))], - }), + Arc::new(Int32Array::from(rows)), + ), }) }; acc.build_filter(FinalizeInput::Partitioned(vec![ @@ -1480,11 +1546,10 @@ mod tests { pushdown: PushdownStrategy::Large(None), bounds: no_bounds(), keys_have_null: false, - build_keys: test_build_keys(BuildKeys { - bloom_filter: None, - num_distinct: keys.len(), - keys: vec![Arc::new(Float64Array::from(keys))], - }), + build_keys: test_build_keys( + keys.len(), + Arc::new(Float64Array::from(keys)), + ), }) }; acc.build_filter(FinalizeInput::Partitioned(vec![ @@ -1527,7 +1592,7 @@ mod tests { let acc = make_partitioned_expr_accumulator_for_test(2); let mut no_hashes = reported_map((1_000_000..1_000_100).collect()); if let PartitionStatus::Reported(data) = &mut no_hashes { - data.build_keys.bloom_filter = None; + data.build_keys.filter = None; } acc.build_filter(FinalizeInput::Partitioned(vec![ reported_map((0..100).collect()), diff --git a/datafusion/physical-plan/src/joins/hash_join/stream.rs b/datafusion/physical-plan/src/joins/hash_join/stream.rs index ba2f4ec3351e6..3be041626dc59 100644 --- a/datafusion/physical-plan/src/joins/hash_join/stream.rs +++ b/datafusion/physical-plan/src/joins/hash_join/stream.rs @@ -678,8 +678,7 @@ impl HashJoinStream { .any(|array| array.logical_null_count() > 0); let build_keys = BuildKeys { - keys: left_data.values().to_vec(), - bloom_filter: left_data.bloom_filter().cloned(), + filter: left_data.report_partition_filter(), num_distinct: left_data.map().num_of_distinct_key(), }; diff --git a/datafusion/pruning/src/pruning_predicate.rs b/datafusion/pruning/src/pruning_predicate.rs index 612d89c9410e8..40b4e7d4e7f42 100644 --- a/datafusion/pruning/src/pruning_predicate.rs +++ b/datafusion/pruning/src/pruning_predicate.rs @@ -2869,6 +2869,77 @@ mod tests { /// Row count should only be referenced once in the pruning expression, even if we need the row count /// for multiple columns. + /// A narrowing cast does not keep the order of the file's statistics, so the + /// bitmap must not prune through it. + #[test] + fn key_range_bitmap_prunes_only_through_widening_casts() { + let keys: ArrayRef = Arc::new(Int64Array::from(vec![10, 20, 30, 100_000])); + let bitmap = Arc::new( + KeyRangeBitmap::try_new( + &keys, + &ScalarValue::Int64(Some(10)), + &ScalarValue::Int64(Some(100_000)), + 4, + ) + .unwrap(), + ); + for (file_type, cast_type, pruned) in [ + (DataType::Int32, DataType::Int64, true), + (DataType::UInt32, DataType::Int64, true), + (DataType::UInt16, DataType::Int64, true), + (DataType::Int64, DataType::Int64, true), + (DataType::Int64, DataType::Int32, false), + (DataType::UInt32, DataType::Int16, false), + ] { + let schema = Schema::new(vec![Field::new("b", file_type.clone(), true)]); + let column: Arc = Arc::new(phys_expr::Column::new("b", 0)); + let cast: Arc = + Arc::new(phys_expr::CastExpr::new(column, cast_type.clone(), None)); + let mut required = RequiredColumns::new(); + let expr = build_key_range_bitmap_pruning_expr( + &bitmap, + &[&cast], + &schema, + &mut required, + ); + assert_eq!(expr.is_some(), pruned, "{file_type} to {cast_type}"); + let Some(expr) = expr else { + assert!(required.columns.is_empty()); + continue; + }; + + // Containers [5000, 8000] and [50000, 60000] are in the gap between + // 30 and 100000. Container [15, 25] holds 20. + let columns: Vec = required + .columns + .iter() + .map(|(_, statistics, _)| { + let values = match statistics { + StatisticsType::Min => vec![5000, 15, 50000], + StatisticsType::Max => vec![8000, 25, 60000], + StatisticsType::NullCount => vec![0, 0, 0], + StatisticsType::RowCount => vec![10, 10, 10], + }; + let values: ArrayRef = Arc::new(Int64Array::from(values)); + match statistics { + StatisticsType::Min | StatisticsType::Max => { + arrow::compute::cast(&values, &file_type).unwrap() + } + _ => arrow::compute::cast(&values, &DataType::UInt64).unwrap(), + } + }) + .collect(); + let batch = + RecordBatch::try_new(Arc::new(required.schema()), columns).unwrap(); + let result = expr.evaluate(&batch).unwrap().into_array(3).unwrap(); + assert_eq!( + result.as_boolean(), + &BooleanArray::from(vec![false, true, false]), + "{file_type} to {cast_type}" + ); + } + } + #[test] fn test_unique_row_count_field_and_column() { // c1 = 100 AND c2 = 200 diff --git a/datafusion/sqllogictest/test_files/preserve_file_partitioning.slt b/datafusion/sqllogictest/test_files/preserve_file_partitioning.slt index 21bef2bedf96a..6185f93492613 100644 --- a/datafusion/sqllogictest/test_files/preserve_file_partitioning.slt +++ b/datafusion/sqllogictest/test_files/preserve_file_partitioning.slt @@ -1048,6 +1048,33 @@ A 10 C 30 D 40 +# The dynamic filter prunes files of the probe side by their partition value. +statement ok +set datafusion.explain.analyze_categories = 'rows'; + +query TT +EXPLAIN ANALYZE SELECT f.f_dkey, f.value +FROM pruning_fact f +INNER JOIN pruning_dim d ON f.f_dkey = d.d_dkey +WHERE f.value = 10; +---- +Plan with Metrics +01)HashJoinExec: mode=Partitioned, join_type=Inner, on=[(f_dkey@1, d_dkey@0)], projection=[f_dkey@1, value@0], metrics=[output_rows=1, output_batches=1, array_map_created_count=0, build_input_batches=1, build_input_rows=1, input_batches=1, input_rows=1, avg_fanout=100% (1/1), probe_hit_rate=100% (1/1)] +02)--FilterExec: value@0 = 10, metrics=[output_rows=1, output_batches=1, selectivity=100% (1/1)] +03)----DataSourceExec: file_groups={3 groups: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/preserve_file_partitioning/pruning_fact/f_dkey=A/data.parquet], [], []]}, projection=[value, f_dkey], output_partitioning=Range([f_dkey@1 ASC], [(B), (C)], 3), file_type=parquet, predicate=value@0 = 10, pruning_predicate=value_null_count@2 != row_count@3 AND value_min@0 <= 10 AND 10 <= value_max@1, required_guarantees=[value in (10)], metrics=[output_rows=1, output_batches=1, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=1 total → 1 matched, page_index_pages_pruned=0 total → 0 matched, page_index_rows_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, batches_split=0, file_open_errors=0, file_scan_errors=0, files_opened=1, files_processed=1, num_predicate_creation_errors=0, predicate_evaluation_errors=0, pushdown_rows_matched=0, pushdown_rows_pruned=0, predicate_cache_inner_records=0, predicate_cache_records=0, scan_efficiency_ratio=0% (0/478)] +04)--DataSourceExec: file_groups={3 groups: [[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/preserve_file_partitioning/pruning_dim/d_dkey=A/data.parquet], [WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/preserve_file_partitioning/pruning_dim/d_dkey=B/data.parquet], [WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/preserve_file_partitioning/pruning_dim/d_dkey=C/data.parquet, WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/preserve_file_partitioning/pruning_dim/d_dkey=D/data.parquet]]}, projection=[d_dkey], output_partitioning=Range([d_dkey@0 ASC], [(B), (C)], 3), file_type=parquet, predicate=DynamicFilter [ d_dkey@1 >= A AND d_dkey@1 <= A AND bloom_filter ], dynamic_rg_pruning=eligible, pruning_predicate=d_dkey_null_count@1 != row_count@2 AND d_dkey_max@0 >= A AND d_dkey_null_count@1 != row_count@2 AND d_dkey_min@3 <= A, required_guarantees=[], metrics=[output_rows=1, output_batches=1, files_ranges_pruned_statistics=1 total → 1 matched, row_groups_pruned_statistics=1 total → 1 matched, page_index_pages_pruned=0 total → 0 matched, page_index_rows_pruned=0 total → 0 matched, limit_pruned_row_groups=0 total → 0 matched, batches_split=0, file_open_errors=0, file_scan_errors=0, files_opened=1, files_processed=1, num_predicate_creation_errors=0, predicate_evaluation_errors=0, pushdown_rows_matched=0, pushdown_rows_pruned=0, predicate_cache_inner_records=0, predicate_cache_records=0, scan_efficiency_ratio=0% (0/438)] + +query TI +SELECT f.f_dkey, f.value +FROM pruning_fact f +INNER JOIN pruning_dim d ON f.f_dkey = d.d_dkey +WHERE f.value = 10; +---- +A 10 + +statement ok +reset datafusion.explain.analyze_categories; + # Pruning B from the fact side keeps [A], [], [C, D]. query TT EXPLAIN SELECT f.f_dkey, f.value From 276bd5342d9511427dd6a564f9e9f0abb0b21461 Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Tue, 1 Dec 2026 16:52:20 -0800 Subject: [PATCH 8/9] perf: size the hash join bloom filter after every build partition reports Each build partition sized its bloom filter from its own number of keys times the number of partitions. With skewed keys this lost the filter for the whole join: one partition with many keys made the estimate too large, and one small partition that reported first made the filter too small, so it saturated. The filters also reserved memory while other partitions were still building, so a query could fail under a memory limit that is enough on main. Now each partition reports its number of distinct keys and a handle to its build data. After the last report, the join decides between an InList, no filter (too large) and a bloom filter of the exact size. Then the tasks that wait for the filter fill it together: each task takes partitions from a queue into one borrowed filter, and the tasks OR their filters as they finish. The number of filters is at most the number of tasks that help, and the last reporter fills what no task takes, so a task that is not polled blocks nobody. The accumulator holds only weak references to the build data. The plan owns the accumulator, so a strong reference would keep the build data of a dropped stream until the plan is dropped. A stream that is dropped never probes, so its keys are not needed. Co-Authored-By: Claude Opus 5.5 --- datafusion/common/src/config.rs | 11 +- .../src/joins/hash_join/bloom_filter.rs | 647 +++++++++--------- .../src/joins/hash_join/build.rs | 14 +- .../physical-plan/src/joins/hash_join/exec.rs | 177 ++--- .../src/joins/hash_join/exec/prepared.rs | 5 +- .../src/joins/hash_join/shared_bounds.rs | 626 +++++++++++------ .../src/joins/hash_join/stream.rs | 34 +- .../test_files/information_schema.slt | 4 +- docs/source/user-guide/configs.md | 4 +- 9 files changed, 828 insertions(+), 694 deletions(-) diff --git a/datafusion/common/src/config.rs b/datafusion/common/src/config.rs index a6b19388d3ff1..301bd98e95f33 100644 --- a/datafusion/common/src/config.rs +++ b/datafusion/common/src/config.rs @@ -1904,16 +1904,15 @@ config_namespace! { /// filter of a hash join. /// /// When the build side of a hash join has more distinct keys than - /// `hash_join_inlist_pushdown_max_distinct_values` (over all partitions), + /// `hash_join_dynamic_filter_inlist_max_distinct_values` (over all partitions), /// the dynamic filter tests probe rows against one bloom filter of all - /// build side keys. Each build partition fills a filter of its keys, sized - /// for the estimated number of distinct keys of all partitions at this - /// probability. Then one task ORs the partition filters and folds the - /// result to this probability. + /// build side keys. After every build partition reports its number of + /// distinct keys, the join sizes the filter for their sum at this + /// probability, and the build tasks fill it together. pub hash_join_bloom_filter_fpp: f64, default = 0.01 /// Maximum size, in bytes, of the bloom filter in the dynamic filter of a - /// hash join (and of the filter of each build partition). A build side that needs a larger + /// hash join. A build side that needs a larger /// filter at `hash_join_bloom_filter_fpp` gets no bloom filter: such a /// large build side is rarely selective, so the filter is too big to be /// useful. The dynamic filter then tests only the min/max bounds (and diff --git a/datafusion/physical-plan/src/joins/hash_join/bloom_filter.rs b/datafusion/physical-plan/src/joins/hash_join/bloom_filter.rs index 92267558913eb..dfa922bdc0554 100644 --- a/datafusion/physical-plan/src/joins/hash_join/bloom_filter.rs +++ b/datafusion/physical-plan/src/joins/hash_join/bloom_filter.rs @@ -5,11 +5,16 @@ //! Bloom filter membership test over the build side keys of a hash join, for //! the join's dynamic filter. //! -//! Each build partition inserts the hash join hashes of its keys into its own -//! bloom filter ([`partition_filter`]), in its own build task. Once every -//! partition is done, one task ORs the partition filters into one filter -//! ([`Sbbf::union`]) and folds it to the target false positive probability. -//! Folding and union commute, so this is the filter of all build keys. +//! The filter is sized for the number of keys of all build partitions, so no +//! task fills a filter before every partition has reported its number of keys. +//! Then the tasks of the partitions fill it together (see +//! [`BloomFilterBuilder`]): +//! +//! ```text +//! partition 0: build map ─ report ┐ ┌ fill ─┐ +//! partition 1: build map ─ report ┼ size the filter ─┼ fill ─┼ publish +//! partition 2: build map ─ report ┘ └ fill ─┘ +//! ``` use std::fmt::{self, Display}; use std::hash::Hash; @@ -20,11 +25,12 @@ use arrow::buffer::NullBuffer; use arrow::datatypes::{DataType, Schema}; use arrow::record_batch::RecordBatch; use datafusion_common::hash_utils::create_hashes; -use datafusion_common::{DataFusionError, Result, ScalarValue, internal_err}; +use datafusion_common::{DataFusionError, Result, ScalarValue}; use datafusion_execution::memory_pool::{MemoryConsumer, MemoryPool, MemoryReservation}; use datafusion_expr::ColumnarValue; use datafusion_expr_common::dyn_eq::DynHash; use datafusion_physical_expr_common::physical_expr::{PhysicalExpr, PhysicalExprRef}; +use parking_lot::Mutex; use parquet::bloom_filter::Sbbf; use crate::joins::hash_join::exec::HASH_JOIN_SEED; @@ -64,9 +70,9 @@ pub(crate) const HASH_BATCH_SIZE: usize = 8192; /// Hashes `keys` like the hash join and calls `f` with the hashes of each /// batch of rows. A row with a NULL key gets no hash. -fn for_each_hash_batch( +pub(crate) fn for_each_hash_batch( keys: &[ArrayRef], - mut f: impl FnMut(&[u64]) -> Result<()>, + f: &mut dyn FnMut(&[u64]), ) -> Result<()> { let num_rows = keys.first().map_or(0, |k| k.len()); let mut hashes = Vec::with_capacity(HASH_BATCH_SIZE.min(num_rows)); @@ -84,7 +90,7 @@ fn for_each_hash_batch( } hashes.truncate(num_valid); } - f(&hashes)?; + f(&hashes); } Ok(()) } @@ -95,176 +101,157 @@ fn valid_rows(keys: &[ArrayRef]) -> Option { NullBuffer::union_many(nulls.iter().map(Option::as_ref)) } -/// A build partition with at most this many distinct keys reports their hashes -/// instead of a bloom filter. A filter sized from so few keys is a poor estimate -/// of the whole build side, and one such partition would shrink the merged -/// filter until it is saturated. -const MAX_PARTITION_HASHES: usize = 1024; - -/// What one build partition contributes to the global bloom filter. It owns the -/// reservation of its memory. -#[derive(Debug)] -pub(crate) struct PartitionFilter { - keys: PartitionKeys, - reservation: MemoryReservation, +/// Passes the hash of each distinct key of one build partition to a function. +pub(crate) type BuildKeys = + Arc Result<()> + Send + Sync>; + +/// Builds one bloom filter of the keys of all build partitions, from concurrent +/// tasks that each call [`Self::fill`]. +/// +/// Each task fills its own filter, and the tasks OR their filters. A task takes +/// partitions until none is left, so the number of filters is at most the number +/// of worker threads, and a task that the runtime polls late delays nobody. +pub(crate) struct BloomFilterBuilder { + state: Mutex, + bytes: usize, + options: BloomFilterOptions, + pool: Arc, } -#[derive(Debug)] -enum PartitionKeys { - /// The hashes of the distinct keys - Hashes(Vec), - /// Sized for the keys of all partitions, estimated from this one, so that - /// the partition filters can be ORed. - Bloom(Sbbf), +struct BuilderState { + /// Partitions that no task has taken + partitions: Vec, + /// Filters that no task is filling. One is here or with a task until the + /// last task takes the result. + filters: Vec, + /// Tasks that are filling or merging a filter + tasks: usize, + /// The keys of some partition are missing + error: Option, } -impl PartitionFilter { - /// An empty filter for a partition with `num_distinct` keys, one of - /// `num_partitions`. `None` when the filter would be larger than - /// `options.max_bytes`, or `reservation` cannot grow. +impl BloomFilterBuilder { + /// A builder for the keys of `partitions`, which have `num_distinct` keys + /// together. `None` when there is no partition, or when the filter is too + /// large or `pool` cannot hold it. pub(crate) fn try_new( + partitions: Vec, num_distinct: usize, - num_partitions: usize, options: BloomFilterOptions, - reservation: MemoryReservation, + pool: Arc, ) -> Option { - let keys = if num_distinct <= MAX_PARTITION_HASHES { - options.bytes(num_distinct)?; - reservation.try_grow(num_distinct * size_of::()).ok()?; - PartitionKeys::Hashes(Vec::with_capacity(num_distinct)) - } else { - let num_distinct = num_distinct.saturating_mul(num_partitions); - PartitionKeys::Bloom(new_filter(num_distinct, options, &reservation)?) - }; - Some(Self { keys, reservation }) - } - - #[inline] - pub(crate) fn insert_hash(&mut self, hash: u64) { - match &mut self.keys { - PartitionKeys::Hashes(hashes) => hashes.push(hash), - PartitionKeys::Bloom(filter) => filter.insert_hash(hash), + if partitions.is_empty() { + return None; } - } - - /// Hashes `keys` like the hash join and inserts each row without a NULL key. - pub(crate) fn insert_keys(&mut self, keys: &[ArrayRef]) -> Result<()> { - for_each_hash_batch(keys, |hashes| { - for &hash in hashes { - self.insert_hash(hash); - } - Ok(()) + let bytes = options.bytes(num_distinct)?; + let filter = Filter::try_new(bytes, &pool)?; + Some(Self { + state: Mutex::new(BuilderState { + partitions, + filters: vec![filter], + tasks: 0, + error: None, + }), + bytes, + options, + pool, }) } - /// Bytes charged to the memory pool. - pub(crate) fn reserved_bytes(&self) -> usize { - self.reservation.size() - } - - #[cfg(test)] - pub(crate) fn has_hashes(&self) -> bool { - matches!(self.keys, PartitionKeys::Hashes(_)) + /// Adds the keys of partitions until every partition is taken. The call that + /// completes the filter returns it, or an inner `None` when it is saturated. + /// Every other call returns `None`. + pub(crate) fn fill(&self) -> Option>> { + let (mut partition, spare) = { + let mut state = self.state.lock(); + let partition = state.partitions.pop()?; + state.tasks += 1; + (partition, state.filters.pop()) + }; + let mut filter = match spare.or_else(|| Filter::try_new(self.bytes, &self.pool)) { + Some(filter) => filter, + None => { + let mut state = self.state.lock(); + match state.filters.pop() { + Some(filter) => filter, + // The other tasks hold all filters. They look for another + // partition before they leave. + None => { + state.partitions.push(partition); + state.tasks -= 1; + return None; + } + } + } + }; + loop { + let result = partition(&mut |hashes| { + for &hash in hashes { + filter.filter.insert_hash(hash); + } + }); + // Release the build data of the partition without the lock + drop(partition); + let mut state = self.state.lock(); + if let Err(e) = result { + state.error = Some(e); + } + partition = loop { + if let Some(partition) = state.partitions.pop() { + break partition; + } + if let Some(other) = state.filters.pop() { + drop(state); + let result = filter.filter.union(&other.filter); + drop(other); + state = self.state.lock(); + if let Err(e) = result { + state.error = Some(parquet_err(e)); + } + continue; + } + state.tasks -= 1; + if state.tasks > 0 { + state.filters.push(filter); + return None; + } + return Some(match state.error.take() { + Some(e) => Err(e), + None => Ok(filter.finish(self.options)), + }); + }; + } } } -/// An empty filter sized for `num_distinct` keys, with its memory charged to -/// `reservation`. -fn new_filter( - num_distinct: usize, - options: BloomFilterOptions, - reservation: &MemoryReservation, -) -> Option { - let bytes = options.bytes(num_distinct)?; - reservation.try_grow(bytes).ok()?; - Some(Sbbf::new_with_num_of_bytes(bytes)) -} - /// The bloom filter of a [`BloomFilterExpr`] and the reservation for its /// memory, which is released when the filter is dropped. -struct Filter { +pub(crate) struct Filter { filter: Sbbf, - _reservation: MemoryReservation, + reservation: MemoryReservation, } -/// ORs the filters of all build partitions, inserts the keys of the partitions -/// that reported hashes, and folds the result. Returns `None` (no bloom filter) -/// when `pool` cannot hold the filter or it is saturated. -/// -/// Reuses the memory of a partition filter that nothing else holds. -fn merge( - partitions: Vec>, - num_distinct: usize, - options: BloomFilterOptions, - pool: &Arc, -) -> Result> { - let mut blooms = Vec::with_capacity(partitions.len()); - let mut hash_lists = vec![]; - for partition in partitions { - match &partition.keys { - PartitionKeys::Bloom(filter) => { - blooms.push((filter.num_blocks(), partition)); - } - PartitionKeys::Hashes(_) => hash_lists.push(partition), - } - } - // Partitions estimate the number of keys of all partitions from their own, - // so their filters can differ in size: fold the larger ones into the - // smallest. - let smallest = blooms - .iter() - .enumerate() - .min_by_key(|(_, (blocks, _))| *blocks) - .map(|(i, _)| i); - let new_reservation = || MemoryConsumer::new("HashJoinBloomFilter").register(pool); - let (mut filter, reservation) = match smallest.map(|i| blooms.swap_remove(i).1) { - Some(partition) => match Arc::try_unwrap(partition) { - Ok(PartitionFilter { - keys: PartitionKeys::Bloom(filter), - reservation, - }) => (filter, reservation), - Ok(_) => return internal_err!("expected a bloom filter"), - Err(shared) => { - let PartitionKeys::Bloom(filter) = &shared.keys else { - return internal_err!("expected a bloom filter"); - }; - let reservation = new_reservation(); - if reservation.try_grow(filter.num_blocks() * 32).is_err() { - return Ok(None); - } - (filter.clone(), reservation) - } - }, - None => { - let reservation = new_reservation(); - let Some(filter) = new_filter(num_distinct, options, &reservation) else { - return Ok(None); - }; - (filter, reservation) - } - }; - for (_, partition) in blooms { - if let PartitionKeys::Bloom(other) = &partition.keys { - filter.union(other).map_err(parquet_err)?; - } +impl Filter { + /// An empty filter. `None` when `pool` cannot hold it. + fn try_new(bytes: usize, pool: &Arc) -> Option { + let reservation = MemoryConsumer::new("HashJoinBloomFilter").register(pool); + reservation.try_grow(bytes).ok()?; + Some(Self { + filter: Sbbf::new_with_num_of_bytes(bytes), + reservation, + }) } - for partition in hash_lists { - if let PartitionKeys::Hashes(hashes) = &partition.keys { - for &hash in hashes { - filter.insert_hash(hash); - } + + /// Folds the filter. `None` when it is saturated. + fn finish(mut self, options: BloomFilterOptions) -> Option { + self.filter.fold_to_target_fpp(options.fpp); + // A saturated filter costs CPU time and removes almost no rows + if self.filter.estimated_fpp() > options.max_fpp { + return None; } + self.reservation.resize(self.filter.num_blocks() * 32); + Some(self) } - filter.fold_to_target_fpp(options.fpp); - // A saturated filter costs CPU time and removes almost no rows - if filter.estimated_fpp() > options.max_fpp { - return Ok(None); - } - reservation.resize(filter.num_blocks() * 32); - Ok(Some(Filter { - filter, - _reservation: reservation, - })) } /// Physical expression that tests join keys against a bloom filter of the @@ -284,32 +271,19 @@ pub struct BloomFilterExpr { } impl BloomFilterExpr { - /// Merges the [`PartitionFilter`]s of every build partition, with - /// `num_distinct` keys together, into one filter charged to `pool`. - /// - /// When no useful filter is possible (`partitions` is `None` because a - /// partition has no filter, `pool` cannot hold the filter, or the filter - /// is saturated), the expression has no filter: it is `true` for every - /// row and only carries `pruning_bitmap` (see [`Self::has_filter`]). - pub(crate) fn try_new( + /// An expression that tests rows against `filter`. Without a filter, it is + /// `true` for every row and only carries `pruning_bitmap` (see + /// [`Self::has_filter`]). + pub(crate) fn new( on_columns: Vec, - partitions: Option>>, - num_distinct: usize, - options: BloomFilterOptions, - pool: &Arc, + filter: Option, pruning_bitmap: Option>, - ) -> Result { - let filter = match partitions { - Some(partitions) => { - merge(partitions, num_distinct, options, pool)?.map(Arc::new) - } - None => None, - }; - Ok(Self { + ) -> Self { + Self { on_columns, - filter, + filter: filter.map(Arc::new), pruning_bitmap, - }) + } } /// Returns `true` if the expression tests rows against a bloom filter. @@ -479,72 +453,38 @@ mod tests { Arc::new(UnboundedMemoryPool::default()) } - /// The filters of `partitions`, each sized for the keys of all partitions. - fn partition_filters( + fn expr_with( + columns: Vec, partitions: &[Vec], options: BloomFilterOptions, pool: &Arc, - ) -> Option>> { + ) -> Option { let num_distinct = partitions.iter().map(|p| p[0].len()).sum(); - partitions - .iter() - .map(|keys| filter_in(keys, num_distinct, options, pool)) - .collect() - } - - fn filter_of( - keys: &[ArrayRef], - num_distinct: usize, - options: BloomFilterOptions, - ) -> Option> { - filter_in(keys, num_distinct, options, &unbounded()) - } - - fn filter_in( - keys: &[ArrayRef], - num_distinct: usize, - options: BloomFilterOptions, - pool: &Arc, - ) -> Option> { - let reservation = MemoryConsumer::new("test").register(pool); - let filter = new_filter(num_distinct, options, &reservation)?; - let mut filter = PartitionFilter { - keys: PartitionKeys::Bloom(filter), - reservation, - }; - filter.insert_keys(keys).unwrap(); - Some(Arc::new(filter)) - } - - /// The hashes of `keys`, as a partition with few keys reports them. - fn hashes_of(keys: &[ArrayRef]) -> Arc { - let reservation = MemoryConsumer::new("test").register(&unbounded()); - let mut filter = - PartitionFilter::try_new(keys[0].len(), 8, OPTIONS, reservation).unwrap(); - filter.insert_keys(keys).unwrap(); - assert!(filter.has_hashes()); - Arc::new(filter) + let partitions = partitions.iter().cloned().map(build_keys).collect(); + let builder = BloomFilterBuilder::try_new( + partitions, + num_distinct, + options, + Arc::clone(pool), + )?; + let filter = builder.fill().unwrap().unwrap()?; + Some(BloomFilterExpr::new(columns, Some(filter), None)) } - fn sbbf(filter: &PartitionFilter) -> &Sbbf { - match &filter.keys { - PartitionKeys::Bloom(filter) => filter, - PartitionKeys::Hashes(_) => panic!("expected a bloom filter"), - } + fn build_keys(keys: Vec) -> BuildKeys { + Arc::new(move |f| for_each_hash_batch(&keys, f)) } - fn expr_with( - columns: Vec, - partitions: &[Vec], - options: BloomFilterOptions, - pool: &Arc, - ) -> Option { - let filters = partition_filters(partitions, options, pool); - let num_distinct = partitions.iter().map(|p| p[0].len()).sum(); - let expr = - BloomFilterExpr::try_new(columns, filters, num_distinct, options, pool, None) - .unwrap(); - expr.has_filter().then_some(expr) + fn filter_of(keys: &[ArrayRef], num_distinct: usize) -> Sbbf { + let bytes = OPTIONS.bytes(num_distinct).unwrap(); + let mut filter = Filter::try_new(bytes, &unbounded()).unwrap().filter; + for_each_hash_batch(keys, &mut |hashes| { + for &hash in hashes { + filter.insert_hash(hash); + } + }) + .unwrap(); + filter } fn int_keys(keys: Vec) -> Vec { @@ -634,57 +574,147 @@ mod tests { .collect(); let expr = int_expr(partitions.clone()); let all: Vec = partitions.into_iter().flatten().collect(); - let expected = filter_of(&int_keys(all.clone()), all.len(), OPTIONS).unwrap(); - let mut expected = sbbf(&expected).clone(); + let mut expected = filter_of(&int_keys(all.clone()), all.len()); expected.fold_to_target_fpp(OPTIONS.fpp); - let (mut a, mut b) = (vec![], vec![]); - expected.write_bitset(&mut a).unwrap(); let filter = &expr.filter.as_ref().unwrap().filter; - filter.write_bitset(&mut b).unwrap(); - assert_eq!(a, b); + assert_eq!(bitset(filter), bitset(&expected)); } - /// Partition filters of different sizes (from different estimates of the - /// number of keys) are folded into the smallest. + fn bitset(filter: &Sbbf) -> Vec { + let mut bytes = vec![]; + filter.write_bitset(&mut bytes).unwrap(); + bytes + } + + /// The published filter does not depend on how the keys are spread over the + /// partitions, on the order of the partitions, or on the number of tasks. #[test] - fn partition_filters_of_different_sizes() { - let small = filter_of(&int_keys((0..100).collect()), 1000, OPTIONS).unwrap(); - let large = - filter_of(&int_keys((1000..1100).collect()), 100_000, OPTIONS).unwrap(); - assert!(sbbf(&large).num_blocks() > sbbf(&small).num_blocks()); - let expr = BloomFilterExpr::try_new( - vec![Arc::new(Column::new("k", 0))], - Some(vec![large, small]), - 200, - OPTIONS, - &unbounded(), - None, - ) - .unwrap(); - let result = eval(&expr, &int_batch((0..1100).map(Some).collect())); - assert!(result[..100].iter().all(|v| *v)); - assert!(result[1000..].iter().all(|v| *v)); + fn filter_does_not_depend_on_skew_or_order() { + // (partitions, keys of one partition, keys of each other partition) + let shapes = [ + (128, 200_000, 8), + (128, 60_000, 2000), + (128, 1025, 30_000), + (128, 1, 1025), + (1024, 10_000, 100), + (12, 700_000, 10_000), + (12, 1100, 100_000), + ]; + for (n, one, rest) in shapes { + let total = one + (n - 1) * rest; + let expected = { + let all = int_keys((0..total as i64).collect()); + let mut expected = filter_of(&all, total); + expected.fold_to_target_fpp(OPTIONS.fpp); + bitset(&expected) + }; + for position in [0, n / 2, n - 1] { + let mut start = 0; + let partitions: Vec<_> = (0..n) + .map(|p| { + let len = if p == position { one } else { rest } as i64; + start += len; + build_keys(int_keys((start - len..start).collect())) + }) + .collect(); + for tasks in [1, 8] { + let pool = unbounded(); + let builder = BloomFilterBuilder::try_new( + partitions.clone(), + total, + OPTIONS, + Arc::clone(&pool), + ) + .unwrap(); + let filter = fill_with_tasks(&builder, tasks).unwrap().unwrap(); + assert_eq!( + bitset(&filter.filter), + expected, + "{n} {one} {rest} {position} {tasks}" + ); + assert_eq!(pool.reserved(), filter.filter.num_blocks() * 32); + } + } + } + } + + /// Calls `fill` from `tasks` threads. Exactly one call returns the result. + fn fill_with_tasks( + builder: &BloomFilterBuilder, + tasks: usize, + ) -> Result> { + let results: Vec<_> = std::thread::scope(|s| { + let tasks: Vec<_> = (0..tasks).map(|_| s.spawn(|| builder.fill())).collect(); + tasks + .into_iter() + .filter_map(|t| t.join().unwrap()) + .collect() + }); + assert_eq!(results.len(), 1); + results.into_iter().next().unwrap() } - /// A partition that reports hashes does not shrink the merged filter. + /// A pool that holds one filter is enough for any number of tasks. #[test] - fn hashes_merge_into_partition_filters() { - let large: Vec = (0..100_000).collect(); - let hashes = hashes_of(&int_keys(vec![1_000_001, 1_000_002, 1_000_003])); - let large = filter_of(&int_keys(large.clone()), large.len(), OPTIONS).unwrap(); - let expr = BloomFilterExpr::try_new( - vec![Arc::new(Column::new("k", 0))], - Some(vec![hashes, large]), - 100_003, - OPTIONS, - &unbounded(), - None, - ) - .unwrap(); - assert!(expr.has_filter()); - let keys = [0, 50_000, 99_999, 1_000_001, 1_000_002, 1_000_003]; - let result = eval(&expr, &int_batch(keys.map(Some).to_vec())); - assert_eq!(result, vec![true; keys.len()]); + fn tasks_share_the_filters_that_fit_the_pool() { + let bytes = OPTIONS.bytes(128 * 2000).unwrap(); + let all = int_keys((0..128 * 2000).collect()); + let mut expected = filter_of(&all, 128 * 2000); + expected.fold_to_target_fpp(OPTIONS.fpp); + for filters in [1, 2] { + for _ in 0..20 { + let pool: Arc = + Arc::new(GreedyMemoryPool::new(bytes * filters)); + let partitions = (0..128) + .map(|p| build_keys(int_keys((p * 2000..(p + 1) * 2000).collect()))) + .collect(); + let builder = BloomFilterBuilder::try_new( + partitions, + 128 * 2000, + OPTIONS, + Arc::clone(&pool), + ) + .unwrap(); + let filter = fill_with_tasks(&builder, 8).unwrap().unwrap(); + assert_eq!(bitset(&filter.filter), bitset(&expected)); + drop((filter, builder)); + assert_eq!(pool.reserved(), 0); + } + } + } + + /// A build side that is too large reads no key and reserves no memory. + #[test] + fn too_large_build_side_does_no_work() { + let pool = unbounded(); + let keys: BuildKeys = Arc::new(|_| panic!("reads keys")); + let total = 8 * 1024 * 1024; + assert_eq!(OPTIONS.bytes(total), None); + let partitions = vec![keys; 128]; + assert!( + BloomFilterBuilder::try_new(partitions, total, OPTIONS, Arc::clone(&pool)) + .is_none() + ); + assert_eq!(pool.reserved(), 0); + } + + /// A filter that misses the keys of a partition is never used. + #[test] + fn error_in_one_partition_is_returned() { + for tasks in [1, 8] { + let mut partitions: Vec<_> = (0..64) + .map(|p| build_keys(int_keys((p * 100..(p + 1) * 100).collect()))) + .collect(); + partitions[20] = + Arc::new(|_| datafusion_common::exec_err!("cannot read keys")); + let pool = unbounded(); + let builder = + BloomFilterBuilder::try_new(partitions, 6400, OPTIONS, Arc::clone(&pool)) + .unwrap(); + let error = fill_with_tasks(&builder, tasks).err().unwrap(); + assert!(error.to_string().contains("cannot read keys"), "{error}"); + assert_eq!(pool.reserved(), 0); + } } /// The limit applies to the allocated size, which is a power of two. @@ -698,46 +728,6 @@ mod tests { assert_eq!(options(1024).bytes(800), Some(1024)); } - /// The merged filter takes the memory of an unshared partition filter, and - /// leaves a shared one to its other holders. - #[test] - fn merge_reuses_unshared_memory() { - let pool: Arc = Arc::new(GreedyMemoryPool::new(64 << 20)); - let partition = || { - let reservation = MemoryConsumer::new("test").register(&pool); - let mut filter = - PartitionFilter::try_new(10_000, 1, OPTIONS, reservation).unwrap(); - filter - .insert_keys(&int_keys((0..10_000).collect())) - .unwrap(); - Arc::new(filter) - }; - let expr = |partition| { - BloomFilterExpr::try_new( - vec![Arc::new(Column::new("k", 0))], - Some(vec![partition]), - 10_000, - OPTIONS, - &pool, - None, - ) - .unwrap() - }; - let unshared = partition(); - let bytes = unshared.reserved_bytes(); - let merged = expr(unshared); - assert_eq!(pool.reserved(), merged.filter_bytes()); - drop(merged); - - let shared = partition(); - let merged = expr(Arc::clone(&shared)); - assert_eq!(pool.reserved(), bytes + merged.filter_bytes()); - drop(merged); - assert_eq!(pool.reserved(), bytes); - drop(shared); - assert_eq!(pool.reserved(), 0); - } - /// A build side that needs a filter larger than `max_bytes` at the target /// fpp gets no bloom filter: it is too big to be useful. #[test] @@ -756,18 +746,13 @@ mod tests { #[cfg(not(feature = "force_hash_collisions"))] #[test] fn saturated_filter_is_not_used() { - // Sized for 10 keys, filled with 10k keys - let keys = int_keys((0..10_000).collect()); - let filter = filter_of(&keys, 10, OPTIONS).unwrap(); - let expr = BloomFilterExpr::try_new( - vec![Arc::new(Column::new("k", 0))], - Some(vec![filter]), - 10, - OPTIONS, - &unbounded(), - None, - ) - .unwrap(); + // Sized for 2k keys, filled with 100k keys + let keys = build_keys(int_keys((0..100_000).collect())); + let builder = + BloomFilterBuilder::try_new(vec![keys], 2000, OPTIONS, unbounded()).unwrap(); + let filter = builder.fill().unwrap().unwrap(); + let expr = + BloomFilterExpr::new(vec![Arc::new(Column::new("k", 0))], filter, None); assert!(!expr.has_filter()); assert_eq!(expr.to_string(), "key_range_pruning"); let batch = int_batch(vec![Some(-1)]); diff --git a/datafusion/physical-plan/src/joins/hash_join/build.rs b/datafusion/physical-plan/src/joins/hash_join/build.rs index 281f20f0a8f63..3f12e91e33f09 100644 --- a/datafusion/physical-plan/src/joins/hash_join/build.rs +++ b/datafusion/physical-plan/src/joins/hash_join/build.rs @@ -19,7 +19,7 @@ use parking_lot::Mutex; use super::exec::JoinBuildData; use crate::joins::PartitionMode; use crate::joins::hash_join::shared_bounds::{ - BuildKeys, PartitionBounds, PartitionBuildData, SharedBuildAccumulator, + PartitionBounds, PartitionBuildData, SharedBuildAccumulator, build_keys, }; use crate::joins::utils::{BuildProbeJoinMetrics, OnceFut}; @@ -145,23 +145,23 @@ impl HashJoinBuild { .values .iter() .any(|array| array.logical_null_count() > 0); - let build_keys = BuildKeys { - filter: data.report_partition_filter(), - num_distinct: data.map.num_of_distinct_key(), - }; + let num_distinct = data.map.num_of_distinct_key(); + let keys = build_keys(data); let report = match self.mode { PartitionMode::Partitioned => PartitionBuildData::Partitioned { partition_id: self.partition, pushdown, bounds, keys_have_null, - build_keys, + num_distinct, + keys, }, PartitionMode::CollectLeft => PartitionBuildData::CollectLeft { pushdown, bounds, keys_have_null, - build_keys, + num_distinct, + keys, }, PartitionMode::Auto => { return internal_err!( diff --git a/datafusion/physical-plan/src/joins/hash_join/exec.rs b/datafusion/physical-plan/src/joins/hash_join/exec.rs index c90fd8a6db5ca..dd157157c5735 100644 --- a/datafusion/physical-plan/src/joins/hash_join/exec.rs +++ b/datafusion/physical-plan/src/joins/hash_join/exec.rs @@ -21,9 +21,6 @@ use crate::joins::array_map::ArrayMap; use crate::joins::hash_join::batched_join_processor::{ BatchedJoinProcessorConfig, RegularJoinKind, }; -use crate::joins::hash_join::bloom_filter::{ - BloomFilterOptions, HASH_BATCH_SIZE, PartitionFilter, -}; use crate::joins::hash_join::build::{HashJoinBuild, RegularJoinLeftData}; use crate::joins::hash_join::inlist_builder::build_struct_inlist_values; use crate::joins::hash_join::not_in::{NotIn, NotInBuild}; @@ -67,8 +64,8 @@ use crate::{ metrics::{ExecutionPlanMetricsSet, MetricsSet}, }; -use arrow::array::{Array, ArrayData, ArrayRef, BooleanBufferBuilder, UInt32Array}; -use arrow::compute::{concat_batches, take}; +use arrow::array::{Array, ArrayData, ArrayRef, BooleanBufferBuilder}; +use arrow::compute::concat_batches; use arrow::datatypes::SchemaRef; use arrow::record_batch::RecordBatch; use arrow::util::bit_util; @@ -243,35 +240,6 @@ fn array_map_key_range( Ok(Some((min_val, max_val))) } -/// The [`PartitionFilter`] of a build, or `None` when the build has none. -enum BuildFilter { - /// Only one execution uses the build. The first report takes the filter, so - /// the probe phase does not hold its memory. - Once(Mutex>>), - /// A prepared build serves several executions, which all need the filter. - Shared(Option>), -} - -impl JoinBuildData { - /// The filter to report to the dynamic filter, see [`BuildFilter`]. - pub(super) fn report_partition_filter(&self) -> Option> { - match &self.partition_filter { - BuildFilter::Once(filter) => filter.lock().take(), - BuildFilter::Shared(filter) => filter.clone(), - } - } - - fn partition_filter_bytes(&self) -> usize { - let bytes = |f: &Option>| { - f.as_ref().map_or(0, |f| f.reserved_bytes()) - }; - match &self.partition_filter { - BuildFilter::Once(filter) => bytes(&filter.lock()), - BuildFilter::Shared(filter) => bytes(filter), - } - } -} - /// Immutable build buffers and their durable reservation. pub(super) struct JoinBuildData { /// Hash table with row indices into `batch`, charged to its own @@ -296,8 +264,6 @@ pub(super) struct JoinBuildData { /// Used for pre-filter probing, similar to [`Self::bounds`], but in different /// representations: IN-list values or a hash-table reference. pub(super) membership: PushdownStrategy, - /// The build keys, for the bloom filter of the global dynamic filter. - partition_filter: BuildFilter, // Keep the reservation after the allocations it accounts for. reservation: MemoryReservation, } @@ -350,6 +316,11 @@ pub(super) struct JoinLeftData { } impl JoinLeftData { + /// The build data, shared with the dynamic filter until it is complete + pub(super) fn build(&self) -> &Arc { + &self.build + } + /// return a reference to the map pub(super) fn map(&self) -> &Map { &self.build.map @@ -399,11 +370,6 @@ impl JoinLeftData { &self.build.membership } - /// The filter to report to the dynamic filter, see [`BuildFilter`]. - pub(super) fn report_partition_filter(&self) -> Option> { - self.build.report_partition_filter() - } - /// Borrow bounds without sharing a consuming join's dynamic-filter state. pub(super) fn bounds(&self) -> Option<&PartitionBounds> { self.build.bounds.as_ref() @@ -1813,10 +1779,6 @@ impl ExecutionPlan for HashJoinExec { // Both paths share collection, then create their own left-side data inside // the once-only future before sibling probes can access it. - let num_build_partitions = match self.mode { - PartitionMode::CollectLeft => 1, - _ => self.left.output_partitioning().partition_count(), - }; let collect = |build_partition, probe_partitions| { let left_stream = self.left.execute(build_partition, Arc::clone(&context))?; let name = match self.mode { @@ -1838,7 +1800,6 @@ impl ExecutionPlan for HashJoinExec { not_in, array_map_created_count.clone(), BuildMode::Ordinary, - num_build_partitions, )) }; @@ -3236,7 +3197,6 @@ async fn collect_left_input( not_in: Option, array_map_created_count: Count, mode: BuildMode, - num_build_partitions: usize, ) -> Result { let schema = left_stream.schema(); let prepared = mode == BuildMode::Prepared; @@ -3330,7 +3290,6 @@ async fn collect_left_input( not_in, &array_map_created_count, prepared, - num_build_partitions, ) } @@ -3362,7 +3321,6 @@ fn build_in_memory( not_in: Option, array_map_created_count: &Count, prepared: bool, - num_build_partitions: usize, ) -> Result { let is_phj_candidate = is_perfect_hash_join_candidate(on_left, schema)?; @@ -3560,30 +3518,6 @@ fn build_in_memory( bounds = None; } - // Collect this partition's key hashes here, in its build task, so the - // finalizing task only merges them. Partitions hold disjoint keys. - // A prepared build needs them even when it computes no bounds. - let filter = if matches!(membership, PushdownStrategy::Empty) { - None - } else { - build_partition_filter( - &map, - &left_values, - num_build_partitions, - GlobalFilterOptions::new(config).bloom_filter, - // A separate reservation, released when the filter is merged - reservation.new_empty(), - )? - }; - if let Some(filter) = &filter { - metrics.build_mem_used.add(filter.reserved_bytes()); - } - let partition_filter = if prepared { - BuildFilter::Shared(filter.map(Arc::new)) - } else { - BuildFilter::Once(Mutex::new(filter.map(Arc::new))) - }; - if prepared { batches.clear(); // Prepared keys are direct columns. IN-list arrays share these batch @@ -3604,7 +3538,6 @@ fn build_in_memory( values: normalized_values, bounds, membership, - partition_filter, reservation, }), not_in, @@ -3613,47 +3546,6 @@ fn build_in_memory( }) } -/// The [`PartitionFilter`] of the distinct keys of `map`. `None` when the filter -/// is too large or `reservation` cannot grow. -fn build_partition_filter( - map: &Map, - keys: &[ArrayRef], - num_build_partitions: usize, - options: BloomFilterOptions, - reservation: MemoryReservation, -) -> Result> { - let Some(mut filter) = PartitionFilter::try_new( - map.num_of_distinct_key(), - num_build_partitions, - options, - reservation, - ) else { - return Ok(None); - }; - match map { - // The map hashes the keys with `random_state`, which is - // `HASH_JOIN_SEED`, the seed the filter is probed with. Keys whose - // hashes collide share an entry, and also share their filter bits. - Map::HashMap(map) => map.for_each_entry_hash(&mut |h| filter.insert_hash(h)), - Map::ArrayMap(map) => { - let mut rows = map.distinct_rows(); - loop { - // In chunks, because the copied keys are not reserved - let chunk: UInt32Array = rows.by_ref().take(HASH_BATCH_SIZE).collect(); - if chunk.is_empty() { - break; - } - let chunk = keys - .iter() - .map(|key| take(key.as_ref(), &chunk, None)) - .collect::, _>>()?; - filter.insert_keys(&chunk)?; - } - } - } - Ok(Some(filter)) -} - #[cfg(test)] mod tests { use super::*; @@ -3953,6 +3845,60 @@ mod tests { Ok(()) } + /// The plan owns the dynamic filter state. After a stream reports its build + /// side and is dropped, that state does not hold the build data. + #[rstest] + #[tokio::test] + async fn dropped_stream_releases_build_data( + #[values(false, true)] migration: bool, + ) -> Result<()> { + let schema = Arc::new(Schema::new(vec![Field::new("k", DataType::Int32, false)])); + let keys: ArrayRef = + Arc::new(Int32Array::from_iter_values((0..10_000).map(|i| i * 7919))); + let batch = RecordBatch::try_new(Arc::clone(&schema), vec![keys])?; + let exec = || { + TestMemoryExec::try_new_exec( + &[vec![batch.clone()], vec![batch.clone()]], + Arc::clone(&schema), + None, + ) + }; + let on = vec![( + Arc::new(Column::new_with_schema("k", &schema)?) as _, + Arc::new(Column::new_with_schema("k", &schema)?) as _, + )]; + let (join, _filter) = hash_join_with_dynamic_filter_and_mode( + exec()?, + exec()?, + on, + JoinType::Inner, + PartitionMode::Partitioned, + )?; + let pool: Arc = Arc::new(GreedyMemoryPool::new(1 << 30)); + let runtime = RuntimeEnvBuilder::new() + .with_memory_pool(Arc::clone(&pool)) + .build_arc()?; + let mut config = SessionConfig::new(); + let options = config.options_mut(); + options.optimizer.hash_join_inlist_pushdown_max_size = 0; + options.execution.enable_migration_hash_join = migration; + let task_ctx = Arc::new( + TaskContext::default() + .with_runtime(runtime) + .with_session_config(config), + ); + // Partition 1 never reports, so partition 0 waits for it + let mut stream = join.execute(0, task_ctx)?; + for _ in 0..20 { + assert!(futures::poll!(stream.next()).is_pending()); + tokio::task::yield_now().await; + } + assert!(pool.reserved() > 0); + drop(stream); + assert_eq!(pool.reserved(), 0); + Ok(()) + } + /// A hash map build fills the bloom filter from the hashes stored in the /// map, and the filter is probed with `HASH_JOIN_SEED`. String keys take /// the hash map path, so this fails if the build map uses another seed. @@ -4665,7 +4611,6 @@ mod tests { None, Count::new(), BuildMode::Ordinary, - 1, ) .await? .into_legacy_left_data(); diff --git a/datafusion/physical-plan/src/joins/hash_join/exec/prepared.rs b/datafusion/physical-plan/src/joins/hash_join/exec/prepared.rs index bdc256103d408..7b580f09d3866 100644 --- a/datafusion/physical-plan/src/joins/hash_join/exec/prepared.rs +++ b/datafusion/physical-plan/src/joins/hash_join/exec/prepared.rs @@ -45,9 +45,7 @@ impl fmt::Debug for PreparedHashJoinBuild { impl PreparedHashJoinBuild { /// Return the retained build reservation, excluding all per-probe state. pub fn reserved_bytes(&self) -> usize { - self.build.reservation.size() - + self.build.map.reserved_bytes() - + self.build.partition_filter_bytes() + self.build.reservation.size() + self.build.map.reserved_bytes() } /// Return the complete build row count, including duplicate and null keys. @@ -191,7 +189,6 @@ impl HashJoinExec { None, count, BuildMode::Prepared, - 1, ) .await?; Ok(Arc::new(PreparedHashJoinBuild { diff --git a/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs b/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs index caa4d9250c268..70760462de1e9 100644 --- a/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs +++ b/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs @@ -11,10 +11,13 @@ use std::sync::Arc; use crate::ExecutionPlan; use crate::ExecutionPlanProperties; +use crate::joins::Map; use crate::joins::PartitionMode; use crate::joins::hash_join::bloom_filter::{ - BloomFilterExpr, BloomFilterOptions, PartitionFilter, + BloomFilterBuilder, BloomFilterExpr, BloomFilterOptions, BuildKeys, Filter, + HASH_BATCH_SIZE, for_each_hash_batch, }; +use crate::joins::hash_join::exec::JoinBuildData; use crate::joins::hash_join::inlist_builder::build_struct_fields; use crate::joins::hash_join::not_in::NotIn; use crate::joins::key_range_bitmap::{KeyRangeBitmap, KeyRangePart}; @@ -55,6 +58,60 @@ fn distinct_values(values: &ArrayRef) -> Result { Ok(arrow::compute::take(values.as_ref(), &indices, None)?) } +/// The distinct keys of `data`, for the bloom filter. +/// +/// The plan owns the dynamic filter state, so a strong reference would keep the +/// build data and its reservation after a canceled query. Every live stream that +/// probes `data` holds it, so no live stream needs the keys of released data. +pub(super) fn build_keys(data: &Arc) -> BuildKeys { + let data = Arc::downgrade(data); + Arc::new(move |add| match data.upgrade() { + Some(data) => for_each_distinct_hash(&data.map, &data.values, add), + None => Ok(()), + }) +} + +/// Passes the hash of each distinct key of `map`, whose build key columns are +/// `keys`, to `add`. +fn for_each_distinct_hash( + map: &Map, + keys: &[ArrayRef], + add: &mut dyn FnMut(&[u64]), +) -> Result<()> { + match map { + // The map hashes the keys with `random_state`, which is + // `HASH_JOIN_SEED`, the seed the filter is probed with. Keys whose + // hashes collide share an entry, and also share their filter bits. + Map::HashMap(map) => { + let mut hashes = Vec::with_capacity(HASH_BATCH_SIZE); + map.for_each_entry_hash(&mut |hash| { + hashes.push(hash); + if hashes.len() == HASH_BATCH_SIZE { + add(&hashes); + hashes.clear(); + } + }); + add(&hashes); + } + Map::ArrayMap(map) => { + let mut rows = map.distinct_rows(); + loop { + // In chunks, because the copied keys are not reserved + let chunk: UInt32Array = rows.by_ref().take(HASH_BATCH_SIZE).collect(); + if chunk.is_empty() { + break; + } + let chunk = keys + .iter() + .map(|key| arrow::compute::take(key.as_ref(), &chunk, None)) + .collect::, _>>()?; + for_each_hash_batch(&chunk, add)?; + } + } + } + Ok(()) +} + /// The IN lists of all partitions in one array, or `None` when their dictionaries /// together have more values than the key type of the result can index. fn concat_in_lists(in_lists: &[ArrayRef]) -> Result> { @@ -136,16 +193,6 @@ impl PartitionBounds { } } -/// The build side keys of one partition, for the global dynamic filter. -#[derive(Clone, Default)] -pub(crate) struct BuildKeys { - /// `None` when this partition has no filter, then the global filter has no - /// bloom filter. - pub(crate) filter: Option>, - /// Number of distinct key tuples - pub(crate) num_distinct: usize, -} - /// Options for the global dynamic filter of a hash join, from the session /// configuration. #[derive(Debug, Clone)] @@ -174,6 +221,12 @@ impl GlobalFilterOptions { } } + fn pool(&self) -> Arc { + self.memory_pool + .clone() + .unwrap_or_else(|| Arc::new(UnboundedMemoryPool::default())) + } + /// Charge the bloom filter memory to `pool`. pub(crate) fn with_memory_pool(mut self, pool: Arc) -> Self { self.memory_pool = Some(pool); @@ -300,9 +353,12 @@ fn combine_membership_and_bounds( /// 1. Each partition computes information from its build-side data (keys and bounds) /// 2. Information is stored in the shared state, which tracks how many partitions have reported /// 3. When the last partition reports, one waiter is elected as the finalizer; it merges the -/// collected information, updates the dynamic filter exactly once, and publishes the -/// terminal result by transitioning [`CompletionState`] to `Ready` -/// 4. A [`tokio::sync::Notify`] wakes any other partitions parked in `wait_for_completion`, +/// collected information. If the filter needs a bloom filter, it transitions +/// [`CompletionState`] to `Filling`, and it and the waiters fill the bloom filter together +/// 4. The finalizer, or the task that completes the bloom filter, updates the dynamic filter +/// exactly once, and publishes the terminal result by transitioning [`CompletionState`] +/// to `Ready` +/// 5. A [`tokio::sync::Notify`] wakes any other partitions parked in `wait_for_completion`, /// which then observe the `Ready` state under the mutex and return immediately /// /// ## Filter @@ -354,7 +410,7 @@ pub(crate) enum PushdownStrategy { /// Use InList for small build sides (< 128MB) InList(ArrayRef), /// The build side has too many keys for an InList. The global filter - /// uses the filter of [`BuildKeys`]. `Some` holds a bucket bitmap of + /// uses a bloom filter. `Some` holds a bucket bitmap of /// the keys, for pruning. Large(Option>), /// There was no data in this partition, do not build a dynamic filter for it @@ -368,13 +424,15 @@ pub(crate) enum PartitionBuildData { pushdown: PushdownStrategy, bounds: PartitionBounds, keys_have_null: bool, - build_keys: BuildKeys, + num_distinct: usize, + keys: BuildKeys, }, CollectLeft { pushdown: PushdownStrategy, bounds: PartitionBounds, keys_have_null: bool, - build_keys: BuildKeys, + num_distinct: usize, + keys: BuildKeys, }, } @@ -386,8 +444,10 @@ struct PartitionData { /// Whether any build key of this partition is NULL. Decides whether the pushed /// filter must keep probe-side NULL rows for a null-equal join to match them. keys_have_null: bool, - /// The build side keys, for the global filter - build_keys: BuildKeys, + /// Number of distinct key tuples + num_distinct: usize, + /// The keys, for the bloom filter. See [`build_keys`]. + keys: BuildKeys, } /// Build-side data organized by partition mode @@ -406,9 +466,24 @@ enum AccumulatedBuildData { enum CompletionState { Pending, Finalizing, + /// Every partition has reported. Tasks fill the bloom filter. + Filling(Arc), Ready(SharedResult<()>), } +/// A global filter without its bloom filter, which tasks are filling. +struct PendingFilter { + bloom_filter: BloomFilterBuilder, + rest: FilterParts, +} + +/// The parts of a global filter beside its membership test. +struct FilterParts { + bounds: Option, + keys_have_null: bool, + pruning_bitmap: Option>, +} + struct AccumulatorState { data: AccumulatedBuildData, completion: CompletionState, @@ -583,7 +658,8 @@ impl SharedBuildAccumulator { pushdown, bounds, keys_have_null, - build_keys, + num_distinct, + keys, }, AccumulatedBuildData::Partitioned { partitions, @@ -597,7 +673,8 @@ impl SharedBuildAccumulator { bounds, pushdown, keys_have_null, - build_keys, + num_distinct, + keys, }); } ( @@ -605,7 +682,8 @@ impl SharedBuildAccumulator { pushdown, bounds, keys_have_null, - build_keys, + num_distinct, + keys, }, AccumulatedBuildData::CollectLeft { data, @@ -613,18 +691,13 @@ impl SharedBuildAccumulator { .. }, ) => { - // Only one of the streams that share the build gets its filter, - // and it may not be the first to report. - if let PartitionStatus::Reported(data) = data { - if let Some(filter) = build_keys.filter { - data.build_keys.filter = Some(filter); - } - } else { + if matches!(data, PartitionStatus::Pending) { *data = PartitionStatus::Reported(PartitionData { bounds, pushdown, keys_have_null, - build_keys, + num_distinct, + keys, }); } *reported_count += 1; @@ -689,7 +762,32 @@ impl SharedBuildAccumulator { } fn finish(&self, finalize_input: FinalizeInput) { - let result = self.build_filter(finalize_input).map_err(Arc::new); + match self.build_filter(finalize_input) { + Ok(Some(pending)) => { + let pending = Arc::new(pending); + self.inner.lock().completion = + CompletionState::Filling(Arc::clone(&pending)); + self.completion_notify.notify_waiters(); + // Do not only wait for the other tasks: the runtime may not poll + // them until this task makes progress. + self.fill_bloom_filter(&pending); + } + Ok(None) => self.complete(Ok(())), + Err(e) => self.complete(Err(Arc::new(e))), + } + } + + /// Helps to fill the bloom filter, and publishes the filter if this call + /// completes it. + fn fill_bloom_filter(&self, pending: &PendingFilter) { + if let Some(filter) = pending.bloom_filter.fill() { + let result = + filter.and_then(|filter| self.update_bloom_filter(&pending.rest, filter)); + self.complete(result.map_err(Arc::new)); + } + } + + fn complete(&self, result: SharedResult<()>) { self.dynamic_filter.mark_complete(); let mut guard = self.inner.lock(); @@ -699,24 +797,37 @@ impl SharedBuildAccumulator { } async fn wait_for_completion(&self) -> Result<()> { + let mut helped = false; loop { let notified = { let guard = self.inner.lock(); match &guard.completion { + CompletionState::Filling(pending) if !helped => { + let pending = Arc::clone(pending); + drop(guard); + self.fill_bloom_filter(&pending); + helped = true; + continue; + } CompletionState::Ready(Ok(())) => return Ok(()), CompletionState::Ready(Err(err)) => { return Err(DataFusionError::Shared(Arc::clone(err))); } - CompletionState::Pending | CompletionState::Finalizing => { - self.completion_notify.notified() - } + CompletionState::Pending + | CompletionState::Finalizing + | CompletionState::Filling(_) => self.completion_notify.notified(), } }; notified.await; } } - fn build_filter(&self, finalize_input: FinalizeInput) -> Result<()> { + /// Updates the dynamic filter, or returns the filter that needs the keys of + /// every partition in a bloom filter first. + fn build_filter( + &self, + finalize_input: FinalizeInput, + ) -> Result> { match finalize_input { FinalizeInput::CollectLeft(partition) => { self.build_collect_left_filter(partition) @@ -728,16 +839,17 @@ impl SharedBuildAccumulator { } /// Builds the single global filter used by a collect-left join. - fn build_collect_left_filter(&self, partition: PartitionStatus) -> Result<()> { + fn build_collect_left_filter( + &self, + partition: PartitionStatus, + ) -> Result> { match partition { // An empty build side keeps the filter unchanged. PartitionStatus::Reported(PartitionData { pushdown: PushdownStrategy::Empty, .. - }) => Ok(()), - PartitionStatus::Reported(data) => self - .dynamic_filter - .update(self.build_global_filter(vec![data])?), + }) => Ok(None), + PartitionStatus::Reported(data) => self.build_global_filter(vec![data]), PartitionStatus::Pending => datafusion_common::internal_err!( "attempted to finalize collect-left dynamic filter without reported build data" ), @@ -750,7 +862,10 @@ impl SharedBuildAccumulator { /// Builds the global filter from finalized partitioned build data. /// /// Empty and canceled partitions contribute no key. - fn build_partitioned_filter(&self, partitions: Vec) -> Result<()> { + fn build_partitioned_filter( + &self, + partitions: Vec, + ) -> Result> { let mut reported = Vec::with_capacity(partitions.len()); for partition in partitions { match partition { @@ -765,8 +880,7 @@ impl SharedBuildAccumulator { } } } - self.dynamic_filter - .update(self.build_global_filter(reported)?) + self.build_global_filter(reported) } /// Builds one filter for all probe rows from the build data of every partition, @@ -785,19 +899,19 @@ impl SharedBuildAccumulator { fn build_global_filter( &self, partitions: Vec, - ) -> Result> { + ) -> Result> { let non_empty: Vec = partitions .into_iter() .filter(|p| !matches!(p.pushdown, PushdownStrategy::Empty)) .collect(); if non_empty.is_empty() { // No build row exists, so no probe row can match. - return Ok(lit(false)); + self.dynamic_filter.update(lit(false))?; + return Ok(None); } let keys_have_null = non_empty.iter().any(|p| p.keys_have_null); let bounds = PartitionBounds::merge(non_empty.iter().map(|p| &p.bounds)); - let num_distinct: usize = - non_empty.iter().map(|p| p.build_keys.num_distinct).sum(); + let num_distinct: usize = non_empty.iter().map(|p| p.num_distinct).sum(); let in_lists = non_empty .iter() @@ -817,45 +931,67 @@ impl SharedBuildAccumulator { } _ => None, }; - let membership_expr = if let Some(values) = in_list_values { - Some(create_in_list_predicate( + let mut rest = FilterParts { + bounds, + keys_have_null, + pruning_bitmap: None, + }; + if let Some(values) = in_list_values { + let in_list = create_in_list_predicate( &self.on_right, distinct_values(&values)?, self.probe_schema.as_ref(), - )?) - } else { - let pruning_bitmap = self.merge_pruning_bitmaps(&non_empty, bounds.as_ref()); - let pool = self - .global_filter_options - .memory_pool - .clone() - .unwrap_or_else(|| Arc::new(UnboundedMemoryPool::default())); - // Every partition must have its filter. A partition has none when - // the build side is too big for a bloom filter, or its memory - // reservation failed. - let filters = non_empty - .into_iter() - .map(|p| p.build_keys.filter) - .collect::>>(); - let bloom = BloomFilterExpr::try_new( - self.on_right.clone(), - filters, - num_distinct, - self.global_filter_options.bloom_filter, - &pool, - pruning_bitmap, )?; - // Without a bloom filter, keep the expression only for its - // pruning bitmap - (bloom.has_filter() || bloom.pruning_bitmap().is_some()) - .then(|| Arc::new(bloom) as Arc) - }; - let bounds_expr = bounds + self.update_filter(&rest, Some(in_list))?; + return Ok(None); + } + rest.pruning_bitmap = + self.merge_pruning_bitmaps(&non_empty, rest.bounds.as_ref()); + let bloom_filter = BloomFilterBuilder::try_new( + non_empty.into_iter().map(|p| p.keys).collect(), + num_distinct, + self.global_filter_options.bloom_filter, + self.global_filter_options.pool(), + ); + match bloom_filter { + Some(bloom_filter) => Ok(Some(PendingFilter { bloom_filter, rest })), + None => { + self.update_bloom_filter(&rest, None)?; + Ok(None) + } + } + } + + /// Updates the dynamic filter with `filter` as its membership test. + fn update_bloom_filter( + &self, + rest: &FilterParts, + filter: Option, + ) -> Result<()> { + let bloom = BloomFilterExpr::new( + self.on_right.clone(), + filter, + rest.pruning_bitmap.clone(), + ); + // Without a bloom filter, keep the expression only for its pruning bitmap + let membership = (bloom.has_filter() || bloom.pruning_bitmap().is_some()) + .then(|| Arc::new(bloom) as Arc); + self.update_filter(rest, membership) + } + + fn update_filter( + &self, + rest: &FilterParts, + membership_expr: Option>, + ) -> Result<()> { + let bounds_expr = rest + .bounds .as_ref() .and_then(|b| create_bounds_predicate(&self.on_right, b)); let filter_expr = combine_membership_and_bounds(membership_expr, bounds_expr) .unwrap_or_else(|| lit(true)); - self.preserve_probe_nulls(filter_expr, keys_have_null) + self.dynamic_filter + .update(self.preserve_probe_nulls(filter_expr, rest.keys_have_null)?) } /// One [`KeyRangeBitmap`] for the keys of all partitions, so that scans can @@ -1006,27 +1142,25 @@ mod tests { use super::*; use crate::joins::hash_join::not_in::{NotInOutput, NotInScope}; - /// The build keys of one of 4 partitions, with `num_distinct` distinct keys. - fn test_build_keys(num_distinct: usize, keys: ArrayRef) -> BuildKeys { - let reservation = MemoryConsumer::new("test") - .register(&(Arc::new(UnboundedMemoryPool::default()) as Arc)); - let mut filter = PartitionFilter::try_new( - num_distinct, - 4, - GlobalFilterOptions::default().bloom_filter, - reservation, - ) - .unwrap(); - filter.insert_keys(&[keys]).unwrap(); - BuildKeys { - filter: Some(Arc::new(filter)), - num_distinct, + use arrow::array::{ArrayRef, BooleanArray, Float64Array, Int32Array}; + use arrow::record_batch::RecordBatch; + + fn array_keys(keys: ArrayRef) -> BuildKeys { + Arc::new(move |f| for_each_hash_batch(&[Arc::clone(&keys)], f)) + } + + impl SharedBuildAccumulator { + fn build_filter_for_test(&self, input: FinalizeInput) -> Result<()> { + self.finish(input); + match &self.inner.lock().completion { + CompletionState::Ready(result) => { + result.clone().map_err(DataFusionError::Shared) + } + _ => panic!("the filter is not complete"), + } } } - use arrow::array::{ArrayRef, BooleanArray, Float64Array, Int32Array}; - use arrow::record_batch::RecordBatch; - use datafusion_execution::memory_pool::MemoryConsumer; use datafusion_physical_expr::expressions::{Column, Literal}; fn test_on_right() -> Vec { @@ -1110,10 +1244,15 @@ mod tests { pushdown, bounds, keys_have_null: false, - build_keys: BuildKeys::default(), + num_distinct: 0, + keys: no_keys(), }) } + fn no_keys() -> BuildKeys { + Arc::new(|_| Ok(())) + } + fn current_expr(acc: &SharedBuildAccumulator) -> PhysicalExprRef { acc.dynamic_filter .current() @@ -1191,7 +1330,8 @@ mod tests { pushdown: PushdownStrategy::Large(None), bounds: bounds(min, max), keys_have_null: false, - build_keys: test_build_keys(num_distinct, Arc::new(Int32Array::from(keys))), + num_distinct, + keys: array_keys(Arc::new(Int32Array::from(keys))), }) } @@ -1215,7 +1355,7 @@ mod tests { #[test] fn partitioned_global_bloom_filter() { let acc = make_partitioned_expr_accumulator_for_test(3); - acc.build_filter(FinalizeInput::Partitioned(vec![ + acc.build_filter_for_test(FinalizeInput::Partitioned(vec![ reported_map((0..1000).map(|k| k * 3).collect()), reported(PushdownStrategy::Empty, no_bounds()), reported_map((0..1000).map(|k| k * 3 + 10_000).collect()), @@ -1256,6 +1396,7 @@ mod tests { let n = if p == 0 { 4 } else { 16 }; (0..n).map(|i| (p * 100 + i) as i32).collect() }; + let acc = make_partitioned_expr_accumulator_for_test(PARTITIONS); let partitions = (0..PARTITIONS) .map(|p| { let keys = partition_keys(p); @@ -1264,12 +1405,12 @@ mod tests { pushdown: PushdownStrategy::InList(Arc::clone(&values)), bounds: bounds(keys[0], *keys.last().unwrap()), keys_have_null: false, - build_keys: test_build_keys(keys.len(), values), + num_distinct: keys.len(), + keys: array_keys(values), }) }) .collect(); - let acc = make_partitioned_expr_accumulator_for_test(PARTITIONS); - acc.build_filter(FinalizeInput::Partitioned(partitions)) + acc.build_filter_for_test(FinalizeInput::Partitioned(partitions)) .unwrap(); let expr = current_expr(&acc); @@ -1308,7 +1449,8 @@ mod tests { pushdown: PushdownStrategy::InList(Arc::clone(&keys)), bounds: no_bounds(), keys_have_null: false, - build_keys: test_build_keys(80, keys), + num_distinct: 80, + keys: array_keys(keys), }) }) .collect() @@ -1320,23 +1462,144 @@ mod tests { for limit in [20, 1000] { let mut acc = make_partitioned_expr_accumulator_for_test(2); acc.global_filter_options.inlist_max_distinct_values = limit; - acc.build_filter(FinalizeInput::Partitioned(int8_dictionary_partitions())) - .unwrap(); + acc.build_filter_for_test(FinalizeInput::Partitioned( + int8_dictionary_partitions(), + )) + .unwrap(); assert_eq!(current_expr(&acc).to_string(), "bloom_filter"); } } - /// One tiny partition next to large ones does not remove the bloom filter. + /// Skewed partitions report in any order and wait for each other. The filter + /// holds the keys of all partitions, and its size depends only on their number. + #[cfg(not(feature = "force_hash_collisions"))] + #[tokio::test(flavor = "multi_thread", worker_threads = 4)] + async fn skewed_partitions_keep_bloom_filter() { + // (partitions, keys of one partition, keys of each other partition, + // filter bytes after folding) + let shapes = [ + (128, 200_000, 8, 256 * 1024), + (128, 60_000, 2000, 512 * 1024), + (128, 1025, 30_000, 8 * 1024 * 1024), + (12, 700_000, 10_000, 1024 * 1024), + ]; + for (n, one, rest, bytes) in shapes { + for position in [0, n / 2, n - 1] { + let acc = Arc::new(make_partitioned_expr_accumulator_for_test(n)); + let mut start = 0; + let reports: Vec<_> = (0..n) + .map(|p| { + let len = if p == position { one } else { rest }; + let keys = (start..start + len).map(|k| k * 3); + let data = PartitionBuildData::Partitioned { + partition_id: p, + pushdown: PushdownStrategy::Large(None), + bounds: bounds(start * 3, (start + len - 1) * 3), + keys_have_null: false, + num_distinct: len as usize, + keys: array_keys(Arc::new(Int32Array::from_iter_values( + keys, + ))), + }; + start += len; + data + }) + .collect(); + let mut tasks = tokio::task::JoinSet::new(); + for data in reports { + let acc = Arc::clone(&acc); + tasks.spawn(async move { acc.report_build_data(data).await }); + } + tokio::time::timeout(std::time::Duration::from_secs(60), async { + while let Some(result) = tasks.join_next().await { + result.unwrap().unwrap(); + } + }) + .await + .unwrap(); + let expr = current_expr(&acc); + let bloom = binary_expr(&expr) + .right() + .downcast_ref::() + .unwrap(); + assert_eq!(bloom.filter_bytes(), bytes, "{n} {one} {rest} {position}"); + let probe = (0..start).map(|k| k * 3).collect(); + assert!(eval_bool(&expr, probe).into_iter().all(|kept| kept)); + let others: Vec = (0..100_000).map(|k| k * 3 + 1).collect(); + let passed = eval_bool(&expr, others).iter().filter(|v| **v).count(); + assert!(passed < 1500, "{passed} false positives"); + } + } + } + + /// The last partition to arrive completes the filter by itself, also when + /// nothing polls the partitions that wait, and when it only cancels. #[test] - fn skewed_partitions_keep_bloom_filter() { - let acc = make_partitioned_expr_accumulator_for_test(4); - let mut partitions = vec![reported_map(vec![1, 2, 3])]; - partitions - .extend((1..4).map(|p| reported_map((p * 100_000..).take(20_000).collect()))); - acc.build_filter(FinalizeInput::Partitioned(partitions)) - .unwrap(); - let expr = current_expr(&acc).to_string(); - assert!(expr.contains("AND bloom_filter"), "{expr}"); + fn last_partition_fills_for_partitions_that_are_not_polled() { + for cancel in [false, true] { + let mut acc = make_partitioned_expr_accumulator_for_test(2); + let pool: Arc = Arc::new(UnboundedMemoryPool::default()); + acc.global_filter_options.memory_pool = Some(Arc::clone(&pool)); + let report = |partition_id: usize| { + let start = partition_id as i32 * 10_000; + let keys: Vec<_> = (start..start + 2000).step_by(2).collect(); + acc.report_build_data(PartitionBuildData::Partitioned { + partition_id, + pushdown: PushdownStrategy::Large(None), + bounds: bounds(start, start + 1998), + keys_have_null: false, + num_distinct: keys.len(), + keys: array_keys(Arc::new(Int32Array::from(keys))), + }) + }; + let mut first = std::pin::pin!(report(0)); + let mut cx = std::task::Context::from_waker(futures::task::noop_waker_ref()); + assert!(first.as_mut().poll(&mut cx).is_pending()); + // A filter does not take memory that the other builds need + assert_eq!(pool.reserved(), 0); + let mut build_keys: Vec = (0..2000).step_by(2).collect(); + if cancel { + acc.report_canceled_partition(1); + } else { + let mut second = std::pin::pin!(report(1)); + assert!(second.as_mut().poll(&mut cx).is_ready()); + build_keys.extend((10_000..12_000).step_by(2)); + } + assert!(first.as_mut().poll(&mut cx).is_ready()); + let expr = current_expr(&acc); + assert!(expr.to_string().ends_with("AND bloom_filter"), "{expr}"); + assert!(eval_bool(&expr, build_keys).into_iter().all(|kept| kept)); + } + } + + /// A stream that is dropped after it reported does not block the others. + #[test] + fn stream_dropped_after_report_does_not_block_others() { + let acc = make_partitioned_expr_accumulator_for_test(2); + let mut cx = std::task::Context::from_waker(futures::task::noop_waker_ref()); + let data = + |partition_id: usize, keys: Vec| PartitionBuildData::Partitioned { + partition_id, + pushdown: PushdownStrategy::Large(None), + bounds: bounds(keys[0], *keys.last().unwrap()), + keys_have_null: false, + num_distinct: keys.len(), + keys: array_keys(Arc::new(Int32Array::from(keys))), + }; + { + let mut dropped = + Box::pin(acc.report_build_data(data(0, (0..2000).step_by(2).collect()))); + assert!(dropped.as_mut().poll(&mut cx).is_pending()); + } + acc.report_canceled_partition(0); + let mut last = std::pin::pin!( + acc.report_build_data(data(1, (4000..6000).step_by(2).collect())) + ); + assert!(last.as_mut().poll(&mut cx).is_ready()); + let expr = current_expr(&acc); + let keys: Vec = (0..2000).chain(4000..6000).step_by(2).collect(); + assert!(expr.to_string().ends_with("AND bloom_filter"), "{expr}"); + assert!(eval_bool(&expr, keys).into_iter().all(|kept| kept)); } /// The accumulator keeps no build data after it builds the filter. @@ -1344,58 +1607,22 @@ mod tests { async fn finalized_accumulator_releases_build_data() { let acc = make_partitioned_expr_accumulator_for_test(1); let values: ArrayRef = Arc::new(Int32Array::from_iter_values(0..2000)); - let build_keys = test_build_keys(2000, Arc::clone(&values)); - let filter = Arc::downgrade(build_keys.filter.as_ref().unwrap()); - let report = |build_keys| PartitionBuildData::Partitioned { + let report = || PartitionBuildData::Partitioned { partition_id: 0, pushdown: PushdownStrategy::InList(Arc::clone(&values)), bounds: bounds(0, 1999), keys_have_null: false, - build_keys, + num_distinct: 2000, + keys: array_keys(Arc::clone(&values)), }; - acc.report_build_data(report(build_keys)).await.unwrap(); - assert_eq!(filter.strong_count(), 0); + acc.report_build_data(report()).await.unwrap(); assert_eq!(Arc::strong_count(&values), 1); // A late report or cancellation changes nothing - acc.report_build_data(report(BuildKeys::default())) - .await - .unwrap(); + acc.report_build_data(report()).await.unwrap(); acc.report_canceled_partition(0); assert_eq!(Arc::strong_count(&values), 1); } - /// The stream that takes the filter of a shared build may report last. - #[tokio::test] - async fn collect_left_uses_filter_of_later_report() { - let mut acc = make_collect_left_accumulator_for_test(); - let AccumulatedBuildData::CollectLeft { - expected_reports, .. - } = &mut acc.inner.get_mut().data - else { - panic!("expected collect-left"); - }; - *expected_reports = 2; - let values: ArrayRef = Arc::new(Int32Array::from_iter_values(0..2000)); - let report = |build_keys| PartitionBuildData::CollectLeft { - pushdown: PushdownStrategy::Large(None), - bounds: bounds(0, 1999), - keys_have_null: false, - build_keys, - }; - let without_filter = BuildKeys { - filter: None, - num_distinct: 2000, - }; - let (a, b) = futures::join!( - acc.report_build_data(report(without_filter)), - acc.report_build_data(report(test_build_keys(2000, values))), - ); - a.unwrap(); - b.unwrap(); - let expr = current_expr(&acc).to_string(); - assert!(expr.ends_with("AND bloom_filter"), "{expr}"); - } - /// Partitions that each fit an InList but together exceed the InList limit get /// a bloom filter of all their keys. #[test] @@ -1407,10 +1634,11 @@ mod tests { pushdown: in_list(&keys), bounds: bounds(*keys.iter().min().unwrap(), *keys.iter().max().unwrap()), keys_have_null: false, - build_keys: test_build_keys(keys.len(), Arc::new(Int32Array::from(keys))), + num_distinct: keys.len(), + keys: array_keys(Arc::new(Int32Array::from(keys))), }) }; - acc.build_filter(FinalizeInput::Partitioned(vec![ + acc.build_filter_for_test(FinalizeInput::Partitioned(vec![ in_list_partition(vec![1, 3]), in_list_partition(vec![5, 7]), ])) @@ -1426,7 +1654,7 @@ mod tests { // At the limit: one InList with the keys of both partitions let mut acc = make_partitioned_expr_accumulator_for_test(2); acc.global_filter_options.inlist_max_distinct_values = 4; - acc.build_filter(FinalizeInput::Partitioned(vec![ + acc.build_filter_for_test(FinalizeInput::Partitioned(vec![ in_list_partition(vec![1, 3]), in_list_partition(vec![5, 7]), ])) @@ -1446,13 +1674,11 @@ mod tests { pushdown: in_list(&rows), bounds: bounds(*rows.iter().min().unwrap(), *rows.iter().max().unwrap()), keys_have_null: false, - build_keys: test_build_keys( - num_distinct, - Arc::new(Int32Array::from(rows)), - ), + num_distinct, + keys: array_keys(Arc::new(Int32Array::from(rows))), }) }; - acc.build_filter(FinalizeInput::Partitioned(vec![ + acc.build_filter_for_test(FinalizeInput::Partitioned(vec![ partition(vec![1, 1, 3, 1, 3], 2), partition(vec![5, 5, 5], 1), ])) @@ -1493,8 +1719,8 @@ mod tests { #[test] fn partitioned_canceled_partition_does_not_widen_filter() { let acc = make_partitioned_expr_accumulator_for_test(3); - acc.build_filter(FinalizeInput::Partitioned(vec![ - reported_map((0..1000).collect()), + acc.build_filter_for_test(FinalizeInput::Partitioned(vec![ + reported_map((0..1000).map(|k| k * 2).collect()), PartitionStatus::CanceledUnknown, reported(PushdownStrategy::Empty, no_bounds()), ])) @@ -1502,17 +1728,20 @@ mod tests { let expr = current_expr(&acc); assert_eq!( expr.to_string(), - "probe_key@0 >= 0 AND probe_key@0 <= 999 AND bloom_filter" + "probe_key@0 >= 0 AND probe_key@0 <= 1998 AND bloom_filter" ); - assert_eq!(eval_bool(&expr, (0..1000).collect()), vec![true; 1000]); - assert_eq!(eval_bool(&expr, vec![-1, 1000]), vec![false, false]); + assert_eq!( + eval_bool(&expr, (0..1000).map(|k| k * 2).collect()), + vec![true; 1000] + ); + assert_eq!(eval_bool(&expr, vec![-1, 1999]), vec![false, false]); } /// Only canceled partitions: no stream probes, so the filter rejects every row. #[test] fn partitioned_all_canceled_partitions_set_filter_to_false() { let acc = make_partitioned_expr_accumulator_for_test(2); - acc.build_filter(FinalizeInput::Partitioned(vec![ + acc.build_filter_for_test(FinalizeInput::Partitioned(vec![ PartitionStatus::CanceledUnknown, PartitionStatus::CanceledUnknown, ])) @@ -1524,7 +1753,7 @@ mod tests { #[test] fn partitioned_all_empty_partitions_set_filter_to_false() { let acc = make_partitioned_expr_accumulator_for_test(2); - acc.build_filter(FinalizeInput::Partitioned(vec![ + acc.build_filter_for_test(FinalizeInput::Partitioned(vec![ reported(PushdownStrategy::Empty, no_bounds()), reported(PushdownStrategy::Empty, no_bounds()), ])) @@ -1546,13 +1775,11 @@ mod tests { pushdown: PushdownStrategy::Large(None), bounds: no_bounds(), keys_have_null: false, - build_keys: test_build_keys( - keys.len(), - Arc::new(Float64Array::from(keys)), - ), + num_distinct: keys.len(), + keys: array_keys(Arc::new(Float64Array::from(keys))), }) }; - acc.build_filter(FinalizeInput::Partitioned(vec![ + acc.build_filter_for_test(FinalizeInput::Partitioned(vec![ float_partition((0..1000).map(f64::from).collect()), float_partition((1000..2000).map(f64::from).collect()), ])) @@ -1570,7 +1797,7 @@ mod tests { let mut acc = make_partitioned_expr_accumulator_for_test(2); let pool: Arc = Arc::new(GreedyMemoryPool::new(1024)); acc.global_filter_options.memory_pool = Some(Arc::clone(&pool)); - acc.build_filter(FinalizeInput::Partitioned(vec![ + acc.build_filter_for_test(FinalizeInput::Partitioned(vec![ reported_map((0..1000).map(|k| k * 4).collect()), reported_map((0..1000).map(|k| k * 4 + 1).collect()), ])) @@ -1584,34 +1811,12 @@ mod tests { assert_eq!(pool.reserved(), 0); } - /// A partition without hashes (too big alone, or its memory reservation - /// failed) means no bloom filter: the filter keeps only the bounds and the - /// pruning bitmap. - #[test] - fn partition_without_hashes_skips_bloom_filter() { - let acc = make_partitioned_expr_accumulator_for_test(2); - let mut no_hashes = reported_map((1_000_000..1_000_100).collect()); - if let PartitionStatus::Reported(data) = &mut no_hashes { - data.build_keys.filter = None; - } - acc.build_filter(FinalizeInput::Partitioned(vec![ - reported_map((0..100).collect()), - no_hashes, - ])) - .unwrap(); - let expr = current_expr(&acc); - assert_eq!( - expr.to_string(), - "probe_key@0 >= 0 AND probe_key@0 <= 1000099 AND key_range_pruning" - ); - } - /// The bloom filter carries one key-range bitmap for all partitions, so scans /// can prune containers with it. #[test] fn global_bloom_filter_has_merged_pruning_bitmap() { let acc = make_partitioned_expr_accumulator_for_test(2); - acc.build_filter(FinalizeInput::Partitioned(vec![ + acc.build_filter_for_test(FinalizeInput::Partitioned(vec![ reported_map((0..100).collect()), reported_map((1_000_000..1_000_100).collect()), ])) @@ -1632,7 +1837,7 @@ mod tests { fn collect_left_updates_with_membership_only() { let acc = make_collect_left_accumulator_for_test(); - acc.build_filter(FinalizeInput::CollectLeft(reported( + acc.build_filter_for_test(FinalizeInput::CollectLeft(reported( in_list(&[1, 2, 3]), no_bounds(), ))) @@ -1647,7 +1852,7 @@ mod tests { let acc = make_collect_left_accumulator_for_test(); let initial_generation = acc.dynamic_filter.snapshot_generation(); - acc.build_filter(FinalizeInput::CollectLeft(reported( + acc.build_filter_for_test(FinalizeInput::CollectLeft(reported( PushdownStrategy::Empty, no_bounds(), ))) @@ -1666,7 +1871,7 @@ mod tests { fn partitioned_one_real_partition_with_rest_empty_uses_its_in_list() { let acc = make_partitioned_expr_accumulator_for_test(3); - acc.build_filter(FinalizeInput::Partitioned(vec![ + acc.build_filter_for_test(FinalizeInput::Partitioned(vec![ reported(PushdownStrategy::Empty, no_bounds()), reported(in_list(&[2]), no_bounds()), reported(PushdownStrategy::Empty, no_bounds()), @@ -1698,7 +1903,8 @@ mod tests { pushdown: PushdownStrategy::Empty, bounds: PartitionBounds::new(vec![]), keys_have_null: false, - build_keys: BuildKeys::default(), + num_distinct: 0, + keys: Arc::new(|_| Ok(())), }, ) .unwrap(); @@ -1746,7 +1952,8 @@ mod tests { pushdown: in_list(&[1, 2]), bounds: bounds(1, 2), keys_have_null: false, - build_keys: BuildKeys::default(), + num_distinct: 0, + keys: no_keys(), }); let mut report = std::pin::pin!(report); let mut cx = std::task::Context::from_waker(futures::task::noop_waker_ref()); @@ -1814,7 +2021,8 @@ mod tests { pushdown, bounds, keys_have_null: true, - build_keys: BuildKeys::default(), + num_distinct: 0, + keys: no_keys(), }) } @@ -1845,7 +2053,7 @@ mod tests { fn partitioned_null_keys_in_one_partition_widen_whole_global_filter() { let acc = null_equal_partitioned_accumulator(2); - acc.build_filter(FinalizeInput::Partitioned(vec![ + acc.build_filter_for_test(FinalizeInput::Partitioned(vec![ reported(in_list(&[1]), no_bounds()), reported_with_null_keys(in_list(&[2]), no_bounds()), ])) @@ -1868,7 +2076,7 @@ mod tests { fn partitioned_canceled_partition_keeps_null_keys_of_live_partition() { let acc = null_equal_partitioned_accumulator(2); - acc.build_filter(FinalizeInput::Partitioned(vec![ + acc.build_filter_for_test(FinalizeInput::Partitioned(vec![ PartitionStatus::CanceledUnknown, reported_with_null_keys(in_list(&[2]), no_bounds()), ])) @@ -2009,7 +2217,7 @@ mod tests { }) .collect(), ); - acc.build_filter(FinalizeInput::CollectLeft(reported( + acc.build_filter_for_test(FinalizeInput::CollectLeft(reported( PushdownStrategy::Large(None), bounds, ))) diff --git a/datafusion/physical-plan/src/joins/hash_join/stream.rs b/datafusion/physical-plan/src/joins/hash_join/stream.rs index 3be041626dc59..92280e8986653 100644 --- a/datafusion/physical-plan/src/joins/hash_join/stream.rs +++ b/datafusion/physical-plan/src/joins/hash_join/stream.rs @@ -30,7 +30,7 @@ use crate::joins::PartitionMode; use crate::joins::hash_join::exec::JoinLeftData; use crate::joins::hash_join::not_in::NotInProbe; use crate::joins::hash_join::shared_bounds::{ - BuildKeys, PartitionBounds, PartitionBuildData, SharedBuildAccumulator, + PartitionBounds, PartitionBuildData, SharedBuildAccumulator, build_keys, }; use crate::joins::join_hash_map::ProbeOffset; use crate::joins::join_hash_map::ProbeOffset::AtRow; @@ -319,10 +319,6 @@ impl BuildReportHandle { } } - fn has_accumulator(&self) -> bool { - self.build_accumulator.is_some() - } - fn schedule(&mut self, build_data: PartitionBuildData) { let Some(build_accumulator) = &self.build_accumulator else { // Defensive no-op terminal state; current callers avoid scheduling @@ -659,9 +655,12 @@ impl HashJoinStream { fn transition_after_build_collected( &mut self, left_data: &Arc, - ) -> HashJoinStreamState { - if !self.build_report.has_accumulator() { - return Self::state_after_build_ready(self.join_type, left_data.as_ref()); + ) -> Result { + if self.build_report.build_accumulator.is_none() { + return Ok(Self::state_after_build_ready( + self.join_type, + left_data.as_ref(), + )); } let pushdown = left_data.membership().clone(); @@ -677,10 +676,8 @@ impl HashJoinStream { .iter() .any(|array| array.logical_null_count() > 0); - let build_keys = BuildKeys { - filter: left_data.report_partition_filter(), - num_distinct: left_data.map().num_of_distinct_key(), - }; + let num_distinct = left_data.map().num_of_distinct_key(); + let keys = build_keys(left_data.build()); let build_data = match self.mode { PartitionMode::Partitioned => PartitionBuildData::Partitioned { @@ -688,13 +685,15 @@ impl HashJoinStream { pushdown, bounds, keys_have_null, - build_keys, + num_distinct, + keys, }, PartitionMode::CollectLeft => PartitionBuildData::CollectLeft { pushdown, bounds, keys_have_null, - build_keys, + num_distinct, + keys, }, PartitionMode::Auto => unreachable!( "PartitionMode::Auto should not be present at execution time. This is a bug in DataFusion, please report it!" @@ -702,7 +701,7 @@ impl HashJoinStream { }; self.build_report.schedule(build_data); - HashJoinStreamState::WaitPartitionBoundsReport + Ok(HashJoinStreamState::WaitPartitionBoundsReport) } /// Separate implementation function that unpins the [`HashJoinStream`] so @@ -796,7 +795,7 @@ impl HashJoinStream { // not the build side (left). The probe-side NULL check happens during process_probe_batch. // The probe_side_has_null flag will be set there if any probe batch contains NULL. - self.state = self.transition_after_build_collected(&left_data); + self.state = self.transition_after_build_collected(&left_data)?; self.build_side = BuildSide::Ready(BuildSideReadyState { left_data }); Poll::Ready(Ok(StatefulStreamResult::Continue)) @@ -1284,7 +1283,8 @@ mod tests { pushdown: PushdownStrategy::Empty, bounds: PartitionBounds::new(vec![]), keys_have_null: false, - build_keys: Default::default(), + num_distinct: 0, + keys: Arc::new(|_| Ok(())), } } diff --git a/datafusion/sqllogictest/test_files/information_schema.slt b/datafusion/sqllogictest/test_files/information_schema.slt index 4cce77659cefd..91c50cd567a85 100644 --- a/datafusion/sqllogictest/test_files/information_schema.slt +++ b/datafusion/sqllogictest/test_files/information_schema.slt @@ -498,8 +498,8 @@ datafusion.optimizer.enable_window_limits true When set to true, the optimizer w datafusion.optimizer.enable_window_topn true When set to true, the optimizer will replace Filter(rn<=K) → Window(ROW_NUMBER, RANK or DENSE_RANK) → Sort patterns with a PartitionedTopKExec that keeps only the top K rows of each window partition, avoiding a full sort of the input. datafusion.optimizer.expand_views_at_output false When set to true, if the returned type is a view type then the output will be coerced to a non-view. Coerces `Utf8View` to `LargeUtf8`, and `BinaryView` to `LargeBinary`. datafusion.optimizer.filter_null_join_keys false When set to true, the optimizer will insert filters before a join between a nullable and non-nullable column to filter out nulls on the nullable side. This filter can add additional overhead when the file format does not fully support predicate push down. -datafusion.optimizer.hash_join_bloom_filter_fpp 0.01 Target false positive probability of the bloom filter in the dynamic filter of a hash join. When the build side of a hash join has more distinct keys than `hash_join_inlist_pushdown_max_distinct_values` (over all partitions), the dynamic filter tests probe rows against one bloom filter of all build side keys. Each build partition fills a filter of its keys, sized for the estimated number of distinct keys of all partitions at this probability. Then one task ORs the partition filters and folds the result to this probability. -datafusion.optimizer.hash_join_bloom_filter_max_bytes 8388608 Maximum size, in bytes, of the bloom filter in the dynamic filter of a hash join (and of the filter of each build partition). A build side that needs a larger filter at `hash_join_bloom_filter_fpp` gets no bloom filter: such a large build side is rarely selective, so the filter is too big to be useful. The dynamic filter then tests only the min/max bounds (and keeps key range pruning). +datafusion.optimizer.hash_join_bloom_filter_fpp 0.01 Target false positive probability of the bloom filter in the dynamic filter of a hash join. When the build side of a hash join has more distinct keys than `hash_join_dynamic_filter_inlist_max_distinct_values` (over all partitions), the dynamic filter tests probe rows against one bloom filter of all build side keys. After every build partition reports its number of distinct keys, the join sizes the filter for their sum at this probability, and the build tasks fill it together. +datafusion.optimizer.hash_join_bloom_filter_max_bytes 8388608 Maximum size, in bytes, of the bloom filter in the dynamic filter of a hash join. A build side that needs a larger filter at `hash_join_bloom_filter_fpp` gets no bloom filter: such a large build side is rarely selective, so the filter is too big to be useful. The dynamic filter then tests only the min/max bounds (and keeps key range pruning). datafusion.optimizer.hash_join_bloom_filter_max_fpp 0.15 The bloom filter of a hash join dynamic filter is not used when its estimated false positive probability is above this value (the filter is saturated, a safety net). Then the dynamic filter tests only the min/max bounds of the build keys, because a saturated filter removes few rows and costs CPU time for each probe row. datafusion.optimizer.hash_join_dynamic_filter_inlist_max_distinct_values 20 Maximum number of distinct build side keys, over all partitions of a hash join, for the global dynamic filter to test membership with one InList of all keys. Above this limit the dynamic filter uses a bloom filter (see `hash_join_bloom_filter_fpp`). Every partition must also use an InList by itself (`hash_join_inlist_pushdown_max_distinct_values` and `hash_join_inlist_pushdown_max_size`). For larger sets, a bloom filter is cheaper per probe row than an InList. datafusion.optimizer.hash_join_inlist_pushdown_max_distinct_values 150 Maximum number of distinct values (rows) in the build side of a hash join to be pushed down as an InList expression for dynamic filtering. Build sides with more rows than this will use hash table lookups instead. Set to 0 to always use hash table lookups. This provides an additional limit beyond `hash_join_inlist_pushdown_max_size` to prevent very large IN lists that might not provide much benefit over hash table lookups. This uses the deduplicated row count once the build side has been evaluated. The default is 150 values per partition. This is inspired by Trino's `max-filter-keys-per-column` setting. See: diff --git a/docs/source/user-guide/configs.md b/docs/source/user-guide/configs.md index f22ce39eb10ef..7cdf2f3323d0c 100644 --- a/docs/source/user-guide/configs.md +++ b/docs/source/user-guide/configs.md @@ -175,8 +175,8 @@ The following configuration settings are available: | datafusion.optimizer.hash_join_inlist_pushdown_max_size | 131072 | Maximum size in bytes for the build side of a hash join to be pushed down as an InList expression for dynamic filtering. Build sides larger than this will use hash table lookups instead. Set to 0 to always use hash table lookups. InList pushdown can be more efficient for small build sides because it can result in better statistics pruning as well as use any bloom filters present on the scan side. InList expressions are also more transparent and easier to serialize over the network in distributed uses of DataFusion. On the other hand InList pushdown requires making a copy of the data and thus adds some overhead to the build side and uses more memory. This setting is per-partition, so we may end up using `hash_join_inlist_pushdown_max_size` * `target_partitions` memory. The default is 128kB per partition. This should allow point lookup joins (e.g. joining on a unique primary key) to use InList pushdown in most cases but avoids excessive memory usage or overhead for larger joins. | | datafusion.optimizer.hash_join_inlist_pushdown_max_distinct_values | 150 | Maximum number of distinct values (rows) in the build side of a hash join to be pushed down as an InList expression for dynamic filtering. Build sides with more rows than this will use hash table lookups instead. Set to 0 to always use hash table lookups. This provides an additional limit beyond `hash_join_inlist_pushdown_max_size` to prevent very large IN lists that might not provide much benefit over hash table lookups. This uses the deduplicated row count once the build side has been evaluated. The default is 150 values per partition. This is inspired by Trino's `max-filter-keys-per-column` setting. See: | | datafusion.optimizer.hash_join_dynamic_filter_inlist_max_distinct_values | 20 | Maximum number of distinct build side keys, over all partitions of a hash join, for the global dynamic filter to test membership with one InList of all keys. Above this limit the dynamic filter uses a bloom filter (see `hash_join_bloom_filter_fpp`). Every partition must also use an InList by itself (`hash_join_inlist_pushdown_max_distinct_values` and `hash_join_inlist_pushdown_max_size`). For larger sets, a bloom filter is cheaper per probe row than an InList. | -| datafusion.optimizer.hash_join_bloom_filter_fpp | 0.01 | Target false positive probability of the bloom filter in the dynamic filter of a hash join. When the build side of a hash join has more distinct keys than `hash_join_inlist_pushdown_max_distinct_values` (over all partitions), the dynamic filter tests probe rows against one bloom filter of all build side keys. Each build partition fills a filter of its keys, sized for the estimated number of distinct keys of all partitions at this probability. Then one task ORs the partition filters and folds the result to this probability. | -| datafusion.optimizer.hash_join_bloom_filter_max_bytes | 8388608 | Maximum size, in bytes, of the bloom filter in the dynamic filter of a hash join (and of the filter of each build partition). A build side that needs a larger filter at `hash_join_bloom_filter_fpp` gets no bloom filter: such a large build side is rarely selective, so the filter is too big to be useful. The dynamic filter then tests only the min/max bounds (and keeps key range pruning). | +| datafusion.optimizer.hash_join_bloom_filter_fpp | 0.01 | Target false positive probability of the bloom filter in the dynamic filter of a hash join. When the build side of a hash join has more distinct keys than `hash_join_dynamic_filter_inlist_max_distinct_values` (over all partitions), the dynamic filter tests probe rows against one bloom filter of all build side keys. After every build partition reports its number of distinct keys, the join sizes the filter for their sum at this probability, and the build tasks fill it together. | +| datafusion.optimizer.hash_join_bloom_filter_max_bytes | 8388608 | Maximum size, in bytes, of the bloom filter in the dynamic filter of a hash join. A build side that needs a larger filter at `hash_join_bloom_filter_fpp` gets no bloom filter: such a large build side is rarely selective, so the filter is too big to be useful. The dynamic filter then tests only the min/max bounds (and keeps key range pruning). | | datafusion.optimizer.hash_join_bloom_filter_max_fpp | 0.15 | The bloom filter of a hash join dynamic filter is not used when its estimated false positive probability is above this value (the filter is saturated, a safety net). Then the dynamic filter tests only the min/max bounds of the build keys, because a saturated filter removes few rows and costs CPU time for each probe row. | | datafusion.optimizer.default_filter_selectivity | 20 | The default filter selectivity used by Filter Statistics when an exact selectivity cannot be determined. Valid values are between 0 (no selectivity) and 100 (all rows are selected). | | datafusion.optimizer.prefer_existing_union | false | When set to true, the optimizer will not attempt to convert Union to Interleave | From f6260925a340331db162a8745eb0e24df5407880 Mon Sep 17 00:00:00 2001 From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Tue, 1 Dec 2026 16:52:20 -0800 Subject: [PATCH 9/9] perf: use only the bounds when the build keys are dense When a single integer key has every value between its min and max, the bounds predicate already tests membership exactly. A bloom filter then rejects no row and costs a hash and a lookup for each probe row (hj Q3 and Q17). Co-Authored-By: Claude Opus 5.5 --- .../src/joins/hash_join/shared_bounds.rs | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs b/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs index 70760462de1e9..0fb2d895f12a4 100644 --- a/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs +++ b/datafusion/physical-plan/src/joins/hash_join/shared_bounds.rs @@ -11,6 +11,7 @@ use std::sync::Arc; use crate::ExecutionPlan; use crate::ExecutionPlanProperties; +use crate::joins::ArrayMap; use crate::joins::Map; use crate::joins::PartitionMode; use crate::joins::hash_join::bloom_filter::{ @@ -945,6 +946,10 @@ impl SharedBuildAccumulator { self.update_filter(&rest, Some(in_list))?; return Ok(None); } + if self.bounds_are_exact(rest.bounds.as_ref(), num_distinct) { + self.update_filter(&rest, None)?; + return Ok(None); + } rest.pruning_bitmap = self.merge_pruning_bitmaps(&non_empty, rest.bounds.as_ref()); let bloom_filter = BloomFilterBuilder::try_new( @@ -994,6 +999,26 @@ impl SharedBuildAccumulator { .update(self.preserve_probe_nulls(filter_expr, rest.keys_have_null)?) } + /// Whether the build side holds every key between its bounds, so that the + /// bounds predicate alone tests membership. + /// + /// Safe for any `num_distinct`: the bounds keep every build key, so a wrong + /// count can only make the filter less selective. + fn bounds_are_exact( + &self, + bounds: Option<&PartitionBounds>, + num_distinct: usize, + ) -> bool { + if self.on_right.len() != 1 { + return false; + } + let span = bounds.and_then(|b| b.get_column_bounds(0)).and_then(|b| { + let min = ArrayMap::key_to_u64(&b.min)?; + Some(ArrayMap::key_to_u64(&b.max)?.wrapping_sub(min)) + }); + span.and_then(|span| span.checked_add(1)) == Some(num_distinct as u64) + } + /// One [`KeyRangeBitmap`] for the keys of all partitions, so that scans can /// prune containers (for example parquet row groups and pages) with the /// global filter. Only for a single integer key. @@ -1811,6 +1836,39 @@ mod tests { assert_eq!(pool.reserved(), 0); } + /// The bounds of a build side that holds every key of its range need no bloom + /// filter. A range with one missing key does. + #[test] + fn dense_keys_use_only_the_bounds() { + let pool: Arc = Arc::new(UnboundedMemoryPool::default()); + let filter = |parts: Vec>| { + let mut acc = make_partitioned_expr_accumulator_for_test(2); + acc.global_filter_options.memory_pool = Some(Arc::clone(&pool)); + let parts = parts.into_iter().map(reported_map).collect(); + acc.build_filter_for_test(FinalizeInput::Partitioned(parts)) + .unwrap(); + (current_expr(&acc), acc) + }; + let part = |r: i32| (-2000..2000).filter(|k| k & 1 == r).collect::>(); + + let (expr, _acc) = filter(vec![part(0), part(1)]); + assert_eq!( + expr.to_string(), + "probe_key@0 >= -2000 AND probe_key@0 <= 1999" + ); + assert_eq!( + eval_bool(&expr, vec![-2001, -2000, 0, 1999, 2000]), + [false, true, true, true, false] + ); + // The unused filter is released while the accumulator is alive + assert_eq!(pool.reserved(), 0); + + let mut one_missing = part(1); + one_missing.remove(1000); + let (expr, _acc) = filter(vec![part(0), one_missing]); + assert!(expr.to_string().ends_with("AND bloom_filter"), "{expr}"); + } + /// The bloom filter carries one key-range bitmap for all partitions, so scans /// can prune containers with it. #[test]