Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
Signed-off-by: lzmhhh123 <[email protected]>
  • Loading branch information
lzmhhh123 committed Dec 4, 2020
1 parent d725807 commit fe3d89c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 94 deletions.
109 changes: 29 additions & 80 deletions expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,16 +829,8 @@ func (b *builtinDateFormatSig) evalString(row chunk.Row) (string, bool, error) {
}

if t.InvalidZero() {
// MySQL compatibility, #11203
// 0 | 0.0 should be converted to null without warnings
n, err := t.ToNumber().ToInt()
isOriginalIntOrDecimalZero := err == nil && n == 0
// Args like "0000-00-00", "0000-00-00 00:00:00" set Fsp to 6
isOriginalStringZero := t.Fsp() > 0
if isOriginalIntOrDecimalZero && !isOriginalStringZero {
return "", true, nil
}
return "", true, handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, t.String()))
isNull, err := handleInvalidZeroTime(b.ctx, t)
return "", isNull, err
}

res, err := t.DateFormat(formatMask)
Expand Down Expand Up @@ -1079,16 +1071,8 @@ func (b *builtinMonthSig) evalInt(row chunk.Row) (int64, bool, error) {

if date.IsZero() {
if b.ctx.GetSessionVars().SQLMode.HasNoZeroDateMode() {
// MySQL compatibility, #11203
// 0 | 0.0 should be converted to 0 value (not null)
n, err := date.ToNumber().ToInt()
isOriginalIntOrDecimalZero := err == nil && n == 0
// Args like "0000-00-00", "0000-00-00 00:00:00" set Fsp to 6
isOriginalStringZero := date.Fsp() > 0
if isOriginalIntOrDecimalZero && !isOriginalStringZero {
return 0, false, nil
}
return 0, true, handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, date.String()))
isNull, err = handleInvalidZeroTime(b.ctx, date)
return 0, isNull, err
}
return 0, false, nil
}
Expand Down Expand Up @@ -1247,16 +1231,8 @@ func (b *builtinDayOfMonthSig) evalInt(row chunk.Row) (int64, bool, error) {
}
if arg.IsZero() {
if b.ctx.GetSessionVars().SQLMode.HasNoZeroDateMode() {
// MySQL compatibility, #11203
// 0 | 0.0 should be converted to 0 value (not null)
n, err := arg.ToNumber().ToInt()
isOriginalIntOrDecimalZero := err == nil && n == 0
// Args like "0000-00-00", "0000-00-00 00:00:00" set Fsp to 6
isOriginalStringZero := arg.Fsp() > 0
if isOriginalIntOrDecimalZero && !isOriginalStringZero {
return 0, false, nil
}
return 0, true, handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, arg.String()))
isNull, err = handleInvalidZeroTime(b.ctx, arg)
return 0, isNull, err
}
return 0, false, nil
}
Expand Down Expand Up @@ -1299,16 +1275,8 @@ func (b *builtinDayOfWeekSig) evalInt(row chunk.Row) (int64, bool, error) {
return 0, true, handleInvalidTimeError(b.ctx, err)
}
if arg.InvalidZero() {
// MySQL compatibility, #11203
// 0 | 0.0 should be converted to 0 value (not null)
n, err := arg.ToNumber().ToInt()
isOriginalIntOrDecimalZero := err == nil && n == 0
// Args like "0000-00-00", "0000-00-00 00:00:00" set Fsp to 6
isOriginalStringZero := arg.Fsp() > 0
if isOriginalIntOrDecimalZero && !isOriginalStringZero {
return 0, false, nil
}
return 0, true, handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, arg.String()))
isNull, err = handleInvalidZeroTime(b.ctx, arg)
return 0, isNull, err
}
// 1 is Sunday, 2 is Monday, .... 7 is Saturday
return int64(arg.Weekday() + 1), false, nil
Expand Down Expand Up @@ -1350,16 +1318,8 @@ func (b *builtinDayOfYearSig) evalInt(row chunk.Row) (int64, bool, error) {
return 0, isNull, handleInvalidTimeError(b.ctx, err)
}
if arg.InvalidZero() {
// MySQL compatibility, #11203
// 0 | 0.0 should be converted to 0 value (not null)
n, err := arg.ToNumber().ToInt()
isOriginalIntOrDecimalZero := err == nil && n == 0
// Args like "0000-00-00", "0000-00-00 00:00:00" set Fsp to 6
isOriginalStringZero := arg.Fsp() > 0
if isOriginalIntOrDecimalZero && !isOriginalStringZero {
return 0, false, nil
}
return 0, true, handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, arg.String()))
isNull, err := handleInvalidZeroTime(b.ctx, t)
return 0, isNull, err
}

return int64(arg.YearDay()), false, nil
Expand Down Expand Up @@ -1593,16 +1553,8 @@ func (b *builtinYearSig) evalInt(row chunk.Row) (int64, bool, error) {

if date.IsZero() {
if b.ctx.GetSessionVars().SQLMode.HasNoZeroDateMode() {
// MySQL compatibility, #11203
// 0 | 0.0 should be converted to 0 value (not null)
n, err := date.ToNumber().ToInt()
isOriginalIntOrDecimalZero := err == nil && n == 0
// Args like "0000-00-00", "0000-00-00 00:00:00" set Fsp to 6
isOriginalStringZero := date.Fsp() > 0
if isOriginalIntOrDecimalZero && !isOriginalStringZero {
return 0, false, nil
}
return 0, true, handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, date.String()))
isNull, err := handleInvalidZeroTime(b.ctx, t)
return 0, isNull, err
}
return 0, false, nil
}
Expand Down Expand Up @@ -2711,16 +2663,8 @@ func (b *builtinExtractDatetimeSig) evalInt(row chunk.Row) (int64, bool, error)
}
if dt.IsZero() {
if b.ctx.GetSessionVars().SQLMode.HasNoZeroDateMode() {
// MySQL compatibility, #11203
// 0 | 0.0 should be converted to 0 value (not null)
n, err := dt.ToNumber().ToInt()
isOriginalIntOrDecimalZero := err == nil && n == 0
// Args like "0000-00-00", "0000-00-00 00:00:00" set Fsp to 6
isOriginalStringZero := dt.Fsp() > 0
if isOriginalIntOrDecimalZero && !isOriginalStringZero {
return 0, false, nil
}
return 0, true, handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, dt.String()))
isNull, err := handleInvalidZeroTime(b.ctx, t)
return 0, isNull, err
}
return 0, false, nil
}
Expand Down Expand Up @@ -6080,16 +6024,8 @@ func (b *builtinQuarterSig) evalInt(row chunk.Row) (int64, bool, error) {
}

