be/src/exec/sink/viceberg_delete_sink.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 "exec/sink/viceberg_delete_sink.h" |
19 | | |
20 | | #include <fmt/format.h> |
21 | | #include <rapidjson/stringbuffer.h> |
22 | | #include <rapidjson/writer.h> |
23 | | #include <zlib.h> |
24 | | |
25 | | #include "common/logging.h" |
26 | | #include "core/block/column_with_type_and_name.h" |
27 | | #include "core/column/column_nullable.h" |
28 | | #include "core/column/column_string.h" |
29 | | #include "core/column/column_struct.h" |
30 | | #include "core/column/column_vector.h" |
31 | | #include "core/data_type/data_type_factory.hpp" |
32 | | #include "core/data_type/data_type_nullable.h" |
33 | | #include "core/data_type/data_type_number.h" |
34 | | #include "core/data_type/data_type_string.h" |
35 | | #include "core/data_type/data_type_struct.h" |
36 | | #include "exec/common/endian.h" |
37 | | #include "exprs/vexpr.h" |
38 | | #include "format/table/deletion_vector.h" |
39 | | #include "format/table/iceberg_delete_file_reader_helper.h" |
40 | | #include "format/transformer/vfile_format_transformer.h" |
41 | | #include "io/file_factory.h" |
42 | | #include "runtime/runtime_state.h" |
43 | | #include "util/slice.h" |
44 | | #include "util/string_util.h" |
45 | | #include "util/uid_util.h" |
46 | | |
47 | | namespace doris { |
48 | | |
49 | | namespace { |
50 | | |
51 | | class RewriteBitmapVisitor final : public IcebergPositionDeleteVisitor { |
52 | | public: |
53 | | RewriteBitmapVisitor(const std::string& referenced_data_file_path, |
54 | | roaring::Roaring64Map* rows_to_delete) |
55 | 4 | : _referenced_data_file_path(referenced_data_file_path), |
56 | 4 | _rows_to_delete(rows_to_delete) {} |
57 | | |
58 | 4 | Status visit(const std::string& file_path, int64_t pos) override { |
59 | 4 | if (_rows_to_delete == nullptr) { |
60 | 0 | return Status::InvalidArgument("rows_to_delete is null"); |
61 | 0 | } |
62 | 4 | if (file_path == _referenced_data_file_path) { |
63 | 4 | _rows_to_delete->add(static_cast<uint64_t>(pos)); |
64 | 4 | } |
65 | 4 | return Status::OK(); |
66 | 4 | } |
67 | | |
68 | | private: |
69 | | const std::string& _referenced_data_file_path; |
70 | | roaring::Roaring64Map* _rows_to_delete; |
71 | | }; |
72 | | |
73 | | Status load_rewritable_delete_rows(RuntimeState* state, RuntimeProfile* profile, |
74 | | const std::string& referenced_data_file_path, |
75 | | const std::vector<TIcebergDeleteFileDesc>& delete_files, |
76 | | const std::map<std::string, std::string>& hadoop_conf, |
77 | | TFileType::type file_type, |
78 | | const std::vector<TNetworkAddress>& broker_addresses, |
79 | 28 | roaring::Roaring64Map* rows_to_delete) { |
80 | 28 | if (rows_to_delete == nullptr) { |
81 | 0 | return Status::InvalidArgument("rows_to_delete is null"); |
82 | 0 | } |
83 | 28 | if (state == nullptr || profile == nullptr || delete_files.empty()) { |
84 | 0 | return Status::OK(); |
85 | 0 | } |
86 | | |
87 | 28 | TFileScanRangeParams params = |
88 | 28 | build_iceberg_delete_scan_range_params(hadoop_conf, file_type, broker_addresses); |
89 | 28 | IcebergDeleteFileIOContext delete_file_io_ctx(state); |
90 | 28 | IcebergDeleteFileReaderOptions options; |
91 | 28 | options.state = state; |
92 | 28 | options.profile = profile; |
93 | 28 | options.scan_params = ¶ms; |
94 | 28 | options.io_ctx = &delete_file_io_ctx.io_ctx; |
95 | 28 | options.batch_size = 102400; |
96 | | |
97 | 28 | for (const auto& delete_file : delete_files) { |
98 | 28 | if (is_iceberg_deletion_vector(delete_file)) { |
99 | 24 | RETURN_IF_ERROR(read_iceberg_deletion_vector(delete_file, options, rows_to_delete)); |
100 | 24 | continue; |
101 | 24 | } |
102 | 4 | RewriteBitmapVisitor visitor(referenced_data_file_path, rows_to_delete); |
103 | 4 | RETURN_IF_ERROR(read_iceberg_position_delete_file(delete_file, options, &visitor)); |
104 | 4 | } |
105 | 28 | return Status::OK(); |
106 | 28 | } |
107 | | |
108 | | } // namespace |
109 | | |
110 | 192 | Status calculate_iceberg_deletion_vector_content_size(size_t bitmap_size, int64_t* content_size) { |
111 | 192 | DORIS_CHECK(content_size != nullptr); |
112 | 192 | constexpr size_t max_bitmap_size = static_cast<size_t>(MAX_ICEBERG_DELETION_VECTOR_BYTES) - |
113 | 192 | ICEBERG_DELETION_VECTOR_BLOB_OVERHEAD_BYTES; |
114 | 192 | if (bitmap_size > max_bitmap_size) { |
115 | 1 | return Status::NotSupported( |
116 | 1 | "Iceberg deletion vector bitmap size exceeds Doris supported limit: {}, " |
117 | 1 | "maximum bitmap size: {}, content size limit: {}", |
118 | 1 | bitmap_size, max_bitmap_size, MAX_ICEBERG_DELETION_VECTOR_BYTES); |
119 | 1 | } |
120 | 191 | *content_size = static_cast<int64_t>(bitmap_size + ICEBERG_DELETION_VECTOR_BLOB_OVERHEAD_BYTES); |
121 | 191 | return Status::OK(); |
122 | 192 | } |
123 | | |
124 | | VIcebergDeleteSink::VIcebergDeleteSink(const TDataSink& t_sink, |
125 | | const VExprContextSPtrs& output_exprs, |
126 | | std::shared_ptr<Dependency> dep, |
127 | | std::shared_ptr<Dependency> fin_dep) |
128 | 2.16k | : AsyncResultWriter(output_exprs, dep, fin_dep), _t_sink(t_sink) { |
129 | 2.16k | DCHECK(_t_sink.__isset.iceberg_delete_sink); |
130 | 2.16k | } |
131 | | |
132 | 2.15k | Status VIcebergDeleteSink::init_properties(ObjectPool* pool) { |
133 | 2.15k | const auto& delete_sink = _t_sink.iceberg_delete_sink; |
134 | | |
135 | 2.15k | _delete_type = delete_sink.delete_type; |
136 | 2.15k | if (_delete_type != TFileContent::POSITION_DELETES) { |
137 | 1 | return Status::NotSupported("Iceberg delete only supports position delete files"); |
138 | 1 | } |
139 | | |
140 | | // Get file format settings |
141 | 2.15k | if (delete_sink.__isset.file_format) { |
142 | 2.15k | _file_format_type = delete_sink.file_format; |
143 | 2.15k | } |
144 | | |
145 | 2.15k | if (delete_sink.__isset.compress_type) { |
146 | 2.15k | _compress_type = delete_sink.compress_type; |
147 | 2.15k | } |
148 | | |
149 | | // Get output path and table location |
150 | 2.15k | if (delete_sink.__isset.output_path) { |
151 | 2.15k | _output_path = delete_sink.output_path; |
152 | 2.15k | } |
153 | | |
154 | 2.15k | if (delete_sink.__isset.table_location) { |
155 | 2.15k | _table_location = delete_sink.table_location; |
156 | 2.15k | } |
157 | | |
158 | | // Get Hadoop configuration |
159 | 2.15k | if (delete_sink.__isset.hadoop_config) { |
160 | 2.14k | _hadoop_conf.insert(delete_sink.hadoop_config.begin(), delete_sink.hadoop_config.end()); |
161 | 2.14k | } |
162 | | |
163 | 2.15k | if (delete_sink.__isset.file_type) { |
164 | 2.15k | _file_type = delete_sink.file_type; |
165 | 2.15k | } |
166 | | |
167 | 2.15k | if (delete_sink.__isset.broker_addresses) { |
168 | 0 | _broker_addresses.assign(delete_sink.broker_addresses.begin(), |
169 | 0 | delete_sink.broker_addresses.end()); |
170 | 0 | } |
171 | | |
172 | | // Get partition information |
173 | 2.15k | if (delete_sink.__isset.partition_spec_id) { |
174 | 919 | _partition_spec_id = delete_sink.partition_spec_id; |
175 | 919 | } |
176 | | |
177 | 2.15k | if (delete_sink.__isset.partition_data_json) { |
178 | 1 | _partition_data_json = delete_sink.partition_data_json; |
179 | 1 | } |
180 | | |
181 | 2.15k | if (delete_sink.__isset.format_version) { |
182 | 2.14k | _format_version = delete_sink.format_version; |
183 | 2.14k | } |
184 | | |
185 | | // for merge old deletion vector and old position delete to a new deletion vector. |
186 | 2.15k | if (_format_version >= 3 && delete_sink.__isset.rewritable_delete_file_sets) { |
187 | 1.95k | for (const auto& delete_file_set : delete_sink.rewritable_delete_file_sets) { |
188 | 1.95k | if (!delete_file_set.__isset.referenced_data_file_path || |
189 | 1.95k | !delete_file_set.__isset.delete_files || |
190 | 1.95k | delete_file_set.referenced_data_file_path.empty() || |
191 | 1.95k | delete_file_set.delete_files.empty()) { |
192 | 1.53k | continue; |
193 | 1.53k | } |
194 | 416 | _rewritable_delete_files.emplace(delete_file_set.referenced_data_file_path, |
195 | 416 | delete_file_set.delete_files); |
196 | 416 | } |
197 | 1.31k | } |
198 | | |
199 | 2.15k | return Status::OK(); |
200 | 2.15k | } |
201 | | |
202 | 2.14k | Status VIcebergDeleteSink::open(RuntimeState* state, RuntimeProfile* profile) { |
203 | 2.14k | _state = state; |
204 | | |
205 | | // Initialize counters |
206 | 2.14k | _written_rows_counter = ADD_COUNTER(profile, "RowsWritten", TUnit::UNIT); |
207 | 2.14k | _send_data_timer = ADD_TIMER(profile, "SendDataTime"); |
208 | 2.14k | _write_delete_files_timer = ADD_TIMER(profile, "WriteDeleteFilesTime"); |
209 | 2.14k | _delete_file_count_counter = ADD_COUNTER(profile, "DeleteFileCount", TUnit::UNIT); |
210 | 2.14k | _open_timer = ADD_TIMER(profile, "OpenTime"); |
211 | 2.14k | _close_timer = ADD_TIMER(profile, "CloseTime"); |
212 | | |
213 | 2.14k | SCOPED_TIMER(_open_timer); |
214 | | |
215 | 2.14k | if (_format_version < 3) { |
216 | 833 | RETURN_IF_ERROR(_init_position_delete_output_exprs()); |
217 | 833 | } |
218 | | |
219 | 2.14k | LOG(INFO) << fmt::format( |
220 | 2.14k | "VIcebergDeleteSink opened: delete_type={}, output_path={}, format_version={}", |
221 | 2.14k | to_string(_delete_type), _output_path, _format_version); |
222 | | |
223 | 2.14k | return Status::OK(); |
224 | 2.14k | } |
225 | | |
226 | 292 | Status VIcebergDeleteSink::write(RuntimeState* state, Block& block) { |
227 | 292 | SCOPED_TIMER(_send_data_timer); |
228 | | |
229 | 292 | if (block.rows() == 0) { |
230 | 0 | return Status::OK(); |
231 | 0 | } |
232 | | |
233 | 292 | _row_count += block.rows(); |
234 | | |
235 | 292 | if (_delete_type != TFileContent::POSITION_DELETES) { |
236 | 0 | return Status::NotSupported("Iceberg delete only supports position delete files"); |
237 | 0 | } |
238 | | |
239 | | // Extract $row_id column and group by file_path |
240 | 292 | RETURN_IF_ERROR(_collect_position_deletes(block, _file_deletions)); |
241 | | |
242 | 292 | if (_written_rows_counter) { |
243 | 292 | COUNTER_UPDATE(_written_rows_counter, block.rows()); |
244 | 292 | } |
245 | | |
246 | 292 | return Status::OK(); |
247 | 292 | } |
248 | | |
249 | 2.14k | Status VIcebergDeleteSink::close(Status close_status) { |
250 | 2.14k | SCOPED_TIMER(_close_timer); |
251 | | |
252 | 2.14k | if (!close_status.ok()) { |
253 | 0 | LOG(WARNING) << fmt::format("VIcebergDeleteSink close with error: {}", |
254 | 0 | close_status.to_string()); |
255 | 0 | return close_status; |
256 | 0 | } |
257 | | |
258 | 2.14k | if (_delete_type == TFileContent::POSITION_DELETES && !_file_deletions.empty()) { |
259 | 292 | SCOPED_TIMER(_write_delete_files_timer); |
260 | 292 | if (_format_version >= 3) { |
261 | 186 | RETURN_IF_ERROR(_write_deletion_vector_files(_file_deletions)); |
262 | 186 | } else { |
263 | 106 | RETURN_IF_ERROR(_write_position_delete_files(_file_deletions)); |
264 | 106 | } |
265 | 292 | } |
266 | | |
267 | | // Update counters |
268 | 2.14k | if (_delete_file_count_counter) { |
269 | 2.14k | COUNTER_UPDATE(_delete_file_count_counter, _delete_file_count); |
270 | 2.14k | } |
271 | | |
272 | 2.14k | LOG(INFO) << fmt::format("VIcebergDeleteSink closed: rows={}, delete_files={}", _row_count, |
273 | 2.14k | _delete_file_count); |
274 | | |
275 | 2.14k | if (_state != nullptr) { |
276 | 2.14k | for (const auto& commit_data : _commit_data_list) { |
277 | 296 | _state->add_iceberg_commit_datas(commit_data); |
278 | 296 | } |
279 | 2.14k | } |
280 | | |
281 | 2.14k | return Status::OK(); |
282 | 2.14k | } |
283 | | |
284 | 299 | int VIcebergDeleteSink::_get_row_id_column_index(const Block& block) { |
285 | | // Find __DORIS_ICEBERG_ROWID_COL__ column in block |
286 | 380 | for (size_t i = 0; i < block.columns(); ++i) { |
287 | 380 | const auto& col_name = block.get_by_position(i).name; |
288 | 380 | if (col_name == doris::BeConsts::ICEBERG_ROWID_COL) { |
289 | 299 | return static_cast<int>(i); |
290 | 299 | } |
291 | 380 | } |
292 | 0 | return -1; |
293 | 299 | } |
294 | | |
295 | | Status VIcebergDeleteSink::_collect_position_deletes( |
296 | 297 | const Block& block, std::map<std::string, IcebergFileDeletion>& file_deletions) { |
297 | | // Find row id column |
298 | 297 | int row_id_col_idx = _get_row_id_column_index(block); |
299 | 297 | if (row_id_col_idx < 0) { |
300 | 0 | return Status::InternalError( |
301 | 0 | "__DORIS_ICEBERG_ROWID_COL__ column not found in block for position delete"); |
302 | 0 | } |
303 | | |
304 | 297 | const auto& row_id_col = block.get_by_position(row_id_col_idx); |
305 | 297 | const IColumn* row_id_data = row_id_col.column.get(); |
306 | 297 | const IDataType* row_id_type = row_id_col.type.get(); |
307 | 297 | const auto* nullable_col = check_and_get_column<ColumnNullable>(row_id_data); |
308 | 297 | if (nullable_col != nullptr) { |
309 | 108 | row_id_data = nullable_col->get_nested_column_ptr().get(); |
310 | 108 | } |
311 | 297 | const auto* nullable_type = check_and_get_data_type<DataTypeNullable>(row_id_type); |
312 | 297 | if (nullable_type != nullptr) { |
313 | 108 | row_id_type = nullable_type->get_nested_type().get(); |
314 | 108 | } |
315 | 297 | const auto* struct_col = check_and_get_column<ColumnStruct>(row_id_data); |
316 | 297 | const auto* struct_type = check_and_get_data_type<DataTypeStruct>(row_id_type); |
317 | 297 | if (!struct_col || !struct_type) { |
318 | 0 | return Status::InternalError("__DORIS_ICEBERG_ROWID_COL__ column is not a struct column"); |
319 | 0 | } |
320 | | |
321 | | // __DORIS_ICEBERG_ROWID_COL__ struct: |
322 | | // (file_path: STRING, row_position: BIGINT, partition_spec_id: INT, partition_data: STRING) |
323 | 297 | size_t field_count = struct_col->tuple_size(); |
324 | 297 | if (field_count < 2) { |
325 | 0 | return Status::InternalError( |
326 | 0 | "__DORIS_ICEBERG_ROWID_COL__ struct must have at least 2 fields " |
327 | 0 | "(file_path, row_position)"); |
328 | 0 | } |
329 | | |
330 | 1.18k | auto normalize = [](const std::string& name) { return doris::to_lower(name); }; |
331 | | |
332 | 297 | int file_path_idx = -1; |
333 | 297 | int row_position_idx = -1; |
334 | 297 | int spec_id_idx = -1; |
335 | 297 | int partition_data_idx = -1; |
336 | 297 | const auto& field_names = struct_type->get_element_names(); |
337 | 1.48k | for (size_t i = 0; i < field_names.size(); ++i) { |
338 | 1.18k | std::string name = normalize(field_names[i]); |
339 | 1.18k | if (file_path_idx < 0 && name == "file_path") { |
340 | 297 | file_path_idx = static_cast<int>(i); |
341 | 887 | } else if (row_position_idx < 0 && name == "row_position") { |
342 | 296 | row_position_idx = static_cast<int>(i); |
343 | 591 | } else if (spec_id_idx < 0 && name == "partition_spec_id") { |
344 | 294 | spec_id_idx = static_cast<int>(i); |
345 | 297 | } else if (partition_data_idx < 0 && name == "partition_data") { |
346 | 294 | partition_data_idx = static_cast<int>(i); |
347 | 294 | } |
348 | 1.18k | } |
349 | | |
350 | 297 | if (file_path_idx < 0 || row_position_idx < 0) { |
351 | 1 | return Status::InternalError( |
352 | 1 | "__DORIS_ICEBERG_ROWID_COL__ must contain standard fields file_path and " |
353 | 1 | "row_position"); |
354 | 1 | } |
355 | 296 | if (field_count >= 3 && spec_id_idx < 0) { |
356 | 0 | return Status::InternalError( |
357 | 0 | "__DORIS_ICEBERG_ROWID_COL__ must use standard field name partition_spec_id"); |
358 | 0 | } |
359 | 296 | if (field_count >= 4 && partition_data_idx < 0) { |
360 | 0 | return Status::InternalError( |
361 | 0 | "__DORIS_ICEBERG_ROWID_COL__ must use standard field name partition_data"); |
362 | 0 | } |
363 | | |
364 | 296 | const auto* file_path_col = check_and_get_column<ColumnString>( |
365 | 296 | remove_nullable(struct_col->get_column_ptr(file_path_idx)).get()); |
366 | 296 | const auto* row_position_col = check_and_get_column<ColumnVector<TYPE_BIGINT>>( |
367 | 296 | remove_nullable(struct_col->get_column_ptr(row_position_idx)).get()); |
368 | | |
369 | 296 | if (!file_path_col || !row_position_col) { |
370 | 0 | return Status::InternalError( |
371 | 0 | "__DORIS_ICEBERG_ROWID_COL__ struct fields have incorrect types"); |
372 | 0 | } |
373 | | |
374 | 296 | const ColumnVector<TYPE_INT>* spec_id_col = nullptr; |
375 | 296 | const ColumnString* partition_data_col = nullptr; |
376 | 296 | if (spec_id_idx >= 0 && spec_id_idx < static_cast<int>(field_count)) { |
377 | 294 | spec_id_col = check_and_get_column<ColumnVector<TYPE_INT>>( |
378 | 294 | remove_nullable(struct_col->get_column_ptr(spec_id_idx)).get()); |
379 | 294 | if (!spec_id_col) { |
380 | 0 | return Status::InternalError( |
381 | 0 | "__DORIS_ICEBERG_ROWID_COL__ partition_spec_id has incorrect type"); |
382 | 0 | } |
383 | 294 | } |
384 | 296 | if (partition_data_idx >= 0 && partition_data_idx < static_cast<int>(field_count)) { |
385 | 294 | partition_data_col = check_and_get_column<ColumnString>( |
386 | 294 | remove_nullable(struct_col->get_column_ptr(partition_data_idx)).get()); |
387 | 294 | if (!partition_data_col) { |
388 | 0 | return Status::InternalError( |
389 | 0 | "__DORIS_ICEBERG_ROWID_COL__ partition_data has incorrect type"); |
390 | 0 | } |
391 | 294 | } |
392 | | |
393 | | // Group by file_path using roaring bitmap |
394 | 617 | for (size_t i = 0; i < block.rows(); ++i) { |
395 | 322 | std::string file_path = file_path_col->get_data_at(i).to_string(); |
396 | 322 | int64_t row_position = row_position_col->get_element(i); |
397 | 322 | if (row_position < 0) { |
398 | 1 | return Status::InternalError("Invalid row_position {} in row_id column", row_position); |
399 | 1 | } |
400 | | |
401 | 321 | int32_t partition_spec_id = _partition_spec_id; |
402 | 321 | std::string partition_data_json = _partition_data_json; |
403 | 321 | if (spec_id_col != nullptr) { |
404 | 320 | partition_spec_id = spec_id_col->get_element(i); |
405 | 320 | } |
406 | 321 | if (partition_data_col != nullptr) { |
407 | 320 | partition_data_json = partition_data_col->get_data_at(i).to_string(); |
408 | 320 | } |
409 | | |
410 | 321 | auto [iter, inserted] = file_deletions.emplace( |
411 | 321 | file_path, IcebergFileDeletion(partition_spec_id, partition_data_json)); |
412 | 321 | if (!inserted) { |
413 | 21 | if (iter->second.partition_spec_id != partition_spec_id || |
414 | 21 | iter->second.partition_data_json != partition_data_json) { |
415 | 0 | LOG(WARNING) << fmt::format( |
416 | 0 | "Mismatched partition info for file {}, existing spec_id={}, data={}, " |
417 | 0 | "new spec_id={}, data={}", |
418 | 0 | file_path, iter->second.partition_spec_id, iter->second.partition_data_json, |
419 | 0 | partition_spec_id, partition_data_json); |
420 | 0 | } |
421 | 21 | } |
422 | 321 | iter->second.rows_to_delete.add(static_cast<uint64_t>(row_position)); |
423 | 321 | } |
424 | | |
425 | 295 | return Status::OK(); |
426 | 296 | } |
427 | | |
428 | | Status VIcebergDeleteSink::_write_position_delete_files( |
429 | 106 | const std::map<std::string, IcebergFileDeletion>& file_deletions) { |
430 | 106 | constexpr size_t kBatchSize = 4096; |
431 | 108 | for (const auto& [data_file_path, deletion] : file_deletions) { |
432 | 108 | if (deletion.rows_to_delete.isEmpty()) { |
433 | 0 | continue; |
434 | 0 | } |
435 | | // Generate unique delete file path |
436 | 108 | std::string delete_file_path = _generate_delete_file_path(data_file_path); |
437 | | |
438 | | // Create delete file writer |
439 | 108 | auto writer = VIcebergDeleteFileWriterFactory::create_writer( |
440 | 108 | TFileContent::POSITION_DELETES, delete_file_path, _file_format_type, |
441 | 108 | _compress_type); |
442 | | |
443 | | // Build column names for position delete |
444 | 108 | std::vector<std::string> column_names = {"file_path", "pos"}; |
445 | | |
446 | 108 | if (_position_delete_output_expr_ctxs.empty()) { |
447 | 0 | RETURN_IF_ERROR(_init_position_delete_output_exprs()); |
448 | 0 | } |
449 | | |
450 | | // Open writer |
451 | 108 | RETURN_IF_ERROR(writer->open(_state, _state->runtime_profile(), |
452 | 108 | _position_delete_output_expr_ctxs, column_names, _hadoop_conf, |
453 | 108 | _file_type, _broker_addresses)); |
454 | | |
455 | | // Build block with (file_path, pos) columns |
456 | 108 | std::vector<int64_t> positions; |
457 | 108 | positions.reserve(kBatchSize); |
458 | 228 | for (auto it = deletion.rows_to_delete.begin(); it != deletion.rows_to_delete.end(); ++it) { |
459 | 120 | positions.push_back(static_cast<int64_t>(*it)); |
460 | 120 | if (positions.size() >= kBatchSize) { |
461 | 0 | Block delete_block; |
462 | 0 | RETURN_IF_ERROR( |
463 | 0 | _build_position_delete_block(data_file_path, positions, delete_block)); |
464 | 0 | RETURN_IF_ERROR(writer->write(delete_block)); |
465 | 0 | positions.clear(); |
466 | 0 | } |
467 | 120 | } |
468 | 108 | if (!positions.empty()) { |
469 | 108 | Block delete_block; |
470 | 108 | RETURN_IF_ERROR(_build_position_delete_block(data_file_path, positions, delete_block)); |
471 | 108 | RETURN_IF_ERROR(writer->write(delete_block)); |
472 | 108 | } |
473 | | |
474 | | // Set partition info on writer before close |
475 | 108 | writer->set_partition_info(deletion.partition_spec_id, deletion.partition_data_json); |
476 | | |
477 | | // Close writer and collect commit data |
478 | 108 | TIcebergCommitData commit_data; |
479 | 108 | RETURN_IF_ERROR(writer->close(commit_data)); |
480 | | |
481 | | // Set referenced data file path |
482 | 108 | commit_data.__set_referenced_data_file_path(data_file_path); |
483 | | |
484 | 108 | _commit_data_list.push_back(commit_data); |
485 | 108 | _delete_file_count++; |
486 | | |
487 | 108 | VLOG(1) << fmt::format("Written position delete file: path={}, rows={}, referenced_file={}", |
488 | 0 | delete_file_path, commit_data.row_count, data_file_path); |
489 | 108 | } |
490 | | |
491 | 106 | return Status::OK(); |
492 | 106 | } |
493 | | |
494 | 835 | Status VIcebergDeleteSink::_init_position_delete_output_exprs() { |
495 | 835 | if (!_position_delete_output_expr_ctxs.empty()) { |
496 | 0 | return Status::OK(); |
497 | 0 | } |
498 | | |
499 | 835 | std::vector<TExpr> texprs; |
500 | 835 | texprs.reserve(2); |
501 | | |
502 | 835 | std::string empty_string; |
503 | 835 | TExprNode file_path_node = |
504 | 835 | create_texpr_node_from(&empty_string, PrimitiveType::TYPE_STRING, 0, 0); |
505 | 835 | file_path_node.__set_num_children(0); |
506 | 835 | file_path_node.__set_output_scale(0); |
507 | 835 | file_path_node.__set_is_nullable(false); |
508 | 835 | TExpr file_path_expr; |
509 | 835 | file_path_expr.nodes.emplace_back(std::move(file_path_node)); |
510 | 835 | texprs.emplace_back(std::move(file_path_expr)); |
511 | | |
512 | 835 | int64_t zero = 0; |
513 | 835 | TExprNode pos_node = create_texpr_node_from(&zero, PrimitiveType::TYPE_BIGINT, 0, 0); |
514 | 835 | pos_node.__set_num_children(0); |
515 | 835 | pos_node.__set_output_scale(0); |
516 | 835 | pos_node.__set_is_nullable(false); |
517 | 835 | TExpr pos_expr; |
518 | 835 | pos_expr.nodes.emplace_back(std::move(pos_node)); |
519 | 835 | texprs.emplace_back(std::move(pos_expr)); |
520 | | |
521 | 835 | RETURN_IF_ERROR(VExpr::create_expr_trees(texprs, _position_delete_output_expr_ctxs)); |
522 | 835 | return Status::OK(); |
523 | 835 | } |
524 | | |
525 | | Status VIcebergDeleteSink::_build_position_delete_block(const std::string& file_path, |
526 | | const std::vector<int64_t>& positions, |
527 | 109 | Block& output_block) { |
528 | | // Create file_path column (repeated for each position) |
529 | 109 | auto file_path_col = ColumnString::create(); |
530 | 233 | for (size_t i = 0; i < positions.size(); ++i) { |
531 | 124 | file_path_col->insert_data(file_path.data(), file_path.size()); |
532 | 124 | } |
533 | | |
534 | | // Create pos column |
535 | 109 | auto pos_col = ColumnVector<TYPE_BIGINT>::create(); |
536 | 109 | pos_col->get_data().assign(positions.begin(), positions.end()); |
537 | | |
538 | | // Build block |
539 | 109 | output_block.insert(ColumnWithTypeAndName(std::move(file_path_col), |
540 | 109 | std::make_shared<DataTypeString>(), "file_path")); |
541 | 109 | output_block.insert( |
542 | 109 | ColumnWithTypeAndName(std::move(pos_col), std::make_shared<DataTypeInt64>(), "pos")); |
543 | | |
544 | 109 | return Status::OK(); |
545 | 109 | } |
546 | | |
547 | 109 | std::string VIcebergDeleteSink::_get_file_extension() const { |
548 | 109 | std::string compress_name; |
549 | 109 | switch (_compress_type) { |
550 | 1 | case TFileCompressType::SNAPPYBLOCK: { |
551 | 1 | compress_name = ".snappy"; |
552 | 1 | break; |
553 | 0 | } |
554 | 52 | case TFileCompressType::ZLIB: { |
555 | 52 | compress_name = ".zlib"; |
556 | 52 | break; |
557 | 0 | } |
558 | 54 | case TFileCompressType::ZSTD: { |
559 | 54 | compress_name = ".zstd"; |
560 | 54 | break; |
561 | 0 | } |
562 | 2 | default: { |
563 | 2 | compress_name = ""; |
564 | 2 | break; |
565 | 0 | } |
566 | 109 | } |
567 | | |
568 | 109 | std::string file_format_name; |
569 | 109 | switch (_file_format_type) { |
570 | 57 | case TFileFormatType::FORMAT_PARQUET: { |
571 | 57 | file_format_name = ".parquet"; |
572 | 57 | break; |
573 | 0 | } |
574 | 52 | case TFileFormatType::FORMAT_ORC: { |
575 | 52 | file_format_name = ".orc"; |
576 | 52 | break; |
577 | 0 | } |
578 | 0 | default: { |
579 | 0 | file_format_name = ""; |
580 | 0 | break; |
581 | 0 | } |
582 | 109 | } |
583 | 109 | return fmt::format("{}{}", compress_name, file_format_name); |
584 | 109 | } |
585 | | |
586 | | Status VIcebergDeleteSink::_write_deletion_vector_files( |
587 | 187 | const std::map<std::string, IcebergFileDeletion>& file_deletions) { |
588 | 187 | std::vector<DeletionVectorBlob> blobs; |
589 | 190 | for (const auto& [data_file_path, deletion] : file_deletions) { |
590 | 190 | if (deletion.rows_to_delete.isEmpty()) { |
591 | 0 | continue; |
592 | 0 | } |
593 | 190 | roaring::Roaring64Map merged_rows = deletion.rows_to_delete; |
594 | 190 | DeletionVectorBlob blob; |
595 | 190 | blob.delete_count = static_cast<int64_t>(merged_rows.cardinality()); |
596 | 190 | auto previous_delete_it = _rewritable_delete_files.find(data_file_path); |
597 | 190 | if (previous_delete_it != _rewritable_delete_files.end()) { |
598 | 28 | roaring::Roaring64Map previous_rows; |
599 | 28 | RETURN_IF_ERROR(load_rewritable_delete_rows( |
600 | 28 | _state, _state->runtime_profile(), data_file_path, previous_delete_it->second, |
601 | 28 | _hadoop_conf, _file_type, _broker_addresses, &previous_rows)); |
602 | 28 | merged_rows |= previous_rows; |
603 | 28 | } |
604 | | |
605 | 190 | size_t bitmap_size = merged_rows.getSizeInBytes(); |
606 | 190 | blob.referenced_data_file = data_file_path; |
607 | 190 | blob.partition_spec_id = deletion.partition_spec_id; |
608 | 190 | blob.partition_data_json = deletion.partition_data_json; |
609 | 190 | blob.merged_count = static_cast<int64_t>(merged_rows.cardinality()); |
610 | 190 | RETURN_IF_ERROR(calculate_iceberg_deletion_vector_content_size( |
611 | 190 | bitmap_size, &blob.content_size_in_bytes)); |
612 | 190 | blob.blob_data.resize(static_cast<size_t>(blob.content_size_in_bytes)); |
613 | 190 | merged_rows.write(blob.blob_data.data() + 8); |
614 | | |
615 | 190 | uint32_t total_length = static_cast<uint32_t>(4 + bitmap_size); |
616 | 190 | BigEndian::Store32(blob.blob_data.data(), total_length); |
617 | | |
618 | 190 | constexpr char DV_MAGIC[] = {'\xD1', '\xD3', '\x39', '\x64'}; |
619 | 190 | memcpy(blob.blob_data.data() + 4, DV_MAGIC, 4); |
620 | | |
621 | 190 | uint32_t crc = static_cast<uint32_t>( |
622 | 190 | ::crc32(0, reinterpret_cast<const Bytef*>(blob.blob_data.data() + 4), |
623 | 190 | static_cast<uInt>(4 + bitmap_size))); |
624 | 190 | BigEndian::Store32(blob.blob_data.data() + 8 + bitmap_size, crc); |
625 | 190 | blobs.emplace_back(std::move(blob)); |
626 | 190 | } |
627 | | |
628 | 187 | if (blobs.empty()) { |
629 | 0 | return Status::OK(); |
630 | 0 | } |
631 | | |
632 | 187 | std::string puffin_path = _generate_puffin_file_path(); |
633 | 187 | int64_t puffin_file_size = 0; |
634 | 187 | RETURN_IF_ERROR(_write_puffin_file(puffin_path, &blobs, &puffin_file_size)); |
635 | | |
636 | 190 | for (const auto& blob : blobs) { |
637 | 190 | TIcebergCommitData commit_data; |
638 | 190 | commit_data.__set_file_path(puffin_path); |
639 | 190 | commit_data.__set_row_count(blob.merged_count); |
640 | 190 | commit_data.__set_affected_rows(blob.delete_count); |
641 | 190 | commit_data.__set_file_size(puffin_file_size); |
642 | 190 | commit_data.__set_file_content(TFileContent::DELETION_VECTOR); |
643 | 190 | commit_data.__set_content_offset(blob.content_offset); |
644 | 190 | commit_data.__set_content_size_in_bytes(blob.content_size_in_bytes); |
645 | 190 | commit_data.__set_referenced_data_file_path(blob.referenced_data_file); |
646 | 190 | if (blob.partition_spec_id != 0 || !blob.partition_data_json.empty()) { |
647 | 106 | commit_data.__set_partition_spec_id(blob.partition_spec_id); |
648 | 106 | commit_data.__set_partition_data_json(blob.partition_data_json); |
649 | 106 | } |
650 | | |
651 | 190 | _commit_data_list.push_back(commit_data); |
652 | 190 | _delete_file_count++; |
653 | 190 | } |
654 | 187 | return Status::OK(); |
655 | 187 | } |
656 | | |
657 | | Status VIcebergDeleteSink::_write_puffin_file(const std::string& puffin_path, |
658 | | std::vector<DeletionVectorBlob>* blobs, |
659 | 187 | int64_t* out_file_size) { |
660 | 187 | DCHECK(blobs != nullptr); |
661 | 187 | DCHECK(!blobs->empty()); |
662 | | |
663 | 187 | io::FSPropertiesRef fs_properties(_file_type); |
664 | 187 | fs_properties.properties = &_hadoop_conf; |
665 | 187 | if (!_broker_addresses.empty()) { |
666 | 0 | fs_properties.broker_addresses = &_broker_addresses; |
667 | 0 | } |
668 | 187 | io::FileDescription file_description = {.path = puffin_path, .fs_name {}}; |
669 | 187 | auto fs = DORIS_TRY(FileFactory::create_fs(fs_properties, file_description)); |
670 | 187 | io::FileWriterOptions file_writer_options = {.used_by_s3_committer = false}; |
671 | 187 | io::FileWriterPtr file_writer; |
672 | 187 | RETURN_IF_ERROR(fs->create_file(file_description.path, &file_writer, &file_writer_options)); |
673 | | |
674 | 187 | constexpr char PUFFIN_MAGIC[] = {'\x50', '\x46', '\x41', '\x31'}; |
675 | 187 | RETURN_IF_ERROR(file_writer->append(Slice(reinterpret_cast<const uint8_t*>(PUFFIN_MAGIC), 4))); |
676 | 187 | int64_t current_offset = 4; |
677 | 190 | for (auto& blob : *blobs) { |
678 | 190 | blob.content_offset = current_offset; |
679 | 190 | RETURN_IF_ERROR(file_writer->append(Slice( |
680 | 190 | reinterpret_cast<const uint8_t*>(blob.blob_data.data()), blob.blob_data.size()))); |
681 | 190 | current_offset += static_cast<int64_t>(blob.blob_data.size()); |
682 | 190 | } |
683 | 187 | RETURN_IF_ERROR(file_writer->append(Slice(reinterpret_cast<const uint8_t*>(PUFFIN_MAGIC), 4))); |
684 | | |
685 | 187 | std::string footer_json = _build_puffin_footer_json(*blobs); |
686 | 187 | RETURN_IF_ERROR(file_writer->append( |
687 | 187 | Slice(reinterpret_cast<const uint8_t*>(footer_json.data()), footer_json.size()))); |
688 | | |
689 | 187 | char footer_size_buf[4]; |
690 | 187 | LittleEndian::Store32(footer_size_buf, static_cast<uint32_t>(footer_json.size())); |
691 | 187 | RETURN_IF_ERROR(file_writer->append( |
692 | 187 | Slice(reinterpret_cast<const uint8_t*>(footer_size_buf), sizeof(footer_size_buf)))); |
693 | | |
694 | 187 | char flags[4] = {0, 0, 0, 0}; |
695 | 187 | RETURN_IF_ERROR( |
696 | 187 | file_writer->append(Slice(reinterpret_cast<const uint8_t*>(flags), sizeof(flags)))); |
697 | 187 | RETURN_IF_ERROR(file_writer->append(Slice(reinterpret_cast<const uint8_t*>(PUFFIN_MAGIC), 4))); |
698 | 187 | RETURN_IF_ERROR(file_writer->close()); |
699 | | |
700 | 187 | *out_file_size = current_offset + 4 + static_cast<int64_t>(footer_json.size()) + 4 + 4 + 4; |
701 | 187 | return Status::OK(); |
702 | 187 | } |
703 | | |
704 | | std::string VIcebergDeleteSink::_build_puffin_footer_json( |
705 | 187 | const std::vector<DeletionVectorBlob>& blobs) { |
706 | 187 | rapidjson::StringBuffer buffer; |
707 | 187 | rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); |
708 | 187 | writer.StartObject(); |
709 | 187 | writer.Key("blobs"); |
710 | 187 | writer.StartArray(); |
711 | 190 | for (const auto& blob : blobs) { |
712 | 190 | writer.StartObject(); |
713 | 190 | writer.Key("type"); |
714 | 190 | writer.String("deletion-vector-v1"); |
715 | 190 | writer.Key("fields"); |
716 | 190 | writer.StartArray(); |
717 | 190 | writer.EndArray(); |
718 | 190 | writer.Key("snapshot-id"); |
719 | 190 | writer.Int64(-1); |
720 | 190 | writer.Key("sequence-number"); |
721 | 190 | writer.Int64(-1); |
722 | 190 | writer.Key("offset"); |
723 | 190 | writer.Int64(blob.content_offset); |
724 | 190 | writer.Key("length"); |
725 | 190 | writer.Int64(blob.content_size_in_bytes); |
726 | 190 | writer.Key("properties"); |
727 | 190 | writer.StartObject(); |
728 | 190 | writer.Key("referenced-data-file"); |
729 | 190 | writer.String(blob.referenced_data_file.c_str(), |
730 | 190 | static_cast<rapidjson::SizeType>(blob.referenced_data_file.size())); |
731 | 190 | std::string cardinality = std::to_string(blob.merged_count); |
732 | 190 | writer.Key("cardinality"); |
733 | 190 | writer.String(cardinality.c_str(), static_cast<rapidjson::SizeType>(cardinality.size())); |
734 | 190 | writer.EndObject(); |
735 | 190 | writer.EndObject(); |
736 | 190 | } |
737 | 187 | writer.EndArray(); |
738 | 187 | writer.Key("properties"); |
739 | 187 | writer.StartObject(); |
740 | 187 | writer.Key("created-by"); |
741 | 187 | writer.String("doris-puffin-v1"); |
742 | 187 | writer.EndObject(); |
743 | 187 | writer.EndObject(); |
744 | 187 | return {buffer.GetString(), buffer.GetSize()}; |
745 | 187 | } |
746 | | |
747 | | std::string VIcebergDeleteSink::_generate_delete_file_path( |
748 | 109 | const std::string& referenced_data_file) { |
749 | | // Generate unique delete file name using UUID |
750 | 109 | std::string uuid = generate_uuid_string(); |
751 | 109 | std::string file_name; |
752 | | |
753 | 109 | std::string file_extension = _get_file_extension(); |
754 | 109 | file_name = |
755 | 109 | fmt::format("delete_pos_{}_{}{}", uuid, |
756 | 109 | std::hash<std::string> {}(referenced_data_file) % 10000000, file_extension); |
757 | | |
758 | | // Combine with output path or table location |
759 | 109 | std::string base_path = _output_path.empty() ? _table_location : _output_path; |
760 | | |
761 | | // Ensure base path ends with / |
762 | 109 | if (!base_path.empty() && base_path.back() != '/') { |
763 | 109 | base_path += '/'; |
764 | 109 | } |
765 | | |
766 | | // Delete files are data files in Iceberg, write under data location |
767 | 109 | return fmt::format("{}{}", base_path, file_name); |
768 | 109 | } |
769 | | |
770 | 187 | std::string VIcebergDeleteSink::_generate_puffin_file_path() { |
771 | 187 | std::string uuid = generate_uuid_string(); |
772 | 187 | std::string file_name = fmt::format("delete_dv_{}.puffin", uuid); |
773 | 187 | std::string base_path = _output_path.empty() ? _table_location : _output_path; |
774 | 187 | if (!base_path.empty() && base_path.back() != '/') { |
775 | 187 | base_path += '/'; |
776 | 187 | } |
777 | 187 | return fmt::format("{}{}", base_path, file_name); |
778 | 187 | } |
779 | | |
780 | | } // namespace doris |