-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Core: Add v4 manifest reader #16958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Core: Add v4 manifest reader #16958
Changes from all commits
b70b31d
e45c65e
0853492
f1821b3
d872a76
67579d4
c78b664
cb918d6
765b5e7
08f5888
a951e11
f0925d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,272 @@ | ||
| /* | ||
| * 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.Map; | ||
| import java.util.Set; | ||
| import org.apache.iceberg.expressions.Evaluator; | ||
| import org.apache.iceberg.expressions.Expression; | ||
| import org.apache.iceberg.expressions.Expressions; | ||
| 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.Lists; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Maps; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Sets; | ||
| import org.apache.iceberg.types.TypeUtil; | ||
| import org.apache.iceberg.types.Types; | ||
| import org.apache.iceberg.util.StructProjection; | ||
|
|
||
| /** Reader that reads a v4+ manifest file as {@link TrackedFile}s. */ | ||
| class V4ManifestReader extends CloseableGroup implements CloseableIterable<TrackedFile> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this name
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class implements and returns a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is actually a good point. Leaving it open to hear from others.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good point, but I don't have a name suggestion that is better right now. Let's keep it in mind. |
||
| // tracking fields read on the scan path; row_position backs Tracking.manifestPos | ||
| private static final Types.StructType SCAN_TRACKING = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we keep this on the Something like the following in /** Returns the subset of tracking fields projected on the scan path. */
static Types.StructType scanSchema() {
return Types.StructType.of(
STATUS,
SNAPSHOT_ID,
SEQUENCE_NUMBER,
FILE_SEQUENCE_NUMBER,
FIRST_ROW_ID,
MetadataColumns.ROW_POSITION);
}Then One caveat: this pulls |
||
| Types.StructType.of( | ||
| Tracking.STATUS, | ||
| Tracking.SNAPSHOT_ID, | ||
| Tracking.SEQUENCE_NUMBER, | ||
| Tracking.FILE_SEQUENCE_NUMBER, | ||
| Tracking.FIRST_ROW_ID, | ||
| MetadataColumns.ROW_POSITION); | ||
|
|
||
| private final InputFile file; | ||
| private final Schema readSchema; | ||
| private final boolean onlyLive; | ||
| private final ScanMetrics scanMetrics; | ||
|
|
||
| // partition pruning state, keyed by spec ID | ||
| private final Map<Integer, Evaluator> partitionEvaluators; | ||
| private final Map<Integer, StructProjection> partitionProjections; | ||
|
|
||
| private V4ManifestReader( | ||
| InputFile file, | ||
| Schema readSchema, | ||
| Map<Integer, Evaluator> partitionEvaluators, | ||
| Map<Integer, StructProjection> partitionProjections, | ||
| boolean onlyLive, | ||
| ScanMetrics scanMetrics) { | ||
| this.file = file; | ||
| this.readSchema = readSchema; | ||
| this.partitionEvaluators = partitionEvaluators; | ||
| this.partitionProjections = partitionProjections; | ||
| this.onlyLive = onlyLive; | ||
| this.scanMetrics = scanMetrics; | ||
| } | ||
|
|
||
| static Builder builder(InputFile file, Map<Integer, PartitionSpec> specsById) { | ||
| return new Builder(file, specsById); | ||
| } | ||
|
|
||
| /** Returns copies of the tracked files that match this reader's configured filters. */ | ||
| @Override | ||
| public CloseableIterator<TrackedFile> iterator() { | ||
| CloseableIterable<TrackedFile> entries = CloseableIterable.transform(open(), this::prepare); | ||
| if (!partitionEvaluators.isEmpty()) { | ||
| // manifest references are expanded later and are not pruned by the partition filter | ||
| entries = | ||
| CloseableIterable.filter(entries, entry -> isManifest(entry) || matchesPartition(entry)); | ||
| } | ||
|
|
||
| if (onlyLive) { | ||
| entries = CloseableIterable.filter(entries, entry -> entry.tracking().isLive()); | ||
| } | ||
|
|
||
| return CloseableIterable.transform(entries, TrackedFile::copy).iterator(); | ||
| } | ||
|
|
||
| private static boolean isManifest(TrackedFile trackedFile) { | ||
| FileContent content = trackedFile.contentType(); | ||
| return content == FileContent.DATA_MANIFEST || content == FileContent.DELETE_MANIFEST; | ||
| } | ||
|
|
||
| private boolean matchesPartition(TrackedFile trackedFile) { | ||
| Integer specId = trackedFile.specId(); | ||
| Evaluator evaluator = specId != null ? partitionEvaluators.get(specId) : null; | ||
| StructProjection projection = specId != null ? partitionProjections.get(specId) : null; | ||
| Preconditions.checkState( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trying to wrap my head around this partition filtering:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a table goes from unpartitioned, to partitioned, new files also can be written in the unpartitioned space - this is a supported use case. So an unpartitioned file should not throw. Instead, it should not get filtered out by partition filtering, so that filters should apply to rows instead. e.g. table is partitioned by a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My point was that specId = null is a valid state, but we throw an exception here because of that because both the evaluator and the projection will be null.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The specID is a required field, so it should not be null. Even unpartitioned data has a spec (the unpartitioned spec). Also, any spec that exists within the table metadata is valid and can be written to directly (it isn't limited to older data written and then spec being evolved).
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hence the evaluation and preconditions check seem to only apply to data file entries in a leaf manifest file.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I get the point, we return early for leaf manifests so here we only have data/delete entries where we expect Since the field is optional, shouldn't we have an extra Preconditions check to verify that?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the spec needs to be a bit clear on this. I have a slight preference to make it optional for data/delete files as well, for truly unpartitioned cases. I will leave this thread open till we resolve this. |
||
| evaluator != null && projection != null, | ||
| "Cannot apply partition filter: spec ID %s is not one of the known specs %s in manifest %s", | ||
| specId, | ||
| partitionEvaluators.keySet(), | ||
| file.location()); | ||
|
|
||
| boolean matches = evaluator.eval(projection.wrap(trackedFile.partition())); | ||
| if (!matches) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use the Maybe constructing a predicate for partition filter and use
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Taking a second look, for this probably we should know the contentType beforehand, but it depends on the TrackedFile entry.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, your second comment is correct. A v4 manifest can mix data files, delete files, and manifest references in one file. So the counter depends on each entry's contentType and can only be selected inside the predicate. The current code is quite simple to undersand.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hope #17118 can help here where filter can now provide onKeep/onSkip callback and counters can be configured directly in the callback with more flexibility for the heterogeneous entry of TrackedFile |
||
| incrementSkipCount(trackedFile.contentType()); | ||
| } | ||
|
|
||
| return matches; | ||
| } | ||
|
|
||
| private void incrementSkipCount(FileContent content) { | ||
| switch (content) { | ||
| case DATA: | ||
| scanMetrics.skippedDataFiles().increment(); | ||
| break; | ||
| case EQUALITY_DELETES: | ||
| scanMetrics.skippedDeleteFiles().increment(); | ||
| break; | ||
| case DATA_MANIFEST: | ||
| scanMetrics.skippedDataManifests().increment(); | ||
| break; | ||
| case DELETE_MANIFEST: | ||
| scanMetrics.skippedDeleteManifests().increment(); | ||
| break; | ||
| default: | ||
| throw new UnsupportedOperationException("Unsupported content type: " + content); | ||
| } | ||
| } | ||
|
|
||
| private CloseableIterable<TrackedFile> open() { | ||
|
stevenzwu marked this conversation as resolved.
|
||
| FileFormat format = FileFormat.fromFileName(file.location()); | ||
| Preconditions.checkArgument( | ||
| format != null, "Cannot determine format of manifest: %s", file.location()); | ||
|
|
||
| CloseableIterable<TrackedFile> reader = | ||
| InternalData.read(format, file) | ||
| .project(readSchema) | ||
| .setRootType(TrackedFileStruct.class) | ||
| .setCustomType(TrackedFile.TRACKING.fieldId(), TrackingStruct.class) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: maybe a TODO that ContentStats is not covered now?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally we don't add many TODOs in the code (I got past feedback on this).
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, not for things that are currently being worked on. The TODO should not catch anything. Tests should catch when something is missing. |
||
| .setCustomType(TrackedFile.DELETION_VECTOR.fieldId(), DeletionVectorStruct.class) | ||
| .setCustomType(TrackedFile.MANIFEST_INFO.fieldId(), ManifestInfoStruct.class) | ||
| .setCustomType(TrackedFile.PARTITION_ID, PartitionData.class) | ||
| .reuseContainers() | ||
| .build(); | ||
| addCloseable(reader); | ||
| return reader; | ||
| } | ||
|
|
||
| private TrackedFile prepare(TrackedFile trackedFile) { | ||
| Tracking tracking = trackedFile.tracking(); | ||
| // manifestLocation is not stored in the manifest; the reader fills it in | ||
| if (tracking instanceof TrackingStruct) { | ||
| ((TrackingStruct) tracking).setManifestLocation(file.location()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We populate
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They look symmetric but the underlying mechanism differs. ROW_POSITION is synthesized by the reader itself. FILE_PATH us only populated when the caller injects it. I'd lean toward keeping the manual set for now
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess the difficulty here is that for InternalData API we don't have a way to pass the constants map to the underlying reader, right? I wonder what others say. Probably is an overkill at this point.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that we should worry much about this right now. I think @gaborkaszab is right that this should be set by the readers, but we don't have that done yet so we have left This and inheritance are going to change a bit, but we need the basic structure in place before we can do that. |
||
| } | ||
|
|
||
| return trackedFile; | ||
| } | ||
|
|
||
| static class Builder { | ||
| private final InputFile file; | ||
| private final Types.StructType partitionType; | ||
| private final Map<Integer, PartitionSpec> specsById; | ||
| private Expression rowFilter = Expressions.alwaysTrue(); | ||
| private boolean caseSensitive = true; | ||
| private boolean onlyLive = false; | ||
| private Schema fileProjection = null; | ||
| private ScanMetrics scanMetrics = ScanMetrics.noop(); | ||
|
|
||
| private Builder(InputFile file, Map<Integer, PartitionSpec> specsById) { | ||
| this.file = file; | ||
| this.partitionType = Partitioning.unionPartitionTypes(specsById.values()); | ||
| this.specsById = specsById; | ||
| } | ||
|
|
||
| /** Sets a row filter; files that cannot match the expression are skipped. */ | ||
| Builder filterRows(Expression expr) { | ||
| Preconditions.checkArgument(expr != null, "Invalid row filter: null"); | ||
| this.rowFilter = expr; | ||
| return this; | ||
| } | ||
|
|
||
| Builder caseSensitive(boolean isCaseSensitive) { | ||
| this.caseSensitive = isCaseSensitive; | ||
| return this; | ||
| } | ||
|
|
||
| /** Returns only files whose tracking {@link Tracking#isLive() is live}. */ | ||
| Builder liveOnly() { | ||
| this.onlyLive = true; | ||
| return this; | ||
| } | ||
|
|
||
| Builder project(Schema newFileProjection) { | ||
| this.fileProjection = newFileProjection; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: precondition for != null?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is intentional. Null projection means read all columns, and it's a supported case. This is the current semantics of the existing manifest reader as well. |
||
| return this; | ||
| } | ||
|
|
||
| Builder scanMetrics(ScanMetrics newScanMetrics) { | ||
| Preconditions.checkArgument(newScanMetrics != null, "Invalid scan metrics: null"); | ||
| this.scanMetrics = newScanMetrics; | ||
| return this; | ||
| } | ||
|
|
||
| V4ManifestReader build() { | ||
| Map<Integer, Evaluator> partitionEvaluators = Maps.newHashMap(); | ||
| Map<Integer, StructProjection> partitionProjections = Maps.newHashMap(); | ||
| if (hasPartitionFilter()) { | ||
| for (PartitionSpec spec : specsById.values()) { | ||
| Expression partFilter = Projections.inclusive(spec, caseSensitive).project(rowFilter); | ||
| partitionEvaluators.put( | ||
| spec.specId(), new Evaluator(spec.partitionType(), partFilter, caseSensitive)); | ||
| partitionProjections.put( | ||
| spec.specId(), StructProjection.create(partitionType, spec.partitionType())); | ||
| } | ||
| } | ||
|
|
||
| return new V4ManifestReader( | ||
| file, readSchema(), partitionEvaluators, partitionProjections, onlyLive, scanMetrics); | ||
| } | ||
|
|
||
| private boolean hasPartitionFilter() { | ||
| return rowFilter != Expressions.alwaysTrue() && !partitionType.fields().isEmpty(); | ||
| } | ||
|
|
||
| private Schema readSchema() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two loops here could go away. Loop at 240-249 (replacing // in TrackedFile.java
static Types.StructType schema(
Types.StructType partitionType, Types.StructType contentStatsType) {
return schema(Tracking.schema(), partitionType, contentStatsType);
}
static Types.StructType schema(
Types.StructType trackingType,
Types.StructType partitionType,
Types.StructType contentStatsType) {
return Types.StructType.of(
Types.NestedField.required(TRACKING.fieldId(), TRACKING.name(), trackingType, TRACKING.doc()),
CONTENT_TYPE,
FORMAT_VERSION,
LOCATION,
// ...rest unchanged
);
}Loop at 256-259 (collecting projected field IDs) is Set<Integer> projectedIds = Sets.newHashSet(TypeUtil.getProjectedIds(fileProjection));Pairs with the private Schema readSchema() {
Schema fullSchema = new Schema(
TrackedFile.schema(Tracking.scanSchema(), partitionType, Types.StructType.of()).fields());
if (fileProjection == null) {
return fullSchema;
}
Set<Integer> projectedIds = Sets.newHashSet(TypeUtil.getProjectedIds(fileProjection));
projectedIds.add(TrackedFile.TRACKING.fieldId());
projectedIds.add(TrackedFile.CONTENT_TYPE.fieldId());
if (hasPartitionFilter()) {
projectedIds.add(TrackedFile.SPEC_ID.fieldId());
projectedIds.add(TrackedFile.PARTITION_ID);
}
return TypeUtil.select(fullSchema, projectedIds);
} |
||
| Types.StructType fullType = TrackedFile.schema(partitionType, Types.StructType.of()); | ||
|
|
||
| // replace tracking with the subset of fields read on the scan path | ||
| List<Types.NestedField> fields = Lists.newArrayList(); | ||
| for (Types.NestedField field : fullType.fields()) { | ||
| if (field.fieldId() == TrackedFile.TRACKING.fieldId()) { | ||
| fields.add( | ||
| Types.NestedField.required( | ||
| field.fieldId(), field.name(), SCAN_TRACKING, field.doc())); | ||
| } else { | ||
| fields.add(field); | ||
| } | ||
| } | ||
|
|
||
| Schema fullSchema = new Schema(fields); | ||
| if (fileProjection == null) { | ||
| return fullSchema; | ||
| } | ||
|
|
||
| Set<Integer> projectedIds = Sets.newHashSet(); | ||
| for (Types.NestedField field : fileProjection.asStruct().fields()) { | ||
| projectedIds.add(field.fieldId()); | ||
| } | ||
|
|
||
| // status drives live-file filtering and content type distinguishes entry kinds | ||
| projectedIds.add(TrackedFile.TRACKING.fieldId()); | ||
| projectedIds.add(TrackedFile.CONTENT_TYPE.fieldId()); | ||
| if (hasPartitionFilter()) { | ||
| projectedIds.add(TrackedFile.SPEC_ID.fieldId()); | ||
| projectedIds.add(TrackedFile.PARTITION_ID); | ||
| } | ||
|
|
||
| return TypeUtil.select(fullSchema, projectedIds); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.