Fix incorrect results with NOT EXISTS and IS DISTINCT FROM operator (#19680)#19811
Merged
Mytherin merged 2 commits intoduckdb:mainfrom Nov 20, 2025
Merged
Fix incorrect results with NOT EXISTS and IS DISTINCT FROM operator (#19680)#19811Mytherin merged 2 commits intoduckdb:mainfrom
Mytherin merged 2 commits intoduckdb:mainfrom
Conversation
…uckdb#19680) This fixes a bug where queries using NOT EXISTS with IS DISTINCT FROM returned incorrect results due to improper handling of NULL semantics in the optimizer. The issue was that the optimizer's deliminator incorrectly treated DISTINCT FROM variants the same as regular equality/inequality comparisons, which have different NULL handling: - IS DISTINCT FROM: NULL-aware (NULL IS DISTINCT FROM NULL = FALSE) - != or =: NULL-unaware (NULL != NULL = NULL, filters out NULLs) Changes: - Added negation support for COMPARE_DISTINCT_FROM and COMPARE_NOT_DISTINCT_FROM in expression type handling - Updated parser to properly negate IS DISTINCT FROM expressions when wrapped with NOT - Fixed deliminator optimizer to preserve DISTINCT FROM semantics and avoid incorrectly converting to != or adding unnecessary IS NOT NULL filters - Added regression test for correlated EXISTS with derived tables
Tmonster
approved these changes
Nov 18, 2025
Contributor
Tmonster
left a comment
There was a problem hiding this comment.
Thanks for the PR! Was a tricky one to understand, but I finally understood it.
Just one small nit on the comments, otherwise LGTM.
src/optimizer/deliminator.cpp
Outdated
| // DELIM JOIN need to do that for not DELIM_GET side. Easiest way is to change the | ||
| // comparison expression type. See duckdb/duckdb#16803 | ||
| if (delim_join.join_type != JoinType::MARK) { | ||
| // NOTE: We should NOT convert DISTINCT FROM to != in general, as they have different NULL semantics |
Contributor
There was a problem hiding this comment.
nit: While this is true, I don't think these comments provide much value here, as there is a specific case where we do want to change it. See #16910 which has almost the same query, expect the original join is a != and not a compare distinct from
| // NOTE: We should NOT convert DISTINCT FROM to != in general, as they have different NULL semantics | ||
| // - DISTINCT FROM: NULL IS DISTINCT FROM NULL = FALSE (NULL-aware) | ||
| // - !=: NULL != NULL = NULL (filters out NULL) | ||
| // Only convert if the ORIGINAL join had != or = (not DISTINCT FROM variants) |
Contributor
There was a problem hiding this comment.
🙏 nice, this is a case I missed.
Contributor
Author
|
@Tmonster Thanks a lot for your review. I have updated the PR as you suggested. Best, Henry |
github-actions bot
pushed a commit
to duckdb/duckdb-r
that referenced
this pull request
Dec 31, 2025
Fix incorrect results with NOT EXISTS and IS DISTINCT FROM operator (duckdb/duckdb#19680) (duckdb/duckdb#19811)
github-actions bot
added a commit
to duckdb/duckdb-r
that referenced
this pull request
Dec 31, 2025
Fix incorrect results with NOT EXISTS and IS DISTINCT FROM operator (duckdb/duckdb#19680) (duckdb/duckdb#19811) Co-authored-by: krlmlr <krlmlr@users.noreply.github.com>
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.
Fixes #19680
This fixes a bug where queries using
NOT EXISTSwithIS DISTINCT FROMreturned incorrect results due to improper handling of NULL semantics in the optimizer.The issue was that the optimizer's deliminator incorrectly treated
DISTINCT FROMvariants the same as regular equality/inequality comparisons, which have different NULL handling:IS DISTINCT FROM: NULL-aware (NULL IS DISTINCT FROM NULL = FALSE)Incorrect Query Plan
The buggy plan shows two critical issues:
Solution
This PR adds proper support for DISTINCT FROM operators throughout the optimization pipeline:
in expression type handling.(src/common/enums/expression_type.cpp)