Skip to content
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

expression: PropagateType should never let orignal data overflow #53045

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pkg/expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/tidb/pkg/types"
"github.com/pingcap/tidb/pkg/util/chunk"
"github.com/pingcap/tidb/pkg/util/generatedexpr"
"github.com/pingcap/tidb/pkg/util/mathutil"
"github.com/pingcap/tidb/pkg/util/size"
"github.com/pingcap/tidb/pkg/util/zeropool"
)
Expand Down Expand Up @@ -1192,6 +1193,10 @@ func wrapWithIsTrue(ctx BuildContext, keepNull bool, arg Expression, wrapForInt
func PropagateType(evalType types.EvalType, args ...Expression) {
switch evalType {
case types.ETReal:
// if _, ok := args[0].(*Constant); ok {
// // don't propagate type for constant
// return
// }
expr := args[0]
oldFlen, oldDecimal := expr.GetType().GetFlen(), expr.GetType().GetDecimal()
newFlen, newDecimal := setDataTypeDouble(expr.GetType().GetDecimal())
Expand All @@ -1214,6 +1219,16 @@ func PropagateType(evalType types.EvalType, args ...Expression) {
if newDecimal > mysql.MaxDecimalScale {
newDecimal = mysql.MaxDecimalScale
}
if oldFlen-oldDecimal > newFlen-newDecimal {
// the input data should never be overflow under the new type
if newDecimal > oldDecimal {
incDecimal := mathutil.Min(newDecimal-oldDecimal, mysql.MaxDecimalWidth-oldFlen)
newFlen = oldFlen + incDecimal
newDecimal = oldDecimal + incDecimal
} else {
newFlen, newDecimal = oldFlen, oldDecimal
}
}
}
args[0].GetType().SetFlenUnderLimit(newFlen)
args[0].GetType().SetDecimalUnderLimit(newDecimal)
Expand Down
3 changes: 3 additions & 0 deletions tests/integrationtest/r/executor/issues.result
Original file line number Diff line number Diff line change
Expand Up @@ -865,3 +865,6 @@ SELECT count(`t`.`c`) FROM (`s`) JOIN `t` GROUP BY `t`.`c`;
count(`t`.`c`)
170
set @@tidb_max_chunk_size = default;
select tan(9021874879467600608071521900001091070693729763119983979);
tan(9021874879467600608071521900001091070693729763119983979)
8.068627196084492
5 changes: 4 additions & 1 deletion tests/integrationtest/t/executor/issues.test
Original file line number Diff line number Diff line change
Expand Up @@ -659,4 +659,7 @@ insert into s values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),
insert into s values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1),(1);
SELECT /*+ stream_agg()*/ count(`t`.`c`) FROM (`s`) JOIN `t` GROUP BY `t`.`c`;
SELECT count(`t`.`c`) FROM (`s`) JOIN `t` GROUP BY `t`.`c`;
set @@tidb_max_chunk_size = default;
set @@tidb_max_chunk_size = default;

# TestIssue52672
select tan(9021874879467600608071521900001091070693729763119983979);