/root/doris/be/src/exprs/vbloom_predicate.cpp
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/vbloom_predicate.h" |
19 | | |
20 | | #include <cstddef> |
21 | | #include <utility> |
22 | | |
23 | | #include "common/status.h" |
24 | | #include "core/block/block.h" |
25 | | #include "core/block/column_numbers.h" |
26 | | #include "core/block/column_with_type_and_name.h" |
27 | | #include "core/column/column.h" |
28 | | #include "core/column/column_nullable.h" |
29 | | #include "core/column/column_vector.h" |
30 | | #include "core/data_type/data_type.h" |
31 | | #include "core/data_type/data_type_nullable.h" |
32 | | #include "core/types.h" |
33 | | #include "exprs/bloom_filter_func.h" |
34 | | #include "exprs/expr_zonemap_filter.h" |
35 | | #include "exprs/vslot_ref.h" |
36 | | #include "runtime/runtime_state.h" |
37 | | |
38 | | namespace doris { |
39 | | class RowDescriptor; |
40 | | class TExprNode; |
41 | | |
42 | | } // namespace doris |
43 | | |
44 | | namespace doris { |
45 | | #include "common/compile_check_begin.h" |
46 | | |
47 | | class VExprContext; |
48 | | |
49 | 35 | VBloomPredicate::VBloomPredicate(const TExprNode& node) : VExpr(node), _filter(nullptr) {} |
50 | | |
51 | | Status VBloomPredicate::prepare(RuntimeState* state, const RowDescriptor& desc, |
52 | 3 | VExprContext* context) { |
53 | 3 | RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context)); |
54 | | |
55 | 3 | if (_children.size() != 1) { |
56 | 0 | return Status::InternalError("Invalid argument for VBloomPredicate."); |
57 | 0 | } |
58 | | |
59 | 3 | _prepare_finished = true; |
60 | 3 | return Status::OK(); |
61 | 3 | } |
62 | | |
63 | | Status VBloomPredicate::open(RuntimeState* state, VExprContext* context, |
64 | 3 | FunctionContext::FunctionStateScope scope) { |
65 | 3 | DCHECK(_prepare_finished); |
66 | 3 | RETURN_IF_ERROR(VExpr::open(state, context, scope)); |
67 | 3 | _open_finished = true; |
68 | 3 | return Status::OK(); |
69 | 3 | } |
70 | | |
71 | 32 | void VBloomPredicate::close(VExprContext* context, FunctionContext::FunctionStateScope scope) { |
72 | 32 | VExpr::close(context, scope); |
73 | 32 | } |
74 | | |
75 | | Status VBloomPredicate::_do_execute(VExprContext* context, const Block* block, |
76 | | const uint8_t* __restrict filter, Selector* selector, |
77 | 1 | size_t count, ColumnPtr& result_column) const { |
78 | 1 | DCHECK(_open_finished || block == nullptr); |
79 | 1 | DCHECK(!(filter != nullptr && selector != nullptr)) |
80 | 0 | << "filter and selector can not be both set"; |
81 | 1 | DCHECK_EQ(_children.size(), 1); |
82 | | |
83 | 1 | ColumnPtr argument_column; |
84 | 1 | RETURN_IF_ERROR(_children[0]->execute_column(context, block, selector, count, argument_column)); |
85 | 1 | argument_column = argument_column->convert_to_full_column_if_const(); |
86 | | |
87 | 1 | size_t sz = argument_column->size(); |
88 | 1 | auto res_data_column = ColumnUInt8::create(sz); |
89 | | |
90 | 1 | res_data_column->resize(sz); |
91 | 1 | auto* ptr = ((ColumnUInt8*)res_data_column.get())->get_data().data(); |
92 | | |
93 | 1 | _filter->find_fixed_len(argument_column, ptr, filter); |
94 | | |
95 | 1 | result_column = std::move(res_data_column); |
96 | 1 | DCHECK_EQ(result_column->size(), count); |
97 | 1 | return Status::OK(); |
98 | 1 | } |
99 | | |
100 | | Status VBloomPredicate::execute_column(VExprContext* context, const Block* block, |
101 | | Selector* selector, size_t count, |
102 | 1 | ColumnPtr& result_column) const { |
103 | 1 | return _do_execute(context, block, nullptr, selector, count, result_column); |
104 | 1 | } |
105 | | |
106 | | Status VBloomPredicate::execute_runtime_filter(VExprContext* context, const Block* block, |
107 | | const uint8_t* __restrict filter, size_t count, |
108 | | ColumnPtr& result_column, |
109 | 0 | ColumnPtr* arg_column) const { |
110 | 0 | return _do_execute(context, block, filter, nullptr, count, result_column); |
111 | 0 | } |
112 | | |
113 | | namespace { |
114 | | |
115 | 120 | bool bloom_filter_type_matches(PrimitiveType filter_type, const DataTypePtr& data_type) { |
116 | 120 | if (data_type == nullptr) { |
117 | 0 | return false; |
118 | 0 | } |
119 | 120 | const auto value_type = remove_nullable(data_type)->get_primitive_type(); |
120 | 120 | return filter_type == value_type || (is_string_type(filter_type) && is_string_type(value_type)); |
121 | 120 | } |
122 | | |
123 | | } // namespace |
124 | | |
125 | | bool VBloomPredicate::can_execute_on_raw_fixed_values(const DataTypePtr& data_type, |
126 | 38 | int column_id) const { |
127 | 38 | if (_filter == nullptr || !_filter->supports_raw_fixed_values() || _children.size() != 1) { |
128 | 5 | return false; |
129 | 5 | } |
130 | 33 | const auto slot = std::dynamic_pointer_cast<VSlotRef>(_children[0]); |
131 | 33 | return slot != nullptr && slot->column_id() == column_id && |
132 | 33 | bloom_filter_type_matches(_filter->primitive_type(), slot->data_type()) && |
133 | 33 | bloom_filter_type_matches(_filter->primitive_type(), data_type); |
134 | 38 | } |
135 | | |
136 | | Status VBloomPredicate::execute_on_raw_fixed_values(const uint8_t* values, size_t num_values, |
137 | | size_t value_width, |
138 | | const DataTypePtr& data_type, int column_id, |
139 | 3 | uint8_t* matches) const { |
140 | 3 | if (!can_execute_on_raw_fixed_values(data_type, column_id)) { |
141 | 0 | return Status::NotSupported("Bloom predicate cannot evaluate raw fixed-width values"); |
142 | 0 | } |
143 | | // Hash physical values inside BloomFilterFunc<T>; reconstructing an untyped hash here could |
144 | | // disagree with the build-side hash for dates, decimals, and other fixed-width wrappers. |
145 | 3 | return _filter->find_batch_raw_fixed(values, num_values, value_width, matches); |
146 | 3 | } |
147 | | |
148 | | bool VBloomPredicate::can_execute_on_raw_binary_values(const DataTypePtr& data_type, |
149 | 16 | int column_id) const { |
150 | 16 | if (_filter == nullptr || !_filter->supports_raw_binary_values() || _children.size() != 1) { |
151 | 1 | return false; |
152 | 1 | } |
153 | 15 | const auto slot = std::dynamic_pointer_cast<VSlotRef>(_children[0]); |
154 | 15 | return slot != nullptr && slot->column_id() == column_id && |
155 | 15 | bloom_filter_type_matches(_filter->primitive_type(), slot->data_type()) && |
156 | 15 | bloom_filter_type_matches(_filter->primitive_type(), data_type); |
157 | 16 | } |
158 | | |
159 | | Status VBloomPredicate::execute_on_raw_binary_values(const StringRef* values, size_t num_values, |
160 | | const DataTypePtr& data_type, int column_id, |
161 | 2 | uint8_t* matches) const { |
162 | 2 | if (!can_execute_on_raw_binary_values(data_type, column_id)) { |
163 | 0 | return Status::NotSupported("Bloom predicate cannot evaluate raw binary values"); |
164 | 0 | } |
165 | 2 | return _filter->find_batch_raw_binary(values, num_values, matches); |
166 | 2 | } |
167 | | |
168 | | ZoneMapFilterResult VBloomPredicate::evaluate_dictionary_filter( |
169 | 3 | const DictionaryEvalContext& ctx) const { |
170 | 3 | if (!can_evaluate_dictionary_filter()) { |
171 | 0 | return ZoneMapFilterResult::kUnsupported; |
172 | 0 | } |
173 | 3 | const auto slot = std::dynamic_pointer_cast<VSlotRef>(_children[0]); |
174 | 3 | DORIS_CHECK(slot != nullptr); |
175 | 3 | const auto* dictionary = ctx.slot(slot->column_id()); |
176 | 3 | if (dictionary == nullptr || |
177 | 3 | !bloom_filter_type_matches(_filter->primitive_type(), dictionary->data_type)) { |
178 | 0 | return ZoneMapFilterResult::kUnsupported; |
179 | 0 | } |
180 | 4 | for (const auto& value : dictionary->values) { |
181 | 4 | if (_filter->test_field(value)) { |
182 | 2 | return ZoneMapFilterResult::kMayMatch; |
183 | 2 | } |
184 | 4 | } |
185 | 1 | return ZoneMapFilterResult::kNoMatch; |
186 | 3 | } |
187 | | |
188 | 21 | bool VBloomPredicate::can_evaluate_dictionary_filter() const { |
189 | 21 | if (_filter == nullptr || _children.size() != 1) { |
190 | 0 | return false; |
191 | 0 | } |
192 | 21 | const auto slot = std::dynamic_pointer_cast<VSlotRef>(_children[0]); |
193 | 21 | return slot != nullptr && |
194 | 21 | bloom_filter_type_matches(_filter->primitive_type(), slot->data_type()); |
195 | 21 | } |
196 | | |
197 | 3 | const std::string& VBloomPredicate::expr_name() const { |
198 | 3 | return EXPR_NAME; |
199 | 3 | } |
200 | | |
201 | 35 | void VBloomPredicate::set_filter(std::shared_ptr<BloomFilterFuncBase> filter) { |
202 | 35 | _filter = filter; |
203 | 35 | } |
204 | | |
205 | 4 | uint64_t VBloomPredicate::get_digest(uint64_t seed) const { |
206 | 4 | seed = _children[0]->get_digest(seed); |
207 | 4 | if (seed) { |
208 | 4 | char* data; |
209 | 4 | int len; |
210 | 4 | _filter->get_data(&data, &len); |
211 | 4 | return HashUtil::hash64(data, len, seed); |
212 | 4 | } |
213 | 0 | return 0; |
214 | 4 | } |
215 | | |
216 | | #include "common/compile_check_end.h" |
217 | | } // namespace doris |