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 | | #include "exprs/vslot_ref.h" |
19 | | |
20 | | #include <gen_cpp/Exprs_types.h> |
21 | | #include <glog/logging.h> |
22 | | |
23 | | #include <ostream> |
24 | | |
25 | | #include "common/status.h" |
26 | | #include "core/block/block.h" |
27 | | #include "exprs/vexpr_context.h" |
28 | | #include "runtime/descriptors.h" |
29 | | #include "runtime/runtime_state.h" |
30 | | |
31 | | namespace doris { |
32 | | class VExprContext; |
33 | | |
34 | | VSlotRef::VSlotRef(const doris::TExprNode& node) |
35 | 4.06M | : VExpr(node), |
36 | 4.06M | _slot_id(node.slot_ref.slot_id), |
37 | 4.06M | _column_id(-1), |
38 | 4.06M | _column_name(nullptr), |
39 | 4.06M | _column_label(node.label) {} |
40 | | |
41 | | VSlotRef::VSlotRef(const SlotDescriptor* desc) |
42 | 17 | : VExpr(desc->type(), true), _slot_id(desc->id()), _column_id(-1), _column_name(nullptr) {} |
43 | | |
44 | | VSlotRef::VSlotRef(int slot_id, int column_id, int column_uniq_id, const DataTypePtr& type, |
45 | | std::string column_name) |
46 | 3.26k | : VExpr(type, true), |
47 | 3.26k | _slot_id(slot_id), |
48 | 3.26k | _column_id(column_id), |
49 | 3.26k | _column_uniq_id(column_uniq_id), |
50 | 3.26k | _owned_column_name(std::move(column_name)), |
51 | 3.26k | _column_name(&_owned_column_name) {} |
52 | | |
53 | | Status VSlotRef::prepare(doris::RuntimeState* state, const doris::RowDescriptor& desc, |
54 | 3.84M | VExprContext* context) { |
55 | 3.84M | DCHECK_EQ(_children.size(), 0); |
56 | 3.84M | if (_prepared) { |
57 | 32 | return Status::OK(); |
58 | 32 | } |
59 | 3.84M | if (_column_id >= 0 && _column_name != nullptr) { |
60 | 3.02k | _prepared = true; |
61 | 3.02k | _prepare_finished = true; |
62 | 3.02k | return Status::OK(); |
63 | 3.02k | } |
64 | 3.83M | _prepared = true; |
65 | 3.83M | RETURN_IF_ERROR(VExpr::prepare(state, desc, context)); |
66 | 3.83M | if (_slot_id == -1) { |
67 | 0 | _prepare_finished = true; |
68 | 0 | return Status::OK(); |
69 | 0 | } |
70 | 3.83M | const SlotDescriptor* slot_desc = state->desc_tbl().get_slot_descriptor(_slot_id); |
71 | 3.83M | if (slot_desc == nullptr) { |
72 | 0 | return Status::Error<ErrorCode::INTERNAL_ERROR>( |
73 | 0 | "couldn't resolve slot descriptor {}, desc: {}", _slot_id, |
74 | 0 | state->desc_tbl().debug_string()); |
75 | 0 | } |
76 | 3.83M | _column_name = &slot_desc->col_name(); |
77 | 3.83M | _column_uniq_id = slot_desc->col_unique_id(); |
78 | 3.83M | _column_id = desc.get_column_id(_slot_id); |
79 | 3.83M | if (_column_id < 0) { |
80 | 0 | return Status::Error<ErrorCode::INTERNAL_ERROR>( |
81 | 0 | "VSlotRef {} have invalid slot id: {}, desc: {}, slot_desc: {}, desc_tbl: {}", |
82 | 0 | *_column_name, _slot_id, desc.debug_string(), slot_desc->debug_string(), |
83 | 0 | state->desc_tbl().debug_string()); |
84 | 0 | } |
85 | 3.83M | _prepare_finished = true; |
86 | 3.83M | return Status::OK(); |
87 | 3.83M | } |
88 | | |
89 | | Status VSlotRef::open(RuntimeState* state, VExprContext* context, |
90 | 16.2M | FunctionContext::FunctionStateScope scope) { |
91 | 16.2M | DCHECK(_prepare_finished); |
92 | 16.2M | RETURN_IF_ERROR(VExpr::open(state, context, scope)); |
93 | 16.2M | _open_finished = true; |
94 | 16.2M | return Status::OK(); |
95 | 16.2M | } |
96 | | |
97 | 1.06M | Status VSlotRef::execute(VExprContext* context, Block* block, int* result_column_id) const { |
98 | 1.06M | if (_column_id >= 0 && _column_id >= block->columns()) { |
99 | 0 | return Status::Error<ErrorCode::INTERNAL_ERROR>( |
100 | 0 | "input block not contain slot column {}, column_id={}, block={}", *_column_name, |
101 | 0 | _column_id, block->dump_structure()); |
102 | 0 | } |
103 | 1.06M | *result_column_id = _column_id; |
104 | 1.06M | return Status::OK(); |
105 | 1.06M | } |
106 | | |
107 | | Status VSlotRef::execute_column_impl(VExprContext* context, const Block* block, |
108 | | const Selector* selector, size_t count, |
109 | 2.11M | ColumnPtr& result_column) const { |
110 | 2.11M | if (_column_id >= 0 && _column_id >= block->columns()) { |
111 | 0 | return Status::Error<ErrorCode::INTERNAL_ERROR>( |
112 | 0 | "input block not contain slot column {}, column_id={}, block={}", *_column_name, |
113 | 0 | _column_id, block->dump_structure()); |
114 | 0 | } |
115 | 2.11M | result_column = |
116 | 2.11M | filter_column_with_selector(block->get_by_position(_column_id).column, selector, count); |
117 | 2.11M | DCHECK_EQ(result_column->size(), count); |
118 | 2.11M | return Status::OK(); |
119 | 2.11M | } |
120 | | |
121 | 3.37M | DataTypePtr VSlotRef::execute_type(const Block* block) const { |
122 | 3.37M | if (_column_id >= 0 && _column_id >= block->columns()) { |
123 | 0 | throw doris::Exception(ErrorCode::INTERNAL_ERROR, |
124 | 0 | "input block not contain slot column {}, column_id={}, block={}", |
125 | 0 | *_column_name, _column_id, block->dump_structure()); |
126 | 0 | } |
127 | 3.37M | return block->get_by_position(_column_id).type; |
128 | 3.37M | } |
129 | | |
130 | 108 | Status VSlotRef::clone_node(VExprSPtr* cloned_expr) const { |
131 | 108 | DORIS_CHECK(cloned_expr != nullptr); |
132 | 108 | if (_column_id >= 0 && _column_name != nullptr) { |
133 | 108 | *cloned_expr = VSlotRef::create_shared(_slot_id, _column_id, _column_uniq_id, _data_type, |
134 | 108 | *_column_name); |
135 | 108 | return Status::OK(); |
136 | 108 | } |
137 | 0 | auto node = clone_texpr_node(); |
138 | 0 | TSlotRef slot_ref; |
139 | 0 | slot_ref.__set_slot_id(_slot_id); |
140 | 0 | node.__set_slot_ref(slot_ref); |
141 | 0 | node.__set_label(_column_label); |
142 | 0 | auto cloned = VSlotRef::create_shared(node); |
143 | 0 | auto* cloned_slot_ref = static_cast<VSlotRef*>(cloned.get()); |
144 | 0 | cloned_slot_ref->_column_id = _column_id; |
145 | 0 | cloned_slot_ref->_column_uniq_id = _column_uniq_id; |
146 | 0 | cloned_slot_ref->_column_name = _column_name; |
147 | 0 | *cloned_expr = std::move(cloned); |
148 | 0 | return Status::OK(); |
149 | 108 | } |
150 | | |
151 | 2.39M | const std::string& VSlotRef::expr_name() const { |
152 | 2.39M | return *_column_name; |
153 | 2.39M | } |
154 | 60 | std::string VSlotRef::expr_label() { |
155 | 60 | return _column_label; |
156 | 60 | } |
157 | | |
158 | 8 | std::string VSlotRef::debug_string() const { |
159 | 8 | std::stringstream out; |
160 | 8 | out << "SlotRef(slot_id=" << _slot_id << VExpr::debug_string() << ")"; |
161 | 8 | return out.str(); |
162 | 8 | } |
163 | | |
164 | 0 | bool VSlotRef::equals(const VExpr& other) { |
165 | 0 | if (!VExpr::equals(other)) { |
166 | 0 | return false; |
167 | 0 | } |
168 | 0 | const auto* other_ptr = dynamic_cast<const VSlotRef*>(&other); |
169 | 0 | if (!other_ptr) { |
170 | 0 | return false; |
171 | 0 | } |
172 | 0 | if (this->_slot_id != other_ptr->_slot_id || this->_column_id != other_ptr->_column_id || |
173 | 0 | this->_column_name != other_ptr->_column_name || |
174 | 0 | this->_column_label != other_ptr->_column_label) { |
175 | 0 | return false; |
176 | 0 | } |
177 | 0 | return true; |
178 | 0 | } |
179 | | |
180 | 287k | uint64_t VSlotRef::get_digest(uint64_t seed) const { |
181 | 287k | seed = HashUtil::hash64(&_column_uniq_id, sizeof(int), seed); |
182 | 287k | return HashUtil::hash64(_column_name->c_str(), _column_name->size(), seed); |
183 | 287k | } |
184 | | |
185 | | } // namespace doris |