Coverage Report

Created: 2026-07-15 13:51

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 <cstddef>
19
#include <cstdint>
20
#include <map>
21
#include <memory>
22
#include <optional>
23
#include <utility>
24
#include <vector>
25
26
#include "common/status.h"
27
#include "core/column/column.h"
28
#include "format_v2/file_reader.h"
29
#include "format_v2/parquet/parquet_profile.h"
30
#include "format_v2/parquet/parquet_statistics.h"
31
#include "format_v2/parquet/reader/column_reader.h"
32
#include "format_v2/parquet/selection_vector.h"
33
#include "runtime/runtime_profile.h"
34
#include "storage/segment/condition_cache.h"
35
36
namespace parquet {
37
class FileMetaData;
38
class ParquetFileReader;
39
class RowGroupMetaData;
40
class RowGroupReader;
41
} // namespace parquet
42
43
namespace cctz {
44
class time_zone;
45
} // namespace cctz
46
47
namespace doris {
48
class Block;
49
class RuntimeState;
50
51
namespace format {
52
struct FileScanRequest;
53
} // namespace format
54
} // namespace doris
55
56
namespace doris::format::parquet {
57
58
struct ParquetFileContext;
59
struct ParquetColumnSchema;
60
61
// ============================================================================
62
// ============================================================================
63
64
struct ParquetScanRange {
65
    int64_t start_offset = 0;
66
    int64_t size = -1;      // -1 means read the whole file
67
    int64_t file_size = -1; // -1 means unknown
68
};
69
70
struct RowGroupReadPlan {
71
    int row_group_id = -1;                 // row group id
72
    int64_t first_file_row = 0;            // first file row for this row group (0-based)
73
    int64_t row_group_rows = 0;            // row count of this row group
74
    std::vector<RowRange> selected_ranges; // row ranges to read after page-index pruning
75
    std::map<int, ParquetPageSkipPlan>
76
            page_skip_plans; // leaf_column_id -> data pages that can be skipped completely
77
};
78
79
struct RowGroupScanPlan {
80
    std::vector<RowGroupReadPlan> row_groups; // row groups selected after pruning
81
    ParquetPruningStats pruning_stats;        // pruning statistics
82
};
83
84
// ============================================================================
85
// ============================================================================
86
87
Status plan_parquet_row_groups(const ::parquet::FileMetaData& metadata,
88
                               ::parquet::ParquetFileReader* file_reader,
89
                               const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema,
90
                               const format::FileScanRequest& request,
91
                               const ParquetScanRange& scan_range, bool enable_bloom_filter,
92
                               RowGroupScanPlan* plan, const cctz::time_zone* timezone = nullptr,
93
                               const RuntimeState* runtime_state = nullptr);
94
95
IColumn::Filter selection_to_filter(const SelectionVector& selection, uint16_t selected_rows,
96
                                    int64_t batch_rows);
97
98
uint16_t apply_compact_filter_to_selection(const IColumn::Filter& filter,
99
                                           SelectionVector* selection, uint16_t selected_rows);
100
101
Status execute_batch_filters(const format::FileScanRequest& request, int64_t batch_rows,
102
                             Block* file_block, SelectionVector* selection, uint16_t* selected_rows,
103
                             int64_t* conjunct_filtered_rows = nullptr);
104
105
// ============================================================================
106
// ============================================================================
107
//   while true:
108
//     3. read_current_row_group_batch(batch_rows)
109
// ============================================================================
110
class ParquetScanScheduler {
111
public:
112
    static constexpr int64_t DEFAULT_READ_BATCH_SIZE = 4096;
113
114
    void set_plan(RowGroupScanPlan plan);
115
168
    void set_page_skip_profile(ParquetPageSkipProfile page_skip_profile) {
116
168
        _page_skip_profile = page_skip_profile;
117
168
    }
118
168
    void set_scan_profile(ParquetScanProfile scan_profile) { _scan_profile = scan_profile; }
119
172
    void set_merge_read_options(RuntimeProfile* profile, int64_t merge_read_slice_size) {
120
172
        _profile = profile;
121
172
        _merge_read_slice_size = merge_read_slice_size;
122
172
    }
123
168
    void set_global_rowid_context(std::optional<format::GlobalRowIdContext> context) {
124
168
        _global_rowid_context = context;
125
168
    }
126
    void set_condition_cache_context(std::shared_ptr<ConditionCacheContext> ctx);
127
172
    void set_timezone(const cctz::time_zone* timezone) { _timezone = timezone; }
128
172
    void set_enable_strict_mode(bool enable_strict_mode) {
129
172
        _enable_strict_mode = enable_strict_mode;
130
172
    }
131
    // Upper scanner owns adaptive memory feedback; scheduler only applies the current row cap when
132
    // splitting selected row ranges into physical read batches.
133
172
    void set_batch_size(size_t batch_size) {
134
172
        _batch_size = batch_size == 0 ? 1 : static_cast<int64_t>(batch_size);
135
172
    }
136
    void reset();
137
169
    bool empty() const { return _row_group_plans.empty(); }
138
2
    int64_t condition_cache_filtered_rows() const { return _condition_cache_filtered_rows; }
139
353
    int64_t predicate_filtered_rows() const { return _predicate_filtered_rows; }
140
494
    int64_t raw_rows_read() const { return _raw_rows_read; }
141
142
    Status read_next_batch(ParquetFileContext& file_context,
143
                           const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema,
144
                           const format::FileScanRequest& request, Block* file_block, size_t* rows,
145
                           bool* eof);
146
147
private:
148
    void reset_current_row_group();
149
150
    Status open_next_row_group(ParquetFileContext& file_context,
151
                               const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema,
152
                               const format::FileScanRequest& request, bool* has_row_group);
153
154
    Status skip_current_row_group_rows(int64_t rows);
155
    Status flush_pending_non_predicate_skip_rows();
156
157
    Status read_filter_columns(int64_t batch_rows, const format::FileScanRequest& request,
158
                               Block* file_block, SelectionVector* selection,
159
                               uint16_t* selected_rows, int64_t* conjunct_filtered_rows,
160
                               bool* predicate_columns_filtered);
161
162
    Status prepare_current_dictionary_filters(
163
            ParquetFileContext& file_context,
164
            const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema,
165
            const format::FileScanRequest& request, int row_group_idx,
166
            const ::parquet::RowGroupMetaData& row_group_metadata);
167
168
    void prefetch_current_row_group_columns(
169
            ParquetFileContext& file_context,
170
            const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema,
171
            const std::vector<format::LocalColumnIndex>& scan_columns, bool* prefetched);
172
173
    bool prepare_current_row_group_reader(
174
            ParquetFileContext& file_context,
175
            const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema,
176
            const format::FileScanRequest& request, int row_group_idx);
177
178
    Status read_current_row_group_batch(
179
            ParquetFileContext& file_context,
180
            const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema,
181
            int64_t batch_rows, const format::FileScanRequest& request,
182
            int64_t batch_first_file_row, Block* file_block, size_t* rows);
183
184
    void mark_condition_cache_granules(const SelectionVector& selection, uint16_t selected_rows,
185
                                       int64_t batch_first_file_row);
186
187
    std::vector<RowGroupReadPlan> _row_group_plans; // row group queue to scan
188
    size_t _next_row_group_plan_idx = 0;            // index of the next row group to process
189
190
    std::shared_ptr<::parquet::RowGroupReader> _current_row_group; // Arrow RowGroup reader
191
    std::map<ColumnId, std::unique_ptr<ParquetColumnReader>>
192
            _current_predicate_columns; // predicate ColumnReaders
193
    std::map<ColumnId, std::unique_ptr<ParquetColumnReader>>
194
            _current_non_predicate_columns; // non-predicate ColumnReaders
195
    std::map<ColumnId, IColumn::Filter>
196
            _current_dictionary_filters; // local id -> dict entry bitmap
197
    std::map<ColumnId, std::vector<std::pair<VExprContextSPtr, VExprSPtr>>>
198
            _current_dictionary_residual_conjuncts; // local id -> row-level residual conjuncts
199
    int64_t _current_row_group_rows = 0;            // current row group row count
200
    int _current_row_group_id = -1;                 // current row group id in parquet metadata
201
    int64_t _current_row_group_rows_read = 0;       // rows read in the current row group (cursor)
202
    int64_t _current_row_group_first_row = 0;       // first file row of the current row group
203
    std::vector<RowRange>
204
            _current_selected_ranges; // selected ranges for the current row group after page-index pruning
205
    size_t _current_range_idx = 0;        // current selected_range index
206
    int64_t _current_range_rows_read = 0; // rows read in the current range
207
    // Predicate readers move immediately because they decide which rows survive. Non-predicate
208
    // readers can lag behind across fully filtered batches and range gaps; the lag is flushed once
209
    // before the next surviving batch is materialized, or discarded with the row group.
210
    int64_t _pending_non_predicate_skip_rows = 0;
211
212
    bool _current_predicate_prefetched = false;
213
    bool _current_non_predicate_prefetched = false;
214
    bool _current_merge_range_active = false;
215
    ParquetPageSkipProfile _page_skip_profile;
216
    ParquetScanProfile _scan_profile;
217
    RuntimeProfile* _profile = nullptr;
218
    int64_t _merge_read_slice_size = -1;
219
    std::optional<format::GlobalRowIdContext> _global_rowid_context;
220
    const cctz::time_zone* _timezone = nullptr;
221
    bool _enable_strict_mode = false;
222
    int64_t _batch_size = DEFAULT_READ_BATCH_SIZE;
223
    std::shared_ptr<ConditionCacheContext> _condition_cache_ctx;
224
    int64_t _condition_cache_filtered_rows = 0;
225
    int64_t _predicate_filtered_rows = 0;
226
    int64_t _raw_rows_read = 0;
227
};
228
229
} // namespace doris::format::parquet