release-22.2: opt: fix normalization of comparisons with constants #90368
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 1/2 commits from #90266.
/cc @cockroachdb/release
opt: fix normalization of comparisons with constants
A prior commit in #88199 attempted to fix a bug in the
NormalizeCmpPlusConst
,NormalizeCmpMinusConst
, andNormalizeCmpConstMinus
rules by checking for overflow/underflow in theaddition/subtraction of constants in a comparison expression. This was
insufficient to completely fix the bug because the transformation is
invalid if the non-normalized expression would have overflowed. Consider
an expression:
NormalizeCmpPlusConst
would successively normalize it to this:This expression is not semantically equivalent to the original
expression. It yields different results when
t
is a value that wouldunderflow when eleven hours is subtracted from it. For example, consider
t = '03:00'::TIME
:These normalization rules are only valid with types where overflow or
underflow during addition and subtraction results in an error.
This commit restricts these normalization rules to only operate on
integers, floats, and decimals, which will error if there is underflow
or overflow.
Fixes #90053
Release note (bug fix): A bug has been fixed that caused incorrect
evaluation of comparison expressions involving time and interval types,
like
col::TIME + '10 hrs'::INTERVAL' > '01:00'::TIME
.Release justification: Fixes a long-standing correctness bug in the optimizer.