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

*: refine the behavior of StrToInt and StrToFloat and support convert JSON to date, time and timestamp (#17902) #18159

Merged
merged 3 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 4 additions & 8 deletions expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ func (b *builtinCastStringAsIntSig) handleOverflow(origRes int64, origStr string
}

sc := b.ctx.GetSessionVars().StmtCtx
if sc.InSelectStmt && types.ErrOverflow.Equal(origErr) {
if types.ErrOverflow.Equal(origErr) {
if isNegative {
res = math.MinInt64
} else {
Expand All @@ -1137,15 +1137,11 @@ func (b *builtinCastStringAsIntSig) evalInt(row chunk.Row) (res int64, isNull bo
if len(val) > 1 && val[0] == '-' { // negative number
isNegative = true
}
sctx := b.ctx.GetSessionVars().StmtCtx
if val == "" && (sctx.InInsertStmt || sctx.InUpdateStmt) {
return 0, false, nil
}

var ures uint64
sc := b.ctx.GetSessionVars().StmtCtx
if !isNegative {
ures, err = types.StrToUint(sc, val)
ures, err = types.StrToUint(sc, val, true)
res = int64(ures)

if err == nil && !mysql.HasUnsignedFlag(b.tp.Flag) && ures > uint64(math.MaxInt64) {
Expand All @@ -1154,7 +1150,7 @@ func (b *builtinCastStringAsIntSig) evalInt(row chunk.Row) (res int64, isNull bo
} else if b.inUnion && mysql.HasUnsignedFlag(b.tp.Flag) {
res = 0
} else {
res, err = types.StrToInt(sc, val)
res, err = types.StrToInt(sc, val, true)
if err == nil && mysql.HasUnsignedFlag(b.tp.Flag) {
// If overflow, don't append this warnings
sc.AppendWarning(types.ErrCastNegIntAsUnsigned)
Expand Down Expand Up @@ -1188,7 +1184,7 @@ func (b *builtinCastStringAsRealSig) evalReal(row chunk.Row) (res float64, isNul
return 0, false, nil
}
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.StrToFloat(sc, val)
res, err = types.StrToFloat(sc, val, true)
if err != nil {
return 0, false, err
}
Expand Down
6 changes: 3 additions & 3 deletions expression/builtin_cast_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,15 +855,15 @@ func (b *builtinCastStringAsIntSig) vecEvalInt(input *chunk.Chunk, result *chunk
val := strings.TrimSpace(buf.GetString(i))
isNegative := len(val) > 1 && val[0] == '-'
if !isNegative {
ures, err = types.StrToUint(sc, val)
ures, err = types.StrToUint(sc, val, true)
if !isUnsigned && err == nil && ures > uint64(math.MaxInt64) {
sc.AppendWarning(types.ErrCastAsSignedOverflow)
}
res = int64(ures)
} else if unionUnsigned {
res = 0
} else {
res, err = types.StrToInt(sc, val)
res, err = types.StrToInt(sc, val, true)
if err == nil && isUnsigned {
// If overflow, don't append this warnings
sc.AppendWarning(types.ErrCastNegIntAsUnsigned)
Expand Down Expand Up @@ -1554,7 +1554,7 @@ func (b *builtinCastStringAsRealSig) vecEvalReal(input *chunk.Chunk, result *chu
if result.IsNull(i) {
continue
}
res, err := types.StrToFloat(sc, buf.GetString(i))
res, err := types.StrToFloat(sc, buf.GetString(i), true)
if err != nil {
return err
}
Expand Down
4 changes: 1 addition & 3 deletions expression/builtin_compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ func (s *testEvaluatorSuite) TestCompareFunctionWithRefine(c *C) {
{"-123456789123456789123456789.12345 > a", "0"},
{"123456789123456789123456789.12345 < a", "0"},
{"-123456789123456789123456789.12345 < a", "1"},
// This cast can not be eliminated,
// since converting "aaaa" to an int will cause DataTruncate error.
{"'aaaa'=a", "eq(cast(aaaa, double BINARY), cast(a, double BINARY))"},
{"'aaaa'=a", "eq(0, a)"},
}
cols, names := ColumnInfos2ColumnsAndNames(s.ctx, model.NewCIStr(""), tblInfo.Name, tblInfo.Columns)
schema := NewSchema(cols...)
Expand Down
Loading