Coverage Report

Created: 2026-03-02 21:54

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