Skip to content

Commit 4d50daa

Browse files
committed
fix: bound dictionary null scans in left mark joins
1 parent d3ccef0 commit 4d50daa

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

‎datafusion/physical-plan/src/joins/hash_join/stream.rs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,13 +1343,23 @@ fn null_aware_left_mark_column(
13431343
left_side: &UInt64Array,
13441344
right_side: &UInt32Array,
13451345
) -> ArrayRef {
1346-
let build_key_column = &left_data.values()[0];
13471346
// Correlated joins precomputed the UNKNOWN decision per build row.
13481347
let null_indices_bitmap = correlated.then(|| left_data.null_indices_bitmap().lock());
1348+
// `LeftMark` final indices are a contiguous chunk of build rows. Limit the
1349+
// potentially allocating dictionary logical-null computation to this chunk;
1350+
// correlated joins already carry the answer in `null_indices_bitmap`.
1351+
let build_key_nulls = if correlated || left_side.is_empty() {
1352+
None
1353+
} else {
1354+
let start = left_side.value(0) as usize;
1355+
left_data.values()[0]
1356+
.slice(start, left_side.len())
1357+
.logical_nulls()
1358+
};
13491359
build_null_aware_left_mark_column(
13501360
left_side,
13511361
right_side,
1352-
build_key_column.as_ref(),
1362+
build_key_nulls.as_ref(),
13531363
null_indices_bitmap.as_deref(),
13541364
probe_summary.has_null,
13551365
probe_summary.non_empty,

‎datafusion/physical-plan/src/joins/utils.rs‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,43 +1381,43 @@ pub(crate) fn build_batch_from_indices(
13811381
/// For correlated scalar `NOT IN`, `null_indices_bitmap` carries the same UNKNOWN
13821382
/// decision per build row, scoped by the correlated equality keys.
13831383
///
1384+
/// When present, `build_key_nulls` is aligned with the output rows in
1385+
/// `build_indices`, rather than indexed by the original build row number.
1386+
///
13841387
/// This is the helper equivalent of the paper's "null bucket" and `hadNull` handling.
13851388
/// It is intentionally scoped to scalar null-aware mark joins.
13861389
pub(crate) fn build_null_aware_left_mark_column(
13871390
build_indices: &UInt64Array,
13881391
probe_indices: &UInt32Array,
1389-
build_key_column: &dyn Array,
1392+
build_key_nulls: Option<&NullBuffer>,
13901393
null_indices_bitmap: Option<&BooleanBufferBuilder>,
13911394
probe_side_has_null: bool,
13921395
probe_side_non_empty: bool,
13931396
) -> ArrayRef {
1394-
let build_key_nulls = build_key_column.logical_nulls();
1395-
13961397
// Whether an unmatched build row's mark is NULL (UNKNOWN) instead of FALSE:
13971398
// correlated joins precomputed this per row in `null_indices_bitmap`; the
13981399
// uncorrelated rules are cases 1-4 in the doc above.
1399-
let unmatched_mark_is_null = |build_idx: usize| match null_indices_bitmap {
1400-
Some(bitmap) => bitmap.get_bit(build_idx),
1401-
None if build_key_nulls
1402-
.as_ref()
1403-
.is_some_and(|nulls| nulls.is_null(build_idx)) =>
1404-
{
1405-
probe_side_non_empty
1406-
}
1407-
None => probe_side_has_null,
1408-
};
1400+
let unmatched_mark_is_null =
1401+
|build_idx: usize, output_idx: usize| match null_indices_bitmap {
1402+
Some(bitmap) => bitmap.get_bit(build_idx),
1403+
None if build_key_nulls.is_some_and(|nulls| nulls.is_null(output_idx)) => {
1404+
probe_side_non_empty
1405+
}
1406+
None => probe_side_has_null,
1407+
};
14091408

14101409
let marks: BooleanArray = build_indices
14111410
.iter()
14121411
.zip(probe_indices.iter())
1413-
.map(|(build_idx, probe_idx)| {
1412+
.enumerate()
1413+
.map(|(output_idx, (build_idx, probe_idx))| {
14141414
if probe_idx.is_some() {
14151415
return Some(true);
14161416
}
14171417
let build_idx = build_idx
14181418
.expect("LeftMark final indices should always contain build-side rows")
14191419
as usize;
1420-
if unmatched_mark_is_null(build_idx) {
1420+
if unmatched_mark_is_null(build_idx, output_idx) {
14211421
None
14221422
} else {
14231423
Some(false)

0 commit comments

Comments
 (0)