Skip to content

Commit

Permalink
types,expression: make 'invalid time format' error log more friendly (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored Mar 17, 2018
1 parent ebdcdb1 commit 11616c4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -2874,7 +2874,7 @@ type builtinUnixTimestampIntSig struct {
// See https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_unix-timestamp
func (b *builtinUnixTimestampIntSig) evalInt(row types.Row) (int64, bool, error) {
val, isNull, err := b.args[0].EvalTime(b.ctx, row)
if err != nil && terror.ErrorEqual(types.ErrInvalidTimeFormat, err) {
if err != nil && terror.ErrorEqual(types.ErrInvalidTimeFormat.GenByArgs(val), err) {
// Return 0 for invalid date time.
return 0, false, nil
}
Expand Down Expand Up @@ -4425,7 +4425,7 @@ func (b *builtinTimestampAddSig) evalString(row types.Row) (string, bool, error)
case "YEAR":
tb = tm1.AddDate(int(v), 0, 0)
default:
return "", true, errors.Trace(types.ErrInvalidTimeFormat)
return "", true, errors.Trace(types.ErrInvalidTimeFormat.GenByArgs(unit))
}
r := types.Time{Time: types.FromGoTime(tb), Type: mysql.TypeDatetime, Fsp: fsp}
if err = r.Check(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2660,14 +2660,14 @@ func (s *testIntegrationSuite) TestCompareBuiltin(c *C) {
result = tk.MustQuery(`select greatest(cast("2017-01-01" as datetime), "123", "234", cast("2018-01-01" as date)), greatest(cast("2017-01-01" as date), "123", null)`)
// todo: MySQL returns "2018-01-01 <nil>"
result.Check(testkit.Rows("2018-01-01 00:00:00 <nil>"))
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1105|invalid time format", "Warning|1105|invalid time format", "Warning|1105|invalid time format"))
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|invalid time format: '123'", "Warning|1292|invalid time format: '234'", "Warning|1292|invalid time format: '123'"))
// for least
result = tk.MustQuery(`select least(1, 2, 3), least("a", "b", "c"), least(1.1, 1.2, 1.3), least("123a", 1, 2)`)
result.Check(testkit.Rows("1 a 1.1 1"))
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1265|Data Truncated"))
result = tk.MustQuery(`select least(cast("2017-01-01" as datetime), "123", "234", cast("2018-01-01" as date)), least(cast("2017-01-01" as date), "123", null)`)
result.Check(testkit.Rows("123 <nil>"))
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1105|invalid time format", "Warning|1105|invalid time format", "Warning|1105|invalid time format"))
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1292|invalid time format: '123'", "Warning|1292|invalid time format: '234'", "Warning|1292|invalid time format: '123'"))

tk.MustQuery(`select 1 < 17666000000000000000, 1 > 17666000000000000000, 1 = 17666000000000000000`).Check(testkit.Rows("1 0 0"))

Expand Down
2 changes: 1 addition & 1 deletion types/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func NumberToDuration(number int64, fsp int) (Duration, error) {
}

if number/10000 > TimeMaxHour || number%100 >= 60 || (number/100)%100 >= 60 {
return ZeroDuration, ErrInvalidTimeFormat
return ZeroDuration, errors.Trace(ErrInvalidTimeFormat.GenByArgs(number))
}
t := Time{Time: FromDate(0, 0, 0, int(number/10000), int((number/100)%100), int(number%100), 0), Type: mysql.TypeDuration, Fsp: fsp}
dur, err := t.ConvertToDuration()
Expand Down
2 changes: 1 addition & 1 deletion types/mytime.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (t MysqlTime) GoTime(loc *gotime.Location) (gotime.Time, error) {
if year != t.Year() || int(month) != t.Month() || day != t.Day() ||
hour != t.Hour() || minute != t.Minute() || second != t.Second() ||
microsec != t.Microsecond() {
return tm, errors.Trace(ErrInvalidTimeFormat)
return tm, errors.Trace(ErrInvalidTimeFormat.GenByArgs(t))
}
return tm, nil
}
Expand Down
42 changes: 21 additions & 21 deletions types/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

// Portable analogs of some common call errors.
var (
ErrInvalidTimeFormat = errors.New("invalid time format")
ErrInvalidTimeFormat = terror.ClassTypes.New(mysql.ErrTruncatedWrongValue, "invalid time format: '%v'")
ErrInvalidYearFormat = errors.New("invalid year format")
ErrInvalidYear = errors.New("invalid year")
ErrZeroDate = errors.New("datetime zero in date")
Expand Down Expand Up @@ -369,7 +369,7 @@ func (t Time) RoundFrac(sc *stmtctx.StatementContext, fsp int) (Time, error) {
// TODO: when hh:mm:ss overflow one day after rounding, it should be add to yy:mm:dd part,
// but mm:dd may contain 0, it makes the code complex, so we ignore it here.
if t2.Day()-1 > 0 {
return t, errors.Trace(ErrInvalidTimeFormat)
return t, errors.Trace(ErrInvalidTimeFormat.GenByArgs(t.String()))
}
nt = FromDate(t.Time.Year(), t.Time.Month(), t.Time.Day(), hour, minute, second, microsecond)
}
Expand Down Expand Up @@ -617,7 +617,7 @@ func parseDatetime(str string, fsp int, isFloat bool) (Time, error) {
_, err = fmt.Sscanf(sep, "%2d%2d%2d", &year, &month, &day)
year = adjustYear(year)
} else {
return ZeroDatetime, errors.Trace(ErrInvalidTimeFormat)
return ZeroDatetime, errors.Trace(ErrInvalidTimeFormat.GenByArgs(str))
}
if len(sep) == 6 || len(sep) == 8 {
// YYMMDD or YYYYMMDD
Expand All @@ -643,7 +643,7 @@ func parseDatetime(str string, fsp int, isFloat bool) (Time, error) {
err = scanTimeArgs(seps, &year, &month, &day, &hour, &minute, &second)
hhmmss = true
default:
return ZeroDatetime, errors.Trace(ErrInvalidTimeFormat)
return ZeroDatetime, errors.Trace(ErrInvalidTimeFormat.GenByArgs(str))
}
if err != nil {
return ZeroDatetime, errors.Trace(err)
Expand Down Expand Up @@ -690,7 +690,7 @@ func parseDatetime(str string, fsp int, isFloat bool) (Time, error) {

func scanTimeArgs(seps []string, args ...*int) error {
if len(seps) != len(args) {
return errors.Trace(ErrInvalidTimeFormat)
return errors.Trace(ErrInvalidTimeFormat.GenByArgs(seps))
}

var err error
Expand Down Expand Up @@ -1121,7 +1121,7 @@ func parseDateTimeFromNum(sc *stmtctx.StatementContext, num int64) (Time, error)

// Check MMDD.
if num < 101 {
return t, errors.Trace(ErrInvalidTimeFormat)
return t, errors.Trace(ErrInvalidTimeFormat.GenByArgs(num))
}

// Adjust year
Expand All @@ -1133,7 +1133,7 @@ func parseDateTimeFromNum(sc *stmtctx.StatementContext, num int64) (Time, error)

// Check YYMMDD.
if num < 70*10000+101 {
return t, errors.Trace(ErrInvalidTimeFormat)
return t, errors.Trace(ErrInvalidTimeFormat.GenByArgs(num))
}

// Adjust year
Expand All @@ -1145,7 +1145,7 @@ func parseDateTimeFromNum(sc *stmtctx.StatementContext, num int64) (Time, error)

// Check YYYYMMDD.
if num < 10000101 {
return t, errors.Trace(ErrInvalidTimeFormat)
return t, errors.Trace(ErrInvalidTimeFormat.GenByArgs(num))
}

// Adjust hour/min/second.
Expand All @@ -1156,7 +1156,7 @@ func parseDateTimeFromNum(sc *stmtctx.StatementContext, num int64) (Time, error)

// Check MMDDHHMMSS.
if num < 101000000 {
return t, errors.Trace(ErrInvalidTimeFormat)
return t, errors.Trace(ErrInvalidTimeFormat.GenByArgs(num))
}

// Set TypeDatetime type.
Expand All @@ -1171,7 +1171,7 @@ func parseDateTimeFromNum(sc *stmtctx.StatementContext, num int64) (Time, error)

// Check YYYYMMDDHHMMSS.
if num < 70*10000000000+101000000 {
return t, errors.Trace(ErrInvalidTimeFormat)
return t, errors.Trace(ErrInvalidTimeFormat.GenByArgs(num))
}

// Adjust year
Expand Down Expand Up @@ -1318,17 +1318,17 @@ func checkDateRange(t MysqlTime) error {
// Oddly enough, MySQL document says date range should larger than '1000-01-01',
// but we can insert '0001-01-01' actually.
if t.Year() < 0 || t.Month() < 0 || t.Day() < 0 {
return errors.Trace(ErrInvalidTimeFormat)
return errors.Trace(ErrInvalidTimeFormat.GenByArgs(t))
}
if compareTime(t, MaxDatetime) > 0 {
return errors.Trace(ErrInvalidTimeFormat)
return errors.Trace(ErrInvalidTimeFormat.GenByArgs(t))
}
return nil
}

func checkMonthDay(year, month, day int) error {
if month < 0 || month > 12 {
return errors.Trace(ErrInvalidTimeFormat)
return errors.Trace(ErrInvalidTimeFormat.GenByArgs(month))
}

maxDay := 31
Expand All @@ -1340,7 +1340,7 @@ func checkMonthDay(year, month, day int) error {
}

if day < 0 || day > maxDay {
return errors.Trace(ErrInvalidTimeFormat)
return errors.Trace(ErrInvalidTimeFormat.GenByArgs(day))
}
return nil
}
Expand All @@ -1351,7 +1351,7 @@ func checkTimestampType(t MysqlTime) error {
}

if compareTime(t, maxTimestamp) > 0 || compareTime(t, MinTimestamp) < 0 {
return errors.Trace(ErrInvalidTimeFormat)
return errors.Trace(ErrInvalidTimeFormat.GenByArgs(t))
}

if _, err := t.GoTime(gotime.Local); err != nil {
Expand All @@ -1368,13 +1368,13 @@ func checkDatetimeType(t MysqlTime, allowZeroInDate bool) error {

hour, minute, second := t.Hour(), t.Minute(), t.Second()
if hour < 0 || hour >= 24 {
return errors.Trace(ErrInvalidTimeFormat)
return errors.Trace(ErrInvalidTimeFormat.GenByArgs(hour))
}
if minute < 0 || minute >= 60 {
return errors.Trace(ErrInvalidTimeFormat)
return errors.Trace(ErrInvalidTimeFormat.GenByArgs(minute))
}
if second < 0 || second >= 60 {
return errors.Trace(ErrInvalidTimeFormat)
return errors.Trace(ErrInvalidTimeFormat.GenByArgs(second))
}

return nil
Expand Down Expand Up @@ -1818,13 +1818,13 @@ func (t Time) convertDateFormat(b rune, buf *bytes.Buffer) error {
case 'b':
m := t.Time.Month()
if m == 0 || m > 12 {
return errors.Trace(ErrInvalidTimeFormat)
return errors.Trace(ErrInvalidTimeFormat.GenByArgs(m))
}
buf.WriteString(MonthNames[m-1][:3])
case 'M':
m := t.Time.Month()
if m == 0 || m > 12 {
return errors.Trace(ErrInvalidTimeFormat)
return errors.Trace(ErrInvalidTimeFormat.GenByArgs(m))
}
buf.WriteString(MonthNames[m-1])
case 'm':
Expand Down Expand Up @@ -1976,7 +1976,7 @@ func mysqlTimeFix(t *MysqlTime, ctx map[string]int) error {
}
if valueAMorPm, ok := ctx["%p"]; ok {
if t.hour == 0 {
return ErrInvalidTimeFormat
return ErrInvalidTimeFormat.GenByArgs(t)
}
if t.hour == 12 {
// 12 is a special hour.
Expand Down

0 comments on commit 11616c4

Please sign in to comment.