Skip to content

Commit 47b2e1d

Browse files
authored
[improvement](inverted index) Push the candidate row bitmap down into phrase queries (apache#67180)
### What problem does this PR solve? Problem Summary: The cost of a phrase-family inverted index query (MATCH_PHRASE / MATCH_PHRASE_PREFIX multi-term path) is proportional to the whole segment's postings and positions, regardless of how small the surviving candidate set already is. On a production log table a 9-minute time window left only 22.4% of each segment's rows after short-key pruning, yet every phrase conjunct still walked the full segment: per-query profile showed 2,657 segments, 11,913s of InvertedIndexSearcherSearchExecTime (99.7% of scan cost) and 125 GiB of index reads for a query whose final result was 0 rows. A microbenchmark against the same code path calibrates the cost model to ~0.9µs per co-occurrence candidate, so evaluation cost tracks candidates, not results. Fix: expose the scan's current candidate row bitmap to index queries through IndexQueryContext (the same SegmentIterator -> reader handshake channel the count-on-index fast path already uses): - `IndexQueryContext::candidate_rows`: set by SegmentIterator around the index-apply phase when the candidate bitmap is smaller than `num_rows * inverted_index_candidate_pushdown_ratio` (new BE config, default 0.3). Non-positive finite values disable the feature; values above 1 or non-finite values are rejected by the config validator. The use site checks the domain before arithmetic. The pointer is reset on every exit path via the existing DEFER. `_row_bitmap` only shrinks during the applies, so restricting to its current state stays correct for every later conjunct. - `RoaringDocIdIterator`: a read-only DISI adapter over the candidate bitmap. PhraseQuery joins it into the leapfrog intersection (its doc_freq() is the cardinality, so a small candidate naturally becomes the lead), while matchers keep only real term iterators -- a classic two-phase iterator: candidates drive the approximation, terms keep the position semantics. The single-term path is unchanged (no restriction, same semantics). - Query cache: only a query that actually consumes the candidate produces a partial bitmap and skips cache insertion. Cache lookups stay enabled -- a cached full-segment bitmap intersected later is still correct and cheaper. Non-consuming queries continue filling the cache (see review round 2/3). SNII indexes take the same candidates (added after the CLucene path above): - The snii::query phrase and phrase-prefix entry points accept a Roaring candidate bitmap. After term planning, a candidate set within 8x of the rarest term drives the docid intersection through SNII's existing prefilter path, so windowed postings are decoded only for windows covering candidates; a far larger candidate set filters the intersection result before PRX decode, so a rare phrase never pays for every candidate. - Phrase-prefix keeps or enables its tail-union prefilter under candidates when the union is cheaper than repeated leading-term candidate probes; small candidate sets bypass it when candidate probes are cheaper. - SniiIndexReader consumes candidates only for multi-term MATCH_PHRASE and MATCH_PHRASE_PREFIX, follows the same consumption-keyed cache policy, and keeps candidate-restricted bitmaps out of single-flight. - SEARCH roots are evaluated without the candidate, like compound roots. Measured effect (`PhraseCandidatePushdownBench`, committed as a DISABLED_ UT: one synthetic log corpus indexed through the production column writer as CLucene V2 and as SNII, queried through the production readers with the result cache disabled for both lookup and insertion; RELEASE build, 1,000,000-row segment. Each speedup is the ratio of medians from three independent runs, with ten thread-CPU samples per query in each run; every point asserts restricted == full ∧ candidates): | format | query | full ms | random 1% | random 10% | random 30% | range 1% | range 10% | range 30% | |---|---|---:|---:|---:|---:|---:|---:|---:| | SNII | exact_2 | 11.470 | 2.22x | 1.56x | 1.12x | 36.53x | 8.93x | 3.27x | | SNII | exact_4 | 22.331 | 2.74x | 2.00x | 1.54x | 40.02x | 8.47x | 3.20x | | SNII | prefix_2 | 3.799 | 1.28x | 1.47x | 1.11x | 11.73x | 3.45x | 2.15x | | SNII | prefix_5 | 1.634 | 3.01x | 2.28x | 2.11x | 3.77x | 2.94x | 1.95x | | SNII | rare_exact | 0.150 | 1.12x | 0.95x | 1.10x | 1.00x | 1.01x | 1.01x | | V2 | exact_2 | 18.606 | 3.02x | 1.70x | 1.06x | 64.16x | 7.72x | 2.92x | | V2 | exact_4 | 21.953 | 2.90x | 1.59x | 1.12x | 65.14x | 9.30x | 3.19x | | V2 | prefix_2 | 4.584 | 3.14x | 1.54x | 1.15x | 23.39x | 7.58x | 3.11x | | V2 | prefix_5 | 1.529 | 2.78x | 1.40x | 1.18x | 6.51x | 4.09x | 2.14x | | V2 | rare_exact | 0.064 | 1.14x | 0.98x | 1.07x | 1.02x | 1.02x | 1.00x | `range` candidates model a time range over time-sorted rows, `random` a selective non-index predicate. The exact and prefix queries improved at the 0.3 default in this corpus; the rare exact query stayed near break-even. The previous V2 rare-query speedup included a cache insertion that should have been disabled. The machine is shared, so small differences near 1x can move between runs. The earlier single-shape table (200k-doc CLucene segment, phrase `big red`) is superseded by this reproducible benchmark: BUILD_TYPE_UT=RELEASE PHRASE_CANDIDATE_BENCH_DOCS=1000000 \ PHRASE_CANDIDATE_BENCH_ITERATIONS=10 GTEST_ALSO_RUN_DISABLED_TESTS=1 \ ./run-be-ut.sh --run \ --filter='PhraseCandidatePushdownBench.DISABLED_RestrictedVersusFullPhrase' -j <N> ### Release note Inverted index: phrase and phrase-prefix queries on CLucene and SNII indexes now restrict doc-list intersection and position verification to the scan's surviving candidate rows when the candidate set is small (BE config inverted_index_candidate_pushdown_ratio, default 0.3). ### Check List (For Author) - Test <!-- At least one of them must be included. --> - [x] Regression test - [x] Unit Test - [ ] Manual test (add detailed scripts or steps below) - [ ] No need to test or manual test. Explain why: - [ ] This is a refactor/code format and no logic has been changed. - [ ] Previous test can cover this change. - [ ] No code files have been changed. - [ ] Other reason <!-- Add your reason? --> - Behavior changed: - [ ] No. - [x] Yes. <!-- Same query results; phrase evaluation cost now tracks the candidate set. Only candidate-restricted phrase results skip fulltext query-cache insertion. --> - Does this need documentation? - [x] No. - [ ] Yes. --- Review round 1 (automated review, addressed in the second commit): 1. Scoring norm source: `_norm_source` is pinned to the first real postings iterator, so BM25 norms never come from the candidate adapter. Red-first test: under a candidate, two documents of different lengths scored identically (2.9729729) before the fix vs. the correct distinct baseline scores after it. 2. Engage-gate timing: the decision is extracted into `_refresh_candidate_pushdown()` and re-evaluated after column-level index predicates, so an entry bitmap above the ratio that indexed predicates shrink below it still publishes `candidate_rows` (threshold-crossing test added). 3. Config domain safety: validator (finite, <= 1.0) plus a use-site `std::isfinite`/domain guard before the multiply and integer conversion. 4. Default calibration: 0.1 -> 0.3. The motivating workload keeps 22.4% of each segment after short-key pruning; the previous default never engaged the pushdown for exactly that case. A finer crossover sweep is planned as a follow-up benchmark. Review round 2/3 (automated review, addressed in the follow-up commits): 5. Cache policy is now keyed to actual candidate consumption: a reply-direction `candidate_rows_consumed` flag is set only by the candidate-joining phrase path and reset per search, so non-consuming queries (MATCH_ANY/ALL, term, regexp, single-term phrase, untokenized range) keep filling the query cache with their full-segment bitmaps while a candidate is engaged. Red-first tests: cold-miss/second-hit for MATCH_ANY under a published candidate, plus a cross-reader stale-flag case (phrase then untokenized range). 6. Three-valued correctness: a candidate-restricted TRUE bitmap paired with the full-segment null bitmap could spuriously trigger VCompoundPred's AND shortcut (NOT(A AND B) with nullable A: FALSE mis-typed as NULL, row dropped). Compound roots are now evaluated with the candidate suppressed in both the conjunct and virtual-column loops; top-level single-predicate consumption keeps the restriction (exact within the candidate). A SQL-level nullable-compound regression case is planned as a follow-up. 7. The `_norm_source` invariant check moved out of the per-document scoring loop (release-mode checks stay out of hot loops per AGENTS.md). Update (SNII support, benchmark, regression test; rebased onto current master): 8. Rebase fix: on current master `segment_iterator_candidate_pushdown_test.cpp` no longer expands `#define protected public` (the segment headers arrive through column_reader.h first), so -Wunused-macros failed the BE UT build; the unused macro is removed. 9. SNII candidate pushdown, as described above. Red-first unit tests: restricted SNII phrases returned the full-segment result, consumed nothing, were cached and shared through single-flight; a dense candidate set turned off the phrase-prefix tail-union prefilter (10,156 vs 1,143 leading candidates decoded). The benchmark then drove two refinements: filtering instead of driving when candidates vastly outnumber the rarest term (no O(candidates) cost for rare phrases) and retaining affordable tail-union prefilters under candidates. 10. Follow-ups from earlier rounds are done: the crossover benchmark above, and the SQL-level nullable-compound case in `test_phrase_candidate_pushdown` (V2 and SNII tables, pushdown on, off, and on again over cached full-segment results, plus a merge-on-write table with deletes). 11. Review of the SNII change: candidate suppression now checks the whole expression tree, so a compound predicate or SEARCH below another root (for example `search(...) IS NOT NULL`) no longer runs its leaves under the candidate (red-first test); the SNII streaming exact verifier and the repeated-term route, which fail loudly on a leftover non-candidate doc, are now exercised with driving and filtering candidates. 12. CLucene full-result cache insertions now use the existing option-aware helper in both fulltext and string readers. A disabled-cache test failed before the fix because a later enabled query hit the entry from the disabled run; it passed afterward. This also makes V2 benchmark samples symmetric when the result cache is disabled. 13. The symmetric benchmark exposed an SNII phrase-prefix regression at sparse random candidates. A profile test showed 1,998 leading candidates under a 10% candidate set versus 1,143 with the unrestricted tail-union prefilter. The planner now retains that prefilter when its postings cost no more than bounded candidate probes; the profile test passed, and three RELEASE runs moved `prefix_5` at 1% random candidates from 0.19–0.29x to 2.93–3.04x. The one-candidate bypass test still passes.
1 parent ef72b93 commit 47b2e1d

35 files changed

Lines changed: 4077 additions & 307 deletions

‎be/src/common/config.cpp‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
// IWYU pragma: no_include <bthread/errno.h>
2525
#include <lz4/lz4hc.h>
2626

27+
#include <atomic>
2728
#include <cerrno> // IWYU pragma: keep
29+
#include <cmath>
2830
#include <cstdlib>
2931
#include <cstring>
3032
#include <fstream> // IWYU pragma: keep
@@ -1320,6 +1322,17 @@ DEFINE_Bool(enable_inverted_index_cache_check_timestamp, "true");
13201322
DEFINE_mBool(enable_inverted_index_correct_term_write, "true");
13211323
DEFINE_Int32(inverted_index_fd_number_limit_percent, "20"); // 20%
13221324
DEFINE_Int32(inverted_index_query_cache_shards, "256");
1325+
DEFINE_mDouble(inverted_index_candidate_pushdown_ratio, "0.3");
1326+
DEFINE_Validator(inverted_index_candidate_pushdown_ratio,
1327+
[](const double v) -> bool { return std::isfinite(v) && v <= 1.0; });
1328+
static std::atomic<double> published_inverted_index_candidate_pushdown_ratio {0.0};
1329+
DEFINE_ON_UPDATE(inverted_index_candidate_pushdown_ratio, [](double, double value) {
1330+
published_inverted_index_candidate_pushdown_ratio.store(value);
1331+
});
1332+
1333+
double get_inverted_index_candidate_pushdown_ratio() {
1334+
return published_inverted_index_candidate_pushdown_ratio.load();
1335+
}
13231336

13241337
// inverted index match bitmap cache size
13251338
DEFINE_String(inverted_index_query_cache_limit, "10%");
@@ -2321,6 +2334,8 @@ bool init(const char* conf_file, bool fill_conf_map, bool must_exist, bool set_t
23212334
SET_FIELD(it.second, std::vector<double>, fill_conf_map, set_to_default);
23222335
SET_FIELD(it.second, std::vector<std::string>, fill_conf_map, set_to_default);
23232336
}
2337+
published_inverted_index_candidate_pushdown_ratio.store(
2338+
inverted_index_candidate_pushdown_ratio);
23242339

23252340
// Emit a warning for every key present in the conf file that does not correspond to a
23262341
// registered BE config field. Such keys (typos or configs removed in a newer version)
@@ -2369,14 +2384,18 @@ bool init(const char* conf_file, bool fill_conf_map, bool must_exist, bool set_t
23692384
(FIELD).name, new_value); \
23702385
} \
23712386
} \
2387+
if (PERSIST) { \
2388+
Status persist_status = persist_config(std::string((FIELD).name), VALUE); \
2389+
if (!persist_status.ok()) { \
2390+
ref_conf_value = old_value; \
2391+
return persist_status; \
2392+
} \
2393+
} \
23722394
if (full_conf_map != nullptr) { \
23732395
std::ostringstream oss; \
23742396
oss << new_value; \
23752397
(*full_conf_map)[(FIELD).name] = oss.str(); \
23762398
} \
2377-
if (PERSIST) { \
2378-
RETURN_IF_ERROR(persist_config(std::string((FIELD).name), VALUE)); \
2379-
} \
23802399
if (RegisterConfUpdateCallback::_s_field_update_callback != nullptr) { \
23812400
auto callback_it = \
23822401
RegisterConfUpdateCallback::_s_field_update_callback->find((FIELD).name); \
@@ -2394,7 +2413,7 @@ Status persist_config(const std::string& field, const std::string& value) {
23942413
// lock to make sure only one thread can modify the be_custom.conf
23952414
std::lock_guard<std::mutex> l(custom_conf_lock);
23962415

2397-
static const std::string conffile = config::custom_config_dir + "/be_custom.conf";
2416+
const std::string conffile = config::custom_config_dir + "/be_custom.conf";
23982417

23992418
Properties tmp_props;
24002419
if (!tmp_props.load(conffile.c_str(), false)) {
@@ -2418,16 +2437,14 @@ Status set_config(const std::string& field, const std::string& value, bool need_
24182437
"'{}' is not support to modify", field);
24192438
}
24202439

2440+
// Keep the value, config map, and callback in the same update order.
2441+
std::lock_guard<std::mutex> lock(mutable_string_config_lock);
24212442
UPDATE_FIELD(it->second, value, bool, need_persist);
24222443
UPDATE_FIELD(it->second, value, int16_t, need_persist);
24232444
UPDATE_FIELD(it->second, value, int32_t, need_persist);
24242445
UPDATE_FIELD(it->second, value, int64_t, need_persist);
24252446
UPDATE_FIELD(it->second, value, double, need_persist);
2426-
{
2427-
// add lock to ensure thread safe
2428-
std::lock_guard<std::mutex> lock(mutable_string_config_lock);
2429-
UPDATE_FIELD(it->second, value, std::string, need_persist);
2430-
}
2447+
UPDATE_FIELD(it->second, value, std::string, need_persist);
24312448

24322449
// The other types are not thread safe to change dynamically.
24332450
return Status::Error<ErrorCode::NOT_IMPLEMENTED_ERROR, false>(

‎be/src/common/config.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,12 @@ DECLARE_Bool(enable_inverted_index_cache_check_timestamp);
13711371
DECLARE_mBool(enable_inverted_index_correct_term_write);
13721372
DECLARE_Int32(inverted_index_fd_number_limit_percent); // 50%
13731373
DECLARE_Int32(inverted_index_query_cache_shards);
1374+
// When the candidate row bitmap of a segment scan is smaller than
1375+
// num_rows * this ratio, it is pushed down into inverted index queries so
1376+
// doc-list intersection and verification run only over the candidates
1377+
// (see IndexQueryContext::candidate_rows). <= 0 disables the pushdown.
1378+
DECLARE_mDouble(inverted_index_candidate_pushdown_ratio);
1379+
double get_inverted_index_candidate_pushdown_ratio();
13741380

13751381
// inverted index match bitmap cache size
13761382
DECLARE_String(inverted_index_query_cache_limit);

‎be/src/storage/index/index_query_context.h‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ struct IndexQueryContext {
4848
// under a key a row-accurate query could hit.
4949
bool count_on_index_fastpath = false;
5050

51+
// Candidate-pushdown handshake. Set by SegmentIterator ONLY while it
52+
// evaluates pushed-down conjuncts and the current candidate row bitmap is
53+
// small enough (config::inverted_index_candidate_pushdown_ratio), reset
54+
// right after. When set, an index query MAY restrict doc-list intersection
55+
// and verification to this candidate set (a CLucene PhraseQuery joins it
56+
// into its leapfrog; an SNII phrase restricts its docid intersection to
57+
// it). A bitmap produced under a non-null candidate is PARTIAL and must
58+
// never be inserted into the query cache or shared with other scanners;
59+
// cache lookups stay valid (a cached full-segment bitmap intersected later
60+
// is still correct). The pointee is owned by the caller and outlives the
61+
// evaluation.
62+
const roaring::Roaring* candidate_rows = nullptr;
63+
5164
// ---- Reply direction: fields a READER writes and the CALLER reads back ----
5265
//
5366
// A caller that hands a reader a COPY of this context rather than the context itself must
@@ -66,10 +79,20 @@ struct IndexQueryContext {
6679
// remaining count as default rows without iterating the row bitmap.
6780
bool count_on_index_fastpath_hit = false;
6881

82+
// Reply direction of the candidate handshake. Set by a query iff it DID
83+
// join candidate_rows into its evaluation (a multi-term phrase on CLucene
84+
// or SNII), i.e. its result bitmap is partial; reset by the reader before
85+
// each search.
86+
// Only such a partial result must stay out of the query cache -- a query
87+
// that never consumes the candidate (MATCH_ANY/ALL, term, regexp, single
88+
// term phrase) still computes the full-segment bitmap and stays cacheable.
89+
bool candidate_rows_consumed = false;
90+
6991
// Folds the reply-direction fields a reader wrote on a copy of this context back into it.
7092
// Latching (never clearing) is what makes this safe to call for each of several readers.
7193
void merge_reader_outputs(const IndexQueryContext& reader_context) {
7294
count_on_index_fastpath_hit |= reader_context.count_on_index_fastpath_hit;
95+
candidate_rows_consumed |= reader_context.candidate_rows_consumed;
7396
}
7497
};
7598
using IndexQueryContextPtr = std::shared_ptr<IndexQueryContext>;

‎be/src/storage/index/inverted/inverted_index_reader.cpp‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ Status InvertedIndexReader::match_index_search(
347347
context->runtime_state->query_options().inverted_index_compatible_read) {
348348
reader->setCompatibleRead(true);
349349
}
350+
// Fresh per-search reply: only the query about to run decides whether it
351+
// consumes the candidate set (and thus produces an uncacheable partial
352+
// result); a consumed flag left by an earlier search must not leak in.
353+
context->candidate_rows_consumed = false;
350354
try {
351355
SCOPED_RAW_TIMER(&context->stats->inverted_index_searcher_search_timer);
352356
auto query = QueryFactory::create(query_type, index_searcher, context);
@@ -477,7 +481,13 @@ Status FullTextIndexReader::query(const IndexQueryContextPtr& context,
477481
RETURN_IF_ERROR(match_index_search(context, query_type, query_info, *searcher_ptr,
478482
term_match_bitmap));
479483
term_match_bitmap->runOptimize();
480-
cache->insert(cache_key, term_match_bitmap, &cache_handler);
484+
// Only a bitmap whose query actually joined the candidate set is
485+
// partial and must stay out of the cache; a non-consuming query
486+
// (MATCH_ANY/ALL, term, regexp, single-term phrase) computed the
487+
// full-segment result even while candidate_rows was published.
488+
if (!context->candidate_rows_consumed) {
489+
insert_query_cache(context, cache, cache_key, term_match_bitmap, &cache_handler);
490+
}
481491
bit_map = term_match_bitmap;
482492
}
483493
return Status::OK();
@@ -543,6 +553,10 @@ Status StringTypeInvertedIndexReader::query(const IndexQueryContextPtr& context,
543553
query_info.field_name = column_name_ws;
544554
query_info.term_infos.emplace_back(search_str, 0);
545555

556+
// Fresh per-search reply (the range-query cases below never pass
557+
// through match_index_search, so a stale consumed flag from an
558+
// earlier fulltext search must be cleared here too).
559+
context->candidate_rows_consumed = false;
546560
auto result = std::make_shared<roaring::Roaring>();
547561
FulltextIndexSearcherPtr* searcher_ptr = nullptr;
548562
InvertedIndexCacheHandle inverted_index_cache_handle;
@@ -601,9 +615,11 @@ Status StringTypeInvertedIndexReader::query(const IndexQueryContextPtr& context,
601615
"invalid query type when query untokenized inverted index");
602616
}
603617
}
604-
// add to cache
618+
// add to cache (unless a candidate-consuming query made it partial)
605619
result->runOptimize();
606-
cache->insert(cache_key, result, &cache_handler);
620+
if (!context->candidate_rows_consumed) {
621+
insert_query_cache(context, cache, cache_key, result, &cache_handler);
622+
}
607623

608624
bit_map = result;
609625
return Status::OK();

‎be/src/storage/index/inverted/query/phrase_query.cpp‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ void PhraseQuery::add(const InvertedIndexQueryInfo& query_info) {
5353
init_ordered_sloppy_phrase_matcher(query_info, is_similarity);
5454
}
5555

56+
// A missing required posting makes the phrase empty for the entire segment.
57+
if (_context->candidate_rows != nullptr &&
58+
std::ranges::none_of(_iterators,
59+
[](const DISI& iter) { return visit_node(iter, DocFreq {}) == 0; })) {
60+
_iterators.emplace_back(std::make_shared<RoaringDocIdIterator>(_context->candidate_rows));
61+
_context->candidate_rows_consumed = true;
62+
}
63+
5664
std::sort(_iterators.begin(), _iterators.end(), [](const DISI& a, const DISI& b) {
5765
int64_t freq1 = visit_node(a, DocFreq {});
5866
int64_t freq2 = visit_node(b, DocFreq {});
@@ -64,6 +72,12 @@ void PhraseQuery::add(const InvertedIndexQueryInfo& query_info) {
6472
for (int32_t i = 2; i < _iterators.size(); i++) {
6573
_others.emplace_back(&_iterators[i]);
6674
}
75+
for (auto& iter : _iterators) {
76+
if (const auto* term_iter = std::get_if<TermPositionsIterPtr>(&iter)) {
77+
_norm_source = term_iter->get();
78+
break;
79+
}
80+
}
6781

6882
init_similarities(query_info.field_name, is_similarity);
6983
}
@@ -160,6 +174,11 @@ void PhraseQuery::search(roaring::Roaring& roaring) {
160174
}
161175

162176
void PhraseQuery::search_by_skiplist(roaring::Roaring& roaring) {
177+
if (_phrase_similarity) {
178+
// _norm_source is fixed once in add(); validate it before the loop so
179+
// the per-document path below stays free of release-mode checks.
180+
DORIS_CHECK(_norm_source != nullptr);
181+
}
163182
int32_t doc = 0;
164183
while ((doc = do_next(visit_node(*_lead1, NextDoc {}))) != INT32_MAX) {
165184
if (_phrase_similarity) {
@@ -168,7 +187,7 @@ void PhraseQuery::search_by_skiplist(roaring::Roaring& roaring) {
168187
continue;
169188
}
170189
roaring.add(doc);
171-
int32_t norm = visit_node(*_lead1, Norm {});
190+
int32_t norm = _norm_source->norm();
172191
float score = _phrase_similarity->score(phrase_freq, static_cast<int64_t>(norm));
173192

174193
_context->collection_similarity->collect(doc, score);
@@ -285,4 +304,4 @@ void PhraseQuery::parser_info(OlapReaderStatistics* stats, std::string& query,
285304
}
286305
}
287306

288-
} // namespace doris::segment_v2
307+
} // namespace doris::segment_v2

‎be/src/storage/index/inverted/query/phrase_query.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ class PhraseQuery : public Query {
7474

7575
DISI* _lead1 = nullptr;
7676
DISI* _lead2 = nullptr;
77+
// Norm source for scoring: only an exact term iterator owns per-document
78+
// norms. Candidate and multi-term union iterators remain approximations.
79+
TermPositionsIterator* _norm_source = nullptr;
7780
std::vector<DISI*> _others;
7881
std::vector<DISI> _iterators;
7982

@@ -82,4 +85,4 @@ class PhraseQuery : public Query {
8285
SimilarityPtr _phrase_similarity;
8386
};
8487

85-
} // namespace doris::segment_v2
88+
} // namespace doris::segment_v2

‎be/src/storage/index/inverted/util/docid_set_iterator.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121

2222
#include "common/exception.h"
2323
#include "storage/index/inverted/util/mock_iterator.h"
24+
#include "storage/index/inverted/util/roaring_docid_iterator.h"
2425
#include "storage/index/inverted/util/union_term_iterator.h"
2526

2627
namespace doris::segment_v2 {
2728

28-
using DISI = std::variant<TermPositionsIterPtr, UnionTermIterPtr, MockIterPtr>;
29+
using DISI = std::variant<TermPositionsIterPtr, UnionTermIterPtr, MockIterPtr, RoaringDocIdIterPtr>;
2930

3031
template <typename DISIType, typename Func, typename... Args>
3132
auto visit_node(DISIType&& disi, Func&& func, Args&&... args) {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#pragma once
19+
20+
#include <climits>
21+
#include <cstdint>
22+
#include <memory>
23+
24+
#include "roaring/roaring.hh"
25+
26+
namespace doris::segment_v2 {
27+
28+
// Read-only DISI adapter over a candidate row bitmap
29+
// (IndexQueryContext::candidate_rows). Joining the leapfrog intersection of
30+
// PhraseQuery, it restricts doc-list intersection and position verification to
31+
// the candidate set (two-phase evaluation: this iterator drives the
32+
// approximation, real term iterators keep the position semantics). It never
33+
// joins a matcher's postings, so freq()/next_position()/norm() only satisfy
34+
// the DISI interface with neutral values.
35+
//
36+
// The underlying bitmap is NOT owned and must outlive the iterator; leapfrog
37+
// only moves forward, so advance() targets are monotonically non-decreasing.
38+
class RoaringDocIdIterator {
39+
public:
40+
explicit RoaringDocIdIterator(const roaring::Roaring* rows)
41+
: _rows(rows), _iter(rows->begin()) {}
42+
43+
// DISI convention: an iterator starts positioned BEFORE its first doc
44+
// (search_by_skiplist opens with a NextDoc on the lead), so doc_id() is -1
45+
// until the first next_doc()/advance() moves onto a real position.
46+
int32_t doc_id() const {
47+
if (!_started) {
48+
return -1;
49+
}
50+
return _iter == _rows->end() ? INT_MAX : static_cast<int32_t>(*_iter);
51+
}
52+
53+
int32_t freq() const { return 1; }
54+
55+
int32_t next_doc() {
56+
if (!_started) {
57+
_started = true;
58+
} else if (_iter != _rows->end()) {
59+
++_iter;
60+
}
61+
return doc_id();
62+
}
63+
64+
int32_t advance(int32_t target) {
65+
_started = true;
66+
if (target > 0) {
67+
_iter.equalorlarger(static_cast<uint32_t>(target));
68+
}
69+
return doc_id();
70+
}
71+
72+
int32_t doc_freq() const {
73+
uint64_t cardinality = _rows->cardinality();
74+
return cardinality > INT_MAX ? INT_MAX : static_cast<int32_t>(cardinality);
75+
}
76+
77+
int32_t next_position() { return 0; }
78+
79+
int32_t norm() const { return 1; }
80+
81+
private:
82+
const roaring::Roaring* _rows;
83+
roaring::Roaring::const_iterator _iter;
84+
bool _started = false;
85+
};
86+
using RoaringDocIdIterPtr = std::shared_ptr<RoaringDocIdIterator>;
87+
88+
} // namespace doris::segment_v2

0 commit comments

Comments
 (0)