Coverage Report

Created: 2025-11-28 11:12

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
0
    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
88.9k
    const std::string& col_default_value() const { return _col_default_value; }
96
    PrimitiveType col_type() const;
97
98
271k
    std::shared_ptr<doris::TExpr> get_virtual_column_expr() const {
99
        // virtual_column_expr need do prepare.
100
271k
        return virtual_column_expr;
101
271k
    }
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
186
    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
249
private:
250
    std::string _region; //deprecated
251
    std::string _project;
252
    std::string _table;
253
    std::string _odps_url;   //deprecated
254
    std::string _tunnel_url; //deprecated
255
    std::string _access_key;
256
    std::string _secret_key;
257
    std::string _public_access; //deprecated
258
    std::string _endpoint;
259
    std::string _quota;
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
292k
    MOCK_DEFINE(virtual) ~TupleDescriptor() {
354
292k
        if (_own_slots) {
355
13
            for (SlotDescriptor* slot : _slots) {
356
13
                delete slot;
357
13
            }
358
4
        }
359
292k
    }
360
361
    MOCK_DEFINE(TupleDescriptor() : _id {0} {};)
362
363
504k
    int num_materialized_slots() const { return _num_materialized_slots; }
364
1.33M
    MOCK_FUNCTION const std::vector<SlotDescriptor*>& slots() const { return _slots; }
365
366
487k
    bool has_varlen_slots() const { return _has_varlen_slots; }
367
0
    const TableDescriptor* table_desc() const { return _table_desc; }
368
369
1.01M
    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
200k
    DescriptorTbl() = default;
403
200k
    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
0
    const std::vector<TTupleId>& get_row_tuples() const { return _row_tuples; }
414
415
    // return all registered tuple descriptors
416
44
    std::vector<TupleDescriptor*> get_tuple_descs() const {
417
44
        std::vector<TupleDescriptor*> descs;
418
419
100
        for (auto it : _tuple_desc_map) {
420
100
            descs.push_back(it.second);
421
100
        }
422
423
44
        return descs;
424
44
    }
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
    DescriptorTbl() = default;
440
#endif
441
};
442
443
#define RETURN_IF_INVALID_TUPLE_IDX(tuple_id, tuple_idx)                                         \
444
0
    do {                                                                                         \
445
0
        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
0
    } 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
                  const std::vector<bool>& nullable_tuples);
461
462
    // standard copy c'tor, made explicit here
463
    RowDescriptor(const RowDescriptor& desc)
464
0
            : _tuple_desc_map(desc._tuple_desc_map),
465
0
              _tuple_idx_nullable_map(desc._tuple_idx_nullable_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, bool is_nullable);
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
872k
    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
    // _tuple_idx_nullable_map[i] is true if tuple i can be null
527
    std::vector<bool> _tuple_idx_nullable_map;
528
529
    // map from TupleId to position of tuple w/in row
530
    std::vector<int> _tuple_idx_map;
531
532
    // Provide quick way to check if there are variable length slots.
533
    bool _has_varlen_slots = false;
534
535
    int _num_materialized_slots = 0;
536
    int _num_slots = 0;
537
};
538
#include "common/compile_check_end.h"
539
} // namespace doris