]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fix MSSQL / Oracle limit/offset regressions
authorFederico Caselli <cfederico87@gmail.com>
Tue, 30 Mar 2021 21:15:04 +0000 (23:15 +0200)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 1 Apr 2021 02:13:30 +0000 (22:13 -0400)
Fixed a regression in MSSQL 2012+ that prevented the order clause
to be rendered when ``offset=0`` is used in a subquery.

Fixed critical regression where the Oracle compiler would not maintain the
correct parameter values in the LIMIT/OFFSET for a select due to a caching
issue.

Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com>
Fixes: #6163
Fixes: #6173
Change-Id: Ieb12354271d09ad935d684ee0db4fa0128837215

doc/build/changelog/unreleased_14/6163.rst [new file with mode: 0644]
doc/build/changelog/unreleased_14/6173.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/mssql/base.py
lib/sqlalchemy/dialects/oracle/base.py
lib/sqlalchemy/sql/selectable.py
lib/sqlalchemy/testing/suite/test_select.py
test/dialect/mssql/test_compiler.py
test/dialect/oracle/test_compiler.py
test/dialect/oracle/test_dialect.py

diff --git a/doc/build/changelog/unreleased_14/6163.rst b/doc/build/changelog/unreleased_14/6163.rst
new file mode 100644 (file)
index 0000000..f8811a3
--- /dev/null
@@ -0,0 +1,6 @@
+.. change::
+    :tags: bug, sql, mssql
+    :tickets: 6163
+
+    Fixed a regression in MSSQL 2012+ that prevented the order by clause
+    to be rendered when ``offset=0`` is used in a subquery.
diff --git a/doc/build/changelog/unreleased_14/6173.rst b/doc/build/changelog/unreleased_14/6173.rst
new file mode 100644 (file)
index 0000000..5d09b12
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, regression, oracle
+    :tickets: 6173
+
+    Fixed critical regression where the Oracle compiler would not maintain the
+    correct parameter values in the LIMIT/OFFSET for a select due to a caching
+    issue.
+
index 1be3965b8f8247e5be824a7916367a898dafa307..bfa1cf08c4c52e8532541ffcb3c45c81b90e5c0a 100644 (file)
@@ -1784,7 +1784,7 @@ class MSSQLCompiler(compiler.SQLCompiler):
     def limit_clause(self, cs, **kwargs):
         return ""
 
-    def _check_can_use_fetch_like(self, select):
+    def _check_can_use_fetch_limit(self, select):
         # to use ROW_NUMBER(), an ORDER BY is required.
         # OFFSET are FETCH are options of the ORDER BY clause
         if not select._order_by_clause.clauses:
