From 04028a9a5140b542c39002b24700d20f4b254949 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) --- 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 bf73ac85c27..debc21cff6d 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 a0c29e1510f..0e9219c648a 100644 --- a/go/vt/vtgate/evalengine/compiler_test.go +++ b/go/vt/vtgate/evalengine/compiler_test.go @@ -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")