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

types: fix the behavior when inserting a big scientific notation number to keep it same with mysql (#47803) #54631

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions pkg/executor/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,28 @@ func TestInsertLockUnchangedKeys(t *testing.T) {
}
}

// see issue https://github.com/pingcap/tidb/issues/47787
func TestInsertBigScientificNotation(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("create table t1(id int, a int)")

tk.MustExec("set @@SQL_MODE='STRICT_TRANS_TABLES'")
err := tk.ExecToErr("insert into t1 values(1, '1e100')")
require.EqualError(t, err, "[types:1264]Out of range value for column 'a' at row 1")
err = tk.ExecToErr("insert into t1 values(2, '-1e100')")
require.EqualError(t, err, "[types:1264]Out of range value for column 'a' at row 1")
tk.MustQuery("select id, a from t1").Check(testkit.Rows())

tk.MustExec("set @@SQL_MODE=''")
tk.MustExec("insert into t1 values(1, '1e100')")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1264 Out of range value for column 'a' at row 1"))
tk.MustExec("insert into t1 values(2, '-1e100')")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1264 Out of range value for column 'a' at row 1"))
tk.MustQuery("select id, a from t1 order by id asc").Check(testkit.Rows("1 2147483647", "2 -2147483648"))
}

// see issue: https://github.com/pingcap/tidb/issues/47945
func TestUnsignedDecimalFloatInsertNegative(t *testing.T) {
store := testkit.CreateMockStore(t)
Expand Down
22 changes: 17 additions & 5 deletions pkg/types/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func getValidIntPrefix(sc *stmtctx.StatementContext, str string, isFuncCast bool
if err != nil {
return floatPrefix, errors.Trace(err)
}
return floatStrToIntStr(sc, floatPrefix, str)
return floatStrToIntStr(floatPrefix, str)
}

validLen := 0
Expand Down Expand Up @@ -444,14 +444,17 @@ func roundIntStr(numNextDot byte, intStr string) string {
return string(retStr)
}

var maxUintStr = strconv.FormatUint(math.MaxUint64, 10)
var minIntStr = strconv.FormatInt(math.MinInt64, 10)

// floatStrToIntStr converts a valid float string into valid integer string which can be parsed by
// strconv.ParseInt, we can't parse float first then convert it to string because precision will
// be lost. For example, the string value "18446744073709551615" which is the max number of unsigned
// int will cause some precision to lose. intStr[0] may be a positive and negative sign like '+' or '-'.
//
// This func will find serious overflow such as the len of intStr > 20 (without prefix `+/-`)
// however, it will not check whether the intStr overflow BIGINT.
func floatStrToIntStr(sc *stmtctx.StatementContext, validFloat string, oriStr string) (intStr string, _ error) {
func floatStrToIntStr(validFloat string, oriStr string) (intStr string, _ error) {
var dotIdx = -1
var eIdx = -1
for i := 0; i < len(validFloat); i++ {
Expand Down Expand Up @@ -499,16 +502,25 @@ func floatStrToIntStr(sc *stmtctx.StatementContext, validFloat string, oriStr st
}
exp, err := strconv.Atoi(validFloat[eIdx+1:])
if err != nil {
return validFloat, errors.Trace(err)
if digits[0] == '-' {
intStr = minIntStr
} else {
intStr = maxUintStr
}
return intStr, ErrOverflow.GenWithStackByArgs("BIGINT", oriStr)
}
intCnt += exp
if exp >= 0 && (intCnt > 21 || intCnt < 0) {
// MaxInt64 has 19 decimal digits.
// MaxUint64 has 20 decimal digits.
// And the intCnt may contain the len of `+/-`,
// so I use 21 here as the early detection.
sc.AppendWarning(ErrOverflow.GenWithStackByArgs("BIGINT", oriStr))
return validFloat[:eIdx], nil
if digits[0] == '-' {
intStr = minIntStr
} else {
intStr = maxUintStr
}
return intStr, ErrOverflow.GenWithStackByArgs("BIGINT", oriStr)
}
if intCnt <= 0 {
intStr = "0"
Expand Down
45 changes: 27 additions & 18 deletions pkg/types/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -977,26 +977,35 @@ func TestGetValidFloat(t *testing.T) {
tests2 := []struct {
origin string
expected string
overflow bool
}{
{"1e9223372036854775807", "1"},
{"125e342", "125"},
{"1e21", "1"},
{"1e5", "100000"},
{"-123.45678e5", "-12345678"},
{"+0.5", "1"},
{"-0.5", "-1"},
{".5e0", "1"},
{"+.5e0", "+1"},
{"-.5e0", "-1"},
{".5", "1"},
{"123.456789e5", "12345679"},
{"123.456784e5", "12345678"},
{"+999.9999e2", "+100000"},
{"1e29223372036854775807", "18446744073709551615", true},
{"1e9223372036854775807", "18446744073709551615", true},
{"125e342", "18446744073709551615", true},
{"1e21", "18446744073709551615", true},
{"-1e29223372036854775807", "-9223372036854775808", true},
{"-1e9223372036854775807", "-9223372036854775808", true},
{"1e5", "100000", false},
{"-123.45678e5", "-12345678", false},
{"+0.5", "1", false},
{"-0.5", "-1", false},
{".5e0", "1", false},
{"+.5e0", "+1", false},
{"-.5e0", "-1", false},
{".5", "1", false},
{"123.456789e5", "12345679", false},
{"123.456784e5", "12345678", false},
{"+999.9999e2", "+100000", false},
}
for _, tt := range tests2 {
str, err := floatStrToIntStr(sc, tt.origin, tt.origin)
require.NoError(t, err)
require.Equalf(t, tt.expected, str, "%v, %v", tt.origin, tt.expected)
for i, tt := range tests2 {
msg := fmt.Sprintf("%d: %v, %v", i, tt.origin, tt.expected)
str, err := floatStrToIntStr(tt.origin, tt.origin)
if tt.overflow {
require.True(t, terror.ErrorEqual(err, ErrOverflow), msg)
} else {
require.NoError(t, err, msg)
}
require.Equalf(t, tt.expected, str, msg)
}
}

Expand Down