Coverage Report

Created: 2026-03-31 00:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format/parquet/vparquet_group_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
#pragma once
18
#include <stddef.h>
19
#include <stdint.h>
20
21
#include <limits>
22
#include <memory>
23
#include <string>
24
#include <tuple>
25
#include <unordered_map>
26
#include <utility>
27
#include <vector>
28
29
#include "common/status.h"
30
#include "core/column/column.h"
31
#include "exprs/vexpr_fwd.h"
32
#include "format/parquet/parquet_common.h"
33
#include "format/parquet/vparquet_column_reader.h"
34
#include "format/table/table_format_reader.h"
35
#include "io/fs/file_reader_writer_fwd.h"
36
#include "storage/id_manager.h"
37
#include "storage/utils.h"
38
39
namespace cctz {
40
class time_zone;
41
} // namespace cctz
42
namespace doris {
43
class ObjectPool;
44
class RowDescriptor;
45
class RuntimeState;
46
class SlotDescriptor;
47
class TupleDescriptor;
48
49
namespace io {
50
struct IOContext;
51
} // namespace io
52
class Block;
53
class FieldDescriptor;
54
struct RowLineageColumns;
55
} // namespace doris
56
namespace tparquet {
57
class ColumnMetaData;
58
class OffsetIndex;
59
class RowGroup;
60
} // namespace tparquet
61
62
namespace doris::segment_v2 {
63
class RowIdColumnIteratorV2;
64
}
65
66
namespace doris {
67
#include "common/compile_check_begin.h"
68
// TODO: we need to determine it by test.
69
70
class RowGroupReader : public ProfileCollector {
71
public:
72
    struct IcebergRowIdParams {
73
        bool enabled = false;
74
        std::string file_path;
75
        int32_t partition_spec_id = 0;
76
        std::string partition_data_json;
77
        int row_id_column_pos = -1;
78
    };
79
    std::shared_ptr<TableSchemaChangeHelper::Node> _table_info_node_ptr;
80
    static const std::vector<int64_t> NO_DELETE;
81
82
    struct RowGroupIndex {
83
        int32_t row_group_id;
84
        int64_t first_row;
85
        int64_t last_row;
86
        RowGroupIndex(int32_t id, int64_t first, int64_t last)
87
188
                : row_group_id(id), first_row(first), last_row(last) {}
88
    };
89
90
    // table name
91
    struct LazyReadContext {
92
        // all conjuncts: in sql, join runtime filter, topn runtime filter.
93
        VExprContextSPtrs conjuncts;
94
95
        // ParquetReader::set_fill_columns(xxx, xxx) will set these two members
96
        std::unordered_map<std::string, std::tuple<std::string, const SlotDescriptor*>>
97
                fill_partition_columns;
98
        std::unordered_map<std::string, VExprContextSPtr> fill_missing_columns;
99
100
        phmap::flat_hash_map<int, std::vector<std::shared_ptr<ColumnPredicate>>>
101
                slot_id_to_predicates;
102
        bool can_lazy_read = false;
103
        // block->rows() returns the number of rows of the first column,
104
        // so we should check and resize the first column
105
        bool resize_first_column = true;
106
        std::vector<std::string> all_read_columns;
107
        // include predicate_partition_columns & predicate_missing_columns
108
        std::vector<uint32_t> all_predicate_col_ids;
109
        // save slot_id to find dict filter column name, because expr column name may
110
        // be different with parquet column name
111
        // std::pair<std::vector<col_name>, std::vector<slot_id>>
112
        std::pair<std::vector<std::string>, std::vector<int>> predicate_columns;
113
        std::vector<std::string> lazy_read_columns;
114
        std::unordered_map<std::string, std::tuple<std::string, const SlotDescriptor*>>
115
                predicate_partition_columns;
116
        // lazy read partition columns or all partition columns
117
        std::unordered_map<std::string, std::tuple<std::string, const SlotDescriptor*>>
118
                partition_columns;
119
        std::unordered_map<std::string, VExprContextSPtr> predicate_missing_columns;
120
        VExprContextSPtrs missing_columns_conjuncts;
121
        // lazy read missing columns or all missing columns
122
        std::unordered_map<std::string, VExprContextSPtr> missing_columns;
123
        // should turn off filtering by page index, lazy read and dict filter if having complex type
124
        bool has_complex_type = false;
125
    };
126
127
    /**
128
     * Support row-level delete in iceberg:
129
     * https://iceberg.apache.org/spec/#position-delete-files
130
     */
131
    struct PositionDeleteContext {
132
        // the filtered rows in current row group
133
        const std::vector<int64_t>& delete_rows;
134
        // the first row id of current row group in parquet file
135
        const int64_t first_row_id;
136
        // the number of rows in current row group
137
        const int64_t num_rows;
138
        const int64_t last_row_id;
139
        // current row id to read in the row group
140
        int64_t current_row_id;
141
        // start index in delete_rows
142
        const int64_t start_index;
143
        // end index in delete_rows
144
        const int64_t end_index;
145
        // current index in delete_rows
146
        int64_t index;
147
        const bool has_filter;
148
149
        PositionDeleteContext(const std::vector<int64_t>& delete_rows, const int64_t num_rows,
150
                              const int64_t first_row_id, const int64_t start_index,
151
                              const int64_t end_index)
152
37
                : delete_rows(delete_rows),
153
37
                  first_row_id(first_row_id),
154
37
                  num_rows(num_rows),
155
37
                  last_row_id(first_row_id + num_rows),
156
37
                  current_row_id(first_row_id),
157
37
                  start_index(start_index),
158
37
                  end_index(end_index),
159
37
                  index(start_index),
160
37
                  has_filter(end_index > start_index) {}
161
162
        PositionDeleteContext(const int64_t num_rows, const int64_t first_row)
163
37
                : PositionDeleteContext(NO_DELETE, num_rows, first_row, 0, 0) {}
164
165
        PositionDeleteContext(const PositionDeleteContext& filter) = default;
166
    };
167
168
    RowGroupReader(io::FileReaderSPtr file_reader, const std::vector<std::string>& read_columns,
169
                   const int32_t row_group_id, const tparquet::RowGroup& row_group,
170
                   const cctz::time_zone* ctz, io::IOContext* io_ctx,
171
                   const PositionDeleteContext& position_delete_ctx,
172
                   const LazyReadContext& lazy_read_ctx, RuntimeState* state,
173
                   const std::set<uint64_t>& column_ids,
174
                   const std::set<uint64_t>& filter_column_ids);
175
176
    ~RowGroupReader();
177
    Status init(const FieldDescriptor& schema, RowRanges& row_ranges,
178
                std::unordered_map<int, tparquet::OffsetIndex>& col_offsets,
179
                const TupleDescriptor* tuple_descriptor, const RowDescriptor* row_descriptor,
180
                const std::unordered_map<std::string, int>* colname_to_slot_id,
181
                const VExprContextSPtrs* not_single_slot_filter_conjuncts,
182
                const std::unordered_map<int, VExprContextSPtrs>* slot_id_to_filter_conjuncts);
183
    Status next_batch(Block* block, size_t batch_size, size_t* read_rows, bool* batch_eof);
184
37
    int64_t lazy_read_filtered_rows() const { return _lazy_read_filtered_rows; }
185
37
    int64_t predicate_filter_time() const { return _predicate_filter_time; }
186
37
    int64_t dict_filter_rewrite_time() const { return _dict_filter_rewrite_time; }
187
14
    int64_t condition_cache_filtered_rows() const { return _condition_cache_filtered_rows; }
188
189
    ParquetColumnReader::ColumnStatistics merged_column_statistics();
190
0
    void set_remaining_rows(int64_t rows) { _remaining_rows = rows; }
191
0
    int64_t get_remaining_rows() { return _remaining_rows; }
192
193
    // Filters read_ranges by removing row chunks whose condition cache granules are all-false.
194
    // Pure algorithm, exposed as static for testability.
195
    static RowRanges filter_ranges_by_cache(const RowRanges& read_ranges,
196
                                            const std::vector<bool>& cache, int64_t first_row,
197
                                            int64_t base_granule = 0);
198
199
    void set_row_id_column_iterator(
200
            const std::pair<std::shared_ptr<segment_v2::RowIdColumnIteratorV2>, int>&
201
37
                    iterator_pair) {
202
37
        _row_id_column_iterator_pair = iterator_pair;
203
37
    }
204
205
0
    void set_iceberg_rowid_params(const IcebergRowIdParams& params) {
206
0
        _iceberg_rowid_params = params;
207
0
    }
208
209
37
    void set_row_lineage_columns(std::shared_ptr<RowLineageColumns> row_lineage_columns) {
210
37
        _row_lineage_columns = std::move(row_lineage_columns);
211
37
    }
212
213
37
    void set_current_row_group_idx(RowGroupIndex row_group_idx) {
214
37
        _current_row_group_idx = row_group_idx;
215
37
    }
216
217
    void set_col_name_to_block_idx(
218
37
            std::unordered_map<std::string, uint32_t>* col_name_to_block_idx) {
219
37
        _col_name_to_block_idx = col_name_to_block_idx;
220
37
    }
221
222
0
    void set_condition_cache_context(std::shared_ptr<ConditionCacheContext> ctx) {
223
0
        _condition_cache_ctx = std::move(ctx);
224
0
    }
225
226
protected:
227
15
    void _collect_profile_before_close() override {
228
15
        if (_file_reader != nullptr) {
229
15
            _file_reader->collect_profile_before_close();
230
15
        }
231
15
    }
232
233
private:
234
    Status _read_empty_batch(size_t batch_size, size_t* read_rows, bool* batch_eof,
235
                             bool* modify_row_ids);
236
237
    Status _read_column_data(Block* block, const std::vector<std::string>& columns,
238
                             size_t batch_size, size_t* read_rows, bool* batch_eof,
239
                             FilterMap& filter_map);
240
241
    Status _do_lazy_read(Block* block, size_t batch_size, size_t* read_rows, bool* batch_eof);
242
    Status _rebuild_filter_map(FilterMap& filter_map,
243
                               DorisUniqueBufferPtr<uint8_t>& filter_map_data,
244
                               size_t pre_read_rows) const;
245
    Status _fill_partition_columns(
246
            Block* block, size_t rows,
247
            const std::unordered_map<std::string, std::tuple<std::string, const SlotDescriptor*>>&
248
                    partition_columns);
249
    Status _fill_missing_columns(
250
            Block* block, size_t rows,
251
            const std::unordered_map<std::string, VExprContextSPtr>& missing_columns);
252
    Status _build_pos_delete_filter(size_t read_rows);
253
    Status _filter_block(Block* block, int column_to_keep,
254
                         const std::vector<uint32_t>& columns_to_filter);
255
    Status _filter_block_internal(Block* block, const std::vector<uint32_t>& columns_to_filter,
256
                                  const IColumn::Filter& filter);
257
258
    bool _can_filter_by_dict(int slot_id, const tparquet::ColumnMetaData& column_metadata);
259
    bool is_dictionary_encoded(const tparquet::ColumnMetaData& column_metadata);
260
    Status _rewrite_dict_predicates();
261
    Status _rewrite_dict_conjuncts(std::vector<int32_t>& dict_codes, int slot_id, bool is_nullable);
262
    Status _convert_dict_cols_to_string_cols(Block* block);
263
    void _filter_read_ranges_by_condition_cache();
264
    void _mark_condition_cache_granules(const uint8_t* filter_data, size_t num_rows,
265
                                        int64_t batch_seq_start);
266
267
    Status _get_current_batch_row_id(size_t read_rows);
268
    Status _fill_row_id_columns(Block* block, size_t read_rows, bool is_current_row_ids);
269
    Status _append_iceberg_rowid_column(Block* block, size_t read_rows, bool is_current_row_ids);
270
271
    io::FileReaderSPtr _file_reader;
272
    std::unordered_map<std::string, std::unique_ptr<ParquetColumnReader>>
273
            _column_readers; // table_column_name
274
    const std::vector<std::string>& _read_table_columns;
275
276
    const int32_t _row_group_id;
277
    const tparquet::RowGroup& _row_group_meta;
278
    int64_t _remaining_rows;
279
    const cctz::time_zone* _ctz = nullptr;
280
    io::IOContext* _io_ctx = nullptr;
281
    PositionDeleteContext _position_delete_ctx;
282
    std::shared_ptr<RowLineageColumns> _row_lineage_columns;
283
    // merge the row ranges generated from page index and position delete.
284
    RowRanges _read_ranges;
285
286
    const LazyReadContext& _lazy_read_ctx;
287
    int64_t _lazy_read_filtered_rows = 0;
288
    int64_t _predicate_filter_time = 0;
289
    int64_t _dict_filter_rewrite_time = 0;
290
    int64_t _condition_cache_filtered_rows = 0;
291
    // If continuous batches are skipped, we can cache them to skip a whole page
292
    size_t _cached_filtered_rows = 0;
293
    std::shared_ptr<ConditionCacheContext> _condition_cache_ctx;
294
    std::unique_ptr<IColumn::Filter> _pos_delete_filter_ptr;
295
    int64_t _total_read_rows = 0;
296
    const TupleDescriptor* _tuple_descriptor = nullptr;
297
    const RowDescriptor* _row_descriptor = nullptr;
298
    const std::unordered_map<std::string, int>* _col_name_to_slot_id = nullptr;
299
    const std::unordered_map<int, VExprContextSPtrs>* _slot_id_to_filter_conjuncts = nullptr;
300
    VExprContextSPtrs _dict_filter_conjuncts;
301
    VExprContextSPtrs _filter_conjuncts;
302
    // std::pair<col_name, slot_id>
303
    std::vector<std::pair<std::string, int>> _dict_filter_cols;
304
    RuntimeState* _state = nullptr;
305
    std::shared_ptr<ObjectPool> _obj_pool;
306
    const std::set<uint64_t>& _column_ids;
307
    const std::set<uint64_t>& _filter_column_ids;
308
    bool _is_row_group_filtered = false;
309
310
    RowGroupIndex _current_row_group_idx {0, 0, 0};
311
    std::pair<std::shared_ptr<segment_v2::RowIdColumnIteratorV2>, int>
312
            _row_id_column_iterator_pair = {nullptr, -1};
313
    std::vector<rowid_t> _current_batch_row_ids;
314
315
    std::unordered_map<std::string, uint32_t>* _col_name_to_block_idx = nullptr;
316
    IcebergRowIdParams _iceberg_rowid_params;
317
};
318
#include "common/compile_check_end.h"
319
320
} // namespace doris