Repository navigation
Expand file tree
/
Copy pathBaseRowDelta.java
More file actions
194 lines (167 loc) · 5.8 KB
/
Copy pathBaseRowDelta.java
File metadata and controls
194 lines (167 loc) · 5.8 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* 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.
*/
package org.apache.iceberg;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.iceberg.exceptions.ValidationException;
import org.apache.iceberg.expressions.Expression;
import org.apache.iceberg.expressions.Expressions;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.util.CharSequenceSet;
import org.apache.iceberg.util.DataFileSet;
import org.apache.iceberg.util.SnapshotUtil;
public class BaseRowDelta extends MergingSnapshotProducer<RowDelta> implements RowDelta {
private Long startingSnapshotId = null; // check all versions by default
private final CharSequenceSet referencedDataFiles = CharSequenceSet.empty();
private final DataFileSet removedDataFiles = DataFileSet.create();
private boolean validateDeletes = false;
private Expression conflictDetectionFilter = Expressions.alwaysTrue();
private boolean validateNewDataFiles = false;
private boolean validateNewDeleteFiles = false;
protected BaseRowDelta(String tableName, TableOperations ops) {
super(tableName, ops);
}
@Override
protected BaseRowDelta self() {
return this;
}
@Override
protected String operation() {
if (addsDataFiles() && !addsDeleteFiles() && !deletesDataFiles()) {
return DataOperations.APPEND;
}
if (addsDeleteFiles() && !addsDataFiles()) {
return DataOperations.DELETE;
}
return DataOperations.OVERWRITE;
}
@Override
public RowDelta addRows(DataFile inserts) {
add(inserts);
return this;
}
@Override
public RowDelta addDeletes(DeleteFile deletes) {
add(deletes);
return this;
}
@Override
public RowDelta removeRows(DataFile file) {
removedDataFiles.add(file);
delete(file);
return this;
}
@Override
public RowDelta removeDeletes(DeleteFile deletes) {
delete(deletes);
return this;
}
@Override
public RowDelta validateFromSnapshot(long snapshotId) {
this.startingSnapshotId = snapshotId;
return this;
}
@Override
public RowDelta validateDeletedFiles() {
this.validateDeletes = true;
return this;
}
@Override
public RowDelta validateDataFilesExist(Iterable<? extends CharSequence> referencedFiles) {
referencedFiles.forEach(referencedDataFiles::add);
return this;
}
@Override
public RowDelta conflictDetectionFilter(Expression newConflictDetectionFilter) {
Preconditions.checkArgument(
newConflictDetectionFilter != null, "Conflict detection filter cannot be null");
this.conflictDetectionFilter = newConflictDetectionFilter;
return this;
}
@Override
public RowDelta validateNoConflictingDataFiles() {
this.validateNewDataFiles = true;
return this;
}
@Override
public RowDelta validateNoConflictingDeleteFiles() {
this.validateNewDeleteFiles = true;
return this;
}
@Override
public RowDelta toBranch(String branch) {
targetBranch(branch);
return this;
}
@Override
protected void validate(TableMetadata base, Snapshot parent) {
if (parent != null) {
if (startingSnapshotId != null) {
Preconditions.checkArgument(
SnapshotUtil.isAncestorOf(parent.snapshotId(), startingSnapshotId, base::snapshot),
"Snapshot %s is not an ancestor of %s",
startingSnapshotId,
parent.snapshotId());
}
if (!referencedDataFiles.isEmpty()) {
validateDataFilesExist(
base,
startingSnapshotId,
referencedDataFiles,
!validateDeletes,
conflictDetectionFilter,
parent);
}
if (validateDeletes) {
failMissingDeletePaths();
}
if (validateNewDataFiles) {
validateAddedDataFiles(base, startingSnapshotId, conflictDetectionFilter, parent);
}
if (validateNewDeleteFiles) {
// validate that explicitly deleted files have not had added deletes
if (!removedDataFiles.isEmpty()) {
validateNoNewDeletesForDataFiles(
base, startingSnapshotId, conflictDetectionFilter, removedDataFiles, parent);
}
// validate that previous deletes do not conflict with added deletes
validateNoNewDeleteFiles(base, startingSnapshotId, conflictDetectionFilter, parent);
}
validateNoConflictingFileAndPositionDeletes();
validateAddedDVs(base, startingSnapshotId, conflictDetectionFilter, parent);
}
}
/**
* Validates that the data files removed in this commit do not overlap with data files with delete
* files added
*/
@SuppressWarnings("CollectionUndefinedEquality")
private void validateNoConflictingFileAndPositionDeletes() {
List<CharSequence> deletedFileWithNewDVs =
removedDataFiles.stream()
.map(DataFile::path)
.filter(referencedDataFiles::contains)
.collect(Collectors.toList());
if (!deletedFileWithNewDVs.isEmpty()) {
throw new ValidationException(
"Cannot delete data files %s that are referenced by new delete files",
deletedFileWithNewDVs);
}
}
}