Coverage Report

Created: 2026-08-06 19:50

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