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.38k | : VExpr(node), |
39 | 1.38k | _column_id(node.column_ref.column_id), |
40 | 1.38k | _column_name(node.column_ref.column_name) {} |
41 | | |
42 | 1.38k | Status prepare(RuntimeState* state, const RowDescriptor& desc, VExprContext* context) override { |
43 | 1.38k | RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context)); |
44 | 1.38k | DCHECK_EQ(_children.size(), 0); |
45 | 1.38k | 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.38k | _prepare_finished = true; |
51 | 1.38k | return Status::OK(); |
52 | 1.38k | } |
53 | | |
54 | | Status open(RuntimeState* state, VExprContext* context, |
55 | 6.14k | FunctionContext::FunctionStateScope scope) override { |
56 | 6.14k | DCHECK(_prepare_finished); |
57 | 6.14k | RETURN_IF_ERROR(VExpr::open(state, context, scope)); |
58 | 6.14k | _open_finished = true; |
59 | 6.14k | return Status::OK(); |
60 | 6.14k | } |
61 | | |
62 | | Status execute_column_impl(VExprContext* context, const Block* block, const Selector* selector, |
63 | 15.6k | size_t count, ColumnPtr& result_column) const override { |
64 | 15.6k | DCHECK(_open_finished || block == nullptr); |
65 | 15.6k | const int column_position = _get_column_position(context, block); |
66 | 15.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 | 15.6k | auto origin_column = block->get_by_position(column_position).column; |
72 | 15.6k | result_column = filter_column_with_selector(origin_column, selector, count); |
73 | 15.6k | return Status::OK(); |
74 | 15.6k | } |
75 | | |
76 | 31.2k | DataTypePtr execute_type(const Block* block) const override { |
77 | 31.2k | DCHECK(_open_finished || block == nullptr); |
78 | 31.2k | const int column_position = _get_column_position_without_context(block); |
79 | 31.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 | 31.2k | return block->get_by_position(column_position).type; |
86 | 31.2k | } |
87 | | |
88 | 21.0k | bool is_constant() const override { return false; } |
89 | | |
90 | 720 | int column_id() const { return _column_id; } |
91 | | |
92 | 18.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 | Status clone_node(VExprSPtr* cloned_expr) const override { |
101 | 0 | DORIS_CHECK(cloned_expr != nullptr); |
102 | 0 | auto node = clone_texpr_node(); |
103 | 0 | TColumnRef column_ref; |
104 | 0 | column_ref.__set_column_id(_column_id); |
105 | 0 | column_ref.__set_column_name(_column_name); |
106 | 0 | node.__set_column_ref(column_ref); |
107 | 0 | auto cloned = VColumnRef::create_shared(node); |
108 | 0 | cloned->set_gap(_gap.load()); |
109 | 0 | *cloned_expr = std::move(cloned); |
110 | 0 | return Status::OK(); |
111 | 0 | } |
112 | | |
113 | 0 | std::string debug_string() const override { |
114 | 0 | std::stringstream out; |
115 | 0 | out << "VColumnRef(slot_id: " << _column_id << ",column_name: " << _column_name |
116 | 0 | << VExpr::debug_string() << ")"; |
117 | 0 | return out.str(); |
118 | 0 | } |
119 | | |
120 | 67 | double execute_cost() const override { return 0.0; } |
121 | | |
122 | | private: |
123 | 15.6k | int _get_column_position(VExprContext* context, const Block* block) const { |
124 | 15.6k | const auto resolve_result = |
125 | 15.6k | context->lambda_execution_context().resolve_column_position(_column_name); |
126 | 15.6k | if (resolve_result.found) { |
127 | 2.71k | return resolve_result.column_position; |
128 | 2.71k | } |
129 | 12.9k | if (resolve_result.searched_named_scope) { |
130 | 0 | return -1; |
131 | 0 | } |
132 | 12.9k | return _get_column_position_without_context(block); |
133 | 12.9k | } |
134 | | |
135 | 44.1k | int _get_column_position_without_context(const Block* block) const { |
136 | 44.1k | if (_gap.load() == -1) { |
137 | 5.44k | return _find_column_position_by_name(block); |
138 | 5.44k | } |
139 | 38.7k | return _column_id + _gap.load(); |
140 | 44.1k | } |
141 | | |
142 | 5.44k | int _find_column_position_by_name(const Block* block) const { |
143 | 5.52k | for (int position = block->columns() - 1; position >= 0; --position) { |
144 | 5.52k | if (block->get_by_position(position).name == _column_name) { |
145 | 5.44k | return position; |
146 | 5.44k | } |
147 | 5.52k | } |
148 | 0 | return -1; |
149 | 5.44k | } |
150 | | |
151 | | int _column_id; |
152 | | std::atomic<int> _gap = -1; |
153 | | std::string _column_name; |
154 | | }; |
155 | | } // namespace doris |