Coverage Report

Created: 2026-07-14 18:43

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