Skip to content

feat(core): support sort manifest entries by partition#7866

Open
baiyangtx wants to merge 14 commits into
apache:masterfrom
baiyangtx:manifest-sort
Open

feat(core): support sort manifest entries by partition#7866
baiyangtx wants to merge 14 commits into
apache:masterfrom
baiyangtx:manifest-sort

Conversation

@baiyangtx
Copy link
Copy Markdown
Contributor

Purpose

Manifest entries are currently written in arrival order, which scatters entries belonging
to the same partition across multiple manifest files. This leads to:

  • Inefficient partition-pruning during reads — the reader must scan many manifest files
    to find all entries for a single partition.
  • Suboptimal data locality when compaction reorganizes files.

This PR introduces manifest entry sorting by partition, so that entries for the same
partition are clustered together within manifest files.

Changes

New configuration options:

Option Default Description
manifest.merge.sorted true Sort entries by partition during manifest full compaction
manifest.merge.sort-on-commit false Sort entries by partition during manifest full merge in commit
manifest.delta.sorted true Sort entries by partition when writing manifest delta

Core implementation:

  • ManifestFileMerger: Introduces mergeSortedByPartition() and mergeUnsorted().
    When manifest.merge.sorted is enabled, entries are collected into a
    BinaryExternalSortBuffer (with spill-to-disk support), then written in
    partition-major order: (partition, bucket, level).
  • ManifestFileMerger.createManifestEntryComparator(): Comparator used for
    sorting delta manifests, falling back to pure-Java comparison when codegen
    is unavailable.
  • FileStoreCommitImpl: Wires sort parameters into all three paths —
    commit manifest merge, delta file writing, and manifest compaction.

Tests

# Manifest merge with sorting
mvn -pl paimon-core -am -DfailIfNoTests=false \
    -Dtest=ManifestFileMetaTest test

# No-partition edge case
mvn -pl paimon-core -am -DfailIfNoTests=false \
    -Dtest=NoPartitionManifestFileMetaTest test

# Spark compact procedure
mvn -pl paimon-spark/paimon-spark-ut -am -DfailIfNoTests=false \
    -Dtest=CompactProcedureTestBase test

Copy link
Copy Markdown
Contributor

@Aitozi Aitozi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM +1, Please resolve the conflict

@Aitozi
Copy link
Copy Markdown
Contributor

Aitozi commented May 21, 2026

CC @JingsongLi for another look

@JingsongLi
Copy link
Copy Markdown
Contributor

Hi @baiyangtx @Aitozi , please take a look to #7842 , I didn't look closely, maybe the abilities of these two PR implementations are the same.

@baiyangtx
Copy link
Copy Markdown
Contributor Author

baiyangtx commented May 22, 2026

Hi @baiyangtx @Aitozi , please take a look to #7842 , I didn't look closely, maybe the abilities of these two PR implementations are the same.

Thanks @discivigour for working on this too. My PR #7866 takes a different approach — sorting inline during the existing full compaction path, rather than a separate post-commit sort pipeline. Key differences:

  • No new compact type — sorting happens inside ManifestFileMerger.tryFullCompaction, reusing the existing full compaction trigger and lifecycle. No additional manifest scans or rewrite passes.
  • Memory-safe with external sort — uses BinaryExternalSortBuffer (already battle-tested across Paimon). When entries exceed manifest.merge.sort.buffer, it automatically spills to disk. No risk of OOM regardless of manifest count.
  • Finer-grained config — separate toggles for merge compaction, commit-time merge, and delta writing, instead of a single switch.

Happy to align with the community's direction, but I believe the inline approach is simpler and less invasive.

张永翔 added 14 commits May 22, 2026 14:44
- Sort delta entries during commit
- Sort base entries via full merge

See merge request: !854
The manifest entry sorting feature introduced in the parent commit
used FileKind.DELETE in mergeUnsorted() and mergeSortedByPartition()
without importing org.apache.paimon.manifest.FileKind, causing all
modules that depend on paimon-core to fail compilation.
The sorting refactor eliminated the single call to sequentialBatchedExecute,
but left the static import, causing Checkstyle to fail the build.
- FileStoreCommitImpl: use options.xxx() instead of local variables
  (manifestTargetSize, manifestMergeMinCount, etc. don't exist)
- FileStoreCommitImpl: replace coreOptions with options
- ManifestFileMerger: replace entry.toBytes() with
  entrySerializer.serializeToBytes() (toBytes() doesn't exist on apache/master)
- ManifestFileMerger: extract createPartitionRecordComparator() to handle
  partition row comparison (InternalRowUtils.compare takes DataTypeRoot,
  not RowType; newRecordComparator takes boolean[], not boolean)
- Remove stale reader Function referencing deleted readForFullCompaction
- Add missing DataType import, remove unused Function/singletonList imports
createPartitionRecordComparator now returns RecordComparator instead of
Comparator<BinaryRow>, since RecordComparator extends
Comparator<InternalRow> and newRecordComparator already returns
RecordComparator. This avoids a type incompatibility at the assignment
site where the result was used as RecordComparator.
Manifest sorting changes the read order of results across partitions.
The test compared results using containsExactly which requires exact
order; changed to containsExactlyInAnyOrder to match the same approach
used in CompactProcedureTestBase.scala.
Manifest sorting changes read order; the test expected sorted output.
Added explicit sort by id to make the test order-independent.
… assertion

- testFullCompactionReadManifestsInParallel: removed parallel read
  assertions since reads are now sequential in mergeSortedByPartition
- BlobTableTest/MultipleBlobTableTest: disable MANIFEST_MERGE_SORTED
  and MANIFEST_DELTA_SORTED since blob/vector-store code depends on
  original file ordering
…rtions

The manifest sorting feature changes the order of results. All
containsExactlyElementsOf in IncrementalClusterActionITCase need to
be changed to containsExactlyInAnyOrderElementsOf, not just the one
in the assertResult helper.
…in Spark tests

CompactProcedureTestBase and RescaleProcedureTest had additional
containsExactlyElementsOf calls that were not updated in the
original PR. All changed to containsExactlyInAnyOrderElementsOf.
- LanceFormatTest: add ORDER BY a to LIMIT queries
- SparkWriteITCase.testWriteWithDefaultValue: add ORDER BY a to
  all SELECT queries whose results are compared via toString()
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.

3 participants