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