Skip to content

Commit

Permalink
Fix week number for date_format evalengine function (#17432)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuptaManan100 authored Dec 26, 2024
1 parent 30e1e40 commit 9383943
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
14 changes: 4 additions & 10 deletions go/mysql/datetime/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down
20 changes: 20 additions & 0 deletions go/vt/vtgate/evalengine/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions go/vt/vtgate/evalengine/testcases/inputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,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'",
Expand Down

0 comments on commit 9383943

Please sign in to comment.