@@ -1810,7 +1810,7 @@ class MSSQLCompiler(compiler.SQLCompiler):
         """
 
         if self.dialect._supports_offset_fetch and not self._use_top(select):
-            self._check_can_use_fetch_like(select)
+            self._check_can_use_fetch_limit(select)
 
             text = ""
 
@@ -1850,7 +1850,7 @@ class MSSQLCompiler(compiler.SQLCompiler):
             and not self._use_top(select)
             and not getattr(select, "_mssql_visit", None)
         ):
-            self._check_can_use_fetch_like(select)
+            self._check_can_use_fetch_limit(select)
 
             _order_by_clauses = [
                 sql_util.unwrap_label_reference(elem)
@@ -2031,7 +2031,10 @@ class MSSQLCompiler(compiler.SQLCompiler):
         if (
             self.is_subquery()
             and not select._limit
-            and (not select._offset or not self.dialect._supports_offset_fetch)
+            and (
+                select._offset is None
+                or not self.dialect._supports_offset_fetch
+            )
         ):
             # avoid processing the order by clause if we won't end up
             # using it, because we don't want all the bind params tacked
index 88b1018113c6243d55f7d460d77c6386a5e22b83..46fcbbbe1a2347a1c47fc5c124fc8bd3d096571e 100644 (file)
@@ -561,7 +561,6 @@ from ...types import CHAR
 from ...types import CLOB
 from ...types import FLOAT
 from ...types import INTEGER
-from ...types import Integer
 from ...types import NCHAR
 from ...types import NVARCHAR
 from ...types import TIMESTAMP
@@ -1092,6 +1091,13 @@ class OracleCompiler(compiler.SQLCompiler):
             ):
                 limit_clause = select._limit_clause
                 offset_clause = select._offset_clause
+
+                if select._simple_int_clause(limit_clause):
+                    limit_clause = limit_clause._render_literal_execute()
+
+                if select._simple_int_clause(offset_clause):
+                    offset_clause = offset_clause._render_literal_execute()
+
                 # currently using form at:
                 # https://blogs.oracle.com/oraclemagazine/\
                 # on-rownum-and-limiting-results
@@ -1120,22 +1126,17 @@ class OracleCompiler(compiler.SQLCompiler):
                         is not None
                     ]
                 )
+
                 if (
                     limit_clause is not None
                     and self.dialect.optimize_limits
                     and select._simple_int_clause(limit_clause)
                 ):
-                    param = sql.bindparam(
-                        "_ora_frow",
-                        select._limit,
-                        type_=Integer,
-                        literal_execute=True,
-                        unique=True,
-                    )
                     limitselect = limitselect.prefix_with(
                         expression.text(
-                            "/*+ FIRST_ROWS(:_ora_frow) */"
-                        ).bindparams(param)
+                            "/*+ FIRST_ROWS(%s) */"
+                            % self.process(limit_clause, **kwargs)
+                        )
                     )
 
                 limitselect._oracle_visit = True
@@ -1155,19 +1156,14 @@ class OracleCompiler(compiler.SQLCompiler):
                         offset_clause is None
                         or select._simple_int_clause(offset_clause)
                     ):
-                        max_row = select._limit
+                        max_row = limit_clause
 
                         if offset_clause is not None:
-                            max_row += select._offset
-                        max_row = sql.bindparam(
-                            None,
-                            max_row,
-                            type_=Integer,
-                            literal_execute=True,
-                            unique=True,
-                        )
+                            max_row = max_row + offset_clause
+
                     else:
                         max_row = limit_clause
+
                         if offset_clause is not None:
                             max_row = max_row + offset_clause
                     limitselect = limitselect.where(
@@ -1214,15 +1210,6 @@ class OracleCompiler(compiler.SQLCompiler):
                             adapter.traverse(elem) for elem in for_update.of
                         ]
 
-                    if select._simple_int_clause(offset_clause):
-                        offset_clause = sql.bindparam(
-                            None,
-                            select._offset,
-                            Integer,
-                            literal_execute=True,
-                            unique=True,
-                        )
-
                     offsetselect = offsetselect.where(
                         sql.literal_column("ora_rn") > offset_clause
                     )
index 7c53f437c6b4b503388c0cda6174f2495e6ea137..d28ac775384402eec6e46b07585ba988de657d9e 100644 (file)
@@ -69,6 +69,14 @@ class _OffsetLimitParam(BindParameter):
     def _limit_offset_value(self):
         return self.effective_value
 
+    def _render_literal_execute(self):
+        return _OffsetLimitParam(
+            self.key,
+            self.value,
+            type_=self.type,
+            literal_execute=True,
+        )
+
 
 @util.deprecated(
     "1.4",
index c6dacd42394c234914c0d6e5be684655844c84f8..c519154f6f195ad77d3b9207e0f69d8451a2e592 100644 (file)
@@ -194,61 +194,116 @@ class FetchLimitOffsetTest(fixtures.TablesTest):
             ],
         )
 
-    def _assert_result(self, select, result, params=()):
-        with config.db.connect() as conn:
-            eq_(conn.execute(select, params).fetchall(), result)
+    def _assert_result(
+        self, connection, select, result, params=(), set_=False
+    ):
+        if set_:
+            query_res = connection.execute(select, params).fetchall()
+            eq_(len(query_res), len(result))
+            eq_(set(query_res), set(result))
+
+        else:
+            eq_(connection.execute(select, params).fetchall(), result)
 
     def _assert_result_str(self, select, result, params=()):
         conn = config.db.connect(close_with_result=True)
         eq_(conn.exec_driver_sql(select, params).fetchall(), result)
 
-    def test_simple_limit(self):
+    def test_simple_limit(self, connection):
         table = self.tables.some_table
+        stmt = select(table).order_by(table.c.id)
         self._assert_result(
-            select(table).order_by(table.c.id).limit(2),
+            connection,
+            stmt.limit(2),
             [(1, 1, 2), (2, 2, 3)],
         )
+        self._assert_result(
+            connection,
+            stmt.limit(3),
+            [(1, 1, 2), (2, 2, 3), (3, 3, 4)],
+        )
 
     @testing.requires.fetch_first
-    def test_simple_fetch(self):
+    def test_simple_fetch(self, connection):
         table = self.tables.some_table
         self._assert_result(
+            connection,
             select(table).order_by(table.c.id).fetch(2),
             [(1, 1, 2), (2, 2, 3)],
         )
+        self._assert_result(
+            connection,
+            select(table).order_by(table.c.id).fetch(3),
+            [(1, 1, 2), (2, 2, 3), (3, 3, 4)],
+        )
 
     @testing.requires.offset
-    def test_simple_offset(self):
+    def test_simple_offset(self, connection):
         table = self.tables.some_table
         self._assert_result(
+            connection,
             select(table).order_by(table.c.id).offset(2),
             [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
         )
+        self._assert_result(
+            connection,
+            select(table).order_by(table.c.id).offset(3),
+            [(4, 4, 5), (5, 4, 6)],
+        )
 
     @testing.requires.offset
-    def test_simple_limit_offset(self):
+    def test_simple_limit_offset(self, connection):
         table = self.tables.some_table
         self._assert_result(
+            connection,
             select(table).order_by(table.c.id).limit(2).offset(1),
             [(2, 2, 3), (3, 3, 4)],
         )
+        self._assert_result(
+            connection,
+            select(table).order_by(table.c.id).limit(3).offset(2),
+            [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
+        )
 
     @testing.requires.fetch_first
-    def test_simple_fetch_offset(self):
+    def test_simple_fetch_offset(self, connection):
         table = self.tables.some_table
         self._assert_result(
+            connection,
             select(table).order_by(table.c.id).fetch(2).offset(1),
             [(2, 2, 3), (3, 3, 4)],
         )
 
+        self._assert_result(
+            connection,
+            select(table).order_by(table.c.id).fetch(3).offset(2),
+            [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
+        )
+
     @testing.requires.fetch_no_order_by
-    def test_fetch_offset_no_order(self):
+    def test_fetch_offset_no_order(self, connection):
         table = self.tables.some_table
-        with config.db.connect() as conn:
-            eq_(
-                set(conn.execute(select(table).fetch(10))),
-                set([(1, 1, 2), (2, 2, 3), (3, 3, 4), (4, 4, 5), (5, 4, 6)]),
-            )
+        self._assert_result(
+            connection,
+            select(table).fetch(10),
+            [(1, 1, 2), (2, 2, 3), (3, 3, 4), (4, 4, 5), (5, 4, 6)],
+            set_=True,
+        )
+
+    @testing.requires.offset
+    def test_simple_offset_zero(self, connection):
+        table = self.tables.some_table
+        self._assert_result(
+            connection,
+            select(table).order_by(table.c.id).offset(0),
+            [(1, 1, 2), (2, 2, 3), (3, 3, 4), (4, 4, 5), (5, 4, 6)],
+        )
+
+        self._assert_result(
+            connection,
+            select(table).order_by(table.c.id).offset(1),
+            [(2, 2, 3), (3, 3, 4), (4, 4, 5), (5, 4, 6)],
+        )
 
     @testing.requires.offset
     def test_limit_offset_nobinds(self):
@@ -277,27 +332,44 @@ class FetchLimitOffsetTest(fixtures.TablesTest):
         self._assert_result_str(sql, [(2, 2, 3), (3, 3, 4)])
 
     @testing.requires.bound_limit_offset
-    def test_bound_limit(self):
+    def test_bound_limit(self, connection):
         table = self.tables.some_table
         self._assert_result(
+            connection,
             select(table).order_by(table.c.id).limit(bindparam("l")),
             [(1, 1, 2), (2, 2, 3)],
             params={"l": 2},
         )
 
+        self._assert_result(
+            connection,
+            select(table).order_by(table.c.id).limit(bindparam("l")),
+            [(1, 1, 2), (2, 2, 3), (3, 3, 4)],
+            params={"l": 3},
+        )
+
     @testing.requires.bound_limit_offset
-    def test_bound_offset(self):
+    def test_bound_offset(self, connection):
         table = self.tables.some_table
         self._assert_result(
+            connection,
             select(table).order_by(table.c.id).offset(bindparam("o")),
             [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
             params={"o": 2},
         )
 
+        self._assert_result(
+            connection,
+            select(table).order_by(table.c.id).offset(bindparam("o")),
+            [(2, 2, 3), (3, 3, 4), (4, 4, 5), (5, 4, 6)],
+            params={"o": 1},
+        )
+
     @testing.requires.bound_limit_offset
-    def test_bound_limit_offset(self):
+    def test_bound_limit_offset(self, connection):
         table = self.tables.some_table
         self._assert_result(
+            connection,
             select(table)
             .order_by(table.c.id)
             .limit(bindparam("l"))
@@ -306,10 +378,21 @@ class FetchLimitOffsetTest(fixtures.TablesTest):
             params={"l": 2, "o": 1},
         )
 
+        self._assert_result(
+            connection,
+            select(table)
+            .order_by(table.c.id)
+            .limit(bindparam("l"))
+            .offset(bindparam("o")),
+            [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
+            params={"l": 3, "o": 2},
+        )
+
     @testing.requires.fetch_first
-    def test_bound_fetch_offset(self):
+    def test_bound_fetch_offset(self, connection):
         table = self.tables.some_table
         self._assert_result(
+            connection,
             select(table)
             .order_by(table.c.id)
             .fetch(bindparam("f"))
@@ -318,10 +401,21 @@ class FetchLimitOffsetTest(fixtures.TablesTest):
             params={"f": 2, "o": 1},
         )
 
+        self._assert_result(
+            connection,
+            select(table)
+            .order_by(table.c.id)
+            .fetch(bindparam("f"))
+            .offset(bindparam("o")),
+            [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
+            params={"f": 3, "o": 2},
+        )
+
     @testing.requires.sql_expression_limit_offset
-    def test_expr_offset(self):
+    def test_expr_offset(self, connection):
         table = self.tables.some_table
         self._assert_result(
+            connection,
             select(table)
             .order_by(table.c.id)
             .offset(literal_column("1") + literal_column("2")),
@@ -329,9 +423,10 @@ class FetchLimitOffsetTest(fixtures.TablesTest):
         )
 
     @testing.requires.sql_expression_limit_offset
-    def test_expr_limit(self):
+    def test_expr_limit(self, connection):
         table = self.tables.some_table
         self._assert_result(
+            connection,
             select(table)
             .order_by(table.c.id)
             .limit(literal_column("1") + literal_column("2")),
@@ -339,9 +434,10 @@ class FetchLimitOffsetTest(fixtures.TablesTest):
         )
 
     @testing.requires.sql_expression_limit_offset
-    def test_expr_limit_offset(self):
+    def test_expr_limit_offset(self, connection):
         table = self.tables.some_table
         self._assert_result(
+            connection,
             select(table)
             .order_by(table.c.id)
             .limit(literal_column("1") + literal_column("1"))
@@ -350,9 +446,10 @@ class FetchLimitOffsetTest(fixtures.TablesTest):
         )
 
     @testing.requires.fetch_first
-    def test_expr_fetch_offset(self):
+    def test_expr_fetch_offset(self, connection):
         table = self.tables.some_table
         self._assert_result(
+            connection,
             select(table)
             .order_by(table.c.id)
             .fetch(literal_column("1") + literal_column("1"))
@@ -361,9 +458,10 @@ class FetchLimitOffsetTest(fixtures.TablesTest):
         )
 
     @testing.requires.sql_expression_limit_offset
-    def test_simple_limit_expr_offset(self):
+    def test_simple_limit_expr_offset(self, connection):
         table = self.tables.some_table
         self._assert_result(
+            connection,
             select(table)
             .order_by(table.c.id)
             .limit(2)
@@ -371,10 +469,20 @@ class FetchLimitOffsetTest(fixtures.TablesTest):
             [(3, 3, 4), (4, 4, 5)],
         )
 
+        self._assert_result(
+            connection,
+            select(table)
+            .order_by(table.c.id)
+            .limit(3)
+            .offset(literal_column("1") + literal_column("1")),
+            [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
+        )
+
     @testing.requires.sql_expression_limit_offset
-    def test_expr_limit_simple_offset(self):
+    def test_expr_limit_simple_offset(self, connection):
         table = self.tables.some_table
         self._assert_result(
+            connection,
             select(table)
             .order_by(table.c.id)
             .limit(literal_column("1") + literal_column("1"))
@@ -382,40 +490,51 @@ class FetchLimitOffsetTest(fixtures.TablesTest):
             [(3, 3, 4), (4, 4, 5)],
         )
 
+        self._assert_result(
+            connection,
+            select(table)
+            .order_by(table.c.id)
+            .limit(literal_column("1") + literal_column("1"))
+            .offset(1),
+            [(2, 2, 3), (3, 3, 4)],
+        )
+
     @testing.requires.fetch_ties
-    def test_simple_fetch_ties(self):
+    def test_simple_fetch_ties(self, connection):
         table = self.tables.some_table
-        with config.db.connect() as conn:
-            eq_(
-                set(
-                    conn.execute(
-                        select(table)
-                        .order_by(table.c.x.desc())
-                        .fetch(1, with_ties=True)
-                    )
-                ),
-                set([(4, 4, 5), (5, 4, 6)]),
-            )
+        self._assert_result(
+            connection,
+            select(table).order_by(table.c.x.desc()).fetch(1, with_ties=True),
+            [(4, 4, 5), (5, 4, 6)],
+            set_=True,
+        )
+
+        self._assert_result(
+            connection,
+            select(table).order_by(table.c.x.desc()).fetch(3, with_ties=True),
+            [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
+            set_=True,
+        )
 
     @testing.requires.fetch_ties
     @testing.requires.fetch_offset_with_options
-    def test_fetch_offset_ties(self):
+    def test_fetch_offset_ties(self, connection):
         table = self.tables.some_table
-        with config.db.connect() as conn:
-            fa = conn.execute(
-                select(table)
-                .order_by(table.c.x)
-                .fetch(2, with_ties=True)
-                .offset(2)
-            ).fetchall()
-            eq_(fa[0], (3, 3, 4))
-            eq_(set(fa), set([(3, 3, 4), (4, 4, 5), (5, 4, 6)]))
+        fa = connection.execute(
+            select(table)
+            .order_by(table.c.x)
+            .fetch(2, with_ties=True)
+            .offset(2)
+        ).fetchall()
+        eq_(fa[0], (3, 3, 4))
+        eq_(set(fa), set([(3, 3, 4), (4, 4, 5), (5, 4, 6)]))
 
     @testing.requires.fetch_ties
     @testing.requires.fetch_offset_with_options
-    def test_fetch_offset_ties_exact_number(self):
+    def test_fetch_offset_ties_exact_number(self, connection):
         table = self.tables.some_table
         self._assert_result(
+            connection,
             select(table)
             .order_by(table.c.x)
             .fetch(2, with_ties=True)
@@ -423,19 +542,30 @@ class FetchLimitOffsetTest(fixtures.TablesTest):
             [(2, 2, 3), (3, 3, 4)],
         )
 
+        self._assert_result(
+            connection,
+            select(table)
+            .order_by(table.c.x)
+            .fetch(3, with_ties=True)
+            .offset(3),
+            [(4, 4, 5), (5, 4, 6)],
+        )
+
     @testing.requires.fetch_percent
-    def test_simple_fetch_percent(self):
+    def test_simple_fetch_percent(self, connection):
         table = self.tables.some_table
         self._assert_result(
+            connection,
             select(table).order_by(table.c.id).fetch(20, percent=True),
             [(1, 1, 2)],
         )
 
     @testing.requires.fetch_percent
     @testing.requires.fetch_offset_with_options
-    def test_fetch_offset_percent(self):
+    def test_fetch_offset_percent(self, connection):
         table = self.tables.some_table
         self._assert_result(
+            connection,
             select(table)
             .order_by(table.c.id)
             .fetch(40, percent=True)
@@ -445,32 +575,30 @@ class FetchLimitOffsetTest(fixtures.TablesTest):
 
     @testing.requires.fetch_ties
     @testing.requires.fetch_percent
-    def test_simple_fetch_percent_ties(self):
+    def test_simple_fetch_percent_ties(self, connection):
         table = self.tables.some_table
-        with config.db.connect() as conn:
-            fa = conn.execute(
-                select(table)
-                .order_by(table.c.x.desc())
-                .fetch(20, percent=True, with_ties=True)
-            ).fetchall()
-
-        eq_(len(fa), 2)
-        eq_(set(fa), set([(4, 4, 5), (5, 4, 6)]))
+        self._assert_result(
+            connection,
+            select(table)
+            .order_by(table.c.x.desc())
+            .fetch(20, percent=True, with_ties=True),
+            [(4, 4, 5), (5, 4, 6)],
+            set_=True,
+        )
 
     @testing.requires.fetch_ties
     @testing.requires.fetch_percent
     @testing.requires.fetch_offset_with_options
-    def test_fetch_offset_percent_ties(self):
+    def test_fetch_offset_percent_ties(self, connection):
         table = self.tables.some_table
-        with config.db.connect() as conn:
-            fa = conn.execute(
-                select(table)
-                .order_by(table.c.x)
-                .fetch(40, percent=True, with_ties=True)
-                .offset(2)
-            ).fetchall()
-            eq_(fa[0], (3, 3, 4))
-            eq_(set(fa), set([(3, 3, 4), (4, 4, 5), (5, 4, 6)]))
+        fa = connection.execute(
+            select(table)
+            .order_by(table.c.x)
+            .fetch(40, percent=True, with_ties=True)
+            .offset(2)
+        ).fetchall()
+        eq_(fa[0], (3, 3, 4))
+        eq_(set(fa), set([(3, 3, 4), (4, 4, 5), (5, 4, 6)]))
 
 
 class JoinTest(fixtures.TablesTest):
index 759833ecaefa3898383651583bd6bb52e672223c..582205ea106720ab8b39cc5a577cc1462fac523e 100644 (file)
@@ -440,7 +440,8 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             "foo.myid = mytable.myid",
         )
 
-    def test_noorderby_insubquery_offset_oldstyle(self):
+    @testing.combinations(10, 0)
+    def test_noorderby_insubquery_offset_oldstyle(self, offset):
         """test "no ORDER BY in subqueries unless TOP / LIMIT / OFFSET"
         present"""
 
@@ -454,7 +455,7 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
         q = (
             select(table1.c.myid)
             .order_by(table1.c.myid)
-            .offset(10)
+            .offset(offset)
             .alias("foo")
         )
         crit = q.c.myid == table1.c.myid
@@ -467,7 +468,8 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             "foo.myid = mytable.myid",
         )
 
-    def test_noorderby_insubquery_offset_newstyle(self, dialect_2012):
+    @testing.combinations(10, 0, argnames="offset")
+    def test_noorderby_insubquery_offset_newstyle(self, dialect_2012, offset):
         """test "no ORDER BY in subqueries unless TOP / LIMIT / OFFSET"
         present"""
 
@@ -481,7 +483,7 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
         q = (
             select(table1.c.myid)
             .order_by(table1.c.myid)
-            .offset(10)
+            .offset(offset)
             .alias("foo")
         )
         crit = q.c.myid == table1.c.myid
index c8e0ff7ef66af8153981d5e2cc2aaa8515bdfc9e..5e9f46e1a451c459ceb21393911588ca14527061 100644 (file)
@@ -163,9 +163,10 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             "anon_2.col2 AS col2, ROWNUM AS ora_rn FROM (SELECT "
             "sometable.col1 AS col1, sometable.col2 AS "
             "col2 FROM sometable) anon_2 WHERE ROWNUM <= "
-            "[POSTCOMPILE_param_1]) anon_1 WHERE ora_rn > "
+            "[POSTCOMPILE_param_1] + [POSTCOMPILE_param_2]) anon_1 "
+            "WHERE ora_rn > "
             "[POSTCOMPILE_param_2]",
-            checkparams={"param_1": 30, "param_2": 20},
+            checkparams={"param_1": 10, "param_2": 20},
         )
 
         c = s.compile(dialect=oracle.OracleDialect())
@@ -179,14 +180,15 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
         self.assert_compile(
             s,
             "SELECT anon_1.col1, anon_1.col2 FROM "
-            "(SELECT /*+ FIRST_ROWS([POSTCOMPILE_ora_frow_1]) */ "
+            "(SELECT /*+ FIRST_ROWS([POSTCOMPILE_param_1]) */ "
             "anon_2.col1 AS col1, "
             "anon_2.col2 AS col2, ROWNUM AS ora_rn FROM (SELECT "
             "sometable.col1 AS col1, sometable.col2 AS "
             "col2 FROM sometable) anon_2 WHERE ROWNUM <= "
-            "[POSTCOMPILE_param_1]) anon_1 WHERE ora_rn > "
+            "[POSTCOMPILE_param_1] + [POSTCOMPILE_param_2]) anon_1 "
+            "WHERE ora_rn > "
             "[POSTCOMPILE_param_2]",
-            checkparams={"ora_frow_1": 10, "param_1": 30, "param_2": 20},
+            checkparams={"param_1": 10, "param_2": 20},
             dialect=oracle.OracleDialect(optimize_limits=True),
         )
 
@@ -204,9 +206,10 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             "ROWNUM AS ora_rn "
             "FROM (SELECT sometable.col1 AS col1, "
             "sometable.col2 AS col2 FROM sometable) anon_3 "
-            "WHERE ROWNUM <= [POSTCOMPILE_param_1]) anon_2 "
+            "WHERE ROWNUM <= [POSTCOMPILE_param_1] + [POSTCOMPILE_param_2]) "
+            "anon_2 "
             "WHERE ora_rn > [POSTCOMPILE_param_2]) anon_1",
-            checkparams={"param_1": 30, "param_2": 20},
+            checkparams={"param_1": 10, "param_2": 20},
         )
 
         self.assert_compile(
@@ -218,7 +221,8 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             "ROWNUM AS ora_rn "
             "FROM (SELECT sometable.col1 AS col1, "
             "sometable.col2 AS col2 FROM sometable) anon_3 "
-            "WHERE ROWNUM <= [POSTCOMPILE_param_1]) anon_2 "
+            "WHERE ROWNUM <= [POSTCOMPILE_param_1] + [POSTCOMPILE_param_2]) "
+            "anon_2 "
             "WHERE ora_rn > [POSTCOMPILE_param_2]) anon_1",
         )
         c = s2.compile(dialect=oracle.OracleDialect())
@@ -237,9 +241,9 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             "sometable.col1 AS col1, sometable.col2 AS "
             "col2 FROM sometable ORDER BY "
             "sometable.col2) anon_2 WHERE ROWNUM <= "
-            "[POSTCOMPILE_param_1]) anon_1 "
+            "[POSTCOMPILE_param_1] + [POSTCOMPILE_param_2]) anon_1 "
             "WHERE ora_rn > [POSTCOMPILE_param_2]",
-            checkparams={"param_1": 30, "param_2": 20},
+            checkparams={"param_1": 10, "param_2": 20},
         )
         c = s.compile(dialect=oracle.OracleDialect())
         eq_(len(c._result_columns), 2)
@@ -265,13 +269,13 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
         s = select(t).with_for_update().limit(10).order_by(t.c.col2)
         self.assert_compile(
             s,
-            "SELECT /*+ FIRST_ROWS([POSTCOMPILE_ora_frow_1]) */ "
+            "SELECT /*+ FIRST_ROWS([POSTCOMPILE_param_1]) */ "
             "anon_1.col1, anon_1.col2 FROM (SELECT "
             "sometable.col1 AS col1, sometable.col2 AS "
             "col2 FROM sometable ORDER BY "
             "sometable.col2) anon_1 WHERE ROWNUM <= [POSTCOMPILE_param_1] "
             "FOR UPDATE",
-            checkparams={"param_1": 10, "ora_frow_1": 10},
+            checkparams={"param_1": 10},
             dialect=oracle.OracleDialect(optimize_limits=True),
         )
 
@@ -287,10 +291,10 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             "sometable.col1 AS col1, sometable.col2 AS "
             "col2 FROM sometable ORDER BY "
             "sometable.col2) anon_2 WHERE ROWNUM <= "
-            "[POSTCOMPILE_param_1]) anon_1 "
+            "[POSTCOMPILE_param_1] + [POSTCOMPILE_param_2]) anon_1 "
             "WHERE ora_rn > [POSTCOMPILE_param_2] FOR "
             "UPDATE",
-            checkparams={"param_1": 30, "param_2": 20},
+            checkparams={"param_1": 10, "param_2": 20},
         )
 
     def test_limit_six(self):
@@ -308,7 +312,7 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             "col1, anon_2.col2 AS col2, ROWNUM AS ora_rn FROM "
             "(SELECT sometable.col1 AS col1, sometable.col2 AS col2 "
             "FROM sometable ORDER BY sometable.col2) anon_2 WHERE "
-            "ROWNUM <= :param_1 + :param_2 + :param_3) anon_1 "
+            "ROWNUM <= [POSTCOMPILE_param_1] + :param_2 + :param_3) anon_1 "
             "WHERE ora_rn > :param_2 + :param_3",
             checkparams={"param_1": 10, "param_2": 10, "param_3": 20},
         )
@@ -512,10 +516,11 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             "ROWNUM AS ora_rn "
             "FROM (SELECT mytable.myid AS myid, mytable.name AS name "
             "FROM mytable WHERE mytable.myid = :myid_1) anon_2 "
-            "WHERE ROWNUM <= [POSTCOMPILE_param_1]) anon_1 "
+            "WHERE ROWNUM <= [POSTCOMPILE_param_1] + [POSTCOMPILE_param_2]) "
+            "anon_1 "
             "WHERE ora_rn > [POSTCOMPILE_param_2] "
             "FOR UPDATE OF anon_1.name NOWAIT",
-            checkparams={"param_1": 60, "param_2": 50, "myid_1": 7},
+            checkparams={"param_1": 10, "param_2": 50, "myid_1": 7},
         )
 
     def test_for_update_of_w_limit_offset_adaption_col_unpresent(self):
@@ -531,10 +536,11 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             "ROWNUM AS ora_rn, anon_2.name AS name "
             "FROM (SELECT mytable.myid AS myid, mytable.name AS name "
             "FROM mytable WHERE mytable.myid = :myid_1) anon_2 "
-            "WHERE ROWNUM <= [POSTCOMPILE_param_1]) anon_1 "
+            "WHERE "
+            "ROWNUM <= [POSTCOMPILE_param_1] + [POSTCOMPILE_param_2]) anon_1 "
             "WHERE ora_rn > [POSTCOMPILE_param_2] "
             "FOR UPDATE OF anon_1.name NOWAIT",
-            checkparams={"param_1": 60, "param_2": 50, "myid_1": 7},
+            checkparams={"param_1": 10, "param_2": 50, "myid_1": 7},
         )
 
     def test_for_update_of_w_limit_offset_adaption_partial_col_unpresent(self):
@@ -552,10 +558,11 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             "mytable.bar AS bar, "
             "mytable.foo AS foo FROM mytable "
             "WHERE mytable.myid = :myid_1) anon_2 "
-            "WHERE ROWNUM <= [POSTCOMPILE_param_1]) anon_1 "
+            "WHERE ROWNUM <= [POSTCOMPILE_param_1] + [POSTCOMPILE_param_2]) "
+            "anon_1 "
             "WHERE ora_rn > [POSTCOMPILE_param_2] "
             "FOR UPDATE OF anon_1.foo, anon_1.bar NOWAIT",
-            checkparams={"param_1": 60, "param_2": 50, "myid_1": 7},
+            checkparams={"param_1": 10, "param_2": 50, "myid_1": 7},
         )
 
     def test_limit_preserves_typing_information(self):
@@ -616,7 +623,8 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             "anon_2.col1 AS col1, anon_2.col2 AS col2, ROWNUM AS ora_rn "
             "FROM (SELECT sometable.col1 AS col1, sometable.col2 AS col2 "
             "FROM sometable) anon_2 "
-            "WHERE ROWNUM <= [POSTCOMPILE_param_1]) anon_1 "
+            "WHERE ROWNUM <= [POSTCOMPILE_param_1] + "
+            "[POSTCOMPILE_param_2]) anon_1 "
             "WHERE ora_rn > [POSTCOMPILE_param_2]",
             dialect=dialect,
         )
@@ -672,10 +680,11 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             "ROWNUM AS ora_rn "
             "FROM (SELECT sometable.col1 AS col1, sometable.col2 AS col2 "
             "FROM sometable) anon_2 "
-            "WHERE ROWNUM <= [POSTCOMPILE_param_1]) anon_1 "
+            "WHERE ROWNUM <= [POSTCOMPILE_param_1] + "
+            "[POSTCOMPILE_param_2]) anon_1 "
             "WHERE ora_rn > [POSTCOMPILE_param_2]",
             dialect=dialect,
-            checkparams={"param_1": 20, "param_2": 10},
+            checkparams={"param_1": 10, "param_2": 10},
         )
 
     def test_long_labels_legacy_ident_length(self):
@@ -875,9 +884,10 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             "thirdtable.userid(+) = "
             "myothertable.otherid AND mytable.myid = "
             "myothertable.otherid ORDER BY mytable.name) anon_2 "
-            "WHERE ROWNUM <= [POSTCOMPILE_param_1]) anon_1 "
+            "WHERE ROWNUM <= [POSTCOMPILE_param_1] + [POSTCOMPILE_param_2]) "
+            "anon_1 "
             "WHERE ora_rn > [POSTCOMPILE_param_2]",
-            checkparams={"param_1": 15, "param_2": 5},
+            checkparams={"param_1": 10, "param_2": 5},
             dialect=oracle.dialect(use_ansi=False),
         )
 
index cce33d4a26a26f8b3eff953081cfc1f464abf2d9..554e5f18b4be58195c8841f2d5af24be63ec393a 100644 (file)
@@ -33,6 +33,7 @@ from sqlalchemy.testing import mock
 from sqlalchemy.testing.mock import Mock
 from sqlalchemy.testing.schema import Column
 from sqlalchemy.testing.schema import Table
+from sqlalchemy.testing.suite import test_select
 from sqlalchemy.util import u
 from sqlalchemy.util import ue
 
@@ -1054,3 +1055,11 @@ END;
         fn = func.three_pairs().table_valued("string1", "string2")
         result = connection.execute(select(fn.c.string1, fn.c.string2)).all()
         eq_(result, [("a", "b"), ("c", "d"), ("e", "f")])
+
+
+class OptimizedFetchLimitOffsetTest(test_select.FetchLimitOffsetTest):
+    __only_on__ = "oracle"
+
+    @classmethod
+    def setup_bind(cls):
+        return engines.testing_engine(options={"optimize_limits": True})