/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 | PrimitiveType 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={}", int(type), |
47 | 0 | 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 | | void RuntimePredicate::init_target( |
59 | 0 | int32_t target_node_id, phmap::flat_hash_map<int, SlotDescriptor*> slot_id_to_slot_desc) { |
60 | 0 | std::unique_lock<std::shared_mutex> wlock(_rwlock); |
61 | 0 | check_target_node_id(target_node_id); |
62 | 0 | if (target_is_slot(target_node_id)) { |
63 | 0 | _contexts[target_node_id].col_name = |
64 | 0 | slot_id_to_slot_desc[get_texpr(target_node_id).nodes[0].slot_ref.slot_id] |
65 | 0 | ->col_name(); |
66 | 0 | } |
67 | 0 | _detected_target = true; |
68 | 0 | } |
69 | | |
70 | | template <PrimitiveType type> |
71 | 0 | std::string get_normal_value(const Field& field) { |
72 | 0 | using ValueType = typename PrimitiveTypeTraits<type>::CppType; |
73 | 0 | return cast_to_string<type, ValueType>(field.get<ValueType>(), 0); |
74 | 0 | } Unexecuted instantiation: _ZN5doris10vectorized16get_normal_valueILNS_13PrimitiveTypeE2EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE Unexecuted instantiation: _ZN5doris10vectorized16get_normal_valueILNS_13PrimitiveTypeE3EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE Unexecuted instantiation: _ZN5doris10vectorized16get_normal_valueILNS_13PrimitiveTypeE4EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE Unexecuted instantiation: _ZN5doris10vectorized16get_normal_valueILNS_13PrimitiveTypeE5EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE Unexecuted instantiation: _ZN5doris10vectorized16get_normal_valueILNS_13PrimitiveTypeE6EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE Unexecuted instantiation: _ZN5doris10vectorized16get_normal_valueILNS_13PrimitiveTypeE7EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE Unexecuted instantiation: _ZN5doris10vectorized16get_normal_valueILNS_13PrimitiveTypeE25EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE Unexecuted instantiation: _ZN5doris10vectorized16get_normal_valueILNS_13PrimitiveTypeE26EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE Unexecuted instantiation: _ZN5doris10vectorized16get_normal_valueILNS_13PrimitiveTypeE36EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE Unexecuted instantiation: _ZN5doris10vectorized16get_normal_valueILNS_13PrimitiveTypeE37EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE |
75 | | |
76 | 0 | std::string get_date_value(const Field& field) { |
77 | 0 | using ValueType = typename PrimitiveTypeTraits<TYPE_DATE>::CppType; |
78 | 0 | ValueType value; |
79 | 0 | Int64 v = field.get<Int64>(); |
80 | 0 | auto* p = (VecDateTimeValue*)&v; |
81 | 0 | value.from_olap_date(p->to_olap_date()); |
82 | 0 | value.cast_to_date(); |
83 | 0 | return cast_to_string<TYPE_DATE, ValueType>(value, 0); |
84 | 0 | } |
85 | | |
86 | 0 | std::string get_datetime_value(const Field& field) { |
87 | 0 | using ValueType = typename PrimitiveTypeTraits<TYPE_DATETIME>::CppType; |
88 | 0 | ValueType value; |
89 | 0 | Int64 v = field.get<Int64>(); |
90 | 0 | auto* p = (VecDateTimeValue*)&v; |
91 | 0 | value.from_olap_datetime(p->to_olap_datetime()); |
92 | 0 | value.to_datetime(); |
93 | 0 | return cast_to_string<TYPE_DATETIME, ValueType>(value, 0); |
94 | 0 | } |
95 | | |
96 | 0 | std::string get_time_value(const Field& field) { |
97 | 0 | using ValueType = typename PrimitiveTypeTraits<TYPE_TIMEV2>::CppType; |
98 | 0 | ValueType value = field.get<ValueType>(); |
99 | 0 | return cast_to_string<TYPE_TIMEV2, ValueType>(value, 0); |
100 | 0 | } |
101 | | |
102 | 0 | std::string get_decimalv2_value(const Field& field) { |
103 | | // can NOT use PrimitiveTypeTraits<TYPE_DECIMALV2>::CppType since |
104 | | // it is DecimalV2Value and Decimal128V2 can not convert to it implicitly |
105 | 0 | using ValueType = Decimal128V2::NativeType; |
106 | 0 | auto v = field.get<DecimalField<Decimal128V2>>(); |
107 | | // use TYPE_DECIMAL128I instead of TYPE_DECIMALV2 since v.get_scale() |
108 | | // is always 9 for DECIMALV2 |
109 | 0 | return cast_to_string<TYPE_DECIMAL128I, ValueType>(v.get_value(), v.get_scale()); |
110 | 0 | } |
111 | | |
112 | | template <PrimitiveType type> |
113 | 0 | std::string get_decimal_value(const Field& field) { |
114 | 0 | using ValueType = typename PrimitiveTypeTraits<type>::CppType; |
115 | 0 | auto v = field.get<DecimalField<ValueType>>(); |
116 | 0 | return cast_to_string<type, ValueType>(v.get_value(), v.get_scale()); |
117 | 0 | } Unexecuted instantiation: _ZN5doris10vectorized17get_decimal_valueILNS_13PrimitiveTypeE28EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE Unexecuted instantiation: _ZN5doris10vectorized17get_decimal_valueILNS_13PrimitiveTypeE29EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE Unexecuted instantiation: _ZN5doris10vectorized17get_decimal_valueILNS_13PrimitiveTypeE30EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE Unexecuted instantiation: _ZN5doris10vectorized17get_decimal_valueILNS_13PrimitiveTypeE35EEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS0_5FieldE |
118 | | |
119 | 0 | bool RuntimePredicate::_init(PrimitiveType type) { |
120 | | // set get value function |
121 | 0 | switch (type) { |
122 | 0 | case PrimitiveType::TYPE_BOOLEAN: { |
123 | 0 | _get_value_fn = get_normal_value<TYPE_BOOLEAN>; |
124 | 0 | break; |
125 | 0 | } |
126 | 0 | case PrimitiveType::TYPE_TINYINT: { |
127 | 0 | _get_value_fn = get_normal_value<TYPE_TINYINT>; |
128 | 0 | break; |
129 | 0 | } |
130 | 0 | case PrimitiveType::TYPE_SMALLINT: { |
131 | 0 | _get_value_fn = get_normal_value<TYPE_SMALLINT>; |
132 | 0 | break; |
133 | 0 | } |
134 | 0 | case PrimitiveType::TYPE_INT: { |
135 | 0 | _get_value_fn = get_normal_value<TYPE_INT>; |
136 | 0 | break; |
137 | 0 | } |
138 | 0 | case PrimitiveType::TYPE_BIGINT: { |
139 | 0 | _get_value_fn = get_normal_value<TYPE_BIGINT>; |
140 | 0 | break; |
141 | 0 | } |
142 | 0 | case PrimitiveType::TYPE_LARGEINT: { |
143 | 0 | _get_value_fn = get_normal_value<TYPE_LARGEINT>; |
144 | 0 | break; |
145 | 0 | } |
146 | 0 | case PrimitiveType::TYPE_CHAR: |
147 | 0 | case PrimitiveType::TYPE_VARCHAR: |
148 | 0 | case PrimitiveType::TYPE_STRING: { |
149 | 0 | _get_value_fn = [](const Field& field) { return field.get<String>(); }; |
150 | 0 | break; |
151 | 0 | } |
152 | 0 | case PrimitiveType::TYPE_DATEV2: { |
153 | 0 | _get_value_fn = get_normal_value<TYPE_DATEV2>; |
154 | 0 | break; |
155 | 0 | } |
156 | 0 | case PrimitiveType::TYPE_DATETIMEV2: { |
157 | 0 | _get_value_fn = get_normal_value<TYPE_DATETIMEV2>; |
158 | 0 | break; |
159 | 0 | } |
160 | 0 | case PrimitiveType::TYPE_DATE: { |
161 | 0 | _get_value_fn = get_date_value; |
162 | 0 | break; |
163 | 0 | } |
164 | 0 | case PrimitiveType::TYPE_DATETIME: { |
165 | 0 | _get_value_fn = get_datetime_value; |
166 | 0 | break; |
167 | 0 | } |
168 | 0 | case PrimitiveType::TYPE_TIMEV2: { |
169 | 0 | _get_value_fn = get_time_value; |
170 | 0 | break; |
171 | 0 | } |
172 | 0 | case PrimitiveType::TYPE_DECIMAL32: { |
173 | 0 | _get_value_fn = get_decimal_value<TYPE_DECIMAL32>; |
174 | 0 | break; |
175 | 0 | } |
176 | 0 | case PrimitiveType::TYPE_DECIMAL64: { |
177 | 0 | _get_value_fn = get_decimal_value<TYPE_DECIMAL64>; |
178 | 0 | break; |
179 | 0 | } |
180 | 0 | case PrimitiveType::TYPE_DECIMALV2: { |
181 | 0 | _get_value_fn = get_decimalv2_value; |
182 | 0 | break; |
183 | 0 | } |
184 | 0 | case PrimitiveType::TYPE_DECIMAL128I: { |
185 | 0 | _get_value_fn = get_decimal_value<TYPE_DECIMAL128I>; |
186 | 0 | break; |
187 | 0 | } |
188 | 0 | case PrimitiveType::TYPE_DECIMAL256: { |
189 | 0 | _get_value_fn = get_decimal_value<TYPE_DECIMAL256>; |
190 | 0 | break; |
191 | 0 | } |
192 | 0 | case PrimitiveType::TYPE_IPV4: { |
193 | 0 | _get_value_fn = get_normal_value<TYPE_IPV4>; |
194 | 0 | break; |
195 | 0 | } |
196 | 0 | case PrimitiveType::TYPE_IPV6: { |
197 | 0 | _get_value_fn = get_normal_value<TYPE_IPV6>; |
198 | 0 | break; |
199 | 0 | } |
200 | 0 | default: |
201 | 0 | return false; |
202 | 0 | } |
203 | | |
204 | 0 | return true; |
205 | 0 | } |
206 | | |
207 | 0 | Status RuntimePredicate::update(const Field& value) { |
208 | 0 | std::unique_lock<std::shared_mutex> wlock(_rwlock); |
209 | | // skip null value |
210 | 0 | if (value.is_null()) { |
211 | 0 | return Status::OK(); |
212 | 0 | } |
213 | | |
214 | 0 | bool updated = false; |
215 | |
|
216 | 0 | if (UNLIKELY(_orderby_extrem.is_null())) { |
217 | 0 | _orderby_extrem = value; |
218 | 0 | updated = true; |
219 | 0 | } else { |
220 | 0 | if ((_is_asc && value < _orderby_extrem) || (!_is_asc && value > _orderby_extrem)) { |
221 | 0 | _orderby_extrem = value; |
222 | 0 | updated = true; |
223 | 0 | } |
224 | 0 | } |
225 | |
|
226 | 0 | _has_value = true; |
227 | |
|
228 | 0 | if (!updated) { |
229 | 0 | return Status::OK(); |
230 | 0 | } |
231 | 0 | for (auto p : _contexts) { |
232 | 0 | auto ctx = p.second; |
233 | 0 | if (!ctx.tablet_schema) { |
234 | 0 | continue; |
235 | 0 | } |
236 | 0 | const auto& column = *DORIS_TRY(ctx.tablet_schema->column(ctx.col_name)); |
237 | 0 | std::unique_ptr<ColumnPredicate> pred {_pred_constructor(column, ctx.predicate->column_id(), |
238 | 0 | _get_value_fn(_orderby_extrem), |
239 | 0 | false, _predicate_arena)}; |
240 | | |
241 | | // For NULLS FIRST, wrap a AcceptNullPredicate to return true for NULL |
242 | | // since ORDER BY ASC/DESC should get NULL first but pred returns NULL |
243 | | // and NULL in where predicate will be treated as FALSE |
244 | 0 | if (_nulls_first) { |
245 | 0 | pred = AcceptNullPredicate::create_unique(pred.release()); |
246 | 0 | } |
247 | |
|
248 | 0 | ((SharedPredicate*)ctx.predicate.get())->set_nested(pred.release()); |
249 | 0 | } |
250 | 0 | return Status::OK(); |
251 | 0 | } |
252 | | |
253 | | } // namespace doris::vectorized |