NoahKusaba opened a new pull request, #138:
URL: https://github.com/apache/datafusion-iceberg/pull/138

   ## Which issue does this PR close?
   
   - Closes #38 (including #43, folded into it).
   
   ## Rationale for this change
   
   `insert_into` added a hash repartition and, with 
`write.datafusion.fanout.enabled=false`, a sort on `_partition` before 
`IcebergWriteExec`. Nothing in the plan required either, so the physical 
optimizer was free to remove them, and it did:
   
   - **Fanout disabled, partitioned table:** the sort is removed, so the 
clustered writer gets unsorted input and the INSERT fails (#38).
   - **Fanout disabled, unpartitioned table:** `insert_into` sorted on 
`_partition` whenever fanout was off, but only partitioned tables have that 
column, so the INSERT failed at planning (#43):
   
     ```
     Error during planning: Partition column '_partition' not found in schema. 
Ensure the plan has been extended with partition values using 
project_with_partition.
     ```
   - **Fanout enabled (the default), identity or bucket partitioning:** the 
hash repartition is removed when the input already has enough partitions, so 
every write task can receive every partition value and writes more, smaller 
files.
   
   The unit tests didn't catch this because they inspected the plan 
`insert_into` returns, before physical optimization.
   
   ## What changes are included in this PR?
   
   `IcebergWriteExec` declares what it needs from its input, and the optimizer 
adds it:
   
   - `input_distribution_requirements`: hashed on `_partition` when a partition 
field uses an identity or bucket transform, as `repartition` chose before.
   - `required_input_ordering`: sorted on `_partition` when a partitioned table 
is written without fanout, which is when `TaskWriter` uses the clustered 
writer. Unpartitioned tables require neither.
   
   `insert_into` now only adds the `_partition` column and the write and commit 
nodes. `write/repartition.rs`, `write/sort.rs` and the hand-parsing of the 
fanout property are removed, along with the two unit tests that checked the 
unoptimized plan.
   
   `IcebergWriteExec` no longer overrides `benefits_from_input_partitioning` to 
`false`. `insert_into` used to add a round-robin repartition to 
`target_partitions` itself; with the default, the optimizer adds it, so writes 
from a single-partition source are still spread over `target_partitions` tasks.
   
   AGENTS.md describes the new write path, and notes that write requirements 
belong on `IcebergWriteExec` rather than as nodes added in `insert_into`.
   
   ## Are these changes tested?
   
   Yes. Three tests in `tests/insert_into.rs` run an `INSERT` through SQL from 
a two-partition source and snapshot the optimized plan, the row count and the 
rows read back:
   
   - `hashes_partitioned_writes_by_partition`: `RepartitionExec: 
partitioning=Hash([_partition@2], 2)` under the write.
   - `sorts_partitioned_writes_by_partition_without_fanout`: `SortExec: 
expr=[_partition@2 ASC], preserve_partitioning=[true]` over the hash 
repartition.
   - `inserts_into_an_unpartitioned_table_without_fanout`: neither node, and 
the insert succeeds.
   
   All three fail on `main`: the first two because the optimizer removes the 
repartition and sort, the third with the planning error above.
   
   Larger fanout-disabled inserts into partitioned tables still fail with "The 
input is not sorted!" until #39 is fixed (#81): the plan is now correct, but 
the writer splits each batch's partitions in arbitrary order. These tests write 
one batch per table partition, so they don't depend on it.
   
   ## Are there any user-facing changes?
   
   - `INSERT` with `write.datafusion.fanout.enabled=false` no longer fails at 
planning for unpartitioned tables, and keeps its partition sort for partitioned 
ones.
   - Partitioned writes with identity or bucket transforms are hashed by 
partition even when the input already has enough partitions, so they write 
fewer files.
   - An invalid `write.datafusion.fanout.enabled` value is now reported when 
the write executes rather than at planning.
   


-- 
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]

Reply via email to