Coverage Report

Created: 2025-05-02 01:45

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