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/27 07:11:59 UTC

[shardingsphere] branch master updated: Add IF `NOT EXISTS` to `create broadcast table rule` (#23105)

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 5da02b58565 Add IF `NOT EXISTS` to `create broadcast table rule` (#23105)
5da02b58565 is described below

commit 5da02b58565bfd6063fb48f22204c5b72c210242
Author: Zichao <57...@users.noreply.github.com>
AuthorDate: Tue Dec 27 20:11:52 2022 +1300

    Add IF `NOT EXISTS` to `create broadcast table rule` (#23105)
    
    * Add `IF NOT EXISTS` to `create broadcast table rule`
    
    * Add `IF NOT EXISTS` to `create broadcast table rule`
---
 .../sharding/create-broadcast-table-rule.cn.md     |   1 -
 .../sharding/create-broadcast-table-rule.en.md     |   4 +-
 .../sharding/show-broadcast-table-rule.cn.md       |   6 +-
 .../CreateBroadcastTableRuleStatementUpdater.java  |  14 ++-
 ...eateBroadcastTableRuleStatementUpdaterTest.java | 105 +++++++++++++++++++++
 .../main/antlr4/imports/sharding/RDLStatement.g4   |   2 +-
 .../core/ShardingDistSQLStatementVisitor.java      |   2 +-
 .../CreateBroadcastTableRuleStatement.java         |   7 +-
 8 files changed, 129 insertions(+), 12 deletions(-)

diff --git a/docs/document/content/reference/distsql/syntax/rdl/rule-definition/sharding/create-broadcast-table-rule.cn.md b/docs/document/content/reference/distsql/syntax/rdl/rule-definition/sharding/create-broadcast-table-rule.cn.md
index 941880893c8..e4f4cc4d1c7 100644
--- a/docs/document/content/reference/distsql/syntax/rdl/rule-definition/sharding/create-broadcast-table-rule.cn.md
+++ b/docs/document/content/reference/distsql/syntax/rdl/rule-definition/sharding/create-broadcast-table-rule.cn.md
@@ -20,7 +20,6 @@ tableName ::=
 ### 补充说明
 
 - `tableName` 可使用已经存在的表或者将要创建的表;
-- 只能存在一个广播规则,但可包含多个广播表,因此无法重复执行 `CREATE BROADCAST TABLE RULE`。
 
 ### 示例
 
diff --git a/docs/document/content/reference/distsql/syntax/rdl/rule-definition/sharding/create-broadcast-table-rule.en.md b/docs/document/content/reference/distsql/syntax/rdl/rule-definition/sharding/create-broadcast-table-rule.en.md
index c52a0f4e0f8..14e468f9637 100644
--- a/docs/document/content/reference/distsql/syntax/rdl/rule-definition/sharding/create-broadcast-table-rule.en.md
+++ b/docs/document/content/reference/distsql/syntax/rdl/rule-definition/sharding/create-broadcast-table-rule.en.md
@@ -20,9 +20,7 @@ tableName ::=
 
 ### Supplement
 
-- `tableName` can use an existing table or a table that will be created;
-- Only one broadcast rule can exist, but can contain multiple broadcast tables, so can not
-  execute `CREATE BROADCAST TABLE RULE` more than one time. 
+- `tableName` can use an existing table or a table that will be created.
 
 ### Example
 
diff --git a/docs/document/content/reference/distsql/syntax/rql/rule-query/sharding/show-broadcast-table-rule.cn.md b/docs/document/content/reference/distsql/syntax/rql/rule-query/sharding/show-broadcast-table-rule.cn.md
index 4f49fe1b07f..024447a369d 100644
--- a/docs/document/content/reference/distsql/syntax/rql/rule-query/sharding/show-broadcast-table-rule.cn.md
+++ b/docs/document/content/reference/distsql/syntax/rql/rule-query/sharding/show-broadcast-table-rule.cn.md
@@ -23,9 +23,9 @@ databaseName ::=
 
 ### 返回值说明
 
-| 列              | 说明      |
-|-----------------|-----------|
-| broadcast_table | 广播表名称 |
+| 列                | 说明      |
+|------------------|-----------|
+ |  broadcast_table | 广播表名称 |
 
 ### 示例
 
diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateBroadcastTableRuleStatementUpdater.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateBroadcastTableRuleStatementUpdater.java
index 012cf2f7f19..cae41635af4 100644
--- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateBroadcastTableRuleStatementUpdater.java
+++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateBroadcastTableRuleStatementUpdater.java
@@ -34,10 +34,15 @@ import java.util.LinkedList;
  */
 public final class CreateBroadcastTableRuleStatementUpdater implements RuleDefinitionCreateUpdater<CreateBroadcastTableRuleStatement, ShardingRuleConfiguration> {
     
+    private boolean ifNotExists;
+    
     @Override
     public void checkSQLStatement(final ShardingSphereDatabase database, final CreateBroadcastTableRuleStatement sqlStatement,
                                   final ShardingRuleConfiguration currentRuleConfig) throws RuleDefinitionViolationException {
-        checkDuplicate(sqlStatement, currentRuleConfig);
+        ifNotExists = sqlStatement.isIfNotExists();
+        if (!ifNotExists) {
+            checkDuplicate(sqlStatement, currentRuleConfig);
+        }
     }
     
     private void checkDuplicate(final CreateBroadcastTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) throws RuleInUsedException {
@@ -59,6 +64,13 @@ public final class CreateBroadcastTableRuleStatementUpdater implements RuleDefin
     @Override
     public void updateCurrentRuleConfiguration(final ShardingRuleConfiguration currentRuleConfig, final ShardingRuleConfiguration toBeCreatedRuleConfig) {
         if (null != currentRuleConfig) {
+            if (ifNotExists) {
+                Collection<String> currentBroadCastTables = currentRuleConfig.getBroadcastTables();
+                toBeCreatedRuleConfig.getBroadcastTables().removeIf(currentBroadCastTables::contains);
+            }
+            if (toBeCreatedRuleConfig.getBroadcastTables().isEmpty()) {
+                return;
+            }
             currentRuleConfig.getBroadcastTables().addAll(toBeCreatedRuleConfig.getBroadcastTables());
         }
     }
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateBroadcastTableRuleStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateBroadcastTableRuleStatementUpdaterTest.java
new file mode 100644
index 00000000000..571ff5756bc
--- /dev/null
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateBroadcastTableRuleStatementUpdaterTest.java
@@ -0,0 +1,105 @@
+/*
+ * 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.sharding.distsql.update;
+
+import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import org.apache.shardingsphere.sharding.distsql.handler.update.CreateBroadcastTableRuleStatementUpdater;
+import org.apache.shardingsphere.sharding.distsql.parser.statement.CreateBroadcastTableRuleStatement;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.LinkedList;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class CreateBroadcastTableRuleStatementUpdaterTest {
+    
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+    private ShardingSphereDatabase database;
+    
+    private final CreateBroadcastTableRuleStatementUpdater updater = new CreateBroadcastTableRuleStatementUpdater();
+    
+    @Test
+    public void assertCreateBroadcastRule() {
+        ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration();
+        CreateBroadcastTableRuleStatement sqlStatement = new CreateBroadcastTableRuleStatement(false, Arrays.asList("t_1", "t_2"));
+        updater.checkSQLStatement(database, sqlStatement, currentRuleConfig);
+        ShardingRuleConfiguration toBeCreatedRuleConfig = updater.buildToBeCreatedRuleConfiguration(sqlStatement);
+        updater.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig);
+        assertThat(currentRuleConfig.getBroadcastTables().size(), is(2));
+        assertTrue(currentRuleConfig.getBroadcastTables().contains("t_1"));
+        assertTrue(currentRuleConfig.getBroadcastTables().contains("t_2"));
+    }
+    
+    @Test(expected = DuplicateRuleException.class)
+    public void assertCheckDuplicatedBroadcastTable() {
+        ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
+        CreateBroadcastTableRuleStatement sqlStatement = new CreateBroadcastTableRuleStatement(false, Arrays.asList("t_order", "t_address"));
+        updater.checkSQLStatement(database, sqlStatement, currentRuleConfig);
+    }
+    
+    @Test
+    public void assertCreateBroadcastRuleWhenBroadcastRuleExistsInCurrentRuleConfig() {
+        ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
+        CreateBroadcastTableRuleStatement sqlStatement = new CreateBroadcastTableRuleStatement(false, Arrays.asList("t_1", "t_2"));
+        updater.checkSQLStatement(database, sqlStatement, currentRuleConfig);
+        ShardingRuleConfiguration toBeCreatedRuleConfig = updater.buildToBeCreatedRuleConfiguration(sqlStatement);
+        updater.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig);
+        assertThat(currentRuleConfig.getBroadcastTables().size(), is(4));
+        assertTrue(currentRuleConfig.getBroadcastTables().contains("t_1"));
+        assertTrue(currentRuleConfig.getBroadcastTables().contains("t_2"));
+        assertTrue(currentRuleConfig.getBroadcastTables().contains("t_order"));
+        assertTrue(currentRuleConfig.getBroadcastTables().contains("t_address"));
+    }
+    
+    @Test
+    public void assertCreateBroadcastRuleWithIfNotExist() {
+        Collection<String> tables = new LinkedList<>();
+        tables.add("t_1");
+        tables.add("t_2");
+        tables.add("t_order");
+        tables.add("t_address");
+        ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
+        CreateBroadcastTableRuleStatement sqlStatement = new CreateBroadcastTableRuleStatement(true, tables);
+        updater.checkSQLStatement(database, sqlStatement, currentRuleConfig);
+        ShardingRuleConfiguration toBeCreatedRuleConfig = updater.buildToBeCreatedRuleConfiguration(sqlStatement);
+        updater.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig);
+        assertThat(currentRuleConfig.getBroadcastTables().size(), is(4));
+        assertTrue(currentRuleConfig.getBroadcastTables().contains("t_1"));
+        assertTrue(currentRuleConfig.getBroadcastTables().contains("t_2"));
+        assertTrue(currentRuleConfig.getBroadcastTables().contains("t_order"));
+        assertTrue(currentRuleConfig.getBroadcastTables().contains("t_address"));
+    }
+    
+    private ShardingRuleConfiguration createCurrentRuleConfiguration() {
+        ShardingRuleConfiguration result = new ShardingRuleConfiguration();
+        result.getBroadcastTables().add("t_order");
+        result.getBroadcastTables().add("t_address");
+        return result;
+    }
+}
diff --git a/features/sharding/distsql/parser/src/main/antlr4/imports/sharding/RDLStatement.g4 b/features/sharding/distsql/parser/src/main/antlr4/imports/sharding/RDLStatement.g4
index cc633a197ef..8d97b5d56d8 100644
--- a/features/sharding/distsql/parser/src/main/antlr4/imports/sharding/RDLStatement.g4
+++ b/features/sharding/distsql/parser/src/main/antlr4/imports/sharding/RDLStatement.g4
@@ -44,7 +44,7 @@ dropShardingTableReferenceRule
     ;
 
 createBroadcastTableRule
-    : CREATE BROADCAST TABLE RULE tableName (COMMA_ tableName)*
+    : CREATE BROADCAST TABLE RULE ifNotExists? tableName (COMMA_ tableName)*
     ;
 
 dropBroadcastTableRule
diff --git a/features/sharding/distsql/parser/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/core/ShardingDistSQLStatementVisitor.java b/features/sharding/distsql/parser/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/core/ShardingDistSQLStatementVisitor.java
index 5b63c168386..1ae81d1de6a 100644
--- a/features/sharding/distsql/parser/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/core/ShardingDistSQLStatementVisitor.java
+++ b/features/sharding/distsql/parser/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/core/ShardingDistSQLStatementVisitor.java
@@ -156,7 +156,7 @@ public final class ShardingDistSQLStatementVisitor extends ShardingDistSQLStatem
     
     @Override
     public ASTNode visitCreateBroadcastTableRule(final CreateBroadcastTableRuleContext ctx) {
-        return new CreateBroadcastTableRuleStatement(ctx.tableName().stream().map(this::getIdentifierValue).collect(Collectors.toList()));
+        return new CreateBroadcastTableRuleStatement(null != ctx.ifNotExists(), ctx.tableName().stream().map(this::getIdentifierValue).collect(Collectors.toList()));
     }
     
     @Override
diff --git a/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/statement/CreateBroadcastTableRuleStatement.java b/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/statement/CreateBroadcastTableRuleStatement.java
index 3a8775fb6d3..70a3b56e744 100644
--- a/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/statement/CreateBroadcastTableRuleStatement.java
+++ b/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/statement/CreateBroadcastTableRuleStatement.java
@@ -18,7 +18,6 @@
 package org.apache.shardingsphere.sharding.distsql.parser.statement;
 
 import lombok.Getter;
-import lombok.RequiredArgsConstructor;
 import org.apache.shardingsphere.distsql.parser.statement.rdl.create.CreateRuleStatement;
 
 import java.util.Collection;
@@ -26,9 +25,13 @@ import java.util.Collection;
 /**
  * Create broadcast table rule statement.
  */
-@RequiredArgsConstructor
 @Getter
 public final class CreateBroadcastTableRuleStatement extends CreateRuleStatement {
     
     private final Collection<String> tables;
+    
+    public CreateBroadcastTableRuleStatement(final boolean ifNotExists, final Collection<String> tables) {
+        super(ifNotExists);
+        this.tables = tables;
+    }
 }