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

Wei-hao-Li pushed a commit to branch fragmentCSRPC
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit c5d363c2cb961d3e7bbcfcfacb0f29aefb1c685b
Author: Weihao Li <[email protected]>
AuthorDate: Fri Nov 13 16:47:36 2026 +0800

    done
    
    Signed-off-by: Weihao Li <[email protected]>
---
 .../v1/handler/QueryDataSetHandlerTest.java        |   4 +-
 .../java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java |  11 +-
 .../apache/iotdb/session/SessionConnection.java    |   3 +-
 .../protocol/thrift/impl/ClientRPCServiceImpl.java | 125 +++++-----
 .../execution/exchange/source/ISourceHandle.java   |  12 +-
 .../exchange/source/LocalSourceHandle.java         |   8 +-
 .../execution/exchange/source/SourceHandle.java    |  97 ++++----
 .../plan/execution/IQueryExecution.java            |  10 +-
 .../plan/execution/PendingResultFragment.java      |  30 ++-
 .../queryengine/plan/execution/QueryExecution.java |  16 +-
 .../plan/execution/config/ConfigExecution.java     |   7 +-
 .../plan/execution/memory/MemorySourceHandle.java  |   8 +-
 .../apache/iotdb/db/utils/QueryDataSetUtils.java   |  33 +--
 .../db/protocol/session/QueryOwnershipTest.java    |   2 +-
 .../execution/exchange/SourceHandleTest.java       | 116 ++++++++++
 .../operator/MergeTreeSortOperatorTest.java        |   4 +-
 .../iotdb/db/utils/QueryDataSetUtilsTest.java      |  57 ++++-
 .../iotdb/db/utils/ResultChunkingEndToEndTest.java | 255 +++++++++++++++++++++
 18 files changed, 615 insertions(+), 183 deletions(-)

diff --git 
a/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/v1/handler/QueryDataSetHandlerTest.java
 
