/root/doris/be/src/runtime/descriptors.h
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.h |
19 | | // and modified by Doris |
20 | | |
21 | | #pragma once |
22 | | |
23 | | #include <gen_cpp/Descriptors_types.h> |
24 | | #include <gen_cpp/Types_types.h> |
25 | | #include <glog/logging.h> |
26 | | #include <google/protobuf/stubs/port.h> |
27 | | #include <stdint.h> |
28 | | |
29 | | #include <ostream> |
30 | | #include <string> |
31 | | #include <unordered_map> |
32 | | #include <utility> |
33 | | #include <vector> |
34 | | |
35 | | #include "common/be_mock_util.h" |
36 | | #include "common/compiler_util.h" // IWYU pragma: keep |
37 | | #include "common/global_types.h" |
38 | | #include "common/object_pool.h" |
39 | | #include "common/status.h" |
40 | | #include "olap/utils.h" |
41 | | #include "runtime/define_primitive_type.h" |
42 | | #include "runtime/types.h" |
43 | | #include "vec/data_types/data_type.h" |
44 | | namespace google::protobuf { |
45 | | template <typename Element> |
46 | | class RepeatedField; |
47 | | } // namespace google::protobuf |
48 | | |
49 | | namespace doris { |
50 | | |
51 | | class ObjectPool; |
52 | | class PTupleDescriptor; |
53 | | class PSlotDescriptor; |
54 | | |
55 | | class SlotDescriptor { |
56 | | public: |
57 | | MOCK_DEFINE(virtual ~SlotDescriptor() = default;) |
58 | 707k | SlotId id() const { return _id; } |
59 | 2.96M | const vectorized::DataTypePtr type() const { return _type; } |
60 | 0 | TupleId parent() const { return _parent; } |
61 | | // Returns the column index of this slot, including partition keys. |
62 | | // (e.g., col_pos - num_partition_keys = the table column this slot corresponds to) |
63 | 0 | int col_pos() const { return _col_pos; } |
64 | | // Returns the field index in the generated llvm struct for this slot's tuple |
65 | 0 | int field_idx() const { return _field_idx; } |
66 | 1.63M | bool is_materialized() const { return _is_materialized; } |
67 | 244 | bool is_nullable() const { return _type->is_nullable(); } |
68 | | |
69 | 675k | const std::string& col_name() const { return _col_name; } |
70 | 0 | const std::string& col_name_lower_case() const { return _col_name_lower_case; } |
71 | | |
72 | | void to_protobuf(PSlotDescriptor* pslot) const; |
73 | | |
74 | | std::string debug_string() const; |
75 | | |
76 | | vectorized::MutableColumnPtr get_empty_mutable_column() const; |
77 | | |
78 | 23 | int32_t col_unique_id() const { return _col_unique_id; } |
79 | | |
80 | 0 | bool is_key() const { return _is_key; } |
81 | 0 | const std::vector<std::string>& column_paths() const { return _column_paths; }; |
82 | | |
83 | 0 | bool is_auto_increment() const { return _is_auto_increment; } |
84 | | |
85 | 0 | bool is_skip_bitmap_col() const { return _col_name == SKIP_BITMAP_COL; } |
86 | 0 | bool is_sequence_col() const { return _col_name == SEQUENCE_COL; } |
87 | | |
88 | 4 | const std::string& col_default_value() const { return _col_default_value; } |
89 | 180 | PrimitiveType col_type() const { return _type->get_primitive_type(); } |
90 | | |
91 | | private: |
92 | | friend class DescriptorTbl; |
93 | | friend class TupleDescriptor; |
94 | | friend class SchemaScanner; |
95 | | friend class OlapTableSchemaParam; |
96 | | friend class PInternalServiceImpl; |
97 | | friend class RowIdStorageReader; |
98 | | friend class Tablet; |
99 | | friend class TabletSchema; |
100 | | |
101 | | MOCK_REMOVE(const) SlotId _id; |
102 | | MOCK_REMOVE(const) vectorized::DataTypePtr _type; |
103 | | const TupleId _parent; |
104 | | const int _col_pos; |
105 | | const std::string _col_name; |
106 | | const std::string _col_name_lower_case; |
107 | | |
108 | | const int32_t _col_unique_id; |
109 | | |
110 | | // the idx of the slot in the tuple descriptor (0-based). |
111 | | // this is provided by the FE |
112 | | const int _slot_idx; |
113 | | |
114 | | // the idx of the slot in the llvm codegen'd tuple struct |
115 | | // this is set by TupleDescriptor during codegen and takes into account |
116 | | // leading null bytes. |
117 | | int _field_idx; |
118 | | |
119 | | const bool _is_materialized; |
120 | | |
121 | | const bool _is_key; |
122 | | const std::vector<std::string> _column_paths; |
123 | | |
124 | | const bool _is_auto_increment; |
125 | | const std::string _col_default_value; |
126 | | |
127 | | SlotDescriptor(const TSlotDescriptor& tdesc); |
128 | | SlotDescriptor(const PSlotDescriptor& pdesc); |
129 | | MOCK_DEFINE(SlotDescriptor();) |
130 | | }; |
131 | | |
132 | | // Base class for table descriptors. |
133 | | class TableDescriptor { |
134 | | public: |
135 | | TableDescriptor(const TTableDescriptor& tdesc); |
136 | 20 | virtual ~TableDescriptor() = default; |
137 | 0 | int num_cols() const { return _num_cols; } |
138 | 0 | int num_clustering_cols() const { return _num_clustering_cols; } |
139 | | virtual std::string debug_string() const; |
140 | | |
141 | | // The first _num_clustering_cols columns by position are clustering |
142 | | // columns. |
143 | 0 | bool is_clustering_col(const SlotDescriptor* slot_desc) const { |
144 | 0 | return slot_desc->col_pos() < _num_clustering_cols; |
145 | 0 | } |
146 | | |
147 | 0 | ::doris::TTableType::type table_type() const { return _table_type; } |
148 | 0 | const std::string& name() const { return _name; } |
149 | 0 | const std::string& database() const { return _database; } |
150 | 0 | int64_t table_id() const { return _table_id; } |
151 | | |
152 | | private: |
153 | | ::doris::TTableType::type _table_type; |
154 | | std::string _name; |
155 | | std::string _database; |
156 | | int64_t _table_id; |
157 | | int _num_cols; |
158 | | int _num_clustering_cols; |
159 | | }; |
160 | | |
161 | | class OlapTableDescriptor : public TableDescriptor { |
162 | | public: |
163 | | OlapTableDescriptor(const TTableDescriptor& tdesc); |
164 | | std::string debug_string() const override; |
165 | | }; |
166 | | |
167 | | class SchemaTableDescriptor : public TableDescriptor { |
168 | | public: |
169 | | SchemaTableDescriptor(const TTableDescriptor& tdesc); |
170 | | ~SchemaTableDescriptor() override; |
171 | | std::string debug_string() const override; |
172 | 0 | TSchemaTableType::type schema_table_type() const { return _schema_table_type; } |
173 | | |
174 | | private: |
175 | | TSchemaTableType::type _schema_table_type; |
176 | | }; |
177 | | |
178 | | class BrokerTableDescriptor : public TableDescriptor { |
179 | | public: |
180 | | BrokerTableDescriptor(const TTableDescriptor& tdesc); |
181 | | ~BrokerTableDescriptor() override; |
182 | | std::string debug_string() const override; |
183 | | |
184 | | private: |
185 | | }; |
186 | | |
187 | | class HiveTableDescriptor : public TableDescriptor { |
188 | | public: |
189 | | HiveTableDescriptor(const TTableDescriptor& tdesc); |
190 | | ~HiveTableDescriptor() override; |
191 | | std::string debug_string() const override; |
192 | | |
193 | | private: |
194 | | }; |
195 | | |
196 | | class IcebergTableDescriptor : public TableDescriptor { |
197 | | public: |
198 | | IcebergTableDescriptor(const TTableDescriptor& tdesc); |
199 | | ~IcebergTableDescriptor() override; |
200 | | std::string debug_string() const override; |
201 | | |
202 | | private: |
203 | | }; |
204 | | |
205 | | class MaxComputeTableDescriptor : public TableDescriptor { |
206 | | public: |
207 | | MaxComputeTableDescriptor(const TTableDescriptor& tdesc); |
208 | | ~MaxComputeTableDescriptor() override; |
209 | | std::string debug_string() const override; |
210 | 0 | std::string region() const { return _region; } |
211 | 0 | std::string project() const { return _project; } |
212 | 0 | std::string table() const { return _table; } |
213 | 0 | std::string odps_url() const { return _odps_url; } |
214 | 0 | std::string tunnel_url() const { return _tunnel_url; } |
215 | 0 | std::string access_key() const { return _access_key; } |
216 | 0 | std::string secret_key() const { return _secret_key; } |
217 | 0 | std::string public_access() const { return _public_access; } |
218 | 0 | std::string endpoint() const { return _endpoint; } |
219 | 0 | std::string quota() const { return _quota; } |
220 | 0 | Status init_status() const { return _init_status; } |
221 | | |
222 | | private: |
223 | | std::string _region; //deprecated |
224 | | std::string _project; |
225 | | std::string _table; |
226 | | std::string _odps_url; //deprecated |
227 | | std::string _tunnel_url; //deprecated |
228 | | std::string _access_key; |
229 | | std::string _secret_key; |
230 | | std::string _public_access; //deprecated |
231 | | std::string _endpoint; |
232 | | std::string _quota; |
233 | | Status _init_status = Status::OK(); |
234 | | }; |
235 | | |
236 | | class TrinoConnectorTableDescriptor : public TableDescriptor { |
237 | | public: |
238 | | TrinoConnectorTableDescriptor(const TTableDescriptor& tdesc); |
239 | | ~TrinoConnectorTableDescriptor() override; |
240 | | std::string debug_string() const override; |
241 | | |
242 | | private: |
243 | | }; |
244 | | |
245 | | class EsTableDescriptor : public TableDescriptor { |
246 | | public: |
247 | | EsTableDescriptor(const TTableDescriptor& tdesc); |
248 | | ~EsTableDescriptor() override; |
249 | | std::string debug_string() const override; |
250 | | |
251 | | private: |
252 | | }; |
253 | | |
254 | | class MySQLTableDescriptor : public TableDescriptor { |
255 | | public: |
256 | | MySQLTableDescriptor(const TTableDescriptor& tdesc); |
257 | | std::string debug_string() const override; |
258 | 0 | std::string mysql_db() const { return _mysql_db; } |
259 | 0 | std::string mysql_table() const { return _mysql_table; } |
260 | 0 | std::string host() const { return _host; } |
261 | 0 | std::string port() const { return _port; } |
262 | 0 | std::string user() const { return _user; } |
263 | 0 | std::string passwd() const { return _passwd; } |
264 | 0 | std::string charset() const { return _charset; } |
265 | | |
266 | | private: |
267 | | std::string _mysql_db; |
268 | | std::string _mysql_table; |
269 | | std::string _host; |
270 | | std::string _port; |
271 | | std::string _user; |
272 | | std::string _passwd; |
273 | | std::string _charset; |
274 | | }; |
275 | | |
276 | | class ODBCTableDescriptor : public TableDescriptor { |
277 | | public: |
278 | | ODBCTableDescriptor(const TTableDescriptor& tdesc); |
279 | | std::string debug_string() const override; |
280 | 0 | std::string db() const { return _db; } |
281 | 0 | std::string table() const { return _table; } |
282 | 0 | std::string host() const { return _host; } |
283 | 0 | std::string port() const { return _port; } |
284 | 0 | std::string user() const { return _user; } |
285 | 0 | std::string passwd() const { return _passwd; } |
286 | 0 | std::string driver() const { return _driver; } |
287 | 0 | TOdbcTableType::type type() const { return _type; } |
288 | | |
289 | | private: |
290 | | std::string _db; |
291 | | std::string _table; |
292 | | std::string _host; |
293 | | std::string _port; |
294 | | std::string _user; |
295 | | std::string _passwd; |
296 | | std::string _driver; |
297 | | TOdbcTableType::type _type; |
298 | | }; |
299 | | |
300 | | class JdbcTableDescriptor : public TableDescriptor { |
301 | | public: |
302 | | JdbcTableDescriptor(const TTableDescriptor& tdesc); |
303 | | std::string debug_string() const override; |
304 | 0 | int64_t jdbc_catalog_id() const { return _jdbc_catalog_id; } |
305 | 0 | const std::string& jdbc_resource_name() const { return _jdbc_resource_name; } |
306 | 0 | const std::string& jdbc_driver_url() const { return _jdbc_driver_url; } |
307 | 0 | const std::string& jdbc_driver_class() const { return _jdbc_driver_class; } |
308 | 0 | const std::string& jdbc_driver_checksum() const { return _jdbc_driver_checksum; } |
309 | 0 | const std::string& jdbc_url() const { return _jdbc_url; } |
310 | 0 | const std::string& jdbc_table_name() const { return _jdbc_table_name; } |
311 | 0 | const std::string& jdbc_user() const { return _jdbc_user; } |
312 | 0 | const std::string& jdbc_passwd() const { return _jdbc_passwd; } |
313 | 0 | int32_t connection_pool_min_size() const { return _connection_pool_min_size; } |
314 | 0 | int32_t connection_pool_max_size() const { return _connection_pool_max_size; } |
315 | 0 | int32_t connection_pool_max_wait_time() const { return _connection_pool_max_wait_time; } |
316 | 0 | int32_t connection_pool_max_life_time() const { return _connection_pool_max_life_time; } |
317 | 0 | bool connection_pool_keep_alive() const { return _connection_pool_keep_alive; } |
318 | | |
319 | | private: |
320 | | int64_t _jdbc_catalog_id; |
321 | | std::string _jdbc_resource_name; |
322 | | std::string _jdbc_driver_url; |
323 | | std::string _jdbc_driver_class; |
324 | | std::string _jdbc_driver_checksum; |
325 | | std::string _jdbc_url; |
326 | | std::string _jdbc_table_name; |
327 | | std::string _jdbc_user; |
328 | | std::string _jdbc_passwd; |
329 | | int32_t _connection_pool_min_size; |
330 | | int32_t _connection_pool_max_size; |
331 | | int32_t _connection_pool_max_wait_time; |
332 | | int32_t _connection_pool_max_life_time; |
333 | | bool _connection_pool_keep_alive; |
334 | | }; |
335 | | |
336 | | class TupleDescriptor { |
337 | | public: |
338 | | TupleDescriptor(TupleDescriptor&&) = delete; |
339 | | void operator=(const TupleDescriptor&) = delete; |
340 | | |
341 | 312k | MOCK_DEFINE(virtual) ~TupleDescriptor() { |
342 | 312k | if (_own_slots) { |
343 | 13 | for (SlotDescriptor* slot : _slots) { |
344 | 13 | delete slot; |
345 | 13 | } |
346 | 4 | } |
347 | 312k | } |
348 | | |
349 | | MOCK_DEFINE(TupleDescriptor() : _id {0} {};) |
350 | | |
351 | 546k | int num_materialized_slots() const { return _num_materialized_slots; } |
352 | 1.21M | MOCK_FUNCTION const std::vector<SlotDescriptor*>& slots() const { return _slots; } |
353 | | |
354 | 524k | bool has_varlen_slots() const { return _has_varlen_slots; } |
355 | 0 | const TableDescriptor* table_desc() const { return _table_desc; } |
356 | | |
357 | 1.09M | TupleId id() const { return _id; } |
358 | | |
359 | | std::string debug_string() const; |
360 | | |
361 | | void to_protobuf(PTupleDescriptor* ptuple) const; |
362 | | |
363 | | private: |
364 | | friend class DescriptorTbl; |
365 | | friend class SchemaScanner; |
366 | | friend class OlapTableSchemaParam; |
367 | | friend class PInternalServiceImpl; |
368 | | friend class RowIdStorageReader; |
369 | | friend class TabletSchema; |
370 | | |
371 | | const TupleId _id; |
372 | | TableDescriptor* _table_desc = nullptr; |
373 | | int _num_materialized_slots; |
374 | | std::vector<SlotDescriptor*> _slots; // contains all slots |
375 | | |
376 | | // Provide quick way to check if there are variable length slots. |
377 | | // True if _string_slots or _collection_slots have entries. |
378 | | bool _has_varlen_slots; |
379 | | bool _own_slots = false; |
380 | | |
381 | | TupleDescriptor(const TTupleDescriptor& tdesc, bool own_slot = false); |
382 | | TupleDescriptor(const PTupleDescriptor& tdesc, bool own_slot = false); |
383 | | |
384 | | void add_slot(SlotDescriptor* slot); |
385 | | }; |
386 | | |
387 | | class DescriptorTbl { |
388 | | public: |
389 | | // Creates a descriptor tbl within 'pool' from thrift_tbl and returns it via 'tbl'. |
390 | | // Returns OK on success, otherwise error (in which case 'tbl' will be unset). |
391 | | static Status create(ObjectPool* pool, const TDescriptorTable& thrift_tbl, DescriptorTbl** tbl); |
392 | | |
393 | | TableDescriptor* get_table_descriptor(TableId id) const; |
394 | | TupleDescriptor* get_tuple_descriptor(TupleId id) const; |
395 | | SlotDescriptor* get_slot_descriptor(SlotId id) const; |
396 | 0 | const std::vector<TTupleId>& get_row_tuples() const { return _row_tuples; } |
397 | | |
398 | | // return all registered tuple descriptors |
399 | 45 | std::vector<TupleDescriptor*> get_tuple_descs() const { |
400 | 45 | std::vector<TupleDescriptor*> descs; |
401 | | |
402 | 102 | for (auto it : _tuple_desc_map) { |
403 | 102 | descs.push_back(it.second); |
404 | 102 | } |
405 | | |
406 | 45 | return descs; |
407 | 45 | } |
408 | | |
409 | | std::string debug_string() const; |
410 | | |
411 | | private: |
412 | | using TableDescriptorMap = std::unordered_map<TableId, TableDescriptor*>; |
413 | | using TupleDescriptorMap = std::unordered_map<TupleId, TupleDescriptor*>; |
414 | | using SlotDescriptorMap = std::unordered_map<SlotId, SlotDescriptor*>; |
415 | | |
416 | | TableDescriptorMap _tbl_desc_map; |
417 | | TupleDescriptorMap _tuple_desc_map; |
418 | | SlotDescriptorMap _slot_desc_map; |
419 | | std::vector<TTupleId> _row_tuples; |
420 | | |
421 | 78.1k | DescriptorTbl() = default; |
422 | | }; |
423 | | |
424 | | #define RETURN_IF_INVALID_TUPLE_IDX(tuple_id, tuple_idx) \ |
425 | 0 | do { \ |
426 | 0 | if (UNLIKELY(RowDescriptor::INVALID_IDX == tuple_idx)) { \ |
427 | 0 | return Status::InternalError("failed to get tuple idx with tuple id: {}", tuple_id); \ |
428 | 0 | } \ |
429 | 0 | } while (false) |
430 | | |
431 | | // Records positions of tuples within row produced by ExecNode. |
432 | | // TODO: this needs to differentiate between tuples contained in row |
433 | | // and tuples produced by ExecNode (parallel to PlanNode.rowTupleIds and |
434 | | // PlanNode.tupleIds); right now, we conflate the two (and distinguish based on |
435 | | // context; for instance, HdfsScanNode uses these tids to create row batches, ie, the |
436 | | // first case, whereas TopNNode uses these tids to copy output rows, ie, the second |
437 | | // case) |
438 | | class RowDescriptor { |
439 | | public: |
440 | | RowDescriptor(const DescriptorTbl& desc_tbl, const std::vector<TTupleId>& row_tuples, |
441 | | const std::vector<bool>& nullable_tuples); |
442 | | |
443 | | // standard copy c'tor, made explicit here |
444 | | RowDescriptor(const RowDescriptor& desc) |
445 | | : _tuple_desc_map(desc._tuple_desc_map), |
446 | | _tuple_idx_nullable_map(desc._tuple_idx_nullable_map), |
447 | | _tuple_idx_map(desc._tuple_idx_map), |
448 | 0 | _has_varlen_slots(desc._has_varlen_slots) { |
449 | 0 | auto it = desc._tuple_desc_map.begin(); |
450 | 0 | for (; it != desc._tuple_desc_map.end(); ++it) { |
451 | 0 | _num_materialized_slots += (*it)->num_materialized_slots(); |
452 | 0 | _num_slots += (*it)->slots().size(); |
453 | 0 | } |
454 | 0 | } |
455 | | |
456 | | RowDescriptor(TupleDescriptor* tuple_desc, bool is_nullable); |
457 | | |
458 | | RowDescriptor(const RowDescriptor& lhs_row_desc, const RowDescriptor& rhs_row_desc); |
459 | | |
460 | | // dummy descriptor, needed for the JNI EvalPredicate() function |
461 | 156k | RowDescriptor() = default; |
462 | | |
463 | | MOCK_DEFINE(virtual ~RowDescriptor() = default;) |
464 | | |
465 | 176k | int num_materialized_slots() const { return _num_materialized_slots; } |
466 | | |
467 | 2 | int num_slots() const { return _num_slots; } |
468 | | |
469 | | static const int INVALID_IDX; |
470 | | |
471 | | // Returns INVALID_IDX if id not part of this row. |
472 | | int get_tuple_idx(TupleId id) const; |
473 | | |
474 | | // Return true if any Tuple has variable length slots. |
475 | 0 | bool has_varlen_slots() const { return _has_varlen_slots; } |
476 | | |
477 | | // Return descriptors for all tuples in this row, in order of appearance. |
478 | 390k | MOCK_FUNCTION const std::vector<TupleDescriptor*>& tuple_descriptors() const { |
479 | 390k | return _tuple_desc_map; |
480 | 390k | } |
481 | | |
482 | | // Populate row_tuple_ids with our ids. |
483 | | void to_thrift(std::vector<TTupleId>* row_tuple_ids); |
484 | | void to_protobuf(google::protobuf::RepeatedField<google::protobuf::int32>* row_tuple_ids) const; |
485 | | |
486 | | // Return true if the tuple ids of this descriptor are a prefix |
487 | | // of the tuple ids of other_desc. |
488 | | bool is_prefix_of(const RowDescriptor& other_desc) const; |
489 | | |
490 | | // Return true if the tuple ids of this descriptor match tuple ids of other desc. |
491 | | bool equals(const RowDescriptor& other_desc) const; |
492 | | |
493 | | std::string debug_string() const; |
494 | | |
495 | | int get_column_id(int slot_id, bool force_materialize_slot = false) const; |
496 | | |
497 | | private: |
498 | | // Initializes tupleIdxMap during c'tor using the _tuple_desc_map. |
499 | | void init_tuple_idx_map(); |
500 | | |
501 | | // Initializes _has_varlen_slots during c'tor using the _tuple_desc_map. |
502 | | void init_has_varlen_slots(); |
503 | | |
504 | | // map from position of tuple w/in row to its descriptor |
505 | | std::vector<TupleDescriptor*> _tuple_desc_map; |
506 | | |
507 | | // _tuple_idx_nullable_map[i] is true if tuple i can be null |
508 | | std::vector<bool> _tuple_idx_nullable_map; |
509 | | |
510 | | // map from TupleId to position of tuple w/in row |
511 | | std::vector<int> _tuple_idx_map; |
512 | | |
513 | | // Provide quick way to check if there are variable length slots. |
514 | | bool _has_varlen_slots = false; |
515 | | |
516 | | int _num_materialized_slots = 0; |
517 | | int _num_slots = 0; |
518 | | }; |
519 | | } // namespace doris |