fix: SortMergeJoin full outer join incorrectly matches rows when filter evaluates to NULL#21660
Merged
mbutrovich merged 5 commits intoapache:mainfrom Apr 16, 2026
Merged
Conversation
mbutrovich
added a commit
to mbutrovich/datafusion-comet
that referenced
this pull request
Apr 16, 2026
martin-g
approved these changes
Apr 16, 2026
Contributor
Author
|
CI was green in Comet for all Spark SQL tests with SMJ with filters enabled: https://github.com/apache/datafusion-comet/actions/runs/24487781374 The only failure was this Miri issue which is now fixed in DataFusion main: #21663 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
While enabling
SortMergeJoinExecwith filters in DataFusion Comet, I hit a correctness bug in DataFusion'sSortMergeJoinExecfor full outer joins with nullable filter columns. The bug was originally surfaced via the SPARK-43113 reproducer.When a join filter expression evaluates to
NULL(e.g.,l.b < (r.b + 1)wherer.bisNULL), the full outer join treats the row pair as matched instead of unmatched. Per SQL semantics,NULLin a boolean filter context isfalse(not satisfied), so the rows should be emitted as separate unmatched rows.The bug has been present since filtered full outer join support was added to SMJ in #12764 / #13369. It was never caught because:
Int32Array::from_iter_values(), which never producesNULLvalues.NULL.What changes are included in this PR?
Root cause: The full outer join code path had a special case that preserved raw
NULLvalues from the filter expression result (pre_mask) instead of converting them tofalseviaprep_null_mask_filterlike left/right outer joins do. This caused two problems:Streamed (left) side: In
get_corrected_filter_mask(),NULLentries infilter_maskare treated as "pass through" (for pre-null-joined rows fromappend_nulls()). But when the filter expression itself returnsNULL, those entries also appear asNULLin the mask — and get incorrectly treated as matched. This produced wrong join output (matched rows instead of unmatched).Buffered (right) side:
BooleanArray::value()was called onNULLpositions inpre_maskto updateFilterState. At NULL positions, the values buffer contains a deterministic but semantically meaningless result (computed from the default zero-storage of NULL inputs). For some rows this value happens to betrue, which incorrectly marks unmatched buffered rows asSomePassedand silently drops them from the output.Fix: Remove the full outer join special case in
materializing_stream.rs. All outer join types now uniformly use the null-correctedmask(whereNULL→falseviaprep_null_mask_filter) for both deferred filtering metadata andFilterStatetracking. Semi/anti/mark joins are unaffected — they useBitwiseSortMergeJoinStreamwhich already converts NULLs tofalse.Tests:
join_full_null_filter_resultreproducing the SPARK-43113 scenario with a nullable right-side column.make_staggered_batches_i32injoin_fuzz.rsto inject ~10%NULLvalues into the filter column (x), so the fuzz tests exerciseNULLfilter handling across all join types.Are these changes tested?
Yes.
join_full_null_filter_result) directly reproduces the bug.test_full_join_1k_filteredwhich comparesHashJoinExecvsSortMergeJoinExecand would have caught this bug if the fuzz data had includedNULLs.Are there any user-facing changes?
Full outer sort-merge joins with filters involving nullable columns now produce correct results. Previously, rows where the filter evaluated to
NULLwere incorrectly included as matched; they are now correctly emitted as unmatched (null-joined) rows.