Coverage Report

Created: 2025-12-26 18:42

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
768k
          _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
302
        : _id(0),
133
302
          _type(nullptr),
134
302
          _parent(0),
135
302
          _col_pos(0),
136
302
          _col_unique_id(0),
137
302
          _slot_idx(0),
138
302
          _field_idx(-1),
139
302
          _is_key(false),
140
302
          _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
271k
vectorized::MutableColumnPtr SlotDescriptor::get_empty_mutable_column() const {
194
271k
    if (this->get_virtual_column_expr() != nullptr) {
195
0
        return vectorized::ColumnNothing::create(0);
196
0
    }
197
198
271k
    return type()->create_column();
199
271k
}
200
201
321
bool SlotDescriptor::is_nullable() const {
202
321
    return _type->is_nullable();
203
321
}
204
205
793
PrimitiveType SlotDescriptor::col_type() const {
206
793
    return _type->get_primitive_type();
207
793
}
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
191
        : _table_type(tdesc.tableType),
219
191
          _name(tdesc.tableName),
220
191
          _database(tdesc.dbName),
221
191
          _table_id(tdesc.id),
222
191
          _num_cols(tdesc.numCols),
223
191
          _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
71
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
0
}
314
315
0
MaxComputeTableDescriptor::~MaxComputeTableDescriptor() = default;
316
317
0
std::string MaxComputeTableDescriptor::debug_string() const {
318
0
    std::stringstream out;
319
0
    out << "MaxComputeTable(" << TableDescriptor::debug_string() << ")";
320
0
    return out.str();
321
0
}
322
323
TrinoConnectorTableDescriptor::TrinoConnectorTableDescriptor(const TTableDescriptor& tdesc)
324
0
        : TableDescriptor(tdesc) {}
325
326
0
TrinoConnectorTableDescriptor::~TrinoConnectorTableDescriptor() = default;
327
328
0
std::string TrinoConnectorTableDescriptor::debug_string() const {
329
0
    std::stringstream out;
330
0
    out << "TrinoConnectorTable(" << TableDescriptor::debug_string() << ")";
331
0
    return out.str();
332
0
}
333
334
0
EsTableDescriptor::EsTableDescriptor(const TTableDescriptor& tdesc) : TableDescriptor(tdesc) {}
335
336
0
EsTableDescriptor::~EsTableDescriptor() = default;
337
338
0
std::string EsTableDescriptor::debug_string() const {
339
0
    std::stringstream out;
340
0
    out << "EsTable(" << TableDescriptor::debug_string() << ")";
341
0
    return out.str();
342
0
}
343
344
MySQLTableDescriptor::MySQLTableDescriptor(const TTableDescriptor& tdesc)
345
84
        : TableDescriptor(tdesc),
346
84
          _mysql_db(tdesc.mysqlTable.db),
347
84
          _mysql_table(tdesc.mysqlTable.table),
348
84
          _host(tdesc.mysqlTable.host),
349
84
          _port(tdesc.mysqlTable.port),
350
84
          _user(tdesc.mysqlTable.user),
351
84
          _passwd(tdesc.mysqlTable.passwd),
352
84
          _charset(tdesc.mysqlTable.charset) {}
353
354
0
std::string MySQLTableDescriptor::debug_string() const {
355
0
    std::stringstream out;
356
0
    out << "MySQLTable(" << TableDescriptor::debug_string() << " _db" << _mysql_db
357
0
        << " table=" << _mysql_table << " host=" << _host << " port=" << _port << " user=" << _user
358
0
        << " passwd=" << _passwd << " charset=" << _charset;
359
0
    return out.str();
360
0
}
361
362
JdbcTableDescriptor::JdbcTableDescriptor(const TTableDescriptor& tdesc)
363
0
        : TableDescriptor(tdesc),
364
0
          _jdbc_catalog_id(tdesc.jdbcTable.catalog_id),
365
0
          _jdbc_resource_name(tdesc.jdbcTable.jdbc_resource_name),
366
0
          _jdbc_driver_url(tdesc.jdbcTable.jdbc_driver_url),
367
0
          _jdbc_driver_class(tdesc.jdbcTable.jdbc_driver_class),
368
0
          _jdbc_driver_checksum(tdesc.jdbcTable.jdbc_driver_checksum),
369
0
          _jdbc_url(tdesc.jdbcTable.jdbc_url),
