be/src/exprs/vtopn_pred.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 <gen_cpp/types.pb.h> |
21 | | |
22 | | #include <utility> |
23 | | |
24 | | #include "core/block/column_numbers.h" |
25 | | #include "core/data_type/data_type.h" |
26 | | #include "exec/common/util.hpp" |
27 | | #include "exprs/function/simple_function_factory.h" |
28 | | #include "exprs/vectorized_fn_call.h" |
29 | | #include "exprs/vexpr.h" |
30 | | #include "exprs/vslot_ref.h" |
31 | | #include "runtime/query_context.h" |
32 | | #include "runtime/runtime_predicate.h" |
33 | | #include "runtime/runtime_state.h" |
34 | | |
35 | | namespace doris { |
36 | | |
37 | | // only used for dynamic topn filter |
38 | | class VTopNPred : public VExpr { |
39 | | ENABLE_FACTORY_CREATOR(VTopNPred); |
40 | | |
41 | | public: |
42 | | VTopNPred(const TExprNode& node, int source_node_id, VExprContextSPtr target_ctx) |
43 | 33 | : VExpr(node), |
44 | 33 | _source_node_id(source_node_id), |
45 | 33 | _expr_name(fmt::format("VTopNPred(source_node_id={})", _source_node_id)), |
46 | 33 | _target_ctx(std::move(target_ctx)) {} |
47 | 2 | bool is_topn_filter() const override { return true; } |
48 | | |
49 | 0 | static Status create_vtopn_pred(const TExpr& target_expr, int source_node_id, VExprSPtr& expr) { |
50 | 0 | VExprContextSPtr target_ctx; |
51 | 0 | RETURN_IF_ERROR(VExpr::create_expr_tree(target_expr, target_ctx)); |
52 | | |
53 | 0 | TExprNode node; |
54 | 0 | node.__set_node_type(TExprNodeType::FUNCTION_CALL); |
55 | 0 | node.__set_type(create_type_desc(PrimitiveType::TYPE_BOOLEAN)); |
56 | 0 | node.__set_is_nullable(target_ctx->root()->is_nullable()); |
57 | 0 | expr = VTopNPred::create_shared(node, source_node_id, target_ctx); |
58 | |
|
59 | 0 | DCHECK(target_ctx->root() != nullptr); |
60 | 0 | expr->add_child(target_ctx->root()); |
61 | |
|
62 | 0 | return Status::OK(); |
63 | 0 | } |
64 | | |
65 | 0 | int source_node_id() const { return _source_node_id; } |
66 | 0 | Status clone_node(VExprSPtr* cloned_expr) const override { |
67 | 0 | DORIS_CHECK(cloned_expr != nullptr); |
68 | 0 | *cloned_expr = VTopNPred::create_shared(clone_texpr_node(), _source_node_id, nullptr); |
69 | 0 | return Status::OK(); |
70 | 0 | } |
71 | | |
72 | 33 | Status prepare(RuntimeState* state, const RowDescriptor& desc, VExprContext* context) override { |
73 | 33 | _predicate = &state->get_query_ctx()->get_runtime_predicate(_source_node_id); |
74 | 33 | RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context)); |
75 | | |
76 | 33 | ColumnsWithTypeAndName argument_template; |
77 | 33 | argument_template.emplace_back(nullptr, _children[0]->data_type(), |
78 | 33 | _children[0]->expr_name()); |
79 | 33 | argument_template.emplace_back(nullptr, _children[0]->data_type(), "topn value"); |
80 | | |
81 | 33 | _function = SimpleFunctionFactory::instance().get_function( |
82 | 33 | _predicate->is_asc() ? "le" : "ge", argument_template, _data_type, {}, |
83 | 33 | state->be_exec_version()); |
84 | 33 | if (!_function) { |
85 | 0 | return Status::InternalError("get function failed"); |
86 | 0 | } |
87 | 33 | return Status::OK(); |
88 | 33 | } |
89 | | |
90 | | Status execute_column_impl(VExprContext* context, const Block* block, const Selector* selector, |
91 | 3 | size_t count, ColumnPtr& result_column) const override { |
92 | 3 | if (!_predicate->has_value()) { |
93 | 1 | result_column = create_always_true_column(count, _data_type->is_nullable()); |
94 | 1 | return Status::OK(); |
95 | 1 | } |
96 | | |
97 | 2 | Block temp_block; |
98 | | |
99 | | // slot |
100 | 2 | ColumnPtr slot_column; |
101 | 2 | RETURN_IF_ERROR(_children[0]->execute_column(context, block, selector, count, slot_column)); |
102 | 2 | auto slot_type = _children[0]->execute_type(block); |
103 | 2 | temp_block.insert({slot_column, slot_type, _children[0]->expr_name()}); |
104 | 2 | int slot_id = 0; |
105 | | |
106 | | // topn value |
107 | 2 | Field field = _predicate->get_value(); |
108 | 2 | auto column_ptr = _children[0]->data_type()->create_column_const(1, field); |
109 | 2 | int topn_value_id = VExpr::insert_param(&temp_block, |
110 | 2 | {column_ptr, _children[0]->data_type(), _expr_name}, |
111 | 2 | std::max(count, column_ptr->size())); |
112 | | |
113 | | // if error(slot_id == -1), will return. |
114 | 2 | ColumnNumbers arguments = {static_cast<uint32_t>(slot_id), |
115 | 2 | static_cast<uint32_t>(topn_value_id)}; |
116 | | |
117 | 2 | uint32_t num_columns_without_result = temp_block.columns(); |
118 | | // prepare a column to save result |
119 | 2 | temp_block.insert({nullptr, _data_type, _expr_name}); |
120 | | |
121 | 2 | RETURN_IF_ERROR(_function->execute(nullptr, temp_block, arguments, |
122 | 2 | num_columns_without_result, temp_block.rows())); |
123 | 2 | result_column = std::move(temp_block.get_by_position(num_columns_without_result).column); |
124 | 2 | if (auto mutable_result = IColumn::mutate(std::move(result_column)); |
125 | 2 | auto* nullable = check_and_get_column<ColumnNullable>(*mutable_result)) { |
126 | 2 | auto& values = assert_cast<ColumnUInt8&>(*nullable->get_nested_column_ptr()).get_data(); |
127 | 2 | const auto& null_map = nullable->get_null_map_data(); |
128 | | // Master validates execute_type() against the physical result column. Collapse SQL |
129 | | // NULL to the filter decision here: NULLS FIRST keeps it, while NULLS LAST rejects it. |
130 | 6 | for (size_t row = 0; row < values.size(); ++row) { |
131 | 4 | values[row] = null_map[row] ? _predicate->nulls_first() : values[row]; |
132 | 4 | } |
133 | 2 | result_column = nullable->get_nested_column_ptr(); |
134 | 2 | } else { |
135 | 0 | result_column = std::move(mutable_result); |
136 | 0 | } |
137 | 2 | DCHECK_EQ(result_column->size(), count); |
138 | 2 | return Status::OK(); |
139 | 2 | } |
140 | | |
141 | | // Returns true only for a direct slot binding whose logical fixed-width type exactly matches |
142 | | // `data_type`. Eligibility does not depend on whether the dynamic TopN bound has arrived: a |
143 | | // reader may cache this answer during initialization and observe the bound in a later batch. |
144 | | bool can_execute_on_raw_fixed_values(const DataTypePtr& data_type, |
145 | | int column_id) const override; |
146 | | |
147 | | // Compares the contiguous non-NULL physical values with one snapshot of the current TopN bound |
148 | | // and ANDs the result into `matches`. `value_width` must equal the logical Doris value width. |
149 | | // When no bound is available yet, this is deliberately an all-pass operation. |
150 | | Status execute_on_raw_fixed_values(const uint8_t* values, size_t num_values, size_t value_width, |
151 | | const DataTypePtr& data_type, int column_id, |
152 | | uint8_t* matches) const override; |
153 | | |
154 | | // Returns true for a directly bound STRING-like or VARBINARY slot. As with the fixed-width |
155 | | // capability, a not-yet-published bound does not force the scanner onto a materializing path. |
156 | | bool can_execute_on_raw_binary_values(const DataTypePtr& data_type, |
157 | | int column_id) const override; |
158 | | |
159 | | // Compares decoder-owned immutable byte slices with the current TopN bound and ANDs each |
160 | | // decision into `matches`; it never copies the slices into a ColumnString/ColumnVarbinary. |
161 | | Status execute_on_raw_binary_values(const StringRef* values, size_t num_values, |
162 | | const DataTypePtr& data_type, int column_id, |
163 | | uint8_t* matches) const override; |
164 | | |
165 | 9 | bool raw_predicate_result_for_null() const override { |
166 | | // execute_column() is all-pass until publication; afterwards NULLS FIRST keeps NULL while |
167 | | // NULLS LAST rejects it. Sample this state per fragment just like the mutable bound. |
168 | 9 | return _predicate != nullptr && (!_predicate->has_value() || _predicate->nulls_first()); |
169 | 9 | } |
170 | | |
171 | | // Dictionary capability is restricted to a direct slot and NULLS-LAST semantics. It stays |
172 | | // enabled before the first bound so row-group setup can cache an all-pass bitmap; the scan |
173 | | // scheduler retains a per-batch residual and defers dictionary-id filtering while NULL still |
174 | | // matches, then safely resumes it after bound publication. |
175 | | bool can_evaluate_dictionary_filter() const override; |
176 | | |
177 | | // Evaluates every non-NULL entry in the bound slot dictionary against one current-bound |
178 | | // snapshot. kNoMatch proves that no entry can pass; kMayMatch includes both a matching entry and |
179 | | // a not-yet-published bound; kUnsupported reports an absent or incompatible dictionary slot. |
180 | | ZoneMapFilterResult evaluate_dictionary_filter(const DictionaryEvalContext& ctx) const override; |
181 | | |
182 | 0 | const std::string& expr_name() const override { return _expr_name; } |
183 | | |
184 | | // only used in external table (for min-max filter). get `slot > xxx`, not `function(slot) > xxx`. |
185 | 0 | bool get_binary_expr(VExprSPtr& new_root) const { |
186 | 0 | if (!get_child(0)->is_slot_ref()) { |
187 | | // top rf maybe is `xxx order by abs(column) limit xxx`. |
188 | 0 | return false; |
189 | 0 | } |
190 | | |
191 | 0 | if (!_predicate->has_value()) { |
192 | 0 | return false; |
193 | 0 | } |
194 | | |
195 | 0 | auto* slot_ref = assert_cast<VSlotRef*>(get_child(0).get()); |
196 | 0 | auto slot_data_type = remove_nullable(slot_ref->data_type()); |
197 | 0 | { |
198 | 0 | TFunction fn; |
199 | 0 | TFunctionName fn_name; |
200 | 0 | fn_name.__set_db_name(""); |
201 | 0 | fn_name.__set_function_name(_predicate->is_asc() ? "le" : "ge"); |
202 | 0 | fn.__set_name(fn_name); |
203 | 0 | fn.__set_binary_type(TFunctionBinaryType::BUILTIN); |
204 | 0 | std::vector<TTypeDesc> arg_types; |
205 | 0 | arg_types.push_back(create_type_desc(slot_data_type->get_primitive_type(), |
206 | 0 | slot_data_type->get_precision(), |
207 | 0 | slot_data_type->get_scale())); |
208 | |
|
209 | 0 | arg_types.push_back(create_type_desc(slot_data_type->get_primitive_type(), |
210 | 0 | slot_data_type->get_precision(), |
211 | 0 | slot_data_type->get_scale())); |
212 | 0 | fn.__set_arg_types(arg_types); |
213 | 0 | fn.__set_ret_type(create_type_desc(PrimitiveType::TYPE_BOOLEAN)); |
214 | 0 | fn.__set_has_var_args(false); |
215 | |
|
216 | 0 | TExprNode texpr_node; |
217 | 0 | texpr_node.__set_type(create_type_desc(PrimitiveType::TYPE_BOOLEAN)); |
218 | 0 | texpr_node.__set_node_type(TExprNodeType::BINARY_PRED); |
219 | 0 | texpr_node.__set_opcode(_predicate->is_asc() ? TExprOpcode::LE : TExprOpcode::GE); |
220 | 0 | texpr_node.__set_fn(fn); |
221 | 0 | texpr_node.__set_num_children(2); |
222 | 0 | texpr_node.__set_is_nullable(is_nullable()); |
223 | 0 | new_root = VectorizedFnCall::create_shared(texpr_node); |
224 | 0 | } |
225 | |
|
226 | 0 | { |
227 | | // add slot |
228 | 0 | new_root->add_child(children().at(0)); |
229 | 0 | } |
230 | | // add Literal |
231 | 0 | { |
232 | 0 | Field field = _predicate->get_value(); |
233 | 0 | TExprNode node = create_texpr_node_from(field, slot_data_type->get_primitive_type(), |
234 | 0 | slot_data_type->get_precision(), |
235 | 0 | slot_data_type->get_scale()); |
236 | 0 | new_root->add_child(VLiteral::create_shared(node)); |
237 | 0 | } |
238 | | |
239 | | // Since the normal greater than or less than relationship does not consider the relationship of null values, the generated `col >=/<= xxx OR col is null.` |
240 | 0 | if (_predicate->nulls_first()) { |
241 | 0 | VExprSPtr col_is_null_node; |
242 | 0 | { |
243 | 0 | TFunction fn; |
244 | 0 | TFunctionName fn_name; |
245 | 0 | fn_name.__set_db_name(""); |
246 | 0 | fn_name.__set_function_name("is_null_pred"); |
247 | 0 | fn.__set_name(fn_name); |
248 | 0 | fn.__set_binary_type(TFunctionBinaryType::BUILTIN); |
249 | 0 | std::vector<TTypeDesc> arg_types; |
250 | 0 | arg_types.push_back(create_type_desc(slot_data_type->get_primitive_type(), |
251 | 0 | slot_data_type->get_precision(), |
252 | 0 | slot_data_type->get_scale())); |
253 | 0 | fn.__set_arg_types(arg_types); |
254 | 0 | fn.__set_ret_type(create_type_desc(PrimitiveType::TYPE_BOOLEAN)); |
255 | 0 | fn.__set_has_var_args(false); |
256 | |
|
257 | 0 | TExprNode texpr_node; |
258 | 0 | texpr_node.__set_type(create_type_desc(PrimitiveType::TYPE_BOOLEAN)); |
259 | 0 | texpr_node.__set_node_type(TExprNodeType::FUNCTION_CALL); |
260 | 0 | texpr_node.__set_fn(fn); |
261 | 0 | texpr_node.__set_num_children(1); |
262 | 0 | col_is_null_node = VectorizedFnCall::create_shared(texpr_node); |
263 | | |
264 | | // add slot. |
265 | 0 | col_is_null_node->add_child(children().at(0)); |
266 | 0 | } |
267 | |
|
268 | 0 | VExprSPtr or_node; |
269 | 0 | { |
270 | 0 | TExprNode texpr_node; |
271 | 0 | texpr_node.__set_type(create_type_desc(PrimitiveType::TYPE_BOOLEAN)); |
272 | 0 | texpr_node.__set_node_type(TExprNodeType::COMPOUND_PRED); |
273 | 0 | texpr_node.__set_opcode(TExprOpcode::COMPOUND_OR); |
274 | 0 | texpr_node.__set_num_children(2); |
275 | 0 | or_node = VectorizedFnCall::create_shared(texpr_node); |
276 | 0 | } |
277 | |
|
278 | 0 | or_node->add_child(col_is_null_node); |
279 | 0 | or_node->add_child(new_root); |
280 | 0 | new_root = or_node; |
281 | 0 | } |
282 | |
|
283 | 0 | return true; |
284 | 0 | } |
285 | | |
286 | | private: |
287 | | int _source_node_id; |
288 | | std::string _expr_name; |
289 | | RuntimePredicate* _predicate = nullptr; |
290 | | FunctionBasePtr _function; |
291 | | VExprContextSPtr _target_ctx; |
292 | | }; |
293 | | |
294 | | } // namespace doris |