Coverage Report

Created: 2026-09-28 19:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/index/index_query_context.h
Line
Count
Source
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 <memory>
21
#include <roaring/roaring.hh>
22
23
#include "storage/compaction/collection_similarity.h"
24
#include "storage/index/inverted/similarity/collection_statistics.h"
25
26
namespace doris::segment_v2 {
27
28
struct IndexQueryContext {
29
    io::IOContext* io_ctx = nullptr;
30
    OlapReaderStatistics* stats = nullptr;
31
    RuntimeState* runtime_state = nullptr;
32
33
    CollectionStatisticsPtr collection_statistics;
34
    CollectionSimilarityPtr collection_similarity;
35
    std::shared_ptr<const roaring::Roaring> delete_bitmap;
36
37
    size_t query_limit = 0;
38
    bool is_asc = false;
39
40
    // G02 count-only fast-path handshake. Set by SegmentIterator ONLY while it
41
    // evaluates the single pushed-down MATCH predicate of a COUNT_ON_INDEX scan
42
    // whose row space is provably unfiltered (no deletes, no other conjuncts,
43
    // full row bitmap, no row-id consumers -- see count_on_index_fastpath.h),
44
    // and reset immediately after. When set, an index reader MAY answer the
45
    // query with a bitmap whose CARDINALITY equals the match count without the
46
    // row ids being real (SNII returns [0, df) straight from dict-entry df,
47
    // skipping the posting decode). Readers must never cache such a bitmap
48
    // under a key a row-accurate query could hit.
49
    bool count_on_index_fastpath = false;
50
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
64
    // ---- Reply direction: fields a READER writes and the CALLER reads back ----
65
    //
66
    // A caller that hands a reader a COPY of this context rather than the context itself must
67
    // fold the copy back with merge_reader_outputs(), or the reader's reply is dropped in
68
    // silence: nothing fails to compile, no test goes red, the query simply takes the wrong plan.
69
    // FunctionSearch's SNII leaf builder is such a caller -- it copies the context so the reader
70
    // publishes its BM25 into a throwaway CollectionSimilarity instead of the query's own.
71
    //
72
    // Every field added below this line must also be merged in merge_reader_outputs().
73
74
    // G03 reply direction of the same handshake. Set by a reader iff it DID
75
    // answer with such a fabricated count bitmap (never on a query-cache hit,
76
    // a single-flight shared result, or any row-accurate decode). Read and
77
    // reset by SegmentIterator right after the index apply; a true value is
78
    // the precondition for the count-emission shortcut that materializes the
79
    // remaining count as default rows without iterating the row bitmap.
80
    bool count_on_index_fastpath_hit = false;
81
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
91
    // Folds the reply-direction fields a reader wrote on a copy of this context back into it.
92
    // Latching (never clearing) is what makes this safe to call for each of several readers.
93
4
    void merge_reader_outputs(const IndexQueryContext& reader_context) {
94
4
        count_on_index_fastpath_hit |= reader_context.count_on_index_fastpath_hit;
95
4
        candidate_rows_consumed |= reader_context.candidate_rows_consumed;
96
4
    }
97
};
98
using IndexQueryContextPtr = std::shared_ptr<IndexQueryContext>;
99
100
} // namespace doris::segment_v2