From: Federico Caselli Date: Tue, 30 Mar 2021 21:15:04 +0000 (+0200) Subject: Fix MSSQL / Oracle limit/offset regressions X-Git-Tag: rel_1_4_5~10^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a357baec856bb73c511f8f8484d316f19a29252a;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Fix MSSQL / Oracle limit/offset regressions 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 Fixes: #6163 Fixes: #6173 Change-Id: Ieb12354271d09ad935d684ee0db4fa0128837215 --- diff --git a/doc/build/changelog/unreleased_14/6163.rst b/doc/build/changelog/unreleased_14/6163.rst new file mode 100644 index 0000000000..f8811a3411 --- /dev/null +++ b/doc/build/changelog/unreleased_14/6163.rst @@ -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 index 0000000000..5d09b129c5 --- /dev/null +++ b/doc/build/changelog/unreleased_14/6173.rst @@ -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. + diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py index 1be3965b8f..bfa1cf08c4 100644 --- a/lib/sqlalchemy/dialects/mssql/base.py +++ b/lib/sqlalchemy/dialects/mssql/base.py @@ -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 diff --git a/lib/sqlalchemy/dialects/oracle/base.py b/lib/sqlalchemy/dialects/oracle/base.py index 88b1018113..46fcbbbe1a 100644 --- a/lib/sqlalchemy/dialects/oracle/base.py +++ b/lib/sqlalchemy/dialects/oracle/base.py @@ -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 ) diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py index 7c53f437c6..d28ac77538 100644 --- a/lib/sqlalchemy/sql/selectable.py +++ b/lib/sqlalchemy/sql/selectable.py @@ -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", diff --git a/lib/sqlalchemy/testing/suite/test_select.py b/lib/sqlalchemy/testing/suite/test_select.py index c6dacd4239..c519154f6f 100644 --- a/lib/sqlalchemy/testing/suite/test_select.py +++ b/lib/sqlalchemy/testing/suite/test_select.py @@ -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): diff --git a/test/dialect/mssql/test_compiler.py b/test/dialect/mssql/test_compiler.py index 759833ecae..582205ea10 100644 --- a/test/dialect/mssql/test_compiler.py +++ b/test/dialect/mssql/test_compiler.py @@ -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 diff --git a/test/dialect/oracle/test_compiler.py b/test/dialect/oracle/test_compiler.py index c8e0ff7ef6..5e9f46e1a4 100644 --- a/test/dialect/oracle/test_compiler.py +++ b/test/dialect/oracle/test_compiler.py @@ -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), ) diff --git a/test/dialect/oracle/test_dialect.py b/test/dialect/oracle/test_dialect.py index cce33d4a26..554e5f18b4 100644 --- a/test/dialect/oracle/test_dialect.py +++ b/test/dialect/oracle/test_dialect.py @@ -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})