You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2026/04/07 10:59:24 UTC

(shardingsphere) branch master updated: Make SHOW UNLOADED SINGLE TABLES result order deterministic (#38594)

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

menghaoran 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 04cecfc6f98 Make SHOW UNLOADED SINGLE TABLES result order deterministic (#38594)
04cecfc6f98 is described below

commit 04cecfc6f980319ad3aa5c8fabce30f9b06ea61f
Author: Haoran Meng <me...@gmail.com>
AuthorDate: Tue Apr 7 18:59:17 2026 +0800

    Make SHOW UNLOADED SINGLE TABLES result order deterministic (#38594)
---
 .../query/ShowUnloadedSingleTablesExecutor.java      | 20 +++++++++-----------
 .../query/ShowUnloadedSingleTablesExecutorTest.java  | 14 ++++++++++++++
 2 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowUnloadedSingleTablesExecutor.java b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowUnloadedSingleTablesExecutor.java
index e97fc58de5e..d0a158ca60a 100644
--- a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowUnloadedSingleTablesExecutor.java
+++ b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowUnloadedSingleTablesExecutor.java
@@ -36,8 +36,8 @@ import org.apache.shardingsphere.single.util.SingleTableLoadUtils;
 import javax.sql.DataSource;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Comparator;
 import java.util.LinkedHashMap;
-import java.util.LinkedList;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.stream.Collectors;
@@ -48,6 +48,9 @@ import java.util.stream.Collectors;
 @Setter
 public final class ShowUnloadedSingleTablesExecutor implements DistSQLQueryExecutor<ShowUnloadedSingleTablesStatement>, DistSQLExecutorDatabaseAware, DistSQLExecutorRuleAware<SingleRule> {
     
+    private static final Comparator<DataNode> DATA_NODE_COMPARATOR = Comparator.comparing(DataNode::getTableName)
+            .thenComparing(DataNode::getDataSourceName).thenComparing(DataNode::getSchemaName, Comparator.nullsFirst(String::compareTo));
+    
     private ShardingSphereDatabase database;
     
     private SingleRule rule;
@@ -72,16 +75,11 @@ public final class ShowUnloadedSingleTablesExecutor implements DistSQLQueryExecu
                 tableNodes.removeIf(each -> entry.getValue().contains(each));
             }
         }
-        Collection<LocalDataQueryResultRow> result = new LinkedList<>();
-        actualDataNodes.values().stream().map(this::getRows).forEach(result::addAll);
-        return result;
-    }
-    
-    private Collection<LocalDataQueryResultRow> getRows(final Collection<DataNode> dataNodes) {
-        if (new DatabaseTypeRegistry(database.getProtocolType()).getDialectDatabaseMetaData().getSchemaOption().isSchemaAvailable()) {
-            return dataNodes.stream().map(each -> new LocalDataQueryResultRow(each.getTableName(), each.getDataSourceName(), each.getSchemaName())).collect(Collectors.toList());
-        }
-        return dataNodes.stream().map(each -> new LocalDataQueryResultRow(each.getTableName(), each.getDataSourceName())).collect(Collectors.toList());
+        boolean isSchemaAvailable = new DatabaseTypeRegistry(database.getProtocolType()).getDialectDatabaseMetaData().getSchemaOption().isSchemaAvailable();
+        return actualDataNodes.values().stream().flatMap(Collection::stream).sorted(DATA_NODE_COMPARATOR)
+                .map(each -> isSchemaAvailable ? new LocalDataQueryResultRow(each.getTableName(), each.getDataSourceName(), each.getSchemaName())
+                        : new LocalDataQueryResultRow(each.getTableName(), each.getDataSourceName()))
+                .collect(Collectors.toList());
     }
     
     private Map<String, Collection<DataNode>> getActualDataNodes(final ShardingSphereDatabase database) {
diff --git a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowUnloadedSingleTablesExecutorTest.java b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowUnloadedSingleTablesExecutorTest.java
index 89a5c8fabd6..ffa10109164 100644
--- a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowUnloadedSingleTablesExecutorTest.java
+++ b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowUnloadedSingleTablesExecutorTest.java
@@ -51,6 +51,7 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -176,10 +177,23 @@ class ShowUnloadedSingleTablesExecutorTest {
                 Collections.singletonMap("t_order_item", Collections.singleton(new DataNode("ds_2", (String) null, "t_order_item"))));
         Map<String, Collection<DataNode>> unmatchedRuleDataNodes = new HashMap<>(
                 Collections.singletonMap("t_order", Collections.singleton(new DataNode("ds_0", (String) null, "t_order"))));
+        Map<String, Collection<DataNode>> unorderedActualDataNodes = new LinkedHashMap<>(2, 1F);
+        unorderedActualDataNodes.put("t_order_item", Collections.singleton(new DataNode("ds_1", (String) null, "t_order_item")));
+        unorderedActualDataNodes.put("t_order", Collections.singleton(new DataNode("ds_0", (String) null, "t_order")));
+        Map<String, Collection<DataNode>> unorderedStorageUnitActualDataNodes = new HashMap<>(
+                Collections.singletonMap("t_order", new LinkedList<>(Arrays.asList(new DataNode("ds_1", "public", "t_order"), new DataNode("ds_0", "public", "t_order")))));
+        Map<String, Collection<DataNode>> unorderedSchemaActualDataNodes = new HashMap<>(
+                Collections.singletonMap("t_order", new LinkedList<>(Arrays.asList(new DataNode("ds_0", "schema_b", "t_order"), new DataNode("ds_0", "schema_a", "t_order")))));
         return Stream.of(
                 Arguments.of("all loaded tables are excluded", false, allLoadedActualDataNodes, allLoadedRuleDataNodes, Collections.<List<String>>emptyList()),
+                Arguments.of("remaining tables are sorted by table name",
+                        false, unorderedActualDataNodes, Collections.emptyMap(), Arrays.asList(Arrays.asList("t_order", "ds_0"), Arrays.asList("t_order_item", "ds_1"))),
                 Arguments.of("partially loaded table keeps remaining nodes",
                         true, partiallyLoadedActualDataNodes, partiallyLoadedRuleDataNodes, Collections.singletonList(Arrays.asList("t_order", "ds_1", "public"))),
+                Arguments.of("same table nodes are sorted by storage unit name",
+                        true, unorderedStorageUnitActualDataNodes, Collections.emptyMap(), Arrays.asList(Arrays.asList("t_order", "ds_0", "public"), Arrays.asList("t_order", "ds_1", "public"))),
+                Arguments.of("same table and storage unit nodes are sorted by schema name",
+                        true, unorderedSchemaActualDataNodes, Collections.emptyMap(), Arrays.asList(Arrays.asList("t_order", "ds_0", "schema_a"), Arrays.asList("t_order", "ds_0", "schema_b"))),
                 Arguments.of("unmatched rule key keeps actual table nodes",
                         false, unmatchedActualDataNodes, unmatchedRuleDataNodes, Collections.singletonList(Arrays.asList("t_order_item", "ds_2"))));
     }