|
| 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/row_delta.h" |
| 21 | + |
| 22 | +#include <memory> |
| 23 | +#include <optional> |
| 24 | +#include <string> |
| 25 | +#include <vector> |
| 26 | + |
| 27 | +#include <gmock/gmock.h> |
| 28 | +#include <gtest/gtest.h> |
| 29 | + |
| 30 | +#include "iceberg/avro/avro_register.h" |
| 31 | +#include "iceberg/manifest/manifest_entry.h" |
| 32 | +#include "iceberg/partition_spec.h" |
| 33 | +#include "iceberg/row/partition_values.h" |
| 34 | +#include "iceberg/schema.h" |
| 35 | +#include "iceberg/snapshot.h" |
| 36 | +#include "iceberg/table.h" |
| 37 | +#include "iceberg/table_metadata.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 RowDeltaTest : 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 | + |
| 54 | + file_a_ = MakeDataFile("/data/file_a.parquet", /*partition_x=*/1L); |
| 55 | + file_b_ = MakeDataFile("/data/file_b.parquet", /*partition_x=*/2L); |
| 56 | + } |
| 57 | + |
| 58 | + std::shared_ptr<DataFile> MakeDataFile(const std::string& path, int64_t partition_x) { |
| 59 | + auto file = std::make_shared<DataFile>(); |
| 60 | + file->content = DataFile::Content::kData; |
| 61 | + file->file_path = table_location_ + path; |
| 62 | + file->file_format = FileFormatType::kParquet; |
| 63 | + file->partition = PartitionValues(std::vector<Literal>{Literal::Long(partition_x)}); |
| 64 | + file->file_size_in_bytes = 1024; |
| 65 | + file->record_count = 100; |
| 66 | + file->partition_spec_id = spec_->spec_id(); |
| 67 | + return file; |
| 68 | + } |
| 69 | + |
| 70 | + std::shared_ptr<DataFile> MakeDeleteFile(const std::string& path, int64_t partition_x) { |
| 71 | + auto file = MakeDataFile(path, partition_x); |
| 72 | + file->content = DataFile::Content::kPositionDeletes; |
| 73 | + file->file_size_in_bytes = 256; |
| 74 | + file->record_count = 7; |
| 75 | + return file; |
| 76 | + } |
| 77 | + |
| 78 | + std::shared_ptr<DataFile> MakeDeletionVector(const std::string& path, |
| 79 | + const std::string& referenced_data_file, |
| 80 | + int64_t partition_x, |
| 81 | + int64_t content_offset = 0) { |
| 82 | + auto file = MakeDeleteFile(path, partition_x); |
| 83 | + file->file_format = FileFormatType::kPuffin; |
| 84 | + file->referenced_data_file = referenced_data_file; |
| 85 | + file->content_offset = content_offset; |
| 86 | + file->content_size_in_bytes = 10; |
| 87 | + return file; |
| 88 | + } |
| 89 | + |
| 90 | + void CommitFileA() { |
| 91 | + ICEBERG_UNWRAP_OR_FAIL(auto fast_append, table_->NewFastAppend()); |
| 92 | + fast_append->AppendFile(file_a_); |
| 93 | + EXPECT_THAT(fast_append->Commit(), IsOk()); |
| 94 | + EXPECT_THAT(table_->Refresh(), IsOk()); |
| 95 | + } |
| 96 | + |
| 97 | + void SetTableFormatVersion(int8_t format_version) { |
| 98 | + table_->metadata()->format_version = format_version; |
| 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 | + |
| 107 | +TEST_F(RowDeltaTest, AddRowsCommitsAppendOperation) { |
| 108 | + std::shared_ptr<RowDelta> row_delta; |
| 109 | + ICEBERG_UNWRAP_OR_FAIL(row_delta, table_->NewRowDelta()); |
| 110 | + row_delta->AddRows(file_a_); |
| 111 | + |
| 112 | + EXPECT_THAT(row_delta->Commit(), IsOk()); |
| 113 | + |
| 114 | + EXPECT_THAT(table_->Refresh(), IsOk()); |
| 115 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot()); |
| 116 | + EXPECT_EQ(snapshot->Operation(), std::make_optional(DataOperation::kAppend)); |
| 117 | + EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedDataFiles), "1"); |
| 118 | + EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedRecords), "100"); |
| 119 | + EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedFileSize), "1024"); |
| 120 | +} |
| 121 | + |
| 122 | +TEST_F(RowDeltaTest, AddDeletesCommitsDeleteOperation) { |
| 123 | + auto delete_file = MakeDeleteFile("/delete/file_a_pos_deletes.parquet", |
| 124 | + /*partition_x=*/1L); |
| 125 | + |
| 126 | + std::shared_ptr<RowDelta> row_delta; |
| 127 | + ICEBERG_UNWRAP_OR_FAIL(row_delta, table_->NewRowDelta()); |
| 128 | + row_delta->AddDeletes(delete_file); |
| 129 | + |
| 130 | + EXPECT_THAT(row_delta->Commit(), IsOk()); |
| 131 | + |
| 132 | + EXPECT_THAT(table_->Refresh(), IsOk()); |
| 133 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot()); |
| 134 | + EXPECT_EQ(snapshot->Operation(), std::make_optional(DataOperation::kDelete)); |
| 135 | + EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedDeleteFiles), "1"); |
| 136 | + EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedPosDeleteFiles), "1"); |
| 137 | + EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedPosDeletes), "7"); |
| 138 | +} |
| 139 | + |
| 140 | +TEST_F(RowDeltaTest, RemoveRowsCommitsOverwriteOperation) { |
| 141 | + CommitFileA(); |
| 142 | + |
| 143 | + std::shared_ptr<RowDelta> row_delta; |
| 144 | + ICEBERG_UNWRAP_OR_FAIL(row_delta, table_->NewRowDelta()); |
| 145 | + row_delta->RemoveRows(file_a_); |
| 146 | + |
| 147 | + EXPECT_THAT(row_delta->Commit(), IsOk()); |
| 148 | + |
| 149 | + EXPECT_THAT(table_->Refresh(), IsOk()); |
| 150 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot()); |
| 151 | + EXPECT_EQ(snapshot->Operation(), std::make_optional(DataOperation::kOverwrite)); |
| 152 | + EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kDeletedDataFiles), "1"); |
| 153 | + EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kDeletedRecords), "100"); |
| 154 | + EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kRemovedFileSize), "1024"); |
| 155 | +} |
| 156 | + |
| 157 | +TEST_F(RowDeltaTest, RemoveRowsAndAddDeletesCommitsDeleteOperation) { |
| 158 | + CommitFileA(); |
| 159 | + |
| 160 | + auto delete_file = MakeDeleteFile("/delete/file_a_pos_deletes.parquet", |
| 161 | + /*partition_x=*/1L); |
| 162 | + |
| 163 | + std::shared_ptr<RowDelta> row_delta; |
| 164 | + ICEBERG_UNWRAP_OR_FAIL(row_delta, table_->NewRowDelta()); |
| 165 | + row_delta->RemoveRows(file_a_); |
| 166 | + row_delta->AddDeletes(delete_file); |
| 167 | + |
| 168 | + EXPECT_THAT(row_delta->Commit(), IsOk()); |
| 169 | + |
| 170 | + EXPECT_THAT(table_->Refresh(), IsOk()); |
| 171 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot()); |
| 172 | + EXPECT_EQ(snapshot->Operation(), std::make_optional(DataOperation::kDelete)); |
| 173 | + EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kDeletedDataFiles), "1"); |
| 174 | + EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedDeleteFiles), "1"); |
| 175 | +} |
| 176 | + |
| 177 | +TEST_F(RowDeltaTest, AddRowsAndRemoveDeletesCommitsAppendOperation) { |
| 178 | + auto delete_file = MakeDeleteFile("/delete/file_a_pos_deletes.parquet", |
| 179 | + /*partition_x=*/1L); |
| 180 | + { |
| 181 | + std::shared_ptr<RowDelta> row_delta; |
| 182 | + ICEBERG_UNWRAP_OR_FAIL(row_delta, table_->NewRowDelta()); |
| 183 | + row_delta->AddDeletes(delete_file); |
| 184 | + EXPECT_THAT(row_delta->Commit(), IsOk()); |
| 185 | + EXPECT_THAT(table_->Refresh(), IsOk()); |
| 186 | + } |
| 187 | + |
| 188 | + std::shared_ptr<RowDelta> row_delta; |
| 189 | + ICEBERG_UNWRAP_OR_FAIL(row_delta, table_->NewRowDelta()); |
| 190 | + row_delta->AddRows(file_a_); |
| 191 | + row_delta->RemoveDeletes(delete_file); |
| 192 | + |
| 193 | + EXPECT_THAT(row_delta->Commit(), IsOk()); |
| 194 | + |
| 195 | + EXPECT_THAT(table_->Refresh(), IsOk()); |
| 196 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot()); |
| 197 | + EXPECT_EQ(snapshot->Operation(), std::make_optional(DataOperation::kAppend)); |
| 198 | + EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedDataFiles), "1"); |
| 199 | + EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kRemovedDeleteFiles), "1"); |
| 200 | +} |
| 201 | + |
| 202 | +TEST_F(RowDeltaTest, CannotRemoveReferencedDataFile) { |
| 203 | + CommitFileA(); |
| 204 | + |
| 205 | + std::shared_ptr<RowDelta> row_delta; |
| 206 | + ICEBERG_UNWRAP_OR_FAIL(row_delta, table_->NewRowDelta()); |
| 207 | + std::vector<std::string> referenced_files{file_a_->file_path}; |
| 208 | + row_delta->ValidateDataFilesExist(referenced_files); |
| 209 | + row_delta->RemoveRows(file_a_); |
| 210 | + |
| 211 | + auto result = row_delta->Commit(); |
| 212 | + EXPECT_THAT(result, IsError(ErrorKind::kValidationFailed)); |
| 213 | + EXPECT_THAT(result, HasErrorMessage("Cannot delete data files")); |
| 214 | + EXPECT_THAT(result, HasErrorMessage(file_a_->file_path)); |
| 215 | +} |
| 216 | + |
| 217 | +TEST_F(RowDeltaTest, AddDeleteFileForRemovedDataFileCommitsDeleteOperation) { |
| 218 | + CommitFileA(); |
| 219 | + |
| 220 | + auto delete_file = MakeDeleteFile("/delete/file_a_pos_deletes.parquet", |
| 221 | + /*partition_x=*/1L); |
| 222 | + delete_file->referenced_data_file = file_a_->file_path; |
| 223 | + |
| 224 | + std::shared_ptr<RowDelta> row_delta; |
| 225 | + ICEBERG_UNWRAP_OR_FAIL(row_delta, table_->NewRowDelta()); |
| 226 | + row_delta->RemoveRows(file_a_); |
| 227 | + row_delta->AddDeletes(delete_file); |
| 228 | + |
| 229 | + EXPECT_THAT(row_delta->Commit(), IsOk()); |
| 230 | + |
| 231 | + EXPECT_THAT(table_->Refresh(), IsOk()); |
| 232 | + ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot()); |
| 233 | + EXPECT_EQ(snapshot->Operation(), std::make_optional(DataOperation::kDelete)); |
| 234 | + EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kDeletedDataFiles), "1"); |
| 235 | + EXPECT_EQ(snapshot->summary.at(SnapshotSummaryFields::kAddedDeleteFiles), "1"); |
| 236 | +} |
| 237 | + |
| 238 | +TEST_F(RowDeltaTest, ValidateDeletedFilesAllowsMissingRowsOnEmptyTable) { |
| 239 | + std::shared_ptr<RowDelta> row_delta; |
| 240 | + ICEBERG_UNWRAP_OR_FAIL(row_delta, table_->NewRowDelta()); |
| 241 | + row_delta->ValidateDeletedFiles(); |
| 242 | + row_delta->RemoveRows(file_a_); |
| 243 | + |
| 244 | + EXPECT_THAT(row_delta->Commit(), IsOk()); |
| 245 | +} |
| 246 | + |
| 247 | +TEST_F(RowDeltaTest, ValidateDeletedFilesAllowsMissingDeletesOnEmptyTable) { |
| 248 | + auto delete_file = MakeDeleteFile("/delete/file_a_pos_deletes.parquet", |
| 249 | + /*partition_x=*/1L); |
| 250 | + |
| 251 | + std::shared_ptr<RowDelta> row_delta; |
| 252 | + ICEBERG_UNWRAP_OR_FAIL(row_delta, table_->NewRowDelta()); |
| 253 | + row_delta->ValidateDeletedFiles(); |
| 254 | + row_delta->RemoveDeletes(delete_file); |
| 255 | + |
| 256 | + EXPECT_THAT(row_delta->Commit(), IsOk()); |
| 257 | +} |
| 258 | + |
| 259 | +TEST_F(RowDeltaTest, AddDeletionVectorValidatesConcurrentDVs) { |
| 260 | + CommitFileA(); |
| 261 | + ICEBERG_UNWRAP_OR_FAIL(auto starting_snapshot, table_->current_snapshot()); |
| 262 | + SetTableFormatVersion(3); |
| 263 | + |
| 264 | + auto concurrent_dv = |
| 265 | + MakeDeletionVector("/delete/concurrent-dv-a.puffin", file_a_->file_path, |
| 266 | + /*partition_x=*/1L, /*content_offset=*/0); |
| 267 | + std::shared_ptr<RowDelta> concurrent_delta; |
| 268 | + ICEBERG_UNWRAP_OR_FAIL(concurrent_delta, table_->NewRowDelta()); |
| 269 | + concurrent_delta->AddDeletes(concurrent_dv); |
| 270 | + EXPECT_THAT(concurrent_delta->Commit(), IsOk()); |
| 271 | + EXPECT_THAT(table_->Refresh(), IsOk()); |
| 272 | + SetTableFormatVersion(3); |
| 273 | + |
| 274 | + auto dv = MakeDeletionVector("/delete/dv-a.puffin", file_a_->file_path, |
| 275 | + /*partition_x=*/1L, /*content_offset=*/10); |
| 276 | + std::shared_ptr<RowDelta> row_delta; |
| 277 | + ICEBERG_UNWRAP_OR_FAIL(row_delta, table_->NewRowDelta()); |
| 278 | + row_delta->ValidateFromSnapshot(starting_snapshot->snapshot_id); |
| 279 | + row_delta->AddDeletes(dv); |
| 280 | + |
| 281 | + auto result = row_delta->Commit(); |
| 282 | + EXPECT_THAT(result, IsError(ErrorKind::kValidationFailed)); |
| 283 | + EXPECT_THAT(result, HasErrorMessage("Found concurrently added DV")); |
| 284 | + EXPECT_THAT(result, HasErrorMessage(file_a_->file_path)); |
| 285 | +} |
| 286 | + |
| 287 | +} // namespace iceberg |
0 commit comments