Coverage Report

Created: 2026-01-05 12:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/olap/delete_handler.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 "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
101
std::string construct_sub_predicate(const TCondition& condition) {
48
101
    string op = condition.condition_op;
49
101
    if (op == "<") {
50
50
        op += "<";
51
51
    } else if (op == ">") {
52
3
        op += ">";
53
3
    }
54
101
    string condition_str;
55
101
    if ("IS" == op) {
56
        // ATTN: tricky! Surround IS with spaces to make it "special"
57
2
        condition_str = condition.column_name + " IS " + condition.condition_values[0];
58
99
    } else { // multi-elements IN expr has been processed with InPredicatePB
59
99
        if (op == "*=") {
60
2
            op = "=";
61
97
        } else if (op == "!*=") {
62
2
            op = "!=";
63
2
        }
64
99
        condition_str = condition.column_name + op + "'" + condition.condition_values[0] + "'";
65
99
    }
66
101
    return condition_str;
67
101
}
68
69
// make operators from FE adaptive to BE
70
101
std::string trans_op(const std::string& opt) {
71
101
    std::string op = string(opt);
72
101
    if (op == "<") {
73
50
        op += "<";
74
51
    } else if (op == ">") {
75
3
        op += ">";
76
3
    }
77
101
    if ("IS" != op) {
78
99
        if (op == "*=") {
79
2
            op = "=";
80
97
        } else if (op == "!*=") {
81
2
            op = "!=";
82
2
        }
83
99
    }
84
101
    return op;
85
101
}
86
87
Status DeleteHandler::generate_delete_predicate(const TabletSchema& schema,
88
                                                const std::vector<TCondition>& conditions,
89
106
                                                DeletePredicatePB* del_pred) {
90
106
    DBUG_EXECUTE_IF("DeleteHandler::generate_delete_predicate.inject_failure", {
91
106
        return Status::Error<false>(dp->param<int>("error_code"),
92
106
                                    dp->param<std::string>("error_msg"));
93
106
    })
94
106
    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
135
    for (const TCondition& condition : conditions) {
101
135
        RETURN_IF_ERROR(check_condition_valid(schema, condition));
102
135
    }
103
104
    // Store delete condition
105
105
    for (const TCondition& condition : conditions) {
106
105
        if (condition.condition_values.size() > 1) {
107
4
            InPredicatePB* in_pred = del_pred->add_in_predicates();
108
4
            if (condition.__isset.column_unique_id) {
109
0
                in_pred->set_column_unique_id(condition.column_unique_id);
110
0
            }
111
4
            in_pred->set_column_name(condition.column_name);
112
4
            bool is_not_in = condition.condition_op == "!*=";
113
4
            in_pred->set_is_not_in(is_not_in);
114
10
            for (const auto& condition_value : condition.condition_values) {
115
10
                in_pred->add_values(condition_value);
116
10
            }
117
118
4
            LOG(INFO) << "store one sub-delete condition. condition name=" << in_pred->column_name()
119
4
                      << "condition size=" << in_pred->values().size();
120
101
        } else {
121
            // write sub predicate v1 for compactbility
122
101
            std::string condition_str = construct_sub_predicate(condition);
123
101
            VLOG_NOTICE << __PRETTY_FUNCTION__ << " condition_str: " << condition_str;
124
101
            del_pred->add_sub_predicates(condition_str);
125
101
            DeleteSubPredicatePB* sub_predicate = del_pred->add_sub_predicates_v2();
126
101
            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
101
            } 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
101
            sub_predicate->set_column_name(condition.column_name);
138
101
            sub_predicate->set_op(trans_op(condition.condition_op));
139
101
            sub_predicate->set_cond_value(condition.condition_values[0]);
140
101
            LOG(INFO) << "store one sub-delete condition. condition="
141
101
                      << fmt::format(" {} {} {}", condition.column_name, condition.condition_op,
142
101
                                     condition.condition_values[0]);
143
101
        }
144
105
    }
145
75
    del_pred->set_version(-1);
146
147
75
    return Status::OK();
148
75
}
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
140
                                             const string& value_str) {
176
140
    if ("IS" == condition_op && ("NULL" == value_str || "NOT NULL" == value_str)) {
177
2
        return true;
178
2
    }
179
180
138
    FieldType field_type = column.type();
181
138
    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
24
    case FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ:
220
24
        return valid_datetime(value_str, column.frac());
221
0
    case FieldType::OLAP_FIELD_TYPE_BOOL:
222
0
        return valid_bool(value_str);
223
5
    case FieldType::OLAP_FIELD_TYPE_IPV4:
224
5
        return valid_ipv4(value_str);
225
8
    case FieldType::OLAP_FIELD_TYPE_IPV6:
226
8
        return valid_ipv6(value_str);
227
0
    default:
228
0
        LOG(WARNING) << "unknown field type. [type=" << int(field_type) << "]";
229
138
    }
