Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7f2d5b0
Add ParquetFileMerger for efficient row-group level file merging
shangxinli Oct 13, 2025
fa1d073
Address feedbacks
shangxinli Nov 4, 2025
7a34353
Address feedbacks for second round
shangxinli Nov 12, 2025
c150887
Address comments round 3
shangxinli Nov 16, 2025
c593e9e
Trigger CI
shangxinli Nov 17, 2025
c71b419
Add row lineage preservation to ParquetFileMerger with binpack-compat…
shangxinli Nov 23, 2025
4ddb5b4
Address feedback of adding row_id support
shangxinli Nov 24, 2025
4130a79
Address feedback for another round
shangxinli Nov 26, 2025
4e4874e
Address comments for another round
shangxinli Nov 26, 2025
eabaa0d
Address another round of feedbacks
shangxinli Nov 27, 2025
55aa295
Merge branch 'main' into rewrite_data_files2
shangxinli Nov 27, 2025
047f9b6
Address feedback for another round
shangxinli Nov 28, 2025
0709582
Simplify ParquetFileMerger API to accept DataFile objects
shangxinli Dec 2, 2025
cdc322d
Initialize columnIndexTruncateLength internally in ParquetFileMerger
shangxinli Dec 3, 2025
853fd19
Address review feedback: refactor ParquetFileMerger API and validation
shangxinli Dec 5, 2025
4c4f2cb
Refactor ParquetFileMerger API to return MessageType
shangxinli Dec 6, 2025
aa5fc36
Address review feedback: optimize validation and file I/O
shangxinli Dec 8, 2025
5962e74
Address review feedback
shangxinli Dec 20, 2025
2eca995
Refactor SparkParquetFileMergeRunner to pass RewriteFileGroup to exec…
shangxinli Dec 21, 2025
45b0197
Inline ParquetFileReader in try-with-resources block
shangxinli Dec 28, 2025
3194f1e
Address pvary's review comments on ParquetFileMerger PR
shangxinli Jan 7, 2026
66532a3
Address reviewer comments on ParquetFileMerger PR
Jan 25, 2026
417e0fa
Trigger CI re-run
Jan 25, 2026
2404008
Add partition spec ID validation for binary merge
Jan 31, 2026
a471220
Address pvary review comments on test structure
Feb 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions api/src/main/java/org/apache/iceberg/actions/RewriteDataFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,30 @@ public interface RewriteDataFiles
*/
String OUTPUT_SPEC_ID = "output-spec-id";

/**
* Use Parquet row-group level merging during rewrite operations when applicable.
*
* <p>When enabled, Parquet files will be merged at the row-group level using zero-copy operations
Comment thread
pvary marked this conversation as resolved.
Outdated
* instead of full read-rewrite. This provides significant performance improvements (up to 13x
* faster) for compatible Parquet files.
*
* <p>Requirements for row-group merging:
*
* <ul>
* <li>All files must be in Parquet format
* <li>Files must have compatible schemas
* <li>Files must not be encrypted
* </ul>
Comment thread
pvary marked this conversation as resolved.
*
* <p>If requirements are not met, the rewrite will automatically fall back to the standard
* read-rewrite approach with a warning logged.
*
* <p>Defaults to false.
*/
String USE_PARQUET_ROW_GROUP_MERGE = "use-parquet-row-group-merge";
Comment thread
pvary marked this conversation as resolved.

boolean USE_PARQUET_ROW_GROUP_MERGE_DEFAULT = false;

