Coverage Report

Created: 2025-07-27 03:09

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