refactor(mergetree): introduce InMemorySortBuffer and MergedKeyValueRecordReader#253
Merged
Conversation
…ValueRecordReader Refactor write_buffer to use the new SortBuffer abstraction (BinaryInMemorySortBuffer / BinaryExternalSortBuffer) and introduce MergedKeyValueRecordReader for merged key-value reading. Also adapt key_value_in_memory_record_reader, merge_tree_writer, spill reader/writer and core_options accordingly.
There was a problem hiding this comment.
Pull request overview
This PR refactors merge-tree buffering/iteration by introducing a SortBuffer abstraction (with a new in-memory implementation) and adds MergedKeyValueRecordReader to merge duplicate keys on read, updating writer/read paths and unit tests accordingly.
Changes:
- Add
SortBufferinterface andBinaryInMemorySortBufferimplementation, moving in-memory buffering/sorting logic out ofWriteBuffer. - Refactor
WriteBuffer/MergeTreeWriterto build readers viaSortBufferand to clear buffer state explicitly. - Introduce
MergedKeyValueRecordReader(+ tests) and updateKeyValueInMemoryRecordReadersemantics/tests.
Reviewed changes
Copilot reviewed 20 out of 20 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/paimon/core/utils/batch_writer.h | Extends BatchWriter with memory usage reporting and FlushMemory() API. |
| src/paimon/core/postpone/postpone_bucket_writer.h | Implements FlushMemory() via existing flush path. |
| src/paimon/core/append/append_only_writer.h | Implements FlushMemory() via non-blocking flush. |
| src/paimon/core/mergetree/write_buffer.h | Refactors WriteBuffer to delegate to SortBuffer and expose reader creation/clearing. |
| src/paimon/core/mergetree/write_buffer.cpp | Builds BinaryInMemorySortBuffer and wraps produced readers with MergedKeyValueRecordReader. |
| src/paimon/core/mergetree/write_buffer_test.cpp | Updates tests to new WriteBuffer API + memory estimation location. |
| src/paimon/core/mergetree/sort_buffer.h | Adds the new SortBuffer interface. |
| src/paimon/core/mergetree/binary_in_memory_sort_buffer.h / .cpp | Adds in-memory sort buffer implementation and memory estimation helper. |
| src/paimon/core/io/key_value_in_memory_record_reader.h / .cpp | Adjusts in-memory reader to emit sorted raw KVs (no merge), adds sequence field sort direction. |
| src/paimon/core/io/key_value_in_memory_record_reader_test.cpp | Updates expected behavior and adds descending sequence-fields coverage. |
| src/paimon/core/io/merged_key_value_record_reader.h / .cpp | Adds reader wrapper that merges duplicate keys using merge function. |
| src/paimon/core/io/merged_key_value_record_reader_test.cpp | New tests validating merge behavior across underlying batches. |
| src/paimon/core/mergetree/spill_writer.cpp | Minor type annotation tweak for compression type. |
| src/paimon/core/mergetree/spill_reader.cpp | Minor type annotation tweak for file status + level constant usage. |
| src/paimon/core/mergetree/merge_tree_writer.h / .cpp | Removes writer-held sequence tracking and adapts flush path to new WriteBuffer API. |
| src/paimon/CMakeLists.txt | Registers new sources/tests in the build. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
lxy-9602
reviewed
Apr 27, 2026
lucasfang
reviewed
Apr 28, 2026
lucasfang
reviewed
Apr 28, 2026
lucasfang
reviewed
Apr 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
Linked issue: #149
This PR refactors the merge-tree write buffer to be built on top of a new
SortBufferabstraction, and introduces aMergedKeyValueRecordReaderthat performs merge-on-read over multiple key-value record readers.Main changes:
SortBufferinterface andInMemorySortBufferWriteBufferto delegate sorting/iteration to the newSortBufferimplementations, removing the previous duplicated sort logic.KeyValueInMemoryRecordReader: previously,KeyValueInMemoryRecordReaderhandled both sorting and merging of duplicate keys. Now,KeyValueInMemoryRecordReaderis responsible only for sorting, whileMergedKeyValueRecordReaderhandles merging duplicate keys.Tests
New / updated unit tests:
core/io/merged_key_value_record_reader_test.cpp(new)core/io/key_value_in_memory_record_reader_test.cpp(updated)core/mergetree/write_buffer_test.cpp(updated)API and Format
No.
Documentation
No new user-facing feature; no documentation change required.
Generative AI tooling
Generated-by: Aone Copilot(Claude-4.7-Opus) and Github Copilot(GPT-5.4)