From 658c7b42ee2920bb2f744eeb58cb8327ec7ba3fa Mon Sep 17 00:00:00 2001 From: Itai Shirav Date: Sat, 13 Oct 2018 19:49:12 +0300 Subject: [PATCH] Bug fix: wrong parentheses when building queries using Q objects --- CHANGELOG.md | 1 + src/infi/clickhouse_orm/query.py | 4 ++-- tests/test_querysets.py | 4 ++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e856cf..8b3ea0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Change Log Unreleased ---------- - Bug fix: `QuerySet.count()` ignores slicing +- Bug fix: wrong parentheses when building queries using Q objects v1.0.2 ---------- diff --git a/src/infi/clickhouse_orm/query.py b/src/infi/clickhouse_orm/query.py index 332f39a..1035dd4 100644 --- a/src/infi/clickhouse_orm/query.py +++ b/src/infi/clickhouse_orm/query.py @@ -188,7 +188,7 @@ class Q(object): q = Q() q._l_child = l_child q._r_child = r_child - q._mode = mode + q._mode = mode # AND/OR return q def _build_fov(self, key, value): @@ -203,7 +203,7 @@ class Q(object): sql = ' {} '.format(self._mode).join(fov.to_sql(model_cls) for fov in self._fovs) else: if self._l_child and self._r_child: - sql = '({}) {} ({})'.format( + sql = '({} {} {})'.format( self._l_child.to_sql(model_cls), self._mode, self._r_child.to_sql(model_cls)) else: return '1' diff --git a/tests/test_querysets.py b/tests/test_querysets.py index a6ea1ad..a4fef14 100644 --- a/tests/test_querysets.py +++ b/tests/test_querysets.py @@ -75,6 +75,10 @@ class QuerySetTestCase(TestCaseWithData): self._test_qs(qs.filter((Q(first_name__in=['Warren', 'Whilemina', 'Whitney']) & Q(height__gte=1.7) | (Q(first_name__in=['Victoria', 'Victor', 'Venus']) & Q(height__lt=1.7)))), 4) self._test_qs(qs.filter(Q(first_name='Elton') & ~Q(last_name='Smith')), 1) + # Check operator precendence + self._test_qs(qs.filter(first_name='Cassady').filter(Q(last_name='Knapp') | Q(last_name='Rogers') | Q(last_name='Gregory')), 2) + self._test_qs(qs.filter(Q(first_name='Cassady') & Q(last_name='Knapp') | Q(first_name='Beatrice') & Q(last_name='Gregory')), 2) + self._test_qs(qs.filter(Q(first_name='Courtney') | Q(first_name='Cassady') & Q(last_name='Knapp')), 3) def test_filter_unicode_string(self): self.database.insert([