Coverage Report

Created: 2026-03-13 09:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/virtual_slot_ref.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 "exprs/vexpr.h"
21
22
namespace doris {
23
#include "common/compile_check_begin.h"
24
class VirtualSlotRef MOCK_REMOVE(final) : public VExpr {
25
    ENABLE_FACTORY_CREATOR(VirtualSlotRef);
26
27
public:
28
    VirtualSlotRef(const TExprNode& node);
29
    VirtualSlotRef(const SlotDescriptor* desc);
30
31
    Status prepare(RuntimeState* state, const RowDescriptor& desc, VExprContext* context) override;
32
    Status open(RuntimeState* state, VExprContext* context,
33
                FunctionContext::FunctionStateScope scope) override;
34
    Status execute_column(VExprContext* context, const Block* block, Selector* selector,
35
                          size_t count, ColumnPtr& result_column) const override;
36
    const std::string& expr_name() const override;
37
    std::string expr_label() override;
38
    std::string debug_string() const override;
39
822
    bool is_constant() const override { return false; }
40
156
    int column_id() const { return _column_id; }
41
    int slot_id() const { return _slot_id; }
42
    bool equals(const VExpr& other) override;
43
1
    size_t estimate_memory(const size_t rows) override { return 0; }
44
0
    void collect_slot_column_ids(std::set<int>& column_ids) const override {
45
0
        column_ids.insert(_column_id);
46
0
    }
47
894
    std::shared_ptr<VExpr> get_virtual_column_expr() const { return _virtual_column_expr; }
48
49
0
    Status evaluate_inverted_index(VExprContext* context, uint32_t segment_num_rows) override {
50
0
        return _virtual_column_expr->evaluate_inverted_index(context, segment_num_rows);
51
0
    }
52
53
    /*
54
    @brief SQL expression tree patterns for ANN range search optimization.
55
    
56
    Pattern 1 (should not happen):
57
    SELECT * FROM tbl WHERE distance_function(columnA, ArrayLiteral) > 100
58
    VirtualSlotRef
59
    |
60
    BINARY_PRED
61
    |---------------------------------------|
62
    |                                       |
63
    FUNCTION_CALL(l2_distance_approximate)  IntLiteral
64
    |
65
    |-----------------------|
66
    |                       |
67
    SlotRef                 ArrayLiteral
68
69
    Pattern 2 (optimizable case):
70
    SELECT distance_function(columnA, ArrayLiteral) AS dis FROM tbl WHERE dis > 100
71
    BINARY_PRED
72
    |
73
    |---------------------------------------|
74
    |                                       |
75
    VIRTUAL_SLOT_REF                        IntLiteral
76
    |
77
    FUNCTION_CALL(l2_distance_approximate)
78
    |
79
    |-----------------------|
80
    |                       |
81
    SlotRef                 ArrayLiteral
82
    */
83
84
    /**
85
     * @brief Evaluates ANN range search using index-based optimization.
86
     * 
87
     * This method implements the core logic for ANN range search optimization.
88
     * Instead of computing distances for all rows and then filtering, it uses
89
     * the ANN index to efficiently find only the rows within the specified range.
90
     * 
91
     * The method:
92
     * 1. Extracts query parameters from the range search runtime info
93
     * 2. Calls the ANN index to perform range search
94
     * 3. Updates the row bitmap with matching results
95
     * 4. Collects performance statistics
96
     * 
97
     * @param range_search_runtime Runtime info containing query vector, radius, and metrics
98
     * @param cid_to_index_iterators Vector of index iterators for each column
99
     * @param idx_to_cid Mapping from index position to column ID
100
     * @param column_iterators Vector of column iterators for data access
101
     * @param row_bitmap Output bitmap updated with matching row IDs
102
     * @param ann_index_stats Statistics collector for performance monitoring
103
     * @return Status indicating success or failure of the search operation
104
     */
105
    Status evaluate_ann_range_search(
106
            const segment_v2::AnnRangeSearchRuntime& range_search_runtime,
107
            const std::vector<std::unique_ptr<segment_v2::IndexIterator>>& cid_to_index_iterators,
108
            const std::vector<ColumnId>& idx_to_cid,
109
            const std::vector<std::unique_ptr<segment_v2::ColumnIterator>>& column_iterators,
110
            roaring::Roaring& row_bitmap, segment_v2::AnnIndexStats& ann_index_stats) override;
111
112
#ifdef BE_TEST
113
    // Test-only setter methods for unit testing
114
    void set_column_id(int column_id) { _column_id = column_id; }
115
    void set_column_name(const std::string* column_name) { _column_name = column_name; }
116
    void set_column_data_type(DataTypePtr column_data_type) {
117
        _column_data_type = std::move(column_data_type);
118
    }
119
    void set_virtual_column_expr(std::shared_ptr<VExpr> virtual_column_expr) {
120
        _virtual_column_expr = virtual_column_expr;
121
    }
122
#endif
123
124
private:
125
    int _column_id;                              ///< Column ID in the table schema
126
    int _slot_id;                                ///< Slot ID in the expression context
127
    const std::string* _column_name;             ///< Column name for debugging/logging
128
    const std::string _column_label;             ///< Column label for display purposes
129
    std::shared_ptr<VExpr> _virtual_column_expr; ///< Underlying virtual expression
130
    DataTypePtr _column_data_type;               ///< Data type of the column
131
};
132
#include "common/compile_check_end.h"
133
} // namespace doris