/root/doris/be/src/olap/push_handler.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 | | |
18 | | #include "olap/push_handler.h" |
19 | | |
20 | | #include <fmt/core.h> |
21 | | #include <gen_cpp/AgentService_types.h> |
22 | | #include <gen_cpp/Descriptors_types.h> |
23 | | #include <gen_cpp/MasterService_types.h> |
24 | | #include <gen_cpp/PaloInternalService_types.h> |
25 | | #include <gen_cpp/PlanNodes_types.h> |
26 | | #include <gen_cpp/Types_types.h> |
27 | | #include <gen_cpp/olap_file.pb.h> |
28 | | #include <gen_cpp/types.pb.h> |
29 | | #include <glog/logging.h> |
30 | | |
31 | | #include <algorithm> |
32 | | #include <iostream> |
33 | | #include <mutex> |
34 | | #include <new> |
35 | | #include <queue> |
36 | | #include <shared_mutex> |
37 | | #include <type_traits> |
38 | | |
39 | | #include "common/compiler_util.h" // IWYU pragma: keep |
40 | | #include "common/config.h" |
41 | | #include "common/logging.h" |
42 | | #include "common/status.h" |
43 | | #include "io/hdfs_builder.h" |
44 | | #include "olap/cumulative_compaction_time_series_policy.h" |
45 | | #include "olap/delete_handler.h" |
46 | | #include "olap/olap_define.h" |
47 | | #include "olap/rowset/pending_rowset_helper.h" |
48 | | #include "olap/rowset/rowset_writer.h" |
49 | | #include "olap/rowset/rowset_writer_context.h" |
50 | | #include "olap/storage_engine.h" |
51 | | #include "olap/tablet.h" |
52 | | #include "olap/tablet_manager.h" |
53 | | #include "olap/tablet_schema.h" |
54 | | #include "olap/txn_manager.h" |
55 | | #include "runtime/descriptors.h" |
56 | | #include "runtime/exec_env.h" |
57 | | #include "util/time.h" |
58 | | #include "vec/core/block.h" |
59 | | #include "vec/core/column_with_type_and_name.h" |
60 | | #include "vec/data_types/data_type_bitmap.h" |
61 | | #include "vec/data_types/data_type_factory.hpp" |
62 | | #include "vec/data_types/data_type_nullable.h" |
63 | | #include "vec/exec/format/parquet/vparquet_reader.h" |
64 | | #include "vec/exprs/vexpr_context.h" |
65 | | #include "vec/functions/function_helpers.h" |
66 | | #include "vec/functions/simple_function_factory.h" |
67 | | |
68 | | namespace doris { |
69 | | #include "common/compile_check_begin.h" |
70 | | using namespace ErrorCode; |
71 | | |
72 | | // Process push command, the main logical is as follows: |
73 | | // a. related tablets not exist: |
74 | | // current table isn't in schemachange state, only push for current |
75 | | // tablet |
76 | | // b. related tablets exist |
77 | | // I. current tablet is old table (cur.creation_time < |
78 | | // related.creation_time): |
79 | | // push for current table and than convert data for related tables |
80 | | // II. current table is new table: |
81 | | // this usually means schema change is over, |
82 | | // clear schema change info in both current tablet and related |
83 | | // tablets, finally we will only push for current tablets. this is |
84 | | // very useful in rollup action. |
85 | | Status PushHandler::process_streaming_ingestion(TabletSharedPtr tablet, const TPushReq& request, |
86 | | PushType push_type, |
87 | 0 | std::vector<TTabletInfo>* tablet_info_vec) { |
88 | 0 | LOG(INFO) << "begin to realtime push. tablet=" << tablet->tablet_id() |
89 | 0 | << ", transaction_id=" << request.transaction_id; |
90 | |
|
91 | 0 | Status res = Status::OK(); |
92 | 0 | _request = request; |
93 | |
|
94 | 0 | RETURN_IF_ERROR(DescriptorTbl::create(&_pool, _request.desc_tbl, &_desc_tbl)); |
95 | | |
96 | 0 | res = _do_streaming_ingestion(tablet, request, push_type, tablet_info_vec); |
97 | |
|
98 | 0 | if (res.ok()) { |
99 | 0 | if (tablet_info_vec != nullptr) { |
100 | 0 | TTabletInfo tablet_info; |
101 | 0 | tablet_info.tablet_id = tablet->tablet_id(); |
102 | 0 | tablet_info.schema_hash = tablet->schema_hash(); |
103 | 0 | RETURN_IF_ERROR(_engine.tablet_manager()->report_tablet_info(&tablet_info)); |
104 | 0 | tablet_info_vec->push_back(tablet_info); |
105 | 0 | } |
106 | 0 | LOG(INFO) << "process realtime push successfully. " |
107 | 0 | << "tablet=" << tablet->tablet_id() << ", partition_id=" << request.partition_id |
108 | 0 | << ", transaction_id=" << request.transaction_id; |
109 | 0 | } |
110 | | |
111 | 0 | return res; |
112 | 0 | } |
113 | | |
114 | | Status PushHandler::_do_streaming_ingestion(TabletSharedPtr tablet, const TPushReq& request, |
115 | | PushType push_type, |
116 | 0 | std::vector<TTabletInfo>* tablet_info_vec) { |
117 | | // add transaction in engine, then check sc status |
118 | | // lock, prevent sc handler checking transaction concurrently |
119 | 0 | if (tablet == nullptr) { |
120 | 0 | return Status::Error<TABLE_NOT_FOUND>( |
121 | 0 | "PushHandler::_do_streaming_ingestion input tablet is nullptr"); |
122 | 0 | } |
123 | | |
124 | 0 | PUniqueId load_id; |
125 | 0 | load_id.set_hi(0); |
126 | 0 | load_id.set_lo(0); |
127 | |
|
128 | 0 | RETURN_IF_ERROR( |
129 | 0 | tablet->prepare_txn(request.partition_id, request.transaction_id, load_id, false)); |
130 | | |
131 | | // not call validate request here, because realtime load does not |
132 | | // contain version info |
133 | | |
134 | 0 | Status res; |
135 | | // check delete condition if push for delete |
136 | 0 | std::queue<DeletePredicatePB> del_preds; |
137 | 0 | if (push_type == PushType::PUSH_FOR_DELETE) { |
138 | 0 | DeletePredicatePB del_pred; |
139 | 0 | TabletSchema tablet_schema; |
140 | 0 | tablet_schema.copy_from(*tablet->tablet_schema()); |
141 | 0 | if (!request.columns_desc.empty() && request.columns_desc[0].col_unique_id >= 0) { |
142 | 0 | tablet_schema.clear_columns(); |
143 | 0 | for (const auto& column_desc : request.columns_desc) { |
144 | 0 | tablet_schema.append_column(TabletColumn(column_desc)); |
145 | 0 | } |
146 | 0 | } |
147 | 0 | res = DeleteHandler::generate_delete_predicate(tablet_schema, request.delete_conditions, |
148 | 0 | &del_pred); |
149 | 0 | del_preds.push(del_pred); |
150 | 0 | if (!res.ok()) { |
151 | 0 | LOG(WARNING) << "fail to generate delete condition. res=" << res |
152 | 0 | << ", tablet=" << tablet->tablet_id(); |
153 | 0 | return res; |
154 | 0 | } |
155 | 0 | } |
156 | | |
157 | 0 | int32_t max_version_config = tablet->max_version_config(); |
158 | | // check if version number exceed limit |
159 | 0 | if (tablet->exceed_version_limit(max_version_config)) { |
160 | 0 | return Status::Status::Error<TOO_MANY_VERSION>( |
161 | 0 | "failed to push data. version count: {}, exceed limit: {}, tablet: {}. Please " |
162 | 0 | "reduce the frequency of loading data or adjust the max_tablet_version_num or " |
163 | 0 | "time_series_max_tablet_version_num in " |
164 | 0 | "be.conf to a larger value.", |
165 | 0 | tablet->version_count(), max_version_config, tablet->tablet_id()); |
166 | 0 | } |
167 | | |
168 | 0 | auto version_count = tablet->version_count() + tablet->stale_version_count(); |
169 | 0 | if (tablet->avg_rs_meta_serialize_size() * version_count > |
170 | 0 | config::tablet_meta_serialize_size_limit) { |
171 | 0 | return Status::Error<TOO_MANY_VERSION>( |
172 | 0 | "failed to init rowset builder. meta serialize size : {}, exceed limit: {}, " |
173 | 0 | "tablet: {}. Please reduce the frequency of loading data or adjust the " |
174 | 0 | "max_tablet_version_num in be.conf to a larger value.", |
175 | 0 | tablet->avg_rs_meta_serialize_size() * version_count, |
176 | 0 | config::tablet_meta_serialize_size_limit, tablet->tablet_id()); |
177 | 0 | } |
178 | | |
179 | 0 | auto tablet_schema = std::make_shared<TabletSchema>(); |
180 | 0 | tablet_schema->copy_from(*tablet->tablet_schema()); |
181 | 0 | if (!request.columns_desc.empty() && request.columns_desc[0].col_unique_id >= 0) { |
182 | 0 | tablet_schema->clear_columns(); |
183 | | // TODO(lhy) handle variant |
184 | 0 | for (const auto& column_desc : request.columns_desc) { |
185 | 0 | tablet_schema->append_column(TabletColumn(column_desc)); |
186 | 0 | } |
187 | 0 | } |
188 | 0 | RowsetSharedPtr rowset_to_add; |
189 | | // writes |
190 | 0 | res = _convert_v2(tablet, &rowset_to_add, tablet_schema, push_type); |
191 | 0 | if (!res.ok()) { |
192 | 0 | LOG(WARNING) << "fail to convert tmp file when realtime push. res=" << res |
193 | 0 | << ", failed to process realtime push." |
194 | 0 | << ", tablet=" << tablet->tablet_id() |
195 | 0 | << ", transaction_id=" << request.transaction_id; |
196 | |
|
197 | 0 | Status rollback_status = _engine.txn_manager()->rollback_txn(request.partition_id, *tablet, |
198 | 0 | request.transaction_id); |
199 | | // has to check rollback status to ensure not delete a committed rowset |
200 | 0 | if (rollback_status.ok()) { |
201 | 0 | _engine.add_unused_rowset(rowset_to_add); |
202 | 0 | } |
203 | 0 | return res; |
204 | 0 | } |
205 | | |
206 | | // add pending data to tablet |
207 | | |
208 | 0 | if (push_type == PushType::PUSH_FOR_DELETE) { |
209 | 0 | rowset_to_add->rowset_meta()->set_delete_predicate(std::move(del_preds.front())); |
210 | 0 | del_preds.pop(); |
211 | 0 | } |
212 | | // Transfer ownership of `PendingRowsetGuard` to `TxnManager` |
213 | 0 | Status commit_status = _engine.txn_manager()->commit_txn( |
214 | 0 | request.partition_id, *tablet, request.transaction_id, load_id, rowset_to_add, |
215 | 0 | std::move(_pending_rs_guard), false); |
216 | 0 | if (!commit_status.ok() && !commit_status.is<PUSH_TRANSACTION_ALREADY_EXIST>()) { |
217 | 0 | res = std::move(commit_status); |
218 | 0 | } |
219 | 0 | return res; |
220 | 0 | } |
221 | | |
222 | | Status PushHandler::_convert_v2(TabletSharedPtr cur_tablet, RowsetSharedPtr* cur_rowset, |
223 | 0 | TabletSchemaSPtr tablet_schema, PushType push_type) { |
224 | 0 | Status st = Status::OK(); |
225 | 0 | uint32_t num_rows = 0; |
226 | 0 | PUniqueId load_id; |
227 | 0 | load_id.set_hi(0); |
228 | 0 | load_id.set_lo(0); |
229 | |
|
230 | 0 | do { |
231 | 0 | VLOG_NOTICE << "start to convert delta file."; |
232 | | |
233 | | // 1. init RowsetBuilder of cur_tablet for current push |
234 | 0 | VLOG_NOTICE << "init rowset builder. tablet=" << cur_tablet->tablet_id() |
235 | 0 | << ", block_row_size=" << tablet_schema->num_rows_per_row_block(); |
236 | | // although the spark load output files are fully sorted, |
237 | | // but it depends on thirparty implementation, so we conservatively |
238 | | // set this value to OVERLAP_UNKNOWN |
239 | 0 | RowsetWriterContext context; |
240 | 0 | context.txn_id = _request.transaction_id; |
241 | 0 | context.load_id = load_id; |
242 | 0 | context.rowset_state = PREPARED; |
243 | 0 | context.segments_overlap = OVERLAP_UNKNOWN; |
244 | 0 | context.tablet_schema = tablet_schema; |
245 | 0 | context.newest_write_timestamp = UnixSeconds(); |
246 | 0 | auto rowset_writer = DORIS_TRY(cur_tablet->create_rowset_writer(context, false)); |
247 | 0 | _pending_rs_guard = _engine.pending_local_rowsets().add(context.rowset_id); |
248 | | |
249 | | // 2. Init PushBrokerReader to read broker file if exist, |
250 | | // in case of empty push this will be skipped. |
251 | 0 | std::string path; |
252 | | // If it is push delete, the broker_scan_range is not set. |
253 | 0 | if (push_type == PushType::PUSH_NORMAL_V2) { |
254 | 0 | path = _request.broker_scan_range.ranges[0].path; |
255 | 0 | LOG(INFO) << "tablet=" << cur_tablet->tablet_id() << ", file path=" << path |
256 | 0 | << ", file size=" << _request.broker_scan_range.ranges[0].file_size; |
257 | 0 | } |
258 | | // For push load, this tablet maybe not need push data, so that the path maybe empty |
259 | 0 | if (!path.empty()) { |
260 | | // init Reader |
261 | 0 | std::unique_ptr<PushBrokerReader> reader = |
262 | 0 | PushBrokerReader::create_unique(_request.broker_scan_range, _request.desc_tbl); |
263 | 0 | st = reader->init(); |
264 | 0 | if (reader == nullptr || !st.ok()) { |
265 | 0 | st = Status::Error<PUSH_INIT_ERROR>("fail to init reader. st={}, tablet={}", st, |
266 | 0 | cur_tablet->tablet_id()); |
267 | 0 | break; |
268 | 0 | } |
269 | | |
270 | | // 3. Init Block |
271 | 0 | vectorized::Block block; |
272 | | |
273 | | // 4. Read data from broker and write into cur_tablet |
274 | 0 | VLOG_NOTICE << "start to convert etl file to delta."; |
275 | 0 | while (!reader->eof()) { |
276 | 0 | st = reader->next(&block); |
277 | 0 | if (!st.ok()) { |
278 | 0 | LOG(WARNING) << "read next row failed." |
279 | 0 | << " st=" << st << " read_rows=" << num_rows; |
280 | 0 | break; |
281 | 0 | } else { |
282 | 0 | if (reader->eof()) { |
283 | 0 | break; |
284 | 0 | } |
285 | 0 | if (!(st = rowset_writer->add_block(&block)).ok()) { |
286 | 0 | LOG(WARNING) << "fail to attach block to rowset_writer. " |
287 | 0 | << "st=" << st << ", tablet=" << cur_tablet->tablet_id() |
288 | 0 | << ", read_rows=" << num_rows; |
289 | 0 | break; |
290 | 0 | } |
291 | 0 | num_rows++; |
292 | 0 | } |
293 | 0 | } |
294 | |
|
295 | 0 | reader->print_profile(); |
296 | 0 | RETURN_IF_ERROR(reader->close()); |
297 | 0 | } |
298 | | |
299 | 0 | if (!st.ok()) { |
300 | 0 | break; |
301 | 0 | } |
302 | | |
303 | 0 | if (!(st = rowset_writer->flush()).ok()) { |
304 | 0 | LOG(WARNING) << "failed to finalize writer"; |
305 | 0 | break; |
306 | 0 | } |
307 | | |
308 | 0 | if (!(st = rowset_writer->build(*cur_rowset)).ok()) { |
309 | 0 | LOG(WARNING) << "failed to build rowset"; |
310 | 0 | break; |
311 | 0 | } |
312 | | |
313 | 0 | _write_bytes += (*cur_rowset)->data_disk_size(); |
314 | 0 | _write_rows += (*cur_rowset)->num_rows(); |
315 | 0 | } while (false); |
316 | | |
317 | 0 | VLOG_TRACE << "convert delta file end. st=" << st << ", tablet=" << cur_tablet->tablet_id() |
318 | 0 | << ", processed_rows" << num_rows; |
319 | 0 | return st; |
320 | 0 | } |
321 | | |
322 | | PushBrokerReader::PushBrokerReader(const TBrokerScanRange& t_scan_range, |
323 | | const TDescriptorTable& t_desc_tbl) |
324 | 0 | : _ready(false), |
325 | 0 | _eof(false), |
326 | 0 | _next_range(0), |
327 | 0 | _t_desc_tbl(t_desc_tbl), |
328 | 0 | _cur_reader_eof(false), |
329 | 0 | _params(t_scan_range.params), |
330 | 0 | _ranges(t_scan_range.ranges) { |
331 | | // change broker params to file params |
332 | 0 | if (_ranges.empty()) { |
333 | 0 | return; |
334 | 0 | } |
335 | 0 | _file_params.format_type = _ranges[0].format_type; |
336 | 0 | _file_params.src_tuple_id = _params.src_tuple_id; |
337 | 0 | _file_params.dest_tuple_id = _params.dest_tuple_id; |
338 | 0 | _file_params.num_of_columns_from_file = _ranges[0].num_of_columns_from_file; |
339 | 0 | _file_params.properties = _params.properties; |
340 | 0 | _file_params.expr_of_dest_slot = _params.expr_of_dest_slot; |
341 | 0 | _file_params.dest_sid_to_src_sid_without_trans = _params.dest_sid_to_src_sid_without_trans; |
342 | 0 | _file_params.strict_mode = _params.strict_mode; |
343 | 0 | if (_ranges[0].file_type == TFileType::FILE_HDFS) { |
344 | 0 | _file_params.hdfs_params = parse_properties(_params.properties); |
345 | 0 | } else { |
346 | 0 | _file_params.__isset.broker_addresses = true; |
347 | 0 | _file_params.broker_addresses = t_scan_range.broker_addresses; |
348 | 0 | } |
349 | |
|
350 | 0 | for (const auto& range : _ranges) { |
351 | 0 | TFileRangeDesc file_range; |
352 | | // TODO(cmy): in previous implementation, the file_type is set in _file_params |
353 | | // and it use _ranges[0].file_type. |
354 | | // Later, this field is moved to TFileRangeDesc, but here we still only use _ranges[0]'s |
355 | | // file_type. |
356 | | // Because I don't know if other range has this field, so just keep it same as before. |
357 | 0 | file_range.__set_file_type(_ranges[0].file_type); |
358 | 0 | file_range.__set_load_id(range.load_id); |
359 | 0 | file_range.__set_path(range.path); |
360 | 0 | file_range.__set_start_offset(range.start_offset); |
361 | 0 | file_range.__set_size(range.size); |
362 | 0 | file_range.__set_file_size(range.file_size); |
363 | 0 | file_range.__set_columns_from_path(range.columns_from_path); |
364 | |
|
365 | 0 | _file_ranges.push_back(file_range); |
366 | 0 | } |
367 | 0 | } |
368 | | |
369 | 0 | Status PushBrokerReader::init() { |
370 | | // init runtime state, runtime profile, counter |
371 | 0 | TUniqueId dummy_id; |
372 | 0 | dummy_id.hi = 0; |
373 | 0 | dummy_id.lo = 0; |
374 | 0 | TPlanFragmentExecParams params; |
375 | 0 | params.fragment_instance_id = dummy_id; |
376 | 0 | params.query_id = dummy_id; |
377 | 0 | TQueryOptions query_options; |
378 | 0 | TQueryGlobals query_globals; |
379 | 0 | std::shared_ptr<MemTrackerLimiter> tracker = MemTrackerLimiter::create_shared( |
380 | 0 | MemTrackerLimiter::Type::LOAD, |
381 | 0 | fmt::format("PushBrokerReader:dummy_id={}", print_id(dummy_id))); |
382 | 0 | _runtime_state = RuntimeState::create_unique(params, query_options, query_globals, |
383 | 0 | ExecEnv::GetInstance(), nullptr, tracker); |
384 | 0 | DescriptorTbl* desc_tbl = nullptr; |
385 | 0 | Status status = DescriptorTbl::create(_runtime_state->obj_pool(), _t_desc_tbl, &desc_tbl); |
386 | 0 | if (UNLIKELY(!status.ok())) { |
387 | 0 | return Status::Error<PUSH_INIT_ERROR>("Failed to create descriptor table, msg: {}", status); |
388 | 0 | } |
389 | 0 | _runtime_state->set_desc_tbl(desc_tbl); |
390 | 0 | _runtime_profile = _runtime_state->runtime_profile(); |
391 | 0 | _runtime_profile->set_name("PushBrokerReader"); |
392 | |
|
393 | 0 | _file_cache_statistics.reset(new io::FileCacheStatistics()); |
394 | 0 | _file_reader_stats.reset(new io::FileReaderStats()); |
395 | 0 | _io_ctx.reset(new io::IOContext()); |
396 | 0 | _io_ctx->file_cache_stats = _file_cache_statistics.get(); |
397 | 0 | _io_ctx->file_reader_stats = _file_reader_stats.get(); |
398 | 0 | _io_ctx->query_id = &_runtime_state->query_id(); |
399 | |
|
400 | 0 | auto slot_descs = desc_tbl->get_tuple_descriptor(0)->slots(); |
401 | 0 | for (auto& slot_desc : slot_descs) { |
402 | 0 | _all_col_names.push_back(to_lower((slot_desc->col_name()))); |
403 | 0 | } |
404 | |
|
405 | 0 | RETURN_IF_ERROR(_init_expr_ctxes()); |
406 | | |
407 | 0 | _ready = true; |
408 | 0 | return Status::OK(); |
409 | 0 | } |
410 | | |
411 | 0 | Status PushBrokerReader::next(vectorized::Block* block) { |
412 | 0 | if (!_ready || block == nullptr) { |
413 | 0 | return Status::Error<INVALID_ARGUMENT>("PushBrokerReader not ready or block is nullptr"); |
414 | 0 | } |
415 | 0 | if (_cur_reader == nullptr || _cur_reader_eof) { |
416 | 0 | RETURN_IF_ERROR(_get_next_reader()); |
417 | 0 | if (_eof) { |
418 | 0 | return Status::OK(); |
419 | 0 | } |
420 | 0 | } |
421 | 0 | RETURN_IF_ERROR(_init_src_block()); |
422 | 0 | size_t read_rows = 0; |
423 | 0 | RETURN_IF_ERROR(_cur_reader->get_next_block(_src_block_ptr, &read_rows, &_cur_reader_eof)); |
424 | 0 | if (read_rows > 0) { |
425 | 0 | RETURN_IF_ERROR(_cast_to_input_block()); |
426 | 0 | RETURN_IF_ERROR(_convert_to_output_block(block)); |
427 | 0 | } |
428 | 0 | return Status::OK(); |
429 | 0 | } |
430 | | |
431 | 0 | Status PushBrokerReader::close() { |
432 | 0 | _ready = false; |
433 | 0 | return Status::OK(); |
434 | 0 | } |
435 | | |
436 | 0 | Status PushBrokerReader::_init_src_block() { |
437 | 0 | _src_block.clear(); |
438 | 0 | int idx = 0; |
439 | 0 | for (auto& slot : _src_slot_descs) { |
440 | 0 | vectorized::DataTypePtr data_type; |
441 | 0 | auto it = _name_to_col_type.find(slot->col_name()); |
442 | 0 | if (it == _name_to_col_type.end()) { |
443 | | // not exist in file, using type from _input_tuple_desc |
444 | 0 | data_type = slot->get_data_type_ptr(); |
445 | 0 | } else { |
446 | 0 | data_type = it->second; |
447 | 0 | } |
448 | 0 | if (data_type == nullptr) { |
449 | 0 | return Status::NotSupported("Not support data type {} for column {}", |
450 | 0 | it == _name_to_col_type.end() ? slot->type()->get_name() |
451 | 0 | : it->second->get_name(), |
452 | 0 | slot->col_name()); |
453 | 0 | } |
454 | 0 | vectorized::MutableColumnPtr data_column = data_type->create_column(); |
455 | 0 | _src_block.insert(vectorized::ColumnWithTypeAndName(std::move(data_column), data_type, |
456 | 0 | slot->col_name())); |
457 | 0 | _src_block_name_to_idx.emplace(slot->col_name(), idx++); |
458 | 0 | } |
459 | 0 | _src_block_ptr = &_src_block; |
460 | 0 | return Status::OK(); |
461 | 0 | } |
462 | | |
463 | 0 | Status PushBrokerReader::_cast_to_input_block() { |
464 | 0 | uint32_t idx = 0; |
465 | 0 | for (auto& slot_desc : _src_slot_descs) { |
466 | 0 | if (_name_to_col_type.find(slot_desc->col_name()) == _name_to_col_type.end()) { |
467 | 0 | continue; |
468 | 0 | } |
469 | 0 | if (slot_desc->type()->get_primitive_type() == PrimitiveType::TYPE_VARIANT) { |
470 | 0 | continue; |
471 | 0 | } |
472 | | // remove nullable here, let the get_function decide whether nullable |
473 | 0 | auto return_type = slot_desc->get_data_type_ptr(); |
474 | 0 | idx = _src_block_name_to_idx[slot_desc->col_name()]; |
475 | 0 | auto& arg = _src_block_ptr->get_by_position(idx); |
476 | | // bitmap convert:src -> to_base64 -> bitmap_from_base64 |
477 | 0 | if (slot_desc->type()->get_primitive_type() == TYPE_BITMAP) { |
478 | 0 | auto base64_return_type = vectorized::DataTypeFactory::instance().create_data_type( |
479 | 0 | PrimitiveType::TYPE_STRING, slot_desc->is_nullable()); |
480 | 0 | auto func_to_base64 = vectorized::SimpleFunctionFactory::instance().get_function( |
481 | 0 | "to_base64", {arg}, base64_return_type); |
482 | 0 | RETURN_IF_ERROR(func_to_base64->execute(nullptr, *_src_block_ptr, {idx}, idx, |
483 | 0 | arg.column->size())); |
484 | 0 | _src_block_ptr->get_by_position(idx).type = std::move(base64_return_type); |
485 | 0 | auto& arg_base64 = _src_block_ptr->get_by_position(idx); |
486 | 0 | auto func_bitmap_from_base64 = |
487 | 0 | vectorized::SimpleFunctionFactory::instance().get_function( |
488 | 0 | "bitmap_from_base64", {arg_base64}, return_type); |
489 | 0 | RETURN_IF_ERROR(func_bitmap_from_base64->execute(nullptr, *_src_block_ptr, {idx}, idx, |
490 | 0 | arg_base64.column->size())); |
491 | 0 | _src_block_ptr->get_by_position(idx).type = std::move(return_type); |
492 | 0 | } else { |
493 | 0 | vectorized::ColumnsWithTypeAndName arguments { |
494 | 0 | arg, |
495 | 0 | {vectorized::DataTypeString().create_column_const( |
496 | 0 | arg.column->size(), |
497 | 0 | vectorized::Field::create_field<TYPE_STRING>( |
498 | 0 | is_decimal(return_type->get_primitive_type()) |
499 | 0 | ? "Decimal" |
500 | 0 | : remove_nullable(return_type)->get_family_name())), |
501 | 0 | std::make_shared<vectorized::DataTypeString>(), ""}}; |
502 | 0 | auto func_cast = vectorized::SimpleFunctionFactory::instance().get_function( |
503 | 0 | "CAST", arguments, return_type); |
504 | 0 | RETURN_IF_ERROR( |
505 | 0 | func_cast->execute(nullptr, *_src_block_ptr, {idx}, idx, arg.column->size())); |
506 | 0 | _src_block_ptr->get_by_position(idx).type = std::move(return_type); |
507 | 0 | } |
508 | 0 | } |
509 | 0 | return Status::OK(); |
510 | 0 | } |
511 | | |
512 | 0 | Status PushBrokerReader::_convert_to_output_block(vectorized::Block* block) { |
513 | 0 | block->clear(); |
514 | |
|
515 | 0 | int ctx_idx = 0; |
516 | 0 | size_t rows = _src_block.rows(); |
517 | 0 | auto filter_column = vectorized::ColumnUInt8::create(rows, 1); |
518 | |
|
519 | 0 | for (auto* slot_desc : _dest_tuple_desc->slots()) { |
520 | 0 | int dest_index = ctx_idx++; |
521 | 0 | vectorized::ColumnPtr column_ptr; |
522 | |
|
523 | 0 | auto& ctx = _dest_expr_ctxs[dest_index]; |
524 | 0 | int result_column_id = -1; |
525 | | // PT1 => dest primitive type |
526 | 0 | RETURN_IF_ERROR(ctx->execute(&_src_block, &result_column_id)); |
527 | 0 | column_ptr = _src_block.get_by_position(result_column_id).column; |
528 | | // column_ptr maybe a ColumnConst, convert it to a normal column |
529 | 0 | column_ptr = column_ptr->convert_to_full_column_if_const(); |
530 | 0 | DCHECK(column_ptr); |
531 | | |
532 | | // because of src_slot_desc is always be nullable, so the column_ptr after do dest_expr |
533 | | // is likely to be nullable |
534 | 0 | if (LIKELY(column_ptr->is_nullable())) { |
535 | 0 | if (!slot_desc->is_nullable()) { |
536 | 0 | column_ptr = remove_nullable(column_ptr); |
537 | 0 | } |
538 | 0 | } else if (slot_desc->is_nullable()) { |
539 | 0 | column_ptr = make_nullable(column_ptr); |
540 | 0 | } |
541 | 0 | block->insert(dest_index, |
542 | 0 | vectorized::ColumnWithTypeAndName(column_ptr, slot_desc->get_data_type_ptr(), |
543 | 0 | slot_desc->col_name())); |
544 | 0 | } |
545 | 0 | _src_block.clear(); |
546 | |
|
547 | 0 | size_t dest_size = block->columns(); |
548 | 0 | block->insert(vectorized::ColumnWithTypeAndName(std::move(filter_column), |
549 | 0 | std::make_shared<vectorized::DataTypeUInt8>(), |
550 | 0 | "filter column")); |
551 | 0 | RETURN_IF_ERROR(vectorized::Block::filter_block(block, dest_size, dest_size)); |
552 | 0 | return Status::OK(); |
553 | 0 | } |
554 | | |
555 | 0 | void PushBrokerReader::print_profile() { |
556 | 0 | std::stringstream ss; |
557 | 0 | _runtime_profile->pretty_print(&ss); |
558 | 0 | LOG(INFO) << ss.str(); |
559 | 0 | } |
560 | | |
561 | 0 | Status PushBrokerReader::_init_expr_ctxes() { |
562 | | // Construct _src_slot_descs |
563 | 0 | const TupleDescriptor* src_tuple_desc = |
564 | 0 | _runtime_state->desc_tbl().get_tuple_descriptor(_params.src_tuple_id); |
565 | 0 | if (src_tuple_desc == nullptr) { |
566 | 0 | return Status::InternalError("Unknown source tuple descriptor, tuple_id={}", |
567 | 0 | _params.src_tuple_id); |
568 | 0 | } |
569 | | |
570 | 0 | std::map<SlotId, SlotDescriptor*> src_slot_desc_map; |
571 | 0 | std::unordered_map<SlotDescriptor*, int> src_slot_desc_to_index {}; |
572 | 0 | for (size_t i = 0, len = src_tuple_desc->slots().size(); i < len; ++i) { |
573 | 0 | auto* slot_desc = src_tuple_desc->slots()[i]; |
574 | 0 | src_slot_desc_to_index.emplace(slot_desc, i); |
575 | 0 | src_slot_desc_map.emplace(slot_desc->id(), slot_desc); |
576 | 0 | } |
577 | 0 | for (auto slot_id : _params.src_slot_ids) { |
578 | 0 | auto it = src_slot_desc_map.find(slot_id); |
579 | 0 | if (it == std::end(src_slot_desc_map)) { |
580 | 0 | return Status::InternalError("Unknown source slot descriptor, slot_id={}", slot_id); |
581 | 0 | } |
582 | 0 | _src_slot_descs.emplace_back(it->second); |
583 | 0 | } |
584 | 0 | _row_desc.reset(new RowDescriptor(_runtime_state->desc_tbl(), |
585 | 0 | std::vector<TupleId>({_params.src_tuple_id}), |
586 | 0 | std::vector<bool>({false}))); |
587 | |
|
588 | 0 | if (!_pre_filter_texprs.empty()) { |
589 | 0 | DCHECK(_pre_filter_texprs.size() == 1); |
590 | 0 | RETURN_IF_ERROR( |
591 | 0 | vectorized::VExpr::create_expr_tree(_pre_filter_texprs[0], _pre_filter_ctx_ptr)); |
592 | 0 | RETURN_IF_ERROR(_pre_filter_ctx_ptr->prepare(_runtime_state.get(), *_row_desc)); |
593 | 0 | RETURN_IF_ERROR(_pre_filter_ctx_ptr->open(_runtime_state.get())); |
594 | 0 | } |
595 | | |
596 | 0 | _dest_tuple_desc = _runtime_state->desc_tbl().get_tuple_descriptor(_params.dest_tuple_id); |
597 | 0 | if (_dest_tuple_desc == nullptr) { |
598 | 0 | return Status::InternalError("Unknown dest tuple descriptor, tuple_id={}", |
599 | 0 | _params.dest_tuple_id); |
600 | 0 | } |
601 | 0 | bool has_slot_id_map = _params.__isset.dest_sid_to_src_sid_without_trans; |
602 | 0 | for (auto slot_desc : _dest_tuple_desc->slots()) { |
603 | 0 | auto it = _params.expr_of_dest_slot.find(slot_desc->id()); |
604 | 0 | if (it == std::end(_params.expr_of_dest_slot)) { |
605 | 0 | return Status::InternalError("No expr for dest slot, id={}, name={}", slot_desc->id(), |
606 | 0 | slot_desc->col_name()); |
607 | 0 | } |
608 | | |
609 | 0 | vectorized::VExprContextSPtr ctx; |
610 | 0 | RETURN_IF_ERROR(vectorized::VExpr::create_expr_tree(it->second, ctx)); |
611 | 0 | RETURN_IF_ERROR(ctx->prepare(_runtime_state.get(), *_row_desc.get())); |
612 | 0 | RETURN_IF_ERROR(ctx->open(_runtime_state.get())); |
613 | 0 | _dest_expr_ctxs.emplace_back(ctx); |
614 | 0 | if (has_slot_id_map) { |
615 | 0 | auto it1 = _params.dest_sid_to_src_sid_without_trans.find(slot_desc->id()); |
616 | 0 | if (it1 == std::end(_params.dest_sid_to_src_sid_without_trans)) { |
617 | 0 | _src_slot_descs_order_by_dest.emplace_back(nullptr); |
618 | 0 | } else { |
619 | 0 | auto _src_slot_it = src_slot_desc_map.find(it1->second); |
620 | 0 | if (_src_slot_it == std::end(src_slot_desc_map)) { |
621 | 0 | return Status::InternalError("No src slot {} in src slot descs", it1->second); |
622 | 0 | } |
623 | 0 | _dest_slot_to_src_slot_index.emplace(_src_slot_descs_order_by_dest.size(), |
624 | 0 | src_slot_desc_to_index[_src_slot_it->second]); |
625 | 0 | _src_slot_descs_order_by_dest.emplace_back(_src_slot_it->second); |
626 | 0 | } |
627 | 0 | } |
628 | 0 | } |
629 | 0 | return Status::OK(); |
630 | 0 | } |
631 | | |
632 | 0 | Status PushBrokerReader::_get_next_reader() { |
633 | 0 | _cur_reader.reset(nullptr); |
634 | 0 | if (_next_range >= _file_ranges.size()) { |
635 | 0 | _eof = true; |
636 | 0 | return Status::OK(); |
637 | 0 | } |
638 | 0 | const TFileRangeDesc& range = _file_ranges[_next_range++]; |
639 | 0 | Status init_status; |
640 | 0 | switch (_file_params.format_type) { |
641 | 0 | case TFileFormatType::FORMAT_PARQUET: { |
642 | 0 | std::unique_ptr<vectorized::ParquetReader> parquet_reader = |
643 | 0 | vectorized::ParquetReader::create_unique(_runtime_profile, _file_params, range, |
644 | 0 | _runtime_state->query_options().batch_size, |
645 | 0 | &_runtime_state->timezone_obj(), |
646 | 0 | _io_ctx.get(), _runtime_state.get()); |
647 | |
|
648 | 0 | init_status = parquet_reader->init_reader( |
649 | 0 | _all_col_names, _push_down_exprs, _real_tuple_desc, _default_val_row_desc.get(), |
650 | 0 | _col_name_to_slot_id, &_not_single_slot_filter_conjuncts, |
651 | 0 | &_slot_id_to_filter_conjuncts, |
652 | 0 | vectorized::TableSchemaChangeHelper::ConstNode::get_instance(), false); |
653 | 0 | _cur_reader = std::move(parquet_reader); |
654 | 0 | if (!init_status.ok()) { |
655 | 0 | return Status::InternalError("failed to init reader for file {}, err: {}", range.path, |
656 | 0 | init_status.to_string()); |
657 | 0 | } |
658 | 0 | std::unordered_map<std::string, std::tuple<std::string, const SlotDescriptor*>> |
659 | 0 | partition_columns; |
660 | 0 | std::unordered_map<std::string, vectorized::VExprContextSPtr> missing_columns; |
661 | 0 | RETURN_IF_ERROR(_cur_reader->get_columns(&_name_to_col_type, &_missing_cols)); |
662 | 0 | RETURN_IF_ERROR(_cur_reader->set_fill_columns(partition_columns, missing_columns)); |
663 | 0 | break; |
664 | 0 | } |
665 | 0 | default: |
666 | 0 | return Status::Error<PUSH_INIT_ERROR>("Unsupported file format type: {}", |
667 | 0 | _file_params.format_type); |
668 | 0 | } |
669 | 0 | _cur_reader_eof = false; |
670 | |
|
671 | 0 | return Status::OK(); |
672 | 0 | } |
673 | | |
674 | | #include "common/compile_check_end.h" |
675 | | } // namespace doris |