Coverage Report

Created: 2026-04-02 17:01

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