Coverage Report

Created: 2026-03-27 17:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format/table/iceberg_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 <cstddef>
21
#include <cstdint>
22
#include <string>
23
#include <unordered_map>
24
#include <unordered_set>
25
#include <utility>
26
#include <vector>
27
28
#include "common/status.h"
29
#include "core/column/column_dictionary.h"
30
#include "core/data_type/define_primitive_type.h"
31
#include "core/data_type/primitive_type.h"
32
#include "exprs/vslot_ref.h"
33
#include "format/orc/vorc_reader.h"
34
#include "format/parquet/vparquet_reader.h"
35
#include "format/table/equality_delete.h"
36
#include "format/table/table_format_reader.h"
37
#include "storage/olap_scan_common.h"
38
39
namespace tparquet {
40
class KeyValue;
41
} // namespace tparquet
42
43
namespace doris {
44
#include "common/compile_check_begin.h"
45
class RowDescriptor;
46
class RuntimeState;
47
class SlotDescriptor;
48
class TFileRangeDesc;
49
class TFileScanRangeParams;
50
class TIcebergDeleteFileDesc;
51
class TupleDescriptor;
52
53
namespace io {
54
struct IOContext;
55
} // namespace io
56
template <typename T>
57
class ColumnStr;
58
using ColumnString = ColumnStr<UInt32>;
59
class Block;
60
class GenericReader;
61
class ShardedKVCache;
62
class VExprContext;
63
64
class IcebergTableReader : public TableFormatReader, public TableSchemaChangeHelper {
65
public:
66
    struct PositionDeleteRange {
67
        std::vector<std::string> data_file_path;
68
        std::vector<std::pair<int, int>> range;
69
    };
70
71
    IcebergTableReader(std::unique_ptr<GenericReader> file_format_reader, RuntimeProfile* profile,
72
                       RuntimeState* state, const TFileScanRangeParams& params,
73
                       const TFileRangeDesc& range, ShardedKVCache* kv_cache, io::IOContext* io_ctx,
74
                       FileMetaCache* meta_cache);
75
16
    ~IcebergTableReader() override = default;
76
77
0
    void set_need_row_id_column(bool need) { _need_row_id_column = need; }
78
0
    bool need_row_id_column() const { return _need_row_id_column; }
79
0
    void set_row_id_column_position(int position) { _row_id_column_position = position; }
80
81
    Status init_row_filters() final;
82
83
    Status get_next_block_inner(Block* block, size_t* read_rows, bool* eof) final;
84
85
    enum { DATA, POSITION_DELETE, EQUALITY_DELETE, DELETION_VECTOR };
86
    enum Fileformat { NONE, PARQUET, ORC, AVRO };
87
88
    virtual void set_delete_rows() = 0;
89
90
2
    bool has_delete_operations() const override {
91
2
        return _equality_delete_impls.size() > 0 || TableFormatReader::has_delete_operations();
92
2
    }
93
94
    Status read_deletion_vector(const std::string& data_file_path,
95
                                const TIcebergDeleteFileDesc& delete_file_desc);
96
97
protected:
98
    struct IcebergProfile {
99
        RuntimeProfile::Counter* num_delete_files;
100
        RuntimeProfile::Counter* num_delete_rows;
101
        RuntimeProfile::Counter* delete_files_read_time;
102
        RuntimeProfile::Counter* delete_rows_sort_time;
103
        RuntimeProfile::Counter* parse_delete_file_time;
104
    };
105
    using DeleteRows = std::vector<int64_t>;
106
    using DeleteFile = phmap::parallel_flat_hash_map<
107
            std::string, std::unique_ptr<DeleteRows>, std::hash<std::string>, std::equal_to<>,
108
            std::allocator<std::pair<const std::string, std::unique_ptr<DeleteRows>>>, 8,
109
            std::mutex>;
110
111
    // $row_id metadata column generation state
112
    bool _need_row_id_column = false;
113
    int _row_id_column_position = -1;
114
    /**
115
     * https://iceberg.apache.org/spec/#position-delete-files
116
     * The rows in the delete file must be sorted by file_path then position to optimize filtering rows while scanning.
117
     * Sorting by file_path allows filter pushdown by file in columnar storage formats.
118
     * Sorting by position allows filtering rows while scanning, to avoid keeping deletes in memory.
119
     */
120
    static void _sort_delete_rows(const std::vector<std::vector<int64_t>*>& delete_rows_array,
121
                                  int64_t num_delete_rows, std::vector<int64_t>& result);
122
123
    PositionDeleteRange _get_range(const ColumnDictI32& file_path_column);
124
125
    PositionDeleteRange _get_range(const ColumnString& file_path_column);
126
127
0
    static std::string _delet_file_cache_key(const std::string& path) { return "delete_" + path; }
128
129
    Status _position_delete_base(const std::string data_file_path,
130
                                 const std::vector<TIcebergDeleteFileDesc>& delete_files);
131
    virtual Status _process_equality_delete(
132
            const std::vector<TIcebergDeleteFileDesc>& delete_files) = 0;
133
    void _generate_equality_delete_block(Block* block,
134
                                         const std::vector<std::string>& equality_delete_col_names,
135
                                         const std::vector<DataTypePtr>& equality_delete_col_types);
136
    // Equality delete should read the primary columns. Add the missing columns
137
    Status _expand_block_if_need(Block* block);
138
    // Remove the added delete columns
139
    Status _shrink_block_if_need(Block* block);
140
141
    // owned by scan node
142
    ShardedKVCache* _kv_cache;
143
    IcebergProfile _iceberg_profile;
144
    // _iceberg_delete_rows from kv_cache
145
    const std::vector<int64_t>* _iceberg_delete_rows = nullptr;
146
147
    // Pointer to external column name to block index mapping (from FileScanner)
148
    // Used to dynamically add expand columns for equality delete
149
    std::unordered_map<std::string, uint32_t>* _col_name_to_block_idx = nullptr;
150
151
    Fileformat _file_format = Fileformat::NONE;
152
153
    const int64_t MIN_SUPPORT_DELETE_FILES_VERSION = 2;
154
    const std::string ICEBERG_FILE_PATH = "file_path";
155
    const std::string ICEBERG_ROW_POS = "pos";
156
    const std::vector<std::string> delete_file_col_names {ICEBERG_FILE_PATH, ICEBERG_ROW_POS};
157
    const std::unordered_map<std::string, uint32_t> DELETE_COL_NAME_TO_BLOCK_IDX = {
158
            {ICEBERG_FILE_PATH, 0}, {ICEBERG_ROW_POS, 1}};
159
    const int ICEBERG_FILE_PATH_INDEX = 0;
160
    const int ICEBERG_FILE_POS_INDEX = 1;
161
    const int READ_DELETE_FILE_BATCH_SIZE = 102400;
162
163
    //Read position_delete_file  TFileRangeDesc, generate DeleteFile
164
    virtual Status _read_position_delete_file(const TFileRangeDesc*, DeleteFile*) = 0;
165
166
    void _gen_position_delete_file_range(Block& block, DeleteFile* const position_delete,
167
                                         size_t read_rows, bool file_path_column_dictionary_coded);
168
169
    // read table colummn + extra equality delete columns
170
    std::vector<std::string> _all_required_col_names;
171
172
    // extra equality delete name and type
173
    std::vector<std::string> _expand_col_names;
174
    std::vector<ColumnWithTypeAndName> _expand_columns;
175
176
    // all ids that need read for eq delete (from all qe delte file.)
177
    std::set<int> _equality_delete_col_ids;
178
    // eq delete column ids -> location of _equality_delete_blocks / _equality_delete_impls
179
    std::map<std::vector<int>, int> _equality_delete_block_map;
180
    // EqualityDeleteBase stores raw pointers to these blocks, so do not modify this vector after
181
    // creating entries in _equality_delete_impls.
182
    std::vector<Block> _equality_delete_blocks;
183
    std::vector<std::unique_ptr<EqualityDeleteBase>> _equality_delete_impls;
184
185
    // id -> block column name.
186
    std::unordered_map<int, std::string> _id_to_block_column_name;
187
};
188
189
class IcebergParquetReader final : public IcebergTableReader {
190
public:
191
    ENABLE_FACTORY_CREATOR(IcebergParquetReader);
192
193
    IcebergParquetReader(std::unique_ptr<GenericReader> file_format_reader, RuntimeProfile* profile,
194
                         RuntimeState* state, const TFileScanRangeParams& params,
195
                         const TFileRangeDesc& range, ShardedKVCache* kv_cache,
196
                         io::IOContext* io_ctx, FileMetaCache* meta_cache)
197
7
            : IcebergTableReader(std::move(file_format_reader), profile, state, params, range,
198
7
                                 kv_cache, io_ctx, meta_cache) {}
199
    Status init_reader(
200
            const std::vector<std::string>& file_col_names,
201
            std::unordered_map<std::string, uint32_t>* col_name_to_block_idx,
202
            const VExprContextSPtrs& conjuncts,
203
            phmap::flat_hash_map<int, std::vector<std::shared_ptr<ColumnPredicate>>>&
204
                    slot_id_to_predicates,
205
            const TupleDescriptor* tuple_descriptor, const RowDescriptor* row_descriptor,
206
            const std::unordered_map<std::string, int>* colname_to_slot_id,
207
            const VExprContextSPtrs* not_single_slot_filter_conjuncts,
208
            const std::unordered_map<int, VExprContextSPtrs>* slot_id_to_filter_conjuncts);
209
210
0
    void set_delete_rows() final {
211
0
        auto* parquet_reader = (ParquetReader*)(_file_format_reader.get());
212
0
        parquet_reader->set_delete_rows(_iceberg_delete_rows);
213
0
    }
214
215
private:
216
    static ColumnIdResult _create_column_ids(const FieldDescriptor* field_desc,
217
                                             const TupleDescriptor* tuple_descriptor);
218
219
    Status _read_position_delete_file(const TFileRangeDesc* delete_range,
220
                                      DeleteFile* position_delete) final;
221
    Status _process_equality_delete(const std::vector<TIcebergDeleteFileDesc>& delete_files) final;
222
223
    const FieldDescriptor* _data_file_field_desc = nullptr;
224
};
225
class IcebergOrcReader final : public IcebergTableReader {
226
public:
227
    ENABLE_FACTORY_CREATOR(IcebergOrcReader);
228
229
    Status _read_position_delete_file(const TFileRangeDesc* delete_range,
230
                                      DeleteFile* position_delete) final;
231
232
    IcebergOrcReader(std::unique_ptr<GenericReader> file_format_reader, RuntimeProfile* profile,
233
                     RuntimeState* state, const TFileScanRangeParams& params,
234
                     const TFileRangeDesc& range, ShardedKVCache* kv_cache, io::IOContext* io_ctx,
235
                     FileMetaCache* meta_cache)
236
7
            : IcebergTableReader(std::move(file_format_reader), profile, state, params, range,
237
7
                                 kv_cache, io_ctx, meta_cache) {}
238
239
0
    void set_delete_rows() final {
240
0
        auto* orc_reader = (OrcReader*)_file_format_reader.get();
241
0
        orc_reader->set_position_delete_rowids(_iceberg_delete_rows);
242
0
    }
243
244
    Status init_reader(
245
            const std::vector<std::string>& file_col_names,
246
            std::unordered_map<std::string, uint32_t>* col_name_to_block_idx,
247
            const VExprContextSPtrs& conjuncts, const TupleDescriptor* tuple_descriptor,
248
            const RowDescriptor* row_descriptor,
249
            const std::unordered_map<std::string, int>* colname_to_slot_id,
250
            const VExprContextSPtrs* not_single_slot_filter_conjuncts,
251
            const std::unordered_map<int, VExprContextSPtrs>* slot_id_to_filter_conjuncts);
252
253
private:
254
    Status _process_equality_delete(const std::vector<TIcebergDeleteFileDesc>& delete_files) final;
255
256
    static ColumnIdResult _create_column_ids(const orc::Type* orc_type,
257
                                             const TupleDescriptor* tuple_descriptor);
258
259
private:
260
    static const std::string ICEBERG_ORC_ATTRIBUTE;
261
    const orc::Type* _data_file_type_desc = nullptr;
262
};
263
264
#include "common/compile_check_end.h"
265
} // namespace doris