Skip to content

Commit df2a504

Browse files
committed
Check the coherence of the aliases of GROUP BY and ORDER BY expressions
1 parent 92ee8c9 commit df2a504

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* Check the coherence of the aliases of GROUP BY and ORDER BY expressions
12
* Do not use parameter for EXTRACT field
23
* Remove support for Python older than 3.6
34

sql/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,20 @@ def __str__(self):
629629
and (self.limit is not None or self.offset is not None)):
630630
return self._rownum(str)
631631

632+
for expression in chain(
633+
self.group_by or [],
634+
self.order_by or []):
635+
if not isinstance(expression, As):
636+
continue
637+
for column in self.columns:
638+
if not isinstance(column, As):
639+
continue
640+
if column.output_name != expression.output_name:
641+
continue
642+
if (str(column.expression) != str(expression.expression)
643+
or column.params != expression.params):
644+
raise ValueError("%r != %r" % (expression, column))
645+
632646
with AliasManager():
633647
if self.from_ is not None:
634648
from_ = ' FROM %s' % self.from_

sql/tests/test_select.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,12 @@ def test_select_invalid_group_by(self):
253253
with self.assertRaises(ValueError):
254254
self.table.select(group_by=['foo'])
255255

256+
def test_select_invalid_group_by_alias(self):
257+
query = self.table.select(
258+
self.table.c1.as_('c'), group_by=self.table.c2.as_('c'))
259+
with self.assertRaises(ValueError):
260+
str(query)
261+
256262
def test_select_having(self):
257263
col1 = self.table.col1
258264
col2 = self.table.col2
@@ -268,16 +274,28 @@ def test_select_invalid_having(self):
268274
self.table.select(having='foo')
269275

270276
def test_select_order(self):
271-
c = self.table.c
272-
query = self.table.select(c, order_by=Literal(1))
277+
column = self.table.c
278+
query = self.table.select(column, order_by=column)
273279
self.assertEqual(str(query),
274-
'SELECT "a"."c" FROM "t" AS "a" ORDER BY %s')
275-
self.assertEqual(tuple(query.params), (1,))
280+
'SELECT "a"."c" FROM "t" AS "a" ORDER BY "a"."c"')
281+
self.assertEqual(tuple(query.params), ())
282+
283+
output = column.as_('c1')
284+
query = self.table.select(output, order_by=output)
285+
self.assertEqual(str(query),
286+
'SELECT "a"."c" AS "c1" FROM "t" AS "a" ORDER BY "c1"')
287+
self.assertEqual(tuple(query.params), ())
276288

277289
def test_select_invalid_order(self):
278290
with self.assertRaises(ValueError):
279291
self.table.select(order_by='foo')
280292

293+
def test_select_invalid_order_alias(self):
294+
query = self.table.select(
295+
self.table.c1.as_('c'), order_by=self.table.c2.as_('c'))
296+
with self.assertRaises(ValueError):
297+
str(query)
298+
281299
def test_select_limit_offset(self):
282300
try:
283301
Flavor.set(Flavor(limitstyle='limit'))

0 commit comments

Comments
 (0)