Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cherrypick-1.1: sql: fix tuple equality comparisons for tuples with NULL values #21237

Merged
merged 2 commits into from
Jan 4, 2018

Conversation

RaduBerinde
Copy link
Member

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 to NULL, correctly,
but (1, 2, 4) = (1, NULL, 5) also evaluated to NULL, incorrectly. Instead,
the latter expression should have evaluated to false. This is because tuple
comparison 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, and
ALL comparison operators, so the behavior replaced here must have simply
been 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 handling
NULLs. 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.

nvanbenschoten and others added 2 commits January 4, 2018 14:25
Fixes cockroachdb#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 to `NULL`, correctly,
but `(1, 2, 4) = (1, NULL, 5)` also evaluated to `NULL`, incorrectly. Instead,
the latter expression should have evaluated to `false`. This is because tuple
comparison 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`, and
`ALL` comparison operators, so the behavior replaced here must have simply
been 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.
PR cockroachdb#21115 fixed the logic for `tuple != tuple` which was incorrectly handling
NULLs. 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.
@RaduBerinde RaduBerinde requested review from knz, nvanbenschoten and a team January 4, 2018 19:35
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@RaduBerinde RaduBerinde merged commit bb985e9 into cockroachdb:release-1.1 Jan 4, 2018
@RaduBerinde RaduBerinde deleted the fix-null-ineq-1.1 branch January 4, 2018 23:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants