Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/iceberg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,15 @@ if(MSVC_TOOLCHAIN)
endif()

set(ICEBERG_DATA_SOURCES
action/position_delete_action.cc
data/arrow_batch_sizer.cc
data/data_writer.cc
data/delete_filter.cc
data/delete_loader.cc
data/equality_delete_writer.cc
data/file_scan_task_reader.cc
data/parallel_scan_reader.cc
data/position_deletes.cc
data/position_delete_writer.cc
data/writer.cc)

Expand Down Expand Up @@ -306,6 +308,7 @@ iceberg_install_all_headers(iceberg)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/version.h"
DESTINATION "${ICEBERG_INSTALL_INCLUDEDIR}/iceberg")

add_subdirectory(action)
add_subdirectory(catalog)
add_subdirectory(compat)
add_subdirectory(data)
Expand Down
18 changes: 18 additions & 0 deletions src/iceberg/action/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

iceberg_install_all_headers(iceberg/action)
55 changes: 55 additions & 0 deletions src/iceberg/action/position_delete_action.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include <set>
#include <string>
#include <utility>

#include "iceberg/action/position_delete_action_internal.h"
#include "iceberg/data/output_file_cleanup_internal.h"
#include "iceberg/table.h"
#include "iceberg/update/row_delta.h"

namespace iceberg::internal {

Status CommitPositionDeletes(const Table& table, RowDelta& delta,
int64_t scan_snapshot_id,
const std::shared_ptr<FileIO>& scan_io,
std::vector<FilePositionDeletes> files) {
std::set<std::string> outputs;
auto written = WritePositionDeletes(table, scan_io, std::move(files), outputs);
if (!written) {
return FailWithOutputCleanup(std::move(written.error()), *table.io(), outputs);
}
if (written->data_files.empty()) return {};

delta.ValidateFromSnapshot(scan_snapshot_id)
.ValidateDataFilesExist(written->referenced_data_files)
.ValidateDeletedFiles();
for (const auto& file : written->data_files) delta.AddDeletes(file);
for (const auto& file : written->rewritten_delete_files) delta.RemoveDeletes(file);

auto status = delta.Commit();
if (!status && status.error().kind != ErrorKind::kCommitStateUnknown) {
return FailWithOutputCleanup(std::move(status.error()), *table.io(), outputs);
}
return status;
}

} // namespace iceberg::internal
37 changes: 37 additions & 0 deletions src/iceberg/action/position_delete_action_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#pragma once

#include <cstdint>
#include <memory>
#include <vector>

#include "iceberg/data/position_deletes_internal.h"

namespace iceberg::internal {

// Write positions from the original scan and commit them through a fresh RowDelta.
// The caller may configure conflict detection on the delta before invoking the action.
ICEBERG_DATA_EXPORT Status CommitPositionDeletes(const Table& table, RowDelta& delta,
int64_t scan_snapshot_id,
const std::shared_ptr<FileIO>& scan_io,
std::vector<FilePositionDeletes> files);

} // namespace iceberg::internal
8 changes: 7 additions & 1 deletion src/iceberg/arrow/arrow_io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#include <algorithm>
#include <cerrno>
#include <chrono>
#include <limits>
#include <mutex>
Expand All @@ -30,6 +31,7 @@
#include <arrow/io/interfaces.h>
#include <arrow/result.h>
#include <arrow/status.h>
#include <arrow/util/io_util.h>
#include <arrow/util/uri.h>

#include "iceberg/arrow/arrow_io_internal.h"
Expand Down Expand Up @@ -591,7 +593,11 @@ Result<std::unique_ptr<OutputFile>> ArrowFileSystemFileIO::NewOutputFile(
/// \brief Delete a file at the given location.
Status ArrowFileSystemFileIO::DeleteFile(const std::string& file_location) {
ICEBERG_ASSIGN_OR_RAISE(auto path, ResolvePath(file_location));
ICEBERG_ARROW_RETURN_NOT_OK(arrow_fs_->DeleteFile(path));
auto status = arrow_fs_->DeleteFile(path);
if (::arrow::internal::ErrnoFromStatus(status) == ENOENT) {
return {};
}
ICEBERG_ARROW_RETURN_NOT_OK(status);
return {};
}

Expand Down
56 changes: 56 additions & 0 deletions src/iceberg/data/output_file_cleanup_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#pragma once

#include <set>
#include <string>
#include <utility>

#include "iceberg/file_io.h"
#include "iceberg/result.h"

namespace iceberg::internal {

// Try every owned output, keeping failed paths for a subsequent cleanup attempt.
inline Status CleanupOutputFiles(FileIO& io, std::set<std::string>& paths) {
Status first_error;
for (auto it = paths.begin(); it != paths.end();) {
auto status = io.DeleteFile(*it);
if (status.has_value()) {
it = paths.erase(it);
} else {
if (first_error.has_value()) first_error = std::move(status);
++it;
}
}
return first_error;
}

// Preserve the operation's error kind while reporting a cleanup failure, if any.
inline Status FailWithOutputCleanup(Error error, FileIO& io,
std::set<std::string>& paths) {
auto cleanup = CleanupOutputFiles(io, paths);
if (!cleanup.has_value()) {
error.message += "; output cleanup failed: " + cleanup.error().message;
}
return iceberg::unexpected(std::move(error));
}

} // namespace iceberg::internal
170 changes: 170 additions & 0 deletions src/iceberg/data/position_deletes.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include <algorithm>
#include <format>
#include <iterator>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>

#include "iceberg/data/delete_loader.h"
#include "iceberg/data/position_delete_writer.h"
#include "iceberg/data/position_deletes_internal.h"
#include "iceberg/deletes/dv_writer.h"
#include "iceberg/file_format.h"
#include "iceberg/file_io.h"
#include "iceberg/location_provider.h"
#include "iceberg/manifest/manifest_entry.h"
#include "iceberg/partition_spec.h"
#include "iceberg/schema.h"
#include "iceberg/table.h"
#include "iceberg/table_metadata.h"
#include "iceberg/table_properties.h"
#include "iceberg/table_scan.h"
#include "iceberg/util/macros.h"
#include "iceberg/util/uuid.h"

namespace iceberg::internal {
namespace {

Result<DeleteWriteResult> WriteParquetDeletes(const Table& table,
std::vector<FilePositionDeletes>& files,
std::set<std::string>& output_paths) {
ICEBERG_ASSIGN_OR_RAISE(auto location_provider, table.location_provider());
ICEBERG_ASSIGN_OR_RAISE(auto schema, table.schema());

DeleteWriteResult result;
result.data_files.reserve(files.size());
result.referenced_data_files.reserve(files.size());
auto properties = table.properties().configs();
properties[TableProperties::kParquetCompression.key()] =
table.properties().Get(TableProperties::kDeleteParquetCompression);
properties[TableProperties::kParquetCompressionLevel.key()] =
table.properties().Get(TableProperties::kDeleteParquetCompressionLevel);
const auto write_uuid = Uuid::GenerateV7().ToString();
size_t file_number = 0;

for (auto& [task, positions] : files) {
const auto& data_file = *task->data_file();
const auto& path = data_file.file_path;
std::ranges::sort(positions);

ICEBERG_ASSIGN_OR_RAISE(auto spec, table.metadata()->PartitionSpecById(
data_file.partition_spec_id.value()));
const auto filename =
std::format("position-deletes-{}-{}.parquet", write_uuid, file_number++);
auto output_path = location_provider->NewDataLocation(filename);
output_paths.insert(output_path);

ICEBERG_ASSIGN_OR_RAISE(auto writer,
PositionDeleteWriter::Make(PositionDeleteWriterOptions{
.path = output_path,
.schema = schema,
.spec = spec,
.partition = data_file.partition,
.format = FileFormatType::kParquet,
.io = table.io(),
.properties = properties,
}));
for (int64_t pos : positions) {
ICEBERG_RETURN_UNEXPECTED(writer->WriteDelete(path, pos));
}
ICEBERG_RETURN_UNEXPECTED(writer->Close());
ICEBERG_ASSIGN_OR_RAISE(auto metadata, writer->Metadata());
result.data_files.insert(result.data_files.end(),
std::make_move_iterator(metadata.data_files.begin()),
std::make_move_iterator(metadata.data_files.end()));
result.referenced_data_files.push_back(path);
}
return result;
}

Result<DeleteWriteResult> WriteDeletionVectors(
const Table& table, const std::shared_ptr<FileIO>& scan_io,
const std::vector<FilePositionDeletes>& files, std::set<std::string>& output_paths) {
ICEBERG_ASSIGN_OR_RAISE(auto locations, table.location_provider());
auto path = locations->NewDataLocation(
std::format("position-deletes-{}.puffin", Uuid::GenerateV7().ToString()));
output_paths.insert(path);
std::unordered_map<std::string_view, std::vector<std::shared_ptr<DataFile>>> previous;
for (const auto& file : files) {
for (const auto& entry : file.task->delete_files()) {
if (entry->content == DataFile::Content::kPositionDeletes) {
previous[file.task->data_file()->file_path].push_back(entry);
}
}
}
DeleteLoader loader(scan_io);
ICEBERG_ASSIGN_OR_RAISE(
auto writer,
DVWriter::Make({.path = path,
.io = table.io(),
.load_previous_deletes = [&](std::string_view target)
-> Result<std::optional<PositionDeleteIndex>> {
auto it = previous.find(target);
if (it == previous.end()) return std::nullopt;
ICEBERG_ASSIGN_OR_RAISE(
auto index, loader.LoadPositionDeletes(it->second, target));
return std::optional<PositionDeleteIndex>(std::move(index));
}}));
for (const auto& [task, positions] : files) {
const auto& file = *task->data_file();
ICEBERG_ASSIGN_OR_RAISE(
auto spec, table.metadata()->PartitionSpecById(file.partition_spec_id.value()));
for (int64_t pos : positions) {
ICEBERG_RETURN_UNEXPECTED(
writer->Delete(file.file_path, pos, spec, file.partition));
}
}
ICEBERG_RETURN_UNEXPECTED(writer->Close());
return writer->Metadata();
}

} // namespace

Result<DeleteWriteResult> WritePositionDeletes(const Table& table,
const std::shared_ptr<FileIO>& scan_io,
std::vector<FilePositionDeletes> files,
std::set<std::string>& output_paths) {
const auto version = table.metadata()->format_version;
ICEBERG_PRECHECK(version >= 2,
"Position deletes require table format version 2 or later");
std::erase_if(files, [](const auto& file) { return file.positions.empty(); });
if (files.empty()) return DeleteWriteResult{};
for (const auto& [task, positions] : files) {
const auto& file = *task->data_file();
ICEBERG_PRECHECK(file.partition_spec_id.has_value(),
"Missing partition spec ID for {}", file.file_path);
for (int64_t pos : positions) {
ICEBERG_PRECHECK(pos >= 0 && pos < file.record_count,
"Invalid position {} for {} ({} rows)", pos, file.file_path,
file.record_count);
}
}
return version >= 3 ? WriteDeletionVectors(table, scan_io, files, output_paths)
: WriteParquetDeletes(table, files, output_paths);
}

} // namespace iceberg::internal
Loading
Loading