From fcc8377cbed6165a34529464c9fbb373a6afa6d2 Mon Sep 17 00:00:00 2001 From: "cmccaffrey@cloudsmith.io" Date: Mon, 28 Jul 2025 10:27:13 +0100 Subject: [PATCH 1/3] Added toYearWeek functionality --- .../models/functions/datetime.py | 19 +++++++++++++++ tests/clickhouse_functions/test_datetime.py | 23 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/clickhouse_backend/models/functions/datetime.py b/clickhouse_backend/models/functions/datetime.py index 9dd204a..67c7c89 100644 --- a/clickhouse_backend/models/functions/datetime.py +++ b/clickhouse_backend/models/functions/datetime.py @@ -14,6 +14,7 @@ "toYYYYMM", "toYYYYMMDD", "toYYYYMMDDhhmmss", + "toYearWeek", ] @@ -77,3 +78,21 @@ class toStartOfFifteenMinutes(toStartOfMinute): class toStartOfHour(toStartOfMinute): pass + +class toYearWeek(Func): + output_field = fields.UInt32Field() + + def __init__(self, *expressions): + arity = len(expressions) + if arity < 1 or arity > 3: + raise TypeError( + "'%s' takes between 1 and 3 arguments (%s given)" + % ( + self.__class__.__name__, + len(expressions), + ) + ) + + expressions = (expressions[0], *(models.Value(expr) for expr in expressions[1:])) + + super().__init__(*expressions) \ No newline at end of file diff --git a/tests/clickhouse_functions/test_datetime.py b/tests/clickhouse_functions/test_datetime.py index 36e6cda..3924734 100644 --- a/tests/clickhouse_functions/test_datetime.py +++ b/tests/clickhouse_functions/test_datetime.py @@ -27,6 +27,13 @@ def setUpTestData(cls): datetime(2023, 11, 30, hour=16, minute=59, second=59), is_dst=False ), ) + cls.sarah = Author.objects.create( + name="Sarah Connor", + alias="sconnor", + birthday=pytz.utc.localize( + datetime(2023, 12, 31, hour=23, minute=30, second=00), is_dst=False + ), + ) def test_yyyymm(self): john = Author.objects.annotate(v=models.toYYYYMM("birthday")).get( @@ -177,3 +184,19 @@ def test_tostartofhour(self): elena.v, datetime(2023, 11, 30, hour=10, minute=00, second=00), ) + + def test_toyearweek(self): + sarah = Author.objects.annotate(v=models.toYearWeek("birthday")).get( + id=self.sarah.id + ) + self.assertEqual(sarah.v, 202353) + + sarah = Author.objects.annotate(v=models.toYearWeek("birthday", 1)).get( + id=self.sarah.id + ) + self.assertEqual(sarah.v, 202352) + + sarah = Author.objects.annotate(v=models.toYearWeek("birthday", 1, "Pacific/Kiritimati")).get( + id=self.sarah.id + ) + self.assertEqual(sarah.v, 202401) From 138d0dbbb78a43fc1d44b5bd63b2b032793c48ac Mon Sep 17 00:00:00 2001 From: "cmccaffrey@cloudsmith.io" Date: Mon, 28 Jul 2025 10:38:16 +0100 Subject: [PATCH 2/3] Updating changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1569e7d..c103702 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 1.5.0 + +- feat: #129: Add `toYearWeek` datetime functionality + ### 1.4.0 - feat: #119 Allow query results returned in columns and deserialized to `numpy` objects From de54d8d41a1fa9259a1fb0746f84550aa8881ec2 Mon Sep 17 00:00:00 2001 From: "cmccaffrey@cloudsmith.io" Date: Mon, 28 Jul 2025 14:42:03 +0100 Subject: [PATCH 3/3] Formatting and linting --- clickhouse_backend/models/functions/datetime.py | 8 ++++++-- tests/clickhouse_functions/test_datetime.py | 8 ++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/clickhouse_backend/models/functions/datetime.py b/clickhouse_backend/models/functions/datetime.py index 67c7c89..637eca9 100644 --- a/clickhouse_backend/models/functions/datetime.py +++ b/clickhouse_backend/models/functions/datetime.py @@ -79,6 +79,7 @@ class toStartOfFifteenMinutes(toStartOfMinute): class toStartOfHour(toStartOfMinute): pass + class toYearWeek(Func): output_field = fields.UInt32Field() @@ -93,6 +94,9 @@ def __init__(self, *expressions): ) ) - expressions = (expressions[0], *(models.Value(expr) for expr in expressions[1:])) + expressions = ( + expressions[0], + *(models.Value(expr) for expr in expressions[1:]), + ) - super().__init__(*expressions) \ No newline at end of file + super().__init__(*expressions) diff --git a/tests/clickhouse_functions/test_datetime.py b/tests/clickhouse_functions/test_datetime.py index 3924734..047028b 100644 --- a/tests/clickhouse_functions/test_datetime.py +++ b/tests/clickhouse_functions/test_datetime.py @@ -184,7 +184,7 @@ def test_tostartofhour(self): elena.v, datetime(2023, 11, 30, hour=10, minute=00, second=00), ) - + def test_toyearweek(self): sarah = Author.objects.annotate(v=models.toYearWeek("birthday")).get( id=self.sarah.id @@ -196,7 +196,7 @@ def test_toyearweek(self): ) self.assertEqual(sarah.v, 202352) - sarah = Author.objects.annotate(v=models.toYearWeek("birthday", 1, "Pacific/Kiritimati")).get( - id=self.sarah.id - ) + sarah = Author.objects.annotate( + v=models.toYearWeek("birthday", 1, "Pacific/Kiritimati") + ).get(id=self.sarah.id) self.assertEqual(sarah.v, 202401)