-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
execution: refine precision of cast as decimal in agg func #30805
Merged
ti-chi-bot
merged 34 commits into
pingcap:master
from
dragonly:dragonly/agg-cast-decimal-precision
Dec 23, 2021
Merged
Changes from 24 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
bf4c7c7
refine precision of cast as decimal in agg func
dragonly a101ff5
fix for unsigned
dragonly 4996c92
fix typo
dragonly f2dfa13
fix: only modify args decimal precision when function is sum
dragonly 55c6523
fix plan test cases
dragonly 0661403
fix plan test cases
dragonly cd0d33a
fix plan test cases
dragonly 1c5f59d
fix explaintest
dragonly 999fffa
fix explaintest
dragonly cebe292
fix explaintest & plan test cases
dragonly 520647f
fix: accomodate float decimal
dragonly f2639b4
./run-tests.sh -r explain_easy
dragonly 9581de0
fix ut
dragonly 4ee8129
fix explain_complex
dragonly b1c2b1b
Apply suggestions from code review
dragonly f8f77da
Update expression/aggregation/base_func.go
dragonly b0f94ec
./run-tests.sh -r explain_complex_stats
dragonly eb60493
fix compile
dragonly 471bd09
address reviewer's comments
dragonly 19a80ad
improve code
dragonly 69b4cad
improve comment
dragonly 1cf8e97
add explaintest for sum & avg
dragonly bc9dca9
restore mysql.opt_rule_blacklist
dragonly ee5e055
./run-tests.sh -r explain_easy
dragonly 9948be7
improve comment
dragonly 7a7d943
Update expression/aggregation/base_func.go
dragonly d1cad1e
fix explaintest
dragonly 55d3bdc
fix explaintest
dragonly 7b538e0
fix all explaintest
dragonly 459caec
fix ut
dragonly df374b1
refactor
dragonly cd8f8f8
Merge branch 'master' into dragonly/agg-cast-decimal-precision
hawkingrei 9a5c03c
Merge branch 'master' into dragonly/agg-cast-decimal-precision
ti-chi-bot 425a3dc
Merge branch 'master' into dragonly/agg-cast-decimal-precision
ti-chi-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,7 +178,7 @@ func (a *baseFuncDesc) typeInfer4ApproxPercentile(ctx sessionctx.Context) error | |
return nil | ||
} | ||
|
||
// typeInfer4Sum should returns a "decimal", otherwise it returns a "double". | ||
// typeInfer4Sum should return a "decimal", otherwise it returns a "double". | ||
// Because child returns integer or decimal type. | ||
func (a *baseFuncDesc) typeInfer4Sum(ctx sessionctx.Context) { | ||
switch a.Args[0].GetType().Tp { | ||
|
@@ -421,6 +421,7 @@ func (a *baseFuncDesc) WrapCastForAggArgs(ctx sessionctx.Context) { | |
if a.Args[i].GetType().Tp == mysql.TypeNull { | ||
continue | ||
} | ||
tpOld := a.Args[i].GetType().Tp | ||
a.Args[i] = castFunc(ctx, a.Args[i]) | ||
if a.Name != ast.AggFuncAvg && a.Name != ast.AggFuncSum { | ||
continue | ||
|
@@ -443,5 +444,31 @@ func (a *baseFuncDesc) WrapCastForAggArgs(ctx sessionctx.Context) { | |
originTp := a.Args[i].GetType().Tp | ||
*(a.Args[i].GetType()) = *(a.RetTp) | ||
a.Args[i].GetType().Tp = originTp | ||
// refine each mysql integer type to the needed decimal precision for sum | ||
if a.Name == ast.AggFuncSum && types.IsTypeInteger(tpOld) { | ||
dragonly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if flen, err := minimalDecimalLenForHoldingInteger(tpOld); err != nil { | ||
dragonly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// avg could be split into sum and count, so we should take the `.Decimal` field into account | ||
a.Args[i].GetType().Flen = mathutil.Min(a.Args[i].GetType().Flen, flen+a.Args[i].GetType().Decimal) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also explain the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just integer min.
dragonly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
} | ||
|
||
func minimalDecimalLenForHoldingInteger(tp byte) (int, error) { | ||
switch tp { | ||
case mysql.TypeTiny: | ||
return 3, nil | ||
case mysql.TypeShort: | ||
return 5, nil | ||
case mysql.TypeInt24: | ||
return 8, nil | ||
case mysql.TypeLong: | ||
return 10, nil | ||
case mysql.TypeLonglong: | ||
return 20, nil | ||
case mysql.TypeYear: | ||
return 4, nil | ||
default: | ||
return -1, errors.Errorf("Invalid type: %v", tp) | ||
} | ||
} |
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
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
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
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.
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.
Besides
minimalDecimalLenForHoldingInteger
, how about extract L447-L454 to another function?adjustDecimalLenForSumInteger
?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.
This function is so tightly coupled with the surrounding code (like the iterating index
i
, and the if-true-then-modify logic), which results in many input/output of the extracted function (not so clean abstraction).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.
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.
then here is:
An explicit function can be more readable for readers who don't care the details.