This is an automated email from the ASF dual-hosted git repository.
airborne12 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new 891ae34f49a [opt](inverted index) Add a sparse SNII norms format that
skips NULL rows (#70883)
891ae34f49a is described below
commit 891ae34f49a22b67c6c16321bddb23704fa39b60
Author: Jack <[email protected]>
AuthorDate: Tue Nov 24 21:36:36 2026 +0800
[opt](inverted index) Add a sparse SNII norms format that skips NULL rows
(#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, or one norm that all of them share,
and reads back
exactly the bytes a dense section holds, so BM25 scores do not change.
Writers keep writing
dense norms; a debug point lets the writer keep the smaller of the two
forms so the tests
cover write and read end to end, and a follow-up PR makes that choice
the default. After an
upgrade, existing segments and users see no change.
### Details
- **Change map:** 1200 changed lines: 527 of production code, 669 of
tests, 4 of proto.
- Format (about 275): `SniiSectionRefsPB.sparse_norms = 6`, section type
`kNormsSparse = 15`, the sparse codec and `dense_section_size` in
`norms_pod`, and
`NullBitmapReader::for_each_non_null_run`.
- Reader (about 175): `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 80): `LogicalIndexWriter::build_norms_section` (moved
out of
`finalize_build`) computes the size of both forms behind the debug point
`LogicalIndexWriter::finalize_build.sparse_norms` and writes the smaller
one, and the
compound writer records a sparse section 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. The writer keeps the sparse form only when its section is
strictly smaller:
the sparse header costs a mode byte and a varint value count, so with
very few NULL rows
the dense form wins, and a tie keeps the dense form, which every reader
of norms knows.
- **Results:** no size change in this PR, since writers keep dense
norms. By the format, for
40 rows: with 23 NULL rows (the unit test corpus) the sparse section is
26 bytes instead
of 47; with 2 NULL rows and differing norms both forms are 47 bytes and
the dense one is
kept; with 3 the sparse one is 46; 40 rows that share one norm, without
NULL rows, store
10 bytes. 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
whose sparse section is smaller: enough NULL rows, or one norm shared by
every non-NULL
row; any other index keeps the dense section byte for byte. Choosing
costs one scan over
the norms of the index to find a shared norm. 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 the PR head (based 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.
Before the size rule, `WriterKeepsTheSmallerForm`,
`SharedNormIsStoredOnceWithoutNullRows`
and `DenseSectionSizeIsTheWrittenSize` failed the same way.
- `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; the writer keeps the smaller
form (a tie stays
dense, one more NULL row turns sparse); rows that share one norm store
it once without
NULL rows; differing norms without NULL rows stay 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*`,
1748 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.
---
be/src/storage/index/index_disk_usage.cpp | 3 +-
.../index/inverted/snii/format/core_metadata.cpp | 38 +++-
.../index/inverted/snii/format/core_metadata.h | 2 +
.../index/inverted/snii/format/format_constants.h | 2 +
.../index/inverted/snii/format/norms_pod.cpp | 173 ++++++++++++++++-
.../storage/index/inverted/snii/format/norms_pod.h | 37 ++++
.../index/inverted/snii/format/null_bitmap.cpp | 17 ++
.../index/inverted/snii/format/null_bitmap.h | 5 +
.../inverted/snii/reader/logical_index_reader.cpp | 140 ++++++++++----
.../inverted/snii/reader/logical_index_reader.h | 28 ++-
.../inverted/snii/reader/snii_segment_reader.cpp | 2 +-
.../inverted/snii/writer/logical_index_writer.cpp | 67 +++++--
.../inverted/snii/writer/logical_index_writer.h | 6 +
.../inverted/snii/writer/snii_compound_writer.cpp | 7 +-
be/test/storage/index/index_disk_usage_test.cpp | 44 +++++
.../inverted/snii/format/core_metadata_test.cpp | 77 +++++++-
.../index/inverted/snii/format/norms_pod_test.cpp | 203 ++++++++++++++++++++
.../inverted/snii/format/null_bitmap_test.cpp | 32 ++++
.../snii/reader/snii_index_source_test.cpp | 208 +++++++++++++++++++++
.../snii/reader/snii_rewrite_snapshot_test.cpp | 26 ++-
.../snii/snii_index_reader_analyzed_query_test.cpp | 79 ++++++++
gensrc/proto/snii.proto | 4 +
22 files changed, 1125 insertions(+), 75 deletions(-)
diff --git a/be/src/storage/index/index_disk_usage.cpp
b/be/src/storage/index/index_disk_usage.cpp
index 2f83c2c78f7..ecefa436040 100644
--- a/be/src/storage/index/index_disk_usage.cpp
+++ b/be/src/storage/index/index_disk_usage.cpp
@@ -476,7 +476,8 @@ Status IndexDiskUsageCollector::_collect_snii(const
IndexDiskUsageOptions& optio
cast_set<int64_t>(refs.dict_region.length +
entry.sampled_term_index.length +
entry.dict_block_directory.length +
refs.bsbf.length);
record.posting_bytes =
cast_set<int64_t>(refs.posting_region.length);
- record.stats_bytes = cast_set<int64_t>(entry.core_metadata.length
+ refs.norms.length);
+ record.stats_bytes = cast_set<int64_t>(entry.core_metadata.length
+ refs.norms.length +
+ refs.sparse_norms.length);
record.other_bytes = cast_set<int64_t>(refs.null_bitmap.length);
if (!snii::format::has_positions(core.index_config)) {
record.position_bytes = 0;
diff --git a/be/src/storage/index/inverted/snii/format/core_metadata.cpp
b/be/src/storage/index/inverted/snii/format/core_metadata.cpp
index dee4c5a8f65..5ea83bd1b21 100644
--- a/be/src/storage/index/inverted/snii/format/core_metadata.cpp
+++ b/be/src/storage/index/inverted/snii/format/core_metadata.cpp
@@ -116,6 +116,29 @@ Status decode_region_ref(const
doris::snii::SniiRegionRefPB& input, RegionRef* o
return Status::OK();
}
+// An index has dense norms, sparse norms or none. Norms encode BM25 document
lengths in one byte
+// and require positions for term frequencies.
+Status decode_norms_refs(const doris::snii::SniiSectionRefsPB& refs,
IndexConfig index_config,
+ SectionRefs* out) {
+ out->norms = {};
+ out->sparse_norms = {};
+ if (refs.has_norms()) {
+ RETURN_IF_ERROR(decode_region_ref(refs.norms(), &out->norms));
+ }
+ if (refs.has_sparse_norms()) {
+ RETURN_IF_ERROR(decode_region_ref(refs.sparse_norms(),
&out->sparse_norms));
+ }
+ const bool dense_norms = out->norms.length != 0;
+ const bool sparse_norms = out->sparse_norms.length != 0;
+ if (dense_norms && sparse_norms) {
+ return corrupted("core metadata: an index has dense or sparse norms,
not both");
+ }
+ if ((dense_norms || sparse_norms) && !has_positions(index_config)) {
+ return corrupted("core metadata: norms require positions");
+ }
+ return Status::OK();
+}
+
Status decode_core_pb(const doris::snii::SniiCoreMetadataPB& input,
CoreMetadata* out) {
if (!input.has_index_config() || !input.has_stats() ||
!input.has_section_refs()) {
return corrupted("core metadata: missing required field");
@@ -142,11 +165,7 @@ Status decode_core_pb(const
doris::snii::SniiCoreMetadataPB& input, CoreMetadata
}
RETURN_IF_ERROR(decode_region_ref(refs.dict_region(),
&out->section_refs.dict_region));
RETURN_IF_ERROR(decode_region_ref(refs.posting_region(),
&out->section_refs.posting_region));
- if (refs.has_norms()) {
- RETURN_IF_ERROR(decode_region_ref(refs.norms(),
&out->section_refs.norms));
- } else {
- out->section_refs.norms = {};
- }
+ RETURN_IF_ERROR(decode_norms_refs(refs, out->index_config,
&out->section_refs));
RETURN_IF_ERROR(decode_region_ref(refs.null_bitmap(),
&out->section_refs.null_bitmap));
RETURN_IF_ERROR(decode_region_ref(refs.bsbf(), &out->section_refs.bsbf));
@@ -185,11 +204,6 @@ Status decode_core_pb(const
doris::snii::SniiCoreMetadataPB& input, CoreMetadata
"core metadata: high-df digest hashes are not ascending");
}
}
-
- // Norms encode BM25 document lengths in one byte and require positions
for term frequencies.
- if (out->section_refs.norms.length != 0 &&
!has_positions(out->index_config)) {
- return corrupted("core metadata: norms require positions");
- }
return Status::OK();
}
@@ -217,6 +231,10 @@ Status encode_core_metadata(const CoreMetadata& metadata,
ByteSink* out) {
}
encode_region_ref(metadata.section_refs.null_bitmap,
refs->mutable_null_bitmap());
encode_region_ref(metadata.section_refs.bsbf, refs->mutable_bsbf());
+ // Field 6 is written only for sparse norms, so every other index keeps
its bytes.
+ if (metadata.section_refs.sparse_norms.length != 0) {
+ encode_region_ref(metadata.section_refs.sparse_norms,
refs->mutable_sparse_norms());
+ }
if (metadata.gram_scheme.has_value()) {
core_metadata_detail::encode_gram_scheme(*metadata.gram_scheme,
core.mutable_gram_scheme());
}
diff --git a/be/src/storage/index/inverted/snii/format/core_metadata.h
b/be/src/storage/index/inverted/snii/format/core_metadata.h
index 90e1766f515..96ade844ac8 100644
--- a/be/src/storage/index/inverted/snii/format/core_metadata.h
+++ b/be/src/storage/index/inverted/snii/format/core_metadata.h
@@ -42,6 +42,8 @@ struct SectionRefs {
RegionRef norms;
RegionRef null_bitmap;
RegionRef bsbf;
+ // Set instead of `norms` when the index stores norms for its non-NULL
rows only.
+ RegionRef sparse_norms;
};
// Digest of the highest-df terms of one index: (hash, df) pairs small enough
to travel with
diff --git a/be/src/storage/index/inverted/snii/format/format_constants.h
b/be/src/storage/index/inverted/snii/format/format_constants.h
index 2d880d44a8d..3292a9d7c53 100644
--- a/be/src/storage/index/inverted/snii/format/format_constants.h
+++ b/be/src/storage/index/inverted/snii/format/format_constants.h
@@ -59,6 +59,8 @@ enum class SectionType : uint8_t {
// Core metadata so a corrupt section reference cannot reinterpret valid
// collection statistics as document norms.
kNormsPod = 14,
+ // BM25 norms of the non-NULL rows only, in docid order; the null bitmap
tells which rows.
+ kNormsSparse = 15,
};
// ---- Logical index postings storage content configuration (fixed per logical
diff --git a/be/src/storage/index/inverted/snii/format/norms_pod.cpp
b/be/src/storage/index/inverted/snii/format/norms_pod.cpp
index e9de60945bd..6e31a1b7a8e 100644
--- a/be/src/storage/index/inverted/snii/format/norms_pod.cpp
+++ b/be/src/storage/index/inverted/snii/format/norms_pod.cpp
@@ -17,16 +17,55 @@
#include "storage/index/inverted/snii/format/norms_pod.h"
+#include <algorithm>
+#include <cstring>
#include <limits>
+#include <string_view>
+#include "common/check.h"
#include "storage/index/inverted/snii/common/slice.h"
#include "storage/index/inverted/snii/encoding/byte_source.h"
#include "storage/index/inverted/snii/encoding/section_framer.h"
#include "storage/index/inverted/snii/encoding/varint.h"
#include "storage/index/inverted/snii/format/format_constants.h"
+#include "storage/index/inverted/snii/format/null_bitmap.h"
namespace doris::snii::format {
+// Storage is a unity build, so the helpers of this file live in their own
namespace instead of
+// an anonymous one that other files merged into the same TU may also use.
+namespace norms_pod_detail {
+namespace {
+
+Status sparse_corrupted(std::string_view message) {
+ return Status::Error<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED,
false>(message);
+}
+
+// [u8 mode][varint64 doc_count][varint64 value_count]
+size_t sparse_header_size(uint64_t doc_count, uint64_t value_count) {
+ return 1 + varint_len(doc_count) + varint_len(value_count);
+}
+
+// Calls fn(begin, end) for each range of rows between the ascending
`null_docids`.
+template <typename Fn>
+void for_each_gap(size_t doc_count, std::span<const uint32_t> null_docids,
Fn&& fn) {
+ size_t begin = 0;
+ for (const uint32_t null_docid : null_docids) {
+ DCHECK_GE(null_docid, begin);
+ DCHECK_LT(null_docid, doc_count);
+ if (begin < null_docid) {
+ fn(begin, static_cast<size_t>(null_docid));
+ }
+ begin = static_cast<size_t>(null_docid) + 1;
+ }
+ if (begin < doc_count) {
+ fn(begin, doc_count);
+ }
+}
+
+} // namespace
+} // namespace norms_pod_detail
+
void NormsPodWriter::finish(ByteSink* sink) const {
finish(norms_, sink);
}
@@ -39,7 +78,7 @@ void NormsPodWriter::finish(std::span<const uint8_t> norms,
ByteSink* sink) {
payload.put_varint64(norms.size());
payload.put_bytes(Slice(norms.data(), norms.size()));
// Delegate outer framing to SectionFramer to append type+len+crc32c,
avoiding manual checksum assembly.
- sink->reserve(1 + varint_len(payload_size) + payload_size +
sizeof(uint32_t));
+ sink->reserve(dense_section_size(norms.size()));
SectionFramer::write(*sink, static_cast<uint8_t>(SectionType::kNormsPod),
payload.view());
}
@@ -82,4 +121,136 @@ Status NormsPodReader::open(Slice framed, NormsPodReader*
out) {
return Status::OK();
}
+size_t NormsPodWriter::dense_section_size(uint64_t doc_count) {
+ const size_t payload_size = varint_len(doc_count) +
static_cast<size_t>(doc_count);
+ return 1 + varint_len(payload_size) + payload_size + sizeof(uint32_t);
+}
+
+bool NormsPodWriter::sparse_values_constant(std::span<const uint8_t> norms,
+ std::span<const uint32_t>
null_docids) {
+ const uint8_t* shared = nullptr;
+ bool constant = true;
+ norms_pod_detail::for_each_gap(norms.size(), null_docids, [&](size_t
begin, size_t end) {
+ if (shared == nullptr) {
+ shared = &norms[begin];
+ }
+ constant = constant && std::all_of(norms.begin() + begin,
norms.begin() + end,
+ [&](uint8_t norm) { return norm ==
*shared; });
+ });
+ return shared != nullptr && constant;
+}
+
+size_t NormsPodWriter::sparse_section_size(uint64_t doc_count, uint64_t
value_count,
+ bool constant) {
+ const uint64_t stored = constant ? 1 : value_count;
+ const size_t payload_size =
norms_pod_detail::sparse_header_size(doc_count, value_count) +
+ static_cast<size_t>(stored);
+ return 1 + varint_len(payload_size) + payload_size + sizeof(uint32_t);
+}
+
+void NormsPodWriter::finish_sparse(std::span<const uint8_t> norms,
+ std::span<const uint32_t> null_docids, bool
constant,
+ ByteSink* sink) {
+ DCHECK_EQ(constant, sparse_values_constant(norms, null_docids));
+ DORIS_CHECK_LE(null_docids.size(), norms.size());
+ const uint64_t value_count = norms.size() - null_docids.size();
+ const size_t section_size = sparse_section_size(norms.size(), value_count,
constant);
+ ByteSink payload;
+ payload.reserve(section_size);
+ payload.put_u8(
+ static_cast<uint8_t>(constant ? SparseNormsMode::kConstant :
SparseNormsMode::kValues));
+ payload.put_varint64(norms.size());
+ payload.put_varint64(value_count);
+ bool shared_written = false;
+ norms_pod_detail::for_each_gap(norms.size(), null_docids, [&](size_t
begin, size_t end) {
+ if (!constant) {
+ payload.put_bytes(Slice(norms.data() + begin, end - begin));
+ } else if (!shared_written) {
+ payload.put_u8(norms[begin]);
+ shared_written = true;
+ }
+ });
+ sink->reserve(section_size);
+ SectionFramer::write(*sink,
static_cast<uint8_t>(SectionType::kNormsSparse), payload.view());
+}
+
+Status NormsPodReader::open_sparse(Slice framed, uint32_t doc_count, const
NullBitmapReader* nulls,
+ std::vector<uint8_t>* dense,
NormsPodReader* out) {
+ ByteSource src(framed);
+ FramedSection sec;
+ RETURN_IF_ERROR(SectionFramer::read(src, &sec));
+ if (sec.type != static_cast<uint8_t>(SectionType::kNormsSparse)) {
+ return norms_pod_detail::sparse_corrupted("sparse norms section type
mismatch");
+ }
+ if (!src.eof()) {
+ return norms_pod_detail::sparse_corrupted("sparse norms trailing
framed bytes");
+ }
+
+ ByteSource payload(sec.payload);
+ uint8_t mode = 0;
+ uint64_t section_doc_count = 0;
+ uint64_t value_count = 0;
+ RETURN_IF_ERROR(payload.get_u8(&mode));
+ RETURN_IF_ERROR(payload.get_varint64(§ion_doc_count));
+ RETURN_IF_ERROR(payload.get_varint64(&value_count));
+ if (payload.position() !=
+ norms_pod_detail::sparse_header_size(section_doc_count, value_count)) {
+ return norms_pod_detail::sparse_corrupted("sparse norms non-canonical
counts");
+ }
+ if (section_doc_count != doc_count) {
+ return norms_pod_detail::sparse_corrupted("sparse norms doc count
mismatch");
+ }
+ // The values belong to the non-NULL rows of this index: against another
null bitmap they
+ // would land on the wrong rows.
+ if (nulls != nullptr && nulls->doc_count() != doc_count) {
+ return norms_pod_detail::sparse_corrupted("sparse norms null bitmap
doc count mismatch");
+ }
+ const uint64_t null_count = nulls == nullptr ? 0 : nulls->null_count();
+ if (null_count > doc_count || value_count != doc_count - null_count) {
+ return norms_pod_detail::sparse_corrupted("sparse norms value count
mismatch");
+ }
+ const bool constant = mode ==
static_cast<uint8_t>(SparseNormsMode::kConstant);
+ if (!constant && mode != static_cast<uint8_t>(SparseNormsMode::kValues)) {
+ return norms_pod_detail::sparse_corrupted("sparse norms unknown mode");
+ }
+ if (constant && value_count == 0) {
+ return norms_pod_detail::sparse_corrupted("sparse norms shared value
without rows");
+ }
+ const uint64_t stored = constant ? 1 : value_count;
+ if (payload.remaining() != stored) {
+ return norms_pod_detail::sparse_corrupted("sparse norms length
mismatch");
+ }
+ Slice bytes;
+ RETURN_IF_ERROR(payload.get_bytes(static_cast<size_t>(stored), &bytes));
+
+ dense->assign(doc_count, kNullRowNorm);
+ uint64_t placed = 0;
+ bool overflow = false;
+ auto place = [&](uint32_t begin, uint32_t end) {
+ const uint64_t rows = end - begin;
+ // A NULL docid at or past doc_count leaves more rows in range than
the value count.
+ if (overflow || placed + rows > value_count) {
+ overflow = true;
+ return;
+ }
+ if (constant) {
+ std::memset(dense->data() + begin, bytes.data()[0], rows);
+ } else {
+ std::memcpy(dense->data() + begin, bytes.data() + placed, rows);
+ }
+ placed += rows;
+ };
+ if (nulls == nullptr) {
+ place(0, doc_count);
+ } else {
+ nulls->for_each_non_null_run(place);
+ }
+ if (overflow || placed != value_count) {
+ return norms_pod_detail::sparse_corrupted("sparse norms rows disagree
with null bitmap");
+ }
+ out->doc_count_ = doc_count;
+ out->norms_ = dense->data();
+ return Status::OK();
+}
+
} // namespace doris::snii::format
diff --git a/be/src/storage/index/inverted/snii/format/norms_pod.h
b/be/src/storage/index/inverted/snii/format/norms_pod.h
index 9996343fc2d..b78c053c850 100644
--- a/be/src/storage/index/inverted/snii/format/norms_pod.h
+++ b/be/src/storage/index/inverted/snii/format/norms_pod.h
@@ -29,6 +29,18 @@
namespace doris::snii::format {
+class NullBitmapReader;
+
+// The norm a NULL row reads. A NULL row has no tokens, so the writer encodes
it as a document of
+// zero tokens (query::encode_norm(0)), and a sparse section restores it
without storing it.
+inline constexpr uint8_t kNullRowNorm = 1;
+
+// How a sparse norms section stores the norms of its non-NULL rows.
+enum class SparseNormsMode : uint8_t {
+ kValues = 0, // one byte per non-NULL row, in docid order
+ kConstant = 1, // one byte shared by every non-NULL row
+};
+
// Stores one encoded length byte per document for BM25 normalization.
// SectionFramer wraps a payload of [varint64 doc_count][encoded_norm bytes].
class NormsPodWriter {
@@ -43,6 +55,25 @@ public:
void finish(ByteSink* sink) const;
// Zero-copy source overload used by streamed compaction.
static void finish(std::span<const uint8_t> norms, ByteSink* sink);
+ // The framed size finish() writes for `doc_count` rows.
+ static size_t dense_section_size(uint64_t doc_count);
+
+ // Sparse norms keep only the rows a query can score. A NULL row has no
tokens, so no posting
+ // reaches it; the null bitmap of the same index tells the reader which
rows those are.
+ // SectionFramer wraps a payload of
+ // [u8 SparseNormsMode][varint64 doc_count][varint64 value_count][stored
bytes]
+ // where value_count is the number of non-NULL rows. For example, with 7
rows of which 1, 2
+ // and 5 are NULL, the dense norms [3, 1, 1, 1, 2, 1, 4] become the values
[3, 1, 2, 4].
+ //
+ // Whether every row outside `null_docids` has the same norm. False when
no such row exists.
+ static bool sparse_values_constant(std::span<const uint8_t> norms,
+ std::span<const uint32_t> null_docids);
+ // The framed size of a sparse section for `value_count` non-NULL rows out
of `doc_count`.
+ static size_t sparse_section_size(uint64_t doc_count, uint64_t
value_count, bool constant);
+ // Writes the norms of the rows outside `null_docids` (ascending, each
below norms.size()) as
+ // a sparse section. `constant` must be sparse_values_constant(norms,
null_docids).
+ static void finish_sparse(std::span<const uint8_t> norms, std::span<const
uint32_t> null_docids,
+ bool constant, ByteSink* sink);
private:
std::vector<uint8_t> norms_;
@@ -57,6 +88,12 @@ public:
// Parses the entire section (including the framer envelope). Returns
Corruption on CRC mismatch, truncation, or length inconsistency.
// On success, *out borrows the memory pointed to by framer_payload; the
caller must ensure its lifetime.
static Status open(Slice framed, NormsPodReader* out);
+ // Expands a sparse section into one byte per row in `*dense`, with
kNullRowNorm on the rows
+ // `nulls` marks NULL, so every docid reads the byte a dense section would
hold. `nulls` is
+ // null when the index has no NULL rows. Returns Corruption when the
section does not match
+ // `doc_count` and `nulls`. On success, *out borrows `*dense`.
+ static Status open_sparse(Slice framed, uint32_t doc_count, const
NullBitmapReader* nulls,
+ std::vector<uint8_t>* dense, NormsPodReader*
out);
uint32_t doc_count() const { return doc_count_; }
diff --git a/be/src/storage/index/inverted/snii/format/null_bitmap.cpp
b/be/src/storage/index/inverted/snii/format/null_bitmap.cpp
index 78eb5c55a6d..ed501546d2f 100644
--- a/be/src/storage/index/inverted/snii/format/null_bitmap.cpp
+++ b/be/src/storage/index/inverted/snii/format/null_bitmap.cpp
@@ -298,4 +298,21 @@ void
NullBitmapReader::append_docids(std::vector<uint32_t>& out) const {
}
}
+void NullBitmapReader::for_each_non_null_run(
+ const std::function<void(uint32_t, uint32_t)>& fn) const {
+ uint32_t begin = 0;
+ for (const uint32_t null_docid : *bitmap_) {
+ if (null_docid >= doc_count_) {
+ break;
+ }
+ if (begin < null_docid) {
+ fn(begin, null_docid);
+ }
+ begin = null_docid + 1;
+ }
+ if (begin < doc_count_) {
+ fn(begin, doc_count_);
+ }
+}
+
} // namespace doris::snii::format
diff --git a/be/src/storage/index/inverted/snii/format/null_bitmap.h
b/be/src/storage/index/inverted/snii/format/null_bitmap.h
index 6f24e9151a1..cefdddbd844 100644
--- a/be/src/storage/index/inverted/snii/format/null_bitmap.h
+++ b/be/src/storage/index/inverted/snii/format/null_bitmap.h
@@ -19,6 +19,7 @@
#include <cstddef>
#include <cstdint>
+#include <functional>
#include <memory>
#include <span>
#include <vector>
@@ -119,6 +120,10 @@ public:
// Appends decoded docids in ascending order without cloning the bitmap.
void append_docids(std::vector<uint32_t>& out) const;
+ // Calls fn(begin, end) for each maximal range [begin, end) of non-NULL
docids below
+ // doc_count(), in ascending order. With NULL rows {1, 2, 5} of 7 it calls
(0, 1), (3, 5) and
+ // (6, 7).
+ void for_each_non_null_run(const std::function<void(uint32_t, uint32_t)>&
fn) const;
// Total doc count of the logical index, as recorded by the writer.
uint32_t doc_count() const { return doc_count_; }
diff --git a/be/src/storage/index/inverted/snii/reader/logical_index_reader.cpp
b/be/src/storage/index/inverted/snii/reader/logical_index_reader.cpp
index 3e58ac1e864..3ed9fc8da5d 100644
--- a/be/src/storage/index/inverted/snii/reader/logical_index_reader.cpp
+++ b/be/src/storage/index/inverted/snii/reader/logical_index_reader.cpp
@@ -48,6 +48,7 @@ namespace doris::snii::reader {
struct LogicalIndexReader::NormsCacheState {
struct Data {
+ // The dense section as read, or the sparse norms expanded to one byte
per row.
std::vector<uint8_t> bytes;
format::NormsPodReader reader;
};
@@ -83,8 +84,9 @@ constexpr uint64_t kDefaultDictResidentMaxBytes = 256ULL *
1024;
constexpr size_t kMaxDictLookupBatchRuns = 16;
constexpr uint64_t kMaxDictLookupBatchBytes = 4ULL * 1024 * 1024;
// Conservatively covers make_shared's control block and allocator bookkeeping
-// for the state/data/vector allocations. The framed norms bytes are charged
-// separately at their exact validated length.
+// for the state/data/vector allocations. The loaded norms bytes are charged
+// separately: a dense section at its validated length, sparse norms at one
+// byte per row, the size they are expanded to.
constexpr size_t kNormsAllocationOverheadCharge = 128;
// The term lookup cache keeps at most this many terms per reader, each at
most this long and with
// at most this many bytes of inline postings, so its footprint has a fixed
bound.
@@ -148,10 +150,7 @@ Status validate_norms_region(io::FileReader* reader, const
RegionRef& norms, uin
return Status::Error<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED, false>(
"logical_index: norms doc count exceeds uint32");
}
- const uint64_t payload_length = varint_len(doc_count) + doc_count;
- const uint64_t expected_length =
- 1 + varint_len(payload_length) + payload_length + sizeof(uint32_t);
- if (norms.length != expected_length) {
+ if (norms.length != format::NormsPodWriter::dense_section_size(doc_count))
{
return Status::Error<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED, false>(
"logical_index: norms region length mismatch");
}
@@ -163,6 +162,36 @@ Status validate_norms_region(io::FileReader* reader, const
RegionRef& norms, uin
return Status::OK();
}
+// Opening an index reads no norm bytes, so this checks only what the core
metadata fixes: a sparse
+// section holds one byte per non-NULL row, or one byte that all of them share.
+Status validate_sparse_norms_region(io::FileReader* reader, const RegionRef&
norms,
+ uint64_t doc_count, uint64_t null_count) {
+ if (norms.length == 0) {
+ return Status::OK();
+ }
+ const uint64_t file_size = reader->size();
+ if (norms.offset > file_size || norms.length > file_size - norms.offset) {
+ return Status::Error<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED, false>(
+ "logical_index: sparse norms region past end of file");
+ }
+ if (doc_count > std::numeric_limits<uint32_t>::max() || null_count >
doc_count) {
+ return Status::Error<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED, false>(
+ "logical_index: sparse norms doc count {} with {} NULL rows",
doc_count,
+ null_count);
+ }
+ const uint64_t value_count = doc_count - null_count;
+ const bool one_byte_per_row = norms.length ==
format::NormsPodWriter::sparse_section_size(
+ doc_count,
value_count, false);
+ const bool shared_byte =
+ value_count != 0 && norms.length ==
format::NormsPodWriter::sparse_section_size(
+ doc_count,
value_count, true);
+ if (!one_byte_per_row && !shared_byte) {
+ return Status::Error<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED, false>(
+ "logical_index: sparse norms region length mismatch");
+ }
+ return Status::OK();
+}
+
Status validate_null_bitmap_frame(Slice framed) {
ByteSource source(framed);
FramedSection section;
@@ -465,14 +494,22 @@ Status LogicalIndexReader::open(io::FileReader*
file_reader, Slice core_frame, S
size_t norms_length = 0;
RETURN_IF_ERROR(validate_norms_region(file_reader,
candidate.core_.section_refs.norms,
candidate.core_.stats.doc_count,
&norms_length));
- if (norms_length != 0) {
+ RETURN_IF_ERROR(validate_sparse_norms_region(
+ file_reader, candidate.core_.section_refs.sparse_norms,
candidate.core_.stats.doc_count,
+ candidate.core_.stats.null_count));
+ if (candidate.has_norms()) {
+ // Dense norms stay in memory as the section read; sparse norms are
expanded to one byte
+ // per row, which can be far more than their section on disk.
+ const size_t loaded_bytes = norms_length != 0
+ ? norms_length
+ :
static_cast<size_t>(candidate.core_.stats.doc_count);
constexpr size_t fixed_charge = sizeof(NormsCacheState) +
sizeof(NormsCacheState::Data) +
kNormsAllocationOverheadCharge;
- if (norms_length > std::numeric_limits<size_t>::max() - fixed_charge) {
+ if (loaded_bytes > std::numeric_limits<size_t>::max() - fixed_charge) {
return Status::Error<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED,
false>(
"logical_index: norms cache charge overflow");
}
- candidate.norms_reserved_charge_ = fixed_charge + norms_length;
+ candidate.norms_reserved_charge_ = fixed_charge + loaded_bytes;
candidate.norms_cache_ = std::make_shared<NormsCacheState>();
}
// Raw frames alias the group read and compressed carriers materialize into
@@ -550,8 +587,7 @@ Status
LogicalIndexReader::open_norms(format::NormsPodReader* out) const {
if (reader_ == nullptr) {
return Status::Error<ErrorCode::INVALID_ARGUMENT,
false>("logical_index: not opened");
}
- const RegionRef& norms = section_refs().norms;
- if (norms.length == 0) {
+ if (!has_norms()) {
return Status::Error<ErrorCode::INVALID_ARGUMENT, false>(
"logical_index: index has no norms");
}
@@ -584,15 +620,9 @@ Status
LogicalIndexReader::open_norms(format::NormsPodReader* out) const {
}
auto data = std::make_unique<NormsCacheState::Data>();
- const size_t read_len = static_cast<size_t>(norms.length);
- Status status = reader_->read_at(norms.offset, read_len, &data->bytes);
- if (status.ok() && data->bytes.size() != read_len) {
- status = Status::Error<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED,
false>(
- "logical_index: short norms read");
- }
- if (status.ok()) {
- status = format::NormsPodReader::open(Slice(data->bytes),
&data->reader);
- }
+ Status status = section_refs().sparse_norms.length != 0
+ ? read_sparse_norms(&data->bytes, &data->reader)
+ : read_dense_norms(&data->bytes, &data->reader);
{
std::lock_guard lock(norms_cache_->mutex);
if (status.ok()) {
@@ -605,6 +635,37 @@ Status
LogicalIndexReader::open_norms(format::NormsPodReader* out) const {
return status;
}
+Status LogicalIndexReader::read_dense_norms(std::vector<uint8_t>* bytes,
+ format::NormsPodReader* out) const
{
+ const RegionRef& norms = section_refs().norms;
+ const auto read_len = static_cast<size_t>(norms.length);
+ RETURN_IF_ERROR(reader_->read_at(norms.offset, read_len, bytes));
+ if (bytes->size() != read_len) {
+ return Status::Error<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED, false>(
+ "logical_index: short norms read");
+ }
+ return format::NormsPodReader::open(Slice(*bytes), out);
+}
+
+Status LogicalIndexReader::read_sparse_norms(std::vector<uint8_t>* dense,
+ format::NormsPodReader* out)
const {
+ const RegionRef& norms = section_refs().sparse_norms;
+ const auto read_len = static_cast<size_t>(norms.length);
+ std::vector<uint8_t> section;
+ RETURN_IF_ERROR(reader_->read_at(norms.offset, read_len, §ion));
+ if (section.size() != read_len) {
+ return Status::Error<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED, false>(
+ "logical_index: short sparse norms read");
+ }
+ format::NullBitmapReader nulls;
+ bool has_nulls = false;
+ RETURN_IF_ERROR(open_null_bitmap(NullDocidsDecodeReservation(), &nulls,
&has_nulls));
+ // Validated against uint32 when the index was opened.
+ const auto doc_count = static_cast<uint32_t>(stats().doc_count);
+ return format::NormsPodReader::open_sparse(Slice(section), doc_count,
+ has_nulls ? &nulls : nullptr,
dense, out);
+}
+
void LogicalIndexReader::release_compaction_norms() const {
DORIS_CHECK(open_mode_ == LogicalIndexOpenMode::kCompaction);
DORIS_CHECK(norms_cache_ != nullptr);
@@ -624,6 +685,27 @@ Status LogicalIndexReader::read_null_docids(
return Status::Error<ErrorCode::INVALID_ARGUMENT,
false>("logical_index: not opened");
}
+ format::NullBitmapReader null_bitmap_reader;
+ bool present = false;
+ RETURN_IF_ERROR(open_null_bitmap(reserve_decode, &null_bitmap_reader,
&present));
+ if (!present) {
+ return Status::OK();
+ }
+ out->reserve(null_bitmap_reader.null_count());
+ null_bitmap_reader.append_docids(*out);
+ for (uint32_t docid : *out) {
+ if (docid >= stats().doc_count) {
+ out->clear();
+ return Status::Error<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED,
false>(
+ "logical_index: null docid outside document domain");
+ }
+ }
+ return Status::OK();
+}
+
+Status LogicalIndexReader::open_null_bitmap(const NullDocidsDecodeReservation&
reserve_decode,
+ format::NullBitmapReader* out,
bool* present) const {
+ *present = false;
const RegionRef& ref = section_refs().null_bitmap;
if (ref.length == 0) {
if (stats().null_count != 0) {
@@ -656,26 +738,16 @@ Status LogicalIndexReader::read_null_docids(
RETURN_IF_ERROR(reserve_decode(decoded_memory_bytes));
}
- format::NullBitmapReader null_bitmap_reader;
- RETURN_IF_ERROR(format::NullBitmapReader::open(Slice(bytes),
&null_bitmap_reader));
- if (null_bitmap_reader.doc_count() != stats().doc_count) {
+ RETURN_IF_ERROR(format::NullBitmapReader::open(Slice(bytes), out));
+ if (out->doc_count() != stats().doc_count) {
return Status::Error<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED, false>(
"logical_index: null bitmap doc count mismatch");
}
- if (null_bitmap_reader.null_count() != stats().null_count) {
+ if (out->null_count() != stats().null_count) {
return Status::Error<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED, false>(
"logical_index: null bitmap cardinality mismatch");
}
-
- out->reserve(null_bitmap_reader.null_count());
- null_bitmap_reader.append_docids(*out);
- for (uint32_t docid : *out) {
- if (docid >= stats().doc_count) {
- out->clear();
- return Status::Error<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED,
false>(
- "logical_index: null docid outside document domain");
- }
- }
+ *present = true;
return Status::OK();
}
diff --git a/be/src/storage/index/inverted/snii/reader/logical_index_reader.h
b/be/src/storage/index/inverted/snii/reader/logical_index_reader.h
index 2e707f9dab8..45f27a01506 100644
--- a/be/src/storage/index/inverted/snii/reader/logical_index_reader.h
+++ b/be/src/storage/index/inverted/snii/reader/logical_index_reader.h
@@ -62,7 +62,8 @@ class BatchRangeFetcher;
namespace doris::snii::format {
class NormsPodReader;
-}
+class NullBitmapReader;
+} // namespace doris::snii::format
namespace doris::snii::reader {
@@ -244,8 +245,11 @@ public:
const format::HighDfTerms& high_df_terms() const { return
core_.high_df_terms; }
format::IndexTier tier() const { return tier_; }
bool has_positions() const { return has_positions_; }
- // Whether BM25 norms exist: current writers emit them for analyzed
indexes with positions.
- bool has_norms() const { return core_.section_refs.norms.length != 0; }
+ // Whether BM25 norms exist: current writers emit them for analyzed
indexes with positions,
+ // either one byte per row or, as sparse norms, one byte per non-NULL row.
+ bool has_norms() const {
+ return core_.section_refs.norms.length != 0 ||
core_.section_refs.sparse_norms.length != 0;
+ }
LogicalIndexOpenMode open_mode() const { return open_mode_; }
// The chunking scheme of a gram-family index, read back from the core
metadata this segment
// carries. It is what the query side compiles a LIKE / REGEXP pattern
against, so a pattern
@@ -256,11 +260,12 @@ public:
}
io::FileReader* reader() const { return reader_; }
- // Returns a reader over the validated norms section. The first call reads
- // and validates the section; later calls share the immutable reader-owned
- // bytes. The full on-disk section is reserved in memory_usage() before
this
- // LogicalIndexReader enters the searcher cache, so lazy loading cannot
make
- // the cache under-report its eventual resident size.
+ // Returns a reader with one norm byte per row. The first call reads and
+ // validates the section, expanding sparse norms with kNullRowNorm on the
NULL
+ // rows; later calls share the immutable reader-owned bytes. Those loaded
+ // bytes are reserved in memory_usage() before this LogicalIndexReader
enters
+ // the searcher cache, so lazy loading cannot make the cache under-report
its
+ // eventual resident size.
Status open_norms(format::NormsPodReader* out) const;
// Compaction scans one source norm vector at a time. This charge matches
the
// reader's full cache accounting; release_compaction_norms() drops the
loaded
@@ -288,6 +293,13 @@ public:
private:
struct NormsCacheState;
struct TermLookupCache;
+
+ Status read_dense_norms(std::vector<uint8_t>* bytes,
format::NormsPodReader* out) const;
+ Status read_sparse_norms(std::vector<uint8_t>* dense,
format::NormsPodReader* out) const;
+ // Reads and validates the null bitmap. *present is false when the index
has
+ // no section, which is valid only when it counts no NULL row.
+ Status open_null_bitmap(const NullDocidsDecodeReservation& reserve_decode,
+ format::NullBitmapReader* out, bool* present)
const;
io::FileReader* reader_ = nullptr;
format::IndexTier tier_ = format::IndexTier::kT1;
bool has_positions_ = false;
diff --git a/be/src/storage/index/inverted/snii/reader/snii_segment_reader.cpp
b/be/src/storage/index/inverted/snii/reader/snii_segment_reader.cpp
index 816a2f4e4d4..aef7cca61a9 100644
--- a/be/src/storage/index/inverted/snii/reader/snii_segment_reader.cpp
+++ b/be/src/storage/index/inverted/snii/reader/snii_segment_reader.cpp
@@ -262,7 +262,7 @@ Status SniiSegmentReader::load_inherited_index(const
LogicalIndexKey& key,
for (const format::RegionRef& region :
{core.section_refs.dict_region, core.section_refs.posting_region,
core.section_refs.norms,
- core.section_refs.null_bitmap, core.section_refs.bsbf}) {
+ core.section_refs.null_bitmap, core.section_refs.bsbf,
core.section_refs.sparse_norms}) {
if (region.offset > metadata_area_begin ||
region.length > metadata_area_begin - region.offset) {
return corrupted("segment: inherited section reference is outside
the physical area");
diff --git a/be/src/storage/index/inverted/snii/writer/logical_index_writer.cpp
b/be/src/storage/index/inverted/snii/writer/logical_index_writer.cpp
index 54167f73d9b..c6ee1231da5 100644
--- a/be/src/storage/index/inverted/snii/writer/logical_index_writer.cpp
+++ b/be/src/storage/index/inverted/snii/writer/logical_index_writer.cpp
@@ -41,6 +41,7 @@
#include "storage/index/inverted/snii/format/prx_pod.h"
#include "storage/index/inverted/snii/writer/posting_prx_encoder.h"
#include "storage/index/inverted/snii/writer/posting_window_emitter.h"
+#include "util/debug_points.h"
namespace doris::snii::writer {
@@ -933,6 +934,55 @@ Status LogicalIndexWriter::prepare_build(io::FileWriter*
posting_out) {
return Status::OK();
}
+Status LogicalIndexWriter::build_norms_section() {
+ // Sparse norms drop the bytes of the NULL rows, and store a norm that
every row shares once.
+ // Writers keep dense norms; this debug point lets tests choose between
the two forms.
+ bool consider_sparse = false;
+ DBUG_EXECUTE_IF("LogicalIndexWriter::finalize_build.sparse_norms", {
consider_sparse = true; });
+ const std::span<const uint32_t> null_docids(null_docids_.data(),
null_docids_.size());
+ size_t section_size =
format::NormsPodWriter::dense_section_size(doc_count_);
+ bool constant = false;
+ if (consider_sparse) {
+ constant =
format::NormsPodWriter::sparse_values_constant(encoded_norms_, null_docids);
+ const size_t sparse_size = format::NormsPodWriter::sparse_section_size(
+ doc_count_, doc_count_ - null_docids.size(), constant);
+ // The smaller form wins; a tie keeps the dense form, which every
reader of norms knows.
+ if (sparse_size < section_size) {
+ sparse_norms_ = true;
+ section_size = sparse_size;
+ }
+ }
+ if (sparse_norms_) {
+ // The reader restores a NULL row as an empty document, so only that
norm is dropped.
+ for (const uint32_t null_docid : null_docids) {
+ DCHECK_EQ(encoded_norms_[null_docid], format::kNullRowNorm);
+ }
+ }
+ // The sparse payload buffer is reserved at the section size, which bounds
it.
+ const size_t payload_size = sparse_norms_
+ ? section_size
+ : varint_len(encoded_norms_.size()) +
encoded_norms_.size();
+ MemoryReporter::Reservation build_reservation = memory_reporter_ == nullptr
+ ?
MemoryReporter::Reservation()
+ :
memory_reporter_->make_reservation();
+ if (memory_reporter_ != nullptr) {
+ RETURN_IF_ERROR(build_reservation.set_bytes(payload_size));
+ RETURN_IF_ERROR(norms_section_reservation_.set_bytes(section_size));
+ }
+ ByteSink nsink;
+ if (sparse_norms_) {
+ format::NormsPodWriter::finish_sparse(encoded_norms_, null_docids,
constant, &nsink);
+ } else {
+ format::NormsPodWriter::finish(encoded_norms_, &nsink);
+ }
+ norms_section_ = nsink.take();
+ DORIS_CHECK_EQ(norms_section_.capacity(), section_size);
+ if (memory_reporter_ != nullptr) {
+ DORIS_CHECK_EQ(norms_section_reservation_.bytes(),
norms_section_.capacity());
+ }
+ return Status::OK();
+}
+
Status LogicalIndexWriter::finalize_build() {
if (has_norms_ && encoded_norms_.size() != doc_count_) {
return Status::Error<ErrorCode::INVALID_ARGUMENT, false>(
@@ -948,22 +998,7 @@ Status LogicalIndexWriter::finalize_build() {
stats_.null_count = static_cast<uint32_t>(null_docids_.size());
if (has_norms_) {
- const size_t payload_size = varint_len(encoded_norms_.size()) +
encoded_norms_.size();
- const size_t section_size = 1 + varint_len(payload_size) +
payload_size + sizeof(uint32_t);
- MemoryReporter::Reservation build_reservation =
- memory_reporter_ == nullptr ? MemoryReporter::Reservation()
- :
memory_reporter_->make_reservation();
- if (memory_reporter_ != nullptr) {
- RETURN_IF_ERROR(build_reservation.set_bytes(payload_size));
-
RETURN_IF_ERROR(norms_section_reservation_.set_bytes(section_size));
- }
- ByteSink nsink;
- format::NormsPodWriter::finish(encoded_norms_, &nsink);
- norms_section_ = nsink.take();
- DORIS_CHECK_EQ(norms_section_.capacity(), section_size);
- if (memory_reporter_ != nullptr) {
- DORIS_CHECK_EQ(norms_section_reservation_.bytes(),
norms_section_.capacity());
- }
+ RETURN_IF_ERROR(build_norms_section());
}
if (!null_docids_.empty()) {
diff --git a/be/src/storage/index/inverted/snii/writer/logical_index_writer.h
b/be/src/storage/index/inverted/snii/writer/logical_index_writer.h
index 9213fe6d98f..0ff4cb1d0e7 100644
--- a/be/src/storage/index/inverted/snii/writer/logical_index_writer.h
+++ b/be/src/storage/index/inverted/snii/writer/logical_index_writer.h
@@ -269,6 +269,9 @@ public:
bool has_prx() const { return has_prx_; }
bool has_norms() const { return has_norms_; }
+ // Whether norms_bytes() holds sparse norms, which the core metadata
references in their own
+ // field. Set by finalize_build().
+ bool has_sparse_norms() const { return sparse_norms_; }
format::IndexTier tier() const { return tier_; }
uint64_t index_id() const { return index_id_; }
const std::string& index_suffix() const { return index_suffix_; }
@@ -308,6 +311,8 @@ private:
// stats/norms/null-bitmap/BSBF.
Status prepare_build(io::FileWriter* posting_out);
Status finalize_build();
+ // Encodes the norms section, dense or sparse; part of finalize_build().
+ Status build_norms_section();
// Iterates terms (from the streaming source or the materialized vector),
// splitting DICT blocks by target size and filling PODs + blocks_.
Status build_blocks();
@@ -333,6 +338,7 @@ private:
format::IndexTier tier_;
bool has_prx_;
bool has_norms_;
+ bool sparse_norms_ = false;
uint32_t doc_count_;
TrackedNullDocids null_docids_;
const std::vector<TermPostings>& terms_; // materialized fallback (may be
empty)
diff --git a/be/src/storage/index/inverted/snii/writer/snii_compound_writer.cpp
b/be/src/storage/index/inverted/snii/writer/snii_compound_writer.cpp
index 11868e3c1ae..94c5ed3346b 100644
--- a/be/src/storage/index/inverted/snii/writer/snii_compound_writer.cpp
+++ b/be/src/storage/index/inverted/snii/writer/snii_compound_writer.cpp
@@ -597,7 +597,12 @@ Status SniiCompoundWriter::write_tail() {
SectionRefs refs;
refs.dict_region = {.offset = p.dict_off, .length = p.dict_len};
refs.posting_region = {.offset = p.post_off, .length = p.post_len};
- refs.norms = {.offset = p.norms_off, .length = p.norms_len};
+ const format::RegionRef norms = {.offset = p.norms_off, .length =
p.norms_len};
+ if (w.has_sparse_norms()) {
+ refs.sparse_norms = norms;
+ } else {
+ refs.norms = norms;
+ }
refs.null_bitmap = {.offset = p.null_off, .length = p.null_len};
refs.bsbf = {.offset = p.bsbf_off, .length = p.bsbf_len};
diff --git a/be/test/storage/index/index_disk_usage_test.cpp
b/be/test/storage/index/index_disk_usage_test.cpp
index 2b1da522bd5..637c769de2c 100644
--- a/be/test/storage/index/index_disk_usage_test.cpp
+++ b/be/test/storage/index/index_disk_usage_test.cpp
@@ -41,6 +41,7 @@
#include "storage/index/inverted/snii/reader/logical_index_reader.h"
#include "storage/options.h"
#include "storage/tablet/tablet_schema.h"
+#include "util/debug_points.h"
#include "util/defer_op.h"
#include "util/slice.h"
@@ -698,6 +699,49 @@ TEST_F(IndexDiskUsageCollectorTest,
CollectSniiDocsOnlyHasNoPositions) {
EXPECT_GT(text->posting_bytes, 0);
}
+// Sparse norms count toward the index that owns them, as dense norms do, so
the container keeps
+// only its own bytes whichever form the norms take.
+TEST_F(IndexDiskUsageCollectorTest,
CollectSniiAttributesSparseNormsToTheIndex) {
+ // Six rows of text among forty NULL rows, so the sparse norms are far
smaller than the dense.
+ auto feed_text_among_nulls = [](IndexColumnWriter* writer) {
+ feed_text(writer);
+ ASSERT_TRUE(writer->add_nulls(40).ok());
+ feed_text(writer);
+ };
+ auto collect = [&](bool sparse, const std::string& rowset_id) {
+ const bool debug_points_were_enabled = config::enable_debug_points;
+ config::enable_debug_points = true;
+ DebugPoints::instance()->clear();
+ if (sparse) {
+
DebugPoints::instance()->add("LogicalIndexWriter::finalize_build.sparse_norms");
+ }
+ auto schema = create_schema();
+ std::vector<IndexSpec> specs {
+ {.index = text_index(1, true), .column_index = 1, .feed =
feed_text_among_nulls}};
+ const std::string prefix =
+ write_segment(InvertedIndexStorageFormatPB::SNII, rowset_id,
schema, &specs);
+ DebugPoints::instance()->clear();
+ config::enable_debug_points = debug_points_were_enabled;
+ return collect_snii(prefix, schema, /*position_detail=*/false);
+ };
+
+ const auto dense = collect(/*sparse=*/false, "rs_snii_dense_norms");
+ const auto sparse = collect(/*sparse=*/true, "rs_snii_sparse_norms");
+ const IndexDiskUsageRecord* dense_text = find_record(dense, 1,
IndexDiskUsageStructure::kTerm);
+ const IndexDiskUsageRecord* sparse_text =
+ find_record(sparse, 1, IndexDiskUsageStructure::kTerm);
+ const IndexDiskUsageRecord* dense_container =
+ find_record(dense, -1, IndexDiskUsageStructure::kContainer);
+ const IndexDiskUsageRecord* sparse_container =
+ find_record(sparse, -1, IndexDiskUsageStructure::kContainer);
+ ASSERT_NE(dense_text, nullptr);
+ ASSERT_NE(sparse_text, nullptr);
+ ASSERT_NE(dense_container, nullptr);
+ ASSERT_NE(sparse_container, nullptr);
+ EXPECT_LT(sparse_text->stats_bytes, dense_text->stats_bytes);
+ EXPECT_EQ(sparse_container->total_bytes, dense_container->total_bytes);
+}
+
TEST_F(IndexDiskUsageCollectorTest, CollectSniiPositionDetail) {
auto schema = create_schema();
TabletIndex index = text_index(1, true);
diff --git a/be/test/storage/index/inverted/snii/format/core_metadata_test.cpp
b/be/test/storage/index/inverted/snii/format/core_metadata_test.cpp
index 029f0daba00..bf72c096cf4 100644
--- a/be/test/storage/index/inverted/snii/format/core_metadata_test.cpp
+++ b/be/test/storage/index/inverted/snii/format/core_metadata_test.cpp
@@ -49,7 +49,8 @@ CoreMetadata sample_core(IndexConfig index_config =
IndexConfig::kDocsOnly) {
.posting_region = {.offset = 21, .length = 22},
.norms = {},
.null_bitmap = {.offset = 41, .length = 42},
- .bsbf = {.offset = 51, .length = 52}};
+ .bsbf = {.offset = 51, .length = 52},
+ .sparse_norms = {}};
// Norms require positions for BM25 frequencies; docs-only samples omit
them.
if (has_positions(index_config)) {
metadata.section_refs.norms = {.offset = 31, .length = 32};
@@ -111,9 +112,19 @@ void expect_core_eq(const CoreMetadata& expected, const
CoreMetadata& actual) {
EXPECT_EQ(expected.section_refs.null_bitmap.length,
actual.section_refs.null_bitmap.length);
EXPECT_EQ(expected.section_refs.bsbf.offset,
actual.section_refs.bsbf.offset);
EXPECT_EQ(expected.section_refs.bsbf.length,
actual.section_refs.bsbf.length);
+ EXPECT_EQ(expected.section_refs.sparse_norms.offset,
actual.section_refs.sparse_norms.offset);
+ EXPECT_EQ(expected.section_refs.sparse_norms.length,
actual.section_refs.sparse_norms.length);
EXPECT_EQ(expected.gram_scheme, actual.gram_scheme);
}
+// A positions sample whose norms cover only its non-NULL rows.
+CoreMetadata sample_sparse_core() {
+ auto metadata = sample_core(IndexConfig::kDocsPositions);
+ metadata.section_refs.norms = {};
+ metadata.section_refs.sparse_norms = {.offset = 61, .length = 62};
+ return metadata;
+}
+
// Since A2, core metadata forbids norms on docs-only segments: norms require
positions.
TEST(SniiCoreMetadata, RejectsNormsOnDocsOnlyIndex) {
auto metadata = sample_core();
@@ -245,6 +256,70 @@ TEST(SniiCoreMetadata, OmitsEmptyNormsRefOnEncode) {
expect_core_eq(metadata, decoded);
}
+// Sparse norms travel in field 6 and leave field 5 out, so a reader that
predates field 6 sees
+// an index without norms instead of misreading the sparse bytes as one byte
per row.
+TEST(SniiCoreMetadata, RoundTripsSparseNormsInTheirOwnField) {
+ const auto expected = sample_sparse_core();
+ const auto framed = encode(expected);
+ const auto payload = payload_of(framed);
+ doris::snii::SniiCoreMetadataPB core;
+ ASSERT_TRUE(core.ParseFromArray(payload.data(),
static_cast<int>(payload.size())));
+ EXPECT_FALSE(core.section_refs().has_norms());
+ ASSERT_TRUE(core.section_refs().has_sparse_norms());
+ EXPECT_EQ(core.section_refs().sparse_norms().offset(), 61U);
+ EXPECT_EQ(core.section_refs().sparse_norms().length(), 62U);
+
+ CoreMetadata actual;
+ ASSERT_TRUE(decode_core_metadata(Slice(framed), &actual).ok());
+ expect_core_eq(expected, actual);
+}
+
+TEST(SniiCoreMetadata, OmitsEmptySparseNormsRefOnEncode) {
+ const auto payload =
payload_of(encode(sample_core(IndexConfig::kDocsPositions)));
+ doris::snii::SniiCoreMetadataPB core;
+ ASSERT_TRUE(core.ParseFromArray(payload.data(),
static_cast<int>(payload.size())));
+ EXPECT_TRUE(core.section_refs().has_norms());
+ EXPECT_FALSE(core.section_refs().has_sparse_norms());
+}
+
+TEST(SniiCoreMetadata, RejectsDenseAndSparseNormsTogether) {
+ auto metadata = sample_sparse_core();
+ metadata.section_refs.norms = {.offset = 31, .length = 32};
+ ByteSink sink;
+ const auto encoded = encode_core_metadata(metadata, &sink);
+ EXPECT_TRUE(encoded.is<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED>()) <<
encoded;
+
+ const auto payload = mutate_core_payload(sample_sparse_core(), [](auto*
core) {
+ auto* norms = core->mutable_section_refs()->mutable_norms();
+ norms->set_offset(31);
+ norms->set_length(32);
+ });
+ CoreMetadata actual;
+ const auto decoded = decode_core_metadata(Slice(frame_payload(payload)),
&actual);
+ EXPECT_TRUE(decoded.is<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED>()) <<
decoded;
+}
+
+TEST(SniiCoreMetadata, RejectsSparseNormsOnDocsOnlyIndex) {
+ auto metadata = sample_core();
+ metadata.section_refs.sparse_norms = {.offset = 61, .length = 62};
+ ByteSink sink;
+ const auto status = encode_core_metadata(metadata, &sink);
+ EXPECT_TRUE(status.is<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED>()) <<
status;
+}
+
+TEST(SniiCoreMetadata, RejectsSparseNormsRefMissingOffsetOrLength) {
+ for (const auto clear : std::array<void
(doris::snii::SniiRegionRefPB::*)(), 2> {
+ &doris::snii::SniiRegionRefPB::clear_offset,
+ &doris::snii::SniiRegionRefPB::clear_length}) {
+ const auto payload = mutate_core_payload(sample_sparse_core(),
[clear](auto* core) {
+ (core->mutable_section_refs()->mutable_sparse_norms()->*clear)();
+ });
+ CoreMetadata actual;
+ const auto status =
decode_core_metadata(Slice(frame_payload(payload)), &actual);
+ EXPECT_TRUE(status.is<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED>()) <<
status;
+ }
+}
+
TEST(SniiCoreMetadata, RejectsMissingEachStatsField) {
const auto metadata = sample_core();
// sum_total_term_freq is optional and omitted by the production writer.
diff --git a/be/test/storage/index/inverted/snii/format/norms_pod_test.cpp
b/be/test/storage/index/inverted/snii/format/norms_pod_test.cpp
index ca8bd4e5eee..de49fc83a0d 100644
--- a/be/test/storage/index/inverted/snii/format/norms_pod_test.cpp
+++ b/be/test/storage/index/inverted/snii/format/norms_pod_test.cpp
@@ -27,11 +27,17 @@
#include "storage/index/inverted/snii/encoding/byte_sink.h"
#include "storage/index/inverted/snii/encoding/section_framer.h"
#include "storage/index/inverted/snii/format/format_constants.h"
+#include "storage/index/inverted/snii/format/null_bitmap.h"
+#include "storage/index/inverted/snii/query/bm25_scorer.h"
using namespace doris::snii;
using doris::Status; // RETURN_IF_ERROR expands to bare Status
+using doris::snii::format::kNullRowNorm;
using doris::snii::format::NormsPodReader;
using doris::snii::format::NormsPodWriter;
+using doris::snii::format::NullBitmapReader;
+using doris::snii::format::NullBitmapWriter;
+using doris::snii::format::SparseNormsMode;
namespace {
@@ -217,3 +223,200 @@ TEST(SniiNormsPod, TryEncodedNormChecksBounds) {
Status s = reader.try_encoded_norm(3, &v);
EXPECT_TRUE(s.is<doris::ErrorCode::INVALID_ARGUMENT>()) << s.to_string();
}
+
+namespace {
+
+NullBitmapReader OpenNulls(const std::vector<uint32_t>& nulls, uint32_t
doc_count) {
+ NullBitmapWriter writer;
+ writer.add_many(nulls);
+ ByteSink sink;
+ EXPECT_TRUE(writer.finish(doc_count, &sink).ok());
+ NullBitmapReader reader;
+ EXPECT_TRUE(NullBitmapReader::open(sink.view(), &reader).ok());
+ return reader;
+}
+
+// Encodes the rows of `norms` outside `nulls` and checks the size the writer
reserves for it.
+std::vector<uint8_t> BuildSparse(const std::vector<uint8_t>& norms,
+ const std::vector<uint32_t>& nulls) {
+ const bool constant = NormsPodWriter::sparse_values_constant(norms, nulls);
+ ByteSink sink;
+ NormsPodWriter::finish_sparse(norms, nulls, constant, &sink);
+ EXPECT_EQ(sink.buffer().size(), NormsPodWriter::sparse_section_size(
+ norms.size(), norms.size() -
nulls.size(), constant));
+ return sink.buffer();
+}
+
+Status ExpandSparse(const std::vector<uint8_t>& framed, const
std::vector<uint32_t>& nulls,
+ uint32_t doc_count, std::vector<uint8_t>* dense,
NormsPodReader* reader) {
+ const auto bitmap = OpenNulls(nulls, doc_count);
+ return NormsPodReader::open_sparse(Slice(framed), doc_count, &bitmap,
dense, reader);
+}
+
+// What a reader of the dense section sees for the same rows: a NULL row reads
kNullRowNorm.
+std::vector<uint8_t> DenseView(std::vector<uint8_t> norms, const
std::vector<uint32_t>& nulls) {
+ for (const uint32_t docid : nulls) {
+ norms[docid] = kNullRowNorm;
+ }
+ return norms;
+}
+
+std::vector<uint8_t> FrameSparse(uint8_t mode, uint64_t doc_count, uint64_t
value_count,
+ const std::vector<uint8_t>& bytes) {
+ ByteSink payload;
+ payload.put_u8(mode);
+ payload.put_varint64(doc_count);
+ payload.put_varint64(value_count);
+ for (const uint8_t byte : bytes) {
+ payload.put_u8(byte);
+ }
+ ByteSink sink;
+ SectionFramer::write(sink,
static_cast<uint8_t>(format::SectionType::kNormsSparse),
+ payload.view());
+ return sink.buffer();
+}
+
+} // namespace
+
+// The writer compares this size with the sparse one, so it must be the size
finish() writes.
+TEST(SniiNormsPod, DenseSectionSizeIsTheWrittenSize) {
+ for (const size_t doc_count : {0, 7, 127, 128, 5000}) {
+ EXPECT_EQ(NormsPodWriter::dense_section_size(doc_count),
+ BuildPod(std::vector<uint8_t>(doc_count, 3)).size())
+ << "doc_count=" << doc_count;
+ }
+}
+
+// The writer and the reader must agree on the norm of a NULL row, which the
reader restores
+// without storing it: the writer encodes a NULL row as a document of zero
tokens.
+TEST(SniiNormsPod, NullRowNormIsTheNormOfAnEmptyDocument) {
+ EXPECT_EQ(query::encode_norm(0), kNullRowNorm);
+}
+
+// Rows 1, 2 and 5 are NULL: the section keeps the norms of rows 0, 3, 4 and 6
only, and the
+// reader rebuilds one byte per row with kNullRowNorm on the NULL rows. The 9s
written for the
+// NULL rows are dropped, so the reader cannot return them.
+TEST(SniiNormsPod, SparseKeepsOnlyNonNullRows) {
+ const std::vector<uint8_t> norms = {3, 9, 9, 1, 2, 9, 4};
+ const std::vector<uint32_t> nulls = {1, 2, 5};
+ const auto framed = BuildSparse(norms, nulls);
+ EXPECT_EQ(framed.size(), NormsPodWriter::sparse_section_size(7, 4,
/*constant=*/false));
+
+ std::vector<uint8_t> dense;
+ NormsPodReader reader;
+ ASSERT_TRUE(ExpandSparse(framed, nulls, 7, &dense, &reader).ok());
+ EXPECT_EQ(dense, (std::vector<uint8_t> {3, kNullRowNorm, kNullRowNorm, 1,
2, kNullRowNorm, 4}));
+ ASSERT_EQ(reader.doc_count(), 7U);
+ for (uint32_t docid = 0; docid < 7; ++docid) {
+ EXPECT_EQ(reader.encoded_norm(docid), dense[docid]) << "docid=" <<
docid;
+ }
+}
+
+// Rows of one token each share one norm, so the section stores that byte once.
+TEST(SniiNormsPod, SparseStoresASharedNormOnce) {
+ const std::vector<uint8_t> norms = {5, 1, 5, 5, 1, 5};
+ const std::vector<uint32_t> nulls = {1, 4};
+ ASSERT_TRUE(NormsPodWriter::sparse_values_constant(norms, nulls));
+ const auto framed = BuildSparse(norms, nulls);
+ EXPECT_EQ(framed.size(), NormsPodWriter::sparse_section_size(6, 4,
/*constant=*/true));
+
+ std::vector<uint8_t> dense;
+ NormsPodReader reader;
+ ASSERT_TRUE(ExpandSparse(framed, nulls, 6, &dense, &reader).ok());
+ EXPECT_EQ(dense, DenseView(norms, nulls));
+}
+
+TEST(SniiNormsPod, SparseWithEveryRowNullStoresNoValue) {
+ const std::vector<uint8_t> norms = {1, 1, 1};
+ const std::vector<uint32_t> nulls = {0, 1, 2};
+ EXPECT_FALSE(NormsPodWriter::sparse_values_constant(norms, nulls));
+ const auto framed = BuildSparse(norms, nulls);
+
+ std::vector<uint8_t> dense;
+ NormsPodReader reader;
+ ASSERT_TRUE(ExpandSparse(framed, nulls, 3, &dense, &reader).ok());
+ EXPECT_EQ(dense, std::vector<uint8_t>(3, kNullRowNorm));
+}
+
+// An index without NULL rows has no null bitmap, and its sparse section holds
every row.
+TEST(SniiNormsPod, SparseWithoutNullRowsHoldsEveryRow) {
+ const std::vector<uint8_t> norms = {2, 7, 4};
+ const auto framed = BuildSparse(norms, {});
+
+ std::vector<uint8_t> dense;
+ NormsPodReader reader;
+ ASSERT_TRUE(NormsPodReader::open_sparse(Slice(framed), 3, nullptr, &dense,
&reader).ok());
+ EXPECT_EQ(dense, norms);
+}
+
+TEST(SniiNormsPod, SparseRoundTripMatchesDenseOnManyRows) {
+ constexpr uint32_t kDocs = 5000;
+ std::vector<uint8_t> norms(kDocs);
+ std::vector<uint32_t> nulls;
+ for (uint32_t docid = 0; docid < kDocs; ++docid) {
+ norms[docid] = static_cast<uint8_t>((docid * 31 + 7) & 0xFF);
+ // Nine rows in ten are NULL, in runs of different lengths.
+ if ((docid * 7919) % 10 != 0) {
+ nulls.push_back(docid);
+ }
+ }
+ const auto framed = BuildSparse(norms, nulls);
+ EXPECT_LT(framed.size(), BuildPod(norms).size());
+
+ std::vector<uint8_t> dense;
+ NormsPodReader reader;
+ ASSERT_TRUE(ExpandSparse(framed, nulls, kDocs, &dense, &reader).ok());
+ EXPECT_EQ(dense, DenseView(norms, nulls));
+}
+
+// The section belongs to one null bitmap: read against another, its values
would land on the
+// wrong rows, so the counts must agree.
+TEST(SniiNormsPod, SparseRejectsAnotherNullBitmap) {
+ const auto framed = BuildSparse({3, 1, 1, 4}, {1, 2});
+ std::vector<uint8_t> dense;
+ NormsPodReader reader;
+ const Status status = ExpandSparse(framed, {1}, 4, &dense, &reader);
+ EXPECT_TRUE(status.is<doris::ErrorCode::INVERTED_INDEX_FILE_CORRUPTED>())
<< status;
+}
+
+TEST(SniiNormsPod, SparseRejectsAnotherDocCount) {
+ const auto framed = BuildSparse({3, 1, 4}, {1});
+ std::vector<uint8_t> dense;
+ NormsPodReader reader;
+ const auto bitmap = OpenNulls({1}, 3);
+ const Status status = NormsPodReader::open_sparse(Slice(framed), 4,
&bitmap, &dense, &reader);
+ EXPECT_TRUE(status.is<doris::ErrorCode::INVERTED_INDEX_FILE_CORRUPTED>())
<< status;
+}
+
+TEST(SniiNormsPod, SparseRejectsMalformedSections) {
+ const auto values = static_cast<uint8_t>(SparseNormsMode::kValues);
+ const auto constant = static_cast<uint8_t>(SparseNormsMode::kConstant);
+ const std::vector<std::vector<uint8_t>> malformed = {
+ // Unknown mode.
+ FrameSparse(2, 3, 2, {3, 4}),
+ // Fewer values than non-NULL rows.
+ FrameSparse(values, 3, 2, {3}),
+ // More bytes than values.
+ FrameSparse(values, 3, 2, {3, 4, 5}),
+ // A shared norm needs at least one non-NULL row.
+ FrameSparse(constant, 3, 0, {3}),
+ // A shared norm is one byte.
+ FrameSparse(constant, 3, 2, {3, 3}),
+ // The dense frame type.
+ BuildPod({3, 1, 4}),
+ };
+ for (const auto& framed : malformed) {
+ std::vector<uint8_t> dense;
+ NormsPodReader reader;
+ const Status status = ExpandSparse(framed, {1}, 3, &dense, &reader);
+
EXPECT_TRUE(status.is<doris::ErrorCode::INVERTED_INDEX_FILE_CORRUPTED>()) <<
status;
+ }
+
+ auto flipped = BuildSparse({3, 1, 4}, {1});
+ ASSERT_GT(flipped.size(), 5U);
+ flipped[flipped.size() - 5] ^= 0xFF;
+ std::vector<uint8_t> dense;
+ NormsPodReader reader;
+ const Status status = ExpandSparse(flipped, {1}, 3, &dense, &reader);
+ EXPECT_TRUE(status.is<doris::ErrorCode::INVERTED_INDEX_FILE_CORRUPTED>())
<< status;
+}
diff --git a/be/test/storage/index/inverted/snii/format/null_bitmap_test.cpp
b/be/test/storage/index/inverted/snii/format/null_bitmap_test.cpp
index 6ad1bd78f34..78c1e2c90f1 100644
--- a/be/test/storage/index/inverted/snii/format/null_bitmap_test.cpp
+++ b/be/test/storage/index/inverted/snii/format/null_bitmap_test.cpp
@@ -25,6 +25,7 @@
#include <numeric>
#include <span>
#include <string>
+#include <utility>
#include <vector>
#include "common/status.h"
@@ -315,3 +316,34 @@ TEST(SniiNullBitmap,
DecodedMemoryAccountsEachPortableContainer) {
EXPECT_EQ(decoded_bytes,
roaring_bytes + kContainerCount * kContainerMetadataBytes +
kFixedBytes);
}
+
+namespace {
+
+using Runs = std::vector<std::pair<uint32_t, uint32_t>>;
+
+Runs NonNullRuns(const std::vector<uint32_t>& nulls, uint32_t doc_count) {
+ const auto buffer = BuildBitmap(nulls, doc_count);
+ NullBitmapReader reader;
+ EXPECT_TRUE(NullBitmapReader::open(Slice(buffer), &reader).ok());
+ Runs runs;
+ reader.for_each_non_null_run(
+ [&runs](uint32_t begin, uint32_t end) { runs.emplace_back(begin,
end); });
+ return runs;
+}
+
+} // namespace
+
+// Sparse norms are laid out over the non-NULL rows, so the reader walks the
gaps between the
+// NULL rows: with NULL rows {1, 2, 5} of 7 the rows holding values are [0,
1), [3, 5) and [6, 7).
+TEST(SniiNullBitmap, NonNullRunsAreTheGapsBetweenNulls) {
+ EXPECT_EQ(NonNullRuns({1, 2, 5}, 7), (Runs {{0, 1}, {3, 5}, {6, 7}}));
+ EXPECT_EQ(NonNullRuns({0, 6}, 7), (Runs {{1, 6}}));
+ EXPECT_EQ(NonNullRuns({}, 4), (Runs {{0, 4}}));
+ EXPECT_EQ(NonNullRuns({0, 1, 2}, 3), Runs {});
+ EXPECT_EQ(NonNullRuns({}, 0), Runs {});
+
+ // A long stretch of NULL rows is serialized as a run container.
+ std::vector<uint32_t> stretch(1000);
+ std::iota(stretch.begin(), stretch.end(), 100U);
+ EXPECT_EQ(NonNullRuns(stretch, 2000), (Runs {{0, 100}, {1100, 2000}}));
+}
diff --git
a/be/test/storage/index/inverted/snii/reader/snii_index_source_test.cpp
b/be/test/storage/index/inverted/snii/reader/snii_index_source_test.cpp
index 2516f267551..c2aaa88ece5 100644
--- a/be/test/storage/index/inverted/snii/reader/snii_index_source_test.cpp
+++ b/be/test/storage/index/inverted/snii/reader/snii_index_source_test.cpp
@@ -19,20 +19,26 @@
#include <gtest/gtest.h>
+#include <algorithm>
#include <memory>
+#include <numeric>
#include <span>
#include <string>
+#include <utility>
#include <vector>
+#include "common/config.h"
#include "storage/index/inverted/query/exec/block_doc_set.h"
#include "storage/index/inverted/query/exec/roaring_docid_sink.h"
#include "storage/index/inverted/query/exec/term_pattern.h"
#include "storage/index/inverted/query/exec/term_waves.h"
+#include "storage/index/inverted/snii/format/norms_pod.h"
#include "storage/index/inverted/snii/format/phrase_bigram.h"
#include "storage/index/inverted/snii/io/metered_file_reader.h"
#include "storage/index/inverted/snii/query/internal/docid_posting_reader.h"
#include "storage/index/inverted/snii/snii_query_test_util.h"
#include "storage/index/inverted/snii/writer/snii_compound_writer.h"
+#include "util/debug_points.h"
namespace doris::snii::reader {
namespace {
@@ -406,6 +412,208 @@ TEST_F(SniiScoredSourceTest,
EncodedNormsAreReadOnceAndOnlyWhenAsked) {
EXPECT_EQ(rounds(), read);
}
+// The same corpus written with dense norms and through the writer path that
may choose sparse
+// norms. Every non-NULL row holds "alpha" twice and, when even, "beta" once.
+class SniiSparseNormsTest : public ::testing::Test {
+protected:
+ static constexpr uint32_t kDocCount = 40;
+ static constexpr const char* kSparseNormsPoint =
+ "LogicalIndexWriter::finalize_build.sparse_norms";
+
+ struct Written {
+ MemoryFile file;
+ SniiSegmentReader segment;
+ LogicalIndexReader index;
+ std::unique_ptr<SniiIndexSource> source;
+ };
+
+ // Rows 3 to 5 and every row from 20 on: enough NULL rows for the sparse
form to win.
+ static bool is_null(uint32_t doc) { return (doc >= 3 && doc <= 5) || doc
>= 20; }
+
+ static std::vector<uint32_t> mostly_null_rows() {
+ std::vector<uint32_t> nulls;
+ for (uint32_t doc = 0; doc < kDocCount; ++doc) {
+ if (is_null(doc)) {
+ nulls.push_back(doc);
+ }
+ }
+ return nulls;
+ }
+
+ // `nulls` are the NULL rows, ascending. The norms of the other rows
differ unless
+ // `shared_norm`, which gives them all the norm 2.
+ static writer::SniiIndexInput corpus(const std::vector<uint32_t>& nulls,
+ bool shared_norm = false) {
+ std::vector<snii_test::PostingDoc> alpha;
+ std::vector<snii_test::PostingDoc> beta;
+ writer::SniiIndexInput input;
+ input.index_id = 12;
+ input.index_suffix = "sparse";
+ input.config = format::IndexConfig::kDocsPositions;
+ input.doc_count = kDocCount;
+ input.encoded_norms.resize(kDocCount);
+ for (uint32_t doc = 0; doc < kDocCount; ++doc) {
+ if (std::ranges::binary_search(nulls, doc)) {
+ input.null_docids.push_back(doc);
+ input.encoded_norms[doc] = format::kNullRowNorm;
+ continue;
+ }
+ alpha.push_back({.docid = doc, .positions = {0, 2}});
+ if (doc % 2 == 0) {
+ beta.push_back({.docid = doc, .positions = {1}});
+ }
+ input.encoded_norms[doc] = shared_norm ? 2 :
static_cast<uint8_t>(doc % 7 + 2);
+ }
+ input.terms = {make_term("alpha", std::move(alpha)), make_term("beta",
std::move(beta))};
+ return input;
+ }
+
+ // Writes `input`, through the writer path that may choose sparse norms
when `sparse`, and
+ // opens it for reading.
+ static std::unique_ptr<Written> write(const writer::SniiIndexInput& input,
bool sparse) {
+ auto written = std::make_unique<Written>();
+ const bool debug_points_were_enabled = config::enable_debug_points;
+ config::enable_debug_points = true;
+ DebugPoints::instance()->clear();
+ if (sparse) {
+ DebugPoints::instance()->add(kSparseNormsPoint);
+ }
+ writer::SniiCompoundWriter compound_writer(&written->file);
+ assert_ok(compound_writer.add_logical_index(input));
+ assert_ok(compound_writer.finish());
+ DebugPoints::instance()->clear();
+ config::enable_debug_points = debug_points_were_enabled;
+
+ assert_ok(SniiSegmentReader::open(&written->file, &written->segment));
+ assert_ok(written->segment.open_index(12, "sparse", &written->index));
+ written->source = std::make_unique<SniiIndexSource>(written->index);
+ return written;
+ }
+
+ // (docid, norm) of every posting of `term` as a scoring cursor reads them.
+ static std::vector<std::pair<uint32_t, uint32_t>>
scored_norms(SniiIndexSource& source,
+ const
std::string& term) {
+ std::unique_ptr<index_query::PostingsCursor> cursor;
+ assert_ok(source.open_term(term, /*positions=*/true, /*scoring=*/true,
&cursor));
+ EXPECT_NE(cursor, nullptr) << term;
+ std::vector<std::pair<uint32_t, uint32_t>> norms;
+ index_query::PostingsBlock block;
+ bool eof = false;
+ assert_ok(cursor->next_block(&block, &eof));
+ while (!eof) {
+ for (uint64_t ordinal = 0; ordinal < block.size(); ++ordinal) {
+ norms.emplace_back(block.doc_at(ordinal),
block.norm_at(ordinal));
+ }
+ assert_ok(cursor->next_block(&block, &eof));
+ }
+ return norms;
+ }
+
+ static std::vector<uint32_t> all_norms(SniiIndexSource& source) {
+ std::vector<uint32_t> docs(kDocCount);
+ std::iota(docs.begin(), docs.end(), 0U);
+ std::vector<uint32_t> norms;
+ assert_ok(source.encoded_norms(docs, &norms));
+ return norms;
+ }
+};
+
+// Sparse norms drop the bytes of the NULL rows into a smaller section of
their own.
+TEST_F(SniiSparseNormsTest, SparseNormsLiveInTheirOwnSmallerSection) {
+ const auto input = corpus(mostly_null_rows());
+ const auto dense = write(input, /*sparse=*/false);
+ const auto sparse = write(input, /*sparse=*/true);
+
+ const auto& dense_refs = dense->index.section_refs();
+ const auto& sparse_refs = sparse->index.section_refs();
+ ASSERT_NE(dense_refs.norms.length, 0U);
+ EXPECT_EQ(dense_refs.sparse_norms.length, 0U);
+ EXPECT_EQ(sparse_refs.norms.length, 0U);
+ ASSERT_NE(sparse_refs.sparse_norms.length, 0U);
+ EXPECT_LT(sparse_refs.sparse_norms.length, dense_refs.norms.length);
+ EXPECT_TRUE(dense->index.has_norms());
+ EXPECT_TRUE(sparse->index.has_norms());
+}
+
+// Every reader path sees the bytes a dense section holds: the norms of the
scored postings, the
+// norm of every row, and the stats.
+TEST_F(SniiSparseNormsTest, SparseNormsReadLikeDenseNorms) {
+ const auto input = corpus(mostly_null_rows());
+ const auto dense = write(input, /*sparse=*/false);
+ const auto sparse = write(input, /*sparse=*/true);
+ ASSERT_NE(sparse->index.section_refs().sparse_norms.length, 0U);
+
+ for (const char* term : {"alpha", "beta"}) {
+ const auto expected = scored_norms(*dense->source, term);
+ ASSERT_FALSE(expected.empty()) << term;
+ EXPECT_EQ(scored_norms(*sparse->source, term), expected) << term;
+ }
+ const auto expected_rows = all_norms(*dense->source);
+ EXPECT_EQ(all_norms(*sparse->source), expected_rows);
+ for (uint32_t doc = 0; doc < kDocCount; ++doc) {
+ if (is_null(doc)) {
+ EXPECT_EQ(expected_rows[doc], format::kNullRowNorm) << doc;
+ }
+ }
+
+ index_query::IndexScoringStats dense_stats;
+ index_query::IndexScoringStats sparse_stats;
+ assert_ok(dense->source->scoring_stats(&dense_stats));
+ assert_ok(sparse->source->scoring_stats(&sparse_stats));
+ EXPECT_EQ(sparse_stats.doc_count, dense_stats.doc_count);
+ EXPECT_EQ(sparse_stats.token_count, dense_stats.token_count);
+ EXPECT_TRUE(sparse_stats.has_norms);
+}
+
+// The reader keeps sparse norms expanded to one byte per row, so their cache
charge is one byte per
+// row, not the smaller section on disk. Dense norms stay in memory as the
section read, framing
+// included, so the two charges differ by the framing of the dense section.
+TEST_F(SniiSparseNormsTest, CacheChargeCoversTheExpandedNorms) {
+ const auto input = corpus(mostly_null_rows());
+ const auto dense = write(input, /*sparse=*/false);
+ const auto sparse = write(input, /*sparse=*/true);
+ const uint64_t dense_length = dense->index.section_refs().norms.length;
+ ASSERT_NE(sparse->index.section_refs().sparse_norms.length, 0U);
+ ASSERT_LT(sparse->index.section_refs().sparse_norms.length, kDocCount);
+ EXPECT_EQ(dense->index.compaction_norms_cache_charge() -
+ sparse->index.compaction_norms_cache_charge(),
+ dense_length - kDocCount);
+}
+
+// Without NULL rows and with norms that differ, the sparse form only adds its
header, so the
+// index keeps the dense section.
+TEST_F(SniiSparseNormsTest, IndexWithoutNullRowsStaysDense) {
+ const auto written = write(corpus({}), /*sparse=*/true);
+ EXPECT_NE(written->index.section_refs().norms.length, 0U);
+ EXPECT_EQ(written->index.section_refs().sparse_norms.length, 0U);
+}
+
+// The writer keeps the smaller form, and a tie keeps the dense one, which
every reader of norms
+// knows. Both forms frame their payload alike, so the payloads decide: for 40
rows whose norms
+// differ, dense is 1 + 40 bytes and sparse is 3 + (40 - NULL rows) bytes. Two
NULL rows tie,
+// three make the sparse form smaller.
+TEST_F(SniiSparseNormsTest, WriterKeepsTheSmallerForm) {
+ const auto tie = write(corpus({0, 1}), /*sparse=*/true);
+ EXPECT_NE(tie->index.section_refs().norms.length, 0U);
+ EXPECT_EQ(tie->index.section_refs().sparse_norms.length, 0U);
+
+ const auto smaller = write(corpus({0, 1, 2}), /*sparse=*/true);
+ EXPECT_EQ(smaller->index.section_refs().norms.length, 0U);
+ EXPECT_NE(smaller->index.section_refs().sparse_norms.length, 0U);
+}
+
+// Rows that all share one norm store it once, with or without NULL rows: 40
rows of one-token
+// values keep a sparse section of a few bytes instead of one byte per row.
+TEST_F(SniiSparseNormsTest, SharedNormIsStoredOnceWithoutNullRows) {
+ const auto input = corpus({}, /*shared_norm=*/true);
+ const auto dense = write(input, /*sparse=*/false);
+ const auto sparse = write(input, /*sparse=*/true);
+ EXPECT_EQ(sparse->index.section_refs().norms.length, 0U);
+ EXPECT_EQ(sparse->index.section_refs().sparse_norms.length,
+ format::NormsPodWriter::sparse_section_size(kDocCount,
kDocCount, true));
+ EXPECT_EQ(all_norms(*sparse->source), all_norms(*dense->source));
+}
+
// The corpus read through a metered reader, so the rounds of every open count.
class SniiIndexSourceRoundsTest : public ::testing::Test {
protected:
diff --git
a/be/test/storage/index/inverted/snii/reader/snii_rewrite_snapshot_test.cpp
b/be/test/storage/index/inverted/snii/reader/snii_rewrite_snapshot_test.cpp
index 4c6df8feac2..80f5fd3a6ce 100644
--- a/be/test/storage/index/inverted/snii/reader/snii_rewrite_snapshot_test.cpp
+++ b/be/test/storage/index/inverted/snii/reader/snii_rewrite_snapshot_test.cpp
@@ -194,8 +194,8 @@ uint64_t MetadataAreaBegin(const std::vector<uint8_t>&
image) {
uint64_t SectionEnd(const SectionRefs& refs) {
uint64_t end = 0;
- for (const RegionRef& region :
- {refs.dict_region, refs.posting_region, refs.norms, refs.null_bitmap,
refs.bsbf}) {
+ for (const RegionRef& region : {refs.dict_region, refs.posting_region,
refs.norms,
+ refs.null_bitmap, refs.bsbf,
refs.sparse_norms}) {
end = std::max(end, region.offset + region.length);
}
return end;
@@ -450,6 +450,28 @@ TEST(SniiRewriteSnapshot,
RejectsSectionReferenceOutsideThePhysicalArea) {
EXPECT_TRUE(status.is<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED>()) <<
status;
}
+// Sparse norms are a section like the others: the prefix copy must carry them
too.
+TEST(SniiRewriteSnapshot, RejectsSparseNormsReferenceOutsideThePhysicalArea) {
+ const std::vector<uint8_t> good = BuildTwoIndexImage();
+ const uint64_t metadata_begin = MetadataAreaBegin(good);
+ const std::vector<uint8_t> bad =
+ RebuildMetadataArea(good, [metadata_begin](uint64_t index_id,
CoreMetadata* core) {
+ if (index_id == kIndexIdA) {
+ core->section_refs.sparse_norms.offset = metadata_begin;
+ core->section_refs.sparse_norms.length = 1;
+ }
+ });
+
+ ImageFileReader reader(bad);
+ SniiSegmentReader segment;
+ ASSERT_TRUE(SniiSegmentReader::open(&reader, &segment).ok());
+
+ SniiRewriteSnapshot snapshot;
+ const Status status =
+ segment.prepare_rewrite_snapshot(KeysOf({{kIndexIdA, kSuffixA}}),
kDocCount, &snapshot);
+ EXPECT_TRUE(status.is<ErrorCode::INVERTED_INDEX_FILE_CORRUPTED>()) <<
status;
+}
+
TEST(SniiRewriteSnapshot, RejectsCorruptBootstrapHeader) {
const std::vector<uint8_t> image = BuildTwoIndexImage();
ImageFileReader reader(image);
diff --git
a/be/test/storage/index/inverted/snii/snii_index_reader_analyzed_query_test.cpp
b/be/test/storage/index/inverted/snii/snii_index_reader_analyzed_query_test.cpp
index e0811c38c2e..595ed3e8aad 100644
---
a/be/test/storage/index/inverted/snii/snii_index_reader_analyzed_query_test.cpp
+++
b/be/test/storage/index/inverted/snii/snii_index_reader_analyzed_query_test.cpp
@@ -26,6 +26,7 @@
#include <vector>
#include "common/check.h"
+#include "common/config.h"
#include "core/column/column_nullable.h"
#include "core/column/column_vector.h"
#include "io/fs/local_file_system.h"
@@ -48,6 +49,7 @@
#include "storage/index/inverted/snii/writer/snii_compound_writer.h"
#include "storage/olap_common.h"
#include "storage/tablet/tablet_schema.h"
+#include "util/debug_points.h"
// The analyzed query entry: the caller hands the reader terms it already
// analyzed, the reader takes them verbatim, and everything after analysis (the
@@ -137,6 +139,34 @@ doris::snii::writer::SniiIndexInput scored_segment_input()
{
return input;
}
+// Every third document from doc 1 is NULL. The others hold "alpha", the even
ones also "beta",
+// and their lengths run from 2 to 6 so that the norms move the scores.
+bool is_null_row(uint32_t docid) {
+ return docid % 3 == 1;
+}
+
+doris::snii::writer::SniiIndexInput scored_segment_with_nulls_input() {
+ std::vector<PostingDoc> alpha;
+ std::vector<PostingDoc> beta;
+ doris::snii::writer::SniiIndexInput input;
+ input.doc_count = kDocCount;
+ input.encoded_norms.resize(kDocCount);
+ for (uint32_t docid = 0; docid < kDocCount; ++docid) {
+ if (is_null_row(docid)) {
+ input.null_docids.push_back(docid);
+ input.encoded_norms[docid] = doris::snii::query::encode_norm(0);
+ continue;
+ }
+ alpha.push_back({.docid = docid, .positions = {0}});
+ if (docid % 2 == 0) {
+ beta.push_back({.docid = docid, .positions = {1}});
+ }
+ input.encoded_norms[docid] = doris::snii::query::encode_norm(2 + docid
% 5);
+ }
+ input.terms = {make_term("alpha", std::move(alpha)), make_term("beta",
std::move(beta))};
+ return input;
+}
+
// doc 0 "alpha beta", doc 1 "alpha gamma beta", doc 2 "beta alpha", doc 3
"alpha".
doris::snii::writer::SniiIndexInput slop_segment_input() {
doris::snii::writer::SniiIndexInput input;
@@ -531,5 +561,54 @@ TEST_F(SniiIndexReaderAnalyzedQueryTest,
ScoresReachTheCollectionSimilarity) {
EXPECT_FLOAT_EQ(analyzed_score, raw_score);
}
+// Sparse norms change what is stored, not what is scored: through MATCH and
the collection
+// similarity, every matched row scores bit for bit what it scores with dense
norms.
+TEST_F(SniiIndexReaderAnalyzedQueryTest, SparseNormsScoreLikeDenseNorms) {
+ std::vector<uint32_t> beta_docs;
+ for (uint32_t docid = 0; docid < kDocCount; ++docid) {
+ if (!is_null_row(docid) && docid % 2 == 0) {
+ beta_docs.push_back(docid);
+ }
+ }
+ const auto scores_of = [&](bool sparse, std::string_view name, int64_t*
file_size) {
+ const bool debug_points_were_enabled = config::enable_debug_points;
+ config::enable_debug_points = true;
+ DebugPoints::instance()->clear();
+ if (sparse) {
+
DebugPoints::instance()->add("LogicalIndexWriter::finalize_build.sparse_norms");
+ }
+ auto reader = open_reader(scored_segment_with_nulls_input(), name,
_text_meta,
+ InvertedIndexReaderType::FULLTEXT);
+ DebugPoints::instance()->clear();
+ config::enable_debug_points = debug_points_were_enabled;
+ assert_ok(io::global_local_filesystem()->file_size(
+
InvertedIndexDescriptor::get_index_file_path_v2(std::string(kTestDir) + "/" +
+
std::string(name)),
+ file_size));
+
+ std::vector<float> scores;
+ for (const uint32_t docid : beta_docs) {
+ QueryExecution execution;
+ execution.context->collection_statistics =
+ std::make_shared<FixedCollectionStatistics>();
+ execution.context->collection_similarity =
std::make_shared<CollectionSimilarity>();
+ EXPECT_EQ(raw(*reader, execution, "beta",
InvertedIndexQueryType::MATCH_ANY_QUERY),
+ beta_docs);
+
scores.push_back(score_for_doc(*execution.context->collection_similarity,
docid));
+ }
+ return scores;
+ };
+
+ int64_t dense_size = 0;
+ int64_t sparse_size = 0;
+ const std::vector<float> dense = scores_of(/*sparse=*/false,
"dense_norms", &dense_size);
+ const std::vector<float> sparse = scores_of(/*sparse=*/true,
"sparse_norms", &sparse_size);
+ EXPECT_LT(sparse_size, dense_size);
+ ASSERT_EQ(dense.size(), beta_docs.size());
+ // Rows 0 and 2 have lengths 2 and 4, so the norms do move the scores.
+ EXPECT_NE(dense[0], dense[1]);
+ EXPECT_EQ(sparse, dense);
+}
+
} // namespace
} // namespace doris::segment_v2
diff --git a/gensrc/proto/snii.proto b/gensrc/proto/snii.proto
index 08f11b9fbc3..c47d74cbd0c 100644
--- a/gensrc/proto/snii.proto
+++ b/gensrc/proto/snii.proto
@@ -106,6 +106,10 @@ message SniiSectionRefsPB {
optional SniiRegionRefPB null_bitmap = 3;
optional SniiRegionRefPB bsbf = 4;
optional SniiRegionRefPB norms = 5;
+ // Norms of the non-NULL rows only; an index sets at most one of norms and
sparse_norms.
+ // Readers that predate this field see an index without norms: filtering
still works and
+ // scoring is refused.
+ optional SniiRegionRefPB sparse_norms = 6;
}
// Stores the gram scheme used to write the index, so readers can split query
// patterns the same way. Auto mode is resolved before serialization.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]