cherrypick-1.1: sql: fix tuple equality comparisons for tuples with NULL values #21237
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.
Cherry-picking the fixes in #21115 and #21230. CC @cockroachdb/release
sql: fix tuple equality comparisons for tuples with NULL values
Fixes #21113.
Previously, tuple equality evaluation would short-circuit on NULL elements
just like tuple inequality evaluation. This behavior was desired for tuple
comparisons using inequality operators but was a deviation from the SQL
standard for tuple comparisons using the equality operator.
In other words,
(1, 2, 4) > (1, NULL, 5)
evaluated toNULL
, correctly,but
(1, 2, 4) = (1, NULL, 5)
also evaluated toNULL
, incorrectly. Instead,the latter expression should have evaluated to
false
. This is because tuplecomparison should only return NULL when the non-NULL elements are not sufficient
to determine the result. Since tuple inequality is defined lexicographically,
the first NULL element encountered causes ambiguity. For tuple equality, it
may still be possible to evaluate the expression even after NULL elements are
seen, so the evaluation cannot short circuit.
This change fixes the behavior for tuple equality, bringing it inline with
PostgreSQL and MySQL.
Note that we already had very similar logic for the
IN
,ANY
,SOME
, andALL
comparison operators, so the behavior replaced here must have simplybeen an oversight when tuple comparison was introduced.
Release note (sql change/bug fix): Fix tuple equality to evaluate correctly
in the presence of NULL elements.
sql: fix not-NULL spans for tuple !=
PR #21115 fixed the logic for
tuple != tuple
which was incorrectly handlingNULLs. This change fixes the corresponding logic in the index selection code
(which is incorrectly generating
/!NULL
spans).Release note (sql change/bug fix): Fix tuple equality to evaluate correctly
in the presence of NULL elements.