public static Pair<TSQueryDataSet, Boolean> convertTsBlockByFetchSize(
      IQueryExecution queryExecution, int fetchSize) throws IOException, IoTDBException {
    fetchSize = fetchSize > 0 ? fetchSize : TSFLE_CONFIG.getMaxTsBlockLineNumber();
    boolean finished = false;
    int columnNum = queryExecution.getOutputValueColumnCount();
    // one time column and each value column has an actual value buffer and a bitmap value to
    // indicate whether it is a null
    int columnNumWithTime = columnNum * 2 + 1;
    DataOutputStream[] dataOutputStreams = new DataOutputStream[columnNumWithTime];
    ByteArrayOutputStream[] byteArrayOutputStreams = new ByteArrayOutputStream[columnNumWithTime];
    for (int i = 0; i < columnNumWithTime; i++) {
      byteArrayOutputStreams[i] = new ByteArrayOutputStream();
      dataOutputStreams[i] = new DataOutputStream(byteArrayOutputStreams[i]);
    }

    int rowCount = 0;
    int[] valueOccupation = new int[columnNum];

    // used to record a bitmap for every 8 points
    int[] bitmaps = new int[columnNum];
    while (rowCount < fetchSize) {
      Optional<TsBlock> optionalTsBlock = queryExecution.getBatchResult();
      if (!optionalTsBlock.isPresent()) {
        finished = true;
        break;
      }
      TsBlock tsBlock = optionalTsBlock.get();
      if (!tsBlock.isEmpty()) {
        int currentCount = tsBlock.getPositionCount();
        serializeTsBlock(
            rowCount,
            currentCount,
            tsBlock,
            columnNum,
            dataOutputStreams,
            valueOccupation,
            bitmaps);
        rowCount += currentCount;
      }
    }

    fillRemainingBitMap(rowCount, columnNum, dataOutputStreams, bitmaps);

    TSQueryDataSet tsQueryDataSet = new TSQueryDataSet();

    fillTimeColumn(rowCount, byteArrayOutputStreams, tsQueryDataSet);

    fillValueColumnsAndBitMaps(rowCount, byteArrayOutputStreams, valueOccupation, tsQueryDataSet);

    return new Pair<>(tsQueryDataSet, finished);
  }
TimeValuePairUtils to convert between thrift format and TsFile format.
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java