/root/doris/be/src/olap/delete_handler.cpp
Line | Count | Source (jump to first uncovered line) |
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 "olap/delete_handler.h" |
19 | | |
20 | | #include <gen_cpp/PaloInternalService_types.h> |
21 | | #include <gen_cpp/olap_file.pb.h> |
22 | | #include <thrift/protocol/TDebugProtocol.h> |
23 | | |
24 | | #include <string> |
25 | | #include <vector> |
26 | | |
27 | | #include "common/config.h" |
28 | | #include "common/logging.h" |
29 | | #include "common/status.h" |
30 | | #include "olap/block_column_predicate.h" |
31 | | #include "olap/column_predicate.h" |
32 | | #include "olap/olap_common.h" |
33 | | #include "olap/predicate_creator.h" |
34 | | #include "olap/tablet_schema.h" |
35 | | #include "olap/utils.h" |
36 | | #include "util/debug_points.h" |
37 | | |
38 | | using apache::thrift::ThriftDebugString; |
39 | | using std::vector; |
40 | | using std::string; |
41 | | |
42 | | using ::google::protobuf::RepeatedPtrField; |
43 | | |
44 | | namespace doris { |
45 | | |
46 | | // construct sub condition from TCondition |
47 | 87 | std::string construct_sub_predicate(const TCondition& condition) { |
48 | 87 | string op = condition.condition_op; |
49 | 87 | if (op == "<") { |
50 | 50 | op += "<"; |
51 | 50 | } else if (op == ">") { |
52 | 3 | op += ">"; |
53 | 3 | } |
54 | 87 | string condition_str; |
55 | 87 | if ("IS" == op) { |
56 | | // ATTN: tricky! Surround IS with spaces to make it "special" |
57 | 1 | condition_str = condition.column_name + " IS " + condition.condition_values[0]; |
58 | 86 | } else { // multi-elements IN expr has been processed with InPredicatePB |
59 | 86 | if (op == "*=") { |
60 | 1 | op = "="; |
61 | 85 | } else if (op == "!*=") { |
62 | 1 | op = "!="; |
63 | 1 | } |
64 | 86 | condition_str = condition.column_name + op + "'" + condition.condition_values[0] + "'"; |
65 | 86 | } |
66 | 87 | return condition_str; |
67 | 87 | } |
68 | | |
69 | | // make operators from FE adaptive to BE |
70 | 87 | std::string trans_op(const std::string& opt) { |
71 | 87 | std::string op = string(opt); |
72 | 87 | if (op == "<") { |
73 | 50 | op += "<"; |
74 | 50 | } else if (op == ">") { |
75 | 3 | op += ">"; |
76 | 3 | } |
77 | 87 | if ("IS" != op) { |
78 | 86 | if (op == "*=") { |
79 | 1 | op = "="; |
80 | 85 | } else if (op == "!*=") { |
81 | 1 | op = "!="; |
82 | 1 | } |
83 | 86 | } |
84 | 87 | return op; |
85 | 87 | } |
86 | | |
87 | | Status DeleteHandler::generate_delete_predicate(const TabletSchema& schema, |
88 | | const std::vector<TCondition>& conditions, |
89 | 97 | DeletePredicatePB* del_pred) { |
90 | 97 | DBUG_EXECUTE_IF("DeleteHandler::generate_delete_predicate.inject_failure", { |
91 | 97 | return Status::Error<false>(dp->param<int>("error_code"), |
92 | 97 | dp->param<std::string>("error_msg")); |
93 | 97 | }) |
94 | 97 | if (conditions.empty()) { |
95 | 1 | return Status::Error<ErrorCode::INVALID_ARGUMENT>( |
96 | 1 | "invalid parameters for store_cond. condition_size={}", conditions.size()); |
97 | 1 | } |
98 | | |
99 | | // Check whether the delete condition meets the requirements |
100 | 113 | for (const TCondition& condition : conditions) { |
101 | 113 | RETURN_IF_ERROR(check_condition_valid(schema, condition)); |
102 | 113 | } |
103 | | |
104 | | // Store delete condition |
105 | 88 | for (const TCondition& condition : conditions) { |
106 | 88 | if (condition.condition_values.size() > 1) { |
107 | 1 | InPredicatePB* in_pred = del_pred->add_in_predicates(); |
108 | 1 | if (condition.__isset.column_unique_id) { |
109 | 0 | in_pred->set_column_unique_id(condition.column_unique_id); |
110 | 0 | } |
111 | 1 | in_pred->set_column_name(condition.column_name); |
112 | 1 | bool is_not_in = condition.condition_op == "!*="; |
113 | 1 | in_pred->set_is_not_in(is_not_in); |
114 | 2 | for (const auto& condition_value : condition.condition_values) { |
115 | 2 | in_pred->add_values(condition_value); |
116 | 2 | } |
117 | | |
118 | 1 | LOG(INFO) << "store one sub-delete condition. condition name=" << in_pred->column_name() |
119 | 1 | << "condition size=" << in_pred->values().size(); |
120 | 87 | } else { |
121 | | // write sub predicate v1 for compactbility |
122 | 87 | std::string condition_str = construct_sub_predicate(condition); |
123 | 87 | VLOG_NOTICE << __PRETTY_FUNCTION__ << " condition_str: " << condition_str; |
124 | 87 | del_pred->add_sub_predicates(condition_str); |
125 | 87 | DeleteSubPredicatePB* sub_predicate = del_pred->add_sub_predicates_v2(); |
126 | 87 | if (condition.__isset.column_unique_id) { |
127 | | // only light schema change capable table set this field |
128 | 0 | sub_predicate->set_column_unique_id(condition.column_unique_id); |
129 | 87 | } else if (TCondition tmp; !DeleteHandler::parse_condition(condition_str, &tmp)) { |
130 | | // for non light shema change tables, check regex match for condition str |
131 | 0 | LOG(WARNING) << "failed to parse condition_str, condtion=" |
132 | 0 | << ThriftDebugString(condition); |
133 | 0 | return Status::Error<ErrorCode::INVALID_ARGUMENT>( |
134 | 0 | "failed to parse condition_str, condtion={}", ThriftDebugString(condition)); |
135 | 0 | } |
136 | | |
137 | 87 | sub_predicate->set_column_name(condition.column_name); |
138 | 87 | sub_predicate->set_op(trans_op(condition.condition_op)); |
139 | 87 | sub_predicate->set_cond_value(condition.condition_values[0]); |
140 | 87 | LOG(INFO) << "store one sub-delete condition. condition=" |
141 | 87 | << fmt::format(" {} {} {}", condition.column_name, condition.condition_op, |
142 | 87 | condition.condition_values[0]); |
143 | 87 | } |
144 | 88 | } |
145 | 71 | del_pred->set_version(-1); |
146 | | |
147 | 71 | return Status::OK(); |
148 | 71 | } |
149 | | |
150 | | Status DeleteHandler::convert_to_sub_pred_v2(DeletePredicatePB* delete_pred, |
151 | 0 | TabletSchemaSPtr schema) { |
152 | 0 | if (!delete_pred->sub_predicates().empty() && delete_pred->sub_predicates_v2().empty()) { |
153 | 0 | for (const auto& condition_str : delete_pred->sub_predicates()) { |
154 | 0 | auto* sub_pred = delete_pred->add_sub_predicates_v2(); |
155 | 0 | TCondition condition; |
156 | 0 | static_cast<void>(parse_condition(condition_str, &condition)); |
157 | 0 | const auto& column = *DORIS_TRY(schema->column(condition.column_name)); |
158 | 0 | sub_pred->set_column_unique_id(column.unique_id()); |
159 | 0 | sub_pred->set_column_name(condition.column_name); |
160 | 0 | sub_pred->set_op(condition.condition_op); |
161 | 0 | sub_pred->set_cond_value(condition.condition_values[0]); |
162 | 0 | } |
163 | 0 | } |
164 | | |
165 | 0 | auto* in_pred_list = delete_pred->mutable_in_predicates(); |
166 | 0 | for (auto& in_pred : *in_pred_list) { |
167 | 0 | const auto& column = *DORIS_TRY(schema->column(in_pred.column_name())); |
168 | 0 | in_pred.set_column_unique_id(column.unique_id()); |
169 | 0 | } |
170 | 0 | return Status::OK(); |
171 | 0 | } |
172 | | |
173 | | bool DeleteHandler::is_condition_value_valid(const TabletColumn& column, |
174 | | const std::string& condition_op, |
175 | 113 | const string& value_str) { |
176 | 113 | if ("IS" == condition_op && ("NULL" == value_str || "NOT NULL" == value_str)) { |
177 | 1 | return true; |
178 | 1 | } |
179 | | |
180 | 112 | FieldType field_type = column.type(); |
181 | 112 | switch (field_type) { |
182 | 11 | case FieldType::OLAP_FIELD_TYPE_TINYINT: |
183 | 11 | return valid_signed_number<int8_t>(value_str); |
184 | 13 | case FieldType::OLAP_FIELD_TYPE_SMALLINT: |
185 | 13 | return valid_signed_number<int16_t>(value_str); |
186 | 54 | case FieldType::OLAP_FIELD_TYPE_INT: |
187 | 54 | return valid_signed_number<int32_t>(value_str); |
188 | 5 | case FieldType::OLAP_FIELD_TYPE_BIGINT: |
189 | 5 | return valid_signed_number<int64_t>(value_str); |
190 | 4 | case FieldType::OLAP_FIELD_TYPE_LARGEINT: |
191 | 4 | return valid_signed_number<int128_t>(value_str); |
192 | 0 | case FieldType::OLAP_FIELD_TYPE_UNSIGNED_TINYINT: |
193 | 0 | return valid_unsigned_number<uint8_t>(value_str); |
194 | 0 | case FieldType::OLAP_FIELD_TYPE_UNSIGNED_SMALLINT: |
195 | 0 | return valid_unsigned_number<uint16_t>(value_str); |
196 | 0 | case FieldType::OLAP_FIELD_TYPE_UNSIGNED_INT: |
197 | 0 | return valid_unsigned_number<uint32_t>(value_str); |
198 | 0 | case FieldType::OLAP_FIELD_TYPE_UNSIGNED_BIGINT: |
199 | 0 | return valid_unsigned_number<uint64_t>(value_str); |
200 | 7 | case FieldType::OLAP_FIELD_TYPE_DECIMAL: |
201 | 7 | return valid_decimal(value_str, column.precision(), column.frac()); |
202 | 0 | case FieldType::OLAP_FIELD_TYPE_DECIMAL32: |
203 | 0 | return valid_decimal(value_str, column.precision(), column.frac()); |
204 | 0 | case FieldType::OLAP_FIELD_TYPE_DECIMAL64: |
205 | 0 | return valid_decimal(value_str, column.precision(), column.frac()); |
206 | 0 | case FieldType::OLAP_FIELD_TYPE_DECIMAL128I: |
207 | 0 | return valid_decimal(value_str, column.precision(), column.frac()); |
208 | 0 | case FieldType::OLAP_FIELD_TYPE_DECIMAL256: |
209 | 0 | return valid_decimal(value_str, column.precision(), column.frac()); |
210 | 3 | case FieldType::OLAP_FIELD_TYPE_CHAR: |
211 | 7 | case FieldType::OLAP_FIELD_TYPE_VARCHAR: |
212 | 7 | return value_str.size() <= column.length(); |
213 | 0 | case FieldType::OLAP_FIELD_TYPE_STRING: |
214 | 0 | return value_str.size() <= config::string_type_length_soft_limit_bytes; |
215 | 5 | case FieldType::OLAP_FIELD_TYPE_DATE: |
216 | 11 | case FieldType::OLAP_FIELD_TYPE_DATETIME: |
217 | 11 | case FieldType::OLAP_FIELD_TYPE_DATEV2: |
218 | 11 | case FieldType::OLAP_FIELD_TYPE_DATETIMEV2: |
219 | 11 | return valid_datetime(value_str, column.frac()); |
220 | 0 | case FieldType::OLAP_FIELD_TYPE_BOOL: |
221 | 0 | return valid_bool(value_str); |
222 | 0 | case FieldType::OLAP_FIELD_TYPE_IPV4: |
223 | 0 | return valid_ipv4(value_str); |
224 | 0 | case FieldType::OLAP_FIELD_TYPE_IPV6: |
225 | 0 | return valid_ipv6(value_str); |
226 | 0 | default: |
227 | 0 | LOG(WARNING) << "unknown field type. [type=" << int(field_type) << "]"; |
228 | 112 | } |
229 | 0 | return false; |
230 | 112 | } |
231 | | |
232 | 113 | Status DeleteHandler::check_condition_valid(const TabletSchema& schema, const TCondition& cond) { |
233 | | // Check whether the column exists |
234 | 113 | int32_t field_index = schema.field_index(cond.column_name); |
235 | 113 | if (field_index < 0) { |
236 | 1 | return Status::Error<ErrorCode::INVALID_ARGUMENT>("field is not existent. [field_index={}]", |
237 | 1 | field_index); |
238 | 1 | } |
239 | | |
240 | | // Delete condition should only applied on key columns or duplicate key table, and |
241 | | // the condition column type should not be float or double. |
242 | 112 | const TabletColumn& column = schema.column(field_index); |
243 | | |
244 | 112 | if (column.type() == FieldType::OLAP_FIELD_TYPE_DOUBLE || |
245 | 112 | column.type() == FieldType::OLAP_FIELD_TYPE_FLOAT) { |
246 | 0 | return Status::Error<ErrorCode::INVALID_ARGUMENT>("data type is float or double."); |
247 | 0 | } |
248 | | |
249 | | // Check operator and operands size are matched. |
250 | 112 | if ("*=" != cond.condition_op && "!*=" != cond.condition_op && |
251 | 112 | cond.condition_values.size() != 1) { |
252 | 0 | return Status::Error<ErrorCode::INVALID_ARGUMENT>("invalid condition value size. [size={}]", |
253 | 0 | cond.condition_values.size()); |
254 | 0 | } |
255 | | |
256 | | // Check each operand is valid |
257 | 113 | for (const auto& condition_value : cond.condition_values) { |
258 | 113 | if (!is_condition_value_valid(column, cond.condition_op, condition_value)) { |
259 | 24 | return Status::Error<ErrorCode::INVALID_ARGUMENT>("invalid condition value. [value={}]", |
260 | 24 | condition_value); |
261 | 24 | } |
262 | 113 | } |
263 | | |
264 | 88 | if (!cond.__isset.column_unique_id) { |
265 | 88 | LOG(WARNING) << "column=" << cond.column_name |
266 | 88 | << " in predicate does not have uid, table id=" << schema.table_id(); |
267 | | // TODO(tsy): make it fail here after FE forbidding hard-link-schema-change |
268 | 88 | return Status::OK(); |
269 | 88 | } |
270 | 0 | if (schema.field_index(cond.column_unique_id) == -1) { |
271 | 0 | const auto& err_msg = |
272 | 0 | fmt::format("column id does not exists in table={}, schema version={},", |
273 | 0 | schema.table_id(), schema.schema_version()); |
274 | 0 | return Status::Error<ErrorCode::INVALID_ARGUMENT>(err_msg); |
275 | 0 | } |
276 | 0 | if (!iequal(schema.column_by_uid(cond.column_unique_id).name(), cond.column_name)) { |
277 | 0 | const auto& err_msg = fmt::format( |
278 | 0 | "colum name={} does not belongs to column uid={}, which " |
279 | 0 | "column name={}, " |
280 | 0 | "delete_cond.column_name ={}", |
281 | 0 | cond.column_name, cond.column_unique_id, |
282 | 0 | schema.column_by_uid(cond.column_unique_id).name(), cond.column_name); |
283 | 0 | return Status::Error<ErrorCode::INVALID_ARGUMENT>(err_msg); |
284 | 0 | } |
285 | | |
286 | 0 | return Status::OK(); |
287 | 0 | } |
288 | | |
289 | 90 | Status DeleteHandler::parse_condition(const DeleteSubPredicatePB& sub_cond, TCondition* condition) { |
290 | 90 | if (!sub_cond.has_column_name() || !sub_cond.has_op() || !sub_cond.has_cond_value()) { |
291 | 0 | return Status::Error<ErrorCode::INVALID_ARGUMENT>( |
292 | 0 | "fail to parse condition. condition={} {} {}", sub_cond.column_name(), |
293 | 0 | sub_cond.op(), sub_cond.cond_value()); |
294 | 0 | } |
295 | 90 | if (sub_cond.has_column_unique_id()) { |
296 | 0 | condition->column_unique_id = sub_cond.column_unique_id(); |
297 | 0 | } |
298 | 90 | condition->column_name = sub_cond.column_name(); |
299 | 90 | condition->condition_op = sub_cond.op(); |
300 | 90 | condition->condition_values.push_back(sub_cond.cond_value()); |
301 | 90 | return Status::OK(); |
302 | 90 | } |
303 | | |
304 | | // clang-format off |
305 | | // Condition string format, the format is (column_name)(op)(value) |
306 | | // eg: condition_str="c1 = 1597751948193618247 and length(source)<1;\n;\n" |
307 | | // column_name: matches "c1", must include FeNameFormat.java COLUMN_NAME_REGEX |
308 | | // and compactible with any the lagacy |
309 | | // operator: matches "=" |
310 | | // value: matches "1597751948193618247 and length(source)<1;\n;\n" |
311 | | // |
312 | | // For more info, see DeleteHandler::construct_sub_predicates |
313 | | // FIXME(gavin): This is a tricky implementation, it should not be the final resolution, refactor it. |
314 | | const char* const CONDITION_STR_PATTERN = |
315 | | // .----------------- column-name --------------------------. .----------------------- operator ------------------------. .------------ value ----------. |
316 | | R"(([_a-zA-Z@0-9\s/\p{L}][.a-zA-Z0-9_+-/?@#$%^&*"\s,:\p{L}]*)\s*((?:=)|(?:!=)|(?:>>)|(?:<<)|(?:>=)|(?:<=)|(?:\*=)|(?: IS ))\s*('((?:[\s\S]+)?)'|(?:[\s\S]+)?))"; |
317 | | // '----------------- group 1 ------------------------------' '--------------------- group 2 ---------------------------' | '-- group 4--' | |
318 | | // match any of: = != >> << >= <= *= " IS " '----------- group 3 ---------' |
319 | | // match **ANY THING** without(4) |
320 | | // or with(3) single quote |
321 | | // clang-format on |
322 | | RE2 DELETE_HANDLER_REGEX(CONDITION_STR_PATTERN); |
323 | | |
324 | 117 | Status DeleteHandler::parse_condition(const std::string& condition_str, TCondition* condition) { |
325 | 117 | std::string col_name, op, value, g4; |
326 | | |
327 | 117 | bool matched = RE2::FullMatch(condition_str, DELETE_HANDLER_REGEX, &col_name, &op, &value, |
328 | 117 | &g4); // exact match |
329 | | |
330 | 117 | if (!matched) { |
331 | 3 | return Status::InvalidArgument("fail to sub condition. condition={}", condition_str); |
332 | 3 | } |
333 | | |
334 | 114 | condition->column_name = col_name; |
335 | 114 | condition->condition_op = op == " IS " ? "IS" : op; |
336 | | // match string with single quotes, a = b or a = 'b' |
337 | 114 | if (!g4.empty()) { |
338 | 93 | condition->condition_values.push_back(g4); |
339 | 93 | } else { |
340 | 21 | condition->condition_values.push_back(value); |
341 | 21 | } |
342 | 114 | VLOG_NOTICE << "parsed condition_str: col_name={" << condition->column_name << "} op={" |
343 | 0 | << condition->condition_op << "} val={" << condition->condition_values.back() |
344 | 0 | << "}"; |
345 | 114 | return Status::OK(); |
346 | 117 | } |
347 | | |
348 | | template <typename SubPredType> |
349 | | requires(std::is_same_v<SubPredType, DeleteSubPredicatePB> or |
350 | | std::is_same_v<SubPredType, std::string>) |
351 | | Status DeleteHandler::_parse_column_pred(TabletSchemaSPtr complete_schema, |
352 | | TabletSchemaSPtr delete_pred_related_schema, |
353 | | const RepeatedPtrField<SubPredType>& sub_pred_list, |
354 | 89 | DeleteConditions* delete_conditions) { |
355 | 100 | for (const auto& sub_predicate : sub_pred_list) { |
356 | 100 | TCondition condition; |
357 | 100 | RETURN_IF_ERROR(parse_condition(sub_predicate, &condition)); |
358 | 100 | int32_t col_unique_id = -1; |
359 | 100 | if constexpr (std::is_same_v<SubPredType, DeleteSubPredicatePB>) { |
360 | 90 | if (sub_predicate.has_column_unique_id()) [[likely]] { |
361 | 0 | col_unique_id = sub_predicate.column_unique_id(); |
362 | 0 | } |
363 | 90 | } |
364 | 100 | if (col_unique_id < 0) { |
365 | 100 | const auto& column = |
366 | 100 | *DORIS_TRY(delete_pred_related_schema->column(condition.column_name)); |
367 | 100 | col_unique_id = column.unique_id(); |
368 | 100 | } |
369 | 100 | condition.__set_column_unique_id(col_unique_id); |
370 | 100 | const auto& column = complete_schema->column_by_uid(col_unique_id); |
371 | 100 | uint32_t index = complete_schema->field_index(col_unique_id); |
372 | 100 | auto* predicate = |
373 | 100 | parse_to_predicate(column, index, condition, _predicate_arena.get(), true); |
374 | 100 | if (predicate != nullptr) { |
375 | 100 | delete_conditions->column_predicate_vec.push_back(predicate); |
376 | 100 | } |
377 | 100 | } |
378 | 89 | return Status::OK(); |
379 | 89 | } _ZN5doris13DeleteHandler18_parse_column_predINS_20DeleteSubPredicatePBEEENS_6StatusESt10shared_ptrINS_12TabletSchemaEES6_RKN6google8protobuf16RepeatedPtrFieldIT_EEPNS_16DeleteConditionsE Line | Count | Source | 354 | 85 | DeleteConditions* delete_conditions) { | 355 | 90 | for (const auto& sub_predicate : sub_pred_list) { | 356 | 90 | TCondition condition; | 357 | 90 | RETURN_IF_ERROR(parse_condition(sub_predicate, &condition)); | 358 | 90 | int32_t col_unique_id = -1; | 359 | 90 | if constexpr (std::is_same_v<SubPredType, DeleteSubPredicatePB>) { | 360 | 90 | if (sub_predicate.has_column_unique_id()) [[likely]] { | 361 | 0 | col_unique_id = sub_predicate.column_unique_id(); | 362 | 0 | } | 363 | 90 | } | 364 | 90 | if (col_unique_id < 0) { | 365 | 90 | const auto& column = | 366 | 90 | *DORIS_TRY(delete_pred_related_schema->column(condition.column_name)); | 367 | 90 | col_unique_id = column.unique_id(); | 368 | 90 | } | 369 | 90 | condition.__set_column_unique_id(col_unique_id); | 370 | 90 | const auto& column = complete_schema->column_by_uid(col_unique_id); | 371 | 90 | uint32_t index = complete_schema->field_index(col_unique_id); | 372 | 90 | auto* predicate = | 373 | 90 | parse_to_predicate(column, index, condition, _predicate_arena.get(), true); | 374 | 90 | if (predicate != nullptr) { | 375 | 90 | delete_conditions->column_predicate_vec.push_back(predicate); | 376 | 90 | } | 377 | 90 | } | 378 | 85 | return Status::OK(); | 379 | 85 | } |
_ZN5doris13DeleteHandler18_parse_column_predINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEENS_6StatusESt10shared_ptrINS_12TabletSchemaEESB_RKN6google8protobuf16RepeatedPtrFieldIT_EEPNS_16DeleteConditionsE Line | Count | Source | 354 | 4 | DeleteConditions* delete_conditions) { | 355 | 10 | for (const auto& sub_predicate : sub_pred_list) { | 356 | 10 | TCondition condition; | 357 | 10 | RETURN_IF_ERROR(parse_condition(sub_predicate, &condition)); | 358 | 10 | int32_t col_unique_id = -1; | 359 | 10 | if constexpr (std::is_same_v<SubPredType, DeleteSubPredicatePB>) { | 360 | 10 | if (sub_predicate.has_column_unique_id()) [[likely]] { | 361 | 10 | col_unique_id = sub_predicate.column_unique_id(); | 362 | 10 | } | 363 | 10 | } | 364 | 10 | if (col_unique_id < 0) { | 365 | 10 | const auto& column = | 366 | 10 | *DORIS_TRY(delete_pred_related_schema->column(condition.column_name)); | 367 | 10 | col_unique_id = column.unique_id(); | 368 | 10 | } | 369 | 10 | condition.__set_column_unique_id(col_unique_id); | 370 | 10 | const auto& column = complete_schema->column_by_uid(col_unique_id); | 371 | 10 | uint32_t index = complete_schema->field_index(col_unique_id); | 372 | 10 | auto* predicate = | 373 | 10 | parse_to_predicate(column, index, condition, _predicate_arena.get(), true); | 374 | 10 | if (predicate != nullptr) { | 375 | 10 | delete_conditions->column_predicate_vec.push_back(predicate); | 376 | 10 | } | 377 | 10 | } | 378 | 4 | return Status::OK(); | 379 | 4 | } |
|
380 | | |
381 | | template Status DeleteHandler::_parse_column_pred<DeleteSubPredicatePB>( |
382 | | TabletSchemaSPtr complete_schema, TabletSchemaSPtr delete_pred_related_schema, |
383 | | const ::google::protobuf::RepeatedPtrField<DeleteSubPredicatePB>& sub_pred_list, |
384 | | DeleteConditions* delete_conditions); |
385 | | |
386 | | template Status DeleteHandler::_parse_column_pred<std::string>( |
387 | | TabletSchemaSPtr complete_schema, TabletSchemaSPtr delete_pred_related_schema, |
388 | | const ::google::protobuf::RepeatedPtrField<std::string>& sub_pred_list, |
389 | | DeleteConditions* delete_conditions); |
390 | | |
391 | | Status DeleteHandler::init(TabletSchemaSPtr tablet_schema, |
392 | 168 | const std::vector<RowsetMetaSharedPtr>& delete_preds, int64_t version) { |
393 | 168 | DCHECK(!_is_inited) << "reinitialize delete handler."; |
394 | 168 | DCHECK(version >= 0) << "invalid parameters. version=" << version; |
395 | 168 | _predicate_arena = std::make_unique<vectorized::Arena>(); |
396 | | |
397 | 168 | for (const auto& delete_pred : delete_preds) { |
398 | | // Skip the delete condition with large version |
399 | 89 | if (delete_pred->version().first > version) { |
400 | 0 | continue; |
401 | 0 | } |
402 | | // Need the tablet schema at the delete condition to parse the accurate column |
403 | 89 | const auto& delete_pred_related_schema = delete_pred->tablet_schema(); |
404 | 89 | const auto& delete_condition = delete_pred->delete_predicate(); |
405 | 89 | DeleteConditions temp; |
406 | 89 | temp.filter_version = delete_pred->version().first; |
407 | 89 | if (!delete_condition.sub_predicates_v2().empty()) { |
408 | 85 | RETURN_IF_ERROR(_parse_column_pred(tablet_schema, delete_pred_related_schema, |
409 | 85 | delete_condition.sub_predicates_v2(), &temp)); |
410 | 85 | } else { |
411 | | // make it compatible with the former versions |
412 | 4 | RETURN_IF_ERROR(_parse_column_pred(tablet_schema, delete_pred_related_schema, |
413 | 4 | delete_condition.sub_predicates(), &temp)); |
414 | 4 | } |
415 | 89 | for (const auto& in_predicate : delete_condition.in_predicates()) { |
416 | 0 | TCondition condition; |
417 | 0 | condition.__set_column_name(in_predicate.column_name()); |
418 | |
|
419 | 0 | int32_t col_unique_id = -1; |
420 | 0 | if (in_predicate.has_column_unique_id()) { |
421 | 0 | col_unique_id = in_predicate.column_unique_id(); |
422 | 0 | } else { |
423 | | // if upgrade from version 2.0.x, column_unique_id maybe not set |
424 | 0 | const auto& pre_column = |
425 | 0 | *DORIS_TRY(delete_pred_related_schema->column(condition.column_name)); |
426 | 0 | col_unique_id = pre_column.unique_id(); |
427 | 0 | } |
428 | 0 | if (col_unique_id == -1) { |
429 | 0 | return Status::Error<ErrorCode::DELETE_INVALID_CONDITION>( |
430 | 0 | "cannot get column_unique_id for column {}", condition.column_name); |
431 | 0 | } |
432 | 0 | condition.__set_column_unique_id(col_unique_id); |
433 | |
|
434 | 0 | if (in_predicate.is_not_in()) { |
435 | 0 | condition.__set_condition_op("!*="); |
436 | 0 | } else { |
437 | 0 | condition.__set_condition_op("*="); |
438 | 0 | } |
439 | 0 | for (const auto& value : in_predicate.values()) { |
440 | 0 | condition.condition_values.push_back(value); |
441 | 0 | } |
442 | 0 | const auto& column = tablet_schema->column_by_uid(col_unique_id); |
443 | 0 | uint32_t index = tablet_schema->field_index(col_unique_id); |
444 | 0 | temp.column_predicate_vec.push_back( |
445 | 0 | parse_to_predicate(column, index, condition, _predicate_arena.get(), true)); |
446 | 0 | } |
447 | | |
448 | 89 | _del_conds.emplace_back(std::move(temp)); |
449 | 89 | } |
450 | | |
451 | 168 | _is_inited = true; |
452 | | |
453 | 168 | return Status::OK(); |
454 | 168 | } |
455 | | |
456 | 170 | DeleteHandler::~DeleteHandler() { |
457 | 170 | if (!_is_inited) { |
458 | 2 | return; |
459 | 2 | } |
460 | | |
461 | 168 | for (auto& cond : _del_conds) { |
462 | 100 | for (const auto* pred : cond.column_predicate_vec) { |
463 | 100 | delete pred; |
464 | 100 | } |
465 | 89 | } |
466 | | |
467 | 168 | _del_conds.clear(); |
468 | 168 | _is_inited = false; |
469 | 168 | } |
470 | | |
471 | | void DeleteHandler::get_delete_conditions_after_version( |
472 | | int64_t version, AndBlockColumnPredicate* and_block_column_predicate_ptr, |
473 | | std::unordered_map<int32_t, std::vector<const ColumnPredicate*>>* |
474 | 423 | del_predicates_for_zone_map) const { |
475 | 423 | for (const auto& del_cond : _del_conds) { |
476 | 267 | if (del_cond.filter_version > version) { |
477 | | // now, only query support delete column predicate operator |
478 | 190 | if (!del_cond.column_predicate_vec.empty()) { |
479 | 190 | if (del_cond.column_predicate_vec.size() == 1) { |
480 | 190 | auto single_column_block_predicate = SingleColumnBlockPredicate::create_unique( |
481 | 190 | del_cond.column_predicate_vec[0]); |
482 | 190 | and_block_column_predicate_ptr->add_column_predicate( |
483 | 190 | std::move(single_column_block_predicate)); |
484 | 190 | if (del_predicates_for_zone_map->count( |
485 | 190 | del_cond.column_predicate_vec[0]->column_id()) < 1) { |
486 | 190 | del_predicates_for_zone_map->insert( |
487 | 190 | {del_cond.column_predicate_vec[0]->column_id(), |
488 | 190 | std::vector<const ColumnPredicate*> {}}); |
489 | 190 | } |
490 | 190 | (*del_predicates_for_zone_map)[del_cond.column_predicate_vec[0]->column_id()] |
491 | 190 | .push_back(del_cond.column_predicate_vec[0]); |
492 | 190 | } else { |
493 | 0 | auto or_column_predicate = OrBlockColumnPredicate::create_unique(); |
494 | | |
495 | | // build or_column_predicate |
496 | | // when delete from where a = 1 and b = 2, we can not use del_predicates_for_zone_map to filter zone page, |
497 | | // so here do not put predicate to del_predicates_for_zone_map, |
498 | | // refer #17145 for more details. |
499 | | // // TODO: need refactor design and code to use more version delete and more column delete to filter zone page. |
500 | 0 | std::for_each(del_cond.column_predicate_vec.cbegin(), |
501 | 0 | del_cond.column_predicate_vec.cend(), |
502 | 0 | [&or_column_predicate](const ColumnPredicate* predicate) { |
503 | 0 | or_column_predicate->add_column_predicate( |
504 | 0 | SingleColumnBlockPredicate::create_unique(predicate)); |
505 | 0 | }); |
506 | 0 | and_block_column_predicate_ptr->add_column_predicate( |
507 | 0 | std::move(or_column_predicate)); |
508 | 0 | } |
509 | 190 | } |
510 | 190 | } |
511 | 267 | } |
512 | 423 | } |
513 | | |
514 | | } // namespace doris |