szehon-ho commented on a change in pull request #4120: URL: https://github.com/apache/iceberg/pull/4120#discussion_r807563588
########## File path: core/src/test/java/org/apache/iceberg/avro/TestBuildAvroProjection.java ########## @@ -0,0 +1,244 @@ +/* + * 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.avro; + +import java.util.Collections; +import java.util.function.Supplier; +import org.apache.avro.SchemaBuilder; +import org.apache.iceberg.types.Type; +import org.apache.iceberg.types.Types; +import org.junit.Test; + +import static org.apache.iceberg.types.Types.NestedField.optional; +import static org.junit.Assert.assertEquals; + +public class TestBuildAvroProjection { + + @Test + public void projectArrayWithElementSchemaUnchanged() { + + final Type icebergType = Types.ListType.ofRequired(0, + Types.StructType.of( + optional(1, "int1", Types.IntegerType.get()), + optional(2, "string1", Types.StringType.get()) + ) + ); + + final org.apache.avro.Schema expected = SchemaBuilder.array().prop(AvroSchemaUtil.ELEMENT_ID_PROP, "0") + .items( + SchemaBuilder.record("elem").namespace("unit.test").fields() + .name("int1").prop(AvroSchemaUtil.FIELD_ID_PROP, "1").type().nullable().intType().noDefault() + .name("string1").prop(AvroSchemaUtil.FIELD_ID_PROP, "2").type().nullable().stringType().noDefault() + .endRecord()); + + final BuildAvroProjection testSubject = new BuildAvroProjection(icebergType, Collections.emptyMap()); + + final Supplier<org.apache.avro.Schema> supplier = expected::getElementType; + + final org.apache.avro.Schema actual = testSubject.array(expected, supplier); + + assertEquals(expected, actual); + assertEquals(0, Integer.valueOf(actual.getProp(AvroSchemaUtil.ELEMENT_ID_PROP)).intValue()); + } + + @Test + public void projectArrayWithExtraFieldInElementSchema() { + + final Type icebergType = Types.ListType.ofRequired(0, + Types.StructType.of( + optional(1, "int1", Types.IntegerType.get()), + optional(2, "string1", Types.StringType.get()) + ) + ); + + final org.apache.avro.Schema extraField = SchemaBuilder.array().prop(AvroSchemaUtil.ELEMENT_ID_PROP, "0") + .items( + SchemaBuilder.record("elem").namespace("unit.test").fields() + .name("int1").prop(AvroSchemaUtil.FIELD_ID_PROP, "1").type().nullable().intType().noDefault() + .name("string1").prop(AvroSchemaUtil.FIELD_ID_PROP, "2").type().nullable().stringType().noDefault() + .name("float1").prop(AvroSchemaUtil.FIELD_ID_PROP, "3").type().nullable().floatType().noDefault() + .endRecord()); + + // once projected onto iceberg schema, the avro schema will lose the extra float field + final org.apache.avro.Schema expected = SchemaBuilder.array().prop(AvroSchemaUtil.ELEMENT_ID_PROP, "0") + .items( + SchemaBuilder.record("elem").namespace("unit.test").fields() + .name("int1").prop(AvroSchemaUtil.FIELD_ID_PROP, "1").type().nullable().intType().noDefault() + .name("string1").prop(AvroSchemaUtil.FIELD_ID_PROP, "2").type().nullable().stringType().noDefault() + .endRecord()); + + final BuildAvroProjection testSubject = new BuildAvroProjection(icebergType, Collections.emptyMap()); + + final Supplier<org.apache.avro.Schema> supplier = expected::getElementType; + + final org.apache.avro.Schema actual = testSubject.array(extraField, supplier); + + assertEquals(expected, actual); + assertEquals(0, Integer.valueOf(actual.getProp(AvroSchemaUtil.ELEMENT_ID_PROP)).intValue()); + } + + @Test + public void projectArrayWithLessFieldInElementSchema() { + + final Type icebergType = Types.ListType.ofRequired(0, + Types.StructType.of( + optional(1, "int1", Types.IntegerType.get()), + optional(2, "string1", Types.StringType.get()) + ) + ); + + final org.apache.avro.Schema lessField = SchemaBuilder.array().prop(AvroSchemaUtil.ELEMENT_ID_PROP, "0") + .items( + SchemaBuilder.record("elem").namespace("unit.test").fields() + .name("int1").prop(AvroSchemaUtil.FIELD_ID_PROP, "1").type().nullable().intType().noDefault() + .endRecord()); + + // once projected onto iceberg schema, the avro schema will have an extra string column + final org.apache.avro.Schema expected = SchemaBuilder.array().prop(AvroSchemaUtil.ELEMENT_ID_PROP, "0") + .items( + SchemaBuilder.record("elem").namespace("unit.test").fields() + .name("int1").prop(AvroSchemaUtil.FIELD_ID_PROP, "1").type().nullable().intType().noDefault() + .name("string1_r").prop(AvroSchemaUtil.FIELD_ID_PROP, "2").type().nullable().stringType().noDefault() + .endRecord()); + + final BuildAvroProjection testSubject = new BuildAvroProjection(icebergType, Collections.emptyMap()); + + final Supplier<org.apache.avro.Schema> supplier = expected::getElementType; + + final org.apache.avro.Schema actual = testSubject.array(lessField, supplier); + + assertEquals(expected, actual); + assertEquals(0, Integer.valueOf(actual.getProp(AvroSchemaUtil.ELEMENT_ID_PROP)).intValue()); + } + + @Test + public void projectMapWithValueSchemaUnchanged() { + + final Type icebergType = Types.MapType.ofRequired(0, 1, + Types.StringType.get(), + Types.StructType.of( + optional(2, "int1", Types.IntegerType.get()), + optional(3, "string1", Types.StringType.get()) + ) + ); + + final org.apache.avro.Schema expected = SchemaBuilder.map() + .prop(AvroSchemaUtil.KEY_ID_PROP, "0") + .prop(AvroSchemaUtil.VALUE_ID_PROP, "1") + .values( + SchemaBuilder.record("value").namespace("unit.test").fields() + .name("int1").prop(AvroSchemaUtil.FIELD_ID_PROP, "1").type().nullable().intType().noDefault() + .name("string1").prop(AvroSchemaUtil.FIELD_ID_PROP, "2").type().nullable().stringType().noDefault() + .endRecord()); + + final BuildAvroProjection testSubject = new BuildAvroProjection(icebergType, Collections.emptyMap()); + + final Supplier<org.apache.avro.Schema> supplier = expected::getValueType; + + final org.apache.avro.Schema actual = testSubject.map(expected, supplier); + + assertEquals(expected, actual); + assertEquals(0, Integer.valueOf(actual.getProp(AvroSchemaUtil.KEY_ID_PROP)).intValue()); + assertEquals(1, Integer.valueOf(actual.getProp(AvroSchemaUtil.VALUE_ID_PROP)).intValue()); + } + + @Test + public void projectMapWithExtraFieldInValueSchema() { + + final Type icebergType = Types.MapType.ofRequired(0, 1, + Types.StringType.get(), + Types.StructType.of( + optional(2, "int1", Types.IntegerType.get()), + optional(3, "string1", Types.StringType.get()) + ) + ); + + final org.apache.avro.Schema extraField = SchemaBuilder.map() + .prop(AvroSchemaUtil.KEY_ID_PROP, "0") + .prop(AvroSchemaUtil.VALUE_ID_PROP, "1") + .values( + SchemaBuilder.record("value").namespace("unit.test").fields() + .name("int1").prop(AvroSchemaUtil.FIELD_ID_PROP, "1").type().nullable().intType().noDefault() + .name("string1").prop(AvroSchemaUtil.FIELD_ID_PROP, "2").type().nullable().stringType().noDefault() + .name("float1").prop(AvroSchemaUtil.FIELD_ID_PROP, "3").type().nullable().floatType().noDefault() + .endRecord()); + + // once projected onto iceberg schema, the avro schema will lose the extra float field + final org.apache.avro.Schema expected = SchemaBuilder.map() + .prop(AvroSchemaUtil.KEY_ID_PROP, "0") + .prop(AvroSchemaUtil.VALUE_ID_PROP, "1") + .values( + SchemaBuilder.record("value").namespace("unit.test").fields() + .name("int1").prop(AvroSchemaUtil.FIELD_ID_PROP, "1").type().nullable().intType().noDefault() + .name("string1").prop(AvroSchemaUtil.FIELD_ID_PROP, "2").type().nullable().stringType().noDefault() + .endRecord()); + + final BuildAvroProjection testSubject = new BuildAvroProjection(icebergType, Collections.emptyMap()); + + final Supplier<org.apache.avro.Schema> supplier = expected::getValueType; + + final org.apache.avro.Schema actual = testSubject.map(extraField, supplier); + + assertEquals(expected, actual); + assertEquals(0, Integer.valueOf(actual.getProp(AvroSchemaUtil.KEY_ID_PROP)).intValue()); Review comment: I think most of the Iceberg unit test use asserts with a message so it's slightly more clear what fails. ########## File path: core/src/main/java/org/apache/iceberg/avro/MissingIds.java ########## @@ -0,0 +1,80 @@ +/* + * 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.avro; + +import java.util.List; +import java.util.function.Supplier; +import org.apache.avro.Schema; + +/** + * Returns true once the first node is found with ID property missing. Reverse of {@link HasIds} + * <p> + * Note: To use {@link AvroSchemaUtil#toIceberg(Schema)} on an avro schema, the avro schema need to be either + * have IDs on every node or not have IDs at all. Invoke {@link AvroSchemaUtil#hasIds(Schema)} only proves + * that the schema has at least one ID, and not sufficient condition for invoking + * {@link AvroSchemaUtil#toIceberg(Schema)} on the schema. + */ +class MissingIds extends AvroCustomOrderSchemaVisitor<Boolean, Boolean> { + @Override + public Boolean record(Schema record, List<String> names, Iterable<Boolean> fields) { + for (Boolean field : fields) { + if (field) { + return true; Review comment: I was going to say we should use guava's 'Iterables.any' for these checks, but seems the hasIds class was also this way as well, oh well.. -- 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]
