You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2020/12/14 12:01:57 UTC
[shardingsphere] branch master updated: Standardize RDL of create
sharding rule (#8614)
This is an automated email from the ASF dual-hosted git repository.
panjuan 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 d5cbee1 Standardize RDL of create sharding rule (#8614)
d5cbee1 is described below
commit d5cbee1e34ebb1c0b86ff0fe13aba98f6a730286
Author: Liang Zhang <te...@163.com>
AuthorDate: Mon Dec 14 20:01:35 2020 +0800
Standardize RDL of create sharding rule (#8614)
---
.../src/main/antlr4/imports/RDLStatement.g4 | 28 +++++++++--------
.../distsql/parser/core/DistSQLVisitor.java | 28 ++++++++---------
.../parser/segment/rdl/TableRuleSegment.java | 3 +-
...reateShardingRuleStatementContextConverter.java | 35 ++--------------------
...eShardingRuleStatementContextConverterTest.java | 5 +++-
.../sql/common/value/props/PropertiesValue.java | 22 ++++----------
6 files changed, 45 insertions(+), 76 deletions(-)
diff --git a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RDLStatement.g4 b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RDLStatement.g4
index 4c352b6..cf6b43a 100644
--- a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RDLStatement.g4
+++ b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RDLStatement.g4
@@ -60,31 +60,35 @@ password
;
createShardingRule
- : CREATE SHARDING RULE LP tableRule (COMMA tableRule)* RP
+ : CREATE SHARDING RULE LP shardingTableRuleDefinition (COMMA shardingTableRuleDefinition)* RP
;
-tableRule
- : tableName EQ tableRuleDefinition
+shardingTableRuleDefinition
+ : tableName columName shardingAlgorithmDefinition
;
-tableRuleDefinition
- : strategyType LP strategyDefinition RP
+shardingAlgorithmDefinition
+ : shardingAlgorithmType LP shardingAlgorithmProperties RP
;
-strategyType
+shardingAlgorithmType
: IDENTIFIER
;
-strategyDefinition
- : columName COMMA strategyProps
+shardingAlgorithmProperties
+ : shardingAlgorithmProperty (COMMA shardingAlgorithmProperty)*
;
-strategyProps
- : strategyProp (COMMA strategyProp)*
+shardingAlgorithmProperty
+ : shardingAlgorithmPropertyKey EQ shardingAlgorithmPropertyValue
;
-strategyProp
- : IDENTIFIER | NUMBER | INT
+shardingAlgorithmPropertyKey
+ : IDENTIFIER
+ ;
+
+shardingAlgorithmPropertyValue
+ : NUMBER | INT | STRING
;
tableName
diff --git a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
index 261f8d1..c5bfa15 100644
--- a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
+++ b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
@@ -23,11 +23,11 @@ import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.C
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DataSourceContext;
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DataSourceDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.SchemaNameContext;
+import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShardingAlgorithmPropertiesContext;
+import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShardingAlgorithmPropertyContext;
+import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShardingTableRuleDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowDataSourcesContext;
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowRuleContext;
-import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.StrategyPropContext;
-import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.StrategyPropsContext;
-import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.TableRuleContext;
import org.apache.shardingsphere.distsql.parser.segment.rdl.DataSourceConnectionSegment;
import org.apache.shardingsphere.distsql.parser.segment.rdl.TableRuleSegment;
import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateDataSourcesStatement;
@@ -36,8 +36,8 @@ import org.apache.shardingsphere.distsql.parser.statement.rdl.show.impl.ShowData
import org.apache.shardingsphere.distsql.parser.statement.rdl.show.impl.ShowRuleStatement;
import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.SchemaSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.value.collection.CollectionValue;
import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
+import org.apache.shardingsphere.sql.parser.sql.common.value.props.PropertiesValue;
import java.util.Collection;
import java.util.LinkedList;
@@ -77,30 +77,30 @@ public final class DistSQLVisitor extends DistSQLStatementBaseVisitor<ASTNode> {
@Override
public ASTNode visitCreateShardingRule(final CreateShardingRuleContext ctx) {
Collection<TableRuleSegment> tables = new LinkedList<>();
- for (TableRuleContext each : ctx.tableRule()) {
+ for (ShardingTableRuleDefinitionContext each : ctx.shardingTableRuleDefinition()) {
tables.add((TableRuleSegment) visit(each));
}
return new CreateShardingRuleStatement(tables);
}
@Override
- public ASTNode visitTableRule(final TableRuleContext ctx) {
+ public ASTNode visitShardingTableRuleDefinition(final ShardingTableRuleDefinitionContext ctx) {
TableRuleSegment result = new TableRuleSegment();
result.setLogicTable(ctx.tableName().getText());
- result.setAlgorithmType(ctx.tableRuleDefinition().strategyType().getText());
- result.setShardingColumn(ctx.tableRuleDefinition().strategyDefinition().columName().getText());
+ result.setShardingColumn(ctx.columName().getText());
+ result.setAlgorithmType(ctx.shardingAlgorithmDefinition().shardingAlgorithmType().getText());
// TODO Future feature.
result.setDataSources(new LinkedList<>());
- CollectionValue<String> props = (CollectionValue) visit(ctx.tableRuleDefinition().strategyDefinition().strategyProps());
- result.setProperties(props.getValue());
+ PropertiesValue propertiesValue = (PropertiesValue) visit(ctx.shardingAlgorithmDefinition().shardingAlgorithmProperties());
+ result.setAlgorithmProps(propertiesValue.getValue());
return result;
}
@Override
- public ASTNode visitStrategyProps(final StrategyPropsContext ctx) {
- CollectionValue<String> result = new CollectionValue<>();
- for (StrategyPropContext each : ctx.strategyProp()) {
- result.getValue().add(each.getText());
+ public ASTNode visitShardingAlgorithmProperties(final ShardingAlgorithmPropertiesContext ctx) {
+ PropertiesValue result = new PropertiesValue();
+ for (ShardingAlgorithmPropertyContext each : ctx.shardingAlgorithmProperty()) {
+ result.getValue().setProperty(each.shardingAlgorithmPropertyKey().getText(), each.shardingAlgorithmPropertyValue().getText());
}
return result;
}
diff --git a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/TableRuleSegment.java b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/TableRuleSegment.java
index 1d24f50..1405c16 100644
--- a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/TableRuleSegment.java
+++ b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/TableRuleSegment.java
@@ -22,6 +22,7 @@ import lombok.Setter;
import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
import java.util.Collection;
+import java.util.Properties;
/**
* Table rule segment.
@@ -38,5 +39,5 @@ public final class TableRuleSegment implements ASTNode {
private String algorithmType;
- private Collection<String> properties;
+ private Properties algorithmProps;
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/converter/CreateShardingRuleStatementContextConverter.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/converter/CreateShardingRuleStatementContextConverter.java
index a5aa9d8..4b03399 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/converter/CreateShardingRuleStatementContextConverter.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/converter/CreateShardingRuleStatementContextConverter.java
@@ -18,26 +18,16 @@
package org.apache.shardingsphere.sharding.converter;
import com.google.common.base.Joiner;
-import com.google.common.base.Preconditions;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.distsql.parser.segment.rdl.TableRuleSegment;
import org.apache.shardingsphere.infra.binder.statement.rdl.CreateShardingRuleStatementContext;
-import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmPropertiesAware;
-import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.yaml.config.algorithm.YamlShardingSphereAlgorithmConfiguration;
-import org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorithm;
-import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
import org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.rule.YamlShardingAutoTableRuleConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.strategy.sharding.YamlShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.yaml.config.strategy.sharding.YamlStandardShardingStrategyConfiguration;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.Properties;
-
/**
* Create sharding rule statement context converter.
*/
@@ -53,38 +43,19 @@ public final class CreateShardingRuleStatementContextConverter {
public static YamlShardingRuleConfiguration convert(final CreateShardingRuleStatementContext sqlStatementContext) {
YamlShardingRuleConfiguration result = new YamlShardingRuleConfiguration();
for (TableRuleSegment each : sqlStatementContext.getSqlStatement().getTables()) {
- result.getShardingAlgorithms().put(getAlgorithmName(each.getLogicTable(), each.getAlgorithmType()),
- createAlgorithmConfiguration(each, getAlgorithmProperties(each.getAlgorithmType(), each.getProperties())));
+ result.getShardingAlgorithms().put(getAlgorithmName(each.getLogicTable(), each.getAlgorithmType()), createAlgorithmConfiguration(each));
result.getAutoTables().put(each.getLogicTable(), createAutoTableRuleConfiguration(each));
}
return result;
}
- private static YamlShardingSphereAlgorithmConfiguration createAlgorithmConfiguration(final TableRuleSegment segment, final Properties properties) {
+ private static YamlShardingSphereAlgorithmConfiguration createAlgorithmConfiguration(final TableRuleSegment segment) {
YamlShardingSphereAlgorithmConfiguration result = new YamlShardingSphereAlgorithmConfiguration();
result.setType(segment.getAlgorithmType());
- result.setProps(properties);
+ result.setProps(segment.getAlgorithmProps());
return result;
}
- private static Properties getAlgorithmProperties(final String type, final Collection<String> propertyValues) {
- Collection<String> propertyKeys = findAlgorithmPropertyKeys(type);
- Preconditions.checkArgument(propertyKeys.size() == propertyValues.size(), "%s needs %d properties, but %s properties are given.", type, propertyKeys.size(), propertyValues.size());
- Properties result = new Properties();
- Iterator<String> keys = propertyKeys.iterator();
- Iterator<String> values = propertyValues.iterator();
- while (keys.hasNext()) {
- result.setProperty(keys.next(), values.next());
- }
- return result;
- }
-
- private static Collection<String> findAlgorithmPropertyKeys(final String algorithmType) {
- return ShardingSphereServiceLoader.newServiceInstances(ShardingAlgorithm.class).stream()
- .filter(each -> each instanceof ShardingAutoTableAlgorithm && each.getType().equals(algorithmType)).findFirst()
- .map(each -> ((ShardingSphereAlgorithmPropertiesAware) each).getAllPropertyKeys()).orElse(Collections.emptyList());
- }
-
private static YamlShardingAutoTableRuleConfiguration createAutoTableRuleConfiguration(final TableRuleSegment segment) {
YamlShardingAutoTableRuleConfiguration result = new YamlShardingAutoTableRuleConfiguration();
result.setLogicTable(segment.getLogicTable());
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/converter/CreateShardingRuleStatementContextConverterTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/converter/CreateShardingRuleStatementContextConverterTest.java
index f8cfcf0..a3a06ed 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/converter/CreateShardingRuleStatementContextConverterTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/converter/CreateShardingRuleStatementContextConverterTest.java
@@ -28,6 +28,7 @@ import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
+import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -47,7 +48,9 @@ public final class CreateShardingRuleStatementContextConverterTest {
segment.setDataSources(Arrays.asList("ds0", "ds1"));
segment.setShardingColumn("order_id");
segment.setAlgorithmType("MOD");
- segment.setProperties(Collections.singleton("2"));
+ Properties props = new Properties();
+ props.setProperty("sharding_count", "2");
+ segment.setAlgorithmProps(props);
context = new CreateShardingRuleStatementContext(new CreateShardingRuleStatement(Collections.singleton(segment)));
}
diff --git a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/TableRuleSegment.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/value/props/PropertiesValue.java
similarity index 64%
copy from shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/TableRuleSegment.java
copy to shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/value/props/PropertiesValue.java
index 1d24f50..9beea45 100644
--- a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/TableRuleSegment.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/value/props/PropertiesValue.java
@@ -15,28 +15,18 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.distsql.parser.segment.rdl;
+package org.apache.shardingsphere.sql.parser.sql.common.value.props;
import lombok.Getter;
-import lombok.Setter;
-import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
+import org.apache.shardingsphere.sql.parser.sql.common.value.ValueASTNode;
-import java.util.Collection;
+import java.util.Properties;
/**
- * Table rule segment.
+ * Properties value.
*/
@Getter
-@Setter
-public final class TableRuleSegment implements ASTNode {
+public final class PropertiesValue implements ValueASTNode<Properties> {
- private String logicTable;
-
- private Collection<String> dataSources;
-
- private String shardingColumn;
-
- private String algorithmType;
-
- private Collection<String> properties;
+ private final Properties value = new Properties();
}