Coverage Report

Created: 2024-11-21 14:00

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