From 16d66d35644eaa6609fdc5c8f805314f7cf4d0fc Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 22 Aug 2012 18:55:59 -0400 Subject: - [bug] Fixed bug whereby usage of a UNION or similar inside of an embedded subquery would interfere with result-column targeting, in the case that a result-column had the same ultimate name as a name inside the embedded UNION. [ticket:2552] --- lib/sqlalchemy/sql/compiler.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'lib/sqlalchemy/sql/compiler.py') diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index ee96c8c81..81aa62c25 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -574,9 +574,10 @@ class SQLCompiler(engine.Compiled): return func.clause_expr._compiler_dispatch(self, **kwargs) def visit_compound_select(self, cs, asfrom=False, - parens=True, compound_index=1, **kwargs): + parens=True, compound_index=0, **kwargs): entry = self.stack and self.stack[-1] or {} - self.stack.append({'from':entry.get('from', None), 'iswrapper':True}) + self.stack.append({'from': entry.get('from', None), + 'iswrapper': not entry}) keyword = self.compound_keywords.get(cs.keyword) @@ -597,7 +598,7 @@ class SQLCompiler(engine.Compiled): self.limit_clause(cs) or "" if self.ctes and \ - compound_index == 1 and not entry: + compound_index == 0 and not entry: text = self._render_cte_clause() + text self.stack.pop(-1) @@ -1017,7 +1018,7 @@ class SQLCompiler(engine.Compiled): def visit_select(self, select, asfrom=False, parens=True, iswrapper=False, fromhints=None, - compound_index=1, + compound_index=0, positional_names=None, **kwargs): entry = self.stack and self.stack[-1] or {} @@ -1033,11 +1034,14 @@ class SQLCompiler(engine.Compiled): # to outermost if existingfroms: correlate_froms = # correlate_froms.union(existingfroms) + populate_result_map = compound_index == 0 and ( + not entry or \ + entry.get('iswrapper', False) + ) + self.stack.append({'from': correlate_froms, 'iswrapper': iswrapper}) - populate_result_map = compound_index == 1 and not entry or \ - entry.get('iswrapper', False) column_clause_args = {'positional_names': positional_names} # the actual list of columns to print in the SELECT column list. @@ -1112,7 +1116,7 @@ class SQLCompiler(engine.Compiled): text += self.for_update_clause(select) if self.ctes and \ - compound_index == 1 and not entry: + compound_index == 0 and not entry: text = self._render_cte_clause() + text self.stack.pop(-1) -- cgit v1.2.1