/**
* Choose BINPACK as a strategy for this rewrite operation
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/*
* 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.iceberg.parquet;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.apache.iceberg.io.InputFile;
import org.apache.iceberg.io.OutputFile;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.parquet.hadoop.ParquetFileWriter;
import org.apache.parquet.hadoop.ParquetWriter;
import org.apache.parquet.schema.MessageType;

/**
* Utility class for performing strict schema validation and merging of Parquet files at the
* row-group level.
*
* <p>This class ensures that all input files have identical Parquet schemas before merging. The
* merge operation is performed by copying row groups directly without
* serialization/deserialization, providing significant performance benefits over traditional
* read-rewrite approaches.
*
* <p>This class works with any Iceberg FileIO implementation (HadoopFileIO, S3FileIO, GCSFileIO,
* etc.), making it cloud-agnostic.
*
* <p>TODO: Encrypted tables are not supported
Comment thread
pvary marked this conversation as resolved.
Outdated
*
* <p>Key features:
*
* <ul>
* <li>Zero-copy row group merging using {@link ParquetFileWriter#appendFile}
* <li>Strict schema validation - all files must have identical {@link MessageType}
* <li>Metadata merging for Iceberg-specific footer data
* <li>Works with any FileIO implementation (local, S3, GCS, Azure, etc.)
* </ul>
Comment thread
pvary marked this conversation as resolved.
Outdated
*
* <p>Typical usage:
*
* <pre>
* FileIO fileIO = table.io();
* List&lt;InputFile&gt; inputFiles = Arrays.asList(
* fileIO.newInputFile("s3://bucket/file1.parquet"),
* fileIO.newInputFile("s3://bucket/file2.parquet")
* );
* OutputFile outputFile = fileIO.newOutputFile("s3://bucket/merged.parquet");
* ParquetFileMerger.mergeFiles(inputFiles, outputFile, null);
* </pre>
*/
public class ParquetFileMerger {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please describe me why we order the methods in this file, as we do it?

In several comment I have mentioned that please make sure that we follow some logic here.
Examples:

  • Public methods first
  • package private methods second
  • private methods at the end

Or:

