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 convert str -00* to uint (#46721) #46875

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,11 +811,20 @@ func TestStringBuiltin(t *testing.T) {
result.Check(testkit.Rows("114.57011441 38.04620115 114.57011441,38.04620115",
"-38.04620119 38.04620115 -38.04620119,38.04620115"))

<<<<<<< HEAD:expression/integration_test.go
// For issue 31603, only affects unistore.
tk.MustExec("drop table if exists t1;")
tk.MustExec("create table t1(c1 varbinary(100));")
tk.MustExec("insert into t1 values('abc');")
tk.MustQuery("select 1 from t1 where char_length(c1) = 10;").Check(testkit.Rows())
=======
// issue 44359
tk.MustExec("drop table if exists t1")
tk.MustExec("CREATE TABLE t1 (c1 INT UNSIGNED NOT NULL )")
tk.MustExec("INSERT INTO t1 VALUES (0)")
tk.MustQuery("SELECT c1 FROM t1 WHERE c1 <> CAST(POW(-'0', 1) AS BINARY)").Check(testkit.Rows())
tk.MustQuery("SELECT c1 FROM t1 WHERE c1 = CAST('-000' AS BINARY)").Check(testkit.Rows("0"))
>>>>>>> 6397d4753f4 (types: fix convert str `-00*` to uint (#46721)):expression/integration_test/integration_test.go
solotzg marked this conversation as resolved.
Show resolved Hide resolved
}

func TestInvalidStrings(t *testing.T) {
Expand Down
23 changes: 19 additions & 4 deletions types/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,26 @@ func StrToInt(sc *stmtctx.StatementContext, str string, isFuncCast bool) (int64,
func StrToUint(sc *stmtctx.StatementContext, str string, isFuncCast bool) (uint64, error) {
str = strings.TrimSpace(str)
validPrefix, err := getValidIntPrefix(sc, str, isFuncCast)
if validPrefix[0] == '+' {
validPrefix = validPrefix[1:]
uVal := uint64(0)
hasParseErr := false

if validPrefix[0] == '-' {
// only `-000*` is valid to be converted into unsigned integer
for _, v := range validPrefix[1:] {
if v != '0' {
hasParseErr = true
break
}
}
} else {
if validPrefix[0] == '+' {
validPrefix = validPrefix[1:]
}
v, e := strconv.ParseUint(validPrefix, 10, 64)
uVal, hasParseErr = v, e != nil
}
uVal, err1 := strconv.ParseUint(validPrefix, 10, 64)
if err1 != nil {

if hasParseErr {
return uVal, ErrOverflow.GenWithStackByArgs("BIGINT UNSIGNED", validPrefix)
}
return uVal, errors.Trace(err)
Expand Down
3 changes: 3 additions & 0 deletions types/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,9 @@ func TestStrToNum(t *testing.T) {
testStrToUint(t, "11xx", 11, true, ErrTruncatedWrongVal)
testStrToUint(t, "xx11", 0, true, ErrTruncatedWrongVal)

// for issue #44359
testStrToUint(t, "-00", 0, true, nil)

// TODO: makes StrToFloat return truncated value instead of zero to make it pass.
testStrToFloat(t, "", 0, true, ErrTruncatedWrongVal)
testStrToFloat(t, "-1", -1.0, true, nil)
Expand Down