# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1251285744 -7200
#      Wed Aug 26 13:22:24 2009 +0200
# Node ID 34b63632386cde3db175037b3be5086255b7c076
# Parent  19ad9d65a95e36412adae8a5103b71fb259c76e0
new subquery_selection_index method on Union

diff --git a/stmts.py b/stmts.py
--- a/stmts.py
+++ b/stmts.py
@@ -303,6 +303,24 @@
         self._subq_cache[(col, etype)] = self._locate_subquery(col, etype, kwargs)
         return self._subq_cache[(col, etype)]
 
+    def subquery_selection_index(self, subselect, col):
+        """given a select sub-query and a column index in this sub-query, return
+        the selection index for this column in the root query
+        """
+        for select in self.children:
+            if select is subselect:
+                return col
+            term = select.selection[col]
+            try:
+                if term.name in select.aliases:
+                    alias = select.aliases[term.name]
+                    return alias.query.subquery_selection_index(subselect,
+                                                                alias.colnum)
+            except AttributeError:
+                # term has no 'name' attribute
+                pass
+        raise Exception('internal error, col %s not found in subqueries' % col)
+
     # recoverable modification methods ########################################
 
     # don't use @cached: we want to be able to disable it while this must still