/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_predicate0<PredicateType::LE> |
55 | 0 | : create_comparison_predicate0<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 | 0 | if (target_is_slot(target_node_id)) { |
67 | 0 | _contexts[target_node_id].col_name = |
68 | 0 | slot_id_to_slot_desc[get_texpr(target_node_id).nodes[0].slot_ref.slot_id] |
69 | 0 | ->col_name(); |
70 | 0 | _contexts[target_node_id].predicate = |
71 | 0 | SharedPredicate::create_shared(cast_set<uint32_t>(column_id)); |
72 | 0 | } |
73 | 0 | _detected_target = true; |
74 | 0 | return Status::OK(); |
75 | 0 | } |
76 | | |
77 | 0 | StringRef RuntimePredicate::_get_string_ref(const Field& field, const PrimitiveType type) { |
78 | 0 | switch (type) { |
79 | 0 | case PrimitiveType::TYPE_BOOLEAN: { |
80 | 0 | const auto& v = field.get<typename PrimitiveTypeTraits<TYPE_BOOLEAN>::CppType>(); |
81 | 0 | return StringRef((char*)&v, sizeof(v)); |
82 | 0 | } |
83 | 0 | case PrimitiveType::TYPE_TINYINT: { |
84 | 0 | const auto& v = field.get<typename PrimitiveTypeTraits<TYPE_TINYINT>::CppType>(); |
85 | 0 | return StringRef((char*)&v, sizeof(v)); |
86 | 0 | } |
87 | 0 | case PrimitiveType::TYPE_SMALLINT: { |
88 | 0 | const auto& v = field.get<typename PrimitiveTypeTraits<TYPE_SMALLINT>::CppType>(); |
89 | 0 | return StringRef((char*)&v, sizeof(v)); |
90 | 0 | } |
91 | 0 | case PrimitiveType::TYPE_INT: { |
92 | 0 | const auto& v = field.get<typename PrimitiveTypeTraits<TYPE_INT>::CppType>(); |
93 | 0 | return StringRef((char*)&v, sizeof(v)); |
94 | 0 | } |
95 | 0 | case PrimitiveType::TYPE_BIGINT: { |
96 | 0 | const auto& v = field.get<typename PrimitiveTypeTraits<TYPE_BIGINT>::CppType>(); |
97 | 0 | return StringRef((char*)&v, sizeof(v)); |
98 | 0 | } |
99 | 0 | case PrimitiveType::TYPE_LARGEINT: { |
100 | 0 | const auto& v = field.get<typename PrimitiveTypeTraits<TYPE_LARGEINT>::CppType>(); |
101 | 0 | return StringRef((char*)&v, sizeof(v)); |
102 | 0 | } |
103 | 0 | case PrimitiveType::TYPE_CHAR: |
104 | 0 | case PrimitiveType::TYPE_VARCHAR: |
105 | 0 | case PrimitiveType::TYPE_STRING: { |
106 | 0 | const auto& v = field.get<String>(); |
107 | 0 | auto length = v.size(); |
108 | 0 | char* buffer = _predicate_arena.alloc(length); |
109 | 0 | memset(buffer, 0, length); |
110 | 0 | memcpy(buffer, v.data(), v.length()); |
111 | |
|
112 | 0 | return {buffer, length}; |
113 | 0 | } |
114 | 0 | case PrimitiveType::TYPE_DATEV2: { |
115 | 0 | const auto& v = field.get<typename PrimitiveTypeTraits<TYPE_DATEV2>::CppType>(); |
116 | 0 | return StringRef((char*)&v, sizeof(v)); |
117 | 0 | } |
118 | 0 | case PrimitiveType::TYPE_DATETIMEV2: { |
119 | 0 | const auto& v = field.get<typename PrimitiveTypeTraits<TYPE_DATETIMEV2>::CppType>(); |
120 | 0 | return StringRef((char*)&v, sizeof(v)); |
121 | 0 | } |
122 | 0 | case PrimitiveType::TYPE_TIMESTAMPTZ: { |
123 | 0 | const auto& v = field.get<typename PrimitiveTypeTraits<TYPE_TIMESTAMPTZ>::CppType>(); |
124 | 0 | return StringRef((char*)&v, sizeof(v)); |
125 | 0 | break; |
126 | 0 | } |
127 | 0 | case PrimitiveType::TYPE_DATE: { |
128 | 0 | const auto& v = field.get<typename PrimitiveTypeTraits<TYPE_DATE>::CppType>(); |
129 | 0 | return StringRef((char*)&v, sizeof(v)); |
130 | 0 | } |
131 | 0 | case PrimitiveType::TYPE_DATETIME: { |
132 | 0 | const auto& v = field.get<typename PrimitiveTypeTraits<TYPE_DATETIME>::CppType>(); |
133 | 0 | return StringRef((char*)&v, sizeof(v)); |
134 | 0 | } |
135 | 0 | case PrimitiveType::TYPE_TIMEV2: { |
136 | 0 | const auto& v = field.get<typename PrimitiveTypeTraits<TYPE_TIMEV2>::CppType>(); |
137 | 0 | return StringRef((char*)&v, sizeof(v)); |
138 | 0 | } |
139 | 0 | case PrimitiveType::TYPE_DECIMAL32: { |
140 | 0 | const auto& v = field.get<typename PrimitiveTypeTraits<TYPE_DECIMAL32>::CppType>(); |
141 | 0 | return StringRef((char*)&v, sizeof(v)); |
142 | 0 | } |
143 | 0 | case PrimitiveType::TYPE_DECIMAL64: { |
144 | 0 | const auto& v = field.get<typename PrimitiveTypeTraits<TYPE_DECIMAL64>::CppType>(); |
145 | 0 | return StringRef((char*)&v, sizeof(v)); |
146 | 0 | } |
147 | 0 | case PrimitiveType::TYPE_DECIMALV2: { |
148 | 0 | const auto& v = field.get<typename PrimitiveTypeTraits<TYPE_DECIMALV2>::CppType>(); |
149 | 0 | return StringRef((char*)&v, sizeof(v)); |
150 | 0 | } |
151 | 0 | case PrimitiveType::TYPE_DECIMAL128I: { |
152 | 0 | const auto& v = field.get<typename PrimitiveTypeTraits<TYPE_DECIMAL128I>::CppType>(); |
153 | 0 | return StringRef((char*)&v, sizeof(v)); |
154 | 0 | } |
155 | 0 | case PrimitiveType::TYPE_DECIMAL256: { |
156 | 0 | const auto& v = field.get<typename PrimitiveTypeTraits<TYPE_DECIMAL256>::CppType>(); |
157 | 0 | return StringRef((char*)&v, sizeof(v)); |
158 | 0 | } |
159 | 0 | case PrimitiveType::TYPE_IPV4: { |
160 | 0 | const auto& v = field.get<typename PrimitiveTypeTraits<TYPE_IPV4>::CppType>(); |
161 | 0 | return StringRef((char*)&v, sizeof(v)); |
162 | 0 | } |
163 | 0 | case PrimitiveType::TYPE_IPV6: { |
164 | 0 | const auto& v = field.get<typename PrimitiveTypeTraits<TYPE_IPV6>::CppType>(); |
165 | 0 | return StringRef((char*)&v, sizeof(v)); |
166 | 0 | } |
167 | 0 | case doris::PrimitiveType::TYPE_VARBINARY: { |
168 | | // For VARBINARY type, use StringViewField to store binary data |
169 | 0 | const auto& v = field.get<StringViewField>(); |
170 | 0 | auto length = v.size(); |
171 | 0 | char* buffer = _predicate_arena.alloc(length); |
172 | 0 | memset(buffer, 0, length); |
173 | 0 | memcpy(buffer, v.data(), length); |
174 | 0 | return {buffer, length}; |
175 | 0 | } |
176 | 0 | default: |
177 | 0 | break; |
178 | 0 | } |
179 | | |
180 | 0 | throw Exception(ErrorCode::INTERNAL_ERROR, "meet invalid type, type={}", type_to_string(type)); |
181 | 0 | return StringRef(); |
182 | 0 | } |
183 | | |
184 | 0 | bool RuntimePredicate::_init(PrimitiveType type) { |
185 | 0 | return is_int_or_bool(type) || is_decimal(type) || is_string_type(type) || is_date_type(type) || |
186 | 0 | is_time_type(type) || is_ip(type) || is_varbinary(type); |
187 | 0 | } |
188 | | |
189 | 0 | Status RuntimePredicate::update(const Field& value) { |
190 | 0 | std::unique_lock<std::shared_mutex> wlock(_rwlock); |
191 | | // skip null value |
192 | 0 | if (value.is_null()) { |
193 | 0 | return Status::OK(); |
194 | 0 | } |
195 | | |
196 | 0 | bool updated = false; |
197 | |
|
198 | 0 | if (UNLIKELY(_orderby_extrem.is_null())) { |
199 | 0 | _orderby_extrem = value; |
200 | 0 | updated = true; |
201 | 0 | } else { |
202 | 0 | if ((_is_asc && value < _orderby_extrem) || (!_is_asc && value > _orderby_extrem)) { |
203 | 0 | _orderby_extrem = value; |
204 | 0 | updated = true; |
205 | 0 | } |
206 | 0 | } |
207 | |
|
208 | 0 | _has_value = true; |
209 | |
|
210 | 0 | if (!updated) { |
211 | 0 | return Status::OK(); |
212 | 0 | } |
213 | 0 | for (auto p : _contexts) { |
214 | 0 | auto ctx = p.second; |
215 | 0 | if (!ctx.tablet_schema) { |
216 | 0 | continue; |
217 | 0 | } |
218 | 0 | const auto& column = *DORIS_TRY(ctx.tablet_schema->column(ctx.col_name)); |
219 | 0 | auto str_ref = _get_string_ref(_orderby_extrem, _type); |
220 | 0 | std::shared_ptr<ColumnPredicate> pred = |
221 | 0 | _pred_constructor(ctx.predicate->column_id(), column.get_vec_type(), str_ref, false, |
222 | 0 | _predicate_arena); |
223 | | |
224 | | // For NULLS FIRST, wrap a AcceptNullPredicate to return true for NULL |
225 | | // since ORDER BY ASC/DESC should get NULL first but pred returns NULL |
226 | | // and NULL in where predicate will be treated as FALSE |
227 | 0 | if (_nulls_first) { |
228 | 0 | pred = AcceptNullPredicate::create_shared(pred); |
229 | 0 | } |
230 | |
|
231 | 0 | ((SharedPredicate*)ctx.predicate.get())->set_nested(pred); |
232 | 0 | } |
233 | 0 | return Status::OK(); |
234 | 0 | } |
235 | | |
236 | | } // namespace doris::vectorized |