You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by yx...@apache.org on 2022/04/21 02:49:20 UTC
[shardingsphere] branch master updated: Support `DROP SHARDING TABLE RULE WITH UNUSED ALGORITHMS` syntax. (#16948)
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 706b8142553 Support `DROP SHARDING TABLE RULE WITH UNUSED ALGORITHMS` syntax. (#16948)
706b8142553 is described below
commit 706b81425533435eac831147b2f3d8176906baac
Author: lanchengx <52...@users.noreply.github.com>
AuthorDate: Thu Apr 21 10:49:04 2022 +0800
Support `DROP SHARDING TABLE RULE WITH UNUSED ALGORITHMS` syntax. (#16948)
* Support `DROP SHARDING TABLE RULE WITH UNUSED ALGORITHMS` syntax.
* Reformat.
---
.../DropShardingTableRuleStatementUpdater.java | 25 ++++++++++++++++++++--
.../DropShardingTableRuleStatementUpdaterTest.java | 23 ++++++++++++++++++--
.../src/main/antlr4/imports/sharding/Keyword.g4 | 4 ++++
.../main/antlr4/imports/sharding/RDLStatement.g4 | 6 +++++-
.../core/ShardingDistSQLStatementVisitor.java | 5 ++++-
.../statement/DropShardingTableRuleStatement.java | 4 ++++
6 files changed, 61 insertions(+), 6 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableRuleStatementUpdater.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableRuleStatementUpdater.java
index 9b2a7337813..dae6751980e 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableRuleStatementUpdater.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableRuleStatementUpdater.java
@@ -26,12 +26,15 @@ import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
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.ShardingTableRuleConfiguration;
+import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.distsql.parser.statement.DropShardingTableRuleStatement;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.LinkedList;
+import java.util.Objects;
import java.util.stream.Collectors;
/**
@@ -108,12 +111,30 @@ public final class DropShardingTableRuleStatementUpdater implements RuleDefiniti
@Override
public boolean updateCurrentRuleConfiguration(final DropShardingTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) {
- for (String each : getToBeDroppedShardingTableNames(sqlStatement)) {
- dropShardingTable(currentRuleConfig, each);
+ boolean dropUnusedAlgorithms = sqlStatement.isDropUnusedAlgorithms();
+ Collection<String> toBeDroppedShardingTableNames = getToBeDroppedShardingTableNames(sqlStatement);
+ toBeDroppedShardingTableNames.forEach(each -> dropShardingTable(currentRuleConfig, each));
+ if (dropUnusedAlgorithms) {
+ toBeDroppedShardingTableNames.forEach(each -> dropUnusedAlgorithms(currentRuleConfig));
}
return false;
}
+ private void dropUnusedAlgorithms(final ShardingRuleConfiguration currentRuleConfig) {
+ Collection<String> inUsedAlgorithms = currentRuleConfig.getTables().stream()
+ .map(each -> Arrays.asList(each.getTableShardingStrategy(), each.getDatabaseShardingStrategy()))
+ .flatMap(Collection::stream).filter(Objects::nonNull).map(ShardingStrategyConfiguration::getShardingAlgorithmName).collect(Collectors.toSet());
+ inUsedAlgorithms.addAll(currentRuleConfig.getAutoTables().stream().map(each -> each.getShardingStrategy().getShardingAlgorithmName()).collect(Collectors.toSet()));
+ if (null != currentRuleConfig.getDefaultTableShardingStrategy()) {
+ inUsedAlgorithms.add(currentRuleConfig.getDefaultTableShardingStrategy().getShardingAlgorithmName());
+ }
+ if (null != currentRuleConfig.getDefaultDatabaseShardingStrategy()) {
+ inUsedAlgorithms.add(currentRuleConfig.getDefaultDatabaseShardingStrategy().getShardingAlgorithmName());
+ }
+ Collection<String> unusedAlgorithms = currentRuleConfig.getShardingAlgorithms().keySet().stream().filter(each -> !inUsedAlgorithms.contains(each)).collect(Collectors.toSet());
+ unusedAlgorithms.forEach(each -> currentRuleConfig.getShardingAlgorithms().remove(each));
+ }
+
private void dropShardingTable(final ShardingRuleConfiguration currentRuleConfig, final String tableName) {
currentRuleConfig.getTables().removeAll(currentRuleConfig.getTables().stream().filter(each -> tableName.equals(each.getLogicTable())).collect(Collectors.toList()));
currentRuleConfig.getAutoTables().removeAll(currentRuleConfig.getAutoTables().stream().filter(each -> tableName.equals(each.getLogicTable())).collect(Collectors.toList()));
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableRuleStatementUpdaterTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableRuleStatementUpdaterTest.java
index 5ef0bf7bf80..6df785fb606 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableRuleStatementUpdaterTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableRuleStatementUpdaterTest.java
@@ -25,6 +25,7 @@ import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
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.ShardingTableRuleConfiguration;
+import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.distsql.handler.update.DropShardingTableRuleStatementUpdater;
import org.apache.shardingsphere.sharding.distsql.parser.statement.DropShardingTableRuleStatement;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
@@ -40,6 +41,7 @@ import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.stream.Collectors;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -74,22 +76,39 @@ public final class DropShardingTableRuleStatementUpdaterTest {
}
@Test
- public void assertUpdateCurrentRuleConfiguration() {
+ public void assertDropRule() {
ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
updater.updateCurrentRuleConfiguration(createSQLStatement("t_order"), currentRuleConfig);
assertFalse(getShardingTables(currentRuleConfig).contains("t_order"));
assertTrue(getBindingTables(currentRuleConfig).contains("t_order_item"));
}
+ @Test
+ public void assertDropRuleAndUnusedAlgorithm() {
+ ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
+ DropShardingTableRuleStatement sqlStatement = createSQLStatement("t_order");
+ sqlStatement.setDropUnusedAlgorithms(true);
+ updater.updateCurrentRuleConfiguration(sqlStatement, currentRuleConfig);
+ assertFalse(getShardingTables(currentRuleConfig).contains("t_order"));
+ assertTrue(getBindingTables(currentRuleConfig).contains("t_order_item"));
+ assertEquals(2, currentRuleConfig.getShardingAlgorithms().size());
+ }
+
private DropShardingTableRuleStatement createSQLStatement(final String tableName) {
return new DropShardingTableRuleStatement(Collections.singleton(new TableNameSegment(0, 3, new IdentifierValue(tableName))));
}
private ShardingRuleConfiguration createCurrentRuleConfiguration() {
ShardingRuleConfiguration result = new ShardingRuleConfiguration();
- result.getTables().add(new ShardingTableRuleConfiguration("t_order_item"));
+ ShardingTableRuleConfiguration tableRuleConfiguration = new ShardingTableRuleConfiguration("t_order_item");
+ tableRuleConfiguration.setDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "t_order_item_algorithm"));
+ result.getTables().add(tableRuleConfiguration);
result.getAutoTables().add(new ShardingAutoTableRuleConfiguration("t_order"));
result.setBindingTableGroups(Collections.singleton("t_order_item"));
+ result.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "default_table_strategy"));
+ result.getShardingAlgorithms().put("unused_algorithm", null);
+ result.getShardingAlgorithms().put("t_order_item_algorithm", null);
+ result.getShardingAlgorithms().put("default_table_strategy", null);
return result;
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/imports/sharding/Keyword.g4 b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/imports/sharding/Keyword.g4
index daffc1cdd03..d672af7648c 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/imports/sharding/Keyword.g4
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/imports/sharding/Keyword.g4
@@ -206,3 +206,7 @@ IF
EXISTS
: E X I S T S
;
+
+WITH
+ : W I T H
+ ;
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 759e526d9e3..f3697aeeee8 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
@@ -72,7 +72,7 @@ alterShardingKeyGenerator
;
dropShardingTableRule
- : DROP SHARDING TABLE RULE existsClause? tableName (COMMA tableName)*
+ : DROP SHARDING TABLE RULE existsClause? tableName (COMMA tableName)* withUnusedAlgorithmsClause?
;
dropShardingBindingTableRules
@@ -218,3 +218,7 @@ algorithmProperty
existClause
: IF EXISTS
;
+
+withUnusedAlgorithmsClause
+ : WITH UNUSED ALGORITHMS
+ ;
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 f4edc9381ab..bdc287b8fd6 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
@@ -192,7 +192,10 @@ public final class ShardingDistSQLStatementVisitor extends ShardingDistSQLStatem
@Override
public ASTNode visitDropShardingTableRule(final DropShardingTableRuleContext ctx) {
- return new DropShardingTableRuleStatement(null != ctx.existsClause(), ctx.tableName().stream().map(each -> (TableNameSegment) visit(each)).collect(Collectors.toList()));
+ DropShardingTableRuleStatement result = new DropShardingTableRuleStatement(null != ctx.existsClause(),
+ ctx.tableName().stream().map(each -> (TableNameSegment) visit(each)).collect(Collectors.toList()));
+ result.setDropUnusedAlgorithms(null != ctx.withUnusedAlgorithmsClause());
+ return result;
}
@Override
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/statement/DropShardingTableRuleStatement.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/statement/DropShardingTableRuleStatement.java
index 29926ab979f..47aaefbbd15 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/statement/DropShardingTableRuleStatement.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/statement/DropShardingTableRuleStatement.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.sharding.distsql.parser.statement;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
+import lombok.Setter;
import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.DropRuleStatement;
import org.apache.shardingsphere.distsql.parser.subject.impl.ShardingSubjectSupplier;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
@@ -34,6 +35,9 @@ public final class DropShardingTableRuleStatement extends DropRuleStatement impl
private final Collection<TableNameSegment> tableNames;
+ @Setter
+ private boolean dropUnusedAlgorithms;
+
public DropShardingTableRuleStatement(final boolean containsExistsClause, final Collection<TableNameSegment> tableNames) {
setContainsExistClause(containsExistsClause);
this.tableNames = tableNames;