]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fix label referencing in SQL Server OFFSET logic
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 10 May 2016 16:49:56 +0000 (12:49 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 10 May 2016 16:55:23 +0000 (12:55 -0400)
Fixed bug where by ROW_NUMBER OVER clause applied for OFFSET
selects in SQL Server would inappropriately substitute a plain column
from the local statement that overlaps with a label name used by
the ORDER BY criteria of the statement.

Change-Id: Ic2500c886cbfc83a1ad5a2681783f008b9f23838
Fixes: #3711
doc/build/changelog/changelog_10.rst
lib/sqlalchemy/dialects/mssql/base.py
lib/sqlalchemy/sql/util.py
test/dialect/mssql/test_compiler.py

index c51040dd5569e56a211c774365f4208a8aabcba8..e508e7b8105e0cc0267c4318a99427c14ef6b564 100644 (file)
 .. changelog::
     :version: 1.0.13
 
+    .. change::
+        :tags: bug, mssql
+        :tickets: 3711
+
+        Fixed bug where by ROW_NUMBER OVER clause applied for OFFSET
+        selects in SQL Server would inappropriately substitute a plain column
+        from the local statement that overlaps with a label name used by
+        the ORDER BY criteria of the statement.
+
     .. change::
         :tags: bug, orm
         :tickets: 3710
index 051efa719167acff1f51773d44810f2073cd1c9e..966700420f493014af8470f25a0c56bb9819b0f8 100644 (file)
@@ -1155,7 +1155,11 @@ class MSSQLCompiler(compiler.SQLCompiler):
                                        'using an OFFSET or a non-simple '
                                        'LIMIT clause')
 
-            _order_by_clauses = select._order_by_clause.clauses
+            _order_by_clauses = [
+                sql_util.unwrap_label_reference(elem)
+                for elem in select._order_by_clause.clauses
+            ]
+
             limit_clause = select._limit_clause
             offset_clause = select._offset_clause
             kwargs['select_wraps_for'] = select
index 5f180646c2393e8c15b84839c916375aa000dec4..24c6f5441fb51841dd7554630db79f8f48eda5a4 100644 (file)
@@ -176,6 +176,16 @@ def unwrap_order_by(clause):
     return result
 
 
+def unwrap_label_reference(element):
+    def replace(elem):
+        if isinstance(elem, (_label_reference, _textual_label_reference)):
+            return elem.element
+
+    return visitors.replacement_traverse(
+        element, {}, replace
+    )
+
+
 def expand_column_list_from_order_by(collist, order_by):
     """Given the columns clause and ORDER BY of a selectable,
     return a list of column expressions that can be added to the collist
index b59ca4fd10d71ea7582a07824f7044e7f117efa6..59982049288da10a7ac522f53c5844046297630f 100644 (file)
@@ -571,6 +571,31 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
         assert t1.c.x in set(c._create_result_map()['x'][1])
         assert t1.c.y in set(c._create_result_map()['y'][1])
 
+    def test_offset_dont_misapply_labelreference(self):
+        m = MetaData()
+
+        t = Table('t', m, Column('x', Integer))
+
+        expr1 = func.foo(t.c.x).label('x')
+        expr2 = func.foo(t.c.x).label('y')
+
+        stmt1 = select([expr1]).order_by(expr1.desc()).offset(1)
+        stmt2 = select([expr2]).order_by(expr2.desc()).offset(1)
+
+        self.assert_compile(
+            stmt1,
+            "SELECT anon_1.x FROM (SELECT foo(t.x) AS x, "
+            "ROW_NUMBER() OVER (ORDER BY foo(t.x) DESC) AS mssql_rn FROM t) "
+            "AS anon_1 WHERE mssql_rn > :param_1"
+        )
+
+        self.assert_compile(
+            stmt2,
+            "SELECT anon_1.y FROM (SELECT foo(t.x) AS y, "
+            "ROW_NUMBER() OVER (ORDER BY foo(t.x) DESC) AS mssql_rn FROM t) "
+            "AS anon_1 WHERE mssql_rn > :param_1"
+        )
+
     def test_limit_zero_offset_using_window(self):
         t = table('t', column('x', Integer), column('y', Integer))