Skip to content

HIVE-30253: Iceberg: V2 merge-on-read deletes with write.fanout.enabled=false are sorted, but always written by the fanout writer - #6972

Open
deniskuzZ wants to merge 1 commit into
apache:masterfrom
deniskuzZ:HIVE-30253

Conversation

@deniskuzZ

@deniskuzZ deniskuzZ commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Depends on #6925 (HIVE-30113): the clustered writer needs its exact PARTITION__KEY sort. Until it is merged this branch carries its commits; the change of this PR is the last commit.

What changes were proposed in this pull request?

HiveIcebergWriterBase.newDeleteWriter picks the position delete writer of a V2 table by the same rule as the data
writer: ClusteredPositionDeleteWriter when fanout is disabled (!Context.useFanoutWriter()),
FanoutPositionOnlyDeleteWriter otherwise. Before, it built the clustered writer only when the input was marked sorted
by the sorted dynamic partition optimization, which a merge-on-read delete never is. WriterBuilder.Context.inputOrdered() is removed:
nothing reads it now (the flag still feeds useFanoutWriter).

With write.fanout.enabled=false, a merge-on-read DELETE, split UPDATE or MERGE on a V2 table sorts its deleted records
by PARTITION__SPEC__ID, PARTITION__KEY, FILE__PATH, ROW__POSITION (HiveIcebergStorageHandler.acidSortColumns,
since HIVE-28510) so that the clustered writer can stream them. But HiveIcebergStorageHandler.createDPContext returns
no dynamic partition context for a merge-on-read delete (HIVE-26156: the rewrite's sort already clusters the records;
since HIVE-30112 SemanticAnalyzer skips it instead), so FileSinkOperator never marks the write sorted, and
HIVE-28510's inputOrdered ? clustered : fanout always took the fanout writer. Every such statement paid for the sort and
wrote with FanoutPositionOnlyDeleteWriter. The data writer does not have this gap: it already uses the clustered writer
whenever fanout is disabled, with the sort that the SerDe forces through the dynamic partition threshold.

Why are the changes needed?

write.fanout.enabled=false asks for writers that take clustered input and keep one partition open at a time. Spark
honours it the same way for position deletes: with fanout disabled its position-delta writes are ordered by
(_spec_id, _partition, _file, _pos) (SparkWriteUtil), with fanout enabled they are not sorted.

The clustered writer takes each (spec, partition) once and throws if one comes back, so the sort has to cluster the
partitions exactly. PARTITION__KEY (HIVE-30113, the serialized partition values) does; the former PARTITION__HASH
(Objects.hash of the values) could give two partitions one key and interleave them by file path. Hence the base.

Both sides read write.fanout.enabled from the table: the rewrite from the metastore parameters, the writer from the
Iceberg table properties, the same pairing the data writer relies on.

Data and delete writers now take the same rule (!useFanoutWriter() = sorted input or fanout disabled), with one
difference in where sorted input comes from: data writes are sorted either for write.fanout.enabled=false (the SerDe
forces the threshold to 1) or by the sorted dynamic partition optimization's own decision
(hive.optimize.sort.dynamic.partition.threshold: 1 always, N above N writers, 0 by the NDV-estimated partition count,
HIVE-25948). Merge-on-read deletes are sorted by the rewrite, at parse time and before any statistics, and only for
write.fanout.enabled=false, so they follow the property alone, as Spark does. A statistics-based choice for deletes
would move their sort into the optimizer; the max-writers estimate prices ORC stripe buffers, while a position delete
writer holds a bitmap per data file.

Does this PR introduce any user-facing change?

No. With write.fanout.enabled=false, V2 merge-on-read DML now writes with the clustered position delete writer, as the
property asks. Plans and rows are unchanged.

How was this patch tested?

  • iceberg_v2_deletes_fanout_disabled.q (TestIcebergCliDriver): EXPLAIN of the DELETE, the split UPDATE and the MERGE
    (the delete sink behind the ++++ sort) and the rows after each; a table partitioned on two int columns with
    partitions (1, 31) and (2, 0), which Objects.hash maps alike, and object storage paths that interleave their files:
    rows after a DELETE in both partitions as expected.
  • The clustered writer runs: with the sort changed to start with ROW__POSITION (mutation, on this change), the DELETE fails with
    ClusteredWriter's "Incoming records violate the writer assumption that records are clustered by spec and by
    partition within each spec"; the fanout writer accepts any order. (EXPLAIN does not show the writer.)
  • Pass unchanged: iceberg_dml_deleted_record_columns, delete_iceberg_partitioned_{orc,parquet},
    update_iceberg_partitioned_parquet, merge_iceberg_{orc,partitioned_orc}, vectorized_iceberg_merge_mixed,
    write_iceberg_branch, dynamic_partition_writes, ctas_iceberg_partitioned_orc, iceberg_v3_dml_distribution.
  • Known corner: the byte comparison of PARTITION__KEY and Iceberg's comparator differ only for float/double identity
    partition values that are NaN with different payloads.

…ed=false are sorted, but always written by the fanout writer

- the V2 position delete writer follows the data writer's rule: ClusteredPositionDeleteWriter unless the fanout writer is used (Context.useFanoutWriter), so with fanout disabled the deletes the rewrite sorts by spec, partition key, file and position are streamed
- before, the clustered writer needed input marked sorted by the sorted dynamic partition optimization, which never applies to a merge-on-read delete (no DynamicPartitionCtx, HIVE-26156): since HIVE-28510 every such statement sorted, then wrote with FanoutPositionOnlyDeleteWriter
- needs the exact PARTITION__KEY sort of HIVE-30113: the clustered writer takes each partition once
- iceberg_v2_deletes_fanout_disabled.q: EXPLAIN and rows of the DELETE, the split UPDATE and the MERGE; partitions (1, 31) and (2, 0), which PARTITION__HASH mapped alike, with interleaved object storage paths
@sonarqubecloud

sonarqubecloud Bot commented Dec 5, 2026

Copy link
Copy Markdown

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants