Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clickhouse_backend/models/aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"uniqCombined64",
"uniqHLL12",
"uniqTheta",
"anyLast",
]


Expand Down Expand Up @@ -62,3 +63,7 @@ class uniqHLL12(uniq):

class uniqTheta(uniq):
pass


class anyLast(Aggregate):
pass
18 changes: 18 additions & 0 deletions tests/aggregates/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.test import TestCase

from clickhouse_backend.models import (
anyLast,
uniq,
uniqCombined,
uniqCombined64,
Expand All @@ -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 = [
Expand Down Expand Up @@ -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)