Coverage Report

Created: 2026-07-19 11:24

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