Skip to content

branch-4.2: [fix](compaction) Seal index files after index compaction in non-vertical compaction - #69936

Merged
yiguolei merged 2 commits into
apache:branch-4.2from
airborne12:fix-non-vertical-index-compaction-branch-4.2
Nov 3, 2026
Merged

yiguolei merged 2 commits into
apache:branch-4.2from
airborne12:fix-non-vertical-index-compaction-branch-4.2

Conversation

@airborne12

@airborne12 airborne12 commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

What problem does this PR solve?

Issue Number: None

Related PR: #65858

Problem Summary:

With enable_vertical_compaction=false and inverted_index_compaction_enable=true, a DUP KEY or MoW table compacted through the non-vertical path loses the inverted indexes of its string columns.

  • Columns in columns_to_do_index_compaction are skipped by the segment writer and merged after all segments are flushed.
  • Non-vertical compaction flushes through SegmentFlusher, which called close_inverted_index() at flush time. With no logical index present, begin_close() sealed an empty container and uploaded a zero byte .idx.
  • Index compaction then wrote into temporary directories of the closed writer, and SegmentFlusher::close() only ran finish_close(), so the merged index was never packed. succeed to do index compaction is still logged.

Result: INDEX_DISK_SIZE=0, and each query logs [E-6004] inverted index file ... is empty and scans the whole segment. Query results stay correct.

With a numeric index on the same table, the file is sealed with the numeric index alone, so INDEX_DISK_SIZE stays above zero while the string index is gone.

master fixed this as part of the row TTL feature #65858. This PR carries only that part, without the feature: when the rowset has index compaction, the flush no longer closes the index files, and BaseBetaRowsetWriter::_close_file_writers() calls the existing _close_inverted_index_file_writers(), the same close the vertical writer uses. All segments are already flushed at that point, because the merger flushes the rowset writer when it finishes. This branch still has SegmentWriter, so both _flush_segment_writer overloads are changed.

A DCHECK in BlockReader::_direct_next_block is relaxed as well. At EOF the block is empty and the iterator still reports the previous block's locations, so ASAN and debug builds abort on this path. Merger only reads the locations when the block has rows, so release builds were not affected.

Existing rowsets are not repaired by this change.

Branch adaptations

  • has_index_compaction() also checks snii_indexes_to_do_compaction, same as master.
  • master seals in SegmentFlusher::close(). This PR reuses the rowset level close instead, so the result is the same but the code differs from master.
  • master covers this path with a BE unit test, not with a regression test. test_index_compaction_non_vertical is new here, and [improvement](compaction) Seal compaction index files through the rowset level close #69976 adds it to master with the same close.

Release note

Fix inverted indexes of string columns being lost by non-vertical compaction when inverted index compaction is enabled.

Check List (For Author)

  • Test
    • Regression test: in non-cloud mode on this branch, new test_index_compaction_non_vertical and test_index_compaction_dup_keys, _unique_keys, _null and _empty_segments pass.
    • Unit Test
    • Manual test: full compaction with vertical off on V2, V3 and SNII, with string indexes only and with numeric plus string indexes. Index size is above zero and no index downgrade happens.
    • No need to test or manual test.

The failure without the fix was checked on branch-4.1, not on this branch. Cloud mode and BE unit tests were not run on this branch. The local build disabled one lance call because the installed lance-c is older than this branch needs; that change is not in this PR.

  • Behavior changed:

    • No.
    • Yes. INDEX_DISK_SIZE of such compaction output is now the real size and the output keeps its inverted indexes.
  • Does this need documentation?

    • No.
    • Yes.

🤖 Generated with Claude Code

…ical compaction

### What problem does this PR solve?

Issue Number: None

Related PR: apache#65858

Problem Summary:

With enable_vertical_compaction=false and inverted_index_compaction_enable=true
(both mutable BE configs), a DUP KEY or MoW table compacted through the
non-vertical path loses the inverted indexes of its string columns.

Reproduce in cloud mode: create a DUP KEY table with an un-tokenized inverted
index on a TEXT column and storage format V2, load 6 batches, set
enable_vertical_compaction=false, run a cumulative compaction. The output
rowset has INDEX_DISK_SIZE=0, the log shows put_object for _0.idx with size=0
before "start index compaction", "succeed to do index compaction" is still
printed, and each query reports "[E-6004] inverted index file ... is empty"
and scans the whole segment.

