]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- union() and union_all() will not whack
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 6 Dec 2008 00:14:50 +0000 (00:14 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 6 Dec 2008 00:14:50 +0000 (00:14 +0000)
any order_by() that has been applied to the
select()s inside.  If you union() a
select() with order_by() (presumably to support
LIMIT/OFFSET), you should also call self_group()
on it to apply parenthesis.

CHANGES
lib/sqlalchemy/sql/expression.py
test/sql/select.py

diff --git a/CHANGES b/CHANGES
index 4ec043e9728f05bd0bcb345d3a2b88f000c9aa6d..be545de640dcb794432ea5c24b2ba25263de2aac 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -111,6 +111,13 @@ CHANGES
     - the "passive" flag on session.is_modified()
       is correctly propagated to the attribute manager.
 
+    - union() and union_all() will not whack 
+      any order_by() that has been applied to the 
+      select()s inside.  If you union() a 
+      select() with order_by() (presumably to support
+      LIMIT/OFFSET), you should also call self_group() 
+      on it to apply parenthesis.
+      
 - documentation
     - Tickets [ticket:1200] [ticket:1149].
       
index 3d66ff21bea13a5346782bc8bb0e64955b139050..0bc7e6a49a4f6f35d2b58b826f3576f304f86323 100644 (file)
@@ -3033,8 +3033,7 @@ class CompoundSelect(_SelectBaseMixin, FromClause):
                         "have identical numbers of columns; select #%d has %d columns, select #%d has %d" %
                         (1, len(self.selects[0].c), n+1, len(s.c))
                 )
-            if s._order_by_clause:
-                s = s.order_by(None)
+
             # unions group from left to right, so don't group first select
             if n:
                 self.selects.append(s.self_group(self))
index 91585e37e4d1d5a60635a5fbe45538e2c581dfa9..352eed42cb50b61d83574658775a22cc52939d9e 100644 (file)
@@ -982,6 +982,16 @@ UNION SELECT mytable.myid FROM mytable"
         s = union(s, s)
         self.assert_compile(s, "SELECT foo, bar UNION SELECT foo, bar UNION (SELECT foo, bar UNION SELECT foo, bar)")
         
+        s = select([column('foo'), column('bar')])
+        # ORDER BY's even though not supported by all DB's, are rendered if requested
+        self.assert_compile(union(s.order_by("foo"), s.order_by("bar")), 
+            "SELECT foo, bar ORDER BY foo UNION SELECT foo, bar ORDER BY bar"
+        )
+        # self_group() is honored
+        self.assert_compile(union(s.order_by("foo").self_group(), s.order_by("bar").limit(10).self_group()), 
+            "(SELECT foo, bar ORDER BY foo) UNION (SELECT foo, bar ORDER BY bar  LIMIT 10)"
+        )
+        
 
     @testing.uses_deprecated()
     def test_binds(self):