Coverage Report

Created: 2026-05-26 00:47

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/parquet/parquet_common.h"
34
#include "format/parquet/parquet_predicate.h"
35
#include "format/parquet/vparquet_column_reader.h"
36
#include "format/parquet/vparquet_group_reader.h"
37
#include "format/table/table_format_reader.h"
38
#include "format/table/table_schema_change_helper.h"
39
#include "io/file_factory.h"
40
#include "io/fs/file_meta_cache.h"
41
#include "io/fs/file_reader.h"
42
#include "io/fs/file_reader_writer_fwd.h"
43
#include "runtime/runtime_profile.h"
44
#include "storage/olap_scan_common.h"
45
#include "util/obj_lru_cache.h"
46
47
namespace cctz {
48
class time_zone;
49
} // namespace cctz
50
namespace doris {
51
class RowDescriptor;
52
class RuntimeState;
53
class SlotDescriptor;
54
class TFileRangeDesc;
55
class TFileScanRangeParams;
56
class TupleDescriptor;
57
58
namespace io {
59
class FileSystem;
60
struct IOContext;
61
} // namespace io
62
class Block;
63
class FileMetaData;
64
class PageIndex;
65
class ShardedKVCache;
66
class VExprContext;
67
struct RowLineageColumns;
68
} // namespace doris
69
70
namespace doris {
71
72
/// Parquet-specific initialization context.
73
/// Extends ReaderInitContext with predicate pushdown fields.
74
struct ParquetInitContext final : public ReaderInitContext {
75
    // Safe defaults for standalone readers (delete file readers, push handler)
76
    // that don't have conjuncts/predicates. Dereferenced by _do_init_reader.
77
    static inline const VExprContextSPtrs EMPTY_CONJUNCTS {};
78
    static inline phmap::flat_hash_map<int, std::vector<std::shared_ptr<ColumnPredicate>>>
79
            EMPTY_SLOT_PREDICATES {};
80
81
    const VExprContextSPtrs* conjuncts = &EMPTY_CONJUNCTS;
82
    phmap::flat_hash_map<int, std::vector<std::shared_ptr<ColumnPredicate>>>*
83
            slot_id_to_predicates = &EMPTY_SLOT_PREDICATES;
84
    const std::unordered_map<std::string, int>* colname_to_slot_id = nullptr;
85
    const VExprContextSPtrs* not_single_slot_filter_conjuncts = nullptr;
86
    const std::unordered_map<int, VExprContextSPtrs>* slot_id_to_filter_conjuncts = nullptr;
87
    bool filter_groups = true;
88
};
89
90
class ParquetReader : public TableFormatReader {
91
    ENABLE_FACTORY_CREATOR(ParquetReader);
92
93
public:
94
    struct ReaderStatistics {
95
        int32_t filtered_row_groups = 0;
96
        int32_t filtered_row_groups_by_min_max = 0;
97
        int32_t filtered_row_groups_by_bloom_filter = 0;
98
        int32_t read_row_groups = 0;
99
        int64_t filtered_group_rows = 0;
100
        int64_t filtered_page_rows = 0;
101
        int64_t lazy_read_filtered_rows = 0;
102
        int64_t read_rows = 0;
103
        int64_t filtered_bytes = 0;
104
        int64_t column_read_time = 0;
105
        int64_t parse_meta_time = 0;
106
        int64_t parse_footer_time = 0;
107
        int64_t file_footer_read_calls = 0;
108
        int64_t file_footer_hit_cache = 0;
109
        int64_t file_footer_hit_memory_cache = 0;
110
        int64_t file_footer_hit_disk_cache = 0;
111
        int64_t file_footer_miss_disk_cache = 0;
112
        int64_t file_footer_write_disk_cache = 0;
113
        int64_t file_footer_read_disk_cache_time = 0;
114
        int64_t file_footer_write_disk_cache_time = 0;
115
        int64_t file_reader_create_time = 0;
116
        int64_t open_file_num = 0;
117
        int64_t row_group_filter_time = 0;
118
        int64_t page_index_filter_time = 0;
119
        int64_t read_page_index_time = 0;
120
        int64_t parse_page_index_time = 0;
121
        int64_t predicate_filter_time = 0;
122
        int64_t dict_filter_rewrite_time = 0;
123
        int64_t bloom_filter_read_time = 0;
124
    };
125
126
    ParquetReader(RuntimeProfile* profile, const TFileScanRangeParams& params,
127
                  const TFileRangeDesc& range, size_t batch_size, const cctz::time_zone* ctz,
128
                  io::IOContext* io_ctx, RuntimeState* state, FileMetaCache* meta_cache = nullptr,
129
                  bool enable_lazy_mat = true);
130
131
    ParquetReader(RuntimeProfile* profile, const TFileScanRangeParams& params,
132
                  const TFileRangeDesc& range, size_t batch_size, const cctz::time_zone* ctz,
133
                  std::shared_ptr<io::IOContext> io_ctx_holder, RuntimeState* state,
134
                  FileMetaCache* meta_cache = nullptr, bool enable_lazy_mat = true);
135
136
    ParquetReader(const TFileScanRangeParams& params, const TFileRangeDesc& range,
137
                  io::IOContext* io_ctx, RuntimeState* state, FileMetaCache* meta_cache = nullptr,
138
                  bool enable_lazy_mat = true);
139
140
    ParquetReader(const TFileScanRangeParams& params, const TFileRangeDesc& range,
141
                  std::shared_ptr<io::IOContext> io_ctx_holder, RuntimeState* state,
142
                  FileMetaCache* meta_cache = nullptr, bool enable_lazy_mat = true);
143
144
    ~ParquetReader() override;
145
#ifdef BE_TEST
146
    // for unit test
147
    void set_file_reader(io::FileReaderSPtr file_reader);
148
#endif
149
150
    // Override to build table_info_node from Parquet file metadata using by_parquet_name.
151
    // Subclasses (HiveParquetReader, etc.) call GenericReader::on_before_init_reader directly,
152
    // so this override only applies to plain ParquetReader (TVF, load).
153
    Status on_before_init_reader(ReaderInitContext* ctx) override;
154
155
    void set_batch_size(size_t batch_size) override;
156
157
    Status close() override;
158
159
    // set the delete rows in current parquet file
160
2
    void set_delete_rows(const std::vector<int64_t>* delete_rows) { _delete_rows = delete_rows; }
161
162
0
    int64_t size() const { return _file_reader->size(); }
163
164
    Status _get_columns_impl(std::unordered_map<std::string, DataTypePtr>* name_to_type) override;
165
166
    Status init_schema_reader() override;
167
168
    Status get_parsed_schema(std::vector<std::string>* col_names,
169
                             std::vector<DataTypePtr>* col_types) override;
170
171
0
    ReaderStatistics& reader_statistics() { return _reader_statistics; }
172
173
2
    const tparquet::FileMetaData* get_meta_data() const { return _t_metadata; }
174
175
    Status get_file_metadata_schema(const FieldDescriptor** ptr);
176
177
    void set_create_row_id_column_iterator_func(
178
14
            std::function<std::shared_ptr<segment_v2::RowIdColumnIteratorV2>()> create_func) {
179
14
        _create_topn_row_id_column_iterator = create_func;
180
14
    }
181
182
    /// Access current batch row positions (delegates to RowGroupReader).
183
    /// Used by IcebergReaderMixin to build $row_id column.
184
5
    const std::vector<segment_v2::rowid_t>& current_batch_row_positions() const {
185
5
        return _current_group_reader->current_batch_row_positions();
186
5
    }
187
188
    Status fill_topn_row_id(
189
            std::shared_ptr<segment_v2::RowIdColumnIteratorV2> _row_id_column_iterator,
190
5
            std::string col_name, Block* block, size_t rows) {
191
5
        int col_pos = block->get_position_by_name(col_name);
192
5
        DCHECK(col_pos >= 0);
193
5
        if (col_pos < 0) {
194
0
            return Status::InternalError("Column {} not found in block", col_name);
195
0
        }
196
5
        auto column_guard = block->mutate_column_scoped(col_pos);
197
5
        auto& col = column_guard.mutable_column();
198
5
        const auto& row_ids = this->current_batch_row_positions();
199
5
        RETURN_IF_ERROR(
200
5
                _row_id_column_iterator->read_by_rowids(row_ids.data(), row_ids.size(), col));
201
202
5
        return Status::OK();
203
5
    }
204
205
14
    bool count_read_rows() override { return true; }
206
207
    void set_condition_cache_context(std::shared_ptr<ConditionCacheContext> ctx) override;
208
209
0
    bool supports_count_pushdown() const override { return true; }
210
211
    int64_t get_total_rows() const override;
212
213
4
    bool has_delete_operations() const override {
214
4
        return _delete_rows != nullptr && !_delete_rows->empty();
215
4
    }
216
217
    /// Disable row-group range filtering (needed when reading delete files
218
    /// whose TFileRangeDesc has size=-1).
219
0
    void set_filter_groups(bool v) { _filter_groups = v; }
220
221
protected:
222
    // ---- Unified init_reader(ReaderInitContext*) overrides ----
223
    Status _open_file_reader(ReaderInitContext* ctx) override;
224
    Status _do_init_reader(ReaderInitContext* ctx) override;
225
226
    void _collect_profile_before_close() override;
227
228
    // Core block reading implementation
229
    Status _do_get_next_block(Block* block, size_t* read_rows, bool* eof) override;
230
231
    // Parquet fills partition/missing columns per-batch internally via RowGroupReader,
232
    // so suppress TableFormatReader's default on_after_read_block fill.
233
97
    Status on_after_read_block(Block* /*block*/, size_t* /*read_rows*/) override {
234
97
        return Status::OK();
235
97
    }
236
237
    // Protected accessors so CRTP mixin subclasses can reach private members
238
0
    io::IOContext* get_io_ctx() const { return _io_ctx; }
239
0
    std::unordered_map<std::string, uint32_t>*& col_name_to_block_idx_ref() {
240
0
        return _col_name_to_block_idx;
241
0
    }
242
42
    RuntimeProfile* get_profile() const { return _profile; }
243
28
    RuntimeState* get_state() const { return _state; }
244
1
    const TFileScanRangeParams& get_scan_params() const { return _scan_params; }
245
1
    const TFileRangeDesc& get_scan_range() const { return _scan_range; }
246
0
    const TupleDescriptor* get_tuple_descriptor() const { return _tuple_descriptor; }
247
0
    const RowDescriptor* get_row_descriptor() const { return _row_descriptor; }
248
0
    const FileMetaData* get_file_metadata() const { return _file_metadata; }
249
250
private:
251
    struct ParquetProfile {
252
        RuntimeProfile::Counter* filtered_row_groups = nullptr;
253
        RuntimeProfile::Counter* filtered_row_groups_by_min_max = nullptr;
254
        RuntimeProfile::Counter* filtered_row_groups_by_bloom_filter = nullptr;
255
        RuntimeProfile::Counter* to_read_row_groups = nullptr;
256
        RuntimeProfile::Counter* total_row_groups = nullptr;
257
        RuntimeProfile::Counter* filtered_group_rows = nullptr;
258
        RuntimeProfile::Counter* filtered_page_rows = nullptr;
259
        RuntimeProfile::Counter* lazy_read_filtered_rows = nullptr;
260
        RuntimeProfile::Counter* filtered_bytes = nullptr;
261
        RuntimeProfile::Counter* raw_rows_read = nullptr;
262
        RuntimeProfile::Counter* column_read_time = nullptr;
263
        RuntimeProfile::Counter* parse_meta_time = nullptr;
264
        RuntimeProfile::Counter* parse_footer_time = nullptr;
265
        RuntimeProfile::Counter* file_reader_create_time = nullptr;
266
        RuntimeProfile::Counter* open_file_num = nullptr;
267
        RuntimeProfile::Counter* row_group_filter_time = nullptr;
268
        RuntimeProfile::Counter* page_index_read_calls = nullptr;
269
        RuntimeProfile::Counter* page_index_filter_time = nullptr;
270
        RuntimeProfile::Counter* read_page_index_time = nullptr;
271
        RuntimeProfile::Counter* parse_page_index_time = nullptr;
272
        RuntimeProfile::Counter* file_footer_read_calls = nullptr;
273
        RuntimeProfile::Counter* file_footer_hit_cache = nullptr;
274
        RuntimeProfile::Counter* file_footer_hit_memory_cache = nullptr;
275
        RuntimeProfile::Counter* file_footer_hit_disk_cache = nullptr;
276
        RuntimeProfile::Counter* file_footer_miss_disk_cache = nullptr;
277
        RuntimeProfile::Counter* file_footer_write_disk_cache = nullptr;
278
        RuntimeProfile::Counter* file_footer_read_disk_cache_time = nullptr;
279
        RuntimeProfile::Counter* file_footer_write_disk_cache_time = nullptr;
280
        RuntimeProfile::Counter* decompress_time = nullptr;
281
        RuntimeProfile::Counter* decompress_cnt = nullptr;
282
        RuntimeProfile::Counter* page_read_counter = nullptr;
283
        RuntimeProfile::Counter* page_cache_write_counter = nullptr;
284
        RuntimeProfile::Counter* page_cache_compressed_write_counter = nullptr;
285
        RuntimeProfile::Counter* page_cache_decompressed_write_counter = nullptr;
286
        RuntimeProfile::Counter* page_cache_hit_counter = nullptr;
287
        RuntimeProfile::Counter* page_cache_missing_counter = nullptr;
288
        RuntimeProfile::Counter* page_cache_compressed_hit_counter = nullptr;
289
        RuntimeProfile::Counter* page_cache_decompressed_hit_counter = nullptr;
290
        RuntimeProfile::Counter* decode_header_time = nullptr;
291
        RuntimeProfile::Counter* read_page_header_time = nullptr;
292
        RuntimeProfile::Counter* decode_value_time = nullptr;
293
        RuntimeProfile::Counter* decode_dict_time = nullptr;
294
        RuntimeProfile::Counter* decode_level_time = nullptr;
295
        RuntimeProfile::Counter* decode_null_map_time = nullptr;
296
        RuntimeProfile::Counter* skip_page_header_num = nullptr;
297
        RuntimeProfile::Counter* parse_page_header_num = nullptr;
298
        RuntimeProfile::Counter* predicate_filter_time = nullptr;
299
        RuntimeProfile::Counter* dict_filter_rewrite_time = nullptr;
300
        RuntimeProfile::Counter* convert_time = nullptr;
301
        RuntimeProfile::Counter* bloom_filter_read_time = nullptr;
302
    };
303
304
    // ---- set_fill_columns sub-functions ----
305
    void _collect_predicate_columns_from_conjuncts(
306
            std::unordered_map<std::string, std::pair<uint32_t, int>>& predicate_columns);
307
    void _classify_columns_for_lazy_read(
308
            const std::unordered_map<std::string, std::pair<uint32_t, int>>& predicate_columns,
309
            const std::unordered_map<std::string, std::tuple<std::string, const SlotDescriptor*>>&
310
                    partition_columns,
311
            const std::unordered_map<std::string, VExprContextSPtr>& missing_columns);
312
313
    Status _open_file();
314
    void _init_profile();
315
    void _close_internal();
316
    Status _next_row_group_reader();
317
    RowGroupReader::PositionDeleteContext _get_position_delete_ctx(
318
            const tparquet::RowGroup& row_group,
319
            const RowGroupReader::RowGroupIndex& row_group_index);
320
    void _init_system_properties();
321
    void _init_file_description();
322
323
    // At the beginning of reading next row group, index should be loaded and used to filter data efficiently.
324
    Status _process_page_index_filter(
325
            const tparquet::RowGroup& row_group,
326
            const RowGroupReader::RowGroupIndex& row_group_index,
327
            const std::vector<std::unique_ptr<MutilColumnBlockPredicate>>& push_down_pred,
328
            RowRanges* candidate_row_ranges);
329
330
    // check this range contain this row group.
331
    bool _is_misaligned_range_group(const tparquet::RowGroup& row_group) const;
332
333
    // Row Group min-max Filter
334
    Status _process_column_stat_filter(
335
            const tparquet::RowGroup& row_group,
336
            const std::vector<std::unique_ptr<MutilColumnBlockPredicate>>& push_down_pred,
337
            bool* filter_group, bool* filtered_by_min_max, bool* filtered_by_bloom_filter);
338
339
    /*
340
     * 1. row group min-max filter
341
     * 2. row group bloom filter
342
     * 3. page index min-max filter
343
     *
344
     * return Status && row_ranges (lines to be read)
345
     */
346
    Status _process_min_max_bloom_filter(
347
            const RowGroupReader::RowGroupIndex& row_group_index,
348
            const tparquet::RowGroup& row_group,
349
            const std::vector<std::unique_ptr<MutilColumnBlockPredicate>>& push_down_pred,
350
            RowRanges* row_ranges);
351
352
    int64_t _get_column_start_offset(
353
            const tparquet::ColumnMetaData& column_init_column_readers) const;
354
0
    std::string _meta_cache_key(const std::string& path) { return "meta_" + path; }
355
    std::vector<io::PrefetchRange> _generate_random_access_ranges(
356
            const RowGroupReader::RowGroupIndex& group, size_t* avg_io_size);
357
    void _collect_profile();
358
359
19
    Status _set_read_one_line_impl() override { return Status::OK(); }
360
361
    bool _exists_in_file(const std::string& expr_name) const;
362
    bool _type_matches(const int cid) const;
363
    void _init_read_columns(const std::vector<std::string>& column_names);
364
365
    io::FileSystemProperties _system_properties;
366
    io::FileDescription _file_description;
367
368
    // the following fields are for parquet meta data cache.
369
    // if _meta_cache is not null, the _file_metadata will be got from _meta_cache,
370
    // and it is owned by _meta_cache_handle.
371
    // if _meta_cache is null, _file_metadata will be managed by _file_metadata_ptr,
372
    // which will be released when deconstructing.
373
    // ATTN: these fields must be before _file_reader, to make sure they will be released
374
    // after _file_reader. Otherwise, there may be heap-use-after-free bug.
375
    ObjLRUCache::CacheHandle _meta_cache_handle;
376
    std::unique_ptr<FileMetaData> _file_metadata_ptr;
377
    const tparquet::FileMetaData* _t_metadata = nullptr;
378
379
    // _tracing_file_reader wraps _file_reader.
380
    // _file_reader is original file reader.
381
    // _tracing_file_reader is tracing file reader with io context.
382
    // If io_ctx is null, _tracing_file_reader will be the same as file_reader.
383
    io::FileReaderSPtr _file_reader = nullptr;
384
    io::FileReaderSPtr _tracing_file_reader = nullptr;
385
    std::unique_ptr<RowGroupReader> _current_group_reader;
386
387
    RowGroupReader::RowGroupIndex _current_row_group_index {-1, 0, 0};
388
    // read to the end of current reader
389
    bool _row_group_eof = true;
390
    size_t _total_groups = 0; // num of groups(stripes) of a parquet(orc) file
391
392
    std::shared_ptr<ConditionCacheContext> _condition_cache_ctx;
393
394
    // Through this node, you can find the file column based on the table column.
395
    std::shared_ptr<TableSchemaChangeHelper::Node> _table_info_node_ptr =
396
            TableSchemaChangeHelper::ConstNode::get_instance();
397
398
    //sequence in file, need to read
399
    std::vector<std::string> _read_table_columns;
400
    std::vector<std::string> _read_file_columns;
401
    // The set of file columns to be read; only columns within this set will be filtered using the min-max predicate.
402
    std::set<std::string> _read_table_columns_set;
403
    // Deleted rows will be marked by Iceberg/Paimon. So we should filter deleted rows when reading it.
404
    const std::vector<int64_t>* _delete_rows = nullptr;
405
    int64_t _delete_rows_index = 0;
406
407
    // parquet file reader object
408
    RuntimeProfile* _profile = nullptr;
409
    const TFileScanRangeParams& _scan_params;
410
    const TFileRangeDesc& _scan_range;
411
    size_t _batch_size;
412
    // Bytes-per-row estimate from the previous batch, used to pre-shrink _batch_size
413
    // before reading so that oversized blocks are prevented from the current call onward.
414
    // Zero means no prior data (first batch).
415
    size_t _load_bytes_per_row = 0;
416
    int64_t _range_start_offset;
417
    int64_t _range_size;
418
    const cctz::time_zone* _ctz = nullptr;
419
420
    std::unordered_map<int, tparquet::OffsetIndex> _col_offsets;
421
422
    ReaderStatistics _reader_statistics;
423
    ParquetColumnReader::ColumnStatistics _column_statistics;
424
    ParquetProfile _parquet_profile;
425
    bool _closed = false;
426
    io::IOContext* _io_ctx = nullptr;
427
    std::shared_ptr<io::IOContext> _io_ctx_holder;
428
    RuntimeState* _state = nullptr;
429
    const TupleDescriptor* _tuple_descriptor = nullptr;
430
    const RowDescriptor* _row_descriptor = nullptr;
431
    const FileMetaData* _file_metadata = nullptr;
432
    // Pointer to external column name to block index mapping (from FileScanner)
433
    std::unordered_map<std::string, uint32_t>* _col_name_to_block_idx = nullptr;
434
    bool _enable_lazy_mat = true;
435
    bool _enable_filter_by_min_max = true;
436
    bool _enable_filter_by_bloom_filter = true;
437
    const std::unordered_map<std::string, int>* _colname_to_slot_id = nullptr;
438
    const VExprContextSPtrs* _not_single_slot_filter_conjuncts = nullptr;
439
    const std::unordered_map<int, VExprContextSPtrs>* _slot_id_to_filter_conjuncts = nullptr;
440
    std::unordered_map<tparquet::Type::type, bool> _ignored_stats;
441
0
    size_t get_batch_size() const override { return _batch_size; }
442
443
protected:
444
    // Used for column lazy read. Protected so Iceberg/Paimon subclasses can
445
    // register synthesized columns in on_before_init_reader.
446
    RowGroupReader::LazyReadContext _lazy_read_ctx;
447
    bool _filter_groups = true;
448
449
    std::function<std::shared_ptr<segment_v2::RowIdColumnIteratorV2>()>
450
            _create_topn_row_id_column_iterator;
451
452
private:
453
    std::set<uint64_t> _column_ids;
454
    std::set<uint64_t> _filter_column_ids;
455
456
    std::vector<std::unique_ptr<MutilColumnBlockPredicate>> _push_down_predicates;
457
    Arena _arena;
458
};
459
460
} // namespace doris