Coverage Report

Created: 2026-04-14 10:07

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