Coverage Report

Created: 2026-07-05 19:28

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