Code review comment: The most risky bug in this code is: The variable `n` used in the `getOpenPartitions` method is undefined, which will cause a compilation error.  You can modify the code like this: ```java public List<Long> getOpenPartitions() {     if (!enableAutomaticPartition || Config.max_load_open_partition_number <= 0             || partitionIds.size() < Config.max_load_open_partition_number) {         return partitionIds;     }     // bigger partition id means newer partition     // open last MAX_OPEN_PARTITION_NUM partitions     Set<Long> openPartitionIds = partitionIds.stream().collect(             Collectors.toCollection(() -> new TreeSet<>(Collections.reverseOrder())))             .stream().limit(MAX_OPEN_PARTITION_NUM).collect(Collectors.toSet());;  // Replace 'n' with 'MAX_OPEN_PARTITION_NUM'     if (!doubleWritePartitions.isEmpty()) {         openPartitionIds.addAll(doubleWritePartitions.keySet());     }      return openPartitionIds.stream().collect(Collectors.toList()); } ```

Code under review:
public void complete() throws UserException {
        TOlapTableSink tSink = tDataSink.getOlap_table_sink();

        tSink.setTable_id(dstTable.getId());
        tSink.setTable_name(dstTable.getName());
        tSink.setTuple_id(tupleDescriptor.getId().asInt());
        int numReplicas = 1;
        Optional<Partition> optionalPartition = dstTable.getPartitions().stream().findFirst();
        if (optionalPartition.isPresent()) {
            long partitionId = optionalPartition.get().getId();
            numReplicas = dstTable.getPartitionInfo().getReplicationNum(partitionId);
        }
        tSink.setNum_replicas(numReplicas);
        tSink.setNeed_gen_rollup(dstTable.shouldLoadToNewRollup());
        tSink.setSchema(createSchema(tSink.getDb_id(), dstTable, tupleDescriptor));
        TOlapTablePartitionParam partitionParam = createPartition(tSink.getDb_id(), dstTable, tupleDescriptor,
                enableAutomaticPartition, automaticBucketSize, getOpenPartitions());
        tSink.setPartition(partitionParam);
        tSink.setLocation(createLocation(dstTable, partitionParam, enableReplicatedStorage, warehouseId));
        tSink.setNodes_info(GlobalStateMgr.getCurrentState().createNodesInfo(warehouseId));
        tSink.setPartial_update_mode(this.partialUpdateMode);
        tSink.setAutomatic_bucket_size(automaticBucketSize);
        if (canUseColocateMVIndex(dstTable)) {
            tSink.setEnable_colocate_mv_index(true);
        }

        Map<Long, Long> doubleWritePartitions = dstTable.getDoubleWritePartitions();
        if (!doubleWritePartitions.isEmpty()) {
            List<Long> doubleWritePartitionIds = new ArrayList<>();
            for (Long partitionId : partitionIds) {
                if (doubleWritePartitions.containsKey(partitionId)) {
                    doubleWritePartitionIds.add(doubleWritePartitions.get(partitionId));
                }
            }

            if (!doubleWritePartitionIds.isEmpty()) {
                TOlapTableSink tSink2 = new TOlapTableSink(tSink);
                tSink2.unsetPartition();
                tSink2.unsetLocation();
                TOlapTablePartitionParam partitionParam2 = createPartition(tSink2.getDb_id(), dstTable, tupleDescriptor,
                        enableAutomaticPartition, automaticBucketSize, doubleWritePartitionIds);
                tSink2.setPartition(partitionParam2);
                tSink2.setLocation(createLocation(dstTable, partitionParam2, enableReplicatedStorage, warehouseId));
                tSink2.setIgnore_out_of_partition(true);

                TDataSink tDataSink2 = new TDataSink();
                tDataSink2.setType(TDataSinkType.OLAP_TABLE_SINK);
                tDataSink2.setOlap_table_sink(tSink2);

                TDataSink newDataSink = new TDataSink(TDataSinkType.MULTI_OLAP_TABLE_SINK);
                tDataSink.setSink_id(0);
                newDataSink.addToMulti_olap_table_sinks(tDataSink);
                tDataSink2.setSink_id(1);
                newDataSink.addToMulti_olap_table_sinks(tDataSink2);
                tDataSink = newDataSink;
            }
        }

        LOG.debug("tDataSink: {}", tDataSink);
    }

