feat(dataobj/postings,stats): Add merge builders and row decoders - #22229
Merged
Conversation
Introduces the row-level primitives the IndexMerge compaction executor
needs to merge index sections, kept entirely within the section
packages so they have no dependency on the engine/executor.
postings:
- MergeBuilder: a row-only sibling of Builder that stores
already-aggregated label/bloom rows directly, bypassing the
per-observation aggregator (which would double-count
UncompressedSize when fed pre-merged rows). It encodes via the same
columnarEncode helper as observation-mode flush, so both builders
produce byte-identical on-disk sections — a querier cannot tell
which one wrote a section.
- Row / DecodeRow / BuildColumnIndex: canonical Arrow RecordBatch
decoders, plus LabelEntry()/BloomEntry() conversions, so the
executor and tests decode rows through one shared path instead of
re-implementing column lookup.
- LabelEntry/BloomEntry now carry int64 timestamps directly (matching
the on-disk encoding), and columnarEncode consumes the public types,
dropping a round-trip and a bloom unmarshal on the merge path with
no change to the observation hot path.
- sortLabelEntries/sortBloomEntries: shared sort helpers so Builder
and MergeBuilder flush agree by construction.
stats:
- Row decoding (ColumnIndex/BuildColumnIndex/DecodeRow) mirroring the
postings decoders, reusing the existing Stat type.
Observation-mode Builder behavior and on-disk bytes are unchanged.
This was referenced Jun 3, 2026
trevorwhitney
marked this pull request as ready for review
June 3, 2026 17:10
rfratto
previously approved these changes
Jun 3, 2026
Comment on lines
+12
to
+17
| // MergeBuilder accumulates posting entries from pre-aggregated rows (e.g., from | ||
| // existing postings sections) and merges them. Unlike Builder, which aggregates | ||
| // per-observation, MergeBuilder accepts already-aggregated entries. | ||
| // Call [MergeBuilder.Flush] to encode all accumulated data and write a postings | ||
| // section to the provided [dataobj.SectionWriter]. | ||
| type MergeBuilder struct { |
Member
There was a problem hiding this comment.
The name here is a little confusing, but I really don't have any better suggestions for what it should be instead
Collaborator
Author
There was a problem hiding this comment.
agreed, but I don't know what to call it, let'd follow up on this later if it's still a concern
rfratto
reviewed
Jun 3, 2026
| return fmt.Errorf("label posting entry with key (%q, %d, %q, %q) already exists", entry.ObjectPath, entry.SectionIndex, entry.ColumnName, entry.LabelValue) | ||
| } | ||
|
|
||
| b.labels[key] = &entry |
Member
There was a problem hiding this comment.
not sure why this is taking a reference. Feels like an unnecessary allocation?
Collaborator
Author
There was a problem hiding this comment.
I did it to avoid the map copy, but you're right this is worse because of allocation / heap escape. fixed.
Storing *LabelEntry/*BloomEntry forced the by-value parameter to escape to the heap on every Append call, adding a per-entry allocation in the merge hot path. The entries are never mutated after insertion and Flush copies them back into value slices, so the pointer indirection bought nothing. Store values directly to drop the allocation.
rfratto
approved these changes
Jun 3, 2026
This was referenced Jun 8, 2026
zwmyxzs
pushed a commit
to zwmyxzs/loki
that referenced
this pull request
Jun 12, 2026
This was referenced Jun 15, 2026
This was referenced Jul 13, 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.
What this does
Part 1 of 6 splitting up #21972 (IndexMerge executor) into reviewable PRs.
Introduces the row-level primitives the IndexMerge compaction executor needs to merge index sections, kept entirely within the
postingsandstatssection packages so they have no dependency on the engine/executor.postings
MergeBuilder: a row-only sibling ofBuilderthat stores already-aggregated label/bloom rows directly, bypassing the per-observation aggregator. Both builders produce identical sections.Row/DecodeRow/BuildColumnIndex: canonical ArrowRecordBatchdecoders, plusLabelEntry()/BloomEntry()conversions, so the executor and tests decode rows through one shared path.LabelEntry/BloomEntrynow carryint64timestamps directly (matching the on-disk encoding);columnarEncodeconsumes the public types, dropping a round-trip and a bloom unmarshal on the merge path with no change to the observation hot path.sortLabelEntries/sortBloomEntries: shared sort helpers soBuilderandMergeBuilderflush agree by construction.Stacked PR series (split of #21972)