Coverage Report

Created: 2025-03-10 22:58

/root/doris/be/src/runtime/descriptors.cpp
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.cc
19
// and modified by Doris
20
21
#include "runtime/descriptors.h"
22
23
#include <fmt/format.h>
24
#include <gen_cpp/Descriptors_types.h>
25
#include <gen_cpp/Types_types.h>
26
#include <gen_cpp/descriptors.pb.h>
27
#include <stddef.h>
28
29
#include <algorithm>
30
#include <boost/algorithm/string/join.hpp>
31
#include <memory>
32
33
#include "common/object_pool.h"
34
#include "runtime/primitive_type.h"
35
#include "util/string_util.h"
36
#include "vec/aggregate_functions/aggregate_function.h"
37
#include "vec/data_types/data_type_factory.hpp"
38
39
namespace doris {
40
41
const int RowDescriptor::INVALID_IDX = -1;
42
43
SlotDescriptor::SlotDescriptor(const TSlotDescriptor& tdesc)
44
        : _id(tdesc.id),
45
          _type(TypeDescriptor::from_thrift(tdesc.slotType)),
46
          _parent(tdesc.parent),
47
          _col_pos(tdesc.columnPos),
48
          _is_nullable(tdesc.nullIndicatorBit != -1),
49
          _col_name(tdesc.colName),
50
          _col_name_lower_case(to_lower(tdesc.colName)),
51
          _col_unique_id(tdesc.col_unique_id),
52
          _slot_idx(tdesc.slotIdx),
53
          _field_idx(-1),
54
          _is_materialized(tdesc.isMaterialized && tdesc.need_materialize),
55
          _is_key(tdesc.is_key),
56
          _column_paths(tdesc.column_paths),
57
          _is_auto_increment(tdesc.__isset.is_auto_increment ? tdesc.is_auto_increment : false),
58
467
          _col_default_value(tdesc.__isset.col_default_value ? tdesc.col_default_value : "") {}
59
60
SlotDescriptor::SlotDescriptor(const PSlotDescriptor& pdesc)
61
        : _id(pdesc.id()),
62
          _type(TypeDescriptor::from_protobuf(pdesc.slot_type())),
63
          _parent(pdesc.parent()),
64
          _col_pos(pdesc.column_pos()),
65
          _is_nullable(pdesc.null_indicator_bit() != -1),
66
          _col_name(pdesc.col_name()),
67
          _col_name_lower_case(to_lower(pdesc.col_name())),
68
          _col_unique_id(pdesc.col_unique_id()),
69
          _slot_idx(pdesc.slot_idx()),
70
          _field_idx(-1),
71
          _is_materialized(pdesc.is_materialized()),
72
          _is_key(pdesc.is_key()),
73
          _column_paths(pdesc.column_paths().begin(), pdesc.column_paths().end()),
74
84
          _is_auto_increment(pdesc.is_auto_increment()) {}
75
76
#ifdef BE_TEST
77
SlotDescriptor::SlotDescriptor()
78
        : _id(0),
79
          _type(TypeDescriptor {}),
80
          _parent(0),
81
          _col_pos(0),
82
          _is_nullable(false),
83
          _col_unique_id(0),
84
          _slot_idx(0),
85
          _field_idx(-1),
86
          _is_materialized(true),
87
          _is_key(false),
88
30
          _is_auto_increment(false) {}
89
#endif
90
91
96
void SlotDescriptor::to_protobuf(PSlotDescriptor* pslot) const {
92
96
    pslot->set_id(_id);
93
96
    pslot->set_parent(_parent);
94
96
    _type.to_protobuf(pslot->mutable_slot_type());
95
96
    pslot->set_column_pos(_col_pos);
96
96
    pslot->set_byte_offset(0);
97
96
    pslot->set_null_indicator_byte(0);
98
96
    pslot->set_null_indicator_bit(_is_nullable ? 0 : -1);
99
96
    pslot->set_col_name(_col_name);
100
96
    pslot->set_slot_idx(_slot_idx);
101
96
    pslot->set_is_materialized(_is_materialized);
102
96
    pslot->set_col_unique_id(_col_unique_id);
103
96
    pslot->set_is_key(_is_key);
104
96
    pslot->set_is_auto_increment(_is_auto_increment);
105
96
    pslot->set_col_type(_type.type);
106
96
    for (const std::string& path : _column_paths) {
107
0
        pslot->add_column_paths(path);
108
0
    }
109
96
}
110
111
82
vectorized::MutableColumnPtr SlotDescriptor::get_empty_mutable_column() const {
112
82
    auto data_type = get_data_type_ptr();
113
82
    if (data_type) {
114
82
        return data_type->create_column();
115
82
    }
116
0
    return nullptr;
117
82
}
118
119
214
vectorized::DataTypePtr SlotDescriptor::get_data_type_ptr() const {
120
214
    return vectorized::DataTypeFactory::instance().create_data_type(type(), is_nullable());
121
214
}
122
123
0
std::string SlotDescriptor::debug_string() const {
124
0
    std::stringstream out;
125
0
    out << "Slot(id=" << _id << " type=" << _type << " col=" << _col_pos
126
0
        << ", colname=" << _col_name << ", nullable=" << is_nullable() << ")";
127
0
    return out.str();
128
0
}
129
130
TableDescriptor::TableDescriptor(const TTableDescriptor& tdesc)
131
        : _table_type(tdesc.tableType),
132
          _name(tdesc.tableName),
133
          _database(tdesc.dbName),
134
          _table_id(tdesc.id),
135
          _num_cols(tdesc.numCols),
136
17
          _num_clustering_cols(tdesc.numClusteringCols) {}
137
138
0
std::string TableDescriptor::debug_string() const {
139
0
    std::stringstream out;
140
0
    out << "#cols=" << _num_cols << " #clustering_cols=" << _num_clustering_cols;
141
0
    return out.str();
142
0
}
143
144
17
OlapTableDescriptor::OlapTableDescriptor(const TTableDescriptor& tdesc) : TableDescriptor(tdesc) {}
145
146
0
std::string OlapTableDescriptor::debug_string() const {
147
0
    std::stringstream out;
148
0
    out << "OlapTable(" << TableDescriptor::debug_string() << ")";
149
0
    return out.str();
150
0
}
151
152
SchemaTableDescriptor::SchemaTableDescriptor(const TTableDescriptor& tdesc)
153
0
        : TableDescriptor(tdesc), _schema_table_type(tdesc.schemaTable.tableType) {}
154
0
SchemaTableDescriptor::~SchemaTableDescriptor() = default;
155
156
0
std::string SchemaTableDescriptor::debug_string() const {
157
0
    std::stringstream out;
158
0
    out << "SchemaTable(" << TableDescriptor::debug_string() << ")";
159
0
    return out.str();
160
0
}
161
162
BrokerTableDescriptor::BrokerTableDescriptor(const TTableDescriptor& tdesc)
163
0
        : TableDescriptor(tdesc) {}
164
165
0
BrokerTableDescriptor::~BrokerTableDescriptor() = default;
166
167
0
std::string BrokerTableDescriptor::debug_string() const {
168
0
    std::stringstream out;
169
0
    out << "BrokerTable(" << TableDescriptor::debug_string() << ")";
170
0
    return out.str();
171
0
}
172
173
0
HiveTableDescriptor::HiveTableDescriptor(const TTableDescriptor& tdesc) : TableDescriptor(tdesc) {}
174
175
0
HiveTableDescriptor::~HiveTableDescriptor() = default;
176
177
0
std::string HiveTableDescriptor::debug_string() const {
178
0
    std::stringstream out;
179
0
    out << "HiveTable(" << TableDescriptor::debug_string() << ")";
180
0
    return out.str();
181
0
}
182
183
IcebergTableDescriptor::IcebergTableDescriptor(const TTableDescriptor& tdesc)
184
0
        : TableDescriptor(tdesc) {}
185
186
0
IcebergTableDescriptor::~IcebergTableDescriptor() = default;
187
188
0
std::string IcebergTableDescriptor::debug_string() const {
189
0
    std::stringstream out;
190
0
    out << "IcebergTable(" << TableDescriptor::debug_string() << ")";
191
0
    return out.str();
192
0
}
193
194
MaxComputeTableDescriptor::MaxComputeTableDescriptor(const TTableDescriptor& tdesc)
195
        : TableDescriptor(tdesc),
196
          _region(tdesc.mcTable.region),
197
          _project(tdesc.mcTable.project),
198
          _table(tdesc.mcTable.table),
199
          _odps_url(tdesc.mcTable.odps_url),
200
          _tunnel_url(tdesc.mcTable.tunnel_url),
201
          _access_key(tdesc.mcTable.access_key),
202
          _secret_key(tdesc.mcTable.secret_key),
203
0
          _public_access(tdesc.mcTable.public_access) {
204
0
    if (tdesc.mcTable.__isset.endpoint) {
205
0
        _endpoint = tdesc.mcTable.endpoint;
206
0
    } else {
207
0
        _init_status = Status::InvalidArgument(
208
0
                "fail to init MaxComputeTableDescriptor, missing endpoint.");
209
0
    }
210
211
0
    if (tdesc.mcTable.__isset.quota) {
212
0
        _quota = tdesc.mcTable.quota;
213
0
    } else {
214
0
        _init_status =
215
0
                Status::InvalidArgument("fail to init MaxComputeTableDescriptor, missing quota.");
216
0
    }
217
0
}
218
219
0
MaxComputeTableDescriptor::~MaxComputeTableDescriptor() = default;
220
221
0
std::string MaxComputeTableDescriptor::debug_string() const {
222
0
    std::stringstream out;
223
0
    out << "MaxComputeTable(" << TableDescriptor::debug_string() << ")";
224
0
    return out.str();
225
0
}
226
227
TrinoConnectorTableDescriptor::TrinoConnectorTableDescriptor(const TTableDescriptor& tdesc)
228
0
        : TableDescriptor(tdesc) {}
229
230
0
TrinoConnectorTableDescriptor::~TrinoConnectorTableDescriptor() = default;
231
232
0
std::string TrinoConnectorTableDescriptor::debug_string() const {
233
0
    std::stringstream out;
234
0
    out << "TrinoConnectorTable(" << TableDescriptor::debug_string() << ")";
235
0
    return out.str();
236
0
}
237
238
0
EsTableDescriptor::EsTableDescriptor(const TTableDescriptor& tdesc) : TableDescriptor(tdesc) {}
239
240
0
EsTableDescriptor::~EsTableDescriptor() = default;
241
242
0
std::string EsTableDescriptor::debug_string() const {
243
0
    std::stringstream out;
244
0
    out << "EsTable(" << TableDescriptor::debug_string() << ")";
245
0
    return out.str();
246
0
}
247
248
MySQLTableDescriptor::MySQLTableDescriptor(const TTableDescriptor& tdesc)
249
        : TableDescriptor(tdesc),
250
          _mysql_db(tdesc.mysqlTable.db),
251
          _mysql_table(tdesc.mysqlTable.table),
252
          _host(tdesc.mysqlTable.host),
253
          _port(tdesc.mysqlTable.port),
254
          _user(tdesc.mysqlTable.user),
255
          _passwd(tdesc.mysqlTable.passwd),
256
0
          _charset(tdesc.mysqlTable.charset) {}
257
258
0
std::string MySQLTableDescriptor::debug_string() const {
259
0
    std::stringstream out;
260
0
    out << "MySQLTable(" << TableDescriptor::debug_string() << " _db" << _mysql_db
261
0
        << " table=" << _mysql_table << " host=" << _host << " port=" << _port << " user=" << _user
262
0
        << " passwd=" << _passwd << " charset=" << _charset;
263
0
    return out.str();
264
0
}
265
266
ODBCTableDescriptor::ODBCTableDescriptor(const TTableDescriptor& tdesc)
267
        : TableDescriptor(tdesc),
268
          _db(tdesc.odbcTable.db),
269
          _table(tdesc.odbcTable.table),
270
          _host(tdesc.odbcTable.host),
271
          _port(tdesc.odbcTable.port),
272
          _user(tdesc.odbcTable.user),
273
          _passwd(tdesc.odbcTable.passwd),
274
          _driver(tdesc.odbcTable.driver),
275
0
          _type(tdesc.odbcTable.type) {}
276
277
0
std::string ODBCTableDescriptor::debug_string() const {
278
0
    std::stringstream out;
279
0
    out << "ODBCTable(" << TableDescriptor::debug_string() << " _db" << _db << " table=" << _table
280
0
        << " host=" << _host << " port=" << _port << " user=" << _user << " passwd=" << _passwd
281
0
        << " driver=" << _driver << " type" << _type;
282
0
    return out.str();
283
0
}
284
285
JdbcTableDescriptor::JdbcTableDescriptor(const TTableDescriptor& tdesc)
286
        : TableDescriptor(tdesc),
287
          _jdbc_catalog_id(tdesc.jdbcTable.catalog_id),
288
          _jdbc_resource_name(tdesc.jdbcTable.jdbc_resource_name),
289
          _jdbc_driver_url(tdesc.jdbcTable.jdbc_driver_url),
290
          _jdbc_driver_class(tdesc.jdbcTable.jdbc_driver_class),
291
          _jdbc_driver_checksum(tdesc.jdbcTable.jdbc_driver_checksum),
292
          _jdbc_url(tdesc.jdbcTable.jdbc_url),
293
          _jdbc_table_name(tdesc.jdbcTable.jdbc_table_name),
294
          _jdbc_user(tdesc.jdbcTable.jdbc_user),
295
          _jdbc_passwd(tdesc.jdbcTable.jdbc_password),
296
          _connection_pool_min_size(tdesc.jdbcTable.connection_pool_min_size),
297
          _connection_pool_max_size(tdesc.jdbcTable.connection_pool_max_size),
298
          _connection_pool_max_wait_time(tdesc.jdbcTable.connection_pool_max_wait_time),
299
          _connection_pool_max_life_time(tdesc.jdbcTable.connection_pool_max_life_time),
300
0
          _connection_pool_keep_alive(tdesc.jdbcTable.connection_pool_keep_alive) {}
301
302
0
std::string JdbcTableDescriptor::debug_string() const {
303
0
    fmt::memory_buffer buf;
304
0
    fmt::format_to(
305
0
            buf,
306
0
            "JDBCTable({} ,_jdbc_catalog_id = {}, _jdbc_resource_name={} ,_jdbc_driver_url={} "
307
0
            ",_jdbc_driver_class={} ,_jdbc_driver_checksum={} ,_jdbc_url={} "
308
0
            ",_jdbc_table_name={} ,_jdbc_user={} ,_jdbc_passwd={} ,_connection_pool_min_size={} "
309
0
            ",_connection_pool_max_size={} ,_connection_pool_max_wait_time={} "
310
0
            ",_connection_pool_max_life_time={} ,_connection_pool_keep_alive={})",
311
0
            TableDescriptor::debug_string(), _jdbc_catalog_id, _jdbc_resource_name,
312
0
            _jdbc_driver_url, _jdbc_driver_class, _jdbc_driver_checksum, _jdbc_url,
313
0
            _jdbc_table_name, _jdbc_user, _jdbc_passwd, _connection_pool_min_size,
314
0
            _connection_pool_max_size, _connection_pool_max_wait_time,
315
0
            _connection_pool_max_life_time, _connection_pool_keep_alive);
316
0
    return fmt::to_string(buf);
317
0
}
318
319
TupleDescriptor::TupleDescriptor(const TTupleDescriptor& tdesc, bool own_slots)
320
        : _id(tdesc.id),
321
          _num_materialized_slots(0),
322
          _has_varlen_slots(false),
323
154
          _own_slots(own_slots) {}
324
325
TupleDescriptor::TupleDescriptor(const PTupleDescriptor& pdesc, bool own_slots)
326
        : _id(pdesc.id()),
327
          _num_materialized_slots(0),
328
          _has_varlen_slots(false),
329
18
          _own_slots(own_slots) {}
330
331
551
void TupleDescriptor::add_slot(SlotDescriptor* slot) {
332
551
    _slots.push_back(slot);
333
334
551
    if (slot->is_materialized()) {
335
516
        ++_num_materialized_slots;
336
337
516
        if (slot->type().is_string_type() || slot->type().is_collection_type()) {
338
141
            _has_varlen_slots = true;
339
141
        }
340
516
    }
341
551
}
342
343
16
void TupleDescriptor::to_protobuf(PTupleDescriptor* ptuple) const {
344
16
    ptuple->Clear();
345
16
    ptuple->set_id(_id);
346
    // Useless not set
347
16
    ptuple->set_byte_size(0);
348
16
    ptuple->set_table_id(-1);
349
16
    ptuple->set_num_null_bytes(0);
350
16
}
351
352
0
std::string TupleDescriptor::debug_string() const {
353
0
    std::stringstream out;
354
0
    out << "Tuple(id=" << _id;
355
0
    if (_table_desc != nullptr) {
356
        //out << " " << _table_desc->debug_string();
357
0
    }
358
359
0
    out << " slots=[";
360
0
    for (size_t i = 0; i < _slots.size(); ++i) {
361
0
        if (i > 0) {
362
0
            out << ", ";
363
0
        }
364
0
        out << _slots[i]->debug_string();
365
0
    }
366
367
0
    out << "]";
368
0
    out << " has_varlen_slots=" << _has_varlen_slots;
369
0
    out << ")";
370
0
    return out.str();
371
0
}
372
373
RowDescriptor::RowDescriptor(const DescriptorTbl& desc_tbl, const std::vector<TTupleId>& row_tuples,
374
                             const std::vector<bool>& nullable_tuples)
375
354
        : _tuple_idx_nullable_map(nullable_tuples) {
376
354
    DCHECK(nullable_tuples.size() == row_tuples.size())
377
0
            << "nullable_tuples size " << nullable_tuples.size() << " != row_tuples size "
378
0
            << row_tuples.size();
379
354
    DCHECK_GT(row_tuples.size(), 0);
380
354
    _num_materialized_slots = 0;
381
354
    _num_slots = 0;
382
383
432
    for (int row_tuple : row_tuples) {
384
432
        TupleDescriptor* tupleDesc = desc_tbl.get_tuple_descriptor(row_tuple);
385
432
        _num_materialized_slots += tupleDesc->num_materialized_slots();
386
432
        _num_slots += tupleDesc->slots().size();
387
432
        _tuple_desc_map.push_back(tupleDesc);
388
432
        DCHECK(_tuple_desc_map.back() != nullptr);
389
432
    }
390
391
354
    init_tuple_idx_map();
392
354
    init_has_varlen_slots();
393
354
}
394
395
RowDescriptor::RowDescriptor(TupleDescriptor* tuple_desc, bool is_nullable)
396
23
        : _tuple_desc_map(1, tuple_desc), _tuple_idx_nullable_map(1, is_nullable) {
397
23
    init_tuple_idx_map();
398
23
    init_has_varlen_slots();
399
23
    _num_slots = tuple_desc->slots().size();
400
23
}
401
402
0
RowDescriptor::RowDescriptor(const RowDescriptor& lhs_row_desc, const RowDescriptor& rhs_row_desc) {
403
0
    _tuple_desc_map.insert(_tuple_desc_map.end(), lhs_row_desc._tuple_desc_map.begin(),
404
0
                           lhs_row_desc._tuple_desc_map.end());
405
0
    _tuple_desc_map.insert(_tuple_desc_map.end(), rhs_row_desc._tuple_desc_map.begin(),
406
0
                           rhs_row_desc._tuple_desc_map.end());
407
0
    _tuple_idx_nullable_map.insert(_tuple_idx_nullable_map.end(),
408
0
                                   lhs_row_desc._tuple_idx_nullable_map.begin(),
409
0
                                   lhs_row_desc._tuple_idx_nullable_map.end());
410
0
    _tuple_idx_nullable_map.insert(_tuple_idx_nullable_map.end(),
411
0
                                   rhs_row_desc._tuple_idx_nullable_map.begin(),
412
0
                                   rhs_row_desc._tuple_idx_nullable_map.end());
413
0
    init_tuple_idx_map();
414
0
    init_has_varlen_slots();
415
416
0
    _num_slots = lhs_row_desc.num_slots() + rhs_row_desc.num_slots();
417
0
}
418
419
377
void RowDescriptor::init_tuple_idx_map() {
420
    // find max id
421
377
    TupleId max_id = 0;
422
455
    for (auto& i : _tuple_desc_map) {
423
455
        max_id = std::max(i->id(), max_id);
424
455
    }
425
426
377
    _tuple_idx_map.resize(max_id + 1, INVALID_IDX);
427
832
    for (int i = 0; i < _tuple_desc_map.size(); ++i) {
428
455
        _tuple_idx_map[_tuple_desc_map[i]->id()] = i;
429
455
    }
430
377
}
431
432
377
void RowDescriptor::init_has_varlen_slots() {
433
377
    _has_varlen_slots = false;
434
455
    for (auto& i : _tuple_desc_map) {
435
455
        if (i->has_varlen_slots()) {
436
22
            _has_varlen_slots = true;
437
22
            break;
438
22
        }
439
455
    }
440
377
}
441
442
0
int RowDescriptor::get_tuple_idx(TupleId id) const {
443
    // comment CHECK temporarily to make fuzzy test run smoothly
444
    // DCHECK_LT(id, _tuple_idx_map.size()) << "RowDescriptor: " << debug_string();
445
0
    if (_tuple_idx_map.size() <= id) {
446
0
        return RowDescriptor::INVALID_IDX;
447
0
    }
448
0
    return _tuple_idx_map[id];
449
0
}
450
451
0
void RowDescriptor::to_thrift(std::vector<TTupleId>* row_tuple_ids) {
452
0
    row_tuple_ids->clear();
453
454
0
    for (auto& i : _tuple_desc_map) {
455
0
        row_tuple_ids->push_back(i->id());
456
0
    }
457
0
}
458
459
void RowDescriptor::to_protobuf(
460
0
        google::protobuf::RepeatedField<google::protobuf::int32>* row_tuple_ids) const {
461
0
    row_tuple_ids->Clear();
462
0
    for (auto* desc : _tuple_desc_map) {
463
0
        row_tuple_ids->Add(desc->id());
464
0
    }
465
0
}
466
467
0
bool RowDescriptor::is_prefix_of(const RowDescriptor& other_desc) const {
468
0
    if (_tuple_desc_map.size() > other_desc._tuple_desc_map.size()) {
469
0
        return false;
470
0
    }
471
472
0
    for (int i = 0; i < _tuple_desc_map.size(); ++i) {
473
        // pointer comparison okay, descriptors are unique
474
0
        if (_tuple_desc_map[i] != other_desc._tuple_desc_map[i]) {
475
0
            return false;
476
0
        }
477
0
    }
478
479
0
    return true;
480
0
}
481
482
0
bool RowDescriptor::equals(const RowDescriptor& other_desc) const {
483
0
    if (_tuple_desc_map.size() != other_desc._tuple_desc_map.size()) {
484
0
        return false;
485
0
    }
486
487
0
    for (int i = 0; i < _tuple_desc_map.size(); ++i) {
488
        // pointer comparison okay, descriptors are unique
489
0
        if (_tuple_desc_map[i] != other_desc._tuple_desc_map[i]) {
490
0
            return false;
491
0
        }
492
0
    }
493
494
0
    return true;
495
0
}
496
497
0
std::string RowDescriptor::debug_string() const {
498
0
    std::stringstream ss;
499
500
0
    ss << "tuple_desc_map: [";
501
0
    for (int i = 0; i < _tuple_desc_map.size(); ++i) {
502
0
        ss << _tuple_desc_map[i]->debug_string();
503
0
        if (i != _tuple_desc_map.size() - 1) {
504
0
            ss << ", ";
505
0
        }
506
0
    }
507
0
    ss << "] ";
508
509
0
    ss << "tuple_id_map: [";
510
0
    for (int i = 0; i < _tuple_idx_map.size(); ++i) {
511
0
        ss << _tuple_idx_map[i];
512
0
        if (i != _tuple_idx_map.size() - 1) {
513
0
            ss << ", ";
514
0
        }
515
0
    }
516
0
    ss << "] ";
517
518
0
    ss << "tuple_is_nullable: [";
519
0
    for (int i = 0; i < _tuple_idx_nullable_map.size(); ++i) {
520
0
        ss << _tuple_idx_nullable_map[i];
521
0
        if (i != _tuple_idx_nullable_map.size() - 1) {
522
0
            ss << ", ";
523
0
        }
524
0
    }
525
0
    ss << "] ";
526
527
0
    return ss.str();
528
0
}
529
530
55
int RowDescriptor::get_column_id(int slot_id, bool force_materialize_slot) const {
531
55
    int column_id_counter = 0;
532
56
    for (auto* const tuple_desc : _tuple_desc_map) {
533
103
        for (auto* const slot : tuple_desc->slots()) {
534
103
            if (!force_materialize_slot && !slot->is_materialized()) {
535
0
                continue;
536
0
            }
537
103
            if (slot->id() == slot_id) {
538
55
                return column_id_counter;
539
55
            }
540
48
            column_id_counter++;
541
48
        }
542
56
    }
543
0
    return -1;
544
55
}
545
546
Status DescriptorTbl::create(ObjectPool* pool, const TDescriptorTable& thrift_tbl,
547
88
                             DescriptorTbl** tbl) {
548
88
    *tbl = pool->add(new DescriptorTbl());
549
550
    // deserialize table descriptors first, they are being referenced by tuple descriptors
551
88
    for (const auto& tdesc : thrift_tbl.tableDescriptors) {
552
17
        TableDescriptor* desc = nullptr;
553
554
17
        switch (tdesc.tableType) {
555
0
        case TTableType::MYSQL_TABLE:
556
0
            desc = pool->add(new MySQLTableDescriptor(tdesc));
557
0
            break;
558
559
0
        case TTableType::ODBC_TABLE:
560
0
            desc = pool->add(new ODBCTableDescriptor(tdesc));
561
0
            break;
562
563
17
        case TTableType::OLAP_TABLE:
564
17
            desc = pool->add(new OlapTableDescriptor(tdesc));
565
17
            break;
566
567
0
        case TTableType::SCHEMA_TABLE:
568
0
            desc = pool->add(new SchemaTableDescriptor(tdesc));
569
0
            break;
570
0
        case TTableType::BROKER_TABLE:
571
0
            desc = pool->add(new BrokerTableDescriptor(tdesc));
572
0
            break;
573
0
        case TTableType::ES_TABLE:
574
0
            desc = pool->add(new EsTableDescriptor(tdesc));
575
0
            break;
576
0
        case TTableType::HIVE_TABLE:
577
0
            desc = pool->add(new HiveTableDescriptor(tdesc));
578
0
            break;
579
0
        case TTableType::ICEBERG_TABLE:
580
0
            desc = pool->add(new IcebergTableDescriptor(tdesc));
581
0
            break;
582
0
        case TTableType::JDBC_TABLE:
583
0
            desc = pool->add(new JdbcTableDescriptor(tdesc));
584
0
            break;
585
0
        case TTableType::MAX_COMPUTE_TABLE:
586
0
            desc = pool->add(new MaxComputeTableDescriptor(tdesc));
587
0
            break;
588
0
        case TTableType::TRINO_CONNECTOR_TABLE:
589
0
            desc = pool->add(new TrinoConnectorTableDescriptor(tdesc));
590
0
            break;
591
0
        default:
592
0
            DCHECK(false) << "invalid table type: " << tdesc.tableType;
593
17
        }
594
595
17
        (*tbl)->_tbl_desc_map[tdesc.id] = desc;
596
17
    }
597
598
135
    for (const auto& tdesc : thrift_tbl.tupleDescriptors) {
599
135
        TupleDescriptor* desc = pool->add(new TupleDescriptor(tdesc));
600
601
        // fix up table pointer
602
135
        if (tdesc.__isset.tableId) {
603
5
            desc->_table_desc = (*tbl)->get_table_descriptor(tdesc.tableId);
604
5
            DCHECK(desc->_table_desc != nullptr);
605
5
        }
606
607
135
        (*tbl)->_tuple_desc_map[tdesc.id] = desc;
608
135
        (*tbl)->_row_tuples.emplace_back(tdesc.id);
609
135
    }
610
611
325
    for (const auto& tdesc : thrift_tbl.slotDescriptors) {
612
325
        SlotDescriptor* slot_d = pool->add(new SlotDescriptor(tdesc));
613
325
        (*tbl)->_slot_desc_map[tdesc.id] = slot_d;
614
615
        // link to parent
616
325
        auto entry = (*tbl)->_tuple_desc_map.find(tdesc.parent);
617
618
325
        if (entry == (*tbl)->_tuple_desc_map.end()) {
619
0
            return Status::InternalError("unknown tid in slot descriptor msg");
620
0
        }
621
325
        entry->second->add_slot(slot_d);
622
325
    }
623
624
88
    return Status::OK();
625
88
}
626
627
5
TableDescriptor* DescriptorTbl::get_table_descriptor(TableId id) const {
628
    // TODO: is there some boost function to do exactly this?
629
5
    auto i = _tbl_desc_map.find(id);
630
631
5
    if (i == _tbl_desc_map.end()) {
632
0
        return nullptr;
633
5
    } else {
634
5
        return i->second;
635
5
    }
636
5
}
637
638
499
TupleDescriptor* DescriptorTbl::get_tuple_descriptor(TupleId id) const {
639
    // TODO: is there some boost function to do exactly this?
640
499
    auto i = _tuple_desc_map.find(id);
641
642
499
    if (i == _tuple_desc_map.end()) {
643
4
        return nullptr;
644
495
    } else {
645
495
        return i->second;
646
495
    }
647
499
}
648
649
54
SlotDescriptor* DescriptorTbl::get_slot_descriptor(SlotId id) const {
650
    // TODO: is there some boost function to do exactly this?
651
54
    auto i = _slot_desc_map.find(id);
652
653
54
    if (i == _slot_desc_map.end()) {
654
0
        return nullptr;
655
54
    } else {
656
54
        return i->second;
657
54
    }
658
54
}
659
660
0
std::string DescriptorTbl::debug_string() const {
661
0
    std::stringstream out;
662
0
    out << "tuples:\n";
663
664
0
    for (auto i : _tuple_desc_map) {
665
0
        out << i.second->debug_string() << '\n';
666
0
    }
667
668
0
    return out.str();
669
0
}
670
671
} // namespace doris