public static Pair<AggregationNode, AggregationTableScanNode> split(
      AggregationTableScanNode node, SymbolAllocator symbolAllocator, QueryId queryId) {
    AggregationTreeDeviceViewScanNode aggregationTreeDeviceViewScanNode;
    if (node instanceof AggregationTreeDeviceViewScanNode) {
      aggregationTreeDeviceViewScanNode = (AggregationTreeDeviceViewScanNode) node;
    } else {
      aggregationTreeDeviceViewScanNode = null;
    }

    Map<Symbol, AggregationNode.Aggregation> intermediateAggregation = new LinkedHashMap<>();
    Map<Symbol, AggregationNode.Aggregation> finalAggregation = new LinkedHashMap<>();
    for (Map.Entry<Symbol, AggregationNode.Aggregation> entry : node.getAggregations().entrySet()) {
      AggregationNode.Aggregation originalAggregation = entry.getValue();
      ResolvedFunction resolvedFunction = originalAggregation.getResolvedFunction();
      Type intermediateType =
          TableBuiltinAggregationFunction.getIntermediateType(
              resolvedFunction.getSignature().getName(),
              resolvedFunction.getSignature().getArgumentTypes());
      Symbol intermediateSymbol =
          symbolAllocator.newSymbol(resolvedFunction.getSignature().getName(), intermediateType);

      checkState(
          !originalAggregation.getOrderingScheme().isPresent(),
          "Aggregate with ORDER BY does not support partial aggregation");
      intermediateAggregation.put(
          intermediateSymbol,
          new AggregationNode.Aggregation(
              resolvedFunction,
              originalAggregation.getArguments(),
              originalAggregation.isDistinct(),
              originalAggregation.getFilter(),
              originalAggregation.getOrderingScheme(),
              originalAggregation.getMask()));

      // rewrite final aggregation in terms of intermediate function
      finalAggregation.put(
          entry.getKey(),
          new AggregationNode.Aggregation(
              resolvedFunction,
              ImmutableList.of(intermediateSymbol.toSymbolReference()),
              false,
              Optional.empty(),
              Optional.empty(),
              Optional.empty()));
    }

    AggregationTableScanNode rightResult =
        aggregationTreeDeviceViewScanNode == null
            ? new AggregationTableScanNode(
                queryId.genPlanNodeId(),
                node.getQualifiedObjectName(),
                node.getOutputSymbols(),
                node.getAssignments(),
                ImmutableList.of(),
                node.getTagAndAttributeIndexMap(),
                node.getScanOrder(),
                node.getTimePredicate().orElse(null),
                node.getPushDownPredicate(),
                node.getPushDownLimit(),
                node.getPushDownOffset(),
                node.isPushLimitToEachDevice(),
                node.containsNonAlignedDevice(),
                node.getProjection(),
                intermediateAggregation,
                node.getGroupingSets(),
                node.getPreGroupedSymbols(),
                PARTIAL,
                node.getGroupIdSymbol())
            : new AggregationTreeDeviceViewScanNode(
                queryId.genPlanNodeId(),
                node.getQualifiedObjectName(),
                node.getOutputSymbols(),
                node.getAssignments(),
                ImmutableList.of(),
                node.getTagAndAttributeIndexMap(),
                node.getScanOrder(),
                node.getTimePredicate().orElse(null),
                node.getPushDownPredicate(),
                node.getPushDownLimit(),
                node.getPushDownOffset(),
                node.isPushLimitToEachDevice(),
                node.containsNonAlignedDevice(),
                node.getProjection(),
                intermediateAggregation,
                node.getGroupingSets(),
                node.getPreGroupedSymbols(),
                PARTIAL,
                node.getGroupIdSymbol(),
                aggregationTreeDeviceViewScanNode.getTreeDBName(),
                aggregationTreeDeviceViewScanNode.getMeasurementColumnNameMap());

    return new Pair<>(
        new AggregationNode(
            node.getPlanNodeId(),
            null,
            finalAggregation,
            node.getGroupingSets(),
            node.getPreGroupedSymbols(),
            FINAL,
            Optional.empty(),
            node.getGroupIdSymbol()),
        rightResult);
  }
Split AggregationTableScanNode into two-stage.

@return left of pair is FINAL AggregationNode, right is PARTIAL AggregationTableScanNode
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/planner/optimizations/Util.java