Coverage Report

Created: 2026-07-20 19:43

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/vliteral.h"
38
#include "exprs/vslot_ref.h"
39
#include "format/table/deletion_vector_reader.h"
40
#include "format_v2/expr/cast.h"
41
#include "format_v2/expr/equality_delete_predicate.h"
42
#include "format_v2/orc/orc_reader.h"
43
#include "format_v2/parquet/parquet_reader.h"
44
#include "format_v2/parquet/reader/column_reader.h"
45
#include "format_v2/table_reader.h"
46
#include "io/file_factory.h"
47
#include "util/url_coding.h"
48
49
namespace doris::format::iceberg {
50
51
static constexpr const char* ROW_LINEAGE_ROW_ID = "_row_id";
52
static constexpr int32_t ROW_LINEAGE_ROW_ID_FIELD_ID = 2147483540;
53
54
template <typename T>
55
0
static std::string join_values_for_debug(const std::vector<T>& values) {
56
0
    std::ostringstream out;
57
0
    out << "[";
58
0
    for (size_t idx = 0; idx < values.size(); ++idx) {
59
0
        if (idx > 0) {
60
0
            out << ", ";
61
0
        }
62
0
        out << values[idx];
63
0
    }
64
0
    out << "]";
65
0
    return out.str();
66
0
}
67
68
1
static bool is_projected_row_lineage_row_id(const format::ColumnDefinition& column) {
69
    // Iceberg row lineage columns can be bound by field id when a mapper has already been built,
70
    // but customize_file_scan_request() is also exercised directly by scan-request tests before the
71
    // mapper exists. In that path, inspect the projected table schema so row-position dependencies
72
    // are still added for `_row_id`.
73
1
    return column.name == ROW_LINEAGE_ROW_ID ||
74
1
           (column.has_identifier_field_id() &&
75
0
            column.get_identifier_field_id() == ROW_LINEAGE_ROW_ID_FIELD_ID);
76
1
}
77
78
36
static bool is_projected_iceberg_rowid(const format::ColumnDefinition& column) {
79
36
    return column.name == BeConsts::ICEBERG_ROWID_COL;
80
36
}
81
82
static Status build_missing_equality_delete_key_expr(const format::ColumnDefinition& table_field,
83
                                                     const DataTypePtr& delete_key_type,
84
7
                                                     VExprSPtr* key_expr) {
85
7
    DORIS_CHECK(delete_key_type != nullptr);
86
7
    DORIS_CHECK(key_expr != nullptr);
87
7
    if (!table_field.initial_default_value.has_value()) {
88
        // A newly added optional field without an initial default is logically NULL in older
89
        // files. EqualityDeletePredicate treats NULL == NULL as a match.
90
2
        *key_expr = VLiteral::create_shared(make_nullable(delete_key_type), Field());
91
2
        return Status::OK();
92
2
    }
93
94
5
    Field initial_default;
95
5
    if (table_field.initial_default_value_is_base64 ||
96
5
        table_field.type->get_primitive_type() == TYPE_VARBINARY) {
97
        // New FE versions mark every Iceberg UUID/BINARY/FIXED default as Base64 regardless of its
98
        // Doris mapping. Keep the VARBINARY fallback for scan descriptors produced before that
99
        // marker existed. Decode before parsing so STRING/CHAR and VARBINARY all compare against
100
        // the raw bytes stored in equality-delete files.
101
3
        std::string decoded_default;
102
3
        if (!base64_decode(*table_field.initial_default_value, &decoded_default)) {
103
0
            return Status::InvalidArgument("Invalid Base64 Iceberg initial default for field {}",
104
0
                                           table_field.name);
105
0
        }
106
3
        if (table_field.type->get_primitive_type() == TYPE_VARBINARY) {
107
1
            initial_default = Field::create_field<TYPE_VARBINARY>(StringView(decoded_default));
108
2
        } else {
109
2
            DORIS_CHECK(is_string_type(table_field.type->get_primitive_type()));
110
2
            initial_default = Field::create_field<TYPE_STRING>(decoded_default);
111
2
        }
112
3
    } else {
113
        // An added field's initial default is its logical value in every older data file that lacks
114
        // the physical column. FE normalizes the string for the current Doris table type.
115
2
        RETURN_IF_ERROR(table_field.type->get_serde()->from_fe_string(
116
2
                *table_field.initial_default_value, initial_default));
117
2
    }
118
119
5
    auto literal = VLiteral::create_shared(table_field.type, initial_default);
120
5
    if (table_field.type->equals(*delete_key_type)) {
121
1
        *key_expr = std::move(literal);
122
1
        return Status::OK();
123
1
    }
124
4
    auto cast_expr = Cast::create_shared(delete_key_type);
125
4
    cast_expr->add_child(std::move(literal));
126
4
    *key_expr = std::move(cast_expr);
127
4
    return Status::OK();
128
5
}
129
130
0
static std::string iceberg_delete_file_debug_string(const TIcebergDeleteFileDesc& delete_file) {
131
0
    std::ostringstream out;
132
0
    out << "TIcebergDeleteFileDesc{path=" << (delete_file.__isset.path ? delete_file.path : "null")
133
0
        << ", content=" << (delete_file.__isset.content ? delete_file.content : -1)
134
0
        << ", file_format="
135
0
        << (delete_file.__isset.file_format ? static_cast<int>(delete_file.file_format) : -1)
136
0
        << ", position_lower_bound="
137
0
        << (delete_file.__isset.position_lower_bound ? delete_file.position_lower_bound : -1)
138
0
        << ", position_upper_bound="
139
0
        << (delete_file.__isset.position_upper_bound ? delete_file.position_upper_bound : -1)
140
0
        << ", field_ids="
141
0
        << (delete_file.__isset.field_ids ? join_values_for_debug(delete_file.field_ids) : "[]")
142
0
        << ", content_offset="
143
0
        << (delete_file.__isset.content_offset ? delete_file.content_offset : -1)
144
0
        << ", content_size_in_bytes="
145
0
        << (delete_file.__isset.content_size_in_bytes ? delete_file.content_size_in_bytes : -1)
146
0
        << "}";
147
0
    return out.str();
148
0
}
149
150
static std::string iceberg_delete_files_debug_string(
151
0
        const std::vector<TIcebergDeleteFileDesc>& delete_files) {
152
0
    std::ostringstream out;
153
0
    out << "[";
154
0
    for (size_t idx = 0; idx < delete_files.size(); ++idx) {
155
0
        if (idx > 0) {
156
0
            out << ", ";
157
0
        }
158
0
        out << iceberg_delete_file_debug_string(delete_files[idx]);
159
0
    }
160
0
    out << "]";
161
0
    return out.str();
162
0
}
163
164
0
static std::string iceberg_params_debug_string(const std::optional<TIcebergFileDesc>& params) {
165
0
    if (!params.has_value()) {
166
0
        return "null";
167
0
    }
168
0
    const auto& iceberg_params = *params;
169
0
    std::ostringstream out;
170
0
    out << "TIcebergFileDesc{format_version="
171
0
        << (iceberg_params.__isset.format_version ? iceberg_params.format_version : -1)
172
0
        << ", content=" << (iceberg_params.__isset.content ? iceberg_params.content : -1)
173
0
        << ", original_file_path="
174
0
        << (iceberg_params.__isset.original_file_path ? iceberg_params.original_file_path : "null")
175
0
        << ", row_count=" << (iceberg_params.__isset.row_count ? iceberg_params.row_count : -1)
176
0
        << ", partition_spec_id="
177
0
        << (iceberg_params.__isset.partition_spec_id ? iceberg_params.partition_spec_id : 0)
178
0
        << ", has_partition_data_json=" << iceberg_params.__isset.partition_data_json
179
0
        << ", first_row_id="
180
0
        << (iceberg_params.__isset.first_row_id ? iceberg_params.first_row_id : -1)
181
0
        << ", last_updated_sequence_number="
182
0
        << (iceberg_params.__isset.last_updated_sequence_number
183
0
                    ? iceberg_params.last_updated_sequence_number
184
0
                    : -1)
185
0
        << ", delete_file_count="
186
0
        << (iceberg_params.__isset.delete_files ? iceberg_params.delete_files.size() : 0)
187
0
        << ", delete_files="
188
0
        << (iceberg_params.__isset.delete_files
189
0
                    ? iceberg_delete_files_debug_string(iceberg_params.delete_files)
190
0
                    : "[]")
191
0
        << ", has_serialized_split=" << iceberg_params.__isset.serialized_split << "}";
192
0
    return out.str();
193
0
}
194
195
IcebergTableReader::PositionDeleteRowsCollector::PositionDeleteRowsCollector(
196
        PositionDeleteFile* rows_by_data_file)
197
12
        : _rows_by_data_file(rows_by_data_file) {
198
12
    DORIS_CHECK(_rows_by_data_file != nullptr);
199
12
}
200
201
Status IcebergTableReader::PositionDeleteRowsCollector::collect(const Block& block,
202
22
                                                                size_t read_rows) {
203
22
    if (read_rows == 0) {
204
10
        return Status::OK();
205
10
    }
206
12
    const auto& file_path_column_ptr =
207
12
            block.get_by_position(ICEBERG_FILE_PATH_BLOCK_POSITION).column;
208
12
    const auto& pos_column_ptr = block.get_by_position(ICEBERG_ROW_POS_BLOCK_POSITION).column;
209
12
    if (const auto* nullable_column = check_and_get_column<ColumnNullable>(*file_path_column_ptr);
210
12
        nullable_column != nullptr && nullable_column->has_null(0, read_rows)) {
211
1
        return Status::Corruption("Iceberg position delete column file_path contains null values");
212
1
    }
213
11
    if (const auto* nullable_column = check_and_get_column<ColumnNullable>(*pos_column_ptr);
214
11
        nullable_column != nullptr && nullable_column->has_null(0, read_rows)) {
215
1
        return Status::Corruption("Iceberg position delete column pos contains null values");
216
1
    }
217
10
    const auto& file_path_column =
218
10
            assert_cast<const ColumnString&>(*remove_nullable(file_path_column_ptr));
219
10
    const auto& pos_column = assert_cast<const ColumnInt64&>(*remove_nullable(pos_column_ptr));
220
26
    for (size_t row = 0; row < read_rows; ++row) {
221
16
        const auto file_path = file_path_column.get_data_at(row).to_string();
222
16
        (*_rows_by_data_file)[file_path].push_back(pos_column.get_element(row));
223
16
    }
224
10
    return Status::OK();
225
11
}
226
227
48
Status IcebergTableReader::prepare_split(const format::SplitReadOptions& options) {
228
48
    _row_lineage_columns = {};
229
48
    _iceberg_params.reset();
230
48
    _delete_predicates_initialized = false;
231
48
    _position_delete_rows_storage.clear();
232
48
    _equality_delete_filters.clear();
233
48
    _split_cache = options.cache;
234
48
    if (options.current_range.__isset.table_format_params &&
235
48
        options.current_range.table_format_params.__isset.iceberg_params) {
236
46
        const auto& iceberg_params = options.current_range.table_format_params.iceberg_params;
237
46
        _iceberg_params = iceberg_params;
238
46
        if (iceberg_params.__isset.first_row_id) {
239
9
            _row_lineage_columns.first_row_id = iceberg_params.first_row_id;
240
9
        }
241
46
        if (iceberg_params.__isset.last_updated_sequence_number) {
242
6
            _row_lineage_columns.last_updated_sequence_number =
243
6
                    iceberg_params.last_updated_sequence_number;
244
6
        }
245
46
    }
246
48
    RETURN_IF_ERROR(TableReader::prepare_split(options));
247
45
    if (current_split_pruned()) {
248
0
        return Status::OK();
249
0
    }
250
    // Iceberg data files are immutable once referenced by a snapshot; updates create new data files
251
    // at new paths instead of overwriting existing files. This lets the Parquet V2 reader use page
252
    // cache when the scan range does not carry an mtime, without extending V1's path::0 behavior to
253
    // mutable Hive/local files.
254
45
    mark_current_data_file_immutable();
255
45
    if (_is_table_level_count_active()) {
256
1
        return Status::OK();
257
1
    }
258
44
    RETURN_IF_ERROR(_init_delete_predicates(options.current_range.table_format_params));
259
42
    return Status::OK();
260
44
}
261
262
0
std::string IcebergTableReader::debug_string() const {
263
0
    size_t position_delete_file_count = 0;
264
0
    size_t equality_delete_file_count = 0;
265
0
    size_t deletion_vector_file_count = 0;
266
0
    if (_iceberg_params.has_value() && _iceberg_params->__isset.delete_files) {
267
0
        for (const auto& delete_file : _iceberg_params->delete_files) {
268
0
            if (!delete_file.__isset.content) {
269
0
                continue;
270
0
            }
271
0
            if (delete_file.content == POSITION_DELETE) {
272
0
                ++position_delete_file_count;
273
0
            } else if (delete_file.content == EQUALITY_DELETE) {
274
0
                ++equality_delete_file_count;
275
0
            } else if (delete_file.content == DELETION_VECTOR) {
276
0
                ++deletion_vector_file_count;
277
0
            }
278
0
        }
279
0
    }
280
281
0
    std::ostringstream equality_filters;
282
0
    equality_filters << "[";
283
0
    for (size_t idx = 0; idx < _equality_delete_filters.size(); ++idx) {
284
0
        if (idx > 0) {
285
0
            equality_filters << ", ";
286
0
        }
287
0
        const auto& filter = _equality_delete_filters[idx];
288
0
        equality_filters << "EqualityDeleteFilter{field_ids="
289
0
                         << join_values_for_debug(filter.field_ids) << ", key_types=[";
290
0
        for (size_t type_idx = 0; type_idx < filter.key_types.size(); ++type_idx) {
291
0
            if (type_idx > 0) {
292
0
                equality_filters << ", ";
293
0
            }
294
0
            equality_filters << (filter.key_types[type_idx] == nullptr
295
0
                                         ? "null"
296
0
                                         : filter.key_types[type_idx]->get_name());
297
0
        }
298
0
        equality_filters << "], delete_block_rows=" << filter.delete_block.rows()
299
0
                         << ", delete_block_columns=" << filter.delete_block.columns() << "}";
300
0
    }
301
0
    equality_filters << "]";
302
303
0
    std::ostringstream out;
304
0
    out << "IcebergTableReader{base=" << format::TableReader::debug_string()
305
0
        << ", iceberg_params=" << iceberg_params_debug_string(_iceberg_params)
306
0
        << ", row_lineage_first_row_id=" << _row_lineage_columns.first_row_id
307
0
        << ", row_lineage_last_updated_sequence_number="
308
0
        << _row_lineage_columns.last_updated_sequence_number
309
0
        << ", need_row_lineage_row_id=" << _need_row_lineage_row_id()
310
0
        << ", need_iceberg_rowid=" << _need_iceberg_rowid()
311
0
        << ", row_position_block_position=" << _row_position_block_position
312
0
        << ", delete_predicates_initialized=" << _delete_predicates_initialized
313
0
        << ", position_delete_file_count=" << position_delete_file_count
314
0
        << ", equality_delete_file_count=" << equality_delete_file_count
315
0
        << ", deletion_vector_file_count=" << deletion_vector_file_count
316
0
        << ", position_delete_rows_storage_count=" << _position_delete_rows_storage.size()
317
0
        << ", equality_delete_filter_count=" << _equality_delete_filters.size()
318
0
        << ", equality_delete_filters=" << equality_filters.str() << "}";
319
0
    return out.str();
320
0
}
321
322
34
Status IcebergTableReader::materialize_virtual_columns(Block* table_block) {
323
86
    for (size_t column_idx = 0; column_idx < _data_reader.column_mapper->mappings().size();
324
52
         ++column_idx) {
325
52
        const auto& mapping = _data_reader.column_mapper->mappings()[column_idx];
326
52
        switch (mapping.virtual_column_type) {
327
9
        case format::TableVirtualColumnType::ROW_ID:
328
9
            RETURN_IF_ERROR(_materialize_row_lineage_row_id(table_block, column_idx));
329
9
            break;
330
9
        case format::TableVirtualColumnType::LAST_UPDATED_SEQUENCE_NUMBER:
331
8
            RETURN_IF_ERROR(
332
8
                    _materialize_row_lineage_last_updated_sequence_number(table_block, column_idx));
333
8
            break;
334
8
        case format::TableVirtualColumnType::ICEBERG_ROWID:
335
1
            RETURN_IF_ERROR(_materialize_iceberg_rowid(table_block, column_idx));
336
1
            break;
337
34
        case format::TableVirtualColumnType::INVALID:
338
34
            break;
339
52
        }
340
52
    }
341
34
    return Status::OK();
342
34
}
343
344
41
Status IcebergTableReader::customize_file_scan_request(format::FileScanRequest* file_request) {
345
41
    RETURN_IF_ERROR(TableReader::customize_file_scan_request(file_request));
346
41
    if ((_row_lineage_columns.first_row_id >= 0 && _need_row_lineage_row_id()) ||
347
41
        _need_iceberg_rowid()) {
348
9
        RETURN_IF_ERROR(_append_row_position_output_column(file_request));
349
9
    }
350
41
    RETURN_IF_ERROR(_append_equality_delete_predicates(file_request));
351
41
    return Status::OK();
352
41
}
353
354
40
bool IcebergTableReader::_supports_aggregate_pushdown(TPushAggOp::type agg_type) const {
355
40
    if (!TableReader::_supports_aggregate_pushdown(agg_type)) {
356
40
        return false;
357
40
    }
358
0
    return _equality_delete_filters.empty();
359
40
}
360
361
Status IcebergTableReader::_parse_deletion_vector_file(const TTableFormatFileDesc& t_desc,
362
                                                       DeleteFileDesc* desc,
363
55
                                                       bool* has_delete_file) {
364
55
    DORIS_CHECK(desc != nullptr);
365
55
    DORIS_CHECK(has_delete_file != nullptr);
366
55
    *has_delete_file = false;
367
55
    if (!t_desc.__isset.iceberg_params) {
368
2
        return Status::OK();
369
2
    }
370
53
    const auto& iceberg_params = t_desc.iceberg_params;
371
53
    if (!iceberg_params.__isset.format_version ||
372
53
        iceberg_params.format_version < MIN_SUPPORT_DELETE_FILES_VERSION ||
373
53
        !iceberg_params.__isset.delete_files || iceberg_params.delete_files.empty()) {
374
9
        return Status::OK();
375
9
    }
376
377
44
    const TIcebergDeleteFileDesc* deletion_vector = nullptr;
378
46
    for (const auto& delete_file : iceberg_params.delete_files) {
379
46
        if (!delete_file.__isset.content || delete_file.content != DELETION_VECTOR) {
380
31
            continue;
381
31
        }
382
15
        if (deletion_vector != nullptr) {
383
1
            return Status::DataQualityError("This iceberg data file has multiple DVs.");
384
1
        }
385
14
        deletion_vector = &delete_file;
386
14
    }
387
43
    if (deletion_vector == nullptr) {
388
30
        return Status::OK();
389
30
    }
390
13
    size_t bytes_read = 0;
391
13
    RETURN_IF_ERROR(validate_iceberg_deletion_vector_descriptor(*deletion_vector, bytes_read));
392
393
11
    const std::string data_file_path = iceberg_params.__isset.original_file_path
394
11
                                               ? iceberg_params.original_file_path
395
11
                                               : _data_file_path();
396
11
    desc->key = build_iceberg_deletion_vector_cache_key(data_file_path, *deletion_vector);
397
11
    desc->path = deletion_vector->path;
398
11
    desc->start_offset = deletion_vector->content_offset;
399
11
    desc->size = static_cast<int64_t>(bytes_read);
400
11
    desc->file_size = -1;
401
11
    desc->format = DeleteFileDesc::Format::ICEBERG;
402
11
    *has_delete_file = true;
403
11
    return Status::OK();
404
13
}
405
406
44
Status IcebergTableReader::_init_delete_predicates(const TTableFormatFileDesc& t_desc) {
407
44
    if (!t_desc.__isset.iceberg_params || _delete_predicates_initialized) {
408
2
        _delete_predicates_initialized = true;
409
2
        return Status::OK();
410
2
    }
411
42
    const auto& iceberg_params = t_desc.iceberg_params;
412
42
    if (!iceberg_params.__isset.format_version ||
413
42
        iceberg_params.format_version < MIN_SUPPORT_DELETE_FILES_VERSION ||
414
42
        !iceberg_params.__isset.delete_files || iceberg_params.delete_files.empty()) {
415
9
        _delete_predicates_initialized = true;
416
9
        return Status::OK();
417
9
    }
418
419
33
    std::vector<TIcebergDeleteFileDesc> position_delete_files;
420
33
    std::vector<TIcebergDeleteFileDesc> equality_delete_files;
421
34
    for (const auto& delete_file : iceberg_params.delete_files) {
422
34
        if (!delete_file.__isset.content) {
423
0
            continue;
424
0
        }
425
34
        if (delete_file.content == POSITION_DELETE) {
426
14
            position_delete_files.push_back(delete_file);
427
20
        } else if (delete_file.content == EQUALITY_DELETE) {
428
17
            equality_delete_files.push_back(delete_file);
429
17
        }
430
34
    }
431
    // Per Iceberg scan planning, position delete files apply only when there is no deletion vector
432
    // for the data file. DVs and position deletes now intentionally use different in-memory
433
    // representations, so use the Roaring pointer as the DV sentinel.
434
33
    if (_deletion_vector != nullptr) {
435
3
        position_delete_files.clear();
436
3
    }
437
    // Initialize position and equality delete predicates. Position delete files contain row
438
    // positions of deleted rows, which can be directly added to `_delete_rows`. Equality delete
439
    // files contain values of deleted rows, which require reading the files and building
440
    // predicates for later filtering.
441
33
    if (!position_delete_files.empty()) {
442
13
        RETURN_IF_ERROR(_init_position_delete_rows(position_delete_files));
443
13
    }
444
31
    if (!equality_delete_files.empty()) {
445
17
        RETURN_IF_ERROR(_init_equality_delete_predicates(equality_delete_files));
446
17
    }
447
448
31
    _delete_predicates_initialized = true;
449
31
    return Status::OK();
450
31
}
451
452
std::shared_ptr<io::FileSystemProperties> IcebergTableReader::_delete_file_system_properties(
453
28
        const TFileScanRangeParams& scan_params) {
454
28
    auto system_properties = std::make_shared<io::FileSystemProperties>();
455
28
    system_properties->system_type =
456
28
            scan_params.__isset.file_type ? scan_params.file_type : TFileType::FILE_LOCAL;
457
28
    system_properties->properties = scan_params.properties;
458
28
    system_properties->hdfs_params = scan_params.hdfs_params;
459
28
    if (scan_params.__isset.broker_addresses) {
460
0
        system_properties->broker_addresses.assign(scan_params.broker_addresses.begin(),
461
0
                                                   scan_params.broker_addresses.end());
462
0
    }
463
28
    return system_properties;
464
28
}
465
466
std::unique_ptr<io::FileDescription> IcebergTableReader::_delete_file_description(
467
28
        const TFileRangeDesc& range) {
468
28
    auto file_description = std::make_unique<io::FileDescription>();
469
28
    file_description->path = range.path;
470
28
    file_description->file_size = range.__isset.file_size ? range.file_size : -1;
471
28
    file_description->range_start_offset = range.__isset.start_offset ? range.start_offset : 0;
472
28
    file_description->range_size = range.__isset.size ? range.size : -1;
473
    // Iceberg delete files follow the same immutable-file contract as data files: a snapshot
474
    // references a fixed object and later changes publish a new file rather than replacing it.
475
28
    file_description->is_immutable = true;
476
28
    if (range.__isset.fs_name) {
477
4
        file_description->fs_name = range.fs_name;
478
4
    }
479
28
    return file_description;
480
28
}
481
482
12
std::string IcebergTableReader::_data_file_path() const {
483
12
    if (_iceberg_params.has_value() && _iceberg_params->__isset.original_file_path) {
484
11
        return _iceberg_params->original_file_path;
485
11
    }
486
1
    DORIS_CHECK(_current_task != nullptr);
487
1
    DORIS_CHECK(_current_task->data_file != nullptr);
488
1
    return _current_task->data_file->path;
489
12
}
490
491
9
Status IcebergTableReader::_append_row_position_output_column(format::FileScanRequest* request) {
492
9
    const auto row_position_column_id = format::LocalColumnId(format::ROW_POSITION_COLUMN_ID);
493
9
    _append_file_scan_column(request, row_position_column_id, &request->non_predicate_columns);
494
9
    _row_position_block_position = request->local_positions.at(row_position_column_id).value();
495
9
    return Status::OK();
496
9
}
497
498
const format::ColumnDefinition* IcebergTableReader::_find_equality_delete_data_field(
499
17
        const EqualityDeleteFilter& filter, size_t key_idx) const {
500
17
    DORIS_CHECK(key_idx < filter.field_ids.size());
501
17
    DORIS_CHECK(key_idx < filter.field_names.size());
502
17
    if (mapping_mode() != format::TableColumnMappingMode::BY_NAME) {
503
13
        const int field_id = filter.field_ids[key_idx];
504
13
        const auto field_it = std::ranges::find_if(
505
13
                _data_reader.file_schema, [field_id](const format::ColumnDefinition& field) {
506
13
                    return field.has_identifier_field_id() &&
507
13
                           field.get_identifier_field_id() == field_id;
508
13
                });
509
13
        return field_it == _data_reader.file_schema.end() ? nullptr : &*field_it;
510
13
    }
511
512
    // Equality keys are hidden scan dependencies and need not appear in the query projection.
513
    // Resolve their current name and aliases from the full table schema supplied by FE, falling
514
    // back to the delete-file name when history metadata is unavailable. Reuse ColumnMapper's
515
    // exact BY_NAME rules so case, string identifiers, and aliases on either side stay consistent.
516
4
    auto table_field = _find_equality_delete_table_field(filter, key_idx);
517
4
    return format::find_column_by_name(*table_field, _data_reader.file_schema);
518
17
}
519
520
std::optional<format::ColumnDefinition> IcebergTableReader::_find_equality_delete_table_field(
521
11
        const EqualityDeleteFilter& filter, size_t key_idx) const {
522
11
    DORIS_CHECK(key_idx < filter.field_ids.size());
523
11
    DORIS_CHECK(key_idx < filter.field_names.size());
524
11
    const int field_id = filter.field_ids[key_idx];
525
11
    auto table_field = _find_current_table_column_by_field_id(field_id, filter.key_types[key_idx]);
526
11
    if (!table_field.has_value()) {
527
5
        const auto projected_field = std::ranges::find_if(
528
5
                _projected_columns, [field_id](const format::ColumnDefinition& field) {
529
5
                    return field.has_identifier_field_id() &&
530
5
                           field.get_identifier_field_id() == field_id;
531
5
                });
532
5
        if (projected_field != _projected_columns.end()) {
533
            // Older scan descriptors and focused unit tests may omit history_schema_info. Keep the
534
            // projected metadata as a compatibility fallback, but never require projection when
535
            // the complete current schema is available.
536
2
            table_field = *projected_field;
537
2
        }
538
5
    }
539
11
    if (!table_field.has_value()) {
540
3
        table_field = format::ColumnDefinition {
541
3
                .identifier = {},
542
3
                .name = filter.field_names[key_idx],
543
3
                .type = filter.key_types[key_idx],
544
3
        };
545
3
    }
546
11
    return table_field;
547
11
}
548
549
std::string IcebergTableReader::_delete_file_cache_key(const char* prefix,
550
30
                                                       const std::string& path) const {
551
30
    DORIS_CHECK(prefix != nullptr);
552
30
    std::string fs_name;
553
30
    if (_current_task != nullptr && _current_task->data_file != nullptr) {
554
30
        fs_name = _current_task->data_file->fs_name;
555
30
    }
556
    // Delete descriptors can reuse the same path text in different filesystem namespaces. Encode
557
    // both variable-length strings so neither an fs/path boundary nor equality field-id suffixes
558
    // can be reinterpreted as path content; scan-level credentials/properties are shared here.
559
30
    std::ostringstream key;
560
30
    key << prefix << fs_name.size() << ':' << fs_name << ':' << path.size() << ':' << path;
561
30
    return key.str();
562
30
}
563
564
void IcebergTableReader::_append_equality_delete_row_count_carrier(
565
7
        format::FileScanRequest* request) {
566
7
    DORIS_CHECK(request != nullptr);
567
    // Columnar readers establish a filter batch's row count from predicate columns. If all
568
    // equality keys are missing, the predicate consists only of NULL literals and the filter block
569
    // would otherwise have zero rows. Use the virtual row-position column as the carrier instead
570
    // of an arbitrary physical column. For example, a data file may start with an unsupported
571
    // TIME_MILLIS leaf while the query projects only a supported `id`; selecting that TIME leaf as
572
    // a hidden carrier would make Parquet reject a column the query never requested. Row position
573
    // has one value per input row in both Parquet and ORC, is already used by delete predicates,
574
    // and is explicitly excluded from physical logical-type validation.
575
7
    _append_file_scan_column(request, format::LocalColumnId(format::ROW_POSITION_COLUMN_ID),
576
7
                             &request->predicate_columns);
577
7
}
578
579
41
Status IcebergTableReader::_append_equality_delete_predicates(format::FileScanRequest* request) {
580
41
    DORIS_CHECK(request != nullptr);
581
41
    for (const auto& filter : _equality_delete_filters) {
582
17
        auto delete_predicate =
583
17
                std::make_shared<EqualityDeletePredicate>(filter.delete_block, filter.field_ids);
584
17
        DCHECK_EQ(filter.field_ids.size(), filter.key_types.size());
585
17
        bool has_missing_key = false;
586
34
        for (size_t idx = 0; idx < filter.field_ids.size(); ++idx) {
587
17
            const auto* field = _find_equality_delete_data_field(filter, idx);
588
17
            if (field == nullptr) {
589
7
                auto table_field = _find_equality_delete_table_field(filter, idx);
590
7
                DORIS_CHECK(table_field.has_value());
591
7
                VExprSPtr key_expr;
592
7
                RETURN_IF_ERROR(build_missing_equality_delete_key_expr(
593
7
                        *table_field, filter.key_types[idx], &key_expr));
594
7
                delete_predicate->add_child(key_expr);
595
7
                has_missing_key = true;
596
7
                continue;
597
7
            }
598
10
            const auto field_column_id = format::LocalColumnId(field->file_local_id());
599
10
            _append_file_scan_column(request, field_column_id, &request->predicate_columns);
600
10
            const auto block_position = request->local_positions.at(field_column_id).value();
601
10
            auto slot = VSlotRef::create_shared(cast_set<int>(block_position),
602
10
                                                cast_set<int>(block_position), -1, field->type,
603
10
                                                field->name);
604
10
            if (field->type->equals(*filter.key_types[idx])) {
605
9
                delete_predicate->add_child(std::move(slot));
606
9
            } else {
607
1
                auto cast_expr = Cast::create_shared(filter.key_types[idx]);
608
1
                cast_expr->add_child(std::move(slot));
609
1
                delete_predicate->add_child(std::move(cast_expr));
610
1
            }
611
10
        }
612
17
        if (has_missing_key && request->predicate_columns.empty()) {
613
7
            _append_equality_delete_row_count_carrier(request);
614
7
        }
615
17
        request->delete_conjuncts.push_back(
616
17
                VExprContext::create_shared(std::move(delete_predicate)));
617
17
    }
618
41
    return Status::OK();
619
41
}
620
621
Status IcebergTableReader::_create_delete_file_reader(const TIcebergDeleteFileDesc& delete_file,
622
                                                      const TFileScanRangeParams& scan_params,
623
                                                      IcebergDeleteFileIOContext* delete_io_ctx,
624
28
                                                      std::unique_ptr<format::FileReader>* reader) {
625
28
    DORIS_CHECK(delete_io_ctx != nullptr);
626
28
    DORIS_CHECK(reader != nullptr);
627
28
    if (!delete_file.__isset.file_format) {
628
0
        return Status::InternalError("Iceberg delete file is missing file format");
629
0
    }
630
28
    if (delete_file.file_format != TFileFormatType::FORMAT_PARQUET &&
631
28
        delete_file.file_format != TFileFormatType::FORMAT_ORC) {
632
0
        return Status::NotSupported("Unsupported Iceberg delete file format {}",
633
0
                                    delete_file.file_format);
634
0
    }
635
28
    auto delete_range = build_iceberg_delete_file_range(delete_file.path);
636
28
    if (_current_task != nullptr && _current_task->data_file != nullptr &&
637
28
        !_current_task->data_file->fs_name.empty()) {
638
4
        delete_range.__set_fs_name(_current_task->data_file->fs_name);
639
4
    }
640
28
    auto system_properties = _delete_file_system_properties(scan_params);
641
28
    auto file_description = _delete_file_description(delete_range);
642
28
    std::shared_ptr<io::IOContext> io_ctx(&delete_io_ctx->io_ctx, [](io::IOContext*) {});
643
28
    const bool enable_mapping_timestamp_tz = scan_params.__isset.enable_mapping_timestamp_tz &&
644
28
                                             scan_params.enable_mapping_timestamp_tz;
645
28
    if (delete_file.file_format == TFileFormatType::FORMAT_PARQUET) {
646
28
        *reader = std::make_unique<format::parquet::ParquetReader>(
647
28
                system_properties, file_description, io_ctx, _scanner_profile, std::nullopt,
648
28
                enable_mapping_timestamp_tz);
649
28
    } else {
650
0
        *reader = std::make_unique<format::orc::OrcReader>(system_properties, file_description,
651
0
                                                           io_ctx, _scanner_profile, std::nullopt,
652
0
                                                           enable_mapping_timestamp_tz);
653
0
    }
654
28
    RETURN_IF_ERROR((*reader)->init(_runtime_state));
655
28
    return Status::OK();
656
28
}
657
658
Status IcebergTableReader::_read_position_delete_file(const TIcebergDeleteFileDesc& delete_file,
659
                                                      const TFileScanRangeParams& scan_params,
660
                                                      IcebergDeleteFileIOContext* delete_io_ctx,
661
12
                                                      PositionDeleteRowsCollector* collector) {
662
12
    DORIS_CHECK(collector != nullptr);
663
12
    std::unique_ptr<format::FileReader> reader;
664
12
    RETURN_IF_ERROR(_create_delete_file_reader(delete_file, scan_params, delete_io_ctx, &reader));
665
12
    DORIS_CHECK(reader != nullptr);
666
667
12
    std::vector<format::ColumnDefinition> schema;
668
12
    RETURN_IF_ERROR(reader->get_schema(&schema));
669
12
    format::ColumnDefinition* file_path_field = nullptr;
670
12
    format::ColumnDefinition* pos_field = nullptr;
671
24
    for (auto& field : schema) {
672
24
        if (field.name == ICEBERG_FILE_PATH) {
673
12
            file_path_field = &field;
674
12
        } else if (field.name == ICEBERG_ROW_POS) {
675
12
            pos_field = &field;
676
12
        }
677
24
    }
678
12
    if (file_path_field == nullptr || pos_field == nullptr) {
679
0
        return Status::InternalError("Position delete file is missing required columns");
680
0
    }
681
682
12
    auto request = std::make_shared<format::FileScanRequest>();
683
12
    request->non_predicate_columns = {
684
12
            format::LocalColumnIndex::top_level(
685
12
                    format::LocalColumnId(file_path_field->file_local_id())),
686
12
            format::LocalColumnIndex::top_level(format::LocalColumnId(pos_field->file_local_id()))};
687
12
    request->local_positions = {
688
12
            {format::LocalColumnId(file_path_field->file_local_id()),
689
12
             format::LocalIndex(ICEBERG_FILE_PATH_BLOCK_POSITION)},
690
12
            {format::LocalColumnId(pos_field->file_local_id()),
691
12
             format::LocalIndex(ICEBERG_ROW_POS_BLOCK_POSITION)},
692
12
    };
693
12
    RETURN_IF_ERROR(reader->open(request));
694
695
12
    bool eof = false;
696
12
    auto build_position_delete_block = [](const format::ColumnDefinition& file_path_field,
697
22
                                          const format::ColumnDefinition& pos_field) -> Block {
698
22
        Block block;
699
22
        block.insert(
700
22
                {file_path_field.type->create_column(), file_path_field.type, ICEBERG_FILE_PATH});
701
22
        block.insert({pos_field.type->create_column(), pos_field.type, ICEBERG_ROW_POS});
702
22
        return block;
703
22
    };
704
32
    while (!eof) {
705
22
        Block block = build_position_delete_block(*file_path_field, *pos_field);
706
22
        size_t read_rows = 0;
707
22
        RETURN_IF_ERROR(reader->get_block(&block, &read_rows, &eof));
708
22
        RETURN_IF_ERROR(collector->collect(block, read_rows));
709
22
    }
710
10
    return reader->close();
711
12
}
712
713
Status IcebergTableReader::_init_position_delete_rows(
714
13
        const std::vector<TIcebergDeleteFileDesc>& delete_files) {
715
13
    DORIS_CHECK(_split_cache != nullptr);
716
13
    TFileScanRangeParams delete_scan_params =
717
13
            _scan_params == nullptr ? TFileScanRangeParams() : *_scan_params;
718
13
    format::DeleteRows position_delete_rows;
719
13
    IcebergDeleteFileIOContext delete_io_ctx(_runtime_state);
720
13
    for (const auto& delete_file : delete_files) {
721
13
        Status read_status = Status::OK();
722
        // A position delete file normally references many data files. Cache the complete
723
        // path-to-position map once; caching only the current data file would still rescan the
724
        // shared delete file for every subsequent split.
725
13
        auto* rows_by_data_file =
726
13
                _split_cache->get<PositionDeleteRowsCollector::PositionDeleteFile>(
727
13
                        _delete_file_cache_key("iceberg_v2_position_delete_", delete_file.path),
728
13
                        [&]() -> PositionDeleteRowsCollector::PositionDeleteFile* {
729
12
                            auto result = std::make_unique<
730
12
                                    PositionDeleteRowsCollector::PositionDeleteFile>();
731
12
                            PositionDeleteRowsCollector collector(result.get());
732
12
                            read_status = _read_position_delete_file(
733
12
                                    delete_file, delete_scan_params, &delete_io_ctx, &collector);
734
12
                            if (!read_status.ok()) {
735
2
                                return nullptr;
736
2
                            }
737
12
                            for (auto& [_, rows] : *result) {
738
12
                                std::ranges::sort(rows);
739
12
                            }
740
10
                            return result.release();
741
12
                        });
742
13
        RETURN_IF_ERROR(read_status);
743
11
        DORIS_CHECK(rows_by_data_file != nullptr);
744
11
        const auto rows_it = rows_by_data_file->find(_data_file_path());
745
11
        if (rows_it == rows_by_data_file->end()) {
746
0
            continue;
747
0
        }
748
11
        auto first = rows_it->second.begin();
749
11
        auto last = rows_it->second.end();
750
        // Bounds are inclusive Iceberg position statistics supplied by FE. Apply them after the
751
        // cached per-data-file vector is sorted so irrelevant positions are sliced without a scan.
752
11
        if (delete_file.__isset.position_lower_bound) {
753
1
            first = std::lower_bound(first, last, delete_file.position_lower_bound);
754
1
        }
755
11
        if (delete_file.__isset.position_upper_bound) {
756
1
            last = std::upper_bound(first, last, delete_file.position_upper_bound);
757
1
        }
758
11
        position_delete_rows.insert(position_delete_rows.end(), first, last);
759
11
    }
760
11
    if (position_delete_rows.empty()) {
761
0
        return Status::OK();
762
0
    }
763
    // Position delete files and deletion vectors both become row-position deletes for the
764
    // common TableReader DeletePredicate path. Keep the merged rows in a member vector because
765
    // DeletePredicate stores a reference to the vector used by _delete_rows.
766
11
    _position_delete_rows_storage.insert(_position_delete_rows_storage.end(),
767
11
                                         position_delete_rows.begin(), position_delete_rows.end());
768
11
    std::sort(_position_delete_rows_storage.begin(), _position_delete_rows_storage.end());
769
11
    _position_delete_rows_storage.erase(
770
11
            std::unique(_position_delete_rows_storage.begin(), _position_delete_rows_storage.end()),
771
11
            _position_delete_rows_storage.end());
772
11
    _delete_rows = &_position_delete_rows_storage;
773
11
    return Status::OK();
774
11
}
775
776
Status IcebergTableReader::_init_equality_delete_predicates(
777
17
        const std::vector<TIcebergDeleteFileDesc>& delete_files) {
778
17
    DORIS_CHECK(_split_cache != nullptr);
779
17
    TFileScanRangeParams delete_scan_params =
780
17
            _scan_params == nullptr ? TFileScanRangeParams() : *_scan_params;
781
17
    IcebergDeleteFileIOContext delete_io_ctx(_runtime_state);
782
17
    for (const auto& delete_file : delete_files) {
783
17
        RETURN_IF_ERROR(
784
17
                _read_equality_delete_file(delete_file, delete_scan_params, &delete_io_ctx));
785
17
    }
786
17
    return Status::OK();
787
17
}
788
789
Status IcebergTableReader::_resolve_equality_delete_fields(
790
        const TIcebergDeleteFileDesc& delete_file,
791
        const std::vector<format::ColumnDefinition>& schema,
792
16
        std::vector<format::ColumnDefinition>* delete_fields, EqualityDeleteFilter* result) const {
793
16
    DORIS_CHECK(delete_fields != nullptr);
794
16
    DORIS_CHECK(result != nullptr);
795
16
    for (const auto field_id : delete_file.field_ids) {
796
16
        const auto field_it =
797
16
                std::ranges::find_if(schema, [field_id](const format::ColumnDefinition& field) {
798
16
                    return field.has_identifier_field_id() &&
799
16
                           field_id == field.get_identifier_field_id();
800
16
                });
801
16
        if (field_it == schema.end()) {
802
0
            return Status::InternalError("Can not find field id {} in equality delete file {}",
803
0
                                         field_id, delete_file.path);
804
0
        }
805
16
        if (!field_it->children.empty()) {
806
0
            return Status::NotSupported(
807
0
                    "Iceberg equality delete does not support complex column {}", field_it->name);
808
0
        }
809
16
        delete_fields->push_back(*field_it);
810
16
        result->field_ids.push_back(field_id);
811
16
        result->field_names.push_back(field_it->name);
812
16
        result->key_types.push_back(field_it->type);
813
16
    }
814
16
    return Status::OK();
815
16
}
816
817
Status IcebergTableReader::_load_equality_delete_file(const TIcebergDeleteFileDesc& delete_file,
818
                                                      const TFileScanRangeParams& scan_params,
819
                                                      IcebergDeleteFileIOContext* delete_io_ctx,
820
16
                                                      EqualityDeleteFilter* result) {
821
16
    DORIS_CHECK(result != nullptr);
822
16
    std::unique_ptr<format::FileReader> reader;
823
16
    RETURN_IF_ERROR(_create_delete_file_reader(delete_file, scan_params, delete_io_ctx, &reader));
824
16
    DORIS_CHECK(reader != nullptr);
825
826
16
    std::vector<format::ColumnDefinition> schema;
827
16
    RETURN_IF_ERROR(reader->get_schema(&schema));
828
16
    std::vector<format::ColumnDefinition> delete_fields;
829
16
    RETURN_IF_ERROR(_resolve_equality_delete_fields(delete_file, schema, &delete_fields, result));
830
831
16
    auto request = std::make_shared<format::FileScanRequest>();
832
16
    Block delete_block_template;
833
32
    for (size_t idx = 0; idx < delete_fields.size(); ++idx) {
834
16
        const auto& delete_field = delete_fields[idx];
835
16
        const auto local_column_id = format::LocalColumnId(delete_field.file_local_id());
836
16
        request->non_predicate_columns.push_back(
837
16
                format::LocalColumnIndex::top_level(local_column_id));
838
16
        request->local_positions.emplace(local_column_id, format::LocalIndex(idx));
839
16
        delete_block_template.insert(
840
16
                {delete_field.type->create_column(), delete_field.type, delete_field.name});
841
16
    }
842
16
    RETURN_IF_ERROR(reader->open(request));
843
844
16
    MutableBlock mutable_delete_block(delete_block_template.clone_empty());
845
16
    bool eof = false;
846
48
    while (!eof) {
847
32
        Block block = delete_block_template.clone_empty();
848
32
        size_t read_rows = 0;
849
32
        RETURN_IF_ERROR(reader->get_block(&block, &read_rows, &eof));
850
32
        if (read_rows > 0) {
851
16
            RETURN_IF_ERROR(mutable_delete_block.merge(block));
852
16
        }
853
32
    }
854
16
    RETURN_IF_ERROR(reader->close());
855
16
    result->delete_block = mutable_delete_block.to_block();
856
16
    return Status::OK();
857
16
}
858
859
Status IcebergTableReader::_read_equality_delete_file(const TIcebergDeleteFileDesc& delete_file,
860
                                                      const TFileScanRangeParams& scan_params,
861
17
                                                      IcebergDeleteFileIOContext* delete_io_ctx) {
862
17
    if (!delete_file.__isset.field_ids || delete_file.field_ids.empty()) {
863
0
        return Status::InternalError("Iceberg equality delete file is missing field ids");
864
0
    }
865
17
    std::ostringstream cache_key;
866
17
    cache_key << _delete_file_cache_key("iceberg_v2_equality_delete_", delete_file.path);
867
17
    cache_key << ':' << delete_file.field_ids.size();
868
17
    for (const auto field_id : delete_file.field_ids) {
869
17
        cache_key << ':' << field_id;
870
17
    }
871
17
    Status read_status = Status::OK();
872
    // Include the ordered equality ids in the key because the same physical delete file can be
873
    // projected with different key layouts. The cached block and its key metadata are immutable
874
    // after construction and therefore safe to copy into each split-local predicate.
875
17
    auto* cached_filter = _split_cache->get<EqualityDeleteFilter>(
876
17
            cache_key.str(), [&]() -> EqualityDeleteFilter* {
877
16
                auto result = std::make_unique<EqualityDeleteFilter>();
878
16
                read_status = _load_equality_delete_file(delete_file, scan_params, delete_io_ctx,
879
16
                                                         result.get());
880
16
                if (!read_status.ok()) {
881
0
                    return nullptr;
882
0
                }
883
16
                return result.release();
884
16
            });
885
17
    RETURN_IF_ERROR(read_status);
886
17
    DORIS_CHECK(cached_filter != nullptr);
887
17
    _equality_delete_filters.push_back(*cached_filter);
888
17
    return Status::OK();
889
17
}
890
891
9
Status IcebergTableReader::_materialize_row_lineage_row_id(Block* table_block, size_t column_idx) {
892
9
    if (_row_lineage_columns.first_row_id < 0) {
893
2
        return Status::OK();
894
2
    }
895
7
    DORIS_CHECK(_row_position_block_position < _data_reader.block_template.columns());
896
7
    const auto& row_position_column = assert_cast<const ColumnInt64&>(
897
7
            *_data_reader.block_template.get_by_position(_row_position_block_position).column);
898
7
    DORIS_CHECK(row_position_column.size() == table_block->rows());
899
7
    auto column = IColumn::mutate(
900
7
            table_block->get_by_position(column_idx).column->convert_to_full_column_if_const());
901
7
    auto* nullable_column = assert_cast<ColumnNullable*>(column.get());
902
7
    auto& null_map = nullable_column->get_null_map_data();
903
7
    auto& data = assert_cast<ColumnInt64&>(*nullable_column->get_nested_column_ptr()).get_data();
904
7
    DORIS_CHECK(null_map.size() == row_position_column.size());
905
7
    DORIS_CHECK(data.size() == row_position_column.size());
906
23
    for (size_t row = 0; row < row_position_column.size(); ++row) {
907
16
        if (null_map[row]) {
908
10
            null_map[row] = 0;
909
10
            data[row] = _row_lineage_columns.first_row_id + row_position_column.get_element(row);
910
10
        }
911
16
    }
912
7
    table_block->replace_by_position(column_idx, std::move(column));
913
7
    return Status::OK();
914
9
}
915
916
1
Status IcebergTableReader::_materialize_iceberg_rowid(Block* table_block, size_t column_idx) {
917
1
    DORIS_CHECK(_row_position_block_position < _data_reader.block_template.columns());
918
1
    const auto& row_position_column = assert_cast<const ColumnInt64&>(
919
1
            *_data_reader.block_template.get_by_position(_row_position_block_position).column);
920
1
    DORIS_CHECK(row_position_column.size() == table_block->rows());
921
922
1
    const auto& type = table_block->get_by_position(column_idx).type;
923
1
    auto column = type->create_column();
924
1
    auto* nullable_column = check_and_get_column<ColumnNullable>(column.get());
925
1
    auto* struct_column = nullable_column != nullptr
926
1
                                  ? check_and_get_column<ColumnStruct>(
927
1
                                            nullable_column->get_nested_column_ptr().get())
928
1
                                  : check_and_get_column<ColumnStruct>(column.get());
929
1
    DORIS_CHECK(struct_column != nullptr);
930
1
    DORIS_CHECK(struct_column->tuple_size() >= 4);
931
932
1
    const auto rows = row_position_column.size();
933
1
    const auto file_path = _data_file_path();
934
1
    const int32_t partition_spec_id =
935
1
            _iceberg_params.has_value() && _iceberg_params->__isset.partition_spec_id
936
1
                    ? _iceberg_params->partition_spec_id
937
1
                    : 0;
938
1
    const std::string partition_data_json =
939
1
            _iceberg_params.has_value() && _iceberg_params->__isset.partition_data_json
940
1
                    ? _iceberg_params->partition_data_json
941
1
                    : "";
942
943
1
    auto& file_path_column = struct_column->get_column(0);
944
1
    auto& row_pos_column = struct_column->get_column(1);
945
1
    auto& spec_id_column = struct_column->get_column(2);
946
1
    auto& partition_data_column = struct_column->get_column(3);
947
1
    file_path_column.reserve(rows);
948
1
    row_pos_column.reserve(rows);
949
1
    spec_id_column.reserve(rows);
950
1
    partition_data_column.reserve(rows);
951
3
    for (size_t row = 0; row < rows; ++row) {
952
2
        file_path_column.insert_data(file_path.data(), file_path.size());
953
2
        const int64_t row_pos = row_position_column.get_element(row);
954
2
        row_pos_column.insert_data(reinterpret_cast<const char*>(&row_pos), sizeof(row_pos));
955
2
        spec_id_column.insert_data(reinterpret_cast<const char*>(&partition_spec_id),
956
2
                                   sizeof(partition_spec_id));
957
2
        partition_data_column.insert_data(partition_data_json.data(), partition_data_json.size());
958
2
    }
959
1
    if (nullable_column != nullptr) {
960
1
        nullable_column->get_null_map_data().resize_fill(rows, 0);
961
1
    }
962
1
    table_block->replace_by_position(column_idx, std::move(column));
963
1
    return Status::OK();
964
1
}
965
966
Status IcebergTableReader::_materialize_row_lineage_last_updated_sequence_number(
967
8
        Block* table_block, size_t column_idx) {
968
8
    if (_row_lineage_columns.last_updated_sequence_number < 0) {
969
2
        return Status::OK();
970
2
    }
971
6
    auto column = IColumn::mutate(
972
6
            table_block->get_by_position(column_idx).column->convert_to_full_column_if_const());
973
6
    auto* nullable_column = assert_cast<ColumnNullable*>(column.get());
974
6
    auto& null_map = nullable_column->get_null_map_data();
975
6
    auto& data = assert_cast<ColumnInt64&>(*nullable_column->get_nested_column_ptr()).get_data();
976
6
    DORIS_CHECK(null_map.size() == table_block->rows());
977
6
    DORIS_CHECK(data.size() == table_block->rows());
978
20
    for (size_t row = 0; row < table_block->rows(); ++row) {
979
14
        if (null_map[row]) {
980
8
            null_map[row] = 0;
981
8
            data[row] = _row_lineage_columns.last_updated_sequence_number;
982
8
        }
983
14
    }
984
6
    table_block->replace_by_position(column_idx, std::move(column));
985
6
    return Status::OK();
986
8
}
987
988
8
bool IcebergTableReader::_need_row_lineage_row_id() const {
989
8
    if (_data_reader.column_mapper != nullptr) {
990
7
        for (const auto& mapping : _data_reader.column_mapper->mappings()) {
991
7
            if (mapping.virtual_column_type == format::TableVirtualColumnType::ROW_ID) {
992
7
                return true;
993
7
            }
994
7
        }
995
7
    }
996
1
    return std::ranges::any_of(_projected_columns, is_projected_row_lineage_row_id);
997
8
}
998
999
33
bool IcebergTableReader::_need_iceberg_rowid() const {
1000
33
    if (_data_reader.column_mapper != nullptr) {
1001
37
        for (const auto& mapping : _data_reader.column_mapper->mappings()) {
1002
37
            if (mapping.virtual_column_type == format::TableVirtualColumnType::ICEBERG_ROWID) {
1003
1
                return true;
1004
1
            }
1005
37
        }
1006
33
    }
1007
32
    return std::ranges::any_of(_projected_columns, is_projected_iceberg_rowid);
1008
33
}
1009
1010
} // namespace doris::format::iceberg