stevenzwu commented on code in PR #15854:
URL: https://github.com/apache/iceberg/pull/15854#discussion_r3102968758


##########
core/src/main/java/org/apache/iceberg/ManifestInfoStruct.java:
##########
@@ -0,0 +1,225 @@
+/*
+ * 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.nio.ByteBuffer;
+import java.util.Arrays;
+import org.apache.iceberg.avro.SupportsIndexProjection;
+import org.apache.iceberg.relocated.com.google.common.base.MoreObjects;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.util.ByteBuffers;
+
+/** Mutable {@link StructLike} implementation of {@link ManifestInfo}. */
+class ManifestInfoStruct extends SupportsIndexProjection implements 
ManifestInfo {
+  private static final Types.StructType BASE_TYPE =

Review Comment:
   seems that we can avoid the duplication by calling `ManifestInfo.schema()`



##########
core/src/main/java/org/apache/iceberg/TrackedFileStruct.java:
##########
@@ -0,0 +1,354 @@
+/*
+ * 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.io.Serializable;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.iceberg.avro.SupportsIndexProjection;
+import org.apache.iceberg.relocated.com.google.common.base.MoreObjects;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.util.ArrayUtil;
+import org.apache.iceberg.util.ByteBuffers;
+
+/** Mutable {@link StructLike} implementation of {@link TrackedFile}. */
+class TrackedFileStruct extends SupportsIndexProjection implements 
TrackedFile, Serializable {
+  private static final Types.StructType EMPTY_STRUCT_TYPE = 
Types.StructType.of();
+
+  private static final Types.StructType BASE_TYPE =

Review Comment:
   shouldn't the struct type be constructed using 
`TrackedFile.schemaWithContentStats(Types.StructType contentStatsType)`? I 
guess we need a constructor to take in `Types.StructType contentStatsType`? Or 
is that expected to be plumbed in later? 
   
   Even this code can be simplified as 
`TrackedFile.schemaWithContentStats(EMPTY_STRUCT_TYPE)`.



##########
core/src/main/java/org/apache/iceberg/TrackingStruct.java:
##########
@@ -0,0 +1,216 @@
+/*
+ * 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.nio.ByteBuffer;
+import java.util.Arrays;
+import org.apache.iceberg.avro.SupportsIndexProjection;
+import org.apache.iceberg.relocated.com.google.common.base.MoreObjects;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.util.ByteBuffers;
+
+/** Mutable {@link StructLike} implementation of {@link Tracking}. */
+class TrackingStruct extends SupportsIndexProjection implements Tracking {
+  private static final Types.StructType BASE_TYPE =

Review Comment:
   similar comment to avoid duplication by calling `Tracking.schema()`



##########
core/src/main/java/org/apache/iceberg/TrackedFileStruct.java:
##########
@@ -0,0 +1,354 @@
+/*
+ * 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.io.Serializable;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.iceberg.avro.SupportsIndexProjection;
+import org.apache.iceberg.relocated.com.google.common.base.MoreObjects;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.util.ArrayUtil;
+import org.apache.iceberg.util.ByteBuffers;
+
+/** Mutable {@link StructLike} implementation of {@link TrackedFile}. */
+class TrackedFileStruct extends SupportsIndexProjection implements 
TrackedFile, Serializable {
+  private static final Types.StructType EMPTY_STRUCT_TYPE = 
Types.StructType.of();
+
+  private static final Types.StructType BASE_TYPE =
+      Types.StructType.of(
+          TrackedFile.TRACKING,
+          TrackedFile.CONTENT_TYPE,
+          TrackedFile.LOCATION,
+          TrackedFile.FILE_FORMAT,
+          TrackedFile.RECORD_COUNT,
+          TrackedFile.FILE_SIZE_IN_BYTES,
+          TrackedFile.SPEC_ID,
+          Types.NestedField.optional(
+              TrackedFile.CONTENT_STATS_ID,
+              TrackedFile.CONTENT_STATS_NAME,
+              EMPTY_STRUCT_TYPE,
+              TrackedFile.CONTENT_STATS_DOC),
+          TrackedFile.SORT_ORDER_ID,
+          TrackedFile.DELETION_VECTOR,
+          TrackedFile.MANIFEST_INFO,
+          TrackedFile.KEY_METADATA,
+          TrackedFile.SPLIT_OFFSETS,
+          TrackedFile.EQUALITY_IDS,
+          MetadataColumns.ROW_POSITION);
+
+  private FileContent contentType = null;
+  private String location = null;
+  private FileFormat fileFormat = null;
+  private long recordCount = -1L;
+  private long fileSizeInBytes = -1L;
+  private Integer specId = null;
+
+  // optional fields
+  private Tracking tracking = null;
+  private ContentStats contentStats = null;
+  private Integer sortOrderId = null;
+  private DeletionVector deletionVector = null;
+  private ManifestInfo manifestInfo = null;
+  private byte[] keyMetadata = null;
+  private long[] splitOffsets = null;
+  private int[] equalityIds = null;
+
+  // not serialized to manifest, set by manifest readers
+  private transient TrackedFile manifestContext = null;
+  private long manifestPos = 0L;
+
+  /** Used by internal readers to instantiate this class with a projection 
schema. */
+  TrackedFileStruct(Types.StructType projection) {
+    super(BASE_TYPE, projection);
+  }
+
+  /** No-projection constructor for direct construction. */
+  TrackedFileStruct() {
+    super(BASE_TYPE.fields().size());
+  }
+
+  /** Copy constructor. */
+  private TrackedFileStruct(TrackedFileStruct toCopy, boolean withStats, 
Set<Integer> statsIds) {
+    super(toCopy);
+    this.contentType = toCopy.contentType;
+    this.location = toCopy.location;
+    this.fileFormat = toCopy.fileFormat;
+    this.recordCount = toCopy.recordCount;
+    this.fileSizeInBytes = toCopy.fileSizeInBytes;
+    this.specId = toCopy.specId;
+
+    this.manifestContext = toCopy.manifestContext;
+    this.manifestPos = toCopy.manifestPos;
+
+    this.tracking = toCopy.tracking != null ? ((TrackingStruct) 
toCopy.tracking).copy() : null;
+    if (tracking != null && manifestContext != null) {
+      ((TrackingStruct) tracking).setManifestContext(manifestContext);
+    }
+
+    this.sortOrderId = toCopy.sortOrderId;
+    this.deletionVector =
+        toCopy.deletionVector != null
+            ? ((DeletionVectorStruct) toCopy.deletionVector).copy()
+            : null;
+
+    if (withStats && toCopy.contentStats != null) {
+      ContentStats filtered = BaseContentStats.buildFrom(toCopy.contentStats, 
statsIds).build();
+      this.contentStats = filtered.fieldStats().isEmpty() ? null : filtered;
+    } else {
+      this.contentStats = null;
+    }
+
+    this.manifestInfo =
+        toCopy.manifestInfo != null ? ((ManifestInfoStruct) 
toCopy.manifestInfo).copy() : null;
+    this.keyMetadata =
+        toCopy.keyMetadata != null
+            ? Arrays.copyOf(toCopy.keyMetadata, toCopy.keyMetadata.length)
+            : null;
+    this.splitOffsets =
+        toCopy.splitOffsets != null
+            ? Arrays.copyOf(toCopy.splitOffsets, toCopy.splitOffsets.length)
+            : null;
+    this.equalityIds =
+        toCopy.equalityIds != null
+            ? Arrays.copyOf(toCopy.equalityIds, toCopy.equalityIds.length)
+            : null;
+  }
+
+  void setManifestContext(TrackedFile manifest) {
+    this.manifestContext = manifest;
+    if (tracking != null) {
+      ((TrackingStruct) tracking).setManifestContext(manifest);
+    }
+  }
+
+  @Override
+  public Tracking tracking() {
+    return tracking;
+  }
+
+  @Override
+  public FileContent contentType() {
+    return contentType;
+  }
+
+  @Override
+  public String location() {
+    return location;
+  }
+
+  @Override
+  public FileFormat fileFormat() {
+    return fileFormat;
+  }
+
+  @Override
+  public long recordCount() {
+    return recordCount;
+  }
+
+  @Override
+  public long fileSizeInBytes() {
+    return fileSizeInBytes;
+  }
+
+  @Override
+  public Integer specId() {
+    return specId;
+  }
+
+  @Override
+  public ContentStats contentStats() {
+    return contentStats;
+  }
+
+  @Override
+  public Integer sortOrderId() {
+    return sortOrderId;
+  }
+
+  @Override
+  public DeletionVector deletionVector() {
+    return deletionVector;
+  }
+
+  @Override
+  public ManifestInfo manifestInfo() {
+    return manifestInfo;
+  }
+
+  @Override
+  public ByteBuffer keyMetadata() {
+    return keyMetadata != null ? ByteBuffer.wrap(keyMetadata) : null;
+  }
+
+  @Override
+  public List<Long> splitOffsets() {
+    return splitOffsets != null ? 
ArrayUtil.toUnmodifiableLongList(splitOffsets) : null;
+  }
+
+  @Override
+  public List<Integer> equalityIds() {
+    return equalityIds != null ? ArrayUtil.toUnmodifiableIntList(equalityIds) 
: null;
+  }
+
+  @Override
+  public TrackedFile copy() {
+    return new TrackedFileStruct(this, true, null);
+  }
+
+  @Override
+  public TrackedFile copyWithStats(Set<Integer> requestedColumnIds) {
+    return new TrackedFileStruct(this, true, requestedColumnIds);
+  }
+
+  @Override
+  public String manifestLocation() {
+    if (tracking == null) {
+      return null;
+    }
+
+    return manifestContext != null ? manifestContext.location() : null;
+  }
+
+  @Override
+  public long manifestPos() {
+    return manifestPos;
+  }
+
+  @Override
+  protected <T> T internalGet(int pos, Class<T> javaClass) {
+    return javaClass.cast(getByPos(pos));
+  }
+
+  private Object getByPos(int pos) {
+    switch (pos) {
+      case 0:
+        return tracking;
+      case 1:
+        return contentType.id();
+      case 2:
+        return location;
+      case 3:
+        return fileFormat != null ? fileFormat.toString() : null;
+      case 4:
+        return recordCount;
+      case 5:
+        return fileSizeInBytes;
+      case 6:
+        return specId;
+      case 7:
+        return contentStats;
+      case 8:
+        return sortOrderId;
+      case 9:
+        return deletionVector;
+      case 10:
+        return manifestInfo;
+      case 11:
+        return keyMetadata();
+      case 12:
+        return splitOffsets();
+      case 13:
+        return equalityIds();
+      case 14:
+        return manifestPos;
+      default:
+        throw new UnsupportedOperationException("Unknown field ordinal: " + 
pos);
+    }
+  }
+
+  @Override
+  protected <T> void internalSet(int pos, T value) {
+    switch (pos) {
+      case 0:
+        this.tracking = (Tracking) value;
+        ((TrackingStruct) tracking).setManifestContext(manifestContext);
+        break;
+      case 1:
+        this.contentType = FileContent.fromId((Integer) value);
+        break;
+      case 2:
+        // always coerce to String for Serializable
+        this.location = value.toString();
+        break;
+      case 3:
+        this.fileFormat = FileFormat.fromString(value.toString());
+        break;
+      case 4:
+        this.recordCount = (Long) value;
+        break;
+      case 5:
+        this.fileSizeInBytes = (Long) value;
+        break;
+      case 6:
+        this.specId = (Integer) value;
+        break;
+      case 7:
+        this.contentStats = (ContentStats) value;
+        break;
+      case 8:
+        this.sortOrderId = (Integer) value;
+        break;
+      case 9:
+        this.deletionVector = (DeletionVector) value;
+        break;
+      case 10:
+        this.manifestInfo = (ManifestInfo) value;
+        break;
+      case 11:
+        this.keyMetadata = ByteBuffers.toByteArray((ByteBuffer) value);
+        break;
+      case 12:
+        this.splitOffsets = ArrayUtil.toLongArray((List<Long>) value);
+        break;
+      case 13:
+        this.equalityIds = ArrayUtil.toIntArray((List<Integer>) value);
+        break;
+      case 14:
+        this.manifestPos = (long) value;
+        break;
+      default:
+        // ignore the object, it must be from a newer version of the format
+    }
+  }
+
+  @Override
+  public String toString() {
+    return MoreObjects.toStringHelper(this)
+        .add("content", contentType.toString().toLowerCase(Locale.ROOT))

Review Comment:
   nit: NPE if contentType is null



##########
core/src/main/java/org/apache/iceberg/TrackingStruct.java:
##########
@@ -0,0 +1,216 @@
+/*
+ * 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.nio.ByteBuffer;
+import java.util.Arrays;
+import org.apache.iceberg.avro.SupportsIndexProjection;
+import org.apache.iceberg.relocated.com.google.common.base.MoreObjects;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.util.ByteBuffers;
+
+/** Mutable {@link StructLike} implementation of {@link Tracking}. */
+class TrackingStruct extends SupportsIndexProjection implements Tracking {
+  private static final Types.StructType BASE_TYPE =
+      Types.StructType.of(
+          Tracking.STATUS,
+          Tracking.SNAPSHOT_ID,
+          Tracking.SEQUENCE_NUMBER,
+          Tracking.FILE_SEQUENCE_NUMBER,
+          Tracking.DV_SNAPSHOT_ID,
+          Tracking.FIRST_ROW_ID,
+          Tracking.DELETED_POSITIONS,
+          Tracking.REPLACED_POSITIONS);
+
+  private EntryStatus status = null;
+  private Long snapshotId = null;
+  private Long sequenceNumber = null;
+  private Long fileSequenceNumber = null;
+  private Long dvSnapshotId = null;
+  private Long firstRowId = null;
+  private byte[] deletedPositions = null;
+  private byte[] replacedPositions = null;
+
+  // not serialized, set by manifest readers for metadata inheritance
+  private transient TrackedFile manifestContext = null;
+
+  TrackingStruct(Types.StructType type) {
+    super(BASE_TYPE, type);
+  }
+
+  private TrackingStruct(TrackingStruct toCopy) {
+    super(toCopy);
+    this.status = toCopy.status;
+    this.snapshotId = toCopy.snapshotId;
+    this.sequenceNumber = toCopy.sequenceNumber;
+    this.fileSequenceNumber = toCopy.fileSequenceNumber;
+    this.dvSnapshotId = toCopy.dvSnapshotId;
+    this.firstRowId = toCopy.firstRowId;
+    this.deletedPositions =
+        toCopy.deletedPositions != null
+            ? Arrays.copyOf(toCopy.deletedPositions, 
toCopy.deletedPositions.length)
+            : null;
+    this.replacedPositions =
+        toCopy.replacedPositions != null
+            ? Arrays.copyOf(toCopy.replacedPositions, 
toCopy.replacedPositions.length)
+            : null;
+  }
+
+  TrackingStruct copy() {
+    return new TrackingStruct(this);
+  }
+
+  void setManifestContext(TrackedFile manifest) {
+    this.manifestContext = manifest;
+  }
+
+  @Override
+  public EntryStatus status() {
+    return status;
+  }
+
+  @Override
+  public Long snapshotId() {
+    if (snapshotId != null) {
+      return snapshotId;
+    }
+
+    return manifestContext != null ? manifestContext.tracking().snapshotId() : 
null;
+  }
+
+  @Override
+  public Long dataSequenceNumber() {
+    if (sequenceNumber != null) {
+      return sequenceNumber;
+    }
+
+    if (manifestContext != null && status == EntryStatus.ADDED) {
+      return manifestContext.tracking().dataSequenceNumber();
+    }
+
+    return null;
+  }
+
+  @Override
+  public Long fileSequenceNumber() {
+    if (fileSequenceNumber != null) {
+      return fileSequenceNumber;
+    }
+
+    if (manifestContext != null && status == EntryStatus.ADDED) {
+      return manifestContext.tracking().dataSequenceNumber();
+    }
+
+    return null;
+  }
+
+  @Override
+  public Long dvSnapshotId() {
+    return dvSnapshotId;
+  }
+
+  @Override
+  public Long firstRowId() {
+    return firstRowId;
+  }
+
+  @Override
+  public ByteBuffer deletedPositions() {
+    return deletedPositions != null ? ByteBuffer.wrap(deletedPositions) : null;
+  }
+
+  @Override
+  public ByteBuffer replacedPositions() {
+    return replacedPositions != null ? ByteBuffer.wrap(replacedPositions) : 
null;
+  }
+
+  @Override
+  protected <T> T internalGet(int pos, Class<T> javaClass) {
+    return javaClass.cast(getByPos(pos));
+  }
+
+  private Object getByPos(int pos) {
+    switch (pos) {
+      case 0:
+        return status.id();
+      case 1:
+        return snapshotId();
+      case 2:
+        return dataSequenceNumber();

Review Comment:
   `getByPos` for sequence/snapshot goes through dataSequenceNumber() / 
fileSequenceNumber() / snapshotId(), which apply manifestContext inheritance. 
Any serializer/writer that get()s then writes may materialize inherited values 
instead of leaving nulls in the serialized payload. 
   
   Not necessarily a problem. We can revisit this after seeing the getter usage 
later.



##########
core/src/main/java/org/apache/iceberg/TrackedFileStruct.java:
##########
@@ -0,0 +1,354 @@
+/*
+ * 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.io.Serializable;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.iceberg.avro.SupportsIndexProjection;
+import org.apache.iceberg.relocated.com.google.common.base.MoreObjects;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.util.ArrayUtil;
+import org.apache.iceberg.util.ByteBuffers;
+
+/** Mutable {@link StructLike} implementation of {@link TrackedFile}. */
+class TrackedFileStruct extends SupportsIndexProjection implements 
TrackedFile, Serializable {
+  private static final Types.StructType EMPTY_STRUCT_TYPE = 
Types.StructType.of();
+
+  private static final Types.StructType BASE_TYPE =
+      Types.StructType.of(
+          TrackedFile.TRACKING,
+          TrackedFile.CONTENT_TYPE,
+          TrackedFile.LOCATION,
+          TrackedFile.FILE_FORMAT,
+          TrackedFile.RECORD_COUNT,
+          TrackedFile.FILE_SIZE_IN_BYTES,
+          TrackedFile.SPEC_ID,
+          Types.NestedField.optional(
+              TrackedFile.CONTENT_STATS_ID,
+              TrackedFile.CONTENT_STATS_NAME,
+              EMPTY_STRUCT_TYPE,
+              TrackedFile.CONTENT_STATS_DOC),
+          TrackedFile.SORT_ORDER_ID,
+          TrackedFile.DELETION_VECTOR,
+          TrackedFile.MANIFEST_INFO,
+          TrackedFile.KEY_METADATA,
+          TrackedFile.SPLIT_OFFSETS,
+          TrackedFile.EQUALITY_IDS,
+          MetadataColumns.ROW_POSITION);
+
+  private FileContent contentType = null;
+  private String location = null;
+  private FileFormat fileFormat = null;
+  private long recordCount = -1L;
+  private long fileSizeInBytes = -1L;
+  private Integer specId = null;
+
+  // optional fields
+  private Tracking tracking = null;
+  private ContentStats contentStats = null;
+  private Integer sortOrderId = null;
+  private DeletionVector deletionVector = null;
+  private ManifestInfo manifestInfo = null;
+  private byte[] keyMetadata = null;
+  private long[] splitOffsets = null;
+  private int[] equalityIds = null;
+
+  // not serialized to manifest, set by manifest readers
+  private transient TrackedFile manifestContext = null;
+  private long manifestPos = 0L;
+
+  /** Used by internal readers to instantiate this class with a projection 
schema. */
+  TrackedFileStruct(Types.StructType projection) {
+    super(BASE_TYPE, projection);
+  }
+
+  /** No-projection constructor for direct construction. */
+  TrackedFileStruct() {
+    super(BASE_TYPE.fields().size());
+  }
+
+  /** Copy constructor. */
+  private TrackedFileStruct(TrackedFileStruct toCopy, boolean withStats, 
Set<Integer> statsIds) {
+    super(toCopy);
+    this.contentType = toCopy.contentType;
+    this.location = toCopy.location;
+    this.fileFormat = toCopy.fileFormat;
+    this.recordCount = toCopy.recordCount;
+    this.fileSizeInBytes = toCopy.fileSizeInBytes;
+    this.specId = toCopy.specId;
+
+    this.manifestContext = toCopy.manifestContext;
+    this.manifestPos = toCopy.manifestPos;
+
+    this.tracking = toCopy.tracking != null ? ((TrackingStruct) 
toCopy.tracking).copy() : null;
+    if (tracking != null && manifestContext != null) {
+      ((TrackingStruct) tracking).setManifestContext(manifestContext);
+    }
+
+    this.sortOrderId = toCopy.sortOrderId;
+    this.deletionVector =
+        toCopy.deletionVector != null
+            ? ((DeletionVectorStruct) toCopy.deletionVector).copy()
+            : null;
+
+    if (withStats && toCopy.contentStats != null) {
+      ContentStats filtered = BaseContentStats.buildFrom(toCopy.contentStats, 
statsIds).build();
+      this.contentStats = filtered.fieldStats().isEmpty() ? null : filtered;
+    } else {
+      this.contentStats = null;
+    }
+
+    this.manifestInfo =
+        toCopy.manifestInfo != null ? ((ManifestInfoStruct) 
toCopy.manifestInfo).copy() : null;
+    this.keyMetadata =
+        toCopy.keyMetadata != null
+            ? Arrays.copyOf(toCopy.keyMetadata, toCopy.keyMetadata.length)
+            : null;
+    this.splitOffsets =
+        toCopy.splitOffsets != null
+            ? Arrays.copyOf(toCopy.splitOffsets, toCopy.splitOffsets.length)
+            : null;
+    this.equalityIds =
+        toCopy.equalityIds != null
+            ? Arrays.copyOf(toCopy.equalityIds, toCopy.equalityIds.length)
+            : null;
+  }
+
+  void setManifestContext(TrackedFile manifest) {
+    this.manifestContext = manifest;
+    if (tracking != null) {
+      ((TrackingStruct) tracking).setManifestContext(manifest);
+    }
+  }
+
+  @Override
+  public Tracking tracking() {
+    return tracking;
+  }
+
+  @Override
+  public FileContent contentType() {
+    return contentType;
+  }
+
+  @Override
+  public String location() {
+    return location;
+  }
+
+  @Override
+  public FileFormat fileFormat() {
+    return fileFormat;
+  }
+
+  @Override
+  public long recordCount() {
+    return recordCount;
+  }
+
+  @Override
+  public long fileSizeInBytes() {
+    return fileSizeInBytes;
+  }
+
+  @Override
+  public Integer specId() {
+    return specId;
+  }
+
+  @Override
+  public ContentStats contentStats() {
+    return contentStats;
+  }
+
+  @Override
+  public Integer sortOrderId() {
+    return sortOrderId;
+  }
+
+  @Override
+  public DeletionVector deletionVector() {
+    return deletionVector;
+  }
+
+  @Override
+  public ManifestInfo manifestInfo() {
+    return manifestInfo;
+  }
+
+  @Override
+  public ByteBuffer keyMetadata() {
+    return keyMetadata != null ? ByteBuffer.wrap(keyMetadata) : null;
+  }
+
+  @Override
+  public List<Long> splitOffsets() {
+    return splitOffsets != null ? 
ArrayUtil.toUnmodifiableLongList(splitOffsets) : null;
+  }
+
+  @Override
+  public List<Integer> equalityIds() {
+    return equalityIds != null ? ArrayUtil.toUnmodifiableIntList(equalityIds) 
: null;
+  }
+
+  @Override
+  public TrackedFile copy() {
+    return new TrackedFileStruct(this, true, null);
+  }
+
+  @Override
+  public TrackedFile copyWithStats(Set<Integer> requestedColumnIds) {
+    return new TrackedFileStruct(this, true, requestedColumnIds);
+  }
+
+  @Override
+  public String manifestLocation() {
+    if (tracking == null) {
+      return null;
+    }
+
+    return manifestContext != null ? manifestContext.location() : null;
+  }
+
+  @Override
+  public long manifestPos() {
+    return manifestPos;
+  }
+
+  @Override
+  protected <T> T internalGet(int pos, Class<T> javaClass) {
+    return javaClass.cast(getByPos(pos));
+  }
+
+  private Object getByPos(int pos) {
+    switch (pos) {
+      case 0:
+        return tracking;
+      case 1:
+        return contentType.id();

Review Comment:
   nit: NPE if contentType is null



##########
core/src/main/java/org/apache/iceberg/TrackedFileStruct.java:
##########
@@ -0,0 +1,337 @@
+/*
+ * 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.io.Serializable;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.iceberg.avro.SupportsIndexProjection;
+import org.apache.iceberg.relocated.com.google.common.base.MoreObjects;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.stats.BaseContentStats;
+import org.apache.iceberg.stats.ContentStats;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.util.ArrayUtil;
+import org.apache.iceberg.util.ByteBuffers;
+
+/** Mutable {@link StructLike} implementation of {@link TrackedFile}. */
+class TrackedFileStruct extends SupportsIndexProjection implements 
TrackedFile, Serializable {
+  private static final FileContent[] FILE_CONTENT_VALUES = 
FileContent.values();
+  private static final Types.StructType BASE_TYPE =
+      Types.StructType.of(
+          ImmutableList.<Types.NestedField>builder()
+              
.addAll(TrackedFile.schemaWithContentStats(Types.StructType.of()).fields())
+              .add(MetadataColumns.ROW_POSITION)
+              .build());
+
+  private FileContent contentType = FileContent.DATA;
+  private String location = null;
+  private FileFormat fileFormat = null;
+  private long recordCount = 0L;
+  private long fileSizeInBytes = 0L;
+  private Integer specId = null;
+
+  // optional fields
+  private TrackingStruct tracking = null;
+  private ContentStats contentStats = null;
+  private Integer sortOrderId = null;
+  private DeletionVectorStruct deletionVector = null;
+  private ManifestInfoStruct manifestInfo = null;
+  private byte[] keyMetadata = null;
+  private long[] splitOffsets = null;
+  private int[] equalityIds = null;
+
+  // not serialized to manifest, set by manifest readers
+  private transient TrackedFile manifestContext = null;

Review Comment:
   `TrackedFile` can be a manifest file or data file entry. But the manifest 
context probably should be a more specific `ManifestFile` type?



##########
core/src/main/java/org/apache/iceberg/TrackingStruct.java:
##########
@@ -0,0 +1,216 @@
+/*
+ * 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.nio.ByteBuffer;
+import java.util.Arrays;
+import org.apache.iceberg.avro.SupportsIndexProjection;
+import org.apache.iceberg.relocated.com.google.common.base.MoreObjects;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.util.ByteBuffers;
+
+/** Mutable {@link StructLike} implementation of {@link Tracking}. */
+class TrackingStruct extends SupportsIndexProjection implements Tracking {
+  private static final Types.StructType BASE_TYPE =
+      Types.StructType.of(
+          Tracking.STATUS,
+          Tracking.SNAPSHOT_ID,
+          Tracking.SEQUENCE_NUMBER,
+          Tracking.FILE_SEQUENCE_NUMBER,
+          Tracking.DV_SNAPSHOT_ID,
+          Tracking.FIRST_ROW_ID,
+          Tracking.DELETED_POSITIONS,
+          Tracking.REPLACED_POSITIONS);
+
+  private EntryStatus status = null;
+  private Long snapshotId = null;
+  private Long sequenceNumber = null;

Review Comment:
   nit: calling this `dataSequenceNumber`? the getter is already changed to the 
new name.



##########
core/src/main/java/org/apache/iceberg/TrackedFileStruct.java:
##########
@@ -0,0 +1,354 @@
+/*
+ * 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.io.Serializable;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.iceberg.avro.SupportsIndexProjection;
+import org.apache.iceberg.relocated.com.google.common.base.MoreObjects;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.util.ArrayUtil;
+import org.apache.iceberg.util.ByteBuffers;
+
+/** Mutable {@link StructLike} implementation of {@link TrackedFile}. */
+class TrackedFileStruct extends SupportsIndexProjection implements 
TrackedFile, Serializable {
+  private static final Types.StructType EMPTY_STRUCT_TYPE = 
Types.StructType.of();
+
+  private static final Types.StructType BASE_TYPE =
+      Types.StructType.of(
+          TrackedFile.TRACKING,
+          TrackedFile.CONTENT_TYPE,
+          TrackedFile.LOCATION,
+          TrackedFile.FILE_FORMAT,
+          TrackedFile.RECORD_COUNT,
+          TrackedFile.FILE_SIZE_IN_BYTES,
+          TrackedFile.SPEC_ID,
+          Types.NestedField.optional(
+              TrackedFile.CONTENT_STATS_ID,
+              TrackedFile.CONTENT_STATS_NAME,
+              EMPTY_STRUCT_TYPE,
+              TrackedFile.CONTENT_STATS_DOC),
+          TrackedFile.SORT_ORDER_ID,
+          TrackedFile.DELETION_VECTOR,
+          TrackedFile.MANIFEST_INFO,
+          TrackedFile.KEY_METADATA,
+          TrackedFile.SPLIT_OFFSETS,
+          TrackedFile.EQUALITY_IDS,
+          MetadataColumns.ROW_POSITION);
+
+  private FileContent contentType = null;
+  private String location = null;
+  private FileFormat fileFormat = null;
+  private long recordCount = -1L;
+  private long fileSizeInBytes = -1L;
+  private Integer specId = null;
+
+  // optional fields
+  private Tracking tracking = null;
+  private ContentStats contentStats = null;
+  private Integer sortOrderId = null;
+  private DeletionVector deletionVector = null;
+  private ManifestInfo manifestInfo = null;
+  private byte[] keyMetadata = null;
+  private long[] splitOffsets = null;
+  private int[] equalityIds = null;
+
+  // not serialized to manifest, set by manifest readers
+  private transient TrackedFile manifestContext = null;
+  private long manifestPos = 0L;
+
+  /** Used by internal readers to instantiate this class with a projection 
schema. */
+  TrackedFileStruct(Types.StructType projection) {
+    super(BASE_TYPE, projection);
+  }
+
+  /** No-projection constructor for direct construction. */
+  TrackedFileStruct() {
+    super(BASE_TYPE.fields().size());
+  }
+
+  /** Copy constructor. */
+  private TrackedFileStruct(TrackedFileStruct toCopy, boolean withStats, 
Set<Integer> statsIds) {
+    super(toCopy);
+    this.contentType = toCopy.contentType;
+    this.location = toCopy.location;
+    this.fileFormat = toCopy.fileFormat;
+    this.recordCount = toCopy.recordCount;
+    this.fileSizeInBytes = toCopy.fileSizeInBytes;
+    this.specId = toCopy.specId;
+
+    this.manifestContext = toCopy.manifestContext;
+    this.manifestPos = toCopy.manifestPos;
+
+    this.tracking = toCopy.tracking != null ? ((TrackingStruct) 
toCopy.tracking).copy() : null;
+    if (tracking != null && manifestContext != null) {
+      ((TrackingStruct) tracking).setManifestContext(manifestContext);
+    }
+
+    this.sortOrderId = toCopy.sortOrderId;
+    this.deletionVector =
+        toCopy.deletionVector != null
+            ? ((DeletionVectorStruct) toCopy.deletionVector).copy()
+            : null;
+
+    if (withStats && toCopy.contentStats != null) {
+      ContentStats filtered = BaseContentStats.buildFrom(toCopy.contentStats, 
statsIds).build();
+      this.contentStats = filtered.fieldStats().isEmpty() ? null : filtered;
+    } else {
+      this.contentStats = null;
+    }
+
+    this.manifestInfo =
+        toCopy.manifestInfo != null ? ((ManifestInfoStruct) 
toCopy.manifestInfo).copy() : null;
+    this.keyMetadata =
+        toCopy.keyMetadata != null
+            ? Arrays.copyOf(toCopy.keyMetadata, toCopy.keyMetadata.length)
+            : null;
+    this.splitOffsets =
+        toCopy.splitOffsets != null
+            ? Arrays.copyOf(toCopy.splitOffsets, toCopy.splitOffsets.length)
+            : null;
+    this.equalityIds =
+        toCopy.equalityIds != null
+            ? Arrays.copyOf(toCopy.equalityIds, toCopy.equalityIds.length)
+            : null;
+  }
+
+  void setManifestContext(TrackedFile manifest) {
+    this.manifestContext = manifest;
+    if (tracking != null) {
+      ((TrackingStruct) tracking).setManifestContext(manifest);
+    }
+  }
+
+  @Override
+  public Tracking tracking() {
+    return tracking;
+  }
+
+  @Override
+  public FileContent contentType() {
+    return contentType;
+  }
+
+  @Override
+  public String location() {
+    return location;
+  }
+
+  @Override
+  public FileFormat fileFormat() {
+    return fileFormat;
+  }
+
+  @Override
+  public long recordCount() {
+    return recordCount;
+  }
+
+  @Override
+  public long fileSizeInBytes() {
+    return fileSizeInBytes;
+  }
+
+  @Override
+  public Integer specId() {
+    return specId;
+  }
+
+  @Override
+  public ContentStats contentStats() {
+    return contentStats;
+  }
+
+  @Override
+  public Integer sortOrderId() {
+    return sortOrderId;
+  }
+
+  @Override
+  public DeletionVector deletionVector() {
+    return deletionVector;
+  }
+
+  @Override
+  public ManifestInfo manifestInfo() {
+    return manifestInfo;
+  }
+
+  @Override
+  public ByteBuffer keyMetadata() {
+    return keyMetadata != null ? ByteBuffer.wrap(keyMetadata) : null;
+  }
+
+  @Override
+  public List<Long> splitOffsets() {
+    return splitOffsets != null ? 
ArrayUtil.toUnmodifiableLongList(splitOffsets) : null;
+  }
+
+  @Override
+  public List<Integer> equalityIds() {
+    return equalityIds != null ? ArrayUtil.toUnmodifiableIntList(equalityIds) 
: null;
+  }
+
+  @Override
+  public TrackedFile copy() {
+    return new TrackedFileStruct(this, true, null);
+  }
+
+  @Override
+  public TrackedFile copyWithStats(Set<Integer> requestedColumnIds) {
+    return new TrackedFileStruct(this, true, requestedColumnIds);
+  }
+
+  @Override
+  public String manifestLocation() {
+    if (tracking == null) {
+      return null;
+    }
+
+    return manifestContext != null ? manifestContext.location() : null;
+  }
+
+  @Override
+  public long manifestPos() {
+    return manifestPos;
+  }
+
+  @Override
+  protected <T> T internalGet(int pos, Class<T> javaClass) {
+    return javaClass.cast(getByPos(pos));
+  }
+
+  private Object getByPos(int pos) {
+    switch (pos) {
+      case 0:
+        return tracking;
+      case 1:
+        return contentType.id();
+      case 2:
+        return location;
+      case 3:
+        return fileFormat != null ? fileFormat.toString() : null;
+      case 4:
+        return recordCount;
+      case 5:
+        return fileSizeInBytes;
+      case 6:
+        return specId;
+      case 7:
+        return contentStats;
+      case 8:
+        return sortOrderId;
+      case 9:
+        return deletionVector;
+      case 10:
+        return manifestInfo;
+      case 11:
+        return keyMetadata();
+      case 12:
+        return splitOffsets();
+      case 13:
+        return equalityIds();
+      case 14:
+        return manifestPos;
+      default:
+        throw new UnsupportedOperationException("Unknown field ordinal: " + 
pos);
+    }
+  }
+
+  @Override
+  protected <T> void internalSet(int pos, T value) {
+    switch (pos) {
+      case 0:
+        this.tracking = (Tracking) value;
+        ((TrackingStruct) tracking).setManifestContext(manifestContext);

Review Comment:
   this type cast is a little suspicious. should we add the setter to the 
interface?



-- 
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]


Reply via email to