airborne12 opened a new pull request, #71166:
URL: https://github.com/apache/doris/pull/71166

   ### Background
   
   An analyzed inverted index of storage format V2 or V3 writes BM25 norms in 
CLucene's `.nrm` file: one byte per row of the segment, including rows that 
have no value for the field, plus the field's total token count. score() reads 
the norm byte of each matched row and the token count for avgdl. A segment 
holds one index per VARIANT path, and most paths appear in few rows, so dense 
norms cost rows x paths bytes, almost all of them the default byte of rows that 
can never match. #70883 and #70968 removed these bytes for SNII; V2 and V3 
still write them.
   
   ### What problem does this PR solve?
   
   Issue Number: None
   Related PR: apache/doris-thirdparty#444 (the CLucene `.nrs` format; this PR 
moves contrib/clucene to it), #70883 and #70968 (sparse norms for SNII), #68039 
(inverted_index_skip_norms_for_variant), #71103 (releases materialized VARIANT 
V2 writers one path at a time; complementary)
   PR Type: Performance
   
   Problem Summary:
   
   With thousands of VARIANT paths, dense norms dominate a segment's inverted 
index and the memory that builds it: one segment of 100,000 rows over 5,000 
sparse paths writes 500 MB of norms in a 577 MB index. This PR adds BE config 
`inverted_index_sparse_norms_for_variant` (default false). When it is on, a 
variant path index of storage format V3 keeps norms only for the rows that hold 
the path, in CLucene's new `.nrs` file, which decodes to the same bytes, so 
scores do not change. With the config off, nothing changes.
   
   ### Details
   
   - **Change map:** 36 lines of production code in 6 files (the config, the 
policy in `inverted_index_parser`, one flag in `create_field`, `.nrs` in index 
disk usage), the contrib/clucene pointer (apache/doris-thirdparty#444: 349 
production lines, 284 test lines), 275 lines of BE unit tests, and a 238-line 
regression suite with its 91-line generated `.out`.
   - **Why it was slow:** `SDocumentsWriter::BufferedNorms::fill` stores the 
default byte for every row without the field, so a variant path in 1% of the 
rows still writes and buffers one byte per row, and the RAM directory keeps the 
finished `.nrm` until the segment closes.
   - **Optimization:** `create_field` sets `FlagBits::SPARSE_NORMS` on a V3 
variant path field that writes norms. CLucene then keeps norms only for the 
rows that hold the field (a roaring bitmap plus their bytes) in `.nrs`; a row 
outside the bitmap decodes to the byte `.nrm` stores for it, so norms, token 
count and block-max data are unchanged. `.fnm` marks the field OMIT_NORMS plus 
the flag, so a BE without this change sees no norms instead of misreading 
`.nrs`.
   - **Results:** RELEASE build, one BE: a stream load of 100,000 rows into a 
VARIANT with 5,000 analyzed paths, 20 random paths per row (each path in about 
0.4% of the rows), one segment per load, BE restarted before each load, three 
loads per arm. Peak is jemalloc allocated bytes sampled every 0.25 s from 
`/metrics` during the flush.
   
     | | dense norms | sparse norms |
     |---|---|---|
     | segment index file | 577,131,394 B | 83,271,394 B |
     | of which norms | 500,060,000 B | 6,200,000 B |
     | peak during the flush, this branch | 4,676 / 4,759 / 4,780 MB | 3,928 / 
3,924 / 3,921 MB |
     | flush time, this branch | 33.4 / 33.6 / 33.9 s | 29.4 / 31.7 / 31.5 s |
     | peak during the flush, with #71103 merged in | 1,643 / 1,661 / 1,656 MB 
| 1,170 / 1,173 / 1,168 MB |
     | flush time, with #71103 merged in | 30.0 / 30.1 / 30.1 s | 29.7 / 29.8 / 
29.4 s |
   
     #71103 frees each path's writer after its data and index are written, 
