You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
INSERT into a partitioned table with fanout disabled always fails: the optimizer removes the partition sort #38
With write.datafusion.fanout.enabled=false, insert_into places a SortExec on _partition, and a hash RepartitionExec on _partition, in front of IcebergWriteExec (table/mod.rs#L200-L220), because ClusteredWriter needs its input grouped by partition.
IcebergWriteExec doesn't declare required_input_ordering or required_input_distribution. It reports maintains_input_order() == [true] but advertises no output ordering (write.rs#L143-L151). DataFusion's EnforceSorting therefore treats the sort as unnecessary and removes it, and EnforceDistribution replaces the hash repartition.
To Reproduce
Create t6(id INT NOT NULL, cat INT NOT NULL), partitioned by identity(cat), with write.datafusion.fanout.enabled=false.
Run INSERT INTO t6 SELECT id, cat FROM src, where src has 200 rows with cat = id % 10 and batch_size = 8.
Optimized plan with target_partitions = 4:
IcebergCommitExec: table=ns.t6
CoalescePartitionsExec
IcebergWriteExec: table=ns.t6
ProjectionExec: expr=[id@0 as id, cat@1 as cat, iceberg_partition_values(cat) as _partition]
RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
DataSourceExec: partitions=1, partition_sizes=[1]
All 10 runs failed, at both target_partitions = 1 and 4:
The input is not sorted! Cannot write to partition that was previously closed: PartitionKey { ... }
Expected behavior
The insert succeeds, with the writer receiving its input clustered by partition.
Additional context
test_insert_plan_fanout_disabled_has_sort passes because it inspects the plan that insert_into returns, which hasn't been through physical optimization.
Declaring the requirements on IcebergWriteExec would let the optimizer keep or re-insert them: required_input_ordering on _partition when fanout is disabled, and a hash required_input_distribution on _partition.
With fanout enabled (the default), the hash repartition is also replaced by round-robin, so every write task writes every partition. The data is correct, but there are more and smaller files than intended.
Folding #43 into this issue, since the fix overlaps.
With write.datafusion.fanout.enabled=false, unpartitioned tables fail too, earlier: insert_into calls sort_by_partition unconditionally, and unpartitioned inputs have no _partition column.
-- t(id INT NOT NULL), unpartitioned, with table property write.datafusion.fanout.enabled=falseINSERT INTO t VALUES (1);
Error during planning: Partition column '_partition' not found in schema. Ensure the plan has been extended with partition values using project_with_partition.
Once IcebergWriteExec declares the ordering requirement, the manual sort_by_partition call in insert_into can go, which removes this failure too. The requirement should only be declared for partitioned tables with fanout disabled. A test that inserts into an unpartitioned table with the property set would cover it.
Activity
andygrove commentedon Oct 1, 2026
Folding #43 into this issue, since the fix overlaps.
With
write.datafusion.fanout.enabled=false, unpartitioned tables fail too, earlier:insert_intocallssort_by_partitionunconditionally, and unpartitioned inputs have no_partitioncolumn.Once
IcebergWriteExecdeclares the ordering requirement, the manualsort_by_partitioncall ininsert_intocan go, which removes this failure too. The requirement should only be declared for partitioned tables with fanout disabled. A test that inserts into an unpartitioned table with the property set would cover it.