Skip to content

Commit c08e5c6

Browse files
committed
feat: support prewhere, finish test for django<4.2
1 parent cc48e6c commit c08e5c6

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

clickhouse_backend/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from clickhouse_backend.utils.version import get_version
22

3-
VERSION = (1, 1, 7, "final", 0)
3+
VERSION = (1, 2, 0, "final", 0)
44

55
__version__ = get_version(VERSION)

clickhouse_backend/models/sql/compiler.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,7 @@ def pre_sql_setup(self, with_col_aliases=False):
8585
must_group_by=self.query.group_by is not None
8686
)
8787
else:
88-
(
89-
self.prewhere,
90-
prehaving,
91-
prequalify,
92-
) = (
93-
None,
94-
None,
95-
None,
96-
)
88+
self.prewhere, prehaving, prequalify = None, None, None
9789
# Check before ClickHouse complain.
9890
# DB::Exception: Window function is found in PREWHERE in query. (ILLEGAL_AGGREGATION)
9991
if prequalify:
@@ -103,7 +95,10 @@ def pre_sql_setup(self, with_col_aliases=False):
10395
else:
10496
self.setup_query()
10597
self.where, self.having = self.query.where.split_having()
106-
self.prewhere, prehaving = self.query.where.split_having()
98+
if isinstance(self.query, Query):
99+
self.prewhere, prehaving = self.query.prewhere.split_having()
100+
else:
101+
self.prewhere, prehaving = None, None
107102
# Check before ClickHouse complain.
108103
# DB::Exception: Aggregate function is found in PREWHERE in query. (ILLEGAL_AGGREGATION)
109104
if prehaving:

tests/clickhouse_queries/tests.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from django.db.models.functions import Rank
44
from django.test import TestCase
55

6+
from clickhouse_backend import compat
7+
68
from . import models
79

810

@@ -33,12 +35,12 @@ def test_prewhere(self):
3335
self.assertEqual(qs[0].name, "a1")
3436

3537
def test_prewhere_fk(self):
36-
self.assertQuerySetEqual(
38+
b1, b2 = (
3739
models.Book.objects.filter(author__name=self.a1.name)
3840
.prewhere(author_id=self.a1.id)
39-
.order_by("name"),
40-
[self.b1, self.b2],
41+
.order_by("id")
4142
)
43+
self.assertTrue(b1.id == self.b1.id and b2.id == self.b2.id)
4244

4345
# clickhouse backend will generate suitable query, but clickhouse will raise exception.
4446
# clickhouse 23.11
@@ -64,12 +66,15 @@ def test_prewhere_agg(self):
6466
)
6567
)
6668

67-
def test_prewhere_window(self):
68-
with self.assertRaisesMessage(
69-
NotSupportedError, "Window function is disallowed in the prewhere clause."
70-
):
71-
list(
72-
models.Book.objects.annotate(
73-
rank=Window(Rank(), partition_by="author", order_by="name")
74-
).prewhere(rank__gt=1)
75-
)
69+
if compat.dj_ge42:
70+
71+
def test_prewhere_window(self):
72+
with self.assertRaisesMessage(
73+
NotSupportedError,
74+
"Window function is disallowed in the prewhere clause.",
75+
):
76+
list(
77+
models.Book.objects.annotate(
78+
rank=Window(Rank(), partition_by="author", order_by="name")
79+
).prewhere(rank__gt=1)
80+
)

0 commit comments

Comments
 (0)