Root cause: columns in columns_to_do_index_compaction are skipped by the
segment writer and merged by Compaction::do_inverted_index_compaction after
all segments are flushed. Non-vertical compaction flushes through
SegmentFlusher, which called close_inverted_index() at flush time. With no
logical index present, begin_close() sealed an empty container and uploaded a
zero byte object. Index compaction then wrote into temporary directories of
the already closed writer, and SegmentFlusher::close() only ran finish_close(),
so the merged index was never packed. With a numeric index on the same table
the file was sealed with the numeric index alone, so INDEX_DISK_SIZE stayed
above zero while the string index was gone. The vertical writer closes all
index files in _close_file_writers() and is not affected.

Fix, same as the change made on master: when the rowset has index compaction
columns, defer sealing and size accounting to SegmentFlusher::close(), and add
the collected size to the rowset total in _close_file_writers().

Also relax a DCHECK in BlockReader::_direct_next_block. At EOF the block is
empty and the iterator still reports the locations of the previous block, so
ASAN and debug builds abort on this path. Merger only reads the locations when
the block has rows, so release builds were not affected.

Existing rowsets are not repaired by this change. A rowset is rebuilt when it
is compacted together with other rowsets, or by DROP INDEX and ADD INDEX.

### Release note

Fix inverted indexes of string columns being lost by non-vertical compaction
when inverted index compaction is enabled.

### Check List (For Author)

- Test: Regression test, Manual test
    - In non-cloud mode on this branch, new suite
      test_index_compaction_non_vertical and test_index_compaction_dup_keys,
      _unique_keys, _null and _empty_segments pass
    - Manual full compaction with vertical off on V2, V3 and SNII, with string
      indexes only and with numeric plus string indexes: index size above zero
      and no index downgrade
    - The failure without the fix was checked on branch-4.1, not on this branch
    - Cloud mode and BE unit tests not run on this branch
- Behavior changed: Yes. INDEX_DISK_SIZE of such compaction output is now the
  real size and the output keeps its inverted indexes
- Does this need documentation: No

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@airborne12
airborne12 requested a review from yiguolei as a code owner November 2, 2026 00:46
@hello-stephen

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Apache Doris.
Don't know what should be done next? See How to process your PR.