370
0
          _jdbc_table_name(tdesc.jdbcTable.jdbc_table_name),
371
0
          _jdbc_user(tdesc.jdbcTable.jdbc_user),
372
0
          _jdbc_passwd(tdesc.jdbcTable.jdbc_password),
373
0
          _connection_pool_min_size(tdesc.jdbcTable.connection_pool_min_size),
374
0
          _connection_pool_max_size(tdesc.jdbcTable.connection_pool_max_size),
375
0
          _connection_pool_max_wait_time(tdesc.jdbcTable.connection_pool_max_wait_time),
376
0
          _connection_pool_max_life_time(tdesc.jdbcTable.connection_pool_max_life_time),
377
0
          _connection_pool_keep_alive(tdesc.jdbcTable.connection_pool_keep_alive) {}
378
379
0
std::string JdbcTableDescriptor::debug_string() const {
380
0
    fmt::memory_buffer buf;
381
0
    fmt::format_to(
382
0
            buf,
383
0
            "JDBCTable({} ,_jdbc_catalog_id = {}, _jdbc_resource_name={} ,_jdbc_driver_url={} "
384
0
            ",_jdbc_driver_class={} ,_jdbc_driver_checksum={} ,_jdbc_url={} "
385
0
            ",_jdbc_table_name={} ,_jdbc_user={} ,_jdbc_passwd={} ,_connection_pool_min_size={} "
386
0
            ",_connection_pool_max_size={} ,_connection_pool_max_wait_time={} "
387
0
            ",_connection_pool_max_life_time={} ,_connection_pool_keep_alive={})",
388
0
            TableDescriptor::debug_string(), _jdbc_catalog_id, _jdbc_resource_name,
389
0
            _jdbc_driver_url, _jdbc_driver_class, _jdbc_driver_checksum, _jdbc_url,
390
0
            _jdbc_table_name, _jdbc_user, _jdbc_passwd, _connection_pool_min_size,
391
0
            _connection_pool_max_size, _connection_pool_max_wait_time,
392
0
            _connection_pool_max_life_time, _connection_pool_keep_alive);
393
0
    return fmt::to_string(buf);
394
0
}
395
396
RemoteDorisTableDescriptor::RemoteDorisTableDescriptor(const TTableDescriptor& tdesc)
397
0
        : TableDescriptor(tdesc) {}
398
399
0
RemoteDorisTableDescriptor::~RemoteDorisTableDescriptor() = default;
400
401
0
std::string RemoteDorisTableDescriptor::debug_string() const {
402
0
    std::stringstream out;
403
0
    out << "RemoteDorisTable(" << TableDescriptor::debug_string() << ")";
404
0
    return out.str();
405
0
}
406
407
TupleDescriptor::TupleDescriptor(const TTupleDescriptor& tdesc, bool own_slots)
408
292k
        : _id(tdesc.id),
409
292k
          _num_materialized_slots(0),
410
292k
          _has_varlen_slots(false),
411
292k
          _own_slots(own_slots) {}
412
413
TupleDescriptor::TupleDescriptor(const PTupleDescriptor& pdesc, bool own_slots)
414
18
        : _id(pdesc.id()),
415
18
          _num_materialized_slots(0),
416
18
          _has_varlen_slots(false),
417
18
          _own_slots(own_slots) {}
