fix: NOT IN with NULL subquery returns wrong results under SortMergeJoin#22810
Draft
nathanb9 wants to merge 1 commit into
Draft
fix: NOT IN with NULL subquery returns wrong results under SortMergeJoin#22810nathanb9 wants to merge 1 commit into
nathanb9 wants to merge 1 commit into
Conversation
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.
Problem
NOT IN (subquery)is a null-aware anti join: when the subquery yields a NULL the predicate is never TRUE, so the query must return zero rows. Withprefer_hash_join = falseand multiple partitions, the planner routed the null-aware anti join toSortMergeJoinExec, which is not null-aware, so it returned wrong results. HashJoin (the default) was already correct.Proof
Expected 0 rows (the subquery contains a NULL). Before this change it returned
1. Withprefer_hash_join = trueit correctly returned 0 rows.EXPLAINshowed the wrong config selectingSortMergeJoinExec: join_type=LeftAnti.Solution
The planner already requires null-aware joins to use the CollectLeft HashJoin, and the HashJoin branch guards on
!null_aware. The SortMergeJoin branch was missing the same guard, so this adds&& !*null_awareto it. Null-aware anti joins now fall through to the CollectLeft HashJoin regardless ofprefer_hash_join.SortMergeJoinExechas nonull_awareparameter and cannot honor these semantics.Added a regression test in
subquery.slt(underprefer_hash_join = false) covering both a null-containing subquery (zero rows) and a null-free subquery (normal anti join). All 61 SortMergeJoin unit tests pass.