Coverage Report

Created: 2026-06-18 20:06

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