which removes most of the peak, but every finished index stays in the RAM 
directory until the segment closes. Sparse norms shrink those files and the 
norm buffers: about 0.8 GB less on this branch, and 0.48 GB (29%) less on top 
of #71103.
   - **Applicability and cost:** variant path indexes (an index suffix or a 
field_pattern) of storage format V3 that write norms, behind 
`inverted_index_sparse_norms_for_variant`, default false. V2 fields do not 
persist the flags; ordinary columns and SNII are unchanged. A path present in 
every row costs 235 bytes more per 1,000,000 rows (the bitmap). Reading decodes 
`.nrs` into the dense array once per segment and path, as `.nrm` is read today. 
A BE without this change reading a sparse segment, for example during a rolling 
upgrade, after a clone, or after a rollback, sees no norms: MATCH works and 
score() fails with "written without norms" until compaction rewrites the 
segment. Turn the config on only after every BE runs a version with this change.
   
   ### Testing
   
   - BE UT `InvertedIndexWriterTest.SparseNormsDecodeToDenseNorms`: 3000 rows 
with NULL rows, empty strings, and 1 to 40 tokens, written as dense and as 
sparse V3 variant path indexes, in one and in three CLucene segments. Norm 
bytes through both `norms()` overloads, token counts, and `.frq/.prx/.tis/.tii` 
are identical, and the sparse `.fnm` carries OMIT_NORMS. A mutant that decodes 
absent rows to another default byte fails it.
   - BE UT `SparseNormsOnlyForV3VariantPaths` and `IndexDiskUsageClassifyTest`: 
ordinary columns and V2 paths stay dense; `.nrs` counts as norms.
   - Related BE UT groups (`*InvertedIndex*`, `*IndexCompaction*`, 
`*CollectionStatistic*`, `*BM25*`, `*Norm*`, `*CLucene*`, 
`*VariantColumnWriter*`) on this branch: 826 tests, 824 passed, 2 skipped by 
the tests themselves.
   - Regression `inverted_index_p0/test_variant_subcolumn_sparse_norms`: the 
same rows loaded with the config off, on, and switched on between two loads, 
and a merge-on-write table with deleted rows, give the same score() results; 
`.nrs` is written for every variant path index and `.nrm` for the ordinary 
column. The existing `test_variant_subcolumn_index_norms` passes.
   - Rollback: a BE built before this change on the same storage returns the 
same MATCH results on sparse segments and refuses score() with "written without 
norms"; after a full compaction on that BE, score() returns the values in the 
`.out` again.
   - CLucene `cl_test`: 117/117, including the new `SparseNorms` suite 
(apache/doris-thirdparty#444).
   - Unit tests ran on the ASAN UT build; the regression, loads and rollback on 
local RELEASE builds (the #71103 rows with that PR merged in locally).
   
   ### Release note
   
   Add BE config `inverted_index_sparse_norms_for_variant` (default false). 
When it is on, BM25 norms of VARIANT subcolumn inverted indexes (storage format 
V3) are stored only for the rows that hold the path, which shrinks the index 
and the memory used to build it when there are many sparse paths. Scores are 
unchanged. Turn it on only after all BEs are upgraded: an older BE cannot use 
score() on segments written this way.
   
   ### Check List (For Author)
   
   - Behavior changed: No
   - Does this need documentation: Yes, the new BE config, after review
   - Pre-submission status: Partially verified: cloud mode not run; 
contrib/clucene points to the head of apache/doris-thirdparty#444 and must move 
to its merged commit.
   
   ### AI Disclosure
   
   - Tool: Claude Code (claude-opus-5-5)
   - Generated: the BE and CLucene code, the unit tests, the regression suite, 
this description
   - Human review: TODO(author)
   - Prompts: the author asked how V2/V3 inverted index writes behave for 
VARIANT with many subcolumns, noted that memory cannot hold tens of thousands 
of subcolumns, asked for a way to keep subcolumn scoring exact without dense 
norms, then for a design weighing cost and compatibility, and then for this 
implementation.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to