RussellSpitzer commented on code in PR #18252:
URL: https://github.com/apache/iceberg/pull/18252#discussion_r4541196316


##########
api/src/test/java/org/apache/iceberg/types/TestFileType.java:
##########
@@ -0,0 +1,254 @@
+/*
+ * 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.types;
+
+import static org.apache.iceberg.types.Types.NestedField.optional;
+import static org.apache.iceberg.types.Types.NestedField.required;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.iceberg.Schema;
+import org.junit.jupiter.api.Test;
+
+class TestFileType {
+  private static final Types.FileType FILE = Types.FileType.of(5);
+
+  @Test
+  void nestedFieldsAreDerivedFromTheBaseId() {
+    assertThat(FILE.fields())
+        .containsExactly(
+            optional(6, "uri", Types.StringType.get()),
+            optional(7, "offset", Types.LongType.get()),
+            optional(8, "size", Types.LongType.get()),
+            optional(9, "content_type", Types.StringType.get()),
+            optional(10, "checksum", Types.StringType.get()),
+            optional(11, "inline", Types.BinaryType.get()));
+    assertThat(FILE.baseId()).isEqualTo(5);
+    
assertThat(Types.FileType.NUM_NESTED_FIELDS).isEqualTo(FILE.fields().size());
+  }
+
+  @Test
+  void findsNestedFieldsById() {
+    assertThat(FILE.field(6)).isEqualTo(FILE.fields().get(0));
+    assertThat(FILE.field(11)).isEqualTo(FILE.fields().get(5));
+
+    assertThat(FILE.field(5)).isNull();
+    assertThat(FILE.field(12)).isNull();
+  }
+
+  @Test
+  void findsNestedFieldsByName() {
+    assertThat(FILE.field("content_type")).isEqualTo(FILE.fields().get(3));
+    
assertThat(FILE.caseInsensitiveField("Content_Type")).isEqualTo(FILE.fields().get(3));
+    
assertThat(FILE.fieldType("content_type")).isEqualTo(Types.StringType.get());
+
+    assertThat(FILE.field("missing")).isNull();
+    assertThat(FILE.caseInsensitiveField("missing")).isNull();
+    assertThat(FILE.fieldType("missing")).isNull();
+  }
+
+  @Test
+  void isDistinguishableFromAStruct() {
+    assertThat(FILE.isFileType()).isTrue();
+    assertThat(FILE.asFileType()).isSameAs(FILE);
+
+    Types.StructType struct = Types.StructType.of(FILE.fields());
+    assertThat(struct.isFileType()).isFalse();
+    assertThatThrownBy(struct::asFileType)
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessageStartingWith("Not a file type:");
+  }
+
+  @Test
+  void toStringIsFile() {
+    assertThat(FILE).hasToString("file");
+    assertThat(optional(5, "photo", FILE)).hasToString("5: photo: optional 
file");
+  }
+
+  @Test
+  void isNotEqualToAStructWithTheSameFields() {
+    Types.StructType struct = Types.StructType.of(FILE.fields());
+
+    assertThat(FILE).isNotEqualTo(struct);
+    assertThat(struct).isNotEqualTo(FILE);
+    assertThat(FILE.hashCode()).isNotEqualTo(struct.hashCode());
+  }
+
+  @Test
+  void isNotEqualToAFileWithDifferentBaseId() {
+    
assertThat(FILE).isEqualTo(Types.FileType.of(5)).isNotEqualTo(Types.FileType.of(12));
+    assertThat(FILE.hashCode()).isNotEqualTo(Types.FileType.of(12).hashCode());
+  }
+
+  @Test
+  void freshIdsReserveTheNestedIdBlock() {
+    Schema schema =
+        new Schema(
+            required(1, "id", Types.LongType.get()),
+            optional(2, "photo", Types.FileType.of(2)),
+            optional(9, "data", Types.StringType.get()));
+
+    Schema assigned = TypeUtil.assignFreshIds(schema, new 
AtomicInteger(0)::incrementAndGet);
+
+    assertThat(assigned.findField("id").fieldId()).isEqualTo(1);
+    assertThat(assigned.findField("photo").fieldId()).isEqualTo(2);
+    
assertThat(assigned.findField("photo").type()).isEqualTo(Types.FileType.of(2));
+    assertThat(assigned.findField("photo.uri").fieldId()).isEqualTo(3);
+    assertThat(assigned.findField("photo.inline").fieldId()).isEqualTo(8);
+    assertThat(assigned.findField("data").fieldId()).isEqualTo(9);
+    assertThat(assigned.highestFieldId()).isEqualTo(9);
+  }
+
+  @Test
+  void freshIdsHandleAdjacentFileColumns() {
+    Schema schema =
+        new Schema(
+            optional(1, "photo", Types.FileType.of(1)),
+            optional(8, "thumbnail", Types.FileType.of(8)));
+
+    Schema assigned = TypeUtil.assignFreshIds(schema, new 
AtomicInteger(0)::incrementAndGet);
+
+    
assertThat(assigned.findField("photo").type()).isEqualTo(Types.FileType.of(1));
+    
assertThat(assigned.findField("thumbnail").type()).isEqualTo(Types.FileType.of(8));
+    assertThat(assigned.highestFieldId()).isEqualTo(14);
+    assertThat(TypeUtil.indexById(assigned.asStruct())).hasSize(14);
+  }
+
+  @Test
+  void freshIdsReuseBaseSchemaIdsWhenTheBaseColumnIsAlsoAFile() {
+    Schema base =
+        new Schema(
+            required(1, "id", Types.LongType.get()), optional(2, "photo", 
Types.FileType.of(2)));
+    Schema updated =
+        new Schema(
+            required(11, "id", Types.LongType.get()),
+            optional(12, "photo", Types.FileType.of(12)),
+            optional(19, "data", Types.StringType.get()));
+
+    Schema assigned = TypeUtil.assignFreshIds(updated, base, new 
AtomicInteger(8)::incrementAndGet);
+
+    assertThat(assigned.findField("id").fieldId()).isEqualTo(1);
+    assertThat(assigned.findField("photo").fieldId()).isEqualTo(2);
+    assertThat(assigned.findField("photo.uri").fieldId()).isEqualTo(3);
+    assertThat(assigned.findField("photo.inline").fieldId()).isEqualTo(8);
+    assertThat(assigned.findField("data").fieldId()).isEqualTo(9);
+  }
+
+  @Test
+  void freshIdsReserveANewBlockWhenABaseColumnBecomesAFile() {

Review Comment:
   I switched a lot of these checks to just check the whole file as struct so 
we just are comparing everything.



##########
api/src/test/java/org/apache/iceberg/types/TestFileType.java:
##########
@@ -0,0 +1,254 @@
+/*
+ * 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.types;
+
+import static org.apache.iceberg.types.Types.NestedField.optional;
+import static org.apache.iceberg.types.Types.NestedField.required;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.iceberg.Schema;
+import org.junit.jupiter.api.Test;
+
+class TestFileType {
+  private static final Types.FileType FILE = Types.FileType.of(5);
+
+  @Test
+  void nestedFieldsAreDerivedFromTheBaseId() {
+    assertThat(FILE.fields())
+        .containsExactly(
+            optional(6, "uri", Types.StringType.get()),
+            optional(7, "offset", Types.LongType.get()),
+            optional(8, "size", Types.LongType.get()),
+            optional(9, "content_type", Types.StringType.get()),
+            optional(10, "checksum", Types.StringType.get()),
+            optional(11, "inline", Types.BinaryType.get()));
+    assertThat(FILE.baseId()).isEqualTo(5);
+    
assertThat(Types.FileType.NUM_NESTED_FIELDS).isEqualTo(FILE.fields().size());
+  }
+
+  @Test
+  void findsNestedFieldsById() {
+    assertThat(FILE.field(6)).isEqualTo(FILE.fields().get(0));
+    assertThat(FILE.field(11)).isEqualTo(FILE.fields().get(5));
+
+    assertThat(FILE.field(5)).isNull();
+    assertThat(FILE.field(12)).isNull();
+  }
+
+  @Test
+  void findsNestedFieldsByName() {
+    assertThat(FILE.field("content_type")).isEqualTo(FILE.fields().get(3));
+    
assertThat(FILE.caseInsensitiveField("Content_Type")).isEqualTo(FILE.fields().get(3));
+    
assertThat(FILE.fieldType("content_type")).isEqualTo(Types.StringType.get());
+
+    assertThat(FILE.field("missing")).isNull();
+    assertThat(FILE.caseInsensitiveField("missing")).isNull();
+    assertThat(FILE.fieldType("missing")).isNull();
+  }
+
+  @Test
+  void isDistinguishableFromAStruct() {
+    assertThat(FILE.isFileType()).isTrue();
+    assertThat(FILE.asFileType()).isSameAs(FILE);
+
+    Types.StructType struct = Types.StructType.of(FILE.fields());
+    assertThat(struct.isFileType()).isFalse();
+    assertThatThrownBy(struct::asFileType)
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessageStartingWith("Not a file type:");
+  }
+
+  @Test
+  void toStringIsFile() {
+    assertThat(FILE).hasToString("file");
+    assertThat(optional(5, "photo", FILE)).hasToString("5: photo: optional 
file");
+  }
+
+  @Test
+  void isNotEqualToAStructWithTheSameFields() {
+    Types.StructType struct = Types.StructType.of(FILE.fields());
+
+    assertThat(FILE).isNotEqualTo(struct);
+    assertThat(struct).isNotEqualTo(FILE);
+    assertThat(FILE.hashCode()).isNotEqualTo(struct.hashCode());
+  }
+
+  @Test
+  void isNotEqualToAFileWithDifferentBaseId() {
+    
assertThat(FILE).isEqualTo(Types.FileType.of(5)).isNotEqualTo(Types.FileType.of(12));
+    assertThat(FILE.hashCode()).isNotEqualTo(Types.FileType.of(12).hashCode());
+  }
+
+  @Test
+  void freshIdsReserveTheNestedIdBlock() {
+    Schema schema =
+        new Schema(
+            required(1, "id", Types.LongType.get()),
+            optional(2, "photo", Types.FileType.of(2)),
+            optional(9, "data", Types.StringType.get()));
+
+    Schema assigned = TypeUtil.assignFreshIds(schema, new 
AtomicInteger(0)::incrementAndGet);
+
+    assertThat(assigned.findField("id").fieldId()).isEqualTo(1);
+    assertThat(assigned.findField("photo").fieldId()).isEqualTo(2);
+    
assertThat(assigned.findField("photo").type()).isEqualTo(Types.FileType.of(2));
+    assertThat(assigned.findField("photo.uri").fieldId()).isEqualTo(3);
+    assertThat(assigned.findField("photo.inline").fieldId()).isEqualTo(8);
+    assertThat(assigned.findField("data").fieldId()).isEqualTo(9);
+    assertThat(assigned.highestFieldId()).isEqualTo(9);
+  }
+
+  @Test
+  void freshIdsHandleAdjacentFileColumns() {
+    Schema schema =
+        new Schema(
+            optional(1, "photo", Types.FileType.of(1)),
+            optional(8, "thumbnail", Types.FileType.of(8)));
+
+    Schema assigned = TypeUtil.assignFreshIds(schema, new 
AtomicInteger(0)::incrementAndGet);
+
+    
assertThat(assigned.findField("photo").type()).isEqualTo(Types.FileType.of(1));
+    
assertThat(assigned.findField("thumbnail").type()).isEqualTo(Types.FileType.of(8));
+    assertThat(assigned.highestFieldId()).isEqualTo(14);
+    assertThat(TypeUtil.indexById(assigned.asStruct())).hasSize(14);
+  }
+
+  @Test
+  void freshIdsReuseBaseSchemaIdsWhenTheBaseColumnIsAlsoAFile() {
+    Schema base =
+        new Schema(
+            required(1, "id", Types.LongType.get()), optional(2, "photo", 
Types.FileType.of(2)));
+    Schema updated =
+        new Schema(
+            required(11, "id", Types.LongType.get()),
+            optional(12, "photo", Types.FileType.of(12)),
+            optional(19, "data", Types.StringType.get()));
+
+    Schema assigned = TypeUtil.assignFreshIds(updated, base, new 
AtomicInteger(8)::incrementAndGet);
+
+    assertThat(assigned.findField("id").fieldId()).isEqualTo(1);
+    assertThat(assigned.findField("photo").fieldId()).isEqualTo(2);
+    assertThat(assigned.findField("photo.uri").fieldId()).isEqualTo(3);
+    assertThat(assigned.findField("photo.inline").fieldId()).isEqualTo(8);
+    assertThat(assigned.findField("data").fieldId()).isEqualTo(9);
+  }
+
+  @Test
+  void freshIdsReserveANewBlockWhenABaseColumnBecomesAFile() {
+    Schema base =
+        new Schema(
+            required(1, "id", Types.LongType.get()),
+            optional(2, "photo", Types.StringType.get()),
+            optional(3, "data", Types.StringType.get()));
+    Schema updated =
+        new Schema(
+            required(11, "id", Types.LongType.get()),
+            optional(12, "photo", Types.FileType.of(12)),
+            optional(19, "data", Types.StringType.get()));
+
+    Schema assigned = TypeUtil.assignFreshIds(updated, base, new 
AtomicInteger(3)::incrementAndGet);
+
+    assertThat(assigned.findField("id").fieldId()).isEqualTo(1);
+    assertThat(assigned.findField("data").fieldId()).isEqualTo(3);
+
+    Types.NestedField photo = assigned.findField("photo");
+    assertThat(photo.type()).isEqualTo(Types.FileType.of(photo.fieldId()));
+    
assertThat(assigned.findField("photo.uri").fieldId()).isEqualTo(photo.fieldId() 
+ 1);
+    assertThat(assigned.highestFieldId())
+        .isEqualTo(photo.fieldId() + Types.FileType.NUM_NESTED_FIELDS);
+    assertThat(TypeUtil.indexById(assigned.asStruct()))
+        .hasSize(updated.columns().size() + Types.FileType.NUM_NESTED_FIELDS);
+  }
+
+  @Test
+  void freshIdsReserveForFilesInListsAndMaps() {

Review Comment:
   done



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