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