Coverage Report

Created: 2026-07-17 22:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format_v2/parquet/parquet_scan.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
//   http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing,
10
// software distributed under the License is distributed on an
11
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
12
// KIND, either express or implied.  See the License for the
13
// specific language governing permissions and limitations
14
// under the License.
15
16
#pragma once
17
18
#include <gen_cpp/parquet_types.h>
19
20
#include <cstddef>
21
#include <cstdint>
22
#include <map>
23
#include <memory>
24
#include <optional>
25
#include <unordered_map>
26
#include <utility>
27
#include <vector>
28
29
#include "common/status.h"
30
#include "core/column/column.h"
31
#include "format_v2/file_reader.h"
32
#include "format_v2/parquet/parquet_profile.h"
33
#include "format_v2/parquet/parquet_statistics.h"
34
#include "format_v2/parquet/reader/column_reader.h"
35
#include "format_v2/parquet/selection_vector.h"
36
#include "runtime/runtime_profile.h"
37
#include "storage/segment/condition_cache.h"
38
39
namespace parquet {
40
class FileMetaData;
41
class ParquetFileReader;
42
class RowGroupMetaData;
43
class RowGroupReader;
44
} // namespace parquet
45
46
namespace cctz {
47
class time_zone;
48
} // namespace cctz
49
50
namespace doris {
51
class Block;
52
class RuntimeState;
53
54
namespace format {
55
struct FileScanRequest;
56
} // namespace format
57
} // namespace doris
58
59
namespace doris::format::parquet {
60
61
struct ParquetFileContext;
62
struct ParquetColumnSchema;
63
64
namespace detail {
65
struct PredicateConjunctSchedule {
66
    std::map<size_t, VExprContextSPtrs> single_column_conjuncts;
67
    VExprContextSPtrs remaining_conjuncts;
68
};
69
70
struct AdaptivePredicateStats {
71
    double cost_per_input_row_ns = 0;
72
    double survival_ratio = 1;
73
    size_t samples = 0;
74
};
75
76
std::vector<size_t> order_adaptive_predicates(
77
        const std::vector<size_t>& positions,
78
        const std::unordered_map<size_t, AdaptivePredicateStats>& stats);
79
std::vector<size_t> adaptive_prefetch_prefix(
80
        const std::vector<size_t>& ordered_positions,
81
        const std::unordered_map<size_t, AdaptivePredicateStats>& stats,
82
        double minimum_reach_probability);
83
bool should_sample_adaptive_predicate(size_t samples, size_t batch_sequence);
84
} // namespace detail
85
86
// ============================================================================
87
// ============================================================================
88
89
struct ParquetScanRange {
90
    int64_t start_offset = 0;
91
    int64_t size = -1;      // -1 means read the whole file
92
    int64_t file_size = -1; // -1 means unknown
93
};
94
95
struct RowGroupReadPlan {
96
    int row_group_id = -1;                 // row group id
97
    int64_t first_file_row = 0;            // first file row for this row group (0-based)
98
    int64_t row_group_rows = 0;            // row count of this row group
99
    std::vector<RowRange> selected_ranges; // row ranges to read after page-index pruning
100
    std::map<int, ParquetPageSkipPlan>
101
            page_skip_plans; // leaf_column_id -> data pages that can be skipped completely
102
    // Native planning already parsed these indexes. Transfer them to execution so narrowed scans
103
    // do not issue the same remote index reads a second time while opening the row group.
104
    std::unordered_map<int, tparquet::OffsetIndex> offset_indexes;
105
};
106
107
struct RowGroupScanPlan {
108
    std::vector<RowGroupReadPlan> row_groups; // row groups selected after pruning
109
    ParquetPruningStats pruning_stats;        // pruning statistics
110
};
111
112
// ============================================================================
113
// ============================================================================
114
115
Status plan_parquet_row_groups(const ::parquet::FileMetaData& metadata,
116
                               ::parquet::ParquetFileReader* file_reader,
117
                               const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema,
118
                               const format::FileScanRequest& request,
119
                               const ParquetScanRange& scan_range, bool enable_bloom_filter,
120
                               RowGroupScanPlan* plan, const cctz::time_zone* timezone = nullptr,
121
                               const RuntimeState* runtime_state = nullptr,
122
                               ParquetFileContext* file_context = nullptr);
123
124
IColumn::Filter selection_to_filter(const SelectionVector& selection, uint16_t selected_rows,
125
                                    int64_t batch_rows);
126
127
uint16_t apply_compact_filter_to_selection(const IColumn::Filter& filter,
128
                                           SelectionVector* selection, uint16_t selected_rows);
129
130
Status execute_batch_filters(const format::FileScanRequest& request, int64_t batch_rows,
131
                             Block* file_block, SelectionVector* selection, uint16_t* selected_rows,
132
                             int64_t* conjunct_filtered_rows = nullptr);
133
134
// ============================================================================
135
// ============================================================================
136
//   while true:
137
//     3. read_current_row_group_batch(batch_rows)
138
// ============================================================================
139
class ParquetScanScheduler {
140
public:
141
    static constexpr int64_t DEFAULT_READ_BATCH_SIZE = 4096;
142
143
    void set_plan(RowGroupScanPlan plan);
144
171
    void set_page_skip_profile(ParquetPageSkipProfile page_skip_profile) {
145
171
        _page_skip_profile = page_skip_profile;
146
171
    }
147
171
    void set_scan_profile(ParquetScanProfile scan_profile) { _scan_profile = scan_profile; }
148
175
    void set_merge_read_options(RuntimeProfile* profile, int64_t merge_read_slice_size) {
149
175
        _profile = profile;
150
175
        _merge_read_slice_size = merge_read_slice_size;
151
175
    }
152
171
    void set_global_rowid_context(std::optional<format::GlobalRowIdContext> context) {
153
171
        _global_rowid_context = context;
154
171
    }
155
    void set_condition_cache_context(std::shared_ptr<ConditionCacheContext> ctx);
156
175
    void set_timezone(const cctz::time_zone* timezone) { _timezone = timezone; }
157
175
    void set_enable_strict_mode(bool enable_strict_mode) {
158
175
        _enable_strict_mode = enable_strict_mode;
159
175
    }
160
175
    void set_runtime_state(RuntimeState* runtime_state) { _runtime_state = runtime_state; }
161
    // Release row-group readers before the owning RuntimeProfile is reported. Native readers
162
    // publish their accumulated page/decode statistics from their destructor.
163
104
    void close() { reset_current_row_group(); }
164
    // Upper scanner owns adaptive memory feedback; scheduler only applies the current row cap when
165
    // splitting selected row ranges into physical read batches.
166
180
    void set_batch_size(size_t batch_size) {
167
180
        _batch_size = batch_size == 0 ? 1 : static_cast<int64_t>(batch_size);
168
180
    }
169
    void reset();
170
172
    bool empty() const { return _row_group_plans.empty(); }
171
2
    int64_t condition_cache_filtered_rows() const { return _condition_cache_filtered_rows; }
172
365
    int64_t predicate_filtered_rows() const { return _predicate_filtered_rows; }
173
518
    int64_t raw_rows_read() const { return _raw_rows_read; }
174
175
    Status read_next_batch(ParquetFileContext& file_context,
176
                           const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema,
177
                           const format::FileScanRequest& request, Block* file_block, size_t* rows,
178
                           bool* eof);
179
180
private:
181
    static constexpr size_t PROFILE_FLUSH_BATCH_INTERVAL = 16;
182
183
    void reset_current_row_group();
184
    void flush_current_reader_profiles();
185
    const detail::PredicateConjunctSchedule& predicate_conjunct_schedule(
186
            const format::FileScanRequest& request);
187
    std::vector<format::LocalColumnIndex> adaptive_predicate_prefetch_columns(
188
            const format::FileScanRequest& request) const;
189
190
    Status open_next_row_group(ParquetFileContext& file_context,
191
                               const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema,
192
                               const format::FileScanRequest& request, bool* has_row_group);
193
194
    Status skip_current_row_group_rows(int64_t rows);
195
    Status flush_pending_non_predicate_skip_rows();
196
197
    Status read_filter_columns(int64_t batch_rows, const format::FileScanRequest& request,
198
                               Block* file_block, SelectionVector* selection,
199
                               uint16_t* selected_rows, int64_t* conjunct_filtered_rows,
200
                               bool* predicate_columns_filtered);
201
202
    Status prepare_current_dictionary_filters(
203
            ParquetFileContext& file_context,
204
            const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema,
205
            const format::FileScanRequest& request, int row_group_idx,
206
            const ::parquet::RowGroupMetaData& row_group_metadata);
207
208
    void prefetch_current_row_group_columns(
209
            ParquetFileContext& file_context,
210
            const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema,
211
            const std::vector<format::LocalColumnIndex>& scan_columns, bool* prefetched);
212
213
    Status read_current_row_group_batch(
214
            ParquetFileContext& file_context,
215
            const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema,
216
            int64_t batch_rows, const format::FileScanRequest& request,
217
            int64_t batch_first_file_row, Block* file_block, size_t* rows);
218
219
    void mark_condition_cache_granules(const SelectionVector& selection, uint16_t selected_rows,
220
                                       int64_t batch_first_file_row);
221
222
    std::vector<RowGroupReadPlan> _row_group_plans; // row group queue to scan
223
    size_t _next_row_group_plan_idx = 0;            // index of the next row group to process
224
225
    bool _has_current_row_group = false;
226
    // Readers retain pointers into this immutable row-group map, so it must outlive both maps below.
227
    std::unordered_map<int, tparquet::OffsetIndex> _current_offset_indexes;
228
    std::map<ColumnId, std::unique_ptr<ParquetColumnReader>>
229
            _current_predicate_columns; // predicate ColumnReaders
230
    std::map<ColumnId, std::unique_ptr<ParquetColumnReader>>
231
            _current_non_predicate_columns; // non-predicate ColumnReaders
232
    std::map<ColumnId, IColumn::Filter>
233
            _current_dictionary_filters; // local id -> dict entry bitmap
234
    std::map<ColumnId, std::vector<std::pair<VExprContextSPtr, VExprSPtr>>>
235
            _current_dictionary_residual_conjuncts; // local id -> row-level residual conjuncts
236
    int64_t _current_row_group_rows = 0;            // current row group row count
237
    int _current_row_group_id = -1;                 // current row group id in parquet metadata
238
    int64_t _current_row_group_rows_read = 0;       // rows read in the current row group (cursor)
239
    int64_t _current_row_group_first_row = 0;       // first file row of the current row group
240
    std::vector<RowRange>
241
            _current_selected_ranges; // selected ranges for the current row group after page-index pruning
242
    size_t _current_range_idx = 0;        // current selected_range index
243
    int64_t _current_range_rows_read = 0; // rows read in the current range
244
    // Predicate readers move immediately because they decide which rows survive. Non-predicate
245
    // readers can lag behind across fully filtered batches and range gaps; the lag is flushed once
246
    // before the next surviving batch is materialized, or discarded with the row group.
247
    int64_t _pending_non_predicate_skip_rows = 0;
248
249
    bool _current_predicate_prefetched = false;
250
    bool _current_non_predicate_prefetched = false;
251
    bool _current_merge_range_active = false;
252
    ParquetPageSkipProfile _page_skip_profile;
253
    ParquetScanProfile _scan_profile;
254
    RuntimeProfile* _profile = nullptr;
255
    int64_t _merge_read_slice_size = -1;
256
    std::optional<format::GlobalRowIdContext> _global_rowid_context;
257
    const cctz::time_zone* _timezone = nullptr;
258
    bool _enable_strict_mode = false;
259
    RuntimeState* _runtime_state = nullptr;
260
    int64_t _batch_size = DEFAULT_READ_BATCH_SIZE;
261
    // Batch control scratch is scheduler-owned so adaptive row caps change logical sizes without
262
    // reallocating selection indices, dense filter bytes, or compacted-column positions.
263
    SelectionVector _selection;
264
    std::vector<uint32_t> _read_column_positions_scratch;
265
    const format::FileScanRequest* _predicate_schedule_request = nullptr;
266
    detail::PredicateConjunctSchedule _predicate_schedule;
267
    std::vector<size_t> _predicate_positions_scratch;
268
    std::unordered_map<size_t, size_t> _predicate_indices_by_position_scratch;
269
    std::vector<size_t> _ordered_predicate_positions_scratch;
270
    std::unordered_map<uint32_t, std::vector<SelectionVector::Index>>
271
            _predicate_column_selection_scratch;
272
    IColumn::Filter _predicate_compaction_filter_scratch;
273
    size_t _predicate_batch_sequence = 0;
274
    size_t _batches_since_profile_flush = 0;
275
    std::unordered_map<size_t, detail::AdaptivePredicateStats> _predicate_runtime_stats;
276
    double _predicate_survival_ratio = -1;
277
    std::shared_ptr<ConditionCacheContext> _condition_cache_ctx;
278
    int64_t _condition_cache_filtered_rows = 0;
279
    int64_t _predicate_filtered_rows = 0;
280
    int64_t _raw_rows_read = 0;
281
};
282
283
} // namespace doris::format::parquet