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/29 11:37:30 UTC

[shardingsphere] branch master updated: add validate of creating view for broadcast table (#18657)

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 bc1d472306e add validate of creating view for broadcast table (#18657)
bc1d472306e is described below

commit bc1d472306eceda355d896181b183a5b71c550c4
Author: Chuxin Chen <ch...@qq.com>
AuthorDate: Wed Jun 29 19:37:18 2022 +0800

    add validate of creating view for broadcast table (#18657)
    
    * update read-write-split doc
    
    * add view check for broadcast table
    
    * add validate for creating view as select of broadcastTable
---
 .../impl/ShardingCreateViewStatementValidator.java | 24 +++++++++++++++++-----
 .../ShardingCreateViewStatementValidatorTest.java  |  7 +++++++
 2 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidator.java
index e3513c08b1f..66e5c29fe73 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidator.java
@@ -34,6 +34,7 @@ import org.apache.shardingsphere.sql.parser.sql.dialect.handler.dml.SelectStatem
 
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Optional;
 
@@ -49,9 +50,15 @@ public final class ShardingCreateViewStatementValidator extends ShardingDDLState
         if (!selectStatement.isPresent()) {
             return;
         }
-        if (isShardingTablesWithoutBinding(shardingRule, sqlStatementContext, selectStatement.get())) {
+        TableExtractor extractor = new TableExtractor();
+        extractor.extractTablesFromSelect(selectStatement.get());
+        Collection<SimpleTableSegment> tableSegments = extractor.getRewriteTables();
+        if (isShardingTablesWithoutBinding(shardingRule, sqlStatementContext, tableSegments)) {
             throw new ShardingSphereException("View name has to bind to sharding tables!");
         }
+        if (isAllBroadcastTablesWithoutConfigView(shardingRule, sqlStatementContext, tableSegments)) {
+            throw new ShardingSphereException("View name has to config as broadcast table!");
+        }
     }
     
     @Override
@@ -66,10 +73,8 @@ public final class ShardingCreateViewStatementValidator extends ShardingDDLState
         }
     }
     
-    private boolean isShardingTablesWithoutBinding(final ShardingRule shardingRule, final SQLStatementContext<CreateViewStatement> sqlStatementContext, final SelectStatement selectStatement) {
-        TableExtractor extractor = new TableExtractor();
-        extractor.extractTablesFromSelect(selectStatement);
-        Collection<SimpleTableSegment> tableSegments = extractor.getRewriteTables();
+    private boolean isShardingTablesWithoutBinding(final ShardingRule shardingRule, final SQLStatementContext<CreateViewStatement> sqlStatementContext,
+                                                   final Collection<SimpleTableSegment> tableSegments) {
         for (SimpleTableSegment each : tableSegments) {
             String logicTable = each.getTableName().getIdentifier().getValue();
             if (shardingRule.isShardingTable(logicTable) && !isBindingTables(shardingRule, sqlStatementContext.getSqlStatement().getView().getTableName().getIdentifier().getValue(), logicTable)) {
@@ -84,6 +89,15 @@ public final class ShardingCreateViewStatementValidator extends ShardingDDLState
         return shardingRule.isAllBindingTables(bindTables);
     }
     
+    private boolean isAllBroadcastTablesWithoutConfigView(final ShardingRule shardingRule, final SQLStatementContext<CreateViewStatement> sqlStatementContext,
+                                                          final Collection<SimpleTableSegment> tableSegments) {
+        Collection<String> tables = new LinkedList<>();
+        for (SimpleTableSegment each : tableSegments) {
+            tables.add(each.getTableName().getIdentifier().getValue());
+        }
+        return shardingRule.isAllBroadcastTables(tables) && !shardingRule.isBroadcastTable(sqlStatementContext.getSqlStatement().getView().getTableName().getIdentifier().getValue());
+    }
+    
     private boolean isContainsNotSupportedViewStatement(final SelectStatement selectStatement, final RouteContext routeContext) {
         if (routeContext.getRouteUnits().size() <= 1) {
             return false;
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateViewStatementValidatorTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateViewStatementValidatorTest.java
index a8d935924dc..0c0502906e4 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateViewStatementValidatorTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateViewStatementValidatorTest.java
@@ -101,4 +101,11 @@ public final class ShardingCreateViewStatementValidatorTest {
         new ShardingCreateViewStatementValidator().postValidate(shardingRule, createViewStatementContext, Collections.emptyList(), mock(ShardingSphereDatabase.class),
                 mock(ConfigurationProperties.class), routeContext);
     }
+    
+    @Test(expected = ShardingSphereException.class)
+    public void assertPreValidateCreateViewWithBroadcastTable() {
+        when(shardingRule.isAllBroadcastTables(any())).thenReturn(true);
+        when(shardingRule.isBroadcastTable("order_view")).thenReturn(false);
+        new ShardingCreateViewStatementValidator().preValidate(shardingRule, createViewStatementContext, Collections.emptyList(), mock(ShardingSphereDatabase.class));
+    }
 }