Coverage Report

Created: 2026-04-02 14:31

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 "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
#include "common/compile_check_begin.h"
72
class ParquetReader : public GenericReader {
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
    Status init_reader(
127
            const std::vector<std::string>& all_column_names,
128
            std::unordered_map<std::string, uint32_t>* col_name_to_block_idx,
129
            const VExprContextSPtrs& conjuncts,
130
            phmap::flat_hash_map<int, std::vector<std::shared_ptr<ColumnPredicate>>>&
131
                    slot_id_to_predicates,
132
            const TupleDescriptor* tuple_descriptor, const RowDescriptor* row_descriptor,
133
            const std::unordered_map<std::string, int>* colname_to_slot_id,
134
            const VExprContextSPtrs* not_single_slot_filter_conjuncts,
135
            const std::unordered_map<int, VExprContextSPtrs>* slot_id_to_filter_conjuncts,
136
            std::shared_ptr<TableSchemaChangeHelper::Node> table_info_node_ptr =
137
                    TableSchemaChangeHelper::ConstNode::get_instance(),
138
            bool filter_groups = true, const std::set<uint64_t>& column_ids = {},
139
            const std::set<uint64_t>& filter_column_ids = {});
140
141
    Status get_next_block(Block* block, size_t* read_rows, bool* eof) override;
142
143
    void set_batch_size(size_t batch_size) override;
144
145
    Status close() override;
146
147
    // set the delete rows in current parquet file
148
2
    void set_delete_rows(const std::vector<int64_t>* delete_rows) { _delete_rows = delete_rows; }
149
150
0
    int64_t size() const { return _file_reader->size(); }
151
152
    Status get_columns(std::unordered_map<std::string, DataTypePtr>* name_to_type,
153
                       std::unordered_set<std::string>* missing_cols) override;
154
155
    Status init_schema_reader() override;
156
157
    Status get_parsed_schema(std::vector<std::string>* col_names,
158
                             std::vector<DataTypePtr>* col_types) override;
159
160
0
    ReaderStatistics& reader_statistics() { return _reader_statistics; }
161
162
0
    const tparquet::FileMetaData* get_meta_data() const { return _t_metadata; }
163
164
    // Partition columns will not be materialized in parquet files. So we should fill it with missing columns.
165
    Status set_fill_columns(
166
            const std::unordered_map<std::string, std::tuple<std::string, const SlotDescriptor*>>&
167
                    partition_columns,
168
            const std::unordered_map<std::string, VExprContextSPtr>& missing_columns) override;
169
170
    Status get_file_metadata_schema(const FieldDescriptor** ptr);
171
172
    void set_row_id_column_iterator(
173
5
            std::pair<std::shared_ptr<RowIdColumnIteratorV2>, int> iterator_pair) {
174
5
        _row_id_column_iterator_pair = iterator_pair;
175
5
    }
176
177
    void set_iceberg_rowid_params(const std::string& file_path, int32_t partition_spec_id,
178
                                  const std::string& partition_data_json, int row_id_column_pos);
179
180
0
    void set_row_lineage_columns(std::shared_ptr<RowLineageColumns> row_lineage_columns) {
181
0
        _row_lineage_columns = std::move(row_lineage_columns);
182
0
    }
183
184
14
    bool count_read_rows() override { return true; }
185
186
    void set_condition_cache_context(std::shared_ptr<ConditionCacheContext> ctx) override;
187
188
    int64_t get_total_rows() const override;
189
190
4
    bool has_delete_operations() const override {
191
4
        return _delete_rows != nullptr && !_delete_rows->empty();
192
4
    }
193
194
protected:
195
    void _collect_profile_before_close() override;
196
197
private:
198
    struct ParquetProfile {
199
        RuntimeProfile::Counter* filtered_row_groups = nullptr;
200
        RuntimeProfile::Counter* filtered_row_groups_by_min_max = nullptr;
201
        RuntimeProfile::Counter* filtered_row_groups_by_bloom_filter = nullptr;
202
        RuntimeProfile::Counter* to_read_row_groups = nullptr;
203
        RuntimeProfile::Counter* total_row_groups = nullptr;
204
        RuntimeProfile::Counter* filtered_group_rows = nullptr;
205
        RuntimeProfile::Counter* filtered_page_rows = nullptr;
206
        RuntimeProfile::Counter* lazy_read_filtered_rows = nullptr;
207
        RuntimeProfile::Counter* filtered_bytes = nullptr;
208
        RuntimeProfile::Counter* raw_rows_read = nullptr;
209
        RuntimeProfile::Counter* column_read_time = nullptr;
210
        RuntimeProfile::Counter* parse_meta_time = nullptr;
211
        RuntimeProfile::Counter* parse_footer_time = nullptr;
212
        RuntimeProfile::Counter* file_reader_create_time = nullptr;
213
        RuntimeProfile::Counter* open_file_num = nullptr;
214
        RuntimeProfile::Counter* row_group_filter_time = nullptr;
215
        RuntimeProfile::Counter* page_index_read_calls = nullptr;
216
        RuntimeProfile::Counter* page_index_filter_time = nullptr;
217
        RuntimeProfile::Counter* read_page_index_time = nullptr;
218
        RuntimeProfile::Counter* parse_page_index_time = nullptr;
219
        RuntimeProfile::Counter* file_footer_read_calls = nullptr;
220
        RuntimeProfile::Counter* file_footer_hit_cache = nullptr;
221
        RuntimeProfile::Counter* decompress_time = nullptr;
222
        RuntimeProfile::Counter* decompress_cnt = nullptr;
223
        RuntimeProfile::Counter* page_read_counter = nullptr;
224
        RuntimeProfile::Counter* page_cache_write_counter = nullptr;
225
        RuntimeProfile::Counter* page_cache_compressed_write_counter = nullptr;
226
        RuntimeProfile::Counter* page_cache_decompressed_write_counter = nullptr;
227
        RuntimeProfile::Counter* page_cache_hit_counter = nullptr;
228
        RuntimeProfile::Counter* page_cache_missing_counter = nullptr;
229
        RuntimeProfile::Counter* page_cache_compressed_hit_counter = nullptr;
230
        RuntimeProfile::Counter* page_cache_decompressed_hit_counter = nullptr;
231
        RuntimeProfile::Counter* decode_header_time = nullptr;
232
        RuntimeProfile::Counter* read_page_header_time = nullptr;
233
        RuntimeProfile::Counter* decode_value_time = nullptr;
234
        RuntimeProfile::Counter* decode_dict_time = nullptr;
235
        RuntimeProfile::Counter* decode_level_time = nullptr;
236
        RuntimeProfile::Counter* decode_null_map_time = nullptr;
237
        RuntimeProfile::Counter* skip_page_header_num = nullptr;
238
        RuntimeProfile::Counter* parse_page_header_num = nullptr;
239
        RuntimeProfile::Counter* predicate_filter_time = nullptr;
240
        RuntimeProfile::Counter* dict_filter_rewrite_time = nullptr;
241
        RuntimeProfile::Counter* bloom_filter_read_time = nullptr;
242
    };
243
244
    Status _open_file();
245
    void _init_profile();
246
    void _close_internal();
247
    Status _next_row_group_reader();
248
    RowGroupReader::PositionDeleteContext _get_position_delete_ctx(
249
            const tparquet::RowGroup& row_group,
250
            const RowGroupReader::RowGroupIndex& row_group_index);
251
    void _init_system_properties();
252
    void _init_file_description();
253
254
    // At the beginning of reading next row group, index should be loaded and used to filter data efficiently.
255
    Status _process_page_index_filter(
256
            const tparquet::RowGroup& row_group,
257
            const RowGroupReader::RowGroupIndex& row_group_index,
258
            const std::vector<std::unique_ptr<MutilColumnBlockPredicate>>& push_down_pred,
259
            RowRanges* candidate_row_ranges);
260
261
    // check this range contain this row group.
262
    bool _is_misaligned_range_group(const tparquet::RowGroup& row_group) const;
263
264
    // Row Group min-max Filter
265
    Status _process_column_stat_filter(
266
            const tparquet::RowGroup& row_group,
267
            const std::vector<std::unique_ptr<MutilColumnBlockPredicate>>& push_down_pred,
268
            bool* filter_group, bool* filtered_by_min_max, bool* filtered_by_bloom_filter);
269
270
    /*
271
     * 1. row group min-max filter
272
     * 2. row group bloom filter
273
     * 3. page index min-max filter
274
     *
275
     * return Status && row_ranges (lines to be read)
276
     */
277
    Status _process_min_max_bloom_filter(
278
            const RowGroupReader::RowGroupIndex& row_group_index,
279
            const tparquet::RowGroup& row_group,
280
            const std::vector<std::unique_ptr<MutilColumnBlockPredicate>>& push_down_pred,
281
            RowRanges* row_ranges);
282
283
    int64_t _get_column_start_offset(
284
            const tparquet::ColumnMetaData& column_init_column_readers) const;
285
0
    std::string _meta_cache_key(const std::string& path) { return "meta_" + path; }
286
    std::vector<io::PrefetchRange> _generate_random_access_ranges(
287
            const RowGroupReader::RowGroupIndex& group, size_t* avg_io_size);
288
    void _collect_profile();
289
290
19
    Status _set_read_one_line_impl() override { return Status::OK(); }
291
292
    bool _exists_in_file(const std::string& expr_name) const;
293
    bool _type_matches(const int cid) const;
294
295
    RuntimeProfile* _profile = nullptr;
296
    const TFileScanRangeParams& _scan_params;
297
    const TFileRangeDesc& _scan_range;
298
    io::FileSystemProperties _system_properties;
299
    io::FileDescription _file_description;
300
301
    // the following fields are for parquet meta data cache.
302
    // if _meta_cache is not null, the _file_metadata will be got from _meta_cache,
303
    // and it is owned by _meta_cache_handle.
304
    // if _meta_cache is null, _file_metadata will be managed by _file_metadata_ptr,
305
    // which will be released when deconstructing.
306
    // ATTN: these fields must be before _file_reader, to make sure they will be released
307
    // after _file_reader. Otherwise, there may be heap-use-after-free bug.
308
    ObjLRUCache::CacheHandle _meta_cache_handle;
309
    std::unique_ptr<FileMetaData> _file_metadata_ptr;
310
    const FileMetaData* _file_metadata = nullptr;
311
    const tparquet::FileMetaData* _t_metadata = nullptr;
312
313
    // _tracing_file_reader wraps _file_reader.
314
    // _file_reader is original file reader.
315
    // _tracing_file_reader is tracing file reader with io context.
316
    // If io_ctx is null, _tracing_file_reader will be the same as file_reader.
317
    io::FileReaderSPtr _file_reader = nullptr;
318
    io::FileReaderSPtr _tracing_file_reader = nullptr;
319
    std::unique_ptr<RowGroupReader> _current_group_reader;
320
321
    RowGroupReader::RowGroupIndex _current_row_group_index {-1, 0, 0};
322
    // read to the end of current reader
323
    bool _row_group_eof = true;
324
    size_t _total_groups; // num of groups(stripes) of a parquet(orc) file
325
326
    std::shared_ptr<ConditionCacheContext> _condition_cache_ctx;
327
328
    // Through this node, you can find the file column based on the table column.
329
    std::shared_ptr<TableSchemaChangeHelper::Node> _table_info_node_ptr =
330
            TableSchemaChangeHelper::ConstNode::get_instance();
331
332
    //sequence in file, need to read
333
    std::vector<std::string> _read_table_columns;
334
    std::vector<std::string> _read_file_columns;
335
    // The set of file columns to be read; only columns within this set will be filtered using the min-max predicate.
336
    std::set<std::string> _read_table_columns_set;
337
    // Deleted rows will be marked by Iceberg/Paimon. So we should filter deleted rows when reading it.
338
    const std::vector<int64_t>* _delete_rows = nullptr;
339
    int64_t _delete_rows_index = 0;
340
341
    // Used for column lazy read.
342
    RowGroupReader::LazyReadContext _lazy_read_ctx;
343
344
    // parquet file reader object
345
    size_t _batch_size;
346
    int64_t _range_start_offset;
347
    int64_t _range_size;
348
    const cctz::time_zone* _ctz = nullptr;
349
350
    std::unordered_map<int, tparquet::OffsetIndex> _col_offsets;
351
352
    std::vector<std::string> _missing_cols;
353
    // _table_column_names = _missing_cols + _read_table_columns
354
    const std::vector<std::string>* _table_column_names = nullptr;
355
356
    ReaderStatistics _reader_statistics;
357
    ParquetColumnReader::ColumnStatistics _column_statistics;
358
    ParquetProfile _parquet_profile;
359
    bool _closed = false;
360
    io::IOContext* _io_ctx = nullptr;
361
    std::shared_ptr<io::IOContext> _io_ctx_holder;
362
    RuntimeState* _state = nullptr;
363
    bool _enable_lazy_mat = true;
364
    bool _enable_filter_by_min_max = true;
365
    bool _enable_filter_by_bloom_filter = true;
366
    const TupleDescriptor* _tuple_descriptor = nullptr;
367
    const RowDescriptor* _row_descriptor = nullptr;
368
    const std::unordered_map<std::string, int>* _colname_to_slot_id = nullptr;
369
    const VExprContextSPtrs* _not_single_slot_filter_conjuncts = nullptr;
370
    const std::unordered_map<int, VExprContextSPtrs>* _slot_id_to_filter_conjuncts = nullptr;
371
    std::unordered_map<tparquet::Type::type, bool> _ignored_stats;
372
373
    std::pair<std::shared_ptr<RowIdColumnIteratorV2>, int> _row_id_column_iterator_pair = {nullptr,
374
                                                                                           -1};
375
    std::shared_ptr<RowLineageColumns> _row_lineage_columns;
376
377
protected:
378
    bool _filter_groups = true;
379
    RowGroupReader::IcebergRowIdParams _iceberg_rowid_params;
380
381
    std::set<uint64_t> _column_ids;
382
    std::set<uint64_t> _filter_column_ids;
383
384
    std::unordered_map<std::string, uint32_t>* _col_name_to_block_idx = nullptr;
385
386
    std::vector<std::unique_ptr<MutilColumnBlockPredicate>> _push_down_predicates;
387
    Arena _arena;
388
};
389
#include "common/compile_check_end.h"
390
391
} // namespace doris