release-2.1: sql/sem/tree: bugfix in handling unary negatives #39368
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 #39245.
/cc @cockroachdb/release
When performing constant folding, we will now always evaluate unary
negatives right away. To facilitate this, this commit extracts a NumExpr
interface that is implemented by *NumVal. The new interface has methods
to access the constant.Value of the *NumVal, and this accessor always
will perform the necessary unary negation operator.
This works because unary negation has high precedence (only preceded by
the typecast operation).
This change requires us to inspect the Negative flags of both the Expr
and inner Decimal value when resolving a NumExpr that contains a
Decimal, since on some operations like division, the negative value will
now get folded into the Decimal.
Tests have not been backported since there have been significant changes
in testing infrastructure.
Release note (bug fix): unary negatives in constant arithmetic expressions
are no longer ignored.