Skip to content

Commit

Permalink
*: fix warning message when to meet strconv.ErrSyntax (pingcap#43358) (
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Sep 23, 2024
1 parent 3f20732 commit 116ffee
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 1 deletion.
5 changes: 5 additions & 0 deletions errors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2846,6 +2846,11 @@ error = '''
invalid year
'''

["types:8034"]
error = '''
Incorrect datetime value: '%s'
'''

["types:8037"]
error = '''
invalid week mode format: '%v'
Expand Down
4 changes: 4 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5515,6 +5515,10 @@ func TestStrToDateBuiltinWithWarnings(t *testing.T) {
tk.MustExec("use test")
tk.MustQuery(`SELECT STR_TO_DATE('0000-1-01', '%Y-%m-%d');`).Check(testkit.Rows("<nil>"))
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1411 Incorrect datetime value: '0000-1-01' for function str_to_date"))
tk.MustQuery("SELECT CAST('4#,8?Q' AS DATE);").Check(testkit.Rows("<nil>"))
tk.MustQuery(`show warnings;`).Check(testkit.Rows(
`Warning 8034 Incorrect datetime value: '4#,8?Q'`,
))
}

func TestReadPartitionedTable(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions expression/builtin_cast_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package expression

import (
"errors"
"math"
"strconv"
"strings"
Expand Down Expand Up @@ -1764,6 +1765,9 @@ func (b *builtinCastStringAsTimeSig) vecEvalTime(input *chunk.Chunk, result *chu
}
tm, err := types.ParseTime(stmtCtx, buf.GetString(i), b.tp.GetType(), fsp, nil)
if err != nil {
if errors.Is(err, strconv.ErrSyntax) {
err = types.ErrIncorrectDatetimeValue.GenWithStackByArgs(buf.GetString(i))
}
if err = handleInvalidTimeError(b.ctx, err); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion expression/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ var (
func handleInvalidTimeError(ctx sessionctx.Context, err error) error {
if err == nil || !(types.ErrWrongValue.Equal(err) || types.ErrWrongValueForType.Equal(err) ||
types.ErrTruncatedWrongVal.Equal(err) || types.ErrInvalidWeekModeFormat.Equal(err) ||
types.ErrDatetimeFunctionOverflow.Equal(err)) {
types.ErrDatetimeFunctionOverflow.Equal(err) || types.ErrIncorrectDatetimeValue.Equal(err)) {
return err
}
sc := ctx.GetSessionVars().StmtCtx
Expand Down
1 change: 1 addition & 0 deletions sessionctx/stmtctx/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ go_library(
importpath = "github.com/pingcap/tidb/sessionctx/stmtctx",
visibility = ["//visibility:public"],
deps = [
"//errno",
"//parser",
"//parser/ast",
"//parser/model",
Expand Down
17 changes: 17 additions & 0 deletions sessionctx/stmtctx/stmtctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"time"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/errno"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/model"
Expand Down Expand Up @@ -880,6 +881,22 @@ func (sc *StatementContext) HandleTruncate(err error) error {
if err == nil {
return nil
}

err = errors.Cause(err)
if e, ok := err.(*errors.Error); !ok ||
(e.Code() != errno.ErrTruncatedWrongValue &&
e.Code() != errno.ErrDataTooLong &&
e.Code() != errno.ErrTruncatedWrongValueForField &&
e.Code() != errno.ErrWarnDataOutOfRange &&
e.Code() != errno.ErrDataOutOfRange &&
e.Code() != errno.ErrBadNumber &&
e.Code() != errno.ErrWrongValueForType &&
e.Code() != errno.ErrDatetimeFunctionOverflow &&
e.Code() != errno.WarnDataTruncated &&
e.Code() != errno.ErrIncorrectDatetimeValue) {
return err
}

if sc.IgnoreTruncate {
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,6 @@ var (
// ErrPartitionColumnStatsMissing is returned when the partition-level column stats is missing and the build global-level stats fails.
// Put this error here is to prevent `import cycle not allowed`.
ErrPartitionColumnStatsMissing = dbterror.ClassTypes.NewStd(mysql.ErrPartitionColumnStatsMissing)
// ErrIncorrectDatetimeValue is returned when the input value is in wrong format for datetime.
ErrIncorrectDatetimeValue = dbterror.ClassTypes.NewStd(mysql.ErrIncorrectDatetimeValue)
)

0 comments on commit 116ffee

Please sign in to comment.