Coverage Report

Created: 2025-04-28 22:38

/root/doris/be/src/runtime/descriptors.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
// This file is copied from
18
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/runtime/descriptors.h
19
// and modified by Doris
20
21
#pragma once
22
23
#include <gen_cpp/Descriptors_types.h>
24
#include <gen_cpp/Types_types.h>
25
#include <glog/logging.h>
26
#include <google/protobuf/stubs/port.h>
27
#include <stdint.h>
28
29
#include <ostream>
30
#include <string>
31
#include <unordered_map>
32
#include <utility>
33
#include <vector>
34
35
#include "common/be_mock_util.h"
36
#include "common/compiler_util.h" // IWYU pragma: keep
37
#include "common/global_types.h"
38
#include "common/object_pool.h"
39
#include "common/status.h"
40
#include "olap/utils.h"
41
#include "runtime/define_primitive_type.h"
42
#include "runtime/types.h"
43
#include "vec/data_types/data_type.h"
44
namespace google::protobuf {
45
template <typename Element>
46
class RepeatedField;
47
} // namespace google::protobuf
48
49
namespace doris {
50
51
class ObjectPool;
52
class PTupleDescriptor;
53
class PSlotDescriptor;
54
55
class SlotDescriptor {
56
public:
57
    MOCK_DEFINE(virtual ~SlotDescriptor() = default;)
58
707k
    SlotId id() const { return _id; }
59
2.96M
    const vectorized::DataTypePtr type() const { return _type; }
60
0
    TupleId parent() const { return _parent; }
61
    // Returns the column index of this slot, including partition keys.
62
    // (e.g., col_pos - num_partition_keys = the table column this slot corresponds to)
63
0
    int col_pos() const { return _col_pos; }
64
    // Returns the field index in the generated llvm struct for this slot's tuple
65
0
    int field_idx() const { return _field_idx; }
66
1.63M
    bool is_materialized() const { return _is_materialized; }
67
244
    bool is_nullable() const { return _type->is_nullable(); }
68
    vectorized::DataTypePtr get_data_type_ptr() const;
69
70
675k
    const std::string& col_name() const { return _col_name; }
71
0
    const std::string& col_name_lower_case() const { return _col_name_lower_case; }
72
73
    void to_protobuf(PSlotDescriptor* pslot) const;
74
75
    std::string debug_string() const;
76
77
    vectorized::MutableColumnPtr get_empty_mutable_column() const;
78
79
23
    int32_t col_unique_id() const { return _col_unique_id; }
80
81
0
    bool is_key() const { return _is_key; }
82
0
    const std::vector<std::string>& column_paths() const { return _column_paths; };
83
84
0
    bool is_auto_increment() const { return _is_auto_increment; }
85
86
0
    bool is_skip_bitmap_col() const { return _col_name == SKIP_BITMAP_COL; }
87
0
    bool is_sequence_col() const { return _col_name == SEQUENCE_COL; }
88
89
4
    const std::string& col_default_value() const { return _col_default_value; }
90
180
    PrimitiveType col_type() const { return _type->get_primitive_type(); }
91
92
private:
93
    friend class DescriptorTbl;
94
    friend class TupleDescriptor;
95
    friend class SchemaScanner;
96
    friend class OlapTableSchemaParam;
97
    friend class PInternalServiceImpl;
98
    friend class RowIdStorageReader;
99
    friend class Tablet;
100
    friend class TabletSchema;
101
102
    MOCK_REMOVE(const) SlotId _id;
103
    MOCK_REMOVE(const) vectorized::DataTypePtr _type;
104
    const TupleId _parent;
105
    const int _col_pos;
106
    const std::string _col_name;
107
    const std::string _col_name_lower_case;
108
109
    const int32_t _col_unique_id;
110
111
    // the idx of the slot in the tuple descriptor (0-based).
112
    // this is provided by the FE
113
    const int _slot_idx;
114
115
    // the idx of the slot in the llvm codegen'd tuple struct
116
    // this is set by TupleDescriptor during codegen and takes into account
117
    // leading null bytes.
118
    int _field_idx;
119
120
    const bool _is_materialized;
121
122
    const bool _is_key;
123
    const std::vector<std::string> _column_paths;
124
125
    const bool _is_auto_increment;
126
    const std::string _col_default_value;
127
128
    SlotDescriptor(const TSlotDescriptor& tdesc);
129
    SlotDescriptor(const PSlotDescriptor& pdesc);
130
    MOCK_DEFINE(SlotDescriptor();)
131
};
132
133
// Base class for table descriptors.
134
class TableDescriptor {
135
public:
136
    TableDescriptor(const TTableDescriptor& tdesc);
137
20
    virtual ~TableDescriptor() = default;
138
0
    int num_cols() const { return _num_cols; }
139
0
    int num_clustering_cols() const { return _num_clustering_cols; }
140
    virtual std::string debug_string() const;
141
142
    // The first _num_clustering_cols columns by position are clustering
143
    // columns.
144
0
    bool is_clustering_col(const SlotDescriptor* slot_desc) const {
145
0
        return slot_desc->col_pos() < _num_clustering_cols;
146
0
    }
147
148
0
    ::doris::TTableType::type table_type() const { return _table_type; }
149
0
    const std::string& name() const { return _name; }
150
0
    const std::string& database() const { return _database; }
151
0
    int64_t table_id() const { return _table_id; }
152
153
private:
154
    ::doris::TTableType::type _table_type;
155
    std::string _name;
156
    std::string _database;
157
    int64_t _table_id;
158
    int _num_cols;
159
    int _num_clustering_cols;
160
};
161
162
class OlapTableDescriptor : public TableDescriptor {
163
public:
164
    OlapTableDescriptor(const TTableDescriptor& tdesc);
165
    std::string debug_string() const override;
166
};
167
168
class SchemaTableDescriptor : public TableDescriptor {
169
public:
170
    SchemaTableDescriptor(const TTableDescriptor& tdesc);
171
    ~SchemaTableDescriptor() override;
172
    std::string debug_string() const override;
173
0
    TSchemaTableType::type schema_table_type() const { return _schema_table_type; }
174
175
private:
176
    TSchemaTableType::type _schema_table_type;
177
};
178
179
class BrokerTableDescriptor : public TableDescriptor {
180
public:
181
    BrokerTableDescriptor(const TTableDescriptor& tdesc);
182
    ~BrokerTableDescriptor() override;
183
    std::string debug_string() const override;
184
185
private:
186
};
187
188
class HiveTableDescriptor : public TableDescriptor {
189
public:
190
    HiveTableDescriptor(const TTableDescriptor& tdesc);
191
    ~HiveTableDescriptor() override;
192
    std::string debug_string() const override;
193
194
private:
195
};
196
197
class IcebergTableDescriptor : public TableDescriptor {
198
public:
199
    IcebergTableDescriptor(const TTableDescriptor& tdesc);
200
    ~IcebergTableDescriptor() override;
201
    std::string debug_string() const override;
202
203
private:
204
};
205
206
class MaxComputeTableDescriptor : public TableDescriptor {
207
public:
208
    MaxComputeTableDescriptor(const TTableDescriptor& tdesc);
209
    ~MaxComputeTableDescriptor() override;
210
    std::string debug_string() const override;
211
0
    std::string region() const { return _region; }
212
0
    std::string project() const { return _project; }
213
0
    std::string table() const { return _table; }
214
0
    std::string odps_url() const { return _odps_url; }
215
0
    std::string tunnel_url() const { return _tunnel_url; }
216
0
    std::string access_key() const { return _access_key; }
217
0
    std::string secret_key() const { return _secret_key; }
218
0
    std::string public_access() const { return _public_access; }
219
0
    std::string endpoint() const { return _endpoint; }
220
0
    std::string quota() const { return _quota; }
221
0
    Status init_status() const { return _init_status; }
222
223
private:
224
    std::string _region; //deprecated
225
    std::string _project;
226
    std::string _table;
227
    std::string _odps_url;   //deprecated
228
    std::string _tunnel_url; //deprecated
229
    std::string _access_key;
230
    std::string _secret_key;
231
    std::string _public_access; //deprecated
232
    std::string _endpoint;
233
    std::string _quota;
234
    Status _init_status = Status::OK();
235
};
236
237
class TrinoConnectorTableDescriptor : public TableDescriptor {
238
public:
239
    TrinoConnectorTableDescriptor(const TTableDescriptor& tdesc);
240
    ~TrinoConnectorTableDescriptor() override;
241
    std::string debug_string() const override;
242
243
private:
244
};
245
246
class EsTableDescriptor : public TableDescriptor {
247
public:
248
    EsTableDescriptor(const TTableDescriptor& tdesc);
249
    ~EsTableDescriptor() override;
250
    std::string debug_string() const override;
251
252
private:
253
};
254
255
class MySQLTableDescriptor : public TableDescriptor {
256
public:
257
    MySQLTableDescriptor(const TTableDescriptor& tdesc);
258
    std::string debug_string() const override;
259
0
    std::string mysql_db() const { return _mysql_db; }
260
0
    std::string mysql_table() const { return _mysql_table; }
261
0
    std::string host() const { return _host; }
262
0
    std::string port() const { return _port; }
263
0
    std::string user() const { return _user; }
264
0
    std::string passwd() const { return _passwd; }
265
0
    std::string charset() const { return _charset; }
266
267
private:
268
    std::string _mysql_db;
269
    std::string _mysql_table;
270
    std::string _host;
271
    std::string _port;
272
    std::string _user;
273
    std::string _passwd;
274
    std::string _charset;
275
};
276
277
class ODBCTableDescriptor : public TableDescriptor {
278
public:
279
    ODBCTableDescriptor(const TTableDescriptor& tdesc);
280
    std::string debug_string() const override;
281
0
    std::string db() const { return _db; }
282
0
    std::string table() const { return _table; }
283
0
    std::string host() const { return _host; }
284
0
    std::string port() const { return _port; }
285
0
    std::string user() const { return _user; }
286
0
    std::string passwd() const { return _passwd; }
287
0
    std::string driver() const { return _driver; }
288
0
    TOdbcTableType::type type() const { return _type; }
289
290
private:
291
    std::string _db;
292
    std::string _table;
293
    std::string _host;
294
    std::string _port;
295
    std::string _user;
296
    std::string _passwd;
297
    std::string _driver;
298
    TOdbcTableType::type _type;
299
};
300
301
class JdbcTableDescriptor : public TableDescriptor {
302
public:
303
    JdbcTableDescriptor(const TTableDescriptor& tdesc);
304
    std::string debug_string() const override;
305
0
    int64_t jdbc_catalog_id() const { return _jdbc_catalog_id; }
306
0
    const std::string& jdbc_resource_name() const { return _jdbc_resource_name; }
307
0
    const std::string& jdbc_driver_url() const { return _jdbc_driver_url; }
308
0
    const std::string& jdbc_driver_class() const { return _jdbc_driver_class; }
309
0
    const std::string& jdbc_driver_checksum() const { return _jdbc_driver_checksum; }
310
0
    const std::string& jdbc_url() const { return _jdbc_url; }
311
0
    const std::string& jdbc_table_name() const { return _jdbc_table_name; }
312
0
    const std::string& jdbc_user() const { return _jdbc_user; }
313
0
    const std::string& jdbc_passwd() const { return _jdbc_passwd; }
314
0
    int32_t connection_pool_min_size() const { return _connection_pool_min_size; }
315
0
    int32_t connection_pool_max_size() const { return _connection_pool_max_size; }
316
0
    int32_t connection_pool_max_wait_time() const { return _connection_pool_max_wait_time; }
317
0
    int32_t connection_pool_max_life_time() const { return _connection_pool_max_life_time; }
318
0
    bool connection_pool_keep_alive() const { return _connection_pool_keep_alive; }
319
320
private:
321
    int64_t _jdbc_catalog_id;
322
    std::string _jdbc_resource_name;
323
    std::string _jdbc_driver_url;
324
    std::string _jdbc_driver_class;
325
    std::string _jdbc_driver_checksum;
326
    std::string _jdbc_url;
327
    std::string _jdbc_table_name;
328
    std::string _jdbc_user;
329
    std::string _jdbc_passwd;
330
    int32_t _connection_pool_min_size;
331
    int32_t _connection_pool_max_size;
332
    int32_t _connection_pool_max_wait_time;
333
    int32_t _connection_pool_max_life_time;
334
    bool _connection_pool_keep_alive;
335
};
336
337
class TupleDescriptor {
338
public:
339
    TupleDescriptor(TupleDescriptor&&) = delete;
340
    void operator=(const TupleDescriptor&) = delete;
341
342
312k
    MOCK_DEFINE(virtual) ~TupleDescriptor() {
343
312k
        if (_own_slots) {
344
13
            for (SlotDescriptor* slot : _slots) {
345
13
                delete slot;
346
13
            }
347
4
        }
348
312k
    }
349
350
    MOCK_DEFINE(TupleDescriptor() : _id {0} {};)
351
352
546k
    int num_materialized_slots() const { return _num_materialized_slots; }
353
1.21M
    MOCK_FUNCTION const std::vector<SlotDescriptor*>& slots() const { return _slots; }
354
355
524k
    bool has_varlen_slots() const { return _has_varlen_slots; }
356
0
    const TableDescriptor* table_desc() const { return _table_desc; }
357
358
1.09M
    TupleId id() const { return _id; }
359
360
    std::string debug_string() const;
361
362
    void to_protobuf(PTupleDescriptor* ptuple) const;
363
364
private:
365
    friend class DescriptorTbl;
366
    friend class SchemaScanner;
367
    friend class OlapTableSchemaParam;
368
    friend class PInternalServiceImpl;
369
    friend class RowIdStorageReader;
370
    friend class TabletSchema;
371
372
    const TupleId _id;
373
    TableDescriptor* _table_desc = nullptr;
374
    int _num_materialized_slots;
375
    std::vector<SlotDescriptor*> _slots; // contains all slots
376
377
    // Provide quick way to check if there are variable length slots.
378
    // True if _string_slots or _collection_slots have entries.
379
    bool _has_varlen_slots;
380
    bool _own_slots = false;
381
382
    TupleDescriptor(const TTupleDescriptor& tdesc, bool own_slot = false);
383
    TupleDescriptor(const PTupleDescriptor& tdesc, bool own_slot = false);
384
385
    void add_slot(SlotDescriptor* slot);
386
};
387
388
class DescriptorTbl {
389
public:
390
    // Creates a descriptor tbl within 'pool' from thrift_tbl and returns it via 'tbl'.
391
    // Returns OK on success, otherwise error (in which case 'tbl' will be unset).
392
    static Status create(ObjectPool* pool, const TDescriptorTable& thrift_tbl, DescriptorTbl** tbl);
393
394
    TableDescriptor* get_table_descriptor(TableId id) const;
395
    TupleDescriptor* get_tuple_descriptor(TupleId id) const;
396
    SlotDescriptor* get_slot_descriptor(SlotId id) const;
397
0
    const std::vector<TTupleId>& get_row_tuples() const { return _row_tuples; }
398
399
    // return all registered tuple descriptors
400
45
    std::vector<TupleDescriptor*> get_tuple_descs() const {
401
45
        std::vector<TupleDescriptor*> descs;
402
403
102
        for (auto it : _tuple_desc_map) {
404
102
            descs.push_back(it.second);
405
102
        }
406
407
45
        return descs;
408
45
    }
409
410
    std::string debug_string() const;
411
412
private:
413
    using TableDescriptorMap = std::unordered_map<TableId, TableDescriptor*>;
414
    using TupleDescriptorMap = std::unordered_map<TupleId, TupleDescriptor*>;
415
    using SlotDescriptorMap = std::unordered_map<SlotId, SlotDescriptor*>;
416
417
    TableDescriptorMap _tbl_desc_map;
418
    TupleDescriptorMap _tuple_desc_map;
419
    SlotDescriptorMap _slot_desc_map;
420
    std::vector<TTupleId> _row_tuples;
421
422
78.1k
    DescriptorTbl() = default;
423
};
424
425
#define RETURN_IF_INVALID_TUPLE_IDX(tuple_id, tuple_idx)                                         \
426
0
    do {                                                                                         \
427
0
        if (UNLIKELY(RowDescriptor::INVALID_IDX == tuple_idx)) {                                 \
428
0
            return Status::InternalError("failed to get tuple idx with tuple id: {}", tuple_id); \
429
0
        }                                                                                        \
430
0
    } while (false)
431
432
// Records positions of tuples within row produced by ExecNode.
433
// TODO: this needs to differentiate between tuples contained in row
434
// and tuples produced by ExecNode (parallel to PlanNode.rowTupleIds and
435
// PlanNode.tupleIds); right now, we conflate the two (and distinguish based on
436
// context; for instance, HdfsScanNode uses these tids to create row batches, ie, the
437
// first case, whereas TopNNode uses these tids to copy output rows, ie, the second
438
// case)
439
class RowDescriptor {
440
public:
441
    RowDescriptor(const DescriptorTbl& desc_tbl, const std::vector<TTupleId>& row_tuples,
442
                  const std::vector<bool>& nullable_tuples);
443
444
    // standard copy c'tor, made explicit here
445
    RowDescriptor(const RowDescriptor& desc)
446
            : _tuple_desc_map(desc._tuple_desc_map),
447
              _tuple_idx_nullable_map(desc._tuple_idx_nullable_map),
448
              _tuple_idx_map(desc._tuple_idx_map),
449
0
              _has_varlen_slots(desc._has_varlen_slots) {
450
0
        auto it = desc._tuple_desc_map.begin();
451
0
        for (; it != desc._tuple_desc_map.end(); ++it) {
452
0
            _num_materialized_slots += (*it)->num_materialized_slots();
453
0
            _num_slots += (*it)->slots().size();
454
0
        }
455
0
    }
456
457
    RowDescriptor(TupleDescriptor* tuple_desc, bool is_nullable);
458
459
    RowDescriptor(const RowDescriptor& lhs_row_desc, const RowDescriptor& rhs_row_desc);
460
461
    // dummy descriptor, needed for the JNI EvalPredicate() function
462
156k
    RowDescriptor() = default;
463
464
    MOCK_DEFINE(virtual ~RowDescriptor() = default;)
465
466
503k
    int num_materialized_slots() const { return _num_materialized_slots; }
467
468
2
    int num_slots() const { return _num_slots; }
469
470
    static const int INVALID_IDX;
471
472
    // Returns INVALID_IDX if id not part of this row.
473
    int get_tuple_idx(TupleId id) const;
474
475
    // Return true if any Tuple has variable length slots.
476
0
    bool has_varlen_slots() const { return _has_varlen_slots; }
477
478
    // Return descriptors for all tuples in this row, in order of appearance.
479
390k
    MOCK_FUNCTION const std::vector<TupleDescriptor*>& tuple_descriptors() const {
480
390k
        return _tuple_desc_map;
481
390k
    }
482
483
    // Populate row_tuple_ids with our ids.
484
    void to_thrift(std::vector<TTupleId>* row_tuple_ids);
485
    void to_protobuf(google::protobuf::RepeatedField<google::protobuf::int32>* row_tuple_ids) const;
486
487
    // Return true if the tuple ids of this descriptor are a prefix
488
    // of the tuple ids of other_desc.
489
    bool is_prefix_of(const RowDescriptor& other_desc) const;
490
491
    // Return true if the tuple ids of this descriptor match tuple ids of other desc.
492
    bool equals(const RowDescriptor& other_desc) const;
493
494
    std::string debug_string() const;
495
496
    int get_column_id(int slot_id, bool force_materialize_slot = false) const;
497
498
private:
499
    // Initializes tupleIdxMap during c'tor using the _tuple_desc_map.
500
    void init_tuple_idx_map();
501
502
    // Initializes _has_varlen_slots during c'tor using the _tuple_desc_map.
503
    void init_has_varlen_slots();
504
505
    // map from position of tuple w/in row to its descriptor
506
    std::vector<TupleDescriptor*> _tuple_desc_map;
507
508
    // _tuple_idx_nullable_map[i] is true if tuple i can be null
509
    std::vector<bool> _tuple_idx_nullable_map;
510
511
    // map from TupleId to position of tuple w/in row
512
    std::vector<int> _tuple_idx_map;
513
514
    // Provide quick way to check if there are variable length slots.
515
    bool _has_varlen_slots = false;
516
517
    int _num_materialized_slots = 0;
518
    int _num_slots = 0;
519
};
520
} // namespace doris