diff --git a/br/pkg/lightning/backend/kv/session.go b/br/pkg/lightning/backend/kv/session.go index d6b9add03273c..157e3d642175f 100644 --- a/br/pkg/lightning/backend/kv/session.go +++ b/br/pkg/lightning/backend/kv/session.go @@ -33,7 +33,6 @@ import ( "github.com/pingcap/tidb/pkg/parser/model" "github.com/pingcap/tidb/pkg/sessionctx" "github.com/pingcap/tidb/pkg/sessionctx/variable" - "github.com/pingcap/tidb/pkg/types" "github.com/pingcap/tidb/pkg/util/topsql/stmtstats" "go.uber.org/zap" ) @@ -287,7 +286,6 @@ func NewSession(options *encode.SessionOptions, logger log.Logger) *Session { vars.StmtCtx.InInsertStmt = true vars.StmtCtx.BatchCheck = true vars.StmtCtx.BadNullAsWarning = !sqlMode.HasStrictMode() - vars.StmtCtx.OverflowAsWarning = !sqlMode.HasStrictMode() vars.SQLMode = sqlMode typeFlags := vars.StmtCtx.TypeFlags(). @@ -315,7 +313,6 @@ func NewSession(options *encode.SessionOptions, logger log.Logger) *Session { } } vars.StmtCtx.SetTimeZone(vars.Location()) - vars.StmtCtx.SetTypeFlags(types.StrictFlags) if err := vars.SetSystemVar("timestamp", strconv.FormatInt(options.Timestamp, 10)); err != nil { logger.Warn("new session: failed to set timestamp", log.ShortError(err)) diff --git a/pkg/ddl/backfilling_scheduler.go b/pkg/ddl/backfilling_scheduler.go index 958862f342e83..1b172531fdc18 100644 --- a/pkg/ddl/backfilling_scheduler.go +++ b/pkg/ddl/backfilling_scheduler.go @@ -165,7 +165,6 @@ func initSessCtx( } sessCtx.GetSessionVars().StmtCtx.SetTimeZone(sessCtx.GetSessionVars().Location()) sessCtx.GetSessionVars().StmtCtx.BadNullAsWarning = !sqlMode.HasStrictMode() - sessCtx.GetSessionVars().StmtCtx.OverflowAsWarning = !sqlMode.HasStrictMode() sessCtx.GetSessionVars().StmtCtx.DividedByZeroAsWarning = !sqlMode.HasStrictMode() typeFlags := types.StrictFlags. @@ -196,7 +195,6 @@ func restoreSessCtx(sessCtx sessionctx.Context) func(sessCtx sessionctx.Context) timezone = &tz } badNullAsWarn := sv.StmtCtx.BadNullAsWarning - overflowAsWarn := sv.StmtCtx.OverflowAsWarning dividedZeroAsWarn := sv.StmtCtx.DividedByZeroAsWarning typeFlags := sv.StmtCtx.TypeFlags() resGroupName := sv.ResourceGroupName @@ -206,7 +204,6 @@ func restoreSessCtx(sessCtx sessionctx.Context) func(sessCtx sessionctx.Context) uv.SQLMode = sqlMode uv.TimeZone = timezone uv.StmtCtx.BadNullAsWarning = badNullAsWarn - uv.StmtCtx.OverflowAsWarning = overflowAsWarn uv.StmtCtx.DividedByZeroAsWarning = dividedZeroAsWarn uv.StmtCtx.SetTypeFlags(typeFlags) uv.ResourceGroupName = resGroupName diff --git a/pkg/errctx/context.go b/pkg/errctx/context.go index d0b3ab0ab3260..1bb873ef0bf7f 100644 --- a/pkg/errctx/context.go +++ b/pkg/errctx/context.go @@ -176,6 +176,4 @@ func init() { for _, errCode := range truncateErrCodes { errGroupMap[errCode] = ErrGroupTruncate } - - errGroupMap[errno.ErrDataOutOfRange] = ErrGroupOverflow } diff --git a/pkg/errctx/context_test.go b/pkg/errctx/context_test.go index 0162f8e5e2d73..7b6b7c0c9d035 100644 --- a/pkg/errctx/context_test.go +++ b/pkg/errctx/context_test.go @@ -38,7 +38,7 @@ func TestContext(t *testing.T) { require.Equal(t, ctx.HandleErrorWithAlias(testInternalErr, testErr, testWarn), testErr) // set level to "warn" - newCtx := ctx.WithErrGroupLevel(errctx.ErrGroupOverflow, errctx.LevelWarn) + newCtx := ctx.WithErrGroupLevel(errctx.ErrGroupTruncate, errctx.LevelWarn) // ctx is not affected require.Equal(t, ctx.HandleErrorWithAlias(testInternalErr, testErr, testWarn), testErr) // newCtx will handle the error as a warn diff --git a/pkg/executor/executor.go b/pkg/executor/executor.go index 3c685ee286166..885193f9d2a04 100644 --- a/pkg/executor/executor.go +++ b/pkg/executor/executor.go @@ -2116,8 +2116,6 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) { // but should not make DupKeyAsWarning. sc.DupKeyAsWarning = stmt.IgnoreErr sc.BadNullAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr - // see https://dev.mysql.com/doc/refman/8.0/en/out-of-range-and-overflow.html - sc.OverflowAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr sc.IgnoreNoPartition = stmt.IgnoreErr sc.ErrAutoincReadFailedAsWarning = stmt.IgnoreErr sc.DividedByZeroAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr @@ -2144,12 +2142,6 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) { case *ast.SelectStmt: 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 https://dev.mysql.com/doc/refman/5.7/en/out-of-range-and-overflow.html - sc.OverflowAsWarning = true - // Return warning for truncate error in selection. sc.SetTypeFlags(sc.TypeFlags(). WithTruncateAsWarning(true). @@ -2162,7 +2154,6 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) { sc.WeakConsistency = isWeakConsistencyRead(ctx, stmt) case *ast.SetOprStmt: sc.InSelectStmt = true - sc.OverflowAsWarning = true sc.SetTypeFlags(sc.TypeFlags(). WithTruncateAsWarning(true). WithIgnoreZeroInDate(true). diff --git a/pkg/expression/builtin_arithmetic.go b/pkg/expression/builtin_arithmetic.go index 35758835cfc09..f5d3b1c47ea46 100644 --- a/pkg/expression/builtin_arithmetic.go +++ b/pkg/expression/builtin_arithmetic.go @@ -843,7 +843,7 @@ func (s *builtinArithmeticIntDivideDecimalSig) evalInt(ctx EvalContext, row chun } if err == types.ErrOverflow { newErr := errTruncatedWrongValue.GenWithStackByArgs("DECIMAL", c) - err = sc.HandleOverflow(newErr, newErr) + err = sc.HandleError(newErr) } if err != nil { return 0, true, err diff --git a/pkg/expression/builtin_arithmetic_vec.go b/pkg/expression/builtin_arithmetic_vec.go index 8a5c0c3f14705..8aa4df472004e 100644 --- a/pkg/expression/builtin_arithmetic_vec.go +++ b/pkg/expression/builtin_arithmetic_vec.go @@ -620,7 +620,7 @@ func (b *builtinArithmeticIntDivideDecimalSig) vecEvalInt(ctx EvalContext, input err = sc.HandleTruncate(errTruncatedWrongValue.GenWithStackByArgs("DECIMAL", c)) } else if err == types.ErrOverflow { newErr := errTruncatedWrongValue.GenWithStackByArgs("DECIMAL", c) - err = sc.HandleOverflow(newErr, newErr) + err = sc.HandleError(newErr) } if err != nil { return err diff --git a/pkg/expression/builtin_cast.go b/pkg/expression/builtin_cast.go index 16d5848a19356..00055d6dd7f0c 100644 --- a/pkg/expression/builtin_cast.go +++ b/pkg/expression/builtin_cast.go @@ -542,7 +542,7 @@ func convertJSON2Tp(evalType types.EvalType) func(*stmtctx.StatementContext, typ return nil, ErrInvalidJSONForFuncIndex } jsonToInt, err := types.ConvertJSONToInt(sc.TypeCtx(), item, mysql.HasUnsignedFlag(tp.GetFlag()), tp.GetType()) - err = sc.HandleOverflow(err, err) + err = sc.HandleError(err) if mysql.HasUnsignedFlag(tp.GetFlag()) { return uint64(jsonToInt), err } @@ -705,7 +705,7 @@ func (b *builtinCastIntAsDecimalSig) evalDecimal(ctx EvalContext, row chunk.Row) } sc := ctx.GetSessionVars().StmtCtx res, err = types.ProduceDecWithSpecifiedTp(sc.TypeCtx(), res, b.tp) - err = sc.HandleOverflow(err, err) + err = sc.HandleError(err) return res, isNull, err } @@ -790,7 +790,7 @@ func (b *builtinCastIntAsDurationSig) evalDuration(ctx EvalContext, row chunk.Ro dur, err := types.NumberToDuration(val, b.tp.GetDecimal()) if err != nil { if types.ErrOverflow.Equal(err) { - err = ctx.GetSessionVars().StmtCtx.HandleOverflow(err, err) + err = ctx.GetSessionVars().StmtCtx.HandleError(err) } if types.ErrTruncatedWrongVal.Equal(err) { err = ctx.GetSessionVars().StmtCtx.HandleTruncate(err) @@ -987,7 +987,7 @@ func (b *builtinCastRealAsIntSig) evalInt(ctx EvalContext, row chunk.Row) (res i res = int64(uintVal) } if types.ErrOverflow.Equal(err) { - err = ctx.GetSessionVars().StmtCtx.HandleOverflow(err, err) + err = ctx.GetSessionVars().StmtCtx.HandleError(err) } return res, isNull, err } @@ -1012,7 +1012,7 @@ func (b *builtinCastRealAsDecimalSig) evalDecimal(ctx EvalContext, row chunk.Row err = res.FromFloat64(val) if types.ErrOverflow.Equal(err) { warnErr := types.ErrTruncatedWrongVal.GenWithStackByArgs("DECIMAL", b.args[0]) - err = ctx.GetSessionVars().StmtCtx.HandleOverflow(err, warnErr) + err = ctx.GetSessionVars().StmtCtx.HandleErrorWithAlias(err, err, warnErr) } else if types.ErrTruncated.Equal(err) { // This behavior is consistent with MySQL. err = nil @@ -1023,7 +1023,7 @@ func (b *builtinCastRealAsDecimalSig) evalDecimal(ctx EvalContext, row chunk.Row } sc := ctx.GetSessionVars().StmtCtx res, err = types.ProduceDecWithSpecifiedTp(sc.TypeCtx(), res, b.tp) - err = sc.HandleOverflow(err, err) + err = sc.HandleError(err) return res, false, err } @@ -1136,7 +1136,7 @@ func (b *builtinCastDecimalAsDecimalSig) evalDecimal(ctx EvalContext, row chunk. } sc := ctx.GetSessionVars().StmtCtx res, err = types.ProduceDecWithSpecifiedTp(sc.TypeCtx(), res, b.tp) - err = sc.HandleOverflow(err, err) + err = sc.HandleError(err) return res, false, err } @@ -1175,7 +1175,7 @@ func (b *builtinCastDecimalAsIntSig) evalInt(ctx EvalContext, row chunk.Row) (re if types.ErrOverflow.Equal(err) { warnErr := types.ErrTruncatedWrongVal.GenWithStackByArgs("DECIMAL", val) - err = ctx.GetSessionVars().StmtCtx.HandleOverflow(err, warnErr) + err = ctx.GetSessionVars().StmtCtx.HandleErrorWithAlias(err, err, warnErr) } return res, false, err @@ -1343,7 +1343,7 @@ func (*builtinCastStringAsIntSig) handleOverflow(ctx EvalContext, origRes int64, res = int64(uval) } warnErr := types.ErrTruncatedWrongVal.GenWithStackByArgs("INTEGER", origStr) - err = sc.HandleOverflow(origErr, warnErr) + err = sc.HandleErrorWithAlias(origErr, origErr, warnErr) } return } @@ -1461,7 +1461,7 @@ func (b *builtinCastStringAsDecimalSig) evalDecimal(ctx EvalContext, row chunk.R } } res, err = types.ProduceDecWithSpecifiedTp(sc.TypeCtx(), res, b.tp) - err = sc.HandleOverflow(err, err) + err = sc.HandleError(err) return res, false, err } @@ -1607,7 +1607,7 @@ func (b *builtinCastTimeAsDecimalSig) evalDecimal(ctx EvalContext, row chunk.Row } sc := ctx.GetSessionVars().StmtCtx res, err = types.ProduceDecWithSpecifiedTp(sc.TypeCtx(), val.ToNumber(), b.tp) - err = sc.HandleOverflow(err, err) + err = sc.HandleError(err) return res, false, err } @@ -1747,7 +1747,7 @@ func (b *builtinCastDurationAsDecimalSig) evalDecimal(ctx EvalContext, row chunk } sc := ctx.GetSessionVars().StmtCtx res, err = types.ProduceDecWithSpecifiedTp(sc.TypeCtx(), val.ToNumber(), b.tp) - err = sc.HandleOverflow(err, err) + err = sc.HandleError(err) return res, false, err } @@ -1850,7 +1850,7 @@ func (b *builtinCastJSONAsIntSig) evalInt(ctx EvalContext, row chunk.Row) (res i } sc := ctx.GetSessionVars().StmtCtx res, err = types.ConvertJSONToInt64(sc.TypeCtx(), val, mysql.HasUnsignedFlag(b.tp.GetFlag())) - err = sc.HandleOverflow(err, err) + err = sc.HandleError(err) return } @@ -1895,7 +1895,7 @@ func (b *builtinCastJSONAsDecimalSig) evalDecimal(ctx EvalContext, row chunk.Row return res, false, err } res, err = types.ProduceDecWithSpecifiedTp(sc.TypeCtx(), res, b.tp) - err = sc.HandleOverflow(err, err) + err = sc.HandleError(err) return res, false, err } diff --git a/pkg/expression/builtin_cast_test.go b/pkg/expression/builtin_cast_test.go index 24b5816e86fe6..06fc231f493d5 100644 --- a/pkg/expression/builtin_cast_test.go +++ b/pkg/expression/builtin_cast_test.go @@ -98,7 +98,6 @@ func TestCastFunctions(t *testing.T) { defer func() { sc.InSelectStmt = oldInSelectStmt }() - sc.OverflowAsWarning = true // cast('18446744073709551616' as unsigned); tp1 := types.NewFieldTypeBuilder().SetType(mysql.TypeLonglong).SetFlag(mysql.BinaryFlag).SetFlen(mysql.MaxIntWidth).SetCharset(charset.CharsetBin).SetCollate(charset.CollationBin).BuildP() diff --git a/pkg/expression/builtin_cast_vec.go b/pkg/expression/builtin_cast_vec.go index cb53c44c3ac98..37f0890195e13 100644 --- a/pkg/expression/builtin_cast_vec.go +++ b/pkg/expression/builtin_cast_vec.go @@ -48,7 +48,7 @@ func (b *builtinCastIntAsDurationSig) vecEvalDuration(ctx EvalContext, input *ch dur, err := types.NumberToDuration(i64s[i], b.tp.GetDecimal()) if err != nil { if types.ErrOverflow.Equal(err) { - err = ctx.GetSessionVars().StmtCtx.HandleOverflow(err, err) + err = ctx.GetSessionVars().StmtCtx.HandleError(err) } if types.ErrTruncatedWrongVal.Equal(err) { err = ctx.GetSessionVars().StmtCtx.HandleTruncate(err) @@ -308,7 +308,7 @@ func (b *builtinCastTimeAsDecimalSig) vecEvalDecimal(ctx EvalContext, input *chu *dec = types.MyDecimal{} times[i].FillNumber(dec) dec, err = types.ProduceDecWithSpecifiedTp(sc.TypeCtx(), dec, b.tp) - if err = sc.HandleOverflow(err, err); err != nil { + if err = sc.HandleError(err); err != nil { return err } decs[i] = *dec @@ -632,7 +632,7 @@ func (b *builtinCastDecimalAsDecimalSig) vecEvalDecimal(ctx EvalContext, input * *dec = decs[i] } dec, err := types.ProduceDecWithSpecifiedTp(sc.TypeCtx(), dec, b.tp) - if err = sc.HandleOverflow(err, err); err != nil { + if err = sc.HandleError(err); err != nil { return err } decs[i] = *dec @@ -778,7 +778,7 @@ func (b *builtinCastRealAsIntSig) vecEvalInt(ctx EvalContext, input *chunk.Chunk i64s[i] = int64(uintVal) } if types.ErrOverflow.Equal(err) { - err = ctx.GetSessionVars().StmtCtx.HandleOverflow(err, err) + err = ctx.GetSessionVars().StmtCtx.HandleError(err) } if err != nil { return err @@ -812,7 +812,7 @@ func (b *builtinCastTimeAsRealSig) vecEvalReal(ctx EvalContext, input *chunk.Chu f64, err := times[i].ToNumber().ToFloat64() if err != nil { if types.ErrOverflow.Equal(err) { - err = ctx.GetSessionVars().StmtCtx.HandleOverflow(err, err) + err = ctx.GetSessionVars().StmtCtx.HandleError(err) } if err != nil { return err @@ -916,7 +916,7 @@ func (b *builtinCastRealAsDecimalSig) vecEvalDecimal(ctx EvalContext, input *chu if err = resdecimal[i].FromFloat64(bufreal[i]); err != nil { if types.ErrOverflow.Equal(err) { warnErr := types.ErrTruncatedWrongVal.GenWithStackByArgs("DECIMAL", b.args[0]) - err = ctx.GetSessionVars().StmtCtx.HandleOverflow(err, warnErr) + err = ctx.GetSessionVars().StmtCtx.HandleErrorWithAlias(err, err, warnErr) } else if types.ErrTruncated.Equal(err) { // This behavior is consistent with MySQL. err = nil @@ -927,7 +927,7 @@ func (b *builtinCastRealAsDecimalSig) vecEvalDecimal(ctx EvalContext, input *chu } } dec, err := types.ProduceDecWithSpecifiedTp(sc.TypeCtx(), &resdecimal[i], b.tp) - if err = sc.HandleOverflow(err, err); err != nil { + if err = sc.HandleError(err); err != nil { return err } resdecimal[i] = *dec @@ -1068,7 +1068,7 @@ func (b *builtinCastDurationAsDecimalSig) vecEvalDecimal(ctx EvalContext, input duration.Duration = ds[i] duration.Fsp = fsp res, err := types.ProduceDecWithSpecifiedTp(sc.TypeCtx(), duration.ToNumber(), b.tp) - if err = sc.HandleOverflow(err, err); err != nil { + if err = sc.HandleError(err); err != nil { return err } d64s[i] = *res @@ -1114,7 +1114,7 @@ func (b *builtinCastIntAsDecimalSig) vecEvalDecimal(ctx EvalContext, input *chun } dec, err = types.ProduceDecWithSpecifiedTp(sc.TypeCtx(), dec, b.tp) - if err = sc.HandleOverflow(err, err); err != nil { + if err = sc.HandleError(err); err != nil { return err } decs[i] = *dec @@ -1269,7 +1269,7 @@ func (b *builtinCastJSONAsIntSig) vecEvalInt(ctx EvalContext, input *chunk.Chunk continue } i64s[i], err = types.ConvertJSONToInt64(tc, buf.GetJSON(i), mysql.HasUnsignedFlag(b.tp.GetFlag())) - if err = sc.HandleOverflow(err, err); err != nil { + if err = sc.HandleError(err); err != nil { return err } } @@ -1455,7 +1455,7 @@ func (b *builtinCastDecimalAsRealSig) vecEvalReal(ctx EvalContext, input *chunk. res, err := d[i].ToFloat64() if err != nil { if types.ErrOverflow.Equal(err) { - err = ctx.GetSessionVars().StmtCtx.HandleOverflow(err, err) + err = ctx.GetSessionVars().StmtCtx.HandleError(err) } if err != nil { return err @@ -1654,7 +1654,7 @@ func (b *builtinCastJSONAsDecimalSig) vecEvalDecimal(ctx EvalContext, input *chu return err } tempres, err = types.ProduceDecWithSpecifiedTp(sc.TypeCtx(), tempres, b.tp) - if err = sc.HandleOverflow(err, err); err != nil { + if err = sc.HandleError(err); err != nil { return err } res[i] = *tempres @@ -1744,7 +1744,7 @@ func (b *builtinCastStringAsDecimalSig) vecEvalDecimal(ctx EvalContext, input *c return err } dec, err := types.ProduceDecWithSpecifiedTp(stmtCtx.TypeCtx(), dec, b.tp) - if err = stmtCtx.HandleOverflow(err, err); err != nil { + if err = stmtCtx.HandleError(err); err != nil { return err } res[i] = *dec @@ -1848,7 +1848,7 @@ func (b *builtinCastDecimalAsIntSig) vecEvalInt(ctx EvalContext, input *chunk.Ch if types.ErrOverflow.Equal(err) { warnErr := types.ErrTruncatedWrongVal.GenWithStackByArgs("DECIMAL", d64s[i]) - err = ctx.GetSessionVars().StmtCtx.HandleOverflow(err, warnErr) + err = ctx.GetSessionVars().StmtCtx.HandleErrorWithAlias(err, err, warnErr) } if err != nil { diff --git a/pkg/expression/typeinfer_test.go b/pkg/expression/typeinfer_test.go index 23d7b3b1b4bf2..ce77470663757 100644 --- a/pkg/expression/typeinfer_test.go +++ b/pkg/expression/typeinfer_test.go @@ -205,11 +205,11 @@ func (s *InferTypeSuite) createTestCase4Cast() []typeInferTestCase { {"CAST(c_varchar AS DECIMAL)", mysql.TypeNewDecimal, charset.CharsetBin, mysql.BinaryFlag, 10, 0}, {"CAST(\"123456789.0123456789\" AS DECIMAL)", mysql.TypeNewDecimal, charset.CharsetBin, mysql.NotNullFlag | mysql.BinaryFlag, 10, 0}, // TODO: flen should be 11. {"CAST(\"123456789.0123456789\" AS DECIMAL(10,0))", mysql.TypeNewDecimal, charset.CharsetBin, mysql.NotNullFlag | mysql.BinaryFlag, 10, 0}, // TODO: flen should be 11. - {"CAST(\"123456789.0123456789\" AS DECIMAL(5,5))", mysql.TypeNewDecimal, charset.CharsetBin, mysql.BinaryFlag, 5, 5}, // TODO: flen should be 7. flag should be mysql.NotNullFlag | mysql.BinaryFlag. - {"CAST(\"123456789.0123456789\" AS DECIMAL(6,5))", mysql.TypeNewDecimal, charset.CharsetBin, mysql.BinaryFlag, 6, 5}, // TODO: flen should be 8. flag should be mysql.NotNullFlag | mysql.BinaryFlag. + {"CAST(\"123456789.0123456789\" AS DECIMAL(5,5))", mysql.TypeNewDecimal, charset.CharsetBin, mysql.NotNullFlag | mysql.BinaryFlag, 5, 5}, // TODO: flen should be 7. + {"CAST(\"123456789.0123456789\" AS DECIMAL(6,5))", mysql.TypeNewDecimal, charset.CharsetBin, mysql.NotNullFlag | mysql.BinaryFlag, 6, 5}, // TODO: flen should be 8. {"CAST(\"-123456789.0123456789\" AS DECIMAL(64,30))", mysql.TypeNewDecimal, charset.CharsetBin, mysql.NotNullFlag | mysql.BinaryFlag, 64, 30}, // TODO: flen should be 66. {"CAST(\"-123456789.0123456789\" AS DECIMAL(10,0))", mysql.TypeNewDecimal, charset.CharsetBin, mysql.NotNullFlag | mysql.BinaryFlag, 10, 0}, // TODO: flen should be 11. - {"CAST(\"-123456789.0123456789\" AS DECIMAL(5,5))", mysql.TypeNewDecimal, charset.CharsetBin, mysql.BinaryFlag, 5, 5}, // TODO: flen should be 7. flag should be mysql.NotNullFlag | mysql.BinaryFlag. + {"CAST(\"-123456789.0123456789\" AS DECIMAL(5,5))", mysql.TypeNewDecimal, charset.CharsetBin, mysql.NotNullFlag | mysql.BinaryFlag, 5, 5}, // TODO: flen should be 7. {"CAST(c_int_d AS JSON)", mysql.TypeJSON, charset.CharsetUTF8MB4, mysql.BinaryFlag | mysql.ParseToJSONFlag, 12582912 / 3, 0}, {"CAST(c_int_d AS SIGNED)", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag, 22, 0}, // TODO: flen should be 11. {"CAST(c_int_d AS SIGNED INTEGER)", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag, 22, 0}, // TODO: flen should be 11. diff --git a/pkg/sessionctx/stmtctx/stmtctx.go b/pkg/sessionctx/stmtctx/stmtctx.go index 4cbacb77afd55..127ea8d7744e3 100644 --- a/pkg/sessionctx/stmtctx/stmtctx.go +++ b/pkg/sessionctx/stmtctx/stmtctx.go @@ -183,7 +183,6 @@ type StatementContext struct { DupKeyAsWarning bool BadNullAsWarning bool DividedByZeroAsWarning bool - OverflowAsWarning bool ErrAutoincReadFailedAsWarning bool InShowWarning bool UseCache bool @@ -478,9 +477,6 @@ func (sc *StatementContext) ErrCtx() errctx.Context { ctx = ctx.WithErrGroupLevel(errctx.ErrGroupTruncate, errctx.LevelWarn) } - if sc.OverflowAsWarning { - ctx = ctx.WithErrGroupLevel(errctx.ErrGroupOverflow, errctx.LevelWarn) - } return ctx } @@ -510,6 +506,16 @@ func (sc *StatementContext) HandleError(err error) error { return errCtx.HandleError(err) } +// HandleErrorWithAlias handles the error based on `ErrCtx()` +func (sc *StatementContext) HandleErrorWithAlias(internalErr, err, warnErr error) error { + intest.AssertNotNil(sc) + if sc == nil { + return err + } + errCtx := sc.ErrCtx() + return errCtx.HandleErrorWithAlias(internalErr, err, warnErr) +} + // StmtHints are SessionVars related sql hints. type StmtHints struct { // Hint Information @@ -1052,19 +1058,6 @@ func (sc *StatementContext) AppendExtraError(warn error) { } } -// HandleOverflow treats ErrOverflow as warnings or returns the error based on the StmtCtx.OverflowAsWarning state. -func (sc *StatementContext) HandleOverflow(err error, warnErr error) error { - if err == nil { - return nil - } - - if sc.OverflowAsWarning { - sc.AppendWarning(warnErr) - return nil - } - return err -} - // resetMuForRetry resets the changed states of sc.mu during execution. func (sc *StatementContext) resetMuForRetry() { sc.mu.Lock() @@ -1173,8 +1166,7 @@ func (sc *StatementContext) PushDownFlags() uint64 { flags |= model.FlagIgnoreTruncate } else if sc.TypeFlags().TruncateAsWarning() { flags |= model.FlagTruncateAsWarning - } - if sc.OverflowAsWarning { + // TODO: remove this flag from TiKV. flags |= model.FlagOverflowAsWarning } if sc.TypeFlags().IgnoreZeroInDate() { @@ -1241,7 +1233,6 @@ func (sc *StatementContext) InitFromPBFlagAndTz(flags uint64, tz *time.Location) sc.InInsertStmt = (flags & model.FlagInInsertStmt) > 0 sc.InSelectStmt = (flags & model.FlagInSelectStmt) > 0 sc.InDeleteStmt = (flags & model.FlagInUpdateOrDeleteStmt) > 0 - sc.OverflowAsWarning = (flags & model.FlagOverflowAsWarning) > 0 sc.DividedByZeroAsWarning = (flags & model.FlagDividedByZeroAsWarning) > 0 sc.SetTimeZone(tz) sc.SetTypeFlags(types.DefaultStmtFlags. diff --git a/pkg/sessionctx/stmtctx/stmtctx_test.go b/pkg/sessionctx/stmtctx/stmtctx_test.go index 26ec689e304ea..24c60d9375c34 100644 --- a/pkg/sessionctx/stmtctx/stmtctx_test.go +++ b/pkg/sessionctx/stmtctx/stmtctx_test.go @@ -94,15 +94,14 @@ func TestStatementContextPushDownFLags(t *testing.T) { {newStmtCtx(func(sc *stmtctx.StatementContext) { sc.InDeleteStmt = true }), 16}, {newStmtCtx(func(sc *stmtctx.StatementContext) { sc.InSelectStmt = true }), 32}, {newStmtCtx(func(sc *stmtctx.StatementContext) { sc.SetTypeFlags(sc.TypeFlags().WithIgnoreTruncateErr(true)) }), 1}, - {newStmtCtx(func(sc *stmtctx.StatementContext) { sc.SetTypeFlags(sc.TypeFlags().WithTruncateAsWarning(true)) }), 2}, - {newStmtCtx(func(sc *stmtctx.StatementContext) { sc.OverflowAsWarning = true }), 64}, + {newStmtCtx(func(sc *stmtctx.StatementContext) { sc.SetTypeFlags(sc.TypeFlags().WithTruncateAsWarning(true)) }), 66}, {newStmtCtx(func(sc *stmtctx.StatementContext) { sc.SetTypeFlags(sc.TypeFlags().WithIgnoreZeroInDate(true)) }), 128}, {newStmtCtx(func(sc *stmtctx.StatementContext) { sc.DividedByZeroAsWarning = true }), 256}, {newStmtCtx(func(sc *stmtctx.StatementContext) { sc.InLoadDataStmt = true }), 1024}, {newStmtCtx(func(sc *stmtctx.StatementContext) { sc.InSelectStmt = true sc.SetTypeFlags(sc.TypeFlags().WithTruncateAsWarning(true)) - }), 34}, + }), 98}, {newStmtCtx(func(sc *stmtctx.StatementContext) { sc.DividedByZeroAsWarning = true sc.SetTypeFlags(sc.TypeFlags().WithIgnoreTruncateErr(true)) diff --git a/pkg/table/column.go b/pkg/table/column.go index 6c1c3aa4e1d90..34efc261323bb 100644 --- a/pkg/table/column.go +++ b/pkg/table/column.go @@ -353,7 +353,6 @@ func CastValue(ctx sessionctx.Context, val types.Datum, col *model.ColumnInfo, r } err = sc.HandleTruncate(err) - err = sc.HandleOverflow(err, err) if forceIgnoreTruncate { err = nil diff --git a/pkg/util/codec/codec_test.go b/pkg/util/codec/codec_test.go index c67aac19f188e..12cfdb7fda93d 100644 --- a/pkg/util/codec/codec_test.go +++ b/pkg/util/codec/codec_test.go @@ -786,7 +786,7 @@ func TestDecimal(t *testing.T) { err = sc.HandleError(err) require.NoError(t, err) - sc.OverflowAsWarning = true + sc.SetTypeFlags(types.DefaultStmtFlags.WithTruncateAsWarning(true)) decimalDatum.SetLength(12) decimalDatum.SetFrac(10) _, err = EncodeValue(sc.TimeZone(), nil, decimalDatum) diff --git a/tests/integrationtest/r/ddl/integration.result b/tests/integrationtest/r/ddl/integration.result index f4c64ac851ba6..31d3984d97e3b 100644 --- a/tests/integrationtest/r/ddl/integration.result +++ b/tests/integrationtest/r/ddl/integration.result @@ -91,3 +91,11 @@ rename table t to t1; Error 8242 (HY000): 'Rename Table' is unsupported on cache tables. alter table t nocache; drop table if exists t; +drop table if exists t; +create table t (d int default '18446744073709551616' ); +Error 1067 (42000): Invalid default value for 'd' +set sql_mode=''; +create table t (d int default '18446744073709551616' ); +Level Code Message +Warning 1690 BIGINT value is out of range in '18446744073709551616' +set sql_mode=DEFAULT; diff --git a/tests/integrationtest/r/executor/delete.result b/tests/integrationtest/r/executor/delete.result index d452dd98a9e21..3f20878d397c3 100644 --- a/tests/integrationtest/r/executor/delete.result +++ b/tests/integrationtest/r/executor/delete.result @@ -107,3 +107,18 @@ id data 11 321 22 322 23 323 +drop table if exists t; +create table t (id char(255)); +insert into t values ('18446744073709551616'); +delete from t where cast(id as unsigned) = 1; +Error 1690 (22003): BIGINT value is out of range in '18446744073709551616' +update t set id = '1' where cast(id as unsigned) = 1; +Error 1690 (22003): BIGINT value is out of range in '18446744073709551616' +set sql_mode='' +delete from t where cast(id as unsigned) = 1; +Level Code Message +Warning 1292 Truncated incorrect INTEGER value: '18446744073709551616' +update t set id = '1' where cast(id as unsigned) = 1; +Level Code Message +Warning 1292 Truncated incorrect INTEGER value: '18446744073709551616' +set sql_mode=DEFAULT diff --git a/tests/integrationtest/r/executor/insert.result b/tests/integrationtest/r/executor/insert.result index 977ff1e9ac41e..b6efffe682611 100644 --- a/tests/integrationtest/r/executor/insert.result +++ b/tests/integrationtest/r/executor/insert.result @@ -2131,3 +2131,13 @@ insert into t1 set c1 = 0.1 on duplicate key update c1 = 1; select * from t1 use index(primary); c1 1.0000 +drop table if exists t; +create table t (d int); +insert into t values (cast('18446744073709551616' as unsigned)); +Error 1690 (22003): BIGINT UNSIGNED value is out of range in '18446744073709551616' +set sql_mode=''; +insert into t values (cast('18446744073709551616' as unsigned)); +Level Code Message +Warning 1264 Out of range value for column 'd' at row 1 +Warning 1292 Truncated incorrect INTEGER value: '18446744073709551616' +set sql_mode=DEFAULT; diff --git a/tests/integrationtest/t/ddl/integration.test b/tests/integrationtest/t/ddl/integration.test index 36d87b0e74f5f..4104c1f824c67 100644 --- a/tests/integrationtest/t/ddl/integration.test +++ b/tests/integrationtest/t/ddl/integration.test @@ -73,3 +73,12 @@ rename table t to t1; alter table t nocache; drop table if exists t; +# TestNonStrictCreateTableOverflowError +drop table if exists t; +-- error 1067 +create table t (d int default '18446744073709551616' ); +set sql_mode=''; +-- enable_warnings +create table t (d int default '18446744073709551616' ); +-- disable_warnings +set sql_mode=DEFAULT; diff --git a/tests/integrationtest/t/executor/delete.test b/tests/integrationtest/t/executor/delete.test index ff22401ba0de5..b238e70274716 100644 --- a/tests/integrationtest/t/executor/delete.test +++ b/tests/integrationtest/t/executor/delete.test @@ -97,4 +97,23 @@ delete t1, t2 from t1 inner join t2 inner join t3 where t1.id=t2.id and t2.id=t3 --sorted_result select * from t3; +# TestNonStrictDeleteUpdateOverflowExpression +drop table if exists t; +create table t (id char(255)); +insert into t values ('18446744073709551616'); +# TODO: the error and warning message is different between TiDB and TiKV. Fix it. +-- replace_regex /BIGINT UNSIGNED/BIGINT/ +-- error 1690 +delete from t where cast(id as unsigned) = 1; +-- replace_regex /BIGINT UNSIGNED/BIGINT/ +-- error 1690 +update t set id = '1' where cast(id as unsigned) = 1; +set sql_mode='' +-- enable_warnings +-- replace_regex /evaluation failed: // +delete from t where cast(id as unsigned) = 1; +-- replace_regex /evaluation failed: // +update t set id = '1' where cast(id as unsigned) = 1; +-- disable_warnings +set sql_mode=DEFAULT diff --git a/tests/integrationtest/t/executor/insert.test b/tests/integrationtest/t/executor/insert.test index 8d241c2a5d7e6..f1b0ca3826393 100644 --- a/tests/integrationtest/t/executor/insert.test +++ b/tests/integrationtest/t/executor/insert.test @@ -1615,3 +1615,14 @@ create table t1(c1 decimal(6,4), primary key(c1)); insert into t1 set c1 = 0.1; insert into t1 set c1 = 0.1 on duplicate key update c1 = 1; select * from t1 use index(primary); + +# TestNonStrictInsertOverflowValue +drop table if exists t; +create table t (d int); +-- error 1690 +insert into t values (cast('18446744073709551616' as unsigned)); +set sql_mode=''; +--enable_warnings +insert into t values (cast('18446744073709551616' as unsigned)); +--disable_warnings +set sql_mode=DEFAULT;