From 6eea9ca437084feae6a7b00276547e70ef6b40ad Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 7 Jul 2020 11:26:39 -0400 Subject: ensure we unwrap desc() /label() all the way w/ order by The deprecated logic to move order_by expressions up into the columns clause needed adjustment to accommodate for a more deeply-wrapped structure when desc() + label() are combined in an order by column. This structure now comes from coercions in 1.4. it's not clear to me at the moment why it's different from 1.3 but this shouldn't really matter. Fixes: #5443 Change-Id: If909a86f715992318d7aa283603197f7711f1d3b --- lib/sqlalchemy/sql/util.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy/sql') diff --git a/lib/sqlalchemy/sql/util.py b/lib/sqlalchemy/sql/util.py index e8726000b..b803ef912 100644 --- a/lib/sqlalchemy/sql/util.py +++ b/lib/sqlalchemy/sql/util.py @@ -27,6 +27,7 @@ from .elements import _textual_label_reference from .elements import BindParameter from .elements import ColumnClause from .elements import ColumnElement +from .elements import Label from .elements import Null from .elements import UnaryExpression from .schema import Column @@ -279,14 +280,31 @@ def unwrap_order_by(clause): cols = util.column_set() result = [] stack = deque([clause]) + + # examples + # column -> ASC/DESC == column + # column -> ASC/DESC -> label == column + # column -> label -> ASC/DESC -> label == column + # scalar_select -> label -> ASC/DESC == scalar_select -> label + while stack: t = stack.popleft() if isinstance(t, ColumnElement) and ( not isinstance(t, UnaryExpression) or not operators.is_ordering_modifier(t.modifier) ): - if isinstance(t, _label_reference): + if isinstance(t, Label) and not isinstance( + t.element, ScalarSelect + ): + t = t.element + + stack.append(t) + continue + elif isinstance(t, _label_reference): t = t.element + + stack.append(t) + continue if isinstance(t, (_textual_label_reference)): continue if t not in cols: -- cgit v1.2.1