summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/compiler.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2017-01-06 17:02:32 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2017-01-06 17:56:41 -0500
commit6b489db89970b1fcec38a7c3772960ed3291a2ed (patch)
tree8caab4e8618f1864ab5029fbdab352aad9fbab80 /lib/sqlalchemy/sql/compiler.py
parent2b4d028a69270c1c7918281a60280dd0b65963a2 (diff)
downloadsqlalchemy-6b489db89970b1fcec38a7c3772960ed3291a2ed.tar.gz
Tighten rules for order_by(Label) resolution
- Fixed bug originally introduced in 0.9 via :ticket:`1068` where order_by(<some Label()>) would order by the label name based on name alone, that is, even if the labeled expression were not at all the same expression otherwise present, implicitly or explicitly, in the selectable. The logic that orders by label now ensures that the labeled expression is related to the one that resolves to that name before ordering by the label name; additionally, the name has to resolve to an actual label explicit in the expression elsewhere, not just a column name. This logic is carefully kept separate from the order by(textual name) feature that has a slightly different purpose. Change-Id: I44fc36dab34380cc238c1e79ecbe23f1628d588a Fixes: #3882
Diffstat (limited to 'lib/sqlalchemy/sql/compiler.py')
-rw-r--r--lib/sqlalchemy/sql/compiler.py12
1 files changed, 6 insertions, 6 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
index b8c897cb9..f6f9fa45d 100644
--- a/lib/sqlalchemy/sql/compiler.py
+++ b/lib/sqlalchemy/sql/compiler.py
@@ -580,11 +580,11 @@ class SQLCompiler(Compiled):
if self.stack and self.dialect.supports_simple_order_by_label:
selectable = self.stack[-1]['selectable']
- with_cols, only_froms = selectable._label_resolve_dict
+ with_cols, only_froms, only_cols = selectable._label_resolve_dict
if within_columns_clause:
resolve_dict = only_froms
else:
- resolve_dict = with_cols
+ resolve_dict = only_cols
# this can be None in the case that a _label_reference()
# were subject to a replacement operation, in which case
@@ -593,11 +593,11 @@ class SQLCompiler(Compiled):
order_by_elem = element.element._order_by_label_element
if order_by_elem is not None and order_by_elem.name in \
- resolve_dict:
-
+ resolve_dict and \
+ order_by_elem.shares_lineage(
+ resolve_dict[order_by_elem.name]):
kwargs['render_label_as_label'] = \
element.element._order_by_label_element
-
return self.process(
element.element, within_columns_clause=within_columns_clause,
**kwargs)
@@ -611,7 +611,7 @@ class SQLCompiler(Compiled):
)
selectable = self.stack[-1]['selectable']
- with_cols, only_froms = selectable._label_resolve_dict
+ with_cols, only_froms, only_cols = selectable._label_resolve_dict
try:
if within_columns_clause:
col = only_froms[element.element]