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

yx9o 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 494a1192a23 Check inline expression when create sharding table rule 
with inline sharding algorithm (#33735)
494a1192a23 is described below

commit 494a1192a23cfcd38e045af3519ced0d3d9acf74
Author: Raigor <[email protected]>
AuthorDate: Wed Nov 20 21:16:49 2024 +0800

    Check inline expression when create sharding table rule with inline 
sharding algorithm (#33735)
    
    * Check inline expression when create sharding table rule with inline 
sharding algorithm
    
    * Update RELEASE-NOTES.md
---
 RELEASE-NOTES.md                                   |  1 +
 .../sharding/constant/ShardingTableConstants.java  | 34 +++++++++++
 .../sharding/rule/ShardingTable.java               | 16 ++---
 .../checker/ShardingTableRuleStatementChecker.java | 69 ++++++++++++++++++----
 .../ShardingTableRuleStatementConverter.java       | 16 ++++-
 .../ShardingTableRuleStatementCheckerTest.java     |  7 ++-
 .../CreateShardingTableRuleExecutorTest.java       | 22 +++++++
 7 files changed, 139 insertions(+), 26 deletions(-)

diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md
index 5620de9569b..fb6ee36bd1e 100644
--- a/RELEASE-NOTES.md
+++ b/RELEASE-NOTES.md
@@ -20,6 +20,7 @@
 1. Agent: Simplify the use of Agent's Docker Image - 
[#33356](https://github.com/apache/shardingsphere/pull/33356)
 1. Build: Avoid using `-proc:full` when compiling ShardingSphere with 
OpenJDK23 - [#33681](https://github.com/apache/shardingsphere/pull/33681)
 1. Doc: Adds documentation for HiveServer2 support - 
[#33717](https://github.com/apache/shardingsphere/pull/33717)
+1. DistSQL: Check inline expression when create sharding table rule with 
inline sharding algorithm - 
[#33735](https://github.com/apache/shardingsphere/pull/33735)
 
 ### Bug Fixes
 
diff --git 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/constant/ShardingTableConstants.java
 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/constant/ShardingTableConstants.java
new file mode 100644
index 00000000000..8a30cf76651
--- /dev/null
+++ 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/constant/ShardingTableConstants.java
@@ -0,0 +1,34 @@
+/*
+ * 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.constant;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+import java.util.regex.Pattern;
+
+/**
+ * Sharding table constants.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class ShardingTableConstants {
+    
+    public static final Pattern DATA_NODE_SUFFIX_PATTERN = 
Pattern.compile("\\d+$");
+    
+    public static final char DEFAULT_PADDING_CHAR = '0';
+}
diff --git 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingTable.java
 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingTable.java
index d5ad5b7efdd..e49310b58cb 100644
--- 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingTable.java
+++ 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingTable.java
@@ -33,6 +33,7 @@ import 
org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerate
 import 
org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration;
 import 
org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
 import 
org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorithm;
+import org.apache.shardingsphere.sharding.constant.ShardingTableConstants;
 import 
org.apache.shardingsphere.sharding.exception.metadata.DataNodeGenerateException;
 import 
org.apache.shardingsphere.sharding.exception.metadata.MissingRequiredDataNodesException;
 
@@ -47,7 +48,6 @@ import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
 import java.util.TreeSet;
-import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
 /**
@@ -57,10 +57,6 @@ import java.util.stream.Collectors;
 @ToString(exclude = {"dataNodeIndexMap", "actualTables", 
"actualDataSourceNames", "dataSourceDataNode", "tableDataNode"})
 public final class ShardingTable {
     
-    private static final Pattern DATA_NODE_SUFFIX_PATTERN = 
Pattern.compile("\\d+$");
-    
-    private static final char DEFAULT_PADDING_CHAR = '0';
-    
     private final String logicTable;
     
     private final List<DataNode> actualDataNodes;
@@ -140,17 +136,17 @@ public final class ShardingTable {
     }
     
     private DataNodeInfo createDataSourceDataNode(final Collection<DataNode> 
actualDataNodes) {
-        String prefix = 
DATA_NODE_SUFFIX_PATTERN.matcher(actualDataNodes.iterator().next().getDataSourceName()).replaceAll("");
+        String prefix = 
ShardingTableConstants.DATA_NODE_SUFFIX_PATTERN.matcher(actualDataNodes.iterator().next().getDataSourceName()).replaceAll("");
         int suffixMinLength = actualDataNodes.stream().map(each -> 
each.getDataSourceName().length() - 
prefix.length()).min(Comparator.comparing(Integer::intValue)).orElse(1);
-        return new DataNodeInfo(prefix, suffixMinLength, DEFAULT_PADDING_CHAR);
+        return new DataNodeInfo(prefix, suffixMinLength, 
ShardingTableConstants.DEFAULT_PADDING_CHAR);
     }
     
     private DataNodeInfo createTableDataNode(final Collection<DataNode> 
actualDataNodes) {
         String tableName = actualDataNodes.iterator().next().getTableName();
-        String prefix = tableName.startsWith(logicTable) ? logicTable + 
DATA_NODE_SUFFIX_PATTERN.matcher(tableName.substring(logicTable.length())).replaceAll("")
-                : DATA_NODE_SUFFIX_PATTERN.matcher(tableName).replaceAll("");
+        String prefix = tableName.startsWith(logicTable) ? logicTable + 
ShardingTableConstants.DATA_NODE_SUFFIX_PATTERN.matcher(tableName.substring(logicTable.length())).replaceAll("")
+                : 
ShardingTableConstants.DATA_NODE_SUFFIX_PATTERN.matcher(tableName).replaceAll("");
         int suffixMinLength = actualDataNodes.stream().map(each -> 
each.getTableName().length() - 
prefix.length()).min(Comparator.comparing(Integer::intValue)).orElse(1);
-        return new DataNodeInfo(prefix, suffixMinLength, DEFAULT_PADDING_CHAR);
+        return new DataNodeInfo(prefix, suffixMinLength, 
ShardingTableConstants.DEFAULT_PADDING_CHAR);
     }
     
     private List<String> getDataNodes(final ShardingAutoTableRuleConfiguration 
tableRuleConfig, final ShardingAutoTableAlgorithm shardingAlgorithm, final 
Collection<String> dataSourceNames) {
diff --git 
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
 
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
index eb5c7407e4e..ee48e6cc370 100644
--- 
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
+++ 
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
@@ -26,15 +26,17 @@ import 
org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitial
 import 
org.apache.shardingsphere.infra.algorithm.core.exception.InvalidAlgorithmConfigurationException;
 import 
org.apache.shardingsphere.infra.algorithm.keygen.core.KeyGenerateAlgorithm;
 import org.apache.shardingsphere.infra.datanode.DataNode;
+import org.apache.shardingsphere.infra.datanode.DataNodeInfo;
 import 
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
+import 
org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.MissingRequiredStorageUnitsException;
 import 
org.apache.shardingsphere.infra.exception.kernel.metadata.rule.DuplicateRuleException;
 import 
org.apache.shardingsphere.infra.exception.kernel.metadata.rule.InvalidRuleConfigurationException;
 import 
org.apache.shardingsphere.infra.exception.kernel.metadata.rule.MissingRequiredRuleException;
-import 
org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.MissingRequiredStorageUnitsException;
 import org.apache.shardingsphere.infra.expr.core.InlineExpressionParserFactory;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import 
org.apache.shardingsphere.infra.rule.attribute.datasource.DataSourceMapperRuleAttribute;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import 
org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineShardingAlgorithm;
 import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
 import 
org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
 import 
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableReferenceRuleConfiguration;
@@ -45,6 +47,8 @@ import 
org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShard
 import 
org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
 import 
org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
 import 
org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorithm;
+import 
org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
+import org.apache.shardingsphere.sharding.constant.ShardingTableConstants;
 import 
org.apache.shardingsphere.sharding.distsql.handler.converter.ShardingTableRuleStatementConverter;
 import 
org.apache.shardingsphere.sharding.distsql.handler.enums.ShardingStrategyType;
 import 
org.apache.shardingsphere.sharding.distsql.segment.strategy.AuditStrategySegment;
@@ -65,6 +69,7 @@ import 
org.apache.shardingsphere.sharding.spi.ShardingAuditAlgorithm;
 
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -422,8 +427,7 @@ public final class ShardingTableRuleStatementChecker {
                     ShardingSpherePreconditions.checkState(1 == 
requiredDataSources.size(),
                             () -> new 
InvalidShardingStrategyConfigurationException("database", 
databaseStrategySegment.get().getType()));
                 } else {
-                    AlgorithmSegment databaseShardingAlgorithm = 
databaseStrategySegment.get().getShardingAlgorithm();
-                    checkDatabaseShardingAlgorithm(each, 
databaseShardingAlgorithm);
+                    checkDatabaseShardingAlgorithm(each, 
databaseStrategySegment.get());
                 }
             }
             Optional<ShardingStrategySegment> tableStrategySegment = 
Optional.ofNullable(each.getTableStrategySegment());
@@ -433,30 +437,39 @@ public final class ShardingTableRuleStatementChecker {
                     ShardingSpherePreconditions.checkState(1 == 
requiredTables.size(),
                             () -> new 
InvalidShardingStrategyConfigurationException("table", 
tableStrategySegment.get().getType()));
                 } else {
-                    AlgorithmSegment tableShardingAlgorithm = 
tableStrategySegment.get().getShardingAlgorithm();
-                    checkTableShardingAlgorithm(each, tableShardingAlgorithm);
+                    checkTableShardingAlgorithm(each, 
tableStrategySegment.get());
                 }
             }
         }
     }
     
-    private static void checkDatabaseShardingAlgorithm(final TableRuleSegment 
each, final AlgorithmSegment databaseShardingAlgorithm) {
+    private static void checkDatabaseShardingAlgorithm(final TableRuleSegment 
tableRuleSegment, final ShardingStrategySegment databaseStrategySegment) {
+        AlgorithmSegment databaseShardingAlgorithm = 
databaseStrategySegment.getShardingAlgorithm();
         if (null != databaseShardingAlgorithm) {
             ShardingAlgorithm shardingAlgorithm = 
TypedSPILoader.getService(ShardingAlgorithm.class, 
databaseShardingAlgorithm.getName(), databaseShardingAlgorithm.getProps());
             ShardingSpherePreconditions.checkState(!(shardingAlgorithm 
instanceof ShardingAutoTableAlgorithm),
-                    () -> new 
AlgorithmInitializationException(shardingAlgorithm, "Auto sharding algorithm 
can not be used to create a table in table '%s'", each.getLogicTable()));
+                    () -> new 
AlgorithmInitializationException(shardingAlgorithm, "Auto sharding algorithm 
can not be used to create a table in table '%s'", 
tableRuleSegment.getLogicTable()));
+            if (shardingAlgorithm instanceof InlineShardingAlgorithm) {
+                DataNodeInfo dataSourceDataNode = 
createDataSourceDataNode(ShardingTableRuleStatementConverter.getActualDataNodes(tableRuleSegment));
+                checkInlineExpression(tableRuleSegment.getLogicTable(), 
databaseStrategySegment.getShardingColumn(), (InlineShardingAlgorithm) 
shardingAlgorithm, dataSourceDataNode);
+            }
         }
-        
ShardingSpherePreconditions.checkState(isValidStrategy(each.getDatabaseStrategySegment()),
+        
ShardingSpherePreconditions.checkState(isValidStrategy(tableRuleSegment.getDatabaseStrategySegment()),
                 () -> new InvalidAlgorithmConfigurationException("sharding", 
null == databaseShardingAlgorithm ? null : 
databaseShardingAlgorithm.getName()));
     }
     
-    private static void checkTableShardingAlgorithm(final TableRuleSegment 
each, final AlgorithmSegment tableShardingAlgorithm) {
+    private static void checkTableShardingAlgorithm(final TableRuleSegment 
tableRuleSegment, final ShardingStrategySegment tableStrategySegment) {
+        AlgorithmSegment tableShardingAlgorithm = 
tableStrategySegment.getShardingAlgorithm();
         if (null != tableShardingAlgorithm) {
             ShardingAlgorithm shardingAlgorithm = 
TypedSPILoader.getService(ShardingAlgorithm.class, 
tableShardingAlgorithm.getName(), tableShardingAlgorithm.getProps());
             ShardingSpherePreconditions.checkState(!(shardingAlgorithm 
instanceof ShardingAutoTableAlgorithm),
-                    () -> new 
AlgorithmInitializationException(shardingAlgorithm, "Auto sharding algorithm 
can not be used to create a table in table '%s'", each.getLogicTable()));
+                    () -> new 
AlgorithmInitializationException(shardingAlgorithm, "Auto sharding algorithm 
can not be used to create a table in table '%s'", 
tableRuleSegment.getLogicTable()));
+            if (shardingAlgorithm instanceof InlineShardingAlgorithm) {
+                DataNodeInfo tableDataNode = 
createTableDataNode(tableRuleSegment.getLogicTable(), 
ShardingTableRuleStatementConverter.getActualDataNodes(tableRuleSegment));
+                checkInlineExpression(tableRuleSegment.getLogicTable(), 
tableStrategySegment.getShardingColumn(), (InlineShardingAlgorithm) 
shardingAlgorithm, tableDataNode);
+            }
         }
-        
ShardingSpherePreconditions.checkState(isValidStrategy(each.getTableStrategySegment()),
+        
ShardingSpherePreconditions.checkState(isValidStrategy(tableRuleSegment.getTableStrategySegment()),
                 () -> new InvalidAlgorithmConfigurationException("sharding", 
null == tableShardingAlgorithm ? null : tableShardingAlgorithm.getName()));
     }
     
@@ -541,4 +554,38 @@ public final class ShardingTableRuleStatementChecker {
             uniqueActualDataNodes.add(sampleActualDataNode);
         });
     }
+    
+    private static DataNodeInfo createDataSourceDataNode(final 
Collection<DataNode> actualDataNodes) {
+        String prefix = 
ShardingTableConstants.DATA_NODE_SUFFIX_PATTERN.matcher(actualDataNodes.iterator().next().getDataSourceName()).replaceAll("");
+        int suffixMinLength = actualDataNodes.stream().map(each -> 
each.getDataSourceName().length() - 
prefix.length()).min(Comparator.comparing(Integer::intValue)).orElse(1);
+        return new DataNodeInfo(prefix, suffixMinLength, 
ShardingTableConstants.DEFAULT_PADDING_CHAR);
+    }
+    
+    private static DataNodeInfo createTableDataNode(final String logicTable, 
final Collection<DataNode> actualDataNodes) {
+        String tableName = actualDataNodes.iterator().next().getTableName();
+        String prefix = tableName.startsWith(logicTable) ? logicTable + 
ShardingTableConstants.DATA_NODE_SUFFIX_PATTERN.matcher(tableName.substring(logicTable.length())).replaceAll("")
+                : 
ShardingTableConstants.DATA_NODE_SUFFIX_PATTERN.matcher(tableName).replaceAll("");
+        int suffixMinLength = actualDataNodes.stream().map(each -> 
each.getTableName().length() - 
prefix.length()).min(Comparator.comparing(Integer::intValue)).orElse(1);
+        return new DataNodeInfo(prefix, suffixMinLength, 
ShardingTableConstants.DEFAULT_PADDING_CHAR);
+    }
+    
+    /**
+     * Check inline sharding algorithms.
+     *
+     * @param logicTable logic table
+     * @param shardingColumn sharding column
+     * @param inlineShardingAlgorithm inline sharding algorithm
+     * @param dataNodeInfo data node info
+     */
+    public static void checkInlineExpression(final String logicTable, final 
String shardingColumn, final InlineShardingAlgorithm inlineShardingAlgorithm, 
final DataNodeInfo dataNodeInfo) {
+        String result = null;
+        try {
+            result = 
inlineShardingAlgorithm.doSharding(Collections.emptySet(), new 
PreciseShardingValue<>(logicTable, shardingColumn, dataNodeInfo, 1));
+            // CHECKSTYLE:OFF
+        } catch (final Exception ignored) {
+            // CHECKSTYLE:ON
+        }
+        ShardingSpherePreconditions.checkState(null == result || 
result.startsWith(dataNodeInfo.getPrefix()),
+                () -> new 
AlgorithmInitializationException(inlineShardingAlgorithm, "inline expression of 
rule `%s` does not match the actual data nodes", logicTable));
+    }
 }
diff --git 
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/converter/ShardingTableRuleStatementConverter.java
 
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/converter/ShardingTableRuleStatementConverter.java
index 3a5502076a3..ef964d61d9c 100644
--- 
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/converter/ShardingTableRuleStatementConverter.java
+++ 
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/converter/ShardingTableRuleStatementConverter.java
@@ -224,7 +224,13 @@ public final class ShardingTableRuleStatementConverter {
         return result;
     }
     
-    private static Collection<DataNode> getActualDataNodes(final 
TableRuleSegment ruleSegment) {
+    /**
+     * Get actual data nodes for sharding table rule segment.
+     *
+     * @param ruleSegment sharding table rule segment
+     * @return data nodes
+     */
+    public static Collection<DataNode> getActualDataNodes(final 
TableRuleSegment ruleSegment) {
         Collection<DataNode> result = new LinkedList<>();
         for (String each : ruleSegment.getDataSourceNodes()) {
             List<String> dataNodes = 
InlineExpressionParserFactory.newInstance(each).splitAndEvaluate();
@@ -233,7 +239,13 @@ public final class ShardingTableRuleStatementConverter {
         return result;
     }
     
-    private static Collection<DataNode> getActualDataNodes(final 
AutoTableRuleSegment ruleSegment) {
+    /**
+     * Get actual data nodes for auto sharding table rule segment.
+     *
+     * @param ruleSegment auto sharding table rule segment
+     * @return data nodes
+     */
+    public static Collection<DataNode> getActualDataNodes(final 
AutoTableRuleSegment ruleSegment) {
         ShardingAlgorithm shardingAlgorithm =
                 TypedSPILoader.getService(ShardingAlgorithm.class, 
ruleSegment.getShardingAlgorithmSegment().getName(), 
ruleSegment.getShardingAlgorithmSegment().getProps());
         ShardingSpherePreconditions.checkState(shardingAlgorithm instanceof 
ShardingAutoTableAlgorithm,
diff --git 
a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementCheckerTest.java
 
b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementCheckerTest.java
index f41d1e0a6d9..bca62d47e3e 100644
--- 
a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementCheckerTest.java
+++ 
b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementCheckerTest.java
@@ -22,9 +22,10 @@ import 
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfigurat
 import 
org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
 import 
org.apache.shardingsphere.infra.algorithm.core.exception.InvalidAlgorithmConfigurationException;
 import 
org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
+import 
org.apache.shardingsphere.infra.exception.kernel.metadata.datanode.InvalidDataNodeFormatException;
+import 
org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.MissingRequiredStorageUnitsException;
 import 
org.apache.shardingsphere.infra.exception.kernel.metadata.rule.DuplicateRuleException;
 import 
org.apache.shardingsphere.infra.exception.kernel.metadata.rule.MissingRequiredRuleException;
-import 
org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.MissingRequiredStorageUnitsException;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import 
org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
 import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
@@ -234,13 +235,13 @@ class ShardingTableRuleStatementCheckerTest {
     }
     
     @Test
-    void assertCheckNullAlgorithmNameAndAlgorithmSegment() {
+    void assertCheckTableWithInvalidDataNodeFormat() {
         AlgorithmSegment databaseAlgorithmSegment = new 
AlgorithmSegment("inline", PropertiesBuilder.build(new 
Property("algorithm-expression", "ds_${product_id % 2}")));
         KeyGenerateStrategySegment keyGenerateStrategy = new 
KeyGenerateStrategySegment("product_id", new 
AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
         TableRuleSegment tableRuleSegment = new TableRuleSegment("t_product", 
Arrays.asList("ds_0", "ds_1"), keyGenerateStrategy, null);
         tableRuleSegment.setTableStrategySegment(new 
ShardingStrategySegment("standard", "product_id", databaseAlgorithmSegment));
         Collection<AbstractTableRuleSegment> rules = 
Collections.singleton(tableRuleSegment);
-        ShardingTableRuleStatementChecker.checkCreation(database, rules, 
false, shardingRuleConfig);
+        assertThrows(InvalidDataNodeFormatException.class, () -> 
ShardingTableRuleStatementChecker.checkCreation(database, rules, false, null));
     }
     
     @Test
diff --git 
a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateShardingTableRuleExecutorTest.java
 
b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateShardingTableRuleExecutorTest.java
index 8ba24b907c9..fc89b18bf0a 100644
--- 
a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateShardingTableRuleExecutorTest.java
+++ 
b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateShardingTableRuleExecutorTest.java
@@ -201,6 +201,28 @@ class CreateShardingTableRuleExecutorTest {
         assertThrows(DuplicateShardingActualDataNodeException.class, () -> 
executor.checkBeforeUpdate(sqlStatement));
     }
     
+    @Test
+    void assertCheckWithInvalidDataSourceInlineExpression() {
+        String sql = "CREATE SHARDING TABLE RULE t_order("
+                + "DATANODES('ds_${0..1}.t_order_${0..1}'),"
+                + 
"DATABASE_STRATEGY(TYPE='standard',SHARDING_COLUMN=user_id,SHARDING_ALGORITHM(TYPE(NAME='inline',PROPERTIES('algorithm-expression'='foo_${user_id
 % 2}')))),"
+                + 
"TABLE_STRATEGY(TYPE='standard',SHARDING_COLUMN=order_id,SHARDING_ALGORITHM(TYPE(NAME='inline',PROPERTIES('algorithm-expression'='t_order_${order_id
 % 2}'))))"
+                + ");";
+        CreateShardingTableRuleStatement sqlStatement = 
(CreateShardingTableRuleStatement) getDistSQLStatement(sql);
+        assertThrows(AlgorithmInitializationException.class, () -> 
executor.checkBeforeUpdate(sqlStatement));
+    }
+    
+    @Test
+    void assertCheckWithInvalidTableInlineExpression() {
+        String sql = "CREATE SHARDING TABLE RULE t_order("
+                + "DATANODES('ds_${0..1}.t_order_${0..1}'),"
+                + 
"DATABASE_STRATEGY(TYPE='standard',SHARDING_COLUMN=user_id,SHARDING_ALGORITHM(TYPE(NAME='inline',PROPERTIES('algorithm-expression'='ds_${user_id
 % 2}')))),"
+                + 
"TABLE_STRATEGY(TYPE='standard',SHARDING_COLUMN=order_id,SHARDING_ALGORITHM(TYPE(NAME='inline',PROPERTIES('algorithm-expression'='bar_${order_id
 % 2}'))))"
+                + ");";
+        CreateShardingTableRuleStatement sqlStatement = 
(CreateShardingTableRuleStatement) getDistSQLStatement(sql);
+        assertThrows(AlgorithmInitializationException.class, () -> 
executor.checkBeforeUpdate(sqlStatement));
+    }
+    
     @Test
     void assertUpdateWithIfNotExistsStatement() {
         ShardingRule rule = mock(ShardingRule.class);

Reply via email to