|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/update/delete_files.h" |
| 21 | + |
| 22 | +#include <memory> |
| 23 | +#include <string> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +#include <gmock/gmock.h> |
| 27 | +#include <gtest/gtest.h> |
| 28 | + |
| 29 | +#include "iceberg/avro/avro_register.h" |
| 30 | +#include "iceberg/expression/expressions.h" |
| 31 | +#include "iceberg/expression/literal.h" |
| 32 | +#include "iceberg/manifest/manifest_entry.h" |
| 33 | +#include "iceberg/partition_spec.h" |
| 34 | +#include "iceberg/row/partition_values.h" |
| 35 | +#include "iceberg/schema.h" |
| 36 | +#include "iceberg/snapshot.h" |
| 37 | +#include "iceberg/table.h" |
| 38 | +#include "iceberg/test/matchers.h" |
| 39 | +#include "iceberg/test/update_test_base.h" |
| 40 | +#include "iceberg/update/fast_append.h" |
| 41 | + |
| 42 | +namespace iceberg { |
| 43 | + |
| 44 | +class DeleteFilesTest : public MinimalUpdateTestBase { |
| 45 | + protected: |
| 46 | + static void SetUpTestSuite() { avro::RegisterAll(); } |
| 47 | + |
| 48 | + void SetUp() override { |
| 49 | + MinimalUpdateTestBase::SetUp(); |
| 50 | + |
| 51 | + ICEBERG_UNWRAP_OR_FAIL(spec_, table_->spec()); |
| 52 | + ICEBERG_UNWRAP_OR_FAIL(schema_, table_->schema()); |
| 53 | + file_a_ = MakeDataFile("/data/file_a.parquet", /*partition_x=*/1L); |
| 54 | + file_b_ = MakeDataFile("/data/file_b.parquet", /*partition_x=*/2L); |
| 55 | + } |
| 56 | + |
| 57 | + std::shared_ptr<DataFile> MakeDataFile(const std::string& path, int64_t partition_x) { |
| 58 | + auto file = std::make_shared<DataFile>(); |
| 59 | + file->content = DataFile::Content::kData; |
| 60 | + file->file_path = table_location_ + path; |
| 61 | + file->file_format = FileFormatType::kParquet; |
| 62 | + file->partition = PartitionValues(std::vector<Literal>{Literal::Long(partition_x)}); |
| 63 | + file->file_size_in_bytes = 1024; |
| 64 | + file->record_count = 100; |
| 65 | + file->partition_spec_id = spec_->spec_id(); |
| 66 | + return file; |
| 67 | + } |
| 68 | + |
| 69 | + void SetLongBounds(const std::shared_ptr<DataFile>& file, int32_t field_id, |
| 70 | + int64_t lower, int64_t upper) { |
| 71 | + ASSERT_NE(file, nullptr); |
| 72 | + ICEBERG_UNWRAP_OR_FAIL(auto lower_bound, Literal::Long(lower).Serialize()); |
| 73 | + ICEBERG_UNWRAP_OR_FAIL(auto upper_bound, Literal::Long(upper).Serialize()); |
| 74 | + file->value_counts[field_id] = file->record_count; |
| 75 | + file->null_value_counts[field_id] = 0; |
| 76 | + file->lower_bounds[field_id] = lower_bound; |
| 77 | + file->upper_bounds[field_id] = upper_bound; |
| 78 | + } |
| 79 | + |
| 80 | + void CommitFiles(const std::vector<std::shared_ptr<DataFile>>& files) { |
| 81 | + ICEBERG_UNWRAP_OR_FAIL(auto append, table_->NewFastAppend()); |
| 82 | + for (const auto& file : files) { |
| 83 | + append->AppendFile(file); |
| 84 | + } |
| 85 | + ASSERT_THAT(append->Commit(), IsOk()); |
| 86 | + ASSERT_THAT(table_->Refresh(), IsOk()); |
| 87 | + } |
| 88 | + |
| 89 | + void CommitInitialFiles() { CommitFiles({file_a_, file_b_}); } |
| 90 | + |
| 91 | + void ExpectOneFileDeleted() { |
| 92 | + ASSERT_THAT(table_->Refresh(), IsOk()); |
| 93 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot()); |
| 94 | + EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kOperation), |
| 95 | + DataOperation::kDelete); |
| 96 | + EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kDeletedDataFiles), "1"); |
| 97 | + EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kDeletedRecords), "100"); |
| 98 | + EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kRemovedFileSize), "1024"); |
| 99 | + } |
| 100 | + |
| 101 | + std::shared_ptr<PartitionSpec> spec_; |
| 102 | + std::shared_ptr<Schema> schema_; |
| 103 | + std::shared_ptr<DataFile> file_a_; |
| 104 | + std::shared_ptr<DataFile> file_b_; |
| 105 | + |
| 106 | + static constexpr int32_t kYFieldId = 2; |
| 107 | +}; |
| 108 | + |
| 109 | +TEST_F(DeleteFilesTest, DeleteFileByPath) { |
| 110 | + CommitInitialFiles(); |
| 111 | + |
| 112 | + ICEBERG_UNWRAP_OR_FAIL(auto delete_files, table_->NewDeleteFiles()); |
| 113 | + delete_files->DeleteFile(file_a_->file_path); |
| 114 | + |
| 115 | + EXPECT_THAT(delete_files->Commit(), IsOk()); |
| 116 | + ExpectOneFileDeleted(); |
| 117 | +} |
| 118 | + |
| 119 | +TEST_F(DeleteFilesTest, DeleteFileByDataFile) { |
| 120 | + CommitInitialFiles(); |
| 121 | + |
| 122 | + ICEBERG_UNWRAP_OR_FAIL(auto delete_files, table_->NewDeleteFiles()); |
| 123 | + delete_files->DeleteFile(file_a_); |
| 124 | + |
| 125 | + EXPECT_THAT(delete_files->Commit(), IsOk()); |
| 126 | + ExpectOneFileDeleted(); |
| 127 | +} |
| 128 | + |
| 129 | +TEST_F(DeleteFilesTest, DeleteFromRowFilterCaseInsensitive) { |
| 130 | + CommitInitialFiles(); |
| 131 | + |
| 132 | + ICEBERG_UNWRAP_OR_FAIL(auto delete_files, table_->NewDeleteFiles()); |
| 133 | + delete_files->CaseSensitive(false).DeleteFromRowFilter( |
| 134 | + Expressions::Equal("X", Literal::Long(1L))); |
| 135 | + |
| 136 | + EXPECT_THAT(delete_files->Commit(), IsOk()); |
| 137 | + ExpectOneFileDeleted(); |
| 138 | +} |
| 139 | + |
| 140 | +TEST_F(DeleteFilesTest, EmptyDeleteCommit) { |
| 141 | + CommitInitialFiles(); |
| 142 | + ICEBERG_UNWRAP_OR_FAIL(auto previous_snapshot, table_->current_snapshot()); |
| 143 | + |
| 144 | + ICEBERG_UNWRAP_OR_FAIL(auto delete_files, table_->NewDeleteFiles()); |
| 145 | + |
| 146 | + EXPECT_THAT(delete_files->Commit(), IsOk()); |
| 147 | + |
| 148 | + ASSERT_THAT(table_->Refresh(), IsOk()); |
| 149 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot()); |
| 150 | + ASSERT_TRUE(snapshot->parent_snapshot_id.has_value()); |
| 151 | + EXPECT_EQ(snapshot->parent_snapshot_id.value(), previous_snapshot->snapshot_id); |
| 152 | + EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kOperation), |
| 153 | + DataOperation::kDelete); |
| 154 | + EXPECT_EQ(snapshot->summary.count(SnapshotSummaryFields::kDeletedDataFiles), 0U); |
| 155 | + EXPECT_EQ(snapshot->summary.count(SnapshotSummaryFields::kDeletedRecords), 0U); |
| 156 | + EXPECT_EQ(snapshot->summary.count(SnapshotSummaryFields::kRemovedFileSize), 0U); |
| 157 | +} |
| 158 | + |
| 159 | +TEST_F(DeleteFilesTest, DeleteFromRowFilter) { |
| 160 | + CommitInitialFiles(); |
| 161 | + |
| 162 | + ICEBERG_UNWRAP_OR_FAIL(auto delete_files, table_->NewDeleteFiles()); |
| 163 | + delete_files->DeleteFromRowFilter(Expressions::Equal("x", Literal::Long(1L))); |
| 164 | + |
| 165 | + EXPECT_THAT(delete_files->Commit(), IsOk()); |
| 166 | + ExpectOneFileDeleted(); |
| 167 | +} |
| 168 | + |
| 169 | +TEST_F(DeleteFilesTest, DeleteFromRowFilterRejectsPartialMatchFile) { |
| 170 | + auto partial_match_file = MakeDataFile("/data/partial_match.parquet", |
| 171 | + /*partition_x=*/1L); |
| 172 | + SetLongBounds(partial_match_file, kYFieldId, /*lower=*/0L, /*upper=*/10L); |
| 173 | + CommitFiles({partial_match_file}); |
| 174 | + ICEBERG_UNWRAP_OR_FAIL(auto previous_snapshot, table_->current_snapshot()); |
| 175 | + |
| 176 | + ICEBERG_UNWRAP_OR_FAIL(auto delete_files, table_->NewDeleteFiles()); |
| 177 | + delete_files->DeleteFromRowFilter(Expressions::Equal("y", Literal::Long(5L))); |
| 178 | + |
| 179 | + auto status = delete_files->Commit(); |
| 180 | + EXPECT_THAT(status, IsError(ErrorKind::kValidationFailed)); |
| 181 | + EXPECT_THAT(status, |
| 182 | + HasErrorMessage("Cannot delete file where some, but not all, rows match " |
| 183 | + "filter")); |
| 184 | + EXPECT_THAT(status, HasErrorMessage(partial_match_file->file_path)); |
| 185 | + |
| 186 | + ASSERT_THAT(table_->Refresh(), IsOk()); |
| 187 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot()); |
| 188 | + EXPECT_EQ(snapshot->snapshot_id, previous_snapshot->snapshot_id); |
| 189 | +} |
| 190 | + |
| 191 | +TEST_F(DeleteFilesTest, ValidateFilesExistRejectsMissingPath) { |
| 192 | + CommitInitialFiles(); |
| 193 | + |
| 194 | + ICEBERG_UNWRAP_OR_FAIL(auto delete_files, table_->NewDeleteFiles()); |
| 195 | + delete_files->DeleteFile(table_location_ + "/data/missing.parquet") |
| 196 | + .ValidateFilesExist(); |
| 197 | + |
| 198 | + auto status = delete_files->Commit(); |
| 199 | + EXPECT_THAT(status, IsError(ErrorKind::kValidationFailed)); |
| 200 | + EXPECT_THAT(status, HasErrorMessage("Missing required files to delete")); |
| 201 | +} |
| 202 | + |
| 203 | +} // namespace iceberg |
0 commit comments