release-22.1: opt: prevent overflow when normalizing comparison with constants#88970
Merged
mgartner merged 3 commits intocockroachdb:release-22.1from Sep 30, 2022
Merged
Conversation
Previously, the `NormalizeCmpPlusConst`, `NormalizeCmpMinusConst`, and
`NormalizeCmpMinusConst` would normalize expressions in the form
`var +/- const1 ? const2` and `const1 - var ? const2`, where `const1`
and `const2` were const constants and `?` is any comparison operator, to
attempt to isolate the variable on one side of the comparison. Below are
some examples of the transformations these rules made:
a+1 > 10 => a > 10-1
a-1 > 10 => a > 10+1
1-a > 10 => 1-10 > a
However, these transformations were invalid when the newly created
addition or subtraction operators overflowed. In the case of an
expression involving TIME and INTERVAL types, this could cause incorrect
query results. For example:
t-'2 hr'::INTERVAL < '23:00:00'::TIME
=>
t < '23:00:00'::TIME+'2 hr'::INTERVAL
=>
t < '01:00:00'::TIME
The first expression and last expression have different semantic meaning
because the addition of the constant TIME and constant INTERVAL
overflowed.
This commit prevents the rules from transforming expressions if they
would cause an overflow/underflow. In order to detect overflow, there
are new restrictions that prevent these rules from firing in some cases.
Notably, the datum on the RHS of the newly constructed +/- operator must
be an integer, float, decimal, or interval. Also, the result type of the
newly constructed +/- operator must be equivalent to the LHS of the
operator. Both of these restrictions are required in order to detect
overflow/underflow.
Informs cockroachdb#88128
Release note (bug fix): A bug has been fixed that caused incorrect
evaluation of expressions in the form `col +/- const1 ? const2`, where
`const1` and `const2` are constant values and `?` is any comparison
operator. The bug was caused by operator overflow when the optimizer
attempted to simplify these expressions to have a single constant value.
|
Thanks for opening a backport. Please check the backport criteria before merging:
If some of the basic criteria cannot be satisfied, ensure that the exceptional criteria are satisfied within.
Add a brief release justification to the body of your PR to justify this backport. Some other things to consider:
|
Member
rytaft
approved these changes
Sep 29, 2022
Collaborator
rytaft
left a comment
There was a problem hiding this comment.
Reviewed 8 of 8 files at r1, 3 of 3 files at r2, 4 of 4 files at r3, all commit messages.
Reviewable status:complete! 1 of 0 LGTMs obtained (waiting on @DrewKimball and @msirek)
It was recently discovered that normalization rules in the optimizer were invalid (see cockroachdb#88128). These rules were adapted from the heuristic optimizer several years ago, and they still remain in the `normalize` package even though the heuristic optimizer no longer exists. The `normalize` package is still used to normalize expressions during backfilling, so the invalid rules can cause incorrect computed column values and corrupt indexes. This commit removes these rules entirely. Informs cockroachdb#88128 Release note: None
This commit fixes minor typos introduced in cockroachdb#88199. Release note: None
4f0f144 to
8db6c09
Compare
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.
Backport:
Please see individual PRs for details.
Release justification: Fixes long-standing correctness bug in optimizer.
/cc @cockroachdb/release