Coverage Report

Created: 2026-04-11 14:25

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