Coverage Report

Created: 2026-01-01 04:10

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