Commits

Mike Bayer  committed 4dabd89

- [bug] Repaired common table expression
rendering to function correctly when the
SELECT statement contains UNION or other
compound expressions, courtesy btbuilder.
[ticket:2490]

  • Participants
  • Parent commits 299aa58

Comments (0)

Files changed (3)

     also in 0.7.7, [ticket:2499] also 
     in 0.7.8.
 
+  - [bug] Repaired common table expression
+    rendering to function correctly when the 
+    SELECT statement contains UNION or other
+    compound expressions, courtesy btbuilder.  
+    [ticket:2490]  Also in 0.7.8.
+
+
 - sqlite
   - [feature] the SQLite date and time types
     have been overhauled to support a more open

File lib/sqlalchemy/sql/compiler.py

         text += (cs._limit is not None or cs._offset is not None) and \
                         self.limit_clause(cs) or ""
 
+        if self.ctes and \
+            compound_index==1 and not entry:
+            text = self._render_cte_clause() + text
+
         self.stack.pop(-1)
         if asfrom and parens:
             return "(" + text + ")"
 
         if self.ctes and \
             compound_index==1 and not entry:
-            cte_text = self.get_cte_preamble(self.ctes_recursive) + " "
-            cte_text += ", \n".join(
-                [txt for txt in self.ctes.values()]
-            )
-            cte_text += "\n "
-            text = cte_text + text
+            text  = self._render_cte_clause() + text
 
         self.stack.pop(-1)
 
         else:
             return text
 
+    def _render_cte_clause(self):
+        cte_text = self.get_cte_preamble(self.ctes_recursive) + " "
+        cte_text += ", \n".join(
+            [txt for txt in self.ctes.values()]
+        )
+        cte_text += "\n "
+        return cte_text
+
     def get_cte_preamble(self, recursive):
         if recursive:
             return "WITH RECURSIVE"

File test/sql/test_compiler.py

         eq_(s.positiontup, ['a', 'b', 'c'])
 
     def test_nested_label_targeting(self):
-        """test nested anonymous label generation.  
+        """test nested anonymous label generation.
 
         """
         s1 = table1.select()
             "SELECT mytable.myid, mytable.name, mytable.description "
             "FROM mytable WHERE mytable.myid = %(myid_1)s FOR SHARE",
             dialect=postgresql.dialect())
-        
+
         self.assert_compile(
             table1.select(table1.c.myid==7, for_update="read_nowait"),
             "SELECT mytable.myid, mytable.name, mytable.description "
                 dialect=mssql.dialect()
             )
 
+    def test_cte_union(self):
+        orders = table('orders', 
+            column('region'),
+            column('amount'),
+        )
+
+        regional_sales = select([
+                            orders.c.region,
+                            orders.c.amount
+                        ]).cte("regional_sales")
+
+        s = select([regional_sales.c.region]).\
+                where(
+                    regional_sales.c.amount > 500
+                )
+
+        self.assert_compile(s, 
+            "WITH regional_sales AS "
+            "(SELECT orders.region AS region, "
+            "orders.amount AS amount FROM orders) "
+            "SELECT regional_sales.region "
+            "FROM regional_sales WHERE "
+            "regional_sales.amount > :amount_1")
+
+        s = s.union_all(
+            select([regional_sales.c.region]).\
+                where(
+                    regional_sales.c.amount < 300
+                )
+        )
+        self.assert_compile(s, 
+            "WITH regional_sales AS "
+            "(SELECT orders.region AS region, "
+            "orders.amount AS amount FROM orders) "
+            "SELECT regional_sales.region FROM regional_sales "
+            "WHERE regional_sales.amount > :amount_1 "
+            "UNION ALL SELECT regional_sales.region "
+            "FROM regional_sales WHERE "
+            "regional_sales.amount < :amount_2")
+
     def test_date_between(self):
         import datetime
         table = Table('dt', metadata,