summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sqlalchemy/sql')
-rw-r--r--lib/sqlalchemy/sql/compiler.py14
-rw-r--r--lib/sqlalchemy/sql/expression.py13
2 files changed, 20 insertions, 7 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
index 5fbfa34f3..5dd7ec564 100644
--- a/lib/sqlalchemy/sql/compiler.py
+++ b/lib/sqlalchemy/sql/compiler.py
@@ -536,14 +536,14 @@ class SQLCompiler(engine.Compiled):
# note that its OK we aren't expanding tables and other selectables
# here; we can only add a label in the ORDER BY for an individual
# label expression in the columns clause.
- raw_col = set(order_by_select._raw_columns)
+
+ raw_col = set(l._order_by_label_element.name
+ for l in order_by_select._raw_columns
+ if l._order_by_label_element is not None)
def label_ok(c):
- if c in raw_col:
- return c
- elif getattr(c, 'modifier', None) in \
- (operators.desc_op, operators.asc_op) and \
- c.element.proxy_set.intersection(raw_col):
- return c.element
+ if c._order_by_label_element is not None and \
+ c._order_by_label_element.name in raw_col:
+ return c._order_by_label_element
else:
return None
diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py
index 3f9aef2b7..402e52272 100644
--- a/lib/sqlalchemy/sql/expression.py
+++ b/lib/sqlalchemy/sql/expression.py
@@ -1659,6 +1659,8 @@ class ClauseElement(Visitable):
is_selectable = False
is_clause_element = True
+ _order_by_label_element = None
+
def _clone(self):
"""Create a shallow copy of this ClauseElement.
@@ -3690,6 +3692,13 @@ class UnaryExpression(ColumnElement):
self.type = sqltypes.to_instance(type_)
self.negate = negate
+ @util.memoized_property
+ def _order_by_label_element(self):
+ if self.modifier in (operators.desc_op, operators.asc_op):
+ return self.element._order_by_label_element
+ else:
+ return None
+
@property
def _from_objects(self):
return self.element._from_objects
@@ -4327,6 +4336,10 @@ class Label(ColumnElement):
self._proxies = [element]
@util.memoized_property
+ def _order_by_label_element(self):
+ return self
+
+ @util.memoized_property
def type(self):
return sqltypes.to_instance(
self._type or getattr(self._element, 'type', None)