/root/doris/be/src/olap/delete_handler.h
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 | | #pragma once |
19 | | |
20 | | #include <butil/macros.h> |
21 | | |
22 | | #include <cstdint> |
23 | | #include <string> |
24 | | #include <type_traits> |
25 | | |
26 | | #include "common/factory_creator.h" |
27 | | #include "common/status.h" |
28 | | #include "olap/rowset/rowset_meta.h" |
29 | | #include "olap/tablet_schema.h" |
30 | | #include "vec/common/arena.h" |
31 | | |
32 | | namespace doris { |
33 | | |
34 | | class AndBlockColumnPredicate; |
35 | | class ColumnPredicate; |
36 | | class DeletePredicatePB; |
37 | | class TCondition; |
38 | | |
39 | | // Represent a delete condition. |
40 | | struct DeleteConditions { |
41 | | int64_t filter_version = 0; // The version of this condition |
42 | | std::vector<const ColumnPredicate*> column_predicate_vec; |
43 | | }; |
44 | | |
45 | | // This class is used for checking whether a row should be deleted. |
46 | | // It is used in the following processes: |
47 | | // 1. Create and initialize a DeleteHandler object: |
48 | | // Status res; |
49 | | // DeleteHandler delete_handler; |
50 | | // res = delete_handler.init(tablet, condition_version); |
51 | | // 2. After all rows have been checked, you should release this object by calling: |
52 | | // delete_handler.finalize(); |
53 | | // |
54 | | // NOTE: |
55 | | // * In the first step, before calling delete_handler.init(), you should lock the tablet's header file. |
56 | | class DeleteHandler { |
57 | | ENABLE_FACTORY_CREATOR(DeleteHandler); |
58 | | // These static method is used to generate delete predicate pb during write or push handler |
59 | | public: |
60 | | // generated DeletePredicatePB by TCondition |
61 | | static Status generate_delete_predicate(const TabletSchema& schema, |
62 | | const std::vector<TCondition>& conditions, |
63 | | DeletePredicatePB* del_pred); |
64 | | |
65 | | static Status convert_to_sub_pred_v2(DeletePredicatePB* delete_pred, TabletSchemaSPtr schema); |
66 | | |
67 | | /** |
68 | | * Use regular expression to extract 'column_name', 'op' and 'operands' |
69 | | * |
70 | | * @param condition_str input predicate string in form of `X OP Y` |
71 | | * @param condition output param |
72 | | * @return OK if matched and extracted correctly otherwise DELETE_INVALID_PARAMETERS |
73 | | */ |
74 | | static Status parse_condition(const std::string& condition_str, TCondition* condition); |
75 | | |
76 | | private: |
77 | | // Validate the condition on the schema. |
78 | | static Status check_condition_valid(const TabletSchema& tablet_schema, const TCondition& cond); |
79 | | |
80 | | // Check whether the condition value is valid according to its type. |
81 | | // 1. For integers(int8,int16,in32,int64,uint8,uint16,uint32,uint64), check whether they are overflow |
82 | | // 2. For decimal, check whether precision or scale is overflow |
83 | | // 3. For date and datetime, check format and value |
84 | | // 4. For char and varchar, check length |
85 | | static bool is_condition_value_valid(const TabletColumn& column, |
86 | | const std::string& condition_op, |
87 | | const std::string& value_str); |
88 | | |
89 | | // extract 'column_name', 'op' and 'operands' to condition |
90 | | static Status parse_condition(const DeleteSubPredicatePB& sub_cond, TCondition* condition); |
91 | | |
92 | | public: |
93 | 170 | DeleteHandler() = default; |
94 | | ~DeleteHandler(); |
95 | | |
96 | | // Initialize DeleteHandler, use the delete conditions of this tablet whose version less than or equal to |
97 | | // 'version' to fill '_del_conds'. |
98 | | // NOTE: You should lock the tablet's header file before calling this function. |
99 | | // input: |
100 | | // * schema: tablet's schema, the delete conditions and data rows are in this schema |
101 | | // * version: maximum version |
102 | | // * with_sub_pred_v2: whether to use delete sub predicate v2 (v2 is based on PB and use column uid to specify a column, |
103 | | // v1 is based on condition string, and relies on regex for parse) |
104 | | // return: |
105 | | // * Status::Error<DELETE_INVALID_PARAMETERS>(): input parameters are not valid |
106 | | // * Status::Error<MEM_ALLOC_FAILED>(): alloc memory failed |
107 | | Status init(TabletSchemaSPtr tablet_schema, |
108 | | const std::vector<RowsetMetaSharedPtr>& delete_preds, int64_t version); |
109 | | |
110 | 0 | [[nodiscard]] bool empty() const { return _del_conds.empty(); } |
111 | | |
112 | | void get_delete_conditions_after_version( |
113 | | int64_t version, AndBlockColumnPredicate* and_block_column_predicate_ptr, |
114 | | std::unordered_map<int32_t, std::vector<const ColumnPredicate*>>* |
115 | | del_predicates_for_zone_map) const; |
116 | | |
117 | | private: |
118 | | template <typename SubPredType> |
119 | | requires(std::is_same_v<SubPredType, DeleteSubPredicatePB> or |
120 | | std::is_same_v<SubPredType, std::string>) |
121 | | Status _parse_column_pred( |
122 | | TabletSchemaSPtr complete_schema, TabletSchemaSPtr delete_pred_related_schema, |
123 | | const ::google::protobuf::RepeatedPtrField<SubPredType>& sub_pred_list, |
124 | | DeleteConditions* delete_conditions); |
125 | | |
126 | | bool _is_inited = false; |
127 | | // DeleteConditions in _del_conds are in 'OR' relationship |
128 | | std::vector<DeleteConditions> _del_conds; |
129 | | std::unique_ptr<vectorized::Arena> _predicate_arena; |
130 | | |
131 | | DISALLOW_COPY_AND_ASSIGN(DeleteHandler); |
132 | | }; |
133 | | |
134 | | } // namespace doris |