230
0
    return false;
231
138
}
232
233
135
Status DeleteHandler::check_condition_valid(const TabletSchema& schema, const TCondition& cond) {
234
    // Check whether the column exists
235
135
    int32_t field_index = schema.field_index(cond.column_name);
236
135
    if (field_index < 0) {
237
1
        return Status::Error<ErrorCode::INVALID_ARGUMENT>("field is not existent. [field_index={}]",
238
1
                                                          field_index);
239
1
    }
240
241
    // Delete condition should only applied on key columns or duplicate key table, and
242
    // the condition column type should not be float or double.
243
134
    const TabletColumn& column = schema.column(field_index);
244
245
134
    if (column.type() == FieldType::OLAP_FIELD_TYPE_DOUBLE ||
246
134
        column.type() == FieldType::OLAP_FIELD_TYPE_FLOAT) {
247
0
        return Status::Error<ErrorCode::INVALID_ARGUMENT>("data type is float or double.");
248
0
    }
249
250
    // Check operator and operands size are matched.
251
134
    if ("*=" != cond.condition_op && "!*=" != cond.condition_op &&
252
134
        cond.condition_values.size() != 1) {
253
0
        return Status::Error<ErrorCode::INVALID_ARGUMENT>("invalid condition value size. [size={}]",
254
0
                                                          cond.condition_values.size());
255
0
    }
256
257
    // Check each operand is valid
258
140
    for (const auto& condition_value : cond.condition_values) {
259
140
        if (!is_condition_value_valid(column, cond.condition_op, condition_value)) {
260
29
            return Status::Error<ErrorCode::INVALID_ARGUMENT>("invalid condition value. [value={}]",
261
29
                                                              condition_value);
262
29
        }
263
140
    }
264
265
105
    if (!cond.__isset.column_unique_id) {
266
105
        LOG(WARNING) << "column=" << cond.column_name
267
105
                     << " in predicate does not have uid, table id=" << schema.table_id();
268
        // TODO(tsy): make it fail here after FE forbidding hard-link-schema-change
269
105
        return Status::OK();
270
105
    }
271
0
    if (schema.field_index(cond.column_unique_id) == -1) {
272
0
        const auto& err_msg =
273
0
                fmt::format("column id does not exists in table={}, schema version={},",
274
0
                            schema.table_id(), schema.schema_version());
275
0
        return Status::Error<ErrorCode::INVALID_ARGUMENT>(err_msg);
276
0
    }
277
0
    if (!iequal(schema.column_by_uid(cond.column_unique_id).name(), cond.column_name)) {
278
0
        const auto& err_msg = fmt::format(
279
0
                "colum name={} does not belongs to column uid={}, which "
280
0
                "column name={}, "
281
0
                "delete_cond.column_name ={}",
282
0
                cond.column_name, cond.column_unique_id,
283
0
                schema.column_by_uid(cond.column_unique_id).name(), cond.column_name);
284
0
        return Status::Error<ErrorCode::INVALID_ARGUMENT>(err_msg);
285
0
    }
286
287
0
    return Status::OK();
288
0
}
289
290
100
Status DeleteHandler::parse_condition(const DeleteSubPredicatePB& sub_cond, TCondition* condition) {
291
100
    if (!sub_cond.has_column_name() || !sub_cond.has_op() || !sub_cond.has_cond_value()) {
292
0
        return Status::Error<ErrorCode::INVALID_ARGUMENT>(
293
0
                "fail to parse condition. condition={} {} {}", sub_cond.column_name(),
294
0
                sub_cond.op(), sub_cond.cond_value());
295
0
    }
296
100
    if (sub_cond.has_column_unique_id()) {
297
0
        condition->column_unique_id = sub_cond.column_unique_id();
298
0
    }
299
100
    condition->column_name = sub_cond.column_name();
300
100
    condition->condition_op = sub_cond.op();
301
100
    condition->condition_values.push_back(sub_cond.cond_value());
302
100
    return Status::OK();
303
100
}
304
305
// clang-format off
306
// Condition string format, the format is (column_name)(op)(value)
307
// eg: condition_str="c1 = 1597751948193618247 and length(source)<1;\n;\n"
308
// column_name: matches "c1", must include FeNameFormat.java COLUMN_NAME_REGEX
309
//              and compactible with any the lagacy
310
// operator: matches "="
311
// value: matches "1597751948193618247  and length(source)<1;\n;\n"
312
//
313
// For more info, see DeleteHandler::construct_sub_predicates
314
// FIXME(gavin): This is a tricky implementation, it should not be the final resolution, refactor it.
315
const char* const CONDITION_STR_PATTERN =
316
    // .----------------- column-name --------------------------.   .----------------------- operator ------------------------.   .------------ value ----------.
