Skip to content
Draft
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
4 changes: 4 additions & 0 deletions src/iceberg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,16 @@ if(MSVC_TOOLCHAIN)
endif()

set(ICEBERG_DATA_SOURCES
action/delete_rows.cc
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 +309,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)
161 changes: 161 additions & 0 deletions src/iceberg/action/delete_rows.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* 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 "iceberg/action/delete_rows.h"

#include <cstdint>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#include <nanoarrow/nanoarrow.hpp>
#include <nlohmann/json.hpp>

#include "iceberg/action/position_delete_action_internal.h"
#include "iceberg/data/file_scan_task_reader.h"
#include "iceberg/data_table_scan.h"
#include "iceberg/expression/binder.h"
#include "iceberg/json_serde_internal.h"
#include "iceberg/metadata_columns.h"
#include "iceberg/name_mapping.h"
#include "iceberg/schema.h"
#include "iceberg/table.h"
#include "iceberg/table_metadata.h"
#include "iceberg/table_properties.h"
#include "iceberg/update/row_delta.h"

namespace iceberg {
namespace {

// Record the original references before binding can fold a predicate to a constant.
// The task reader needs these columns when it binds residuals to the read schema.
class FilterColumns : public Binder {
public:
explicit FilterColumns(const Schema& schema)
: Binder(schema, /*case_sensitive=*/true) {}
using Binder::Predicate;
Result<std::shared_ptr<Expression>> Predicate(
const std::shared_ptr<UnboundPredicate>& pred) override {
names.emplace_back(pred->reference()->name());
return Binder::Predicate(pred);
}
std::vector<std::string> names;
};

Result<std::vector<int64_t>> MatchingPositions(FileScanTaskReader& reader,
const FileScanTask& task) {
ICEBERG_ASSIGN_OR_RAISE(auto opened, reader.Open(task));
nanoarrow::UniqueArrayStream stream(&opened);
std::vector<int64_t> positions;
while (true) {
nanoarrow::UniqueArray batch;
ArrowError error{};
if (ArrowArrayStreamGetNext(stream.get(), batch.get(), &error) != NANOARROW_OK) {
return IOError("Reading rows for deletion: {}", error.message);
}
if (!batch->release) break;
if (batch->length == 0) continue;
// _pos is the first projected field, a required int64 absolute file position.
const auto& column = *batch->children[0];
const auto* begin =
static_cast<const int64_t*>(column.buffers[1]) + column.offset + batch->offset;
positions.insert(positions.end(), begin, begin + batch->length);
}
return positions;
}

Result<std::vector<internal::FilePositionDeletes>> CollectDeletes(
FileScanTaskReader& reader, FileScanTaskStream& tasks) {
std::vector<internal::FilePositionDeletes> groups;
std::unordered_map<std::string, size_t> by_file;
while (true) {
ICEBERG_ASSIGN_OR_RAISE(auto task, tasks.Next());
if (!task) break;
ICEBERG_ASSIGN_OR_RAISE(auto positions, MatchingPositions(reader, **task));
if (positions.empty()) continue;
auto [it, inserted] =
by_file.try_emplace((*task)->data_file()->file_path, groups.size());
if (inserted) {
groups.push_back({std::move(*task), std::move(positions)});
} else {
auto& group = groups[it->second].positions;
group.insert(group.end(), positions.begin(), positions.end());
}
}
return groups;
}

} // namespace

Status MergeOnReadDelete(Table& table, std::shared_ptr<Expression> filter) {
ICEBERG_PRECHECK(filter != nullptr, "Delete filter cannot be null");
auto metadata = table.metadata();
ICEBERG_PRECHECK(metadata->format_version >= 2,
"Merge-on-read deletes require v2 or later");
auto isolation = table.properties().Get(TableProperties::kDeleteIsolationLevel);
ICEBERG_PRECHECK(isolation == "serializable" || isolation == "snapshot",
"Unsupported delete isolation level: {}", isolation);
ICEBERG_ASSIGN_OR_RAISE(auto schema, metadata->Schema());
FilterColumns columns(*schema);
ICEBERG_RETURN_UNEXPECTED(
(Visit<std::shared_ptr<Expression>, Binder>(filter, columns)));
const auto snapshot_id = metadata->current_snapshot_id;
if (snapshot_id == kInvalidSnapshotId) return {};

ICEBERG_ASSIGN_OR_RAISE(auto selected, schema->Select(columns.names));
std::vector<SchemaField> fields{MetadataColumns::kRowPosition};
fields.insert(fields.end(), selected->fields().begin(), selected->fields().end());
ICEBERG_ASSIGN_OR_RAISE(auto builder, table.NewScan());
builder->UseSnapshot(snapshot_id, /*use_snapshot_schema=*/false)
.Filter(filter)
.Project(std::move(selected));
ICEBERG_ASSIGN_OR_RAISE(auto scan, builder->Build());
ICEBERG_ASSIGN_OR_RAISE(auto tasks, scan->PlanFilesStream());

const auto& properties = table.properties().configs();
std::shared_ptr<NameMapping> mapping;
auto mapping_it = properties.find(std::string(TableProperties::kDefaultNameMapping));
if (mapping_it != properties.end()) {
ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(mapping_it->second));
ICEBERG_ASSIGN_OR_RAISE(mapping, NameMappingFromJson(json));
}
ICEBERG_ASSIGN_OR_RAISE(
auto reader, FileScanTaskReader::Make({
.io = scan->io(),
.table_schema = schema,
.schemas = metadata->schemas,
.projected_schema = std::make_shared<Schema>(std::move(fields)),
.name_mapping = std::move(mapping),
.properties = properties,
.apply_residuals = true,
}));
ICEBERG_ASSIGN_OR_RAISE(auto groups, CollectDeletes(*reader, *tasks));
if (groups.empty()) return {};

ICEBERG_ASSIGN_OR_RAISE(auto delta, table.NewRowDelta());
delta->ConflictDetectionFilter(std::move(filter));
if (isolation == "serializable") delta->ValidateNoConflictingDataFiles();

return internal::CommitPositionDeletes(table, *delta, snapshot_id, scan->io(),
std::move(groups));
}

} // namespace iceberg
36 changes: 36 additions & 0 deletions src/iceberg/action/delete_rows.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 <memory>

#include "iceberg/iceberg_data_export.h"
#include "iceberg/result.h"
#include "iceberg/type_fwd.h"

namespace iceberg {

/// Delete live rows matching an unbound, case-sensitive predicate on the current
/// snapshot. Uses merge-on-read on v2 and v3 tables and honors
/// write.delete.isolation-level. An empty match succeeds without creating a snapshot.
ICEBERG_DATA_EXPORT Status MergeOnReadDelete(Table& table,
std::shared_ptr<Expression> filter);

} // namespace iceberg
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
1 change: 1 addition & 0 deletions src/iceberg/cpp20_compatibility_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#pragma once

#include "iceberg/action/delete_rows.h"
#include "iceberg/arrow_c_data.h"
#include "iceberg/catalog.h"
#include "iceberg/catalog/catalog_util.h"
Expand Down
Loading