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