Coverage Report

Created: 2026-03-16 16:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format/parquet/vparquet_reader.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 <gen_cpp/parquet_types.h>
21
#include <stddef.h>
22
#include <stdint.h>
23
24
#include <list>
25
#include <memory>
26
#include <string>
27
#include <tuple>
28
#include <unordered_map>
29
#include <unordered_set>
30
#include <vector>
31
32
#include "common/status.h"
33
#include "format/generic_reader.h"
34
#include "format/parquet/parquet_common.h"
35
#include "format/parquet/parquet_predicate.h"
36
#include "format/parquet/vparquet_column_reader.h"
37
#include "format/parquet/vparquet_group_reader.h"
38
#include "format/table/table_format_reader.h"
39
#include "format/table/table_schema_change_helper.h"
40
#include "io/file_factory.h"
41
#include "io/fs/file_meta_cache.h"
42
#include "io/fs/file_reader.h"
43
#include "io/fs/file_reader_writer_fwd.h"
44
#include "runtime/runtime_profile.h"
45
#include "storage/olap_scan_common.h"
46
#include "util/obj_lru_cache.h"
47
48
namespace cctz {
49
class time_zone;
50
} // namespace cctz
51
namespace doris {
52
class RowDescriptor;
53
class RuntimeState;
54
class SlotDescriptor;
55
class TFileRangeDesc;
56
class TFileScanRangeParams;
57
class TupleDescriptor;
58
59
namespace io {
60
class FileSystem;
61
struct IOContext;
62
} // namespace io
63
class Block;
64
class FileMetaData;
65
class PageIndex;
66
class ShardedKVCache;
67
class VExprContext;
68
} // namespace doris
69
70
namespace doris {
71
#include "common/compile_check_begin.h"
72
class ParquetReader : public TableFormatReader {
73
    ENABLE_FACTORY_CREATOR(ParquetReader);
74
75
public:
76
    struct ReaderStatistics {
77
        int32_t filtered_row_groups = 0;
78
        int32_t filtered_row_groups_by_min_max = 0;
79
        int32_t filtered_row_groups_by_bloom_filter = 0;
80
        int32_t read_row_groups = 0;
81
        int64_t filtered_group_rows = 0;
82
        int64_t filtered_page_rows = 0;
83
        int64_t lazy_read_filtered_rows = 0;
84
        int64_t read_rows = 0;
85
        int64_t filtered_bytes = 0;
86
        int64_t column_read_time = 0;
87
        int64_t parse_meta_time = 0;
88
        int64_t parse_footer_time = 0;
89
        int64_t file_footer_read_calls = 0;
90
        int64_t file_footer_hit_cache = 0;
91
        int64_t file_reader_create_time = 0;
92
        int64_t open_file_num = 0;
93
        int64_t row_group_filter_time = 0;
94
        int64_t page_index_filter_time = 0;
95
        int64_t read_page_index_time = 0;
96
        int64_t parse_page_index_time = 0;
97
        int64_t predicate_filter_time = 0;
98
        int64_t dict_filter_rewrite_time = 0;
99
        int64_t bloom_filter_read_time = 0;
100
    };
101
102
    ParquetReader(RuntimeProfile* profile, const TFileScanRangeParams& params,
103
                  const TFileRangeDesc& range, size_t batch_size, const cctz::time_zone* ctz,
104
                  io::IOContext* io_ctx, RuntimeState* state, FileMetaCache* meta_cache = nullptr,
105
                  bool enable_lazy_mat = true);
106
107
    ParquetReader(RuntimeProfile* profile, const TFileScanRangeParams& params,
108
                  const TFileRangeDesc& range, size_t batch_size, const cctz::time_zone* ctz,
109
                  std::shared_ptr<io::IOContext> io_ctx_holder, RuntimeState* state,
110
                  FileMetaCache* meta_cache = nullptr, bool enable_lazy_mat = true);
111
112
    ParquetReader(const TFileScanRangeParams& params, const TFileRangeDesc& range,
113
                  io::IOContext* io_ctx, RuntimeState* state, FileMetaCache* meta_cache = nullptr,
114
                  bool enable_lazy_mat = true);
115
116
    ParquetReader(const TFileScanRangeParams& params, const TFileRangeDesc& range,
117
                  std::shared_ptr<io::IOContext> io_ctx_holder, RuntimeState* state,
118
                  FileMetaCache* meta_cache = nullptr, bool enable_lazy_mat = true);
119
120
    ~ParquetReader() override;
121
#ifdef BE_TEST
122
    // for unit test
123
    void set_file_reader(io::FileReaderSPtr file_reader);
124
#endif
125
126
    // Template method: calls on_before_init_columns → _do_init_reader → on_after_init
127
    Status init_reader(
128
            const std::vector<ColumnDescriptor>& column_descs,
129
            std::unordered_map<std::string, uint32_t>* col_name_to_block_idx,
130
            const VExprContextSPtrs& conjuncts,
131
            phmap::flat_hash_map<int, std::vector<std::shared_ptr<ColumnPredicate>>>&
132
                    slot_id_to_predicates,
133
            const TupleDescriptor* tuple_descriptor, const RowDescriptor* row_descriptor,
134
            const std::unordered_map<std::string, int>* colname_to_slot_id,
135
            const VExprContextSPtrs* not_single_slot_filter_conjuncts,
136
            const std::unordered_map<int, VExprContextSPtrs>* slot_id_to_filter_conjuncts);
137
138
    // Low-level init_reader — used by standalone reader instances (tvf, load, push_handler,
139
    // Iceberg delete file readers) that don't go through subclass hooks.
140
    Status _do_init_reader(
141
            const std::vector<std::string>& all_column_names,
142
            std::unordered_map<std::string, uint32_t>* col_name_to_block_idx,
143
            const VExprContextSPtrs& conjuncts,
144
            phmap::flat_hash_map<int, std::vector<std::shared_ptr<ColumnPredicate>>>&
145
                    slot_id_to_predicates,
146
            const TupleDescriptor* tuple_descriptor, const RowDescriptor* row_descriptor,
147
            const std::unordered_map<std::string, int>* colname_to_slot_id,
148
            const VExprContextSPtrs* not_single_slot_filter_conjuncts,
149
            const std::unordered_map<int, VExprContextSPtrs>* slot_id_to_filter_conjuncts,
150
            std::shared_ptr<TableSchemaChangeHelper::Node> table_info_node_ptr =
151
                    TableSchemaChangeHelper::ConstNode::get_instance(),
152
            const std::set<uint64_t>& column_ids = {},
153
            const std::set<uint64_t>& filter_column_ids = {});
154
155
    // Template method: calls on_before_read_block → _do_get_next_block → on_after_read_block
156
    Status get_next_block(Block* block, size_t* read_rows, bool* eof) override;
157
158
    Status close() override;
159
160
    // set the delete rows in current parquet file
161
2.90k
    void set_delete_rows(const std::vector<int64_t>* delete_rows) { _delete_rows = delete_rows; }
162
163
0
    int64_t size() const { return _file_reader->size(); }
164
165
    Status get_columns(std::unordered_map<std::string, DataTypePtr>* name_to_type,
166
                       std::unordered_set<std::string>* missing_cols) override;
167
168
    Status init_schema_reader() override;
169
170
    Status get_parsed_schema(std::vector<std::string>* col_names,
171
                             std::vector<DataTypePtr>* col_types) override;
172
173
0
    ReaderStatistics& reader_statistics() { return _reader_statistics; }
174
175
622
    const tparquet::FileMetaData* get_meta_data() const { return _t_metadata; }
176
177
    // Partition columns will not be materialized in parquet files. So we should fill it with missing columns.
178
    Status set_fill_columns(
179
            const std::unordered_map<std::string, std::tuple<std::string, const SlotDescriptor*>>&
180
                    partition_columns,
181
            const std::unordered_map<std::string, VExprContextSPtr>& missing_columns) override;
182
183
    Status get_file_metadata_schema(const FieldDescriptor** ptr);
184
185
    void set_row_id_column_iterator(
186
2.45k
            std::pair<std::shared_ptr<RowIdColumnIteratorV2>, int> iterator_pair) {
187
2.45k
        _row_id_column_iterator_pair = iterator_pair;
188
2.45k
    }
189
190
    /// Access current batch row positions (delegates to RowGroupReader).
191
    /// Used by IcebergReaderMixin to build $row_id column.
192
14
    const std::vector<segment_v2::rowid_t>& current_batch_row_positions() const {
193
14
        return _current_group_reader->current_batch_row_positions();
194
14
    }
195
196
67.2k
    bool count_read_rows() override { return true; }
197
198
    /// Disable row-group range filtering (needed when reading delete files
199
    /// whose TFileRangeDesc has size=-1).
200
2.14k
    void set_filter_groups(bool v) { _filter_groups = v; }
201
202
protected:
203
    void _collect_profile_before_close() override;
204
205
    // Called before core init. Override to customize column selection,
206
    // schema mapping (table_info_node), and column_ids.
207
    // Parquet subclasses can also set _filter_groups = false to disable.
208
    Status on_before_init_columns(const std::vector<ColumnDescriptor>& column_descs,
209
                                  std::vector<std::string>& column_names,
210
                                  std::shared_ptr<TableSchemaChangeHelper::Node>& table_info_node,
211
                                  std::set<uint64_t>& column_ids,
212
                                  std::set<uint64_t>& filter_column_ids) override;
213
214
    // Internal get_next_block implementation (the actual reading logic)
215
    Status _do_get_next_block(Block* block, size_t* read_rows, bool* eof);
216
217
    // Protected accessors so CRTP mixin subclasses can reach private members
218
3.08k
    io::IOContext* get_io_ctx() const { return _io_ctx; }
219
3.31k
    std::unordered_map<std::string, uint32_t>*& col_name_to_block_idx_ref() {
220
3.31k
        return _col_name_to_block_idx;
221
3.31k
    }
222
119k
    RuntimeProfile* get_profile() const { return _profile; }
223
29.5k
    RuntimeState* get_state() const { return _state; }
224
60.1k
    const TFileScanRangeParams& get_scan_params() const { return _scan_params; }
225
45.1k
    const TFileRangeDesc& get_scan_range() const { return _scan_range; }
226
61.9k
    const TupleDescriptor* get_tuple_descriptor() const { return _tuple_descriptor; }
227
0
    const RowDescriptor* get_row_descriptor() const { return _row_descriptor; }
228
0
    const FileMetaData* get_file_metadata() const { return _file_metadata; }
229
230
private:
231
    struct ParquetProfile {
232
        RuntimeProfile::Counter* filtered_row_groups = nullptr;
233
        RuntimeProfile::Counter* filtered_row_groups_by_min_max = nullptr;
234
        RuntimeProfile::Counter* filtered_row_groups_by_bloom_filter = nullptr;
235
        RuntimeProfile::Counter* to_read_row_groups = nullptr;
236
        RuntimeProfile::Counter* total_row_groups = nullptr;
237
        RuntimeProfile::Counter* filtered_group_rows = nullptr;
238
        RuntimeProfile::Counter* filtered_page_rows = nullptr;
239
        RuntimeProfile::Counter* lazy_read_filtered_rows = nullptr;
240
        RuntimeProfile::Counter* filtered_bytes = nullptr;
241
        RuntimeProfile::Counter* raw_rows_read = nullptr;
242
        RuntimeProfile::Counter* column_read_time = nullptr;
243
        RuntimeProfile::Counter* parse_meta_time = nullptr;
244
        RuntimeProfile::Counter* parse_footer_time = nullptr;
245
        RuntimeProfile::Counter* file_reader_create_time = nullptr;
246
        RuntimeProfile::Counter* open_file_num = nullptr;
247
        RuntimeProfile::Counter* row_group_filter_time = nullptr;
248
        RuntimeProfile::Counter* page_index_read_calls = nullptr;
249
        RuntimeProfile::Counter* page_index_filter_time = nullptr;
250
        RuntimeProfile::Counter* read_page_index_time = nullptr;
251
        RuntimeProfile::Counter* parse_page_index_time = nullptr;
252
        RuntimeProfile::Counter* file_footer_read_calls = nullptr;
253
        RuntimeProfile::Counter* file_footer_hit_cache = nullptr;
254
        RuntimeProfile::Counter* decompress_time = nullptr;
255
        RuntimeProfile::Counter* decompress_cnt = nullptr;
256
        RuntimeProfile::Counter* page_read_counter = nullptr;
257
        RuntimeProfile::Counter* page_cache_write_counter = nullptr;
258
        RuntimeProfile::Counter* page_cache_compressed_write_counter = nullptr;
259
        RuntimeProfile::Counter* page_cache_decompressed_write_counter = nullptr;
260
        RuntimeProfile::Counter* page_cache_hit_counter = nullptr;
261
        RuntimeProfile::Counter* page_cache_missing_counter = nullptr;
262
        RuntimeProfile::Counter* page_cache_compressed_hit_counter = nullptr;
263
        RuntimeProfile::Counter* page_cache_decompressed_hit_counter = nullptr;
264
        RuntimeProfile::Counter* decode_header_time = nullptr;
265
        RuntimeProfile::Counter* read_page_header_time = nullptr;
266
        RuntimeProfile::Counter* decode_value_time = nullptr;
267
        RuntimeProfile::Counter* decode_dict_time = nullptr;
268
        RuntimeProfile::Counter* decode_level_time = nullptr;
269
        RuntimeProfile::Counter* decode_null_map_time = nullptr;
270
        RuntimeProfile::Counter* skip_page_header_num = nullptr;
271
        RuntimeProfile::Counter* parse_page_header_num = nullptr;
272
        RuntimeProfile::Counter* predicate_filter_time = nullptr;
273
        RuntimeProfile::Counter* dict_filter_rewrite_time = nullptr;
274
        RuntimeProfile::Counter* bloom_filter_read_time = nullptr;
275
    };
276
277
    Status _open_file();
278
    void _init_profile();
279
    void _close_internal();
280
    Status _next_row_group_reader();
281
    RowGroupReader::PositionDeleteContext _get_position_delete_ctx(
282
            const tparquet::RowGroup& row_group,
283
            const RowGroupReader::RowGroupIndex& row_group_index);
284
    void _init_system_properties();
285
    void _init_file_description();
286
287
    // At the beginning of reading next row group, index should be loaded and used to filter data efficiently.
288
    Status _process_page_index_filter(
289
            const tparquet::RowGroup& row_group,
290
            const RowGroupReader::RowGroupIndex& row_group_index,
291
            const std::vector<std::unique_ptr<MutilColumnBlockPredicate>>& push_down_pred,
292
            RowRanges* candidate_row_ranges);
293
294
    // check this range contain this row group.
295
    bool _is_misaligned_range_group(const tparquet::RowGroup& row_group);
296
297
    // Row Group min-max Filter
298
    Status _process_column_stat_filter(
299
            const tparquet::RowGroup& row_group,
300
            const std::vector<std::unique_ptr<MutilColumnBlockPredicate>>& push_down_pred,
301
            bool* filter_group, bool* filtered_by_min_max, bool* filtered_by_bloom_filter);
302
303
    /*
304
     * 1. row group min-max filter
305
     * 2. row group bloom filter
306
     * 3. page index min-max filter
307
     *
308
     * return Status && row_ranges (lines to be read)
309
     */
310
    Status _process_min_max_bloom_filter(
311
            const RowGroupReader::RowGroupIndex& row_group_index,
312
            const tparquet::RowGroup& row_group,
313
            const std::vector<std::unique_ptr<MutilColumnBlockPredicate>>& push_down_pred,
314
            RowRanges* row_ranges);
315
316
    int64_t _get_column_start_offset(const tparquet::ColumnMetaData& column_init_column_readers);
317
0
    std::string _meta_cache_key(const std::string& path) { return "meta_" + path; }
318
    std::vector<io::PrefetchRange> _generate_random_access_ranges(
319
            const RowGroupReader::RowGroupIndex& group, size_t* avg_io_size);
320
    void _collect_profile();
321
322
1.32k
    Status _set_read_one_line_impl() override { return Status::OK(); }
323
324
    bool _exists_in_file(const std::string& expr_name) const;
325
    bool _type_matches(const int cid) const;
326
327
    io::FileSystemProperties _system_properties;
328
    io::FileDescription _file_description;
329
330
    // the following fields are for parquet meta data cache.
331
    // if _meta_cache is not null, the _file_metadata will be got from _meta_cache,
332
    // and it is owned by _meta_cache_handle.
333
    // if _meta_cache is null, _file_metadata will be managed by _file_metadata_ptr,
334
    // which will be released when deconstructing.
335
    // ATTN: these fields must be before _file_reader, to make sure they will be released
336
    // after _file_reader. Otherwise, there may be heap-use-after-free bug.
337
    ObjLRUCache::CacheHandle _meta_cache_handle;
338
    std::unique_ptr<FileMetaData> _file_metadata_ptr;
339
    const tparquet::FileMetaData* _t_metadata = nullptr;
340
341
    // _tracing_file_reader wraps _file_reader.
342
    // _file_reader is original file reader.
343
    // _tracing_file_reader is tracing file reader with io context.
344
    // If io_ctx is null, _tracing_file_reader will be the same as file_reader.
345
    io::FileReaderSPtr _file_reader = nullptr;
346
    io::FileReaderSPtr _tracing_file_reader = nullptr;
347
    std::unique_ptr<RowGroupReader> _current_group_reader;
348
349
    RowGroupReader::RowGroupIndex _current_row_group_index {-1, 0, 0};
350
    // read to the end of current reader
351
    bool _row_group_eof = true;
352
    size_t _total_groups; // num of groups(stripes) of a parquet(orc) file
353
354
    // Through this node, you can find the file column based on the table column.
355
    std::shared_ptr<TableSchemaChangeHelper::Node> _table_info_node_ptr =
356
            TableSchemaChangeHelper::ConstNode::get_instance();
357
358
    //sequence in file, need to read
359
    std::vector<std::string> _read_table_columns;
360
    std::vector<std::string> _read_file_columns;
361
    // The set of file columns to be read; only columns within this set will be filtered using the min-max predicate.
362
    std::set<std::string> _read_table_columns_set;
363
    // Deleted rows will be marked by Iceberg/Paimon. So we should filter deleted rows when reading it.
364
    const std::vector<int64_t>* _delete_rows = nullptr;
365
    int64_t _delete_rows_index = 0;
366
367
    // parquet file reader object
368
    RuntimeProfile* _profile = nullptr;
369
    const TFileScanRangeParams& _scan_params;
370
    const TFileRangeDesc& _scan_range;
371
    size_t _batch_size;
372
    int64_t _range_start_offset;
373
    int64_t _range_size;
374
    const cctz::time_zone* _ctz = nullptr;
375
376
    std::unordered_map<int, tparquet::OffsetIndex> _col_offsets;
377
378
    std::vector<std::string> _missing_cols;
379
    // _table_column_names = _missing_cols + _read_table_columns
380
    const std::vector<std::string>* _table_column_names = nullptr;
381
382
    ReaderStatistics _reader_statistics;
383
    ParquetColumnReader::ColumnStatistics _column_statistics;
384
    ParquetProfile _parquet_profile;
385
    bool _closed = false;
386
    io::IOContext* _io_ctx = nullptr;
387
    std::shared_ptr<io::IOContext> _io_ctx_holder;
388
    RuntimeState* _state = nullptr;
389
    const TupleDescriptor* _tuple_descriptor = nullptr;
390
    const RowDescriptor* _row_descriptor = nullptr;
391
    const FileMetaData* _file_metadata = nullptr;
392
    // Pointer to external column name to block index mapping (from FileScanner)
393
    std::unordered_map<std::string, uint32_t>* _col_name_to_block_idx = nullptr;
394
    bool _enable_lazy_mat = true;
395
    bool _enable_filter_by_min_max = true;
396
    bool _enable_filter_by_bloom_filter = true;
397
    const std::unordered_map<std::string, int>* _colname_to_slot_id = nullptr;
398
    const VExprContextSPtrs* _not_single_slot_filter_conjuncts = nullptr;
399
    const std::unordered_map<int, VExprContextSPtrs>* _slot_id_to_filter_conjuncts = nullptr;
400
    std::unordered_map<tparquet::Type::type, bool> _ignored_stats;
401
402
    std::pair<std::shared_ptr<RowIdColumnIteratorV2>, int> _row_id_column_iterator_pair = {nullptr,
403
                                                                                           -1};
404
405
protected:
406
    // Used for column lazy read. Protected so Iceberg/Paimon subclasses can
407
    // register synthesized columns in on_before_init_columns.
408
    RowGroupReader::LazyReadContext _lazy_read_ctx;
409
    bool _filter_groups = true;
410
106
    size_t get_batch_size() const { return _batch_size; }
411
412
private:
413
    std::set<uint64_t> _column_ids;
414
    std::set<uint64_t> _filter_column_ids;
415
416
    std::vector<std::unique_ptr<MutilColumnBlockPredicate>> _push_down_predicates;
417
    Arena _arena;
418
};
419
#include "common/compile_check_end.h"
420
421
} // namespace doris