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

   ### Background
   
   SNII writes BM25 norms, one byte per row, for every analyzed index with 
positions
   (`support_phrase`): `SniiIndexColumnWriter::add_nulls` appends 
`encode_norm(0)` for each
   NULL row, and `NormsPodWriter` stores `[doc_count][one byte per row]`. A 
VARIANT column has
   one logical index per extracted path, so a segment stores these bytes once 
per path, for
   every row of the segment, even where the path is NULL. BM25 reads the norm 
of a row only
   when a posting reaches it, and a NULL row has no postings.
   
   ### What problem does this PR solve?
   
   Issue Number: None
   Related PR: #68039 (lets users drop norms on VARIANT paths, at the cost of 
score() on them)
   PR Type: Performance
   
   Problem Summary:
   
   On a VARIANT column with many sparse paths, the bytes of the NULL rows make 
up most of the
   norms: #68039 reports norms as 97.1% of an index file on V2/V3, whose norms 
have the same
   one-byte-per-row layout as SNII's. This PR adds a sparse norms section to 
SNII that stores the norms of the non-NULL
   rows only and reads back exactly the bytes a dense section holds, so BM25 
scores do not
   change. Writers keep writing dense norms; a debug point writes the sparse 
form so the tests
   cover write and read end to end, and a follow-up PR makes it the default for 
indexes with
   NULL rows. After an upgrade, existing segments and users see no change.
   
   ### Details
   
   - **Change map:** 1128 changed lines: 507 of production code, 617 of tests, 
4 of proto.
     - Format (about 265): `SniiSectionRefsPB.sparse_norms = 6`, section type
       `kNormsSparse = 15`, the sparse codec in `norms_pod`, and
       `NullBitmapReader::for_each_non_null_run`.
     - Reader (about 165): `LogicalIndexReader` checks the new region's length 
at open, expands
       it with the null bitmap on first use, and charges the searcher cache by 
the expanded size;
       the segment rewrite snapshot and `index_disk_usage` count the region.
     - Writer (about 75): `LogicalIndexWriter::build_norms_section` (moved out 
of
       `finalize_build`) writes the sparse section behind the debug point
       `LogicalIndexWriter::finalize_build.sparse_norms`, and the compound 
writer records it in
       field 6.
   - **Why it was slow:** the cost follows from the code: each SNII index that 
writes norms
     stores `doc_count` bytes, one per row, NULL rows included, and a segment 
holds one such
     index per indexed VARIANT path. A path present in 1% of the rows still 
stores a byte for
     every row.
   - **Optimization:** the sparse payload is
     `[u8 mode][varint64 doc_count][varint64 value_count][stored bytes]`: one 
byte per non-NULL
     row in docid order (`kValues`), or one byte shared by all of them 
(`kConstant`). The null
     bitmap the index already stores tells which rows are NULL, so the section 
needs no
     presence set of its own. The reader rebuilds one byte per row and gives a 
NULL row
     `kNullRowNorm`, which equals `encode_norm(0)`, the byte the dense form 
stores for it. The
     BM25 inputs (doc count, total term frequency, the norm of every row) are 
therefore
     unchanged.
   - **Results:** no size change in this PR, since writers keep dense norms. By 
the format, an
     index of 40 rows with 23 NULL rows (the unit test corpus) stores a 26-byte 
sparse section
     instead of a 47-byte dense one, and an index whose rows all share one norm 
stores one byte
     for them. Sizes on real VARIANT data come with the follow-up that turns 
the writer on.
   - **Applicability and cost:** sparse norms apply only to SNII indexes that 
write norms and
     have NULL rows; an index without NULL rows keeps the dense section byte 
for byte. The
     reader expands the section once per index, on the first scoring query, 
with a walk over the
     null bitmap and one copy or fill per run of non-NULL rows, and then holds 
`doc_count` bytes
     as it does for dense norms. Field 6 is optional and new; a BE that 
predates it ignores it
     and sees an index without norms, so `MATCH` still works and `score()` 
returns
     `INVERTED_INDEX_NOT_SUPPORTED` instead of reading the bytes wrongly.
   
   ### Testing
   
   All on a local ASAN BE UT build of this commit (rebased on ac5b289d296), with
   `GLIBC_COMPATIBILITY=OFF` (see Notes for Reviewers):
   
   - Before the implementation, with the new APIs stubbed, the new cases failed 
