Coverage Report

Created: 2025-12-04 15:52

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