You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2022/02/25 03:58:09 UTC

[shardingsphere] branch master updated: `drop resource` syntax adds `if exist` keyword. (#15617)

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

jianglongtao 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 909869c  `drop resource` syntax adds `if exist` keyword. (#15617)
909869c is described below

commit 909869c4905d501dc1bb533b7793a904d80f72a9
Author: lanchengx <52...@users.noreply.github.com>
AuthorDate: Fri Feb 25 11:57:12 2022 +0800

    `drop resource` syntax adds `if exist` keyword. (#15617)
    
    * `drop resource` syntax adds if exist keyword.
    
    * Add tests for the `if exists` keyword
    
    * Update Keyword.g4
---
 .../src/main/antlr4/imports/Keyword.g4             | 16 ++++++++--------
 .../src/main/antlr4/imports/RDLStatement.g4        |  8 ++++++--
 .../core/common/CommonDistSQLStatementVisitor.java |  3 ++-
 .../statement/rdl/drop/DropResourceStatement.java  |  8 ++++++++
 .../rdl/resource/DropResourceBackendHandler.java   |  9 ++++++---
 .../resource/DropResourceBackendHandlerTest.java   | 22 ++++++++++++++++++++++
 .../rdl/drop/DropResourceStatementAssert.java      |  1 +
 .../rdl/drop/DropResourceStatementTestCase.java    |  4 ++++
 .../src/main/resources/case/rdl/drop.xml           |  6 ++++++
 .../src/main/resources/sql/supported/rdl/drop.xml  |  1 +
 10 files changed, 64 insertions(+), 14 deletions(-)

diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/Keyword.g4 b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/Keyword.g4
index 6ffdee8..fa01cd7 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/Keyword.g4
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/Keyword.g4
@@ -215,6 +215,14 @@ CONCURRENCY_LEVEL
     : C O N C U R R E N C Y UL_ L E V E L
     ;
 
+IF  
+    : I F
+    ;
+
+EXISTS
+    : E X I S T S
+    ;
+
 TYPE
     : T Y P E
     ;
@@ -255,14 +263,6 @@ LOAD_BALANCER
     : L O A D UL_ B A L A N C E R
     ;
 
-IF
-    : I F
-    ;
-
-EXISTS
-    : E X I S T S
-    ;
-
 EXPORT
     : E X P O R T
     ;
diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/RDLStatement.g4 b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/RDLStatement.g4
index 6f0b189..2166445 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/RDLStatement.g4
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/RDLStatement.g4
@@ -28,7 +28,7 @@ alterResource
     ;
 
 dropResource
-    : DROP RESOURCE IDENTIFIER (COMMA IDENTIFIER)* ignoreSingleTables?
+    : DROP RESOURCE existClause? IDENTIFIER (COMMA IDENTIFIER)* ignoreSingleTables?
     ;
 
 createDefaultSingleTableRule
@@ -40,7 +40,7 @@ alterDefaultSingleTableRule
     ;
 
 dropDefaultSingleTableRule
-    : DROP DEFAULT SINGLE TABLE RULE
+    : DROP  DEFAULT SINGLE TABLE RULE existClause?
     ;
 
 dataSource
@@ -86,3 +86,7 @@ password
 ignoreSingleTables
     : IGNORE SINGLE TABLES
     ;
+
+existClause
+    : IF EXISTS
+    ;
diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/common/CommonDistSQLStatementVisitor.java b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/common/CommonDistSQLStatementVisitor.java
index c6c3ba1..c559e0b 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/common/CommonDistSQLStatementVisitor.java
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/common/CommonDistSQLStatementVisitor.java
@@ -269,7 +269,8 @@ public final class CommonDistSQLStatementVisitor extends CommonDistSQLStatementB
     @Override
     public ASTNode visitDropResource(final DropResourceContext ctx) {
         boolean ignoreSingleTables = null != ctx.ignoreSingleTables();
-        return new DropResourceStatement(ctx.IDENTIFIER().stream().map(ParseTree::getText).map(each -> new IdentifierValue(each).getValue()).collect(Collectors.toList()), ignoreSingleTables);
+        return new DropResourceStatement(ctx.existClause() != null, 
+                ctx.IDENTIFIER().stream().map(ParseTree::getText).map(each -> new IdentifierValue(each).getValue()).collect(Collectors.toList()), ignoreSingleTables);
     }
     
     @Override
diff --git a/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/drop/DropResourceStatement.java b/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/drop/DropResourceStatement.java
index e595829..5ee29b3 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/drop/DropResourceStatement.java
+++ b/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/drop/DropResourceStatement.java
@@ -30,7 +30,15 @@ import java.util.Collection;
 @Getter
 public final class DropResourceStatement extends ResourceDefinitionStatement {
     
+    private final boolean containsExistClause;
+    
     private final Collection<String> names;
     
     private final boolean ignoreSingleTables;
+    
+    public DropResourceStatement(final Collection<String> names, final boolean ignoreSingleTables) {
+        this.containsExistClause = false;
+        this.names = names;
+        this.ignoreSingleTables = ignoreSingleTables;
+    }
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/DropResourceBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/DropResourceBackendHandler.java
index 1a634c9..7a9b18f 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/DropResourceBackendHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/DropResourceBackendHandler.java
@@ -53,13 +53,16 @@ public final class DropResourceBackendHandler extends SchemaRequiredBackendHandl
     @Override
     public ResponseHeader execute(final String schemaName, final DropResourceStatement sqlStatement) throws ResourceDefinitionViolationException {
         Collection<String> toBeDroppedResourceNames = sqlStatement.getNames();
-        check(schemaName, toBeDroppedResourceNames, sqlStatement.isIgnoreSingleTables());
+        check(schemaName, toBeDroppedResourceNames, sqlStatement.isIgnoreSingleTables(), sqlStatement.isContainsExistClause());
         ProxyContext.getInstance().getContextManager().dropResource(schemaName, toBeDroppedResourceNames);
         return new UpdateResponseHeader(sqlStatement);
     }
     
-    private void check(final String schemaName, final Collection<String> toBeDroppedResourceNames, final boolean ignoreSingleTables) throws RequiredResourceMissedException, ResourceInUsedException {
-        checkResourceNameExisted(schemaName, toBeDroppedResourceNames);
+    private void check(final String schemaName, final Collection<String> toBeDroppedResourceNames, 
+                       final boolean ignoreSingleTables, final boolean allowNotExist) throws RequiredResourceMissedException, ResourceInUsedException {
+        if (!allowNotExist) {
+            checkResourceNameExisted(schemaName, toBeDroppedResourceNames);
+        }
         checkResourceNameNotInUse(schemaName, toBeDroppedResourceNames, ignoreSingleTables);
     }
     
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/DropResourceBackendHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/DropResourceBackendHandlerTest.java
index ffd6c58..a470bd4 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/DropResourceBackendHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/DropResourceBackendHandlerTest.java
@@ -21,6 +21,7 @@ import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.DropResourceS
 import org.apache.shardingsphere.infra.datanode.DataNode;
 import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
 import org.apache.shardingsphere.infra.distsql.exception.resource.ResourceDefinitionViolationException;
+import org.apache.shardingsphere.infra.distsql.exception.resource.ResourceInUsedException;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
 import org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
@@ -160,6 +161,23 @@ public final class DropResourceBackendHandlerTest {
         verify(contextManager).dropResource("test", dropResourceStatement.getNames());
     }
     
+    @Test
+    public void assertExecuteWithIfExists() throws ResourceDefinitionViolationException {
+        DropResourceStatement dropResourceStatement = createDropResourceStatementWithIfExists();
+        ResponseHeader responseHeader = dropResourceBackendHandler.execute("test", dropResourceStatement);
+        assertTrue(responseHeader instanceof UpdateResponseHeader);
+        verify(contextManager).dropResource("test", dropResourceStatement.getNames());
+    }
+    
+    @Test(expected = ResourceInUsedException.class)
+    public void assertResourceNameInUseWithIfExists() throws ResourceDefinitionViolationException {
+        when(ruleMetaData.getRules()).thenReturn(Collections.singleton(shadowRule));
+        when(shadowRule.getType()).thenReturn("ShadowRule");
+        when(shadowRule.getDataSourceMapper()).thenReturn(Collections.singletonMap("", Collections.singleton("test0")));
+        DropResourceStatement dropResourceStatement = createDropResourceStatementWithIfExists();
+        dropResourceBackendHandler.execute("test", dropResourceStatement);
+    }
+    
     private Map<String, DataSource> getDataSourceMapForSupportRemove() {
         Map<String, DataSource> result = new LinkedHashMap<>();
         result.put("test0", dataSource);
@@ -173,4 +191,8 @@ public final class DropResourceBackendHandlerTest {
     private DropResourceStatement createDropResourceStatementIgnoreSingleTables() {
         return new DropResourceStatement(Collections.singleton("test0"), true);
     }
+    
+    private DropResourceStatement createDropResourceStatementWithIfExists() {
+        return new DropResourceStatement(true, Collections.singleton("test0"), true);
+    }
 }
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/rdl/drop/DropResourceStatementAssert.java b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/rdl/drop/DropResourceStatementAssert.java
index 47f21a9..0a0b81d 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/rdl/drop/DropResourceStatementAssert.java
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/rdl/drop/DropResourceStatementAssert.java
@@ -46,6 +46,7 @@ public final class DropResourceStatementAssert {
         } else {
             assertThat(assertContext.getText("resource assertion error: "), actual.getNames(), is(expected.getDataSources()));
             assertThat(assertContext.getText("resource assertion error: "), actual.isIgnoreSingleTables(), is(expected.getIgnoreSingleTables().iterator().next()));
+            assertThat(assertContext.getText("resource assertion error: "), actual.isContainsExistClause(), is(expected.isContainsExistClause()));
         }
     }
 }
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/distsql/rdl/drop/DropResourceStatementTestCase.java b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/distsql/rdl/drop/DropResourceStatementTestCase.java
index ae0c3b8..cbd7a57 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/distsql/rdl/drop/DropResourceStatementTestCase.java
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/distsql/rdl/drop/DropResourceStatementTestCase.java
@@ -21,6 +21,7 @@ import lombok.Getter;
 import lombok.Setter;
 import org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.SQLParserTestCase;
 
+import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlElement;
 import java.util.LinkedList;
 import java.util.List;
@@ -32,6 +33,9 @@ import java.util.List;
 @Setter
 public final class DropResourceStatementTestCase extends SQLParserTestCase {
     
+    @XmlAttribute(name = "contains-exist-clause")
+    private boolean containsExistClause;
+    
     @XmlElement(name = "data-source")
     private final List<String> dataSources = new LinkedList<>();
     
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/rdl/drop.xml b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/rdl/drop.xml
index d256743..c24a145 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/rdl/drop.xml
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/rdl/drop.xml
@@ -23,6 +23,12 @@
         <ignore-single-tables>false</ignore-single-tables>
     </drop-resource>
 
+    <drop-resource sql-case-id="drop-resource-if-exists" contains-exist-clause="true">
+        <data-source>ds_0</data-source>
+        <data-source>ds_1</data-source>
+        <ignore-single-tables>false</ignore-single-tables>
+    </drop-resource>
+    
     <drop-resource sql-case-id="drop-resource-ignore-single-tables">
         <data-source>ds_0</data-source>
         <data-source>ds_1</data-source>
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/rdl/drop.xml b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/rdl/drop.xml
index cd55eb7..f00651b 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/rdl/drop.xml
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/rdl/drop.xml
@@ -18,6 +18,7 @@
 
 <sql-cases>
     <distsql-case id="drop-resource" value="DROP RESOURCE ds_0,ds_1" />
+    <distsql-case id="drop-resource-if-exists" value="DROP RESOURCE IF EXISTS ds_0,ds_1" />
     <distsql-case id="drop-resource-ignore-single-tables" value="DROP RESOURCE ds_0,ds_1 ignore single tables;" />
     <distsql-case id="drop-sharding-table-rule" value="DROP SHARDING TABLE RULE t_order,t_order_item" />
     <distsql-case id="drop-sharding-binding-table-rules" value="DROP SHARDING BINDING TABLE RULES" />