NoahKusaba opened a new pull request, #134:
URL: https://github.com/apache/datafusion-iceberg/pull/134
## Which issue does this PR close?
N/A. Refactor; no behavior change.
## Rationale for this change
`insert_into` calls `repartition` to spread its input across the session's
target partitions before writing. It hashes rows on `_partition` when a
partition field uses an identity or bucket transform, and otherwise distributes
batches round-robin. The code around that is harder to read than it needs to be:
- **The docs make a false claim.** They say it "only adds repartitioning
when the input partitioning differs from the target", but it always wraps its
input in a `RepartitionExec`.
- **The docs are long and repeated.** About 100 lines document two
`pub(crate)` functions. The strategy is explained on both `repartition` and
`determine_partitioning_strategy`, and the example is an `ignore` block that is
never compiled.
- **The function takes more than it uses.** It takes `TableMetadataRef` but
only reads the default partition spec, so its tests build whole tables.
- **The tests are long and check little.** 13 tests and about 700 lines,
checked with `matches!`, `contains` and hand-written panics. Several don't test
this function:
- `test_repartition_zero_partitions_fails` only checks that
`NonZeroUsize::new(0)` is `None`.
- `test_schema_ref_convenience_method` checks iceberg's
`Table::current_schema_ref`.
- `test_none_distribution_mode_fallback` sets `write.distribution-mode`,
which the code never reads.
- `test_bucket_aware_partitioning` puts the bucket transform in the sort
order of an unpartitioned table and accepts either partitioning, so it can't
fail.
## What changes are included in this PR?
- `repartition` takes `&PartitionSpec` instead of `TableMetadataRef`. Its
one caller passes `table.metadata().default_partition_spec()`.
- A short doc comment on `repartition` replaces the docs on both functions.
- `partitioning`, renamed from `determine_partitioning_strategy`, takes the
input schema and uses early returns instead of matching on `(bool, Result)`.
- Two tests replace 13:
- `test_partitioning_by_transform` snapshots the partitioning chosen for
each transform and for a spec mixing them:
```
[] => RoundRobinBatch(4)
[identity(id)] => Hash([_partition@1], 4)
[bucket[8](id)] => Hash([_partition@1], 4)
[truncate[2](name)] => RoundRobinBatch(4)
[year(ts)] => RoundRobinBatch(4)
[month(ts)] => RoundRobinBatch(4)
[day(ts)] => RoundRobinBatch(4)
[hour(ts)] => RoundRobinBatch(4)
[day(ts), identity(id)] => Hash([_partition@1], 4)
```
The old tests didn't cover year, hour or truncate.
- `test_partition_column_is_required_only_for_partitioned_tables`
snapshots that an unpartitioned table needs no `_partition` column and that a
partitioned one fails without it. The old tests didn't cover the error.
The decisions and the error message are unchanged. Both new tests also pass
against the old function body, with only its parameter changed from
`TableMetadata` to the partition spec it read.
## Are these changes tested?
Yes, by the two tests above. Partitioned inserts in `tests/insert_into.rs`
run the repartition end to end.
## Are there any user-facing changes?
No.
Round-robin for truncate and the year/month/day/hour transforms can leave
each writer with every partition value, and so more, smaller files. That's a
behavior question for a separate issue; the table above shows the current rules.
## AI Disclosure
- Used Claude Code to review the write path, make the change and draft this
description. I reviewed the change.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]