Skip to content

Commit

Permalink
evalengine: Fix week overflow (#14859)
Browse files Browse the repository at this point in the history
Signed-off-by: Manan Gupta <[email protected]>
  • Loading branch information
dbussink authored and GuptaManan100 committed Dec 27, 2023
1 parent e630548 commit a791d63
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/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,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 @@ -332,12 +336,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 @@ -476,6 +476,22 @@ func TestCompilerSingle(t *testing.T) {
expression: `case when null is null then 23 else null end`,
result: `INT64(23)`,
},
{
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)`,
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit a791d63

Please sign in to comment.