Skip to content

Commit

Permalink
evalengine: Fix week overflow
Browse files Browse the repository at this point in the history
Some of the last days in the year can fall in the first week of the next
year. If that happens, there are various modes when we should return 53
as the week number and not week 1 for the next year.

This can always be 53. There are weeks which already contain 53 weeks,
but it can never be 54 in those cases as in years with 53 weeks, there's
never a last day of the year that falls in the first week of next year.
It then always falls in week 53 already.

Signed-off-by: Dirkjan Bussink <[email protected]>
  • Loading branch information
dbussink committed Dec 26, 2023
1 parent d807985 commit 5a9f5bb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions go/mysql/datetime/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,16 @@ func (d Date) Week(mode int) int {
year, week := d.SundayWeek()
if year < d.Year() {
return 0
} else if year > d.Year() {
return 53
}
return week
case 1:
year, week := d.ISOWeek()
if year < d.Year() {
return 0
} else if year > d.Year() {
return 53
}
return week
case 2:
Expand All @@ -333,12 +337,16 @@ func (d Date) Week(mode int) int {
year, week := d.Sunday4DayWeek()
if year < d.Year() {
return 0
} else if year > d.Year() {
return 53
}
return week
case 5:
year, week := d.MondayWeek()
if year < d.Year() {
return 0
} else if year > d.Year() {
return 53
}
return week
case 6:
Expand Down
16 changes: 16 additions & 0 deletions go/vt/vtgate/evalengine/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,22 @@ func TestCompilerSingle(t *testing.T) {
expression: `DAYOFMONTH(0)`,
result: `INT64(0)`,
},
{
expression: `week('2023-12-31', 4)`,
result: `INT64(53)`,
},
{
expression: `week('2023-12-31', 2)`,
result: `INT64(53)`,
},
{
expression: `week('2024-12-31', 1)`,
result: `INT64(53)`,
},
{
expression: `week('2024-12-31', 5)`,
result: `INT64(53)`,
},
}

tz, _ := time.LoadLocation("Europe/Madrid")
Expand Down

0 comments on commit 5a9f5bb

Please sign in to comment.