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 0e5a4c6 optimize show index merge logic (#10596)
0e5a4c6 is described below
commit 0e5a4c6805f9646d0c71aae5bfe4555b1420036e
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Tue Jun 1 16:59:31 2021 +0800
optimize show index merge logic (#10596)
---
.../merge/dal/ShardingDALResultMerger.java | 10 +++-
.../merge/dal/show/ShowIndexMergedResult.java | 61 ++++++++++++++++++++
.../merge/dal/ShardingDALResultMergerTest.java | 18 ++++--
.../merge/dal/show/ShowIndexMergedResultTest.java | 66 ++++++++++++++++++++++
4 files changed, 148 insertions(+), 7 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/main/java/org/apache/shardingsphere/sharding/merge/dal/ShardingDALResultMerger.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/main/java/org/apache/shardingsphere/sharding/merge/dal/ShardingDALResultMerger.java
index b128832..1838f2d 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/main/java/org/apache/shardingsphere/sharding/merge/dal/ShardingDALResultMerger.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/main/java/org/apache/shardingsphere/sharding/merge/dal/ShardingDALResultMerger.java
@@ -18,17 +18,18 @@
package org.apache.shardingsphere.sharding.merge.dal;
import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.database.DefaultSchema;
import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
import org.apache.shardingsphere.infra.merge.engine.merger.ResultMerger;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import
org.apache.shardingsphere.infra.merge.result.impl.transparent.TransparentMergedResult;
+import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import
org.apache.shardingsphere.sharding.merge.dal.common.SingleLocalDataMergedResult;
import
org.apache.shardingsphere.sharding.merge.dal.show.LogicTablesMergedResult;
import
org.apache.shardingsphere.sharding.merge.dal.show.ShowCreateTableMergedResult;
+import org.apache.shardingsphere.sharding.merge.dal.show.ShowIndexMergedResult;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowCreateTableStatement;
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowDatabasesStatement;
@@ -54,9 +55,12 @@ public final class ShardingDALResultMerger implements
ResultMerger {
if (dalStatement instanceof MySQLShowDatabasesStatement) {
return new
SingleLocalDataMergedResult(Collections.singletonList(DefaultSchema.LOGIC_NAME));
}
- if (dalStatement instanceof MySQLShowTablesStatement || dalStatement
instanceof MySQLShowTableStatusStatement || dalStatement instanceof
MySQLShowIndexStatement) {
+ if (dalStatement instanceof MySQLShowTablesStatement || dalStatement
instanceof MySQLShowTableStatusStatement) {
return new LogicTablesMergedResult(shardingRule,
sqlStatementContext, schema, queryResults);
}
+ if (dalStatement instanceof MySQLShowIndexStatement) {
+ return new ShowIndexMergedResult(shardingRule,
sqlStatementContext, schema, queryResults);
+ }
if (dalStatement instanceof MySQLShowCreateTableStatement) {
return new ShowCreateTableMergedResult(shardingRule,
sqlStatementContext, schema, queryResults);
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/main/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowIndexMergedResult.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/main/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowIndexMergedResult.java
new file mode 100644
index 0000000..70edb33
--- /dev/null
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/main/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowIndexMergedResult.java
@@ -0,0 +1,61 @@
+/*
+ * 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.sharding.merge.dal.show;
+
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
+import
org.apache.shardingsphere.infra.merge.result.impl.memory.MemoryMergedResult;
+import
org.apache.shardingsphere.infra.merge.result.impl.memory.MemoryQueryResultRow;
+import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
+import
org.apache.shardingsphere.infra.metadata.schema.builder.loader.util.TableMetaDataLoaderUtil;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sharding.rule.TableRule;
+
+import java.sql.SQLException;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * Merged result for show index.
+ */
+public class ShowIndexMergedResult extends MemoryMergedResult<ShardingRule> {
+
+ public ShowIndexMergedResult(final ShardingRule shardingRule,
+ final SQLStatementContext
sqlStatementContext, final ShardingSphereSchema schema, final List<QueryResult>
queryResults) throws SQLException {
+ super(shardingRule, schema, sqlStatementContext, queryResults);
+ }
+
+ @Override
+ protected final List<MemoryQueryResultRow> init(final ShardingRule
shardingRule, final ShardingSphereSchema schema,
+ final SQLStatementContext
sqlStatementContext, final List<QueryResult> queryResults) throws SQLException {
+ List<MemoryQueryResultRow> result = new LinkedList<>();
+ for (QueryResult each : queryResults) {
+ while (each.next()) {
+ MemoryQueryResultRow memoryResultSetRow = new
MemoryQueryResultRow(each);
+ String actualTableName =
memoryResultSetRow.getCell(1).toString();
+ String actualIndexName =
memoryResultSetRow.getCell(3).toString();
+ Optional<TableRule> tableRule =
shardingRule.findTableRuleByActualTable(actualTableName);
+ tableRule.ifPresent(rule -> memoryResultSetRow.setCell(1,
rule.getLogicTable()));
+ memoryResultSetRow.setCell(3,
TableMetaDataLoaderUtil.getLogicIndexName(actualIndexName, actualTableName));
+ result.add(memoryResultSetRow);
+ }
+ }
+ return result;
+ }
+}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dal/ShardingDALResultMergerTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dal/ShardingDALResultMergerTest.java
index d0cdb3d..81a00d4 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dal/ShardingDALResultMergerTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dal/ShardingDALResultMergerTest.java
@@ -17,19 +17,21 @@
package org.apache.shardingsphere.sharding.merge.dal;
-import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
+import
org.apache.shardingsphere.infra.merge.result.impl.transparent.TransparentMergedResult;
import
org.apache.shardingsphere.sharding.merge.dal.common.SingleLocalDataMergedResult;
import
org.apache.shardingsphere.sharding.merge.dal.show.LogicTablesMergedResult;
import
org.apache.shardingsphere.sharding.merge.dal.show.ShowCreateTableMergedResult;
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.sharding.merge.dal.show.ShowIndexMergedResult;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dal.DALStatement;
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLExplainStatement;
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowCreateTableStatement;
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowDatabasesStatement;
+import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowIndexStatement;
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowOtherStatement;
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowTablesStatement;
-import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
-import
org.apache.shardingsphere.infra.merge.result.impl.transparent.TransparentMergedResult;
import org.junit.Before;
import org.junit.Test;
@@ -91,6 +93,14 @@ public final class ShardingDALResultMergerTest {
assertThat(resultMerger.merge(queryResults, sqlStatementContext,
null), instanceOf(TransparentMergedResult.class));
}
+ @Test
+ public void assertMergeForShowIndexStatement() throws SQLException {
+ DALStatement dalStatement = new MySQLShowIndexStatement();
+ SQLStatementContext<?> sqlStatementContext =
mockSQLStatementContext(dalStatement);
+ ShardingDALResultMerger resultMerger = new
ShardingDALResultMerger(mock(ShardingRule.class));
+ assertThat(resultMerger.merge(queryResults, sqlStatementContext,
null), instanceOf(ShowIndexMergedResult.class));
+ }
+
@SuppressWarnings("unchecked")
private SQLStatementContext<DALStatement> mockSQLStatementContext(final
DALStatement dalStatement) {
SQLStatementContext<DALStatement> result =
mock(SQLStatementContext.class);
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowIndexMergedResultTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowIndexMergedResultTest.java
new file mode 100644
index 0000000..6872d8b
--- /dev/null
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/test/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowIndexMergedResultTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.sharding.merge.dal.show;
+
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
+import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.sql.SQLException;
+import java.util.Collections;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class ShowIndexMergedResultTest {
+
+ @Mock
+ private ShardingRule shardingRule;
+
+ @Mock
+ private ShardingSphereSchema schema;
+
+ @Test
+ public void assertNextForEmptyQueryResult() throws SQLException {
+ assertFalse(new ShowIndexMergedResult(shardingRule,
mock(SQLStatementContext.class), schema, Collections.emptyList()).next());
+ }
+
+ @Test
+ public void assertNextForTableRuleIsPresent() throws SQLException {
+ assertTrue(new ShowIndexMergedResult(shardingRule,
mock(SQLStatementContext.class), schema,
Collections.singletonList(mockQueryResult())).next());
+ }
+
+ private QueryResult mockQueryResult() throws SQLException {
+ QueryResult result = mock(QueryResult.class, RETURNS_DEEP_STUBS);
+ when(result.getMetaData().getColumnCount()).thenReturn(3);
+ when(result.next()).thenReturn(true, false);
+ when(result.getValue(1, Object.class)).thenReturn("t_order_0");
+ when(result.getValue(2, Object.class)).thenReturn(1);
+ when(result.getValue(3,
Object.class)).thenReturn("t_order_index_t_order_0");
+ return result;
+ }
+}