From 1feabf5b11e8eda8feace6c6ac0e69766d0f0436 Mon Sep 17 00:00:00 2001 From: Manan Gupta <35839558+GuptaManan100@users.noreply.github.com> Date: Fri, 27 Dec 2024 03:33:08 +0530 Subject: [PATCH] Fix week number for date_format evalengine function (#17432) --- go/mysql/datetime/spec.go | 14 ++++---------- go/vt/vtgate/evalengine/compiler_test.go | 20 ++++++++++++++++++++ go/vt/vtgate/evalengine/testcases/inputs.go | 1 + 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/go/mysql/datetime/spec.go b/go/mysql/datetime/spec.go index ce19126ce55..fe6b1521e43 100644 --- a/go/mysql/datetime/spec.go +++ b/go/mysql/datetime/spec.go @@ -359,10 +359,7 @@ func (t2 fmtFullTime24) parse(t *timeparts, bytes string) (string, bool) { type fmtWeek0 struct{} func (fmtWeek0) format(dst []byte, t DateTime, prec uint8) []byte { - year, week := t.Date.SundayWeek() - if year < t.Date.Year() { - week = 0 - } + week := t.Date.Week(0) return appendInt(dst, week, 2) } @@ -374,10 +371,7 @@ func (u fmtWeek0) parse(t *timeparts, bytes string) (string, bool) { type fmtWeek1 struct{} func (fmtWeek1) format(dst []byte, t DateTime, prec uint8) []byte { - year, week := t.Date.ISOWeek() - if year < t.Date.Year() { - week = 0 - } + week := t.Date.Week(1) return appendInt(dst, week, 2) } @@ -389,7 +383,7 @@ func (u fmtWeek1) parse(t *timeparts, bytes string) (string, bool) { type fmtWeek2 struct{} func (fmtWeek2) format(dst []byte, t DateTime, prec uint8) []byte { - _, week := t.Date.SundayWeek() + week := t.Date.Week(2) return appendInt(dst, week, 2) } @@ -401,7 +395,7 @@ func (v fmtWeek2) parse(t *timeparts, bytes string) (string, bool) { type fmtWeek3 struct{} func (fmtWeek3) format(dst []byte, t DateTime, prec uint8) []byte { - _, week := t.Date.ISOWeek() + week := t.Date.Week(3) return appendInt(dst, week, 2) } diff --git a/go/vt/vtgate/evalengine/compiler_test.go b/go/vt/vtgate/evalengine/compiler_test.go index cb9b99e7776..88c13a479ed 100644 --- a/go/vt/vtgate/evalengine/compiler_test.go +++ b/go/vt/vtgate/evalengine/compiler_test.go @@ -740,6 +740,26 @@ func TestCompilerSingle(t *testing.T) { expression: `cast(_utf32 0x0000FF as binary)`, result: `VARBINARY("\x00\x00\x00\xff")`, }, + { + expression: `DATE_FORMAT(timestamp '2024-12-30 10:34:58', "%u")`, + result: `VARCHAR("53")`, + }, + { + expression: `WEEK(timestamp '2024-12-30 10:34:58', 0)`, + result: `INT64(52)`, + }, + { + expression: `WEEK(timestamp '2024-12-30 10:34:58', 1)`, + result: `INT64(53)`, + }, + { + expression: `WEEK(timestamp '2024-01-01 10:34:58', 0)`, + result: `INT64(0)`, + }, + { + expression: `WEEK(timestamp '2024-01-01 10:34:58', 1)`, + result: `INT64(1)`, + }, } tz, _ := time.LoadLocation("Europe/Madrid") diff --git a/go/vt/vtgate/evalengine/testcases/inputs.go b/go/vt/vtgate/evalengine/testcases/inputs.go index eb94235d9b4..a1c76a1d202 100644 --- a/go/vt/vtgate/evalengine/testcases/inputs.go +++ b/go/vt/vtgate/evalengine/testcases/inputs.go @@ -103,6 +103,7 @@ var inputConversions = []string{ "time '10:04:58'", "time '31:34:58'", "time '32:34:58'", "time '130:34:58'", "time '5 10:34:58'", "time '10:04:58.1'", "time '31:34:58.4'", "time '32:34:58.5'", "time '130:34:58.6'", "time '5 10:34:58.9'", "date '2000-01-01'", "timestamp '2000-01-01 10:34:58'", "timestamp '2000-01-01 10:34:58.123456'", "timestamp '2000-01-01 10:34:58.978654'", + "timestamp '2024-12-30 10:34:58'", "20000101103458", "20000101103458.1234", "20000101103458.123456", "20000101", "103458", "103458.123456", "'20000101103458'", "'20000101103458.1234'", "'20000101103458.123456'", "'20000101'", "'103458'", "'103458.123456'", "'20000101103458foo'", "'20000101103458.1234foo'", "'20000101103458.123456foo'", "'20000101foo'", "'103458foo'", "'103458.123456foo'",