/root/doris/be/src/runtime/runtime_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 "runtime/runtime_predicate.h" |
19 | | |
20 | | #include <memory> |
21 | | |
22 | | #include "common/compiler_util.h" // IWYU pragma: keep |
23 | | #include "common/exception.h" |
24 | | #include "common/status.h" |
25 | | #include "olap/accept_null_predicate.h" |
26 | | #include "olap/column_predicate.h" |
27 | | #include "olap/predicate_creator.h" |
28 | | #include "runtime/define_primitive_type.h" |
29 | | |
30 | | namespace doris::vectorized { |
31 | | |
32 | | RuntimePredicate::RuntimePredicate(const TTopnFilterDesc& desc) |
33 | 0 | : _nulls_first(desc.null_first), _is_asc(desc.is_asc) { |
34 | 0 | DCHECK(!desc.target_node_id_to_target_expr.empty()); |
35 | 0 | for (auto p : desc.target_node_id_to_target_expr) { |
36 | 0 | _contexts[p.first].expr = p.second; |
37 | 0 | } |
38 | |
|
39 | 0 | _type = thrift_to_type(desc.target_node_id_to_target_expr.begin() |
40 | 0 | ->second.nodes[0] |
41 | 0 | .type.types[0] |
42 | 0 | .scalar_type.type); |
43 | 0 | if (!_init(_type)) { |
44 | 0 | std::stringstream ss; |
45 | 0 | desc.target_node_id_to_target_expr.begin()->second.nodes[0].printTo(ss); |
46 | 0 | throw Exception(ErrorCode::INTERNAL_ERROR, "meet invalid type, type={}, expr={}", |
47 | 0 | type_to_string(_type), ss.str()); |
48 | 0 | } |
49 | | |
50 | | // For ASC sort, create runtime predicate col_name <= max_top_value |
51 | | // since values that > min_top_value are large than any value in current topn values |
52 | | // For DESC sort, create runtime predicate col_name >= min_top_value |
53 | | // since values that < min_top_value are less than any value in current topn values |
54 | 0 | _pred_constructor = _is_asc ? create_comparison_predicate<PredicateType::LE> |
55 | 0 | : create_comparison_predicate<PredicateType::GE>; |
56 | 0 | } |
57 | | |
58 | | Status RuntimePredicate::init_target( |
59 | | int32_t target_node_id, phmap::flat_hash_map<int, SlotDescriptor*> slot_id_to_slot_desc, |
60 | 0 | const int column_id) { |
61 | 0 | if (column_id < 0) { |
62 | 0 | return Status::OK(); |
63 | 0 | } |
64 | 0 | std::unique_lock<std::shared_mutex> wlock(_rwlock); |
65 | 0 | check_target_node_id(target_node_id); |
66 | | // order by abs(col1) limit x; |
67 | | // cannot be used min-max filter, no need create predicate. |
68 | | // but can used in VTopNPred.execute_column |
69 | 0 | if (target_is_slot(target_node_id)) { |
70 | 0 | _contexts[target_node_id].col_name = |
71 | 0 | slot_id_to_slot_desc[get_texpr(target_node_id).nodes[0].slot_ref.slot_id] |
72 | 0 | ->col_name(); |
73 | 0 | _contexts[target_node_id].col_data_type = |
74 | 0 | slot_id_to_slot_desc[get_texpr(target_node_id).nodes[0].slot_ref.slot_id]->type(); |
75 | 0 | _contexts[target_node_id].predicate = SharedPredicate::create_shared( |
76 | 0 | cast_set<uint32_t>(column_id), _contexts[target_node_id].col_name); |
77 | 0 | } |
78 | 0 | _detected_target = true; |
79 | 0 | return Status::OK(); |
80 | 0 | } |
81 | | |
82 | 0 | bool RuntimePredicate::_init(PrimitiveType type) { |
83 | 0 | return is_int_or_bool(type) || is_decimal(type) || is_string_type(type) || is_date_type(type) || |
84 | 0 | is_time_type(type) || is_ip(type) || is_varbinary(type); |
85 | 0 | } |
86 | | |
87 | 0 | Status RuntimePredicate::update(const Field& value) { |
88 | 0 | std::unique_lock<std::shared_mutex> wlock(_rwlock); |
89 | | // skip null value |
90 | 0 | if (value.is_null()) { |
91 | 0 | return Status::OK(); |
92 | 0 | } |
93 | | |
94 | 0 | bool updated = false; |
95 | |
|
96 | 0 | if (UNLIKELY(_orderby_extrem.is_null())) { |
97 | 0 | _orderby_extrem = value; |
98 | 0 | updated = true; |
99 | 0 | } else { |
100 | 0 | if ((_is_asc && value < _orderby_extrem) || (!_is_asc && value > _orderby_extrem)) { |
101 | 0 | _orderby_extrem = value; |
102 | 0 | updated = true; |
103 | 0 | } |
104 | 0 | } |
105 | |
|
106 | 0 | _has_value = true; |
107 | |
|
108 | 0 | if (!updated) { |
109 | 0 | return Status::OK(); |
110 | 0 | } |
111 | 0 | for (auto p : _contexts) { |
112 | 0 | auto ctx = p.second; |
113 | 0 | if (ctx.predicate == nullptr) { |
114 | | // 1. `init_target` will not create predicate. example : `order by abs(col1) limit x;` |
115 | | // So don't need create new `ColumnPredicate`, |
116 | | // but need update `_orderby_extrem` for `VTopNPred.execute_column` |
117 | | // 2. this `RuntimePredicate` will associate multiple scan nodes. |
118 | | // When the sort node is updated, some scan nodes may not have called `init_target` yet. |
119 | | // example: |
120 | | //SELECT subq1.pk AS pk1 FROM ( |
121 | | // ( SELECT t1.pk FROM tb AS t1 ) |
122 | | // UNION ALL |
123 | | // ( SELECT t1.pk FROM tb AS t1 ORDER BY t1.pk )) |
124 | | // subq1 |
125 | | //WHERE subq1.pk <> ( |
126 | | // SELECT t1.pk FROM tb AS t1 ORDER BY t1.pk LIMIT 1 |
127 | | //) ORDER BY 1 LIMIT 1 ; |
128 | 0 | continue; |
129 | 0 | } |
130 | 0 | std::shared_ptr<ColumnPredicate> pred = |
131 | 0 | _pred_constructor(ctx.predicate->column_id(), ctx.col_name, ctx.col_data_type, |
132 | 0 | _orderby_extrem, false); |
133 | | |
134 | | // For NULLS FIRST, wrap a AcceptNullPredicate to return true for NULL |
135 | | // since ORDER BY ASC/DESC should get NULL first but pred returns NULL |
136 | | // and NULL in where predicate will be treated as FALSE |
137 | 0 | if (_nulls_first) { |
138 | 0 | pred = AcceptNullPredicate::create_shared(pred); |
139 | 0 | } |
140 | |
|
141 | 0 | ((SharedPredicate*)ctx.predicate.get())->set_nested(pred); |
142 | 0 | } |
143 | 0 | return Status::OK(); |
144 | 0 | } |
145 | | |
146 | | } // namespace doris::vectorized |