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