Coverage Report

Created: 2026-07-13 21:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/scan/file_scanner.h
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
#pragma once
19
20
#include <stddef.h>
21
#include <stdint.h>
22
23
#include <memory>
24
#include <string>
25
#include <unordered_map>
26
#include <unordered_set>
27
#include <vector>
28
29
#include "common/factory_creator.h"
30
#include "common/global_types.h"
31
#include "common/status.h"
32
#include "core/block/block.h"
33
#include "exec/operator/file_scan_operator.h"
34
#include "exprs/vexpr_fwd.h"
35
#include "format/generic_reader.h"
36
#include "format/orc/vorc_reader.h"
37
#include "format/parquet/vparquet_reader.h"
38
#include "format/table/iceberg_reader.h"
39
#include "io/io_common.h"
40
#include "runtime/descriptors.h"
41
#include "runtime/runtime_profile.h"
42
#include "storage/olap_common.h"
43
#include "storage/olap_scan_common.h"
44
#include "storage/segment/adaptive_block_size_predictor.h"
45
#include "storage/segment/condition_cache.h"
46
47
namespace doris {
48
class RuntimeState;
49
class TFileRangeDesc;
50
class TFileScanRange;
51
class TFileScanRangeParams;
52
53
class ShardedKVCache;
54
class VExpr;
55
class VExprContext;
56
} // namespace doris
57
58
namespace doris {
59
60
class FileScanner : public Scanner {
61
    ENABLE_FACTORY_CREATOR(FileScanner);
62
63
public:
64
    static constexpr const char* NAME = "FileScanner";
65
    static constexpr size_t ADAPTIVE_BATCH_INITIAL_PROBE_ROWS = 32;
66
67
    // sub profile name (for parquet/orc)
68
    static const std::string FileReadBytesProfile;
69
    static const std::string FileReadTimeProfile;
70
71
#ifdef BE_TEST
72
    void TEST_init_runtime_filter_partition_prune_ctxs(
73
            const VExprContextSPtrs& conjuncts,
74
1
            const std::unordered_map<SlotId, int>& partition_slot_index_map) {
75
1
        _conjuncts = conjuncts;
76
1
        _partition_slot_index_map = partition_slot_index_map;
77
1
        _init_runtime_filter_partition_prune_ctxs();
78
1
    }
79
1
    const VExprContextSPtrs& TEST_runtime_filter_partition_prune_ctxs() const {
80
1
        return _runtime_filter_partition_prune_ctxs;
81
1
    }
82
#endif
83
84
    FileScanner(RuntimeState* state, FileScanLocalState* parent, int64_t limit,
85
                std::shared_ptr<SplitSourceConnector> split_source, RuntimeProfile* profile,
86
                ShardedKVCache* kv_cache,
87
                const std::unordered_map<std::string, int>* colname_to_slot_id);
88
89
    Status _open_impl(RuntimeState* state) override;
90
91
    Status close(RuntimeState* state) override;
92
93
    void try_stop() override;
94
95
    Status init(RuntimeState* state, const VExprContextSPtrs& conjuncts) override;
96
97
0
    std::string get_name() override { return FileScanner::NAME; }
98
99
1
    std::string get_current_scan_range_name() override { return _current_range_path; }
100
101
    //only used for read one line.
102
    FileScanner(RuntimeState* state, RuntimeProfile* profile, const TFileScanRangeParams* params,
103
                const std::unordered_map<std::string, int>* colname_to_slot_id,
104
                TupleDescriptor* tuple_desc)
105
17
            : Scanner(state, profile),
106
17
              _params(params),
107
17
              _col_name_to_slot_id(colname_to_slot_id),
108
17
              _real_tuple_desc(tuple_desc) {
109
17
        _configure_file_scan_handlers();
110
17
    };
111
112
    Status read_lines_from_range(const TFileRangeDesc& range, const std::list<int64_t>& row_ids,
113
                                 Block* result_block, const ExternalFileMappingInfo& external_info,
114
                                 int64_t* init_reader_ms, int64_t* get_block_ms);
115
116
    Status prepare_for_read_lines(const TFileRangeDesc& range);
117
118
    void update_realtime_counters() override;
119
120
protected:
121
    Status _get_block_impl(RuntimeState* state, Block* block, bool* eof) override;
122
123
    Status _get_block_wrapped(RuntimeState* state, Block* block, bool* eof);
124
125
    Status _get_next_reader();
126
127
    // Build a ReaderInitContext with shared fields from FileScanner members.
128
    void _fill_base_init_context(ReaderInitContext* ctx);
129
130
    // TODO: cast input block columns type to string.
131
0
    Status _cast_src_block(Block* block) { return Status::OK(); }
132
133
    void _collect_profile_before_close() override;
134
135
    bool _should_update_load_counters() const override;
136
137
    // fe will add skip_bitmap_col to _input_tuple_desc iff the target olaptable has skip_bitmap_col
138
    // and the current load is a flexible partial update
139
0
    bool _should_process_skip_bitmap_col() const { return _skip_bitmap_col_idx != -1; }
140
141
protected:
142
    const TFileScanRangeParams* _params = nullptr;
143
    std::shared_ptr<SplitSourceConnector> _split_source;
144
    bool _first_scan_range = false;
145
    TFileRangeDesc _current_range;
146
147
    std::unique_ptr<GenericReader> _cur_reader;
148
    bool _cur_reader_eof = false;
149
    // File source slot descriptors
150
    std::vector<SlotDescriptor*> _file_slot_descs;
151
    // Unified column descriptors for init_reader (includes file, partition, missing, synthesized cols)
152
    std::vector<ColumnDescriptor> _column_descs;
153
154
    // Partition slot id to partition key index (for matching columns_from_path)
155
    std::unordered_map<SlotId, int> _partition_slot_index_map;
156
    // created from param.expr_of_dest_slot
157
    // For query, it saves default value expr of all dest columns, or nullptr for NULL.
158
    // For load, it saves conversion expr/default value of all dest columns.
159
    VExprContextSPtrs _dest_vexpr_ctx;
160
    // dest slot name to index in _dest_vexpr_ctx;
161
    std::unordered_map<std::string, int> _dest_slot_name_to_idx;
162
    // col name to default value expr
163
    // TODO: only used by json reader. Could we delete this?
164
    std::unordered_map<std::string, VExprContextSPtr> _col_default_value_ctx;
165
    // the map values of dest slot id to src slot desc
166
    // if there is not key of dest slot id in dest_sid_to_src_sid_without_trans, it will be set to nullptr
167
    std::vector<SlotDescriptor*> _src_slot_descs_order_by_dest;
168
    // dest slot desc index to src slot desc index
169
    std::unordered_map<int, int> _dest_slot_to_src_slot_index;
170
171
    std::unordered_map<std::string, uint32_t> _src_block_name_to_idx;
172
173
    // Get from GenericReader, save the existing columns in file to their type.
174
    std::unordered_map<std::string, DataTypePtr> _slot_lower_name_to_col_type;
175
    // Get from GenericReader, save columns that required by scan but not exist in file.
176
177
    // The col lowercase name of source file to type of source file.
178
    std::map<std::string, DataTypePtr> _source_file_col_name_types;
179
180
    // For load task
181
    VExprContextSPtrs _pre_conjunct_ctxs;
182
    std::unique_ptr<RowDescriptor> _src_row_desc;
183
    std::unique_ptr<RowDescriptor> _dest_row_desc;
184
    // row desc for default exprs
185
    std::unique_ptr<RowDescriptor> _default_val_row_desc;
186
    // owned by scan node
187
    ShardedKVCache* _kv_cache = nullptr;
188
189
    std::set<TSlotId> _is_file_slot;
190
    bool _scanner_eof = false;
191
    int _rows = 0;
192
    int _num_of_columns_from_file;
193
194
    bool _src_block_mem_reuse = false;
195
    bool _strict_mode;
196
197
    bool _src_block_init = false;
198
    Block* _src_block_ptr = nullptr;
199
    Block _src_block;
200
201
    VExprContextSPtrs _push_down_conjuncts;
202
    VExprContextSPtrs _runtime_filter_partition_prune_ctxs;
203
    Block _runtime_filter_partition_prune_block;
204
205
    std::unique_ptr<io::FileCacheStatistics> _file_cache_statistics;
206
    std::unique_ptr<io::FileReaderStats> _file_reader_stats;
207
    std::shared_ptr<io::IOContext> _io_ctx;
208
209
    // Whether to fill partition columns from path, default is true.
210
    std::unordered_map<std::string, std::tuple<std::string, const SlotDescriptor*>>
211
            _partition_col_descs;
212
    std::unordered_map<std::string, bool> _partition_value_is_null;
213
214
    // idx of skip_bitmap_col in _input_tuple_desc
215
    int32_t _skip_bitmap_col_idx {-1};
216
    int32_t _sequence_map_col_uid {-1};
217
    int32_t _sequence_col_uid {-1};
218
219
private:
220
    RuntimeProfile::Counter* _get_block_timer = nullptr;
221
    RuntimeProfile::Counter* _cast_to_input_block_timer = nullptr;
222
    RuntimeProfile::Counter* _fill_missing_columns_timer = nullptr;
223
    RuntimeProfile::Counter* _pre_filter_timer = nullptr;
224
    RuntimeProfile::Counter* _convert_to_output_block_timer = nullptr;
225
    RuntimeProfile::Counter* _runtime_filter_partition_prune_timer = nullptr;
226
    RuntimeProfile::Counter* _empty_file_counter = nullptr;
227
    RuntimeProfile::Counter* _not_found_file_counter = nullptr;
228
    RuntimeProfile::Counter* _fully_skipped_file_counter = nullptr;
229
    RuntimeProfile::Counter* _file_counter = nullptr;
230
    RuntimeProfile::Counter* _file_read_bytes_counter = nullptr;
231
    RuntimeProfile::Counter* _file_read_calls_counter = nullptr;
232
    RuntimeProfile::Counter* _file_read_time_counter = nullptr;
233
    RuntimeProfile::Counter* _runtime_filter_partition_pruned_range_counter = nullptr;
234
    RuntimeProfile::Counter* _adaptive_batch_predicted_rows_counter = nullptr;
235
    RuntimeProfile::Counter* _adaptive_batch_actual_bytes_before_truncate_counter = nullptr;
236
    RuntimeProfile::Counter* _adaptive_batch_actual_bytes_after_truncate_counter = nullptr;
237
    RuntimeProfile::Counter* _adaptive_batch_probe_count_counter = nullptr;
238
239
    const std::unordered_map<std::string, int>* _col_name_to_slot_id = nullptr;
240
    // single slot filter conjuncts
241
    std::unordered_map<int, VExprContextSPtrs> _slot_id_to_filter_conjuncts;
242
    // not single(zero or multi) slot filter conjuncts
243
    VExprContextSPtrs _not_single_slot_filter_conjuncts;
244
    // save the path of current scan range
245
    std::string _current_range_path = "";
246
247
    // Only for load scan node.
248
    const TupleDescriptor* _input_tuple_desc = nullptr;
249
    // If _input_tuple_desc is set,
250
    // the _real_tuple_desc will point to _input_tuple_desc,
251
    // otherwise, point to _output_tuple_desc
252
    const TupleDescriptor* _real_tuple_desc = nullptr;
253
254
    int64_t _last_bytes_read_from_local = 0;
255
    int64_t _last_bytes_read_from_remote = 0;
256
257
    Status (FileScanner::*_init_src_block_handler)(Block* block) = nullptr;
258
    Status (FileScanner::*_process_src_block_after_read_handler)(Block* block) = nullptr;
259
    bool (FileScanner::*_should_push_down_predicates_handler)(
260
            TFileFormatType::type format_type) const = nullptr;
261
    bool (FileScanner::*_should_enable_condition_cache_handler)() const = nullptr;
262
263
    // Condition cache for external tables
264
    uint64_t _condition_cache_digest = 0;
265
    segment_v2::ConditionCache::ExternalCacheKey _condition_cache_key;
266
    std::shared_ptr<std::vector<bool>> _condition_cache;
267
    std::shared_ptr<ConditionCacheContext> _condition_cache_ctx;
268
    int64_t _condition_cache_hit_count = 0;
269
    std::unique_ptr<AdaptiveBlockSizePredictor> _block_size_predictor;
270
271
    void _configure_file_scan_handlers();
272
273
    Status _init_expr_ctxes();
274
    Status _init_src_block(Block* block);
275
    Status _init_src_block_for_load(Block* block);
276
    Status _init_src_block_for_query(Block* block);
277
    Status _process_src_block_after_read(Block* block);
278
    Status _process_src_block_after_read_for_load(Block* block);
279
    Status _process_src_block_after_read_for_query(Block* block);
280
    Status _check_output_block_types();
281
    Status _cast_to_input_block(Block* block);
282
    Status _pre_filter_src_block();
283
    Status _convert_to_output_block(Block* block);
284
    Status _truncate_char_or_varchar_columns(Block* block);
285
    void _truncate_char_or_varchar_column(Block* block, int idx, int len);
286
    Status _generate_partition_columns();
287
288
    bool _check_partition_prune_expr(const VExprSPtr& expr);
289
    bool _contains_runtime_filter(const VExprContextSPtrs& conjuncts) const;
290
    void _init_runtime_filter_partition_prune_ctxs();
291
    void _init_runtime_filter_partition_prune_block();
292
    Status _process_runtime_filters_partition_prune(bool& is_partition_pruned);
293
    Status _process_conjuncts();
294
    Status _process_late_arrival_conjuncts();
295
    void _get_slot_ids(VExpr* expr, std::vector<int>* slot_ids);
296
    Status _generate_truncate_columns(bool need_to_get_parsed_schema);
297
    Status _set_fill_or_truncate_columns(bool need_to_get_parsed_schema);
298
    Status _init_orc_reader(FileMetaCache* file_meta_cache_ptr,
299
                            std::unique_ptr<OrcReader> orc_reader = nullptr);
300
    Status _init_parquet_reader(FileMetaCache* file_meta_cache_ptr,
301
                                std::unique_ptr<ParquetReader> parquet_reader = nullptr);
302
    std::shared_ptr<segment_v2::RowIdColumnIteratorV2> _create_row_id_column_iterator();
303
304
26
    TFileFormatType::type _get_current_format_type() {
305
        // for compatibility, if format_type is not set in range, use the format type of params
306
26
        const TFileRangeDesc& range = _current_range;
307
26
        return range.__isset.format_type ? range.format_type : _params->format_type;
308
26
    };
309
310
18
    Status _init_io_ctx() {
311
18
        _io_ctx = std::make_shared<io::IOContext>();
312
18
        _io_ctx->query_id = &_state->query_id();
313
18
        return Status::OK();
314
18
    };
315
316
0
    void _reset_counter() {
317
0
        _counter.num_rows_unselected = 0;
318
0
        _counter.num_rows_filtered = 0;
319
0
    }
320
321
    bool _should_enable_condition_cache();
322
    bool _should_enable_condition_cache_for_load() const;
323
    bool _should_enable_condition_cache_for_query() const;
324
    bool _should_push_down_predicates(TFileFormatType::type format_type) const;
325
    bool _should_push_down_predicates_for_load(TFileFormatType::type format_type) const;
326
    bool _should_push_down_predicates_for_query(TFileFormatType::type format_type) const;
327
    void _init_reader_condition_cache();
328
    void _finalize_reader_condition_cache();
329
    void _reset_adaptive_batch_size_state();
330
    void _init_adaptive_batch_size_state(TFileFormatType::type format_type);
331
    bool _should_enable_adaptive_batch_size(TFileFormatType::type format_type) const;
332
    bool _should_run_adaptive_batch_size() const;
333
    size_t _predict_reader_batch_rows();
334
    void _update_adaptive_batch_size_before_truncate(const Block& block);
335
    void _update_adaptive_batch_size_after_truncate(const Block& block);
336
337
51
    TPushAggOp::type _get_push_down_agg_type() const {
338
51
        return _local_state == nullptr ? TPushAggOp::type::NONE
339
51
                                       : _local_state->get_push_down_agg_type();
340
51
    }
341
342
    // enable the file meta cache only when
343
    // 1. max_external_file_meta_cache_num is > 0
344
    // 2. the file number is less than 1/3 of cache's capacibility
345
    // Otherwise, the cache miss rate will be high
346
0
    bool _should_enable_file_meta_cache() {
347
0
        return ExecEnv::GetInstance()->file_meta_cache()->enabled() &&
348
0
               _split_source->num_scan_ranges() < config::max_external_file_meta_cache_num / 3;
349
0
    }
350
};
351
} // namespace doris