Coverage Report

Created: 2026-06-27 19:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/vcolumn_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
#include <atomic>
20
21
#include "common/exception.h"
22
#include "exprs/function/function.h"
23
#include "exprs/lambda_function/lambda_execution_context.h"
24
#include "exprs/vexpr.h"
25
#include "exprs/vexpr_context.h"
26
#include "runtime/descriptors.h"
27
#include "runtime/runtime_state.h"
28
29
namespace doris {
30
class VColumnRef final : public VExpr {
31
    ENABLE_FACTORY_CREATOR(VColumnRef);
32
33
public:
34
    //this is different of slotref is using slot_id find a column_id
35
    //slotref: need to find the equal id in tuple, then return column_id, the plan of FE is very important
36
    //columnref: is columnid = slotid, not used to find, so you should know this column placed in block
37
    VColumnRef(const TExprNode& node)
38
1.37k
            : VExpr(node),
39
1.37k
              _column_id(node.column_ref.column_id),
40
1.37k
              _column_name(node.column_ref.column_name) {}
41
42
1.37k
    Status prepare(RuntimeState* state, const RowDescriptor& desc, VExprContext* context) override {
43
1.37k
        RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context));
44
1.37k
        DCHECK_EQ(_children.size(), 0);
45
1.37k
        if (_column_id < 0) {
46
0
            return Status::InternalError(
47
0
                    "VColumnRef have invalid slot id: {}, _column_name: {}, desc: {}", _column_id,
48
0
                    _column_name, desc.debug_string());
49
0
        }
50
1.37k
        _prepare_finished = true;
51
1.37k
        return Status::OK();
52
1.37k
    }
53
54
    Status open(RuntimeState* state, VExprContext* context,
55
6.47k
                FunctionContext::FunctionStateScope scope) override {
56
6.47k
        DCHECK(_prepare_finished);
57
6.47k
        RETURN_IF_ERROR(VExpr::open(state, context, scope));
58
6.47k
        _open_finished = true;
59
6.47k
        return Status::OK();
60
6.47k
    }
61
62
    Status execute_column_impl(VExprContext* context, const Block* block, const Selector* selector,
63
13.6k
                               size_t count, ColumnPtr& result_column) const override {
64
13.6k
        DCHECK(_open_finished || block == nullptr);
65
13.6k
        const int column_position = _get_column_position(context, block);
66
13.6k
        if (column_position < 0 || column_position >= block->columns()) {
67
1
            return Status::InternalError(
68
1
                    "input block not contain column ref {}, column_id={}, gap={}, block={}",
69
1
                    _column_name, _column_id, _gap.load(), block->dump_structure());
70
1
        }
71
13.6k
        auto origin_column = block->get_by_position(column_position).column;
72
13.6k
        result_column = filter_column_with_selector(origin_column, selector, count);
73
13.6k
        return Status::OK();
74
13.6k
    }
75
76
27.2k
    DataTypePtr execute_type(const Block* block) const override {
77
27.2k
        DCHECK(_open_finished || block == nullptr);
78
27.2k
        const int column_position = _get_column_position_without_context(block);
79
27.2k
        if (column_position < 0 || column_position >= block->columns()) {
80
1
            throw doris::Exception(
81
1
                    ErrorCode::INTERNAL_ERROR,
82
1
                    "input block not contain column ref {}, column_id={}, gap={}, block={}",
83
1
                    _column_name, _column_id, _gap.load(), block->dump_structure());
84
1
        }
85
27.2k
        return block->get_by_position(column_position).type;
86
27.2k
    }
87
88
18.4k
    bool is_constant() const override { return false; }
89
90
720
    int column_id() const { return _column_id; }
91
92
16.4k
    const std::string& expr_name() const override { return _column_name; }
93
94
363
    void set_gap(int gap) { _gap = gap; }
95
96
0
    int get_gap() const { return _gap.load(); }
97
98
    bool has_gap() const { return _gap.load() >= 0; }
99
100
0
    std::string debug_string() const override {
101
0
        std::stringstream out;
102
0
        out << "VColumnRef(slot_id: " << _column_id << ",column_name: " << _column_name
103
0
            << VExpr::debug_string() << ")";
104
0
        return out.str();
105
0
    }
106
107
67
    double execute_cost() const override { return 0.0; }
108
109
private:
110
13.6k
    int _get_column_position(VExprContext* context, const Block* block) const {
111
13.6k
        if (context != nullptr) {
112
13.6k
            const auto resolve_result =
113
13.6k
                    context->lambda_execution_context().resolve_column_position(_column_name);
114
13.6k
            if (resolve_result.found) {
115
2.71k
                return resolve_result.column_position;
116
2.71k
            }
117
10.9k
            if (resolve_result.searched_named_scope) {
118
0
                return -1;
119
0
            }
120
10.9k
        }
121
10.9k
        return _get_column_position_without_context(block);
122
13.6k
    }
123
124
38.1k
    int _get_column_position_without_context(const Block* block) const {
125
38.1k
        if (_gap.load() == -1) {
126
5.44k
            return _find_column_position_by_name(block);
127
5.44k
        }
128
32.7k
        return _column_id + _gap.load();
129
38.1k
    }
130
131
5.44k
    int _find_column_position_by_name(const Block* block) const {
132
5.44k
        if (block == nullptr) {
133
0
            return -1;
134
0
        }
135
5.52k
        for (int position = block->columns() - 1; position >= 0; --position) {
136
5.52k
            if (block->get_by_position(position).name == _column_name) {
137
5.44k
                return position;
138
5.44k
            }
139
5.52k
        }
140
0
        return -1;
141
5.44k
    }
142
143
    int _column_id;
144
    std::atomic<int> _gap = -1;
145
    std::string _column_name;
146
};
147
} // namespace doris