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/12/22 02:22:38 UTC

[shardingsphere] branch master updated: Add IF NOT EXISTS to CREATE DEFAULT SHADOW ALGORITHM. (#23020)

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 2e2067d9a20 Add IF NOT EXISTS to CREATE DEFAULT SHADOW ALGORITHM. (#23020)
2e2067d9a20 is described below

commit 2e2067d9a20a82aedb6cb327f909619dfe505d65
Author: yx9o <ya...@163.com>
AuthorDate: Thu Dec 22 10:22:32 2022 +0800

    Add IF NOT EXISTS to CREATE DEFAULT SHADOW ALGORITHM. (#23020)
---
 .../statement/rdl/create/CreateRuleStatement.java  | 10 +++++++
 ...eateDefaultShadowAlgorithmStatementUpdater.java | 33 ++++++++++++++--------
 ...DefaultShadowAlgorithmStatementUpdaterTest.java | 25 ++++++++++------
 .../src/main/antlr4/imports/shadow/Keyword.g4      |  4 +++
 .../src/main/antlr4/imports/shadow/RDLStatement.g4 |  6 +++-
 .../parser/core/ShadowDistSQLStatementVisitor.java |  2 +-
 .../CreateDefaultShadowAlgorithmStatement.java     |  7 +++--
 7 files changed, 62 insertions(+), 25 deletions(-)

diff --git a/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/create/CreateRuleStatement.java b/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/create/CreateRuleStatement.java
index b8d5a3d4e6c..c84c79abca8 100644
--- a/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/create/CreateRuleStatement.java
+++ b/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/create/CreateRuleStatement.java
@@ -17,10 +17,20 @@
 
 package org.apache.shardingsphere.distsql.parser.statement.rdl.create;
 
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
 import org.apache.shardingsphere.distsql.parser.statement.rdl.RuleDefinitionStatement;
 
 /**
  * Create rule statement.
  */
+@RequiredArgsConstructor
+@Getter
 public abstract class CreateRuleStatement extends RuleDefinitionStatement {
+    
+    private final boolean ifNotExists;
+    
+    public CreateRuleStatement() {
+        this(false);
+    }
 }
diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateDefaultShadowAlgorithmStatementUpdater.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateDefaultShadowAlgorithmStatementUpdater.java
index c43e6853e9a..317c606ab64 100644
--- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateDefaultShadowAlgorithmStatementUpdater.java
+++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateDefaultShadowAlgorithmStatementUpdater.java
@@ -17,16 +17,15 @@
 
 package org.apache.shardingsphere.shadow.distsql.handler.update;
 
-import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
-import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
 import org.apache.shardingsphere.distsql.handler.exception.algorithm.DuplicateAlgorithmException;
 import org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgorithmConfigurationException;
 import org.apache.shardingsphere.distsql.handler.update.RuleDefinitionCreateUpdater;
+import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
-import org.apache.shardingsphere.shadow.distsql.handler.checker.ShadowRuleStatementChecker;
 import org.apache.shardingsphere.shadow.distsql.parser.statement.CreateDefaultShadowAlgorithmStatement;
 import org.apache.shardingsphere.shadow.factory.ShadowAlgorithmFactory;
 
@@ -45,6 +44,8 @@ public final class CreateDefaultShadowAlgorithmStatementUpdater implements RuleD
     
     private static final String DEFAULT_ALGORITHM_NAME = "default_shadow_algorithm";
     
+    private boolean ifNotExists;
+    
     @Override
     public RuleConfiguration buildToBeCreatedRuleConfiguration(final CreateDefaultShadowAlgorithmStatement sqlStatement) {
         ShadowRuleConfiguration result = new ShadowRuleConfiguration();
@@ -60,27 +61,35 @@ public final class CreateDefaultShadowAlgorithmStatementUpdater implements RuleD
     
     @Override
     public void updateCurrentRuleConfiguration(final ShadowRuleConfiguration currentRuleConfig, final ShadowRuleConfiguration toBeCreatedRuleConfig) {
-        currentRuleConfig.getShadowAlgorithms().putAll(toBeCreatedRuleConfig.getShadowAlgorithms());
-        currentRuleConfig.setDefaultShadowAlgorithmName(toBeCreatedRuleConfig.getDefaultShadowAlgorithmName());
+        if (!ifNotExists) {
+            currentRuleConfig.getShadowAlgorithms().putAll(toBeCreatedRuleConfig.getShadowAlgorithms());
+            currentRuleConfig.setDefaultShadowAlgorithmName(toBeCreatedRuleConfig.getDefaultShadowAlgorithmName());
+        }
     }
     
     @Override
     public void checkSQLStatement(final ShardingSphereDatabase database, final CreateDefaultShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) {
-        checkExist(database.getName(), currentRuleConfig);
+        ifNotExists = sqlStatement.isIfNotExists();
+        if (!ifNotExists) {
+            checkExist(database.getName(), currentRuleConfig);
+        }
         checkAlgorithmCompleteness(Collections.singleton(sqlStatement.getShadowAlgorithmSegment().getAlgorithmSegment()));
         checkAlgorithmType(sqlStatement);
     }
     
-    private void checkExist(final String databaseName, final ShadowRuleConfiguration currentRuleConfig) {
-        Collection<String> requireAlgorithmNames = Collections.singleton(DEFAULT_ALGORITHM_NAME);
+    private static Collection<String> getIdentical(final ShadowRuleConfiguration currentRuleConfig) {
         Collection<String> currentAlgorithmNames = null == currentRuleConfig ? Collections.emptyList() : currentRuleConfig.getShadowAlgorithms().keySet();
-        ShadowRuleStatementChecker.checkAnyDuplicate(requireAlgorithmNames, currentAlgorithmNames, different -> new DuplicateAlgorithmException(SHADOW, databaseName, different));
+        return Collections.singleton(DEFAULT_ALGORITHM_NAME).stream().filter(currentAlgorithmNames::contains).collect(Collectors.toSet());
+    }
+    
+    private void checkExist(final String databaseName, final ShadowRuleConfiguration currentRuleConfig) {
+        Collection<String> identical = getIdentical(currentRuleConfig);
+        ShardingSpherePreconditions.checkState(identical.isEmpty(), () -> new DuplicateAlgorithmException(SHADOW, databaseName, identical));
     }
     
     private void checkAlgorithmType(final CreateDefaultShadowAlgorithmStatement sqlStatement) {
         String shadowAlgorithmType = sqlStatement.getShadowAlgorithmSegment().getAlgorithmSegment().getName();
-        ShardingSpherePreconditions.checkState(ShadowAlgorithmFactory.contains(shadowAlgorithmType),
-                () -> new InvalidAlgorithmConfigurationException(SHADOW, shadowAlgorithmType));
+        ShardingSpherePreconditions.checkState(ShadowAlgorithmFactory.contains(shadowAlgorithmType), () -> new InvalidAlgorithmConfigurationException(SHADOW, shadowAlgorithmType));
     }
     
     private static void checkAlgorithmCompleteness(final Collection<AlgorithmSegment> algorithmSegments) {
diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateDefaultShadowAlgorithmStatementUpdaterTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateDefaultShadowAlgorithmStatementUpdaterTest.java
index 1a922c6d9e1..090c048aee4 100644
--- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateDefaultShadowAlgorithmStatementUpdaterTest.java
+++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateDefaultShadowAlgorithmStatementUpdaterTest.java
@@ -17,8 +17,8 @@
 
 package org.apache.shardingsphere.shadow.distsql.update;
 
-import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
 import org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgorithmConfigurationException;
+import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
 import org.apache.shardingsphere.shadow.distsql.handler.update.CreateDefaultShadowAlgorithmStatementUpdater;
@@ -48,21 +48,28 @@ public final class CreateDefaultShadowAlgorithmStatementUpdaterTest {
     
     @Test(expected = InvalidAlgorithmConfigurationException.class)
     public void assertExecuteWithInvalidAlgorithm() {
-        Properties prop = new Properties();
-        prop.setProperty("type", "value");
         CreateDefaultShadowAlgorithmStatement statement = mock(CreateDefaultShadowAlgorithmStatement.class);
-        ShadowAlgorithmSegment shadowAlgorithmSegment = new ShadowAlgorithmSegment("algorithmName", new AlgorithmSegment("name", prop));
-        when(statement.getShadowAlgorithmSegment()).thenReturn(shadowAlgorithmSegment);
+        when(statement.getShadowAlgorithmSegment()).thenReturn(new ShadowAlgorithmSegment("algorithmName", new AlgorithmSegment("name", createProperties())));
         updater.checkSQLStatement(database, statement, currentConfig);
     }
     
     @Test
     public void assertExecuteSuccess() {
-        Properties prop = new Properties();
-        prop.setProperty("type", "value");
         CreateDefaultShadowAlgorithmStatement statement = mock(CreateDefaultShadowAlgorithmStatement.class);
-        ShadowAlgorithmSegment shadowAlgorithmSegment = new ShadowAlgorithmSegment("algorithmName", new AlgorithmSegment("SIMPLE_HINT", prop));
-        when(statement.getShadowAlgorithmSegment()).thenReturn(shadowAlgorithmSegment);
+        when(statement.getShadowAlgorithmSegment()).thenReturn(new ShadowAlgorithmSegment("algorithmName", new AlgorithmSegment("SIMPLE_HINT", createProperties())));
+        updater.checkSQLStatement(database, statement, currentConfig);
+    }
+    
+    @Test
+    public void assertExecuteWithIfNotExists() {
+        ShadowAlgorithmSegment shadowAlgorithmSegment = new ShadowAlgorithmSegment("algorithmName", new AlgorithmSegment("SIMPLE_HINT", createProperties()));
+        CreateDefaultShadowAlgorithmStatement statement = new CreateDefaultShadowAlgorithmStatement(true, shadowAlgorithmSegment);
         updater.checkSQLStatement(database, statement, currentConfig);
     }
+    
+    private Properties createProperties() {
+        Properties result = new Properties();
+        result.setProperty("type", "value");
+        return result;
+    }
 }
diff --git a/features/shadow/distsql/parser/src/main/antlr4/imports/shadow/Keyword.g4 b/features/shadow/distsql/parser/src/main/antlr4/imports/shadow/Keyword.g4
index 8a6852a05ee..143794355de 100644
--- a/features/shadow/distsql/parser/src/main/antlr4/imports/shadow/Keyword.g4
+++ b/features/shadow/distsql/parser/src/main/antlr4/imports/shadow/Keyword.g4
@@ -146,3 +146,7 @@ REGEX_MATCH
 SIMPLE_HINT
     : S I M P L E UL_ H I N T
     ;
+
+NOT
+    : N O T
+    ;
diff --git a/features/shadow/distsql/parser/src/main/antlr4/imports/shadow/RDLStatement.g4 b/features/shadow/distsql/parser/src/main/antlr4/imports/shadow/RDLStatement.g4
index bd0588a83a4..715a56c73de 100644
--- a/features/shadow/distsql/parser/src/main/antlr4/imports/shadow/RDLStatement.g4
+++ b/features/shadow/distsql/parser/src/main/antlr4/imports/shadow/RDLStatement.g4
@@ -36,7 +36,7 @@ dropShadowAlgorithm
     ;
 
 createDefaultShadowAlgorithm
-    : CREATE DEFAULT SHADOW ALGORITHM algorithmDefinition
+    : CREATE DEFAULT SHADOW ALGORITHM ifNotExists? algorithmDefinition
     ;
 
 dropDefaultShadowAlgorithm
@@ -74,3 +74,7 @@ algorithmName
 ifExists
     : IF EXISTS
     ;
+
+ifNotExists
+    : IF NOT EXISTS
+    ;
diff --git a/features/shadow/distsql/parser/src/main/java/org/apache/shardingsphere/shadow/distsql/parser/core/ShadowDistSQLStatementVisitor.java b/features/shadow/distsql/parser/src/main/java/org/apache/shardingsphere/shadow/distsql/parser/core/ShadowDistSQLStatementVisitor.java
index 0d09d6e444d..4b45a095ab6 100644
--- a/features/shadow/distsql/parser/src/main/java/org/apache/shardingsphere/shadow/distsql/parser/core/ShadowDistSQLStatementVisitor.java
+++ b/features/shadow/distsql/parser/src/main/java/org/apache/shardingsphere/shadow/distsql/parser/core/ShadowDistSQLStatementVisitor.java
@@ -79,7 +79,7 @@ public final class ShadowDistSQLStatementVisitor extends ShadowDistSQLStatementB
     
     @Override
     public ASTNode visitCreateDefaultShadowAlgorithm(final CreateDefaultShadowAlgorithmContext ctx) {
-        return new CreateDefaultShadowAlgorithmStatement((ShadowAlgorithmSegment) visit(ctx.algorithmDefinition()));
+        return new CreateDefaultShadowAlgorithmStatement(null != ctx.ifNotExists(), (ShadowAlgorithmSegment) visit(ctx.algorithmDefinition()));
     }
     
     @Override
diff --git a/features/shadow/distsql/statement/src/main/java/org/apache/shardingsphere/shadow/distsql/parser/statement/CreateDefaultShadowAlgorithmStatement.java b/features/shadow/distsql/statement/src/main/java/org/apache/shardingsphere/shadow/distsql/parser/statement/CreateDefaultShadowAlgorithmStatement.java
index e463c6b7370..58351215473 100644
--- a/features/shadow/distsql/statement/src/main/java/org/apache/shardingsphere/shadow/distsql/parser/statement/CreateDefaultShadowAlgorithmStatement.java
+++ b/features/shadow/distsql/statement/src/main/java/org/apache/shardingsphere/shadow/distsql/parser/statement/CreateDefaultShadowAlgorithmStatement.java
@@ -18,16 +18,19 @@
 package org.apache.shardingsphere.shadow.distsql.parser.statement;
 
 import lombok.Getter;
-import lombok.RequiredArgsConstructor;
 import org.apache.shardingsphere.distsql.parser.statement.rdl.create.CreateRuleStatement;
 import org.apache.shardingsphere.shadow.distsql.parser.segment.ShadowAlgorithmSegment;
 
 /**
  * Create default shadow algorithm rule statement.
  */
-@RequiredArgsConstructor
 @Getter
 public final class CreateDefaultShadowAlgorithmStatement extends CreateRuleStatement {
     
     private final ShadowAlgorithmSegment shadowAlgorithmSegment;
+    
+    public CreateDefaultShadowAlgorithmStatement(final boolean ifNotExists, final ShadowAlgorithmSegment shadowAlgorithmSegment) {
+        super(ifNotExists);
+        this.shadowAlgorithmSegment = shadowAlgorithmSegment;
+    }
 }