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 674b4d40b7b Add sharding algorithm type check (#18699)
674b4d40b7b is described below

commit 674b4d40b7b66a464fa44444aced7fa34f7e3a41
Author: lanchengx <[email protected]>
AuthorDate: Fri Jul 1 10:26:42 2022 +0800

    Add sharding algorithm type check (#18699)
    
    * Add sharding algorithm type check
    
    * Add sharding algorithm type check
    
    * Add sharding algorithm type check
---
 .../checker/ShardingTableRuleStatementChecker.java | 33 +++++++++++++---------
 .../checker/ShardingRuleStatementCheckerTest.java  | 15 ++++++++--
 .../main/antlr4/imports/sharding/RDLStatement.g4   |  6 +++-
 .../core/ShardingDistSQLStatementVisitor.java      |  8 +++++-
 4 files changed, 44 insertions(+), 18 deletions(-)

diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
index fa5360110f5..23290de25aa 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
@@ -20,6 +20,7 @@ package 
org.apache.shardingsphere.sharding.distsql.handler.checker;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
 import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
+import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
 import 
org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
 import org.apache.shardingsphere.infra.datanode.DataNode;
 import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
@@ -52,6 +53,7 @@ import 
org.apache.shardingsphere.sharding.factory.ShardingAlgorithmFactory;
 import 
org.apache.shardingsphere.sharding.rule.BindingTableCheckedConfiguration;
 import org.apache.shardingsphere.sharding.rule.TableRule;
 import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
+import org.apache.shardingsphere.spi.type.typed.TypedSPI;
 
 import java.util.Arrays;
 import java.util.Collection;
@@ -108,7 +110,7 @@ public final class ShardingTableRuleStatementChecker {
     private static void check(final ShardingSphereDatabase database,
                               final Collection<AbstractTableRuleSegment> 
rules, final ShardingRuleConfiguration currentRuleConfig, final boolean 
isCreate) throws DistSQLException {
         String databaseName = database.getName();
-        checkShardingTables(databaseName, rules, currentRuleConfig, isCreate);
+        checkTables(databaseName, rules, currentRuleConfig, isCreate);
         checkResources(databaseName, rules, database);
         checkKeyGenerators(rules, currentRuleConfig);
         Map<String, List<AbstractTableRuleSegment>> groupedTableRule = 
groupingByClassType(rules);
@@ -183,12 +185,12 @@ public final class ShardingTableRuleStatementChecker {
     }
     
     private static Collection<String> getDataSourceNames(final 
ShardingAutoTableRuleConfiguration shardingAutoTableRuleConfig) {
-        List<String> actualDataSources = new 
InlineExpressionParser(shardingAutoTableRuleConfig.getActualDataSources()).splitAndEvaluate();
+        Collection<String> actualDataSources = new 
InlineExpressionParser(shardingAutoTableRuleConfig.getActualDataSources()).splitAndEvaluate();
         return new HashSet<>(actualDataSources);
     }
     
     private static Collection<String> getDataSourceNames(final 
ShardingTableRuleConfiguration shardingTableRuleConfig) {
-        List<String> actualDataNodes = new 
InlineExpressionParser(shardingTableRuleConfig.getActualDataNodes()).splitAndEvaluate();
+        Collection<String> actualDataNodes = new 
InlineExpressionParser(shardingTableRuleConfig.getActualDataNodes()).splitAndEvaluate();
         return actualDataNodes.stream().map(each -> new 
DataNode(each).getDataSourceName()).collect(Collectors.toList());
     }
     
@@ -196,28 +198,28 @@ public final class ShardingTableRuleStatementChecker {
         return dataNodeStr.contains(DELIMITER) && 2 == 
Splitter.on(DELIMITER).omitEmptyStrings().splitToList(dataNodeStr).size();
     }
     
-    private static void checkShardingTables(final String databaseName, final 
Collection<AbstractTableRuleSegment> rules,
-                                            final ShardingRuleConfiguration 
currentRuleConfig, final boolean isCreate) throws DistSQLException {
-        Collection<String> requiredShardingTables = 
rules.stream().map(AbstractTableRuleSegment::getLogicTable).collect(Collectors.toList());
-        Collection<String> duplicatedShardingTables = 
getDuplicate(requiredShardingTables);
-        DistSQLException.predictionThrow(duplicatedShardingTables.isEmpty(), 
() -> new DuplicateRuleException("sharding", databaseName, 
duplicatedShardingTables));
+    private static void checkTables(final String databaseName, final 
Collection<AbstractTableRuleSegment> rules,
+                                    final ShardingRuleConfiguration 
currentRuleConfig, final boolean isCreate) throws DistSQLException {
+        Collection<String> requiredTables = 
rules.stream().map(AbstractTableRuleSegment::getLogicTable).collect(Collectors.toList());
+        Collection<String> duplicatedTables = getDuplicate(requiredTables);
+        DistSQLException.predictionThrow(duplicatedTables.isEmpty(), () -> new 
DuplicateRuleException("sharding", databaseName, duplicatedTables));
         Collection<String> currentShardingTables = null == currentRuleConfig ? 
Collections.emptyList() : getCurrentShardingTables(currentRuleConfig);
         if (isCreate) {
-            Set<String> identical = getIdentical(requiredShardingTables, 
currentShardingTables);
+            Collection<String> identical = getIdentical(requiredTables, 
currentShardingTables);
             DistSQLException.predictionThrow(identical.isEmpty(), () -> new 
DuplicateRuleException("sharding", databaseName, identical));
         } else {
-            Set<String> different = getDifferent(requiredShardingTables, 
currentShardingTables);
+            Collection<String> different = getDifferent(requiredTables, 
currentShardingTables);
             DistSQLException.predictionThrow(different.isEmpty(), () -> new 
RequiredRuleMissedException("sharding", databaseName, different));
         }
     }
     
-    private static Set<String> getDuplicate(final Collection<String> 
collection) {
+    private static Collection<String> getDuplicate(final Collection<String> 
collection) {
         Collection<String> duplicate = 
collection.stream().collect(Collectors.groupingBy(String::toLowerCase, 
Collectors.counting())).entrySet().stream()
                 .filter(each -> each.getValue() > 
1).map(Entry::getKey).collect(Collectors.toSet());
         return collection.stream().filter(each -> 
containsIgnoreCase(duplicate, each)).collect(Collectors.toSet());
     }
     
-    private static Set<String> getIdentical(final Collection<String> require, 
final Collection<String> current) {
+    private static Collection<String> getIdentical(final Collection<String> 
require, final Collection<String> current) {
         return require.stream().filter(each -> containsIgnoreCase(current, 
each)).collect(Collectors.toSet());
     }
     
@@ -252,16 +254,19 @@ public final class ShardingTableRuleStatementChecker {
         Collection<AutoTableRuleSegment> autoTableRules = 
rules.stream().map(each -> (AutoTableRuleSegment) 
each).collect(Collectors.toList());
         Optional<AlgorithmSegment> anyAutoTableRule = 
autoTableRules.stream().map(AutoTableRuleSegment::getShardingAlgorithmSegment).filter(Objects::nonNull).findAny();
         if (anyAutoTableRule.isPresent()) {
-            checkShardingAlgorithms(autoTableRules);
+            checkAutoTableShardingAlgorithms(autoTableRules);
         }
     }
     
-    private static void checkShardingAlgorithms(final 
Collection<AutoTableRuleSegment> rules) throws DistSQLException {
+    private static void checkAutoTableShardingAlgorithms(final 
Collection<AutoTableRuleSegment> rules) throws DistSQLException {
         Collection<AutoTableRuleSegment> incompleteShardingRules = 
rules.stream().filter(each -> 
!each.isCompleteShardingAlgorithm()).collect(Collectors.toList());
         DistSQLException.predictionThrow(incompleteShardingRules.isEmpty(), () 
-> new InvalidAlgorithmConfigurationException("sharding"));
         Collection<String> invalidShardingAlgorithms = rules.stream().map(each 
-> each.getShardingAlgorithmSegment().getName()).distinct()
                 .filter(each -> 
!ShardingAlgorithmFactory.contains(each)).collect(Collectors.toList());
         DistSQLException.predictionThrow(invalidShardingAlgorithms.isEmpty(), 
() -> new InvalidAlgorithmConfigurationException("sharding", 
invalidShardingAlgorithms));
+        
invalidShardingAlgorithms.addAll(rules.stream().map(AutoTableRuleSegment::getShardingAlgorithmSegment).map(each
 -> new ShardingSphereAlgorithmConfiguration(each.getName(), each.getProps()))
+                .map(ShardingAlgorithmFactory::newInstance).filter(each -> 
!(each instanceof 
ShardingAutoTableAlgorithm)).map(TypedSPI::getType).collect(Collectors.toList()));
+        DistSQLException.predictionThrow(invalidShardingAlgorithms.isEmpty(), 
() -> new InvalidAlgorithmConfigurationException("sharding", 
invalidShardingAlgorithms));
     }
     
     private static void checkTableRule(final String databaseName, final 
ShardingRuleConfiguration currentRuleConfig, final 
Collection<AbstractTableRuleSegment> rules) throws DistSQLException {
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/checker/ShardingRuleStatementCheckerTest.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/checker/ShardingRuleStatementCheckerTest.java
index 731c419f0a0..4bfe3fa97e1 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/checker/ShardingRuleStatementCheckerTest.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/checker/ShardingRuleStatementCheckerTest.java
@@ -125,10 +125,21 @@ public final class ShardingRuleStatementCheckerTest {
     }
     
     @Test(expected = InvalidAlgorithmConfigurationException.class)
-    public void assertCheckAutoTableWithInvalidShardingAlgorithms() throws 
DistSQLException {
+    public void assertCheckAutoTableWithNotExistShardingAlgorithms() throws 
DistSQLException {
         AutoTableRuleSegment autoTableRuleSegment = new 
AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"));
         autoTableRuleSegment.setShardingColumn("product_id");
-        autoTableRuleSegment.setShardingAlgorithmSegment(new 
AlgorithmSegment("invalid", newProperties("", "")));
+        autoTableRuleSegment.setShardingAlgorithmSegment(new 
AlgorithmSegment("not_exist", newProperties("", "")));
+        List<AbstractTableRuleSegment> rules = 
Collections.singletonList(autoTableRuleSegment);
+        ShardingTableRuleStatementChecker.checkCreation(database, rules, 
shardingRuleConfig);
+        autoTableRuleSegment.setShardingAlgorithmSegment(new 
AlgorithmSegment("complex", newProperties("", "")));
+        ShardingTableRuleStatementChecker.checkCreation(database, rules, 
shardingRuleConfig);
+    }
+    
+    @Test(expected = InvalidAlgorithmConfigurationException.class)
+    public void assertCheckAutoTableWithComplexShardingAlgorithms() throws 
DistSQLException {
+        AutoTableRuleSegment autoTableRuleSegment = new 
AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"));
+        autoTableRuleSegment.setShardingColumn("product_id");
+        autoTableRuleSegment.setShardingAlgorithmSegment(new 
AlgorithmSegment("complex", newProperties("", "")));
         List<AbstractTableRuleSegment> rules = 
Collections.singletonList(autoTableRuleSegment);
         ShardingTableRuleStatementChecker.checkCreation(database, rules, 
shardingRuleConfig);
     }
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/imports/sharding/RDLStatement.g4
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/imports/sharding/RDLStatement.g4
index 0c147c5fd09..ae0e3fcbf32 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/imports/sharding/RDLStatement.g4
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/imports/sharding/RDLStatement.g4
@@ -96,7 +96,7 @@ shardingTableRuleDefinition
     ;
 
 shardingAutoTableRule
-    : tableName LP resources COMMA shardingColumnDefinition COMMA 
algorithmDefinition (COMMA keyGenerateDeclaration)? RP
+    : tableName LP resources COMMA autoShardingColumnDefinition COMMA 
algorithmDefinition (COMMA keyGenerateDeclaration)? RP
     ;
 
 shardingTableRule
@@ -127,6 +127,10 @@ dataNode
     : IDENTIFIER | STRING
     ;
 
+autoShardingColumnDefinition
+    : shardingColumn
+    ;
+
 shardingColumnDefinition
     : shardingColumn | shardingColumns
     ;
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/core/ShardingDistSQLStatementVisitor.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/core/ShardingDistSQLStatementVisitor.java
index 1bd8285ede9..e1c634438e6 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/core/ShardingDistSQLStatementVisitor.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/core/ShardingDistSQLStatementVisitor.java
@@ -29,6 +29,7 @@ import 
org.apache.shardingsphere.distsql.parser.autogen.ShardingDistSQLStatement
 import 
org.apache.shardingsphere.distsql.parser.autogen.ShardingDistSQLStatementParser.AlterShardingBroadcastTableRulesContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.ShardingDistSQLStatementParser.AlterShardingKeyGeneratorContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.ShardingDistSQLStatementParser.AlterShardingTableRuleContext;
+import 
org.apache.shardingsphere.distsql.parser.autogen.ShardingDistSQLStatementParser.AutoShardingColumnDefinitionContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.ShardingDistSQLStatementParser.BindTableRulesDefinitionContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.ShardingDistSQLStatementParser.ClearShardingHintContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.ShardingDistSQLStatementParser.CreateDefaultShardingStrategyContext;
@@ -323,7 +324,7 @@ public final class ShardingDistSQLStatementVisitor extends 
ShardingDistSQLStatem
         Collection<String> dataSources = getResources(ctx.resources());
         AutoTableRuleSegment result = new AutoTableRuleSegment(tableName, 
dataSources);
         Optional.ofNullable(ctx.keyGenerateDeclaration()).ifPresent(optional 
-> result.setKeyGenerateStrategySegment((KeyGenerateStrategySegment) 
visit(ctx.keyGenerateDeclaration())));
-        Optional.ofNullable(ctx.shardingColumnDefinition()).ifPresent(optional 
-> 
result.setShardingColumn(buildShardingColumn(ctx.shardingColumnDefinition())));
+        
Optional.ofNullable(ctx.autoShardingColumnDefinition()).ifPresent(optional -> 
result.setShardingColumn(buildShardingColumn(ctx.autoShardingColumnDefinition())));
         Optional.ofNullable(ctx.algorithmDefinition()).ifPresent(optional -> 
result.setShardingAlgorithmSegment((AlgorithmSegment) 
visit(ctx.algorithmDefinition())));
         return result;
     }
@@ -460,6 +461,11 @@ public final class ShardingDistSQLStatementVisitor extends 
ShardingDistSQLStatem
         return new 
ShowUnusedShardingAlgorithmsStatement(Objects.nonNull(ctx.databaseName()) ? 
(DatabaseSegment) visit(ctx.databaseName()) : null);
     }
     
+    private String buildShardingColumn(final 
AutoShardingColumnDefinitionContext ctx) {
+        String result = null != ctx.shardingColumn() ? 
getIdentifierValue(ctx.shardingColumn().columnName()) : "";
+        return result.isEmpty() ? null : result;
+    }
+    
     private String buildShardingColumn(final ShardingColumnDefinitionContext 
ctx) {
         String result = Optional.ofNullable(ctx.shardingColumn()).map(optional 
-> getIdentifierValue(optional.columnName()))
                 .orElseGet(() -> 
ctx.shardingColumns().columnName().stream().map(this::getIdentifierValue).collect(Collectors.joining(",")));

Reply via email to