Coverage Report

Created: 2026-02-28 14:41

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