on their
     assertions: 4 `SniiCoreMetadata`, 8 `SniiNormsPod`, and the 
`SniiNullBitmap`,
     `SniiSparseNormsTest`, `SniiRewriteSnapshot` and 
`IndexDiskUsageCollectorTest` cases.
   - `SniiNormsPod.*`: sparse round trips (values, one shared norm, every row 
NULL, no NULL
     row, 5000 rows with 90% NULL rows) and rejection of malformed sections 
(unknown mode,
     counts that disagree with the null bitmap or the doc count, wrong lengths, 
the dense frame
     type, a flipped byte). `NullRowNormIsTheNormOfAnEmptyDocument` ties 
`kNullRowNorm` to
     `query::encode_norm(0)`.
   - `SniiCoreMetadata.*`: field 6 round trip, dense and sparse together 
rejected, sparse norms
     without positions rejected, missing offset or length rejected.
   - `SniiSparseNormsTest.*`: the same corpus written dense and sparse gives 
the same norm for
     every posting a scoring cursor returns, for every row, and the same 
scoring stats; the
     cache charge follows the expanded size; an index without NULL rows stays 
dense.
   - `SniiIndexReaderAnalyzedQueryTest.SparseNormsScoreLikeDenseNorms`: through 
`MATCH` and
     `CollectionSimilarity`, every matched row scores bit for bit the same with 
dense and sparse
     norms, and the sparse file is smaller.
   - `SniiRewriteSnapshot.RejectsSparseNormsReferenceOutsideThePhysicalArea` and
     `IndexDiskUsageCollectorTest.CollectSniiAttributesSparseNormsToTheIndex`.
   - Related suites: filter 
`*Snii*:*snii*:*CollectionStatistic*:IndexDiskUsage*:*InvertedIndexWriter*:*NormsPod*:*NullBitmap*:*CoreMetadata*`,
     1745 tests from 217 suites passed.
   - `build-support/check-format.sh` and `build-support/check-build-hygiene.sh` 
passed.
     `build-support/run-clang-tidy.sh` reports no finding on the changed lines; 
the local
     clang-tidy could not fully analyze the files (see Notes for Reviewers).
   - Not run: regression tests (nothing user-visible changes here; the 
follow-up adds one) and a
     test with a BE that predates field 6.
   
   ### Notes for Reviewers
   
   - Field 6 instead of field 5 or a container feature bit: a BE that predates 
this PR checks
     field 5 against the dense length when it opens a logical index, so sparse 
bytes there would
     stop the whole index from opening, `MATCH` included; a `required_features` 
bit would make it
     refuse the whole container.
   - There is no BE config for the format: the form follows from the data, both 
forms read back
     the same bytes, and no released reader knows field 5 but not field 6. A 
pick of SNII norms
     to another branch should take this reader with it.
   - `LogicalIndexReader::open_null_bitmap` is the null-bitmap part of 
`read_null_docids`, moved
     out unchanged so that the sparse load reuses it; `read_null_docids` keeps 
its behavior.
   - Local environment: with `GLIBC_COMPATIBILITY=ON`, the unmodified base 
fails to link
     `doris_be_test` with the local ldb toolchain, whose 
`libglibc-compatibility.a` also defines
     `preadv`, `posix_spawnp` and others that `lance_symbol_versions.c` 
defines. The option
     changes link libraries only. The local clang-tidy rejects 
`-Wno-c2y-extensions` from the
     clang 20 compile database and misses `sanitizer/asan_interface.h`, so it 
reported every
     changed file as not fully analyzed; filtering its full output by the 
changed lines finds no
     finding.
   
   ### Release note
   
   None
   
   ### Check List (For Author)
   
   - Behavior changed: No
   - Does this need documentation: No
   - Pre-submission status: Partially verified: clang-tidy could not fully 
analyze the changed
     files with the local toolchain, and BE UT ran with 
`GLIBC_COMPATIBILITY=OFF`
   
   ### AI Disclosure
   
   - Tool: Claude Code (Claude Opus 5.5)
   - Generated: code, tests, this description
   - Human review: TODO(author)
   


-- 
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