Coverage Report

Created: 2025-12-10 19:21

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