/
ManifestReader.java
375 lines (322 loc) · 13 KB
/
ManifestReader.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* 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 static org.apache.iceberg.expressions.Expressions.alwaysTrue;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.iceberg.avro.Avro;
import org.apache.iceberg.avro.AvroIterable;
import org.apache.iceberg.exceptions.RuntimeIOException;
import org.apache.iceberg.expressions.Evaluator;
import org.apache.iceberg.expressions.Expression;
import org.apache.iceberg.expressions.Expressions;
import org.apache.iceberg.expressions.InclusiveMetricsEvaluator;
import org.apache.iceberg.expressions.Projections;
import org.apache.iceberg.io.CloseableGroup;
import org.apache.iceberg.io.CloseableIterable;
import org.apache.iceberg.io.CloseableIterator;
import org.apache.iceberg.io.InputFile;
import org.apache.iceberg.metrics.ScanMetrics;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.relocated.com.google.common.collect.Sets;
import org.apache.iceberg.types.Types;
import org.apache.iceberg.util.PartitionSet;
/**
* Base reader for data and delete manifest files.
*
* @param <F> The Java class of files returned by this reader.
*/
public class ManifestReader<F extends ContentFile<F>> extends CloseableGroup
implements CloseableIterable<F> {
static final ImmutableList<String> ALL_COLUMNS = ImmutableList.of("*");
private static final Set<String> STATS_COLUMNS =
ImmutableSet.of(
"value_counts",
"null_value_counts",
"nan_value_counts",
"lower_bounds",
"upper_bounds",
"record_count");
protected enum FileType {
DATA_FILES(GenericDataFile.class.getName()),
DELETE_FILES(GenericDeleteFile.class.getName());
private final String fileClass;
FileType(String fileClass) {
this.fileClass = fileClass;
}
private String fileClass() {
return fileClass;
}
}
private final InputFile file;
private final InheritableMetadata inheritableMetadata;
private final FileType content;
private final PartitionSpec spec;
private final Schema fileSchema;
// updated by configuration methods
private PartitionSet partitionSet = null;
private Expression partFilter = alwaysTrue();
private Expression rowFilter = alwaysTrue();
private Schema fileProjection = null;
private Collection<String> columns = null;
private boolean caseSensitive = true;
private ScanMetrics scanMetrics = ScanMetrics.noop();
// lazily initialized
private Evaluator lazyEvaluator = null;
private InclusiveMetricsEvaluator lazyMetricsEvaluator = null;
protected ManifestReader(
InputFile file,
int specId,
Map<Integer, PartitionSpec> specsById,
InheritableMetadata inheritableMetadata,
FileType content) {
this.file = file;
this.inheritableMetadata = inheritableMetadata;
this.content = content;
if (specsById != null) {
this.spec = specsById.get(specId);
} else {
this.spec = readPartitionSpec(file);
}
this.fileSchema = new Schema(DataFile.getType(spec.partitionType()).fields());
}
private <T extends ContentFile<T>> PartitionSpec readPartitionSpec(InputFile inputFile) {
Map<String, String> metadata = readMetadata(inputFile);
int specId = TableMetadata.INITIAL_SPEC_ID;
String specProperty = metadata.get("partition-spec-id");
if (specProperty != null) {
specId = Integer.parseInt(specProperty);
}
Schema schema = SchemaParser.fromJson(metadata.get("schema"));
return PartitionSpecParser.fromJsonFields(schema, specId, metadata.get("partition-spec"));
}
private static <T extends ContentFile<T>> Map<String, String> readMetadata(InputFile inputFile) {
Map<String, String> metadata;
try {
try (AvroIterable<ManifestEntry<T>> headerReader =
Avro.read(inputFile)
.project(ManifestEntry.getSchema(Types.StructType.of()).select("status"))
.classLoader(GenericManifestEntry.class.getClassLoader())
.build()) {
metadata = headerReader.getMetadata();
}
} catch (IOException e) {
throw new RuntimeIOException(e);
}
return metadata;
}
public boolean isDeleteManifestReader() {
return content == FileType.DELETE_FILES;
}
public InputFile file() {
return file;
}
public Schema schema() {
return fileSchema;
}
public PartitionSpec spec() {
return spec;
}
public ManifestReader<F> select(Collection<String> newColumns) {
Preconditions.checkState(
fileProjection == null,
"Cannot select columns using both select(String...) and project(Schema)");
this.columns = newColumns;
return this;
}
public ManifestReader<F> project(Schema newFileProjection) {
Preconditions.checkState(
columns == null, "Cannot select columns using both select(String...) and project(Schema)");
this.fileProjection = newFileProjection;
return this;
}
public ManifestReader<F> filterPartitions(Expression expr) {
this.partFilter = Expressions.and(partFilter, expr);
return this;
}
public ManifestReader<F> filterPartitions(PartitionSet partitions) {
this.partitionSet = partitions;
return this;
}
public ManifestReader<F> filterRows(Expression expr) {
this.rowFilter = Expressions.and(rowFilter, expr);
return this;
}
public ManifestReader<F> caseSensitive(boolean isCaseSensitive) {
this.caseSensitive = isCaseSensitive;
return this;
}
ManifestReader<F> scanMetrics(ScanMetrics newScanMetrics) {
this.scanMetrics = newScanMetrics;
return this;
}
CloseableIterable<ManifestEntry<F>> entries() {
return entries(false /* all entries */);
}
private CloseableIterable<ManifestEntry<F>> entries(boolean onlyLive) {
if (hasRowFilter() || hasPartitionFilter() || partitionSet != null) {
Evaluator evaluator = evaluator();
InclusiveMetricsEvaluator metricsEvaluator = metricsEvaluator();
// ensure stats columns are present for metrics evaluation
boolean requireStatsProjection = requireStatsProjection(rowFilter, columns);
Collection<String> projectColumns =
requireStatsProjection ? withStatsColumns(columns) : columns;
CloseableIterable<ManifestEntry<F>> entries =
open(projection(fileSchema, fileProjection, projectColumns, caseSensitive));
return CloseableIterable.filter(
content == FileType.DATA_FILES
? scanMetrics.skippedDataFiles()
: scanMetrics.skippedDeleteFiles(),
onlyLive ? filterLiveEntries(entries) : entries,
entry ->
entry != null
&& evaluator.eval(entry.file().partition())
&& metricsEvaluator.eval(entry.file())
&& inPartitionSet(entry.file()));
} else {
CloseableIterable<ManifestEntry<F>> entries =
open(projection(fileSchema, fileProjection, columns, caseSensitive));
return onlyLive ? filterLiveEntries(entries) : entries;
}
}
private boolean hasRowFilter() {
return rowFilter != null && rowFilter != Expressions.alwaysTrue();
}
private boolean hasPartitionFilter() {
return partFilter != null && partFilter != Expressions.alwaysTrue();
}
private boolean inPartitionSet(F fileToCheck) {
return partitionSet == null
|| partitionSet.contains(fileToCheck.specId(), fileToCheck.partition());
}
private CloseableIterable<ManifestEntry<F>> open(Schema projection) {
FileFormat format = FileFormat.fromFileName(file.location());
Preconditions.checkArgument(format != null, "Unable to determine format of manifest: %s", file);
List<Types.NestedField> fields = Lists.newArrayList();
fields.addAll(projection.asStruct().fields());
fields.add(MetadataColumns.ROW_POSITION);
switch (format) {
case AVRO:
AvroIterable<ManifestEntry<F>> reader =
Avro.read(file)
.project(ManifestEntry.wrapFileSchema(Types.StructType.of(fields)))
.rename("manifest_entry", GenericManifestEntry.class.getName())
.rename("partition", PartitionData.class.getName())
.rename("r102", PartitionData.class.getName())
.rename("data_file", content.fileClass())
.rename("r2", content.fileClass())
.classLoader(GenericManifestEntry.class.getClassLoader())
.reuseContainers()
.build();
addCloseable(reader);
return CloseableIterable.transform(reader, inheritableMetadata::apply);
default:
throw new UnsupportedOperationException("Invalid format for manifest file: " + format);
}
}
CloseableIterable<ManifestEntry<F>> liveEntries() {
return entries(true /* only live entries */);
}
private CloseableIterable<ManifestEntry<F>> filterLiveEntries(
CloseableIterable<ManifestEntry<F>> entries) {
return CloseableIterable.filter(entries, this::isLiveEntry);
}
private boolean isLiveEntry(ManifestEntry<F> entry) {
return entry != null && entry.status() != ManifestEntry.Status.DELETED;
}
/** @return an Iterator of DataFile. Makes defensive copies of files before returning */
@Override
public CloseableIterator<F> iterator() {
boolean dropStats = dropStats(columns);
return CloseableIterable.transform(liveEntries(), e -> e.file().copy(!dropStats)).iterator();
}
private static Schema projection(
Schema schema, Schema project, Collection<String> columns, boolean caseSensitive) {
if (columns != null) {
if (caseSensitive) {
return schema.select(columns);
} else {
return schema.caseInsensitiveSelect(columns);
}
} else if (project != null) {
return project;
}
return schema;
}
private Evaluator evaluator() {
if (lazyEvaluator == null) {
Expression projected = Projections.inclusive(spec, caseSensitive).project(rowFilter);
Expression finalPartFilter = Expressions.and(projected, partFilter);
if (finalPartFilter != null) {
this.lazyEvaluator = new Evaluator(spec.partitionType(), finalPartFilter, caseSensitive);
} else {
this.lazyEvaluator =
new Evaluator(spec.partitionType(), Expressions.alwaysTrue(), caseSensitive);
}
}
return lazyEvaluator;
}
private InclusiveMetricsEvaluator metricsEvaluator() {
if (lazyMetricsEvaluator == null) {
if (rowFilter != null) {
this.lazyMetricsEvaluator =
new InclusiveMetricsEvaluator(spec.schema(), rowFilter, caseSensitive);
} else {
this.lazyMetricsEvaluator =
new InclusiveMetricsEvaluator(spec.schema(), Expressions.alwaysTrue(), caseSensitive);
}
}
return lazyMetricsEvaluator;
}
private static boolean requireStatsProjection(Expression rowFilter, Collection<String> columns) {
// Make sure we have all stats columns for metrics evaluator
return rowFilter != Expressions.alwaysTrue()
&& columns != null
&& !columns.containsAll(ManifestReader.ALL_COLUMNS)
&& !columns.containsAll(STATS_COLUMNS);
}
static boolean dropStats(Collection<String> columns) {
// Make sure we only drop all stats if we had projected all stats
// We do not drop stats even if we had partially added some stats columns, except for
// record_count column.
// Since we don't want to keep stats map which could be huge in size just because we select
// record_count, which
// is a primitive type.
if (columns != null && !columns.containsAll(ManifestReader.ALL_COLUMNS)) {
Set<String> intersection = Sets.intersection(Sets.newHashSet(columns), STATS_COLUMNS);
return intersection.isEmpty() || intersection.equals(Sets.newHashSet("record_count"));
}
return false;
}
static List<String> withStatsColumns(Collection<String> columns) {
if (columns.containsAll(ManifestReader.ALL_COLUMNS)) {
return Lists.newArrayList(columns);
} else {
List<String> projectColumns = Lists.newArrayList(columns);
projectColumns.addAll(STATS_COLUMNS); // order doesn't matter
return projectColumns;
}
}
}