418
419
768k
void TupleDescriptor::add_slot(SlotDescriptor* slot) {
420
768k
    _slots.push_back(slot);
421
768k
    ++_num_materialized_slots;
422
423
768k
    if (is_complex_type(slot->type()->get_primitive_type()) ||
424
768k
        is_var_len_object(slot->type()->get_primitive_type()) ||
425
768k
        is_string_type(slot->type()->get_primitive_type())) {
426
129k
        _has_varlen_slots = true;
427
129k
    }
428
768k
}
429
430
16
void TupleDescriptor::to_protobuf(PTupleDescriptor* ptuple) const {
431
16
    ptuple->Clear();
432
16
    ptuple->set_id(_id);
433
    // Useless not set
434
16
    ptuple->set_byte_size(0);
435
16
    ptuple->set_table_id(-1);
436
16
    ptuple->set_num_null_bytes(0);
437
16
}
438
439
2
std::string TupleDescriptor::debug_string() const {
440
2
    std::stringstream out;
441
2
    out << "Tuple(id=" << _id;
442
2
    if (_table_desc != nullptr) {
443
        //out << " " << _table_desc->debug_string();
444
0
    }
445
446
2
    out << " slots=[";
447
8
    for (size_t i = 0; i < _slots.size(); ++i) {
448
6
        if (i > 0) {
449
4
            out << ", ";
450
4
        }
451
6
        out << _slots[i]->debug_string();
452
6
    }
453
454
2
    out << "]";
455
2
    out << " has_varlen_slots=" << _has_varlen_slots;
456
2
    out << ")";
457
2
    return out.str();
458
2
}
459
460
RowDescriptor::RowDescriptor(const DescriptorTbl& desc_tbl,
461
432k
                             const std::vector<TTupleId>& row_tuples) {
462
432k
    DCHECK_GT(row_tuples.size(), 0);
463
432k
    _num_materialized_slots = 0;
464
432k
    _num_slots = 0;
465
466
504k
    for (int row_tuple : row_tuples) {
467
504k
        TupleDescriptor* tupleDesc = desc_tbl.get_tuple_descriptor(row_tuple);
468
504k
        _num_materialized_slots += tupleDesc->num_materialized_slots();
469
504k
        _num_slots += tupleDesc->slots().size();
470
504k
        _tuple_desc_map.push_back(tupleDesc);
471
504k
        DCHECK(_tuple_desc_map.back() != nullptr);
472
504k
    }
473
474
432k
    init_tuple_idx_map();
475
432k
    init_has_varlen_slots();
476
432k
}
477
478
4.09k
RowDescriptor::RowDescriptor(TupleDescriptor* tuple_desc) : _tuple_desc_map(1, tuple_desc) {
479
4.09k
    init_tuple_idx_map();
480
4.09k
    init_has_varlen_slots();
481
4.09k
    _num_slots = static_cast<int32_t>(tuple_desc->slots().size());
482
4.09k
}
483
484
0
RowDescriptor::RowDescriptor(const RowDescriptor& lhs_row_desc, const RowDescriptor& rhs_row_desc) {
485
0
    _tuple_desc_map.insert(_tuple_desc_map.end(), lhs_row_desc._tuple_desc_map.begin(),
486
0
                           lhs_row_desc._tuple_desc_map.end());
487
0
    _tuple_desc_map.insert(_tuple_desc_map.end(), rhs_row_desc._tuple_desc_map.begin(),
488
0
                           rhs_row_desc._tuple_desc_map.end());
489
0
    init_tuple_idx_map();
490
0
    init_has_varlen_slots();
491
492
0
    _num_slots = lhs_row_desc.num_slots() + rhs_row_desc.num_slots();
493
0
}
494
495
436k
void RowDescriptor::init_tuple_idx_map() {
496
    // find max id
497
436k
    TupleId max_id = 0;
498
508k
    for (auto& i : _tuple_desc_map) {
499
508k
        max_id = std::max(i->id(), max_id);
500
508k
    }
501
502
436k
    _tuple_idx_map.resize(max_id + 1, INVALID_IDX);
503
945k
    for (int i = 0; i < _tuple_desc_map.size(); ++i) {
504
508k
        _tuple_idx_map[_tuple_desc_map[i]->id()] = i;
505
508k
    }
506
436k
}
507
508
436k
void RowDescriptor::init_has_varlen_slots() {
509
436k
    _has_varlen_slots = false;
510
487k
    for (auto& i : _tuple_desc_map) {
511
487k
        if (i->has_varlen_slots()) {
512
133k
            _has_varlen_slots = true;
513
133k
            break;
514
133k
        }
515
487k
    }
516
436k
}
517
518
0
int RowDescriptor::get_tuple_idx(TupleId id) const {
519
    // comment CHECK temporarily to make fuzzy test run smoothly
520
    // DCHECK_LT(id, _tuple_idx_map.size()) << "RowDescriptor: " << debug_string();
521
0
    if (_tuple_idx_map.size() <= id) {
522
0
        return RowDescriptor::INVALID_IDX;
523
0
    }
524
0
    return _tuple_idx_map[id];
525
0
}
526
527
0
void RowDescriptor::to_thrift(std::vector<TTupleId>* row_tuple_ids) {
528
0
    row_tuple_ids->clear();
529
530
0
    for (auto& i : _tuple_desc_map) {
531
0
        row_tuple_ids->push_back(i->id());
532
0
    }
533
0
}
534
535
void RowDescriptor::to_protobuf(
536
0
        google::protobuf::RepeatedField<google::protobuf::int32>* row_tuple_ids) const {
537
0
    row_tuple_ids->Clear();
538
0
    for (auto* desc : _tuple_desc_map) {
539
0
        row_tuple_ids->Add(desc->id());
540
0
    }
541
0
}
542
543
0
bool RowDescriptor::is_prefix_of(const RowDescriptor& other_desc) const {
544
0
    if (_tuple_desc_map.size() > other_desc._tuple_desc_map.size()) {
545
0
        return false;
546
0
    }
547
548
0
    for (int i = 0; i < _tuple_desc_map.size(); ++i) {
549
        // pointer comparison okay, descriptors are unique
550
0
        if (_tuple_desc_map[i] != other_desc._tuple_desc_map[i]) {
551
0
            return false;
552
0
        }
553
0
    }
554
555
0
    return true;
556
0
}
557
558
0
bool RowDescriptor::equals(const RowDescriptor& other_desc) const {
559
0
    if (_tuple_desc_map.size() != other_desc._tuple_desc_map.size()) {
560
0
        return false;
561
0
    }
562
563
0
    for (int i = 0; i < _tuple_desc_map.size(); ++i) {
564
        // pointer comparison okay, descriptors are unique
565
0
        if (_tuple_desc_map[i] != other_desc._tuple_desc_map[i]) {
566
0
            return false;
567
0
        }
568
0
    }
569
570
0
    return true;
571
0
}
572
573
0
std::string RowDescriptor::debug_string() const {
574
0
    std::stringstream ss;
575
576
0
    ss << "tuple_desc_map: [";
577
0
    for (int i = 0; i < _tuple_desc_map.size(); ++i) {
578
0
        ss << _tuple_desc_map[i]->debug_string();
579
0
        if (i != _tuple_desc_map.size() - 1) {
580
0
            ss << ", ";
581
0
        }
582
0
    }
583
0
    ss << "] ";
584
585
0
    ss << "tuple_id_map: [";
586
0
    for (int i = 0; i < _tuple_idx_map.size(); ++i) {
587
0
        ss << _tuple_idx_map[i];
588
0
        if (i != _tuple_idx_map.size() - 1) {
589
0
            ss << ", ";
590
0
        }
591
0
    }
592
0
    ss << "] ";
593
594
0
    return ss.str();
595
0
}
596
597
258k
int RowDescriptor::get_column_id(int slot_id) const {
598
258k
    int column_id_counter = 0;
599
258k
    for (auto* const tuple_desc : _tuple_desc_map) {
600
420k
        for (auto* const slot : tuple_desc->slots()) {
601
420k
            if (slot->id() == slot_id) {
602
258k
                return column_id_counter;
603
258k
            }
604
162k
            column_id_counter++;
605
162k
        }
606
258k
    }
607
0
    return -1;
608
258k
}
609
610
Status DescriptorTbl::create(ObjectPool* pool, const TDescriptorTable& thrift_tbl,
611
76.3k
                             DescriptorTbl** tbl) {
612
76.3k
    *tbl = pool->add(new DescriptorTbl());
613
614
    // deserialize table descriptors first, they are being referenced by tuple descriptors
615
76.3k
    for (const auto& tdesc : thrift_tbl.tableDescriptors) {
616
191
        TableDescriptor* desc = nullptr;
617
618
191
        switch (tdesc.tableType) {
619
84
        case TTableType::MYSQL_TABLE:
620
84
            desc = pool->add(new MySQLTableDescriptor(tdesc));
621
84
            break;
622
623
71
        case TTableType::OLAP_TABLE:
624
71
            desc = pool->add(new OlapTableDescriptor(tdesc));
625
71
            break;
626
627
0
        case TTableType::SCHEMA_TABLE:
628
0
            desc = pool->add(new SchemaTableDescriptor(tdesc));
629
0
            break;
630
0
        case TTableType::BROKER_TABLE:
631
0
            desc = pool->add(new BrokerTableDescriptor(tdesc));
632
0
            break;
633
0
        case TTableType::ES_TABLE:
634
0
            desc = pool->add(new EsTableDescriptor(tdesc));
635
0
            break;
636
36
        case TTableType::HIVE_TABLE:
637
36
            desc = pool->add(new HiveTableDescriptor(tdesc));
638
36
            break;
639
0
        case TTableType::ICEBERG_TABLE:
640
0
            desc = pool->add(new IcebergTableDescriptor(tdesc));
641
0
            break;
642
0
        case TTableType::JDBC_TABLE:
643
0
            desc = pool->add(new JdbcTableDescriptor(tdesc));
644
0
            break;
645
0
        case TTableType::MAX_COMPUTE_TABLE:
646
0
            desc = pool->add(new MaxComputeTableDescriptor(tdesc));
647
0
            break;
648
0
        case TTableType::TRINO_CONNECTOR_TABLE:
649
0
            desc = pool->add(new TrinoConnectorTableDescriptor(tdesc));
650
0
            break;
651
0
        case TTableType::DICTIONARY_TABLE:
652
0
            desc = pool->add(new DictionaryTableDescriptor(tdesc));
653
0
            break;
654
0
        case TTableType::REMOTE_DORIS_TABLE:
655
0
            desc = pool->add(new RemoteDorisTableDescriptor(tdesc));
656
0
            break;
657
0
        default:
658
0
            DCHECK(false) << "invalid table type: " << tdesc.tableType;
659
191
        }
660
661
191
        (*tbl)->_tbl_desc_map[static_cast<int32_t>(tdesc.id)] = desc;
662
191
    }
663
664
292k
    for (const auto& tdesc : thrift_tbl.tupleDescriptors) {
665
292k
        TupleDescriptor* desc = pool->add(new TupleDescriptor(tdesc));
666
667
        // fix up table pointer
668
292k
        if (tdesc.__isset.tableId) {
669
181
            desc->_table_desc = (*tbl)->get_table_descriptor(static_cast<int32_t>(tdesc.tableId));
670
181
            DCHECK(desc->_table_desc != nullptr);
671
181
        }
672
673
292k
        (*tbl)->_tuple_desc_map[tdesc.id] = desc;
674
292k
        (*tbl)->_row_tuples.emplace_back(tdesc.id);
675
292k
    }
676
677
767k
    for (const auto& tdesc : thrift_tbl.slotDescriptors) {
678
767k
        SlotDescriptor* slot_d = pool->add(new SlotDescriptor(tdesc));
679
767k
        (*tbl)->_slot_desc_map[tdesc.id] = slot_d;
680
681
        // link to parent
682
767k
        auto entry = (*tbl)->_tuple_desc_map.find(tdesc.parent);
683
684
767k
        if (entry == (*tbl)->_tuple_desc_map.end()) {
685
0
            return Status::InternalError("unknown tid in slot descriptor msg");
686
0
        }
687
767k
        entry->second->add_slot(slot_d);
688
767k
    }
689
690
76.3k
    return Status::OK();
691
76.3k
}
692
693
181
TableDescriptor* DescriptorTbl::get_table_descriptor(TableId id) const {
694
    // TODO: is there some boost function to do exactly this?
695
181
    auto i = _tbl_desc_map.find(id);
696
697
181
    if (i == _tbl_desc_map.end()) {
698
0
        return nullptr;
699
181
    } else {
700
181
        return i->second;
701
181
    }
702
181
}
703
704
710k
TupleDescriptor* DescriptorTbl::get_tuple_descriptor(TupleId id) const {
705
    // TODO: is there some boost function to do exactly this?
706
710k
    auto i = _tuple_desc_map.find(id);
707
708
710k
    if (i == _tuple_desc_map.end()) {
709
2
        return nullptr;
710
710k
    } else {
711
710k
        return i->second;
712
710k
    }
713
710k
}
714
715
258k
SlotDescriptor* DescriptorTbl::get_slot_descriptor(SlotId id) const {
716
    // TODO: is there some boost function to do exactly this?
717
258k
    auto i = _slot_desc_map.find(id);
718
719
258k
    if (i == _slot_desc_map.end()) {
720
0
        return nullptr;
721
258k
    } else {
722
258k
        return i->second;
723
258k
    }
724
258k
}
725
726
0
std::string DescriptorTbl::debug_string() const {
727
0
    std::stringstream out;
728
0
    out << "tuples:\n";
729
730
0
    for (auto i : _tuple_desc_map) {
731
0
        out << i.second->debug_string() << '\n';
732
0
    }
733
734
0
    return out.str();
735
0
}
736
#include "common/compile_check_end.h"
737
} // namespace doris