Coverage Report

Created: 2026-04-10 18:35

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