From bc116a9d7a2b009f321d8f0560f833a3d4ae73e9 Mon Sep 17 00:00:00 2001 From: Amin Date: Tue, 10 Sep 2024 16:51:19 +0330 Subject: [PATCH 1/4] feat(aggragation-function): add anyLast function --- clickhouse_backend/models/aggregates.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/clickhouse_backend/models/aggregates.py b/clickhouse_backend/models/aggregates.py index 03f2654..522e487 100644 --- a/clickhouse_backend/models/aggregates.py +++ b/clickhouse_backend/models/aggregates.py @@ -62,3 +62,7 @@ class uniqHLL12(uniq): class uniqTheta(uniq): pass + + +class anyLast(Aggregate): + pass From d047e02488bffa3edfaac943030a0e36cf7da65b Mon Sep 17 00:00:00 2001 From: Amin Date: Tue, 10 Sep 2024 17:12:13 +0330 Subject: [PATCH 2/4] feat(aggragation-function): add anyLast function - include in __all__ --- clickhouse_backend/models/aggregates.py | 1 + 1 file changed, 1 insertion(+) diff --git a/clickhouse_backend/models/aggregates.py b/clickhouse_backend/models/aggregates.py index 522e487..b89d1d0 100644 --- a/clickhouse_backend/models/aggregates.py +++ b/clickhouse_backend/models/aggregates.py @@ -10,6 +10,7 @@ "uniqCombined64", "uniqHLL12", "uniqTheta", + "anyLast", ] From a91a9d87997c9e183455613d00fbb006424fb58d Mon Sep 17 00:00:00 2001 From: Amin Date: Tue, 17 Sep 2024 14:57:55 +0330 Subject: [PATCH 3/4] feat(aggragation-function): add anyLast function - add unittest --- tests/aggregates/tests.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/aggregates/tests.py b/tests/aggregates/tests.py index a9a807f..c849226 100644 --- a/tests/aggregates/tests.py +++ b/tests/aggregates/tests.py @@ -8,6 +8,7 @@ uniqExact, uniqHLL12, uniqTheta, + anyLast ) from .models import WatchSeries @@ -28,6 +29,14 @@ class AggregatesTestCase(TestCase): {"show": "Game of Thrones", "episode": "S1E2", "uid_count": 1}, ] + expected_result_any_last = [ + {'uid': 'alice', 'user_last_watched_show': 'Game of Thrones'}, + {'uid': 'bob', 'user_last_watched_show': 'Bridgerton'}, + {'uid': 'carol', 'user_last_watched_show': 'Bridgerton'}, + {'uid': 'dan', 'user_last_watched_show': 'Bridgerton'}, + {'uid': 'erin', 'user_last_watched_show': 'Game of Thrones'}, + ] + @classmethod def setUpTestData(cls): data_list = [ @@ -227,3 +236,15 @@ def test_uniqhll12(self): def test_uniqtheta(self): self._test(uniqTheta) + + def test_anylast(self): + result = ( + WatchSeries.objects.values("uid") + .annotate(user_last_watched_show=anyLast("show")) + .order_by("uid") + ) + + self.assertQuerysetEqual( + result, self.expected_result_any_last, transform=dict + ) + From 57910fca18836bc6963801f487f6321991a77189 Mon Sep 17 00:00:00 2001 From: Amin Date: Wed, 25 Sep 2024 09:28:20 +0330 Subject: [PATCH 4/4] feat(aggragation-function): add anyLast function - lint --- tests/aggregates/tests.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/tests/aggregates/tests.py b/tests/aggregates/tests.py index c849226..f92be0f 100644 --- a/tests/aggregates/tests.py +++ b/tests/aggregates/tests.py @@ -2,13 +2,13 @@ from django.test import TestCase from clickhouse_backend.models import ( + anyLast, uniq, uniqCombined, uniqCombined64, uniqExact, uniqHLL12, uniqTheta, - anyLast ) from .models import WatchSeries @@ -30,11 +30,11 @@ class AggregatesTestCase(TestCase): ] expected_result_any_last = [ - {'uid': 'alice', 'user_last_watched_show': 'Game of Thrones'}, - {'uid': 'bob', 'user_last_watched_show': 'Bridgerton'}, - {'uid': 'carol', 'user_last_watched_show': 'Bridgerton'}, - {'uid': 'dan', 'user_last_watched_show': 'Bridgerton'}, - {'uid': 'erin', 'user_last_watched_show': 'Game of Thrones'}, + {"uid": "alice", "user_last_watched_show": "Game of Thrones"}, + {"uid": "bob", "user_last_watched_show": "Bridgerton"}, + {"uid": "carol", "user_last_watched_show": "Bridgerton"}, + {"uid": "dan", "user_last_watched_show": "Bridgerton"}, + {"uid": "erin", "user_last_watched_show": "Game of Thrones"}, ] @classmethod @@ -244,7 +244,4 @@ def test_anylast(self): .order_by("uid") ) - self.assertQuerysetEqual( - result, self.expected_result_any_last, transform=dict - ) - + self.assertQuerysetEqual(result, self.expected_result_any_last, transform=dict)