317
    R"(([_a-zA-Z@0-9\s/\p{L}][.a-zA-Z0-9_+-/?@#$%^&*"\s,:\p{L}]*)\s*((?:=)|(?:!=)|(?:>>)|(?:<<)|(?:>=)|(?:<=)|(?:\*=)|(?: IS ))\s*('((?:[\s\S]+)?)'|(?:[\s\S]+)?))";
318
    // '----------------- group 1 ------------------------------'   '--------------------- group 2 ---------------------------'   | '-- group 4--'              |
319
    //                                                                   match any of: = != >> << >= <= *= " IS "                 '----------- group 3 ---------'
320
    //                                                                                                                             match **ANY THING** without(4)
321
    //                                                                                                                             or with(3) single quote
322
// clang-format on
323
RE2 DELETE_HANDLER_REGEX(CONDITION_STR_PATTERN);
324
325
202
Status DeleteHandler::parse_condition(const std::string& condition_str, TCondition* condition) {
326
202
    std::string col_name, op, value, g4;
327
328
202
    bool matched = RE2::FullMatch(condition_str, DELETE_HANDLER_REGEX, &col_name, &op, &value,
329
202
                                  &g4); // exact match
330
331
202
    if (!matched) {
332
3
        return Status::InvalidArgument("fail to sub condition. condition={}", condition_str);
333
3
    }
334
335
199
    condition->column_name = col_name;
336
199
    condition->condition_op = op == " IS " ? "IS" : op;
337
    // match string with single quotes, a = b  or a = 'b'
338
199
    if (!g4.empty()) {
339
142
        condition->condition_values.push_back(g4);
340
142
    } else {
341
57
        condition->condition_values.push_back(value);
342
57
    }
343
199
    VLOG_NOTICE << "parsed condition_str: col_name={" << condition->column_name << "} op={"
344
50
                << condition->condition_op << "} val={" << condition->condition_values.back()
345
50
                << "}";
346
199
    return Status::OK();
347
202
}
348
349
template <typename SubPredType>
350
    requires(std::is_same_v<SubPredType, DeleteSubPredicatePB> or
351
             std::is_same_v<SubPredType, std::string>)
