summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2020-07-07 11:26:39 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2020-07-07 12:39:13 -0400
commit6eea9ca437084feae6a7b00276547e70ef6b40ad (patch)
treee8b056017bcf71097b79cd28c380008403cb6054 /lib/sqlalchemy/sql
parentf97691e2a7a75657b6ef0d02d814f22f219fd780 (diff)
downloadsqlalchemy-6eea9ca437084feae6a7b00276547e70ef6b40ad.tar.gz
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
Diffstat (limited to 'lib/sqlalchemy/sql')
-rw-r--r--lib/sqlalchemy/sql/util.py20
1 files changed, 19 insertions, 1 deletions
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: