Skip to content

Commit

Permalink
expression: fix the truncate function when the first arg is 0 and s…
Browse files Browse the repository at this point in the history
…econd value is too large (pingcap#58191)

close pingcap#57651
  • Loading branch information
xzhangxian1008 authored Dec 20, 2024
1 parent 05a35ca commit 8388f0f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
11 changes: 11 additions & 0 deletions pkg/expression/builtin_math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,17 @@ func TestTruncate(t *testing.T) {
{[]any{uint64(9223372036854775808), -10}, 9223372030000000000},
{[]any{9223372036854775807, -7}, 9223372036850000000},
{[]any{uint64(18446744073709551615), -10}, uint64(18446744070000000000)},

// For issue 57651
{[]any{math.NaN(), 400}, math.NaN()},
{[]any{math.NaN(), -400}, math.NaN()},
{[]any{math.NaN(), 3}, math.NaN()},
{[]any{1.1, 400}, 1.1},
{[]any{1.1, -400}, 0},
{[]any{1.1, 3}, 1.1},
{[]any{0, 400}, 0},
{[]any{0, -400}, 0},
{[]any{0, 3}, 0},
}

Dtbl := tblToDtbl(tbl)
Expand Down
3 changes: 3 additions & 0 deletions pkg/expression/builtin_math_vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ var vecBuiltinMathCases = map[string][]vecExprBenchCase{
types.NewFieldTypeBuilder().SetType(mysql.TypeInt24).SetFlag(mysql.UnsignedFlag).BuildP(),
}, geners: []dataGenerator{nil, newRangeInt64Gener(-10, 10)}},
{retEvalType: types.ETDecimal, childrenTypes: []types.EvalType{types.ETDecimal, types.ETInt}},

// For issue 57651
{retEvalType: types.ETReal, childrenTypes: []types.EvalType{types.ETReal, types.ETInt}, geners: []dataGenerator{newSelectRealGener([]float64{0, -0.1, 0.1, -1.1, 1.1}), newRangeInt64Gener(-1000, 1000)}},
},
ast.Rand: {
{retEvalType: types.ETReal, childrenTypes: []types.EvalType{types.ETInt}, geners: []dataGenerator{newDefaultGener(0, types.ETInt)}},
Expand Down
5 changes: 4 additions & 1 deletion pkg/types/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ func Round(f float64, dec int) float64 {
func Truncate(f float64, dec int) float64 {
shift := math.Pow10(dec)
tmp := f * shift
if math.IsInf(tmp, 0) {

// When dec is larger than 308, the shift will be inf.
// At the same time, if f is 0, tmp will be NaN.
if math.IsInf(tmp, 0) || math.IsNaN(tmp) {
return f
}
if shift == 0.0 {
Expand Down

0 comments on commit 8388f0f

Please sign in to comment.