airborne12 opened a new pull request, #444:
URL: https://github.com/apache/doris-thirdparty/pull/444
### Background
An analyzed field with norms stores BM25 norms in the segment's `.nrm` file:
the field's total token count, then one byte per document, including documents
that do not hold the field (`SDocumentsWriter::BufferedNorms::fill` writes the
default byte for them). Readers take the token count for avgdl and the byte of
each matched document for its length. Doris writes one CLucene index per
VARIANT path, and most paths appear in few rows, so `.nrm` is mostly the
default byte of documents that can never match.
### What problem does this PR solve?
Issue Number: None
Related PR: the apache/doris PR that turns it on for VARIANT paths (linked
below once opened)
PR Type: Performance
Problem Summary:
A field flagged `FlagBits::SPARSE_NORMS` keeps its norms only for the
documents that hold it, in a new `.nrs` file (a roaring bitmap of those
documents plus their norm bytes). A document outside the bitmap decodes to the
byte `.nrm` stores for it, so readers see the same norms array, token count,
and block-max data as with `.nrm`. Nothing changes for fields without the flag;
Doris sets it only behind a config.
### Details
- **Change map:** 349 production lines (a new `.nrs` codec of 147 lines, the
writer 119, the reader 48, `.fnm` persistence 26, index compaction 8, build 1),
284 test lines in one new suite.
- **Why it was slow:** `.nrm` stores one byte for every document of the
segment per field, and the writer buffers the same bytes, so a field in 1% of
1,000,000 documents writes and holds 1,000,012 bytes.
- **Optimization:**
- Writer: a sparse `BufferedNorms` records the documents that hold the
field and their bytes and skips the fill. The block-max data in the postings
reads a dense view rebuilt for one field at flush, so the postings files are
byte-identical.
- Reader: `SegmentReader::openNorms` opens `.nrs` for a sparse field;
`norms(field, bytes)` decodes it into the same dense array, so no consumer of
norms changes.
- Compatibility: the field is persisted with `OMIT_NORMS` set plus the
flag, which kV3 and later write into `.fnm`. A reader with this change clears
`omitNorms` back; a reader without it sees a field without norms instead of
decoding `.nrs` as `.nrm`. `.nrm` cannot carry a new layout: readers ignore its
header and seek by `maxDoc`, so they would misread it silently. A field without
norms never persists the flag.
- `indexCompaction`: sources may mix layouts, so the flag is left out of
the source consistency check and of the destination fields; the merge reads
both as dense and writes `.nrm`.
- **Results:** norms file and whole index size for one field of 1,000,000
documents, the field in every k-th document:
| documents with the field | `.nrm` | `.nrs` |
|---|---|---|
| 0.1% | 1,000,012 | 3,152 |
| 1% | 1,000,012 | 30,153 |
| 10% | 1,000,012 | 226,426 |
| 50% | 1,000,012 | 631,226 |
| 100% | 1,000,012 | 1,000,247 |
In Doris, a load of 100,000 rows into 5,000 analyzed VARIANT paths shrinks
the segment's index from 577 MB to 83 MB and the jemalloc peak during the flush
from about 4.74 GB to 3.92 GB (numbers in the Doris PR).
- **Applicability and cost:** fields with `FlagBits::SPARSE_NORMS` and kV3
or later; other fields are unchanged. A field present in every document costs a
few hundred bytes more (the bitmap). Reading decodes `.nrs` once per field and
segment, into the array `.nrm` fills today. `setNorm` is refused for a sparse
field, whose rewrite would go to a separate dense norms file.
### Testing
```sh
cmake -G Ninja <clucene-src> && ninja cl_test
cd bin && ./cl_test
```
117/117 pass (clang 20.1.8), including the new `SparseNorms` suite:
- `TestSparseNormsDecodeToDenseNorms`: 3000 documents with null documents,
empty values, and 1 to 40 tokens, in one and in three segments; dense and
sparse writes give the same norms through both `norms()` overloads, the same
token count, and byte-identical `.frq/.prx/.tis/.tii`.
- `TestSparseNormsHiddenFromOlderReaders`: the sparse `.fnm` carries
`OMIT_NORMS`.
- `TestCompactionMergesSparseNormsSources`: compaction of a dense and a
sparse source writes `.nrm` and the same norms as two dense sources. It fails
with "flags inconformity" when the flag is not masked out of the consistency
check.
The Doris PR adds BE unit tests and a regression suite on top of this
commit, and a run of a Doris BE built before this change on sparse segments:
MATCH works and score() fails with "written without norms".
### Release note
None
### Check List (For Author)
- Behavior changed: No
- Does this need documentation: No
- Pre-submission status: Ready
### AI Disclosure
- Tool: Claude Code (claude-opus-5-5)
- Generated: the code, the tests, this description
- Human review: TODO(author)
🤖 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]