Coverage Report

Created: 2025-12-11 01:17

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