forked from apache/iceberg
/
BaseIncrementalChangelogScan.java
183 lines (151 loc) · 6.6 KB
/
BaseIncrementalChangelogScan.java
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
/*
* 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.ArrayDeque;
import java.util.Collection;
import java.util.Deque;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.iceberg.ManifestGroup.CreateTasksFunction;
import org.apache.iceberg.ManifestGroup.TaskContext;
import org.apache.iceberg.io.CloseableIterable;
import org.apache.iceberg.relocated.com.google.common.collect.FluentIterable;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.util.SnapshotUtil;
import org.apache.iceberg.util.TableScanUtil;
class BaseIncrementalChangelogScan
extends BaseIncrementalScan<
IncrementalChangelogScan, ChangelogScanTask, ScanTaskGroup<ChangelogScanTask>>
implements IncrementalChangelogScan {
BaseIncrementalChangelogScan(Table table) {
this(table, table.schema(), TableScanContext.empty());
}
private BaseIncrementalChangelogScan(Table table, Schema schema, TableScanContext context) {
super(table, schema, context);
}
@Override
protected IncrementalChangelogScan newRefinedScan(
Table newTable, Schema newSchema, TableScanContext newContext) {
return new BaseIncrementalChangelogScan(newTable, newSchema, newContext);
}
@Override
protected CloseableIterable<ChangelogScanTask> doPlanFiles(
Long fromSnapshotIdExclusive, long toSnapshotIdInclusive) {
Deque<Snapshot> changelogSnapshots =
orderedChangelogSnapshots(fromSnapshotIdExclusive, toSnapshotIdInclusive);
if (changelogSnapshots.isEmpty()) {
return CloseableIterable.empty();
}
Set<Long> changelogSnapshotIds = toSnapshotIds(changelogSnapshots);
Set<ManifestFile> newDataManifests =
FluentIterable.from(changelogSnapshots)
.transformAndConcat(snapshot -> snapshot.dataManifests(table().io()))
.filter(manifest -> changelogSnapshotIds.contains(manifest.snapshotId()))
.toSet();
ManifestGroup manifestGroup =
new ManifestGroup(table().io(), newDataManifests, ImmutableList.of())
.specsById(table().specs())
.caseSensitive(isCaseSensitive())
.select(scanColumns())
.filterData(filter())
.filterManifestEntries(entry -> changelogSnapshotIds.contains(entry.snapshotId()))
.ignoreExisting()
.columnsToKeepStats(columnsToKeepStats());
if (shouldIgnoreResiduals()) {
manifestGroup = manifestGroup.ignoreResiduals();
}
if (newDataManifests.size() > 1 && shouldPlanWithExecutor()) {
manifestGroup = manifestGroup.planWith(planExecutor());
}
return manifestGroup.plan(new CreateDataFileChangeTasks(changelogSnapshots));
}
@Override
public CloseableIterable<ScanTaskGroup<ChangelogScanTask>> planTasks() {
return TableScanUtil.planTaskGroups(
planFiles(), targetSplitSize(), splitLookback(), splitOpenFileCost());
}
// builds a collection of changelog snapshots (oldest to newest)
// the order of the snapshots is important as it is used to determine change ordinals
private Deque<Snapshot> orderedChangelogSnapshots(Long fromIdExcl, long toIdIncl) {
Deque<Snapshot> changelogSnapshots = new ArrayDeque<>();
for (Snapshot snapshot : SnapshotUtil.ancestorsBetween(table(), toIdIncl, fromIdExcl)) {
if (!snapshot.operation().equals(DataOperations.REPLACE)) {
if (snapshot.deleteManifests(table().io()).size() > 0) {
throw new UnsupportedOperationException(
"Delete files are currently not supported in changelog scans");
}
changelogSnapshots.addFirst(snapshot);
}
}
return changelogSnapshots;
}
private Set<Long> toSnapshotIds(Collection<Snapshot> snapshots) {
return snapshots.stream().map(Snapshot::snapshotId).collect(Collectors.toSet());
}
private static Map<Long, Integer> computeSnapshotOrdinals(Deque<Snapshot> snapshots) {
Map<Long, Integer> snapshotOrdinals = Maps.newHashMap();
int ordinal = 0;
for (Snapshot snapshot : snapshots) {
snapshotOrdinals.put(snapshot.snapshotId(), ordinal++);
}
return snapshotOrdinals;
}
private static class CreateDataFileChangeTasks implements CreateTasksFunction<ChangelogScanTask> {
private static final DeleteFile[] NO_DELETES = new DeleteFile[0];
private final Map<Long, Integer> snapshotOrdinals;
CreateDataFileChangeTasks(Deque<Snapshot> snapshots) {
this.snapshotOrdinals = computeSnapshotOrdinals(snapshots);
}
@Override
public CloseableIterable<ChangelogScanTask> apply(
CloseableIterable<ManifestEntry<DataFile>> entries, TaskContext context) {
return CloseableIterable.transform(
entries,
entry -> {
long commitSnapshotId = entry.snapshotId();
int changeOrdinal = snapshotOrdinals.get(commitSnapshotId);
DataFile dataFile = entry.file().copy(context.shouldKeepStats());
switch (entry.status()) {
case ADDED:
return new BaseAddedRowsScanTask(
changeOrdinal,
commitSnapshotId,
dataFile,
NO_DELETES,
context.schemaAsString(),
context.specAsString(),
context.residuals());
case DELETED:
return new BaseDeletedDataFileScanTask(
changeOrdinal,
commitSnapshotId,
dataFile,
NO_DELETES,
context.schemaAsString(),
context.specAsString(),
context.residuals());
default:
throw new IllegalArgumentException("Unexpected entry status: " + entry.status());
}
});
}
}
}