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