Coverage Report

Created: 2026-07-08 19:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format_v2/table/iceberg_reader.cpp
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
#include "format_v2/table/iceberg_reader.h"
19
20
#include <algorithm>
21
#include <memory>
22
#include <sstream>
23
#include <utility>
24
25
#include "common/cast_set.h"
26
#include "common/consts.h"
27
#include "core/assert_cast.h"
28
#include "core/block/block.h"
29
#include "core/column/column_const.h"
30
#include "core/column/column_nullable.h"
31
#include "core/column/column_string.h"
32
#include "core/column/column_struct.h"
33
#include "core/column/column_vector.h"
34
#include "core/data_type/data_type_number.h"
35
#include "core/data_type/define_primitive_type.h"
36
#include "core/field.h"
37
#include "exprs/vslot_ref.h"
38
#include "format/table/deletion_vector_reader.h"
39
#include "format_v2/expr/cast.h"
40
#include "format_v2/expr/equality_delete_predicate.h"
41
#include "format_v2/parquet/parquet_reader.h"
42
#include "format_v2/parquet/reader/column_reader.h"
43
#include "format_v2/table_reader.h"
44
#include "io/file_factory.h"
45
46
namespace doris::format::iceberg {
47
48
static constexpr const char* ROW_LINEAGE_ROW_ID = "_row_id";
49
static constexpr int32_t ROW_LINEAGE_ROW_ID_FIELD_ID = 2147483540;
50
51
template <typename T>
52
0
static std::string join_values_for_debug(const std::vector<T>& values) {
53
0
    std::ostringstream out;
54
0
    out << "[";
55
0
    for (size_t idx = 0; idx < values.size(); ++idx) {
56
0
        if (idx > 0) {
57
0
            out << ", ";
58
0
        }
59
0
        out << values[idx];
60
0
    }
61
0
    out << "]";
62
0
    return out.str();
63
0
}
64
65
1
static bool is_projected_row_lineage_row_id(const format::ColumnDefinition& column) {
66
    // Iceberg row lineage columns can be bound by field id when a mapper has already been built,
67
    // but customize_file_scan_request() is also exercised directly by scan-request tests before the
68
    // mapper exists. In that path, inspect the projected table schema so row-position dependencies
69
    // are still added for `_row_id`.
70
1
    return column.name == ROW_LINEAGE_ROW_ID ||
71
1
           (column.has_identifier_field_id() &&
72
0
            column.get_identifier_field_id() == ROW_LINEAGE_ROW_ID_FIELD_ID);
73
1
}
74
75
15
static bool is_projected_iceberg_rowid(const format::ColumnDefinition& column) {
76
15
    return column.name == BeConsts::ICEBERG_ROWID_COL;
77
15
}
78
79
0
static std::string iceberg_delete_file_debug_string(const TIcebergDeleteFileDesc& delete_file) {
80
0
    std::ostringstream out;
81
0
    out << "TIcebergDeleteFileDesc{path=" << (delete_file.__isset.path ? delete_file.path : "null")
82
0
        << ", content=" << (delete_file.__isset.content ? delete_file.content : -1)
83
0
        << ", file_format="
84
0
        << (delete_file.__isset.file_format ? static_cast<int>(delete_file.file_format) : -1)
85
0
        << ", position_lower_bound="
86
0
        << (delete_file.__isset.position_lower_bound ? delete_file.position_lower_bound : -1)
87
0
        << ", position_upper_bound="
88
0
        << (delete_file.__isset.position_upper_bound ? delete_file.position_upper_bound : -1)
89
0
        << ", field_ids="
90
0
        << (delete_file.__isset.field_ids ? join_values_for_debug(delete_file.field_ids) : "[]")
91
0
        << ", content_offset="
92
0
        << (delete_file.__isset.content_offset ? delete_file.content_offset : -1)
93
0
        << ", content_size_in_bytes="
94
0
        << (delete_file.__isset.content_size_in_bytes ? delete_file.content_size_in_bytes : -1)
95
0
        << "}";
96
0
    return out.str();
97
0
}
98
99
static std::string iceberg_delete_files_debug_string(
100
0
        const std::vector<TIcebergDeleteFileDesc>& delete_files) {
101
0
    std::ostringstream out;
102
0
    out << "[";
103
0
    for (size_t idx = 0; idx < delete_files.size(); ++idx) {
104
0
        if (idx > 0) {
105
0
            out << ", ";
106
0
        }
107
0
        out << iceberg_delete_file_debug_string(delete_files[idx]);
108
0
    }
109
0
    out << "]";
110
0
    return out.str();
111
0
}
112
113
0
static std::string iceberg_params_debug_string(const std::optional<TIcebergFileDesc>& params) {
114
0
    if (!params.has_value()) {
115
0
        return "null";
116
0
    }
117
0
    const auto& iceberg_params = *params;
118
0
    std::ostringstream out;
119
0
    out << "TIcebergFileDesc{format_version="
120
0
        << (iceberg_params.__isset.format_version ? iceberg_params.format_version : -1)
121
0
        << ", content=" << (iceberg_params.__isset.content ? iceberg_params.content : -1)
122
0
        << ", original_file_path="
123
0
        << (iceberg_params.__isset.original_file_path ? iceberg_params.original_file_path : "null")
124
0
        << ", row_count=" << (iceberg_params.__isset.row_count ? iceberg_params.row_count : -1)
125
0
        << ", partition_spec_id="
126
0
        << (iceberg_params.__isset.partition_spec_id ? iceberg_params.partition_spec_id : 0)
127
0
        << ", has_partition_data_json=" << iceberg_params.__isset.partition_data_json
128
0
        << ", first_row_id="
129
0
        << (iceberg_params.__isset.first_row_id ? iceberg_params.first_row_id : -1)
130
0
        << ", last_updated_sequence_number="
131
0
        << (iceberg_params.__isset.last_updated_sequence_number
132
0
                    ? iceberg_params.last_updated_sequence_number
133
0
                    : -1)
134
0
        << ", delete_file_count="
135
0
        << (iceberg_params.__isset.delete_files ? iceberg_params.delete_files.size() : 0)
136
0
        << ", delete_files="
137
0
        << (iceberg_params.__isset.delete_files
138
0
                    ? iceberg_delete_files_debug_string(iceberg_params.delete_files)
139
0
                    : "[]")
140
0
        << ", has_serialized_split=" << iceberg_params.__isset.serialized_split << "}";
141
0
    return out.str();
142
0
}
143
144
IcebergTableReader::PositionDeleteRowsCollector::PositionDeleteRowsCollector(
145
        std::string data_file_path, format::DeleteRows* rows)
146
5
        : _data_file_path(std::move(data_file_path)), _rows(rows) {}
147
148
Status IcebergTableReader::PositionDeleteRowsCollector::collect(const Block& block,
149
10
                                                                size_t read_rows) {
150
10
    if (read_rows == 0) {
151
5
        return Status::OK();
152
5
    }
153
5
    const auto& file_path_column = assert_cast<const ColumnString&>(
154
5
            *remove_nullable((block.get_by_position(ICEBERG_FILE_PATH_BLOCK_POSITION).column)));
155
5
    const auto& pos_column = assert_cast<const ColumnInt64&>(
156
5
            *remove_nullable(block.get_by_position(ICEBERG_ROW_POS_BLOCK_POSITION).column));
157
12
    for (size_t row = 0; row < read_rows; ++row) {
158
7
        const auto file_path = file_path_column.get_data_at(row).to_string();
159
7
        if (file_path == _data_file_path) {
160
6
            _rows->push_back(pos_column.get_element(row));
161
6
        }
162
7
    }
163
5
    return Status::OK();
164
10
}
165
166
21
Status IcebergTableReader::prepare_split(const format::SplitReadOptions& options) {
167
21
    _row_lineage_columns = {};
168
21
    _iceberg_params.reset();
169
21
    _delete_predicates_initialized = false;
170
21
    _position_delete_rows_storage.clear();
171
21
    _equality_delete_filters.clear();
172
21
    if (options.current_range.__isset.table_format_params &&
173
21
        options.current_range.table_format_params.__isset.iceberg_params) {
174
19
        const auto& iceberg_params = options.current_range.table_format_params.iceberg_params;
175
19
        _iceberg_params = iceberg_params;
176
19
        if (iceberg_params.__isset.first_row_id) {
177
8
            _row_lineage_columns.first_row_id = iceberg_params.first_row_id;
178
8
        }
179
19
        if (iceberg_params.__isset.last_updated_sequence_number) {
180
6
            _row_lineage_columns.last_updated_sequence_number =
181
6
                    iceberg_params.last_updated_sequence_number;
182
6
        }
183
19
    }
184
21
    RETURN_IF_ERROR(TableReader::prepare_split(options));
185
21
    if (_is_table_level_count_active()) {
186
1
        return Status::OK();
187
1
    }
188
20
    RETURN_IF_ERROR(_init_delete_predicates(options.current_range.table_format_params));
189
20
    return Status::OK();
190
20
}
191
192
0
std::string IcebergTableReader::debug_string() const {
193
0
    size_t position_delete_file_count = 0;
194
0
    size_t equality_delete_file_count = 0;
195
0
    size_t deletion_vector_file_count = 0;
196
0
    if (_iceberg_params.has_value() && _iceberg_params->__isset.delete_files) {
197
0
        for (const auto& delete_file : _iceberg_params->delete_files) {
198
0
            if (!delete_file.__isset.content) {
199
0
                continue;
200
0
            }
201
0
            if (delete_file.content == POSITION_DELETE) {
202
0
                ++position_delete_file_count;
203
0
            } else if (delete_file.content == EQUALITY_DELETE) {
204
0
                ++equality_delete_file_count;
205
0
            } else if (delete_file.content == DELETION_VECTOR) {
206
0
                ++deletion_vector_file_count;
207
0
            }
208
0
        }
209
0
    }
210
211
0
    std::ostringstream equality_filters;
212
0
    equality_filters << "[";
213
0
    for (size_t idx = 0; idx < _equality_delete_filters.size(); ++idx) {
214
0
        if (idx > 0) {
215
0
            equality_filters << ", ";
216
0
        }
217
0
        const auto& filter = _equality_delete_filters[idx];
218
0
        equality_filters << "EqualityDeleteFilter{field_ids="
219
0
                         << join_values_for_debug(filter.field_ids) << ", key_types=[";
220
0
        for (size_t type_idx = 0; type_idx < filter.key_types.size(); ++type_idx) {
221
0
            if (type_idx > 0) {
222
0
                equality_filters << ", ";
223
0
            }
224
0
            equality_filters << (filter.key_types[type_idx] == nullptr
225
0
                                         ? "null"
226
0
                                         : filter.key_types[type_idx]->get_name());
227
0
        }
228
0
        equality_filters << "], delete_block_rows=" << filter.delete_block.rows()
229
0
                         << ", delete_block_columns=" << filter.delete_block.columns() << "}";
230
0
    }
231
0
    equality_filters << "]";
232
233
0
    std::ostringstream out;
234
0
    out << "IcebergTableReader{base=" << format::TableReader::debug_string()
235
0
        << ", iceberg_params=" << iceberg_params_debug_string(_iceberg_params)
236
0
        << ", row_lineage_first_row_id=" << _row_lineage_columns.first_row_id
237
0
        << ", row_lineage_last_updated_sequence_number="
238
0
        << _row_lineage_columns.last_updated_sequence_number
239
0
        << ", need_row_lineage_row_id=" << _need_row_lineage_row_id()
240
0
        << ", need_iceberg_rowid=" << _need_iceberg_rowid()
241
0
        << ", row_position_block_position=" << _row_position_block_position
242
0
        << ", delete_predicates_initialized=" << _delete_predicates_initialized
243
0
        << ", position_delete_file_count=" << position_delete_file_count
244
0
        << ", equality_delete_file_count=" << equality_delete_file_count
245
0
        << ", deletion_vector_file_count=" << deletion_vector_file_count
246
0
        << ", position_delete_rows_storage_count=" << _position_delete_rows_storage.size()
247
0
        << ", equality_delete_filter_count=" << _equality_delete_filters.size()
248
0
        << ", equality_delete_filters=" << equality_filters.str() << "}";
249
0
    return out.str();
250
0
}
251
252
19
Status IcebergTableReader::materialize_virtual_columns(Block* table_block) {
253
56
    for (size_t column_idx = 0; column_idx < _data_reader.column_mapper->mappings().size();
254
37
         ++column_idx) {
255
37
        const auto& mapping = _data_reader.column_mapper->mappings()[column_idx];
256
37
        switch (mapping.virtual_column_type) {
257
9
        case format::TableVirtualColumnType::ROW_ID:
258
9
            RETURN_IF_ERROR(_materialize_row_lineage_row_id(table_block, column_idx));
259
9
            break;
260
9
        case format::TableVirtualColumnType::LAST_UPDATED_SEQUENCE_NUMBER:
261
8
            RETURN_IF_ERROR(
262
8
                    _materialize_row_lineage_last_updated_sequence_number(table_block, column_idx));
263
8
            break;
264
8
        case format::TableVirtualColumnType::ICEBERG_ROWID:
265
1
            RETURN_IF_ERROR(_materialize_iceberg_rowid(table_block, column_idx));
266
1
            break;
267
19
        case format::TableVirtualColumnType::INVALID:
268
19
            break;
269
37
        }
270
37
    }
271
19
    return Status::OK();
272
19
}
273
274
20
Status IcebergTableReader::customize_file_scan_request(format::FileScanRequest* file_request) {
275
20
    RETURN_IF_ERROR(TableReader::customize_file_scan_request(file_request));
276
20
    if ((_row_lineage_columns.first_row_id >= 0 && _need_row_lineage_row_id()) ||
277
20
        _need_iceberg_rowid()) {
278
9
        RETURN_IF_ERROR(_append_row_position_output_column(file_request));
279
9
    }
280
20
    RETURN_IF_ERROR(_append_equality_delete_predicates(file_request));
281
20
    return Status::OK();
282
20
}
283
284
19
bool IcebergTableReader::_supports_aggregate_pushdown(TPushAggOp::type agg_type) const {
285
19
    if (!TableReader::_supports_aggregate_pushdown(agg_type)) {
286
18
        return false;
287
18
    }
288
1
    return _equality_delete_filters.empty();
289
19
}
290
291
Status IcebergTableReader::_parse_deletion_vector_file(const TTableFormatFileDesc& t_desc,
292
                                                       DeleteFileDesc* desc,
293
26
                                                       bool* has_delete_file) {
294
26
    DORIS_CHECK(desc != nullptr);
295
26
    DORIS_CHECK(has_delete_file != nullptr);
296
26
    *has_delete_file = false;
297
26
    if (!t_desc.__isset.iceberg_params) {
298
2
        return Status::OK();
299
2
    }
300
24
    const auto& iceberg_params = t_desc.iceberg_params;
301
24
    if (!iceberg_params.__isset.format_version ||
302
24
        iceberg_params.format_version < MIN_SUPPORT_DELETE_FILES_VERSION ||
303
24
        !iceberg_params.__isset.delete_files || iceberg_params.delete_files.empty()) {
304
8
        return Status::OK();
305
8
    }
306
307
16
    const TIcebergDeleteFileDesc* deletion_vector = nullptr;
308
18
    for (const auto& delete_file : iceberg_params.delete_files) {
309
18
        if (!delete_file.__isset.content || delete_file.content != DELETION_VECTOR) {
310
8
            continue;
311
8
        }
312
10
        if (deletion_vector != nullptr) {
313
1
            return Status::DataQualityError("This iceberg data file has multiple DVs.");
314
1
        }
315
9
        deletion_vector = &delete_file;
316
9
    }
317
15
    if (deletion_vector == nullptr) {
318
7
        return Status::OK();
319
7
    }
320
8
    if (!deletion_vector->__isset.content_offset ||
321
8
        !deletion_vector->__isset.content_size_in_bytes) {
322
0
        return Status::InternalError("Deletion vector is missing content offset or length");
323
0
    }
324
325
8
    const std::string data_file_path = iceberg_params.__isset.original_file_path
326
8
                                               ? iceberg_params.original_file_path
327
8
                                               : _data_file_path();
328
8
    desc->key = build_iceberg_deletion_vector_cache_key(data_file_path, *deletion_vector);
329
8
    desc->path = deletion_vector->path;
330
8
    desc->start_offset = deletion_vector->content_offset;
331
8
    desc->size = deletion_vector->content_size_in_bytes;
332
8
    desc->file_size = -1;
333
8
    desc->format = DeleteFileDesc::Format::ICEBERG;
334
8
    *has_delete_file = true;
335
8
    return Status::OK();
336
8
}
337
338
20
Status IcebergTableReader::_init_delete_predicates(const TTableFormatFileDesc& t_desc) {
339
20
    if (!t_desc.__isset.iceberg_params || _delete_predicates_initialized) {
340
2
        _delete_predicates_initialized = true;
341
2
        return Status::OK();
342
2
    }
343
18
    const auto& iceberg_params = t_desc.iceberg_params;
344
18
    if (!iceberg_params.__isset.format_version ||
345
18
        iceberg_params.format_version < MIN_SUPPORT_DELETE_FILES_VERSION ||
346
18
        !iceberg_params.__isset.delete_files || iceberg_params.delete_files.empty()) {
347
8
        _delete_predicates_initialized = true;
348
8
        return Status::OK();
349
8
    }
350
351
10
    std::vector<TIcebergDeleteFileDesc> position_delete_files;
352
10
    std::vector<TIcebergDeleteFileDesc> equality_delete_files;
353
11
    for (const auto& delete_file : iceberg_params.delete_files) {
354
11
        if (!delete_file.__isset.content) {
355
0
            continue;
356
0
        }
357
11
        if (delete_file.content == POSITION_DELETE) {
358
6
            position_delete_files.push_back(delete_file);
359
6
        } else if (delete_file.content == EQUALITY_DELETE) {
360
2
            equality_delete_files.push_back(delete_file);
361
2
        }
362
11
    }
363
    // `_delete_rows != nullptr` means a deletion vector is parsed. Per Iceberg scan planning,
364
    // position delete files apply only when there is no deletion vector for the data file.
365
10
    if (_delete_rows != nullptr) {
366
3
        _position_delete_rows_storage = *_delete_rows;
367
3
        _delete_rows = &_position_delete_rows_storage;
368
3
        position_delete_files.clear();
369
3
    }
370
    // Initialize position and equality delete predicates. Position delete files contain row
371
    // positions of deleted rows, which can be directly added to `_delete_rows`. Equality delete
372
    // files contain values of deleted rows, which require reading the files and building
373
    // predicates for later filtering.
374
10
    if (!position_delete_files.empty()) {
375
5
        RETURN_IF_ERROR(_init_position_delete_rows(position_delete_files));
376
5
    }
377
10
    if (!equality_delete_files.empty()) {
378
2
        RETURN_IF_ERROR(_init_equality_delete_predicates(equality_delete_files));
379
2
    }
380
381
10
    _delete_predicates_initialized = true;
382
10
    return Status::OK();
383
10
}
384
385
std::shared_ptr<io::FileSystemProperties> IcebergTableReader::_delete_file_system_properties(
386
7
        const TFileScanRangeParams& scan_params) {
387
7
    auto system_properties = std::make_shared<io::FileSystemProperties>();
388
7
    system_properties->system_type =
389
7
            scan_params.__isset.file_type ? scan_params.file_type : TFileType::FILE_LOCAL;
390
7
    system_properties->properties = scan_params.properties;
391
7
    system_properties->hdfs_params = scan_params.hdfs_params;
392
7
    if (scan_params.__isset.broker_addresses) {
393
0
        system_properties->broker_addresses.assign(scan_params.broker_addresses.begin(),
394
0
                                                   scan_params.broker_addresses.end());
395
0
    }
396
7
    return system_properties;
397
7
}
398
399
std::unique_ptr<io::FileDescription> IcebergTableReader::_delete_file_description(
400
7
        const TFileRangeDesc& range) {
401
7
    auto file_description = std::make_unique<io::FileDescription>();
402
7
    file_description->path = range.path;
403
7
    file_description->file_size = range.__isset.file_size ? range.file_size : -1;
404
7
    file_description->range_start_offset = range.__isset.start_offset ? range.start_offset : 0;
405
7
    file_description->range_size = range.__isset.size ? range.size : -1;
406
7
    if (range.__isset.fs_name) {
407
0
        file_description->fs_name = range.fs_name;
408
0
    }
409
7
    return file_description;
410
7
}
411
412
6
std::string IcebergTableReader::_data_file_path() const {
413
6
    if (_iceberg_params.has_value() && _iceberg_params->__isset.original_file_path) {
414
5
        return _iceberg_params->original_file_path;
415
5
    }
416
1
    DORIS_CHECK(_current_task != nullptr);
417
1
    DORIS_CHECK(_current_task->data_file != nullptr);
418
1
    return _current_task->data_file->path;
419
6
}
420
421
9
Status IcebergTableReader::_append_row_position_output_column(format::FileScanRequest* request) {
422
9
    const auto row_position_column_id = format::LocalColumnId(format::ROW_POSITION_COLUMN_ID);
423
9
    _append_file_scan_column(request, row_position_column_id, &request->non_predicate_columns);
424
9
    _row_position_block_position = request->local_positions.at(row_position_column_id).value();
425
9
    return Status::OK();
426
9
}
427
428
20
Status IcebergTableReader::_append_equality_delete_predicates(format::FileScanRequest* request) {
429
20
    DORIS_CHECK(request != nullptr);
430
20
    for (const auto& filter : _equality_delete_filters) {
431
2
        auto delete_predicate =
432
2
                std::make_shared<EqualityDeletePredicate>(filter.delete_block, filter.field_ids);
433
2
        DCHECK_EQ(filter.field_ids.size(), filter.key_types.size());
434
4
        for (size_t idx = 0; idx < filter.field_ids.size(); ++idx) {
435
2
            const int field_id = filter.field_ids[idx];
436
2
            auto field_it = std::ranges::find_if(
437
2
                    _data_reader.file_schema, [field_id](const format::ColumnDefinition& field) {
438
2
                        return field.has_identifier_field_id() &&
439
2
                               field.get_identifier_field_id() == field_id;
440
2
                    });
441
2
            if (field_it == _data_reader.file_schema.end()) {
442
0
                return Status::InternalError(
443
0
                        "Can not find equality delete column field id {} in data file schema",
444
0
                        field_id);
445
0
            }
446
2
            const auto field_column_id = format::LocalColumnId(field_it->file_local_id());
447
2
            _append_file_scan_column(request, field_column_id, &request->predicate_columns);
448
2
            const auto block_position = request->local_positions.at(field_column_id).value();
449
2
            auto slot = VSlotRef::create_shared(cast_set<int>(block_position),
450
2
                                                cast_set<int>(block_position), -1, field_it->type,
451
2
                                                field_it->name);
452
2
            if (field_it->type->equals(*filter.key_types[idx])) {
453
1
                delete_predicate->add_child(std::move(slot));
454
1
            } else {
455
1
                auto cast_expr = Cast::create_shared(filter.key_types[idx]);
456
1
                cast_expr->add_child(std::move(slot));
457
1
                delete_predicate->add_child(std::move(cast_expr));
458
1
            }
459
2
        }
460
2
        request->delete_conjuncts.push_back(
461
2
                VExprContext::create_shared(std::move(delete_predicate)));
462
2
    }
463
20
    return Status::OK();
464
20
}
465
466
Status IcebergTableReader::_read_parquet_position_delete_file(
467
        const TIcebergDeleteFileDesc& delete_file, const TFileScanRangeParams& scan_params,
468
5
        IcebergDeleteFileIOContext* delete_io_ctx, PositionDeleteRowsCollector* collector) {
469
5
    if (!delete_file.__isset.file_format) {
470
0
        return Status::InternalError("Iceberg position delete file is missing file format");
471
0
    }
472
5
    if (delete_file.file_format == TFileFormatType::FORMAT_ORC) {
473
0
        return Status::NotSupported("Iceberg ORC position delete file is not supported");
474
0
    }
475
5
    if (delete_file.file_format != TFileFormatType::FORMAT_PARQUET) {
476
0
        return Status::NotSupported("Unsupported Iceberg delete file format {}",
477
0
                                    delete_file.file_format);
478
0
    }
479
480
5
    auto delete_range = build_iceberg_delete_file_range(delete_file.path);
481
5
    if (_current_task != nullptr && _current_task->data_file != nullptr &&
482
5
        !_current_task->data_file->fs_name.empty()) {
483
0
        delete_range.__set_fs_name(_current_task->data_file->fs_name);
484
0
    }
485
5
    auto system_properties = _delete_file_system_properties(scan_params);
486
5
    auto file_description = _delete_file_description(delete_range);
487
5
    std::shared_ptr<io::IOContext> io_ctx(&delete_io_ctx->io_ctx, [](io::IOContext*) {});
488
5
    format::parquet::ParquetReader reader(system_properties, file_description, io_ctx,
489
5
                                          _scanner_profile);
490
5
    RETURN_IF_ERROR(reader.init(_runtime_state));
491
492
5
    std::vector<format::ColumnDefinition> schema;
493
5
    RETURN_IF_ERROR(reader.get_schema(&schema));
494
5
    format::ColumnDefinition* file_path_field = nullptr;
495
5
    format::ColumnDefinition* pos_field = nullptr;
496
10
    for (auto& field : schema) {
497
10
        if (field.name == ICEBERG_FILE_PATH) {
498
5
            file_path_field = &field;
499
5
        } else if (field.name == ICEBERG_ROW_POS) {
500
5
            pos_field = &field;
501
5
        }
502
10
    }
503
5
    if (file_path_field == nullptr || pos_field == nullptr) {
504
0
        return Status::InternalError("Position delete parquet file is missing required columns");
505
0
    }
506
507
5
    auto request = std::make_shared<format::FileScanRequest>();
508
5
    request->non_predicate_columns = {
509
5
            format::LocalColumnIndex::top_level(
510
5
                    format::LocalColumnId(file_path_field->file_local_id())),
511
5
            format::LocalColumnIndex::top_level(format::LocalColumnId(pos_field->file_local_id()))};
512
5
    request->local_positions = {
513
5
            {format::LocalColumnId(file_path_field->file_local_id()),
514
5
             format::LocalIndex(ICEBERG_FILE_PATH_BLOCK_POSITION)},
515
5
            {format::LocalColumnId(pos_field->file_local_id()),
516
5
             format::LocalIndex(ICEBERG_ROW_POS_BLOCK_POSITION)},
517
5
    };
518
5
    RETURN_IF_ERROR(reader.open(request));
519
520
5
    bool eof = false;
521
5
    auto build_position_delete_block = [](const format::ColumnDefinition& file_path_field,
522
10
                                          const format::ColumnDefinition& pos_field) -> Block {
523
10
        Block block;
524
10
        block.insert(
525
10
                {file_path_field.type->create_column(), file_path_field.type, ICEBERG_FILE_PATH});
526
10
        block.insert({pos_field.type->create_column(), pos_field.type, ICEBERG_ROW_POS});
527
10
        return block;
528
10
    };
529
15
    while (!eof) {
530
10
        Block block = build_position_delete_block(*file_path_field, *pos_field);
531
10
        size_t read_rows = 0;
532
10
        RETURN_IF_ERROR(reader.get_block(&block, &read_rows, &eof));
533
10
        RETURN_IF_ERROR(collector->collect(block, read_rows));
534
10
    }
535
5
    return reader.close();
536
5
}
537
538
Status IcebergTableReader::_init_position_delete_rows(
539
5
        const std::vector<TIcebergDeleteFileDesc>& delete_files) {
540
5
    TFileScanRangeParams delete_scan_params =
541
5
            _scan_params == nullptr ? TFileScanRangeParams() : *_scan_params;
542
5
    format::DeleteRows position_delete_rows;
543
5
    IcebergDeleteFileIOContext delete_io_ctx(_runtime_state);
544
5
    PositionDeleteRowsCollector collector(_data_file_path(), &position_delete_rows);
545
5
    for (const auto& delete_file : delete_files) {
546
5
        RETURN_IF_ERROR(_read_parquet_position_delete_file(delete_file, delete_scan_params,
547
5
                                                           &delete_io_ctx, &collector));
548
5
    }
549
5
    if (position_delete_rows.empty()) {
550
0
        return Status::OK();
551
0
    }
552
    // Position delete files and deletion vectors both become row-position deletes for the
553
    // common TableReader DeletePredicate path. Keep the merged rows in a member vector because
554
    // DeletePredicate stores a reference to the vector used by _delete_rows.
555
5
    _position_delete_rows_storage.insert(_position_delete_rows_storage.end(),
556
5
                                         position_delete_rows.begin(), position_delete_rows.end());
557
5
    std::sort(_position_delete_rows_storage.begin(), _position_delete_rows_storage.end());
558
5
    _position_delete_rows_storage.erase(
559
5
            std::unique(_position_delete_rows_storage.begin(), _position_delete_rows_storage.end()),
560
5
            _position_delete_rows_storage.end());
561
5
    _delete_rows = &_position_delete_rows_storage;
562
5
    return Status::OK();
563
5
}
564
565
Status IcebergTableReader::_init_equality_delete_predicates(
566
2
        const std::vector<TIcebergDeleteFileDesc>& delete_files) {
567
2
    TFileScanRangeParams delete_scan_params =
568
2
            _scan_params == nullptr ? TFileScanRangeParams() : *_scan_params;
569
2
    IcebergDeleteFileIOContext delete_io_ctx(_runtime_state);
570
2
    for (const auto& delete_file : delete_files) {
571
2
        RETURN_IF_ERROR(_read_parquet_equality_delete_file(delete_file, delete_scan_params,
572
2
                                                           &delete_io_ctx));
573
2
    }
574
2
    return Status::OK();
575
2
}
576
577
Status IcebergTableReader::_read_parquet_equality_delete_file(
578
        const TIcebergDeleteFileDesc& delete_file, const TFileScanRangeParams& scan_params,
579
2
        IcebergDeleteFileIOContext* delete_io_ctx) {
580
2
    if (!delete_file.__isset.file_format) {
581
0
        return Status::InternalError("Iceberg equality delete file is missing file format");
582
0
    }
583
2
    if (delete_file.file_format != TFileFormatType::FORMAT_PARQUET) {
584
0
        return Status::NotSupported("Unsupported Iceberg equality delete file format {}",
585
0
                                    delete_file.file_format);
586
0
    }
587
2
    if (!delete_file.__isset.field_ids || delete_file.field_ids.empty()) {
588
0
        return Status::InternalError("Iceberg equality delete file is missing field ids");
589
0
    }
590
591
2
    auto delete_range = build_iceberg_delete_file_range(delete_file.path);
592
2
    if (_current_task != nullptr && _current_task->data_file != nullptr &&
593
2
        !_current_task->data_file->fs_name.empty()) {
594
0
        delete_range.__set_fs_name(_current_task->data_file->fs_name);
595
0
    }
596
2
    auto system_properties = _delete_file_system_properties(scan_params);
597
2
    auto file_description = _delete_file_description(delete_range);
598
2
    std::shared_ptr<io::IOContext> io_ctx(&delete_io_ctx->io_ctx, [](io::IOContext*) {});
599
2
    format::parquet::ParquetReader reader(system_properties, file_description, io_ctx,
600
2
                                          _scanner_profile);
601
2
    RETURN_IF_ERROR(reader.init(_runtime_state));
602
603
2
    std::vector<format::ColumnDefinition> schema;
604
2
    RETURN_IF_ERROR(reader.get_schema(&schema));
605
2
    std::vector<format::ColumnDefinition> delete_fields;
606
2
    std::vector<int> delete_field_ids;
607
2
    std::vector<DataTypePtr> delete_key_types;
608
2
    for (const auto field_id : delete_file.field_ids) {
609
2
        auto field_it = std::find_if(schema.begin(), schema.end(),
610
2
                                     [field_id](const format::ColumnDefinition& field) {
611
2
                                         return field.has_identifier_field_id() &&
612
2
                                                field_id == field.get_identifier_field_id();
613
2
                                     });
614
2
        if (field_it == schema.end()) {
615
0
            return Status::InternalError("Can not find field id {} in equality delete file {}",
616
0
                                         field_id, delete_file.path);
617
0
        }
618
2
        if (!field_it->children.empty()) {
619
0
            return Status::NotSupported(
620
0
                    "Iceberg equality delete does not support complex column {}", field_it->name);
621
0
        }
622
2
        delete_fields.push_back(*field_it);
623
2
        delete_field_ids.push_back(field_id);
624
2
        delete_key_types.push_back(field_it->type);
625
2
    }
626
627
2
    auto request = std::make_shared<format::FileScanRequest>();
628
4
    for (size_t idx = 0; idx < delete_fields.size(); ++idx) {
629
2
        const auto local_column_id = format::LocalColumnId(delete_fields[idx].file_local_id());
630
2
        request->non_predicate_columns.push_back(
631
2
                format::LocalColumnIndex::top_level(local_column_id));
632
2
        request->local_positions.emplace(local_column_id, format::LocalIndex(idx));
633
2
    }
634
2
    RETURN_IF_ERROR(reader.open(request));
635
636
2
    auto build_equality_delete_block =
637
6
            [](const std::vector<format::ColumnDefinition> fields) -> Block {
638
6
        Block block;
639
6
        for (const auto& field : fields) {
640
6
            block.insert({field.type->create_column(), field.type, field.name});
641
6
        }
642
6
        return block;
643
6
    };
644
2
    Block delete_block = build_equality_delete_block(delete_fields);
645
2
    MutableBlock mutable_delete_block(std::move(delete_block));
646
2
    bool eof = false;
647
6
    while (!eof) {
648
4
        Block block = build_equality_delete_block(delete_fields);
649
4
        size_t read_rows = 0;
650
4
        RETURN_IF_ERROR(reader.get_block(&block, &read_rows, &eof));
651
4
        if (read_rows > 0) {
652
2
            RETURN_IF_ERROR(mutable_delete_block.merge(block));
653
2
        }
654
4
    }
655
2
    RETURN_IF_ERROR(reader.close());
656
2
    delete_block = mutable_delete_block.to_block();
657
2
    _equality_delete_filters.push_back(
658
2
            EqualityDeleteFilter {.field_ids = std::move(delete_field_ids),
659
2
                                  .key_types = std::move(delete_key_types),
660
2
                                  .delete_block = std::move(delete_block)});
661
2
    return Status::OK();
662
2
}
663
664
9
Status IcebergTableReader::_materialize_row_lineage_row_id(Block* table_block, size_t column_idx) {
665
9
    if (_row_lineage_columns.first_row_id < 0) {
666
2
        return Status::OK();
667
2
    }
668
7
    DORIS_CHECK(_row_position_block_position < _data_reader.block_template.columns());
669
7
    const auto& row_position_column = assert_cast<const ColumnInt64&>(
670
7
            *_data_reader.block_template.get_by_position(_row_position_block_position).column);
671
7
    DORIS_CHECK(row_position_column.size() == table_block->rows());
672
7
    auto column = IColumn::mutate(
673
7
            table_block->get_by_position(column_idx).column->convert_to_full_column_if_const());
674
7
    auto* nullable_column = assert_cast<ColumnNullable*>(column.get());
675
7
    auto& null_map = nullable_column->get_null_map_data();
676
7
    auto& data = assert_cast<ColumnInt64&>(*nullable_column->get_nested_column_ptr()).get_data();
677
7
    DORIS_CHECK(null_map.size() == row_position_column.size());
678
7
    DORIS_CHECK(data.size() == row_position_column.size());
679
23
    for (size_t row = 0; row < row_position_column.size(); ++row) {
680
16
        if (null_map[row]) {
681
10
            null_map[row] = 0;
682
10
            data[row] = _row_lineage_columns.first_row_id + row_position_column.get_element(row);
683
10
        }
684
16
    }
685
7
    table_block->replace_by_position(column_idx, std::move(column));
686
7
    return Status::OK();
687
9
}
688
689
1
Status IcebergTableReader::_materialize_iceberg_rowid(Block* table_block, size_t column_idx) {
690
1
    DORIS_CHECK(_row_position_block_position < _data_reader.block_template.columns());
691
1
    const auto& row_position_column = assert_cast<const ColumnInt64&>(
692
1
            *_data_reader.block_template.get_by_position(_row_position_block_position).column);
693
1
    DORIS_CHECK(row_position_column.size() == table_block->rows());
694
695
1
    const auto& type = table_block->get_by_position(column_idx).type;
696
1
    auto column = type->create_column();
697
1
    auto* nullable_column = check_and_get_column<ColumnNullable>(column.get());
698
1
    auto* struct_column = nullable_column != nullptr
699
1
                                  ? check_and_get_column<ColumnStruct>(
700
1
                                            nullable_column->get_nested_column_ptr().get())
701
1
                                  : check_and_get_column<ColumnStruct>(column.get());
702
1
    DORIS_CHECK(struct_column != nullptr);
703
1
    DORIS_CHECK(struct_column->tuple_size() >= 4);
704
705
1
    const auto rows = row_position_column.size();
706
1
    const auto file_path = _data_file_path();
707
1
    const int32_t partition_spec_id =
708
1
            _iceberg_params.has_value() && _iceberg_params->__isset.partition_spec_id
709
1
                    ? _iceberg_params->partition_spec_id
710
1
                    : 0;
711
1
    const std::string partition_data_json =
712
1
            _iceberg_params.has_value() && _iceberg_params->__isset.partition_data_json
713
1
                    ? _iceberg_params->partition_data_json
714
1
                    : "";
715
716
1
    auto& file_path_column = struct_column->get_column(0);
717
1
    auto& row_pos_column = struct_column->get_column(1);
718
1
    auto& spec_id_column = struct_column->get_column(2);
719
1
    auto& partition_data_column = struct_column->get_column(3);
720
1
    file_path_column.reserve(rows);
721
1
    row_pos_column.reserve(rows);
722
1
    spec_id_column.reserve(rows);
723
1
    partition_data_column.reserve(rows);
724
3
    for (size_t row = 0; row < rows; ++row) {
725
2
        file_path_column.insert_data(file_path.data(), file_path.size());
726
2
        const int64_t row_pos = row_position_column.get_element(row);
727
2
        row_pos_column.insert_data(reinterpret_cast<const char*>(&row_pos), sizeof(row_pos));
728
2
        spec_id_column.insert_data(reinterpret_cast<const char*>(&partition_spec_id),
729
2
                                   sizeof(partition_spec_id));
730
2
        partition_data_column.insert_data(partition_data_json.data(), partition_data_json.size());
731
2
    }
732
1
    if (nullable_column != nullptr) {
733
1
        nullable_column->get_null_map_data().resize_fill(rows, 0);
734
1
    }
735
1
    table_block->replace_by_position(column_idx, std::move(column));
736
1
    return Status::OK();
737
1
}
738
739
Status IcebergTableReader::_materialize_row_lineage_last_updated_sequence_number(
740
8
        Block* table_block, size_t column_idx) {
741
8
    if (_row_lineage_columns.last_updated_sequence_number < 0) {
742
2
        return Status::OK();
743
2
    }
744
6
    auto column = IColumn::mutate(
745
6
            table_block->get_by_position(column_idx).column->convert_to_full_column_if_const());
746
6
    auto* nullable_column = assert_cast<ColumnNullable*>(column.get());
747
6
    auto& null_map = nullable_column->get_null_map_data();
748
6
    auto& data = assert_cast<ColumnInt64&>(*nullable_column->get_nested_column_ptr()).get_data();
749
6
    DORIS_CHECK(null_map.size() == table_block->rows());
750
6
    DORIS_CHECK(data.size() == table_block->rows());
751
20
    for (size_t row = 0; row < table_block->rows(); ++row) {
752
14
        if (null_map[row]) {
753
8
            null_map[row] = 0;
754
8
            data[row] = _row_lineage_columns.last_updated_sequence_number;
755
8
        }
756
14
    }
757
6
    table_block->replace_by_position(column_idx, std::move(column));
758
6
    return Status::OK();
759
8
}
760
761
8
bool IcebergTableReader::_need_row_lineage_row_id() const {
762
8
    if (_data_reader.column_mapper != nullptr) {
763
7
        for (const auto& mapping : _data_reader.column_mapper->mappings()) {
764
7
            if (mapping.virtual_column_type == format::TableVirtualColumnType::ROW_ID) {
765
7
                return true;
766
7
            }
767
7
        }
768
7
    }
769
1
    return std::ranges::any_of(_projected_columns, is_projected_row_lineage_row_id);
770
8
}
771
772
12
bool IcebergTableReader::_need_iceberg_rowid() const {
773
12
    if (_data_reader.column_mapper != nullptr) {
774
16
        for (const auto& mapping : _data_reader.column_mapper->mappings()) {
775
16
            if (mapping.virtual_column_type == format::TableVirtualColumnType::ICEBERG_ROWID) {
776
1
                return true;
777
1
            }
778
16
        }
779
12
    }
780
11
    return std::ranges::any_of(_projected_columns, is_projected_iceberg_rowid);
781
12
}
782
783
} // namespace doris::format::iceberg