Coverage Report

Created: 2026-07-24 05:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/scan/file_scanner_v2.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 <map>
21
#include <memory>
22
#include <optional>
23
#include <string>
24
#include <unordered_map>
25
#include <vector>
26
27
#include "common/factory_creator.h"
28
#include "common/status.h"
29
#include "core/block/block.h"
30
#include "exec/operator/file_scan_operator.h"
31
#include "exec/scan/scanner.h"
32
#include "exec/scan/split_source_connector.h"
33
#include "exprs/vexpr_fwd.h"
34
#include "format_v2/column_mapper.h"
35
#include "format_v2/table_reader.h"
36
#include "gen_cpp/Descriptors_types.h"
37
#include "gen_cpp/PlanNodes_types.h"
38
#include "io/io_common.h"
39
#include "runtime/runtime_profile.h"
40
#include "storage/segment/adaptive_block_size_predictor.h"
41
42
namespace doris {
43
44
class RuntimeState;
45
class SlotDescriptor;
46
class TFileRangeDesc;
47
class TFileScanRangeParams;
48
class ShardedKVCache;
49
50
class FileScannerV2 final : public Scanner {
51
    ENABLE_FACTORY_CREATOR(FileScannerV2);
52
53
public:
54
    static constexpr const char* NAME = "FileScannerV2";
55
    static constexpr size_t ADAPTIVE_BATCH_INITIAL_PROBE_ROWS = 32;
56
57
    struct RealtimeCounterDeltas {
58
        int64_t scan_rows = 0;
59
        int64_t scan_bytes = 0;
60
        int64_t scan_bytes_from_local_storage = 0;
61
        int64_t scan_bytes_from_remote_storage = 0;
62
    };
63
64
    enum class UncachedReaderBytesStorage { LOCAL, REMOTE, NONE };
65
66
    static bool is_supported(const TFileScanRangeParams& params, const TFileRangeDesc& range);
67
#ifdef BE_TEST
68
    FileScannerV2(RuntimeState* state, RuntimeProfile* profile,
69
                  std::unique_ptr<format::TableReader> table_reader);
70
    static Status TEST_validate_scan_range(const TFileScanRangeParams& params,
71
                                           const TFileRangeDesc& range);
72
    static Status TEST_to_file_format(TFileFormatType::type format_type,
73
                                      format::FileFormat* file_format);
74
    static bool TEST_is_partition_slot(const TFileScanSlotInfo& slot_info,
75
                                       const std::string& column_name);
76
    static bool TEST_is_data_file_slot(const TFileScanSlotInfo& slot_info,
77
                                       const std::string& column_name);
78
    static Status TEST_rewrite_slot_refs_to_global_index(
79
            VExprSPtr* expr,
80
            const std::unordered_map<int32_t, format::GlobalIndex>& slot_id_to_global_index);
81
    static RealtimeCounterDeltas TEST_collect_realtime_counter_deltas(
82
            const io::FileReaderStats& file_reader_stats,
83
            const io::FileCacheStatistics& file_cache_statistics,
84
            UncachedReaderBytesStorage uncached_reader_bytes_storage, int64_t* last_read_bytes,
85
            int64_t* last_read_rows, int64_t* last_bytes_read_from_local,
86
            int64_t* last_bytes_read_from_remote);
87
    static void TEST_report_file_cache_profile(
88
            RuntimeProfile* profile, const io::FileCacheStatistics& file_cache_statistics);
89
    static bool TEST_should_skip_not_found(const Status& status, bool ignore_not_found);
90
    static bool TEST_should_skip_empty(const Status& status, bool stopped);
91
    static bool TEST_should_run_adaptive_batch_size(bool predictor_initialized,
92
                                                    bool current_split_uses_metadata_count) {
93
        return _should_run_adaptive_batch_size(predictor_initialized,
94
                                               current_split_uses_metadata_count);
95
    }
96
    void TEST_set_scanner_conjuncts(VExprContextSPtrs conjuncts) {
97
        _conjuncts = std::move(conjuncts);
98
        _initialize_scanner_residual_conjuncts();
99
    }
100
    Status TEST_filter_output_block(Block* block) { return _filter_output_block(block); }
101
    size_t TEST_table_reader_owned_conjunct_count() const {
102
        return _table_reader_owned_conjunct_count;
103
    }
104
    size_t TEST_scanner_residual_conjunct_count() const {
105
        return _scanner_residual_conjuncts.size();
106
    }
107
#endif
108
109
    FileScannerV2(RuntimeState* state, FileScanLocalState* parent, int64_t limit,
110
                  std::shared_ptr<SplitSourceConnector> split_source, RuntimeProfile* profile,
111
                  ShardedKVCache* kv_cache,
112
                  const std::unordered_map<std::string, int>* colname_to_slot_id);
113
114
    Status init(RuntimeState* state, const VExprContextSPtrs& conjuncts) override;
115
    Status _open_impl(RuntimeState* state) override;
116
    Status close(RuntimeState* state) override;
117
    void try_stop() override;
118
0
    std::string get_name() override { return FileScannerV2::NAME; }
119
0
    std::string get_current_scan_range_name() override { return _current_range_path; }
120
    void update_realtime_counters() override;
121
122
protected:
123
    Status _get_block_impl(RuntimeState* state, Block* block, bool* eof) override;
124
    Status _filter_output_block(Block* block) override;
125
    size_t _last_block_rows_read(const Block& block) const override;
126
    size_t _last_block_bytes_read(const Block& block) const override;
127
    void _collect_profile_before_close() override;
128
    bool _should_update_load_counters() const override;
129
130
private:
131
    static Status _validate_scan_range(const TFileScanRangeParams& params,
132
                                       const TFileRangeDesc& range);
133
    Status _get_next_scan_range(bool* has_next);
134
    TFileFormatType::type _get_current_format_type() const;
135
    Status _init_io_ctx();
136
    Status _init_expr_ctxes();
137
    Status _prepare_next_split(bool* eos);
138
    Status _init_table_reader(const TFileRangeDesc& range);
139
    Status _create_table_reader_for_format(const TFileRangeDesc& range,
140
                                           std::unique_ptr<format::TableReader>* reader) const;
141
    Status _prepare_table_reader_split(const TFileRangeDesc& range,
142
                                       std::map<std::string, Field> partition_values);
143
    static bool _should_skip_not_found(const Status& status, bool ignore_not_found);
144
    static bool _should_skip_empty(const Status& status, bool stopped);
145
    bool _should_enable_file_meta_cache() const;
146
    std::optional<format::GlobalRowIdContext> _create_global_rowid_context(
147
            const TFileRangeDesc& range) const;
148
    Status _generate_partition_values(const TFileRangeDesc& range,
149
                                      std::map<std::string, Field>* partition_values) const;
150
    Status _parse_partition_value(const SlotDescriptor* slot_desc, const std::string& value,
151
                                  bool is_null, Field* field) const;
152
    Status _build_projected_columns(const format::TableReader& table_reader);
153
    Status _build_default_expr(const TFileScanSlotInfo& slot_info, VExprContextSPtr* ctx) const;
154
    static format::ColumnDefinition _build_table_column(const SlotDescriptor* slot_desc);
155
    Status _build_table_conjuncts(VExprContextSPtrs* conjuncts) const;
156
    Status _build_table_conjuncts(const VExprContextSPtrs& source,
157
                                  VExprContextSPtrs* conjuncts) const;
158
    Status _sync_table_reader_conjuncts();
159
    static size_t _safe_conjunct_prefix_size(const VExprContextSPtrs& conjuncts);
160
    void _initialize_scanner_residual_conjuncts();
161
    void _refresh_scanner_residual_profile();
162
    static Status _to_file_format(TFileFormatType::type format_type,
163
                                  format::FileFormat* file_format);
164
    void _reset_adaptive_batch_size_state();
165
    void _init_adaptive_batch_size_state(TFileFormatType::type format_type);
166
    bool _should_enable_adaptive_batch_size(TFileFormatType::type format_type) const;
167
    bool _should_run_adaptive_batch_size() const;
168
    static bool _should_run_adaptive_batch_size(bool predictor_initialized,
169
                                                bool current_split_uses_metadata_count);
170
    size_t _predict_reader_batch_rows();
171
    void _update_adaptive_batch_size(const Block& block);
172
    static RealtimeCounterDeltas _collect_realtime_counter_deltas(
173
            const io::FileReaderStats& file_reader_stats,
174
            const io::FileCacheStatistics& file_cache_statistics,
175
            UncachedReaderBytesStorage uncached_reader_bytes_storage, int64_t* last_read_bytes,
176
            int64_t* last_read_rows, int64_t* last_bytes_read_from_local,
177
            int64_t* last_bytes_read_from_remote);
178
    static UncachedReaderBytesStorage _uncached_reader_bytes_storage(TFileType::type file_type);
179
    static void _report_file_cache_profile(RuntimeProfile* profile,
180
                                           const io::FileCacheStatistics& file_cache_statistics);
181
    void _report_file_reader_predicate_filtered_rows();
182
    void _report_condition_cache_profile();
183
184
    struct PartitionSlotInfo {
185
        const SlotDescriptor* slot_desc = nullptr;
186
        std::string canonical_name;
187
    };
188
189
    const TFileScanRangeParams* _params = nullptr;
190
    std::shared_ptr<SplitSourceConnector> _split_source;
191
    bool _first_scan_range = false;
192
    bool _has_prepared_split = false;
193
    TFileRangeDesc _current_range;
194
    std::string _current_range_path;
195
196
    std::unique_ptr<format::TableReader> _table_reader;
197
    size_t _table_reader_owned_conjunct_count = 0;
198
    // Scanner owns one persistent context vector for the first unsafe conjunct and every later
199
    // conjunct. Hybrid child readers may be recreated or switched, but this state must not be.
200
    VExprContextSPtrs _scanner_residual_conjuncts;
201
    std::vector<format::ColumnDefinition> _projected_columns;
202
    // File formats without embedded schema, such as CSV, still need the FE slot descriptors in
203
    // file-column order. This mirrors old FileScanner::_file_slot_descs and is passed only to
204
    // readers that cannot derive their schema from file metadata.
205
    std::vector<SlotDescriptor*> _file_slot_descs;
206
    bool _need_global_rowid_column = false;
207
    std::unordered_map<int32_t, const SlotDescriptor*> _slot_id_to_desc;
208
    std::unordered_map<int32_t, format::GlobalIndex> _slot_id_to_global_index;
209
    std::unordered_map<std::string, PartitionSlotInfo> _partition_slot_descs;
210
211
    std::unique_ptr<io::FileCacheStatistics> _file_cache_statistics;
212
    io::FileCacheStatistics _reported_file_cache_statistics;
213
    std::unique_ptr<io::FileReaderStats> _file_reader_stats;
214
    std::shared_ptr<io::IOContext> _io_ctx;
215
    ShardedKVCache* _kv_cache = nullptr;
216
217
    RuntimeProfile::Counter* _scanner_total_timer = nullptr;
218
    RuntimeProfile::Counter* _init_timer = nullptr;
219
    RuntimeProfile::Counter* _open_timer = nullptr;
220
    RuntimeProfile::Counter* _get_block_timer = nullptr;
221
    RuntimeProfile::Counter* _empty_file_counter = nullptr;
222
    RuntimeProfile::Counter* _prepare_split_timer = nullptr;
223
    RuntimeProfile::Counter* _get_next_range_timer = nullptr;
224
    RuntimeProfile::Counter* _close_timer = nullptr;
225
    RuntimeProfile::Counter* _io_timer = nullptr;
226
    RuntimeProfile::Counter* _not_found_file_counter = nullptr;
227
    RuntimeProfile::Counter* _file_counter = nullptr;
228
    RuntimeProfile::Counter* _file_read_bytes_counter = nullptr;
229
    RuntimeProfile::Counter* _file_read_calls_counter = nullptr;
230
    RuntimeProfile::Counter* _file_read_time_counter = nullptr;
231
    RuntimeProfile::Counter* _adaptive_batch_predicted_rows_counter = nullptr;
232
    RuntimeProfile::Counter* _adaptive_batch_actual_bytes_counter = nullptr;
233
    RuntimeProfile::Counter* _adaptive_batch_probe_count_counter = nullptr;
234
    RuntimeProfile::Counter* _scanner_residual_filter_timer = nullptr;
235
    RuntimeProfile::Counter* _scanner_residual_rows_filtered_counter = nullptr;
236
    RuntimeProfile* _scanner_profile = nullptr;
237
    std::unique_ptr<AdaptiveBlockSizePredictor> _block_size_predictor;
238
    int64_t _reported_predicate_filtered_rows = 0;
239
    int64_t _reported_condition_cache_hit_count = 0;
240
    int64_t _reported_condition_cache_filtered_rows = 0;
241
    int64_t _last_read_bytes = 0;
242
    int64_t _last_read_rows = 0;
243
    int64_t _last_bytes_read_from_local = 0;
244
    int64_t _last_bytes_read_from_remote = 0;
245
    int64_t _reported_io_read_time = 0;
246
    int _table_reader_applied_rf_num = 0;
247
};
248
249
} // namespace doris