|
| 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 | +#pragma once |
| 21 | + |
| 22 | +/// \file iceberg/update/replace_partitions.h |
| 23 | + |
| 24 | +#include <cstdint> |
| 25 | +#include <memory> |
| 26 | +#include <optional> |
| 27 | +#include <string> |
| 28 | + |
| 29 | +#include "iceberg/iceberg_export.h" |
| 30 | +#include "iceberg/result.h" |
| 31 | +#include "iceberg/type_fwd.h" |
| 32 | +#include "iceberg/update/merging_snapshot_update.h" |
| 33 | +#include "iceberg/util/partition_value_util.h" |
| 34 | + |
| 35 | +namespace iceberg { |
| 36 | + |
| 37 | +/// \brief Replaces partitions in a table with new data files. |
| 38 | +/// |
| 39 | +/// ReplacePartitions dynamically identifies which partitions to overwrite based |
| 40 | +/// on the data files added via AddFile(). All existing data files in each |
| 41 | +/// touched partition are marked DELETED, and the new files are written as the |
| 42 | +/// sole data in those partitions. Partitions not referenced by any added file |
| 43 | +/// are left unchanged. |
| 44 | +/// |
| 45 | +/// This operation produces a snapshot with operation="overwrite" and |
| 46 | +/// "replace-partitions"="true" in the summary. For unpartitioned tables, all |
| 47 | +/// existing files are replaced. |
| 48 | +/// |
| 49 | +/// When committing, these changes are applied to the latest table snapshot. |
| 50 | +/// Commit conflicts are resolved by re-applying to the new latest snapshot |
| 51 | +/// and reattempting the commit. |
| 52 | +class ICEBERG_EXPORT ReplacePartitions : public MergingSnapshotUpdate { |
| 53 | + public: |
| 54 | + /// \brief Create a new ReplacePartitions instance. |
| 55 | + /// |
| 56 | + /// \param table_name The name of the table |
| 57 | + /// \param ctx The transaction context |
| 58 | + /// \return A Result containing the ReplacePartitions instance or an error |
| 59 | + static Result<std::unique_ptr<ReplacePartitions>> Make( |
| 60 | + std::string table_name, std::shared_ptr<TransactionContext> ctx); |
| 61 | + |
| 62 | + /// \brief Add a data file and mark its partition for replacement. |
| 63 | + /// |
| 64 | + /// Each call registers the file's partition so all existing data files in |
| 65 | + /// that partition are replaced. Duplicate files (same path) are ignored. |
| 66 | + /// |
| 67 | + /// \param file The data file to add (must have partition_spec_id set) |
| 68 | + /// \return Reference to this for method chaining |
| 69 | + ReplacePartitions& AddFile(const std::shared_ptr<DataFile>& file); |
| 70 | + |
| 71 | + /// \brief Fail the commit if any existing data file would be deleted. |
| 72 | + /// |
| 73 | + /// This validation is useful to ensure the operation is only applied to |
| 74 | + /// tables where no data currently exists in the affected partitions. |
| 75 | + /// |
| 76 | + /// \return Reference to this for method chaining |
| 77 | + ReplacePartitions& ValidateAppendOnly(); |
| 78 | + |
| 79 | + /// \brief Set the snapshot ID used as the baseline for conflict validation. |
| 80 | + /// |
| 81 | + /// Validations check changes that occurred after this snapshot ID. If not |
| 82 | + /// set, all ancestor snapshots through the initial snapshot are validated. |
| 83 | + /// |
| 84 | + /// \param snapshot_id A snapshot ID |
| 85 | + /// \return Reference to this for method chaining |
| 86 | + ReplacePartitions& ValidateFromSnapshot(int64_t snapshot_id); |
| 87 | + |
| 88 | + /// \brief Enable validation that no conflicting data files were added concurrently. |
| 89 | + /// |
| 90 | + /// Fails the commit if a concurrent operation added a data file in any of |
| 91 | + /// the partitions being replaced after the snapshot set by |
| 92 | + /// ValidateFromSnapshot(). |
| 93 | + /// |
| 94 | + /// \return Reference to this for method chaining |
| 95 | + ReplacePartitions& ValidateNoConflictingData(); |
| 96 | + |
| 97 | + /// \brief Enable validation that no conflicting delete files were added concurrently. |
| 98 | + /// |
| 99 | + /// Fails the commit if a concurrent operation added a delete file covering |
| 100 | + /// any of the partitions being replaced after the snapshot set by |
| 101 | + /// ValidateFromSnapshot(). |
| 102 | + /// |
| 103 | + /// \return Reference to this for method chaining |
| 104 | + ReplacePartitions& ValidateNoConflictingDeletes(); |
| 105 | + |
| 106 | + std::string operation() override; |
| 107 | + |
| 108 | + protected: |
| 109 | + Status Validate(const TableMetadata& current_metadata, |
| 110 | + const std::shared_ptr<Snapshot>& snapshot) override; |
| 111 | + |
| 112 | + private: |
| 113 | + explicit ReplacePartitions(std::string table_name, |
| 114 | + std::shared_ptr<TransactionContext> ctx); |
| 115 | + |
| 116 | + std::optional<int64_t> starting_snapshot_id_; |
| 117 | + bool validate_conflicting_data_{false}; |
| 118 | + bool validate_conflicting_deletes_{false}; |
| 119 | + // Partitions touched by AddFile(); used to scope conflict validation to the |
| 120 | + // overwritten partitions instead of the whole table. For unpartitioned specs |
| 121 | + // the partition values are empty, and DropPartition(spec_id, {}) already |
| 122 | + // matches every file in that spec — no separate "whole table" path is needed. |
| 123 | + PartitionSet replaced_partitions_; |
| 124 | +}; |
| 125 | + |
| 126 | +} // namespace iceberg |
0 commit comments