-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
[fix](DECIMALV3)fix cumulative precision when literal and DECIMALV3 operations in Legacy #20354
Merged
Conversation
This file contains 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
run buildall |
Mryange
force-pushed
the
feature-decv3-reg
branch
from
June 2, 2023 13:51
59ba18a
to
a436c6d
Compare
run buildall |
github-actions
bot
added
the
area/planner
Issues or PRs related to the query planner
label
Jun 2, 2023
run p0 |
Mryange
force-pushed
the
feature-decv3-reg
branch
from
June 3, 2023 13:00
a436c6d
to
b2fb246
Compare
Mryange
changed the title
[fix](regression) update some regression test
[fix](DECIMALV3)fix cumulative precision when literal and DECIMALV3 operations in Legacy
Jun 3, 2023
run buildall |
run buildall |
Gabriel39
approved these changes
Jun 6, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
github-actions
bot
added
the
approved
Indicates a PR has been approved by one committer.
label
Jun 6, 2023
PR approved by at least one committer and no changes requested. |
PR approved by anyone and no changes requested. |
xiaokang
pushed a commit
that referenced
this pull request
Jun 9, 2023
…perations in Legacy (#20354) The precision handling for division with DECIMALV3 is as follows (excluding cases where division increases precision): (p1, s1) / (p2, s2) ----> (p1 + s2, s1) However, due to precision loss in division, it is considered to increase the precision of the left operand: (p1, s1) / (p2, s2) =====> (p1 + s2, s1 + s2) / (p2, s2) ----> (p1 + s2, s1) However, the legacy optimizer repeats the analyze and substitute steps for an expression, which can result in the accumulation of precision: (p1, s1) / (p2, s2) =====> (p1 + s2, s1 + s2) / (p2, s2) =====> (p1 + s2 + s2, s1 + s2 + s2) / (p2, s2) To address this, the previous approach was to forcibly convert the left operand of DECIMALV3 calculations. This results in rewriting the expression as: (p1, s1) / (p2, s2) =====> cast((p1, s1) as (p1 + s2, s1 + s2)) / (p2, s2) Then, during the substitution step, a check is performed. If it is a cast expression, the expression modified by the cast is extracted: cast((p1, s1) as (p1 + s2, s1 + s2)) =====> (p1, s1) protected Expr substituteImpl(ExprSubstitutionMap smap, ExprSubstitutionMap disjunctsMap, Analyzer analyzer) { if (isImplicitCast()) { return getChild(0).substituteImpl(smap, disjunctsMap, analyzer); } This way, there won't be repeated analysis, preventing the continuous increase in precision. However, if the left expression is a constant (literal), theoretically, the precision would continue to increase. Unfortunately, the code that was removed in this PR (#19926) obscured this issue. for (Expr child : children) { if (child instanceof DecimalLiteral && child.getType().isDecimalV3()) { ((DecimalLiteral)child).tryToReduceType(); } } An attempt will be made to reduce the precision of literals in the expressions. However, this code snippet can cause such a bug. mysql [test]>select cast(1 as DECIMALV3(16, 2)) / cast(3 as DECIMALV3(16, 2)); +-----------------------------------------------------------+ | CAST(1 AS DECIMALV3(16, 2)) / CAST(3 AS DECIMALV3(16, 2)) | +-----------------------------------------------------------+ | 0.00 | +-----------------------------------------------------------+ 1.00 / 3.00, due to reduced precision, becomes 1 / 3. <--Describe your changes.-->
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
approved
Indicates a PR has been approved by one committer.
area/planner
Issues or PRs related to the query planner
dev/2.0-beta-merged
kind/test
reviewed
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.
Proposed changes
before
after
The precision handling for division with DECIMALV3 is as follows (excluding cases where division increases precision):
(p1, s1) / (p2, s2) ----> (p1 + s2, s1)
However, due to precision loss in division, it is considered to increase the precision of the left operand:
(p1, s1) / (p2, s2) =====> (p1 + s2, s1 + s2) / (p2, s2) ----> (p1 + s2, s1)
However, the legacy optimizer repeats the analyze and substitute steps for an expression, which can result in the accumulation of precision:
(p1, s1) / (p2, s2) =====> (p1 + s2, s1 + s2) / (p2, s2) =====> (p1 + s2 + s2, s1 + s2 + s2) / (p2, s2)
To address this, the previous approach was to forcibly convert the left operand of DECIMALV3 calculations. This results in rewriting the expression as:
(p1, s1) / (p2, s2) =====> cast((p1, s1) as (p1 + s2, s1 + s2)) / (p2, s2)
Then, during the substitution step, a check is performed. If it is a cast expression, the expression modified by the cast is extracted:
cast((p1, s1) as (p1 + s2, s1 + s2)) =====> (p1, s1)
This way, there won't be repeated analysis, preventing the continuous increase in precision. However, if the left expression is a constant (literal), theoretically, the precision would continue to increase. Unfortunately, the code that was removed in this PR (#19926) obscured this issue.
An attempt will be made to reduce the precision of literals in the expressions. However, this code snippet can cause such a bug.
1.00 / 3.00, due to reduced precision, becomes 1 / 3.
<--Describe your changes.-->
Further comments
If this is a relatively large or complex change, kick off the discussion at dev@doris.apache.org by explaining why you chose the solution you did and what alternatives you considered, etc...