This is an automated email from the ASF dual-hosted git repository.

chenjiahao 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 9a706658c9b Refactor ShowSingleTableExecutor & 
ShowSingleTableExecutorTest (#29744)
9a706658c9b is described below

commit 9a706658c9b0c3ace3b1ffd4f115d348560ad451
Author: Raigor <[email protected]>
AuthorDate: Wed Jan 17 13:08:32 2024 +0800

    Refactor ShowSingleTableExecutor & ShowSingleTableExecutorTest (#29744)
    
    * Remove incorrect test case in ShowSingleTableExecutorTest
    
    * Refactor ShowSingleTableExecutor
    
    * Fix style
    
    * Refactor ShowSingleTableExecutor
    
    * Refactor ShowSingleTableExecutor
---
 .../handler/query/ShowSingleTableExecutor.java     | 48 ++++++++++++-----
 .../handler/query/ShowSingleTableExecutorTest.java | 62 +++++-----------------
 2 files changed, 50 insertions(+), 60 deletions(-)

diff --git 
a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTableExecutor.java
 
b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTableExecutor.java
index 900e9ad4537..5d0c7135c5b 100644
--- 
a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTableExecutor.java
+++ 
b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTableExecutor.java
@@ -28,10 +28,12 @@ import 
org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtils;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Comparator;
-import java.util.Objects;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Optional;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
-import java.util.stream.Stream;
 
 /**
  * Show single table executor.
@@ -45,20 +47,42 @@ public final class ShowSingleTableExecutor implements 
RQLExecutor<ShowSingleTabl
     
     @Override
     public Collection<LocalDataQueryResultRow> getRows(final 
ShardingSphereDatabase database, final ShowSingleTableStatement sqlStatement) {
-        return getDataNodes(database, sqlStatement).stream().map(each -> new 
LocalDataQueryResultRow(each.getTableName(), 
each.getDataSourceName())).collect(Collectors.toList());
+        SingleRule singleRule = 
database.getRuleMetaData().getSingleRule(SingleRule.class);
+        Map<String, Collection<DataNode>> singleTableNodes = 
singleRule.getSingleTableDataNodes();
+        return getRows(singleTableNodes, sqlStatement);
     }
     
-    private Collection<DataNode> getDataNodes(final ShardingSphereDatabase 
database, final ShowSingleTableStatement sqlStatement) {
-        Stream<DataNode> singleTableNodes = 
database.getRuleMetaData().findRules(SingleRule.class).stream()
-                .map(each -> 
each.getSingleTableDataNodes().values()).flatMap(Collection::stream).filter(Objects::nonNull).map(each
 -> each.iterator().next());
-        if (sqlStatement.getTableName().isPresent()) {
-            singleTableNodes = singleTableNodes.filter(each -> 
sqlStatement.getTableName().get().equals(each.getTableName()));
+    private Collection<LocalDataQueryResultRow> getRows(final Map<String, 
Collection<DataNode>> singleTableNodes, final ShowSingleTableStatement 
sqlStatement) {
+        Optional<Pattern> pattern = getPattern(sqlStatement);
+        Collection<DataNode> resultDataNodes = pattern.map(optional -> 
getDataNodesWithLikePattern(singleTableNodes, optional)).orElseGet(() -> 
getDataNodes(singleTableNodes, sqlStatement));
+        Collection<DataNode> sortedDataNodes = 
resultDataNodes.stream().sorted(Comparator.comparing(DataNode::getTableName)).collect(Collectors.toList());
+        return sortedDataNodes.stream().map(each -> new 
LocalDataQueryResultRow(each.getTableName(), 
each.getDataSourceName())).collect(Collectors.toList());
+    }
+    
+    private Collection<DataNode> getDataNodesWithLikePattern(final Map<String, 
Collection<DataNode>> singleTableNodes, final Pattern pattern) {
+        Collection<DataNode> result = new LinkedList<>();
+        for (final Entry<String, Collection<DataNode>> entry : 
singleTableNodes.entrySet()) {
+            if (pattern.matcher(entry.getKey()).matches()) {
+                result.add(entry.getValue().iterator().next());
+            }
         }
-        if (sqlStatement.getLikePattern().isPresent()) {
-            String pattern = 
SQLUtils.convertLikePatternToRegex(sqlStatement.getLikePattern().get());
-            singleTableNodes = singleTableNodes.filter(each -> 
Pattern.compile(pattern, 
Pattern.CASE_INSENSITIVE).matcher(each.getTableName()).matches()).collect(Collectors.toList()).stream();
+        return result;
+    }
+    
+    private Collection<DataNode> getDataNodes(final Map<String, 
Collection<DataNode>> singleTableNodes, final ShowSingleTableStatement 
sqlStatement) {
+        Collection<DataNode> result = new LinkedList<>();
+        for (final Entry<String, Collection<DataNode>> entry : 
singleTableNodes.entrySet()) {
+            if (!sqlStatement.getTableName().isPresent() || 
sqlStatement.getTableName().get().equalsIgnoreCase(entry.getKey())) {
+                result.add(entry.getValue().iterator().next());
+            }
         }
-        return 
singleTableNodes.sorted(Comparator.comparing(DataNode::getTableName)).collect(Collectors.toList());
+        return result;
+    }
+    
+    private Optional<Pattern> getPattern(final ShowSingleTableStatement 
sqlStatement) {
+        return sqlStatement.getLikePattern().isPresent()
+                ? 
Optional.of(Pattern.compile(SQLUtils.convertLikePatternToRegex(sqlStatement.getLikePattern().get()),
 Pattern.CASE_INSENSITIVE))
+                : Optional.empty();
     }
     
     @Override
diff --git 
a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTableExecutorTest.java
 
b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTableExecutorTest.java
index 784d19c23c8..22f908d97fe 100644
--- 
a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTableExecutorTest.java
+++ 
b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTableExecutorTest.java
@@ -17,12 +17,10 @@
 
 package org.apache.shardingsphere.single.distsql.handler.query;
 
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
 import org.apache.shardingsphere.infra.datanode.DataNode;
 import 
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import 
org.apache.shardingsphere.single.distsql.statement.rql.ShowSingleTableStatement;
 import org.apache.shardingsphere.single.rule.SingleRule;
 import org.junit.jupiter.api.BeforeEach;
@@ -33,7 +31,6 @@ import org.mockito.junit.jupiter.MockitoExtension;
 import org.mockito.junit.jupiter.MockitoSettings;
 import org.mockito.quality.Strictness;
 
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
@@ -64,58 +61,31 @@ class ShowSingleTableExecutorTest {
     
     @Test
     void assertGetRowData() {
-        RQLExecutor<ShowSingleTableStatement> executor = new 
ShowSingleTableExecutor();
-        Collection<LocalDataQueryResultRow> actual = 
executor.getRows(database, mock(ShowSingleTableStatement.class));
+        Collection<LocalDataQueryResultRow> actual = new 
ShowSingleTableExecutor().getRows(database, 
mock(ShowSingleTableStatement.class));
         assertThat(actual.size(), is(2));
-        Iterator<LocalDataQueryResultRow> rowData = actual.iterator();
-        LocalDataQueryResultRow firstRow = rowData.next();
-        assertThat(firstRow.getCell(1), is("t_order"));
-        assertThat(firstRow.getCell(2), is("ds_1"));
-        LocalDataQueryResultRow secondRow = rowData.next();
-        assertThat(secondRow.getCell(1), is("t_order_item"));
-        assertThat(secondRow.getCell(2), is("ds_2"));
-    }
-    
-    @Test
-    void assertGetRowDataMultipleRules() {
-        Map<String, Collection<DataNode>> singleTableDataNodeMap = new 
HashMap<>();
-        singleTableDataNodeMap.put("t_order_multiple", 
Collections.singleton(new DataNode("ds_1_multiple", "t_order_multiple")));
-        singleTableDataNodeMap.put("t_order_item_multiple", 
Collections.singleton(new DataNode("ds_2_multiple", "t_order_item_multiple")));
-        addShardingSphereRule(mockSingleRule(singleTableDataNodeMap));
-        RQLExecutor<ShowSingleTableStatement> executor = new 
ShowSingleTableExecutor();
-        Collection<LocalDataQueryResultRow> actual = 
executor.getRows(database, mock(ShowSingleTableStatement.class));
-        assertThat(actual.size(), is(4));
-        Iterator<LocalDataQueryResultRow> rowData = actual.iterator();
-        LocalDataQueryResultRow firstRow = rowData.next();
-        assertThat(firstRow.getCell(1), is("t_order"));
-        assertThat(firstRow.getCell(2), is("ds_1"));
-        LocalDataQueryResultRow secondRow = rowData.next();
-        assertThat(secondRow.getCell(1), is("t_order_item"));
-        assertThat(secondRow.getCell(2), is("ds_2"));
-        LocalDataQueryResultRow thirdRow = rowData.next();
-        assertThat(thirdRow.getCell(1), is("t_order_item_multiple"));
-        assertThat(thirdRow.getCell(2), is("ds_2_multiple"));
-        LocalDataQueryResultRow fourthRow = rowData.next();
-        assertThat(fourthRow.getCell(1), is("t_order_multiple"));
-        assertThat(fourthRow.getCell(2), is("ds_1_multiple"));
+        Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
+        LocalDataQueryResultRow row = iterator.next();
+        assertThat(row.getCell(1), is("t_order"));
+        assertThat(row.getCell(2), is("ds_1"));
+        row = iterator.next();
+        assertThat(row.getCell(1), is("t_order_item"));
+        assertThat(row.getCell(2), is("ds_2"));
     }
     
     @Test
     void assertGetSingleTableWithLikeLiteral() {
-        RQLExecutor<ShowSingleTableStatement> executor = new 
ShowSingleTableExecutor();
         ShowSingleTableStatement statement = new 
ShowSingleTableStatement(null, "%item", null);
-        Collection<LocalDataQueryResultRow> actual = 
executor.getRows(database, statement);
+        Collection<LocalDataQueryResultRow> actual = new 
ShowSingleTableExecutor().getRows(database, statement);
         assertThat(actual.size(), is(1));
-        Iterator<LocalDataQueryResultRow> rowData = actual.iterator();
-        LocalDataQueryResultRow firstRow = rowData.next();
-        assertThat(firstRow.getCell(1), is("t_order_item"));
-        assertThat(firstRow.getCell(2), is("ds_2"));
+        Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
+        LocalDataQueryResultRow row = iterator.next();
+        assertThat(row.getCell(1), is("t_order_item"));
+        assertThat(row.getCell(2), is("ds_2"));
     }
     
     @Test
     void assertGetColumns() {
-        RQLExecutor<ShowSingleTableStatement> executor = new 
ShowSingleTableExecutor();
-        Collection<String> columns = executor.getColumnNames();
+        Collection<String> columns = new 
ShowSingleTableExecutor().getColumnNames();
         assertThat(columns.size(), is(2));
         Iterator<String> iterator = columns.iterator();
         assertThat(iterator.next(), is("table_name"));
@@ -127,8 +97,4 @@ class ShowSingleTableExecutorTest {
         
when(result.getSingleTableDataNodes()).thenReturn(singleTableDataNodeMap);
         return result;
     }
-    
-    private void addShardingSphereRule(final ShardingSphereRule... rules) {
-        database.getRuleMetaData().getRules().addAll(Arrays.asList(rules));
-    }
 }

Reply via email to