diff --git a/clickhouse_backend/models/aggregates.py b/clickhouse_backend/models/aggregates.py index 03f2654..b89d1d0 100644 --- a/clickhouse_backend/models/aggregates.py +++ b/clickhouse_backend/models/aggregates.py @@ -10,6 +10,7 @@ "uniqCombined64", "uniqHLL12", "uniqTheta", + "anyLast", ] @@ -62,3 +63,7 @@ class uniqHLL12(uniq): class uniqTheta(uniq): pass + + +class anyLast(Aggregate): + pass diff --git a/tests/aggregates/tests.py b/tests/aggregates/tests.py index a9a807f..f92be0f 100644 --- a/tests/aggregates/tests.py +++ b/tests/aggregates/tests.py @@ -2,6 +2,7 @@ from django.test import TestCase from clickhouse_backend.models import ( + anyLast, uniq, uniqCombined, uniqCombined64, @@ -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,12 @@ 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)