Coverage Report

Created: 2026-08-18 10:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/compaction/collection_similarity.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 <gen_cpp/Opcodes_types.h>
21
22
#include "core/column/column.h"
23
#include "storage/segment/common.h"
24
25
namespace doris {
26
27
using ScoreMap = phmap::flat_hash_map<segment_v2::rowid_t, float>;
28
using ScoreMapIterator = ScoreMap::const_iterator;
29
30
enum class OrderType {
31
    ASC,
32
    DESC,
33
};
34
35
struct ScoreRangeFilter {
36
    TExprOpcode::type op;
37
    double threshold;
38
39
430k
    bool pass(float score) const {
40
430k
        return (op == TExprOpcode::GT) ? (score > threshold) : (score >= threshold);
41
430k
    }
42
};
43
using ScoreRangeFilterPtr = std::shared_ptr<ScoreRangeFilter>;
44
45
class CollectionSimilarity {
46
public:
47
163
    CollectionSimilarity() { _bm25_scores.reserve(1024); }
48
163
    ~CollectionSimilarity() = default;
49
50
    void collect(segment_v2::rowid_t row_id, float score);
51
52
    // Hands the collected scores over to the caller and leaves this instance empty.
53
    // A reader that computes per-document scores inside its own query() cannot return them
54
    // through the query API, so it publishes them into a throwaway CollectionSimilarity that the
55
    // caller then relocates into a scorer. Moving instead of copying keeps that hand-off free of
56
    // a full rehash of a map that can hold one entry per matched row.
57
4
    ScoreMap release_scores() {
58
4
        ScoreMap released = std::move(_bm25_scores);
59
        // A moved-from flat_hash_map is valid but unspecified, not guaranteed empty, so make the
60
        // "leaves this instance empty" half of the contract true rather than merely likely.
61
4
        _bm25_scores.clear();
62
4
        return released;
63
4
    }
64
65
    void get_bm25_scores(roaring::Roaring* row_bitmap, IColumn::MutablePtr& scores,
66
                         std::unique_ptr<std::vector<uint64_t>>& row_ids,
67
                         const ScoreRangeFilterPtr& filter = nullptr) const;
68
69
    void get_topn_bm25_scores(roaring::Roaring* row_bitmap, IColumn::MutablePtr& scores,
70
                              std::unique_ptr<std::vector<uint64_t>>& row_ids, OrderType order_type,
71
                              size_t top_k, const ScoreRangeFilterPtr& filter = nullptr) const;
72
73
private:
74
    template <OrderType order>
75
    void find_top_k_scores(const roaring::Roaring* row_bitmap, const ScoreMap& all_scores,
76
                           size_t top_k, std::vector<std::pair<uint32_t, float>>& top_k_results,
77
                           const ScoreRangeFilterPtr& filter) const;
78
79
    ScoreMap _bm25_scores;
80
};
81
using CollectionSimilarityPtr = std::shared_ptr<CollectionSimilarity>;
82
83
} // namespace doris