Please clearly describe your PR:

  1. What problem was fixed (it's best to include specific error reporting information). How it was fixed.
  2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be.
  3. What features were added. Why was this function added?
  4. Which code was refactored and why was this part of the code refactored?
  5. Which functions were optimized and what is the difference before and after the optimization?

@airborne12

Copy link
Copy Markdown
Member Author

run buildall

@hello-stephen

Copy link
Copy Markdown
Contributor

BE UT Coverage Report

Increment line coverage 85.71% (12/14) 🎉

Increment coverage report
Complete coverage report

Category Coverage
Function Coverage 64.68% (30243/46760)
Line Coverage 49.54% (320008/645917)
Region Coverage 45.34% (258002/569052)
Branch Coverage 46.99% (121872/259342)

… compaction

### What problem does this PR solve?

Issue Number: None

Related PR: apache#65858

Problem Summary:

The first commit sealed the index files in SegmentFlusher::close() and added
their size in BaseBetaRowsetWriter::_close_file_writers(), which repeats the
sequence already in _close_inverted_index_file_writers(). Call that helper from
_close_file_writers() when the rowset has index compaction, as the vertical
writer does. Sealing, size accounting and waiting now stay in one place and
SegmentFlusher::close() is unchanged.

All segments are flushed before index compaction starts, because the merger
flushes the rowset writer when it finishes. SegmentFlusher::close() still runs
finish_close() afterwards, which does nothing for a closed writer.

### Release note

None

### Check List (For Author)

- Test: Regression test, see the PR description
- Behavior changed: No
- Does this need documentation: No

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@airborne12

Copy link
Copy Markdown
Member Author

run buildall

@hello-stephen

Copy link
Copy Markdown
Contributor

BE Regression && UT Coverage Report

Increment line coverage 84.62% (11/13) 🎉

Increment coverage report
Complete coverage report

Category Coverage
Function Coverage 76.35% (34730/45486)
Line Coverage 61.20% (392452/641267)
Region Coverage 58.06% (331151/570366)
Branch Coverage 58.97% (152950/259351)

@yiguolei
yiguolei merged commit 3ff2aa0 into apache:branch-4.2 Nov 3, 2026
42 of 43 checks passed
yiguolei pushed a commit that referenced this pull request Nov 6, 2026
…set level close (#69976)

### What problem does this PR solve?

Issue Number: None

Related PR: #65858

Problem Summary:

#65858 fixed non-vertical compaction losing string indexes, but the fix
made the close path depend on one feature:

- `SegmentFlusher::close()` asked `has_index_compaction()` before
sealing.
- `BaseBetaRowsetWriter::_close_file_writers()` added a collection total
that is only meaningful for deferred writers, and relied on it being
zero for everything else.
- The sequence of sealing, adding the size and waiting already existed
in `_close_inverted_index_file_writers()`, which the vertical writer
uses.

Closing does not need to know about index compaction. It only has to
seal the index files that are still open.

This PR makes three changes:

1. **Closing no longer checks for index compaction.**
`_close_file_writers()` always calls
`_close_inverted_index_file_writers()` after the segments are closed.
`InvertedIndexFileCollection::begin_close()` skips writers sealed when
their segment was flushed, and `SegmentFlusher::close()` only closes
segments. The one remaining `has_index_compaction()` check is at flush,
where the flusher has to know that postings are still to come.
2. **`IndexFileWriter::open()` rejects a closed writer.** The original
bug stayed silent because `open()` accepted a closed writer and the
directory it returned never reached the sealed file. A mistake of this
kind now fails the compaction instead of committing a rowset without its
index.
3. **End to end test.** `test_index_compaction_non_vertical` covers this
path, including a table with both numeric and string indexes. The
existing BE unit test
`HorizontalCompactionSealsIndexesAfterPostingsAreWritten` is unchanged
and passes.

Index size accounting is unchanged:

- A writer sealed at flush is counted in the segment statistics and
skipped by the collection, so it is not added twice.
- A deferred writer is added by the helper, the same `_total_index_size`
update as before.

`finish_close()` keeps its position after the segment files are closed
and the preload is done, so loads and compactions without index
compaction close in the same order as before.

Backports: #69933 (branch-4.1) and #69936 (branch-4.2) carry the fix
with a check in `_close_file_writers()`. They do not have this cleanup
yet.

### Release note

None

### Check List (For Author)

- Test
- [x] Regression test: in non-cloud mode, all 7 suites in
`inverted_index_p0/index_compaction` pass, including the new
`test_index_compaction_non_vertical`.
- [x] Unit Test: 120 tests pass, covering `IndexFileWriterTest` with the
new `OpenAfterCloseFails` and `CollectionClosesOnlyOpenWriters`,
`SegmentFlusherTransformFormatTest`, segment compaction,
`BetaRowsetTest` and cloud compaction.
- [x] Manual test: full compaction with vertical off on V2, V3 and SNII,
with string indexes only and with numeric plus string indexes. Index
size is above zero, matches the previous commit, and no index downgrade
happens.
    - [ ] No need to test or manual test.

Cloud mode was not run for this change.

- Behavior changed:
    - [ ] No.
- [x] Yes. Opening an index on a closed `IndexFileWriter` now returns an
error. No such caller exists after this change.

- Does this need documentation?
    - [x] No.
    - [ ] Yes.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: martin.foo838 <martin.foo838@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
yiguolei pushed a commit that referenced this pull request Dec 5, 2026
…om SEARCH fields #71123 (#71118)

> **Update (merge of branch-4.2 at `dda37e3f019`):** #71121 brought
#68695, #69901, #69976, #70373 and #70883 to `branch-4.2`. After merging
`branch-4.2` into this branch, the PR changes only what #71123 changes:
`query_info.h` (the `@<index_id>@<field>` statistics name) and two
`CollectionStatisticsTest` cases. The one conflict, in `query_info.h`,
was resolved to the #71123 form. The sections below describe the
original six-commit backport.

### What problem does this PR solve?

Issue Number: None

Related PR: #68695, #69901, #69976, #70883, #70373, #71123

Problem Summary:

Backport five merged inverted index changes to `branch-4.2`, one `git
cherry-pick -x` commit per source PR, in master merge order. A sixth
commit picks #71123, the master fix for the review finding on #69901 in
this PR. #71123 is still open, and this pick follows any change made
there.

| Source | Change | Pick |
|---|---|---|
| #68695 | `SegmentIteratorApplyIndexExprTest`: the mock expression
reports `MATCH_PRED`, so the reachable `is_virtual_slot_ref()` check
reads an initialized node type. New and stronger assertions for a null
index context, continued evaluation after a downgrade, and a missing
index error when fallback is off. | Adapted (part of it is already on
4.2) |
| #69901 | `score()` over MATCH clauses that read different inverted
indexes of one column failed with "Scoring predicates for one field
select different inverted indexes". BM25 statistics of a MATCH are now
kept per field and index id, so each clause scores with the statistics
of its own index. SEARCH is unchanged. | Ported, one test hunk N-A |
| #69976 | Closing a rowset no longer depends on index compaction: the
rowset level close seals every index file still open after the segments
are closed. `IndexFileWriter::open()` rejects a closed writer, so an
index written into a sealed file fails the compaction instead of being
lost. | Adapted onto 4.2's #69936 |
| #70883 | SNII sparse norms format: norms of the non-NULL rows only,
read back to the same bytes. Writers still write dense norms (sparse is
behind a debug point), so nothing changes on disk; a BE with this PR can
read segments written by the follow-up that turns sparse norms on, so
the reader should be in 4.2.0. | Ported (verbatim) |
| #70373 | A wildcard that starts with `*` or `?` names its longest
literal run, so CLucene and SNII skip terms that cannot match before
converting them. `*cache*` over 100M rows: CPU 26.7 to 14.8 s on V3,
19.6 to 11.0 s on SNII (source PR numbers). | Ported (verbatim) |
| #71123 | Follow-up to #69901. A MATCH kept its statistics under
`field@index_id`, which a SEARCH on the VARIANT key `key@2004` could
spell, so a score query with both failed with "Scoring predicates for
one field select different inverted indexes: 2004 and 2004". A MATCH
name now starts with `@` (`@<index_id>@<field>`), which no field name
does. | Ported (verbatim) |

### Backport details

- **#68695:** branch-4.2 already builds this test and gives the fixture
its candidate rows (#68691, the 4.2 counterpart of #68695), and the mock
already counts evaluations (#69292). The rest is ported; 4.2's
`set_on_evaluate` hook is kept.
- **#69901:** `SniiDocsOnlyNormalizerStillCollectsScoringStatistics` is
not on branch-4.2; it arrives with #69199 in the open backport #70352.
Its `L"1"` -> `kMatchField` update is therefore not applied here.
**Whichever of this PR and #70352 merges second must switch that test's
two assertions to `kMatchField`.**
- **#69976:** branch-4.2 has the original fix as #69936, which closed
the index files before the segments only when the rowset had index
compaction, and finished them in `SegmentFlusher::close()`. This pick
replaces both with the master close path: `_close_file_writers()` closes
the segments and then calls `_close_inverted_index_file_writers()`,
whose `begin_close()` skips writers sealed at flush. The resulting code
of the three rowset files matches master after #69976. The call sites
are the same on both branches (one `_segment_creator.close()` caller;
the vertical writer closes its own index files; `BetaRowsetWriterV2`
never calls `SegmentCreator::close()`). The file cache preload in
`SegmentFlusher::close()` reads ranges of the segment file only, so
moving the index close after it is safe.
`test_index_compaction_non_vertical` is already on 4.2 from #69936 and
identical to the source.
- **#70373** is picked after #70883 as on master, so its include change
applies unchanged.

<details>
<summary>Hunk audit (source diff per file; added and removed lines
compared ignoring offsets)</summary>

| Source | File | Source hunks | Result |
|---|---|---|---|
| #68695 | `be/test/CMakeLists.txt` | `@@ -100,7 +100,6 @@` | N-A:
branch-4.2 never excluded this test from the UT build (#68691 says so) |
| #68695 |
`be/test/storage/segment/segment_iterator_apply_index_expr_test.cpp` |
`@@ -45,9 +45,13 @@` | Ported `MATCH_PRED`; `evaluate_count()` already
present from #69292 |
| | | `@@ -62,11 +66,13 @@` | N-A: the evaluation counter is already
present from #69292 |
| | | `@@ -119,6 +125,7 @@` | N-A: `_row_bitmap.addRange` already
present from #68691 |
| | | `@@ -145,8 +152,11 @@`, `@@ -169,6 +179,19 @@`, `@@ -215,10
+238,12 @@` | Ported |
| #69901 | 9 production files,
`snii_index_reader_count_fallback_test.cpp`, both regression files | all
| Ported (verbatim) |
| #69901 |
`be/test/storage/index/inverted/reader/collection_statistics_test.cpp` |
`@@ -1507,8 +1511,8 @@ ...
SniiDocsOnlyNormalizerStillCollectsScoringStati` | N-A: test absent on
4.2 (added by #69199, pending in #70352) |
| | | every other hunk | Ported |
| #69976 | `index_file_writer.cpp`, `index_file_writer.h`,
`inverted_index_file_writer_test.cpp` | all | Ported (verbatim) |
| #69976 | `be/src/storage/rowset/beta_rowset_writer.cpp` | `@@ -279,6
+279,10 @@` | Ported |
| | | `@@ -973,10 +977,7 @@` | Adapted: removes 4.2's #69936 pre-close
under `has_index_compaction()`; the function now equals master |
| #69976 | `be/src/storage/rowset/beta_rowset_writer.h` | `@@ -87,7
+87,7 @@` | Ported |
| | | `@@ -232,9 +232,9 @@` | Adapted: removes 4.2's comment "Only
during compaction is this method called" (master's was "vertical
compaction") |
| #69976 | `be/src/storage/rowset/segment_creator.cpp` | `@@ -122,12
+122,6 @@` | Adapted: 4.2 only had `finish_close()` here; removed, as on
master |
| #69976 | `test_index_compaction_non_vertical.{groovy,out}` | new files
| N-A: already on 4.2 from #69936, byte-identical to the source |
| #70883 | all 22 files | all | Ported (verbatim) |
| #70373 | all 5 files | all | Ported (verbatim) |
| #71123 | `query_info.h`, `collection_statistics_test.cpp` | all |
Ported (verbatim); the test hunk had a context conflict only, since the
tests around `BuildFieldNameWithoutSuffix` differ on 4.2 |

</details>

### Release note

Fix score() over MATCH clauses that use different inverted indexes of
one column, including when a SEARCH clause reads a VARIANT key that
looks like a MATCH statistics name. Wildcard queries that start with `*`
or `?` run faster on inverted indexes. A BE can read SNII segments with
sparse norms.

### Check List (For Author)

- Test
    - [x] Regression test
    - [x] Unit Test
    - [ ] Manual test (add detailed scripts or steps below)
    - [ ] No need to test or manual test.

- Behavior changed:
    - [ ] No.
- [x] Yes. score() over MATCH clauses on different indexes of one column
now succeeds; opening an index on a closed `IndexFileWriter` returns an
error (no caller does after #69976).

- Does this need documentation?
    - [x] No.
    - [ ] Yes.

### Validation

- #71123 pick (head `add9c162f7a`): both new `CollectionStatisticsTest`
cases failed before the fix and pass with it. Every suite of the 32 test
files that include the headers of the statistics name, the readers and
the collectors ran: 669 tests in 50 suites, all passed, no sanitizer
report. The format preflight and the English check passed for this head.

The runs below used ASAN builds of the first five commits
(`d7b2ff7ea5a`) on top of `origin/branch-4.2` `017870e630b`.

- Build: `./build.sh --be --fe -j 96` passed; the incremental
`./build.sh --be -j 96` of this head passed.
- Format: clang-format 16 on every changed C++ file, and the shared
format preflight (clang-format and FE checkstyle against
`origin/branch-4.2`): PASS. English check: PASS with 4 reviewed literal
exceptions, the CJK wildcard inputs of #70373's test.
- BE UT: `./run-be-ut.sh --run -j 96 --filter=<266
suites>:-S3FileWriterTest.*`, where the suites are every suite in the
changed test files, in the test files that include a changed header, and
in every SNII test file. 2596 tests ran: 2590 passed, 6 skipped
themselves, 0 failed, no sanitizer report. All 33 test cases the five
source PRs add or rename ran and passed.
- `S3FileWriterTest` was in scope only because it includes
`index_file_writer.h`. Locally it aborts with a SEGV in
`FileCacheFactory::get_by_path` called from the `S3FileWriter`
constructor, also when run alone; no picked code is on that stack.
- Regression: an isolated FE and BE of this build, with
`enable_debug_points = true` as in CI (the `debug_variant_*` session
variables need it). 22 suites, 0 failed:
- SNII: `test_storage_format_snii`, `test_storage_format_snii_norms`,
`test_storage_format_snii_custom_analyzer`,
`test_storage_format_snii_utf8_wildcard`
- Scoring: `test_match_score_multi_index` (new, #69901),
`test_bm25_score`, `test_bm25_score_variant`,
`test_match_keyword_score`, `test_search_multi_analyzer_lucene`,
`test_score_filter_usage`
- Wildcard and SEARCH: `test_search_variant_wildcard`,
`test_search_format_parity`, `test_search_dsl_syntax`,
`test_search_score_pattern_constant`, `test_search_lucene_mode`
- Index compaction: `test_index_compaction_non_vertical`, `_dup_keys`,
`_unique_keys`, `_null`, `_empty_segments`, `_p0`,
`_with_multi_index_segments`
- Cloud mode was not run.

### Check List (For Reviewer who merge this PR)

- [ ] Confirm the release note
- [ ] Confirm test cases
- [ ] Confirm document
- [ ] Add branch pick label

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: martin.foo838 <martin.foo838@gmail.com>
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