You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/06/23 00:46:37 UTC

[shardingsphere] branch master updated: support DECLARE cursor statement (#18519)

This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 612b8fb5a45 support DECLARE cursor statement (#18519)
612b8fb5a45 is described below

commit 612b8fb5a451ad1bc2f3fcca18dd6ec1233c0bb6
Author: Jiaqi Yan <13...@qq.com>
AuthorDate: Thu Jun 23 08:46:23 2022 +0800

    support DECLARE cursor statement (#18519)
    
    * support DECLARE statement
    
    * remove extra blank spaces
---
 .../impl/OpenGaussDDLStatementSQLVisitor.java      |  5 +-
 .../main/antlr4/imports/postgresql/DDLStatement.g4 | 30 +++++------
 .../impl/PostgreSQLDDLStatementSQLVisitor.java     | 13 +++--
 .../sql/common/statement/ddl/DeclareStatement.java | 10 ++++
 .../asserts/statement/ddl/DDLStatementAssert.java  |  5 ++
 .../statement/ddl/impl/DeclareStatementAssert.java | 63 ++++++++++++++++++++++
 .../statement/ddl/DeclareStatementTestCase.java    | 14 +++++
 .../src/main/resources/case/ddl/declare.xml        | 12 ++++-
 .../main/resources/sql/supported/ddl/declare.xml   |  2 +-
 .../main/resources/sql/unsupported/unsupported.xml |  1 -
 10 files changed, 131 insertions(+), 24 deletions(-)

diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-opengauss/src/main/java/org/apache/shardingsphere/sql/parser/opengauss/visitor/statement/impl/OpenGaussDDLStatementSQLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-opengauss/src/main/java/org/apache/shardingsphere/sql/parser/opengauss/visitor/statement/impl/OpenGaussDDLStatementSQLVisitor.java
index b1c0d14f069..4090efa2ed6 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-opengauss/src/main/java/org/apache/shardingsphere/sql/parser/opengauss/visitor/statement/impl/OpenGaussDDLStatementSQLVisitor.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-opengauss/src/main/java/org/apache/shardingsphere/sql/parser/opengauss/visitor/statement/impl/OpenGaussDDLStatementSQLVisitor.java
@@ -934,7 +934,10 @@ public final class OpenGaussDDLStatementSQLVisitor extends OpenGaussStatementSQL
     
     @Override
     public ASTNode visitDeclare(final DeclareContext ctx) {
-        return new OpenGaussDeclareStatement();
+        OpenGaussDeclareStatement result = new OpenGaussDeclareStatement();
+        result.setCursorName((CursorNameSegment) visit(ctx.cursorName()));
+        result.setSelect((SelectStatement) visit(ctx.select()));
+        return result;
     }
     
     @Override
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/antlr4/imports/postgresql/DDLStatement.g4 b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/antlr4/imports/postgresql/DDLStatement.g4
index 9328b45af81..50ec724e105 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/antlr4/imports/postgresql/DDLStatement.g4
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/antlr4/imports/postgresql/DDLStatement.g4
@@ -964,21 +964,6 @@ alterMaterializedViewClauses
     | ALL IN TABLESPACE name (OWNED BY roleList) SET TABLESPACE name NOWAIT?
     ;
 
-declare
-    : DECLARE name cursorOptions CURSOR (WITH HOLD | WITHOUT HOLD)? FOR select
-    ;
-
-cursorOptions
-    : cursorOption*
-    ;
-
-cursorOption
-    : NO SCROLL
-    | SCROLL
-    | BINARY
-    | INSENSITIVE
-    ;
-
 executeStmt
     : EXECUTE name executeParamClause
     ;
@@ -1786,6 +1771,21 @@ listen
     : LISTEN colId
     ;
 
+declare
+    : DECLARE cursorName cursorOptions CURSOR (WITH HOLD | WITHOUT HOLD)? FOR select
+    ;
+
+cursorOptions
+    : cursorOption*
+    ;
+
+cursorOption
+    : NO SCROLL
+    | SCROLL
+    | BINARY
+    | INSENSITIVE
+    ;
+
 move
     : MOVE direction? (FROM | IN)? cursorName
     ;
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLDDLStatementSQLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLDDLStatementSQLVisitor.java
index ef5de7a04bd..e6cbdea221d 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLDDLStatementSQLVisitor.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLDDLStatementSQLVisitor.java
@@ -964,11 +964,6 @@ public final class PostgreSQLDDLStatementSQLVisitor extends PostgreSQLStatementS
         return new PostgreSQLDropExtensionStatement();
     }
     
-    @Override
-    public ASTNode visitDeclare(final DeclareContext ctx) {
-        return new PostgreSQLDeclareStatement();
-    }
-    
     @Override
     public ASTNode visitDiscard(final DiscardContext ctx) {
         return new PostgreSQLDiscardStatement();
@@ -1085,6 +1080,14 @@ public final class PostgreSQLDDLStatementSQLVisitor extends PostgreSQLStatementS
         return new PostgreSQLDropServerStatement();
     }
     
+    @Override
+    public ASTNode visitDeclare(final DeclareContext ctx) {
+        PostgreSQLDeclareStatement result = new PostgreSQLDeclareStatement();
+        result.setCursorName((CursorNameSegment) visit(ctx.cursorName()));
+        result.setSelect((SelectStatement) visit(ctx.select()));
+        return result;
+    }
+    
     @Override
     public ASTNode visitFetch(final FetchContext ctx) {
         PostgreSQLFetchStatement result = new PostgreSQLFetchStatement();
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/ddl/DeclareStatement.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/ddl/DeclareStatement.java
index e109ba245ec..b2aee8c1f63 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/ddl/DeclareStatement.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/ddl/DeclareStatement.java
@@ -17,12 +17,22 @@
 
 package org.apache.shardingsphere.sql.parser.sql.common.statement.ddl;
 
+import lombok.Getter;
+import lombok.Setter;
 import lombok.ToString;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.cursor.CursorNameSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.AbstractSQLStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
 
 /**
  * Declare statement.
  */
+@Getter
+@Setter
 @ToString
 public abstract class DeclareStatement extends AbstractSQLStatement implements DDLStatement {
+    
+    private CursorNameSegment cursorName;
+    
+    private SelectStatement select;
 }
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/ddl/DDLStatementAssert.java b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/ddl/DDLStatementAssert.java
index a4cbe936b6a..a3aa1ac5afd 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/ddl/DDLStatementAssert.java
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/ddl/DDLStatementAssert.java
@@ -28,6 +28,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CommentStat
 import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateIndexStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateTableStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DDLStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DeclareStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropIndexStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropTableStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.FetchStatement;
@@ -57,6 +58,7 @@ import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.statement
 import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.statement.ddl.impl.CreateIndexStatementAssert;
 import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.statement.ddl.impl.CreateTableStatementAssert;
 import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.statement.ddl.impl.CursorStatementAssert;
+import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.statement.ddl.impl.DeclareStatementAssert;
 import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.statement.ddl.impl.DisassociateStatisticsStatementAssert;
 import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.statement.ddl.impl.DropIndexStatementAssert;
 import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.statement.ddl.impl.DropTableStatementAssert;
@@ -80,6 +82,7 @@ import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain
 import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.ddl.CreateIndexStatementTestCase;
 import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.ddl.CreateTableStatementTestCase;
 import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.ddl.CursorStatementTestCase;
+import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.ddl.DeclareStatementTestCase;
 import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.ddl.DisassociateStatisticsStatementTestCase;
 import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.ddl.DropIndexStatementTestCase;
 import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.ddl.DropTableStatementTestCase;
@@ -137,6 +140,8 @@ public final class DDLStatementAssert {
             NoAuditStatementAssert.assertIs(assertContext, (OracleNoAuditStatement) actual, (NoAuditStatementTestCase) expected);
         } else if (actual instanceof OpenGaussCursorStatement) {
             CursorStatementAssert.assertIs(assertContext, (OpenGaussCursorStatement) actual, (CursorStatementTestCase) expected);
+        } else if (actual instanceof DeclareStatement) {
+            DeclareStatementAssert.assertIs(assertContext, (DeclareStatement) actual, (DeclareStatementTestCase) expected);
         } else if (actual instanceof CloseStatement) {
             CloseStatementAssert.assertIs(assertContext, (CloseStatement) actual, (CloseStatementTestCase) expected);
         } else if (actual instanceof MoveStatement) {
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/ddl/impl/DeclareStatementAssert.java b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/ddl/impl/DeclareStatementAssert.java
new file mode 100644
index 00000000000..5d17071563d
--- /dev/null
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/ddl/impl/DeclareStatementAssert.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.test.sql.parser.parameterized.asserts.statement.ddl.impl;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DeclareStatement;
+import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.SQLCaseAssertContext;
+import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.segment.SQLSegmentAssert;
+import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.statement.dml.impl.SelectStatementAssert;
+import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.value.IdentifierValueAssert;
+import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.ddl.DeclareStatementTestCase;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+/**
+ * Declare statement assert.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class DeclareStatementAssert {
+    
+    /**
+     * Assert declare statement is correct with expected parser result.
+     *
+     * @param assertContext assert context
+     * @param actual actual declare statement
+     * @param expected expected declare statement test case
+     */
+    public static void assertIs(final SQLCaseAssertContext assertContext, final DeclareStatement actual, final DeclareStatementTestCase expected) {
+        assertCursorName(assertContext, actual, expected);
+        assertSelect(assertContext, actual, expected);
+    }
+    
+    private static void assertCursorName(final SQLCaseAssertContext assertContext, final DeclareStatement actual, final DeclareStatementTestCase expected) {
+        IdentifierValueAssert.assertIs(assertContext, actual.getCursorName().getIdentifier(), expected.getCursorName(), "Cursor");
+        SQLSegmentAssert.assertIs(assertContext, actual.getCursorName(), expected.getCursorName());
+    }
+    
+    private static void assertSelect(final SQLCaseAssertContext assertContext, final DeclareStatement actual, final DeclareStatementTestCase expected) {
+        if (null != expected.getSelectTestCase()) {
+            assertNotNull(assertContext.getText("Actual select statement should exist."), actual.getSelect());
+            SelectStatementAssert.assertIs(assertContext, actual.getSelect(), expected.getSelectTestCase());
+        } else {
+            assertNull(assertContext.getText("Actual select statement should not exist."), actual.getSelect());
+        }
+    }
+}
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/ddl/DeclareStatementTestCase.java b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/ddl/DeclareStatementTestCase.java
index 09b92482a2d..e98031f073a 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/ddl/DeclareStatementTestCase.java
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/ddl/DeclareStatementTestCase.java
@@ -17,10 +17,24 @@
 
 package org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.ddl;
 
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.segment.impl.cursor.ExpectedCursorName;
 import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.SQLParserTestCase;
+import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.dml.SelectStatementTestCase;
+
+import javax.xml.bind.annotation.XmlElement;
 
 /**
  * Declare statement test case.
  */
+@Getter
+@Setter
 public final class DeclareStatementTestCase extends SQLParserTestCase {
+    
+    @XmlElement(name = "cursor-name")
+    private ExpectedCursorName cursorName;
+    
+    @XmlElement(name = "select")
+    private SelectStatementTestCase selectTestCase;
 }
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/ddl/declare.xml b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/ddl/declare.xml
index d466dea2421..c546b788a6c 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/ddl/declare.xml
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/ddl/declare.xml
@@ -17,5 +17,15 @@
   -->
 
 <sql-parser-test-cases>
-    <declare sql-case-id="declare" />
+    <declare sql-case-id="declare_cursor">
+        <cursor-name name="t_order_cursor" start-index="8" stop-index="21" />
+        <select>
+            <from>
+                <simple-table name="t_order" start-index="55" stop-index="61" />
+            </from>
+            <projections start-index="48" stop-index="48">
+                <shorthand-projection start-index="48" stop-index="48" />
+            </projections>
+        </select>
+    </declare>
 </sql-parser-test-cases>
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/ddl/declare.xml b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/ddl/declare.xml
index 9589750e0b3..38bcd5de17b 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/ddl/declare.xml
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/ddl/declare.xml
@@ -17,5 +17,5 @@
   -->
 
 <sql-cases>
-    <sql-case id="declare" value="DECLARE bc BINARY CURSOR FOR SELECT * FROM tenk1;" db-types="PostgreSQL,openGauss" />
+    <sql-case id="declare_cursor" value="DECLARE t_order_cursor BINARY CURSOR FOR SELECT * FROM t_order;" db-types="PostgreSQL,openGauss" />
 </sql-cases>
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/unsupported/unsupported.xml b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/unsupported/unsupported.xml
index 9df93c5d853..093d5364aaa 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/unsupported/unsupported.xml
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/unsupported/unsupported.xml
@@ -6761,7 +6761,6 @@
     <sql-case id="low_create_by_postgresql_source_test_case559" value="create view tt201v as select   extract(day from now()) as extr,   (now(), &apos;1 day&apos;::interval) overlaps     (current_timestamp(2), &apos;1 day&apos;::interval) as o,   &apos;foo&apos; is normalized isn,   &apos;foo&apos; is nfkc normalized isnn,   normalize(&apos;foo&apos;) as n,   normalize(&apos;foo&apos;, nfkd) as nfkd,   overlay(&apos;foo&apos; placing &apos;bar&apos; from 2) as ovl,   overlay(&apos;foo&ap [...]
     <sql-case id="low_create_by_postgresql_source_test_case560" value="create view tt26v as select x + y + z as c1,        (x * y) + z as c2,        x + (y * z) as c3,        (x + y) * z as c4,        x * (y + z) as c5,        x + (y + z) as c6,        x + (y # z) as c7,        (x &gt; y) AND (y &gt; z OR x &gt; z) as c8,        (x &gt; y) OR (y &gt; z AND NOT (x &gt; z)) as c9,        (x,y) &lt;&gt; ALL (values(1,2),(3,4)) as c10,        (x,y) &lt;= ANY (values(1,2),(3,4)) as c11 from ( [...]
     <sql-case id="low_create_by_postgresql_source_test_case561" value="create view view_of_joins_2d as select * from (tbl1 join tbl1a using (a) as x) as y;" db-types="PostgreSQL" />
-    <sql-case id="low_declare_by_postgresql_source_test_case13" value="declare rf_cur scroll cursor for select * from rows from(generate_series(1,5),generate_series(1,2)) with ordinality as g(i,j,o);" db-types="PostgreSQL" />
     <sql-case id="low_delete_by_postgresql_source_test_case1" value="delete from;" db-types="PostgreSQL" />
     <sql-case id="low_do_by_postgresql_source_test_case1" value="do $$   declare r integer;   begin     select dfunc(a=&gt;-- comment       1) into r;     raise info &apos;r = %&apos;, r;   end; $$;" db-types="PostgreSQL" />
     <sql-case id="low_do_by_postgresql_source_test_case2" value="do $$ BEGIN   EXECUTE &apos;CREATE COLLATION test0 (provider = icu, locale = &apos; ||           quote_literal(current_setting(&apos;lc_collate&apos;)) || &apos;);&apos;; END $$;" db-types="PostgreSQL" />