From 4ae5b0d3813d55f743f7e2928999c098c416b506 Mon Sep 17 00:00:00 2001 From: Dirkjan Bussink Date: Tue, 26 Dec 2023 18:25:51 +0100 Subject: [PATCH] evalengine: Fix week overflow (#14859) Signed-off-by: Manan Gupta --- go/mysql/datetime/datetime.go | 8 ++++++++ go/vt/vtgate/evalengine/compiler_test.go | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/go/mysql/datetime/datetime.go b/go/mysql/datetime/datetime.go index cc1fc92e091..73ae234e932 100644 --- a/go/mysql/datetime/datetime.go +++ b/go/mysql/datetime/datetime.go @@ -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: @@ -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: diff --git a/go/vt/vtgate/evalengine/compiler_test.go b/go/vt/vtgate/evalengine/compiler_test.go index 06dd679920e..468d4737828 100644 --- a/go/vt/vtgate/evalengine/compiler_test.go +++ b/go/vt/vtgate/evalengine/compiler_test.go @@ -487,6 +487,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 {