From 7131063e4706b6bd51563e9c385aad47a1af6cfd Mon Sep 17 00:00:00 2001 From: "vitess-bot[bot]" <108069721+vitess-bot[bot]@users.noreply.github.com> Date: Wed, 27 Dec 2023 15:22:19 +0530 Subject: [PATCH] [release-17.0] evalengine: Fix week overflow (#14859) (#14860) Signed-off-by: Manan Gupta Co-authored-by: Dirkjan Bussink Co-authored-by: Manan Gupta --- go/mysql/datetime/types.go | 8 ++++++++ go/vt/vtgate/evalengine/compiler_test.go | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/go/mysql/datetime/types.go b/go/mysql/datetime/types.go index a57ab51eaeb..8d7c2524722 100644 --- a/go/mysql/datetime/types.go +++ b/go/mysql/datetime/types.go @@ -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: @@ -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: diff --git a/go/vt/vtgate/evalengine/compiler_test.go b/go/vt/vtgate/evalengine/compiler_test.go index 4c21de2dde6..b38b5f14557 100644 --- a/go/vt/vtgate/evalengine/compiler_test.go +++ b/go/vt/vtgate/evalengine/compiler_test.go @@ -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 {