Coverage Report

Created: 2026-03-25 23:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/tablet/tablet_reader.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 "storage/tablet/tablet_reader.h"
19
20
#include <gen_cpp/olap_file.pb.h>
21
#include <gen_cpp/segment_v2.pb.h>
22
#include <thrift/protocol/TDebugProtocol.h>
23
24
#include <algorithm>
25
#include <functional>
26
#include <iterator>
27
#include <memory>
28
#include <numeric>
29
#include <ostream>
30
#include <shared_mutex>
31
32
#include "common/compiler_util.h" // IWYU pragma: keep
33
#include "common/config.h"
34
#include "common/exception.h"
35
#include "common/logging.h"
36
#include "common/status.h"
37
#include "core/arena.h"
38
#include "core/block/block.h"
39
#include "exec/common/variant_util.h"
40
#include "exprs/bitmapfilter_predicate.h"
41
#include "exprs/bloom_filter_func.h"
42
#include "exprs/create_predicate_function.h"
43
#include "exprs/hybrid_set.h"
44
#include "runtime/query_context.h"
45
#include "runtime/runtime_predicate.h"
46
#include "runtime/runtime_state.h"
47
#include "storage/index/bloom_filter/bloom_filter.h"
48
#include "storage/itoken_extractor.h"
49
#include "storage/olap_common.h"
50
#include "storage/olap_define.h"
51
#include "storage/predicate/column_predicate.h"
52
#include "storage/predicate/like_column_predicate.h"
53
#include "storage/predicate/predicate_creator.h"
54
#include "storage/row_cursor.h"
55
#include "storage/schema.h"
56
#include "storage/tablet/tablet.h"
57
#include "storage/tablet/tablet_meta.h"
58
#include "storage/tablet/tablet_schema.h"
59
60
namespace doris {
61
#include "common/compile_check_begin.h"
62
using namespace ErrorCode;
63
64
362
void TabletReader::ReaderParams::check_validation() const {
65
362
    if (UNLIKELY(version.first == -1 && is_segcompaction == false)) {
66
0
        throw Exception(Status::FatalError("version is not set. tablet={}", tablet->tablet_id()));
67
0
    }
68
362
}
69
70
362
Status TabletReader::init(const ReaderParams& read_params) {
71
362
    SCOPED_RAW_TIMER(&_stats.tablet_reader_init_timer_ns);
72
73
362
    Status res = _init_params(read_params);
74
362
    if (!res.ok()) {
75
0
        LOG(WARNING) << "fail to init reader when init params. res:" << res
76
0
                     << ", tablet_id:" << read_params.tablet->tablet_id()
77
0
                     << ", schema_hash:" << read_params.tablet->schema_hash()
78
0
                     << ", reader type:" << int(read_params.reader_type)
79
0
                     << ", version:" << read_params.version;
80
0
    }
81
362
    return res;
82
362
}
83
84
340
Status TabletReader::_capture_rs_readers(const ReaderParams& read_params) {
85
340
    SCOPED_RAW_TIMER(&_stats.tablet_reader_capture_rs_readers_timer_ns);
86
340
    if (read_params.rs_splits.empty()) {
87
0
        return Status::InternalError("fail to acquire data sources. tablet={}",
88
0
                                     _tablet->tablet_id());
89
0
    }
90
91
340
    bool eof = false;
92
340
    bool is_lower_key_included = _keys_param.start_key_include;
93
340
    bool is_upper_key_included = _keys_param.end_key_include;
94
95
340
    for (int i = 0; i < _keys_param.start_keys.size(); ++i) {
96
        // lower bound
97
0
        RowCursor& start_key = _keys_param.start_keys[i];
98
0
        RowCursor& end_key = _keys_param.end_keys[i];
99
100
0
        if (!is_lower_key_included) {
101
0
            if (compare_row_key(start_key, end_key) >= 0) {
102
0
                VLOG_NOTICE << "return EOF when lower key not include"
103
0
                            << ", start_key=" << start_key.to_string()
104
0
                            << ", end_key=" << end_key.to_string();
105
0
                eof = true;
106
0
                break;
107
0
            }
108
0
        } else {
109
0
            if (compare_row_key(start_key, end_key) > 0) {
110
0
                VLOG_NOTICE << "return EOF when lower key include="
111
0
                            << ", start_key=" << start_key.to_string()
112
0
                            << ", end_key=" << end_key.to_string();
113
0
                eof = true;
114
0
                break;
115
0
            }
116
0
        }
117
118
0
        _is_lower_keys_included.push_back(is_lower_key_included);
119
0
        _is_upper_keys_included.push_back(is_upper_key_included);
120
0
    }
121
122
340
    if (eof) {
123
0
        return Status::EndOfFile("reach end of scan range. tablet={}", _tablet->tablet_id());
124
0
    }
125
126
340
    bool need_ordered_result = true;
127
340
    if (read_params.reader_type == ReaderType::READER_QUERY) {
128
0
        if (_tablet_schema->keys_type() == DUP_KEYS) {
129
            // duplicated keys are allowed, no need to merge sort keys in rowset
130
0
            need_ordered_result = false;
131
0
        }
132
0
        if (_tablet_schema->keys_type() == UNIQUE_KEYS &&
133
0
            _tablet->enable_unique_key_merge_on_write()) {
134
            // unique keys with merge on write, no need to merge sort keys in rowset
135
0
            need_ordered_result = false;
136
0
        }
137
0
        if (_aggregation) {
138
            // compute engine will aggregate rows with the same key,
139
            // it's ok for rowset to return unordered result
140
0
            need_ordered_result = false;
141
0
        }
142
143
0
        if (_direct_mode) {
144
            // direct mode indicates that the storage layer does not need to merge,
145
            // it's ok for rowset to return unordered result
146
0
            need_ordered_result = false;
147
0
        }
148
149
0
        if (read_params.read_orderby_key) {
150
0
            need_ordered_result = true;
151
0
        }
152
0
    }
153
154
340
    _reader_context.reader_type = read_params.reader_type;
155
340
    _reader_context.version = read_params.version;
156
340
    _reader_context.tablet_schema = _tablet_schema;
157
340
    _reader_context.need_ordered_result = need_ordered_result;
158
340
    _reader_context.topn_filter_source_node_ids = read_params.topn_filter_source_node_ids;
159
340
    _reader_context.topn_filter_target_node_id = read_params.topn_filter_target_node_id;
160
340
    _reader_context.read_orderby_key_reverse = read_params.read_orderby_key_reverse;
161
340
    _reader_context.read_orderby_key_limit = read_params.read_orderby_key_limit;
162
340
    _reader_context.filter_block_conjuncts = read_params.filter_block_conjuncts;
163
340
    _reader_context.return_columns = &_return_columns;
164
340
    _reader_context.read_orderby_key_columns =
165
340
            !_orderby_key_columns.empty() ? &_orderby_key_columns : nullptr;
166
340
    _reader_context.predicates = &_col_predicates;
167
340
    _reader_context.value_predicates = &_value_col_predicates;
168
340
    _reader_context.lower_bound_keys = &_keys_param.start_keys;
169
340
    _reader_context.is_lower_keys_included = &_is_lower_keys_included;
170
340
    _reader_context.upper_bound_keys = &_keys_param.end_keys;
171
340
    _reader_context.is_upper_keys_included = &_is_upper_keys_included;
172
340
    _reader_context.delete_handler = &_delete_handler;
173
340
    _reader_context.stats = &_stats;
174
340
    _reader_context.use_page_cache = read_params.use_page_cache;
175
340
    _reader_context.sequence_id_idx = _sequence_col_idx;
176
340
    _reader_context.is_unique = tablet()->keys_type() == UNIQUE_KEYS;
177
340
    _reader_context.merged_rows = &_merged_rows;
178
340
    _reader_context.delete_bitmap = read_params.delete_bitmap;
179
340
    _reader_context.enable_unique_key_merge_on_write = tablet()->enable_unique_key_merge_on_write();
180
340
    _reader_context.enable_mor_value_predicate_pushdown =
181
340
            read_params.enable_mor_value_predicate_pushdown;
182
340
    _reader_context.record_rowids = read_params.record_rowids;
183
340
    _reader_context.rowid_conversion = read_params.rowid_conversion;
184
340
    _reader_context.is_key_column_group = read_params.is_key_column_group;
185
340
    _reader_context.remaining_conjunct_roots = read_params.remaining_conjunct_roots;
186
340
    _reader_context.common_expr_ctxs_push_down = read_params.common_expr_ctxs_push_down;
187
340
    _reader_context.output_columns = &read_params.output_columns;
188
340
    _reader_context.push_down_agg_type_opt = read_params.push_down_agg_type_opt;
189
340
    _reader_context.ttl_seconds = _tablet->ttl_seconds();
190
340
    _reader_context.score_runtime = read_params.score_runtime;
191
340
    _reader_context.collection_statistics = read_params.collection_statistics;
192
193
340
    _reader_context.virtual_column_exprs = read_params.virtual_column_exprs;
194
340
    _reader_context.vir_cid_to_idx_in_block = read_params.vir_cid_to_idx_in_block;
195
340
    _reader_context.vir_col_idx_to_type = read_params.vir_col_idx_to_type;
196
340
    _reader_context.ann_topn_runtime = read_params.ann_topn_runtime;
197
198
340
    _reader_context.condition_cache_digest = read_params.condition_cache_digest;
199
340
    _reader_context.all_access_paths = read_params.all_access_paths;
200
340
    _reader_context.predicate_access_paths = read_params.predicate_access_paths;
201
202
340
    return Status::OK();
203
340
}
204
205
0
TabletColumn TabletReader::materialize_column(const TabletColumn& orig) {
206
0
    if (!orig.is_variant_type()) {
207
0
        return orig;
208
0
    }
209
0
    TabletColumn column_with_cast_type = orig;
210
0
    auto cast_type = _reader_context.target_cast_type_for_variants.at(orig.name());
211
0
    return variant_util::get_column_by_type(cast_type, orig.name(),
212
0
                                            {
213
0
                                                    .unique_id = orig.unique_id(),
214
0
                                                    .parent_unique_id = orig.parent_unique_id(),
215
0
                                                    .path_info = *orig.path_info_ptr(),
216
0
                                            });
217
0
}
218
219
362
Status TabletReader::_init_params(const ReaderParams& read_params) {
220
362
    read_params.check_validation();
221
222
362
    _direct_mode = read_params.direct_mode;
223
362
    _aggregation = read_params.aggregation;
224
362
    _reader_type = read_params.reader_type;
225
362
    _tablet = read_params.tablet;
226
362
    _tablet_schema = read_params.tablet_schema;
227
362
    _reader_context.runtime_state = read_params.runtime_state;
228
362
    _reader_context.target_cast_type_for_variants = read_params.target_cast_type_for_variants;
229
230
362
    RETURN_IF_ERROR(_init_conditions_param(read_params));
231
232
362
    Status res = _init_delete_condition(read_params);
233
362
    if (!res.ok()) {
234
0
        LOG(WARNING) << "fail to init delete param. res = " << res;
235
0
        return res;
236
0
    }
237
238
362
    res = _init_return_columns(read_params);
239
362
    if (!res.ok()) {
240
0
        LOG(WARNING) << "fail to init return columns. res = " << res;
241
0
        return res;
242
0
    }
243
244
362
    res = _init_keys_param(read_params);
245
362
    if (!res.ok()) {
246
0
        LOG(WARNING) << "fail to init keys param. res=" << res;
247
0
        return res;
248
0
    }
249
362
    res = _init_orderby_keys_param(read_params);
250
362
    if (!res.ok()) {
251
0
        LOG(WARNING) << "fail to init orderby keys param. res=" << res;
252
0
        return res;
253
0
    }
254
362
    if (_tablet_schema->has_sequence_col()) {
255
8
        auto sequence_col_idx = _tablet_schema->sequence_col_idx();
256
8
        DCHECK_NE(sequence_col_idx, -1);
257
16
        for (auto col : _return_columns) {
258
            // query has sequence col
259
16
            if (col == sequence_col_idx) {
260
4
                _sequence_col_idx = sequence_col_idx;
261
4
                break;
262
4
            }
263
16
        }
264
8
    }
265
266
362
    return res;
267
362
}
268
269
362
Status TabletReader::_init_return_columns(const ReaderParams& read_params) {
270
362
    SCOPED_RAW_TIMER(&_stats.tablet_reader_init_return_columns_timer_ns);
271
362
    if (read_params.reader_type == ReaderType::READER_QUERY) {
272
22
        _return_columns = read_params.return_columns;
273
22
        _tablet_columns_convert_to_null_set = read_params.tablet_columns_convert_to_null_set;
274
37
        for (auto id : read_params.return_columns) {
275
37
            if (_tablet_schema->column(id).is_key()) {
276
22
                _key_cids.push_back(id);
277
22
            } else {
278
15
                _value_cids.push_back(id);
279
15
            }
280
37
        }
281
340
    } else if (read_params.return_columns.empty()) {
282
0
        for (uint32_t i = 0; i < _tablet_schema->num_columns(); ++i) {
283
0
            _return_columns.push_back(i);
284
0
            if (_tablet_schema->column(i).is_key()) {
285
0
                _key_cids.push_back(i);
286
0
            } else {
287
0
                _value_cids.push_back(i);
288
0
            }
289
0
        }
290
0
        VLOG_NOTICE << "return column is empty, using full column as default.";
291
340
    } else if ((read_params.reader_type == ReaderType::READER_CUMULATIVE_COMPACTION ||
292
340
                read_params.reader_type == ReaderType::READER_SEGMENT_COMPACTION ||
293
340
                read_params.reader_type == ReaderType::READER_BASE_COMPACTION ||
294
340
                read_params.reader_type == ReaderType::READER_FULL_COMPACTION ||
295
340
                read_params.reader_type == ReaderType::READER_COLD_DATA_COMPACTION ||
296
340
                read_params.reader_type == ReaderType::READER_ALTER_TABLE) &&
297
340
               !read_params.return_columns.empty()) {
298
340
        _return_columns = read_params.return_columns;
299
1.08k
        for (auto id : read_params.return_columns) {
300
1.08k
            if (_tablet_schema->column(id).is_key()) {
301
115
                _key_cids.push_back(id);
302
965
            } else {
303
965
                _value_cids.push_back(id);
304
965
            }
305
1.08k
        }
306
340
    } else if (read_params.reader_type == ReaderType::READER_CHECKSUM) {
307
0
        _return_columns = read_params.return_columns;
308
0
        for (auto id : read_params.return_columns) {
309
0
            if (_tablet_schema->column(id).is_key()) {
310
0
                _key_cids.push_back(id);
311
0
            } else {
312
0
                _value_cids.push_back(id);
313
0
            }
314
0
        }
315
0
    } else {
316
0
        return Status::Error<INVALID_ARGUMENT>(
317
0
                "fail to init return columns. reader_type={}, return_columns_size={}",
318
0
                int(read_params.reader_type), read_params.return_columns.size());
319
0
    }
320
321
362
    std::sort(_key_cids.begin(), _key_cids.end(), std::greater<>());
322
323
362
    return Status::OK();
324
362
}
325
326
362
Status TabletReader::_init_keys_param(const ReaderParams& read_params) {
327
362
    SCOPED_RAW_TIMER(&_stats.tablet_reader_init_keys_param_timer_ns);
328
362
    if (read_params.start_key.empty()) {
329
362
        return Status::OK();
330
362
    }
331
332
0
    _keys_param.start_key_include = read_params.start_key_include;
333
0
    _keys_param.end_key_include = read_params.end_key_include;
334
335
0
    size_t start_key_size = read_params.start_key.size();
336
    //_keys_param.start_keys.resize(start_key_size);
337
0
    std::vector<RowCursor>(start_key_size).swap(_keys_param.start_keys);
338
339
0
    size_t scan_key_size = read_params.start_key.front().size();
340
0
    if (scan_key_size > _tablet_schema->num_columns()) {
341
0
        return Status::Error<INVALID_ARGUMENT>(
342
0
                "Input param are invalid. Column count is bigger than num_columns of schema. "
343
0
                "column_count={}, schema.num_columns={}",
344
0
                scan_key_size, _tablet_schema->num_columns());
345
0
    }
346
347
0
    std::vector<uint32_t> columns(scan_key_size);
348
0
    std::iota(columns.begin(), columns.end(), 0);
349
350
0
    std::shared_ptr<Schema> schema = std::make_shared<Schema>(_tablet_schema->columns(), columns);
351
352
0
    for (size_t i = 0; i < start_key_size; ++i) {
353
0
        if (read_params.start_key[i].size() != scan_key_size) {
354
0
            return Status::Error<INVALID_ARGUMENT>(
355
0
                    "The start_key.at({}).size={}, not equals the scan_key_size={}", i,
356
0
                    read_params.start_key[i].size(), scan_key_size);
357
0
        }
358
359
0
        Status res =
360
0
                _keys_param.start_keys[i].init(_tablet_schema, read_params.start_key[i], schema);
361
0
        if (!res.ok()) {
362
0
            LOG(WARNING) << "fail to init row cursor. res = " << res;
363
0
            return res;
364
0
        }
365
0
    }
366
367
0
    size_t end_key_size = read_params.end_key.size();
368
    //_keys_param.end_keys.resize(end_key_size);
369
0
    std::vector<RowCursor>(end_key_size).swap(_keys_param.end_keys);
370
0
    for (size_t i = 0; i < end_key_size; ++i) {
371
0
        if (read_params.end_key[i].size() != scan_key_size) {
372
0
            return Status::Error<INVALID_ARGUMENT>(
373
0
                    "The end_key.at({}).size={}, not equals the scan_key_size={}", i,
374
0
                    read_params.end_key[i].size(), scan_key_size);
375
0
        }
376
377
0
        Status res = _keys_param.end_keys[i].init(_tablet_schema, read_params.end_key[i], schema);
378
0
        if (!res.ok()) {
379
0
            LOG(WARNING) << "fail to init row cursor. res = " << res;
380
0
            return res;
381
0
        }
382
0
    }
383
384
    //TODO:check the valid of start_key and end_key.(eg. start_key <= end_key)
385
386
0
    return Status::OK();
387
0
}
388
389
362
Status TabletReader::_init_orderby_keys_param(const ReaderParams& read_params) {
390
362
    SCOPED_RAW_TIMER(&_stats.tablet_reader_init_orderby_keys_param_timer_ns);
391
    // UNIQUE_KEYS will compare all keys as before
392
362
    if (_tablet_schema->keys_type() == DUP_KEYS || (_tablet_schema->keys_type() == UNIQUE_KEYS &&
393
300
                                                    _tablet->enable_unique_key_merge_on_write())) {
394
300
        if (!_tablet_schema->cluster_key_uids().empty()) {
395
0
            if (read_params.read_orderby_key_num_prefix_columns >
396
0
                _tablet_schema->cluster_key_uids().size()) {
397
0
                return Status::Error<ErrorCode::INTERNAL_ERROR>(
398
0
                        "read_orderby_key_num_prefix_columns={} > cluster_keys.size()={}",
399
0
                        read_params.read_orderby_key_num_prefix_columns,
400
0
                        _tablet_schema->cluster_key_uids().size());
401
0
            }
402
0
            for (uint32_t i = 0; i < read_params.read_orderby_key_num_prefix_columns; i++) {
403
0
                auto cid = _tablet_schema->cluster_key_uids()[i];
404
0
                auto index = _tablet_schema->field_index(cid);
405
0
                if (index < 0) {
406
0
                    return Status::Error<ErrorCode::INTERNAL_ERROR>(
407
0
                            "could not find cluster key column with unique_id=" +
408
0
                            std::to_string(cid) +
409
0
                            " in tablet schema, tablet_id=" + std::to_string(_tablet->tablet_id()));
410
0
                }
411
0
                for (uint32_t idx = 0; idx < _return_columns.size(); idx++) {
412
0
                    if (_return_columns[idx] == index) {
413
0
                        _orderby_key_columns.push_back(idx);
414
0
                        break;
415
0
                    }
416
0
                }
417
0
            }
418
300
        } else {
419
            // find index in vector _return_columns
420
            //   for the read_orderby_key_num_prefix_columns orderby keys
421
300
            for (uint32_t i = 0; i < read_params.read_orderby_key_num_prefix_columns; i++) {
422
0
                for (uint32_t idx = 0; idx < _return_columns.size(); idx++) {
423
0
                    if (_return_columns[idx] == i) {
424
0
                        _orderby_key_columns.push_back(idx);
425
0
                        break;
426
0
                    }
427
0
                }
428
0
            }
429
300
        }
430
300
        if (read_params.read_orderby_key_num_prefix_columns != _orderby_key_columns.size()) {
431
0
            return Status::Error<ErrorCode::INTERNAL_ERROR>(
432
0
                    "read_orderby_key_num_prefix_columns != _orderby_key_columns.size, "
433
0
                    "read_params.read_orderby_key_num_prefix_columns={}, "
434
0
                    "_orderby_key_columns.size()={}",
435
0
                    read_params.read_orderby_key_num_prefix_columns, _orderby_key_columns.size());
436
0
        }
437
300
    }
438
439
362
    return Status::OK();
440
362
}
441
442
362
Status TabletReader::_init_conditions_param(const ReaderParams& read_params) {
443
362
    SCOPED_RAW_TIMER(&_stats.tablet_reader_init_conditions_param_timer_ns);
444
362
    std::vector<std::shared_ptr<ColumnPredicate>> predicates;
445
362
    std::copy(read_params.predicates.cbegin(), read_params.predicates.cend(),
446
362
              std::inserter(predicates, predicates.begin()));
447
    // Function filter push down to storage engine
448
362
    auto is_like_predicate = [](std::shared_ptr<ColumnPredicate> _pred) {
449
0
        return dynamic_cast<LikeColumnPredicate<TYPE_CHAR>*>(_pred.get()) != nullptr ||
450
0
               dynamic_cast<LikeColumnPredicate<TYPE_STRING>*>(_pred.get()) != nullptr;
451
0
    };
452
453
362
    for (const auto& filter : read_params.function_filters) {
454
0
        predicates.emplace_back(_parse_to_predicate(filter));
455
0
        auto pred = predicates.back();
456
457
0
        const auto& col = _tablet_schema->column(pred->column_id());
458
0
        const auto* tablet_index = _tablet_schema->get_ngram_bf_index(col.unique_id());
459
0
        if (is_like_predicate(pred) && tablet_index && config::enable_query_like_bloom_filter) {
460
0
            std::unique_ptr<segment_v2::BloomFilter> ng_bf;
461
0
            std::string pattern = pred->get_search_str();
462
0
            auto gram_bf_size = tablet_index->get_gram_bf_size();
463
0
            auto gram_size = tablet_index->get_gram_size();
464
465
0
            RETURN_IF_ERROR(segment_v2::BloomFilter::create(segment_v2::NGRAM_BLOOM_FILTER, &ng_bf,
466
0
                                                            gram_bf_size));
467
0
            NgramTokenExtractor _token_extractor(gram_size);
468
469
0
            if (_token_extractor.string_like_to_bloom_filter(pattern.data(), pattern.length(),
470
0
                                                             *ng_bf)) {
471
0
                pred->set_page_ng_bf(std::move(ng_bf));
472
0
            }
473
0
        }
474
0
    }
475
476
362
    int32_t delete_sign_idx = _tablet_schema->delete_sign_idx();
477
362
    for (auto predicate : predicates) {
478
0
        auto column = _tablet_schema->column(predicate->column_id());
479
0
        if (column.aggregation() != FieldAggregationMethod::OLAP_FIELD_AGGREGATION_NONE) {
480
            // When MOR value predicate pushdown is enabled, drop __DORIS_DELETE_SIGN__
481
            // from storage-layer predicates entirely. Delete sign must only be evaluated
482
            // post-merge via VExpr to prevent deleted rows from reappearing.
483
0
            if (read_params.enable_mor_value_predicate_pushdown && delete_sign_idx >= 0 &&
484
0
                predicate->column_id() == static_cast<uint32_t>(delete_sign_idx)) {
485
0
                continue;
486
0
            }
487
0
            _value_col_predicates.push_back(predicate);
488
0
        } else {
489
0
            _col_predicates.push_back(predicate);
490
0
        }
491
0
    }
492
493
362
    return Status::OK();
494
362
}
495
496
std::shared_ptr<ColumnPredicate> TabletReader::_parse_to_predicate(
497
0
        const FunctionFilter& function_filter) {
498
0
    int32_t index = _tablet_schema->field_index(function_filter._col_name);
499
0
    if (index < 0) {
500
0
        throw Exception(Status::InternalError("Column {} not found in tablet schema",
501
0
                                              function_filter._col_name));
502
0
        return nullptr;
503
0
    }
504
0
    const TabletColumn& column = materialize_column(_tablet_schema->column(index));
505
0
    return create_column_predicate(index, std::make_shared<FunctionFilter>(function_filter),
506
0
                                   column.type(), &column);
507
0
}
508
509
362
Status TabletReader::_init_delete_condition(const ReaderParams& read_params) {
510
362
    SCOPED_RAW_TIMER(&_stats.tablet_reader_init_delete_condition_param_timer_ns);
511
    // If it's cumu and not allow do delete when cumu
512
362
    if (read_params.reader_type == ReaderType::READER_SEGMENT_COMPACTION ||
513
362
        (read_params.reader_type == ReaderType::READER_CUMULATIVE_COMPACTION &&
514
362
         !config::enable_delete_when_cumu_compaction)) {
515
0
        return Status::OK();
516
0
    }
517
362
    bool cumu_delete = read_params.reader_type == ReaderType::READER_CUMULATIVE_COMPACTION &&
518
362
                       config::enable_delete_when_cumu_compaction;
519
    // Delete sign could not be applied when delete on cumu compaction is enabled, bucause it is meant for delete with predicates.
520
    // If delete design is applied on cumu compaction, it will lose effect when doing base compaction.
521
    // `_delete_sign_available` indicates the condition where we could apply delete signs to data.
522
362
    _delete_sign_available = (((read_params.reader_type == ReaderType::READER_BASE_COMPACTION ||
523
362
                                read_params.reader_type == ReaderType::READER_FULL_COMPACTION) &&
524
362
                               config::enable_prune_delete_sign_when_base_compaction) ||
525
362
                              read_params.reader_type == ReaderType::READER_COLD_DATA_COMPACTION ||
526
362
                              read_params.reader_type == ReaderType::READER_CHECKSUM);
527
528
    // `_filter_delete` indicates the condition where we should execlude deleted tuples when reading data.
529
    // However, queries will not use this condition but generate special where predicates to filter data.
530
    // (Though a lille bit confused, it is how the current logic working...)
531
362
    _filter_delete = _delete_sign_available || cumu_delete;
532
362
    return _delete_handler.init(_tablet_schema, read_params.delete_predicates,
533
362
                                read_params.version.second);
534
362
}
535
536
Status TabletReader::init_reader_params_and_create_block(
537
        TabletSharedPtr tablet, ReaderType reader_type,
538
        const std::vector<RowsetSharedPtr>& input_rowsets,
539
0
        TabletReader::ReaderParams* reader_params, Block* block) {
540
0
    reader_params->tablet = tablet;
541
0
    reader_params->reader_type = reader_type;
542
0
    reader_params->version =
543
0
            Version(input_rowsets.front()->start_version(), input_rowsets.back()->end_version());
544
545
0
    TabletReadSource read_source;
546
0
    for (const auto& rowset : input_rowsets) {
547
0
        RowsetReaderSharedPtr rs_reader;
548
0
        RETURN_IF_ERROR(rowset->create_reader(&rs_reader));
549
0
        read_source.rs_splits.emplace_back(std::move(rs_reader));
550
0
    }
551
0
    read_source.fill_delete_predicates();
552
0
    reader_params->set_read_source(std::move(read_source));
553
554
0
    std::vector<RowsetMetaSharedPtr> rowset_metas(input_rowsets.size());
555
0
    std::transform(input_rowsets.begin(), input_rowsets.end(), rowset_metas.begin(),
556
0
                   [](const RowsetSharedPtr& rowset) { return rowset->rowset_meta(); });
557
0
    TabletSchemaSPtr read_tablet_schema =
558
0
            tablet->tablet_schema_with_merged_max_schema_version(rowset_metas);
559
0
    TabletSchemaSPtr merge_tablet_schema = std::make_shared<TabletSchema>();
560
0
    merge_tablet_schema->copy_from(*read_tablet_schema);
561
562
    // Merge the columns in delete predicate that not in latest schema in to current tablet schema
563
0
    for (auto& del_pred : reader_params->delete_predicates) {
564
0
        merge_tablet_schema->merge_dropped_columns(*del_pred->tablet_schema());
565
0
    }
566
0
    reader_params->tablet_schema = merge_tablet_schema;
567
568
0
    reader_params->return_columns.resize(read_tablet_schema->num_columns());
569
0
    std::iota(reader_params->return_columns.begin(), reader_params->return_columns.end(), 0);
570
0
    reader_params->origin_return_columns = &reader_params->return_columns;
571
572
0
    *block = read_tablet_schema->create_block();
573
574
0
    return Status::OK();
575
0
}
576
577
#include "common/compile_check_end.h"
578
} // namespace doris