if date.IsZero() {
// MySQL compatibility, #11203
// 0 | 0.0 should be converted to 0 value (not null)
n, err := date.ToNumber().ToInt()
isOriginalIntOrDecimalZero := err == nil && n == 0
// Args like "0000-00-00", "0000-00-00 00:00:00" set Fsp to 6
isOriginalStringZero := date.Fsp() > 0
if isOriginalIntOrDecimalZero && !isOriginalStringZero {
return 0, false, nil
}
return 0, true, handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, date.String()))
isNull, err := handleInvalidZeroTime(b.ctx, t)
return 0, isNull, err
}

return int64((date.Month() + 2) / 3), false, nil
Expand Down Expand Up @@ -7080,3 +7016,16 @@ func (b *builtinTidbParseTsoSig) evalTime(row chunk.Row) (types.Time, bool, erro
}
return result, false, nil
}

func handleInvalidZeroTime(ctx sessionctx.Context, t types.Time) (bool, error) {
// MySQL compatibility, #11203
// 0 | 0.0 should be converted to null without warnings
n, err := t.ToNumber().ToInt()
isOriginalIntOrDecimalZero := err == nil && n == 0
// Args like "0000-00-00", "0000-00-00 00:00:00" set Fsp to 6
isOriginalStringZero := t.Fsp() > 0
if isOriginalIntOrDecimalZero && !isOriginalStringZero {
return true, nil
}
return true, handleInvalidTimeError(ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, t.String()))
}
32 changes: 18 additions & 14 deletions expression/builtin_time_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ func (b *builtinMonthSig) vecEvalInt(input *chunk.Chunk, result *chunk.Column) e
}
if ds[i].IsZero() {
if b.ctx.GetSessionVars().SQLMode.HasNoZeroDateMode() {
if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, ds[i].String())); err != nil {
isNull, err := handleInvalidZeroTime(b.ctx, ds[i])
if err != nil {
return err
}
result.SetNull(i, true)
result.SetNull(i, isNull)
continue
}
i64s[i] = 0
Expand Down Expand Up @@ -89,10 +90,11 @@ func (b *builtinYearSig) vecEvalInt(input *chunk.Chunk, result *chunk.Column) er
}
if ds[i].IsZero() {
if b.ctx.GetSessionVars().SQLMode.HasNoZeroDateMode() {
if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, ds[i].String())); err != nil {
isNull, err := handleInvalidZeroTime(b.ctx, ds[i])
if err != nil {
return err
}
result.SetNull(i, true)
result.SetNull(i, isNull)
continue
}
i64s[i] = 0
Expand Down Expand Up @@ -923,10 +925,11 @@ func (b *builtinQuarterSig) vecEvalInt(input *chunk.Chunk, result *chunk.Column)
}
date := ds[i]
if date.IsZero() {
if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, date.String())); err != nil {
isNull, err := handleInvalidZeroTime(b.ctx, ds[i])
if err != nil {
return err
}
result.SetNull(i, true)
result.SetNull(i, isNull)
continue
}
i64s[i] = int64((date.Month() + 2) / 3)
Expand Down Expand Up @@ -2068,10 +2071,11 @@ func (b *builtinDayOfYearSig) vecEvalInt(input *chunk.Chunk, result *chunk.Colum
continue
}
if ds[i].InvalidZero() {
if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, ds[i].String())); err != nil {
isNull, err := handleInvalidZeroTime(b.ctx, ds[i])
if err != nil {
return err
}
result.SetNull(i, true)
result.SetNull(i, isNull)
continue
}
i64s[i] = int64(ds[i].YearDay())
Expand Down Expand Up @@ -2452,11 +2456,11 @@ func (b *builtinDayOfWeekSig) vecEvalInt(input *chunk.Chunk, result *chunk.Colum
continue
}
if ds[i].InvalidZero() {
if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, ds[i].String())); err != nil {
isNull, err := handleInvalidZeroTime(b.ctx, ds[i])
if err != nil {
return err
}
result.SetNull(i, true)
continue
result.SetNull(i, isNull)
}
i64s[i] = int64(ds[i].Weekday() + 1)
}
Expand Down Expand Up @@ -2703,11 +2707,11 @@ func (b *builtinDayOfMonthSig) vecEvalInt(input *chunk.Chunk, result *chunk.Colu
}
if ds[i].IsZero() {
if b.ctx.GetSessionVars().SQLMode.HasNoZeroDateMode() {
if err := handleInvalidTimeError(b.ctx, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, ds[i].String())); err != nil {
isNull, err := handleInvalidZeroTime(b.ctx, ds[i])
if err != nil {
return err
}
result.SetNull(i, true)
continue
result.SetNull(i, isNull)
}
i64s[i] = 0
continue
Expand Down

0 comments on commit fe3d89c

Please sign in to comment.