amogh-jahagirdar commented on code in PR #18252: URL: https://github.com/apache/iceberg/pull/18252#discussion_r4526790528
########## 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 { Review Comment: Ideally, we'd be able to test the behavior of file types in the existing `testTypes()` suite in `TestTypeUtil` itself, since that's where we cover every other type for project, getProjectedIds, indexById, indexNameById and reassignDoc. The ID assignment tests there assume the type stays the same when the field's ID changes, which isn't true for a file, but the other five keep it at ID 1, so `FileType.of(1)` should work. Could we add a second method source with `testTypes()` plus `FileType.of(1)` for those five? I think getProjectedIds in particular is worth covering, since a file goes through the struct fallback there. ########## 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 think this needs to assert photo's ID directly. All of the photo assertions are relative to `photo.fieldId()`, so if we regressed and reused the base ID 2, they'd still pass (`highestFieldId` would be 2 + 6 = 8). Only the `indexById` size check would catch it. Could we add `assertThat(photo.fieldId()).isEqualTo(4)`? ########## 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() { Review Comment: Minor: could we assert `FILE.hashCode()` equals `FileType.of(5).hashCode()` instead, and drop the `hashCode` check in `isNotEqualToAStructWithTheSameFields`? (The `hashCode` contract requires equal objects to have the same hash, but unequal objects can collide.) ########## 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() { Review Comment: Could we add a case with a file inside a nested struct (e.g. `meta: struct<photo: file>`)? With a base schema, `AssignFreshIds` looks up the base field by full name and `ReassignIds` looks it up in the matching source struct, so I think it's worth checking that both reuse and rebuild the file the same way as a top-level column. ########## 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: Could we also cover a list element or map value reusing its base ID when there's a base schema? `freshIdsReuseBaseSchemaIdsWhenTheBaseColumnIsAlsoAFile` only covers a top-level column, but for `photos.element` the base field is found by its full name, so I think it's worth checking that it reuses the base ID and gets rebuilt from it the same way. -- 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]
