From 8adec17aa785c6b736aeab2a43d476cf931c5f33 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 15 Sep 2020 19:44:38 +0800 Subject: [PATCH] Fix #7342 --- .../dal/show/LogicTablesMergedResult.java | 2 +- .../dal/show/ShowTablesMergedResultTest.java | 6 --- .../infra/route/DataNodeRouter.java | 17 +++--- .../UnconfiguredSchemaRouteDecorator.java | 54 +++++++++++++++++++ 4 files changed, 64 insertions(+), 15 deletions(-) create mode 100644 shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/decorator/UnconfiguredSchemaRouteDecorator.java diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/main/java/org/apache/shardingsphere/sharding/merge/dal/show/LogicTablesMergedResult.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/main/java/org/apache/shardingsphere/sharding/merge/dal/show/LogicTablesMergedResult.java index bb3a15e4f8000..10dc18503fb99 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/main/java/org/apache/shardingsphere/sharding/merge/dal/show/LogicTablesMergedResult.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/main/java/org/apache/shardingsphere/sharding/merge/dal/show/LogicTablesMergedResult.java @@ -53,7 +53,7 @@ protected final List init(final ShardingRule shardingRule, String actualTableName = memoryResultSetRow.getCell(1).toString(); Optional tableRule = shardingRule.findTableRuleByActualTable(actualTableName); if (!tableRule.isPresent()) { - if (shardingRule.getTableRules().isEmpty() || schemaMetaData.containsTable(actualTableName) && tableNames.add(actualTableName)) { + if (shardingRule.getTableRules().isEmpty() || tableNames.add(actualTableName)) { result.add(memoryResultSetRow); } } else if (tableNames.add(tableRule.get().getLogicTable())) { diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowTablesMergedResultTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowTablesMergedResultTest.java index f3f7f739fe368..33d36411b702e 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowTablesMergedResultTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowTablesMergedResultTest.java @@ -82,10 +82,4 @@ public void assertNextForActualTableNameInTableRule() throws SQLException { LogicTablesMergedResult actual = new LogicTablesMergedResult(shardingRule, mock(SQLStatementContext.class), schemaMetaData, Collections.singletonList(createQueryResult("table_0"))); assertTrue(actual.next()); } - - @Test - public void assertNextForActualTableNameNotInTableRule() throws SQLException { - LogicTablesMergedResult actual = new LogicTablesMergedResult(shardingRule, mock(SQLStatementContext.class), schemaMetaData, Collections.singletonList(createQueryResult("table_3"))); - assertFalse(actual.next()); - } } diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/DataNodeRouter.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/DataNodeRouter.java index 53d75e11726aa..957233ae5deb8 100644 --- a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/DataNodeRouter.java +++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/DataNodeRouter.java @@ -17,19 +17,20 @@ package org.apache.shardingsphere.infra.route; -import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; -import org.apache.shardingsphere.infra.spi.order.OrderedSPIRegistry; -import org.apache.shardingsphere.sql.parser.binder.SQLStatementContextFactory; -import org.apache.shardingsphere.sql.parser.binder.statement.CommonSQLStatementContext; -import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext; -import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; -import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.route.context.RouteResult; import org.apache.shardingsphere.infra.route.decorator.RouteDecorator; +import org.apache.shardingsphere.infra.route.decorator.UnconfiguredSchemaRouteDecorator; import org.apache.shardingsphere.infra.route.hook.SPIRoutingHook; +import org.apache.shardingsphere.infra.rule.ShardingSphereRule; +import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; +import org.apache.shardingsphere.infra.spi.order.OrderedSPIRegistry; +import org.apache.shardingsphere.sql.parser.binder.SQLStatementContextFactory; +import org.apache.shardingsphere.sql.parser.binder.statement.CommonSQLStatementContext; +import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext; +import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; import java.util.Collection; import java.util.List; @@ -89,7 +90,7 @@ private RouteContext executeRoute(final SQLStatement sqlStatement, final List entry : decorators.entrySet()) { result = entry.getValue().decorate(result, metaData, entry.getKey(), props); } - return result; + return new UnconfiguredSchemaRouteDecorator().decorate(result, metaData); } private RouteContext createRouteContext(final SQLStatement sqlStatement, final List parameters) { diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/decorator/UnconfiguredSchemaRouteDecorator.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/decorator/UnconfiguredSchemaRouteDecorator.java new file mode 100644 index 0000000000000..bf3d8a7b22e2c --- /dev/null +++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/decorator/UnconfiguredSchemaRouteDecorator.java @@ -0,0 +1,54 @@ +/* + * 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.infra.route.decorator; + +import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; +import org.apache.shardingsphere.infra.route.context.RouteContext; +import org.apache.shardingsphere.infra.route.context.RouteMapper; +import org.apache.shardingsphere.infra.route.context.RouteUnit; +import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; +import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowTablesStatement; + +import java.util.Collections; + +/** + * Unconfigured schema route decorator. + */ +public final class UnconfiguredSchemaRouteDecorator { + + /** + * Decorate route context. + * + * @param routeContext route context + * @param metaData meta data of ShardingSphere + * @return decorated route context + */ + public RouteContext decorate(final RouteContext routeContext, final ShardingSphereMetaData metaData) { + if (isNeedUnconfiguredSchema(routeContext.getSqlStatementContext().getSqlStatement())) { + for (String each : metaData.getRuleSchemaMetaData().getUnconfiguredSchemaMetaDataMap().keySet()) { + routeContext.getRouteResult().getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList())); + } + } + return routeContext; + } + + // TODO use dynamic config to judge UnconfiguredSchema + private boolean isNeedUnconfiguredSchema(final SQLStatement sqlStatement) { + return sqlStatement instanceof MySQLShowTablesStatement; + } +}