Skip to content

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

Open

Description

@andygrove

Describe the bug

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

  1. Create t6(id INT NOT NULL, cat INT NOT NULL), partitioned by identity(cat), with write.datafusion.fanout.enabled=false.
  2. 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.
  • Even with the sort kept, clustered writes still fail for a second reason: Clustered (fanout disabled) writes fail on sorted input because batches are split in HashMap order #39. The unpartitioned case of this setting fails separately: INSERT into an unpartitioned table fails when fanout is disabled #43.

Activity

  1. added
    bug
    on Oct 1, 2026
  2. andygrove commented on Oct 1, 2026

    @andygrove
    MemberAuthor

    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=false
    INSERT 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions