Coverage Report

Created: 2026-04-14 10:14

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