Skip to content

Apache DataFusion SQL Query Engine

perf: Optimize semi-, anti-join index alignment (#22794)

Neil Conway8036b94051da
diff --git a/benchmarks/src/hj.rs b/benchmarks/src/hj.rs
index 8cd1b8b4b7..7b56e75ea9 100644
--- a/benchmarks/src/hj.rs
+++ b/benchmarks/src/hj.rs
@@ -405,6 +405,27 @@ const HASH_QUERIES: &[HashJoinQuery] = &[
build_size: "100K",
probe_size: "60M_RightAnti",
},
+ // Q22: RightSemi, Medium build (100K rows), ~1% Hit rate, fanout ~100
+ //
+ // Build Side: supplier (100K rows) collapsed onto 1K distinct keys
+ // Probe Side: lineitem (60M rows). Each matching probe row produces many
+ // duplicate probe indices before RightSemi deduplication.
+ HashJoinQuery {
+ sql: r###"SELECT l.k
+ FROM (
+ SELECT CAST(((s_suppkey - 1) % 1000) + 1 AS INT) as k
+ FROM supplier
+ ) s
+ RIGHT SEMI JOIN (
+ SELECT CAST(l_suppkey AS INT) as k
+ FROM lineitem
+ ) l
+ ON s.k = l.k"###,
+ density: 1.0,
+ prob_hit: 0.01,
+ build_size: "100K_(fanout_100)",
+ probe_size: "60M_RightSemi",
+ },
];
impl RunOpt {
diff --git a/datafusion/physical-plan/benches/hash_join_semi_anti.rs b/datafusion/physical-plan/benches/hash_join_semi_anti.rs
index 193230c1d4..1e11da36be 100644
--- a/datafusion/physical-plan/benches/hash_join_semi_anti.rs
+++ b/datafusion/physical-plan/benches/hash_join_semi_anti.rs
@@ -272,6 +272,26 @@ fn bench_hash_join_semi_anti(c: &mut Criterion) {
});
}
+ // RightSemi - 100% Density, ~1% hit rate, fanout ~100
+ // Build keys are duplicated: 100K rows over 1K distinct keys. Matching
+ // probe rows produce many duplicate probe indices before RightSemi
+ // deduplication.
+ {
+ let fanout_keys = 1_000;
+ let left_batches = build_batches(build_rows, fanout_keys, 0, &s);
+ let right_batches = build_batches(probe_rows, build_rows, 0, &s);
+ group.bench_function(
+ BenchmarkId::new("right_semi_fanout100_h1", probe_rows),
+ |b| {
+ b.iter(|| {
+ let left = make_exec(&left_batches, &s);
+ let right = make_exec(&right_batches, &s);
+ do_hash_join(left, right, JoinType::RightSemi, &rt)
+ })
+ },
+ );
+ }
+
// =========================================================================
// RightAnti Join benchmarks
// =========================================================================
diff --git a/datafusion/physical-plan/src/joins/utils.rs b/datafusion/physical-plan/src/joins/utils.rs
index 8cc93dee57..9d302a6061 100644
--- a/datafusion/physical-plan/src/joins/utils.rs
+++ b/datafusion/physical-plan/src/joins/utils.rs
@@ -1555,7 +1555,9 @@ pub(crate) fn append_right_indices(
}
}
-/// Returns `range` indices which are not present in `input_indices`
+/// Returns `range` indices which are not present in `input_indices`.
+///
+/// `input_indices` must be sorted ascending and contain no nulls.
pub(crate) fn get_anti_indices<T: ArrowPrimitiveType>(
range: Range<usize>,
input_indices: &PrimitiveArray<T>,
@@ -1563,18 +1565,51 @@ pub(crate) fn get_anti_indices<T: ArrowPrimitiveType>(
where
NativeAdapter<T>: From<<T as ArrowPrimitiveType>::Native>,
{
- let bitmap = build_range_bitmap(&range, input_indices);
- let offset = range.start;
+ debug_assert_eq!(
+ input_indices.null_count(),
+ 0,
+ "get_anti_indices requires non-null input_indices"
+ );
+ debug_assert!(
+ input_indices
+ .values()
+ .windows(2)
+ .all(|w| w[0].as_usize() <= w[1].as_usize()),
+ "get_anti_indices requires ascending input_indices"
+ );
- // get the anti index
- (range)
- .filter_map(|idx| {
- (!bitmap.get_bit(idx - offset)).then_some(T::Native::from_usize(idx))
- })
- .collect()
+ let mut next_unmatched_idx = range.start;
+ let mut output: Vec<T::Native> = Vec::with_capacity(range.len());
+
+ for &v in input_indices.values() {
+ let idx = v.as_usize();
+
+ if idx < range.start {
+ continue;
+ }
+ if idx >= range.end {
+ break;
+ }
+
+ if next_unmatched_idx < idx {
+ output.extend((next_unmatched_idx..idx).map(|idx| {
+ T::Native::from_usize(idx).expect("join index exceeds output index type")
+ }));
+ }
+ next_unmatched_idx = idx + 1;
+ }
+
+ if next_unmatched_idx < range.end {
+ output.extend((next_unmatched_idx..range.end).map(|idx| {
+ T::Native::from_usize(idx).expect("join index exceeds output index type")
+ }));
+ }
+ PrimitiveArray::<T>::new(output.into(), None)
}
-/// Returns intersection of `range` and `input_indices` omitting duplicates
+/// Returns the intersection of `range` and `input_indices`, omitting duplicates.
+///
+/// `input_indices` must be sorted ascending and contain no nulls.
pub(crate) fn get_semi_indices<T: ArrowPrimitiveType>(
range: Range<usize>,
input_indices: &PrimitiveArray<T>,
@@ -1582,14 +1617,38 @@ pub(crate) fn get_semi_indices<T: ArrowPrimitiveType>(
where
NativeAdapter<T>: From<<T as ArrowPrimitiveType>::Native>,
{
- let bitmap = build_range_bitmap(&range, input_indices);
- let offset = range.start;
- // get the semi index
- (range)
- .filter_map(|idx| {
- (bitmap.get_bit(idx - offset)).then_some(T::Native::from_usize(idx))
- })
- .collect()
+ debug_assert_eq!(
+ input_indices.null_count(),
+ 0,
+ "get_semi_indices requires non-null input_indices"
+ );
+ debug_assert!(
+ input_indices
+ .values()
+ .windows(2)
+ .all(|w| w[0].as_usize() <= w[1].as_usize()),
+ "get_semi_indices requires ascending input_indices"
+ );
+
+ let mut prev_idx: Option<usize> = None;
+ let mut output = Vec::with_capacity(input_indices.len().min(range.len()));
+
+ for &v in input_indices.values() {
+ let idx = v.as_usize();
+
+ if idx < range.start {
+ continue;
+ }
+ if idx >= range.end {
+ break;
+ }
+
+ if prev_idx.replace(idx) != Some(idx) {
+ output.push(v);
+ }
+ }
+
+ PrimitiveArray::<T>::new(output.into(), None)
}
pub(crate) fn get_mark_indices<T: ArrowPrimitiveType>(
@@ -2353,6 +2412,82 @@ mod tests {
use rstest::rstest;
+ fn assert_u32_values(array: &UInt32Array, expected: &[u32]) {
+ assert_eq!(array.values().as_ref(), expected);
+ }
+
+ #[test]
+ fn get_anti_indices_returns_unmatched_range_indices() {
+ let input = UInt32Array::from(vec![3, 5, 5]);
+
+ let result = get_anti_indices(2..8, &input);
+
+ assert_u32_values(&result, &[2, 4, 6, 7]);
+ }
+
+ #[test]
+ fn get_anti_indices_ignores_out_of_range_indices() {
+ let input = UInt32Array::from(vec![0, 1, 3, 5, 8, 12]);
+
+ let result = get_anti_indices(2..8, &input);
+
+ assert_u32_values(&result, &[2, 4, 6, 7]);
+ }
+
+ #[test]
+ fn get_anti_indices_handles_dense_matches() {
+ let input = UInt32Array::from(vec![2, 3, 4, 5]);
+
+ let result = get_anti_indices(2..6, &input);
+
+ assert!(result.is_empty());
+ }
+
+ #[test]
+ fn get_anti_indices_handles_sparse_matches() {
+ let input = UInt32Array::from(vec![0, 8]);
+
+ let result = get_anti_indices(2..6, &input);
+
+ assert_u32_values(&result, &[2, 3, 4, 5]);
+ }
+
+ #[test]
+ fn get_semi_indices_returns_distinct_matches_in_range() {
+ let input = UInt32Array::from(vec![1, 3, 3, 3, 5, 8]);
+
+ let result = get_semi_indices(2..7, &input);
+
+ assert_u32_values(&result, &[3, 5]);
+ }
+
+ #[test]
+ fn get_semi_indices_ignores_out_of_range_indices() {
+ let input = UInt32Array::from(vec![0, 1, 3, 5, 8, 12]);
+
+ let result = get_semi_indices(2..8, &input);
+
+ assert_u32_values(&result, &[3, 5]);
+ }
+
+ #[test]
+ fn get_semi_indices_handles_dense_matches() {
+ let input = UInt32Array::from(vec![2, 3, 4, 5]);
+
+ let result = get_semi_indices(2..6, &input);
+
+ assert_u32_values(&result, &[2, 3, 4, 5]);
+ }
+
+ #[test]
+ fn get_semi_indices_handles_empty_input() {
+ let input = UInt32Array::from(Vec::<u32>::new());
+
+ let result = get_semi_indices(2..6, &input);
+
+ assert!(result.is_empty());
+ }
+
fn check(
left: &[Column],
right: &[Column],