From 6e6886cc6433ba44ae9decf829d5b176ed46b372 Mon Sep 17 00:00:00 2001 From: lukeman Date: Sat, 19 Apr 2025 18:58:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86issue=EF=BC=9AD=E5=BA=93?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=80=A7select=E8=AF=AD=E5=8F=A5=E4=BD=BF?= =?UTF-8?q?=E7=94=A8TOP=20N/M=20percent=E7=BB=93=E5=90=88offset=E5=85=B3?= =?UTF-8?q?=E9=94=AE=E5=AD=97=E4=B8=8D=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contrib/shark/expected/test_ddl_and_dml.out | 61 +++++++++++++++++++++ contrib/shark/sql/test_ddl_and_dml.sql | 14 +++++ src/common/backend/parser/gram.y | 8 +++ 3 files changed, 83 insertions(+) diff --git a/contrib/shark/expected/test_ddl_and_dml.out b/contrib/shark/expected/test_ddl_and_dml.out index 8fca83f243..9a2294aa2b 100644 --- a/contrib/shark/expected/test_ddl_and_dml.out +++ b/contrib/shark/expected/test_ddl_and_dml.out @@ -113,6 +113,67 @@ ERROR: Percent values must be between 0 and 100. --报错:WITH TIES必须和order by子句同时使用 select TOP (select 10) WITH TIES * from Products; ERROR: The WITH TIES clause is not allowed without a corresponding ORDER BY clause. +-- test for issue #IC1VD6 +select * from Products ORDER BY qtyavailable; + qtyavailable | unitprice | inventoryvalue +--------------+-----------+---------------- + 10 | $1.50 | $15.00 + 10 | $1.50 | $15.00 + 10 | $1.50 | $15.00 + 25 | $2.00 | $50.00 + 25 | $2.00 | $50.00 +(5 rows) + +select TOP 1 * from Products ORDER BY qtyavailable; + qtyavailable | unitprice | inventoryvalue +--------------+-----------+---------------- + 10 | $1.50 | $15.00 +(1 row) + +--- expect success +select * from Products ORDER BY qtyavailable offset 2 rows; + qtyavailable | unitprice | inventoryvalue +--------------+-----------+---------------- + 10 | $1.50 | $15.00 + 25 | $2.00 | $50.00 + 25 | $2.00 | $50.00 +(3 rows) + +select * from Products ORDER BY qtyavailable fetch first 1 rows only; + qtyavailable | unitprice | inventoryvalue +--------------+-----------+---------------- + 10 | $1.50 | $15.00 +(1 row) + +select * from Products ORDER BY qtyavailable offset 3 rows fetch first 1 rows only; + qtyavailable | unitprice | inventoryvalue +--------------+-----------+---------------- + 25 | $2.00 | $50.00 +(1 row) + +select * from Products ORDER BY qtyavailable fetch first 1 rows only offset 3 rows; + qtyavailable | unitprice | inventoryvalue +--------------+-----------+---------------- + 25 | $2.00 | $50.00 +(1 row) + +--- expect failed +select TOP 1 * from Products ORDER BY qtyavailable offset 2 rows; +ERROR: A TOP clause cannot be used together with an OFFSET clause in D-format database +LINE 1: ...t TOP 1 * from Products ORDER BY qtyavailable offset 2 rows; + ^ +select TOP 1 * from Products ORDER BY qtyavailable fetch first 1 rows only; +ERROR: multiple LIMIT clauses not allowed +LINE 1: ...* from Products ORDER BY qtyavailable fetch first 1 rows onl... + ^ +select TOP 1 * from Products ORDER BY qtyavailable offset 3 rows fetch first 1 rows only; +ERROR: A TOP clause cannot be used together with an OFFSET clause in D-format database +LINE 1: ...OP 1 * from Products ORDER BY qtyavailable offset 3 rows fet... + ^ +select TOP 1 * from Products ORDER BY qtyavailable fetch first 1 rows only offset 3 rows; +ERROR: A TOP clause cannot be used together with an OFFSET clause in D-format database +LINE 1: ...ORDER BY qtyavailable fetch first 1 rows only offset 3 rows; + ^ DROP TABLE IF EXISTS Products; -- case 3: remove postfix operator -- 报错:移除后缀运算符 diff --git a/contrib/shark/sql/test_ddl_and_dml.sql b/contrib/shark/sql/test_ddl_and_dml.sql index 45a2a82aaf..925edc4769 100644 --- a/contrib/shark/sql/test_ddl_and_dml.sql +++ b/contrib/shark/sql/test_ddl_and_dml.sql @@ -64,6 +64,20 @@ select TOP 200 PERCENT * from Products; --报错:WITH TIES必须和order by子句同时使用 select TOP (select 10) WITH TIES * from Products; +-- test for issue #IC1VD6 +select * from Products ORDER BY qtyavailable; +select TOP 1 * from Products ORDER BY qtyavailable; +--- expect success +select * from Products ORDER BY qtyavailable offset 2 rows; +select * from Products ORDER BY qtyavailable fetch first 1 rows only; +select * from Products ORDER BY qtyavailable offset 3 rows fetch first 1 rows only; +select * from Products ORDER BY qtyavailable fetch first 1 rows only offset 3 rows; +--- expect failed +select TOP 1 * from Products ORDER BY qtyavailable offset 2 rows; +select TOP 1 * from Products ORDER BY qtyavailable fetch first 1 rows only; +select TOP 1 * from Products ORDER BY qtyavailable offset 3 rows fetch first 1 rows only; +select TOP 1 * from Products ORDER BY qtyavailable fetch first 1 rows only offset 3 rows; + DROP TABLE IF EXISTS Products; -- case 3: remove postfix operator diff --git a/src/common/backend/parser/gram.y b/src/common/backend/parser/gram.y index 01643038ac..dab66b56ac 100644 --- a/src/common/backend/parser/gram.y +++ b/src/common/backend/parser/gram.y @@ -32927,6 +32927,14 @@ insertSelectOptions(SelectStmt *stmt, parser_errposition(exprLocation(limitOffset)))); } stmt->limitOffset = limitOffset; + if (DB_IS_CMPT(D_FORMAT) && stmt->limitCount) { + const char* message = "A TOP clause cannot be used together with an OFFSET clause in D-format database"; + InsertErrorMessage(message, u_sess->plsql_cxt.plpgsql_yylloc); + ereport(errstate, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("A TOP clause cannot be used together with an OFFSET clause in D-format database"), + parser_errposition(exprLocation(limitOffset)))); + } } if (limitClause->limitCount) { -- Gitee