@SuppressWarnings("squid:S3824")
    private void write(DataPartitionInfo partitionInfo, ChunkData chunkData) throws IOException {
      if (isClosed) {
        throw new IOException(String.format(MESSAGE_WRITER_MANAGER_HAS_BEEN_CLOSED, taskDir));
      }
      if (!dataPartition2Writer.containsKey(partitionInfo)) {
        File newTsFile =
            SystemFileFactory.INSTANCE.getFile(
                taskDir, partitionInfo.toString() + TsFileConstant.TSFILE_SUFFIX);
        if (!newTsFile.createNewFile()) {
          LOGGER.error("Can not create TsFile {} for writing.", newTsFile.getPath());
          return;
        }

        final long chunkMetadataMaxSizeForEachWriter =
            CONFIG.getLoadChunkMetadataMemorySizeInBytes() / (dataPartition2Writer.size() + 1);
        final TsFileIOWriter writer =
            new TsFileIOWriter(newTsFile, chunkMetadataMaxSizeForEachWriter);
        final TsFileResource resource = new TsFileResource(writer.getFile());
        writer.addFlushListener(
            // Update time index by chunk groups going to be flushed to temp file
            sortedChunkMetadataList ->
                sortedChunkMetadataList.forEach(
                    pair -> {
                      final IDeviceID deviceId = pair.left.left;
                      pair.getRight()
                          .forEach(
                              chunkMetadata -> {
                                resource.updateStartTime(deviceId, chunkMetadata.getStartTime());
                                resource.updateEndTime(deviceId, chunkMetadata.getEndTime());
                              });
                    }));

        // When a new writer is added, we need to reduce the metadata size limit of all existing
        // writers for memory control
        for (final TsFileIOWriter existingWriter : dataPartition2Writer.values()) {
          existingWriter.setMaxMetadataSize(chunkMetadataMaxSizeForEachWriter);
        }
        dataPartition2Writer.put(partitionInfo, writer);
        dataPartition2Resource.put(partitionInfo, resource);
      }
      TsFileIOWriter writer = dataPartition2Writer.get(partitionInfo);

      // Table model needs to register TableSchema
      final String tableName =
          chunkData.getDevice() != null ? chunkData.getDevice().getTableName() : null;
      if (tableName != null
          && !(tableName.startsWith(TREE_MODEL_DATABASE_PREFIX) || tableName.equals(ROOT))) {
        writer
            .getSchema()
            .getTableSchemaMap()
            .computeIfAbsent(
                tableName,
                t ->
                    TableSchema.of(
                            DataNodeTableCache.getInstance()
                                .getTable(partitionInfo.getDataRegion().getDatabaseName(), t))
                        .toTsFileTableSchemaNoAttribute());
      }

      if (!Objects.equals(chunkData.getDevice(), dataPartition2LastDevice.get(partitionInfo))) {
        if (dataPartition2LastDevice.containsKey(partitionInfo)) {
          writer.endChunkGroup();
          writer.checkMetadataSizeAndMayFlush();
        }
        writer.startChunkGroup(chunkData.getDevice());
        dataPartition2LastDevice.put(partitionInfo, chunkData.getDevice());
      }
      chunkData.writeToFileWriter(writer);
    }
It should be noted that all AlignedChunkData of the same partition split from a source file
should be guaranteed to be written to the same new file. Otherwise, for detached
BatchedAlignedChunkData, it may result in no data for the time column in the new file.
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/load/LoadTsFileManager.java