-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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, parser: fix issue #3691, cast compatibility #3894
Changes from 38 commits
7762b8b
f87e0af
465dd1b
17f1e5f
5589c9e
c61fea1
c8f218f
1d15330
4319ade
a803f9d
d6e6c25
ae342fc
869e1f6
b305a4f
bedd270
0e5de09
4045075
636170d
33bf09c
eb58c48
b199772
6005a18
e2aa622
d0395bf
ffc0df7
27202d6
03dc0e5
7f01b42
14e9711
19b2fd9
8f32c92
197ec13
151b0fe
c61275c
e3e5f94
684c314
4edca6b
028658d
07c31ac
bbe1260
b51e4f1
8d98e4d
68f0a1d
266971b
1699498
55d25a1
8ac311c
4ff23cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -333,34 +333,41 @@ func ResetStmtCtx(ctx context.Context, s ast.StmtNode) { | |
sessVars := ctx.GetSessionVars() | ||
sc := new(variable.StatementContext) | ||
sc.TimeZone = sessVars.GetTimeZone() | ||
|
||
switch s.(type) { | ||
case *ast.UpdateStmt, *ast.DeleteStmt: | ||
sc.IgnoreTruncate = false | ||
sc.IgnoreOverflow = false | ||
sc.OverflowAsWarning = false | ||
sc.TruncateAsWarning = !sessVars.StrictSQLMode | ||
sc.InUpdateOrDeleteStmt = true | ||
case *ast.InsertStmt: | ||
sc.IgnoreTruncate = false | ||
sc.IgnoreOverflow = false | ||
sc.TruncateAsWarning = !sessVars.StrictSQLMode | ||
sc.InInsertStmt = true | ||
case *ast.CreateTableStmt, *ast.AlterTableStmt: | ||
// Make sure the sql_mode is strict when checking column default value. | ||
sc.IgnoreTruncate = false | ||
sc.IgnoreOverflow = false | ||
sc.OverflowAsWarning = false | ||
sc.TruncateAsWarning = false | ||
case *ast.LoadDataStmt: | ||
sc.IgnoreTruncate = false | ||
sc.IgnoreOverflow = false | ||
sc.OverflowAsWarning = false | ||
sc.TruncateAsWarning = !sessVars.StrictSQLMode | ||
case *ast.SelectStmt: | ||
sc.IgnoreOverflow = true | ||
sc.InSelectStmt = true | ||
|
||
// see https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sql-mode-strict | ||
// said "For statements such as SELECT that do not change data, invalid values | ||
// generate a warning in strict mode, not an error." | ||
// and and https://dev.mysql.com/doc/refman/5.7/en/out-of-range-and-overflow.html | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and and ? |
||
sc.OverflowAsWarning = true | ||
|
||
// Return warning for truncate error in selection. | ||
sc.IgnoreTruncate = false | ||
sc.TruncateAsWarning = true | ||
default: | ||
sc.IgnoreTruncate = true | ||
sc.IgnoreOverflow = false | ||
sc.OverflowAsWarning = false | ||
if show, ok := s.(*ast.ShowStmt); ok { | ||
if show.Tp == ast.ShowWarnings { | ||
sc.InShowWarning = true | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,12 +23,16 @@ package expression | |
|
||
import ( | ||
"strconv" | ||
"strings" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this empty line. |
||
"math" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move math to line 25 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why make check ignore this? |
||
|
||
"github.com/juju/errors" | ||
"github.com/pingcap/tidb/ast" | ||
"github.com/pingcap/tidb/context" | ||
"github.com/pingcap/tidb/model" | ||
"github.com/pingcap/tidb/mysql" | ||
"github.com/pingcap/tidb/terror" | ||
"github.com/pingcap/tidb/util/charset" | ||
"github.com/pingcap/tidb/util/types" | ||
) | ||
|
@@ -542,22 +546,24 @@ func (b *builtinCastDecimalAsIntSig) evalInt(row []types.Datum) (res int64, isNu | |
if isNull || err != nil { | ||
return res, isNull, errors.Trace(err) | ||
} | ||
|
||
// despite of unsigned or signed, Round is needed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Round is needed for both unsigned and signed. |
||
var to types.MyDecimal | ||
val.Round(&to, 0, types.ModeHalfEven) | ||
|
||
if mysql.HasUnsignedFlag(b.tp.Flag) { | ||
var ( | ||
floatVal float64 | ||
uintRes uint64 | ||
) | ||
floatVal, err = val.ToFloat64() | ||
if err != nil { | ||
return res, false, errors.Trace(err) | ||
} | ||
uintRes, err = types.ConvertFloatToUint(sc, floatVal, types.UnsignedUpperBound[mysql.TypeLonglong], mysql.TypeDouble) | ||
res = int64(uintRes) | ||
var ures uint64 | ||
ures, err = to.ToUint() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The old name |
||
res = int64(ures) | ||
} else { | ||
var to types.MyDecimal | ||
val.Round(&to, 0, types.ModeHalfEven) | ||
res, err = to.ToInt() | ||
} | ||
|
||
if terror.ErrorEqual(err, types.ErrOverflow) { | ||
warnErr := types.ErrTruncatedWrongVal.GenByArgs("DECIMAL", val) | ||
err = sc.HandleOverflow(err, warnErr) | ||
} | ||
|
||
return res, false, errors.Trace(err) | ||
} | ||
|
||
|
@@ -636,6 +642,27 @@ type builtinCastStringAsIntSig struct { | |
baseIntBuiltinFunc | ||
} | ||
|
||
func (b *builtinCastStringAsIntSig) handleOverflow(origRes int64, origStr string, origErr error, isNegative bool) (res int64, err error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. may add a comment for this. |
||
res, err = origRes, origErr | ||
if err == nil { | ||
return | ||
} | ||
|
||
sc := b.getCtx().GetSessionVars().StmtCtx | ||
|
||
if sc.InSelectStmt && terror.ErrorEqual(origErr, types.ErrOverflow) { | ||
if isNegative { | ||
res = math.MinInt64 | ||
} else { | ||
uval := uint64(math.MaxUint64) | ||
res = int64(uval) | ||
} | ||
warnErr := types.ErrTruncatedWrongVal.GenByArgs("INTEGER", origStr) | ||
err = sc.HandleOverflow(origErr, warnErr) | ||
} | ||
return | ||
} | ||
|
||
func (b *builtinCastStringAsIntSig) evalInt(row []types.Datum) (res int64, isNull bool, err error) { | ||
sc := b.getCtx().GetSessionVars().StmtCtx | ||
if IsHybridType(b.args[0]) { | ||
|
@@ -645,13 +672,30 @@ func (b *builtinCastStringAsIntSig) evalInt(row []types.Datum) (res int64, isNul | |
if isNull || err != nil { | ||
return res, isNull, errors.Trace(err) | ||
} | ||
if mysql.HasUnsignedFlag(b.tp.Flag) { | ||
var ures uint64 | ||
|
||
val = strings.TrimSpace(val) | ||
isNegative := false | ||
if len(val) > 1 && val[0] == '-' { // negative number | ||
isNegative = true | ||
} | ||
|
||
var ures uint64 | ||
if isNegative { | ||
res, err = types.StrToInt(sc, val) | ||
if err == nil { | ||
// If overflow, don't append this warnings | ||
sc.AppendWarning(types.ErrCastNegIntToUnsigned) | ||
} | ||
} else { | ||
ures, err = types.StrToUint(sc, val) | ||
res = int64(ures) | ||
} else { | ||
res, err = types.StrToInt(sc, val) | ||
|
||
if err == nil && !mysql.HasUnsignedFlag(b.tp.Flag) && ures > uint64(math.MaxInt64) { | ||
sc.AppendWarning(types.ErrCastSignedOverflow) | ||
} | ||
} | ||
|
||
res, err = b.handleOverflow(res, val, err, isNegative) | ||
return res, false, errors.Trace(err) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ import ( | |
|
||
. "github.com/pingcap/check" | ||
"github.com/pingcap/tidb/mysql" | ||
"github.com/pingcap/tidb/terror" | ||
"github.com/pingcap/tidb/util/charset" | ||
"github.com/pingcap/tidb/util/testleak" | ||
"github.com/pingcap/tidb/util/types" | ||
|
@@ -75,6 +76,90 @@ func (s *testEvaluatorSuite) TestCast(c *C) { | |
c.Assert(len(res.GetString()), Equals, 5) | ||
c.Assert(res.GetString(), Equals, string([]byte{'a', 0x00, 0x00, 0x00, 0x00})) | ||
|
||
origSc := sc | ||
sc.InSelectStmt = true | ||
sc.OverflowAsWarning = true | ||
|
||
// cast('18446744073709551616' as unsigned); | ||
tp1 := types.NewFieldType(mysql.TypeLonglong) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will this be more readable? tp1 := &types.FieldType{
Tp:
Flag:
Charset:
....
} |
||
tp1.Flag |= mysql.UnsignedFlag | ||
tp1.Flen = mysql.MaxIntWidth | ||
tp1.Charset = charset.CharsetBin | ||
tp1.Collate = charset.CollationBin | ||
tp1.Flag |= mysql.BinaryFlag | ||
f = NewCastFunc(tp1, &Constant{Value: types.NewDatum("18446744073709551616"), RetType: types.NewFieldType(mysql.TypeString)}, ctx) | ||
res, err = f.Eval(nil) | ||
c.Assert(err, IsNil) | ||
c.Assert(res.GetUint64() == math.MaxUint64, IsTrue) | ||
|
||
warnings := sc.GetWarnings() | ||
lastWarn := warnings[len(warnings)-1] | ||
c.Assert(terror.ErrorEqual(types.ErrTruncatedWrongVal, lastWarn), IsTrue) | ||
|
||
f = NewCastFunc(tp1, &Constant{Value: types.NewDatum("-1"), RetType: types.NewFieldType(mysql.TypeString)}, ctx) | ||
res, err = f.Eval(nil) | ||
c.Assert(err, IsNil) | ||
c.Assert(res.GetUint64() == 18446744073709551615, IsTrue) | ||
|
||
warnings = sc.GetWarnings() | ||
lastWarn = warnings[len(warnings)-1] | ||
c.Assert(terror.ErrorEqual(types.ErrCastNegIntToUnsigned, lastWarn), IsTrue) | ||
|
||
f = NewCastFunc(tp1, &Constant{Value: types.NewDatum("-18446744073709551616"), RetType: types.NewFieldType(mysql.TypeString)}, ctx) | ||
res, err = f.Eval(nil) | ||
c.Assert(err, IsNil) | ||
t := math.MinInt64 | ||
// 9223372036854775808 | ||
c.Assert(res.GetUint64() == uint64(t), IsTrue) | ||
|
||
warnings = sc.GetWarnings() | ||
lastWarn = warnings[len(warnings)-1] | ||
c.Assert(terror.ErrorEqual(types.ErrTruncatedWrongVal, lastWarn), IsTrue) | ||
|
||
// cast('18446744073709551616' as signed); | ||
mask := ^mysql.UnsignedFlag | ||
tp1.Flag &= uint(mask) | ||
f = NewCastFunc(tp1, &Constant{Value: types.NewDatum("18446744073709551616"), RetType: types.NewFieldType(mysql.TypeString)}, ctx) | ||
res, err = f.Eval(nil) | ||
c.Assert(err, IsNil) | ||
c.Check(res.GetInt64(), Equals, int64(-1)) | ||
|
||
warnings = sc.GetWarnings() | ||
lastWarn = warnings[len(warnings)-1] | ||
c.Assert(terror.ErrorEqual(types.ErrTruncatedWrongVal, lastWarn), IsTrue) | ||
|
||
// cast('18446744073709551614' as signed); | ||
f = NewCastFunc(tp1, &Constant{Value: types.NewDatum("18446744073709551614"), RetType: types.NewFieldType(mysql.TypeString)}, ctx) | ||
res, err = f.Eval(nil) | ||
c.Assert(err, IsNil) | ||
c.Check(res.GetInt64(), Equals, int64(-2)) | ||
|
||
warnings = sc.GetWarnings() | ||
lastWarn = warnings[len(warnings)-1] | ||
c.Assert(terror.ErrorEqual(types.ErrCastSignedOverflow, lastWarn), IsTrue) | ||
|
||
// create table t1(s1 time); | ||
// insert into t1 values('11:11:11'); | ||
// select cast(s1 as decimal(7, 2)) from t1; | ||
tpDecimal := types.NewFieldType(mysql.TypeNewDecimal) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
tpDecimal.Flag |= mysql.UnsignedFlag | ||
tpDecimal.Charset = charset.CharsetBin | ||
tpDecimal.Collate = charset.CollationBin | ||
tpDecimal.Flag |= mysql.BinaryFlag | ||
tpDecimal.Flen = 7 | ||
tpDecimal.Decimal = 2 | ||
f = NewCastFunc(tpDecimal, &Constant{Value: timeDatum, RetType: types.NewFieldType(mysql.TypeDatetime)}, ctx) | ||
res, err = f.Eval(nil) | ||
c.Assert(err, IsNil) | ||
resDecimal := new(types.MyDecimal) | ||
resDecimal.FromString([]byte("99999.99")) | ||
c.Assert(res.GetMysqlDecimal().Compare(resDecimal), Equals, 0) | ||
|
||
warnings = sc.GetWarnings() | ||
lastWarn = warnings[len(warnings)-1] | ||
c.Assert(terror.ErrorEqual(types.ErrOverflow, lastWarn), IsTrue) | ||
sc = origSc | ||
|
||
// cast(bad_string as decimal) | ||
for _, s := range []string{"hello", ""} { | ||
f = NewCastFunc(tp, &Constant{Value: types.NewDatum(s), RetType: types.NewFieldType(mysql.TypeDecimal)}, ctx) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/ compabilities/ compatibility