Coverage Report

Created: 2024-11-21 23:45

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