From 83f3e3da30701ddc4aaac8dc913d579e0031adba Mon Sep 17 00:00:00 2001 From: Rob Verschoor <91290800+robverschoor@users.noreply.github.com> Date: Fri, 26 Jan 2024 23:34:42 +0100 Subject: [PATCH] Support expressions in SELECT...OFFSET...FETCH clauses (#2298) In SELECT...OFFSET...FETCH, T-SQL allows expressions for the expression in OFFSET and FETCH. In Babelfish any expression involving an operator or function call requires that the expression is enclosed in brackets. This fix add those brackets as part of ANTLR rewriting. Signed-off-by: Rob Verschoor rcv@amazon.com Task: BABEL-1174 --- contrib/babelfishpg_tsql/src/tsqlIface.cpp | 42 +- ...ch_rows-before-15_6-or-16_2-vu-cleanup.out | 8 + ...ch_rows-before-15_6-or-16_2-vu-prepare.out | 8 + ...tch_rows-before-15_6-or-16_2-vu-verify.out | 73 ++++ .../order_by_offset_fetch_rows-vu-cleanup.out | 8 + .../order_by_offset_fetch_rows-vu-prepare.out | 16 + .../order_by_offset_fetch_rows-vu-verify.out | 390 ++++++++++++++++++ ...ch_rows-before-15_6-or-16_2-vu-cleanup.sql | 8 + ...ch_rows-before-15_6-or-16_2-vu-prepare.sql | 8 + ...tch_rows-before-15_6-or-16_2-vu-verify.sql | 15 + .../order_by_offset_fetch_rows-vu-cleanup.sql | 8 + .../order_by_offset_fetch_rows-vu-prepare.sql | 12 + .../order_by_offset_fetch_rows-vu-verify.sql | 78 ++++ test/JDBC/upgrade/13_4/schedule | 1 + test/JDBC/upgrade/13_5/schedule | 1 + test/JDBC/upgrade/13_7/schedule | 1 + test/JDBC/upgrade/13_8/schedule | 1 + test/JDBC/upgrade/13_9/schedule | 1 + test/JDBC/upgrade/14_10/schedule | 1 + test/JDBC/upgrade/14_11/schedule | 1 + test/JDBC/upgrade/14_3/schedule | 1 + test/JDBC/upgrade/14_5/schedule | 1 + test/JDBC/upgrade/14_6/schedule | 1 + test/JDBC/upgrade/14_7/schedule | 1 + test/JDBC/upgrade/14_8/schedule | 1 + test/JDBC/upgrade/14_9/schedule | 1 + test/JDBC/upgrade/15_1/schedule | 1 + test/JDBC/upgrade/15_2/schedule | 1 + test/JDBC/upgrade/15_3/schedule | 1 + test/JDBC/upgrade/15_4/schedule | 1 + test/JDBC/upgrade/15_5/schedule | 1 + test/JDBC/upgrade/latest/schedule | 1 + 32 files changed, 691 insertions(+), 2 deletions(-) create mode 100644 test/JDBC/expected/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-cleanup.out create mode 100644 test/JDBC/expected/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-prepare.out create mode 100644 test/JDBC/expected/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-verify.out create mode 100644 test/JDBC/expected/order_by_offset_fetch_rows-vu-cleanup.out create mode 100644 test/JDBC/expected/order_by_offset_fetch_rows-vu-prepare.out create mode 100644 test/JDBC/expected/order_by_offset_fetch_rows-vu-verify.out create mode 100644 test/JDBC/input/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-cleanup.sql create mode 100644 test/JDBC/input/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-prepare.sql create mode 100644 test/JDBC/input/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-verify.sql create mode 100644 test/JDBC/input/order_by_offset_fetch_rows-vu-cleanup.sql create mode 100644 test/JDBC/input/order_by_offset_fetch_rows-vu-prepare.sql create mode 100644 test/JDBC/input/order_by_offset_fetch_rows-vu-verify.sql diff --git a/contrib/babelfishpg_tsql/src/tsqlIface.cpp b/contrib/babelfishpg_tsql/src/tsqlIface.cpp index 8107c52560..6424235ffa 100644 --- a/contrib/babelfishpg_tsql/src/tsqlIface.cpp +++ b/contrib/babelfishpg_tsql/src/tsqlIface.cpp @@ -197,6 +197,7 @@ static void handleBitNotOperator(TSqlParser::Unary_op_exprContext *ctx); static void handleBitOperators(TSqlParser::Plus_minus_bit_exprContext *ctx); static void handleModuloOperator(TSqlParser::Mult_div_percent_exprContext *ctx); static void handleAtAtVarInPredicate(TSqlParser::PredicateContext *ctx); +static void handleOrderByOffsetFetch(TSqlParser::Order_by_clauseContext *ctx); /* * Structure / Utility function for general purpose of query string modification @@ -659,9 +660,14 @@ void PLtsql_expr_query_mutator::run() const std::u32string& orig_text = utf8_to_utf32(entry.second.first.c_str()); const std::u32string& repl_text = utf8_to_utf32(entry.second.second.c_str()); if (isSelectFragment) offset += fragment_SELECT_prefix.length(); // because this is an expression prefixed with 'SELECT ' - + if (orig_text.length() == 0 || orig_text.c_str(), query.substr(offset, orig_text.length()) == orig_text) // local_id maybe already deleted in some cases such as select-assignment. check here if it still exists) { + // Note: the test below does not work, and has never worked, because size_t will not be negative, + // and the result of the subtraction is also of type size_t. + // This test has been in the code since day 1. + // When making the test work, some test cases will start failing as they run into this condition + // (test table_variable_xact_errors and two variants). Therefore, not touching the test for now. if (offset - cursor < 0) throw PGErrorWrapperException(ERROR, ERRCODE_INTERNAL_ERROR, "can't mutate an internal query. might be due to multiple mutations on the same position", 0, 0); if (offset - cursor > 0) // if offset==cursor, no need to copy @@ -2281,7 +2287,12 @@ class tsqlBuilder : public tsqlCommonMutator } } } - + + void exitOrder_by_clause(TSqlParser::Order_by_clauseContext *ctx) override + { + handleOrderByOffsetFetch(ctx); + } + // NB: the following are copied in tsqlMutator void exitColumn_def_table_constraints(TSqlParser::Column_def_table_constraintsContext *ctx) { @@ -8542,3 +8553,30 @@ void handleAtAtVarInPredicate(TSqlParser::PredicateContext *ctx) } return; } + +static void +handleOrderByOffsetFetch(TSqlParser::Order_by_clauseContext *ctx) +{ + // Add brackets around the expressions for OFFSET..ROWS and FETCH..ROWS + + if (ctx->offset_exp) + { + // Do not rewrite the entire expression since that will break the logic in the mutator when there is something inside the + // expression that also needs rewriting (like a local variable @p which needs to be rewritten as "@p"). + // Instead, insert an opening and closing bracket in the right places. + // Also, do not add a rewrite at the start position of the expression since there may be an '@' for a local var + // at that position and the rewrite to double-quote the variable will be lost as a result. + rewritten_query_fragment.emplace(std::make_pair((ctx->offset_exp->start->getStartIndex() - 1), std::make_pair("", " ("))); + rewritten_query_fragment.emplace(std::make_pair((ctx->offset_exp->stop->getStopIndex() + 1), std::make_pair("", ") "))); + } + + if (ctx->fetch_exp) + { + // See comment for offset_exp above. + rewritten_query_fragment.emplace(std::make_pair((ctx->fetch_exp->start->getStartIndex() - 1), std::make_pair("", " ("))); + rewritten_query_fragment.emplace(std::make_pair((ctx->fetch_exp->stop->getStopIndex() + 1), std::make_pair("", ") "))); + } + + return; +} + diff --git a/test/JDBC/expected/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-cleanup.out b/test/JDBC/expected/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-cleanup.out new file mode 100644 index 0000000000..cd290408dc --- /dev/null +++ b/test/JDBC/expected/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-cleanup.out @@ -0,0 +1,8 @@ +DROP PROC p1_upgr_order_by_offset_fetch +go +DROP FUNCTION f1_upgr_order_by_offset_fetch +go +DROP VIEW v1_upgr_order_by_offset_fetch +go +DROP table t1_upgr_order_by_offset_fetch +go diff --git a/test/JDBC/expected/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-prepare.out b/test/JDBC/expected/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-prepare.out new file mode 100644 index 0000000000..9e6807a825 --- /dev/null +++ b/test/JDBC/expected/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-prepare.out @@ -0,0 +1,8 @@ +create table t1_upgr_order_by_offset_fetch(a int, b int) +go +CREATE PROC p1_upgr_order_by_offset_fetch @p int=1,@q int=1 AS SELECT * FROM t1_upgr_order_by_offset_fetch ORDER BY b OFFSET @p*@q ROWS FETCH NEXT square(@q)+1 ROWS ONLY +go +CREATE FUNCTION f1_upgr_order_by_offset_fetch(@p int, @q int) returns int as begin declare @v int SELECT @v=count(a) FROM t1_upgr_order_by_offset_fetch group by b ORDER BY b OFFSET @p*@q ROWS FETCH NEXT square(@q)+1 ROWS ONLY return @v end +go +CREATE VIEW v1_upgr_order_by_offset_fetch as select * FROM t1_upgr_order_by_offset_fetch ORDER BY b OFFSET 5 ROWS FETCH NEXT 3 rows only +go diff --git a/test/JDBC/expected/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-verify.out b/test/JDBC/expected/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-verify.out new file mode 100644 index 0000000000..88e2c4207a --- /dev/null +++ b/test/JDBC/expected/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-verify.out @@ -0,0 +1,73 @@ +insert t1_upgr_order_by_offset_fetch select generate_series, 0 from generate_series(1,100) +go +~~ROW COUNT: 100~~ + +update t1_upgr_order_by_offset_fetch set b=a +go +~~ROW COUNT: 100~~ + +exec p1_upgr_order_by_offset_fetch 1, 3 +go +~~START~~ +int#!#int +4#!#4 +5#!#5 +6#!#6 +7#!#7 +8#!#8 +9#!#9 +10#!#10 +11#!#11 +12#!#12 +13#!#13 +~~END~~ + +exec p1_upgr_order_by_offset_fetch 2, 3 +go +~~START~~ +int#!#int +7#!#7 +8#!#8 +9#!#9 +10#!#10 +11#!#11 +12#!#12 +13#!#13 +14#!#14 +15#!#15 +16#!#16 +~~END~~ + +p1_upgr_order_by_offset_fetch 3, 3 +go +~~START~~ +int#!#int +10#!#10 +11#!#11 +12#!#12 +13#!#13 +14#!#14 +15#!#15 +16#!#16 +17#!#17 +18#!#18 +19#!#19 +~~END~~ + +select dbo.f1_upgr_order_by_offset_fetch(1,1) +go +~~START~~ +int +1 +~~END~~ + +select * from v1_upgr_order_by_offset_fetch +go +~~START~~ +int#!#int +6#!#6 +7#!#7 +8#!#8 +~~END~~ + + diff --git a/test/JDBC/expected/order_by_offset_fetch_rows-vu-cleanup.out b/test/JDBC/expected/order_by_offset_fetch_rows-vu-cleanup.out new file mode 100644 index 0000000000..de82fbfa56 --- /dev/null +++ b/test/JDBC/expected/order_by_offset_fetch_rows-vu-cleanup.out @@ -0,0 +1,8 @@ +DROP PROC p1_order_by_offset_fetch +go +DROP FUNCTION f1_order_by_offset_fetch +go +DROP VIEW v1_order_by_offset_fetch +go +DROP table t1_order_by_offset_fetch +go diff --git a/test/JDBC/expected/order_by_offset_fetch_rows-vu-prepare.out b/test/JDBC/expected/order_by_offset_fetch_rows-vu-prepare.out new file mode 100644 index 0000000000..c7ad5f1556 --- /dev/null +++ b/test/JDBC/expected/order_by_offset_fetch_rows-vu-prepare.out @@ -0,0 +1,16 @@ +create table t1_order_by_offset_fetch(a int, b int) +go +insert t1_order_by_offset_fetch select generate_series, 0 from generate_series(1,100) +go +~~ROW COUNT: 100~~ + +update t1_order_by_offset_fetch set b=a +go +~~ROW COUNT: 100~~ + +CREATE PROC p1_order_by_offset_fetch @p int=1,@q int=1 AS SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*@q ROWS FETCH NEXT square(@q)+1 ROWS ONLY +go +CREATE FUNCTION f1_order_by_offset_fetch(@p int, @q int) returns int as begin declare @v int SELECT @v=count(a) FROM t1_order_by_offset_fetch group by b ORDER BY b OFFSET @p*@q ROWS FETCH NEXT square(@q)+1 ROWS ONLY return @v end +go +CREATE VIEW v1_order_by_offset_fetch as select * FROM t1_order_by_offset_fetch ORDER BY b OFFSET 5 ROWS FETCH NEXT 3 rows only +go diff --git a/test/JDBC/expected/order_by_offset_fetch_rows-vu-verify.out b/test/JDBC/expected/order_by_offset_fetch_rows-vu-verify.out new file mode 100644 index 0000000000..2a69ad9437 --- /dev/null +++ b/test/JDBC/expected/order_by_offset_fetch_rows-vu-verify.out @@ -0,0 +1,390 @@ +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET (@p*5) ROWS FETCH NEXT (5) ROWS ONLY +go +~~START~~ +int#!#int +11#!#11 +12#!#12 +13#!#13 +14#!#14 +15#!#15 +~~END~~ + +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET (@p*5) ROW FETCH NEXT 5 ROW ONLY +go +~~START~~ +int#!#int +11#!#11 +12#!#12 +13#!#13 +14#!#14 +15#!#15 +~~END~~ + +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET (@p*5) ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +11#!#11 +12#!#12 +13#!#13 +~~END~~ + +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET (@p*5) ROWS FETCH NEXT 1+2 ROWS ONLY +go +~~START~~ +int#!#int +11#!#11 +12#!#12 +13#!#13 +~~END~~ + +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET (@p*5) ROWS FETCH NEXT (1+2) ROWS ONLY +go +~~START~~ +int#!#int +11#!#11 +12#!#12 +13#!#13 +~~END~~ + +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p ROWS FETCH NEXT +3 ROWS ONLY +go +~~START~~ +int#!#int +3#!#3 +4#!#4 +5#!#5 +~~END~~ + +declare @p int =2,@q int=0 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +~~END~~ + +declare @p int =0,@q int=1 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +1#!#1 +~~END~~ + +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +3#!#3 +4#!#4 +5#!#5 +~~END~~ + +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET 1+1 ROWS FETCH NEXT 2 ROWS ONLY +go +~~START~~ +int#!#int +3#!#3 +4#!#4 +~~END~~ + +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET 1+1 ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +3#!#3 +4#!#4 +5#!#5 +~~END~~ + +declare @p int =2,@q int=0 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*1 ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +~~END~~ + +declare @p int =2,@q int=1 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*1 ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +3#!#3 +~~END~~ + +declare @p int =0,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*1 ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +1#!#1 +2#!#2 +3#!#3 +~~END~~ + +declare @p int =1,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*1 ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +2#!#2 +3#!#3 +4#!#4 +~~END~~ + +declare @p int =3,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p+1 ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +5#!#5 +6#!#6 +7#!#7 +~~END~~ + +declare @p int =3,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*2 ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +7#!#7 +8#!#8 +9#!#9 +~~END~~ + +declare @p int =1,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*@q ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +4#!#4 +5#!#5 +6#!#6 +~~END~~ + +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*@q ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +7#!#7 +8#!#8 +9#!#9 +~~END~~ + +declare @p int=2,@q int=0 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*@q ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +~~END~~ + +declare @p int=1,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*@q ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +4#!#4 +5#!#5 +6#!#6 +~~END~~ + +declare @p int=2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*@q ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +7#!#7 +8#!#8 +9#!#9 +~~END~~ + +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET square(2) ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +5#!#5 +6#!#6 +7#!#7 +~~END~~ + +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET square(1)*@p ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +3#!#3 +4#!#4 +5#!#5 +~~END~~ + +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET square(@p)+@p ROWS FETCH NEXT @q+1 ROWS ONLY +go +~~START~~ +int#!#int +7#!#7 +8#!#8 +9#!#9 +10#!#10 +~~END~~ + +declare @p int=2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*1 ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +3#!#3 +4#!#4 +5#!#5 +~~END~~ + +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET square(1)*@p+1 ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +4#!#4 +5#!#5 +6#!#6 +~~END~~ + +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET square(1)*(@p) ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +3#!#3 +4#!#4 +5#!#5 +~~END~~ + +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET square(1)*@p ROWS FETCH NEXT @q ROWS ONLY +go +~~START~~ +int#!#int +3#!#3 +4#!#4 +5#!#5 +~~END~~ + +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*square(1) ROWS FETCH NEXT @q*square(1) ROWS ONLY +go +~~START~~ +int#!#int +3#!#3 +4#!#4 +5#!#5 +~~END~~ + +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET square(1)*@p ROWS FETCH NEXT square(1)+@q ROWS ONLY +go +~~START~~ +int#!#int +3#!#3 +4#!#4 +5#!#5 +6#!#6 +~~END~~ + +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET square(1)* @p ROWS FETCH NEXT square(@q)*@q+1 ROWS ONLY +go +~~START~~ +int#!#int +3#!#3 +4#!#4 +5#!#5 +6#!#6 +7#!#7 +8#!#8 +9#!#9 +10#!#10 +11#!#11 +12#!#12 +13#!#13 +14#!#14 +15#!#15 +16#!#16 +17#!#17 +18#!#18 +19#!#19 +20#!#20 +21#!#21 +22#!#22 +23#!#23 +24#!#24 +25#!#25 +26#!#26 +27#!#27 +28#!#28 +29#!#29 +30#!#30 +~~END~~ + +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET square(1)*3+1 ROWS FETCH NEXT @p+1 ROWS ONLY +go +~~START~~ +int#!#int +5#!#5 +6#!#6 +7#!#7 +~~END~~ + +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET (@p*@p) ROWS FETCH NEXT @p*@q ROWS ONLY +go +~~START~~ +int#!#int +5#!#5 +6#!#6 +7#!#7 +8#!#8 +9#!#9 +10#!#10 +~~END~~ + +exec p1_order_by_offset_fetch 1, 3 +go +~~START~~ +int#!#int +4#!#4 +5#!#5 +6#!#6 +7#!#7 +8#!#8 +9#!#9 +10#!#10 +11#!#11 +12#!#12 +13#!#13 +~~END~~ + +exec p1_order_by_offset_fetch 2, 3 +go +~~START~~ +int#!#int +7#!#7 +8#!#8 +9#!#9 +10#!#10 +11#!#11 +12#!#12 +13#!#13 +14#!#14 +15#!#15 +16#!#16 +~~END~~ + +p1_order_by_offset_fetch 3, 3 +go +~~START~~ +int#!#int +10#!#10 +11#!#11 +12#!#12 +13#!#13 +14#!#14 +15#!#15 +16#!#16 +17#!#17 +18#!#18 +19#!#19 +~~END~~ + +select dbo.f1_order_by_offset_fetch(1,1) +go +~~START~~ +int +1 +~~END~~ + +select * from v1_order_by_offset_fetch +go +~~START~~ +int#!#int +6#!#6 +7#!#7 +8#!#8 +~~END~~ + diff --git a/test/JDBC/input/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-cleanup.sql b/test/JDBC/input/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-cleanup.sql new file mode 100644 index 0000000000..6025c74357 --- /dev/null +++ b/test/JDBC/input/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-cleanup.sql @@ -0,0 +1,8 @@ +DROP PROC p1_upgr_order_by_offset_fetch +go +DROP FUNCTION f1_upgr_order_by_offset_fetch +go +DROP VIEW v1_upgr_order_by_offset_fetch +go +DROP table t1_upgr_order_by_offset_fetch +go \ No newline at end of file diff --git a/test/JDBC/input/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-prepare.sql b/test/JDBC/input/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-prepare.sql new file mode 100644 index 0000000000..ca1a86b3b6 --- /dev/null +++ b/test/JDBC/input/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-prepare.sql @@ -0,0 +1,8 @@ +create table t1_upgr_order_by_offset_fetch(a int, b int) +go +CREATE PROC p1_upgr_order_by_offset_fetch @p int=1,@q int=1 AS SELECT * FROM t1_upgr_order_by_offset_fetch ORDER BY b OFFSET @p*@q ROWS FETCH NEXT square(@q)+1 ROWS ONLY +go +CREATE FUNCTION f1_upgr_order_by_offset_fetch(@p int, @q int) returns int as begin declare @v int SELECT @v=count(a) FROM t1_upgr_order_by_offset_fetch group by b ORDER BY b OFFSET @p*@q ROWS FETCH NEXT square(@q)+1 ROWS ONLY return @v end +go +CREATE VIEW v1_upgr_order_by_offset_fetch as select * FROM t1_upgr_order_by_offset_fetch ORDER BY b OFFSET 5 ROWS FETCH NEXT 3 rows only +go diff --git a/test/JDBC/input/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-verify.sql b/test/JDBC/input/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-verify.sql new file mode 100644 index 0000000000..3866ab3e71 --- /dev/null +++ b/test/JDBC/input/order_by_offset_fetch_rows-before-15_6-or-16_2-vu-verify.sql @@ -0,0 +1,15 @@ +insert t1_upgr_order_by_offset_fetch select generate_series, 0 from generate_series(1,100) +go +update t1_upgr_order_by_offset_fetch set b=a +go +exec p1_upgr_order_by_offset_fetch 1, 3 +go +exec p1_upgr_order_by_offset_fetch 2, 3 +go +p1_upgr_order_by_offset_fetch 3, 3 +go +select dbo.f1_upgr_order_by_offset_fetch(1,1) +go +select * from v1_upgr_order_by_offset_fetch +go + diff --git a/test/JDBC/input/order_by_offset_fetch_rows-vu-cleanup.sql b/test/JDBC/input/order_by_offset_fetch_rows-vu-cleanup.sql new file mode 100644 index 0000000000..a91f430520 --- /dev/null +++ b/test/JDBC/input/order_by_offset_fetch_rows-vu-cleanup.sql @@ -0,0 +1,8 @@ +DROP PROC p1_order_by_offset_fetch +go +DROP FUNCTION f1_order_by_offset_fetch +go +DROP VIEW v1_order_by_offset_fetch +go +DROP table t1_order_by_offset_fetch +go \ No newline at end of file diff --git a/test/JDBC/input/order_by_offset_fetch_rows-vu-prepare.sql b/test/JDBC/input/order_by_offset_fetch_rows-vu-prepare.sql new file mode 100644 index 0000000000..4235ed4709 --- /dev/null +++ b/test/JDBC/input/order_by_offset_fetch_rows-vu-prepare.sql @@ -0,0 +1,12 @@ +create table t1_order_by_offset_fetch(a int, b int) +go +insert t1_order_by_offset_fetch select generate_series, 0 from generate_series(1,100) +go +update t1_order_by_offset_fetch set b=a +go +CREATE PROC p1_order_by_offset_fetch @p int=1,@q int=1 AS SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*@q ROWS FETCH NEXT square(@q)+1 ROWS ONLY +go +CREATE FUNCTION f1_order_by_offset_fetch(@p int, @q int) returns int as begin declare @v int SELECT @v=count(a) FROM t1_order_by_offset_fetch group by b ORDER BY b OFFSET @p*@q ROWS FETCH NEXT square(@q)+1 ROWS ONLY return @v end +go +CREATE VIEW v1_order_by_offset_fetch as select * FROM t1_order_by_offset_fetch ORDER BY b OFFSET 5 ROWS FETCH NEXT 3 rows only +go diff --git a/test/JDBC/input/order_by_offset_fetch_rows-vu-verify.sql b/test/JDBC/input/order_by_offset_fetch_rows-vu-verify.sql new file mode 100644 index 0000000000..2bb09eed60 --- /dev/null +++ b/test/JDBC/input/order_by_offset_fetch_rows-vu-verify.sql @@ -0,0 +1,78 @@ +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET (@p*5) ROWS FETCH NEXT (5) ROWS ONLY +go +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET (@p*5) ROW FETCH NEXT 5 ROW ONLY +go +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET (@p*5) ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET (@p*5) ROWS FETCH NEXT 1+2 ROWS ONLY +go +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET (@p*5) ROWS FETCH NEXT (1+2) ROWS ONLY +go +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p ROWS FETCH NEXT +3 ROWS ONLY +go +declare @p int =2,@q int=0 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int =0,@q int=1 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET 1+1 ROWS FETCH NEXT 2 ROWS ONLY +go +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET 1+1 ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int =2,@q int=0 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*1 ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int =2,@q int=1 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*1 ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int =0,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*1 ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int =1,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*1 ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int =3,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p+1 ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int =3,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*2 ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int =1,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*@q ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*@q ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int=2,@q int=0 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*@q ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int=1,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*@q ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int=2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*@q ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET square(2) ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET square(1)*@p ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET square(@p)+@p ROWS FETCH NEXT @q+1 ROWS ONLY +go +declare @p int=2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*1 ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET square(1)*@p+1 ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET square(1)*(@p) ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET square(1)*@p ROWS FETCH NEXT @q ROWS ONLY +go +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET @p*square(1) ROWS FETCH NEXT @q*square(1) ROWS ONLY +go +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET square(1)*@p ROWS FETCH NEXT square(1)+@q ROWS ONLY +go +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET square(1)* @p ROWS FETCH NEXT square(@q)*@q+1 ROWS ONLY +go +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET square(1)*3+1 ROWS FETCH NEXT @p+1 ROWS ONLY +go +declare @p int =2,@q int=3 SELECT * FROM t1_order_by_offset_fetch ORDER BY b OFFSET (@p*@p) ROWS FETCH NEXT @p*@q ROWS ONLY +go +exec p1_order_by_offset_fetch 1, 3 +go +exec p1_order_by_offset_fetch 2, 3 +go +p1_order_by_offset_fetch 3, 3 +go +select dbo.f1_order_by_offset_fetch(1,1) +go +select * from v1_order_by_offset_fetch +go \ No newline at end of file diff --git a/test/JDBC/upgrade/13_4/schedule b/test/JDBC/upgrade/13_4/schedule index 98ecaa3ea6..48f0dba0e1 100644 --- a/test/JDBC/upgrade/13_4/schedule +++ b/test/JDBC/upgrade/13_4/schedule @@ -220,6 +220,7 @@ table_constraint_wo_comma-before-15_6-or-16_2 getdate AUTO_ANALYZE-before-15-5-or-14-10 cast_eliminate +order_by_offset_fetch_rows-before-15_6-or-16_2 TestDatatypeAggSort babel_index_nulls_order-before-15-5 operator_binary_whitespace-before-15_6-or-16_2 diff --git a/test/JDBC/upgrade/13_5/schedule b/test/JDBC/upgrade/13_5/schedule index 762089aef0..0c2ca640b3 100644 --- a/test/JDBC/upgrade/13_5/schedule +++ b/test/JDBC/upgrade/13_5/schedule @@ -273,6 +273,7 @@ table_constraint_wo_comma-before-15_6-or-16_2 getdate AUTO_ANALYZE-before-15-5-or-14-10 cast_eliminate +order_by_offset_fetch_rows-before-15_6-or-16_2 TestDatatypeAggSort babel_index_nulls_order-before-15-5 operator_binary_whitespace-before-15_6-or-16_2 diff --git a/test/JDBC/upgrade/13_7/schedule b/test/JDBC/upgrade/13_7/schedule index 64f1065ce1..f6624a44e1 100644 --- a/test/JDBC/upgrade/13_7/schedule +++ b/test/JDBC/upgrade/13_7/schedule @@ -321,6 +321,7 @@ table_constraint_wo_comma-before-15_6-or-16_2 getdate AUTO_ANALYZE-before-15-5-or-14-10 cast_eliminate +order_by_offset_fetch_rows-before-15_6-or-16_2 TestDatatypeAggSort babel_index_nulls_order-before-15-5 operator_binary_whitespace-before-15_6-or-16_2 diff --git a/test/JDBC/upgrade/13_8/schedule b/test/JDBC/upgrade/13_8/schedule index 64f1065ce1..f6624a44e1 100644 --- a/test/JDBC/upgrade/13_8/schedule +++ b/test/JDBC/upgrade/13_8/schedule @@ -321,6 +321,7 @@ table_constraint_wo_comma-before-15_6-or-16_2 getdate AUTO_ANALYZE-before-15-5-or-14-10 cast_eliminate +order_by_offset_fetch_rows-before-15_6-or-16_2 TestDatatypeAggSort babel_index_nulls_order-before-15-5 operator_binary_whitespace-before-15_6-or-16_2 diff --git a/test/JDBC/upgrade/13_9/schedule b/test/JDBC/upgrade/13_9/schedule index b564d4b46f..ea6e3ca61c 100644 --- a/test/JDBC/upgrade/13_9/schedule +++ b/test/JDBC/upgrade/13_9/schedule @@ -324,6 +324,7 @@ getdate BABEL-4410 AUTO_ANALYZE-before-15-5-or-14-10 cast_eliminate +order_by_offset_fetch_rows-before-15_6-or-16_2 TestDatatypeAggSort babel_index_nulls_order-before-15-5 operator_binary_whitespace-before-15_6-or-16_2 diff --git a/test/JDBC/upgrade/14_10/schedule b/test/JDBC/upgrade/14_10/schedule index b3554f68e4..84343ef7cf 100644 --- a/test/JDBC/upgrade/14_10/schedule +++ b/test/JDBC/upgrade/14_10/schedule @@ -414,6 +414,7 @@ BABEL-4384 default_params BABEL-3326 cast_eliminate +order_by_offset_fetch_rows-before-15_6-or-16_2 TestDatatypeAggSort babel_index_nulls_order-before-15-5 operator_binary_whitespace-before-15_6-or-16_2 diff --git a/test/JDBC/upgrade/14_11/schedule b/test/JDBC/upgrade/14_11/schedule index f2f2ed05e6..fd887c507d 100644 --- a/test/JDBC/upgrade/14_11/schedule +++ b/test/JDBC/upgrade/14_11/schedule @@ -414,6 +414,7 @@ BABEL-4384 default_params BABEL-3326 cast_eliminate +order_by_offset_fetch_rows-before-15_6-or-16_2 TestDatatypeAggSort babel_index_nulls_order-before-15-5 operator_binary_whitespace-before-15_6-or-16_2 diff --git a/test/JDBC/upgrade/14_3/schedule b/test/JDBC/upgrade/14_3/schedule index 28c16b482a..8105025dce 100644 --- a/test/JDBC/upgrade/14_3/schedule +++ b/test/JDBC/upgrade/14_3/schedule @@ -342,6 +342,7 @@ BABEL-2619 BABEL-4410 AUTO_ANALYZE-before-15-5-or-14-10 cast_eliminate +order_by_offset_fetch_rows-before-15_6-or-16_2 TestDatatypeAggSort babel_index_nulls_order-before-15-5 operator_binary_whitespace-before-15_6-or-16_2 diff --git a/test/JDBC/upgrade/14_5/schedule b/test/JDBC/upgrade/14_5/schedule index c5bc5d08d8..297e18b42c 100644 --- a/test/JDBC/upgrade/14_5/schedule +++ b/test/JDBC/upgrade/14_5/schedule @@ -357,6 +357,7 @@ BABEL-2619 BABEL-4410 AUTO_ANALYZE-before-15-5-or-14-10 cast_eliminate +order_by_offset_fetch_rows-before-15_6-or-16_2 TestDatatypeAggSort babel_index_nulls_order-before-15-5 operator_binary_whitespace-before-15_6-or-16_2 diff --git a/test/JDBC/upgrade/14_6/schedule b/test/JDBC/upgrade/14_6/schedule index 7d3768032f..94c9913a73 100644 --- a/test/JDBC/upgrade/14_6/schedule +++ b/test/JDBC/upgrade/14_6/schedule @@ -392,6 +392,7 @@ BABEL-2619 BABEL-4410 AUTO_ANALYZE-before-15-5-or-14-10 cast_eliminate +order_by_offset_fetch_rows-before-15_6-or-16_2 TestDatatypeAggSort babel_index_nulls_order-before-15-5 operator_binary_whitespace-before-15_6-or-16_2 diff --git a/test/JDBC/upgrade/14_7/schedule b/test/JDBC/upgrade/14_7/schedule index 3434f42cfd..73651ed96f 100644 --- a/test/JDBC/upgrade/14_7/schedule +++ b/test/JDBC/upgrade/14_7/schedule @@ -413,6 +413,7 @@ BABEL_4330 BABEL-2619 AUTO_ANALYZE-before-15-5-or-14-10 cast_eliminate +order_by_offset_fetch_rows-before-15_6-or-16_2 TestDatatypeAggSort babel_index_nulls_order-before-15-5 operator_binary_whitespace-before-15_6-or-16_2 diff --git a/test/JDBC/upgrade/14_8/schedule b/test/JDBC/upgrade/14_8/schedule index 187d3bb373..d4cdd4339f 100644 --- a/test/JDBC/upgrade/14_8/schedule +++ b/test/JDBC/upgrade/14_8/schedule @@ -412,6 +412,7 @@ BABEL-2619 AUTO_ANALYZE-before-15-5-or-14-10 default_params cast_eliminate +order_by_offset_fetch_rows-before-15_6-or-16_2 TestDatatypeAggSort babel_index_nulls_order-before-15-5 operator_binary_whitespace-before-15_6-or-16_2 diff --git a/test/JDBC/upgrade/14_9/schedule b/test/JDBC/upgrade/14_9/schedule index 9ce8087217..51710ab12d 100644 --- a/test/JDBC/upgrade/14_9/schedule +++ b/test/JDBC/upgrade/14_9/schedule @@ -414,6 +414,7 @@ BABEL-4410 AUTO_ANALYZE-before-15-5-or-14-10 default_params cast_eliminate +order_by_offset_fetch_rows-before-15_6-or-16_2 TestDatatypeAggSort babel_index_nulls_order-before-15-5 operator_binary_whitespace-before-15_6-or-16_2 diff --git a/test/JDBC/upgrade/15_1/schedule b/test/JDBC/upgrade/15_1/schedule index 90de977840..00bb70e4ad 100644 --- a/test/JDBC/upgrade/15_1/schedule +++ b/test/JDBC/upgrade/15_1/schedule @@ -391,6 +391,7 @@ BABEL_4330 AUTO_ANALYZE-before-15-5-or-14-10 default_params cast_eliminate +order_by_offset_fetch_rows-before-15_6-or-16_2 TestDatatypeAggSort babel_index_nulls_order-before-15-5 operator_binary_whitespace-before-15_6-or-16_2 diff --git a/test/JDBC/upgrade/15_2/schedule b/test/JDBC/upgrade/15_2/schedule index cf8286beee..90ec8fbc33 100644 --- a/test/JDBC/upgrade/15_2/schedule +++ b/test/JDBC/upgrade/15_2/schedule @@ -420,6 +420,7 @@ BABEL-4410 AUTO_ANALYZE-before-15-5-or-14-10 default_params cast_eliminate +order_by_offset_fetch_rows-before-15_6-or-16_2 TestDatatypeAggSort babel_index_nulls_order-before-15-5 operator_binary_whitespace-before-15_6-or-16_2 diff --git a/test/JDBC/upgrade/15_3/schedule b/test/JDBC/upgrade/15_3/schedule index 550386ae47..33af068127 100644 --- a/test/JDBC/upgrade/15_3/schedule +++ b/test/JDBC/upgrade/15_3/schedule @@ -443,6 +443,7 @@ BABEL_4330 AUTO_ANALYZE-before-15-5-or-14-10 default_params cast_eliminate +order_by_offset_fetch_rows-before-15_6-or-16_2 TestDatatypeAggSort babel_index_nulls_order-before-15-5 operator_binary_whitespace-before-15_6-or-16_2 diff --git a/test/JDBC/upgrade/15_4/schedule b/test/JDBC/upgrade/15_4/schedule index 921ca9a0e0..7e2ab7b630 100644 --- a/test/JDBC/upgrade/15_4/schedule +++ b/test/JDBC/upgrade/15_4/schedule @@ -455,6 +455,7 @@ BABEL-4410 AUTO_ANALYZE-before-15-5-or-14-10 default_params cast_eliminate +order_by_offset_fetch_rows-before-15_6-or-16_2 TestDatatypeAggSort babel_index_nulls_order-before-15-5 operator_binary_whitespace-before-15_6-or-16_2 diff --git a/test/JDBC/upgrade/15_5/schedule b/test/JDBC/upgrade/15_5/schedule index 8646c2bee8..5c1ebb3d65 100644 --- a/test/JDBC/upgrade/15_5/schedule +++ b/test/JDBC/upgrade/15_5/schedule @@ -482,6 +482,7 @@ BABEL-4484 pivot #AUTO_ANALYZE #uncomment this test when preparing for new minor version cast_eliminate +order_by_offset_fetch_rows-before-15_6-or-16_2 TestDatatypeAggSort babel_index_nulls_order operator_binary_whitespace-before-15_6-or-16_2 diff --git a/test/JDBC/upgrade/latest/schedule b/test/JDBC/upgrade/latest/schedule index 9b2e41e562..6a247edab0 100644 --- a/test/JDBC/upgrade/latest/schedule +++ b/test/JDBC/upgrade/latest/schedule @@ -461,6 +461,7 @@ TestXML timefromparts todatetimeoffset-dep triggers_with_transaction +order_by_offset_fetch_rows typeid-typename typeid-typename-dep print_null