Line commented on: tSink.setNodes_info(GlobalStateMgr.getCurrentState().createNodesInfo(warehouseId));

Revised code:
public void complete() throws UserException {
        TOlapTableSink tSink = tDataSink.getOlap_table_sink();

        tSink.setTable_id(dstTable.getId());
        tSink.setTable_name(dstTable.getName());
        tSink.setTuple_id(tupleDescriptor.getId().asInt());
        int numReplicas = 1;
        Optional<Partition> optionalPartition = dstTable.getPartitions().stream().findFirst();
        if (optionalPartition.isPresent()) {
            long partitionId = optionalPartition.get().getId();
            numReplicas = dstTable.getPartitionInfo().getReplicationNum(partitionId);
        }
        tSink.setNum_replicas(numReplicas);
        tSink.setNeed_gen_rollup(dstTable.shouldLoadToNewRollup());
        tSink.setSchema(createSchema(tSink.getDb_id(), dstTable, tupleDescriptor));
        TOlapTablePartitionParam partitionParam = createPartition(tSink.getDb_id(), dstTable, tupleDescriptor,
                enableAutomaticPartition, automaticBucketSize, getOpenPartitions());
        tSink.setPartition(partitionParam);
        tSink.setLocation(createLocation(dstTable, partitionParam, enableReplicatedStorage, warehouseId));
        tSink.setNodes_info(GlobalStateMgr.getCurrentState().createNodesInfo(warehouseId));
        tSink.setPartial_update_mode(this.partialUpdateMode);
        tSink.setAutomatic_bucket_size(automaticBucketSize);
        if (canUseColocateMVIndex(dstTable)) {
            tSink.setEnable_colocate_mv_index(true);
        }

        Map<Long, Long> doubleWritePartitions = dstTable.getDoubleWritePartitions();
        if (!doubleWritePartitions.isEmpty()) {
            List<Long> doubleWritePartitionIds = new ArrayList<>();
            for (Long partitionId : partitionIds) {
                if (doubleWritePartitions.containsKey(partitionId)) {
                    doubleWritePartitionIds.add(doubleWritePartitions.get(partitionId));
                }
            }

            if (!doubleWritePartitionIds.isEmpty()) {
                TOlapTableSink tSink2 = new TOlapTableSink(tSink);
                tSink2.unsetPartition();
                tSink2.unsetLocation();
                TOlapTablePartitionParam partitionParam2 = createPartition(tSink2.getDb_id(), dstTable, tupleDescriptor,
                        enableAutomaticPartition, automaticBucketSize, doubleWritePartitionIds);
                tSink2.setPartition(partitionParam2);
                tSink2.setLocation(createLocation(dstTable, partitionParam2, enableReplicatedStorage, warehouseId));
                tSink2.setIgnore_out_of_partition(true);

                TDataSink tDataSink2 = new TDataSink();
                tDataSink2.setType(TDataSinkType.OLAP_TABLE_SINK);
                tDataSink2.setOlap_table_sink(tSink2);

                TDataSink newDataSink = new TDataSink(TDataSinkType.MULTI_OLAP_TABLE_SINK);
                tDataSink.setSink_id(0);
                newDataSink.addToMulti_olap_table_sinks(tDataSink);
                tDataSink2.setSink_id(1);
                newDataSink.addToMulti_olap_table_sinks(tDataSink2);
                tDataSink = newDataSink;
            }
        }

        LOG.debug("tDataSink: {}", tDataSink);
    }