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 bca62d47e3e2c..a52fd04a16a92 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 @@ -95,7 +95,7 @@ void assertCheckCreateSuccess() { @Test void assertCheckAlterSuccess() { Collection rules = new LinkedList<>(); - AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_order", Arrays.asList("ds_0", "ds_1")); + AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_order", Arrays.asList("ds_0", "ds_1"), null, null); autoTableRuleSegment.setShardingColumn("order_id"); autoTableRuleSegment.setShardingAlgorithmSegment(new AlgorithmSegment("CORE.AUTO.FIXTURE", PropertiesBuilder.build(new Property("sharding-count", "2")))); rules.add(autoTableRuleSegment); @@ -107,11 +107,11 @@ void assertCheckerBindingTableSuccess() { ShardingRuleConfiguration shardingRuleConfig = createShardingRuleConfiguration(); shardingRuleConfig.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("reference_0", "t_order,t_order_item")); Collection rules = new LinkedList<>(); - AutoTableRuleSegment autoTable1 = new AutoTableRuleSegment("t_order", Arrays.asList("ds_0", "ds_1")); + AutoTableRuleSegment autoTable1 = new AutoTableRuleSegment("t_order", Arrays.asList("ds_0", "ds_1"), null, null); autoTable1.setShardingColumn("order_id"); autoTable1.setShardingAlgorithmSegment(new AlgorithmSegment("CORE.AUTO.FIXTURE", PropertiesBuilder.build(new Property("sharding-count", "2")))); rules.add(autoTable1); - AutoTableRuleSegment autoTable2 = new AutoTableRuleSegment("t_order_item", Arrays.asList("ds_0", "ds_1")); + AutoTableRuleSegment autoTable2 = new AutoTableRuleSegment("t_order_item", Arrays.asList("ds_0", "ds_1"), null, null); autoTable2.setShardingColumn("order_id"); autoTable2.setShardingAlgorithmSegment(new AlgorithmSegment("CORE.AUTO.FIXTURE", PropertiesBuilder.build(new Property("sharding-count", "2")))); rules.add(autoTable2); @@ -121,49 +121,49 @@ void assertCheckerBindingTableSuccess() { @Test void assertCheckCreationWithDuplicated() { List rules = Arrays.asList( - new AutoTableRuleSegment("t_order_duplicated", Arrays.asList("ds_0", "ds_1")), - new AutoTableRuleSegment("t_order_duplicated", Arrays.asList("ds_0", "ds_1"))); + new AutoTableRuleSegment("t_order_duplicated", Arrays.asList("ds_0", "ds_1"), null, null), + new AutoTableRuleSegment("t_order_duplicated", Arrays.asList("ds_0", "ds_1"), null, null)); assertThrows(DuplicateRuleException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig)); } @Test void assertCheckCreationWithIdentical() { - Collection rules = Collections.singleton(new AutoTableRuleSegment("t_order", Arrays.asList("ds_0", "ds_1"))); + Collection rules = Collections.singleton(new AutoTableRuleSegment("t_order", Arrays.asList("ds_0", "ds_1"), null, null)); assertThrows(DuplicateRuleException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig)); } @Test void assertCheckAlterationWithRuleRequiredMissed() { - Collection rules = Collections.singleton(new AutoTableRuleSegment("t_order_required_missed", Arrays.asList("ds_0", "ds_1"))); + Collection rules = Collections.singleton(new AutoTableRuleSegment("t_order_required_missed", Arrays.asList("ds_0", "ds_1"), null, null)); assertThrows(MissingRequiredRuleException.class, () -> ShardingTableRuleStatementChecker.checkAlteration(database, rules, shardingRuleConfig)); } @Test void assertCheckCreationWithResourceRequiredMissed() { - Collection rules = Collections.singleton(new AutoTableRuleSegment("t_product", Arrays.asList("ds_required_missed", "ds_1"))); + Collection rules = Collections.singleton(new AutoTableRuleSegment("t_product", Arrays.asList("ds_required_missed", "ds_1"), null, null)); assertThrows(MissingRequiredStorageUnitsException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig)); } @Test void assertCheckCreationWithInvalidKeyGenerateAlgorithm() { - AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1")); - autoTableRuleSegment.setKeyGenerateStrategySegment(new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("invalid", PropertiesBuilder.build(new Property("invalid", "invalid"))))); + KeyGenerateStrategySegment keyGenerator = new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("invalid", PropertiesBuilder.build(new Property("invalid", "invalid")))); + AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"), keyGenerator, null); assertThrows(ServiceProviderNotFoundException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, Collections.singleton(autoTableRuleSegment), false, shardingRuleConfig)); } @Test void assertCheckCreationWithInvalidAuditAlgorithm() { - AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1")); - autoTableRuleSegment.setAuditStrategySegment(new AuditStrategySegment(Collections.singleton(new ShardingAuditorSegment("sharding_key_required_auditor", - new AlgorithmSegment("invalid", new Properties()))), true)); + AuditStrategySegment auditStrategySegment = new AuditStrategySegment(Collections.singleton(new ShardingAuditorSegment("sharding_key_required_auditor", + new AlgorithmSegment("invalid", new Properties()))), true); + AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"), null, auditStrategySegment); assertThrows(ServiceProviderNotFoundException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, Collections.singleton(autoTableRuleSegment), false, shardingRuleConfig)); } @Test void assertCheckAutoTableWithNotExistShardingAlgorithms() { - AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1")); + AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"), null, null); autoTableRuleSegment.setShardingColumn("product_id"); autoTableRuleSegment.setShardingAlgorithmSegment(new AlgorithmSegment("not_exist", PropertiesBuilder.build(new Property("", "")))); Collection rules = Collections.singleton(autoTableRuleSegment); @@ -172,7 +172,7 @@ void assertCheckAutoTableWithNotExistShardingAlgorithms() { @Test void assertCheckAutoTableWithComplexShardingAlgorithms() { - AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1")); + AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"), null, null); autoTableRuleSegment.setShardingColumn("product_id"); autoTableRuleSegment.setShardingAlgorithmSegment(new AlgorithmSegment("complex", PropertiesBuilder.build(new Property("", "")))); Collection rules = Collections.singleton(autoTableRuleSegment); @@ -255,7 +255,7 @@ void assertCheckNullAlgorithmNameAndNullAlgorithmSegment() { @Test void assertCheckAutoTableRuleWithStandardShardingAlgorithm() { - AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1")); + AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"), null, null); autoTableRuleSegment.setShardingAlgorithmSegment(new AlgorithmSegment("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${product_id % 2}")))); Collection rules = Collections.singleton(autoTableRuleSegment); assertThrows(AlgorithmInitializationException.class, () -> ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig)); @@ -263,7 +263,7 @@ void assertCheckAutoTableRuleWithStandardShardingAlgorithm() { @Test void assertCheckAutoTableRuleWithAutoShardingAlgorithm() { - AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1")); + AutoTableRuleSegment autoTableRuleSegment = new AutoTableRuleSegment("t_product", Arrays.asList("ds_0", "ds_1"), null, null); autoTableRuleSegment.setShardingAlgorithmSegment(new AlgorithmSegment("CORE.AUTO.FIXTURE", PropertiesBuilder.build(new Property("sharding-count", "4")))); Collection rules = Collections.singleton(autoTableRuleSegment); ShardingTableRuleStatementChecker.checkCreation(database, rules, false, shardingRuleConfig); @@ -314,8 +314,8 @@ private Map createDataSource() { } private AutoTableRuleSegment createCompleteAutoTableRule() { - AutoTableRuleSegment result = new AutoTableRuleSegment("t_product_0", Arrays.asList("ds_0", "ds_1")); - result.setKeyGenerateStrategySegment(new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()))); + KeyGenerateStrategySegment keyGenerator = new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())); + AutoTableRuleSegment result = new AutoTableRuleSegment("t_product_0", Arrays.asList("ds_0", "ds_1"), keyGenerator, null); result.setShardingColumn("product_id"); result.setShardingAlgorithmSegment(new AlgorithmSegment("FOO.DISTSQL.FIXTURE", PropertiesBuilder.build(new Property("", "")))); return result; diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/converter/ShardingTableRuleStatementConverterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/converter/ShardingTableRuleStatementConverterTest.java index 07984d6022d64..a7075bd2a9c0e 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/converter/ShardingTableRuleStatementConverterTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/converter/ShardingTableRuleStatementConverterTest.java @@ -96,19 +96,17 @@ private Collection createNoneStrategyTypeTableRuleSegm } private Collection createTableRuleSegment1() { - AutoTableRuleSegment autoTableRuleSegment1 = new AutoTableRuleSegment("t_order", Arrays.asList("ds0", "ds1")); + AutoTableRuleSegment autoTableRuleSegment1 = new AutoTableRuleSegment("t_order", Arrays.asList("ds0", "ds1"), null, null); autoTableRuleSegment1.setShardingColumn("order_id"); autoTableRuleSegment1.setShardingAlgorithmSegment(new AlgorithmSegment("MOD", PropertiesBuilder.build(new Property("sharding_count", "2")))); - AutoTableRuleSegment autoTableRuleSegment2 = new AutoTableRuleSegment("t_order_2", Arrays.asList("ds0", "ds1")); + KeyGenerateStrategySegment keyGenerator = new KeyGenerateStrategySegment("order_id", new AlgorithmSegment("snowflake", new Properties())); + AuditStrategySegment auditStrategySegment = new AuditStrategySegment(Collections.singleton(new ShardingAuditorSegment("sharding_key_required_auditor", + new AlgorithmSegment("DML_SHARDING_CONDITIONS", new Properties()))), true); + AutoTableRuleSegment autoTableRuleSegment2 = new AutoTableRuleSegment("t_order_2", Arrays.asList("ds0", "ds1"), keyGenerator, auditStrategySegment); autoTableRuleSegment2.setShardingColumn("order_id"); autoTableRuleSegment2.setShardingAlgorithmSegment(new AlgorithmSegment("MOD", PropertiesBuilder.build(new Property("sharding_count", "2")))); - autoTableRuleSegment2.setKeyGenerateStrategySegment(new KeyGenerateStrategySegment("order_id", new AlgorithmSegment("snowflake", new Properties()))); - autoTableRuleSegment2.setAuditStrategySegment(new AuditStrategySegment(Collections.singleton(new ShardingAuditorSegment("sharding_key_required_auditor", - new AlgorithmSegment("DML_SHARDING_CONDITIONS", new Properties()))), true)); - TableRuleSegment tableRuleSegment = new TableRuleSegment("t_order", Arrays.asList("ds0", "ds1"), - new KeyGenerateStrategySegment("order_id", new AlgorithmSegment("snowflake", PropertiesBuilder.build(new Property("", "")))), - new AuditStrategySegment(Collections.singleton(new ShardingAuditorSegment("sharding_key_required_auditor", - new AlgorithmSegment("DML_SHARDING_CONDITIONS", new Properties()))), true)); + KeyGenerateStrategySegment keyGenerator1 = new KeyGenerateStrategySegment("order_id", new AlgorithmSegment("snowflake", PropertiesBuilder.build(new Property("", "")))); + TableRuleSegment tableRuleSegment = new TableRuleSegment("t_order", Arrays.asList("ds0", "ds1"), keyGenerator1, auditStrategySegment); AlgorithmSegment databaseAlgorithmSegment = new AlgorithmSegment("inline", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${product_id % 2}"))); tableRuleSegment.setDatabaseStrategySegment(new ShardingStrategySegment("standard", "order_id", databaseAlgorithmSegment)); tableRuleSegment.setTableStrategySegment(new ShardingStrategySegment("standard", "order_id", new AlgorithmSegment("order_id_algorithm", new Properties()))); diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableRuleExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableRuleExecutorTest.java index f30be35602d2f..a00729d38fea0 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableRuleExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableRuleExecutorTest.java @@ -170,8 +170,8 @@ void assertUpdateTableType() { } private AutoTableRuleSegment createCompleteAutoTableRule(final String logicTableName) { - AutoTableRuleSegment result = new AutoTableRuleSegment(logicTableName, Arrays.asList("ds_0", "ds_1")); - result.setKeyGenerateStrategySegment(new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()))); + AutoTableRuleSegment result = + new AutoTableRuleSegment(logicTableName, Arrays.asList("ds_0", "ds_1"), new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())), null); result.setShardingColumn("order_id"); result.setShardingAlgorithmSegment(new AlgorithmSegment("FOO.DISTSQL.FIXTURE", PropertiesBuilder.build(new Property("", "")))); return result; 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 fc89b18bf0a96..8be31c10bdfd1 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 @@ -258,8 +258,8 @@ void assertUpdateWithIfNotExistsStatement() { } private AutoTableRuleSegment createCompleteAutoTableRule() { - AutoTableRuleSegment result = new AutoTableRuleSegment("t_order_item_input", Collections.singleton("logic_ds")); - result.setKeyGenerateStrategySegment(new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()))); + AutoTableRuleSegment result = new AutoTableRuleSegment("t_order_item_input", Collections.singleton("logic_ds"), + new KeyGenerateStrategySegment("product_id", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())), null); result.setShardingColumn("order_id"); result.setShardingAlgorithmSegment(new AlgorithmSegment("FOO.DISTSQL.FIXTURE", PropertiesBuilder.build(new Property("", "")))); return result; 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 ac4f808d99400..4d6d63a40d7e8 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 @@ -256,10 +256,9 @@ public ASTNode visitShowShardingTableNodes(final ShowShardingTableNodesContext c @Override public ASTNode visitShardingAutoTableRule(final ShardingAutoTableRuleContext ctx) { - AutoTableRuleSegment result = new AutoTableRuleSegment(getIdentifierValue(ctx.tableName()), getResources(ctx.storageUnits())); - Optional.ofNullable(ctx.keyGenerateDefinition()).ifPresent(optional -> result.setKeyGenerateStrategySegment((KeyGenerateStrategySegment) visit(ctx.keyGenerateDefinition()))); - Optional.ofNullable(ctx.auditDefinition()).ifPresent(optional -> result.setAuditStrategySegment((AuditStrategySegment) visitAuditDefinition(getIdentifierValue(ctx.tableName()), - ctx.auditDefinition()))); + KeyGenerateStrategySegment keyGenerateSegment = null == ctx.keyGenerateDefinition() ? null : (KeyGenerateStrategySegment) visit(ctx.keyGenerateDefinition()); + AuditStrategySegment auditStrategySegment = null == ctx.auditDefinition() ? null : (AuditStrategySegment) visitAuditDefinition(getIdentifierValue(ctx.tableName()), ctx.auditDefinition()); + AutoTableRuleSegment result = new AutoTableRuleSegment(getIdentifierValue(ctx.tableName()), getResources(ctx.storageUnits()), keyGenerateSegment, auditStrategySegment); 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; diff --git a/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/segment/table/AutoTableRuleSegment.java b/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/segment/table/AutoTableRuleSegment.java index 9f3835d9251fc..1d88c1f922884 100644 --- a/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/segment/table/AutoTableRuleSegment.java +++ b/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/segment/table/AutoTableRuleSegment.java @@ -22,6 +22,8 @@ import org.apache.shardingsphere.distsql.segment.AlgorithmSegment; import java.util.Collection; +import org.apache.shardingsphere.sharding.distsql.segment.strategy.AuditStrategySegment; +import org.apache.shardingsphere.sharding.distsql.segment.strategy.KeyGenerateStrategySegment; /** * Auto table rule segment. @@ -34,7 +36,8 @@ public final class AutoTableRuleSegment extends AbstractTableRuleSegment { private AlgorithmSegment shardingAlgorithmSegment; - public AutoTableRuleSegment(final String logicTable, final Collection dataSources) { - super(logicTable, dataSources); + public AutoTableRuleSegment(final String logicTable, final Collection dataSources, final KeyGenerateStrategySegment keyGenerateStrategySegment, + final AuditStrategySegment auditStrategySegment) { + super(logicTable, dataSources, keyGenerateStrategySegment, auditStrategySegment); } } diff --git a/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/segment/table/TableRuleSegment.java b/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/segment/table/TableRuleSegment.java index 5d670fe0dcee5..54080e94671ee 100644 --- a/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/segment/table/TableRuleSegment.java +++ b/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/segment/table/TableRuleSegment.java @@ -36,8 +36,8 @@ public final class TableRuleSegment extends AbstractTableRuleSegment { private ShardingStrategySegment databaseStrategySegment; - public TableRuleSegment(final String logicTable, final Collection dataSourcesNodes, + public TableRuleSegment(final String logicTable, final Collection dataSourceNodes, final KeyGenerateStrategySegment keyGenerateStrategySegment, final AuditStrategySegment auditStrategySegment) { - super(logicTable, dataSourcesNodes, keyGenerateStrategySegment, auditStrategySegment); + super(logicTable, dataSourceNodes, keyGenerateStrategySegment, auditStrategySegment); } }