Skip to content

fix: require IcebergWriteExec's input sorted by partition without fanout - #138

Open
NoahKusaba wants to merge 2 commits into
apache:mainfrom
NoahKusaba:fix/write-exec-input-requirements
Open

NoahKusaba wants to merge 2 commits into
apache:mainfrom
NoahKusaba:fix/write-exec-input-requirements

Conversation

@NoahKusaba

@NoahKusaba NoahKusaba commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

It also fixes the unpartitioned case reported in #43.

Rationale for this change

With write.datafusion.fanout.enabled=false, insert_into added a sort on _partition before IcebergWriteExec, because the clustered writer needs each task's rows sorted by partition. Nothing in the plan required the sort, so the physical optimizer removed it:

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 implements required_input_ordering: sorted on _partition when a partitioned table is written without fanout, which is when TaskWriter uses the clustered writer. The optimizer adds the sort. Unpartitioned tables require none.
  • insert_into no longer parses the fanout property or adds a sort; write/sort.rs is removed, along with the two unit tests that checked the unoptimized plan.
  • AGENTS.md shows the sort as added by the optimizer, and notes that input requirements belong on IcebergWriteExec rather than as nodes added in insert_into.

The repartition insert_into adds has the same problem: the optimizer removes it too. That's a performance issue rather than a failure, so it's left for a follow-up.

Are these changes tested?

Yes. Three tests in tests/insert_into.rs run an INSERT through SQL and snapshot the optimized plan and the rows read back:

  • sorts_partitioned_writes_by_partition_without_fanout: SortExec: expr=[_partition@2 ASC] under the write.
  • writes_partitioned_tables_unsorted_with_fanout: no sort with fanout enabled (the default), replacing the unit test that checked this on the unoptimized plan.
  • inserts_into_an_unpartitioned_table_without_fanout: no sort, and the insert succeeds.

The first fails on main because the optimizer removes the sort, and 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 a single batch, so they don't depend on it.

Are there any user-facing changes?

  • INSERT with write.datafusion.fanout.enabled=false keeps its partition sort for partitioned tables, and no longer fails at planning for unpartitioned ones.
  • An invalid write.datafusion.fanout.enabled value is now reported when the write executes rather than at planning, like the other write properties.

@NoahKusaba
NoahKusaba force-pushed the fix/write-exec-input-requirements branch from 468ec41 to 52a1777 Compare November 22, 2026 18:33
@NoahKusaba NoahKusaba changed the title fix: declare IcebergWriteExec's input requirements instead of planning them in insert_into fix: require IcebergWriteExec's input sorted by partition without fanout Nov 22, 2026
insert_into added a sort on `_partition` when fanout was disabled, but no
node required it, so the physical optimizer removed it and the clustered
writer failed on unsorted input. For unpartitioned tables the sort was
added anyway and failed at planning, since they have no `_partition`
column.

IcebergWriteExec now declares the ordering through
`required_input_ordering`, only for partitioned tables written without
fanout, and the optimizer adds the sort. `write/sort.rs` and the fanout
parsing in insert_into are removed, along with the unit tests that
inspected the unoptimized plan.
@NoahKusaba
NoahKusaba force-pushed the fix/write-exec-input-requirements branch from 52a1777 to 18ac36f Compare November 22, 2026 19:09
@NoahKusaba
NoahKusaba marked this pull request as ready for review November 22, 2026 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

INSERT into a partitioned table with fanout disabled always fails: the optimizer removes the partition sort

1 participant