Coverage Report

Created: 2026-02-27 00:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/exec/tablet_info.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/fast_rand.h>
21
#include <gen_cpp/Descriptors_types.h>
22
#include <gen_cpp/Types_types.h>
23
#include <gen_cpp/descriptors.pb.h>
24
#include <gen_cpp/olap_file.pb.h>
25
26
#include <cstdint>
27
#include <functional>
28
#include <iterator>
29
#include <map>
30
#include <memory>
31
#include <string>
32
#include <tuple>
33
#include <unordered_map>
34
#include <utility>
35
#include <vector>
36
37
#include "common/cast_set.h"
38
#include "common/logging.h"
39
#include "common/object_pool.h"
40
#include "common/status.h"
41
#include "runtime/descriptors.h"
42
#include "runtime/raw_value.h"
43
#include "vec/columns/column.h"
44
#include "vec/core/block.h"
45
#include "vec/core/column_with_type_and_name.h"
46
#include "vec/exprs/vexpr.h"
47
#include "vec/exprs/vexpr_fwd.h"
48
49
namespace doris {
50
#include "common/compile_check_begin.h"
51
class MemTracker;
52
class SlotDescriptor;
53
class TExprNode;
54
class TabletColumn;
55
class TabletIndex;
56
class TupleDescriptor;
57
58
struct OlapTableIndexSchema {
59
    int64_t index_id;
60
    std::vector<SlotDescriptor*> slots;
61
    int32_t schema_hash;
62
    std::vector<TabletColumn*> columns;
63
    std::vector<TabletIndex*> indexes;
64
    vectorized::VExprContextSPtr where_clause;
65
66
    void to_protobuf(POlapTableIndexSchema* pindex) const;
67
};
68
69
class OlapTableSchemaParam {
70
public:
71
53
    OlapTableSchemaParam() = default;
72
53
    ~OlapTableSchemaParam() noexcept = default;
73
74
    Status init(const TOlapTableSchemaParam& tschema);
75
    Status init(const POlapTableSchemaParam& pschema);
76
77
30
    int64_t db_id() const { return _db_id; }
78
30
    int64_t table_id() const { return _table_id; }
79
0
    int64_t version() const { return _version; }
80
81
24
    TupleDescriptor* tuple_desc() const { return _tuple_desc; }
82
116
    const std::vector<OlapTableIndexSchema*>& indexes() const { return _indexes; }
83
84
    void to_protobuf(POlapTableSchemaParam* pschema) const;
85
86
    // NOTE: this function is not thread-safe.
87
16
    POlapTableSchemaParam* to_protobuf() const {
88
16
        if (_proto_schema == nullptr) {
89
16
            _proto_schema = _obj_pool.add(new POlapTableSchemaParam());
90
16
            to_protobuf(_proto_schema);
91
16
        }
92
16
        return _proto_schema;
93
16
    }
94
95
28
    UniqueKeyUpdateModePB unique_key_update_mode() const { return _unique_key_update_mode; }
96
97
28
    bool is_partial_update() const {
98
28
        return _unique_key_update_mode != UniqueKeyUpdateModePB::UPSERT;
99
28
    }
100
0
    bool is_fixed_partial_update() const {
101
0
        return _unique_key_update_mode == UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS;
102
0
    }
103
0
    bool is_flexible_partial_update() const {
104
0
        return _unique_key_update_mode == UniqueKeyUpdateModePB::UPDATE_FLEXIBLE_COLUMNS;
105
0
    }
106
107
28
    std::set<std::string> partial_update_input_columns() const {
108
28
        return _partial_update_input_columns;
109
28
    }
110
28
    PartialUpdateNewRowPolicyPB partial_update_new_key_policy() const {
111
28
        return _partial_update_new_row_policy;
112
28
    }
113
28
    std::string auto_increment_coulumn() const { return _auto_increment_column; }
114
0
    int32_t auto_increment_column_unique_id() const { return _auto_increment_column_unique_id; }
115
0
    void set_timestamp_ms(int64_t timestamp_ms) { _timestamp_ms = timestamp_ms; }
116
28
    int64_t timestamp_ms() const { return _timestamp_ms; }
117
0
    void set_nano_seconds(int32_t nano_seconds) { _nano_seconds = nano_seconds; }
118
28
    int32_t nano_seconds() const { return _nano_seconds; }
119
0
    void set_timezone(std::string timezone) { _timezone = timezone; }
120
28
    std::string timezone() const { return _timezone; }
121
28
    bool is_strict_mode() const { return _is_strict_mode; }
122
28
    int32_t sequence_map_col_uid() const { return _sequence_map_col_uid; }
123
    std::string debug_string() const;
124
125
    Status init_unique_key_update_mode(const TOlapTableSchemaParam& tschema);
126
127
private:
128
    int64_t _db_id;
129
    int64_t _table_id;
130
    int64_t _version;
131
132
    TupleDescriptor* _tuple_desc = nullptr;
133
    mutable POlapTableSchemaParam* _proto_schema = nullptr;
134
    std::vector<OlapTableIndexSchema*> _indexes;
135
    mutable ObjectPool _obj_pool;
136
    UniqueKeyUpdateModePB _unique_key_update_mode {UniqueKeyUpdateModePB::UPSERT};
137
    PartialUpdateNewRowPolicyPB _partial_update_new_row_policy {
138
            PartialUpdateNewRowPolicyPB::APPEND};
139
    std::set<std::string> _partial_update_input_columns;
140
    bool _is_strict_mode = false;
141
    std::string _auto_increment_column;
142
    int32_t _auto_increment_column_unique_id;
143
    int64_t _timestamp_ms = 0;
144
    int32_t _nano_seconds {0};
145
    std::string _timezone;
146
    int32_t _sequence_map_col_uid {-1};
147
};
148
149
using OlapTableIndexTablets = TOlapTableIndexTablets;
150
// struct TOlapTableIndexTablets {
151
//     1: required i64 index_id
152
//     2: required list<i64> tablets
153
// }
154
155
using BlockRow = std::pair<vectorized::Block*, int32_t>;
156
using BlockRowWithIndicator =
157
        std::tuple<vectorized::Block*, int32_t, bool>; // [block, row, is_transformed]
158
159
struct VOlapTablePartition {
160
    int64_t id = 0;
161
    BlockRow start_key;
162
    BlockRow end_key;
163
    std::vector<BlockRow> in_keys;
164
    int64_t num_buckets = 0;
165
    std::vector<OlapTableIndexTablets> indexes;
166
    bool is_mutable;
167
    // -1 indicates partition with hash distribution
168
    int64_t load_tablet_idx = -1;
169
    int total_replica_num = 0;
170
    int load_required_replica_num = 0;
171
172
    VOlapTablePartition(vectorized::Block* partition_block)
173
            // the default value of partition bound is -1.
174
21
            : start_key {partition_block, -1}, end_key {partition_block, -1} {}
175
};
176
177
// this is only used by tablet_sink. so we can assume it's inited by its' descriptor.
178
class VOlapTablePartKeyComparator {
179
public:
180
    VOlapTablePartKeyComparator(const std::vector<uint16_t>& slot_locs,
181
                                const std::vector<uint16_t>& params_locs)
182
36
            : _slot_locs(slot_locs), _param_locs(params_locs) {}
183
184
    // return true if lhs < rhs
185
    // 'row' is -1 mean maximal boundary
186
    bool operator()(const BlockRowWithIndicator& lhs, const BlockRowWithIndicator& rhs) const;
187
188
private:
189
    const std::vector<uint16_t>& _slot_locs;
190
    const std::vector<uint16_t>& _param_locs;
191
};
192
193
// store an olap table's tablet information
194
class VOlapTablePartitionParam {
195
public:
196
    VOlapTablePartitionParam(std::shared_ptr<OlapTableSchemaParam>& schema,
197
                             const TOlapTablePartitionParam& param);
198
199
    ~VOlapTablePartitionParam();
200
201
    Status init();
202
203
4
    int64_t db_id() const { return _t_param.db_id; }
204
4
    int64_t table_id() const { return _t_param.table_id; }
205
0
    int64_t version() const { return _t_param.version; }
206
207
    // return true if we found this block_row in partition
208
    ALWAYS_INLINE bool find_partition(vectorized::Block* block, int row,
209
27
                                      VOlapTablePartition*& partition) const {
210
27
        auto it = _is_in_partition ? _partitions_map->find(std::tuple {block, row, true})
211
27
                                   : _partitions_map->upper_bound(std::tuple {block, row, true});
212
27
        VLOG_TRACE << "find row " << row << " of\n"
213
0
                   << block->dump_data() << "in:\n"
214
0
                   << _partition_block.dump_data() << "result line row: " << std::get<1>(it->first);
215
216
        // for list partition it might result in default partition
217
27
        if (_is_in_partition) {
218
0
            partition = (it != _partitions_map->end()) ? it->second : _default_partition;
219
0
            it = _partitions_map->end();
220
0
        }
221
27
        if (it != _partitions_map->end() &&
222
27
            _part_contains(it->second, std::tuple {block, row, true})) {
223
23
            partition = it->second;
224
23
        }
225
27
        return (partition != nullptr);
226
27
    }
227
228
    ALWAYS_INLINE void find_tablets(
229
            vectorized::Block* block, const std::vector<uint32_t>& indexes,
230
            const std::vector<VOlapTablePartition*>& partitions,
231
            std::vector<uint32_t>& tablet_indexes /*result*/,
232
            /*TODO: check if flat hash map will be better*/
233
14
            std::map<VOlapTablePartition*, int64_t>* partition_tablets_buffer = nullptr) const {
234
14
        std::function<uint32_t(vectorized::Block*, uint32_t, const VOlapTablePartition&)>
235
14
                compute_function;
236
14
        if (!_distributed_slot_locs.empty()) {
237
            //TODO: refactor by saving the hash values. then we can calculate in columnwise.
238
11
            compute_function = [this](vectorized::Block* block, uint32_t row,
239
14
                                      const VOlapTablePartition& partition) -> uint32_t {
240
14
                uint32_t hash_val = 0;
241
14
                for (unsigned short _distributed_slot_loc : _distributed_slot_locs) {
242
14
                    auto* slot_desc = _slots[_distributed_slot_loc];
243
14
                    auto& column = block->get_by_position(_distributed_slot_loc).column;
244
14
                    auto val = column->get_data_at(row);
245
14
                    if (val.data != nullptr) {
246
14
                        hash_val = RawValue::zlib_crc32(val.data, val.size,
247
14
                                                        slot_desc->type()->get_primitive_type(),
248
14
                                                        hash_val);
249
14
                    } else {
250
0
                        hash_val = HashUtil::zlib_crc_hash_null(hash_val);
251
0
                    }
252
14
                }
253
14
                return cast_set<uint32_t>(hash_val % partition.num_buckets);
254
14
            };
255
11
        } else { // random distribution
256
3
            compute_function = [](vectorized::Block* block, uint32_t row,
257
3
                                  const VOlapTablePartition& partition) -> uint32_t {
258
3
                if (partition.load_tablet_idx == -1) {
259
                    // for compatible with old version, just do random
260
0
                    return cast_set<uint32_t>(butil::fast_rand() % partition.num_buckets);
261
0
                }
262
3
                return cast_set<uint32_t>(partition.load_tablet_idx % partition.num_buckets);
263
3
            };
264
3
        }
265
266
14
        if (partition_tablets_buffer == nullptr) {
267
14
            for (auto index : indexes) {
268
14
                tablet_indexes[index] = compute_function(block, index, *partitions[index]);
269
14
            }
270
11
        } else { // use buffer
271
6
            for (auto index : indexes) {
272
6
                auto* partition = partitions[index];
273
6
                if (auto it = partition_tablets_buffer->find(partition);
274
6
                    it != partition_tablets_buffer->end()) {
275
3
                    tablet_indexes[index] = cast_set<uint32_t>(it->second); // tablet
276
3
                } else {
277
                    // compute and save in buffer
278
3
                    (*partition_tablets_buffer)[partition] = tablet_indexes[index] =
279
3
                            compute_function(block, index, *partitions[index]);
280
3
                }
281
6
            }
282
3
        }
283
14
    }
284
285
0
    const std::vector<VOlapTablePartition*>& get_partitions() const { return _partitions; }
286
287
    // it's same with auto now because we only support transformed partition in auto partition. may expand in future
288
9
    bool is_projection_partition() const { return _is_auto_partition; }
289
19
    bool is_auto_partition() const { return _is_auto_partition; }
290
291
9
    bool is_auto_detect_overwrite() const { return _is_auto_detect_overwrite; }
292
2
    int64_t get_overwrite_group_id() const { return _overwrite_group_id; }
293
294
0
    std::shared_ptr<TNetworkAddress> get_master_address() const { return _master_address; }
295
296
2
    std::vector<uint16_t> get_partition_keys() const { return _partition_slot_locs; }
297
298
    Status add_partitions(const std::vector<TOlapTablePartition>& partitions);
299
    // no need to del/reinsert partition keys, but change the link. reset the _partitions items
300
    Status replace_partitions(std::vector<int64_t>& old_partition_ids,
301
                              const std::vector<TOlapTablePartition>& new_partitions);
302
303
13
    vectorized::VExprContextSPtrs get_part_func_ctx() { return _part_func_ctx; }
304
13
    vectorized::VExprSPtrs get_partition_function() { return _partition_function; }
305
306
    // which will affect _partition_block
307
    Status generate_partition_from(const TOlapTablePartition& t_part,
308
                                   VOlapTablePartition*& part_result);
309
310
2
    void set_transformed_slots(const std::vector<uint16_t>& new_slots) {
311
2
        _transformed_slot_locs = new_slots;
312
2
    }
313
314
private:
315
    Status _create_partition_keys(const std::vector<TExprNode>& t_exprs, BlockRow* part_key);
316
317
    // check if this partition contain this key
318
    bool _part_contains(VOlapTablePartition* part, BlockRowWithIndicator key) const;
319
320
    // this partition only valid in this schema
321
    std::shared_ptr<OlapTableSchemaParam> _schema;
322
    TOlapTablePartitionParam _t_param;
323
324
    const std::vector<SlotDescriptor*>& _slots;
325
    std::vector<uint16_t> _partition_slot_locs;
326
    std::vector<uint16_t> _transformed_slot_locs;
327
    std::vector<uint16_t> _distributed_slot_locs;
328
329
    ObjectPool _obj_pool;
330
    vectorized::Block _partition_block;
331
    std::unique_ptr<MemTracker> _mem_tracker;
332
    std::vector<VOlapTablePartition*> _partitions;
333
    // For all partition value rows saved in this map, indicator is false. whenever we use a value to find in it, the param is true.
334
    // so that we can distinguish which column index to use (origin slots or transformed slots).
335
    // For range partition we ONLY SAVE RIGHT ENDS. when we find a part's RIGHT by a value, check if part's left cover it then.
336
    std::unique_ptr<
337
            std::map<BlockRowWithIndicator, VOlapTablePartition*, VOlapTablePartKeyComparator>>
338
            _partitions_map;
339
340
    bool _is_in_partition = false;
341
    size_t _mem_usage = 0;
342
    // only works when using list partition, the resource is owned by _partitions
343
    VOlapTablePartition* _default_partition = nullptr;
344
345
    bool _is_auto_partition = false;
346
    vectorized::VExprContextSPtrs _part_func_ctx = {nullptr};
347
    vectorized::VExprSPtrs _partition_function = {nullptr};
348
    TPartitionType::type _part_type; // support list or range
349
    // "insert overwrite partition(*)", detect which partitions by BE
350
    bool _is_auto_detect_overwrite = false;
351
    int64_t _overwrite_group_id = 0;
352
    std::shared_ptr<TNetworkAddress> _master_address = nullptr;
353
};
354
355
// indicate where's the tablet and all its replications (node-wise)
356
using TabletLocation = TTabletLocation;
357
// struct TTabletLocation {
358
//     1: required i64 tablet_id
359
//     2: required list<i64> node_ids
360
// }
361
362
class OlapTableLocationParam {
363
public:
364
8
    OlapTableLocationParam(const TOlapTableLocationParam& t_param) : _t_param(t_param) {
365
16
        for (auto& location : _t_param.tablets) {
366
16
            _tablets.emplace(location.tablet_id, &location);
367
16
        }
368
8
    }
369
370
0
    int64_t db_id() const { return _t_param.db_id; }
371
0
    int64_t table_id() const { return _t_param.table_id; }
372
0
    int64_t version() const { return _t_param.version; }
373
374
0
    TabletLocation* find_tablet(int64_t tablet_id) const {
375
0
        auto it = _tablets.find(tablet_id);
376
0
        if (it != std::end(_tablets)) {
377
0
            return it->second;
378
0
        }
379
0
        return nullptr;
380
0
    }
381
382
0
    void add_locations(std::vector<TTabletLocation>& locations) {
383
0
        for (auto& location : locations) {
384
0
            if (_tablets.find(location.tablet_id) == _tablets.end()) {
385
0
                _tablets[location.tablet_id] = &location;
386
0
            }
387
0
        }
388
0
    }
389
390
private:
391
    TOlapTableLocationParam _t_param;
392
    // [tablet_id, tablet]. tablet has id, also.
393
    std::unordered_map<int64_t, TabletLocation*> _tablets;
394
};
395
396
struct NodeInfo {
397
    int64_t id;
398
    int64_t option;
399
    std::string host;
400
    int32_t brpc_port;
401
402
0
    NodeInfo() = default;
403
404
    NodeInfo(const TNodeInfo& tnode)
405
0
            : id(tnode.id),
406
0
              option(tnode.option),
407
0
              host(tnode.host),
408
0
              brpc_port(tnode.async_internal_port) {}
409
};
410
411
class DorisNodesInfo {
412
public:
413
0
    DorisNodesInfo() = default;
414
0
    DorisNodesInfo(const TPaloNodesInfo& t_nodes) {
415
0
        for (const auto& node : t_nodes.nodes) {
416
0
            _nodes.emplace(node.id, node);
417
0
        }
418
0
    }
419
0
    void setNodes(const TPaloNodesInfo& t_nodes) {
420
0
        _nodes.clear();
421
0
        for (const auto& node : t_nodes.nodes) {
422
0
            _nodes.emplace(node.id, node);
423
0
        }
424
0
    }
425
0
    const NodeInfo* find_node(int64_t id) const {
426
0
        auto it = _nodes.find(id);
427
0
        if (it != std::end(_nodes)) {
428
0
            return &it->second;
429
0
        }
430
0
        return nullptr;
431
0
    }
432
433
0
    void add_nodes(const std::vector<TNodeInfo>& t_nodes) {
434
0
        for (const auto& node : t_nodes) {
435
0
            const auto* node_info = find_node(node.id);
436
0
            if (node_info == nullptr) {
437
0
                _nodes.emplace(node.id, node);
438
0
            }
439
0
        }
440
0
    }
441
442
0
    const std::unordered_map<int64_t, NodeInfo>& nodes_info() { return _nodes; }
443
444
private:
445
    std::unordered_map<int64_t, NodeInfo> _nodes;
446
};
447
448
#include "common/compile_check_end.h"
449
} // namespace doris