from .elements import BindParameter
from .elements import ColumnClause
from .elements import ColumnElement
+from .elements import Grouping
from .elements import Label
from .elements import Null
from .elements import UnaryExpression
):
t = t.element
+ if isinstance(t, Grouping):
+ t = t.element
+
stack.append(t)
continue
elif isinstance(t, _label_reference):
if t not in cols:
cols.add(t)
result.append(t)
+
else:
for c in t.get_children():
stack.append(c)
]
)
- return [
- col
- for col in chain(*[unwrap_order_by(o) for o in order_by])
- if col not in cols_already_present
- ]
+ to_look_for = list(chain(*[unwrap_order_by(o) for o in order_by]))
+
+ return [col for col in to_look_for if col not in cols_already_present]
def clause_is_present(clause, search):
import sqlalchemy as sa
from sqlalchemy import and_
+from sqlalchemy import asc
from sqlalchemy import between
from sqlalchemy import bindparam
from sqlalchemy import Boolean
.all(),
)
+ def test_no_automatic_distinct_thing_w_future(self):
+ User = self.classes.User
+
+ stmt = select(User.id).order_by(User.name).distinct()
+
+ self.assert_compile(
+ stmt, "SELECT DISTINCT users.id FROM users ORDER BY users.name"
+ )
+
+ def test_issue_5470_one(self):
+ User = self.classes.User
+
+ expr = (User.id.op("+")(2)).label("label")
+
+ sess = create_session()
+
+ q = sess.query(expr).select_from(User).order_by(desc(expr)).distinct()
+
+ # no double col in the select list,
+ # orders by the label
+ self.assert_compile(
+ q,
+ "SELECT DISTINCT users.id + :id_1 AS label "
+ "FROM users ORDER BY label DESC",
+ )
+
+ def test_issue_5470_two(self):
+ User = self.classes.User
+
+ expr = User.id + literal(1)
+
+ sess = create_session()
+ q = sess.query(expr).select_from(User).order_by(asc(expr)).distinct()
+
+ # no double col in the select list,
+ # there's no label so this is the requested SQL
+ self.assert_compile(
+ q,
+ "SELECT DISTINCT users.id + :param_1 AS anon_1 "
+ "FROM users ORDER BY users.id + :param_1 ASC",
+ )
+
+ def test_issue_5470_three(self):
+ User = self.classes.User
+
+ expr = (User.id + literal(1)).label("label")
+
+ sess = create_session()
+ q = sess.query(expr).select_from(User).order_by(asc(expr)).distinct()
+
+ # no double col in the select list,
+ # orders by the label
+ self.assert_compile(
+ q,
+ "SELECT DISTINCT users.id + :param_1 AS label "
+ "FROM users ORDER BY label ASC",
+ )
+
+ def test_issue_5470_four(self):
+ User = self.classes.User
+
+ expr = (User.id + literal(1)).label("label")
+
+ sess = create_session()
+ q = (
+ sess.query(expr)
+ .select_from(User)
+ .order_by(asc("label"))
+ .distinct()
+ )
+
+ # no double col in the select list,
+ # orders by the label
+ self.assert_compile(
+ q,
+ "SELECT DISTINCT users.id + :param_1 AS label "
+ "FROM users ORDER BY label ASC",
+ )
+
+ def test_issue_5470_five(self):
+ User = self.classes.User
+
+ expr = (User.id.op("+")(2)).label("label")
+
+ stmt = select(expr).select_from(User).order_by(desc(expr)).distinct()
+
+ # no double col in the select list,
+ # orders by the label
+ self.assert_compile(
+ stmt,
+ "SELECT DISTINCT users.id + :id_1 AS label "
+ "FROM users ORDER BY label DESC",
+ )
+
def test_columns_augmented_roundtrip_one_from_self(self):
"""Test workaround for legacy style DISTINCT on extra column.