feat: warn on NULL equality predicates#22948
Open
ametel01 wants to merge 2 commits into
Open
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.
Which issue does this PR close?
Diagnosticwhen doing= NULL#14434.Rationale for this change
SQL comparisons such as
expr = NULLandexpr <> NULLevaluate toNULLunder SQL three-valued logic, not totrueorfalse. When those comparisons appear in predicate contexts such asWHERE,JOIN ON, orHAVING, they are almost always a user mistake whereIS NULLorIS NOT NULLwas intended.This PR emits non-fatal diagnostic warnings for those cases so callers that surface
Diagnosticinformation can help users find and fix the query without changing planning success or query semantics.What changes are included in this PR?
SqlToRel, withSqlToRel::take_warnings()to drain non-fatal planning diagnostics after planning.= NULLand<> NULLcomparisons.WHERE,JOIN ON, andHAVINGplanning paths.AND/ORbinary predicates andCASE WHENconditions, without warning for projection-only expressions likeSELECT col = NULL.Diagnostic::new_warningentries with a primary span on the comparison expression and help pointing at theNULLliteral when spans are available.Are these changes tested?
Yes. Added regression coverage in
datafusion/sql/tests/cases/diagnostic.rsfor:WHERE col = NULLWHERE NULL = colWHERE col <> NULLJOIN ... ON col = NULLHAVING ... = NULLCASE WHEN col = NULLinside a predicateIS NULLSELECT col = NULLValidation run locally:
Are there any user-facing changes?
Yes, but non-breaking. SQL planning can now collect warning diagnostics for likely mistaken
NULLequality predicates. Consumers can retrieve them withSqlToRel::take_warnings()and decide how to present them. Planning still succeeds and query semantics are unchanged.