Skip to content

Draft: [GLUTEN-12474][VL] Re-add partition-column sort for row-based fallback write with dynamic partitions.#12523

Open
littlelittlewhite09 wants to merge 2 commits into
apache:mainfrom
littlelittlewhite09:fix-writeSort
Open

Draft: [GLUTEN-12474][VL] Re-add partition-column sort for row-based fallback write with dynamic partitions.#12523
littlelittlewhite09 wants to merge 2 commits into
apache:mainfrom
littlelittlewhite09:fix-writeSort

Conversation

@littlelittlewhite09

@littlelittlewhite09 littlelittlewhite09 commented Jul 15, 2026

Copy link
Copy Markdown

Problem:
When writing to a dynamically-partitioned table via the row-based fallback WriteFilesExec (e.g. ORC/Hive tables where the native Velox write is not applicable), the job fails with FileAlreadyExistsException.

Root cause:

  • With planned write enabled, V1Writes drops the write's required sort at logical optimization time when the child already provides the ordering (e.g. a SortAggregate keyed by the partition column outputs rows already ordered by that column). So the physical WriteFilesExec.requiredChildOrdering is Nil.
  • Gluten then replaces the SortAggregate with an unordered native hash aggregate, and EliminateLocalSort eagerly removes the now-redundant local sort feeding it. The ordering the write implicitly depended on is destroyed.
  • When the write falls back to the row-based WriteFilesExec, its DynamicPartitionDataSingleWriter requires rows sorted by the partition columns. Unsorted rows make it re-create an already-closed partition file and fail with FileAlreadyExistsException.

What changes are proposed in this pull request?

fix #12474

How was this patch tested?

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Opus 4.8

…k write with dynamic partitions.

Problem:
When writing to a dynamically-partitioned table via the row-based fallback
WriteFilesExec (e.g. ORC/Hive tables where the native Velox write is not
applicable), the job fails with FileAlreadyExistsException.

Root cause:
  - With planned write enabled, `V1Writes` drops the write's required sort at
    logical optimization time when the child already provides the ordering
    (e.g. a `SortAggregate` keyed by the partition column outputs rows already
    ordered by that column). So the physical `WriteFilesExec.requiredChildOrdering`
    is `Nil`.
  - Gluten then replaces the `SortAggregate` with an unordered native hash
    aggregate, and `EliminateLocalSort` eagerly removes the now-redundant local
    sort feeding it. The ordering the write implicitly depended on is destroyed.
  - When the write falls back to the row-based `WriteFilesExec`, its
    `DynamicPartitionDataSingleWriter` requires rows sorted by the partition
    columns. Unsorted rows make it re-create an already-closed partition file
    and fail with `FileAlreadyExistsException`.

Fix:
Add the `EnsureRowBasedWriteFilesOrdering` post-transform rule that re-adds the
local sort on the dynamic partition (and bucket) columns for the row-based
fallback write only. Native Velox writes do not need the ordering and are
skipped (via the `ColumnarWriteFilesExec.NoopLeaf` check). The required ordering
and the "already satisfied" check reuse `V1WritesUtils` so behavior stays
identical to vanilla `FileFormatWriter`. When the write's child is an offloaded
transformer, a native `SortExecTransformer` is inserted (validated, falling back
to row-based `SortExec`) so the sort runs columnar in Velox. The rule runs
before `InsertTransitions`.
@littlelittlewhite09 littlelittlewhite09 changed the title Daft: [GLUTEN-12474][VL] Re-add partition-column sort for row-based fallback write with dynamic partitions. Draft: [GLUTEN-12474][VL] Re-add partition-column sort for row-based fallback write with dynamic partitions. Jul 15, 2026
@github-actions github-actions Bot added CORE works for Gluten Core VELOX labels Jul 15, 2026
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

@littlelittlewhite09

Copy link
Copy Markdown
Author

It seems that this pr is not compatiable with spark3.3

@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

…m to fix Spark 3.3 build.

EnsureRowBasedWriteFilesOrdering imported V1WritesUtils directly, but that class
only exists since Spark 3.4 (planned write, SPARK-37287). Since the rule lives in
gluten-substrait, which is compiled against every supported Spark profile,
building with -Pspark-3.3 failed to resolve the import.
@github-actions

Copy link
Copy Markdown

Run Gluten Clickhouse CI on x86

@philo-he

Copy link
Copy Markdown
Member

@littlelittlewhite09, thanks for the PR. Please check if the test failure is related. You may need to rebase the code if some errors are unrelated. Thanks.

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

Labels

CORE works for Gluten Core VELOX

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[VL] Gluten fails to satisfy the ordering required by dynamic partition column

2 participants