diff --git a/go/mysql/datetime/datetime.go b/go/mysql/datetime/datetime.go index 7a532818cc8..1673a71d662 100644 --- a/go/mysql/datetime/datetime.go +++ b/go/mysql/datetime/datetime.go @@ -245,7 +245,14 @@ func (d Date) Hash(h *vthash.Hasher) { } func (d Date) Weekday() time.Weekday { - return d.ToStdTime(time.Local).Weekday() + // Go considers 0000-01-01 day as Saturday, while + // MySQL considers it to be Sunday, now 0000-02-29 exists in + // Go but not in MySQL so it balances out after that + wd := d.ToStdTime(time.Local).Weekday() + if d.Year() == 0 && d.Month() <= 2 { + wd = (wd + 1) % 7 + } + return wd } func (d Date) Yearday() int { diff --git a/go/mysql/datetime/helpers.go b/go/mysql/datetime/helpers.go index c199844df19..b91114fd791 100644 --- a/go/mysql/datetime/helpers.go +++ b/go/mysql/datetime/helpers.go @@ -245,7 +245,7 @@ func daysIn(m time.Month, year int) int { } func isLeap(year int) bool { - return year%4 == 0 && (year%100 != 0 || year%400 == 0) + return year%4 == 0 && (year%100 != 0 || year%400 == 0) && (year != 0) } func daysInYear(year int) int { diff --git a/go/vt/vtgate/evalengine/fn_time.go b/go/vt/vtgate/evalengine/fn_time.go index ecb1fedc135..319f8d1b328 100644 --- a/go/vt/vtgate/evalengine/fn_time.go +++ b/go/vt/vtgate/evalengine/fn_time.go @@ -341,6 +341,10 @@ func convertTz(dt datetime.DateTime, from, to *time.Location) (datetime.DateTime if err != nil { return datetime.DateTime{}, false } + + if ts.Unix() < 0 || ts.Unix() >= maxUnixtime { + return dt, true + } return datetime.NewDateTimeFromStd(ts.In(to)), true } @@ -504,7 +508,7 @@ func (b *builtinDayOfWeek) eval(env *ExpressionEnv) (eval, error) { if d == nil || d.isZero() { return nil, nil } - return newEvalInt64(int64(d.dt.Date.ToStdTime(env.currentTimezone()).Weekday() + 1)), nil + return newEvalInt64(int64(d.dt.Date.Weekday() + 1)), nil } func (call *builtinDayOfWeek) compile(c *compiler) (ctype, error) { @@ -1494,6 +1498,10 @@ func dateTimeUnixTimestamp(env *ExpressionEnv, date eval) evalNumeric { } ts := dt.dt.ToStdTime(env.now) + if ts.Unix() < 0 || ts.Unix() >= maxUnixtime { + return newEvalInt64(0) + } + if dt.prec == 0 { return newEvalInt64(ts.Unix()) } diff --git a/go/vt/vtgate/evalengine/testcases/inputs.go b/go/vt/vtgate/evalengine/testcases/inputs.go index f5fa75854e0..b4a558d1145 100644 --- a/go/vt/vtgate/evalengine/testcases/inputs.go +++ b/go/vt/vtgate/evalengine/testcases/inputs.go @@ -108,7 +108,7 @@ var inputConversions = []string{ "'20000101103458foo'", "'20000101103458.1234foo'", "'20000101103458.123456foo'", "'20000101foo'", "'103458foo'", "'103458.123456foo'", "time '-10:04:58'", "time '-31:34:58'", "time '-32:34:58'", "time '-101:34:58'", "time '-5 10:34:58'", - "'10:04:58'", "'101:34:58'", "'5 10:34:58'", "'2000-01-01'", "'2000-01-01 12:34:58'", + "'10:04:58'", "'101:34:58'", "'5 10:34:58'", "'2000-01-01'", "'2000-01-01 12:34:58'", "'0000-02-29'", "'0000-01-03'", "'1969-02-18'", "'1970-01-01 00:00:01'", "'3001-02-19 00:00:00'", "'3001-02-18 23:59:59'", "cast(0 as json)", "cast(1 as json)", "cast(true as json)", "cast(false as json)", "cast('{}' as json)", "cast('[]' as json)",