You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ja...@apache.org on 2026/10/22 08:19:37 UTC

(iotdb) branch master updated: [Query] Share table fragment exchange budgets and consume ready Collect inputs (#18869)

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

JackieTien97 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new ca207944c09 [Query] Share table fragment exchange budgets and consume ready Collect inputs (#18869)
ca207944c09 is described below

commit ca207944c094615a10e0871b7282906e3f4d65ab
Author: Colin Lee <10...@users.noreply.github.com>
AuthorDate: Thu Oct 22 16:19:30 2026 +0800

    [Query] Share table fragment exchange budgets and consume ready Collect inputs (#18869)
---
 .../iotdb/itbase/runtime/ClusterTestResultSet.java |  12 +
 .../org/apache/iotdb/db/it/utils/TestUtils.java    |  51 +-
 .../pipe/it/triple/IoTDBShowReceiversTripleIT.java |  83 +-
 .../it/db/it/IoTDBMultiDeviceTableIT.java          |  27 +-
 .../db/it/udf/IoTDBUserDefinedTableFunctionIT.java |  45 +-
 .../it/insertquery/IoTDBInsertQueryIT.java         |  31 +-
 .../it/query/recent/IoTDBClusterResultSetIT.java   | 163 ++++
 .../it/query/recent/IoTDBCollectFanInIT.java       | 363 +++++++++
 .../relational/it/query/recent/IoTDBCteIT.java     |  82 +-
 .../it/query/recent/IoTDBTableAggregationIT.java   |  28 +-
 .../recent/IoTDBTopKRuntimeFilterScanPathIT.java   |   3 +-
 .../IoTDBUncorrelatedExistsSubqueryIT.java         |  35 +-
 .../IoTDBUncorrelatedQuantifiedComparisonIT.java   | 296 ++++---
 .../IoTDBUncorrelatedScalarSubqueryIT.java         |  33 +-
 .../operator/process/AnyChildBlocked.java          | 274 +++++++
 .../operator/process/CollectOperator.java          | 145 +++-
 .../operator/process/MappingCollectOperator.java   |  56 +-
 .../operator/process/CollectOperatorTest.java      | 707 +++++++++++++++++
 .../iotdb/db/i18n/DataNodeQueryMessages.java       |   3 +
 .../iotdb/db/i18n/DataNodeQueryMessages.java       |   3 +
 .../execution/exchange/SharedTsBlockQueue.java     |   6 +-
 .../execution/exchange/sink/SinkChannel.java       |   6 +-
 .../execution/exchange/source/SourceHandle.java    |   6 +-
 .../fragment/FragmentInstanceContext.java          |  18 +
 .../queryengine/execution/memory/MemoryPool.java   | 448 ++++++++---
 .../queryengine/execution/memory/MemorySide.java   |  26 +
 .../planner/DataNodeTableOperatorGenerator.java    |   4 +-
 .../plan/planner/LocalExecutionPlanContext.java    |  77 +-
 .../plan/planner/LocalExecutionPlanner.java        |  29 +-
 .../planner/SharedExchangeBudgetEligibility.java   |  70 ++
 .../distribute/TableDistributedPlanGenerator.java  |  95 ++-
 .../planner/optimizations/ParallelizeGrouping.java |  16 +
 .../planner/optimizations/SortElimination.java     |  25 +
 .../exchange/MPPDataExchangeManagerTest.java       |  53 ++
 .../execution/exchange/SharedTsBlockQueueTest.java |  43 +
 .../execution/exchange/SourceHandleTest.java       |  64 ++
 .../execution/memory/MemoryPoolTest.java           | 874 ++++++++++++++++++++-
 .../LocalExecutionPlanContextBudgetTest.java       | 596 ++++++++++++++
 .../planner/LocalExecutionPlannerBudgetTest.java   | 245 ++++++
 .../planner/CollectInputPropertyTest.java          | 163 ++++
 .../optimizations/ParallelizeGroupingTest.java     |  58 ++
 41 files changed, 4856 insertions(+), 506 deletions(-)

diff --git a/integration-test/src/main/java/org/apache/iotdb/itbase/runtime/ClusterTestResultSet.java b/integration-test/src/main/java/org/apache/iotdb/itbase/runtime/ClusterTestResultSet.java
index 98686d436be..a9823b28572 100644
--- a/integration-test/src/main/java/org/apache/iotdb/itbase/runtime/ClusterTestResultSet.java
+++ b/integration-test/src/main/java/org/apache/iotdb/itbase/runtime/ClusterTestResultSet.java
@@ -40,6 +40,7 @@ import java.sql.Statement;
 import java.sql.Time;
 import java.sql.Timestamp;
 import java.util.Calendar;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 
@@ -68,6 +69,17 @@ public class ClusterTestResultSet implements ResultSet {
     this.env = env;
   }
 
+  /**
+   * Returns each node's result for assertions that do not require identical row order or plan text.
+   * Every returned result must be checked. The caller must close the original result set and must
+   * not mix per-node iteration with iteration through the cluster wrapper.
+   */
+  public static List<ResultSet> getNodeResultSets(ResultSet resultSet) {
+    return resultSet instanceof ClusterTestResultSet
+        ? Collections.unmodifiableList(((ClusterTestResultSet) resultSet).resultSets)
+        : Collections.singletonList(resultSet);
+  }
+
   @Override
   public boolean next() throws SQLException {
     RequestDelegate<Boolean> delegate = createRequestDelegate();
diff --git a/integration-test/src/test/java/org/apache/iotdb/db/it/utils/TestUtils.java b/integration-test/src/test/java/org/apache/iotdb/db/it/utils/TestUtils.java
index 5eb22c598e0..cc2556578b9 100644
--- a/integration-test/src/test/java/org/apache/iotdb/db/it/utils/TestUtils.java
+++ b/integration-test/src/test/java/org/apache/iotdb/db/it/utils/TestUtils.java
@@ -65,6 +65,7 @@ import static org.apache.iotdb.itbase.constant.TestConstant.NULL;
 import static org.apache.iotdb.itbase.constant.TestConstant.TIMESTAMP_STR;
 import static org.apache.iotdb.itbase.env.BaseEnv.TABLE_SQL_DIALECT;
 import static org.apache.iotdb.itbase.env.BaseEnv.TREE_SQL_DIALECT;
+import static org.apache.iotdb.itbase.runtime.ClusterTestResultSet.getNodeResultSets;
 import static org.awaitility.Awaitility.await;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
@@ -807,6 +808,15 @@ public class TestUtils {
       final String expectedHeader,
       final Collection<String> expectedResult,
       final boolean printResult) {
+    if (expectedResult instanceof Set) {
+      List<ResultSet> nodeResults = getNodeResultSets(actualResultSet);
+      if (nodeResults.size() != 1 || nodeResults.get(0) != actualResultSet) {
+        for (ResultSet nodeResult : nodeResults) {
+          assertResultSetEqual(nodeResult, expectedHeader, expectedResult, printResult);
+        }
+        return;
+      }
+    }
     try {
       final ResultSetMetaData resultSetMetaData = actualResultSet.getMetaData();
       final StringBuilder header = new StringBuilder();
@@ -868,27 +878,36 @@ public class TestUtils {
       String expectedHeader,
       Set<String> expectedRetSet,
       Consumer consumer) {
-    try {
-      ResultSetMetaData resultSetMetaData = actualResultSet.getMetaData();
+    assertResultSetEqual(actualResultSet, expectedHeader, expectedRetSet, false);
+  }
+
+  /**
+   * Checks unordered rows on every node, preserving duplicate multiplicity.
+   *
+   * <p>The caller is responsible for ensuring that the query's final result has no ordering or
+   * arbitrary slice semantics that require deterministic row order.
+   */
+  public static void assertResultSetEqualUnordered(
+      ResultSet resultSet, String expectedHeader, List<String> expectedRows) throws SQLException {
+    List<String> sortedExpected = new ArrayList<>(expectedRows);
+    Collections.sort(sortedExpected);
+    for (ResultSet nodeResult : getNodeResultSets(resultSet)) {
+      ResultSetMetaData metadata = nodeResult.getMetaData();
       StringBuilder header = new StringBuilder();
-      for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {
-        header.append(resultSetMetaData.getColumnName(i)).append(",");
+      for (int i = 1; i <= metadata.getColumnCount(); i++) {
+        header.append(metadata.getColumnName(i)).append(",");
       }
       assertEquals(expectedHeader, header.toString());
-
-      Set<String> actualRetSet = new HashSet<>();
-
-      while (actualResultSet.next()) {
-        StringBuilder builder = new StringBuilder();
-        for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {
-          builder.append(actualResultSet.getString(i)).append(",");
+      List<String> actualRows = new ArrayList<>();
+      while (nodeResult.next()) {
+        StringBuilder row = new StringBuilder();
+        for (int i = 1; i <= metadata.getColumnCount(); i++) {
+          row.append(nodeResult.getString(i)).append(",");
         }
-        actualRetSet.add(builder.toString());
+        actualRows.add(row.toString());
       }
-      assertStringSetEqual(expectedRetSet, actualRetSet);
-    } catch (Exception e) {
-      e.printStackTrace();
-      Assert.fail(String.valueOf(e));
+      Collections.sort(actualRows);
+      assertEquals(sortedExpected, actualRows);
     }
   }
 
diff --git a/integration-test/src/test/java/org/apache/iotdb/pipe/it/triple/IoTDBShowReceiversTripleIT.java b/integration-test/src/test/java/org/apache/iotdb/pipe/it/triple/IoTDBShowReceiversTripleIT.java
index 0712387775b..7fec946aac7 100644
--- a/integration-test/src/test/java/org/apache/iotdb/pipe/it/triple/IoTDBShowReceiversTripleIT.java
+++ b/integration-test/src/test/java/org/apache/iotdb/pipe/it/triple/IoTDBShowReceiversTripleIT.java
@@ -44,6 +44,7 @@ import org.junit.runner.RunWith;
 
 import java.sql.Connection;
 import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.Arrays;
@@ -54,6 +55,8 @@ import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.TimeUnit;
 
+import static org.apache.iotdb.itbase.runtime.ClusterTestResultSet.getNodeResultSets;
+
 @RunWith(IoTDBTestRunner.class)
 @Category({MultiClusterIT3.class})
 public class IoTDBShowReceiversTripleIT {
@@ -214,39 +217,44 @@ public class IoTDBShowReceiversTripleIT {
       final String secondPipeName,
       final int secondReceiverDataNodeId)
       throws SQLException {
-    boolean hasFirstPipe = false;
-    boolean hasSecondPipe = false;
-    final Set<Integer> receiverDataNodeIds = new HashSet<>();
     try (final Connection connection =
             receiverEnv.getConnection(
                 SessionConfig.DEFAULT_USER, SessionConfig.DEFAULT_PASSWORD, sqlDialect);
         final Statement statement = connection.createStatement();
-        final ResultSet resultSet = statement.executeQuery(sql)) {
-      while (resultSet.next()) {
-        final String pipeIds = getString(resultSet, "PipeIDs", "pipe_ids");
-        if (!"DataNode".equals(getString(resultSet, "ReceiverNodeType", "receiver_node_type"))
-            || !"thrift".equals(getString(resultSet, "Protocol", "protocol"))
-            || pipeIds == null) {
-          continue;
-        }
-        final int receiverDataNodeId = getInt(resultSet, "ReceiverNodeId", "receiver_node_id");
-        if (receiverDataNodeId == firstReceiverDataNodeId
-            && pipeIds.contains(firstPipeName + "@")
-            && getInt(resultSet, "ConnectionCount", "connection_count") >= 1
-            && getInt(resultSet, "PipeCount", "pipe_count") >= 1) {
-          hasFirstPipe = true;
-          receiverDataNodeIds.add(receiverDataNodeId);
+        final ResultSet clusterResult = statement.executeQuery(sql)) {
+      for (ResultSet resultSet : getNodeResultSets(clusterResult)) {
+        boolean hasFirstPipe = false;
+        boolean hasSecondPipe = false;
+        final Set<Integer> receiverDataNodeIds = new HashSet<>();
+        while (resultSet.next()) {
+          final String pipeIds = getString(resultSet, "PipeIDs", "pipe_ids");
+          if (!"DataNode".equals(getString(resultSet, "ReceiverNodeType", "receiver_node_type"))
+              || !"thrift".equals(getString(resultSet, "Protocol", "protocol"))
+              || pipeIds == null) {
+            continue;
+          }
+          final int receiverDataNodeId = getInt(resultSet, "ReceiverNodeId", "receiver_node_id");
+          if (receiverDataNodeId == firstReceiverDataNodeId
+              && pipeIds.contains(firstPipeName + "@")
+              && getInt(resultSet, "ConnectionCount", "connection_count") >= 1
+              && getInt(resultSet, "PipeCount", "pipe_count") >= 1) {
+            hasFirstPipe = true;
+            receiverDataNodeIds.add(receiverDataNodeId);
+          }
+          if (receiverDataNodeId == secondReceiverDataNodeId
+              && pipeIds.contains(secondPipeName + "@")
+              && getInt(resultSet, "ConnectionCount", "connection_count") >= 1
+              && getInt(resultSet, "PipeCount", "pipe_count") >= 1) {
+            hasSecondPipe = true;
+            receiverDataNodeIds.add(receiverDataNodeId);
+          }
         }
-        if (receiverDataNodeId == secondReceiverDataNodeId
-            && pipeIds.contains(secondPipeName + "@")
-            && getInt(resultSet, "ConnectionCount", "connection_count") >= 1
-            && getInt(resultSet, "PipeCount", "pipe_count") >= 1) {
-          hasSecondPipe = true;
-          receiverDataNodeIds.add(receiverDataNodeId);
+        if (!hasFirstPipe || !hasSecondPipe || receiverDataNodeIds.size() < 2) {
+          return false;
         }
       }
     }
-    return hasFirstPipe && hasSecondPipe && receiverDataNodeIds.size() >= 2;
+    return true;
   }
 
   private int getDataNodeId(final DataNodeWrapper targetDataNode) throws Exception {
@@ -268,20 +276,27 @@ public class IoTDBShowReceiversTripleIT {
   private static String getString(
       final ResultSet resultSet, final String treeColumnName, final String tableColumnName)
       throws SQLException {
-    try {
-      return resultSet.getString(treeColumnName);
-    } catch (final SQLException ignored) {
-      return resultSet.getString(tableColumnName);
-    }
+    return resultSet.getString(getColumnIndex(resultSet, treeColumnName, tableColumnName));
   }
 
   private static int getInt(
       final ResultSet resultSet, final String treeColumnName, final String tableColumnName)
       throws SQLException {
-    try {
-      return resultSet.getInt(treeColumnName);
-    } catch (final SQLException ignored) {
-      return resultSet.getInt(tableColumnName);
+    return resultSet.getInt(getColumnIndex(resultSet, treeColumnName, tableColumnName));
+  }
+
+  private static int getColumnIndex(
+      final ResultSet resultSet, final String treeColumnName, final String tableColumnName)
+      throws SQLException {
+    // SHOW RECEIVERS and information_schema.receivers expose different column names.
+    final ResultSetMetaData metadata = resultSet.getMetaData();
+    for (int column = 1; column <= metadata.getColumnCount(); column++) {
+      final String name = metadata.getColumnName(column);
+      if (treeColumnName.equals(name) || tableColumnName.equals(name)) {
+        return column;
+      }
     }
+    throw new AssertionError(
+        "Missing receiver column: " + treeColumnName + " / " + tableColumnName);
   }
 }
diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBMultiDeviceTableIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBMultiDeviceTableIT.java
index 16d92c21343..096c1383c10 100644
--- a/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBMultiDeviceTableIT.java
+++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBMultiDeviceTableIT.java
@@ -37,6 +37,7 @@ import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 
+import static org.apache.iotdb.itbase.runtime.ClusterTestResultSet.getNodeResultSets;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
@@ -269,20 +270,22 @@ public class IoTDBMultiDeviceTableIT {
     try (Connection connection = EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
         Statement statement = connection.createStatement()) {
       statement.execute("use \"test\"");
-      Map<String, Long> lastTimeMap = new HashMap<>();
-      try (ResultSet resultSet = statement.executeQuery(selectSql)) {
-        int cnt = 0;
-        while (resultSet.next()) {
-          String tag = resultSet.getString("tag1") + "_" + resultSet.getString("tag2");
-          long before = lastTimeMap.getOrDefault(tag, -1L);
-          long cur = resultSet.getTimestamp("time").getTime();
-          if (cur <= before) {
-            fail("time order wrong!");
+      try (ResultSet clusterResult = statement.executeQuery(selectSql)) {
+        for (ResultSet resultSet : getNodeResultSets(clusterResult)) {
+          Map<String, Long> lastTimeMap = new HashMap<>();
+          int cnt = 0;
+          while (resultSet.next()) {
+            String tag = resultSet.getString("tag1") + "_" + resultSet.getString("tag2");
+            long before = lastTimeMap.getOrDefault(tag, -1L);
+            long cur = resultSet.getTimestamp("time").getTime();
+            if (cur <= before) {
+              fail("time order wrong!");
+            }
+            lastTimeMap.put(tag, cur);
+            cnt++;
           }
-          lastTimeMap.put(tag, cur);
-          cnt++;
+          assertEquals(16030, cnt);
         }
-        assertEquals(16030, cnt);
       }
     } catch (Exception e) {
       e.printStackTrace();
diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/udf/IoTDBUserDefinedTableFunctionIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/udf/IoTDBUserDefinedTableFunctionIT.java
index 9db3acb13e0..25967b8caeb 100644
--- a/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/udf/IoTDBUserDefinedTableFunctionIT.java
+++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/udf/IoTDBUserDefinedTableFunctionIT.java
@@ -41,6 +41,7 @@ import java.util.Set;
 
 import static org.apache.iotdb.db.it.utils.TestUtils.tableAssertTestFail;
 import static org.apache.iotdb.db.it.utils.TestUtils.tableResultSetEqualTest;
+import static org.apache.iotdb.itbase.runtime.ClusterTestResultSet.getNodeResultSets;
 import static org.junit.Assert.fail;
 
 @RunWith(IoTDBTestRunner.class)
@@ -200,37 +201,39 @@ public class IoTDBUserDefinedTableFunctionIT {
         "large_result",
         "org.apache.iotdb.db.query.udf.example.relational.LargeResultTableFunction");
 
-    Set<String> returnedRows = new HashSet<>();
     try (Connection connection = EnvFactory.getEnv().getTableConnection();
         Statement statement = connection.createStatement()) {
       statement.execute("USE " + DATABASE_NAME);
-      try (ResultSet resultSet =
+      try (ResultSet clusterResult =
           statement.executeQuery(
               "SELECT * FROM large_result(vehicle, "
                   + LARGE_RESULT_REPEAT_COUNT
                   + ", "
                   + LARGE_RESULT_PAYLOAD_SIZE
                   + ")")) {
-        while (resultSet.next()) {
-          int repeatIndex = resultSet.getInt("repeat_index");
-          long time = resultSet.getLong("time");
-          Assert.assertTrue(repeatIndex >= 0 && repeatIndex < LARGE_RESULT_REPEAT_COUNT);
-          Assert.assertEquals(
-              repeatIndex + ":" + "x".repeat(LARGE_RESULT_PAYLOAD_SIZE),
-              resultSet.getString("payload"));
-          assertPassThroughColumns(resultSet, time);
-          Assert.assertTrue(
-              "Duplicate result row for time " + time + " and repeat index " + repeatIndex,
-              returnedRows.add(time + ":" + repeatIndex));
-        }
-      }
-    }
+        for (ResultSet resultSet : getNodeResultSets(clusterResult)) {
+          Set<String> returnedRows = new HashSet<>();
+          while (resultSet.next()) {
+            int repeatIndex = resultSet.getInt("repeat_index");
+            long time = resultSet.getLong("time");
+            Assert.assertTrue(repeatIndex >= 0 && repeatIndex < LARGE_RESULT_REPEAT_COUNT);
+            Assert.assertEquals(
+                repeatIndex + ":" + "x".repeat(LARGE_RESULT_PAYLOAD_SIZE),
+                resultSet.getString("payload"));
+            assertPassThroughColumns(resultSet, time);
+            Assert.assertTrue(
+                "Duplicate result row for time " + time + " and repeat index " + repeatIndex,
+                returnedRows.add(time + ":" + repeatIndex));
+          }
 
-    long[] inputTimes = new long[] {1, 2, 3, 5};
-    Assert.assertEquals(inputTimes.length * LARGE_RESULT_REPEAT_COUNT, returnedRows.size());
-    for (long time : inputTimes) {
-      for (int repeatIndex = 0; repeatIndex < LARGE_RESULT_REPEAT_COUNT; repeatIndex++) {
-        Assert.assertTrue(returnedRows.contains(time + ":" + repeatIndex));
+          long[] inputTimes = new long[] {1, 2, 3, 5};
+          Assert.assertEquals(inputTimes.length * LARGE_RESULT_REPEAT_COUNT, returnedRows.size());
+          for (long time : inputTimes) {
+            for (int repeatIndex = 0; repeatIndex < LARGE_RESULT_REPEAT_COUNT; repeatIndex++) {
+              Assert.assertTrue(returnedRows.contains(time + ":" + repeatIndex));
+            }
+          }
+        }
       }
     }
   }
diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/insertquery/IoTDBInsertQueryIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/insertquery/IoTDBInsertQueryIT.java
index d6aa44a18f1..b0cccba6f6c 100644
--- a/integration-test/src/test/java/org/apache/iotdb/relational/it/insertquery/IoTDBInsertQueryIT.java
+++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/insertquery/IoTDBInsertQueryIT.java
@@ -49,6 +49,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
 
+import static org.apache.iotdb.itbase.runtime.ClusterTestResultSet.getNodeResultSets;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
@@ -269,10 +270,14 @@ public class IoTDBInsertQueryIT {
 
       // It is allowed to insert without tags
       statement.execute("INSERT INTO vehicle1(time, s1, s2) SELECT time, s1, s2 FROM vehicle0");
-      ResultSet resultSet = statement.executeQuery("SELECT deviceId, manufacturer FROM vehicle1");
-      while (resultSet.next()) {
-        for (int i = 1; i <= 2; i++) {
-          assertNull(resultSet.getString(i));
+      try (ResultSet clusterResult =
+          statement.executeQuery("SELECT deviceId, manufacturer FROM vehicle1")) {
+        for (ResultSet resultSet : getNodeResultSets(clusterResult)) {
+          while (resultSet.next()) {
+            for (int i = 1; i <= 2; i++) {
+              assertNull(resultSet.getString(i));
+            }
+          }
         }
       }
 
@@ -280,13 +285,17 @@ public class IoTDBInsertQueryIT {
       statement.execute("DELETE FROM vehicle1");
       statement.execute(
           "INSERT INTO vehicle1(time, s2, manufacturer) SELECT time, s2, manufacturer FROM vehicle0");
-      resultSet = statement.executeQuery("SELECT deviceId, manufacturer, s1, s2 FROM vehicle1");
-      while (resultSet.next()) {
-        for (int i = 1; i <= 4; i++) {
-          if (i % 2 == 1) {
-            assertNull(resultSet.getString(i));
-          } else {
-            assertNotNull(resultSet.getString(i));
+      try (ResultSet clusterResult =
+          statement.executeQuery("SELECT deviceId, manufacturer, s1, s2 FROM vehicle1")) {
+        for (ResultSet resultSet : getNodeResultSets(clusterResult)) {
+          while (resultSet.next()) {
+            for (int i = 1; i <= 4; i++) {
+              if (i % 2 == 1) {
+                assertNull(resultSet.getString(i));
+              } else {
+                assertNotNull(resultSet.getString(i));
+              }
+            }
           }
         }
       }
diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBClusterResultSetIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBClusterResultSetIT.java
new file mode 100644
index 00000000000..601d4ae1fe3
--- /dev/null
+++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBClusterResultSetIT.java
@@ -0,0 +1,163 @@
+/*
+ * 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.iotdb.relational.it.query.recent;
+
+import org.apache.iotdb.db.it.utils.TestUtils;
+import org.apache.iotdb.itbase.category.TableClusterIT;
+import org.apache.iotdb.itbase.category.TableLocalStandaloneIT;
+import org.apache.iotdb.itbase.runtime.ClusterTestResultSet;
+
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import javax.sql.rowset.CachedRowSet;
+import javax.sql.rowset.RowSetMetaDataImpl;
+import javax.sql.rowset.RowSetProvider;
+
+import java.lang.reflect.Proxy;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.sql.Types;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
+
+import static org.apache.iotdb.db.it.utils.TestUtils.assertResultSetEqualUnordered;
+import static org.apache.iotdb.itbase.runtime.ClusterTestResultSet.getNodeResultSets;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+
+/** Deterministic coverage of cross-node assertions without depending on network arrival order. */
+@Category({TableLocalStandaloneIT.class, TableClusterIT.class})
+public class IoTDBClusterResultSetIT {
+
+  @Test
+  public void testUnorderedRowsCheckEveryNode() throws SQLException {
+    try (ResultSet result = cluster(new String[] {"a", "b"}, new String[] {"b", "a"})) {
+      assertResultSetEqualUnordered(result, "value,", Arrays.asList("a,", "b,"));
+    }
+    try (ResultSet result = cluster(new String[] {"a", "b"}, new String[] {"b", "c"})) {
+      assertThrows(
+          AssertionError.class,
+          () -> assertResultSetEqualUnordered(result, "value,", Arrays.asList("a,", "b,")));
+    }
+  }
+
+  @Test
+  public void testUnorderedRowsPreserveDuplicateMultiplicity() throws SQLException {
+    try (ResultSet result = cluster(new String[] {"a", "a", "b"}, new String[] {"b", "a", "a"})) {
+      assertResultSetEqualUnordered(result, "value,", Arrays.asList("a,", "a,", "b,"));
+    }
+    for (String[] values : new String[][] {{"a", "b"}, {"a", "a", "a", "b"}}) {
+      try (ResultSet result = cluster(new String[] {"a", "a", "b"}, values)) {
+        assertThrows(
+            AssertionError.class,
+            () -> assertResultSetEqualUnordered(result, "value,", Arrays.asList("a,", "a,", "b,")));
+      }
+    }
+  }
+
+  @Test
+  public void testSetAssertionChecksEveryNode() throws SQLException {
+    try (ResultSet result = cluster(new String[] {"a", "b"}, new String[] {"b", "a"})) {
+      TestUtils.assertResultSetEqual(result, "value,", Set.of("a,", "b,"));
+    }
+    try (ResultSet result = cluster(new String[] {"a", "b"}, new String[] {"a"})) {
+      assertThrows(
+          AssertionError.class,
+          () -> TestUtils.assertResultSetEqual(result, "value,", Set.of("a,", "b,")));
+    }
+  }
+
+  @Test
+  public void testSetConsumerAssertionChecksEveryNode() throws SQLException {
+    try (ResultSet result = cluster(new String[] {"a", "b"}, new String[] {"b", "a"})) {
+      TestUtils.assertResultSetEqual(result, "value,", Set.of("a,", "b,"), message -> {});
+    }
+    try (ResultSet result = cluster(new String[] {"a", "b"}, new String[] {"a"})) {
+      assertThrows(
+          AssertionError.class,
+          () ->
+              TestUtils.assertResultSetEqual(result, "value,", Set.of("a,", "b,"), message -> {}));
+    }
+  }
+
+  @Test
+  public void testOrderedRowsStillRejectDifferentNodeOrder() throws SQLException {
+    try (ResultSet result = cluster(new String[] {"a", "b"}, new String[] {"b", "a"})) {
+      assertThrows(
+          AssertionError.class,
+          () -> TestUtils.assertResultSetEqual(result, "value,", Arrays.asList("a,", "b,")));
+    }
+  }
+
+  @Test
+  public void testSingleNodeAndNullValues() throws SQLException {
+    try (ResultSet result = rows("a", null, "a")) {
+      assertEquals(1, getNodeResultSets(result).size());
+      assertResultSetEqualUnordered(result, "value,", Arrays.asList("null,", "a,", "a,"));
+    }
+    try (ResultSet result = cluster(new String[] {"a"})) {
+      assertEquals(1, getNodeResultSets(result).size());
+      assertResultSetEqualUnordered(result, "value,", Arrays.asList("a,"));
+    }
+  }
+
+  private static ResultSet cluster(String[]... nodeRows) throws SQLException {
+    List<Statement> statements = new ArrayList<>();
+    List<String> endpoints = new ArrayList<>();
+    for (String[] values : nodeRows) {
+      ResultSet result = rows(values);
+      Statement statement =
+          (Statement)
+              Proxy.newProxyInstance(
+                  Statement.class.getClassLoader(),
+                  new Class<?>[] {Statement.class},
+                  (proxy, method, arguments) -> {
+                    if (method.getName().equals("executeQuery")) {
+                      return result;
+                    }
+                    throw new UnsupportedOperationException(method.getName());
+                  });
+      statements.add(statement);
+      endpoints.add("node-" + endpoints.size());
+    }
+    return new ClusterTestResultSet(statements, endpoints, "SELECT value", 10, null);
+  }
+
+  private static CachedRowSet rows(String... values) throws SQLException {
+    CachedRowSet result = RowSetProvider.newFactory().createCachedRowSet();
+    RowSetMetaDataImpl metadata = new RowSetMetaDataImpl();
+    metadata.setColumnCount(1);
+    metadata.setColumnName(1, "value");
+    metadata.setColumnType(1, Types.VARCHAR);
+    result.setMetaData(metadata);
+    for (String value : values) {
+      result.afterLast();
+      result.moveToInsertRow();
+      result.updateString(1, value);
+      result.insertRow();
+      result.moveToCurrentRow();
+    }
+    result.beforeFirst();
+    return result;
+  }
+}
diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBCollectFanInIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBCollectFanInIT.java
new file mode 100644
index 00000000000..745ec969d18
--- /dev/null
+++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBCollectFanInIT.java
@@ -0,0 +1,363 @@
+/*
+ * 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.iotdb.relational.it.query.recent;
+
+import org.apache.iotdb.it.env.EnvFactory;
+import org.apache.iotdb.it.framework.IoTDBTestRunner;
+import org.apache.iotdb.itbase.category.TableClusterIT;
+import org.apache.iotdb.itbase.category.TableLocalStandaloneIT;
+import org.apache.iotdb.pipe.it.dual.tablemodel.TableModelUtils;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import static org.apache.iotdb.db.it.utils.TestUtils.assertResultSetEqualUnordered;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(IoTDBTestRunner.class)
+@Category({TableLocalStandaloneIT.class, TableClusterIT.class})
+public class IoTDBCollectFanInIT {
+  private static final String DATABASE = "collect_fan_in";
+  private static final int DEVICES = 24;
+  private static final int ROWS = 8;
+
+  @BeforeClass
+  public static void setUp() throws Exception {
+    EnvFactory.getEnv()
+        .getConfig()
+        .getCommonConfig()
+        .setDegreeOfParallelism(1)
+        .setMaxTsBlockLineNumber(2)
+        .setSortBufferSize(128 * 1024)
+        .setDataRegionGroupExtensionPolicy("CUSTOM")
+        .setDataRegionPerDataNode(3);
+    EnvFactory.getEnv().initClusterEnvironment();
+    try (Connection connection = EnvFactory.getEnv().getTableConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute("CREATE DATABASE " + DATABASE);
+      statement.execute("USE " + DATABASE);
+      for (String table : new String[] {"readings1", "readings2"}) {
+        statement.execute(
+            "CREATE TABLE " + table + " (device STRING TAG, zone STRING TAG, value INT32 FIELD)");
+        for (int device = 0; device < DEVICES; device++) {
+          for (int value = 1; value <= ROWS; value++) {
+            statement.execute(
+                "INSERT INTO "
+                    + table
+                    + " (time,device,zone,value) VALUES ("
+                    + value
+                    + ",'"
+                    + device(device)
+                    + "',"
+                    + (zone(device) == null ? "NULL" : "'" + zone(device) + "'")
+                    + ","
+                    + value
+                    + ")");
+          }
+        }
+      }
+      statement.execute("FLUSH");
+    }
+  }
+
+  @Test
+  public void testUnorderedUnionPreservesEveryDuplicateOnEachNode() throws Exception {
+    List<String> expectedRows = new ArrayList<>();
+    for (int device = 0; device < DEVICES; device++) {
+      for (int value = 1; value <= ROWS; value++) {
+        String row = device(device) + "," + zone(device) + "," + value + ",";
+        expectedRows.add(row);
+        expectedRows.add(row);
+      }
+    }
+    try (Connection connection = EnvFactory.getEnv().getTableConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute("USE " + DATABASE);
+      String sql =
+          "SELECT device, zone, value FROM readings1 UNION ALL "
+              + "SELECT device, zone, value FROM readings1";
+      try (ResultSet result = statement.executeQuery(sql)) {
+        assertResultSetEqualUnordered(result, "device,zone,value,", expectedRows);
+      }
+    }
+  }
+
+  @Test
+  public void testPipeVerificationQueryRowsAreCompletePerNode() throws Exception {
+    TableModelUtils.createDataBaseAndTable(EnvFactory.getEnv(), "pipe_readings", DATABASE);
+    TableModelUtils.insertData(DATABASE, "pipe_readings", 0, DEVICES * ROWS, EnvFactory.getEnv());
+    List<String> expected =
+        new ArrayList<>(TableModelUtils.generateExpectedResults(0, DEVICES * ROWS));
+    Collections.sort(expected);
+    try (Connection connection = EnvFactory.getEnv().getTableConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute("USE " + DATABASE);
+      // The pipe verification query has no ORDER BY and fans in through a ready-first Collect,
+      // so each node's row set must be checked independently without relying on row order.
+      try (ResultSet result =
+          statement.executeQuery(TableModelUtils.getQuerySql("pipe_readings"))) {
+        assertResultSetEqualUnordered(result, TableModelUtils.generateHeaderResults(), expected);
+      }
+    }
+  }
+
+  @Test
+  public void testLegacyTreeAndSharedTreeToTablePaths() throws Exception {
+    try (Connection connection = EnvFactory.getEnv().getConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute("CREATE DATABASE root.collect_tree");
+      for (int device = 0; device < DEVICES; device++) {
+        String path = "root.collect_tree.z" + device % 3 + "." + device(device);
+        statement.execute("CREATE ALIGNED TIMESERIES " + path + "(value INT32)");
+        for (int value = 1; value <= ROWS; value++) {
+          statement.execute(
+              "INSERT INTO " + path + "(time,value) ALIGNED VALUES (" + value + "," + value + ")");
+        }
+      }
+      try (ResultSet result =
+          statement.executeQuery("SELECT value FROM root.collect_tree.*.* ALIGN BY DEVICE")) {
+        int rows = 0;
+        Set<String> seenDevices = new HashSet<>();
+        String currentDevice = null;
+        int deviceRows = 0;
+        while (result.next()) {
+          String nextDevice = result.getString("Device");
+          if (!nextDevice.equals(currentDevice)) {
+            if (currentDevice != null) {
+              assertEquals(ROWS, deviceRows);
+            }
+            assertTrue("Tree results must retain contiguous devices", seenDevices.add(nextDevice));
+            currentDevice = nextDevice;
+            deviceRows = 0;
+          }
+          assertEquals(++deviceRows, result.getLong("Time"));
+          assertEquals(result.getLong("Time"), result.getLong("value"));
+          rows++;
+        }
+        assertEquals(ROWS, deviceRows);
+        assertEquals(DEVICES, seenDevices.size());
+        assertEquals(DEVICES * ROWS, rows);
+      }
+    }
+    try (Connection connection = EnvFactory.getEnv().getTableConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute("USE " + DATABASE);
+      statement.execute(
+          "CREATE VIEW tree_readings(zone STRING TAG, device STRING TAG, value INT32 FIELD) AS root.collect_tree.**");
+      try (ResultSet result =
+          statement.executeQuery(
+              "SELECT device, SUM(value) FROM ("
+                  + "SELECT device, value FROM tree_readings UNION ALL SELECT device, value FROM tree_readings"
+                  + ") u GROUP BY device ORDER BY device")) {
+        for (int device = 0; device < DEVICES; device++) {
+          assertTrue(result.next());
+          assertEquals(device(device), result.getString(1));
+          assertEquals(72.0, result.getDouble(2), 0.0);
+        }
+        assertFalse(result.next());
+      }
+    }
+  }
+
+  @AfterClass
+  public static void tearDown() {
+    EnvFactory.getEnv().cleanClusterEnvironment();
+  }
+
+  private static String device(int id) {
+    return "d" + (id < 10 ? "0" : "") + id;
+  }
+
+  private static String zone(int id) {
+    return id % 3 == 0 ? null : "z" + id % 3;
+  }
+
+  @Test
+  public void testGroupedAggregationAfterGlobalWindow() throws Exception {
+    checkGroups(
+        "SELECT device, zone, value FROM (SELECT device, zone, value, "
+            + "COUNT(*) OVER () AS n FROM readings1) t WHERE n > 0",
+        1);
+  }
+
+  @Test
+  public void testUnionPartialFinalAndDistinctMask() throws Exception {
+    String input =
+        "SELECT device, zone, value FROM readings1 UNION ALL "
+            + "SELECT device, zone, value FROM readings2";
+    checkGroups(input, 2);
+    try (Connection connection = EnvFactory.getEnv().getTableConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute("USE " + DATABASE);
+      // Without DISTINCT, this query exercises branch PARTIAL and post-Collect FINAL aggregation.
+      try (ResultSet result =
+          statement.executeQuery(
+              "SELECT device, zone, SUM(value) FROM ("
+                  + input
+                  + ") u GROUP BY device, zone ORDER BY device")) {
+        for (int device = 0; device < DEVICES; device++) {
+          assertTrue(result.next());
+          assertEquals(device(device), result.getString(1));
+          assertEquals(zone(device), result.getString(2));
+          assertEquals(72.0, result.getDouble(3), 0.0);
+        }
+        assertFalse(result.next());
+      }
+    }
+  }
+
+  @Test
+  public void testOrderedFanInAndJoinDependencies() throws Exception {
+    try (Connection connection = EnvFactory.getEnv().getTableConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute("USE " + DATABASE);
+      for (String limit : new String[] {"", " LIMIT 13"}) {
+        try (ResultSet result =
+            statement.executeQuery(
+                "SELECT device, value FROM (SELECT device, value FROM readings1 UNION ALL "
+                    + "SELECT device, value FROM readings2) u ORDER BY device, value"
+                    + limit)) {
+          int expectedRows = limit.isEmpty() ? DEVICES * ROWS * 2 : 13;
+          for (int row = 0; row < expectedRows; row++) {
+            assertTrue(result.next());
+            assertEquals(device(row / (ROWS * 2)), result.getString(1));
+            assertEquals(row / 2 % ROWS + 1, result.getInt(2));
+          }
+          assertFalse(result.next());
+        }
+      }
+      try (ResultSet result =
+          statement.executeQuery(
+              "SELECT COUNT(*) FROM readings1 a JOIN readings2 b "
+                  + "ON a.device = b.device AND a.time = b.time WHERE a.value = b.value")) {
+        assertTrue(result.next());
+        assertEquals(DEVICES * ROWS, result.getLong(1));
+        assertFalse(result.next());
+      }
+    }
+  }
+
+  private void checkGroups(String input, int copies) throws Exception {
+    try (Connection connection = EnvFactory.getEnv().getTableConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute("USE " + DATABASE);
+      try (ResultSet result =
+          statement.executeQuery(
+              "SELECT device, zone, SUM(value), "
+                  + "COUNT(*), COUNT(DISTINCT value) FROM ("
+                  + input
+                  + ") u GROUP BY device, zone ORDER BY device")) {
+        for (int device = 0; device < DEVICES; device++) {
+          assertTrue(result.next());
+          assertEquals(device(device), result.getString(1));
+          assertEquals(zone(device), result.getString(2));
+          assertEquals(36.0 * copies, result.getDouble(3), 0.0);
+          assertEquals((long) ROWS * copies, result.getLong(4));
+          assertEquals(ROWS, result.getLong(5));
+        }
+        assertFalse(result.next());
+      }
+    }
+  }
+
+  @Test
+  public void testNestedWindowAfterCollectRestoresCompletePartitions() throws Exception {
+    try (Connection connection = EnvFactory.getEnv().getTableConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute("USE " + DATABASE);
+      try (ResultSet result =
+          statement.executeQuery(
+              "SELECT device, zone, value, "
+                  + "SUM(value) OVER (PARTITION BY device, zone ORDER BY value) AS running_sum FROM ("
+                  + "SELECT device, zone, value, COUNT(*) OVER () AS n FROM readings1) t "
+                  + "WHERE n > 0 ORDER BY device, value")) {
+        for (int device = 0; device < DEVICES; device++) {
+          for (int value = 1; value <= ROWS; value++) {
+            assertTrue(result.next());
+            assertEquals(device(device), result.getString(1));
+            assertEquals(zone(device), result.getString(2));
+            assertEquals(value, result.getInt(3));
+            assertEquals((double) value * (value + 1) / 2, result.getDouble(4), 0.0);
+          }
+        }
+        assertFalse(result.next());
+      }
+    }
+  }
+
+  @Test
+  public void testOrderByAfterGlobalWindowAndConstantFill() throws Exception {
+    try (Connection connection = EnvFactory.getEnv().getTableConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute("USE " + DATABASE);
+      try (ResultSet result =
+          statement.executeQuery(
+              "SELECT device, zone, value, COUNT(*) OVER () AS n "
+                  + "FROM readings1 FILL METHOD CONSTANT 0 ORDER BY device, value DESC")) {
+        for (int device = 0; device < DEVICES; device++) {
+          for (int value = ROWS; value > 0; value--) {
+            assertTrue(result.next());
+            assertEquals(device(device), result.getString(1));
+            assertEquals(value, result.getInt(3));
+            assertEquals((long) DEVICES * ROWS, result.getLong(4));
+          }
+        }
+        assertFalse(result.next());
+      }
+    }
+  }
+
+  @Test
+  public void testExcludedTopKPlanRetainsQueryResults() throws Exception {
+    try (Connection connection = EnvFactory.getEnv().getTableConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute("USE " + DATABASE);
+      int limit = DEVICES + 5;
+      try (ResultSet result =
+          statement.executeQuery(
+              "SELECT device, zone, value, COUNT(*) OVER () AS n FROM readings1 "
+                  + "ORDER BY value DESC, device LIMIT "
+                  + limit)) {
+        for (int row = 0; row < limit; row++) {
+          assertTrue(result.next());
+          assertEquals(device(row % DEVICES), result.getString(1));
+          assertEquals(zone(row % DEVICES), result.getString(2));
+          assertEquals(ROWS - row / DEVICES, result.getInt(3));
+          assertEquals((long) DEVICES * ROWS, result.getLong(4));
+        }
+        assertFalse(result.next());
+      }
+    }
+  }
+}
diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBCteIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBCteIT.java
index 65d3f277e52..e8089db62f1 100644
--- a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBCteIT.java
+++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBCteIT.java
@@ -60,6 +60,7 @@ import java.util.stream.Collectors;
 
 import static org.apache.iotdb.db.it.utils.TestUtils.tableAssertTestFail;
 import static org.apache.iotdb.db.it.utils.TestUtils.tableResultSetEqualTest;
+import static org.apache.iotdb.itbase.runtime.ClusterTestResultSet.getNodeResultSets;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
@@ -361,19 +362,22 @@ public class IoTDBCteIT {
       statement.execute("USE testdb");
 
       // explain
-      ResultSet resultSet = statement.executeQuery(sql);
-      ResultSetMetaData metaData = resultSet.getMetaData();
-      assertEquals(metaData.getColumnCount(), 1);
-      assertEquals(metaData.getColumnName(1), "distribution plan");
-
-      StringBuilder sb = new StringBuilder();
-      while (resultSet.next()) {
-        sb.append(resultSet.getString(1)).append(System.lineSeparator());
+      try (ResultSet clusterResult = statement.executeQuery(sql)) {
+        for (ResultSet resultSet : getNodeResultSets(clusterResult)) {
+          ResultSetMetaData metaData = resultSet.getMetaData();
+          assertEquals(metaData.getColumnCount(), 1);
+          assertEquals(metaData.getColumnName(1), "distribution plan");
+
+          StringBuilder sb = new StringBuilder();
+          while (resultSet.next()) {
+            sb.append(resultSet.getString(1)).append(System.lineSeparator());
+          }
+          String result = sb.toString();
+          assertFalse(result.contains("CTE Query : 'cte1'"));
+          assertTrue(result.contains("CTE Query : 'cte2'"));
+          assertTrue(result.contains("Main Query"));
+        }
       }
-      String result = sb.toString();
-      assertFalse(result.contains("CTE Query : 'cte1'"));
-      assertTrue(result.contains("CTE Query : 'cte2'"));
-      assertTrue(result.contains("Main Query"));
     }
   }
 
@@ -388,19 +392,22 @@ public class IoTDBCteIT {
       statement.execute("USE testdb");
 
       // explain
-      ResultSet resultSet = statement.executeQuery(sql);
-      ResultSetMetaData metaData = resultSet.getMetaData();
-      assertEquals(metaData.getColumnCount(), 1);
-      assertEquals(metaData.getColumnName(1), "distribution plan");
-
-      StringBuilder sb = new StringBuilder();
-      while (resultSet.next()) {
-        sb.append(resultSet.getString(1)).append(System.lineSeparator());
+      try (ResultSet clusterResult = statement.executeQuery(sql)) {
+        for (ResultSet resultSet : getNodeResultSets(clusterResult)) {
+          ResultSetMetaData metaData = resultSet.getMetaData();
+          assertEquals(metaData.getColumnCount(), 1);
+          assertEquals(metaData.getColumnName(1), "distribution plan");
+
+          StringBuilder sb = new StringBuilder();
+          while (resultSet.next()) {
+            sb.append(resultSet.getString(1)).append(System.lineSeparator());
+          }
+          String result = sb.toString();
+          assertTrue(result.contains("CTE Query : 'cte1'"));
+          assertTrue(result.contains("CTE Query : 'cte2'"));
+          assertTrue(result.contains("Main Query"));
+        }
       }
-      String result = sb.toString();
-      assertTrue(result.contains("CTE Query : 'cte1'"));
-      assertTrue(result.contains("CTE Query : 'cte2'"));
-      assertTrue(result.contains("Main Query"));
     }
   }
 
@@ -415,19 +422,22 @@ public class IoTDBCteIT {
       statement.execute("USE testdb");
 
       // explain
-      ResultSet resultSet = statement.executeQuery(sql);
-      ResultSetMetaData metaData = resultSet.getMetaData();
-      assertEquals(metaData.getColumnCount(), 1);
-      assertEquals(metaData.getColumnName(1), "distribution plan");
-
-      StringBuilder sb = new StringBuilder();
-      while (resultSet.next()) {
-        sb.append(resultSet.getString(1)).append(System.lineSeparator());
+      try (ResultSet clusterResult = statement.executeQuery(sql)) {
+        for (ResultSet resultSet : getNodeResultSets(clusterResult)) {
+          ResultSetMetaData metaData = resultSet.getMetaData();
+          assertEquals(metaData.getColumnCount(), 1);
+          assertEquals(metaData.getColumnName(1), "distribution plan");
+
+          StringBuilder sb = new StringBuilder();
+          while (resultSet.next()) {
+            sb.append(resultSet.getString(1)).append(System.lineSeparator());
+          }
+          String result = sb.toString();
+          assertFalse(result.contains("CTE Query : 'cte1'"));
+          assertTrue(result.contains("CTE Query : 'cte2'"));
+          assertTrue(result.contains("Main Query"));
+        }
       }
-      String result = sb.toString();
-      assertFalse(result.contains("CTE Query : 'cte1'"));
-      assertTrue(result.contains("CTE Query : 'cte2'"));
-      assertTrue(result.contains("Main Query"));
     }
   }
 
diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBTableAggregationIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBTableAggregationIT.java
index 518b3da1fdd..99188903b99 100644
--- a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBTableAggregationIT.java
+++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBTableAggregationIT.java
@@ -44,6 +44,7 @@ import java.util.regex.Pattern;
 import static org.apache.iotdb.db.it.utils.TestUtils.prepareTableData;
 import static org.apache.iotdb.db.it.utils.TestUtils.tableAssertTestFail;
 import static org.apache.iotdb.db.it.utils.TestUtils.tableResultSetEqualTest;
+import static org.apache.iotdb.itbase.runtime.ClusterTestResultSet.getNodeResultSets;
 import static org.apache.iotdb.relational.it.db.it.IoTDBMultiTAGsWithAttributesTableIT.buildHeaders;
 import static org.apache.iotdb.relational.it.db.it.IoTDBMultiTAGsWithAttributesTableIT.repeatTest;
 
@@ -5902,19 +5903,22 @@ public class IoTDBTableAggregationIT {
     try (Connection connection = EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
         Statement statement = connection.createStatement()) {
       statement.execute("USE " + DATABASE_NAME);
-      try (ResultSet resultSet = statement.executeQuery("EXPLAIN (FORMAT JSON) " + query)) {
-        Assert.assertTrue(resultSet.next());
-        String plan = resultSet.getString(1);
-        // The PARTIAL/FINAL split only exists when the scanned data spans multiple DataRegions.
-        // Under randomized partition allocation (e.g. the SHUFFLE strategy used by
-        // IoTDBTableAggregation2IT), all devices may occasionally land in one single region,
-        // where a SINGLE-step aggregation is the correct plan, so the check must be skipped.
-        if (countDistinctRegionIds(plan) < 2) {
-          return;
+      try (ResultSet clusterResult = statement.executeQuery("EXPLAIN (FORMAT JSON) " + query)) {
+        for (ResultSet resultSet : getNodeResultSets(clusterResult)) {
+          Assert.assertTrue(resultSet.next());
+          String plan = resultSet.getString(1);
+          // The PARTIAL/FINAL split only exists when the scanned data spans multiple DataRegions.
+          // Under randomized partition allocation (e.g. the SHUFFLE strategy used by
+          // IoTDBTableAggregation2IT), all devices may occasionally land in one single region,
+          // where a SINGLE-step aggregation is the correct plan, so the check must be skipped.
+          if (countDistinctRegionIds(plan) < 2) {
+            continue;
+          }
+          Assert.assertTrue(
+              "Expected a PARTIAL aggregation in plan: " + plan, plan.contains("PARTIAL"));
+          Assert.assertTrue(
+              "Expected a FINAL aggregation in plan: " + plan, plan.contains("FINAL"));
         }
-        Assert.assertTrue(
-            "Expected a PARTIAL aggregation in plan: " + plan, plan.contains("PARTIAL"));
-        Assert.assertTrue("Expected a FINAL aggregation in plan: " + plan, plan.contains("FINAL"));
       }
     } catch (Exception exception) {
       throw new AssertionError(exception);
diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBTopKRuntimeFilterScanPathIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBTopKRuntimeFilterScanPathIT.java
index 7697bc81649..59499aa5db0 100644
--- a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBTopKRuntimeFilterScanPathIT.java
+++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/IoTDBTopKRuntimeFilterScanPathIT.java
@@ -140,7 +140,8 @@ public class IoTDBTopKRuntimeFilterScanPathIT {
     tableResultSetEqualTest(
         "(SELECT time, device_id, s1 FROM table1 WHERE device_id = 'd1' ORDER BY time DESC LIMIT 1)"
             + " UNION ALL"
-            + " (SELECT time, device_id, s1 FROM table1 WHERE device_id = 'd3' ORDER BY time DESC LIMIT 1)",
+            + " (SELECT time, device_id, s1 FROM table1 WHERE device_id = 'd3' ORDER BY time DESC LIMIT 1)"
+            + " ORDER BY time, device_id, s1",
         expectedHeader,
         retArray,
         TABLE_DATABASE);
diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/subquery/uncorrelated/IoTDBUncorrelatedExistsSubqueryIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/subquery/uncorrelated/IoTDBUncorrelatedExistsSubqueryIT.java
index 1f424347e07..216f176ed47 100644
--- a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/subquery/uncorrelated/IoTDBUncorrelatedExistsSubqueryIT.java
+++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/subquery/uncorrelated/IoTDBUncorrelatedExistsSubqueryIT.java
@@ -30,11 +30,20 @@ import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.runner.RunWith;
 
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.apache.iotdb.db.it.utils.TestUtils.assertResultSetEqualUnordered;
 import static org.apache.iotdb.db.it.utils.TestUtils.prepareTableData;
 import static org.apache.iotdb.db.it.utils.TestUtils.tableResultSetEqualTest;
 import static org.apache.iotdb.relational.it.query.recent.subquery.SubqueryDataSetUtils.CREATE_SQLS;
 import static org.apache.iotdb.relational.it.query.recent.subquery.SubqueryDataSetUtils.DATABASE_NAME;
 import static org.apache.iotdb.relational.it.query.recent.subquery.SubqueryDataSetUtils.NUMERIC_MEASUREMENTS;
+import static org.junit.Assert.fail;
 
 @RunWith(IoTDBTestRunner.class)
 @Category({TableLocalStandaloneIT.class, TableClusterIT.class})
@@ -327,8 +336,10 @@ public class IoTDBUncorrelatedExistsSubqueryIT {
           "d01,5,", "d03,5,", "d05,5,", "d07,5,", "d09,5,", "d11,5,", "d13,5,", "d15,5,"
         };
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      tableResultSetEqualTest(
-          String.format(sql, measurement), expectedHeader, retArray, DATABASE_NAME);
+      assertUnorderedResult(
+          String.format(sql, measurement),
+          String.join(",", expectedHeader) + ",",
+          Arrays.asList(retArray));
     }
 
     sql =
@@ -349,8 +360,10 @@ public class IoTDBUncorrelatedExistsSubqueryIT {
           "d01,5,", "d03,5,", "d05,5,", "d07,5,", "d09,5,", "d11,5,", "d13,5,", "d15,5,"
         };
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      tableResultSetEqualTest(
-          String.format(sql, measurement), expectedHeader, retArray, DATABASE_NAME);
+      assertUnorderedResult(
+          String.format(sql, measurement),
+          String.join(",", expectedHeader) + ",",
+          Arrays.asList(retArray));
     }
 
     sql =
@@ -388,4 +401,18 @@ public class IoTDBUncorrelatedExistsSubqueryIT {
           String.format(sql, measurement, measurement), expectedHeader, retArray, DATABASE_NAME);
     }
   }
+
+  private static void assertUnorderedResult(
+      String sql, String expectedHeader, List<String> expectedRows) {
+    try (Connection connection = EnvFactory.getEnv().getTableConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute("USE " + DATABASE_NAME);
+      try (ResultSet resultSet = statement.executeQuery(sql)) {
+        assertResultSetEqualUnordered(resultSet, expectedHeader, expectedRows);
+      }
+    } catch (SQLException e) {
+      e.printStackTrace();
+      fail(e.getMessage());
+    }
+  }
 }
diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/subquery/uncorrelated/IoTDBUncorrelatedQuantifiedComparisonIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/subquery/uncorrelated/IoTDBUncorrelatedQuantifiedComparisonIT.java
index 11eb1feab90..7e0da3cedb3 100644
--- a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/subquery/uncorrelated/IoTDBUncorrelatedQuantifiedComparisonIT.java
+++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/subquery/uncorrelated/IoTDBUncorrelatedQuantifiedComparisonIT.java
@@ -30,12 +30,21 @@ import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.runner.RunWith;
 
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.apache.iotdb.db.it.utils.TestUtils.assertResultSetEqualUnordered;
 import static org.apache.iotdb.db.it.utils.TestUtils.prepareTableData;
 import static org.apache.iotdb.db.it.utils.TestUtils.tableAssertTestFail;
 import static org.apache.iotdb.db.it.utils.TestUtils.tableResultSetEqualTest;
 import static org.apache.iotdb.relational.it.query.recent.subquery.SubqueryDataSetUtils.CREATE_SQLS;
 import static org.apache.iotdb.relational.it.query.recent.subquery.SubqueryDataSetUtils.DATABASE_NAME;
 import static org.apache.iotdb.relational.it.query.recent.subquery.SubqueryDataSetUtils.NUMERIC_MEASUREMENTS;
+import static org.junit.Assert.fail;
 
 @RunWith(IoTDBTestRunner.class)
 @Category({TableLocalStandaloneIT.class, TableClusterIT.class})
@@ -57,6 +66,7 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
   @Test
   public void testAnyAndSomeComparisonInWhereClauseWithoutNull() {
     String sql;
+    String expectedHeaderStr;
     String[] expectedHeader;
     String[] retArray;
 
@@ -65,12 +75,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s > any (SELECT %s FROM table1 WHERE device_id = 'd01')";
     retArray = new String[] {"40,", "50,", "60,", "70,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeaderStr = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
-          expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          expectedHeaderStr,
+          Arrays.asList(retArray));
     }
 
     // Test case: where s > some (subquery), s does not contain null value
@@ -78,12 +87,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s > some (SELECT %s FROM table1 WHERE device_id = 'd01')";
     retArray = new String[] {"40,", "50,", "60,", "70,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeaderStr = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
-          expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          expectedHeaderStr,
+          Arrays.asList(retArray));
     }
 
     // Test case: where s >= any (subquery), s does not contain null value
@@ -91,12 +99,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s >= any (SELECT %s FROM table1 WHERE device_id = 'd01')";
     retArray = new String[] {"30,", "40,", "50,", "60,", "70,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeaderStr = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
-          expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          expectedHeaderStr,
+          Arrays.asList(retArray));
     }
 
     // Test case: where s >= some (subquery), s does not contain null value
@@ -104,12 +111,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s >= some (SELECT %s FROM table1 WHERE device_id = 'd01')";
     retArray = new String[] {"30,", "40,", "50,", "60,", "70,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeaderStr = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
-          expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          expectedHeaderStr,
+          Arrays.asList(retArray));
     }
 
     // Test case: where s < any (subquery), s does not contain null value
@@ -117,12 +123,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s < any (SELECT %s FROM table1 WHERE device_id = 'd01')";
     retArray = new String[] {"30,", "40,", "50,", "60,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeaderStr = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
-          expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          expectedHeaderStr,
+          Arrays.asList(retArray));
     }
 
     // Test case: where < some (subquery), s does not contain null value
@@ -130,12 +135,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s < some (SELECT %s FROM table1 WHERE device_id = 'd01')";
     retArray = new String[] {"30,", "40,", "50,", "60,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeaderStr = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
-          expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          expectedHeaderStr,
+          Arrays.asList(retArray));
     }
 
     // Test case: where s <= any (subquery), s does not contain null value
@@ -143,12 +147,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s <= any (SELECT %s FROM table1 WHERE device_id = 'd01')";
     retArray = new String[] {"30,", "40,", "50,", "60,", "70,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeaderStr = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
-          expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          expectedHeaderStr,
+          Arrays.asList(retArray));
     }
 
     // Test case: where s <= some (subquery), s does not contain null value
@@ -156,12 +159,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s <= some (SELECT %s FROM table1 WHERE device_id = 'd01')";
     retArray = new String[] {"30,", "40,", "50,", "60,", "70,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeaderStr = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
-          expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          expectedHeaderStr,
+          Arrays.asList(retArray));
     }
 
     // Test case: where s = any (subquery), s does not contain null value
@@ -195,12 +197,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s != any (SELECT %s FROM table1 WHERE device_id = 'd01')";
     retArray = new String[] {"30,", "40,", "50,", "60,", "70,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeaderStr = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
-          expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          expectedHeaderStr,
+          Arrays.asList(retArray));
     }
 
     // Test case: where s != some (subquery), s does not contain null value
@@ -208,18 +209,18 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s != some (SELECT %s FROM table1 WHERE device_id = 'd01')";
     retArray = new String[] {"30,", "40,", "50,", "60,", "70,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeaderStr = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
-          expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          expectedHeaderStr,
+          Arrays.asList(retArray));
     }
   }
 
   @Test
   public void testAllComparisonInWhereClauseWithoutNull() {
     String sql;
+    String expectedHeaderStr;
     String[] expectedHeader;
     String[] retArray;
 
@@ -228,12 +229,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s > all (SELECT %s FROM table3 WHERE device_id = 'd01')";
     retArray = new String[] {"50,", "60,", "70,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeaderStr = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
-          expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          expectedHeaderStr,
+          Arrays.asList(retArray));
     }
 
     // Test case: where s >= all (subquery), s does not contain null value
@@ -241,12 +241,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s >= all (SELECT %s FROM table3 WHERE device_id = 'd01')";
     retArray = new String[] {"40,", "50,", "60,", "70,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeaderStr = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
-          expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          expectedHeaderStr,
+          Arrays.asList(retArray));
     }
 
     // Test case: where s < all (subquery), s does not contain null value
@@ -254,12 +253,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s < all (SELECT %s FROM table3 WHERE device_id = 'd01')";
     retArray = new String[] {};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeaderStr = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
-          expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          expectedHeaderStr,
+          Arrays.asList(retArray));
     }
 
     // Test case: where s <= all (subquery), s does not contain null value
@@ -267,12 +265,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s <= all (SELECT %s FROM table3 WHERE device_id = 'd01')";
     retArray = new String[] {"30,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeaderStr = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
-          expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          expectedHeaderStr,
+          Arrays.asList(retArray));
     }
 
     // Test case: where s = all (subquery), s does not contain null value
@@ -280,12 +277,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s = all (SELECT %s FROM table3 WHERE device_id = 'd01')";
     retArray = new String[] {};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeaderStr = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
-          expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          expectedHeaderStr,
+          Arrays.asList(retArray));
     }
 
     // Test case: where s != all (subquery), s does not contain null value
@@ -305,7 +301,7 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
   @Test
   public void testAnyAndSomeComparisonInWhereClauseWithNull() {
     String sql;
-    String[] expectedHeader;
+    String expectedHeader;
     String[] retArray;
 
     // Test case: where s1 > any (subquery), s1 in table3 contains null value
@@ -313,12 +309,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s > any (SELECT s1 FROM table3)";
     retArray = new String[] {"40,", "50,", "60,", "70,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeader = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
           expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          Arrays.asList(retArray));
     }
 
     // Test case: where s1 > some (subquery), s1 in table3 contains null value
@@ -326,12 +321,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s > some (SELECT s1 FROM table3)";
     retArray = new String[] {"40,", "50,", "60,", "70,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeader = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
           expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          Arrays.asList(retArray));
     }
 
     // Test case: where s1 >= any (subquery), s1 in table3 contains null value
@@ -339,12 +333,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s >= any (SELECT s1 FROM table3)";
     retArray = new String[] {"30,", "40,", "50,", "60,", "70,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeader = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
           expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          Arrays.asList(retArray));
     }
 
     // Test case: where s1 >= some (subquery), s1 in table3 contains null value
@@ -352,12 +345,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s >= some (SELECT s1 FROM table3)";
     retArray = new String[] {"30,", "40,", "50,", "60,", "70,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeader = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
           expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          Arrays.asList(retArray));
     }
 
     // Test case: where s1 < any (subquery), s1 in table3 contains null value
@@ -365,12 +357,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s < any (SELECT s1 FROM table3)";
     retArray = new String[] {"30,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeader = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
           expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          Arrays.asList(retArray));
     }
 
     // Test case: where s1 < some (subquery), s1 in table3 contains null value
@@ -378,18 +369,18 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s < some (SELECT s1 FROM table3)";
     retArray = new String[] {"30,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeader = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
           expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          Arrays.asList(retArray));
     }
   }
 
   @Test
   public void testAllComparisonInWhereClauseWithNull() {
     String sql;
+    String expectedHeaderStr;
     String[] expectedHeader;
     String[] retArray;
 
@@ -398,12 +389,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s > all (SELECT s1 FROM table3)";
     retArray = new String[] {};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeaderStr = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
-          expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          expectedHeaderStr,
+          Arrays.asList(retArray));
     }
 
     // Test case: where s1 >= all (subquery), s1 in table3 contains null value.
@@ -411,12 +401,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s >= all (SELECT s1 FROM table3)";
     retArray = new String[] {};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeaderStr = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
-          expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          expectedHeaderStr,
+          Arrays.asList(retArray));
     }
 
     // Test case: where s1 < all (subquery), s1 in table3 contains null value.
@@ -424,12 +413,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s < all (SELECT s1 FROM table3)";
     retArray = new String[] {};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeaderStr = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
-          expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          expectedHeaderStr,
+          Arrays.asList(retArray));
     }
 
     // Test case: where s1 <= all (subquery), s1 in table3 contains null value.
@@ -437,12 +425,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s <= all (SELECT s1 FROM table3)";
     retArray = new String[] {};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeaderStr = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
-          expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          expectedHeaderStr,
+          Arrays.asList(retArray));
     }
 
     // Test case: where s1 = all (subquery), s1 in table3 contains null value.
@@ -450,12 +437,11 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "SELECT cast(%s AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s = all (SELECT s1 FROM table3)";
     retArray = new String[] {};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeaderStr = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
-          expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          expectedHeaderStr,
+          Arrays.asList(retArray));
     }
 
     // Test case: where s1 != all (subquery), s1 in table3 contains null value.
@@ -475,98 +461,92 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
   @Test
   public void testQuantifiedComparisonInWhereWithExpression() {
     String sql;
-    String[] expectedHeader;
+    String expectedHeader;
     String[] retArray;
 
     sql =
         "SELECT cast(%s + 10 AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s + 10 > any (SELECT %s + 10 FROM table1 WHERE device_id = 'd01')";
     retArray = new String[] {"50,", "60,", "70,", "80,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeader = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
           expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          Arrays.asList(retArray));
     }
 
     sql =
         "SELECT cast(%s + 10 AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s + 10 > some (SELECT %s + 10 FROM table1 WHERE device_id = 'd01')";
     retArray = new String[] {"50,", "60,", "70,", "80,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeader = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
           expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          Arrays.asList(retArray));
     }
 
     sql =
         "SELECT cast(%s + 10 AS INT32) as %s FROM table1 WHERE device_id = 'd01' and %s + 10 >= all (SELECT %s + 10 FROM table1 WHERE device_id = 'd01')";
     retArray = new String[] {"80,"};
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      expectedHeader = new String[] {measurement};
-      tableResultSetEqualTest(
+      expectedHeader = measurement + ",";
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
           expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          Arrays.asList(retArray));
     }
   }
 
   @Test
   public void testQuantifiedComparisonInHavingClause() {
     String sql;
-    String[] expectedHeader;
+    String expectedHeader;
     String[] retArray;
 
     // Test case: having s >= any(subquery)
     sql =
         "SELECT device_id, count(*) from table1 group by device_id having count(*) + 25 >= any(SELECT cast(s1 as INT64) from table3 where device_id = 'd01')";
-    expectedHeader = new String[] {"device_id", "_col1"};
+    expectedHeader = "device_id,_col1,";
     retArray =
         new String[] {
           "d01,5,", "d03,5,", "d05,5,", "d07,5,", "d09,5,", "d11,5,", "d13,5,", "d15,5,"
         };
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      tableResultSetEqualTest(
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
           expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          Arrays.asList(retArray));
     }
 
     // Test case: having s >= some(subquery)
     sql =
         "SELECT device_id, count(*) from table1 group by device_id having count(*) + 25 >= some(SELECT cast(s1 as INT64) from table3 where device_id = 'd01')";
-    expectedHeader = new String[] {"device_id", "_col1"};
+    expectedHeader = "device_id,_col1,";
     retArray =
         new String[] {
           "d01,5,", "d03,5,", "d05,5,", "d07,5,", "d09,5,", "d11,5,", "d13,5,", "d15,5,"
         };
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      tableResultSetEqualTest(
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
           expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          Arrays.asList(retArray));
     }
 
     // Test case: having s >= all(subquery)
     sql =
         "SELECT device_id, count(*) from table1 group by device_id having count(*) + 35 >= all(SELECT cast(s1 as INT64) from table3 where device_id = 'd01')";
-    expectedHeader = new String[] {"device_id", "_col1"};
+    expectedHeader = "device_id,_col1,";
     retArray =
         new String[] {
           "d01,5,", "d03,5,", "d05,5,", "d07,5,", "d09,5,", "d11,5,", "d13,5,", "d15,5,"
         };
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      tableResultSetEqualTest(
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
           expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          Arrays.asList(retArray));
     }
   }
 
@@ -671,4 +651,18 @@ public class IoTDBUncorrelatedQuantifiedComparisonIT {
         "mismatched input",
         DATABASE_NAME);
   }
+
+  private static void assertUnorderedResult(
+      String sql, String expectedHeader, List<String> expectedRows) {
+    try (Connection connection = EnvFactory.getEnv().getTableConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute("USE " + DATABASE_NAME);
+      try (ResultSet resultSet = statement.executeQuery(sql)) {
+        assertResultSetEqualUnordered(resultSet, expectedHeader, expectedRows);
+      }
+    } catch (SQLException e) {
+      e.printStackTrace();
+      fail(e.getMessage());
+    }
+  }
 }
diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/subquery/uncorrelated/IoTDBUncorrelatedScalarSubqueryIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/subquery/uncorrelated/IoTDBUncorrelatedScalarSubqueryIT.java
index f6a2cdff48b..4068e53a75d 100644
--- a/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/subquery/uncorrelated/IoTDBUncorrelatedScalarSubqueryIT.java
+++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/query/recent/subquery/uncorrelated/IoTDBUncorrelatedScalarSubqueryIT.java
@@ -30,12 +30,21 @@ import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.runner.RunWith;
 
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.apache.iotdb.db.it.utils.TestUtils.assertResultSetEqualUnordered;
 import static org.apache.iotdb.db.it.utils.TestUtils.prepareTableData;
 import static org.apache.iotdb.db.it.utils.TestUtils.tableAssertTestFail;
 import static org.apache.iotdb.db.it.utils.TestUtils.tableResultSetEqualTest;
 import static org.apache.iotdb.relational.it.query.recent.subquery.SubqueryDataSetUtils.CREATE_SQLS;
 import static org.apache.iotdb.relational.it.query.recent.subquery.SubqueryDataSetUtils.DATABASE_NAME;
 import static org.apache.iotdb.relational.it.query.recent.subquery.SubqueryDataSetUtils.NUMERIC_MEASUREMENTS;
+import static org.junit.Assert.fail;
 
 @RunWith(IoTDBTestRunner.class)
 @Category({TableLocalStandaloneIT.class, TableClusterIT.class})
@@ -366,24 +375,38 @@ public class IoTDBUncorrelatedScalarSubqueryIT {
   @Test
   public void testScalarSubqueryInHavingClause() {
     String sql;
-    String[] expectedHeader;
+    String expectedHeader;
     String[] retArray;
 
     // Test case: scalar subquery in having clause
     sql =
         "SELECT device_id, count(*) from table1 group by device_id having count(*) > 3 + (SELECT count(*) from table2 where device_id = 'd01')";
-    expectedHeader = new String[] {"device_id", "_col1"};
+    expectedHeader = "device_id,_col1,";
     retArray =
         new String[] {
           "d01,5,", "d03,5,", "d05,5,", "d07,5,", "d09,5,", "d11,5,", "d13,5,", "d15,5,"
         };
     for (String measurement : NUMERIC_MEASUREMENTS) {
-      tableResultSetEqualTest(
+      assertUnorderedResult(
           String.format(sql, measurement, measurement, measurement, measurement),
           expectedHeader,
-          retArray,
-          DATABASE_NAME);
+          Arrays.asList(retArray));
     }
   }
+
   // endregion
+
+  private static void assertUnorderedResult(
+      String sql, String expectedHeader, List<String> expectedRows) {
+    try (Connection connection = EnvFactory.getEnv().getTableConnection();
+        Statement statement = connection.createStatement()) {
+      statement.execute("USE " + DATABASE_NAME);
+      try (ResultSet resultSet = statement.executeQuery(sql)) {
+        assertResultSetEqualUnordered(resultSet, expectedHeader, expectedRows);
+      }
+    } catch (SQLException e) {
+      e.printStackTrace();
+      fail(e.getMessage());
+    }
+  }
 }
diff --git a/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/operator/process/AnyChildBlocked.java b/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/operator/process/AnyChildBlocked.java
new file mode 100644
index 00000000000..72058f3cfd3
--- /dev/null
+++ b/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/operator/process/AnyChildBlocked.java
@@ -0,0 +1,274 @@
+/*
+ * 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.iotdb.calc.execution.operator.process;
+
+import org.apache.iotdb.calc.execution.operator.Operator;
+
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.SettableFuture;
+import org.apache.tsfile.utils.Accountable;
+import org.apache.tsfile.utils.RamUsageEstimator;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.ExecutionException;
+
+import static com.google.common.util.concurrent.Futures.getDone;
+import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
+
+/**
+ * Observes direct children without calling operators from completion callbacks. The driver owns
+ * selection/invalidation; the lock protects listener records shared with asynchronous completions.
+ */
+final class AnyChildBlocked implements Accountable {
+  private static final long INSTANCE_SIZE =
+      RamUsageEstimator.shallowSizeOfInstance(AnyChildBlocked.class);
+  private static final long LISTENER_SIZE =
+      RamUsageEstimator.shallowSizeOfInstance(ChildFutureListener.class);
+  private static final long ROUND_SIZE =
+      RamUsageEstimator.shallowSizeOfInstance(WaitRound.class)
+          + RamUsageEstimator.shallowSizeOfInstance(SettableFuture.class);
+
+  private final List<Operator> children;
+  private final Object lock = new Object();
+  private final ChildFutureListener[] listeners;
+  private ChildFutureListener readyHead;
+  private ChildFutureListener readyTail;
+  private WaitRound currentRound;
+  private Throwable firstFailure;
+  private boolean closed;
+  private boolean initialized;
+  private int childToObserve = -1;
+
+  AnyChildBlocked(List<Operator> children) {
+    this.children = children;
+    this.listeners = new ChildFutureListener[children.size()];
+  }
+
+  ListenableFuture<Integer> getAnyChild() {
+    WaitRound round;
+    WaitRound selection;
+    boolean initialize;
+    int invalidatedChild;
+    synchronized (lock) {
+      if (currentRound != null) {
+        return currentRound.selected;
+      }
+      round = new WaitRound();
+      currentRound = round;
+      initialize = !initialized;
+      initialized = true;
+      invalidatedChild = childToObserve;
+      childToObserve = -1;
+      selection = selectReadyChild();
+    }
+    completeSelection(selection);
+
+    if (initialize) {
+      // Activate every direct input once, even if an earlier input is already ready. Pending
+      // listeners enqueue their completion without being retargeted or polled on every block.
+      for (int childId = 0; childId < children.size(); childId++) {
+        observeChild(childId);
+      }
+    } else if (invalidatedChild >= 0) {
+      // Only advancing an input invalidates its readiness. Requeue it behind inputs that are
+      // already ready so that a continuously ready input cannot starve its siblings.
+      observeChild(invalidatedChild);
+    }
+    return round.selected;
+  }
+
+  private void observeChild(int childId) {
+    Operator child;
+    synchronized (lock) {
+      if (closed) {
+        return;
+      }
+      child = children.get(childId);
+    }
+    if (child == null) {
+      return;
+    }
+    ListenableFuture<?> observedFuture = child.isBlocked();
+    ChildFutureListener listener = new ChildFutureListener(childId, observedFuture);
+    synchronized (lock) {
+      if (closed) {
+        return;
+      }
+      listeners[childId] = listener;
+    }
+    // Publish the record before registration, including when completion runs inline. Callbacks
+    // only update readiness; operator methods remain confined to the driver thread.
+    observedFuture.addListener(listener, directExecutor());
+  }
+
+  /** Called under lock; claiming a selection and removing its ready record are atomic. */
+  private WaitRound selectReadyChild() {
+    if (currentRound == null || currentRound.claimed) {
+      return null;
+    }
+    if (closed || children.isEmpty()) {
+      currentRound.childId = -1;
+    } else if (firstFailure != null) {
+      currentRound.failure = firstFailure;
+    } else {
+      ChildFutureListener listener;
+      do {
+        listener = readyHead;
+        if (listener == null) {
+          return null;
+        }
+        readyHead = listener.nextReady;
+        listener.nextReady = null;
+        if (readyHead == null) {
+          readyTail = null;
+        }
+        // Retired records are discarded once, rather than scanning closed input slots per block.
+      } while (listeners[listener.childId] != listener);
+      currentRound.childId = listener.childId;
+    }
+    currentRound.claimed = true;
+    return currentRound;
+  }
+
+  private static void completeSelection(WaitRound selection) {
+    if (selection == null) {
+      return;
+    }
+    // Completing the future can invoke downstream listeners synchronously. Do not hold lock.
+    if (selection.failure == null) {
+      selection.selected.set(selection.childId);
+    } else {
+      selection.selected.setException(selection.failure);
+    }
+  }
+
+  void checkFailure() throws Exception {
+    Throwable failure;
+    synchronized (lock) {
+      failure = firstFailure;
+    }
+    if (failure instanceof Exception) {
+      throw (Exception) failure;
+    }
+    if (failure instanceof Error) {
+      throw (Error) failure;
+    }
+    if (failure != null) {
+      throw new RuntimeException(failure);
+    }
+  }
+
+  void invalidateChild(int childId) {
+    synchronized (lock) {
+      listeners[childId] = null;
+      currentRound = null;
+      childToObserve = childId;
+    }
+  }
+
+  void close() {
+    WaitRound round;
+    synchronized (lock) {
+      closed = true;
+      round = currentRound;
+      currentRound = null;
+      childToObserve = -1;
+      while (readyHead != null) {
+        ChildFutureListener next = readyHead.nextReady;
+        readyHead.nextReady = null;
+        readyHead = next;
+      }
+      readyTail = null;
+      Arrays.fill(listeners, null);
+    }
+    if (round != null) {
+      round.selected.set(-1);
+    }
+  }
+
+  @Override
+  public long ramBytesUsed() {
+    synchronized (lock) {
+      long size =
+          INSTANCE_SIZE
+              + RamUsageEstimator.shallowSizeOfInstance(Object.class)
+              + RamUsageEstimator.shallowSizeOf(listeners);
+      for (ChildFutureListener listener : listeners) {
+        if (listener != null) {
+          size += LISTENER_SIZE;
+        }
+      }
+      return size + (currentRound == null ? 0 : ROUND_SIZE);
+    }
+  }
+
+  private static final class WaitRound {
+    private final SettableFuture<Integer> selected = SettableFuture.create();
+    private boolean claimed;
+    private int childId;
+    private Throwable failure;
+  }
+
+  private final class ChildFutureListener implements Runnable {
+    private final int childId;
+    private final ListenableFuture<?> observedFuture;
+    private ChildFutureListener nextReady;
+    private boolean completed;
+
+    private ChildFutureListener(int childId, ListenableFuture<?> observedFuture) {
+      this.childId = childId;
+      this.observedFuture = observedFuture;
+    }
+
+    @Override
+    public void run() {
+      Throwable failure = null;
+      try {
+        getDone(observedFuture);
+      } catch (ExecutionException e) {
+        failure = e.getCause();
+      } catch (CancellationException e) {
+        failure = e;
+      }
+      WaitRound selection;
+      synchronized (lock) {
+        if (closed || listeners[childId] != this || completed) {
+          return;
+        }
+        completed = true;
+        if (failure != null && firstFailure == null) {
+          firstFailure = failure;
+        }
+        if (failure == null) {
+          if (readyTail == null) {
+            readyHead = this;
+          } else {
+            readyTail.nextReady = this;
+          }
+          readyTail = this;
+        }
+        selection = selectReadyChild();
+      }
+      completeSelection(selection);
+    }
+  }
+}
diff --git a/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/operator/process/CollectOperator.java b/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/operator/process/CollectOperator.java
index 921d273d04e..d0d9b396d76 100644
--- a/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/operator/process/CollectOperator.java
+++ b/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/operator/process/CollectOperator.java
@@ -30,60 +30,104 @@ import org.apache.tsfile.utils.RamUsageEstimator;
 
 import java.util.List;
 
+import static com.google.common.util.concurrent.Futures.getDone;
+import static com.google.common.util.concurrent.Futures.nonCancellationPropagating;
+
+/**
+ * Consumes whichever direct child is ready first. Output rows are ordered per child but interleaved
+ * across children, so the plan must not rely on this operator for any cross-child ordering.
+ */
 public class CollectOperator implements ProcessOperator {
   private static final long INSTANCE_SIZE =
       RamUsageEstimator.shallowSizeOfInstance(CollectOperator.class);
 
   private final CommonOperatorContext operatorContext;
   protected final List<Operator> children;
-  private boolean inited = false;
-
-  protected int currentIndex;
+  private final AnyChildBlocked anyChildBlocked;
+  private ListenableFuture<Integer> selectionFuture;
+  private ListenableFuture<Integer> exposedFuture;
+  private int remainingChildren;
+  private boolean closed;
 
   public CollectOperator(CommonOperatorContext operatorContext, List<Operator> children) {
     this.operatorContext = operatorContext;
     this.children = children;
-    this.currentIndex = 0;
+    this.anyChildBlocked = new AnyChildBlocked(children);
+    this.remainingChildren = children.size();
   }
 
   @Override
-  public boolean hasNext() throws Exception {
-    return currentIndex < children.size();
+  public boolean hasNext() {
+    return !closed && remainingChildren > 0;
   }
 
   @Override
   public TsBlock next() throws Exception {
-    if (children.get(currentIndex).hasNextWithTimer()) {
-      return children.get(currentIndex).nextWithTimer();
-    } else {
-      closeCurrentChild(currentIndex);
-      currentIndex++;
+    anyChildBlocked.checkFailure();
+    if (!hasNext()) {
       return null;
     }
+    if (selectionFuture == null) {
+      isBlocked();
+    }
+    anyChildBlocked.checkFailure();
+    if (!selectionFuture.isDone()) {
+      return null;
+    }
+    int childId = getDone(selectionFuture);
+    // Consume this selection before invoking any operator method. Another next must acquire a
+    // new readiness observation even when this advancement returns null or an empty block.
+    selectionFuture = null;
+    exposedFuture = null;
+    if (childId < 0 || closed || children.get(childId) == null) {
+      return null;
+    }
+    Operator child = children.get(childId);
+    try {
+      if (child.hasNextWithTimer()) {
+        TsBlock block = child.nextWithTimer();
+        return block == null ? null : transformBlock(childId, block);
+      }
+      if (child.isFinished()) {
+        closeCurrentChild(childId);
+      }
+      return null;
+    } finally {
+      anyChildBlocked.invalidateChild(childId);
+    }
+  }
+
+  protected TsBlock transformBlock(int childId, TsBlock block) {
+    return block;
   }
 
   protected void closeCurrentChild(int index) throws Exception {
-    children.get(index).close();
+    Operator child = children.get(index);
+    if (child == null) {
+      return;
+    }
     children.set(index, null);
+    remainingChildren--;
+    child.close();
   }
 
   @Override
   public ListenableFuture<?> isBlocked() {
-    if (!inited) {
-      inited = true;
-      for (Operator child : children) {
-        child.isBlocked();
-      }
-    }
-    if (currentIndex >= children.size()) {
+    if (!hasNext()) {
       return NOT_BLOCKED;
     }
-    return children.get(currentIndex).isBlocked();
+    if (selectionFuture == null) {
+      selectionFuture = anyChildBlocked.getAnyChild();
+    }
+    if (exposedFuture == null || exposedFuture.isCancelled()) {
+      exposedFuture = nonCancellationPropagating(selectionFuture);
+    }
+    return exposedFuture;
   }
 
   @Override
-  public boolean isFinished() throws Exception {
-    return currentIndex >= children.size();
+  public boolean isFinished() {
+    return !hasNext();
   }
 
   @Override
@@ -93,35 +137,75 @@ public class CollectOperator implements ProcessOperator {
 
   @Override
   public void close() throws Exception {
-    for (int i = currentIndex, n = children.size(); i < n; i++) {
-      Operator currentChild = children.get(i);
-      if (currentChild != null) {
+    if (closed) {
+      return;
+    }
+    closed = true;
+    anyChildBlocked.close();
+    selectionFuture = null;
+    exposedFuture = null;
+    Exception firstException = null;
+    for (int i = 0; i < children.size(); i++) {
+      try {
         closeCurrentChild(i);
+      } catch (Exception e) {
+        if (firstException == null) {
+          firstException = e;
+        } else if (firstException != e) {
+          firstException.addSuppressed(e);
+        }
       }
     }
+    if (firstException != null) {
+      throw firstException;
+    }
   }
 
+  /**
+   * Every live child holds its retained state while one of them is advanced from its retained size
+   * to its peak, so the instant peak is {@code retained + max(child.peak - child.retained)}.
+   *
+   * <p>In executed distributed plans every child is an ExchangeOperator whose retained size is
+   * zero, so this degenerates to {@code max(child.peak)} and no parent peak formula counts any
+   * child state twice.
+   */
   @Override
   public long calculateMaxPeekMemory() {
-    long maxPeekMemory = 0;
+    long retained = calculateRetainedSizeAfterCallingNext();
+    long peak = retained;
     for (Operator child : children) {
-      maxPeekMemory = Math.max(maxPeekMemory, child.calculateMaxPeekMemoryWithCounter());
+      if (child != null) {
+        peak =
+            Math.max(
+                peak,
+                retained
+                    - child.calculateRetainedSizeAfterCallingNext()
+                    + child.calculateMaxPeekMemoryWithCounter());
+      }
     }
-    return maxPeekMemory;
+    return peak;
   }
 
   @Override
   public long calculateMaxReturnSize() {
     long maxReturnSize = 0;
     for (Operator child : children) {
-      maxReturnSize = Math.max(maxReturnSize, child.calculateMaxReturnSize());
+      if (child != null) {
+        maxReturnSize = Math.max(maxReturnSize, child.calculateMaxReturnSize());
+      }
     }
     return maxReturnSize;
   }
 
   @Override
   public long calculateRetainedSizeAfterCallingNext() {
-    return 0L;
+    long retained = 0;
+    for (Operator child : children) {
+      if (child != null) {
+        retained += child.calculateRetainedSizeAfterCallingNext();
+      }
+    }
+    return retained;
   }
 
   @TestOnly
@@ -132,6 +216,7 @@ public class CollectOperator implements ProcessOperator {
   @Override
   public long ramBytesUsed() {
     return INSTANCE_SIZE
+        + anyChildBlocked.ramBytesUsed()
         + children.stream()
             .mapToLong(MemoryEstimationHelper::getEstimatedSizeOfAccountableObject)
             .sum()
diff --git a/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/operator/process/MappingCollectOperator.java b/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/operator/process/MappingCollectOperator.java
index 8bc1c18e9e6..02850bf0caf 100644
--- a/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/operator/process/MappingCollectOperator.java
+++ b/iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/execution/operator/process/MappingCollectOperator.java
@@ -45,37 +45,43 @@ public class MappingCollectOperator extends CollectOperator {
       List<List<Integer>> mappings) {
     super(operatorContext, children);
     this.mappings = mappings;
-    outputColumnsCount = mappings.get(0).size();
+    outputColumnsCount = mappings.isEmpty() ? 0 : mappings.get(0).size();
   }
 
   @Override
-  public TsBlock next() throws Exception {
-    if (children.get(currentIndex).hasNextWithTimer()) {
-      TsBlock tsBlock = children.get(currentIndex).nextWithTimer();
-      if (tsBlock == null) {
-        return null;
-      } else {
-        Column[] columns = new Column[outputColumnsCount];
-        List<Integer> mapping = mappings.get(currentIndex);
-        for (int i = 0; i < columns.length; i++) {
-          columns[i] = tsBlock.getColumn(mapping.get(i));
-        }
-        return TsBlock.wrapBlocksWithoutCopy(
-            tsBlock.getPositionCount(),
-            new RunLengthEncodedColumn(
-                CommonOperatorUtils.TIME_COLUMN_TEMPLATE, tsBlock.getPositionCount()),
-            columns);
-      }
-    } else {
-      closeCurrentChild(currentIndex);
-      currentIndex++;
-      return null;
+  protected TsBlock transformBlock(int childId, TsBlock tsBlock) {
+    Column[] columns = new Column[outputColumnsCount];
+    List<Integer> mapping = mappings.get(childId);
+    for (int i = 0; i < columns.length; i++) {
+      columns[i] = tsBlock.getColumn(mapping.get(i));
     }
+    return TsBlock.wrapBlocksWithoutCopy(
+        tsBlock.getPositionCount(),
+        new RunLengthEncodedColumn(
+            CommonOperatorUtils.TIME_COLUMN_TEMPLATE, tsBlock.getPositionCount()),
+        columns);
   }
 
+  @Override
   protected void closeCurrentChild(int index) throws Exception {
-    children.get(index).close();
-    children.set(index, null);
-    mappings.set(index, null);
+    try {
+      super.closeCurrentChild(index);
+    } finally {
+      mappings.set(index, null);
+    }
+  }
+
+  @Override
+  public long ramBytesUsed() {
+    long size =
+        super.ramBytesUsed()
+            + INSTANCE_SIZE
+            - RamUsageEstimator.shallowSizeOfInstance(CollectOperator.class);
+    for (List<Integer> mapping : mappings) {
+      if (mapping != null) {
+        size += RamUsageEstimator.shallowSizeOf(mapping.toArray());
+      }
+    }
+    return size;
   }
 }
diff --git a/iotdb-core/calc-commons/src/test/java/org/apache/iotdb/calc/execution/operator/process/CollectOperatorTest.java b/iotdb-core/calc-commons/src/test/java/org/apache/iotdb/calc/execution/operator/process/CollectOperatorTest.java
new file mode 100644
index 00000000000..00483f30b98
--- /dev/null
+++ b/iotdb-core/calc-commons/src/test/java/org/apache/iotdb/calc/execution/operator/process/CollectOperatorTest.java
@@ -0,0 +1,707 @@
+/*
+ * 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.iotdb.calc.execution.operator.process;
+
+import org.apache.iotdb.calc.execution.operator.CommonOperatorContext;
+import org.apache.iotdb.calc.execution.operator.Operator;
+import org.apache.iotdb.calc.plan.planner.memory.MemoryReservationManager;
+import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNodeId;
+
+import com.google.common.util.concurrent.ForwardingListenableFuture;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.SettableFuture;
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.read.common.block.TsBlock;
+import org.apache.tsfile.read.common.block.TsBlockBuilder;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.AbstractList;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executor;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+public class CollectOperatorTest {
+  private static TsBlock block(int... values) {
+    TsBlockBuilder builder =
+        new TsBlockBuilder(Collections.nCopies(values.length, TSDataType.INT32));
+    builder.getTimeColumnBuilder().writeLong(0);
+    for (int i = 0; i < values.length; i++) {
+      builder.getColumnBuilder(i).writeInt(values[i]);
+    }
+    builder.declarePosition();
+    return builder.build();
+  }
+
+  private static CollectOperator collect(Operator... children) {
+    CollectOperator collect =
+        new CollectOperator(new TestContext(), new ArrayList<>(Arrays.asList(children)));
+    return collect;
+  }
+
+  @Test
+  public void testReadyChildAndStableSelectionDuringBackpressure() throws Exception {
+    Child a = new Child(block(1));
+    Child b = new Child(block(2));
+    b.ready();
+    CollectOperator collect = collect(a, b);
+    ListenableFuture<?> selected = collect.isBlocked();
+    assertTrue(selected.isDone());
+    a.ready();
+    assertSame(selected, collect.isBlocked());
+    assertSame(b.output, collect.next());
+    assertEquals(0, a.advances);
+    assertEquals(1, b.advances);
+    assertTrue(collect.isBlocked().isDone());
+    assertSame(a.output, collect.next());
+    collect.close();
+  }
+
+  @Test
+  public void testThousandChildrenAndOneListenerPerPendingPeriod() throws Exception {
+    for (int count : new int[] {100, 1000}) {
+      List<Operator> children = new ArrayList<>();
+      for (int i = 0; i < count; i++) {
+        children.add(new Child(block(i)));
+      }
+      CollectOperator collect = new CollectOperator(new TestContext(), children);
+      Child ready = (Child) children.get(count - 1);
+      for (int round = 0; round < 5; round++) {
+        ListenableFuture<?> wait = collect.isBlocked();
+        assertFalse(wait.isDone());
+        for (int repeat = 0; repeat < 10; repeat++) {
+          assertSame(wait, collect.isBlocked());
+        }
+        ready.ready();
+        wait.get(5, TimeUnit.SECONDS);
+        assertSame(ready.output, collect.next());
+        ready.reset();
+      }
+      for (int i = 0; i < count - 1; i++) {
+        Child child = (Child) children.get(i);
+        assertEquals(1, child.waitCalls);
+        assertEquals(1, child.wait.registrations.get());
+      }
+      collect.close();
+    }
+  }
+
+  @Test
+  public void testAllReadyChildrenHaveLinearSelectionCost() throws Exception {
+    int childCount = 1000;
+    int rounds = 5;
+    List<Child> children = new ArrayList<>();
+    for (int i = 0; i < childCount; i++) {
+      Child child = new Child(block(i));
+      child.ready();
+      children.add(child);
+    }
+    CountingList countedChildren = new CountingList(children);
+    CollectOperator collect = new CollectOperator(new TestContext(), countedChildren);
+    try {
+      for (int round = 0; round < rounds; round++) {
+        assertTrue(collect.isBlocked().isDone());
+        // A completed future may notify more than once. It must occupy only one ready slot.
+        for (Child child : children) {
+          child.wait.listener.run();
+          child.wait.listener.run();
+        }
+        Set<Integer> consumed = new HashSet<>();
+        for (int i = 0; i < childCount; i++) {
+          assertTrue(collect.isBlocked().isDone());
+          TsBlock result = collect.next();
+          assertTrue(
+              "every ready child must be selected once per round",
+              consumed.add(result.getColumn(0).getInt(0)));
+        }
+        assertEquals(childCount, consumed.size());
+      }
+      for (Child child : children) {
+        assertEquals(rounds, child.advances);
+      }
+      long probes = countedChildren.getCalls + countFutureProbes(children);
+      long linearLimit = 20L * (childCount + childCount * rounds);
+      assertTrue(
+          "selection must use O(children + blocks) probes, observed " + probes,
+          probes <= linearLimit);
+    } finally {
+      collect.close();
+    }
+  }
+
+  @Test
+  public void testClosedChildrenDoNotIncreaseRemainingChildSelectionCost() throws Exception {
+    int childCount = 1000;
+    int remainingBlocks = 3000;
+    List<Child> children = new ArrayList<>();
+    for (int i = 0; i < childCount; i++) {
+      Child child = new Child(block(i));
+      child.hasNext = i == childCount - 1;
+      child.finished = !child.hasNext;
+      child.ready();
+      children.add(child);
+    }
+    CountingList countedChildren = new CountingList(children);
+    CollectOperator collect = new CollectOperator(new TestContext(), countedChildren);
+    try {
+      // Retire the first 999 children and consume one block from the remaining child.
+      for (int i = 0; i < childCount; i++) {
+        assertTrue(collect.isBlocked().isDone());
+        collect.next();
+      }
+      for (int i = 0; i < childCount - 1; i++) {
+        assertEquals(1, children.get(i).closes);
+      }
+      Child remaining = children.get(childCount - 1);
+      assertEquals(1, remaining.advances);
+      long initialProbes = countedChildren.getCalls + countFutureProbes(children);
+      for (int i = 0; i < remainingBlocks; i++) {
+        assertTrue(collect.isBlocked().isDone());
+        assertSame(remaining.output, collect.next());
+      }
+      assertEquals(remainingBlocks + 1, remaining.advances);
+      long probes = countedChildren.getCalls + countFutureProbes(children) - initialProbes;
+      assertTrue(
+          "retired children must not be visited for each remaining block, observed " + probes,
+          probes <= 20L * remainingBlocks);
+    } finally {
+      collect.close();
+    }
+  }
+
+  private static long countFutureProbes(List<Child> children) {
+    long probes = 0;
+    for (Child child : children) {
+      probes += child.wait.probes.get();
+    }
+    return probes;
+  }
+
+  @Test
+  public void testPendingListenerReusedAndStaleCompletionIgnored() throws Exception {
+    Child a = new Child(block(1));
+    Child b = new Child(block(2));
+    CollectOperator collect = collect(a, b);
+    ListenableFuture<?> first = collect.isBlocked();
+    b.ready();
+    assertSame(b.output, collect.next());
+    Runnable stale = b.wait.listener;
+    b.reset();
+    ListenableFuture<?> second = collect.isBlocked();
+    assertFalse(second.isDone());
+    stale.run();
+    assertFalse(second.isDone());
+    a.ready();
+    second.get(5, TimeUnit.SECONDS);
+    assertEquals(1, a.wait.registrations.get());
+    assertSame(a.output, collect.next());
+    assertTrue(first.isDone());
+    collect.close();
+  }
+
+  @Test
+  public void testCompletionBetweenSelectionsIsNotLost() throws Exception {
+    Child a = new Child(block(1));
+    Child b = new Child(block(2));
+    b.ready();
+    CollectOperator collect = collect(a, b);
+    try {
+      assertTrue(collect.isBlocked().isDone());
+      assertSame(b.output, collect.next());
+      b.reset();
+      // No selection is waiting yet: retain this completion until the next driver observation.
+      a.ready();
+      assertTrue(collect.isBlocked().isDone());
+      assertSame(a.output, collect.next());
+      a.reset();
+      assertFalse(collect.isBlocked().isDone());
+      assertEquals(1, a.advances);
+      assertEquals(1, b.advances);
+    } finally {
+      collect.close();
+    }
+  }
+
+  @Test
+  public void testNullEmptyAndUnfinishedChildRefreshReadiness() throws Exception {
+    Child child = new Child(null);
+    child.ready();
+    CollectOperator collect = collect(child);
+    assertTrue(collect.isBlocked().isDone());
+    assertNull(collect.next());
+    child.reset();
+    assertFalse(collect.isBlocked().isDone());
+    child.output = new TsBlockBuilder(Collections.singletonList(TSDataType.INT32)).build();
+    child.ready();
+    assertTrue(collect.next().isEmpty());
+    child.reset();
+    child.hasNext = false;
+    child.ready();
+    assertNull(collect.next());
+    assertFalse(collect.isFinished());
+    assertEquals(0, child.closes);
+    child.finished = true;
+    assertNull(collect.next());
+    assertTrue(collect.isFinished());
+    assertEquals(1, child.closes);
+    assertTrue(collect.isBlocked().isDone());
+  }
+
+  @Test
+  public void testCancellationDoesNotPropagateToChild() throws Exception {
+    Child child = new Child(block(1));
+    CollectOperator collect = collect(child);
+    ListenableFuture<?> exposed = collect.isBlocked();
+    assertTrue(exposed.cancel(true));
+    assertFalse(child.wait.isCancelled());
+    ListenableFuture<?> replacement = collect.isBlocked();
+    assertFalse(replacement.isCancelled());
+    child.ready();
+    replacement.get(5, TimeUnit.SECONDS);
+    assertSame(child.output, collect.next());
+    collect.close();
+  }
+
+  @Test
+  public void testFailureOfUnselectedChildIsNotLost() throws Exception {
+    Child a = new Child(block(1));
+    Child b = new Child(block(2));
+    a.ready();
+    CollectOperator collect = collect(a, b);
+    assertTrue(collect.isBlocked().isDone());
+    IOException expected = new IOException("child failure");
+    b.wait.delegate.setException(expected);
+    try {
+      collect.next();
+      fail("must propagate the failed child even when another child was selected");
+    } catch (IOException e) {
+      assertSame(expected, e);
+    } finally {
+      collect.close();
+    }
+    assertEquals(0, a.advances);
+  }
+
+  @Test
+  public void testChildCancellationPropagates() throws Exception {
+    Child child = new Child(block(1));
+    CollectOperator collect = collect(child);
+    ListenableFuture<?> wait = collect.isBlocked();
+    child.wait.cancel(false);
+    assertTrue(wait.isDone());
+    try {
+      collect.next();
+      fail("must propagate child cancellation");
+    } catch (CancellationException expected) {
+      assertEquals(0, child.advances);
+    } finally {
+      collect.close();
+    }
+  }
+
+  @Test
+  public void testCompletionDuringRegistrationAndOnAnotherThread() throws Exception {
+    Child a = new Child(block(1));
+    a.wait.beforeRegistration = a::ready;
+    CollectOperator collect = collect(a);
+    assertTrue(collect.isBlocked().isDone());
+    assertSame(a.output, collect.next());
+    a.reset();
+    ListenableFuture<?> pending = collect.isBlocked();
+    Thread completion = new Thread(a::ready);
+    completion.start();
+    pending.get(5, TimeUnit.SECONDS);
+    completion.join();
+    assertSame(a.output, collect.next());
+    collect.close();
+  }
+
+  @Test
+  public void testCloseWakesWaitAndClosesEveryChildDespiteException() throws Exception {
+    Child a = new Child(block(1));
+    Child b = new Child(block(2));
+    a.closeFailure = new IOException("close failure");
+    b.closeFailure = new IOException("second close failure");
+    CollectOperator collect = collect(a, b);
+    ListenableFuture<?> pending = collect.isBlocked();
+    try {
+      collect.close();
+      fail("must propagate close failure");
+    } catch (IOException e) {
+      assertSame(a.closeFailure, e);
+      assertEquals(1, e.getSuppressed().length);
+      assertSame(b.closeFailure, e.getSuppressed()[0]);
+    }
+    pending.get(5, TimeUnit.SECONDS);
+    assertEquals(1, a.closes);
+    assertEquals(1, b.closes);
+    a.ready();
+    b.ready();
+    assertTrue(collect.isFinished());
+    assertNull(collect.next());
+    collect.close();
+    assertEquals(1, b.closes);
+  }
+
+  @Test
+  public void testMappingUsesActualChildIdentity() throws Exception {
+    Child a = new Child(block(10, 11));
+    Child b = new Child(block(20, 21));
+    b.ready();
+    List<List<Integer>> mappings = new ArrayList<>();
+    mappings.add(Collections.singletonList(0));
+    mappings.add(Collections.singletonList(1));
+    MappingCollectOperator collect =
+        new MappingCollectOperator(
+            new TestContext(), new ArrayList<>(Arrays.asList(a, b)), mappings);
+    assertTrue(collect.isBlocked().isDone());
+    assertEquals(21, collect.next().getColumn(0).getInt(0));
+    a.ready();
+    assertEquals(10, collect.next().getColumn(0).getInt(0));
+    collect.close();
+    assertNull(mappings.get(0));
+    assertNull(mappings.get(1));
+  }
+
+  @Test
+  public void testMemoryIncludesAllLiveRetainedStates() throws Exception {
+    Child a = new Child(block(1));
+    Child b = new Child(block(2));
+    a.retained = 30;
+    a.peak = 100;
+    a.returnSize = 10;
+    b.retained = 50;
+    b.peak = 80;
+    b.returnSize = 20;
+    CollectOperator collect = collect(a, b);
+    assertEquals(80, collect.calculateRetainedSizeAfterCallingNext());
+    assertEquals(150, collect.calculateMaxPeekMemory());
+    assertEquals(20, collect.calculateMaxReturnSize());
+    assertTrue(collect.ramBytesUsed() > 0);
+    a.hasNext = false;
+    a.finished = true;
+    a.ready();
+    collect.next();
+    assertEquals(50, collect.calculateRetainedSizeAfterCallingNext());
+    assertEquals(80, collect.calculateMaxPeekMemory());
+    collect.close();
+    assertEquals(0, collect.calculateMaxPeekMemory());
+    assertEquals(0, collect.calculateMaxReturnSize());
+    assertEquals(0, collect.calculateRetainedSizeAfterCallingNext());
+  }
+
+  @Test
+  public void testEmptyCollectIsAlreadyFinished() throws Exception {
+    CollectOperator collect = collect();
+    assertFalse(collect.hasNext());
+    assertTrue(collect.isFinished());
+    assertTrue(collect.isBlocked().isDone());
+    assertNull(collect.next());
+    assertEquals(0, collect.calculateMaxPeekMemory());
+    assertEquals(0, collect.calculateMaxReturnSize());
+    collect.close();
+    collect.close();
+  }
+
+  @Test
+  public void testFailureWakesAllPendingChildren() throws Exception {
+    Child a = new Child(block(1));
+    Child b = new Child(block(2));
+    CollectOperator collect = collect(a, b);
+    ListenableFuture<?> pending = collect.isBlocked();
+    assertFalse(pending.isDone());
+    IOException expected = new IOException("blocked child failure");
+    b.wait.delegate.setException(expected);
+    try {
+      pending.get(5, TimeUnit.SECONDS);
+      fail("child failure must wake the driver");
+    } catch (ExecutionException e) {
+      assertSame(expected, e.getCause());
+    }
+    try {
+      collect.next();
+      fail("child failure must propagate without advancing other children");
+    } catch (IOException e) {
+      assertSame(expected, e);
+    } finally {
+      collect.close();
+    }
+    assertEquals(0, a.advances);
+    assertEquals(0, b.advances);
+    assertEquals(1, a.closes);
+    assertEquals(1, b.closes);
+  }
+
+  @Test
+  public void testConcurrentCompletionsConsumeEveryChildExactlyOnce() throws Exception {
+    int count = 16;
+    List<Child> children = new ArrayList<>();
+    for (int i = 0; i < count; i++) children.add(new Child(block(i)));
+    CollectOperator collect = new CollectOperator(new TestContext(), new ArrayList<>(children));
+    ListenableFuture<?> pending = collect.isBlocked();
+    assertFalse(pending.isDone());
+    ExecutorService executor = Executors.newFixedThreadPool(count);
+    CountDownLatch start = new CountDownLatch(1);
+    List<Future<?>> completions = new ArrayList<>();
+    try {
+      for (Child child : children) {
+        completions.add(
+            executor.submit(
+                () -> {
+                  start.await();
+                  child.ready();
+                  return null;
+                }));
+      }
+      start.countDown();
+      pending.get(5, TimeUnit.SECONDS);
+      for (Future<?> completion : completions) completion.get(5, TimeUnit.SECONDS);
+      Set<Integer> consumed = new HashSet<>();
+      for (int round = 0; round < 2 * count && collect.hasNext(); round++) {
+        assertTrue(collect.isBlocked().isDone());
+        TsBlock result = collect.next();
+        if (result != null) {
+          int childId = result.getColumn(0).getInt(0);
+          assertTrue("child emitted more than once", consumed.add(childId));
+          children.get(childId).hasNext = false;
+          children.get(childId).finished = true;
+        }
+      }
+      assertEquals(count, consumed.size());
+      assertTrue(collect.isFinished());
+      assertNull(collect.next());
+      for (Child child : children) {
+        assertEquals(1, child.advances);
+        assertEquals(1, child.closes);
+      }
+    } finally {
+      start.countDown();
+      executor.shutdownNow();
+      assertTrue(executor.awaitTermination(5, TimeUnit.SECONDS));
+      collect.close();
+    }
+  }
+
+  private static final class CountingList extends AbstractList<Operator> {
+    private final List<Operator> delegate;
+    private long getCalls;
+
+    private CountingList(List<? extends Operator> children) {
+      delegate = new ArrayList<>(children);
+    }
+
+    @Override
+    public Operator get(int index) {
+      getCalls++;
+      return delegate.get(index);
+    }
+
+    @Override
+    public Operator set(int index, Operator child) {
+      return delegate.set(index, child);
+    }
+
+    @Override
+    public int size() {
+      return delegate.size();
+    }
+  }
+
+  private static final class CountingFuture
+      extends ForwardingListenableFuture.SimpleForwardingListenableFuture<Void> {
+    private final SettableFuture<Void> delegate;
+    private final AtomicInteger registrations = new AtomicInteger();
+    private final AtomicInteger probes = new AtomicInteger();
+    private Runnable beforeRegistration;
+    private Runnable listener;
+
+    private CountingFuture() {
+      this(SettableFuture.create());
+    }
+
+    private CountingFuture(SettableFuture<Void> delegate) {
+      super(delegate);
+      this.delegate = delegate;
+    }
+
+    @Override
+    public boolean isDone() {
+      probes.incrementAndGet();
+      return super.isDone();
+    }
+
+    @Override
+    public Void get() throws InterruptedException, ExecutionException {
+      probes.incrementAndGet();
+      return super.get();
+    }
+
+    @Override
+    public Void get(long timeout, TimeUnit unit)
+        throws InterruptedException, ExecutionException, TimeoutException {
+      probes.incrementAndGet();
+      return super.get(timeout, unit);
+    }
+
+    @Override
+    public void addListener(Runnable listener, Executor executor) {
+      this.listener = listener;
+      registrations.incrementAndGet();
+      if (beforeRegistration != null) {
+        beforeRegistration.run();
+      }
+      super.addListener(listener, executor);
+    }
+  }
+
+  private static final class TestContext extends CommonOperatorContext {
+    private TestContext() {
+      super(0, new PlanNodeId("collect"), "test");
+    }
+
+    @Override
+    public MemoryReservationManager getMemoryReservationContext() {
+      return null;
+    }
+
+    @Override
+    public int getFragmentId() {
+      return 0;
+    }
+
+    @Override
+    public int getPipelineId() {
+      return 0;
+    }
+
+    @Override
+    public long ramBytesUsed() {
+      return 0;
+    }
+  }
+
+  private static final class Child implements Operator {
+    private final Thread driver = Thread.currentThread();
+    private final CommonOperatorContext context = new TestContext();
+    private CountingFuture wait = new CountingFuture();
+    private TsBlock output;
+    private boolean hasNext = true;
+    private boolean finished;
+    private int advances;
+    private int waitCalls;
+    private int closes;
+    private IOException closeFailure;
+    private long retained, peak, returnSize;
+
+    private Child(TsBlock output) {
+      this.output = output;
+    }
+
+    private void ready() {
+      wait.delegate.set(null);
+    }
+
+    private void reset() {
+      wait = new CountingFuture();
+    }
+
+    private void checkDriver() {
+      assertSame(driver, Thread.currentThread());
+    }
+
+    @Override
+    public CommonOperatorContext getOperatorContext() {
+      return context;
+    }
+
+    @Override
+    public ListenableFuture<?> isBlocked() {
+      checkDriver();
+      waitCalls++;
+      return wait;
+    }
+
+    @Override
+    public boolean hasNext() {
+      checkDriver();
+      return hasNext;
+    }
+
+    @Override
+    public TsBlock next() {
+      checkDriver();
+      advances++;
+      return output;
+    }
+
+    @Override
+    public boolean isFinished() {
+      checkDriver();
+      return finished;
+    }
+
+    @Override
+    public void close() throws Exception {
+      checkDriver();
+      closes++;
+      if (closeFailure != null) throw closeFailure;
+    }
+
+    @Override
+    public long calculateMaxPeekMemory() {
+      return peak;
+    }
+
+    @Override
+    public long calculateMaxReturnSize() {
+      return returnSize;
+    }
+
+    @Override
+    public long calculateRetainedSizeAfterCallingNext() {
+      return retained;
+    }
+
+    @Override
+    public long ramBytesUsed() {
+      return 0;
+    }
+  }
+}
diff --git a/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java b/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
index 3f9abbc40ff..dabf70dd732 100644
--- a/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
+++ b/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
@@ -4089,4 +4089,7 @@ public final class DataNodeQueryMessages {
       "Dispatch LOAD piece: loadId={}, regionId={}, pieceIndex={}, tsFile={}, dataSize={}, replicaSet={}";
   public static final String LOG_DISPATCH_LOAD_PIECE_SUCCESS_LOADID_ARG_REGIONID_ARG_PIECEINDEX_ARG_DATASIZE_ARG_75F7AEFE =
       "Dispatch LOAD piece success: loadId={}, regionId={}, pieceIndex={}, dataSize={}";
+  public static final String EXCEPTION_PLAN_NODE_ARG_IS_ALREADY_REGISTERED_ON_MEMORY_SIDE_ARG_NOT_ARG_4C1BB625 = "Plan node %s is already registered on memory side %s, not %s";
+  public static final String EXCEPTION_MEMORY_RESERVATION_IS_NOT_REGISTERED_FOR_QUERY_ARG_FRAGMENT_ARG_PLAN_NODE_ARG_67A4B81F = "Memory reservation is not registered for query %s, fragment %s, plan node %s";
+  public static final String LOG_SHARED_EXCHANGE_BUDGET_FOR_FI_ARG_INPUT_ARG_OUTPUT_ARG_98AE1109 = "Shared exchange budget for FI {}: INPUT={}, OUTPUT={}";
 }
diff --git a/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java b/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
index 12cb3b07a05..9addf5f954a 100644
--- a/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
+++ b/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java
@@ -4844,4 +4844,7 @@ public final class DataNodeQueryMessages {
       "下发 LOAD piece:loadId={},regionId={},pieceIndex={},tsFile={},dataSize={},replicaSet={}";
   public static final String LOG_DISPATCH_LOAD_PIECE_SUCCESS_LOADID_ARG_REGIONID_ARG_PIECEINDEX_ARG_DATASIZE_ARG_75F7AEFE =
       "下发 LOAD piece 成功:loadId={},regionId={},pieceIndex={},dataSize={}";
+  public static final String EXCEPTION_PLAN_NODE_ARG_IS_ALREADY_REGISTERED_ON_MEMORY_SIDE_ARG_NOT_ARG_4C1BB625 = "PlanNode %s 已注册到内存侧 %s,不能注册到 %s";
+  public static final String EXCEPTION_MEMORY_RESERVATION_IS_NOT_REGISTERED_FOR_QUERY_ARG_FRAGMENT_ARG_PLAN_NODE_ARG_67A4B81F = "查询 %s、Fragment %s、PlanNode %s 尚未注册内存账目";
+  public static final String LOG_SHARED_EXCHANGE_BUDGET_FOR_FI_ARG_INPUT_ARG_OUTPUT_ARG_98AE1109 = "FI {} 的共享 exchange 预算:INPUT={},OUTPUT={}";
 }
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/SharedTsBlockQueue.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/SharedTsBlockQueue.java
index 5812d4e37cd..0d842380361 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/SharedTsBlockQueue.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/SharedTsBlockQueue.java
@@ -27,6 +27,7 @@ import org.apache.iotdb.db.queryengine.execution.exchange.sink.LocalSinkChannel;
 import org.apache.iotdb.db.queryengine.execution.exchange.source.LocalSourceHandle;
 import org.apache.iotdb.db.queryengine.execution.memory.LocalMemoryManager;
 import org.apache.iotdb.db.queryengine.execution.memory.MemoryPool.MemoryReservationResult;
+import org.apache.iotdb.db.queryengine.execution.memory.MemorySide;
 import org.apache.iotdb.db.utils.CommonUtils;
 import org.apache.iotdb.mpp.rpc.thrift.TFragmentInstanceId;
 
@@ -265,7 +266,10 @@ public class SharedTsBlockQueue {
       localMemoryManager
           .getQueryPool()
           .registerPlanNodeIdToQueryMemoryMap(
-              localFragmentInstanceId.queryId, fullFragmentInstanceId, localPlanNodeId);
+              localFragmentInstanceId.queryId,
+              fullFragmentInstanceId,
+              localPlanNodeId,
+              MemorySide.OUTPUT);
       alreadyRegistered = true;
     }
     MemoryReservationResult reserveResult =
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/sink/SinkChannel.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/sink/SinkChannel.java
index d24a1552a5b..4d592771ef2 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/sink/SinkChannel.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/sink/SinkChannel.java
@@ -30,6 +30,7 @@ import org.apache.iotdb.db.queryengine.exception.exchange.GetTsBlockFromClosedOr
 import org.apache.iotdb.db.queryengine.execution.exchange.MPPDataExchangeManager.SinkListener;
 import org.apache.iotdb.db.queryengine.execution.memory.LocalMemoryManager;
 import org.apache.iotdb.db.queryengine.execution.memory.MemoryPool.MemoryReservationResult;
+import org.apache.iotdb.db.queryengine.execution.memory.MemorySide;
 import org.apache.iotdb.db.queryengine.metric.DataExchangeCostMetricSet;
 import org.apache.iotdb.db.queryengine.metric.DataExchangeCountMetricSet;
 import org.apache.iotdb.db.utils.SetThreadName;
@@ -235,7 +236,10 @@ public class SinkChannel implements ISinkChannel {
     localMemoryManager
         .getQueryPool()
         .registerPlanNodeIdToQueryMemoryMap(
-            localFragmentInstanceId.queryId, fullFragmentInstanceId, localPlanNodeId);
+            localFragmentInstanceId.queryId,
+            fullFragmentInstanceId,
+            localPlanNodeId,
+            MemorySide.OUTPUT);
   }
 
   @Override
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/SourceHandle.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/SourceHandle.java
index aed3f9533fb..8ee7ad601ef 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/SourceHandle.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/SourceHandle.java
@@ -31,6 +31,7 @@ import org.apache.iotdb.db.queryengine.common.FragmentInstanceId;
 import org.apache.iotdb.db.queryengine.execution.exchange.MPPDataExchangeManager.SourceHandleListener;
 import org.apache.iotdb.db.queryengine.execution.memory.LocalMemoryManager;
 import org.apache.iotdb.db.queryengine.execution.memory.MemoryPool.MemoryReservationResult;
+import org.apache.iotdb.db.queryengine.execution.memory.MemorySide;
 import org.apache.iotdb.db.queryengine.metric.DataExchangeCostMetricSet;
 import org.apache.iotdb.db.queryengine.metric.DataExchangeCountMetricSet;
 import org.apache.iotdb.db.utils.SetThreadName;
@@ -223,7 +224,10 @@ public class SourceHandle implements ISourceHandle {
     localMemoryManager
         .getQueryPool()
         .registerPlanNodeIdToQueryMemoryMap(
-            localFragmentInstanceId.queryId, fullFragmentInstanceId, localPlanNodeId);
+            localFragmentInstanceId.queryId,
+            fullFragmentInstanceId,
+            localPlanNodeId,
+            MemorySide.INPUT);
   }
 
   @Override
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/fragment/FragmentInstanceContext.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/fragment/FragmentInstanceContext.java
index 5b8ee6ae1c1..16fecf6bc49 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/fragment/FragmentInstanceContext.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/fragment/FragmentInstanceContext.java
@@ -120,6 +120,11 @@ public class FragmentInstanceContext extends QueryContext {
 
   private final MemoryReservationManager memoryReservationManager;
 
+  // Initialized after the complete local plan is generated, before drivers start. Both are zero
+  // when this FI uses independent per-plan-node exchange quotas.
+  private long inputMaxBytes;
+  private long outputMaxBytes;
+
   protected IDataRegionForQuery dataRegion;
   private Filter globalTimeFilter;
   private List<TimeRange> globalTimeFilterTimeRanges;
@@ -551,6 +556,19 @@ public class FragmentInstanceContext extends QueryContext {
     return id;
   }
 
+  public void initializeExchangeMemoryBudget(long inputMaxBytes, long outputMaxBytes) {
+    this.inputMaxBytes = inputMaxBytes;
+    this.outputMaxBytes = outputMaxBytes;
+  }
+
+  public long getInputMaxBytes() {
+    return inputMaxBytes;
+  }
+
+  public long getOutputMaxBytes() {
+    return outputMaxBytes;
+  }
+
   public void failed(Throwable cause) {
     stateMachine.failed(cause);
   }
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/memory/MemoryPool.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/memory/MemoryPool.java
index b2d35568fa3..63cf3ce1d05 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/memory/MemoryPool.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/memory/MemoryPool.java
@@ -37,9 +37,9 @@ import javax.annotation.Nullable;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.Queue;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.ConcurrentSkipListMap;
+import java.util.concurrent.atomic.AtomicLong;
 import java.util.stream.Collectors;
 
 import static com.google.common.util.concurrent.Futures.immediateVoidFuture;
@@ -55,10 +55,14 @@ public class MemoryPool {
     private final String fragmentInstanceId;
     private final String planNodeId;
     private final long bytesToReserve;
+    // Assigned once by the pool before publication. Zero denotes an untracked future.
+    private long reservationId;
+    private FragmentMemoryReservation fragment;
 
     /**
-     * MemoryReservationFuture is created when SinkHandle or SourceHandle tries to reserve memory
-     * from pool. This field is max Bytes that SinkHandle or SourceHandle can reserve.
+     * MemoryReservationFuture is created when an exchange handle tries to reserve memory from the
+     * pool. Local planning gives handles their FI-side limit in shared mode or their independent
+     * plan-node limit in legacy mode. Retain the supplied limit when retrying this request.
      */
     private final long maxBytesCanReserve;
 
@@ -152,14 +156,47 @@ public class MemoryPool {
 
   private final String id;
   private final IMemoryBlock memoryBlock;
-  private final long maxBytesPerFragmentInstance;
 
-  /** queryId -> fragmentInstanceId -> planNodeId -> bytesReserved. */
-  private final Map<String, Map<String, Map<String, Long>>> queryMemoryReservations =
+  private static class PlanNodeReservation {
+    private final MemorySide side;
+    private volatile long bytesReserved;
+    private boolean deregistered;
+
+    private PlanNodeReservation(MemorySide side) {
+      this.side = side;
+    }
+
+    @Override
+    public String toString() {
+      return side + ":" + bytesReserved;
+    }
+  }
+
+  private static class FragmentMemoryReservation {
+    private final Map<String, PlanNodeReservation> planNodes = new ConcurrentHashMap<>();
+    private final AtomicLong inputUsedBytes = new AtomicLong();
+    private final AtomicLong outputUsedBytes = new AtomicLong();
+    private final Map<Long, MemoryReservationFuture<Void>> pendingReservations =
+        new ConcurrentHashMap<>();
+    // Enabled by local planning before drivers start; request limits are supplied by handles.
+    private volatile boolean sharedBudget;
+    // Protected by this monitor, together with waiter publication and retry.
+    private boolean deregistered;
+
+    private AtomicLong getUsedBytes(MemorySide side) {
+      return side == MemorySide.INPUT ? inputUsedBytes : outputUsedBytes;
+    }
+  }
+
+  /** queryId -> fragmentInstanceId -> FI side totals and planNode details. */
+  private final Map<String, Map<String, FragmentMemoryReservation>> queryMemoryReservations =
       new ConcurrentHashMap<>();
 
-  private final Queue<MemoryReservationFuture<Void>> memoryReservationFutures =
-      new ConcurrentLinkedQueue<>();
+  // Sequence keys retain FIFO wakeup order and allow removal without scanning unrelated requests.
+  // Only blocked reservations use these indexes; successful admission keeps its existing fast path.
+  private final Map<Long, MemoryReservationFuture<Void>> memoryReservationFutures =
+      new ConcurrentSkipListMap<>();
+  private final AtomicLong nextReservationId = new AtomicLong();
 
   public MemoryPool(String id, MemoryManager memoryManager, long maxBytesPerFragmentInstance) {
     this.id = Validate.notNull(id, DataNodeQueryMessages.EXCEPTION_ID_CAN_NOT_BE_NULL_DOT_BDD2AD7D);
@@ -176,7 +213,6 @@ public class MemoryPool {
             .EXCEPTION_MAX_BYTES_PER_FI_SHOULD_BE_IN_LEFT_PAREN_0_COMMA_MAXBYTES_RIGHT_BRACKET_DOT_MAXB_4D37C457,
         maxBytesPerFragmentInstance,
         this.memoryBlock.getTotalMemorySizeInBytes());
-    this.maxBytesPerFragmentInstance = maxBytesPerFragmentInstance;
   }
 
   public String getId() {
@@ -204,12 +240,40 @@ public class MemoryPool {
    * planNodeId to queryMemoryReservationsMap first.
    */
   public void registerPlanNodeIdToQueryMemoryMap(
-      String queryId, String fragmentInstanceId, String planNodeId) {
+      String queryId, String fragmentInstanceId, String planNodeId, MemorySide side) {
+    java.util.Objects.requireNonNull(side);
     synchronized (queryMemoryReservations) {
       queryMemoryReservations
           .computeIfAbsent(queryId, x -> new ConcurrentHashMap<>())
-          .computeIfAbsent(fragmentInstanceId, x -> new ConcurrentHashMap<>())
-          .putIfAbsent(planNodeId, 0L);
+          .computeIfAbsent(fragmentInstanceId, x -> new FragmentMemoryReservation())
+          .planNodes
+          .compute(
+              planNodeId,
+              (key, reservation) -> {
+                if (reservation != null && reservation.side != side) {
+                  throw new IllegalArgumentException(
+                      String.format(
+                          DataNodeQueryMessages
+                              .EXCEPTION_PLAN_NODE_ARG_IS_ALREADY_REGISTERED_ON_MEMORY_SIDE_ARG_NOT_ARG_4C1BB625,
+                          planNodeId,
+                          reservation.side,
+                          side));
+                }
+                return reservation == null ? new PlanNodeReservation(side) : reservation;
+              });
+    }
+  }
+
+  /** Enable shared accounting after the complete FI has been checked by local planning. */
+  public void registerFragmentInstanceSharedBudget(String queryId, String fragmentInstanceId) {
+    synchronized (queryMemoryReservations) {
+      FragmentMemoryReservation fragment =
+          queryMemoryReservations
+              .computeIfAbsent(queryId, x -> new ConcurrentHashMap<>())
+              .computeIfAbsent(fragmentInstanceId, x -> new FragmentMemoryReservation());
+      synchronized (fragment) {
+        fragment.sharedBudget = true;
+      }
     }
   }
 
@@ -224,45 +288,74 @@ public class MemoryPool {
    */
   public void deRegisterFragmentInstanceFromQueryMemoryMap(
       String queryId, String fragmentInstanceId, boolean forceDeregister) {
-    Map<String, Map<String, Long>> queryRelatedMemory = queryMemoryReservations.get(queryId);
-    if (queryRelatedMemory != null) {
-      Map<String, Long> fragmentRelatedMemory = queryRelatedMemory.get(fragmentInstanceId);
-      boolean hasPotentialMemoryLeak = false;
-      // fragmentRelatedMemory could be null if the FI has not reserved any memory(For example,
-      // next() of root operator returns no data)
-      if (fragmentRelatedMemory != null) {
-        hasPotentialMemoryLeak =
-            fragmentRelatedMemory.values().stream().anyMatch(value -> value != 0L);
+    Map<String, FragmentMemoryReservation> queryRelatedMemory =
+        queryMemoryReservations.get(queryId);
+    if (queryRelatedMemory == null) {
+      return;
+    }
+    FragmentMemoryReservation fragment = queryRelatedMemory.get(fragmentInstanceId);
+    boolean hasPotentialMemoryLeak = false;
+    synchronized (queryMemoryReservations) {
+      if (fragment != null) {
+        synchronized (fragment) {
+          if (queryRelatedMemory.get(fragmentInstanceId) != fragment) {
+            return;
+          }
+          if (!forceDeregister && hasReservedMemory(fragment)) {
+            // Local queues can still hold this FI's allocation after its driver has closed.
+            return;
+          }
+          fragment.deregistered = true;
+          setPlanNodesDeregistered(fragment, true);
+          // Wait for reservations that had already acquired their plan-node accounting lock.
+          hasPotentialMemoryLeak = hasReservedMemory(fragment);
+          if (!forceDeregister && hasPotentialMemoryLeak) {
+            setPlanNodesDeregistered(fragment, false);
+            fragment.deregistered = false;
+            return;
+          }
+          queryRelatedMemory.remove(fragmentInstanceId, fragment);
+        }
       }
-      if (!forceDeregister && hasPotentialMemoryLeak) {
-        // If hasPotentialMemoryLeak is true, it means that LocalSourceChannel/LocalSourceHandles
-        // have not been closed.
-        // We should wait for them to be closed. Make sure this method is called again with
-        // forceDeregister == true, after all LocalSourceChannel/LocalSourceHandles are closed.
-        return;
+      if (queryRelatedMemory.isEmpty()
+          && queryMemoryReservations.get(queryId) == queryRelatedMemory) {
+        queryMemoryReservations.remove(queryId);
       }
-      synchronized (queryMemoryReservations) {
-        queryRelatedMemory.remove(fragmentInstanceId);
-        // Another fragment may have recreated the query entry while this cleanup waited for the
-        // lock.
-        if (queryRelatedMemory.isEmpty()
-            && queryMemoryReservations.get(queryId) == queryRelatedMemory) {
-          queryMemoryReservations.remove(queryId);
-        }
+    }
+    // Detach without completing the futures. Only owners may cancel and deduct pending bytes;
+    // cancellation listeners otherwise mistake cancellation for admission. Publication and
+    // retries cannot enter this account after it has been marked as deregistered.
+    if (fragment != null) {
+      for (MemoryReservationFuture<Void> future : fragment.pendingReservations.values()) {
+        removePendingReservation(future);
       }
-      if (hasPotentialMemoryLeak) {
-        // hasPotentialMemoryLeak means that fragmentRelatedMemory is not null
-        List<Map.Entry<String, Long>> invalidEntryList =
-            fragmentRelatedMemory.entrySet().stream()
-                .filter(entry -> entry.getValue() != 0L)
-                .collect(Collectors.toList());
-        throw new MemoryLeakException(
-            String.format(
-                DataNodeQueryMessages
-                    .QUERY_EXCEPTION_PLANNODE_RELATED_MEMORY_IS_NOT_ZERO_WHEN_TRYING_TO_DEREGISTER_E01109C5,
-                queryId,
-                fragmentInstanceId,
-                invalidEntryList));
+    }
+    if (hasPotentialMemoryLeak) {
+      List<Map.Entry<String, PlanNodeReservation>> invalidEntryList =
+          fragment.planNodes.entrySet().stream()
+              .filter(entry -> entry.getValue().bytesReserved != 0L)
+              .collect(Collectors.toList());
+      throw new MemoryLeakException(
+          String.format(
+              DataNodeQueryMessages
+                  .QUERY_EXCEPTION_PLANNODE_RELATED_MEMORY_IS_NOT_ZERO_WHEN_TRYING_TO_DEREGISTER_E01109C5,
+              queryId,
+              fragmentInstanceId,
+              invalidEntryList));
+    }
+  }
+
+  private static boolean hasReservedMemory(FragmentMemoryReservation fragment) {
+    return fragment.inputUsedBytes.get() != 0L
+        || fragment.outputUsedBytes.get() != 0L
+        || fragment.planNodes.values().stream().anyMatch(value -> value.bytesReserved != 0L);
+  }
+
+  private static void setPlanNodesDeregistered(
+      FragmentMemoryReservation fragment, boolean deregistered) {
+    for (PlanNodeReservation reservation : fragment.planNodes.values()) {
+      synchronized (reservation) {
+        reservation.deregistered = deregistered;
       }
     }
   }
@@ -289,11 +382,10 @@ public class MemoryPool {
     Validate.notNull(
         planNodeId, DataNodeQueryMessages.EXCEPTION_PLANNODEID_CAN_NOT_BE_NULL_DOT_44027620);
     Validate.isTrue(
-        bytesToReserve > 0L && bytesToReserve <= maxBytesPerFragmentInstance,
-        DataNodeQueryMessages
-            .EXCEPTION_BYTESTORESERVE_SHOULD_BE_IN_LEFT_PAREN_0_COMMA_MAXBYTESPERFI_RIGHT_BRACKET_DOT_M_0753BB69,
-        maxBytesPerFragmentInstance,
-        bytesToReserve);
+        bytesToReserve > 0L,
+        DataNodeQueryMessages.EXCEPTION_BYTESTORESERVE_SHOULD_BE_GREATER_THAN_ZERO_DOT_56D15DE0);
+    // A request must fit both the caller's FI/handle limit and the physical global pool.
+    maxBytesCanReserve = Math.min(maxBytesCanReserve, getMaxBytes());
     if (bytesToReserve > maxBytesCanReserve) {
       LOGGER.warn(
           DataNodeQueryMessages
@@ -312,7 +404,6 @@ public class MemoryPool {
     if (tryReserve(queryId, fragmentInstanceId, planNodeId, bytesToReserve, maxBytesCanReserve)) {
       return new MemoryReservationResult(immediateVoidFuture(), true, bytesToReserve);
     } else {
-      rollbackReserve(queryId, fragmentInstanceId, planNodeId, bytesToReserve);
       if (isHighestPriority) {
         // SHOW QUERIES: treat as success with zero bytes reserved from pool when insufficient.
         return new MemoryReservationResult(immediateVoidFuture(), true, 0L);
@@ -321,10 +412,37 @@ public class MemoryPool {
           DataNodeQueryMessages.BLOCKED_RESERVE_REQUEST_ARG_BYTES_MEMORY_FOR_PLANNODEIDARG,
           bytesToReserve,
           planNodeId);
-      ListenableFuture<Void> result =
+      MemoryReservationFuture<Void> result =
           MemoryReservationFuture.create(
               queryId, fragmentInstanceId, planNodeId, bytesToReserve, maxBytesCanReserve);
-      memoryReservationFutures.add((MemoryReservationFuture<Void>) result);
+      FragmentMemoryReservation fragment =
+          getFragmentReservation(queryId, fragmentInstanceId, planNodeId);
+      synchronized (fragment) {
+        // Deregistration must not miss a request published after its pending-request scan.
+        if (fragment.deregistered) {
+          throw new IllegalArgumentException(
+              String.format(
+                  DataNodeQueryMessages
+                      .EXCEPTION_MEMORY_RESERVATION_IS_NOT_REGISTERED_FOR_QUERY_ARG_FRAGMENT_ARG_PLAN_NODE_ARG_67A4B81F,
+                  queryId,
+                  fragmentInstanceId,
+                  planNodeId));
+        }
+        result.fragment = fragment;
+        result.reservationId = nextReservationId.incrementAndGet();
+        fragment.pendingReservations.put(result.reservationId, result);
+        memoryReservationFutures.put(result.reservationId, result);
+      }
+      // A concurrent free may have missed this request before it was published. Retry after
+      // publication, under the same future lock used by free and tryCancel to avoid charging twice.
+      try {
+        if (tryCompleteReservation(result)) {
+          removePendingReservation(result);
+        }
+      } catch (RuntimeException | Error e) {
+        removePendingReservation(result);
+        throw e;
+      }
       return new MemoryReservationResult(result, false, bytesToReserve);
     }
   }
@@ -343,17 +461,24 @@ public class MemoryPool {
     Validate.notNull(
         planNodeId, DataNodeQueryMessages.EXCEPTION_PLANNODEID_CAN_NOT_BE_NULL_DOT_44027620);
     Validate.isTrue(
-        bytesToReserve > 0L && bytesToReserve <= maxBytesPerFragmentInstance,
-        DataNodeQueryMessages
-            .EXCEPTION_BYTESTORESERVE_SHOULD_BE_IN_LEFT_PAREN_0_COMMA_MAXBYTESPERFI_RIGHT_BRACKET_DOT_M_0753BB69,
-        maxBytesPerFragmentInstance,
-        bytesToReserve);
+        bytesToReserve > 0L,
+        DataNodeQueryMessages.EXCEPTION_BYTESTORESERVE_SHOULD_BE_GREATER_THAN_ZERO_DOT_56D15DE0);
 
-    if (tryReserve(queryId, fragmentInstanceId, planNodeId, bytesToReserve, maxBytesCanReserve)) {
-      return true;
-    } else {
-      rollbackReserve(queryId, fragmentInstanceId, planNodeId, bytesToReserve);
-      return false;
+    return tryReserve(queryId, fragmentInstanceId, planNodeId, bytesToReserve, maxBytesCanReserve);
+  }
+
+  /**
+   * Marks a fragment account as deregistered without removing it from the map, mirroring the
+   * production mark that precedes the pending-request drain. Lets tests deterministically exercise
+   * the publication rejection of a closed account.
+   */
+  @TestOnly
+  public void markFragmentDeregisteredForTest(String queryId, String fragmentInstanceId) {
+    FragmentMemoryReservation fragment =
+        queryMemoryReservations.get(queryId).get(fragmentInstanceId);
+    synchronized (fragment) {
+      fragment.deregistered = true;
+      setPlanNodesDeregistered(fragment, true);
     }
   }
 
@@ -375,6 +500,9 @@ public class MemoryPool {
     synchronized (future) {
       // If the future is not a MemoryReservationFuture, it must have been completed.
       if (future.isDone()) {
+        if (future instanceof MemoryReservationFuture) {
+          removePendingReservation((MemoryReservationFuture<Void>) future);
+        }
         return 0L;
       }
       Validate.isTrue(
@@ -382,6 +510,7 @@ public class MemoryPool {
           DataNodeQueryMessages.EXCEPTION_INVALID_FUTURE_TYPE_14507EF5
               + future.getClass().getSimpleName());
       future.cancel(true);
+      removePendingReservation((MemoryReservationFuture<Void>) future);
     }
     return ((MemoryReservationFuture<Void>) future).getBytesToReserve();
   }
@@ -390,66 +519,105 @@ public class MemoryPool {
     Validate.notNull(queryId, DataNodeQueryMessages.EXCEPTION_QUERYID_CAN_NOT_BE_NULL_DOT_16639DBE);
     Validate.isTrue(bytes > 0L);
 
-    try {
-      queryMemoryReservations
-          .get(queryId)
-          .get(fragmentInstanceId)
-          .computeIfPresent(
-              planNodeId,
-              (k, reservedMemory) -> {
-                if (reservedMemory < bytes) {
-                  throw new IllegalArgumentException(
-                      DataNodeQueryMessages.FREE_MORE_MEMORY_THAN_HAS_BEEN_RESERVED);
-                }
-                return reservedMemory - bytes;
-              });
-    } catch (NullPointerException e) {
+    Map<String, FragmentMemoryReservation> fragments = queryMemoryReservations.get(queryId);
+    FragmentMemoryReservation fragment =
+        fragments == null ? null : fragments.get(fragmentInstanceId);
+    if (fragment == null) {
       throw new IllegalArgumentException(
           DataNodeQueryMessages
-              .QUERY_EXCEPTION_RELATEDMEMORYRESERVED_CAN_T_BE_NULL_WHEN_FREEING_MEMORY_C80009F2,
-          e);
+              .QUERY_EXCEPTION_RELATEDMEMORYRESERVED_CAN_T_BE_NULL_WHEN_FREEING_MEMORY_C80009F2);
     }
-
-    memoryBlock.release(bytes);
+    PlanNodeReservation reservation =
+        requireReservation(
+            fragment.planNodes.get(planNodeId), queryId, fragmentInstanceId, planNodeId);
+    Object accountingLock = fragment.sharedBudget ? fragment : reservation;
+    synchronized (accountingLock) {
+      if (reservation.bytesReserved < bytes) {
+        throw new IllegalArgumentException(
+            DataNodeQueryMessages.FREE_MORE_MEMORY_THAN_HAS_BEEN_RESERVED);
+      }
+      reservation.bytesReserved -= bytes;
+      fragment.getUsedBytes(reservation.side).addAndGet(-bytes);
+      memoryBlock.release(bytes);
+    }
+    // Complete futures outside the accounting lock: listeners can reserve or free other inputs.
 
     if (memoryReservationFutures.isEmpty()) {
       return;
     }
-    Iterator<MemoryReservationFuture<Void>> iterator = memoryReservationFutures.iterator();
+    Iterator<MemoryReservationFuture<Void>> iterator = memoryReservationFutures.values().iterator();
     while (iterator.hasNext()) {
       MemoryReservationFuture<Void> future = iterator.next();
-      synchronized (future) {
-        if (future.isCancelled() || future.isDone()) {
-          iterator.remove();
-          continue;
+      if (tryCompleteReservation(future)) {
+        removePendingReservation(future);
+      }
+    }
+  }
+
+  private void removePendingReservation(MemoryReservationFuture<Void> future) {
+    memoryReservationFutures.remove(future.reservationId, future);
+    if (future.fragment != null) {
+      future.fragment.pendingReservations.remove(future.reservationId, future);
+    }
+  }
+
+  private boolean tryCompleteReservation(MemoryReservationFuture<Void> future) {
+    synchronized (future) {
+      if (future.isDone()) {
+        return true;
+      }
+      long bytesToReserve = future.getBytesToReserve();
+      String queryId = future.getQueryId();
+      String fragmentInstanceId = future.getFragmentInstanceId();
+      String planNodeId = future.getPlanNodeId();
+      synchronized (future.fragment) {
+        if (future.fragment.deregistered) {
+          return true;
         }
-        long bytesToReserve = future.getBytesToReserve();
-        String curQueryId = future.getQueryId();
-        String curFragmentInstanceId = future.getFragmentInstanceId();
-        String curPlanNodeId = future.getPlanNodeId();
-        long maxBytesCanReserve = future.getMaxBytesCanReserve();
-        if (tryReserve(
-            curQueryId, curFragmentInstanceId, curPlanNodeId, bytesToReserve, maxBytesCanReserve)) {
-          future.set(null);
-          iterator.remove();
-        } else {
-          rollbackReserve(curQueryId, curFragmentInstanceId, curPlanNodeId, bytesToReserve);
+        if (!tryReserve(
+            queryId,
+            fragmentInstanceId,
+            planNodeId,
+            bytesToReserve,
+            future.getMaxBytesCanReserve())) {
+          return false;
         }
       }
+      // Owner callbacks may register or release other accounts.
+      future.set(null);
+      return true;
     }
   }
 
   public long getQueryMemoryReservedBytes(String queryId) {
-    if (!queryMemoryReservations.containsKey(queryId)) {
+    Map<String, FragmentMemoryReservation> fragments = queryMemoryReservations.get(queryId);
+    if (fragments == null) {
       return 0L;
     }
     long sum = 0;
-    for (Map<String, Long> map : queryMemoryReservations.get(queryId).values()) {
-      sum = sum + map.values().stream().reduce(0L, Long::sum);
+    for (FragmentMemoryReservation fragment : fragments.values()) {
+      sum += fragment.inputUsedBytes.get() + fragment.outputUsedBytes.get();
     }
     return sum;
   }
 
+  public long getFragmentMemoryReservedBytes(
+      String queryId, String fragmentInstanceId, MemorySide side) {
+    Map<String, FragmentMemoryReservation> fragments = queryMemoryReservations.get(queryId);
+    FragmentMemoryReservation fragment =
+        fragments == null ? null : fragments.get(fragmentInstanceId);
+    return fragment == null ? 0L : fragment.getUsedBytes(side).get();
+  }
+
+  public long getPlanNodeMemoryReservedBytes(
+      String queryId, String fragmentInstanceId, String planNodeId) {
+    Map<String, FragmentMemoryReservation> fragments = queryMemoryReservations.get(queryId);
+    FragmentMemoryReservation fragment =
+        fragments == null ? null : fragments.get(fragmentInstanceId);
+    PlanNodeReservation reservation = fragment == null ? null : fragment.planNodes.get(planNodeId);
+    return reservation == null ? 0L : reservation.bytesReserved;
+  }
+
   public long getReservedBytes() {
     return memoryBlock.getUsedMemoryInBytes();
   }
@@ -460,22 +628,64 @@ public class MemoryPool {
       String planNodeId,
       long bytesToReserve,
       long maxBytesCanReserve) {
-    long tryUsedBytes = memoryBlock.forceAllocateWithoutLimitation(bytesToReserve);
-    long queryRemainingBytes =
-        maxBytesCanReserve
-            - queryMemoryReservations
-                .get(queryId)
-                .get(fragmentInstanceId)
-                .merge(planNodeId, bytesToReserve, Long::sum);
-    return tryUsedBytes <= memoryBlock.getTotalMemorySizeInBytes() && queryRemainingBytes >= 0;
+    FragmentMemoryReservation fragment =
+        getFragmentReservation(queryId, fragmentInstanceId, planNodeId);
+    PlanNodeReservation reservation =
+        requireReservation(
+            fragment.planNodes.get(planNodeId), queryId, fragmentInstanceId, planNodeId);
+    // Legacy quotas are independent per plan node. Only shared quotas require the FI lock.
+    Object accountingLock = fragment.sharedBudget ? fragment : reservation;
+    synchronized (accountingLock) {
+      if (reservation.deregistered) {
+        return false;
+      }
+      long usedBytes =
+          fragment.sharedBudget
+              ? fragment.getUsedBytes(reservation.side).get()
+              : reservation.bytesReserved;
+      // Do not publish speculative charges. Two competing requests must not reject one another
+      // against different counters and then both wait after their rollbacks restore capacity.
+      if (bytesToReserve > maxBytesCanReserve - usedBytes
+          || !memoryBlock.allocate(bytesToReserve)) {
+        return false;
+      }
+      fragment.getUsedBytes(reservation.side).addAndGet(bytesToReserve);
+      reservation.bytesReserved += bytesToReserve;
+      return true;
+    }
   }
 
-  private void rollbackReserve(
-      String queryId, String fragmentInstanceId, String planNodeId, long bytesToReserve) {
-    queryMemoryReservations
-        .get(queryId)
-        .get(fragmentInstanceId)
-        .merge(planNodeId, -bytesToReserve, Long::sum);
-    memoryBlock.release(bytesToReserve);
+  private FragmentMemoryReservation getFragmentReservation(
+      String queryId, String fragmentInstanceId, String planNodeId) {
+    Map<String, FragmentMemoryReservation> fragments = queryMemoryReservations.get(queryId);
+    FragmentMemoryReservation fragment =
+        fragments == null ? null : fragments.get(fragmentInstanceId);
+    if (fragment == null) {
+      throw new IllegalArgumentException(
+          String.format(
+              DataNodeQueryMessages
+                  .EXCEPTION_MEMORY_RESERVATION_IS_NOT_REGISTERED_FOR_QUERY_ARG_FRAGMENT_ARG_PLAN_NODE_ARG_67A4B81F,
+              queryId,
+              fragmentInstanceId,
+              planNodeId));
+    }
+    return fragment;
+  }
+
+  private PlanNodeReservation requireReservation(
+      PlanNodeReservation reservation,
+      String queryId,
+      String fragmentInstanceId,
+      String planNodeId) {
+    if (reservation == null) {
+      throw new IllegalArgumentException(
+          String.format(
+              DataNodeQueryMessages
+                  .EXCEPTION_MEMORY_RESERVATION_IS_NOT_REGISTERED_FOR_QUERY_ARG_FRAGMENT_ARG_PLAN_NODE_ARG_67A4B81F,
+              queryId,
+              fragmentInstanceId,
+              planNodeId));
+    }
+    return reservation;
   }
 }
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/memory/MemorySide.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/memory/MemorySide.java
new file mode 100644
index 00000000000..71499605385
--- /dev/null
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/memory/MemorySide.java
@@ -0,0 +1,26 @@
+/*
+ * 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.iotdb.db.queryengine.execution.memory;
+
+/** The exchange budget charged by a registered plan node. */
+public enum MemorySide {
+  INPUT,
+  OUTPUT
+}
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/DataNodeTableOperatorGenerator.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/DataNodeTableOperatorGenerator.java
index cb2f6f02acb..2fae9aa5370 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/DataNodeTableOperatorGenerator.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/DataNodeTableOperatorGenerator.java
@@ -343,8 +343,7 @@ public class DataNodeTableOperatorGenerator
       Operator child = node.getChildren().get(0).accept(this, context);
       List<Operator> children = new ArrayList<>(1);
       children.add(child);
-      sinkHandle.setMaxBytesCanReserve(context.getMaxBytesOneHandleCanReserve());
-      context.getDriverContext().setSink(sinkHandle);
+      context.setISink(sinkHandle);
       return new IdentitySinkOperator(
           operatorContext, children, downStreamChannelIndex, sinkHandle);
     } else {
@@ -385,7 +384,6 @@ public class DataNodeTableOperatorGenerator
     if (!isSameNode) {
       context.addExchangeSumNum(1);
     }
-    sourceHandle.setMaxBytesCanReserve(context.getMaxBytesOneHandleCanReserve());
     ExchangeOperator exchangeOperator =
         new ExchangeOperator(operatorContext, sourceHandle, node.getUpstreamPlanNodeId());
     context.addExchangeOperator(exchangeOperator);
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/LocalExecutionPlanContext.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/LocalExecutionPlanContext.java
index 7a5f52749aa..ec93a08690c 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/LocalExecutionPlanContext.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/LocalExecutionPlanContext.java
@@ -32,8 +32,11 @@ import org.apache.iotdb.db.queryengine.execution.driver.DataDriverContext;
 import org.apache.iotdb.db.queryengine.execution.driver.DriverContext;
 import org.apache.iotdb.db.queryengine.execution.driver.SchemaDriverContext;
 import org.apache.iotdb.db.queryengine.execution.exchange.sink.ISink;
+import org.apache.iotdb.db.queryengine.execution.exchange.sink.ISinkHandle;
+import org.apache.iotdb.db.queryengine.execution.exchange.source.SourceHandle;
 import org.apache.iotdb.db.queryengine.execution.fragment.DataNodeQueryContext;
 import org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceContext;
+import org.apache.iotdb.db.queryengine.execution.memory.MemoryPool;
 import org.apache.iotdb.db.queryengine.execution.operator.source.ExchangeOperator;
 import org.apache.iotdb.db.queryengine.plan.analyze.TemplatedInfo;
 import org.apache.iotdb.db.queryengine.plan.analyze.TypeProvider;
@@ -41,6 +44,7 @@ import org.apache.iotdb.db.queryengine.plan.planner.memory.PipelineMemoryEstimat
 import org.apache.iotdb.db.schemaengine.schemaregion.ISchemaRegion;
 
 import org.apache.tsfile.common.conf.TSFileConfig;
+import org.apache.tsfile.common.conf.TSFileDescriptor;
 import org.apache.tsfile.enums.TSDataType;
 import org.apache.tsfile.file.metadata.IDeviceID;
 import org.apache.tsfile.read.TimeValuePair;
@@ -55,6 +59,7 @@ import javax.annotation.Nullable;
 import java.time.ZoneId;
 import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.IdentityHashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -71,6 +76,7 @@ import static java.util.Objects.requireNonNull;
 public class LocalExecutionPlanContext implements ITableOperatorGeneratorContext {
 
   private static final Logger LOGGER = LoggerFactory.getLogger(LocalExecutionPlanContext.class);
+
   // Save operators in this pipeline, a new one will be created when creating another pipeline
   private final DriverContext driverContext;
   private final AtomicInteger nextOperatorId;
@@ -83,6 +89,8 @@ public class LocalExecutionPlanContext implements ITableOperatorGeneratorContext
   private List<PipelineDriverFactory> pipelineDriverFactories;
   private List<ExchangeOperator> exchangeOperatorList = new ArrayList<>();
   private int exchangeSumNum = 0;
+  private final long fragmentExchangeMaxBytes;
+  private Map<ISink, Long> legacySinkLimits = new IdentityHashMap<>();
 
   private List<TSDataType> cachedDataTypes;
 
@@ -110,6 +118,8 @@ public class LocalExecutionPlanContext implements ITableOperatorGeneratorContext
       TypeProvider typeProvider,
       FragmentInstanceContext instanceContext,
       DataNodeQueryContext dataNodeQueryContext) {
+    this.fragmentExchangeMaxBytes =
+        IoTDBDescriptor.getInstance().getMemoryConfig().getMaxBytesPerFragmentInstance();
     this.typeProvider = typeProvider;
     this.allSensorsMap = new ConcurrentHashMap<>();
     this.nextOperatorId = new AtomicInteger(0);
@@ -121,6 +131,8 @@ public class LocalExecutionPlanContext implements ITableOperatorGeneratorContext
 
   // For creating subContext, differ from parent context mainly in driver context
   public LocalExecutionPlanContext(LocalExecutionPlanContext parentContext) {
+    this.fragmentExchangeMaxBytes = parentContext.fragmentExchangeMaxBytes;
+    this.legacySinkLimits = parentContext.legacySinkLimits;
     this.nextOperatorId = parentContext.nextOperatorId;
     this.typeProvider = parentContext.typeProvider;
     this.allSensorsMap = parentContext.allSensorsMap;
@@ -142,6 +154,8 @@ public class LocalExecutionPlanContext implements ITableOperatorGeneratorContext
       final TypeProvider typeProvider,
       final FragmentInstanceContext instanceContext,
       final ISchemaRegion schemaRegion) {
+    this.fragmentExchangeMaxBytes =
+        IoTDBDescriptor.getInstance().getMemoryConfig().getMaxBytesPerFragmentInstance();
     this.allSensorsMap = new ConcurrentHashMap<>();
     this.typeProvider = typeProvider;
     this.nextOperatorId = new AtomicInteger(0);
@@ -247,10 +261,11 @@ public class LocalExecutionPlanContext implements ITableOperatorGeneratorContext
     this.exchangeSumNum = exchangeSumNum;
   }
 
+  /** Original per-handle quota for tree planning and independent table FI accounting. */
   public long getMaxBytesOneHandleCanReserve() {
-    long maxBytesPerFI =
-        IoTDBDescriptor.getInstance().getMemoryConfig().getMaxBytesPerFragmentInstance();
-    return exchangeSumNum == 0 ? maxBytesPerFI : maxBytesPerFI / exchangeSumNum;
+    return exchangeSumNum == 0
+        ? fragmentExchangeMaxBytes
+        : fragmentExchangeMaxBytes / exchangeSumNum;
   }
 
   public void addExchangeSumNum(int addValue) {
@@ -273,6 +288,59 @@ public class LocalExecutionPlanContext implements ITableOperatorGeneratorContext
             exchangeOperator.getSourceHandle().setMaxBytesCanReserve(maxBytesOneHandleCanReserve));
   }
 
+  /** Called after operator generation, before root memory estimation and factory registration. */
+  public void setExchangeMemoryLimits(PlanNode plan, MemoryPool memoryPool) {
+    // Only remote sources charge this FI's INPUT. Local inputs are owned by the producing FI;
+    // internal pipeline queues cannot enable sharing because of the pipeline check below.
+    long inputExchangeCount =
+        exchangeOperatorList.stream()
+            .filter(exchange -> exchange.getSourceHandle() instanceof SourceHandle)
+            .count();
+    int outputSinkCount = legacySinkLimits.size();
+    long inputMaxBytes =
+        inputExchangeCount == 0
+            ? 0
+            : (outputSinkCount == 0 ? fragmentExchangeMaxBytes : fragmentExchangeMaxBytes / 2);
+    long outputMaxBytes = outputSinkCount == 0 ? 0 : fragmentExchangeMaxBytes - inputMaxBytes;
+    long maxBlockBytes = TSFileDescriptor.getInstance().getConfig().getMaxTsBlockSizeInBytes();
+    boolean sharedBudget =
+        (inputMaxBytes > 0 || outputMaxBytes > 0)
+            && SharedExchangeBudgetEligibility.isEligible(plan)
+            // Only the root pipeline may exist. Child factories are registered during generation;
+            // the root factory is registered after its memory has been estimated below.
+            && pipelineDriverFactories.isEmpty()
+            && driverContext.getDependencyDriverIndex() == -1
+            && (inputExchangeCount == 0 || inputMaxBytes >= maxBlockBytes)
+            && (outputSinkCount == 0 || outputMaxBytes >= maxBlockBytes);
+    FragmentInstanceContext instanceContext = driverContext.getFragmentInstanceContext();
+    instanceContext.initializeExchangeMemoryBudget(
+        sharedBudget ? inputMaxBytes : 0, sharedBudget ? outputMaxBytes : 0);
+    if (sharedBudget) {
+      memoryPool.registerFragmentInstanceSharedBudget(
+          instanceContext.getId().getQueryId().getId(),
+          instanceContext.getId().getFragmentInstanceId());
+      LOGGER.debug(
+          DataNodeQueryMessages.LOG_SHARED_EXCHANGE_BUDGET_FOR_FI_ARG_INPUT_ARG_OUTPUT_ARG_98AE1109,
+          instanceContext.getId(),
+          instanceContext.getInputMaxBytes(),
+          instanceContext.getOutputMaxBytes());
+    } else {
+      LOGGER.debug(
+          DataNodeQueryMessages
+              .MAXBYTESONEHANDLECANRESERVE_FOR_EXCHANGEOPERATOR_IS_ARG_EXCHANGESUMNUM_IS_ARG,
+          getMaxBytesOneHandleCanReserve(),
+          exchangeSumNum);
+    }
+    legacySinkLimits.forEach(
+        (sink, legacyLimit) ->
+            sink.setMaxBytesCanReserve(
+                sharedBudget ? instanceContext.getOutputMaxBytes() : legacyLimit));
+    long sourceLimit =
+        sharedBudget ? instanceContext.getInputMaxBytes() : getMaxBytesOneHandleCanReserve();
+    exchangeOperatorList.forEach(
+        exchangeOperator -> exchangeOperator.getSourceHandle().setMaxBytesCanReserve(sourceLimit));
+  }
+
   public Set<String> getAllSensors(IDeviceID deviceId, String sensorId) {
     Set<String> allSensors = allSensorsMap.computeIfAbsent(deviceId, k -> new HashSet<>());
     allSensors.add(sensorId);
@@ -303,6 +371,9 @@ public class LocalExecutionPlanContext implements ITableOperatorGeneratorContext
     checkArgument(
         driverContext.getSink() == null,
         DataNodeQueryMessages.EXCEPTION_THERE_MUST_BE_AT_MOST_ONE_SINKNODE_A965AFE7);
+    if (sink instanceof ISinkHandle) {
+      legacySinkLimits.put(sink, getMaxBytesOneHandleCanReserve());
+    }
     driverContext.setSink(sink);
   }
 
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/LocalExecutionPlanner.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/LocalExecutionPlanner.java
index feb6b0cb74a..c600b4eb901 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/LocalExecutionPlanner.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/LocalExecutionPlanner.java
@@ -35,6 +35,7 @@ import org.apache.iotdb.db.conf.IoTDBDescriptor;
 import org.apache.iotdb.db.i18n.DataNodeQueryMessages;
 import org.apache.iotdb.db.queryengine.common.DeviceContext;
 import org.apache.iotdb.db.queryengine.execution.driver.DataDriverContext;
+import org.apache.iotdb.db.queryengine.execution.exchange.MPPDataExchangeService;
 import org.apache.iotdb.db.queryengine.execution.fragment.DataNodeQueryContext;
 import org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceContext;
 import org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceStateMachine;
@@ -115,6 +116,9 @@ public class LocalExecutionPlanner {
 
     Operator root = generateOperator(instanceContext, context, plan);
 
+    // Select Collect consumption before estimating its retained input memory.
+    setExchangeMemoryLimits(plan, context);
+
     PipelineMemoryEstimator memoryEstimator =
         context.constructPipelineMemoryEstimator(root, null, plan, -1);
     // set the map to null for gc
@@ -132,9 +136,6 @@ public class LocalExecutionPlanner {
 
     context.getTimePartitions().ifPresent(instanceContext::setTimePartitions);
 
-    // set maxBytes one SourceHandle can reserve after visiting the whole tree
-    context.setMaxBytesOneHandleCanReserve();
-
     return context.getPipelineDriverFactories();
   }
 
@@ -155,6 +156,9 @@ public class LocalExecutionPlanner {
 
     Operator root = generateOperator(instanceContext, context, plan);
 
+    // Select Collect consumption before estimating its retained input memory.
+    setExchangeMemoryLimits(plan, context);
+
     PipelineMemoryEstimator memoryEstimator =
         context.constructPipelineMemoryEstimator(root, null, plan, -1);
     // set the map to null for gc
@@ -165,9 +169,6 @@ public class LocalExecutionPlanner {
 
     context.addPipelineDriverFactory(root, context.getDriverContext(), 0);
 
-    // set maxBytes one SourceHandle can reserve after visiting the whole tree
-    context.setMaxBytesOneHandleCanReserve();
-
     return context.getPipelineDriverFactories();
   }
 
@@ -196,6 +197,22 @@ public class LocalExecutionPlanner {
     return root;
   }
 
+  private void setExchangeMemoryLimits(PlanNode plan, LocalExecutionPlanContext context) {
+    FragmentInstanceContext instanceContext = context.getInstanceContext();
+    if (instanceContext.getSessionInfo() == null
+        || instanceContext.getSessionInfo().getSqlDialect() == TREE) {
+      context.setMaxBytesOneHandleCanReserve();
+      return;
+    }
+    // Choose the table FI policy only after all operators and pipelines are known.
+    context.setExchangeMemoryLimits(
+        plan,
+        MPPDataExchangeService.getInstance()
+            .getMPPDataExchangeManager()
+            .getLocalMemoryManager()
+            .getQueryPool());
+  }
+
   private long checkMemory(
       final PipelineMemoryEstimator memoryEstimator, FragmentInstanceContext instanceContext)
       throws MemoryNotEnoughException {
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/SharedExchangeBudgetEligibility.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/SharedExchangeBudgetEligibility.java
new file mode 100644
index 00000000000..5c9d78291dd
--- /dev/null
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/SharedExchangeBudgetEligibility.java
@@ -0,0 +1,70 @@
+/*
+ * 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.iotdb.db.queryengine.plan.planner;
+
+import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNode;
+import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.CollectNode;
+import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.TopKNode;
+import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.UnionNode;
+import org.apache.iotdb.db.queryengine.plan.relational.planner.node.ExchangeNode;
+
+import java.util.ArrayDeque;
+import java.util.Deque;
+import java.util.List;
+
+/**
+ * Decides whether a fragment may share its exchange budget across INPUT and OUTPUT sides. Each
+ * fragment contains at most one multi-child level: either a Collect/Union fan-in, whose ready-first
+ * consumption can borrow idle capacity from any child, or an operator that waits for all of its
+ * inputs before advancing, which a shared budget could starve. Ordering plays no part in this
+ * decision; consumers that require ordered input are handled by the planner.
+ */
+public final class SharedExchangeBudgetEligibility {
+  private SharedExchangeBudgetEligibility() {}
+
+  public static boolean isEligible(PlanNode root) {
+    boolean hasCollect = false;
+    int multiChildNodeCount = 0;
+    Deque<PlanNode> nodes = new ArrayDeque<>();
+    nodes.push(root);
+    while (!nodes.isEmpty()) {
+      PlanNode node = nodes.pop();
+      if (node instanceof ExchangeNode) {
+        // An upstream plan, if still attached, belongs to a different FI.
+        continue;
+      }
+      // TopK's input ordering assumptions remain unchanged, including its single-child form.
+      if (node instanceof TopKNode) {
+        return false;
+      }
+      boolean anyReady = node instanceof CollectNode || node instanceof UnionNode;
+      hasCollect |= anyReady;
+      List<PlanNode> children = node.getChildren();
+      if (children.size() > 1) {
+        // A fragment is expected to contain at most one multi-child level. Keep the fallback
+        // conservative if a future plan shape violates that invariant.
+        if (++multiChildNodeCount > 1 || !anyReady) {
+          return false;
+        }
+      }
+      nodes.addAll(children);
+    }
+    return hasCollect;
+  }
+}
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/distribute/TableDistributedPlanGenerator.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/distribute/TableDistributedPlanGenerator.java
index 66f6da892f3..1ead17632aa 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/distribute/TableDistributedPlanGenerator.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/distribute/TableDistributedPlanGenerator.java
@@ -689,6 +689,12 @@ public class TableDistributedPlanGenerator
     for (PlanNode child : childrenNodes) {
       if (canSortEliminated(node.getOrderingScheme(), nodeOrderingMap.get(child.getPlanNodeId()))) {
         result.add(child);
+      } else if (groupingInvalidatedByCollect(
+          child, node.getOrderingScheme().getOrderBy().subList(0, node.getPartitionKeyCount()))) {
+        SortNode sort =
+            new SortNode(queryId.genPlanNodeId(), child, node.getOrderingScheme(), false, false);
+        nodeOrderingMap.put(sort.getPlanNodeId(), sort.getOrderingScheme());
+        result.add(sort);
       } else {
         GroupNode subGroupNode =
             new GroupNode(
@@ -1100,7 +1106,7 @@ public class TableDistributedPlanGenerator
     if (needFinalAggregation) {
       OrderingScheme childOrdering = nodeOrderingMap.get(result.get(0).getPlanNodeId());
       finalAggregation.setChild(mergeChildrenViaCollectOrMergeSort(childOrdering, result));
-      return Collections.singletonList(finalAggregation);
+      return Collections.singletonList(validateAggregationGrouping(finalAggregation));
     }
     return result;
   }
@@ -2118,7 +2124,7 @@ public class TableDistributedPlanGenerator
                       if (physicalAggregation.isStreamable() && childOrdering != null) {
                         nodeOrderingMap.put(planNodeId, expectedOrderingSchema);
                       }
-                      return aggregationNode;
+                      return validateAggregationGrouping(aggregationNode);
                     })
                 .collect(Collectors.toList());
 
@@ -2128,10 +2134,10 @@ public class TableDistributedPlanGenerator
             new CollectNode(queryId.genPlanNodeId(), aggregationNodes.get(0).getOutputSymbols());
         collectNode.setChildren(aggregationNodes);
         splitResult.left.setChild(collectNode);
-        return Collections.singletonList(splitResult.left);
+        return Collections.singletonList(validateAggregationGrouping(splitResult.left));
       }
 
-      return Collections.singletonList(physicalAggregation);
+      return Collections.singletonList(validateAggregationGrouping(physicalAggregation));
     }
 
     // We cannot do multi-stage Aggregate if any aggregation-function is distinct.
@@ -2144,7 +2150,9 @@ public class TableDistributedPlanGenerator
               nodeOrderingMap.get(childrenNodes.get(0).getPlanNodeId()), childrenNodes);
       node.setChild(physicalChild);
       return Collections.singletonList(
-          withRateFunctionInputOrdering(node, nodeOrderingMap.get(physicalChild.getPlanNodeId())));
+          validateAggregationGrouping(
+              withRateFunctionInputOrdering(
+                  node, nodeOrderingMap.get(physicalChild.getPlanNodeId()))));
     }
     Pair<AggregationNode, AggregationNode> splitResult = split(node, symbolAllocator, queryId);
     AggregationNode intermediate = splitResult.right;
@@ -2167,13 +2175,73 @@ public class TableDistributedPlanGenerator
                   if (node.isStreamable() && childOrdering != null) {
                     nodeOrderingMap.put(planNodeId, expectedOrderingSchema);
                   }
-                  return aggregationNode;
+                  return validateAggregationGrouping(aggregationNode);
                 })
             .collect(Collectors.toList());
     splitResult.left.setChild(
         mergeChildrenViaCollectOrMergeSort(
             nodeOrderingMap.get(childrenNodes.get(0).getPlanNodeId()), childrenNodes));
-    return Collections.singletonList(splitResult.left);
+    return Collections.singletonList(validateAggregationGrouping(splitResult.left));
+  }
+
+  private AggregationNode validateAggregationGrouping(AggregationNode node) {
+    if (!node.getPreGroupedSymbols().isEmpty()
+        && groupingInvalidatedByCollect(node.getChild(), node.getPreGroupedSymbols())) {
+      node.setPreGroupedSymbols(ImmutableList.of());
+      nodeOrderingMap.remove(node.getPlanNodeId());
+    }
+    return node;
+  }
+
+  /** Whether an unordered fan-in invalidates a previously established grouping property. */
+  private boolean groupingInvalidatedByCollect(PlanNode node, List<Symbol> grouping) {
+    if (grouping.isEmpty()) {
+      return false;
+    }
+    if ((node instanceof CollectNode || node instanceof UnionNode)
+        && node.getChildren().size() > 1) {
+      return true;
+    }
+    OrderingScheme ordering = nodeOrderingMap.get(node.getPlanNodeId());
+    if (ordering != null && prefixMatched(ordering, grouping)) {
+      return false;
+    }
+    // A completed aggregation emits each full grouping key once, even when it hashes its input.
+    if (node instanceof AggregationNode) {
+      AggregationNode aggregation = (AggregationNode) node;
+      if (!aggregation.getStep().isOutputPartial()
+          && aggregation.getGroupingSetCount() == 1
+          && grouping.containsAll(aggregation.getGroupingKeys())) {
+        return false;
+      }
+    }
+    if (node instanceof ProjectNode) {
+      ProjectNode project = (ProjectNode) node;
+      List<Symbol> inputs = new ArrayList<>();
+      for (Symbol symbol : grouping) {
+        Expression expression = project.getAssignments().get(symbol);
+        if (!(expression instanceof SymbolReference)) {
+          // A computed key cannot inherit a branch-local continuity proof.
+          return containsUnorderedCollect(project.getChild());
+        }
+        inputs.add(Symbol.from(expression));
+      }
+      return groupingInvalidatedByCollect(project.getChild(), inputs);
+    }
+    for (PlanNode child : node.getChildren()) {
+      if (groupingInvalidatedByCollect(child, grouping)) {
+        return true;
+      }
+    }
+    return false;
+  }
+
+  private boolean containsUnorderedCollect(PlanNode node) {
+    if ((node instanceof CollectNode || node instanceof UnionNode)
+        && node.getChildren().size() > 1) {
+      return true;
+    }
+    return node.getChildren().stream().anyMatch(this::containsUnorderedCollect);
   }
 
   private static AggregationNode withRateFunctionInputOrdering(
@@ -2301,7 +2369,8 @@ public class TableDistributedPlanGenerator
         throw new IllegalStateException(
             DataNodeQueryMessages.LIST_PLANNODE_SIZE_SHOULD_1_BUT_NOW_IS);
       }
-      resultTableScanNodeList = Collections.singletonList(finalAggregation);
+      resultTableScanNodeList =
+          Collections.singletonList(validateAggregationGrouping(finalAggregation));
     }
 
     return resultTableScanNodeList;
@@ -2460,7 +2529,7 @@ public class TableDistributedPlanGenerator
             mergeChildrenViaCollectOrMergeSort(
                 nodeOrderingMap.get(result.get(0).getPlanNodeId()), result));
       }
-      return Collections.singletonList(finalAggregation);
+      return Collections.singletonList(validateAggregationGrouping(finalAggregation));
     }
     return result;
   }
@@ -2677,7 +2746,8 @@ public class TableDistributedPlanGenerator
         throw new IllegalStateException(
             DataNodeQueryMessages.LIST_PLANNODE_SIZE_SHOULD_1_BUT_NOW_IS);
       }
-      resultTableScanNodeList = Collections.singletonList(finalAggregation);
+      resultTableScanNodeList =
+          Collections.singletonList(validateAggregationGrouping(finalAggregation));
     }
 
     return resultTableScanNodeList;
@@ -2863,7 +2933,7 @@ public class TableDistributedPlanGenerator
             mergeChildrenViaCollectOrMergeSort(
                 nodeOrderingMap.get(result.get(0).getPlanNodeId()), result));
       }
-      return Collections.singletonList(finalAggregation);
+      return Collections.singletonList(validateAggregationGrouping(finalAggregation));
     }
     return result;
   }
@@ -4007,6 +4077,9 @@ public class TableDistributedPlanGenerator
     public void clearExpectedOrderingScheme() {
       expectedOrderingScheme = null;
       hasSortProperty = false;
+      // Grouping pushdown depends on this ordering. A boundary such as a global window
+      // must not pass an outer Group's partitioning request to its scans.
+      pushDownGrouping = false;
     }
 
     public void setExpectedOrderingScheme(OrderingScheme expectedOrderingScheme) {
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/ParallelizeGrouping.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/ParallelizeGrouping.java
index 66c4eff6e3e..ac3fc65fe66 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/ParallelizeGrouping.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/ParallelizeGrouping.java
@@ -34,6 +34,7 @@ import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.SortNod
 import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.StreamSortNode;
 import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.TableFunctionProcessorNode;
 import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.TopKNode;
+import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.UnionNode;
 import org.apache.iotdb.db.queryengine.plan.planner.plan.node.PlanVisitor;
 import org.apache.iotdb.db.queryengine.plan.relational.analyzer.Analysis;
 import org.apache.iotdb.db.queryengine.plan.relational.planner.node.AggregationTableScanNode;
@@ -101,6 +102,21 @@ public class ParallelizeGrouping implements PlanOptimizer {
       return newNode;
     }
 
+    @Override
+    public PlanNode visitUnion(UnionNode node, Context context) {
+      if (node.getChildren().size() <= 1) {
+        return visitPlan(node, context);
+      }
+      if (!context.canSkip()) {
+        context.canOptimized = TO_SORT;
+      }
+      // Neither branch may establish a property for the interleaved Union output.
+      return node.replaceChildren(
+          node.getChildren().stream()
+              .map(child -> child.accept(this, new Context(null, 0)))
+              .collect(Collectors.toList()));
+    }
+
     /** We need to make sure: context#partitionKey can match the prefix of childOrderSchema */
     private void checkPrefixMatch(Context context, List<Symbol> childOrder) {
       if (context.canSkip()) {
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/SortElimination.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/SortElimination.java
index 64ef1b8c2ad..c35f6612805 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/SortElimination.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/SortElimination.java
@@ -22,6 +22,7 @@ package org.apache.iotdb.db.queryengine.plan.relational.planner.optimizations;
 import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNode;
 import org.apache.iotdb.commons.queryengine.plan.relational.planner.OrderingScheme;
 import org.apache.iotdb.commons.queryengine.plan.relational.planner.Symbol;
+import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.CollectNode;
 import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.FillNode;
 import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.GapFillNode;
 import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.PatternRecognitionNode;
@@ -31,6 +32,7 @@ import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.SortNod
 import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.StreamSortNode;
 import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.TableFunctionProcessorNode;
 import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.TopKRankingNode;
+import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.UnionNode;
 import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.WindowNode;
 import org.apache.iotdb.db.queryengine.plan.planner.plan.node.PlanVisitor;
 import org.apache.iotdb.db.queryengine.plan.relational.planner.iterative.rule.PruneTableScanColumns;
@@ -38,6 +40,7 @@ import org.apache.iotdb.db.queryengine.plan.relational.planner.node.DeviceTableS
 
 import com.google.common.collect.ImmutableSet;
 
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.Optional;
@@ -75,6 +78,28 @@ public class SortElimination implements PlanOptimizer {
       return newNode;
     }
 
+    @Override
+    public PlanNode visitCollect(CollectNode node, Context context) {
+      return visitUnorderedFanIn(node, context);
+    }
+
+    @Override
+    public PlanNode visitUnion(UnionNode node, Context context) {
+      return visitUnorderedFanIn(node, context);
+    }
+
+    private PlanNode visitUnorderedFanIn(PlanNode node, Context context) {
+      if (node.getChildren().size() <= 1) {
+        return visitPlan(node, context);
+      }
+      List<PlanNode> children = new ArrayList<>();
+      for (PlanNode child : node.getChildren()) {
+        children.add(child.accept(this, new Context()));
+      }
+      context.addCannotEliminateSort(true);
+      return node.replaceChildren(children);
+    }
+
     @Override
     public PlanNode visitProject(ProjectNode node, Context context) {
       Context newContext = new Context();
diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/MPPDataExchangeManagerTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/MPPDataExchangeManagerTest.java
index ce491042481..a5573159a94 100644
--- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/MPPDataExchangeManagerTest.java
+++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/MPPDataExchangeManagerTest.java
@@ -40,9 +40,11 @@ import org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceState;
 import org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceStateMachine;
 import org.apache.iotdb.db.queryengine.execution.memory.LocalMemoryManager;
 import org.apache.iotdb.db.queryengine.execution.memory.MemoryPool;
+import org.apache.iotdb.db.queryengine.execution.memory.MemorySide;
 import org.apache.iotdb.mpp.rpc.thrift.TFragmentInstanceId;
 import org.apache.iotdb.mpp.rpc.thrift.TNewDataBlockEvent;
 
+import org.apache.tsfile.read.common.block.TsBlock;
 import org.junit.Assert;
 import org.junit.Test;
 import org.mockito.Mockito;
@@ -180,6 +182,13 @@ public class MPPDataExchangeManagerTest {
     Assert.assertEquals(
         ((LocalSinkChannel) shuffleSinkHandle.getChannel(0)).getSharedTsBlockQueue(),
         ((LocalSourceHandle) localSourceHandle).getSharedTsBlockQueue());
+    assertProducerOwnsQueue(
+        spyMemoryPool,
+        shuffleSinkHandle,
+        localSourceHandle,
+        localFragmentInstanceId,
+        remoteFragmentInstanceId,
+        localPlanNodeId);
   }
 
   @Test
@@ -237,5 +246,49 @@ public class MPPDataExchangeManagerTest {
     Assert.assertEquals(
         ((LocalSinkChannel) shuffleSinkHandle.getChannel(0)).getSharedTsBlockQueue(),
         ((LocalSourceHandle) localSourceHandle).getSharedTsBlockQueue());
+    assertProducerOwnsQueue(
+        spyMemoryPool,
+        shuffleSinkHandle,
+        localSourceHandle,
+        localFragmentInstanceId,
+        remoteFragmentInstanceId,
+        localPlanNodeId);
+  }
+
+  private static void assertProducerOwnsQueue(
+      MemoryPool pool,
+      ISinkHandle sink,
+      ISourceHandle source,
+      TFragmentInstanceId producer,
+      TFragmentInstanceId consumer,
+      String planNodeId) {
+    SharedTsBlockQueue queue = ((LocalSourceHandle) source).getSharedTsBlockQueue();
+    sink.setMaxBytesCanReserve(512);
+    source.setMaxBytesCanReserve(128);
+    Assert.assertEquals(512, queue.getMaxBytesCanReserve());
+    String producerId =
+        FragmentInstanceId.createFragmentInstanceIdFromTFragmentInstanceId(producer);
+    String consumerId =
+        FragmentInstanceId.createFragmentInstanceIdFromTFragmentInstanceId(consumer);
+    TsBlock block = Utils.createMockTsBlock(256);
+    synchronized (queue) {
+      Assert.assertTrue(queue.add(block).isDone());
+      Assert.assertEquals(256, pool.getReservedBytes());
+      Assert.assertEquals(
+          256, pool.getPlanNodeMemoryReservedBytes(producer.queryId, producerId, planNodeId));
+      Assert.assertEquals(
+          256,
+          pool.getFragmentMemoryReservedBytes(producer.queryId, producerId, MemorySide.OUTPUT));
+      Assert.assertEquals(
+          0, pool.getFragmentMemoryReservedBytes(producer.queryId, producerId, MemorySide.INPUT));
+      Assert.assertEquals(
+          0, pool.getFragmentMemoryReservedBytes(consumer.queryId, consumerId, MemorySide.INPUT));
+      Assert.assertEquals(
+          0, pool.getFragmentMemoryReservedBytes(consumer.queryId, consumerId, MemorySide.OUTPUT));
+      Assert.assertSame(block, queue.remove());
+      Assert.assertEquals(0, pool.getReservedBytes());
+      Assert.assertEquals(
+          0, pool.getFragmentMemoryReservedBytes(producer.queryId, producerId, MemorySide.OUTPUT));
+    }
   }
 }
diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/SharedTsBlockQueueTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/SharedTsBlockQueueTest.java
index a92eaa5d0e2..391393cf5fa 100644
--- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/SharedTsBlockQueueTest.java
+++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/SharedTsBlockQueueTest.java
@@ -20,9 +20,11 @@
 package org.apache.iotdb.db.queryengine.execution.exchange;
 
 import org.apache.iotdb.commons.memory.MemoryManager;
+import org.apache.iotdb.db.queryengine.common.FragmentInstanceId;
 import org.apache.iotdb.db.queryengine.execution.memory.LocalMemoryManager;
 import org.apache.iotdb.db.queryengine.execution.memory.MemoryPool;
 import org.apache.iotdb.db.queryengine.execution.memory.MemoryPool.MemoryReservationResult;
+import org.apache.iotdb.db.queryengine.execution.memory.MemorySide;
 import org.apache.iotdb.mpp.rpc.thrift.TFragmentInstanceId;
 
 import com.google.common.util.concurrent.ListenableFuture;
@@ -254,4 +256,45 @@ public class SharedTsBlockQueueTest {
       }
     }
   }
+
+  @Test
+  public void testDependentQueuesKeepIndependentBudgetsByDefault() {
+    MemoryPool pool = new MemoryPool("pipeline-chain", new MemoryManager(1024), 512);
+    LocalMemoryManager manager = Mockito.mock(LocalMemoryManager.class);
+    Mockito.when(manager.getQueryPool()).thenReturn(pool);
+    ExecutorService executor = newDirectExecutorService();
+    TFragmentInstanceId id = new TFragmentInstanceId("pipeline-chain", 0, "0");
+    SharedTsBlockQueue upstream = new SharedTsBlockQueue(id, "a", manager, executor);
+    SharedTsBlockQueue downstream = new SharedTsBlockQueue(id, "b", manager, executor);
+    upstream.setMaxBytesCanReserve(256);
+    downstream.setMaxBytesCanReserve(256);
+    TsBlock block = Utils.createMockTsBlock(64);
+    try {
+      for (int i = 0; i < 4; i++) Assert.assertTrue(upstream.add(block).isDone());
+      ListenableFuture<Void> fifth = upstream.add(block);
+      Assert.assertFalse(fifth.isDone());
+      TsBlock forwarded = upstream.remove();
+      Assert.assertTrue(fifth.isDone());
+      Assert.assertTrue(downstream.add(forwarded).isDone());
+      Assert.assertEquals(4, upstream.getNumOfBufferedTsBlocks());
+      Assert.assertEquals(1, downstream.getNumOfBufferedTsBlocks());
+      Assert.assertEquals(320, pool.getReservedBytes());
+      // Both queues are producer-side OUTPUT buffers of the same fragment instance.
+      String fullFragmentInstanceId =
+          FragmentInstanceId.createFragmentInstanceIdFromTFragmentInstanceId(id);
+      Assert.assertEquals(
+          320,
+          pool.getFragmentMemoryReservedBytes(
+              id.queryId, fullFragmentInstanceId, MemorySide.OUTPUT));
+      Assert.assertEquals(
+          0,
+          pool.getFragmentMemoryReservedBytes(
+              id.queryId, fullFragmentInstanceId, MemorySide.INPUT));
+    } finally {
+      downstream.close();
+      upstream.close();
+      executor.shutdownNow();
+    }
+    Assert.assertEquals(0, pool.getReservedBytes());
+  }
 }
diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/SourceHandleTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/SourceHandleTest.java
index 2d9e5683cfd..95c9b810ce1 100644
--- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/SourceHandleTest.java
+++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/SourceHandleTest.java
@@ -76,6 +76,70 @@ public class SourceHandleTest {
     IoTDBDescriptor.getInstance().getMemoryConfig().setMaxBytesPerFragmentInstance(maxBytesPerFI);
   }
 
+  @Test
+  public void testPendingHandleCanCloseAfterForcedMemoryDeregistration() throws Exception {
+    for (int closeMode = 0; closeMode < 3; closeMode++) {
+      MemoryPool memoryPool =
+          new MemoryPool("pending", new MemoryManager(MOCK_TSBLOCK_SIZE), MOCK_TSBLOCK_SIZE);
+      memoryPool.registerPlanNodeIdToQueryMemoryMap(
+          "holder",
+          "0.0",
+          "holder",
+          org.apache.iotdb.db.queryengine.execution.memory.MemorySide.INPUT);
+      Assert.assertTrue(
+          memoryPool.tryReserve("holder", "0.0", "holder", MOCK_TSBLOCK_SIZE, MOCK_TSBLOCK_SIZE));
+      LocalMemoryManager manager = Mockito.mock(LocalMemoryManager.class);
+      Mockito.when(manager.getQueryPool()).thenReturn(memoryPool);
+      ExecutorService executor = Mockito.mock(ExecutorService.class);
+      Mockito.doAnswer(
+              invocation -> {
+                ((Runnable) invocation.getArgument(0)).run();
+                return null;
+              })
+          .when(executor)
+          .execute(Mockito.any(Runnable.class));
+      SourceHandleListener listener = Mockito.mock(SourceHandleListener.class);
+      SourceHandle source =
+          new SourceHandle(
+              new TEndPoint("remote", 12345),
+              new TFragmentInstanceId("pending", 1, "0"),
+              new TFragmentInstanceId("pending", 0, "0"),
+              "src",
+              0,
+              manager,
+              executor,
+              new TsBlockSerde(),
+              listener,
+              Mockito.mock(IClientManager.class));
+      source.isBlocked();
+      source.updatePendingDataBlockInfo(0, List.of(MOCK_TSBLOCK_SIZE));
+      Assert.assertEquals(1, memoryPool.getMemoryReservationSize());
+      memoryPool.deRegisterFragmentInstanceFromQueryMemoryMap("pending", "0.0", true);
+      // Detachment must not schedule an RPC for an unreserved block.
+      Mockito.verify(executor, Mockito.never()).execute(Mockito.any(Runnable.class));
+      Mockito.verify(executor, Mockito.never()).submit(Mockito.any(Runnable.class));
+      memoryPool.free("holder", "0.0", "holder", MOCK_TSBLOCK_SIZE);
+      if (closeMode == 0) {
+        source.abort();
+      } else if (closeMode == 1) {
+        source.abort(new RuntimeException("cancel query"));
+      } else {
+        source.close();
+      }
+      Assert.assertEquals(0, source.getBufferRetainedSizeInBytes());
+      Assert.assertEquals(0, memoryPool.getMemoryReservationSize());
+      Assert.assertEquals(0, memoryPool.getReservedBytes());
+      if (closeMode < 2) {
+        Assert.assertTrue(source.isAborted());
+        Mockito.verify(listener).onAborted(source);
+      } else {
+        Mockito.verify(listener).onFinished(source);
+      }
+      memoryPool.deRegisterFragmentInstanceFromQueryMemoryMap("holder", "0.0", true);
+      Assert.assertEquals(0, memoryPool.getQueryMemoryReservationSize());
+    }
+  }
+
   @Test
   public void testCloseStopsFragmentFetchAfterInFlightRpc() throws Exception {
     testCancellationStopsFragmentFetchAfterInFlightRpc(false);
diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/memory/MemoryPoolTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/memory/MemoryPoolTest.java
index 5f2285e4ca9..ae0f8ba3257 100644
--- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/memory/MemoryPoolTest.java
+++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/memory/MemoryPoolTest.java
@@ -32,8 +32,23 @@ import org.mockito.Mockito;
 import java.lang.management.ManagementFactory;
 import java.lang.management.ThreadInfo;
 import java.lang.reflect.Field;
+import java.util.AbstractCollection;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.ConcurrentSkipListMap;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
 import java.util.concurrent.FutureTask;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import static org.awaitility.Awaitility.await;
 
@@ -50,7 +65,8 @@ public class MemoryPoolTest {
   public void before() {
     MemoryManager memoryManager = Mockito.spy(new MemoryManager(1024L));
     pool = new MemoryPool("test", memoryManager, 512L);
-    pool.registerPlanNodeIdToQueryMemoryMap(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID);
+    pool.registerPlanNodeIdToQueryMemoryMap(
+        QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, MemorySide.INPUT);
   }
 
   @Test
@@ -87,7 +103,7 @@ public class MemoryPoolTest {
         // Replace the query map while the delayed cleanup still refers to the old one.
         pool.deRegisterFragmentInstanceFromQueryMemoryMap(QUERY_ID, FRAGMENT_INSTANCE_ID, true);
         Assert.assertEquals(0, pool.getQueryMemoryReservationSize());
-        pool.registerPlanNodeIdToQueryMemoryMap(QUERY_ID, "result", PLAN_NODE_ID);
+        pool.registerPlanNodeIdToQueryMemoryMap(QUERY_ID, "result", PLAN_NODE_ID, MemorySide.INPUT);
         Assert.assertTrue(
             pool.tryReserveForTest(QUERY_ID, "result", PLAN_NODE_ID, 256L, Long.MAX_VALUE));
       }
@@ -123,7 +139,7 @@ public class MemoryPoolTest {
 
   @Test
   public void testDeregisterPreservesOtherFragments() {
-    pool.registerPlanNodeIdToQueryMemoryMap(QUERY_ID, "other", PLAN_NODE_ID);
+    pool.registerPlanNodeIdToQueryMemoryMap(QUERY_ID, "other", PLAN_NODE_ID, MemorySide.INPUT);
     Assert.assertTrue(
         pool.tryReserveForTest(QUERY_ID, "other", PLAN_NODE_ID, 256L, Long.MAX_VALUE));
 
@@ -172,6 +188,371 @@ public class MemoryPoolTest {
     Assert.assertEquals(256L, pool.getReservedBytes());
   }
 
+  @Test
+  public void testDeregisterDetachesPendingReservationsBeforeUnrelatedFree() {
+    pool.registerPlanNodeIdToQueryMemoryMap("healthy", "fi", "a", MemorySide.INPUT);
+    pool.registerPlanNodeIdToQueryMemoryMap("healthy", "fi", "b", MemorySide.INPUT);
+    Assert.assertTrue(pool.tryReserve("healthy", "fi", "a", 512, 512));
+    Assert.assertTrue(pool.tryReserve("healthy", "fi", "b", 512, 512));
+    MemoryReservationResult pending =
+        pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 256, 512, false);
+    Assert.assertFalse(pending.getFuture().isDone());
+
+    pool.deRegisterFragmentInstanceFromQueryMemoryMap(QUERY_ID, FRAGMENT_INSTANCE_ID, true);
+
+    Assert.assertFalse(pending.getFuture().isDone());
+    Assert.assertEquals(256, pool.tryCancel(pending.getFuture()));
+    pool.free("healthy", "fi", "a", 512);
+    pool.free("healthy", "fi", "b", 512);
+    Assert.assertEquals(0, pool.getReservedBytes());
+  }
+
+  @Test
+  public void testForceDeregisterDetachesPendingReservationsEvenWhenReportingLeak() {
+    Assert.assertTrue(pool.tryReserve(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 512, 512));
+    MemoryReservationResult pending =
+        pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 256, 512, false);
+    pool.registerPlanNodeIdToQueryMemoryMap("healthy", "fi", "a", MemorySide.INPUT);
+    Assert.assertTrue(pool.tryReserve("healthy", "fi", "a", 256, 512));
+
+    Assert.assertThrows(
+        MemoryLeakException.class,
+        () ->
+            pool.deRegisterFragmentInstanceFromQueryMemoryMap(
+                QUERY_ID, FRAGMENT_INSTANCE_ID, true));
+
+    Assert.assertFalse(pending.getFuture().isDone());
+    Assert.assertEquals(256, pool.tryCancel(pending.getFuture()));
+    pool.free("healthy", "fi", "a", 256);
+    // Forced cleanup still reports and retains the original leaked allocation.
+    Assert.assertEquals(512, pool.getReservedBytes());
+  }
+
+  @Test
+  @SuppressWarnings("unchecked")
+  public void testPublicationIntoDeregisteredFragmentIsRejected() {
+    pool.registerPlanNodeIdToQueryMemoryMap(
+        QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, MemorySide.INPUT);
+    pool.markFragmentDeregisteredForTest(QUERY_ID, FRAGMENT_INSTANCE_ID);
+    // The account is still in the map but marked deregistered, so publication must be refused
+    // without charging any bytes.
+    Assert.assertThrows(
+        IllegalArgumentException.class,
+        () ->
+            pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 32, 512, false));
+    Assert.assertEquals(0, pool.getReservedBytes());
+    Assert.assertEquals(0, pool.getMemoryReservationSize());
+    pool.deRegisterFragmentInstanceFromQueryMemoryMap(QUERY_ID, FRAGMENT_INSTANCE_ID, true);
+  }
+
+  @Test
+  public void testDeregistrationDoesNotPoisonOtherQueries() throws Exception {
+    // Fill the pool from an unrelated fragment so that both later waiters block on capacity.
+    pool.registerPlanNodeIdToQueryMemoryMap("other", "fi", "o1", MemorySide.INPUT);
+    pool.registerPlanNodeIdToQueryMemoryMap("other", "fi", "o2", MemorySide.INPUT);
+    Assert.assertTrue(pool.tryReserve("other", "fi", "o1", 512, 512));
+    Assert.assertTrue(pool.tryReserve("other", "fi", "o2", 512, 512));
+    // A pending waiter in the fragment about to be deregistered, and one in another fragment.
+    pool.registerPlanNodeIdToQueryMemoryMap(
+        QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, MemorySide.INPUT);
+    MemoryPool.MemoryReservationResult detached =
+        pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 32, 512, false);
+    Assert.assertFalse(detached.getFuture().isDone());
+    pool.registerPlanNodeIdToQueryMemoryMap("waiting", "fi", "later", MemorySide.INPUT);
+    MemoryPool.MemoryReservationResult healthy =
+        pool.reserveWithPriority("waiting", "fi", "later", 32, 512, false);
+    Assert.assertFalse(healthy.getFuture().isDone());
+    pool.deRegisterFragmentInstanceFromQueryMemoryMap(QUERY_ID, FRAGMENT_INSTANCE_ID, true);
+    // The deregistered fragment's waiter is detached without completion; the other one is
+    // untouched and stays wakeable by released capacity.
+    Assert.assertFalse(detached.getFuture().isDone());
+    Assert.assertFalse(healthy.getFuture().isDone());
+    pool.free("other", "fi", "o1", 512);
+    Assert.assertTrue(healthy.getFuture().isDone());
+    pool.free("other", "fi", "o2", 512);
+    Assert.assertEquals(32, pool.tryCancel(detached.getFuture()));
+    pool.free("waiting", "fi", "later", 32);
+    pool.deRegisterFragmentInstanceFromQueryMemoryMap("waiting", "fi", true);
+    pool.deRegisterFragmentInstanceFromQueryMemoryMap("other", "fi", true);
+    Assert.assertEquals(0, pool.getReservedBytes());
+    Assert.assertEquals(0, pool.getMemoryReservationSize());
+    Assert.assertEquals(0, pool.getQueryMemoryReservationSize());
+  }
+
+  @Test
+  public void testDetachedWaiterDoesNotUseRecreatedFragmentAccount() {
+    pool.registerPlanNodeIdToQueryMemoryMap("holder", "fi", "a", MemorySide.INPUT);
+    pool.registerPlanNodeIdToQueryMemoryMap("holder", "fi", "b", MemorySide.INPUT);
+    Assert.assertTrue(pool.tryReserve("holder", "fi", "a", 512, 512));
+    Assert.assertTrue(pool.tryReserve("holder", "fi", "b", 512, 512));
+    MemoryReservationResult old =
+        pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 32, 512, false);
+    AtomicInteger callbacks = new AtomicInteger();
+    old.getFuture().addListener(callbacks::incrementAndGet, Runnable::run);
+    pool.deRegisterFragmentInstanceFromQueryMemoryMap(QUERY_ID, FRAGMENT_INSTANCE_ID, true);
+    Assert.assertFalse(old.getFuture().isDone());
+    Assert.assertEquals(0, callbacks.get());
+    pool.registerPlanNodeIdToQueryMemoryMap(
+        QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, MemorySide.INPUT);
+    MemoryReservationResult replacement =
+        pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 32, 512, false);
+    Assert.assertEquals(32, pool.tryCancel(old.getFuture()));
+    Assert.assertEquals(1, pool.getMemoryReservationSize());
+    pool.free("holder", "fi", "a", 512);
+    Assert.assertTrue(replacement.getFuture().isDone());
+    Assert.assertFalse(replacement.getFuture().isCancelled());
+    pool.free("holder", "fi", "b", 512);
+    pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 32);
+    pool.deRegisterFragmentInstanceFromQueryMemoryMap(QUERY_ID, FRAGMENT_INSTANCE_ID, true);
+    pool.deRegisterFragmentInstanceFromQueryMemoryMap("holder", "fi", true);
+    Assert.assertEquals(0, pool.getReservedBytes());
+    Assert.assertEquals(0, pool.getMemoryReservationSize());
+    Assert.assertEquals(0, pool.getQueryMemoryReservationSize());
+  }
+
+  @Test
+  public void testNonForcedDeregisterKeepsPendingReservationWhileMemoryIsLive() {
+    Assert.assertTrue(pool.tryReserve(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 512, 512));
+    MemoryReservationResult pending =
+        pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 256, 512, false);
+
+    pool.deRegisterFragmentInstanceFromQueryMemoryMap(QUERY_ID, FRAGMENT_INSTANCE_ID, false);
+
+    Assert.assertFalse(pending.getFuture().isDone());
+    pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 512);
+    Assert.assertTrue(pending.getFuture().isDone());
+    Assert.assertFalse(pending.getFuture().isCancelled());
+    pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 256);
+    pool.deRegisterFragmentInstanceFromQueryMemoryMap(QUERY_ID, FRAGMENT_INSTANCE_ID, false);
+    Assert.assertEquals(0, pool.getReservedBytes());
+  }
+
+  @Test
+  public void testDeregisterDoesNotCancelOtherFragmentPendingReservation() {
+    pool.registerPlanNodeIdToQueryMemoryMap("healthy", "fi", "a", MemorySide.INPUT);
+    pool.registerPlanNodeIdToQueryMemoryMap("healthy", "fi", "b", MemorySide.INPUT);
+    Assert.assertTrue(pool.tryReserve("healthy", "fi", "a", 512, 512));
+    Assert.assertTrue(pool.tryReserve("healthy", "fi", "b", 512, 512));
+    pool.registerPlanNodeIdToQueryMemoryMap(QUERY_ID, "other", PLAN_NODE_ID, MemorySide.INPUT);
+    MemoryReservationResult pending =
+        pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 256, 512, false);
+    MemoryReservationResult other =
+        pool.reserveWithPriority(QUERY_ID, "other", PLAN_NODE_ID, 256, 512, false);
+
+    pool.deRegisterFragmentInstanceFromQueryMemoryMap(QUERY_ID, FRAGMENT_INSTANCE_ID, true);
+
+    Assert.assertFalse(pending.getFuture().isDone());
+    Assert.assertEquals(256, pool.tryCancel(pending.getFuture()));
+    Assert.assertFalse(other.getFuture().isDone());
+    pool.free("healthy", "fi", "a", 512);
+    Assert.assertTrue(other.getFuture().isDone());
+    Assert.assertFalse(other.getFuture().isCancelled());
+    pool.free(QUERY_ID, "other", PLAN_NODE_ID, 256);
+    pool.free("healthy", "fi", "b", 512);
+    Assert.assertEquals(0, pool.getReservedBytes());
+  }
+
+  @Test
+  public void testOwnerCancellationAndDeregistrationDoNotScanUnrelatedWaiters() throws Exception {
+    assertDeregistrationDoesNotScanUnrelatedWaiters(true);
+  }
+
+  @Test
+  public void testDeregistrationDetachesOnlyIndexedWaiters() throws Exception {
+    assertDeregistrationDoesNotScanUnrelatedWaiters(false);
+  }
+
+  private void assertDeregistrationDoesNotScanUnrelatedWaiters(boolean ownerCancels)
+      throws Exception {
+    AtomicInteger visits = countGlobalPendingVisits();
+    pool.tryReserve(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 512, 512);
+    pool.registerPlanNodeIdToQueryMemoryMap("holder", "fi", "p", MemorySide.INPUT);
+    pool.tryReserve("holder", "fi", "p", 512, 512);
+    int count = 1024;
+    List<MemoryReservationResult> victims = new ArrayList<>();
+    List<MemoryReservationResult> unrelated = new ArrayList<>();
+    for (int i = 0; i < count; i++) {
+      String fi = Integer.toString(i);
+      pool.registerPlanNodeIdToQueryMemoryMap("victim", fi, "p", MemorySide.INPUT);
+      pool.registerPlanNodeIdToQueryMemoryMap("unrelated", fi, "p", MemorySide.INPUT);
+      victims.add(pool.reserveWithPriority("victim", fi, "p", 8, 512, false));
+      unrelated.add(pool.reserveWithPriority("unrelated", fi, "p", 8, 512, false));
+    }
+    for (int i = 0; i < count; i++) {
+      if (ownerCancels) {
+        Assert.assertEquals(8, pool.tryCancel(victims.get(i).getFuture()));
+      }
+      pool.deRegisterFragmentInstanceFromQueryMemoryMap("victim", Integer.toString(i), true);
+      if (!ownerCancels) {
+        Assert.assertFalse(victims.get(i).getFuture().isDone());
+        Assert.assertEquals(8, pool.tryCancel(victims.get(i).getFuture()));
+      }
+      Assert.assertTrue(victims.get(i).getFuture().isCancelled());
+      Assert.assertFalse(unrelated.get(i).getFuture().isDone());
+    }
+    // An operation-count assertion avoids timing noise and detects a global scan for every FI.
+    Assert.assertEquals(0, visits.get());
+    Assert.assertEquals(count, pool.getMemoryReservationSize());
+    for (int i = 0; i < count; i++) {
+      Assert.assertEquals(8, pool.tryCancel(unrelated.get(i).getFuture()));
+      pool.deRegisterFragmentInstanceFromQueryMemoryMap("unrelated", Integer.toString(i), true);
+    }
+    pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 512);
+    pool.free("holder", "fi", "p", 512);
+    pool.deRegisterFragmentInstanceFromQueryMemoryMap(QUERY_ID, FRAGMENT_INSTANCE_ID, true);
+    pool.deRegisterFragmentInstanceFromQueryMemoryMap("holder", "fi", true);
+    Assert.assertEquals(0, pool.getMemoryReservationSize());
+    Assert.assertEquals(0, pool.getQueryMemoryReservationSize());
+    Assert.assertEquals(0, pool.getReservedBytes());
+  }
+
+  @SuppressWarnings("unchecked")
+  private AtomicInteger countGlobalPendingVisits() throws Exception {
+    AtomicInteger visits = new AtomicInteger();
+    Field field = MemoryPool.class.getDeclaredField("memoryReservationFutures");
+    field.setAccessible(true);
+    Object pending = field.get(pool);
+    if (pending instanceof Queue) {
+      // Keep this branch so the regression also runs against the former queue implementation.
+      Queue<MemoryPool.MemoryReservationFuture<Void>> counted =
+          new ConcurrentLinkedQueue<MemoryPool.MemoryReservationFuture<Void>>() {
+            @Override
+            public Iterator<MemoryPool.MemoryReservationFuture<Void>> iterator() {
+              return countVisits(super.iterator(), visits);
+            }
+          };
+      counted.addAll((Queue<MemoryPool.MemoryReservationFuture<Void>>) pending);
+      field.set(pool, counted);
+    } else {
+      Map<Long, MemoryPool.MemoryReservationFuture<Void>> counted =
+          new ConcurrentSkipListMap<Long, MemoryPool.MemoryReservationFuture<Void>>() {
+            @Override
+            public Collection<MemoryPool.MemoryReservationFuture<Void>> values() {
+              Collection<MemoryPool.MemoryReservationFuture<Void>> values = super.values();
+              return new AbstractCollection<MemoryPool.MemoryReservationFuture<Void>>() {
+                @Override
+                public Iterator<MemoryPool.MemoryReservationFuture<Void>> iterator() {
+                  return countVisits(values.iterator(), visits);
+                }
+
+                @Override
+                public int size() {
+                  return values.size();
+                }
+              };
+            }
+          };
+      counted.putAll((Map<Long, MemoryPool.MemoryReservationFuture<Void>>) pending);
+      field.set(pool, counted);
+    }
+    return visits;
+  }
+
+  private static <T> Iterator<T> countVisits(Iterator<T> delegate, AtomicInteger visits) {
+    return new Iterator<T>() {
+      @Override
+      public boolean hasNext() {
+        return delegate.hasNext();
+      }
+
+      @Override
+      public T next() {
+        visits.incrementAndGet();
+        return delegate.next();
+      }
+
+      @Override
+      public void remove() {
+        delegate.remove();
+      }
+    };
+  }
+
+  @Test
+  public void testCancellationKeepsFifoWakeupOrder() {
+    pool.tryReserve(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 512, 512);
+    pool.registerPlanNodeIdToQueryMemoryMap("holder", "fi", "p", MemorySide.INPUT);
+    pool.tryReserve("holder", "fi", "p", 512, 512);
+    List<MemoryReservationResult> requests = new ArrayList<>();
+    for (int i = 0; i < 3; i++) {
+      String fi = Integer.toString(i);
+      pool.registerPlanNodeIdToQueryMemoryMap("waiting", fi, "p", MemorySide.INPUT);
+      requests.add(pool.reserveWithPriority("waiting", fi, "p", 64, 512, false));
+    }
+    Assert.assertEquals(64, pool.tryCancel(requests.get(1).getFuture()));
+    Assert.assertEquals(0, pool.tryCancel(requests.get(1).getFuture()));
+    Assert.assertEquals(2, pool.getMemoryReservationSize());
+    pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 64);
+    Assert.assertTrue(requests.get(0).getFuture().isDone());
+    Assert.assertFalse(requests.get(2).getFuture().isDone());
+    pool.free("waiting", "0", "p", 64);
+    Assert.assertTrue(requests.get(2).getFuture().isDone());
+    pool.free("waiting", "2", "p", 64);
+    pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 448);
+    pool.free("holder", "fi", "p", 512);
+    for (int i = 0; i < 3; i++) {
+      pool.deRegisterFragmentInstanceFromQueryMemoryMap("waiting", Integer.toString(i), true);
+    }
+    Assert.assertEquals(0, pool.getMemoryReservationSize());
+    Assert.assertEquals(0, pool.getReservedBytes());
+  }
+
+  @Test
+  public void testConcurrentCancellationAndGrantRemoveAllPendingIndexes() throws Exception {
+    ExecutorService executor = Executors.newFixedThreadPool(2);
+    try {
+      for (int round = 0; round < 32; round++) {
+        MemoryPool concurrent = new MemoryPool("race", new MemoryManager(1024), 512);
+        concurrent.registerPlanNodeIdToQueryMemoryMap("holder", "fi", "a", MemorySide.INPUT);
+        concurrent.registerPlanNodeIdToQueryMemoryMap("holder", "fi", "b", MemorySide.INPUT);
+        concurrent.tryReserve("holder", "fi", "a", 512, 512);
+        concurrent.tryReserve("holder", "fi", "b", 512, 512);
+        List<MemoryReservationResult> requests = new ArrayList<>();
+        for (int i = 0; i < 16; i++) {
+          String fi = Integer.toString(i);
+          concurrent.registerPlanNodeIdToQueryMemoryMap("waiting", fi, "p", MemorySide.INPUT);
+          requests.add(concurrent.reserveWithPriority("waiting", fi, "p", 16, 512, false));
+        }
+        CountDownLatch start = new CountDownLatch(1);
+        Future<?> grant =
+            executor.submit(
+                () -> {
+                  start.await();
+                  concurrent.free("holder", "fi", "a", 512);
+                  return null;
+                });
+        Future<?> cancel =
+            executor.submit(
+                () -> {
+                  start.await();
+                  for (MemoryReservationResult request : requests) {
+                    concurrent.tryCancel(request.getFuture());
+                  }
+                  return null;
+                });
+        start.countDown();
+        grant.get(10, TimeUnit.SECONDS);
+        cancel.get(10, TimeUnit.SECONDS);
+        for (int i = 0; i < requests.size(); i++) {
+          Assert.assertTrue(requests.get(i).getFuture().isDone());
+          if (!requests.get(i).getFuture().isCancelled()) {
+            concurrent.free("waiting", Integer.toString(i), "p", 16);
+          }
+          concurrent.deRegisterFragmentInstanceFromQueryMemoryMap(
+              "waiting", Integer.toString(i), true);
+        }
+        concurrent.free("holder", "fi", "b", 512);
+        concurrent.deRegisterFragmentInstanceFromQueryMemoryMap("holder", "fi", true);
+        Assert.assertEquals(0, concurrent.getMemoryReservationSize());
+        Assert.assertEquals(0, concurrent.getQueryMemoryReservationSize());
+        Assert.assertEquals(0, concurrent.getReservedBytes());
+      }
+    } finally {
+      executor.shutdownNow();
+      Assert.assertTrue(executor.awaitTermination(10, TimeUnit.SECONDS));
+    }
+  }
+
   @Test
   public void testTryReserve() {
 
@@ -518,4 +899,491 @@ public class MemoryPoolTest {
     Assert.assertTrue(r.getFuture().isDone());
     Assert.assertEquals(256L, pool.getReservedBytes());
   }
+
+  @Test
+  public void testSharedInputBudgetAndIndependentOutput() {
+    pool.registerFragmentInstanceSharedBudget(QUERY_ID, FRAGMENT_INSTANCE_ID);
+    pool.registerPlanNodeIdToQueryMemoryMap(
+        QUERY_ID, FRAGMENT_INSTANCE_ID, "input2", MemorySide.INPUT);
+    pool.registerPlanNodeIdToQueryMemoryMap(
+        QUERY_ID, FRAGMENT_INSTANCE_ID, "output", MemorySide.OUTPUT);
+    Assert.assertTrue(
+        pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 200, 256, false)
+            .isReserveSuccess());
+    MemoryReservationResult pending =
+        pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, "input2", 100, 256, false);
+    Assert.assertFalse(pending.isReserveSuccess());
+    Assert.assertFalse(pending.getFuture().isDone());
+    Assert.assertEquals(
+        200, pool.getFragmentMemoryReservedBytes(QUERY_ID, FRAGMENT_INSTANCE_ID, MemorySide.INPUT));
+    Assert.assertEquals(
+        0, pool.getPlanNodeMemoryReservedBytes(QUERY_ID, FRAGMENT_INSTANCE_ID, "input2"));
+    Assert.assertTrue(
+        pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, "output", 256, 256, false)
+            .isReserveSuccess());
+    Assert.assertEquals(456, pool.getReservedBytes());
+    pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 200);
+    Assert.assertTrue(pending.getFuture().isDone());
+    Assert.assertEquals(
+        100, pool.getFragmentMemoryReservedBytes(QUERY_ID, FRAGMENT_INSTANCE_ID, MemorySide.INPUT));
+    Assert.assertEquals(
+        256,
+        pool.getFragmentMemoryReservedBytes(QUERY_ID, FRAGMENT_INSTANCE_ID, MemorySide.OUTPUT));
+    pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, "input2", 100);
+    pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, "output", 256);
+    Assert.assertEquals(0, pool.getReservedBytes());
+    Assert.assertEquals(0, pool.getQueryMemoryReservedBytes(QUERY_ID));
+  }
+
+  @Test
+  public void testSideRegistrationCannotChange() {
+    pool.registerPlanNodeIdToQueryMemoryMap(
+        QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, MemorySide.INPUT);
+    try {
+      pool.registerPlanNodeIdToQueryMemoryMap(
+          QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, MemorySide.OUTPUT);
+      Assert.fail("conflicting side must be rejected");
+    } catch (IllegalArgumentException expected) {
+      Assert.assertEquals(0, pool.getReservedBytes());
+    }
+  }
+
+  @Test
+  public void testRequestsUseCurrentSuppliedLimitInsteadOfPoolCreationBudget() {
+    pool.registerFragmentInstanceSharedBudget(QUERY_ID, FRAGMENT_INSTANCE_ID);
+    Assert.assertTrue(
+        pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 768, 768, false)
+            .isReserveSuccess());
+    Assert.assertEquals(768, pool.getReservedBytes());
+    pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 768);
+    Assert.assertEquals(0, pool.getReservedBytes());
+  }
+
+  @Test
+  public void testRequestLargerThanGlobalCapacityDoesNotWait() {
+    try {
+      pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 1025, 2048, false);
+      Assert.fail("a request larger than the global pool cannot become ready");
+    } catch (IllegalArgumentException expected) {
+      Assert.assertEquals(0, pool.getReservedBytes());
+      Assert.assertEquals(0, pool.getMemoryReservationSize());
+    }
+  }
+
+  @Test
+  public void testSharedRequestCannotExceedSuppliedInputBudget() {
+    pool.registerFragmentInstanceSharedBudget(QUERY_ID, FRAGMENT_INSTANCE_ID);
+    try {
+      pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 257, 256, false);
+      Assert.fail("request exceeds the supplied input budget");
+    } catch (IllegalArgumentException expected) {
+      Assert.assertEquals(0, pool.getReservedBytes());
+      Assert.assertEquals(0, pool.getQueryMemoryReservedBytes(QUERY_ID));
+      Assert.assertEquals(0, pool.getMemoryReservationSize());
+    }
+  }
+
+  @Test
+  public void testSharedReservationsAndWakeupsUseSuppliedSideLimits() throws Exception {
+    pool.registerFragmentInstanceSharedBudget(QUERY_ID, FRAGMENT_INSTANCE_ID);
+    pool.registerPlanNodeIdToQueryMemoryMap(
+        QUERY_ID, FRAGMENT_INSTANCE_ID, "input2", MemorySide.INPUT);
+    pool.registerPlanNodeIdToQueryMemoryMap(
+        QUERY_ID, FRAGMENT_INSTANCE_ID, "output", MemorySide.OUTPUT);
+    Assert.assertTrue(
+        pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 200, 256, false)
+            .isReserveSuccess());
+    MemoryReservationResult pending =
+        pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, "input2", 100, 256, false);
+    Assert.assertFalse(pending.getFuture().isDone());
+    Assert.assertTrue(
+        pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, "output", 256, 256, false)
+            .isReserveSuccess());
+    // An OUTPUT release cannot grant an INPUT request whose shared side remains full.
+    pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, "output", 256);
+    Assert.assertFalse(pending.getFuture().isDone());
+    Assert.assertFalse(pool.tryReserveForTest(QUERY_ID, FRAGMENT_INSTANCE_ID, "input2", 100, 256));
+    pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 200);
+    pending.getFuture().get(10, TimeUnit.SECONDS);
+    Assert.assertEquals(
+        100, pool.getFragmentMemoryReservedBytes(QUERY_ID, FRAGMENT_INSTANCE_ID, MemorySide.INPUT));
+    pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, "input2", 100);
+    Assert.assertEquals(0, pool.getReservedBytes());
+    Assert.assertEquals(0, pool.getMemoryReservationSize());
+  }
+
+  @Test
+  public void testSingleDirectionKeepsFullBudgetAndSingleBlockLimit() {
+    MemoryPool singleDirectionPool =
+        new MemoryPool("single-direction", new MemoryManager(1024), 513);
+    for (MemorySide side : MemorySide.values()) {
+      String fi = side.name();
+      singleDirectionPool.registerFragmentInstanceSharedBudget(QUERY_ID, fi);
+      singleDirectionPool.registerPlanNodeIdToQueryMemoryMap(QUERY_ID, fi, "active", side);
+      singleDirectionPool.registerPlanNodeIdToQueryMemoryMap(
+          QUERY_ID, fi, "unused", side == MemorySide.INPUT ? MemorySide.OUTPUT : MemorySide.INPUT);
+      Assert.assertTrue(
+          singleDirectionPool
+              .reserveWithPriority(QUERY_ID, fi, "active", 513, 513, false)
+              .isReserveSuccess());
+      try {
+        singleDirectionPool.reserveWithPriority(QUERY_ID, fi, "unused", 1, 0, false);
+        Assert.fail("an unused direction supplies a zero limit");
+      } catch (IllegalArgumentException expected) {
+        Assert.assertEquals(0, singleDirectionPool.getMemoryReservationSize());
+        Assert.assertEquals(513, singleDirectionPool.getReservedBytes());
+      }
+      singleDirectionPool.free(QUERY_ID, fi, "active", 513);
+      Assert.assertEquals(0, singleDirectionPool.getReservedBytes());
+    }
+  }
+
+  @Test
+  public void testConcurrentPlanNodesShareOneSideCounter() throws Exception {
+    pool.registerFragmentInstanceSharedBudget(QUERY_ID, FRAGMENT_INSTANCE_ID);
+    ExecutorService executor = Executors.newFixedThreadPool(16);
+    CountDownLatch start = new CountDownLatch(1);
+    CountDownLatch attempted = new CountDownLatch(16);
+    CountDownLatch release = new CountDownLatch(1);
+    List<Future<?>> tasks = new ArrayList<>();
+    try {
+      for (int i = 0; i < 16; i++) {
+        String id = "pipeline-" + i;
+        pool.registerPlanNodeIdToQueryMemoryMap(
+            QUERY_ID, FRAGMENT_INSTANCE_ID, id, MemorySide.INPUT);
+        tasks.add(
+            executor.submit(
+                () -> {
+                  start.await();
+                  boolean reserved =
+                      pool.tryReserveForTest(QUERY_ID, FRAGMENT_INSTANCE_ID, id, 32, 128);
+                  attempted.countDown();
+                  release.await();
+                  if (reserved) pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, id, 32);
+                  return null;
+                }));
+      }
+      start.countDown();
+      Assert.assertTrue(attempted.await(10, TimeUnit.SECONDS));
+      long details = 0;
+      for (int i = 0; i < 16; i++) {
+        details +=
+            pool.getPlanNodeMemoryReservedBytes(QUERY_ID, FRAGMENT_INSTANCE_ID, "pipeline-" + i);
+      }
+      Assert.assertTrue(details > 0 && details <= 128);
+      Assert.assertEquals(
+          details,
+          pool.getFragmentMemoryReservedBytes(QUERY_ID, FRAGMENT_INSTANCE_ID, MemorySide.INPUT));
+      Assert.assertEquals(details, pool.getReservedBytes());
+      release.countDown();
+      for (Future<?> task : tasks) task.get(10, TimeUnit.SECONDS);
+      Assert.assertEquals(0, pool.getReservedBytes());
+      Assert.assertEquals(0, pool.getQueryMemoryReservedBytes(QUERY_ID));
+    } finally {
+      start.countDown();
+      release.countDown();
+      executor.shutdownNow();
+    }
+  }
+
+  @Test
+  public void testReleaseBeforePendingReservationIsPublished() throws Exception {
+    pool.registerFragmentInstanceSharedBudget(QUERY_ID, FRAGMENT_INSTANCE_ID);
+    pool = Mockito.spy(pool);
+    pool.registerPlanNodeIdToQueryMemoryMap(
+        QUERY_ID, FRAGMENT_INSTANCE_ID, "input2", MemorySide.INPUT);
+    Assert.assertTrue(
+        pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 256, 256, false)
+            .isReserveSuccess());
+    CountDownLatch failedAttempt = new CountDownLatch(1);
+    CountDownLatch released = new CountDownLatch(1);
+    AtomicBoolean firstAttempt = new AtomicBoolean(true);
+    Mockito.doAnswer(
+            invocation -> {
+              boolean granted = (boolean) invocation.callRealMethod();
+              if (!granted && firstAttempt.compareAndSet(true, false)) {
+                failedAttempt.countDown();
+                Assert.assertTrue(released.await(10, TimeUnit.SECONDS));
+              }
+              return granted;
+            })
+        .when(pool)
+        .tryReserve(QUERY_ID, FRAGMENT_INSTANCE_ID, "input2", 256, 256);
+    FutureTask<MemoryReservationResult> reservation =
+        new FutureTask<>(
+            () ->
+                pool.reserveWithPriority(
+                    QUERY_ID, FRAGMENT_INSTANCE_ID, "input2", 256, 256, false));
+    Thread reserver = new Thread(reservation, "reserve-shared-input");
+    reserver.start();
+    try {
+      Assert.assertTrue(failedAttempt.await(10, TimeUnit.SECONDS));
+      pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 256);
+      released.countDown();
+      MemoryReservationResult result = reservation.get(10, TimeUnit.SECONDS);
+      Assert.assertTrue(
+          "Releasing the last owner must wake the pending request", result.getFuture().isDone());
+      result.getFuture().get(10, TimeUnit.SECONDS);
+      Assert.assertEquals(256, pool.getReservedBytes());
+      Assert.assertEquals(
+          256,
+          pool.getFragmentMemoryReservedBytes(QUERY_ID, FRAGMENT_INSTANCE_ID, MemorySide.INPUT));
+      Assert.assertEquals(
+          256, pool.getPlanNodeMemoryReservedBytes(QUERY_ID, FRAGMENT_INSTANCE_ID, "input2"));
+      pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, "input2", 256);
+      Assert.assertEquals(0, pool.getReservedBytes());
+    } finally {
+      released.countDown();
+      reserver.join(10000);
+      Assert.assertFalse(reserver.isAlive());
+    }
+  }
+
+  @Test
+  public void testCancelledInputDoesNotConsumeReleasedSharedBudget() throws Exception {
+    pool.registerFragmentInstanceSharedBudget(QUERY_ID, FRAGMENT_INSTANCE_ID);
+    pool.registerPlanNodeIdToQueryMemoryMap(
+        QUERY_ID, FRAGMENT_INSTANCE_ID, "cancelled", MemorySide.INPUT);
+    pool.registerPlanNodeIdToQueryMemoryMap(
+        QUERY_ID, FRAGMENT_INSTANCE_ID, "waiting", MemorySide.INPUT);
+    pool.registerPlanNodeIdToQueryMemoryMap(
+        QUERY_ID, FRAGMENT_INSTANCE_ID, "output", MemorySide.OUTPUT);
+    pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 256, 256, false);
+    MemoryReservationResult cancelled =
+        pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, "cancelled", 128, 256, false);
+    MemoryReservationResult waiting =
+        pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, "waiting", 256, 256, false);
+    Assert.assertFalse(cancelled.getFuture().isDone());
+    Assert.assertFalse(waiting.getFuture().isDone());
+    Assert.assertTrue(
+        pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, "output", 128, 256, false)
+            .isReserveSuccess());
+    Assert.assertEquals(128, pool.tryCancel(cancelled.getFuture()));
+    pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 256);
+    waiting.getFuture().get(10, TimeUnit.SECONDS);
+    Assert.assertTrue(cancelled.getFuture().isCancelled());
+    Assert.assertEquals(
+        0, pool.getPlanNodeMemoryReservedBytes(QUERY_ID, FRAGMENT_INSTANCE_ID, "cancelled"));
+    Assert.assertEquals(
+        256, pool.getFragmentMemoryReservedBytes(QUERY_ID, FRAGMENT_INSTANCE_ID, MemorySide.INPUT));
+    Assert.assertEquals(
+        128,
+        pool.getFragmentMemoryReservedBytes(QUERY_ID, FRAGMENT_INSTANCE_ID, MemorySide.OUTPUT));
+    Assert.assertEquals(384, pool.getReservedBytes());
+    Assert.assertEquals(0, pool.getMemoryReservationSize());
+    pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, "waiting", 256);
+    pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, "output", 128);
+    Assert.assertEquals(0, pool.getQueryMemoryReservedBytes(QUERY_ID));
+    Assert.assertEquals(0, pool.getReservedBytes());
+  }
+
+  @Test
+  public void testConcurrentPendingReservationsCompleteAndReleaseAllAccounting() throws Exception {
+    pool.registerFragmentInstanceSharedBudget(QUERY_ID, FRAGMENT_INSTANCE_ID);
+    int count = 16;
+    ExecutorService executor = Executors.newFixedThreadPool(count);
+    CountDownLatch attempted = new CountDownLatch(count);
+    List<Future<?>> tasks = new ArrayList<>();
+    pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 128, 128, false);
+    try {
+      for (int i = 0; i < count; i++) {
+        String id = "waiting-" + i;
+        pool.registerPlanNodeIdToQueryMemoryMap(
+            QUERY_ID, FRAGMENT_INSTANCE_ID, id, MemorySide.INPUT);
+        tasks.add(
+            executor.submit(
+                () -> {
+                  for (int round = 0; round < 50; round++) {
+                    MemoryReservationResult result =
+                        pool.reserveWithPriority(
+                            QUERY_ID, FRAGMENT_INSTANCE_ID, id, 32, 128, false);
+                    if (round == 0) attempted.countDown();
+                    result.getFuture().get(10, TimeUnit.SECONDS);
+                    pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, id, result.getReservedBytes());
+                  }
+                  return null;
+                }));
+      }
+      Assert.assertTrue(attempted.await(10, TimeUnit.SECONDS));
+      Assert.assertEquals(count, pool.getMemoryReservationSize());
+      pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 128);
+      for (Future<?> task : tasks) task.get(30, TimeUnit.SECONDS);
+      for (int i = 0; i < count; i++) {
+        Assert.assertEquals(
+            0, pool.getPlanNodeMemoryReservedBytes(QUERY_ID, FRAGMENT_INSTANCE_ID, "waiting-" + i));
+      }
+      Assert.assertEquals(
+          0, pool.getFragmentMemoryReservedBytes(QUERY_ID, FRAGMENT_INSTANCE_ID, MemorySide.INPUT));
+      Assert.assertEquals(0, pool.getReservedBytes());
+      Assert.assertEquals(0, pool.getQueryMemoryReservedBytes(QUERY_ID));
+      Assert.assertEquals(0, pool.getMemoryReservationSize());
+    } finally {
+      executor.shutdownNow();
+      Assert.assertTrue(executor.awaitTermination(10, TimeUnit.SECONDS));
+    }
+  }
+
+  @Test
+  public void testDefaultFragmentKeepsIndependentPlanNodeQuotas() {
+    MemoryPool legacy = new MemoryPool("legacy", new MemoryManager(1024), 512);
+    legacy.registerPlanNodeIdToQueryMemoryMap("q", "f", "a", MemorySide.INPUT);
+    legacy.registerPlanNodeIdToQueryMemoryMap("q", "f", "b", MemorySide.INPUT);
+    Assert.assertTrue(
+        legacy.reserveWithPriority("q", "f", "a", 256, 256, false).isReserveSuccess());
+    Assert.assertTrue(
+        legacy.reserveWithPriority("q", "f", "b", 256, 256, false).isReserveSuccess());
+    Assert.assertEquals(512, legacy.getFragmentMemoryReservedBytes("q", "f", MemorySide.INPUT));
+    legacy.free("q", "f", "a", 256);
+    legacy.free("q", "f", "b", 256);
+    Assert.assertEquals(0, legacy.getReservedBytes());
+  }
+
+  @Test
+  public void testSharedAndLegacyFragmentsDoNotChangeEachOthersPolicy() {
+    pool.registerFragmentInstanceSharedBudget(QUERY_ID, FRAGMENT_INSTANCE_ID);
+    pool.registerPlanNodeIdToQueryMemoryMap(QUERY_ID, "legacy", "a", MemorySide.INPUT);
+    pool.registerPlanNodeIdToQueryMemoryMap(QUERY_ID, "legacy", "b", MemorySide.INPUT);
+    pool.registerPlanNodeIdToQueryMemoryMap(QUERY_ID, FRAGMENT_INSTANCE_ID, "b", MemorySide.INPUT);
+    Assert.assertTrue(
+        pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 256, 256, false)
+            .isReserveSuccess());
+    MemoryReservationResult blocked =
+        pool.reserveWithPriority(QUERY_ID, FRAGMENT_INSTANCE_ID, "b", 64, 256, false);
+    Assert.assertFalse(blocked.getFuture().isDone());
+    Assert.assertTrue(
+        pool.reserveWithPriority(QUERY_ID, "legacy", "a", 256, 256, false).isReserveSuccess());
+    Assert.assertTrue(
+        pool.reserveWithPriority(QUERY_ID, "legacy", "b", 256, 256, false).isReserveSuccess());
+    pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 256);
+    Assert.assertTrue(blocked.getFuture().isDone());
+    pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, "b", 64);
+    pool.free(QUERY_ID, "legacy", "a", 256);
+    pool.free(QUERY_ID, "legacy", "b", 256);
+    Assert.assertEquals(0, pool.getReservedBytes());
+  }
+
+  @Test
+  public void testConcurrentAdmissionAlwaysGrantsOneRequestWhenOneFits() throws Exception {
+    assertConcurrentAdmissionAlwaysGrantsOneRequestWhenOneFits(true);
+  }
+
+  @Test
+  public void testLegacyConcurrentAdmissionAlwaysGrantsOneRequestWhenOneFits() throws Exception {
+    assertConcurrentAdmissionAlwaysGrantsOneRequestWhenOneFits(false);
+  }
+
+  private void assertConcurrentAdmissionAlwaysGrantsOneRequestWhenOneFits(boolean shared)
+      throws Exception {
+    if (shared) {
+      pool.registerFragmentInstanceSharedBudget(QUERY_ID, FRAGMENT_INSTANCE_ID);
+    }
+    for (int i = 0; i < 3; i++) {
+      String fi = "other" + i;
+      pool.registerPlanNodeIdToQueryMemoryMap(QUERY_ID, fi, PLAN_NODE_ID, MemorySide.OUTPUT);
+      Assert.assertTrue(
+          pool.reserveWithPriority(QUERY_ID, fi, PLAN_NODE_ID, 256, 256, false).isReserveSuccess());
+    }
+    pool.registerPlanNodeIdToQueryMemoryMap(QUERY_ID, FRAGMENT_INSTANCE_ID, "b", MemorySide.INPUT);
+    ExecutorService executor = Executors.newFixedThreadPool(2);
+    try {
+      for (int i = 0; i < 100; i++) {
+        CountDownLatch start = new CountDownLatch(1);
+        Future<MemoryReservationResult> first =
+            executor.submit(
+                () -> {
+                  start.await();
+                  return pool.reserveWithPriority(
+                      QUERY_ID, FRAGMENT_INSTANCE_ID, PLAN_NODE_ID, 256, 256, false);
+                });
+        Future<MemoryReservationResult> second =
+            executor.submit(
+                () -> {
+                  start.await();
+                  return pool.reserveWithPriority(
+                      QUERY_ID, FRAGMENT_INSTANCE_ID, "b", 256, 256, false);
+                });
+        start.countDown();
+        MemoryReservationResult a = first.get(10, TimeUnit.SECONDS);
+        MemoryReservationResult b = second.get(10, TimeUnit.SECONDS);
+        Assert.assertTrue(a.getFuture().isDone() ^ b.getFuture().isDone());
+        String granted = a.getFuture().isDone() ? PLAN_NODE_ID : "b";
+        String waiting = a.getFuture().isDone() ? "b" : PLAN_NODE_ID;
+        pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, granted, 256);
+        Assert.assertTrue(a.getFuture().isDone());
+        Assert.assertTrue(b.getFuture().isDone());
+        pool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, waiting, 256);
+        Assert.assertEquals(256, pool.getRemainingBytes());
+        Assert.assertEquals(
+            0,
+            pool.getFragmentMemoryReservedBytes(QUERY_ID, FRAGMENT_INSTANCE_ID, MemorySide.INPUT));
+      }
+    } finally {
+      executor.shutdownNow();
+    }
+    for (int i = 0; i < 3; i++) pool.free(QUERY_ID, "other" + i, PLAN_NODE_ID, 256);
+    Assert.assertEquals(0, pool.getReservedBytes());
+  }
+
+  @Test
+  public void testConcurrentLegacyReservationsKeepIndependentQuotasAndAccurateSideTotals()
+      throws Exception {
+    for (boolean samePlanNode : new boolean[] {true, false}) {
+      MemoryPool legacyPool = new MemoryPool("legacy", new MemoryManager(1024), 512);
+      ExecutorService executor = Executors.newFixedThreadPool(16);
+      CountDownLatch start = new CountDownLatch(1);
+      CountDownLatch attempted = new CountDownLatch(16);
+      CountDownLatch release = new CountDownLatch(1);
+      List<Future<?>> tasks = new ArrayList<>();
+      try {
+        for (int i = 0; i < 16; i++) {
+          String id = samePlanNode ? "same" : "pipeline-" + i;
+          legacyPool.registerPlanNodeIdToQueryMemoryMap(
+              QUERY_ID, FRAGMENT_INSTANCE_ID, id, MemorySide.INPUT);
+          tasks.add(
+              executor.submit(
+                  () -> {
+                    start.await();
+                    boolean reserved =
+                        legacyPool.tryReserve(QUERY_ID, FRAGMENT_INSTANCE_ID, id, 32, 128);
+                    attempted.countDown();
+                    release.await();
+                    if (reserved) {
+                      legacyPool.free(QUERY_ID, FRAGMENT_INSTANCE_ID, id, 32);
+                    }
+                    return null;
+                  }));
+        }
+        start.countDown();
+        Assert.assertTrue(attempted.await(10, TimeUnit.SECONDS));
+        long expected = samePlanNode ? 128 : 512;
+        Assert.assertEquals(expected, legacyPool.getReservedBytes());
+        Assert.assertEquals(
+            expected,
+            legacyPool.getFragmentMemoryReservedBytes(
+                QUERY_ID, FRAGMENT_INSTANCE_ID, MemorySide.INPUT));
+        if (samePlanNode) {
+          Assert.assertEquals(
+              128,
+              legacyPool.getPlanNodeMemoryReservedBytes(QUERY_ID, FRAGMENT_INSTANCE_ID, "same"));
+        } else {
+          for (int i = 0; i < 16; i++) {
+            Assert.assertEquals(
+                32,
+                legacyPool.getPlanNodeMemoryReservedBytes(
+                    QUERY_ID, FRAGMENT_INSTANCE_ID, "pipeline-" + i));
+          }
+        }
+        release.countDown();
+        for (Future<?> task : tasks) {
+          task.get(10, TimeUnit.SECONDS);
+        }
+        Assert.assertEquals(0, legacyPool.getReservedBytes());
+        Assert.assertEquals(0, legacyPool.getQueryMemoryReservedBytes(QUERY_ID));
+      } finally {
+        start.countDown();
+        release.countDown();
+        executor.shutdownNow();
+      }
+    }
+  }
 }
diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/planner/LocalExecutionPlanContextBudgetTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/planner/LocalExecutionPlanContextBudgetTest.java
new file mode 100644
index 00000000000..e1515684116
--- /dev/null
+++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/planner/LocalExecutionPlanContextBudgetTest.java
@@ -0,0 +1,596 @@
+/*
+ * 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.iotdb.db.queryengine.plan.planner;
+
+import org.apache.iotdb.calc.execution.operator.Operator;
+import org.apache.iotdb.calc.execution.operator.process.CollectOperator;
+import org.apache.iotdb.calc.plan.planner.memory.MemoryReservationManager;
+import org.apache.iotdb.common.rpc.thrift.TEndPoint;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.memory.MemoryManager;
+import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNode;
+import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNodeId;
+import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNodeType;
+import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.CollectNode;
+import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.TopKNode;
+import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.UnionNode;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.queryengine.common.FragmentInstanceId;
+import org.apache.iotdb.db.queryengine.common.PlanFragmentId;
+import org.apache.iotdb.db.queryengine.common.QueryId;
+import org.apache.iotdb.db.queryengine.execution.exchange.MPPDataExchangeManager.SourceHandleListener;
+import org.apache.iotdb.db.queryengine.execution.exchange.sink.ISinkHandle;
+import org.apache.iotdb.db.queryengine.execution.exchange.source.ISourceHandle;
+import org.apache.iotdb.db.queryengine.execution.exchange.source.LocalSourceHandle;
+import org.apache.iotdb.db.queryengine.execution.exchange.source.PipelineSourceHandle;
+import org.apache.iotdb.db.queryengine.execution.exchange.source.SourceHandle;
+import org.apache.iotdb.db.queryengine.execution.fragment.DataNodeQueryContext;
+import org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceContext;
+import org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceStateMachine;
+import org.apache.iotdb.db.queryengine.execution.memory.LocalMemoryManager;
+import org.apache.iotdb.db.queryengine.execution.memory.MemoryPool;
+import org.apache.iotdb.db.queryengine.execution.memory.MemorySide;
+import org.apache.iotdb.db.queryengine.execution.operator.source.ExchangeOperator;
+import org.apache.iotdb.db.queryengine.plan.analyze.TypeProvider;
+import org.apache.iotdb.db.queryengine.plan.relational.metadata.Metadata;
+import org.apache.iotdb.mpp.rpc.thrift.TFragmentInstanceId;
+
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.util.concurrent.SettableFuture;
+import org.apache.tsfile.common.conf.TSFileDescriptor;
+import org.apache.tsfile.read.common.block.column.TsBlockSerde;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.same;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class LocalExecutionPlanContextBudgetTest {
+  private long previousBudget;
+  private int previousBlockSize;
+  private MemoryPool pool;
+  private LocalExecutionPlanContext context;
+
+  @Before
+  public void setUp() {
+    previousBudget =
+        IoTDBDescriptor.getInstance().getMemoryConfig().getMaxBytesPerFragmentInstance();
+    previousBlockSize = TSFileDescriptor.getInstance().getConfig().getMaxTsBlockSizeInBytes();
+    IoTDBDescriptor.getInstance().getMemoryConfig().setMaxBytesPerFragmentInstance(1024);
+    TSFileDescriptor.getInstance().getConfig().setMaxTsBlockSizeInBytes(64);
+    pool = new MemoryPool("budget-test", new MemoryManager(4096), 1024);
+    context = newContext();
+  }
+
+  @After
+  public void tearDown() {
+    IoTDBDescriptor.getInstance().getMemoryConfig().setMaxBytesPerFragmentInstance(previousBudget);
+    TSFileDescriptor.getInstance().getConfig().setMaxTsBlockSizeInBytes(previousBlockSize);
+  }
+
+  private LocalExecutionPlanContext newContext() {
+    FragmentInstanceContext instance =
+        FragmentInstanceContext.createFragmentInstanceContext(
+            new FragmentInstanceId(new PlanFragmentId(new QueryId("budget"), 0), "0"),
+            mock(FragmentInstanceStateMachine.class),
+            mock(MemoryReservationManager.class));
+    LocalExecutionPlanContext result =
+        new LocalExecutionPlanContext(new TypeProvider(), instance, (DataNodeQueryContext) null);
+    result.setExchangeSumNum(4);
+    return result;
+  }
+
+  private static PlanNode node(PlanNodeType type, PlanNode... children) {
+    Class<? extends PlanNode> nodeClass;
+    switch (type) {
+      case IDENTITY_SINK:
+        nodeClass =
+            org.apache.iotdb.db.queryengine.plan.planner.plan.node.sink.IdentitySinkNode.class;
+        break;
+      case TABLE_EXCHANGE_NODE:
+        nodeClass = org.apache.iotdb.db.queryengine.plan.relational.planner.node.ExchangeNode.class;
+        break;
+      case EXCHANGE:
+        nodeClass =
+            org.apache.iotdb.db.queryengine.plan.planner.plan.node.process.ExchangeNode.class;
+        break;
+      case TABLE_COLLECT_NODE:
+        nodeClass = CollectNode.class;
+        break;
+      case TABLE_TOPK_NODE:
+        nodeClass = TopKNode.class;
+        break;
+      case TREE_COLLECT:
+        nodeClass =
+            org.apache.iotdb.db.queryengine.plan.planner.plan.node.process.CollectNode.class;
+        break;
+      default:
+        nodeClass = PlanNode.class;
+        break;
+    }
+    PlanNode node = mock(nodeClass);
+    when(node.getType()).thenReturn(type);
+    when(node.getChildren()).thenReturn(Arrays.asList(children));
+    return node;
+  }
+
+  private static PlanNode collect() {
+    return new CollectNode(
+        new PlanNodeId("collect"),
+        Arrays.asList(
+            node(PlanNodeType.TABLE_EXCHANGE_NODE), node(PlanNodeType.TABLE_EXCHANGE_NODE)),
+        Collections.emptyList());
+  }
+
+  private CollectOperator generateTableCollect(PlanNode plan) {
+    Operator first = mock(Operator.class);
+    Operator second = mock(Operator.class);
+    when(first.isBlocked()).thenReturn(SettableFuture.create());
+    doReturn(Operator.NOT_BLOCKED).when(second).isBlocked();
+    when(first.calculateMaxPeekMemoryWithCounter()).thenReturn(100L);
+    when(first.calculateRetainedSizeAfterCallingNext()).thenReturn(30L);
+    when(second.calculateMaxPeekMemoryWithCounter()).thenReturn(80L);
+    when(second.calculateRetainedSizeAfterCallingNext()).thenReturn(50L);
+    doReturn(first).when(plan.getChildren().get(0)).accept(any(), same(context));
+    doReturn(second).when(plan.getChildren().get(1)).accept(any(), same(context));
+    return (CollectOperator)
+        plan.accept(new DataNodeTableOperatorGenerator(mock(Metadata.class)), context);
+  }
+
+  private ISourceHandle addSource(LocalExecutionPlanContext target) {
+    ISourceHandle handle = mock(SourceHandle.class);
+    ExchangeOperator operator = mock(ExchangeOperator.class);
+    when(operator.getSourceHandle()).thenReturn(handle);
+    target.addExchangeOperator(operator);
+    return handle;
+  }
+
+  private void assertAdmissionIsShared(boolean shared) {
+    assertAdmissionIsShared(shared, MemorySide.INPUT);
+  }
+
+  private void assertAdmissionIsShared(boolean shared, MemorySide side) {
+    assertAdmissionIsShared(shared, side, shared ? 1024 : 256);
+  }
+
+  private void assertAdmissionIsShared(boolean shared, MemorySide side, long limit) {
+    FragmentInstanceContext instance = context.getDriverContext().getFragmentInstanceContext();
+    if (shared) {
+      assertEquals(
+          limit,
+          side == MemorySide.INPUT ? instance.getInputMaxBytes() : instance.getOutputMaxBytes());
+    } else {
+      assertEquals(0, instance.getInputMaxBytes());
+      assertEquals(0, instance.getOutputMaxBytes());
+    }
+    String fi = context.getFragmentInstanceId().getFragmentInstanceId();
+    pool.registerPlanNodeIdToQueryMemoryMap("budget", fi, "a" + side, side);
+    pool.registerPlanNodeIdToQueryMemoryMap("budget", fi, "b" + side, side);
+    assertTrue(
+        pool.reserveWithPriority("budget", fi, "a" + side, limit, limit, false).isReserveSuccess());
+    MemoryPool.MemoryReservationResult second =
+        pool.reserveWithPriority("budget", fi, "b" + side, limit, limit, false);
+    if (shared) {
+      assertFalse(second.getFuture().isDone());
+      pool.free("budget", fi, "a" + side, limit);
+      assertTrue(second.getFuture().isDone());
+    } else {
+      assertTrue(second.isReserveSuccess());
+      pool.free("budget", fi, "a" + side, limit);
+    }
+    pool.free("budget", fi, "b" + side, limit);
+  }
+
+  @Test
+  public void testRealSourceHandlesShareCanonicalFragmentAccountAndCleanup() throws Exception {
+    FragmentInstanceId id = context.getFragmentInstanceId();
+    String fi = FragmentInstanceId.createFragmentInstanceIdFromTFragmentInstanceId(id.toThrift());
+    LocalMemoryManager manager = mock(LocalMemoryManager.class);
+    when(manager.getQueryPool()).thenReturn(pool);
+    SourceHandle first = realSourceHandle(id, "first", manager);
+    SourceHandle second = realSourceHandle(id, "second", manager);
+    for (SourceHandle source : Arrays.asList(first, second)) {
+      ExchangeOperator exchange = mock(ExchangeOperator.class);
+      when(exchange.getSourceHandle()).thenReturn(source);
+      context.addExchangeOperator(exchange);
+    }
+    try {
+      context.setExchangeMemoryLimits(collect(), pool);
+      assertEquals(1024, context.getInstanceContext().getInputMaxBytes());
+      first.isBlocked();
+      second.isBlocked();
+      first.updatePendingDataBlockInfo(0, List.of(1024L));
+      second.updatePendingDataBlockInfo(0, List.of(1024L));
+      assertEquals(1024, pool.getFragmentMemoryReservedBytes("budget", fi, MemorySide.INPUT));
+      assertEquals(0, pool.getPlanNodeMemoryReservedBytes("budget", fi, "second"));
+      assertEquals(1, pool.getMemoryReservationSize());
+      first.abort();
+      assertEquals(1024, pool.getPlanNodeMemoryReservedBytes("budget", fi, "second"));
+    } finally {
+      first.abort();
+      second.abort();
+      pool.deRegisterFragmentInstanceFromQueryMemoryMap("budget", fi, true);
+    }
+    assertEquals(0, pool.getMemoryReservationSize());
+    assertEquals(0, pool.getReservedBytes());
+    assertEquals(0, pool.getQueryMemoryReservationSize());
+  }
+
+  private static SourceHandle realSourceHandle(
+      FragmentInstanceId id, String planNode, LocalMemoryManager manager) {
+    return new SourceHandle(
+        new TEndPoint("remote", 12345),
+        new TFragmentInstanceId(id.getQueryId().getId(), 1, "0"),
+        id.toThrift(),
+        planNode,
+        0,
+        manager,
+        mock(ExecutorService.class),
+        new TsBlockSerde(),
+        mock(SourceHandleListener.class),
+        mock(IClientManager.class));
+  }
+
+  @Test
+  public void testPureCollectUsesSharedSideBudgets() {
+    ISourceHandle source = addSource(context);
+    ISourceHandle secondSource = addSource(context);
+    ISinkHandle sink = mock(ISinkHandle.class);
+    context.setISink(sink);
+    context.setExchangeMemoryLimits(node(PlanNodeType.IDENTITY_SINK, collect()), pool);
+    verify(source).setMaxBytesCanReserve(512);
+    verify(secondSource).setMaxBytesCanReserve(512);
+    verify(sink).setMaxBytesCanReserve(512);
+    assertAdmissionIsShared(true, MemorySide.INPUT, 512);
+    assertAdmissionIsShared(true, MemorySide.OUTPUT, 512);
+  }
+
+  @Test
+  public void testOddBudgetIsInitializedAfterBothDirectionsAreKnown() {
+    IoTDBDescriptor.getInstance().getMemoryConfig().setMaxBytesPerFragmentInstance(1025);
+    pool = new MemoryPool("odd-budget", new MemoryManager(4096), 1025);
+    context = newContext();
+    ISourceHandle source = addSource(context);
+    ISinkHandle sink = mock(ISinkHandle.class);
+    context.setISink(sink);
+    context.setExchangeMemoryLimits(node(PlanNodeType.IDENTITY_SINK, collect()), pool);
+    verify(source).setMaxBytesCanReserve(512);
+    verify(sink).setMaxBytesCanReserve(513);
+    assertAdmissionIsShared(true, MemorySide.INPUT, 512);
+    assertAdmissionIsShared(true, MemorySide.OUTPUT, 513);
+  }
+
+  @Test
+  public void testInputOnlyCollectUsesFullBudgetEvenForOneFullBlock() {
+    TSFileDescriptor.getInstance().getConfig().setMaxTsBlockSizeInBytes(1024);
+    context.setExchangeSumNum(1);
+    ISourceHandle source = addSource(context);
+    context.setExchangeMemoryLimits(
+        node(PlanNodeType.TABLE_COLLECT_NODE, node(PlanNodeType.TABLE_EXCHANGE_NODE)), pool);
+    verify(source).setMaxBytesCanReserve(1024);
+    assertEquals(0, context.getDriverContext().getFragmentInstanceContext().getOutputMaxBytes());
+    assertAdmissionIsShared(true);
+  }
+
+  @Test
+  public void testOutputOnlyCollectUsesFullBudgetEvenForOneFullBlock() {
+    TSFileDescriptor.getInstance().getConfig().setMaxTsBlockSizeInBytes(1024);
+    context.setExchangeSumNum(1);
+    ISinkHandle sink = mock(ISinkHandle.class);
+    context.setISink(sink);
+    context.setExchangeMemoryLimits(
+        node(
+            PlanNodeType.IDENTITY_SINK,
+            node(
+                PlanNodeType.TABLE_COLLECT_NODE,
+                node(PlanNodeType.SERIES_SCAN),
+                node(PlanNodeType.SERIES_SCAN))),
+        pool);
+    verify(sink).setMaxBytesCanReserve(1024);
+    assertEquals(0, context.getDriverContext().getFragmentInstanceContext().getInputMaxBytes());
+    assertAdmissionIsShared(true, MemorySide.OUTPUT);
+  }
+
+  private void addLocalInput() {
+    ExchangeOperator exchange = mock(ExchangeOperator.class);
+    when(exchange.getSourceHandle()).thenReturn(mock(LocalSourceHandle.class));
+    context.addExchangeOperator(exchange);
+  }
+
+  @Test
+  public void testLocalInputsDoNotSplitOutputBudget() {
+    TSFileDescriptor.getInstance().getConfig().setMaxTsBlockSizeInBytes(1024);
+    addLocalInput();
+    ISinkHandle sink = mock(ISinkHandle.class);
+    context.setISink(sink);
+    context.setExchangeMemoryLimits(node(PlanNodeType.IDENTITY_SINK, collect()), pool);
+    verify(sink).setMaxBytesCanReserve(1024);
+    assertEquals(0, context.getDriverContext().getFragmentInstanceContext().getInputMaxBytes());
+    assertAdmissionIsShared(true, MemorySide.OUTPUT);
+  }
+
+  @Test
+  public void testMixedLocalAndRemoteInputsStillSplitBidirectionalBudget() {
+    addLocalInput();
+    ISourceHandle source = addSource(context);
+    ISinkHandle sink = mock(ISinkHandle.class);
+    context.setISink(sink);
+    context.setExchangeMemoryLimits(node(PlanNodeType.IDENTITY_SINK, collect()), pool);
+    verify(source).setMaxBytesCanReserve(512);
+    verify(sink).setMaxBytesCanReserve(512);
+    assertAdmissionIsShared(true, MemorySide.INPUT, 512);
+    assertAdmissionIsShared(true, MemorySide.OUTPUT, 512);
+  }
+
+  @Test
+  public void testLocalInputsWithoutSinkDoNotRegisterUnusedSharedBudget() {
+    addLocalInput();
+    context.setExchangeMemoryLimits(collect(), pool);
+    assertAdmissionIsShared(false);
+  }
+
+  @Test
+  public void testMappingCollectUsesSharedBudget() {
+    addSource(context);
+    PlanNode union =
+        new UnionNode(
+            new PlanNodeId("union"),
+            Arrays.asList(
+                node(PlanNodeType.TABLE_EXCHANGE_NODE), node(PlanNodeType.TABLE_EXCHANGE_NODE)),
+            ArrayListMultimap.create(),
+            Collections.emptyList());
+    context.setExchangeMemoryLimits(union, pool);
+    assertAdmissionIsShared(true);
+  }
+
+  @Test
+  public void testTreePlanningRetainsLegacySourceQuota() {
+    ISourceHandle source = addSource(context);
+    context.setMaxBytesOneHandleCanReserve();
+    verify(source).setMaxBytesCanReserve(256);
+    assertFalse(
+        SharedExchangeBudgetEligibility.isEligible(
+            node(
+                PlanNodeType.TREE_COLLECT,
+                node(PlanNodeType.EXCHANGE),
+                node(PlanNodeType.EXCHANGE))));
+    assertAdmissionIsShared(false);
+  }
+
+  @Test
+  public void testEligibleTableCollectEnablesReadyConsumptionAndSharedBudget() {
+    PlanNode plan = collect();
+    CollectOperator collect = generateTableCollect(plan);
+    addSource(context);
+    context.setExchangeMemoryLimits(plan, pool);
+    assertTrue(collect.isBlocked().isDone());
+    assertEquals(80, collect.calculateRetainedSizeAfterCallingNext());
+    assertEquals(150, collect.calculateMaxPeekMemory());
+    assertAdmissionIsShared(true);
+  }
+
+  @Test
+  public void testEligibleTableUnionEnablesReadyConsumptionAndSharedBudget() {
+    PlanNode union =
+        new UnionNode(
+            new PlanNodeId("union"),
+            Arrays.asList(
+                node(PlanNodeType.TABLE_EXCHANGE_NODE), node(PlanNodeType.TABLE_EXCHANGE_NODE)),
+            ArrayListMultimap.create(),
+            Collections.emptyList());
+    CollectOperator collect = generateTableCollect(union);
+    addSource(context);
+    context.setExchangeMemoryLimits(union, pool);
+    assertTrue(collect.isBlocked().isDone());
+    assertEquals(80, collect.calculateRetainedSizeAfterCallingNext());
+    assertEquals(150, collect.calculateMaxPeekMemory());
+    assertAdmissionIsShared(true);
+  }
+
+  @Test
+  public void testSingleChildTopKRetainsLegacyBudgetAndConsumption() {
+    PlanNode child = collect();
+    CollectOperator collect = generateTableCollect(child);
+    ISourceHandle source = addSource(context);
+    PlanNode topK = node(PlanNodeType.TABLE_TOPK_NODE, child);
+    context.setExchangeMemoryLimits(topK, pool);
+    verify(source).setMaxBytesCanReserve(256);
+    assertTrue(collect.isBlocked().isDone());
+    assertAdmissionIsShared(false);
+    assertFalse(
+        SharedExchangeBudgetEligibility.isEligible(
+            node(PlanNodeType.TABLE_COLLECT_NODE, topK, collect())));
+    assertTrue(
+        SharedExchangeBudgetEligibility.isEligible(
+            node(
+                PlanNodeType.TABLE_COLLECT_NODE,
+                node(PlanNodeType.TABLE_EXCHANGE_NODE, topK),
+                node(PlanNodeType.TABLE_EXCHANGE_NODE))));
+  }
+
+  @Test
+  public void testMultiplePipelinesKeepLegacyBudgetWithReadyCollectConsumption() {
+    PlanNode plan = collect();
+    CollectOperator collect = generateTableCollect(plan);
+    LocalExecutionPlanContext child = context.createSubContext();
+    child.addPipelineDriverFactory(mock(Operator.class), child.getDriverContext(), 0);
+    addSource(context);
+    context.setExchangeMemoryLimits(plan, pool);
+    assertTrue(collect.isBlocked().isDone());
+    assertAdmissionIsShared(false);
+  }
+
+  @Test
+  public void testNestedDependencyDisablesSharingForEntireFragment() {
+    for (PlanNodeType dependency :
+        new PlanNodeType[] {
+          PlanNodeType.TABLE_MERGESORT_NODE, PlanNodeType.MERGE_SORT, PlanNodeType.AGG_MERGE_SORT,
+          PlanNodeType.TABLE_JOIN_NODE, PlanNodeType.TABLE_SEMI_JOIN_NODE,
+              PlanNodeType.TABLE_TOPK_NODE,
+          PlanNodeType.COUNT_MERGE, PlanNodeType.DEVICE_VIEW
+        }) {
+      PlanNode dependent =
+          node(
+              dependency,
+              node(PlanNodeType.TABLE_EXCHANGE_NODE),
+              node(PlanNodeType.TABLE_EXCHANGE_NODE));
+      assertFalse(
+          SharedExchangeBudgetEligibility.isEligible(
+              node(PlanNodeType.TABLE_COLLECT_NODE, dependent, collect())));
+      assertFalse(
+          SharedExchangeBudgetEligibility.isEligible(node(dependency, collect(), collect())));
+    }
+    ISourceHandle source = addSource(context);
+    ISinkHandle sink = mock(ISinkHandle.class);
+    context.setExchangeSumNum(2);
+    context.setISink(sink);
+    context.setExchangeSumNum(4);
+    context.setExchangeMemoryLimits(
+        node(
+            PlanNodeType.TABLE_COLLECT_NODE,
+            collect(),
+            node(
+                PlanNodeType.TABLE_MERGESORT_NODE,
+                node(PlanNodeType.TABLE_EXCHANGE_NODE),
+                node(PlanNodeType.TABLE_EXCHANGE_NODE))),
+        pool);
+    verify(source).setMaxBytesCanReserve(256);
+    // The legacy sink quota is captured at its original point in operator generation.
+    verify(sink).setMaxBytesCanReserve(512);
+    assertAdmissionIsShared(false);
+  }
+
+  @Test
+  public void testSequentialAndUnknownMultiInputOperatorsDisableSharing() {
+    assertFalse(
+        SharedExchangeBudgetEligibility.isEligible(
+            node(PlanNodeType.TABLE_COLLECT_NODE, collect(), collect())));
+    assertFalse(
+        SharedExchangeBudgetEligibility.isEligible(
+            node(PlanNodeType.IDENTITY_SINK, collect(), collect())));
+    assertFalse(
+        SharedExchangeBudgetEligibility.isEligible(
+            node(PlanNodeType.SCHEMA_QUERY_MERGE, collect(), collect())));
+    for (PlanNodeType multiChildSetOperation :
+        new PlanNodeType[] {PlanNodeType.TABLE_INTERSECT_NODE, PlanNodeType.TABLE_EXCEPT_NODE}) {
+      assertFalse(
+          SharedExchangeBudgetEligibility.isEligible(
+              node(
+                  multiChildSetOperation,
+                  node(PlanNodeType.TABLE_EXCHANGE_NODE),
+                  node(PlanNodeType.TABLE_EXCHANGE_NODE))));
+    }
+  }
+
+  @Test
+  public void testExchangeBoundaryDoesNotInspectUpstreamFragment() {
+    PlanNode remote = node(PlanNodeType.TABLE_MERGESORT_NODE, collect(), collect());
+    assertTrue(
+        SharedExchangeBudgetEligibility.isEligible(
+            node(
+                PlanNodeType.TABLE_COLLECT_NODE,
+                node(PlanNodeType.TABLE_EXCHANGE_NODE, remote),
+                node(PlanNodeType.TABLE_EXCHANGE_NODE))));
+  }
+
+  @Test
+  public void testSourceFragmentRootedAtExchangeKeepsLegacyBudget() {
+    // The source fragment's root is an ExchangeNode; its upstream child belongs to another
+    // fragment and must not make this fragment eligible for a shared budget.
+    assertFalse(
+        SharedExchangeBudgetEligibility.isEligible(
+            node(
+                PlanNodeType.TABLE_EXCHANGE_NODE,
+                node(PlanNodeType.TABLE_COLLECT_NODE, collect(), collect()))));
+    context.setExchangeMemoryLimits(
+        node(
+            PlanNodeType.TABLE_EXCHANGE_NODE,
+            node(PlanNodeType.TABLE_COLLECT_NODE, collect(), collect())),
+        pool);
+    assertAdmissionIsShared(false);
+  }
+
+  @Test
+  public void testNoCollectKeepsLegacyBudget() {
+    context.setExchangeMemoryLimits(
+        node(
+            PlanNodeType.TABLE_MERGESORT_NODE,
+            node(PlanNodeType.TABLE_EXCHANGE_NODE),
+            node(PlanNodeType.TABLE_EXCHANGE_NODE)),
+        pool);
+    assertAdmissionIsShared(false);
+  }
+
+  @Test
+  public void testInternalPipelineDisablesSharingForEntireFragment() {
+    LocalExecutionPlanContext child = context.createSubContext();
+    child.addPipelineDriverFactory(mock(Operator.class), child.getDriverContext(), 0);
+    ISourceHandle source = addSource(child);
+    PipelineSourceHandle pipelineSource = mock(PipelineSourceHandle.class);
+    ExchangeOperator exchange = mock(ExchangeOperator.class);
+    when(exchange.getSourceHandle()).thenReturn(pipelineSource);
+    context.addExchangeOperator(exchange);
+    context.setExchangeMemoryLimits(collect(), pool);
+    verify(source).setMaxBytesCanReserve(256);
+    verify(pipelineSource).setMaxBytesCanReserve(256);
+    assertAdmissionIsShared(false);
+  }
+
+  @Test
+  public void testPipelineSchedulingDependencyDisablesSharing() {
+    context.getDriverContext().setDependencyDriverIndex(0);
+    context.setExchangeMemoryLimits(collect(), pool);
+    assertAdmissionIsShared(false);
+  }
+
+  @Test
+  public void testSideSmallerThanOneBlockKeepsLegacyBudget() {
+    addSource(context);
+    context.setISink(mock(ISinkHandle.class));
+    TSFileDescriptor.getInstance().getConfig().setMaxTsBlockSizeInBytes(1024);
+    context.setExchangeMemoryLimits(collect(), pool);
+    assertAdmissionIsShared(false);
+  }
+
+  @Test
+  public void testSinkOnlyFragmentCanStillReserveOneFullBlock() {
+    IoTDBDescriptor.getInstance().getMemoryConfig().setMaxBytesPerFragmentInstance(64);
+    context = newContext();
+    context.setExchangeSumNum(1);
+    ISinkHandle sink = mock(ISinkHandle.class);
+    context.setISink(sink);
+    context.setExchangeMemoryLimits(
+        node(PlanNodeType.IDENTITY_SINK, node(PlanNodeType.SERIES_SCAN)), pool);
+    verify(sink).setMaxBytesCanReserve(64);
+  }
+}
diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/planner/LocalExecutionPlannerBudgetTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/planner/LocalExecutionPlannerBudgetTest.java
new file mode 100644
index 00000000000..ea9323fdfdb
--- /dev/null
+++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/planner/LocalExecutionPlannerBudgetTest.java
@@ -0,0 +1,245 @@
+/*
+ * 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.iotdb.db.queryengine.plan.planner;
+
+import org.apache.iotdb.calc.execution.operator.Operator;
+import org.apache.iotdb.calc.execution.operator.process.CollectOperator;
+import org.apache.iotdb.calc.plan.planner.memory.MemoryReservationManager;
+import org.apache.iotdb.commons.queryengine.common.SessionInfo;
+import org.apache.iotdb.commons.queryengine.common.SqlDialect;
+import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNode;
+import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNodeId;
+import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNodeType;
+import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.CollectNode;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.queryengine.common.FragmentInstanceId;
+import org.apache.iotdb.db.queryengine.common.PlanFragmentId;
+import org.apache.iotdb.db.queryengine.common.QueryId;
+import org.apache.iotdb.db.queryengine.execution.exchange.MPPDataExchangeService;
+import org.apache.iotdb.db.queryengine.execution.exchange.source.SourceHandle;
+import org.apache.iotdb.db.queryengine.execution.fragment.DataNodeQueryContext;
+import org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceContext;
+import org.apache.iotdb.db.queryengine.execution.fragment.FragmentInstanceStateMachine;
+import org.apache.iotdb.db.queryengine.execution.memory.MemoryPool;
+import org.apache.iotdb.db.queryengine.execution.memory.MemorySide;
+import org.apache.iotdb.db.queryengine.execution.operator.source.ExchangeOperator;
+import org.apache.iotdb.db.queryengine.plan.analyze.TypeProvider;
+
+import com.google.common.util.concurrent.SettableFuture;
+import org.apache.tsfile.common.conf.TSFileDescriptor;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class LocalExecutionPlannerBudgetTest {
+  private long previousBudget;
+  private int previousBlockSize;
+  private boolean previousEstimation;
+  private int previousDataNodeId;
+
+  @Before
+  public void setUp() {
+    previousDataNodeId = IoTDBDescriptor.getInstance().getConfig().getDataNodeId();
+    IoTDBDescriptor.getInstance().getConfig().setDataNodeId(0);
+    previousBudget =
+        IoTDBDescriptor.getInstance().getMemoryConfig().getMaxBytesPerFragmentInstance();
+    previousBlockSize = TSFileDescriptor.getInstance().getConfig().getMaxTsBlockSizeInBytes();
+    previousEstimation =
+        IoTDBDescriptor.getInstance().getMemoryConfig().isEnableQueryMemoryEstimation();
+    IoTDBDescriptor.getInstance().getMemoryConfig().setMaxBytesPerFragmentInstance(1024);
+    TSFileDescriptor.getInstance().getConfig().setMaxTsBlockSizeInBytes(64);
+    IoTDBDescriptor.getInstance().getMemoryConfig().setEnableQueryMemoryEstimation(true);
+  }
+
+  @After
+  public void tearDown() {
+    IoTDBDescriptor.getInstance().getConfig().setDataNodeId(previousDataNodeId);
+    IoTDBDescriptor.getInstance().getMemoryConfig().setMaxBytesPerFragmentInstance(previousBudget);
+    TSFileDescriptor.getInstance().getConfig().setMaxTsBlockSizeInBytes(previousBlockSize);
+    IoTDBDescriptor.getInstance()
+        .getMemoryConfig()
+        .setEnableQueryMemoryEstimation(previousEstimation);
+  }
+
+  @Test
+  public void testTablePlanEnablesSharingBeforeMemoryEstimationAndRootFactory() throws Exception {
+    LocalExecutionPlanner planner = LocalExecutionPlanner.getInstance();
+    MemoryPool pool =
+        MPPDataExchangeService.getInstance()
+            .getMPPDataExchangeManager()
+            .getLocalMemoryManager()
+            .getQueryPool();
+    FragmentInstanceContext instance = instance("table_planner_budget", SqlDialect.TABLE);
+    PlanNode first = mock(PlanNode.class);
+    PlanNode second = mock(PlanNode.class);
+    SourceHandle firstHandle = mock(SourceHandle.class);
+    SourceHandle secondHandle = mock(SourceHandle.class);
+    addInput(first, firstHandle, false, 100, 30);
+    addInput(second, secondHandle, true, 80, 50);
+    CollectNode plan =
+        spy(
+            new CollectNode(
+                new PlanNodeId("collect"), Arrays.asList(first, second), Collections.emptyList()));
+    doReturn(PlanNodeType.TABLE_COLLECT_NODE).when(plan).getType();
+    when(first.getChildren()).thenReturn(Collections.emptyList());
+    when(second.getChildren()).thenReturn(Collections.emptyList());
+    long freeBefore = planner.getFreeMemoryForOperators();
+    try {
+      List<PipelineDriverFactory> factories =
+          planner.plan(plan, new TypeProvider(), instance, new DataNodeQueryContext(1));
+      assertEquals(1, factories.size());
+      CollectOperator collect = (CollectOperator) factories.get(0).getOperation();
+      // Check the actual reservation, not only the formula after planning has completed.
+      assertEquals(150 + collect.ramBytesUsed(), freeBefore - planner.getFreeMemoryForOperators());
+      assertEquals(1024, instance.getInputMaxBytes());
+      assertEquals(0, instance.getOutputMaxBytes());
+      assertTrue(collect.isBlocked().isDone());
+      verify(firstHandle).setMaxBytesCanReserve(1024);
+      verify(secondHandle).setMaxBytesCanReserve(1024);
+
+      String query = instance.getId().getQueryId().getId();
+      String fi = instance.getId().getFragmentInstanceId();
+      pool.registerPlanNodeIdToQueryMemoryMap(query, fi, "first", MemorySide.INPUT);
+      pool.registerPlanNodeIdToQueryMemoryMap(query, fi, "second", MemorySide.INPUT);
+      assertTrue(
+          pool.reserveWithPriority(query, fi, "first", 1024, 1024, false).isReserveSuccess());
+      MemoryPool.MemoryReservationResult pending =
+          pool.reserveWithPriority(query, fi, "second", 64, 1024, false);
+      assertFalse(pending.getFuture().isDone());
+      pool.free(query, fi, "first", 1024);
+      assertTrue(pending.getFuture().isDone());
+      pool.free(query, fi, "second", 64);
+      collect.close();
+    } finally {
+      pool.deRegisterFragmentInstanceFromQueryMemoryMap(
+          instance.getId().getQueryId().getId(), instance.getId().getFragmentInstanceId(), true);
+      long reserved = freeBefore - planner.getFreeMemoryForOperators();
+      if (reserved > 0) {
+        planner.releaseToFreeMemoryForOperators(reserved);
+      }
+    }
+  }
+
+  @Test
+  public void testNullSessionRetainsTreeGeneratorAndIndependentInputQuotas() throws Exception {
+    FragmentInstanceContext instance = instance("null_session_planner_budget", null);
+    PlanNode plan = mock(PlanNode.class);
+    when(plan.getType()).thenReturn(PlanNodeType.TREE_COLLECT);
+    when(plan.getPlanNodeId()).thenReturn(new PlanNodeId("collect"));
+    SourceHandle firstHandle = mock(SourceHandle.class);
+    SourceHandle secondHandle = mock(SourceHandle.class);
+    Operator first = input(false, 100, 30);
+    Operator second = input(true, 80, 50);
+    doAnswer(
+            invocation -> {
+              assertTrue(invocation.getArgument(0) instanceof OperatorTreeGenerator);
+              LocalExecutionPlanContext context = invocation.getArgument(1);
+              registerInput(context, firstHandle);
+              registerInput(context, secondHandle);
+              CollectOperator collect =
+                  new CollectOperator(
+                      context
+                          .getDriverContext()
+                          .addOperatorContext(0, new PlanNodeId("collect"), "CollectOperator"),
+                      new java.util.ArrayList<>(Arrays.asList(first, second)));
+              return collect;
+            })
+        .when(plan)
+        .accept(any(), any());
+    LocalExecutionPlanner planner = LocalExecutionPlanner.getInstance();
+    long freeBefore = planner.getFreeMemoryForOperators();
+    try {
+      List<PipelineDriverFactory> factories =
+          planner.plan(plan, new TypeProvider(), instance, new DataNodeQueryContext(1));
+      assertEquals(1, factories.size());
+      CollectOperator collect = (CollectOperator) factories.get(0).getOperation();
+      assertEquals(150 + collect.ramBytesUsed(), freeBefore - planner.getFreeMemoryForOperators());
+      assertTrue(collect.isBlocked().isDone());
+      assertEquals(0, instance.getInputMaxBytes());
+      verify(firstHandle).setMaxBytesCanReserve(512);
+      verify(secondHandle).setMaxBytesCanReserve(512);
+      collect.close();
+    } finally {
+      long reserved = freeBefore - planner.getFreeMemoryForOperators();
+      if (reserved > 0) {
+        planner.releaseToFreeMemoryForOperators(reserved);
+      }
+    }
+  }
+
+  private static FragmentInstanceContext instance(String query, SqlDialect dialect) {
+    FragmentInstanceContext instance =
+        spy(
+            FragmentInstanceContext.createFragmentInstanceContext(
+                new FragmentInstanceId(new PlanFragmentId(new QueryId(query), 0), "0"),
+                mock(FragmentInstanceStateMachine.class),
+                mock(MemoryReservationManager.class)));
+    SessionInfo session = null;
+    if (dialect != null) {
+      session = mock(SessionInfo.class);
+      when(session.getSqlDialect()).thenReturn(dialect);
+    }
+    doReturn(session).when(instance).getSessionInfo();
+    return instance;
+  }
+
+  private static void addInput(
+      PlanNode node, SourceHandle handle, boolean ready, long peak, long retained) {
+    Operator operator = input(ready, peak, retained);
+    doAnswer(
+            invocation -> {
+              LocalExecutionPlanContext context = invocation.getArgument(1);
+              registerInput(context, handle);
+              return operator;
+            })
+        .when(node)
+        .accept(any(), any());
+  }
+
+  private static void registerInput(LocalExecutionPlanContext context, SourceHandle handle) {
+    ExchangeOperator exchange = mock(ExchangeOperator.class);
+    when(exchange.getSourceHandle()).thenReturn(handle);
+    context.addExchangeOperator(exchange);
+    context.addExchangeSumNum(1);
+  }
+
+  private static Operator input(boolean ready, long peak, long retained) {
+    Operator operator = mock(Operator.class);
+    doReturn(ready ? Operator.NOT_BLOCKED : SettableFuture.create()).when(operator).isBlocked();
+    when(operator.calculateMaxPeekMemoryWithCounter()).thenReturn(peak);
+    when(operator.calculateRetainedSizeAfterCallingNext()).thenReturn(retained);
+    return operator;
+  }
+}
diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/planner/CollectInputPropertyTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/planner/CollectInputPropertyTest.java
new file mode 100644
index 00000000000..3f4240c7827
--- /dev/null
+++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/planner/CollectInputPropertyTest.java
@@ -0,0 +1,163 @@
+/*
+ * 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.iotdb.db.queryengine.plan.relational.planner;
+
+import org.apache.iotdb.commons.queryengine.plan.planner.plan.node.PlanNode;
+import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.AggregationNode;
+import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.CollectNode;
+import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.GroupNode;
+import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.SortNode;
+import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.WindowNode;
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class CollectInputPropertyTest {
+  private static List<PlanNode> plans(String sql) {
+    PlanTester tester = new PlanTester();
+    tester.createPlan(sql);
+    List<PlanNode> result = new ArrayList<>();
+    for (int i = 0; i < tester.getDistributedPlan().getFragments().size(); i++) {
+      result.add(tester.getFragmentPlan(i));
+    }
+    return result;
+  }
+
+  private static <T> List<T> nodes(PlanNode node, Class<T> type) {
+    List<T> result = new ArrayList<>();
+    if (type.isInstance(node)) result.add(type.cast(node));
+    for (PlanNode child : node.getChildren()) result.addAll(nodes(child, type));
+    return result;
+  }
+
+  private static <T> List<T> nodes(List<PlanNode> plans, Class<T> type) {
+    List<T> result = new ArrayList<>();
+    for (PlanNode plan : plans) {
+      result.addAll(nodes(plan, type));
+    }
+    return result;
+  }
+
+  private static List<PlanNode> allNodes(PlanNode node) {
+    List<PlanNode> result = new ArrayList<>();
+    result.add(node);
+    for (PlanNode child : node.getChildren()) {
+      result.addAll(allNodes(child));
+    }
+    return result;
+  }
+
+  @Test
+  public void testAggregationAboveGlobalWindowHashesInterleavedGroups() {
+    for (String aggregate : new String[] {"SUM(s1)", "COUNT(DISTINCT s1)"}) {
+      List<PlanNode> fragments =
+          plans(
+              "SELECT tag1, tag2, tag3, "
+                  + aggregate
+                  + " FROM ("
+                  + "SELECT tag1, tag2, tag3, s1, COUNT(*) OVER () AS n "
+                  + "FROM table1 WHERE tag1 != 'shenzhen') t WHERE n > 0 GROUP BY tag1, tag2, tag3");
+      PlanNode root = fragments.get(0);
+      List<AggregationNode> aggregations = nodes(fragments, AggregationNode.class);
+      assertFalse(aggregations.isEmpty());
+      assertFalse(nodes(fragments, CollectNode.class).isEmpty());
+      for (AggregationNode aggregation : aggregations) {
+        if (!nodes(aggregation.getChild(), CollectNode.class).isEmpty()) {
+          assertTrue(aggregation.getPreGroupedSymbols().isEmpty());
+          assertFalse(aggregation.isStreamable());
+        }
+      }
+      assertTrue(
+          "Hash aggregation should not add a grouping sort", nodes(root, SortNode.class).isEmpty());
+    }
+  }
+
+  @Test
+  public void testPartialPreGroupingAlsoFallsBackToHash() {
+    List<PlanNode> fragments =
+        plans(
+            "SELECT tag1, tag2, tag3, s1, COUNT(*) FROM ("
+                + "SELECT tag1, tag2, tag3, s1, COUNT(*) OVER () AS n "
+                + "FROM table1 WHERE tag1 != 'shenzhen') t WHERE n > 0 GROUP BY tag1, tag2, tag3, s1");
+    List<AggregationNode> aggregations = nodes(fragments, AggregationNode.class);
+    assertFalse(aggregations.isEmpty());
+    assertFalse(nodes(fragments, CollectNode.class).isEmpty());
+    assertTrue(aggregations.get(0).getPreGroupedSymbols().isEmpty());
+  }
+
+  @Test
+  public void testNestedWindowRestoresPartitionContiguityWithSort() {
+    List<PlanNode> fragments =
+        plans(
+            "SELECT tag1, tag2, tag3, SUM(s1) OVER ("
+                + "PARTITION BY tag1, tag2, tag3 ORDER BY s1) AS running_sum "
+                + "FROM (SELECT tag1, tag2, tag3, s1, COUNT(*) OVER () AS n FROM table1) t WHERE n > 0");
+    PlanNode root = fragments.get(0);
+    assertFalse(nodes(fragments, CollectNode.class).isEmpty());
+    assertTrue(nodes(root, GroupNode.class).isEmpty());
+    List<WindowNode> windows = nodes(root, WindowNode.class);
+    assertEquals(2, windows.size());
+    WindowNode outer =
+        windows.stream()
+            .filter(w -> !w.getSpecification().getPartitionBy().isEmpty())
+            .findFirst()
+            .get();
+    assertFalse(nodes(outer.getChild(), SortNode.class).isEmpty());
+  }
+
+  @Test
+  public void testEveryFragmentHasAtMostOneMultiChildLevel() {
+    String[] queries = {
+      "SELECT tag1, tag2, tag3, SUM(s1) FROM table1 GROUP BY tag1, tag2, tag3",
+      "SELECT tag1, tag2, tag3, SUM(s1) FROM ("
+          + "SELECT tag1, tag2, tag3, s1 FROM table1 UNION ALL "
+          + "SELECT tag1, tag2, tag3, s1 FROM table2) u GROUP BY tag1, tag2, tag3",
+      "SELECT tag1, tag2, tag3, SUM(s1) FROM ("
+          + "SELECT tag1, tag2, tag3, s1, COUNT(*) OVER () AS n "
+          + "FROM table1 WHERE tag1 != 'shenzhen') t WHERE n > 0 GROUP BY tag1, tag2, tag3"
+    };
+    for (String sql : queries) {
+      for (PlanNode fragment : plans(sql)) {
+        int multiChildLevels = 0;
+        for (PlanNode node : allNodes(fragment)) {
+          if (node.getChildren().size() > 1) {
+            multiChildLevels++;
+          }
+        }
+        assertTrue("Fragment contains more than one multi-child level", multiChildLevels <= 1);
+      }
+    }
+  }
+
+  @Test
+  public void testOrderedScanBranchesKeepStreamingAggregation() {
+    List<PlanNode> fragments =
+        plans("SELECT tag1, tag2, tag3, SUM(s1 + 1) FROM table1 GROUP BY tag1, tag2, tag3");
+    List<AggregationNode> aggregations = nodes(fragments, AggregationNode.class);
+    assertFalse(aggregations.isEmpty());
+    assertTrue(aggregations.stream().anyMatch(AggregationNode::isStreamable));
+  }
+}
diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/ParallelizeGroupingTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/ParallelizeGroupingTest.java
index e2326688112..ab852fddb07 100644
--- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/ParallelizeGroupingTest.java
+++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/ParallelizeGroupingTest.java
@@ -26,6 +26,9 @@ import org.apache.iotdb.commons.queryengine.plan.relational.metadata.QualifiedOb
 import org.apache.iotdb.commons.queryengine.plan.relational.planner.OrderingScheme;
 import org.apache.iotdb.commons.queryengine.plan.relational.planner.Symbol;
 import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.GroupNode;
+import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.SortNode;
+import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.UnionNode;
+import org.apache.iotdb.commons.queryengine.plan.relational.planner.node.ValuesNode;
 import org.apache.iotdb.db.queryengine.common.MPPQueryContext;
 import org.apache.iotdb.db.queryengine.common.QueryId;
 import org.apache.iotdb.db.queryengine.execution.warnings.WarningCollector;
@@ -36,6 +39,7 @@ import org.apache.iotdb.db.queryengine.plan.relational.planner.SymbolAllocator;
 import org.apache.iotdb.db.queryengine.plan.relational.planner.node.ExternalTsFileScanNode;
 
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableListMultimap;
 import com.google.common.collect.ImmutableMap;
 import org.junit.Test;
 
@@ -48,6 +52,7 @@ import static org.apache.iotdb.commons.schema.table.column.TsTableColumnCategory
 import static org.apache.iotdb.db.queryengine.plan.relational.analyzer.TestUtils.SESSION_INFO;
 import static org.apache.iotdb.db.queryengine.plan.relational.analyzer.TestUtils.TEST_MATADATA;
 import static org.apache.tsfile.read.common.type.StringType.STRING;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 
@@ -108,4 +113,57 @@ public class ParallelizeGroupingTest {
       resource.closeByQueryExecution();
     }
   }
+
+  @Test
+  public void testUnionDoesNotInheritFirstBranchGrouping() {
+    Symbol key = new Symbol("key");
+    OrderingScheme ordering =
+        new OrderingScheme(ImmutableList.of(key), ImmutableMap.of(key, ASC_NULLS_LAST));
+    PlanNode left =
+        new SortNode(
+            new PlanNodeId("leftSort"),
+            new ValuesNode(new PlanNodeId("left"), ImmutableList.of(key), ImmutableList.of()),
+            ordering,
+            false,
+            false);
+    PlanNode right =
+        new SortNode(
+            new PlanNodeId("rightSort"),
+            new ValuesNode(new PlanNodeId("right"), ImmutableList.of(key), ImmutableList.of()),
+            ordering,
+            false,
+            false);
+    UnionNode union =
+        new UnionNode(
+            new PlanNodeId("union"),
+            ImmutableList.of(left, right),
+            ImmutableListMultimap.of(key, key, key, key),
+            ImmutableList.of(key));
+    PlanNode optimized = optimizeGroup(new GroupNode(new PlanNodeId("group"), union, ordering, 1));
+    assertTrue(optimized instanceof SortNode);
+    // The multi-branch fan-in must not leave a segmented GroupNode above it.
+    assertFalse(optimized instanceof GroupNode);
+    // An explicit sort above the fan-in restores the grouping property.
+    SortNode restored = new SortNode(new PlanNodeId("restored"), union, ordering, false, false);
+    assertTrue(
+        optimizeGroup(new GroupNode(new PlanNodeId("group2"), restored, ordering, 1))
+            instanceof GroupNode);
+  }
+
+  private PlanNode optimizeGroup(GroupNode group) {
+    Analysis analysis = new Analysis(null, Collections.emptyMap());
+    analysis.setQuery(true);
+    return new ParallelizeGrouping()
+        .optimize(
+            group,
+            new PlanOptimizer.Context(
+                SESSION_INFO,
+                analysis,
+                TEST_MATADATA,
+                new MPPQueryContext(new QueryId("group_union")),
+                new SymbolAllocator(),
+                new QueryId("group_union"),
+                WarningCollector.NOOP,
+                PlanOptimizersStatsCollector.createPlanOptimizersStatsCollector()));
+  }
 }