  • one public method first
  • private methods which are used only with this public method
  • another public method
  • private methods which are used only with this public method
  • private methods which are used by both public methods.

I would prefer the first, or migth be accept the one you have chosen, but I don't understand the logic as it stands.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bump.

@shangxinli: Could you please take a look at this comment?


private ParquetFileMerger() {
// Utility class - prevent instantiation
}

/**
* Merges multiple Parquet files into a single output file at the row-group level using Iceberg
* FileIO.
*
* <p>This method works with any Iceberg FileIO implementation (S3FileIO, GCSFileIO, etc.), not
* just HadoopFileIO.
*
* <p>All input files must have identical Parquet schemas ({@link MessageType}), otherwise an
* exception is thrown. The merge is performed by copying row groups directly without
* serialization/deserialization.
*
* @param inputFiles List of Iceberg input files to merge
* @param outputFile Iceberg output file for the merged result
* @param extraMetadata Additional metadata to include in the output file footer (can be null)
* @throws IOException if I/O error occurs during merge operation
* @throws IllegalArgumentException if no input files provided or schemas don't match
*/
Comment on lines +152 to +159

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The javadoc is nice here, but remove the paramters.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

public static void mergeFiles(
Comment thread
pvary marked this conversation as resolved.
Outdated
List<InputFile> inputFiles, OutputFile outputFile, Map<String, String> extraMetadata)
throws IOException {
Preconditions.checkArgument(
inputFiles != null && !inputFiles.isEmpty(), "No input files provided for merging");

// Validate and get the common schema from the first file
org.apache.parquet.io.InputFile firstParquetFile = ParquetIO.file(inputFiles.get(0));
MessageType schema =
org.apache.parquet.hadoop.ParquetFileReader.open(firstParquetFile)
.getFooter()
.getFileMetaData()
.getSchema();
Comment thread
pvary marked this conversation as resolved.
Outdated

// Validate all files have the same schema
for (int i = 1; i < inputFiles.size(); i++) {
org.apache.parquet.io.InputFile parquetFile = ParquetIO.file(inputFiles.get(i));
MessageType currentSchema =
org.apache.parquet.hadoop.ParquetFileReader.open(parquetFile)
.getFooter()
.getFileMetaData()
.getSchema();

if (!schema.equals(currentSchema)) {
throw new IllegalArgumentException(
String.format(
"Schema mismatch detected: file '%s' has schema %s but file '%s' has schema %s. "
+ "All files must have identical Parquet schemas for row-group level merging.",
inputFiles.get(0).location(), schema, inputFiles.get(i).location(), currentSchema));
}
}

// Create the output Parquet file writer
org.apache.parquet.io.OutputFile parquetOutputFile = ParquetIO.file(outputFile);
try (ParquetFileWriter writer =
new ParquetFileWriter(
Comment thread
shangxinli marked this conversation as resolved.
Outdated
parquetOutputFile,
schema,
ParquetFileWriter.Mode.CREATE,
ParquetWriter.DEFAULT_BLOCK_SIZE,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very sure about this part. Should it be directly fixed to ParquetWriter.DEFAULT_BLOCK_SIZE, or does it need to be linked with the table property write.parquet.row-group-size-bytes?

@shangxinli shangxinli Nov 16, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel using the property gives more flexibility for rewriter. But like to hear other's thoughts.

0)) {

writer.start();

// Append each input file's row groups to the output
for (InputFile inputFile : inputFiles) {
writer.appendFile(ParquetIO.file(inputFile));
}

// End writing with optional metadata
if (extraMetadata != null && !extraMetadata.isEmpty()) {
writer.end(extraMetadata);
} else {
writer.end(java.util.Collections.emptyMap());
}
}
}

/**
* Checks if a list of Iceberg InputFiles can be merged (i.e., they all have identical schemas).
*
* <p>This method works with any Iceberg FileIO implementation (S3FileIO, GCSFileIO, etc.).
*
* @param inputFiles List of Iceberg input files to check
* @return true if all files have identical schemas and can be merged, false otherwise
*/
public static boolean canMerge(List<InputFile> inputFiles) {
try {
if (inputFiles == null || inputFiles.isEmpty()) {
return false;
}

// Read schema from the first file
org.apache.parquet.io.InputFile firstParquetFile = ParquetIO.file(inputFiles.get(0));
MessageType firstSchema =
org.apache.parquet.hadoop.ParquetFileReader.open(firstParquetFile)
.getFooter()
.getFileMetaData()
.getSchema();

// Validate all remaining files have the same schema
for (int i = 1; i < inputFiles.size(); i++) {
org.apache.parquet.io.InputFile parquetFile = ParquetIO.file(inputFiles.get(i));
MessageType currentSchema =
org.apache.parquet.hadoop.ParquetFileReader.open(parquetFile)
.getFooter()
.getFileMetaData()
.getSchema();

if (!firstSchema.equals(currentSchema)) {
return false;
}
}

return true;
} catch (IllegalArgumentException | IOException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class RewriteDataFilesSparkAction
REWRITE_JOB_ORDER,
OUTPUT_SPEC_ID,
REMOVE_DANGLING_DELETES,
USE_PARQUET_ROW_GROUP_MERGE,
BinPackRewriteFilePlanner.MAX_FILES_TO_REWRITE);

private static final RewriteDataFilesSparkAction.Result EMPTY_RESULT =
Expand Down Expand Up @@ -119,7 +120,7 @@ protected RewriteDataFilesSparkAction self() {
@Override
public RewriteDataFilesSparkAction binPack() {
ensureRunnerNotSet();
this.runner = new SparkBinPackFileRewriteRunner(spark(), table);
this.runner = createFileRewriteRunner();
Comment thread
pvary marked this conversation as resolved.
Outdated
return this;
}

Expand Down Expand Up @@ -151,6 +152,21 @@ private void ensureRunnerNotSet() {
runner == null ? null : runner.description());
}

private FileRewriteRunner<FileGroupInfo, FileScanTask, DataFile, RewriteFileGroup>
createFileRewriteRunner() {
// Read the option from action options only (no table property fallback)
boolean useParquetRowGroupMerge =
PropertyUtil.propertyAsBoolean(
options(), USE_PARQUET_ROW_GROUP_MERGE, USE_PARQUET_ROW_GROUP_MERGE_DEFAULT);

if (useParquetRowGroupMerge) {
LOG.info("Using Parquet row-group merge runner for rewrite operation");
return new SparkParquetFileMergeRunner(spark(), table);
} else {
return new SparkBinPackFileRewriteRunner(spark(), table);
}
}

@Override
public RewriteDataFilesSparkAction filter(Expression expression) {
filter = Expressions.and(filter, expression);
Expand Down
Loading