be/src/storage/tablet_info.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 "storage/tablet_info.h" |
19 | | |
20 | | #include <butil/logging.h> |
21 | | #include <gen_cpp/Descriptors_types.h> |
22 | | #include <gen_cpp/Exprs_types.h> |
23 | | #include <gen_cpp/Partitions_types.h> |
24 | | #include <gen_cpp/Types_types.h> |
25 | | #include <gen_cpp/descriptors.pb.h> |
26 | | #include <gen_cpp/olap_file.pb.h> |
27 | | #include <glog/logging.h> |
28 | | |
29 | | #include <algorithm> |
30 | | #include <cstddef> |
31 | | #include <cstdint> |
32 | | #include <memory> |
33 | | #include <ostream> |
34 | | #include <string> |
35 | | #include <tuple> |
36 | | |
37 | | #include "common/exception.h" |
38 | | #include "common/logging.h" |
39 | | #include "common/status.h" |
40 | | #include "core/column/column.h" |
41 | | #include "core/data_type/data_type.h" |
42 | | #include "core/data_type/data_type_factory.hpp" |
43 | | #include "core/data_type/define_primitive_type.h" |
44 | | #include "core/data_type/primitive_type.h" |
45 | | #include "core/data_type_serde/data_type_timestamp_ns_serde.h" |
46 | | #include "core/value/large_int_value.h" |
47 | | #include "runtime/descriptors.h" |
48 | | #include "runtime/memory/mem_tracker.h" |
49 | | #include "storage/tablet/tablet_schema.h" |
50 | | #include "util/raw_value.h" |
51 | | #include "util/string_parser.hpp" |
52 | | #include "util/string_util.h" |
53 | | // NOLINTNEXTLINE(unused-includes) |
54 | | #include "core/value/vdatetime_value.h" |
55 | | #include "exprs/function/cast/cast_to_date_or_datetime_impl.hpp" |
56 | | #include "exprs/function/cast/cast_to_datetimev2_impl.hpp" |
57 | | #include "exprs/function/cast/cast_to_datev2_impl.hpp" |
58 | | #include "exprs/function/cast/cast_to_timestamptz.h" |
59 | | #include "exprs/vexpr_context.h" // IWYU pragma: keep |
60 | | #include "exprs/vliteral.h" |
61 | | |
62 | | namespace doris { |
63 | | |
64 | 2 | const OlapTableIndexSchema* OlapTableSchemaParam::row_binlog_index_schema(int64_t index_id) const { |
65 | 2 | for (auto* schema : _row_binlog_index_schemas) { |
66 | 2 | if (schema->index_id == index_id) { |
67 | 2 | return schema; |
68 | 2 | } |
69 | 2 | } |
70 | 0 | return nullptr; |
71 | 2 | } |
72 | | |
73 | 218k | void OlapTableIndexSchema::to_protobuf(POlapTableIndexSchema* pindex) const { |
74 | 218k | pindex->set_id(index_id); |
75 | 218k | pindex->set_schema_hash(schema_hash); |
76 | 218k | if (row_binlog_id > 0) { |
77 | 2 | pindex->set_row_binlog_id(row_binlog_id); |
78 | 2 | } |
79 | 2.27M | for (auto* slot : slots) { |
80 | 2.27M | pindex->add_columns(slot->col_name()); |
81 | 2.27M | } |
82 | 2.32M | for (auto* column : columns) { |
83 | 2.32M | column->to_schema_pb(pindex->add_columns_desc()); |
84 | 2.32M | } |
85 | 218k | for (auto* index : indexes) { |
86 | 133k | index->to_schema_pb(pindex->add_indexes_desc()); |
87 | 133k | } |
88 | 218k | } |
89 | | |
90 | | bool VOlapTablePartKeyComparator::operator()(const BlockRowWithIndicator& lhs, |
91 | 59.6M | const BlockRowWithIndicator& rhs) const { |
92 | 59.6M | Block* l_block = std::get<0>(lhs); |
93 | 59.6M | Block* r_block = std::get<0>(rhs); |
94 | 59.6M | int32_t l_row = std::get<1>(lhs); |
95 | 59.6M | int32_t r_row = std::get<1>(rhs); |
96 | 59.6M | bool l_use_new = std::get<2>(lhs); |
97 | 59.6M | bool r_use_new = std::get<2>(rhs); |
98 | | |
99 | 18.4E | VLOG_TRACE << '\n' << l_block->dump_data() << '\n' << r_block->dump_data(); |
100 | | |
101 | 59.6M | if (l_row == -1) { |
102 | 179 | return false; |
103 | 59.5M | } else if (r_row == -1) { |
104 | 31.2M | return true; |
105 | 31.2M | } |
106 | | |
107 | 28.3M | if (_param_locs.empty()) { // no transform, use origin column |
108 | 27.2M | for (auto slot_loc : _slot_locs) { |
109 | 27.2M | auto res = l_block->get_by_position(slot_loc).column->compare_at( |
110 | 27.2M | l_row, r_row, *r_block->get_by_position(slot_loc).column, -1); |
111 | 27.2M | if (res != 0) { |
112 | 27.2M | return res < 0; |
113 | 27.2M | } |
114 | 27.2M | } |
115 | 27.2M | } else { // use transformed column to compare |
116 | 18.4E | DCHECK(_slot_locs.size() == _param_locs.size()) |
117 | 18.4E | << _slot_locs.size() << ' ' << _param_locs.size(); |
118 | | |
119 | 1.04M | const std::vector<uint16_t>* l_index = l_use_new ? &_param_locs : &_slot_locs; |
120 | 1.04M | const std::vector<uint16_t>* r_index = r_use_new ? &_param_locs : &_slot_locs; |
121 | | |
122 | 1.42M | for (int i = 0; i < _slot_locs.size(); i++) { |
123 | 1.09M | ColumnPtr l_col = l_block->get_by_position((*l_index)[i]).column; |
124 | 1.09M | ColumnPtr r_col = r_block->get_by_position((*r_index)[i]).column; |
125 | | |
126 | 1.09M | auto res = l_col->compare_at(l_row, r_row, *r_col, -1); |
127 | 1.09M | if (res != 0) { |
128 | 713k | return res < 0; |
129 | 713k | } |
130 | 1.09M | } |
131 | 1.04M | } |
132 | | |
133 | | // equal, return false |
134 | 398k | return false; |
135 | 28.3M | } |
136 | | |
137 | 30.3k | Status OlapTableSchemaParam::init(const POlapTableSchemaParam& pschema) { |
138 | 30.3k | _db_id = pschema.db_id(); |
139 | 30.3k | _table_id = pschema.table_id(); |
140 | 30.3k | _version = pschema.version(); |
141 | 30.3k | if (pschema.has_unique_key_update_mode()) { |
142 | 30.3k | _unique_key_update_mode = pschema.unique_key_update_mode(); |
143 | 30.3k | if (pschema.has_sequence_map_col_unique_id()) { |
144 | 30.3k | _sequence_map_col_uid = pschema.sequence_map_col_unique_id(); |
145 | 30.3k | } |
146 | 30.3k | } else { |
147 | | // for backward compatibility |
148 | 1 | if (pschema.has_partial_update() && pschema.partial_update()) { |
149 | 0 | _unique_key_update_mode = UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS; |
150 | 1 | } else { |
151 | 1 | _unique_key_update_mode = UniqueKeyUpdateModePB::UPSERT; |
152 | 1 | } |
153 | 1 | } |
154 | 30.3k | _is_strict_mode = pschema.is_strict_mode(); |
155 | 30.3k | if (_unique_key_update_mode == UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS) { |
156 | 2.66k | _auto_increment_column = pschema.auto_increment_column(); |
157 | 2.66k | if (!_auto_increment_column.empty() && pschema.auto_increment_column_unique_id() == -1) { |
158 | 0 | return Status::InternalError( |
159 | 0 | "Auto increment column id is not set in FE. Maybe FE is an older version " |
160 | 0 | "different from BE."); |
161 | 0 | } |
162 | 2.66k | _auto_increment_column_unique_id = pschema.auto_increment_column_unique_id(); |
163 | 2.66k | } |
164 | 30.3k | if (_unique_key_update_mode != UniqueKeyUpdateModePB::UPSERT) { |
165 | 2.81k | if (pschema.has_partial_update_new_key_policy()) { |
166 | 2.81k | _partial_update_new_row_policy = pschema.partial_update_new_key_policy(); |
167 | 2.81k | } |
168 | 2.81k | } |
169 | 30.3k | _timestamp_ms = pschema.timestamp_ms(); |
170 | 30.3k | if (pschema.has_nano_seconds()) { |
171 | 30.3k | _nano_seconds = pschema.nano_seconds(); |
172 | 30.3k | } |
173 | 30.3k | _timezone = pschema.timezone(); |
174 | | |
175 | 30.3k | for (const auto& col : pschema.partial_update_input_columns()) { |
176 | 17.2k | _partial_update_input_columns.insert(col); |
177 | 17.2k | } |
178 | 30.3k | std::unordered_map<std::string, SlotDescriptor*> slots_map; |
179 | | |
180 | 30.3k | _tuple_desc = _obj_pool.add(new TupleDescriptor(pschema.tuple_desc())); |
181 | | |
182 | 263k | for (const auto& p_slot_desc : pschema.slot_descs()) { |
183 | 263k | auto* slot_desc = _obj_pool.add(new SlotDescriptor(p_slot_desc)); |
184 | 263k | _tuple_desc->add_slot(slot_desc); |
185 | 263k | std::string data_type; |
186 | 263k | EnumToString(TPrimitiveType, to_thrift(slot_desc->col_type()), data_type); |
187 | 263k | std::string is_null_str = slot_desc->is_nullable() ? "true" : "false"; |
188 | 263k | std::string data_type_str = |
189 | 263k | std::to_string(int64_t(TabletColumn::get_field_type_by_string(data_type))); |
190 | 263k | slots_map.emplace(to_lower(slot_desc->col_name()) + "+" + data_type_str + is_null_str, |
191 | 263k | slot_desc); |
192 | 263k | } |
193 | | |
194 | 38.4k | for (const auto& p_index : pschema.indexes()) { |
195 | 38.4k | auto* index = _obj_pool.add(new OlapTableIndexSchema()); |
196 | 38.4k | index->index_id = p_index.id(); |
197 | 38.4k | index->schema_hash = p_index.schema_hash(); |
198 | 38.4k | if (p_index.has_row_binlog_id()) { |
199 | 0 | index->row_binlog_id = p_index.row_binlog_id(); |
200 | 0 | } |
201 | 287k | for (const auto& pcolumn_desc : p_index.columns_desc()) { |
202 | 287k | if (_unique_key_update_mode != UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS || |
203 | 287k | _partial_update_input_columns.contains(pcolumn_desc.name())) { |
204 | 266k | std::string is_null_str = pcolumn_desc.is_nullable() ? "true" : "false"; |
205 | 266k | std::string data_type_str = std::to_string( |
206 | 266k | int64_t(TabletColumn::get_field_type_by_string(pcolumn_desc.type()))); |
207 | 266k | auto it = slots_map.find(to_lower(pcolumn_desc.name()) + "+" + data_type_str + |
208 | 266k | is_null_str); |
209 | 266k | if (it == std::end(slots_map)) { |
210 | 0 | std::string keys {}; |
211 | 0 | for (const auto& [key, _] : slots_map) { |
212 | 0 | keys += fmt::format("{},", key); |
213 | 0 | } |
214 | 0 | LOG_EVERY_SECOND(WARNING) << fmt::format( |
215 | 0 | "[OlapTableSchemaParam::init(const POlapTableSchemaParam& pschema)]: " |
216 | 0 | "unknown index column, column={}, type={}, data_type_str={}, " |
217 | 0 | "is_null_str={}, slots_map.keys()=[{}], {}\npschema={}", |
218 | 0 | pcolumn_desc.name(), pcolumn_desc.type(), data_type_str, is_null_str, |
219 | 0 | keys, debug_string(), pschema.ShortDebugString()); |
220 | |
|
221 | 0 | return Status::InternalError("unknown index column, column={}, type={}", |
222 | 0 | pcolumn_desc.name(), pcolumn_desc.type()); |
223 | 0 | } |
224 | 266k | index->slots.emplace_back(it->second); |
225 | 266k | } |
226 | 287k | TabletColumn* tc = _obj_pool.add(new TabletColumn()); |
227 | 287k | tc->init_from_pb(pcolumn_desc); |
228 | 287k | index->columns.emplace_back(tc); |
229 | 287k | } |
230 | 38.4k | for (const auto& pindex_desc : p_index.indexes_desc()) { |
231 | 5.94k | TabletIndex* ti = _obj_pool.add(new TabletIndex()); |
232 | 5.94k | ti->init_from_pb(pindex_desc); |
233 | 5.94k | index->indexes.emplace_back(ti); |
234 | 5.94k | } |
235 | 38.4k | _indexes.emplace_back(index); |
236 | 38.4k | } |
237 | | |
238 | 30.3k | for (const auto& p_index : pschema.row_binlog_index_schemas()) { |
239 | 0 | auto* index = _obj_pool.add(new OlapTableIndexSchema()); |
240 | 0 | index->index_id = p_index.id(); |
241 | 0 | index->schema_hash = p_index.schema_hash(); |
242 | 0 | if (p_index.has_row_binlog_id()) { |
243 | 0 | index->row_binlog_id = p_index.row_binlog_id(); |
244 | 0 | } |
245 | 0 | for (const auto& pcolumn_desc : p_index.columns_desc()) { |
246 | 0 | TabletColumn* tc = _obj_pool.add(new TabletColumn()); |
247 | 0 | tc->init_from_pb(pcolumn_desc); |
248 | 0 | index->columns.emplace_back(tc); |
249 | 0 | } |
250 | 0 | for (const auto& pindex_desc : p_index.indexes_desc()) { |
251 | 0 | TabletIndex* ti = _obj_pool.add(new TabletIndex()); |
252 | 0 | ti->init_from_pb(pindex_desc); |
253 | 0 | index->indexes.emplace_back(ti); |
254 | 0 | } |
255 | 0 | _row_binlog_index_schemas.emplace_back(index); |
256 | 0 | } |
257 | | |
258 | 30.3k | std::sort(_indexes.begin(), _indexes.end(), |
259 | 33.1k | [](const OlapTableIndexSchema* lhs, const OlapTableIndexSchema* rhs) { |
260 | 33.1k | return lhs->index_id < rhs->index_id; |
261 | 33.1k | }); |
262 | 30.3k | return Status::OK(); |
263 | 30.3k | } |
264 | | |
265 | 96.5k | Status OlapTableSchemaParam::init_unique_key_update_mode(const TOlapTableSchemaParam& tschema) { |
266 | 96.5k | if (tschema.__isset.unique_key_update_mode) { |
267 | 96.5k | switch (tschema.unique_key_update_mode) { |
268 | 89.6k | case doris::TUniqueKeyUpdateMode::UPSERT: { |
269 | 89.6k | _unique_key_update_mode = UniqueKeyUpdateModePB::UPSERT; |
270 | 89.6k | break; |
271 | 0 | } |
272 | 6.73k | case doris::TUniqueKeyUpdateMode::UPDATE_FIXED_COLUMNS: { |
273 | 6.73k | _unique_key_update_mode = UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS; |
274 | 6.73k | break; |
275 | 0 | } |
276 | 157 | case doris::TUniqueKeyUpdateMode::UPDATE_FLEXIBLE_COLUMNS: { |
277 | 157 | _unique_key_update_mode = UniqueKeyUpdateModePB::UPDATE_FLEXIBLE_COLUMNS; |
278 | 157 | break; |
279 | 0 | } |
280 | 0 | default: { |
281 | 0 | return Status::InternalError( |
282 | 0 | "Unknown unique_key_update_mode: {}, should be one of " |
283 | 0 | "UPSERT/UPDATE_FIXED_COLUMNS/UPDATE_FLEXIBLE_COLUMNS", |
284 | 0 | tschema.unique_key_update_mode); |
285 | 0 | } |
286 | 96.5k | } |
287 | 96.5k | if (tschema.__isset.sequence_map_col_unique_id) { |
288 | 96.5k | _sequence_map_col_uid = tschema.sequence_map_col_unique_id; |
289 | 96.5k | } |
290 | 96.5k | } else { |
291 | | // for backward compatibility |
292 | 20 | if (tschema.__isset.is_partial_update && tschema.is_partial_update) { |
293 | 0 | _unique_key_update_mode = UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS; |
294 | 20 | } else { |
295 | 20 | _unique_key_update_mode = UniqueKeyUpdateModePB::UPSERT; |
296 | 20 | } |
297 | 20 | } |
298 | 96.6k | return Status::OK(); |
299 | 96.5k | } |
300 | | |
301 | 96.5k | Status OlapTableSchemaParam::init(const TOlapTableSchemaParam& tschema) { |
302 | 96.5k | _db_id = tschema.db_id; |
303 | 96.5k | _table_id = tschema.table_id; |
304 | 96.5k | _version = tschema.version; |
305 | 96.5k | RETURN_IF_ERROR(init_unique_key_update_mode(tschema)); |
306 | 96.5k | if (tschema.__isset.is_strict_mode) { |
307 | 96.5k | _is_strict_mode = tschema.is_strict_mode; |
308 | 96.5k | } |
309 | 96.5k | if (_unique_key_update_mode == UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS) { |
310 | 6.73k | _auto_increment_column = tschema.auto_increment_column; |
311 | 6.73k | if (!_auto_increment_column.empty() && tschema.auto_increment_column_unique_id == -1) { |
312 | 0 | return Status::InternalError( |
313 | 0 | "Auto increment column id is not set in FE. Maybe FE is an older version " |
314 | 0 | "different from BE."); |
315 | 0 | } |
316 | 6.73k | _auto_increment_column_unique_id = tschema.auto_increment_column_unique_id; |
317 | 6.73k | } |
318 | | |
319 | 96.5k | if (_unique_key_update_mode != UniqueKeyUpdateModePB::UPSERT) { |
320 | 6.89k | if (tschema.__isset.partial_update_new_key_policy) { |
321 | 6.88k | switch (tschema.partial_update_new_key_policy) { |
322 | 6.80k | case doris::TPartialUpdateNewRowPolicy::APPEND: { |
323 | 6.80k | _partial_update_new_row_policy = PartialUpdateNewRowPolicyPB::APPEND; |
324 | 6.80k | break; |
325 | 0 | } |
326 | 85 | case doris::TPartialUpdateNewRowPolicy::ERROR: { |
327 | 85 | _partial_update_new_row_policy = PartialUpdateNewRowPolicyPB::ERROR; |
328 | 85 | break; |
329 | 0 | } |
330 | 0 | default: { |
331 | 0 | return Status::InvalidArgument( |
332 | 0 | "Unknown partial_update_new_key_behavior: {}, should be one of " |
333 | 0 | "'APPEND' or 'ERROR'", |
334 | 0 | tschema.partial_update_new_key_policy); |
335 | 0 | } |
336 | 6.88k | } |
337 | 6.88k | } |
338 | 6.89k | } |
339 | | |
340 | 96.5k | for (const auto& tcolumn : tschema.partial_update_input_columns) { |
341 | 37.9k | _partial_update_input_columns.insert(tcolumn); |
342 | 37.9k | } |
343 | 96.5k | std::unordered_map<std::string, SlotDescriptor*> slots_map; |
344 | 96.5k | _tuple_desc = _obj_pool.add(new TupleDescriptor(tschema.tuple_desc)); |
345 | 734k | for (const auto& t_slot_desc : tschema.slot_descs) { |
346 | 734k | auto* slot_desc = _obj_pool.add(new SlotDescriptor(t_slot_desc)); |
347 | 734k | _tuple_desc->add_slot(slot_desc); |
348 | 734k | std::string is_null_str = slot_desc->is_nullable() ? "true" : "false"; |
349 | 734k | std::string data_type_str = std::to_string(int64_t(slot_desc->col_type())); |
350 | 734k | slots_map.emplace(to_lower(slot_desc->col_name()) + "+" + data_type_str + is_null_str, |
351 | 734k | slot_desc); |
352 | 734k | } |
353 | | |
354 | 99.4k | for (const auto& t_index : tschema.indexes) { |
355 | 99.4k | std::unordered_map<std::string, int32_t> index_slots_map; |
356 | 99.4k | auto* index = _obj_pool.add(new OlapTableIndexSchema()); |
357 | 99.4k | index->index_id = t_index.id; |
358 | 99.4k | index->schema_hash = t_index.schema_hash; |
359 | 99.4k | if (t_index.__isset.row_binlog_id) { |
360 | 99.3k | index->row_binlog_id = t_index.row_binlog_id; |
361 | 99.3k | } |
362 | 788k | for (const auto& tcolumn_desc : t_index.columns_desc) { |
363 | 788k | if (_unique_key_update_mode != UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS || |
364 | 788k | _partial_update_input_columns.contains(tcolumn_desc.column_name)) { |
365 | 738k | std::string is_null_str = tcolumn_desc.is_allow_null ? "true" : "false"; |
366 | 738k | std::string data_type_str = |
367 | 738k | std::to_string(int64_t(thrift_to_type(tcolumn_desc.column_type.type))); |
368 | 738k | auto it = slots_map.find(to_lower(tcolumn_desc.column_name) + "+" + data_type_str + |
369 | 738k | is_null_str); |
370 | 738k | if (it == slots_map.end()) { |
371 | 0 | std::stringstream ss; |
372 | 0 | ss << tschema; |
373 | 0 | std::string keys {}; |
374 | 0 | for (const auto& [key, _] : slots_map) { |
375 | 0 | keys += fmt::format("{},", key); |
376 | 0 | } |
377 | 0 | LOG_EVERY_SECOND(WARNING) << fmt::format( |
378 | 0 | "[OlapTableSchemaParam::init(const TOlapTableSchemaParam& tschema)]: " |
379 | 0 | "unknown index column, column={}, type={}, data_type_str={}, " |
380 | 0 | "is_null_str={}, slots_map.keys()=[{}], {}\ntschema={}", |
381 | 0 | tcolumn_desc.column_name, tcolumn_desc.column_type.type, data_type_str, |
382 | 0 | is_null_str, keys, debug_string(), ss.str()); |
383 | 0 | return Status::InternalError("unknown index column, column={}, type={}", |
384 | 0 | tcolumn_desc.column_name, |
385 | 0 | tcolumn_desc.column_type.type); |
386 | 0 | } |
387 | 738k | index->slots.emplace_back(it->second); |
388 | 738k | } |
389 | 788k | index_slots_map.emplace(to_lower(tcolumn_desc.column_name), tcolumn_desc.col_unique_id); |
390 | 788k | TabletColumn* tc = _obj_pool.add(new TabletColumn()); |
391 | 788k | tc->init_from_thrift(tcolumn_desc); |
392 | 788k | index->columns.emplace_back(tc); |
393 | 788k | } |
394 | 99.4k | if (t_index.__isset.indexes_desc) { |
395 | 99.3k | for (const auto& tindex_desc : t_index.indexes_desc) { |
396 | 15.3k | std::vector<int32_t> column_unique_ids(tindex_desc.columns.size()); |
397 | 30.7k | for (size_t i = 0; i < tindex_desc.columns.size(); i++) { |
398 | 15.3k | auto it = index_slots_map.find(to_lower(tindex_desc.columns[i])); |
399 | 15.3k | if (it != index_slots_map.end()) { |
400 | 15.3k | column_unique_ids[i] = it->second; |
401 | 15.3k | } |
402 | 15.3k | } |
403 | 15.3k | TabletIndex* ti = _obj_pool.add(new TabletIndex()); |
404 | 15.3k | ti->init_from_thrift(tindex_desc, column_unique_ids); |
405 | 15.3k | index->indexes.emplace_back(ti); |
406 | 15.3k | } |
407 | 99.3k | } |
408 | 99.4k | if (t_index.__isset.where_clause) { |
409 | 99 | RETURN_IF_ERROR(VExpr::create_expr_tree(t_index.where_clause, index->where_clause)); |
410 | 99 | } |
411 | 99.4k | _indexes.emplace_back(index); |
412 | 99.4k | } |
413 | | |
414 | 96.5k | if (tschema.__isset.row_binlog_index_schemas) { |
415 | 5 | for (const auto& t_index : tschema.row_binlog_index_schemas) { |
416 | 5 | auto* index = _obj_pool.add(new OlapTableIndexSchema()); |
417 | 5 | index->index_id = t_index.id; |
418 | 5 | index->schema_hash = t_index.schema_hash; |
419 | 5 | if (t_index.__isset.row_binlog_id) { |
420 | 0 | index->row_binlog_id = t_index.row_binlog_id; |
421 | 0 | } |
422 | 19 | for (const auto& tcolumn_desc : t_index.columns_desc) { |
423 | 19 | TabletColumn* tc = _obj_pool.add(new TabletColumn()); |
424 | 19 | tc->init_from_thrift(tcolumn_desc); |
425 | 19 | index->columns.emplace_back(tc); |
426 | 19 | } |
427 | 5 | _row_binlog_index_schemas.emplace_back(index); |
428 | 5 | } |
429 | 5 | } |
430 | | |
431 | 96.5k | std::sort(_indexes.begin(), _indexes.end(), |
432 | 96.5k | [](const OlapTableIndexSchema* lhs, const OlapTableIndexSchema* rhs) { |
433 | 7.85k | return lhs->index_id < rhs->index_id; |
434 | 7.85k | }); |
435 | 96.5k | return Status::OK(); |
436 | 96.5k | } |
437 | | |
438 | 43.3k | void OlapTableSchemaParam::to_protobuf(POlapTableSchemaParam* pschema) const { |
439 | 43.3k | pschema->set_db_id(_db_id); |
440 | 43.3k | pschema->set_table_id(_table_id); |
441 | 43.3k | pschema->set_version(_version); |
442 | 43.3k | pschema->set_unique_key_update_mode(_unique_key_update_mode); |
443 | 43.3k | if (_unique_key_update_mode == UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS) { |
444 | | // for backward compatibility |
445 | 4.19k | pschema->set_partial_update(true); |
446 | 4.19k | } |
447 | 43.3k | pschema->set_partial_update_new_key_policy(_partial_update_new_row_policy); |
448 | 43.3k | pschema->set_is_strict_mode(_is_strict_mode); |
449 | 43.3k | pschema->set_auto_increment_column(_auto_increment_column); |
450 | 43.3k | pschema->set_auto_increment_column_unique_id(_auto_increment_column_unique_id); |
451 | 43.3k | pschema->set_timestamp_ms(_timestamp_ms); |
452 | 43.3k | pschema->set_timezone(_timezone); |
453 | 43.3k | pschema->set_nano_seconds(_nano_seconds); |
454 | 43.3k | pschema->set_sequence_map_col_unique_id(_sequence_map_col_uid); |
455 | 43.3k | for (auto col : _partial_update_input_columns) { |
456 | 21.2k | *pschema->add_partial_update_input_columns() = col; |
457 | 21.2k | } |
458 | 43.3k | _tuple_desc->to_protobuf(pschema->mutable_tuple_desc()); |
459 | 329k | for (auto* slot : _tuple_desc->slots()) { |
460 | 329k | slot->to_protobuf(pschema->add_slot_descs()); |
461 | 329k | } |
462 | 45.1k | for (auto* index : _indexes) { |
463 | 45.1k | index->to_protobuf(pschema->add_indexes()); |
464 | 45.1k | } |
465 | 43.3k | for (auto* index : _row_binlog_index_schemas) { |
466 | 0 | index->to_protobuf(pschema->add_row_binlog_index_schemas()); |
467 | 0 | } |
468 | 43.3k | } |
469 | | |
470 | 0 | std::string OlapTableSchemaParam::debug_string() const { |
471 | 0 | std::stringstream ss; |
472 | 0 | ss << "tuple_desc=" << _tuple_desc->debug_string(); |
473 | 0 | return ss.str(); |
474 | 0 | } |
475 | | |
476 | | VOlapTablePartitionParam::VOlapTablePartitionParam(std::shared_ptr<OlapTableSchemaParam>& schema, |
477 | | const TOlapTablePartitionParam& t_param) |
478 | 43.9k | : _schema(schema), |
479 | 43.9k | _t_param(t_param), |
480 | 43.9k | _slots(_schema->tuple_desc()->slots()), |
481 | 43.9k | _mem_tracker(std::make_unique<MemTracker>("OlapTablePartitionParam")), |
482 | 43.9k | _part_type(t_param.partition_type) { |
483 | 44.1k | if (t_param.__isset.enable_automatic_partition && t_param.enable_automatic_partition) { |
484 | 252 | _is_auto_partition = true; |
485 | 252 | auto size = t_param.partition_function_exprs.size(); |
486 | 252 | _part_func_ctx.resize(size); |
487 | 252 | _partition_function.resize(size); |
488 | 252 | DCHECK((t_param.partition_type == TPartitionType::RANGE_PARTITIONED && size == 1) || |
489 | 3 | (t_param.partition_type == TPartitionType::LIST_PARTITIONED && size >= 1)) |
490 | 3 | << "now support only 1 partition column for auto range partitions. " |
491 | 3 | << t_param.partition_type << " " << size; |
492 | 512 | for (int i = 0; i < size; ++i) { |
493 | 260 | Status st = |
494 | 260 | VExpr::create_expr_tree(t_param.partition_function_exprs[i], _part_func_ctx[i]); |
495 | 260 | if (!st.ok()) { |
496 | 0 | throw Exception(Status::InternalError("Partition function expr is not valid"), |
497 | 0 | "Partition function expr is not valid"); |
498 | 0 | } |
499 | 260 | _partition_function[i] = _part_func_ctx[i]->root(); |
500 | 260 | } |
501 | 252 | } |
502 | | |
503 | 43.9k | if (t_param.__isset.enable_auto_detect_overwrite && t_param.enable_auto_detect_overwrite) { |
504 | 44 | _is_auto_detect_overwrite = true; |
505 | 44 | DCHECK(t_param.__isset.overwrite_group_id); |
506 | 44 | _overwrite_group_id = t_param.overwrite_group_id; |
507 | 44 | } |
508 | | |
509 | 43.9k | if (t_param.__isset.master_address) { |
510 | 0 | _master_address = std::make_shared<TNetworkAddress>(t_param.master_address); |
511 | 0 | } |
512 | | |
513 | 43.9k | if (_is_auto_partition) { |
514 | | // the nullable mode depends on partition_exprs. not column slots. so use them. |
515 | 252 | DCHECK(_partition_function.size() <= _slots.size()) |
516 | 1 | << _partition_function.size() << ", " << _slots.size(); |
517 | | |
518 | | // suppose (k0, [k1], [k2]), so get [k1, 0], [k2, 1] |
519 | 252 | std::map<std::string, int> partition_slots_map; // name to idx in part_exprs |
520 | 514 | for (size_t i = 0; i < t_param.partition_columns.size(); i++) { |
521 | 262 | partition_slots_map.emplace(t_param.partition_columns[i], i); |
522 | 262 | } |
523 | | |
524 | | // here we rely on the same order and number of the _part_funcs and _slots in the prefix |
525 | | // _part_block contains all slots of table. |
526 | 953 | for (auto* slot : _slots) { |
527 | | // try to replace with partition expr. |
528 | 953 | if (auto it = partition_slots_map.find(slot->col_name()); |
529 | 953 | it != partition_slots_map.end()) { // it's a partition column slot |
530 | 262 | auto& expr_type = _partition_function[it->second]->data_type(); |
531 | 262 | _partition_block.insert({expr_type->create_column(), expr_type, slot->col_name()}); |
532 | 691 | } else { |
533 | 691 | _partition_block.insert({slot->get_empty_mutable_column(), |
534 | 691 | slot->get_data_type_ptr(), slot->col_name()}); |
535 | 691 | } |
536 | 953 | } |
537 | 252 | VLOG_TRACE << _partition_block.dump_structure(); |
538 | 43.7k | } else { |
539 | | // we insert all. but not all will be used. it will controlled by _partition_slot_locs |
540 | 328k | for (auto* slot : _slots) { |
541 | 328k | _partition_block.insert({slot->get_empty_mutable_column(), slot->get_data_type_ptr(), |
542 | 328k | slot->col_name()}); |
543 | 328k | } |
544 | 43.7k | } |
545 | 43.9k | } |
546 | | |
547 | 44.4k | VOlapTablePartitionParam::~VOlapTablePartitionParam() { |
548 | 44.4k | _mem_tracker->release(_mem_usage); |
549 | 44.4k | } |
550 | | |
551 | 43.6k | Status VOlapTablePartitionParam::init() { |
552 | 43.6k | std::vector<std::string> slot_column_names; |
553 | 330k | for (auto* slot_desc : _schema->tuple_desc()->slots()) { |
554 | 330k | slot_column_names.emplace_back(slot_desc->col_name()); |
555 | 330k | } |
556 | | |
557 | 43.6k | auto find_slot_locs = [&slot_column_names](const std::string& slot_name, |
558 | 43.6k | std::vector<uint16_t>& locs, |
559 | 71.5k | const std::string& column_type) { |
560 | 71.5k | auto it = std::find(slot_column_names.begin(), slot_column_names.end(), slot_name); |
561 | 71.5k | if (it == slot_column_names.end()) { |
562 | 0 | return Status::InternalError("{} column not found, column ={}", column_type, slot_name); |
563 | 0 | } |
564 | 71.5k | locs.emplace_back(it - slot_column_names.begin()); |
565 | 71.5k | return Status::OK(); |
566 | 71.5k | }; |
567 | | |
568 | | // here we find the partition columns. others maybe non-partition columns/special columns. |
569 | 43.6k | if (_t_param.__isset.partition_columns) { |
570 | 5.85k | for (auto& part_col : _t_param.partition_columns) { |
571 | 5.85k | RETURN_IF_ERROR(find_slot_locs(part_col, _partition_slot_locs, "partition")); |
572 | 5.85k | } |
573 | 5.73k | } |
574 | | |
575 | 43.6k | _partitions_map = std::make_unique< |
576 | 43.6k | std::map<BlockRowWithIndicator, VOlapTablePartition*, VOlapTablePartKeyComparator>>( |
577 | 43.6k | VOlapTablePartKeyComparator(_partition_slot_locs, _transformed_slot_locs)); |
578 | 43.6k | if (_t_param.__isset.distributed_columns) { |
579 | 65.6k | for (auto& col : _t_param.distributed_columns) { |
580 | 65.6k | RETURN_IF_ERROR(find_slot_locs(col, _distributed_slot_locs, "distributed")); |
581 | 65.6k | } |
582 | 43.6k | } |
583 | | |
584 | | // for both auto/non-auto partition table. |
585 | 43.6k | _is_in_partition = _part_type == TPartitionType::type::LIST_PARTITIONED; |
586 | | |
587 | | // initial partitions. if meet dummy partitions only for open BE nodes, not generate key of them for finding |
588 | 59.2k | for (const auto& t_part : _t_param.partitions) { |
589 | 59.2k | VOlapTablePartition* part = nullptr; |
590 | 59.2k | RETURN_IF_ERROR(generate_partition_from(t_part, part)); |
591 | 59.2k | _partitions.emplace_back(part); |
592 | | |
593 | 59.2k | if (!_t_param.partitions_is_fake) { |
594 | 59.2k | if (_is_in_partition) { |
595 | 5.79k | for (auto& in_key : part->in_keys) { |
596 | 5.79k | _partitions_map->emplace(std::tuple {in_key.first, in_key.second, false}, part); |
597 | 5.79k | } |
598 | 55.4k | } else { |
599 | 55.4k | _partitions_map->emplace( |
600 | 55.4k | std::tuple {part->end_key.first, part->end_key.second, false}, part); |
601 | 55.4k | } |
602 | 59.2k | } |
603 | 59.2k | } |
604 | | |
605 | 43.6k | _mem_usage = _partition_block.allocated_bytes(); |
606 | 43.6k | _mem_tracker->consume(_mem_usage); |
607 | 43.6k | return Status::OK(); |
608 | 43.6k | } |
609 | | |
610 | | bool VOlapTablePartitionParam::_part_contains(VOlapTablePartition* part, |
611 | 38.1M | BlockRowWithIndicator key) const { |
612 | 38.1M | VOlapTablePartKeyComparator comparator(_partition_slot_locs, _transformed_slot_locs); |
613 | | // we have used upper_bound to find to ensure key < part.right and this part is closest(right - key is min) |
614 | | // now we only have to check (key >= part.left). the comparator(a,b) means a < b, so we use anti |
615 | 38.1M | return part->start_key.second == -1 /* spj: start_key.second == -1 means only single partition*/ |
616 | 38.1M | || !comparator(key, std::tuple {part->start_key.first, part->start_key.second, false}); |
617 | 38.1M | } |
618 | | |
619 | | // insert value into _partition_block's column |
620 | | // NOLINTBEGIN(readability-function-size) |
621 | 40.0k | static Status _create_partition_key(const TExprNode& t_expr, BlockRow* part_key, uint16_t pos) { |
622 | 40.0k | auto column = std::move(*part_key->first->get_by_position(pos).column).mutate(); |
623 | 40.0k | switch (t_expr.node_type) { |
624 | 24.1k | case TExprNodeType::DATE_LITERAL: { |
625 | 24.1k | auto primitive_type = |
626 | 24.1k | DataTypeFactory::instance().create_data_type(t_expr.type)->get_primitive_type(); |
627 | 24.1k | if (primitive_type == TYPE_DATEV2) { |
628 | 19.1k | DateV2Value<DateV2ValueType> dt; |
629 | 19.1k | CastParameters params; |
630 | 19.1k | if (!CastToDateV2::from_string_strict_mode<DatelikeParseMode::STRICT>( |
631 | 19.1k | {t_expr.date_literal.value.c_str(), t_expr.date_literal.value.size()}, dt, |
632 | 19.1k | nullptr, params)) { |
633 | 0 | std::stringstream ss; |
634 | 0 | ss << "invalid date literal in partition column, date=" << t_expr.date_literal; |
635 | 0 | return Status::InternalError(ss.str()); |
636 | 0 | } |
637 | 19.1k | column->insert_data(reinterpret_cast<const char*>(&dt), 0); |
638 | 19.1k | } else if (primitive_type == TYPE_DATETIMEV2) { |
639 | 4.28k | DateV2Value<DateTimeV2ValueType> dt; |
640 | 4.28k | const int32_t scale = |
641 | 4.28k | t_expr.type.types.empty() ? -1 : t_expr.type.types.front().scalar_type.scale; |
642 | 4.28k | CastParameters params; |
643 | 4.28k | if (!CastToDatetimeV2::from_string_strict_mode<DatelikeParseMode::STRICT>( |
644 | 4.28k | {t_expr.date_literal.value.c_str(), t_expr.date_literal.value.size()}, dt, |
645 | 4.28k | nullptr, scale, params)) { |
646 | 0 | std::stringstream ss; |
647 | 0 | ss << "invalid date literal in partition column, date=" << t_expr.date_literal; |
648 | 0 | return Status::InternalError(ss.str()); |
649 | 0 | } |
650 | 4.28k | column->insert_data(reinterpret_cast<const char*>(&dt), 0); |
651 | 4.28k | } else if (primitive_type == TYPE_TIMESTAMP_NS) { |
652 | 14 | int64_t epoch_nanos = 0; |
653 | 14 | RETURN_IF_ERROR(parse_timestamp_ns( |
654 | 14 | {t_expr.date_literal.value.data(), t_expr.date_literal.value.size()}, |
655 | 14 | &epoch_nanos)); |
656 | 14 | const TimeStampNsValue dt(epoch_nanos); |
657 | 14 | column->insert_data(reinterpret_cast<const char*>(&dt), 0); |
658 | 689 | } else if (primitive_type == TYPE_TIMESTAMPTZ) { |
659 | 384 | TimestampTzValue res; |
660 | 384 | CastParameters params {.status = Status::OK(), .is_strict = true}; |
661 | 384 | const int32_t scale = |
662 | 384 | t_expr.type.types.empty() ? -1 : t_expr.type.types.front().scalar_type.scale; |
663 | 384 | if (!CastToTimestampTz::from_string( |
664 | 384 | {t_expr.date_literal.value.c_str(), t_expr.date_literal.value.size()}, res, |
665 | 384 | params, nullptr, scale)) [[unlikely]] { |
666 | 0 | std::stringstream ss; |
667 | 0 | ss << "invalid timestamptz literal in partition column, value=" |
668 | 0 | << t_expr.date_literal; |
669 | 0 | return Status::InternalError(ss.str()); |
670 | 0 | } |
671 | 384 | column->insert_data(reinterpret_cast<const char*>(&res), 0); |
672 | 384 | } else { |
673 | 305 | VecDateTimeValue dt; |
674 | 305 | CastParameters params; |
675 | 305 | if (!CastToDateOrDatetime::from_string_strict_mode<DatelikeParseMode::STRICT, |
676 | 305 | DatelikeTargetType::DATE_TIME>( |
677 | 305 | {t_expr.date_literal.value.c_str(), t_expr.date_literal.value.size()}, dt, |
678 | 305 | nullptr, params)) { |
679 | 0 | std::stringstream ss; |
680 | 0 | ss << "invalid date literal in partition column, date=" << t_expr.date_literal; |
681 | 0 | return Status::InternalError(ss.str()); |
682 | 0 | } |
683 | 305 | if (primitive_type == TYPE_DATE) { |
684 | 120 | dt.cast_to_date(); |
685 | 120 | } |
686 | 305 | column->insert_data(reinterpret_cast<const char*>(&dt), 0); |
687 | 305 | } |
688 | 24.1k | break; |
689 | 24.1k | } |
690 | 24.1k | case TExprNodeType::INT_LITERAL: { |
691 | 13.8k | switch (t_expr.type.types[0].scalar_type.type) { |
692 | 1.59k | case TPrimitiveType::TINYINT: { |
693 | 1.59k | auto value = cast_set<int8_t>(t_expr.int_literal.value); |
694 | 1.59k | column->insert_data(reinterpret_cast<const char*>(&value), 0); |
695 | 1.59k | break; |
696 | 0 | } |
697 | 401 | case TPrimitiveType::SMALLINT: { |
698 | 401 | auto value = cast_set<int16_t>(t_expr.int_literal.value); |
699 | 401 | column->insert_data(reinterpret_cast<const char*>(&value), 0); |
700 | 401 | break; |
701 | 0 | } |
702 | 11.2k | case TPrimitiveType::INT: { |
703 | 11.2k | auto value = cast_set<int32_t>(t_expr.int_literal.value); |
704 | 11.2k | column->insert_data(reinterpret_cast<const char*>(&value), 0); |
705 | 11.2k | break; |
706 | 0 | } |
707 | 593 | default: |
708 | 593 | int64_t value = t_expr.int_literal.value; |
709 | 593 | column->insert_data(reinterpret_cast<const char*>(&value), 0); |
710 | 13.8k | } |
711 | 13.7k | break; |
712 | 13.8k | } |
713 | 13.7k | case TExprNodeType::LARGE_INT_LITERAL: { |
714 | 149 | StringParser::ParseResult parse_result = StringParser::PARSE_SUCCESS; |
715 | 149 | auto value = StringParser::string_to_int<__int128>(t_expr.large_int_literal.value.c_str(), |
716 | 149 | t_expr.large_int_literal.value.size(), |
717 | 149 | &parse_result); |
718 | 149 | if (parse_result != StringParser::PARSE_SUCCESS) { |
719 | 0 | value = MAX_INT128; |
720 | 0 | } |
721 | 149 | column->insert_data(reinterpret_cast<const char*>(&value), 0); |
722 | 149 | break; |
723 | 13.8k | } |
724 | 1.89k | case TExprNodeType::STRING_LITERAL: { |
725 | 1.89k | size_t len = t_expr.string_literal.value.size(); |
726 | 1.89k | const char* str_val = t_expr.string_literal.value.c_str(); |
727 | 1.89k | column->insert_data(str_val, len); |
728 | 1.89k | break; |
729 | 13.8k | } |
730 | 23 | case TExprNodeType::BOOL_LITERAL: { |
731 | 23 | column->insert_data(reinterpret_cast<const char*>(&t_expr.bool_literal.value), 0); |
732 | 23 | break; |
733 | 13.8k | } |
734 | 64 | case TExprNodeType::NULL_LITERAL: { |
735 | | // insert a null literal |
736 | 64 | if (!column->is_nullable()) { |
737 | | // https://github.com/apache/doris/pull/39449 have forbid this cause. always add this check as protective measures |
738 | 0 | return Status::InternalError("The column {} is not null, can't insert into NULL value.", |
739 | 0 | part_key->first->get_by_position(pos).name); |
740 | 0 | } |
741 | 64 | column->insert_data(nullptr, 0); |
742 | 64 | break; |
743 | 64 | } |
744 | 0 | default: { |
745 | 0 | return Status::InternalError("unsupported partition column node type, type={}", |
746 | 0 | t_expr.node_type); |
747 | 64 | } |
748 | 40.0k | } |
749 | 40.0k | part_key->second = cast_set<int32_t>(column->size() - 1); |
750 | 40.0k | return Status::OK(); |
751 | 40.0k | } |
752 | | // NOLINTEND(readability-function-size) |
753 | | |
754 | | Status VOlapTablePartitionParam::_create_partition_keys(const std::vector<TExprNode>& t_exprs, |
755 | 39.3k | BlockRow* part_key) { |
756 | 79.4k | for (int i = 0; i < t_exprs.size(); i++) { |
757 | 40.1k | RETURN_IF_ERROR(_create_partition_key(t_exprs[i], part_key, _partition_slot_locs[i])); |
758 | 40.1k | } |
759 | 39.3k | return Status::OK(); |
760 | 39.3k | } |
761 | | |
762 | | Status VOlapTablePartitionParam::generate_partition_from(const TOlapTablePartition& t_part, |
763 | 59.1k | VOlapTablePartition*& part_result) { |
764 | 59.1k | DCHECK(part_result == nullptr); |
765 | | // here we set the default value of partition bounds first! if it doesn't have some key, it will be -1. |
766 | 59.1k | part_result = _obj_pool.add(new VOlapTablePartition(&_partition_block)); |
767 | 59.1k | part_result->id = t_part.id; |
768 | 59.1k | part_result->is_mutable = t_part.is_mutable; |
769 | 59.1k | if (t_part.__isset.load_tablet_idx) { |
770 | 9.67k | part_result->load_tablet_idx = t_part.load_tablet_idx; |
771 | 9.67k | } |
772 | 59.1k | if (t_part.__isset.bucket_be_id) { |
773 | 9.50k | part_result->bucket_be_id = t_part.bucket_be_id; |
774 | 9.50k | } |
775 | 59.1k | if (t_part.__isset.local_bucket_seqs) { |
776 | 9.51k | part_result->local_bucket_seqs = t_part.local_bucket_seqs; |
777 | 9.51k | } |
778 | | |
779 | 59.1k | if (_is_in_partition) { |
780 | 6.04k | for (const auto& keys : t_part.in_keys) { |
781 | 6.04k | RETURN_IF_ERROR(_create_partition_keys( |
782 | 6.04k | keys, &part_result->in_keys.emplace_back(&_partition_block, -1))); |
783 | 6.04k | } |
784 | 3.99k | if (t_part.__isset.is_default_partition && t_part.is_default_partition && |
785 | 3.99k | _default_partition == nullptr) { |
786 | 22 | _default_partition = part_result; |
787 | 22 | } |
788 | 55.1k | } else { // range |
789 | 55.1k | if (t_part.__isset.start_keys) { |
790 | 15.4k | RETURN_IF_ERROR(_create_partition_keys(t_part.start_keys, &part_result->start_key)); |
791 | 15.4k | } |
792 | | // we generate the right bound but not insert into partition map |
793 | 55.1k | if (t_part.__isset.end_keys) { |
794 | 17.2k | RETURN_IF_ERROR(_create_partition_keys(t_part.end_keys, &part_result->end_key)); |
795 | 17.2k | } |
796 | 55.1k | } |
797 | | |
798 | 59.1k | part_result->num_buckets = t_part.num_buckets; |
799 | 59.1k | auto num_indexes = _schema->indexes().size(); |
800 | 59.1k | if (t_part.indexes.size() != num_indexes) { |
801 | 0 | return Status::InternalError( |
802 | 0 | "number of partition's index is not equal with schema's" |
803 | 0 | ", num_part_indexes={}, num_schema_indexes={}", |
804 | 0 | t_part.indexes.size(), num_indexes); |
805 | 0 | } |
806 | 59.1k | part_result->indexes = t_part.indexes; |
807 | 59.1k | std::sort(part_result->indexes.begin(), part_result->indexes.end(), |
808 | 59.1k | [](const OlapTableIndexTablets& lhs, const OlapTableIndexTablets& rhs) { |
809 | 7.15k | return lhs.index_id < rhs.index_id; |
810 | 7.15k | }); |
811 | | // check index |
812 | 121k | for (int j = 0; j < num_indexes; ++j) { |
813 | 62.2k | if (part_result->indexes[j].index_id != _schema->indexes()[j]->index_id) { |
814 | 0 | return Status::InternalError( |
815 | 0 | "partition's index is not equal with schema's" |
816 | 0 | ", part_index={}, schema_index={}", |
817 | 0 | part_result->indexes[j].index_id, _schema->indexes()[j]->index_id); |
818 | 0 | } |
819 | 62.2k | } |
820 | 59.5k | if (t_part.__isset.total_replica_num) { |
821 | 59.5k | part_result->total_replica_num = t_part.total_replica_num; |
822 | 59.5k | } |
823 | 59.5k | if (t_part.__isset.load_required_replica_num) { |
824 | 59.5k | part_result->load_required_replica_num = t_part.load_required_replica_num; |
825 | 59.5k | } |
826 | 59.1k | if (t_part.__isset.tablet_version_gap_backends) { |
827 | 0 | for (const auto& [tablet_id, backend_ids] : t_part.tablet_version_gap_backends) { |
828 | 0 | auto& gap_set = part_result->tablet_version_gap_backends[tablet_id]; |
829 | 0 | for (auto backend_id : backend_ids) { |
830 | 0 | gap_set.insert(backend_id); |
831 | 0 | } |
832 | 0 | } |
833 | 0 | } |
834 | 59.1k | return Status::OK(); |
835 | 59.1k | } |
836 | | |
837 | | Status VOlapTablePartitionParam::add_partitions( |
838 | 161 | const std::vector<TOlapTablePartition>& partitions) { |
839 | 345 | for (const auto& t_part : partitions) { |
840 | 345 | auto* part = _obj_pool.add(new VOlapTablePartition(&_partition_block)); |
841 | 345 | part->id = t_part.id; |
842 | 345 | part->is_mutable = t_part.is_mutable; |
843 | | |
844 | | // we dont pass right keys when it's MAX_VALUE. so there's possibility we only have start_key but not end_key |
845 | | // range partition |
846 | 345 | if (t_part.__isset.start_keys) { |
847 | 146 | RETURN_IF_ERROR(_create_partition_keys(t_part.start_keys, &part->start_key)); |
848 | 146 | } |
849 | 345 | if (t_part.__isset.end_keys) { |
850 | 145 | RETURN_IF_ERROR(_create_partition_keys(t_part.end_keys, &part->end_key)); |
851 | 145 | } |
852 | | // list partition - we only set 1 value in 1 partition for new created ones |
853 | 345 | if (t_part.__isset.in_keys) { |
854 | 196 | for (const auto& keys : t_part.in_keys) { |
855 | 196 | RETURN_IF_ERROR(_create_partition_keys( |
856 | 196 | keys, &part->in_keys.emplace_back(&_partition_block, -1))); |
857 | 196 | } |
858 | 196 | if (t_part.__isset.is_default_partition && t_part.is_default_partition) { |
859 | 0 | _default_partition = part; |
860 | 0 | } |
861 | 196 | } |
862 | | |
863 | 345 | part->num_buckets = t_part.num_buckets; |
864 | 345 | if (t_part.__isset.load_tablet_idx) { |
865 | 0 | part->load_tablet_idx = t_part.load_tablet_idx; |
866 | 0 | } |
867 | 345 | if (t_part.__isset.bucket_be_id) { |
868 | 0 | part->bucket_be_id = t_part.bucket_be_id; |
869 | 0 | } |
870 | 345 | if (t_part.__isset.local_bucket_seqs) { |
871 | 0 | part->local_bucket_seqs = t_part.local_bucket_seqs; |
872 | 0 | } |
873 | 345 | auto num_indexes = _schema->indexes().size(); |
874 | 345 | if (t_part.indexes.size() != num_indexes) { |
875 | 0 | return Status::InternalError( |
876 | 0 | "number of partition's index is not equal with schema's" |
877 | 0 | ", num_part_indexes={}, num_schema_indexes={}", |
878 | 0 | t_part.indexes.size(), num_indexes); |
879 | 0 | } |
880 | 345 | part->indexes = t_part.indexes; |
881 | 345 | std::sort(part->indexes.begin(), part->indexes.end(), |
882 | 345 | [](const OlapTableIndexTablets& lhs, const OlapTableIndexTablets& rhs) { |
883 | 0 | return lhs.index_id < rhs.index_id; |
884 | 0 | }); |
885 | | // check index |
886 | 690 | for (int j = 0; j < num_indexes; ++j) { |
887 | 345 | if (part->indexes[j].index_id != _schema->indexes()[j]->index_id) { |
888 | 0 | return Status::InternalError( |
889 | 0 | "partition's index is not equal with schema's" |
890 | 0 | ", part_index={}, schema_index={}", |
891 | 0 | part->indexes[j].index_id, _schema->indexes()[j]->index_id); |
892 | 0 | } |
893 | 345 | } |
894 | 345 | _partitions.emplace_back(part); |
895 | | // after _creating_partiton_keys |
896 | 345 | if (_is_in_partition) { |
897 | 196 | for (auto& in_key : part->in_keys) { |
898 | 196 | _partitions_map->emplace(std::tuple {in_key.first, in_key.second, false}, part); |
899 | 196 | } |
900 | 196 | } else { |
901 | 149 | _partitions_map->emplace(std::tuple {part->end_key.first, part->end_key.second, false}, |
902 | 149 | part); |
903 | 149 | } |
904 | 345 | } |
905 | | |
906 | 161 | return Status::OK(); |
907 | 161 | } |
908 | | |
909 | | Status VOlapTablePartitionParam::replace_partitions( |
910 | | std::vector<int64_t>& old_partition_ids, |
911 | 20 | const std::vector<TOlapTablePartition>& new_partitions) { |
912 | | // remove old replaced partitions |
913 | 20 | DCHECK(old_partition_ids.size() == new_partitions.size()); |
914 | | |
915 | | // init and add new partitions. insert into _partitions |
916 | 52 | for (int i = 0; i < new_partitions.size(); i++) { |
917 | 32 | const auto& t_part = new_partitions[i]; |
918 | | // pair old_partition_ids and new_partitions one by one. TODO: sort to opt performance |
919 | 32 | VOlapTablePartition* old_part = nullptr; |
920 | 32 | auto old_part_id = old_partition_ids[i]; |
921 | 32 | if (auto it = std::find_if( |
922 | 32 | _partitions.begin(), _partitions.end(), |
923 | 77 | [=](const VOlapTablePartition* lhs) { return lhs->id == old_part_id; }); |
924 | 32 | it != _partitions.end()) { |
925 | 32 | old_part = *it; |
926 | 32 | } else { |
927 | 0 | return Status::InternalError("Cannot find old tablet {} in replacing", old_part_id); |
928 | 0 | } |
929 | | |
930 | 32 | auto* part = _obj_pool.add(new VOlapTablePartition(&_partition_block)); |
931 | 32 | part->id = t_part.id; |
932 | 32 | part->is_mutable = t_part.is_mutable; |
933 | | |
934 | | /// just substitute directly. no need to remove and reinsert keys. |
935 | | // range partition |
936 | 32 | part->start_key = std::move(old_part->start_key); |
937 | 32 | part->end_key = std::move(old_part->end_key); |
938 | | // list partition |
939 | 32 | part->in_keys = std::move(old_part->in_keys); |
940 | 32 | if (t_part.__isset.is_default_partition && t_part.is_default_partition) { |
941 | 0 | _default_partition = part; |
942 | 0 | } |
943 | | |
944 | 32 | part->num_buckets = t_part.num_buckets; |
945 | 32 | if (t_part.__isset.load_tablet_idx) { |
946 | 0 | part->load_tablet_idx = t_part.load_tablet_idx; |
947 | 0 | } |
948 | 32 | if (t_part.__isset.bucket_be_id) { |
949 | 0 | part->bucket_be_id = t_part.bucket_be_id; |
950 | 0 | } |
951 | 32 | if (t_part.__isset.local_bucket_seqs) { |
952 | 0 | part->local_bucket_seqs = t_part.local_bucket_seqs; |
953 | 0 | } |
954 | 32 | auto num_indexes = _schema->indexes().size(); |
955 | 32 | if (t_part.indexes.size() != num_indexes) { |
956 | 0 | return Status::InternalError( |
957 | 0 | "number of partition's index is not equal with schema's" |
958 | 0 | ", num_part_indexes={}, num_schema_indexes={}", |
959 | 0 | t_part.indexes.size(), num_indexes); |
960 | 0 | } |
961 | 32 | part->indexes = t_part.indexes; |
962 | 32 | std::sort(part->indexes.begin(), part->indexes.end(), |
963 | 32 | [](const OlapTableIndexTablets& lhs, const OlapTableIndexTablets& rhs) { |
964 | 0 | return lhs.index_id < rhs.index_id; |
965 | 0 | }); |
966 | | // check index |
967 | 64 | for (int j = 0; j < num_indexes; ++j) { |
968 | 32 | if (part->indexes[j].index_id != _schema->indexes()[j]->index_id) { |
969 | 0 | return Status::InternalError( |
970 | 0 | "partition's index is not equal with schema's" |
971 | 0 | ", part_index={}, schema_index={}", |
972 | 0 | part->indexes[j].index_id, _schema->indexes()[j]->index_id); |
973 | 0 | } |
974 | 32 | } |
975 | | |
976 | | // add new partitions with new id. |
977 | 32 | _partitions.emplace_back(part); |
978 | 32 | VLOG_NOTICE << "params add new partition " << part->id; |
979 | | |
980 | | // replace items in _partition_maps |
981 | 32 | if (_is_in_partition) { |
982 | 44 | for (auto& in_key : part->in_keys) { |
983 | 44 | (*_partitions_map)[std::tuple {in_key.first, in_key.second, false}] = part; |
984 | 44 | } |
985 | 21 | } else { |
986 | 11 | (*_partitions_map)[std::tuple {part->end_key.first, part->end_key.second, false}] = |
987 | 11 | part; |
988 | 11 | } |
989 | 32 | } |
990 | | // remove old partitions by id |
991 | 20 | std::ranges::sort(old_partition_ids); |
992 | 129 | for (auto it = _partitions.begin(); it != _partitions.end();) { |
993 | 109 | if (std::ranges::binary_search(old_partition_ids, (*it)->id)) { |
994 | 32 | it = _partitions.erase(it); |
995 | 77 | } else { |
996 | 77 | it++; |
997 | 77 | } |
998 | 109 | } |
999 | | |
1000 | 20 | return Status::OK(); |
1001 | 20 | } |
1002 | | |
1003 | | } // namespace doris |