b/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/v1/handler/QueryDataSetHandlerTest.java
index f8aa9c95a2b..37fa74b9bd5 100644
--- 
a/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/v1/handler/QueryDataSetHandlerTest.java
+++ 
b/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/v1/handler/QueryDataSetHandlerTest.java
@@ -89,7 +89,7 @@ public class QueryDataSetHandlerTest {
 
     private final Queue<Optional<TsBlock>> batches = new ArrayDeque<>();
     private final DatasetHeader datasetHeader;
-    private final PendingResultFragment pendingResultFragment = new 
PendingResultFragment();
+    private final PendingResultFragment pendingResultFragment = new 
PendingResultFragment(() -> {});
 
     private TestQueryExecution(TsBlock... tsBlocks) {
       for (TsBlock tsBlock : tsBlocks) {
@@ -126,7 +126,7 @@ public class QueryDataSetHandlerTest {
     }
 
     @Override
-    public Optional<ByteBuffer> getByteBufferBatchResult() {
+    public Optional<ByteBuffer> getByteBufferBatchResult(boolean retainMemory) 
{
       return Optional.empty();
     }
 
diff --git 
a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java
 
b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java
index f9669d56d7a..a88f473649f 100644
--- 
a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java
+++ 
b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java
@@ -332,7 +332,6 @@ public class IoTDBRpcDataSet {
       } else {
         queryResult = new ArrayList<>();
         queryResultIndex = 0;
-        queryResultSize = 0;
         addResponse(resp.getQueryResult(), resp.isSetOffset(), resp.offset, 
resp.totalLength);
         queryResultSize = queryResult.size();
         this.tsBlockSize = 0;
@@ -351,11 +350,6 @@ public class IoTDBRpcDataSet {
    * the fragment is either appended to the pending {@link 
ResultFragmentBuffer} or starts a new
    * one; the corresponding TsBlock is only added to {@link #queryResult} once 
all of its bytes have
    * been received.
-   *
-   * @param blocks the serialized TsBlocks in the response
-   * @param offsetSet whether the last element of {@code blocks} is a fragment
-   * @param nextOffset the start offset of the next fragment
-   * @param totalLength the total serialized length of the block the first 
fragment belongs to
    */
   private void addResponse(
       List<ByteBuffer> blocks, boolean offsetSet, int nextOffset, int 
totalLength) {
@@ -365,7 +359,6 @@ public class IoTDBRpcDataSet {
     int blockIndex = 0;
 
     if (fragmentBuffer != null) {
-      // the first element completes the pending fragment, if it is not the 
only element
       fragmentBuffer.append(blocks.get(blockIndex++));
       if (offsetSet && blockIndex == blocks.size()) {
         fragmentBuffer.updateOffset(nextOffset);
@@ -418,7 +411,9 @@ public class IoTDBRpcDataSet {
     }
 
     private void updateOffset(int nextOffset) {
-      if (nextOffset <= this.nextOffset || nextOffset != buffer.size()) {
+      // the accumulated buffer only grows with non-empty fragments, so this 
also rejects a
+      // non-increasing offset
+      if (nextOffset != buffer.size()) {
         throw new IllegalArgumentException(
             String.format(
                 RpcMessages
diff --git 
a/iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java
 
b/iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java
index d0b6e8dba52..9808b69f96b 100644
--- 
a/iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java
+++ 
b/iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java
@@ -102,6 +102,7 @@ public class SessionConnection {
   private static final Logger logger = 
LoggerFactory.getLogger(SessionConnection.class);
   private static final String USE_ENCRYPTED_PASSWORD_KEY = 
"use_encrypted_password";
   private static final String ENABLE_AUTO_CREATE_SCHEMA_KEY = 
"enable_auto_create_schema";
+  private static final String ENABLE_RESULT_CHUNKING_KEY = 
"enable_result_chunking";
   public static final String MSG_RECONNECTION_FAIL =
       
SessionMessages.EXCEPTION_FAIL_TO_RECONNECT_TO_SERVER_PLEASE_CHECK_SERVER_STATUS_D9A2790F;
   protected Session session;
@@ -247,7 +248,7 @@ public class SessionConnection {
     openReq.putToConfiguration("version", session.version.toString());
     openReq.putToConfiguration("sql_dialect", sqlDialect);
     // this client can assemble fragmented query results, see 
TSFetchResultsReq#offset
-    openReq.putToConfiguration("enable_result_chunking", 
Boolean.TRUE.toString());
+    openReq.putToConfiguration(ENABLE_RESULT_CHUNKING_KEY, 
Boolean.TRUE.toString());
     if (session.useEncryptedPassword) {
       openReq.putToConfiguration(USE_ENCRYPTED_PASSWORD_KEY, 
Boolean.TRUE.toString());
     }
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java
index 614774a42af..c911e0bcf0c 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java
@@ -312,27 +312,8 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
   }
 
   private static final SelectResult SELECT_RESULT =
-      (resp, queryExecution, fetchSize, maxPayloadSize) -> {
-        QueryDataSetUtils.ResultChunk chunk;
-        if (maxPayloadSize > 0) {
-          // The execute request carries no offset, so it must start a new 
result set
-          chunk =
-              QueryDataSetUtils.convertQueryResultByFetchSize(
-                  queryExecution, fetchSize, 0, maxPayloadSize);
-        } else {
-          Pair<List<ByteBuffer>, Boolean> pair =
-              QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, 
fetchSize);
-          chunk = QueryDataSetUtils.ResultChunk.complete(pair.left, 
pair.right);
-        }
-        resp.setQueryResult(chunk.getQueryResult());
-        if (chunk.hasNextOffset()) {
-          resp.setOffset(chunk.getNextOffset());
-          if (chunk.hasTotalLength()) {
-            resp.setTotalLength(chunk.getTotalLength());
-          }
-        }
-        return chunk.isFinished();
-      };
+      (resp, queryExecution, fetchSize, maxPayloadSize) ->
+          fillQueryResult(resp, fetchResultChunk(queryExecution, fetchSize, 0, 
maxPayloadSize));
 
   private static final SelectResult OLD_SELECT_RESULT =
       (resp, queryExecution, fetchSize, maxPayloadSize) -> {
@@ -341,6 +322,46 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
         return pair.right;
       };
 
+  /**
+   * Fetches one response-worth of query results. Fragmentation is disabled 
when {@code
+   * maxPayloadSize} is not positive, i.e. the client cannot assemble 
fragments.
+   */
+  private static QueryDataSetUtils.ResultChunk fetchResultChunk(
+      IQueryExecution queryExecution, int fetchSize, int requestOffset, int 
maxPayloadSize)
+      throws IoTDBException {
+    if (maxPayloadSize > 0) {
+      return QueryDataSetUtils.convertQueryResultByFetchSize(
+          queryExecution, fetchSize, requestOffset, maxPayloadSize);
+    }
+    Pair<List<ByteBuffer>, Boolean> pair =
+        QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, 
fetchSize);
+    return QueryDataSetUtils.ResultChunk.complete(pair.left, pair.right);
+  }
+
+  private static boolean fillQueryResult(
+      TSExecuteStatementResp resp, QueryDataSetUtils.ResultChunk chunk) {
+    resp.setQueryResult(chunk.getQueryResult());
+    if (chunk.hasNextOffset()) {
+      resp.setOffset(chunk.getNextOffset());
+      if (chunk.hasTotalLength()) {
+        resp.setTotalLength(chunk.getTotalLength());
+      }
+    }
+    return chunk.isFinished();
+  }
+
+  private static boolean fillQueryResult(
+      TSFetchResultsResp resp, QueryDataSetUtils.ResultChunk chunk) {
+    resp.setQueryResult(chunk.getQueryResult());
+    if (chunk.hasNextOffset()) {
+      resp.setOffset(chunk.getNextOffset());
+      if (chunk.hasTotalLength()) {
+        resp.setTotalLength(chunk.getTotalLength());
+      }
+    }
+    return chunk.isFinished();
+  }
+
   /**
    * Returns the maximum payload size of one response carrying query results, 
or 0 if the session's
    * client cannot assemble fragmented results. The payload bound is shared 
with the MPP data
@@ -1615,35 +1636,15 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
       statementType = queryExecution.getStatementType();
 
       try (SetThreadName queryName = new 
SetThreadName(queryExecution.getQueryId())) {
-        if (req.isSetOffset()) {
-          // The client sets offset (0 for the first request) iff it can 
assemble fragment results
-          QueryDataSetUtils.ResultChunk chunk =
-              QueryDataSetUtils.convertQueryResultByFetchSize(
-                  queryExecution,
-                  req.fetchSize,
-                  req.getOffset(),
-                  config.getMppDataExchangeMaxPayloadSizeInBytes());
-          finished = chunk.isFinished();
-          resp.setHasResultSet(!chunk.getQueryResult().isEmpty());
-          resp.setIsAlign(true);
-          resp.setQueryResult(chunk.getQueryResult());
-          if (chunk.hasNextOffset()) {
-            resp.setOffset(chunk.getNextOffset());
-            if (chunk.hasTotalLength()) {
-              resp.setTotalLength(chunk.getTotalLength());
-            }
-          }
-          resp.setMoreData(!finished);
-          return resp;
-        }
-        Pair<List<ByteBuffer>, Boolean> pair =
-            QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, 
req.fetchSize);
-        List<ByteBuffer> result = pair.left;
-        finished = pair.right;
-        boolean hasResultSet = !result.isEmpty();
-        resp.setHasResultSet(hasResultSet);
+        QueryDataSetUtils.ResultChunk chunk =
+            fetchResultChunk(
+                queryExecution,
+                req.fetchSize,
+                req.isSetOffset() ? req.getOffset() : 0,
+                req.isSetOffset() ? 
config.getMppDataExchangeMaxPayloadSizeInBytes() : 0);
+        finished = fillQueryResult(resp, chunk);
+        resp.setHasResultSet(!chunk.getQueryResult().isEmpty());
         resp.setIsAlign(true);
-        resp.setQueryResult(result);
         resp.setMoreData(!finished);
         return resp;
       }
@@ -1855,7 +1856,8 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
 
   @Override
   public TSExecuteStatementResp executePreparedStatement(TSExecutePreparedReq 
req) {
-    return executeStatementInternal(new TSExecutePreparedReqAdapter(req), 
setResultForPrepared);
+    // Use V2 format (queryResult) to match IoTDBTablePreparedStatement client
+    return executeStatementInternal(new TSExecutePreparedReqAdapter(req), 
SELECT_RESULT);
   }
 
   @Override
@@ -1876,29 +1878,6 @@ public class ClientRPCServiceImpl implements 
IClientRPCServiceWithHandler {
     }
   }
 
-  private final SelectResult setResultForPrepared =
-      (resp, queryExecution, fetchSize, maxPayloadSize) -> {
-        // Use V2 format (queryResult) to match IoTDBTablePreparedStatement 
client
-        QueryDataSetUtils.ResultChunk chunk;
-        if (maxPayloadSize > 0) {
-          chunk =
-              QueryDataSetUtils.convertQueryResultByFetchSize(
-                  queryExecution, fetchSize, 0, maxPayloadSize);
-        } else {
-          Pair<List<ByteBuffer>, Boolean> pair =
-              QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, 
fetchSize);
-          chunk = QueryDataSetUtils.ResultChunk.complete(pair.left, 
pair.right);
-        }
-        resp.setQueryResult(chunk.getQueryResult());
-        if (chunk.hasNextOffset()) {
-          resp.setOffset(chunk.getNextOffset());
-          if (chunk.hasTotalLength()) {
-            resp.setTotalLength(chunk.getTotalLength());
-          }
-        }
-        return chunk.isFinished();
-      };
-
   // ========================= End PreparedStatement RPC Methods 
=========================
 
   @Override
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/ISourceHandle.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/ISourceHandle.java
index 32e5557b8f9..210c2ea4800 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/ISourceHandle.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/ISourceHandle.java
@@ -50,9 +50,19 @@ public interface ISourceHandle extends Accountable {
    * iterator with receive(). When one of these two methods is called, the 
cursor in iterator will
    * forward.
    *
+   * @param retainMemory if true, the memory that the returned block retains 
in the memory pool is
+   *     not freed when the block is taken out of this handle. The caller must 
release it via {@link
+   *     #freeRetainedMemory()} once the block is no longer needed. 
Implementations whose serialized
+   *     blocks are not reserved from the memory pool ignore this flag.
    * @throws IoTDBException throw IoTDBException
    */
-  ByteBuffer getSerializedTsBlock() throws IoTDBException;
+  ByteBuffer getSerializedTsBlock(boolean retainMemory) throws IoTDBException;
+
+  /**
+   * Releases the memory retained by the blocks taken out with {@code 
retainMemory=true} and not
+   * released yet. It is safe to call when nothing is retained, and after 
abort/close.
+   */
+  void freeRetainedMemory();
 
   /** If there are more TsBlocks. */
   boolean isFinished();
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/LocalSourceHandle.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/LocalSourceHandle.java
index 54442171266..94f6e09fa05 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/LocalSourceHandle.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/LocalSourceHandle.java
@@ -153,7 +153,8 @@ public class LocalSourceHandle implements ISourceHandle {
   }
 
   @Override
-  public ByteBuffer getSerializedTsBlock() throws IoTDBException {
+  public ByteBuffer getSerializedTsBlock(boolean retainMemory) throws 
IoTDBException {
+    // the returned bytes are a fresh serialization, not reserved from the 
memory pool
     TsBlock tsBlock = receive();
     if (LOGGER.isDebugEnabled()) {
       LOGGER.debug(DataNodeQueryMessages.GET_SERIALIZED_TSBLOCK, 
CommonUtils.toString(tsBlock));
@@ -174,6 +175,11 @@ public class LocalSourceHandle implements ISourceHandle {
     }
   }
 
+  @Override
+  public void freeRetainedMemory() {
+    // nothing is reserved from the memory pool
+  }
+
   @Override
   public boolean isFinished() {
     synchronized (queue) {
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 8ee7ad601ef..703ce3407f6 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
@@ -109,6 +109,13 @@ public class SourceHandle implements ISourceHandle {
   /** The actual buffered memory in bytes, including the amount of memory 
being reserved. */
   private long bufferRetainedSizeInBytes = 0L;
 
+  /**
+   * The memory in bytes taken out of the buffer with {@code 
retainMemory=true}, still reserved in
+   * the memory pool on behalf of the caller. Freed by {@link 
#freeRetainedMemory()}, or by
+   * abort/close as a fallback.
+   */
+  private long pendingRetainedSizeInBytes = 0L;
+
   private int currSequenceId = 0;
   private int nextSequenceId = 0;
   private int lastSequenceId = Integer.MAX_VALUE;
@@ -232,7 +239,7 @@ public class SourceHandle implements ISourceHandle {
 
   @Override
   public synchronized TsBlock receive() {
-    ByteBuffer tsBlock = getSerializedTsBlock();
+    ByteBuffer tsBlock = getSerializedTsBlock(false);
     if (tsBlock != null) {
       long startTime = System.nanoTime();
       try {
@@ -247,7 +254,7 @@ public class SourceHandle implements ISourceHandle {
   }
 
   @Override
-  public synchronized ByteBuffer getSerializedTsBlock() {
+  public synchronized ByteBuffer getSerializedTsBlock(boolean retainMemory) {
     long startTime = System.nanoTime();
     try (SetThreadName sourceHandleName = new SetThreadName(threadName)) {
       checkState();
@@ -270,13 +277,17 @@ public class SourceHandle implements ISourceHandle {
       currSequenceId += 1;
       if (retainedSize > 0) {
         bufferRetainedSizeInBytes -= retainedSize;
-        localMemoryManager
-            .getQueryPool()
-            .free(
-                localFragmentInstanceId.getQueryId(),
-                fullFragmentInstanceId,
-                localPlanNodeId,
-                retainedSize);
+        if (retainMemory) {
+          pendingRetainedSizeInBytes += retainedSize;
+        } else {
+          localMemoryManager
+              .getQueryPool()
+              .free(
+                  localFragmentInstanceId.getQueryId(),
+                  fullFragmentInstanceId,
+                  localPlanNodeId,
+                  retainedSize);
+        }
       }
 
       if (sequenceIdToTsBlock.isEmpty() && !isFinished()) {
@@ -296,6 +307,22 @@ public class SourceHandle implements ISourceHandle {
     }
   }
 
+  @Override
+  public synchronized void freeRetainedMemory() {
+    if (pendingRetainedSizeInBytes <= 0) {
+      return;
+    }
+    long retainedSize = pendingRetainedSizeInBytes;
+    pendingRetainedSizeInBytes = 0;
+    localMemoryManager
+        .getQueryPool()
+        .free(
+            localFragmentInstanceId.getQueryId(),
+            fullFragmentInstanceId,
+            localPlanNodeId,
+            retainedSize);
+  }
+
   private synchronized void trySubmitGetDataBlocksTask() {
     if (aborted || closed) {
       return;
@@ -444,16 +471,7 @@ public class SourceHandle implements ISourceHandle {
         bufferRetainedSizeInBytes -= 
localMemoryManager.getQueryPool().tryCancel(blockedOnMemory);
       }
       sequenceIdToDataBlockSize.clear();
-      if (bufferRetainedSizeInBytes > 0) {
-        localMemoryManager
-            .getQueryPool()
-            .free(
-                localFragmentInstanceId.getQueryId(),
-                fullFragmentInstanceId,
-                localPlanNodeId,
-                bufferRetainedSizeInBytes);
-        bufferRetainedSizeInBytes = 0;
-      }
+      freeAllRetainedMemory();
       aborted = true;
       sourceHandleListener.onAborted(this);
     }
@@ -472,16 +490,7 @@ public class SourceHandle implements ISourceHandle {
         bufferRetainedSizeInBytes -= 
localMemoryManager.getQueryPool().tryCancel(blockedOnMemory);
       }
       sequenceIdToDataBlockSize.clear();
-      if (bufferRetainedSizeInBytes > 0) {
-        localMemoryManager
-            .getQueryPool()
-            .free(
-                localFragmentInstanceId.getQueryId(),
-                fullFragmentInstanceId,
-                localPlanNodeId,
-                bufferRetainedSizeInBytes);
-        bufferRetainedSizeInBytes = 0;
-      }
+      freeAllRetainedMemory();
       aborted = true;
       sourceHandleListener.onAborted(this);
     }
@@ -500,16 +509,7 @@ public class SourceHandle implements ISourceHandle {
         bufferRetainedSizeInBytes -= 
localMemoryManager.getQueryPool().tryCancel(blockedOnMemory);
       }
       sequenceIdToDataBlockSize.clear();
-      if (bufferRetainedSizeInBytes > 0) {
-        localMemoryManager
-            .getQueryPool()
-            .free(
-                localFragmentInstanceId.getQueryId(),
-                fullFragmentInstanceId,
-                localPlanNodeId,
-                bufferRetainedSizeInBytes);
-        bufferRetainedSizeInBytes = 0;
-      }
+      freeAllRetainedMemory();
       closed = true;
       executorService.submit(new SendCloseSinkChannelEventTask());
       currSequenceId = lastSequenceId + 1;
@@ -517,6 +517,25 @@ public class SourceHandle implements ISourceHandle {
     }
   }
 
+  /**
+   * Frees the buffered memory and the memory handed over with {@code 
retainMemory=true}, as a
+   * fallback for blocks the caller has not released yet.
+   */
+  private void freeAllRetainedMemory() {
+    long totalRetainedSize = bufferRetainedSizeInBytes + 
pendingRetainedSizeInBytes;
+    if (totalRetainedSize > 0) {
+      localMemoryManager
+          .getQueryPool()
+          .free(
+              localFragmentInstanceId.getQueryId(),
+              fullFragmentInstanceId,
+              localPlanNodeId,
+              totalRetainedSize);
+      bufferRetainedSizeInBytes = 0;
+      pendingRetainedSizeInBytes = 0;
+    }
+  }
+
   @Override
   public boolean isFinished() {
     return remoteTsBlockedConsumedUp();
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java
index 1a7b96d7728..4a5fa22e1c4 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java
@@ -43,12 +43,14 @@ public interface IQueryExecution {
 
   Optional<TsBlock> getBatchResult() throws IoTDBException;
 
-  Optional<ByteBuffer> getByteBufferBatchResult() throws IoTDBException;
-
   /**
-   * Returns the holder of the serialized TsBlock that is partially sent to 
the client. It is used
-   * to continue the fragmented transfer of one oversized TsBlock in the 
following RPC response.
+   * Gets the next serialized TsBlock of the result. If {@code retainMemory} 
is true, the memory
+   * that the returned block retains in the memory pool is not freed when the 
block is taken out of
+   * the result handle. The caller then hands the block over to {@link 
#getPendingResultFragment()},
+   * which releases the memory once the block has been fully sent.
    */
+  Optional<ByteBuffer> getByteBufferBatchResult(boolean retainMemory) throws 
IoTDBException;
+
   PendingResultFragment getPendingResultFragment();
 
   boolean hasNextResult();
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java
index ebb020ae805..21b988d2a77 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java
@@ -25,32 +25,44 @@ import java.nio.ByteBuffer;
  * The tail of a serialized TsBlock that did not fit into the previous RPC 
response and is waiting
  * to be sent in the following response(s). The block is already serialized 
and removed from the
  * result handle, so this fragment only holds a reference to it and never 
copies the payload.
+ *
+ * <p>The memory the block retains stays reserved in the memory pool until the 
block is fully sent.
+ * The owner binds a {@code memoryReleaser} which is invoked by {@link 
#clear()}, so callers only
+ * interact with this holder and never release the memory themselves.
  */
 public class PendingResultFragment {
 
+  private final Runnable memoryReleaser;
+
   private ByteBuffer block;
   private int nextOffset;
 
+  public PendingResultFragment(Runnable memoryReleaser) {
+    this.memoryReleaser = memoryReleaser;
+  }
+
   /** Records that {@code block} has been sent up to {@code nextOffset} bytes. 
*/
-  public synchronized void reset(ByteBuffer block, int nextOffset) {
-    if (block != this.block) {
-      this.block = block;
-    }
+  public void reset(ByteBuffer block, int nextOffset) {
+    this.block = block;
     this.nextOffset = nextOffset;
   }
 
-  /** Returns the block that is partially sent, or null if no fragment is 
pending. */
-  public synchronized ByteBuffer getBlock() {
+  public ByteBuffer getBlock() {
     return block;
   }
 
-  /** Returns the start offset of the next fragment. */
-  public synchronized int getNextOffset() {
+  public int getNextOffset() {
     return nextOffset;
   }
 
-  public synchronized void clear() {
+  /**
+   * Discards the pending fragment and releases the memory of the blocks that 
have been fully sent.
+   * It is also called right after a complete block has been added to a 
response, when nothing is
+   * pending. Calling it repeatedly is a no-op.
+   */
+  public void clear() {
     block = null;
     nextOffset = 0;
+    memoryReleaser.run();
   }
 }
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java
index a1ff15a98a1..97727be83ce 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java
@@ -109,7 +109,8 @@ public class QueryExecution implements IQueryExecution {
   private final AtomicBoolean resultHandleCleanUp;
 
   // the serialized TsBlock that is partially sent to the client and waits for 
the next RPC
-  private final PendingResultFragment pendingResultFragment = new 
PendingResultFragment();
+  private final PendingResultFragment pendingResultFragment =
+      new PendingResultFragment(this::releaseResultHandleRetainedMemory);
 
   private final AtomicBoolean stopped;
 
@@ -440,6 +441,9 @@ public class QueryExecution implements IQueryExecution {
   /** Release the resources that current QueryExecution hold with a specified 
exception */
   private void releaseResource(Throwable t) {
     context.recordDeviceEntryDiskIOMetricsOnRelease();
+    // the query will not serve any more fetch requests, release the pending 
fragment's memory and
+    // make sure a retried execution does not continue a stale fragment
+    pendingResultFragment.clear();
     // close ResultHandle to unblock client's getResult request
     // Actually, we should not close the ResultHandle when the QueryExecution 
is Finished.
     // There are only two scenarios where the ResultHandle should be closed:
@@ -598,8 +602,8 @@ public class QueryExecution implements IQueryExecution {
   }
 
   @Override
-  public Optional<ByteBuffer> getByteBufferBatchResult() throws IoTDBException 
{
-    return getResult(this::getSerializedTsBlock);
+  public Optional<ByteBuffer> getByteBufferBatchResult(boolean retainMemory) 
throws IoTDBException {
+    return getResult(() -> resultHandle.getSerializedTsBlock(retainMemory));
   }
 
   @Override
@@ -607,8 +611,10 @@ public class QueryExecution implements IQueryExecution {
     return pendingResultFragment;
   }
 
-  private ByteBuffer getSerializedTsBlock() throws IoTDBException {
-    return resultHandle.getSerializedTsBlock();
+  private void releaseResultHandleRetainedMemory() {
+    if (resultHandle != null) {
+      resultHandle.freeRetainedMemory();
+    }
   }
 
   /**
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/ConfigExecution.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/ConfigExecution.java
index cfc8cfdeb98..621fd329d3b 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/ConfigExecution.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/ConfigExecution.java
@@ -116,8 +116,8 @@ public class ConfigExecution implements IQueryExecution {
   private final IConfigTask task;
   private final IConfigTaskExecutor configTaskExecutor;
 
-  // the serialized TsBlock that is partially sent to the client and waits for 
the next RPC
-  private final PendingResultFragment pendingResultFragment = new 
PendingResultFragment();
+  // the result set is not reserved from the memory pool, so nothing is 
released on clear
+  private final PendingResultFragment pendingResultFragment = new 
PendingResultFragment(() -> {});
 
   private final StatementType statementType;
   private long totalExecutionTime;
@@ -281,7 +281,8 @@ public class ConfigExecution implements IQueryExecution {
   }
 
   @Override
-  public Optional<ByteBuffer> getByteBufferBatchResult() throws IoTDBException 
{
+  public Optional<ByteBuffer> getByteBufferBatchResult(boolean retainMemory) 
throws IoTDBException {
+    // the result set is not reserved from the memory pool, so there is no 
memory to retain
     if (!resultSetConsumed) {
       resultSetConsumed = true;
       try {
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/memory/MemorySourceHandle.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/memory/MemorySourceHandle.java
index 7a4919446cf..a325fc671a1 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/memory/MemorySourceHandle.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/memory/MemorySourceHandle.java
@@ -77,7 +77,8 @@ public class MemorySourceHandle implements ISourceHandle {
   }
 
   @Override
-  public synchronized ByteBuffer getSerializedTsBlock() throws IoTDBException {
+  public synchronized ByteBuffer getSerializedTsBlock(boolean retainMemory) 
throws IoTDBException {
+    // serialized blocks are not reserved from the memory pool, so 
retainMemory is ignored
     hasNext = false;
     if (result.isEmpty()) {
       return null;
@@ -90,6 +91,11 @@ public class MemorySourceHandle implements ISourceHandle {
     }
   }
 
+  @Override
+  public void freeRetainedMemory() {
+    // nothing is reserved from the memory pool
+  }
+
   @Override
   public synchronized boolean isFinished() {
     return !hasNext;
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java
index cb36050adba..a398d2613e9 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java
@@ -253,18 +253,12 @@ public class QueryDataSetUtils {
     long memorySize = 0;
     List<ByteBuffer> res = new ArrayList<>();
     while (rowCount < fetchSize && memorySize < MAX_RETURN_SIZE) {
-      Optional<ByteBuffer> optionalByteBuffer = 
queryExecution.getByteBufferBatchResult();
+      Optional<ByteBuffer> optionalByteBuffer = 
queryExecution.getByteBufferBatchResult(false);
       if (!optionalByteBuffer.isPresent()) {
         break;
       }
       ByteBuffer byteBuffer = optionalByteBuffer.get();
-      byteBuffer.mark();
-      int valueColumnCount = byteBuffer.getInt();
-      for (int i = 0; i < valueColumnCount; i++) {
-        byteBuffer.get();
-      }
-      int positionCount = byteBuffer.getInt();
-      byteBuffer.reset();
+      int positionCount = readPositionCount(byteBuffer);
       if (positionCount != 0) {
         res.add(byteBuffer);
       }
@@ -279,11 +273,6 @@ public class QueryDataSetUtils {
    * maxPayloadSize} is cut into fragments: the response only carries the 
bytes that fit into the
    * payload, and the remaining bytes are kept in {@link 
PendingResultFragment} on the query
    * execution, to be sent by the following request carrying the returned 
offset.
-   *
-   * @param queryExecution used to get TsBlock from and judge whether there is 
more data.
-   * @param fetchSize wanted row size
-   * @param requestOffset the offset of the pending fragment to continue, 0 if 
there is none
-   * @param maxPayloadSize the maximum total bytes of {@link 
ResultChunk#getQueryResult()}
    */
   public static ResultChunk convertQueryResultByFetchSize(
       IQueryExecution queryExecution, int fetchSize, int requestOffset, int 
maxPayloadSize)
@@ -294,7 +283,6 @@ public class QueryDataSetUtils {
     int rowCount = 0;
     long memorySize = 0;
 
-    // Continue the oversized TsBlock left by the previous response first
     PendingResultFragment pendingFragment = 
queryExecution.getPendingResultFragment();
     ByteBuffer pendingBlock = pendingFragment.getBlock();
     if (pendingBlock != null) {
@@ -311,10 +299,9 @@ public class QueryDataSetUtils {
       res.add(slice(pendingBlock, nextOffset, fragmentSize));
       if (nextOffset + fragmentSize == pendingBlock.limit()) {
         pendingFragment.clear();
-        // The fragmented block is now complete. Do not fetch new blocks in 
the same response,
-        // otherwise the response would carry the tail of the big block plus 
up to fetchSize new
-        // rows, whose row count is not accounted for. One response either 
starts/continues a
-        // fragmented block or delivers new blocks, never both.
+        // Do not fetch new blocks in the same response, otherwise the 
response would carry the
+        // tail of the big block plus up to fetchSize new rows, whose row 
count is not accounted
+        // for. One response either continues a fragmented block or delivers 
new blocks.
         return ResultChunk.complete(res, !queryExecution.hasNextResult());
       }
       pendingFragment.reset(pendingBlock, nextOffset + fragmentSize);
@@ -328,16 +315,16 @@ public class QueryDataSetUtils {
     }
 
     while (rowCount < fetchSize && memorySize < MAX_RETURN_SIZE && 
remainingPayloadSize > 0) {
-      Optional<ByteBuffer> optionalByteBuffer = 
queryExecution.getByteBufferBatchResult();
+      Optional<ByteBuffer> optionalByteBuffer = 
queryExecution.getByteBufferBatchResult(true);
       if (!optionalByteBuffer.isPresent()) {
         break;
       }
       ByteBuffer byteBuffer = optionalByteBuffer.get();
       int positionCount = readPositionCount(byteBuffer);
       if (positionCount == 0) {
+        pendingFragment.clear();
         continue;
       }
-      // offsets kept in PendingResultFragment are absolute positions of the 
buffer
       int blockStart = byteBuffer.position();
       int blockSize = byteBuffer.remaining();
       rowCount += positionCount;
@@ -345,8 +332,8 @@ public class QueryDataSetUtils {
         res.add(byteBuffer);
         remainingPayloadSize -= blockSize;
         memorySize += blockSize;
+        pendingFragment.clear();
       } else {
-        // The block exceeds the remaining payload, send a leading fragment 
and keep the rest
         res.add(slice(byteBuffer, blockStart, remainingPayloadSize));
         pendingFragment.reset(byteBuffer, blockStart + remainingPayloadSize);
         return ResultChunk.fragment(
@@ -367,7 +354,6 @@ public class QueryDataSetUtils {
     return positionCount;
   }
 
-  /** Returns a view of {@code [offset, offset + length)} bytes of the buffer 
without copying. */
   private static ByteBuffer slice(ByteBuffer buffer, int offset, int length) {
     ByteBuffer duplicate = buffer.duplicate();
     duplicate.position(offset);
@@ -391,7 +377,7 @@ public class QueryDataSetUtils {
       this.finished = finished;
     }
 
-    /** The response ends with a fragment, which starts at {@code nextOffset} 
of its TsBlock. */
+    /** Creates a chunk whose last element is a fragment starting at {@code 
nextOffset}. */
     public static ResultChunk fragment(
         List<ByteBuffer> queryResult, int nextOffset, int totalLength, boolean 
finished) {
       return new ResultChunk(queryResult, nextOffset, totalLength, finished);
@@ -413,7 +399,6 @@ public class QueryDataSetUtils {
       return nextOffset;
     }
 
-    /** The total serialized length of the last block, set only when the 
fragment starts it. */
     public boolean hasTotalLength() {
       return totalLength >= 0;
     }
diff --git 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/protocol/session/QueryOwnershipTest.java
 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/protocol/session/QueryOwnershipTest.java
index b8c53352c09..e277418c2a3 100644
--- 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/protocol/session/QueryOwnershipTest.java
+++ 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/protocol/session/QueryOwnershipTest.java
@@ -298,7 +298,7 @@ public class QueryOwnershipTest {
     IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class);
     Mockito.when(queryExecution.getQueryId()).thenReturn("query");
     // the mock carries no buffered batch, only the "is there anything left" 
flag
-    
Mockito.when(queryExecution.getByteBufferBatchResult()).thenReturn(Optional.empty());
+    
Mockito.when(queryExecution.getByteBufferBatchResult(false)).thenReturn(Optional.empty());
     Mockito.when(queryExecution.hasNextResult()).thenReturn(hasNextResult);
     return queryExecution;
   }
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 95c9b810ce1..f61266b198a 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
@@ -1079,4 +1079,120 @@ public class SourceHandleTest {
     Mockito.verify(mockSourceHandleListener, Mockito.timeout(10_0000).times(1))
         .onAborted(sourceHandle);
   }
+
+  @Test
+  public void testRetainedMemoryIsFreedWhenTheHandedOverBlockIsReleased() 
throws Exception {
+    final String queryId = "q0";
+    final String localPlanNodeId = "exchange_0";
+    final HandleWithPool handleWithPool =
+        createSourceHandleWithOneFetchedBlock(queryId, localPlanNodeId);
+    final SourceHandle sourceHandle = handleWithPool.handle;
+    final MemoryPool memoryPool = handleWithPool.memoryPool;
+    final String fragmentInstanceId =
+        FragmentInstanceId.createFragmentInstanceIdFromTFragmentInstanceId(
+            sourceHandle.getLocalFragmentInstanceId());
+
+    // The block is taken out of the buffer, but its memory stays reserved in 
the pool
+    Assert.assertNotNull(sourceHandle.getSerializedTsBlock(true));
+    Assert.assertEquals(0L, sourceHandle.getBufferRetainedSizeInBytes());
+    Mockito.verify(memoryPool, Mockito.never())
+        .free(queryId, fragmentInstanceId, localPlanNodeId, MOCK_TSBLOCK_SIZE);
+
+    // The caller releases the memory once the block is fully sent
+    sourceHandle.freeRetainedMemory();
+    Mockito.verify(memoryPool, Mockito.times(1))
+        .free(queryId, fragmentInstanceId, localPlanNodeId, MOCK_TSBLOCK_SIZE);
+
+    // Releasing again is a no-op
+    sourceHandle.freeRetainedMemory();
+    Mockito.verify(memoryPool, Mockito.times(1))
+        .free(queryId, fragmentInstanceId, localPlanNodeId, MOCK_TSBLOCK_SIZE);
+  }
+
+  @Test
+  public void testAbortReleasesTheHandedOverMemory() throws Exception {
+    final String queryId = "q0";
+    final String localPlanNodeId = "exchange_0";
+    final HandleWithPool handleWithPool =
+        createSourceHandleWithOneFetchedBlock(queryId, localPlanNodeId);
+    final SourceHandle sourceHandle = handleWithPool.handle;
+    final MemoryPool memoryPool = handleWithPool.memoryPool;
+    final String fragmentInstanceId =
+        FragmentInstanceId.createFragmentInstanceIdFromTFragmentInstanceId(
+            sourceHandle.getLocalFragmentInstanceId());
+
+    Assert.assertNotNull(sourceHandle.getSerializedTsBlock(true));
+    Mockito.verify(memoryPool, Mockito.never())
+        .free(queryId, fragmentInstanceId, localPlanNodeId, MOCK_TSBLOCK_SIZE);
+
+    // abort frees the memory that was not released by the caller (fallback 
path)
+    sourceHandle.abort();
+    Mockito.verify(memoryPool, Mockito.times(1))
+        .free(queryId, fragmentInstanceId, localPlanNodeId, MOCK_TSBLOCK_SIZE);
+
+    // a late release after abort must not free the same memory twice
+    sourceHandle.freeRetainedMemory();
+    Mockito.verify(memoryPool, Mockito.times(1))
+        .free(queryId, fragmentInstanceId, localPlanNodeId, MOCK_TSBLOCK_SIZE);
+  }
+
+  private static final class HandleWithPool {
+    private final SourceHandle handle;
+    private final MemoryPool memoryPool;
+
+    private HandleWithPool(SourceHandle handle, MemoryPool memoryPool) {
+      this.handle = handle;
+      this.memoryPool = memoryPool;
+    }
+  }
+
+  /** Creates a SourceHandle whose single pending block has already been 
fetched. */
+  private HandleWithPool createSourceHandleWithOneFetchedBlock(
+      String queryId, String localPlanNodeId) throws Exception {
+    final TEndPoint remoteEndpoint =
+        new TEndPoint("remote", 
IoTDBDescriptor.getInstance().getConfig().getMppDataExchangePort());
+    final TFragmentInstanceId remoteFragmentInstanceId = new 
TFragmentInstanceId(queryId, 1, "0");
+    final TFragmentInstanceId localFragmentInstanceId = new 
TFragmentInstanceId(queryId, 0, "0");
+
+    final LocalMemoryManager mockLocalMemoryManager = 
Mockito.mock(LocalMemoryManager.class);
+    final MemoryPool mockMemoryPool = Utils.createMockNonBlockedMemoryPool();
+    
Mockito.when(mockLocalMemoryManager.getQueryPool()).thenReturn(mockMemoryPool);
+    final IClientManager<TEndPoint, SyncDataNodeMPPDataExchangeServiceClient> 
mockClientManager =
+        Mockito.mock(IClientManager.class);
+    final SyncDataNodeMPPDataExchangeServiceClient mockClient =
+        Mockito.mock(SyncDataNodeMPPDataExchangeServiceClient.class);
+    
Mockito.when(mockClientManager.borrowClient(remoteEndpoint)).thenReturn(mockClient);
+    Mockito.doAnswer(
+            invocation -> {
+              TGetDataBlockRequest req = invocation.getArgument(0);
+              List<ByteBuffer> byteBuffers = new ArrayList<>();
+              for (int i = req.getStartSequenceId(); i < 
req.getEndSequenceId(); i++) {
+                byteBuffers.add(ByteBuffer.allocate(0));
+              }
+              return new TGetDataBlockResponse(byteBuffers);
+            })
+        .when(mockClient)
+        .getDataBlock(Mockito.any(TGetDataBlockRequest.class));
+
+    final SourceHandle sourceHandle =
+        new SourceHandle(
+            remoteEndpoint,
+            remoteFragmentInstanceId,
+            localFragmentInstanceId,
+            localPlanNodeId,
+            0,
+            mockLocalMemoryManager,
+            Executors.newSingleThreadExecutor(),
+            Utils.createMockTsBlockSerde(MOCK_TSBLOCK_SIZE),
+            Mockito.mock(SourceHandleListener.class),
+            mockClientManager);
+
+    sourceHandle.isBlocked();
+    sourceHandle.updatePendingDataBlockInfo(0, List.of(MOCK_TSBLOCK_SIZE));
+    Mockito.verify(mockClient, Mockito.timeout(10_000).times(1))
+        .getDataBlock(Mockito.any(TGetDataBlockRequest.class));
+    Assert.assertTrue(sourceHandle.isBlocked().isDone());
+    Assert.assertEquals(MOCK_TSBLOCK_SIZE, 
sourceHandle.getBufferRetainedSizeInBytes());
+    return new HandleWithPool(sourceHandle, mockMemoryPool);
+  }
 }
diff --git 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/MergeTreeSortOperatorTest.java
 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/MergeTreeSortOperatorTest.java
index ca22be2e6be..a21757411cf 100644
--- 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/MergeTreeSortOperatorTest.java
+++ 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/MergeTreeSortOperatorTest.java
@@ -1809,7 +1809,7 @@ public class MergeTreeSortOperatorTest {
     private final long startTime;
     private final String queryId;
     private final String sql;
-    private final PendingResultFragment pendingResultFragment = new 
PendingResultFragment();
+    private final PendingResultFragment pendingResultFragment = new 
PendingResultFragment(() -> {});
 
     FakeQueryExecution(long startTime, String queryId, String sql) {
       this.startTime = startTime;
@@ -1891,7 +1891,7 @@ public class MergeTreeSortOperatorTest {
     }
 
     @Override
-    public Optional<ByteBuffer> getByteBufferBatchResult() {
+    public Optional<ByteBuffer> getByteBufferBatchResult(boolean retainMemory) 
{
       return Optional.empty();
     }
 
diff --git 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/QueryDataSetUtilsTest.java
 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/QueryDataSetUtilsTest.java
index 3ea0cce94b6..876d7925936 100644
--- 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/QueryDataSetUtilsTest.java
+++ 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/QueryDataSetUtilsTest.java
@@ -42,6 +42,7 @@ import java.io.DataOutputStream;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import java.util.Optional;
 
@@ -157,7 +158,7 @@ public class QueryDataSetUtilsTest {
     TsBlock tsBlock = buildTsBlock();
     Mockito.when(queryExecution.getBatchResult())
         .thenReturn(Optional.of(tsBlock), Optional.empty());
-    Mockito.when(queryExecution.getByteBufferBatchResult())
+    Mockito.when(queryExecution.getByteBufferBatchResult(false))
         .thenReturn(Optional.of(new TsBlockSerde().serialize(tsBlock)), 
Optional.empty());
     return queryExecution;
   }
@@ -292,10 +293,11 @@ public class QueryDataSetUtilsTest {
     // one byte smaller than the block so that the block has to be split into 
two fragments
     int payloadSize = totalLength - 1;
 
-    PendingResultFragment pendingFragment = new PendingResultFragment();
     IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class);
+    Runnable memoryReleaser = Mockito.mock(Runnable.class);
+    PendingResultFragment pendingFragment = new 
PendingResultFragment(memoryReleaser);
     
Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(pendingFragment);
-    Mockito.when(queryExecution.getByteBufferBatchResult())
+    Mockito.when(queryExecution.getByteBufferBatchResult(true))
         .thenReturn(Optional.of(serialized.duplicate()), Optional.empty());
     Mockito.when(queryExecution.hasNextResult()).thenReturn(false);
 
@@ -309,6 +311,8 @@ public class QueryDataSetUtilsTest {
     assertEquals(1, firstChunk.getQueryResult().size());
     assertEquals(payloadSize, firstChunk.getQueryResult().get(0).remaining());
     assertTrue(pendingFragment.getBlock() != null);
+    // the memory of the pending block is still retained
+    Mockito.verify(memoryReleaser, Mockito.never()).run();
 
     QueryDataSetUtils.ResultChunk secondChunk =
         QueryDataSetUtils.convertQueryResultByFetchSize(
@@ -317,6 +321,8 @@ public class QueryDataSetUtilsTest {
     assertFalse(secondChunk.hasNextOffset());
     assertFalse(secondChunk.hasTotalLength());
     assertTrue(pendingFragment.getBlock() == null);
+    // the memory is released once the fragmented block has been fully sent
+    Mockito.verify(memoryReleaser, Mockito.times(1)).run();
 
     TsBlock result = deserialize(firstChunk.getQueryResult().get(0), 
secondChunk.getQueryResult());
     assertEquals(2, result.getPositionCount());
@@ -334,10 +340,11 @@ public class QueryDataSetUtilsTest {
     ByteBuffer smallBlock = serialized.duplicate();
     int payloadSize = smallBlock.remaining() + blockSize / 2;
 
-    PendingResultFragment pendingFragment = new PendingResultFragment();
     IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class);
+    Runnable memoryReleaser = Mockito.mock(Runnable.class);
+    PendingResultFragment pendingFragment = new 
PendingResultFragment(memoryReleaser);
     
Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(pendingFragment);
-    Mockito.when(queryExecution.getByteBufferBatchResult())
+    Mockito.when(queryExecution.getByteBufferBatchResult(true))
         .thenReturn(Optional.of(smallBlock), 
Optional.of(serialized.duplicate()), Optional.empty());
     Mockito.when(queryExecution.hasNextResult()).thenReturn(false);
 
@@ -350,6 +357,8 @@ public class QueryDataSetUtilsTest {
     assertEquals(blockSize / 2, 
firstChunk.getQueryResult().get(1).remaining());
     assertTrue(firstChunk.hasTotalLength());
     assertEquals(blockSize, firstChunk.getTotalLength());
+    // the complete block is released at once, the fragmented one is released 
when it completes
+    Mockito.verify(memoryReleaser, Mockito.times(1)).run();
 
     QueryDataSetUtils.ResultChunk secondChunk =
         QueryDataSetUtils.convertQueryResultByFetchSize(
@@ -359,6 +368,7 @@ public class QueryDataSetUtilsTest {
     // only the rest of the oversized block remains
     assertEquals(1, secondChunk.getQueryResult().size());
     assertEquals(blockSize - blockSize / 2, 
secondChunk.getQueryResult().get(0).remaining());
+    Mockito.verify(memoryReleaser, Mockito.times(2)).run();
   }
 
   @Test
@@ -368,10 +378,11 @@ public class QueryDataSetUtilsTest {
     int blockSize = serialized.remaining();
     int payloadSize = blockSize / 2;
 
-    PendingResultFragment pendingFragment = new PendingResultFragment();
     IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class);
+    Runnable memoryReleaser = Mockito.mock(Runnable.class);
+    PendingResultFragment pendingFragment = new 
PendingResultFragment(memoryReleaser);
     
Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(pendingFragment);
-    Mockito.when(queryExecution.getByteBufferBatchResult())
+    Mockito.when(queryExecution.getByteBufferBatchResult(true))
         .thenReturn(
             Optional.of(serialized.duplicate()),
             Optional.of(serialized.duplicate()),
@@ -382,6 +393,7 @@ public class QueryDataSetUtilsTest {
         QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, 10, 0, 
payloadSize);
     assertFalse(firstChunk.isFinished());
     assertEquals(payloadSize, firstChunk.getNextOffset());
+    Mockito.verify(memoryReleaser, Mockito.never()).run();
 
     // The response that completes the fragmented block must not fetch new 
blocks, otherwise its
     // row count would exceed fetchSize (the tail of the big block is not 
accounted).
@@ -393,6 +405,7 @@ public class QueryDataSetUtilsTest {
     assertEquals(1, continuationChunk.getQueryResult().size());
     assertEquals(blockSize - payloadSize, 
continuationChunk.getQueryResult().get(0).remaining());
     assertTrue(pendingFragment.getBlock() == null);
+    Mockito.verify(memoryReleaser, Mockito.times(1)).run();
 
     // New blocks are delivered by the following request only
     QueryDataSetUtils.ResultChunk nextChunk =
@@ -401,11 +414,36 @@ public class QueryDataSetUtilsTest {
     assertFalse(nextChunk.hasNextOffset());
     assertEquals(1, nextChunk.getQueryResult().size());
     assertEquals(blockSize, nextChunk.getQueryResult().get(0).remaining());
+    Mockito.verify(memoryReleaser, Mockito.times(2)).run();
+  }
+
+  @Test
+  public void testEmptyBlockReleasesItsMemory() throws IoTDBException, 
IOException {
+    TsBlock emptyTsBlock = new 
TsBlockBuilder(Collections.singletonList(TSDataType.INT64)).build();
+    ByteBuffer emptyBlock = new TsBlockSerde().serialize(emptyTsBlock);
+    TsBlock tsBlock = buildTsBlock();
+    ByteBuffer serialized = new TsBlockSerde().serialize(tsBlock);
+
+    IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class);
+    Runnable memoryReleaser = Mockito.mock(Runnable.class);
+    Mockito.when(queryExecution.getPendingResultFragment())
+        .thenReturn(new PendingResultFragment(memoryReleaser));
+    Mockito.when(queryExecution.getByteBufferBatchResult(true))
+        .thenReturn(Optional.of(emptyBlock), Optional.of(serialized), 
Optional.empty());
+    Mockito.when(queryExecution.hasNextResult()).thenReturn(false);
+
+    QueryDataSetUtils.ResultChunk chunk =
+        QueryDataSetUtils.convertQueryResultByFetchSize(
+            queryExecution, 10, 0, serialized.remaining());
+    assertTrue(chunk.isFinished());
+    assertEquals(1, chunk.getQueryResult().size());
+    // the empty block and the complete block are both released immediately
+    Mockito.verify(memoryReleaser, Mockito.times(2)).run();
   }
 
   @Test(expected = IllegalArgumentException.class)
   public void testConvertQueryResultByFetchSizeRejectsMismatchedOffset() 
throws IoTDBException {
-    PendingResultFragment pendingFragment = new PendingResultFragment();
+    PendingResultFragment pendingFragment = new PendingResultFragment(() -> 
{});
     pendingFragment.reset(ByteBuffer.wrap(new byte[10]), 5);
     IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class);
     
Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(pendingFragment);
@@ -417,7 +455,8 @@ public class QueryDataSetUtilsTest {
   public void 
testConvertQueryResultByFetchSizeRejectsOffsetWithoutPendingFragment()
       throws IoTDBException {
     IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class);
-    Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(new 
PendingResultFragment());
+    Mockito.when(queryExecution.getPendingResultFragment())
+        .thenReturn(new PendingResultFragment(() -> {}));
 
     QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, 10, 5, 8);
   }
diff --git 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/ResultChunkingEndToEndTest.java
 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/ResultChunkingEndToEndTest.java
new file mode 100644
index 00000000000..e7bb00011e6
--- /dev/null
+++ 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/ResultChunkingEndToEndTest.java
@@ -0,0 +1,255 @@
+/*
+ * 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.utils;
+
+import org.apache.iotdb.db.queryengine.plan.execution.IQueryExecution;
+import org.apache.iotdb.db.queryengine.plan.execution.PendingResultFragment;
+import org.apache.iotdb.rpc.IoTDBRpcDataSet;
+import org.apache.iotdb.rpc.RpcUtils;
+import org.apache.iotdb.rpc.TSStatusCode;
+import org.apache.iotdb.service.rpc.thrift.IClientRPCService;
+import org.apache.iotdb.service.rpc.thrift.TSCloseOperationReq;
+import org.apache.iotdb.service.rpc.thrift.TSFetchResultsReq;
+import org.apache.iotdb.service.rpc.thrift.TSFetchResultsResp;
+
+import org.apache.tsfile.common.conf.TSFileConfig;
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.read.common.block.TsBlock;
+import org.apache.tsfile.read.common.block.TsBlockBuilder;
+import org.apache.tsfile.read.common.block.column.TsBlockSerde;
+import org.apache.tsfile.utils.Binary;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import java.nio.ByteBuffer;
+import java.time.ZoneId;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicInteger;
+
+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.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * End-to-end verification of the fragmented query result protocol: the server 
side produces
+ * bounded-payload chunks via {@link QueryDataSetUtils} and the client side 
({@code
+ * IoTDBRpcDataSet}) assembles them back into the original rows.
+ */
+public class ResultChunkingEndToEndTest {
+
+  private static final String COLUMN = "s1";
+  private static final String TEXT_COLUMN = "s2";
+  private static final int LARGE_VALUE_SIZE = 512 * 1024;
+  private static final int MAX_PAYLOAD_SIZE = 256 * 1024;
+
+  @Test(timeout = 60_000)
+  public void testFragmentedResultRoundTrip() throws Exception {
+    TsBlock tsBlock = buildTsBlockWithLargeValue();
+    ByteBuffer serialized = new TsBlockSerde().serialize(tsBlock);
+    int totalLength = serialized.remaining();
+    assertTrue(
+        "the block must be larger than the payload to trigger fragmentation",
+        totalLength > MAX_PAYLOAD_SIZE);
+
+    ServerSideSimulation server =
+        new ServerSideSimulation(serialized.duplicate(), 
tsBlock.getPositionCount(), totalLength);
+
+    IClientRPCService.Iface client = mock(IClientRPCService.Iface.class);
+    when(client.closeOperation(any(TSCloseOperationReq.class)))
+        .thenReturn(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS));
+    when(client.fetchResultsV2(any(TSFetchResultsReq.class)))
+        .thenAnswer(invocation -> 
server.handleFetchResults(invocation.getArgument(0)));
+
+    IoTDBRpcDataSet dataSet =
+        new IoTDBRpcDataSet(
+            "",
+            List.of(COLUMN, TEXT_COLUMN),
+            List.of(TSDataType.INT64.name(), TSDataType.TEXT.name()),
+            null,
+            true,
+            true,
+            1L,
+            2L,
+            client,
+            3L,
+            null,
+            100,
+            0,
+            ZoneId.systemDefault(),
+            RpcUtils.DEFAULT_TIME_FORMAT,
+            1_000_000,
+            true,
+            new ArrayList<>(Arrays.asList(0, 1)));
+
+    // two rows: [time=1, v=10, text="small"] and [time=2, v=20, text=<512KB>]
+    assertTrue(dataSet.next());
+    assertEquals(10L, dataSet.getLong(COLUMN));
+    assertEquals("small", dataSet.getString(TEXT_COLUMN));
+    assertTrue(dataSet.next());
+    assertEquals(20L, dataSet.getLong(COLUMN));
+    assertEquals(LARGE_VALUE_SIZE, dataSet.getString(TEXT_COLUMN).length());
+    assertFalse(dataSet.next());
+
+    assertTrue(
+        "the result must have been transferred in several responses", 
server.rpcCount.get() > 1);
+    for (int payload : server.payloadSizes) {
+      assertTrue(
+          String.format("one response payload %d exceeds the bound %d", 
payload, MAX_PAYLOAD_SIZE),
+          payload <= MAX_PAYLOAD_SIZE);
+    }
+    // every fragmented block was fully released
+    assertTrue(server.releaseCount.get() > 0);
+    assertEquals("the pending fragment must be cleared in the end", null, 
server.getPendingBlock());
+  }
+
+  @Test(timeout = 60_000)
+  public void testSmallResultUsesOneResponseWithoutFragment() throws Exception 
{
+    TsBlockBuilder builder = new TsBlockBuilder(List.of(TSDataType.INT64));
+    builder.getTimeColumnBuilder().writeLong(1L);
+    builder.getColumnBuilder(0).writeLong(10L);
+    builder.declarePosition();
+    TsBlock tsBlock = builder.build();
+    ByteBuffer serialized = new TsBlockSerde().serialize(tsBlock);
+
+    ServerSideSimulation server =
+        new ServerSideSimulation(serialized.duplicate(), 
tsBlock.getPositionCount(), 0);
+
+    IClientRPCService.Iface client = mock(IClientRPCService.Iface.class);
+    when(client.closeOperation(any(TSCloseOperationReq.class)))
+        .thenReturn(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS));
+    when(client.fetchResultsV2(any(TSFetchResultsReq.class)))
+        .thenAnswer(invocation -> 
server.handleFetchResults(invocation.getArgument(0)));
+    // the whole result fits into the execute response
+    IoTDBRpcDataSet dataSet =
+        new IoTDBRpcDataSet(
+            "",
+            List.of(COLUMN),
+            List.of(TSDataType.INT64.name()),
+            null,
+            true,
+            false,
+            1L,
+            2L,
+            client,
+            3L,
+            List.of(serialized.duplicate()),
+            100,
+            0,
+            ZoneId.systemDefault(),
+            RpcUtils.DEFAULT_TIME_FORMAT,
+            1_000_000,
+            true,
+            new ArrayList<>(List.of(0)));
+
+    assertTrue(dataSet.next());
+    assertEquals(10L, dataSet.getLong(COLUMN));
+    assertFalse(dataSet.next());
+    assertEquals("a small result must not trigger any fetch", 0, 
server.rpcCount.get());
+  }
+
+  /** Mimics ClientRPCServiceImpl#fetchResultsV2 with bounded payloads. */
+  private static class ServerSideSimulation {
+    private final ByteBuffer serialized;
+    private final int positionCount;
+    private final int totalLength;
+
+    private final AtomicInteger rpcCount = new AtomicInteger();
+    private final AtomicInteger releaseCount = new AtomicInteger();
+    private final List<Integer> payloadSizes = new ArrayList<>();
+    private final List<ByteBuffer> remainingBlocks = new ArrayList<>();
+    private final PendingResultFragment pendingFragment =
+        new PendingResultFragment(releaseCount::incrementAndGet);
+    private final IQueryExecution queryExecution;
+
+    private ServerSideSimulation(ByteBuffer serialized, int positionCount, int 
totalLength)
+        throws Exception {
+      this.serialized = serialized;
+      this.positionCount = positionCount;
+      this.totalLength = totalLength;
+      this.remainingBlocks.add(serialized);
+      this.queryExecution = Mockito.mock(IQueryExecution.class);
+      
Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(pendingFragment);
+      Mockito.when(queryExecution.getByteBufferBatchResult(true))
+          .thenAnswer(
+              invocation ->
+                  remainingBlocks.isEmpty()
+                      ? Optional.empty()
+                      : Optional.of(remainingBlocks.remove(0)));
+      Mockito.when(queryExecution.hasNextResult())
+          .thenAnswer(
+              invocation -> !remainingBlocks.isEmpty() || 
pendingFragment.getBlock() != null);
+    }
+
+    private TSFetchResultsResp handleFetchResults(TSFetchResultsReq req) 
throws Exception {
+      rpcCount.incrementAndGet();
+      QueryDataSetUtils.ResultChunk chunk =
+          QueryDataSetUtils.convertQueryResultByFetchSize(
+              queryExecution,
+              Math.max(positionCount, 1),
+              req.isSetOffset() ? req.getOffset() : 0,
+              MAX_PAYLOAD_SIZE);
+
+      int payloadSize = 0;
+      for (ByteBuffer block : chunk.getQueryResult()) {
+        payloadSize += block.remaining();
+      }
+      payloadSizes.add(payloadSize);
+
+      TSFetchResultsResp resp =
+          new 
TSFetchResultsResp(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS), true, true);
+      resp.setQueryResult(chunk.getQueryResult());
+      resp.setMoreData(!chunk.isFinished());
+      if (chunk.hasNextOffset()) {
+        resp.setOffset(chunk.getNextOffset());
+        if (chunk.hasTotalLength()) {
+          resp.setTotalLength(chunk.getTotalLength());
+        }
+      }
+      return resp;
+    }
+
+    private ByteBuffer getPendingBlock() {
+      return pendingFragment.getBlock();
+    }
+  }
+
+  /** [time=1, v=10, text="small"], [time=2, v=20, text=<LARGE_VALUE_SIZE 
bytes>]. */
+  private static TsBlock buildTsBlockWithLargeValue() {
+    byte[] largeValue = new byte[LARGE_VALUE_SIZE];
+    Arrays.fill(largeValue, (byte) 'x');
+
+    TsBlockBuilder builder = new 
TsBlockBuilder(Arrays.asList(TSDataType.INT64, TSDataType.TEXT));
+    builder.getTimeColumnBuilder().writeLong(1L);
+    builder.getColumnBuilder(0).writeLong(10L);
+    builder.getColumnBuilder(1).writeBinary(new Binary("small", 
TSFileConfig.STRING_CHARSET));
+    builder.declarePosition();
+    builder.getTimeColumnBuilder().writeLong(2L);
+    builder.getColumnBuilder(0).writeLong(20L);
+    builder.getColumnBuilder(1).writeBinary(new Binary(largeValue));
+    builder.declarePosition();
+    return builder.build();
+  }
+}

Reply via email to