Coverage Report

Created: 2026-03-31 18:42

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