iceberg icon indicating copy to clipboard operation
iceberg copied to clipboard

Rewrite data files can create more small files after partition evolution

Open lirui-apache opened this issue 9 months ago • 3 comments

Feature Request / Improvement

When rewriting data files, the output spec defaults to the current spec. If the partition spec has evolved to be finer-grained, the action can produce more small files instead of fewer. Like in the following example:

sql("CREATE TABLE foo (id int, dt date) USING iceberg PARTITIONED BY (dt)");
sql("INSERT INTO foo VALUES (1, date('2024-01-01')), (2, date('2024-01-01'))");
sql("INSERT INTO foo VALUES (3, date('2024-01-01')), (4, date('2024-01-01'))");

// someone adds a partition field
sql("ALTER TABLE foo ADD PARTITION FIELD id");

// a background service periodically merging small files end up creating more of them
sql("CALL spark_catalog.system.rewrite_data_files("
                + "table => 'foo', strategy => 'binpack', "
                + "options => map('min-input-files', '2', 'rewrite-all', 'true'))");

I wonder whether we should by default write output files using the same spec as input, at least for the bin-pack strategy?

Query engine

Spark

Willingness to contribute

  • [ ] I can contribute this improvement/feature independently
  • [ ] I would be willing to contribute this improvement/feature with guidance from the Iceberg community
  • [ ] I cannot contribute this improvement/feature at this time

lirui-apache avatar Feb 27 '26 13:02 lirui-apache

I think we already let you specify the output spec

It may be helpful to have a parameter which just only does compaction within existing specs but that may be conflicting with some of the other parameters which we have. I'm also not sure if this should be something we do for users vs them calling the function with the specific intent they have (compact to new spec or not)

RussellSpitzer avatar Feb 27 '26 17:02 RussellSpitzer

@RussellSpitzer Yes we have the output-spec-id option. But to use it, our service needs to figure out which spec the input files belong to, which adds extra cost. Besides, it does not handle the case where input files belong to multiple specs. So I think it's helpful to have a parameter indicating compaction should be done within input specs. When the intent is to compact small files, this guarantees we won't get things worse.

lirui-apache avatar Feb 28 '26 02:02 lirui-apache

There is a mechanical detail in the planner that I think answers @RussellSpitzer's question about whether this should be automatic, and also supports @lirui-apache's point that output-spec-id is not sufficient.

Files whose spec is not the current spec are not grouped by their own partition. They are all collapsed into a single bucket:

// BinPackRewriteFilePlanner.groupByPartition
StructLike emptyStruct = GenericRecord.create(partitionType);
...
// If a task uses an incompatible partition spec the data inside could contain values
// which belong to multiple partitions in the current spec. Treating all such files as
// un-partitioned and grouping them together helps to minimize new files made.
StructLike taskPartition =
    task.file().specId() == table.spec().specId() ? task.file().partition() : emptyStruct;

filesByPartition.computeIfAbsent(taskPartition, unused -> Lists.newArrayList()).add(task);

and the output spec defaults to the current one:

// SizeBasedFileRewritePlanner
PropertyUtil.propertyAsInt(options, RewriteDataFiles.OUTPUT_SPEC_ID, table.spec().specId());

Two consequences worth having in the thread:

1. This is the mechanism behind the report, and it is not just a default gone wrong. In @lirui-apache's example the dt-only files land in the emptyStruct bucket and are then written with the current (dt, id) spec. One input group fans out to one output file per distinct (dt, id). The comment above the grouping is accurate about its own intent — minimising new files within the current spec — but when the spec has become finer, minimising groups maximises output files.

2. output-spec-id cannot express "keep the input spec", for a stronger reason than the extra lookup cost. The emptyStruct bucket holds files from every non-current spec at once, so with three specs a single group can contain files from two different old specs. output-spec-id is one value per rewrite, and RewriteFileGroup.outputSpecId() is one value per group, so there is no value that means "each file keeps its own spec" — the grouping has already destroyed that information.

So this is not purely a question of defaults or of user intent. Expressing "compact within the input spec" needs the planner to group by (specId, partition) rather than partition-with-an-emptyStruct-fallback, after which a rewrite-within-input-spec style option becomes representable and the current behaviour stays reachable as the default.

That reframes the choice: rather than "should we do this for users", it is "should the planner keep spec identity so that either behaviour can be asked for". Keeping the current grouping as the default preserves today's semantics for everyone who wants convergence onto the new spec.

I have not written any of this — flagging it because the thread had stalled on a design question that the grouping code partly answers. Happy to prototype the (specId, partition) grouping if that direction seems right to you.

vbhanuchander-lang avatar Aug 11 '26 09:08 vbhanuchander-lang