]> 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:18:14 +0000 (17:18 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 30 Sep 2012 21:18:14 +0000 (17:18 -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 a264aa152263f05f3a43b335a44e7e25d24c90f4..080e9d205c0553b7d3f167604e9681e94aebf866 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -805,6 +805,13 @@ are also present in 0.8.
     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 3b17c040c5f17692fcbd46103a6e6015b010acab..d97c048a9b227cc2e8dffe3edbc618722ae84740 100644 (file)
@@ -530,17 +530,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 3c6e687caca2899ad2a520a3fd00e400ebc53c10..b09ae1ab008374c82481c47ed75bf3df5e5ce9b6 100644 (file)
@@ -2257,6 +2257,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]
@@ -2296,6 +2300,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