]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- [bug] Fixed bug in over() construct whereby
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 30 Sep 2012 21:20:04 +0000 (17:20 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 30 Sep 2012 21:20:04 +0000 (17:20 -0400)
    passing an empty list for either partition_by
    or order_by, as opposed to None, would fail
    to generate correctly.
    Courtesy Gunnlaugur Por Briem.
    [ticket:2574]

CHANGES
lib/sqlalchemy/sql/compiler.py
test/sql/test_compiler.py

diff --git a/CHANGES b/CHANGES
index 1cc2eee98ff3eda4a206e814af44c88e20bda3fd..9543c0a5fd528f732ab95417680326d81735136d 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -50,6 +50,13 @@ CHANGES
     an Index associated with a Table in a remote
     schema. [ticket:2571]
 
+  - [bug] Fixed bug in over() construct whereby
+    passing an empty list for either partition_by
+    or order_by, as opposed to None, would fail
+    to generate correctly.
+    Courtesy Gunnlaugur Þór Briem.
+    [ticket:2574]
+
   - [bug] Fixed CTE bug whereby positional
     bound parameters present in the CTEs themselves
     would corrupt the overall ordering of
index f45c9c896700f060d0b5f394b121f77411c1221c..40f09da8ba3951d856a487b1ec64deb0863429d9 100644 (file)
@@ -520,17 +520,17 @@ class SQLCompiler(engine.Compiled):
                     cast.typeclause._compiler_dispatch(self, **kwargs))
 
     def visit_over(self, over, **kwargs):
-        x ="%s OVER (" % over.func._compiler_dispatch(self, **kwargs)
-        if over.partition_by is not None:
-            x += "PARTITION BY %s" % \
-                over.partition_by._compiler_dispatch(self, **kwargs)
-            if over.order_by is not None:
-                x += " "
-        if over.order_by is not None:
-            x += "ORDER BY %s" % \
-                over.order_by._compiler_dispatch(self, **kwargs)
-        x += ")"
-        return x
+        return "%s OVER (%s)" % (
+            over.func._compiler_dispatch(self, **kwargs),
+             ' '.join(
+                 '%s BY %s' % (word, clause._compiler_dispatch(self, **kwargs))
+                 for word, clause in (
+                     ('PARTITION', over.partition_by),
+                     ('ORDER', over.order_by)
+                 )
+                 if clause is not None and len(clause)
+            )
+        )
 
     def visit_extract(self, extract, **kwargs):
         field = self.extract_map.get(extract.field, extract.field)
index dda9f7346947856ea7166edd5a418ff29dff8699..a5e08744950165a10504fa52a2bb233b47f2902d 100644 (file)
@@ -2253,6 +2253,10 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL):
                             dialect=sqlite.dialect())
 
     def test_over(self):
+        self.assert_compile(
+            func.row_number().over(),
+            "row_number() OVER ()"
+        )
         self.assert_compile(
             func.row_number().over(
                 order_by=[table1.c.name, table1.c.description]
@@ -2292,6 +2296,30 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL):
             "ORDER BY mytable.name, mytable.description)"
         )
 
+        self.assert_compile(
+            func.row_number().over(
+                partition_by=[],
+                order_by=[table1.c.name, table1.c.description]
+            ),
+            "row_number() OVER (ORDER BY mytable.name, mytable.description)"
+        )
+
+        self.assert_compile(
+            func.row_number().over(
+                partition_by=[table1.c.name, table1.c.description],
+                order_by=[]
+            ),
+            "row_number() OVER (PARTITION BY mytable.name, "
+            "mytable.description)"
+        )
+
+        self.assert_compile(
+            func.row_number().over(
+                partition_by=[],
+                order_by=[]
+            ),
+            "row_number() OVER ()"
+        )
         self.assert_compile(
             select([func.row_number().over(
                 order_by=table1.c.description