352
Status DeleteHandler::_parse_column_pred(TabletSchemaSPtr complete_schema,
353
                                         TabletSchemaSPtr delete_pred_related_schema,
354
                                         const RepeatedPtrField<SubPredType>& sub_pred_list,
355
169
                                         DeleteConditions* delete_conditions) {
356
181
    for (const auto& sub_predicate : sub_pred_list) {
357
181
        TCondition condition;
358
181
        RETURN_IF_ERROR(parse_condition(sub_predicate, &condition));
359
181
        int32_t col_unique_id = -1;
360
181
        if constexpr (std::is_same_v<SubPredType, DeleteSubPredicatePB>) {
361
100
            if (sub_predicate.has_column_unique_id()) [[likely]] {
362
0
                col_unique_id = sub_predicate.column_unique_id();
363
0
            }
364
100
        }
365
181
        if (col_unique_id < 0) {
366
181
            const auto& column =
367
181
                    *DORIS_TRY(delete_pred_related_schema->column(condition.column_name));
368
181
            col_unique_id = column.unique_id();
369
181
        }
370
181
        condition.__set_column_unique_id(col_unique_id);
371
181
        const auto& column = complete_schema->column_by_uid(col_unique_id);
372
181
        uint32_t index = complete_schema->field_index(col_unique_id);
373
181
        auto* predicate = parse_to_predicate(column, index, condition, _predicate_arena, true);
374
181
        if (predicate != nullptr) {
375
174
            delete_conditions->column_predicate_vec.push_back(predicate);
376
174
        }
377
181
    }
378
169
    return Status::OK();
379
169
}
_ZN5doris13DeleteHandler18_parse_column_predINS_20DeleteSubPredicatePBEQoosr3stdE9is_same_vIT_S2_Esr3stdE9is_same_vIS3_NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEENS_6StatusESt10shared_ptrINS_12TabletSchemaEESD_RKN6google8protobuf16RepeatedPtrFieldIS3_EEPNS_16DeleteConditionsE
Line
Count
Source
355
87
                                         DeleteConditions* delete_conditions) {
356
100
    for (const auto& sub_predicate : sub_pred_list) {
357
100
        TCondition condition;
358
100
        RETURN_IF_ERROR(parse_condition(sub_predicate, &condition));
359
100
        int32_t col_unique_id = -1;
360
100
        if constexpr (std::is_same_v<SubPredType, DeleteSubPredicatePB>) {
361
100
            if (sub_predicate.has_column_unique_id()) [[likely]] {
362
0
                col_unique_id = sub_predicate.column_unique_id();
363
0
            }
364
100
        }
365
100
        if (col_unique_id < 0) {
366
100
            const auto& column =
367
100
                    *DORIS_TRY(delete_pred_related_schema->column(condition.column_name));
368
100
            col_unique_id = column.unique_id();
369
100
        }
370
100
        condition.__set_column_unique_id(col_unique_id);
371
100
        const auto& column = complete_schema->column_by_uid(col_unique_id);
372
100
        uint32_t index = complete_schema->field_index(col_unique_id);
373
100
        auto* predicate = parse_to_predicate(column, index, condition, _predicate_arena, true);
374
100
        if (predicate != nullptr) {
375
100
            delete_conditions->column_predicate_vec.push_back(predicate);
376
100
        }
377
100
    }
378
87
    return Status::OK();
379
87
}
_ZN5doris13DeleteHandler18_parse_column_predINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEQoosr3stdE9is_same_vIT_NS_20DeleteSubPredicatePBEEsr3stdE9is_same_vIS8_S7_EEENS_6StatusESt10shared_ptrINS_12TabletSchemaEESD_RKN6google8protobuf16RepeatedPtrFieldIS8_EEPNS_16DeleteConditionsE
Line
Count
Source
355
82
                                         DeleteConditions* delete_conditions) {
356
82
    for (const auto& sub_predicate : sub_pred_list) {
357
81
        TCondition condition;
358
81
        RETURN_IF_ERROR(parse_condition(sub_predicate, &condition));
359
81
        int32_t col_unique_id = -1;
360
        if constexpr (std::is_same_v<SubPredType, DeleteSubPredicatePB>) {
361
            if (sub_predicate.has_column_unique_id()) [[likely]] {
362
                col_unique_id = sub_predicate.column_unique_id();
363
            }
364
        }
365
81
        if (col_unique_id < 0) {
366
81
            const auto& column =
367
81
                    *DORIS_TRY(delete_pred_related_schema->column(condition.column_name));
368
81
            col_unique_id = column.unique_id();
369
81
        }
370
81
        condition.__set_column_unique_id(col_unique_id);
371
81
        const auto& column = complete_schema->column_by_uid(col_unique_id);
372
81
        uint32_t index = complete_schema->field_index(col_unique_id);
373
81
        auto* predicate = parse_to_predicate(column, index, condition, _predicate_arena, true);
374
81
        if (predicate != nullptr) {
375
74
            delete_conditions->column_predicate_vec.push_back(predicate);
376
74
        }
377
81
    }
378
82
    return Status::OK();
379
82
}
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
371
                           const std::vector<RowsetMetaSharedPtr>& delete_preds, int64_t version) {
393
371
    DCHECK(!_is_inited) << "reinitialize delete handler.";
394
371
    DCHECK(version >= 0) << "invalid parameters. version=" << version;
395
396
371
    for (const auto& delete_pred : delete_preds) {
397
        // Skip the delete condition with large version
398
169
        if (delete_pred->version().first > version) {
399
0
            continue;
400
0
        }
401
        // Need the tablet schema at the delete condition to parse the accurate column
402
169
        const auto& delete_pred_related_schema = delete_pred->tablet_schema();
403
169
        const auto& delete_condition = delete_pred->delete_predicate();
404
169
        DeleteConditions temp;
405
169
        temp.filter_version = delete_pred->version().first;
406
169
        if (!delete_condition.sub_predicates_v2().empty()) {
407
87
            RETURN_IF_ERROR(_parse_column_pred(tablet_schema, delete_pred_related_schema,
408
87
                                               delete_condition.sub_predicates_v2(), &temp));
409
87
        } else {
410
            // make it compatible with the former versions
411
82
            RETURN_IF_ERROR(_parse_column_pred(tablet_schema, delete_pred_related_schema,
412
82
                                               delete_condition.sub_predicates(), &temp));
413
82
        }
414
169
        for (const auto& in_predicate : delete_condition.in_predicates()) {
415
2
            TCondition condition;
416
2
            condition.__set_column_name(in_predicate.column_name());
417
418
2
            int32_t col_unique_id = -1;
419
2
            if (in_predicate.has_column_unique_id()) {
420
0
                col_unique_id = in_predicate.column_unique_id();
421
2
            } else {
422
                // if upgrade from version 2.0.x, column_unique_id maybe not set
423
2
                const auto& pre_column =
424
2
                        *DORIS_TRY(delete_pred_related_schema->column(condition.column_name));
425
2
                col_unique_id = pre_column.unique_id();
426
2
            }
427
2
            if (col_unique_id == -1) {
428
0
                return Status::Error<ErrorCode::DELETE_INVALID_CONDITION>(
429
0
                        "cannot get column_unique_id for column {}", condition.column_name);
430
0
            }
431
2
            condition.__set_column_unique_id(col_unique_id);
432
433
2
            if (in_predicate.is_not_in()) {
434
1
                condition.__set_condition_op("!*=");
435
1
            } else {
436
1
                condition.__set_condition_op("*=");
437
1
            }
438
5
            for (const auto& value : in_predicate.values()) {
439
5
                condition.condition_values.push_back(value);
440
5
            }
441
2
            const auto& column = tablet_schema->column_by_uid(col_unique_id);
442
2
            uint32_t index = tablet_schema->field_index(col_unique_id);
443
2
            temp.column_predicate_vec.push_back(
444
2
                    parse_to_predicate(column, index, condition, _predicate_arena, true));
445
2
        }
446
447
169
        _del_conds.emplace_back(std::move(temp));
448
169
    }
449
450
371
    _is_inited = true;
451
452
371
    return Status::OK();
453
371
}
454
455
388
DeleteHandler::~DeleteHandler() {
456
388
    if (!_is_inited) {
457
24
        return;
458
24
    }
459
460
364
    for (auto& cond : _del_conds) {
461
176
        for (const auto* pred : cond.column_predicate_vec) {
462
176
            delete pred;
463
176
        }
464
162
    }
465
466
364
    _del_conds.clear();
467
364
    _is_inited = false;
468
364
}
469
470
void DeleteHandler::get_delete_conditions_after_version(
471
        int64_t version, AndBlockColumnPredicate* and_block_column_predicate_ptr,
472
        std::unordered_map<int32_t, std::vector<const ColumnPredicate*>>*
473
1.07k
                del_predicates_for_zone_map) const {
474
1.07k
    for (const auto& del_cond : _del_conds) {
475
555
        if (del_cond.filter_version > version) {
476
            // now, only query support delete column predicate operator
477
406
            if (!del_cond.column_predicate_vec.empty()) {
478
406
                if (del_cond.column_predicate_vec.size() == 1) {
479
406
                    auto single_column_block_predicate = SingleColumnBlockPredicate::create_unique(
480
406
                            del_cond.column_predicate_vec[0]);
481
406
                    and_block_column_predicate_ptr->add_column_predicate(
482
406
                            std::move(single_column_block_predicate));
483
406
                    if (del_predicates_for_zone_map->count(
484
406
                                del_cond.column_predicate_vec[0]->column_id()) < 1) {
485
406
                        del_predicates_for_zone_map->insert(
486
406
                                {del_cond.column_predicate_vec[0]->column_id(),
487
406
                                 std::vector<const ColumnPredicate*> {}});
488
406
                    }
489
406
                    (*del_predicates_for_zone_map)[del_cond.column_predicate_vec[0]->column_id()]
490
406
                            .push_back(del_cond.column_predicate_vec[0]);
491
406
                } else {
492
0
                    auto or_column_predicate = OrBlockColumnPredicate::create_unique();
493
494
                    // build or_column_predicate
495
                    // when delete from where a = 1 and b = 2, we can not use del_predicates_for_zone_map to filter zone page,
496
                    // so here do not put predicate to del_predicates_for_zone_map,
497
                    // refer #17145 for more details.
498
                    // // TODO: need refactor design and code to use more version delete and more column delete to filter zone page.
499
0
                    std::for_each(del_cond.column_predicate_vec.cbegin(),
500
0
                                  del_cond.column_predicate_vec.cend(),
501
0
                                  [&or_column_predicate](const ColumnPredicate* predicate) {
502
0
                                      or_column_predicate->add_column_predicate(
503
0
                                              SingleColumnBlockPredicate::create_unique(predicate));
504
0
                                  });
505
0
                    and_block_column_predicate_ptr->add_column_predicate(
506
0
                            std::move(or_column_predicate));
507
0
                }
508
406
            }
509
406
        }
510
555
    }
511
1.07k
}
512
513
} // namespace doris