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 2021/01/18 11:18:34 UTC
[shardingsphere] branch master updated: Support show sharding rule
(#9075)
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 e006ad6 Support show sharding rule (#9075)
e006ad6 is described below
commit e006ad6e3e40ab64edeac06d1369c2548ef18df9
Author: JingShang Lu <lu...@apache.org>
AuthorDate: Mon Jan 18 19:18:07 2021 +0800
Support show sharding rule (#9075)
* add support for show sharding rule
* fix
* fix support for show sharding rule
* fix
* fix
* fix
---
.../src/main/antlr4/imports/RDLStatement.g4 | 4 +
.../distsql/parser/core/DistSQLVisitor.java | 9 ++
.../text/distsql/rql/RQLBackendHandlerFactory.java | 3 +-
.../rql/impl/ShardingRuleQueryBackendHandler.java | 153 +++++++++++++++++++++
4 files changed, 168 insertions(+), 1 deletion(-)
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 2f512a6..5b1a694 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
@@ -103,6 +103,10 @@ dropShardingRule
: DROP SHARDING RULE LP tableName (COMMA tableName)* RP
;
+showShardingRule
+ : SHOW SHARDING RULE (FROM schemaName)?
+ ;
+
schemaNames
: schemaName (COMMA schemaName)*
;
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 73fd205..cbac984 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
@@ -36,6 +36,7 @@ import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.S
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShardingTableRuleDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowResourcesContext;
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowRuleContext;
+import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowShardingRuleContext;
import org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.TableNameContext;
import org.apache.shardingsphere.distsql.parser.segment.DataSourceSegment;
import org.apache.shardingsphere.distsql.parser.segment.TableRuleSegment;
@@ -218,6 +219,14 @@ public final class DistSQLVisitor extends DistSQLStatementBaseVisitor<ASTNode> {
}
@Override
+ public ASTNode visitShowShardingRule(final ShowShardingRuleContext ctx) {
+ if (null != ctx.schemaName()) {
+ return new ShowRuleStatement("sharding", (SchemaSegment) visitSchemaName(ctx.schemaName()));
+ }
+ return new ShowRuleStatement("sharding", null);
+ }
+
+ @Override
public ASTNode visitShowResources(final ShowResourcesContext ctx) {
return new ShowResourcesStatement(null == ctx.schemaName() ? null : (SchemaSegment) visit(ctx.schemaName()));
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RQLBackendHandlerFactory.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RQLBackendHandlerFactory.java
index d8057e2..f22e394 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RQLBackendHandlerFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RQLBackendHandlerFactory.java
@@ -26,6 +26,7 @@ import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl.DataSourcesQueryBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl.ReplicaQueryRuleQueryBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl.RuleQueryBackendHandler;
+import org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl.ShardingRuleQueryBackendHandler;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import java.util.Optional;
@@ -48,7 +49,7 @@ public final class RQLBackendHandlerFactory {
String ruleType = ((ShowRuleStatement) sqlStatement).getRuleType();
switch (ruleType.toUpperCase()) {
case "SHARDING":
- return Optional.of(new RuleQueryBackendHandler((ShowRuleStatement) sqlStatement, backendConnection));
+ return Optional.of(new ShardingRuleQueryBackendHandler((ShowRuleStatement) sqlStatement, backendConnection));
case "REPLICA_QUERY":
return Optional.of(new ReplicaQueryRuleQueryBackendHandler((ShowRuleStatement) sqlStatement, backendConnection));
case "ENCRYPT":
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingRuleQueryBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingRuleQueryBackendHandler.java
new file mode 100644
index 0000000..a660242
--- /dev/null
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingRuleQueryBackendHandler.java
@@ -0,0 +1,153 @@
+/*
+ * 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.proxy.backend.text.distsql.rql.impl;
+
+import com.google.gson.Gson;
+import org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowRuleStatement;
+import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
+import org.apache.shardingsphere.proxy.backend.response.header.query.QueryResponseHeader;
+import org.apache.shardingsphere.proxy.backend.response.header.query.impl.QueryHeader;
+import org.apache.shardingsphere.proxy.backend.text.SchemaRequiredBackendHandler;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
+import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
+import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ComplexShardingStrategyConfiguration;
+import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
+import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
+
+import java.sql.Types;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Backend handler for show sharding rules.
+ */
+public final class ShardingRuleQueryBackendHandler extends SchemaRequiredBackendHandler<ShowRuleStatement> {
+
+ private Iterator<Map<String, Object>> data;
+
+ private final String schema;
+
+ public ShardingRuleQueryBackendHandler(final ShowRuleStatement sqlStatement, final BackendConnection backendConnection) {
+ super(sqlStatement, backendConnection);
+ if (sqlStatement.getSchema().isPresent()) {
+ schema = sqlStatement.getSchema().get().getIdentifier().getValue();
+ } else {
+ schema = backendConnection.getSchemaName();
+ }
+ }
+
+ @Override
+ protected ResponseHeader execute(final String schemaName, final ShowRuleStatement sqlStatement) {
+ List<QueryHeader> queryHeader = getQueryHeader();
+ data = loadRuleConfiguration();
+ return new QueryResponseHeader(queryHeader);
+ }
+
+ private List<QueryHeader> getQueryHeader() {
+ List<QueryHeader> result = new LinkedList();
+ result.add(new QueryHeader(schema, "", "name", "name", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
+ result.add(new QueryHeader(schema, "", "actualDataNodes", "actualDataNodes", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
+ result.add(new QueryHeader(schema, "", "tableStrategy", "tableStrategy", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
+ result.add(new QueryHeader(schema, "", "databaseStrategy", "databaseStrategy", Types.BIGINT, "BIGINT", 255, 0, false, false, false, false));
+ result.add(new QueryHeader(schema, "", "keyGenerateStrategy", "keyGenerateStrategy", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
+ result.add(new QueryHeader(schema, "", "bindingTable", "bindingTable", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
+ return result;
+ }
+
+ private Iterator<Map<String, Object>> loadRuleConfiguration() {
+ List<Map<String, Object>> result = new LinkedList<>();
+ Optional<ShardingRuleConfiguration> ruleConfig = ProxyContext.getInstance().getMetaData(schema).getRuleMetaData().getConfigurations()
+ .stream().map(each -> (ShardingRuleConfiguration) each).findFirst();
+ if (ruleConfig.isPresent()) {
+ List<List<String>> bindingTables = ruleConfig.get().getBindingTableGroups().stream().filter(each -> null != each && !"".equals(each)).map(each -> Arrays.asList(each.split(",")))
+ .collect(Collectors.toList());
+ for (ShardingTableRuleConfiguration each : ruleConfig.get().getTables()) {
+ Map<String, Object> table = new HashMap<>();
+ table.put("name", each.getLogicTable());
+ table.put("actualDataNodes", each.getActualDataNodes());
+ table.put("tableStrategy", generateShardingStrategy(ruleConfig.get(), each.getTableShardingStrategy()));
+ table.put("databaseStrategy", generateShardingStrategy(ruleConfig.get(), each.getDatabaseShardingStrategy()));
+ table.put("keyGenerateStrategy", generateKeyGenerateStrategy(ruleConfig.get(), each.getKeyGenerateStrategy()));
+ table.put("bindingTable", generateBindingTable(bindingTables, each.getLogicTable()));
+ result.add(table);
+ }
+ }
+ return result.iterator();
+ }
+
+ private String generateBindingTable(final List<List<String>> bindingTableGroups, final String tableName) {
+ Set<String> bindingTable = new HashSet<>();
+ for (List<String> each : bindingTableGroups) {
+ if (each.contains(tableName)) {
+ for (String table : each) {
+ if (!table.equals(tableName)) {
+ bindingTable.add(table);
+ }
+ }
+ }
+ }
+ return (new Gson()).toJson(bindingTable);
+ }
+
+ private String generateShardingStrategy(final ShardingRuleConfiguration ruleConfig, final ShardingStrategyConfiguration shardingStrategy) {
+ StringBuilder result = new StringBuilder();
+ if (shardingStrategy instanceof ComplexShardingStrategyConfiguration) {
+ result.append("shardingColumns:");
+ result.append(((ComplexShardingStrategyConfiguration) shardingStrategy).getShardingColumns());
+ result.append(" ");
+ } else if (shardingStrategy instanceof StandardShardingStrategyConfiguration) {
+ result.append("shardingColumn:");
+ result.append(((StandardShardingStrategyConfiguration) shardingStrategy).getShardingColumn());
+ result.append(" ");
+ }
+ result.append((new Gson()).toJson(ruleConfig.getShardingAlgorithms().get(shardingStrategy.getShardingAlgorithmName())));
+ return result.toString();
+ }
+
+ private String generateKeyGenerateStrategy(final ShardingRuleConfiguration ruleConfig, final KeyGenerateStrategyConfiguration keyGenerateStrategy) {
+ StringBuilder result = new StringBuilder();
+ result.append("column:");
+ result.append(keyGenerateStrategy.getColumn());
+ result.append(" ");
+ result.append((new Gson()).toJson(ruleConfig.getKeyGenerators().get(keyGenerateStrategy.getKeyGeneratorName())));
+ return result.toString();
+ }
+
+ @Override
+ public boolean next() {
+ return data.hasNext();
+ }
+
+ @Override
+ public Collection<Object> getRowData() {
+ Map<String, Object> table = data.next();
+ return Arrays.asList(table.get("name"), table.get("actualDataNodes"), table.get("tableStrategy"), table.get("databaseStrategy"), table.get("keyGenerateStrategy"), table.get("bindingTable"));
+ }
+}