-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathreplace_partitions.cc
More file actions
138 lines (122 loc) · 5.5 KB
/
Copy pathreplace_partitions.cc
File metadata and controls
138 lines (122 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* 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/update/replace_partitions.h"
#include "iceberg/expression/expressions.h"
#include "iceberg/partition_spec.h"
#include "iceberg/snapshot.h"
#include "iceberg/table.h" // IWYU pragma: keep
#include "iceberg/table_metadata.h"
#include "iceberg/transaction.h"
#include "iceberg/util/error_collector.h"
#include "iceberg/util/macros.h"
namespace iceberg {
Result<std::unique_ptr<ReplacePartitions>> ReplacePartitions::Make(
std::string table_name, std::shared_ptr<TransactionContext> ctx) {
ICEBERG_PRECHECK(!table_name.empty(), "Table name cannot be empty");
ICEBERG_PRECHECK(ctx != nullptr, "Cannot create ReplacePartitions without a context");
return std::unique_ptr<ReplacePartitions>(
new ReplacePartitions(std::move(table_name), std::move(ctx)));
}
ReplacePartitions::ReplacePartitions(std::string table_name,
std::shared_ptr<TransactionContext> ctx)
: MergingSnapshotUpdate(std::move(table_name), std::move(ctx)) {
SetSummaryProperty(SnapshotSummaryFields::kReplacePartitions, "true");
}
ReplacePartitions& ReplacePartitions::AddFile(const std::shared_ptr<DataFile>& file) {
ICEBERG_BUILDER_CHECK(file != nullptr, "Invalid data file: null");
ICEBERG_BUILDER_CHECK(file->partition_spec_id.has_value(),
"Data file must have partition spec ID");
int32_t spec_id = file->partition_spec_id.value();
ICEBERG_BUILDER_ASSIGN_OR_RETURN(auto spec, base().PartitionSpecById(spec_id));
ICEBERG_BUILDER_RETURN_IF_ERROR(AddDataFile(file));
if (spec->fields().empty()) {
// Unpartitioned spec: Java's BaseReplacePartitions treats this as a
// table-wide replace rather than a spec-scoped DropPartition with empty
// partition values. Mirror that so every existing data file is dropped
// and conflict validation runs against AlwaysTrue.
ICEBERG_BUILDER_RETURN_IF_ERROR(DeleteByRowFilter(Expressions::AlwaysTrue()));
replace_by_row_filter_ = true;
} else {
ICEBERG_BUILDER_RETURN_IF_ERROR(DropPartition(spec_id, file->partition));
replaced_partitions_.add(spec_id, file->partition);
}
return *this;
}
ReplacePartitions& ReplacePartitions::ValidateAppendOnly() {
FailAnyDelete();
return *this;
}
ReplacePartitions& ReplacePartitions::ValidateFromSnapshot(int64_t snapshot_id) {
starting_snapshot_id_ = snapshot_id;
return *this;
}
ReplacePartitions& ReplacePartitions::ValidateNoConflictingData() {
validate_conflicting_data_ = true;
return *this;
}
ReplacePartitions& ReplacePartitions::ValidateNoConflictingDeletes() {
validate_conflicting_deletes_ = true;
return *this;
}
std::string ReplacePartitions::operation() { return DataOperation::kOverwrite; }
Status ReplacePartitions::Validate(const TableMetadata& current_metadata,
const std::shared_ptr<Snapshot>& snapshot) {
if (snapshot == nullptr) {
return {};
}
// No-op update: no partitions were staged and no table-wide replace was
// requested, so there is nothing to conflict with. Calling the validators
// with AlwaysTrue here would turn an empty builder into a full-table check.
if (!replace_by_row_filter_ && replaced_partitions_.empty()) {
return {};
}
auto io = ctx_->table->io();
if (validate_conflicting_data_) {
if (replace_by_row_filter_) {
ICEBERG_RETURN_UNEXPECTED(ValidateAddedDataFiles(
current_metadata, starting_snapshot_id_, Expressions::AlwaysTrue(), snapshot,
io, IsCaseSensitive()));
} else {
ICEBERG_RETURN_UNEXPECTED(ValidateAddedDataFiles(
current_metadata, starting_snapshot_id_, replaced_partitions_, snapshot, io));
}
}
if (validate_conflicting_deletes_) {
// Java's BaseReplacePartitions.validate gates both ValidateNoNewDeleteFiles
// and ValidateDeletedDataFiles on the same validateNewDeletes flag. The
// second check rejects concurrent overwrite/delete commits in the replaced
// partitions; without it a concurrent delete in a replaced partition would
// commit silently.
if (replace_by_row_filter_) {
ICEBERG_RETURN_UNEXPECTED(ValidateNoNewDeleteFiles(
current_metadata, starting_snapshot_id_, Expressions::AlwaysTrue(), snapshot,
io, IsCaseSensitive()));
ICEBERG_RETURN_UNEXPECTED(ValidateDeletedDataFiles(
current_metadata, starting_snapshot_id_, Expressions::AlwaysTrue(), snapshot,
io, IsCaseSensitive()));
} else {
ICEBERG_RETURN_UNEXPECTED(ValidateNoNewDeleteFiles(
current_metadata, starting_snapshot_id_, replaced_partitions_, snapshot, io));
ICEBERG_RETURN_UNEXPECTED(ValidateDeletedDataFiles(
current_metadata, starting_snapshot_id_, replaced_partitions_, snapshot, io));
}
}
return {};
}
} // namespace iceberg