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