rdblue commented on code in PR #18853:
URL: https://github.com/apache/iceberg/pull/18853#discussion_r4529989679
##########
core/src/main/java/org/apache/iceberg/V4Metadata.java:
##########
@@ -541,4 +545,576 @@ public Long pos() {
return null;
}
}
+
+ /** Base wrapper used to write a {@link TrackedFile} using the v4 tracked
file schema. */
+ private abstract static class TrackedFileWrapper implements StructLike {
+ private static final int SIZE =
+ TrackedFile.schema(Types.StructType.of(),
Types.StructType.of()).columns().size();
+
+ private final Map<Integer, PartitionSpec> specsById;
+ private final Map<Integer, StructProjection> partitionProjections;
+ private final ContentStatsWrapper contentStats;
+ private final DeletionVectorWrapper deletionVector = new
DeletionVectorWrapper();
+ private final TrackingWrapper tracking;
+ private final ManifestInfoWrapper manifestInfo;
+
+ private TrackedFile wrapped = null;
+ private Integer specId = null;
+
+ private TrackedFileWrapper(
+ Map<Integer, PartitionSpec> specsById,
+ Types.StructType partitionType,
+ Types.StructType contentStatsType,
+ TrackingWrapper tracking,
+ ManifestInfoWrapper manifestInfo) {
+ this.specsById = specsById;
+ this.partitionProjections = partitionProjections(specsById.values(),
partitionType);
+ this.contentStats = new ContentStatsWrapper(contentStatsType);
+ this.tracking = tracking;
+ this.manifestInfo = manifestInfo;
+ }
+
+ protected void wrapInternal(TrackedFile file, Tracking newTracking, Long
assignedFirstRowId) {
+ if (file.contentType() == FileContent.DATA) {
+ Preconditions.checkArgument(
+ file.manifestInfo() == null,
+ "Invalid data file, manifest info must be null: %s",
+ file.location());
+ } else {
+ Preconditions.checkArgument(
+ file.manifestInfo() != null,
+ "Invalid manifest, missing manifest info: %s",
+ file.location());
+ }
+
+ // null sequence numbers are inherited from the commit: ADDED entries
inherit both and
+ // MODIFIED entries inherit only the data sequence number
+ EntryStatus status = newTracking.status();
+ Preconditions.checkState(
+ newTracking.dataSequenceNumber() != null
+ || status == EntryStatus.ADDED
+ || status == EntryStatus.MODIFIED,
+ "Cannot write null data sequence number for status %s (must be ADDED
or MODIFIED): %s",
+ status,
+ file.location());
+ Preconditions.checkState(
+ newTracking.fileSequenceNumber() != null || status ==
EntryStatus.ADDED,
+ "Cannot write null file sequence number for status %s (must be
ADDED): %s",
+ status,
+ file.location());
+
+ Integer fileSpecId = file.specId();
+ PartitionSpec spec = fileSpecId != null ? specsById.get(fileSpecId) :
null;
+ Preconditions.checkArgument(
+ fileSpecId == null || spec != null,
+ "Cannot find partition spec for spec ID: %s",
+ fileSpecId);
+
+ this.wrapped = file;
+ // v4 metadata stores a null spec ID and partition for unpartitioned
files
+ this.specId = spec != null && !spec.isUnpartitioned() ? fileSpecId :
null;
+ tracking.wrap(file, newTracking, assignedFirstRowId);
+ }
+
+ @Override
+ public int size() {
+ return SIZE;
+ }
+
+ @Override
+ public <T> void set(int pos, T value) {
+ throw new UnsupportedOperationException("Cannot modify
TrackedFileWrapper wrapper via set");
+ }
+
+ @Override
+ public <T> T get(int pos, Class<T> javaClass) {
+ return javaClass.cast(get(pos));
+ }
+
+ private Object get(int pos) {
+ return switch (pos) {
+ case 0 -> tracking;
+ case 1 -> wrapped.contentType().id();
+ case 2 -> wrapped.location();
+ case 3 -> wrapped.fileFormat().toString();
+ case 4 -> wrapped.recordCount();
+ case 5 -> wrapped.fileSizeInBytes();
+ case 6 -> specId;
+ case 7 -> unionPartition();
+ case 8 -> wrapped.contentStats() != null ?
contentStats.wrap(wrapped.contentStats()) : null;
+ case 9 -> wrapped.sortOrderId();
+ case 10 ->
+ wrapped.deletionVector() != null ?
deletionVector.wrap(wrapped.deletionVector()) : null;
+ case 11 ->
+ wrapped.manifestInfo() != null ?
manifestInfo.wrap(wrapped.manifestInfo()) : null;
+ case 12 -> wrapped.keyMetadata();
+ case 13 -> wrapped.splitOffsets();
+ case 14 -> wrapColumnFiles(wrapped.columnFiles());
+ default -> throw new UnsupportedOperationException("Unknown field
ordinal: " + pos);
+ };
+ }
+
+ private static List<StructLike> wrapColumnFiles(List<ColumnFile>
columnFiles) {
+ if (columnFiles == null) {
+ return null;
+ }
+
+ List<StructLike> wrappers =
Lists.newArrayListWithCapacity(columnFiles.size());
+ for (ColumnFile columnFile : columnFiles) {
+ wrappers.add(new ColumnFileWrapper(columnFile));
+ }
+
+ return wrappers;
+ }
+
+ private StructLike unionPartition() {
+ if (specId == null) {
+ return null;
+ }
+
+ StructProjection projection = partitionProjections.get(specId);
+ return projection != null ? projection.wrap(wrapped.partition()) :
wrapped.partition();
+ }
+
+ private static Map<Integer, StructProjection> partitionProjections(
+ Iterable<PartitionSpec> specs, Types.StructType unionType) {
+ Map<Integer, StructProjection> projections = Maps.newHashMap();
+ for (PartitionSpec spec : specs) {
+ Types.StructType partitionType = spec.partitionType();
+ for (Types.NestedField field : partitionType.fields()) {
+ Preconditions.checkArgument(
+ unionType.field(field.fieldId()) != null,
+ "Invalid union partition type, missing field %s from spec %s:
%s",
+ field.fieldId(),
+ spec.specId(),
+ unionType);
+ }
+
+ StructProjection projection =
+ partitionType.equals(unionType)
+ ? null
+ : StructProjection.createAllowMissing(partitionType,
unionType);
+ projections.put(spec.specId(), projection);
+ }
+
+ return projections;
+ }
+ }
+
+ /** Wrapper used to write a data file to a v4 leaf manifest. */
+ static class LeafTrackedFileWrapper extends TrackedFileWrapper {
+ LeafTrackedFileWrapper(
+ Map<Integer, PartitionSpec> specsById,
+ Types.StructType partitionType,
+ Types.StructType contentStatsType) {
+ super(
+ specsById,
+ partitionType,
+ contentStatsType,
+ new TrackingWrapper(),
+ null /* leaf manifests only contain data files */);
+ }
+
+ LeafTrackedFileWrapper wrap(TrackedFile file, Tracking newTracking) {
+ Preconditions.checkArgument(file != null, "Invalid file: null");
+ Preconditions.checkArgument(newTracking != null, "Invalid tracking:
null");
+ Preconditions.checkArgument(
+ file.contentType() == FileContent.DATA,
+ "Invalid content type for leaf manifest: %s",
+ file.contentType());
+
+ wrapInternal(file, newTracking, null /* assignedFirstRowId */);
+ return this;
+ }
+ }
+
+ /** Wrapper used to write a data file or leaf manifest to a v4 root
manifest. */
+ static class RootTrackedFileWrapper extends TrackedFileWrapper {
+ RootTrackedFileWrapper(
+ long commitSnapshotId,
+ long commitSequenceNumber,
+ Map<Integer, PartitionSpec> specsById,
+ Types.StructType partitionType,
+ Types.StructType contentStatsType) {
+ super(
+ specsById,
+ partitionType,
+ contentStatsType,
+ new TrackingWrapper(commitSnapshotId, commitSequenceNumber),
+ new ManifestInfoWrapper(commitSequenceNumber));
+ }
+
+ /**
+ * Re-points this wrapper at a file and the tracking to write for it.
+ *
+ * @param file a data file or manifest
+ * @param newTracking the tracking to write for the file
+ * @param assignedFirstRowId a first row ID to assign to the file, or null
if it is already
+ * assigned
+ * @return this for method chaining
+ */
+ RootTrackedFileWrapper wrap(TrackedFile file, Tracking newTracking, Long
assignedFirstRowId) {
+ Preconditions.checkArgument(file != null, "Invalid file: null");
+ Preconditions.checkArgument(newTracking != null, "Invalid tracking:
null");
+
+ FileContent content = file.contentType();
+ Preconditions.checkArgument(
+ content == FileContent.DATA
+ || content == FileContent.DATA_MANIFEST
+ || content == FileContent.DELETE_MANIFEST,
+ "Invalid content type for root manifest: %s",
+ content);
+ Preconditions.checkArgument(
+ newTracking.snapshotId() != null, "Missing snapshot ID for file:
%s", file.location());
+
+ wrapInternal(file, newTracking, assignedFirstRowId);
+ return this;
+ }
+ }
+
+ /** Wrapper used to write {@link Tracking} using the v4 tracking schema. */
+ private static class TrackingWrapper implements StructLike {
+ private static final int SIZE = Tracking.schema().fields().size();
+
+ private final Long commitSnapshotId;
+ private final Long commitSequenceNumber;
+
+ private TrackedFile file = null;
+ private Tracking wrapped = null;
+ private Long assignedFirstRowId = null;
+
+ private TrackingWrapper() {
+ this.commitSnapshotId = null;
+ this.commitSequenceNumber = null;
+ }
+
+ private TrackingWrapper(long commitSnapshotId, long commitSequenceNumber) {
+ this.commitSnapshotId = commitSnapshotId;
+ this.commitSequenceNumber = commitSequenceNumber;
+ }
+
+ private TrackingWrapper wrap(TrackedFile trackedFile, Tracking tracking,
Long firstRowId) {
+ if (commitSnapshotId != null) {
+ if (isUnassigned(tracking.dataSequenceNumber())) {
+ // a modified entry inherits the data sequence number of the commit
that modified it
+ Long dataSnapshotId =
+ tracking.status() == EntryStatus.MODIFIED
+ ? tracking.modifiedSnapshotId()
+ : tracking.snapshotId();
+ Preconditions.checkState(
+ commitSnapshotId.equals(dataSnapshotId),
+ "Cannot assign sequence number to file from snapshot %s
(committing snapshot %s): %s",
+ dataSnapshotId,
+ commitSnapshotId,
+ trackedFile.location());
+ }
+
+ if (isUnassigned(tracking.fileSequenceNumber())) {
+ Preconditions.checkState(
+ commitSnapshotId.equals(tracking.snapshotId()),
+ "Cannot assign sequence number to file from snapshot %s
(committing snapshot %s): %s",
+ tracking.snapshotId(),
+ commitSnapshotId,
+ trackedFile.location());
+ }
+
+ if (trackedFile.contentType() == FileContent.DELETE_MANIFEST) {
+ Preconditions.checkState(
+ firstRowId == null,
+ "Cannot assign first-row-id to delete manifest: %s",
+ trackedFile.location());
+ } else if (firstRowId != null) {
+ Preconditions.checkState(
+ tracking.status().isLive(),
+ "Cannot assign first-row-id to file with status %s: %s",
+ tracking.status(),
+ trackedFile.location());
+ Preconditions.checkState(
+ tracking.firstRowId() == null,
+ "Cannot assign first-row-id to file with existing first-row-id
%s: %s",
+ tracking.firstRowId(),
+ trackedFile.location());
+ } else {
+ Preconditions.checkState(
+ tracking.firstRowId() != null,
+ "Missing first-row-id for file: %s",
+ trackedFile.location());
+ }
+ }
+
+ this.file = trackedFile;
+ this.wrapped = tracking;
+ this.assignedFirstRowId = firstRowId;
+ return this;
+ }
+
+ @Override
+ public int size() {
+ return SIZE;
+ }
+
+ @Override
+ public <T> void set(int pos, T value) {
+ throw new UnsupportedOperationException("Cannot modify TrackingWrapper
wrapper via set");
+ }
+
+ @Override
+ public <T> T get(int pos, Class<T> javaClass) {
+ return javaClass.cast(get(pos));
+ }
+
+ private Object get(int pos) {
+ return switch (pos) {
+ case 0 -> wrapped.status().id();
+ case 1 -> wrapped.snapshotId();
+ case 2 -> assignSequenceNumber(wrapped.dataSequenceNumber(),
commitSequenceNumber);
+ case 3 -> assignSequenceNumber(wrapped.fileSequenceNumber(),
commitSequenceNumber);
+ case 4 -> wrapped.modifiedSnapshotId();
+ case 5 -> firstRowId();
+ case 6 -> wrapped.deletedPositions();
+ case 7 -> wrapped.replacedPositions();
+ default -> throw new UnsupportedOperationException("Unknown field
ordinal: " + pos);
+ };
+ }
+
+ private static Long assignSequenceNumber(Long sequenceNumber, Long
commitSequenceNumber) {
+ return commitSequenceNumber != null && isUnassigned(sequenceNumber)
+ ? commitSequenceNumber
+ : sequenceNumber;
+ }
+
+ private Long firstRowId() {
+ if (file.contentType() == FileContent.DELETE_MANIFEST) {
+ return null;
+ }
+
+ return assignedFirstRowId != null ? assignedFirstRowId :
wrapped.firstRowId();
+ }
+
+ private static boolean isUnassigned(Long sequenceNumber) {
+ return sequenceNumber == null || sequenceNumber ==
ManifestWriter.UNASSIGNED_SEQ;
+ }
+ }
+
+ /** Wrapper used to write {@link ManifestInfo} using the v4 manifest info
schema. */
+ private static class ManifestInfoWrapper implements StructLike {
+ private static final int SIZE = ManifestInfo.schema().fields().size();
+
+ private final long commitSequenceNumber;
+ private ManifestInfo wrapped = null;
+
+ private ManifestInfoWrapper(long commitSequenceNumber) {
+ this.commitSequenceNumber = commitSequenceNumber;
+ }
+
+ private ManifestInfoWrapper wrap(ManifestInfo info) {
+ this.wrapped = info;
+ return this;
+ }
+
+ @Override
+ public int size() {
+ return SIZE;
+ }
+
+ @Override
+ public <T> void set(int pos, T value) {
+ throw new UnsupportedOperationException("Cannot modify
ManifestInfoWrapper wrapper via set");
+ }
+
+ @Override
+ public <T> T get(int pos, Class<T> javaClass) {
+ return javaClass.cast(get(pos));
+ }
+
+ private Object get(int pos) {
+ return switch (pos) {
+ case 0 -> wrapped.addedFilesCount();
+ case 1 -> wrapped.existingFilesCount();
+ case 2 -> wrapped.deletedFilesCount();
+ case 3 -> wrapped.replacedFilesCount();
+ case 4 -> wrapped.modifiedFilesCount();
+ case 5 -> wrapped.addedRowsCount();
+ case 6 -> wrapped.existingRowsCount();
+ case 7 -> wrapped.deletedRowsCount();
+ case 8 -> wrapped.replacedRowsCount();
+ case 9 -> wrapped.modifiedRowsCount();
+ case 10 -> minSequenceNumber();
+ case 11 -> mdvBuffer();
+ case 12 -> wrapped.formatVersion();
+ default -> throw new UnsupportedOperationException("Unknown field
ordinal: " + pos);
+ };
+ }
+
+ private long minSequenceNumber() {
+ if (wrapped.minSequenceNumber() == ManifestWriter.UNASSIGNED_SEQ) {
+ return commitSequenceNumber;
+ }
+
+ return wrapped.minSequenceNumber();
+ }
+
+ private ByteBuffer mdvBuffer() {
+ ManifestBitmap mdv = wrapped.manifestDeletionVector();
+ return mdv != null ? mdv.buffer() : null;
+ }
+ }
+
+ /** Wrapper used to write {@link ContentStats} using a manifest's content
stats type. */
+ private static class ContentStatsWrapper implements StructLike {
+ private final int[] posToFieldId;
+ private final FieldStatsWrapper[] fieldStats;
+ private ContentStats wrapped = null;
+
+ private ContentStatsWrapper(Types.StructType type) {
+ List<Types.NestedField> fields = type.fields();
+ this.posToFieldId = ContentStatsStruct.posToId(type);
+ this.fieldStats = new FieldStatsWrapper[fields.size()];
+ for (int pos = 0; pos < fields.size(); pos += 1) {
+ fieldStats[pos] = new
FieldStatsWrapper(fields.get(pos).type().asStructType());
+ }
+ }
+
+ private ContentStatsWrapper wrap(ContentStats stats) {
+ this.wrapped = stats;
+ return this;
+ }
+
+ @Override
+ public int size() {
+ return posToFieldId.length;
+ }
+
+ @Override
+ public <T> void set(int pos, T value) {
+ throw new UnsupportedOperationException("Cannot modify
ContentStatsWrapper wrapper via set");
+ }
+
+ @Override
+ public <T> T get(int pos, Class<T> javaClass) {
+ FieldStats<?> stats = wrapped.statsFor(posToFieldId[pos]);
+ return stats != null ? javaClass.cast(fieldStats[pos].wrap(stats)) :
null;
+ }
+ }
+
+ /** Wrapper used to write {@link DeletionVector} using the v4 deletion
vector schema. */
+ private static class DeletionVectorWrapper implements StructLike {
+ private static final int SIZE = DeletionVector.schema().fields().size();
+
+ private DeletionVector wrapped = null;
+
+ private DeletionVectorWrapper wrap(DeletionVector dv) {
+ this.wrapped = dv;
+ return this;
+ }
+
+ @Override
+ public int size() {
+ return SIZE;
+ }
+
+ @Override
+ public <T> void set(int pos, T value) {
+ throw new UnsupportedOperationException(
+ "Cannot modify DeletionVectorWrapper wrapper via set");
+ }
+
+ @Override
+ public <T> T get(int pos, Class<T> javaClass) {
+ return javaClass.cast(get(pos));
+ }
+
+ private Object get(int pos) {
+ return switch (pos) {
+ case 0 -> wrapped.location();
+ case 1 -> wrapped.offset();
+ case 2 -> wrapped.sizeInBytes();
+ case 3 -> wrapped.cardinality();
+ case 4 -> wrapped.keyMetadata();
+ default -> throw new UnsupportedOperationException("Unknown field
ordinal: " + pos);
+ };
+ }
+ }
+
+ /** Wrapper used to write {@link ColumnFile} using the v4 column file
schema. */
+ private static class ColumnFileWrapper implements StructLike {
+ private static final int SIZE = ColumnFile.schema().fields().size();
+
+ private final ColumnFile wrapped;
+
+ private ColumnFileWrapper(ColumnFile columnFile) {
+ this.wrapped = columnFile;
+ }
+
+ @Override
+ public int size() {
+ return SIZE;
+ }
+
+ @Override
+ public <T> void set(int pos, T value) {
+ throw new UnsupportedOperationException("Cannot modify ColumnFileWrapper
wrapper via set");
+ }
+
+ @Override
+ public <T> T get(int pos, Class<T> javaClass) {
+ return javaClass.cast(get(pos));
+ }
+
+ private Object get(int pos) {
+ return switch (pos) {
+ case 0 -> wrapped.location();
+ case 1 -> ImmutableList.copyOf(wrapped.fieldIds());
Review Comment:
The writer is a `CollectionWriter` so it doesn't matter whether this returns
a `List` or a `Set` because both are `Collection`. You can return the `Set`
directly.
The only change I would make is to use a `LinkedHashSet` in
`ColumnFileStruct`:
```java
@Override
public Set<Integer> fieldIds() {
return fieldIds != null ? Sets.newLinkedHashSet(Ints.asList(fieldIds)) :
null;
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]