ajantha-bhat commented on code in PR #5067: URL: https://github.com/apache/iceberg/pull/5067#discussion_r899811526
########## api/src/test/java/org/apache/iceberg/TestAccessors.java: ########## @@ -0,0 +1,174 @@ +/* + * Licensed 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.math.BigDecimal; +import java.nio.ByteBuffer; +import java.util.UUID; +import org.apache.iceberg.TestHelpers.Row; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.types.Type; +import org.apache.iceberg.types.Types; +import org.junit.Assert; +import org.junit.Test; + +import static org.apache.iceberg.types.Types.NestedField.optional; +import static org.apache.iceberg.types.Types.NestedField.required; + +public class TestAccessors { + + private static Accessor<StructLike> direct(Type type) { + Schema schema = new Schema(required(17, "field_" + type.typeId(), type)); + return schema.accessorForField(17); + } + + private static Accessor<StructLike> nested1(Type type) { + Schema schema = new Schema(required(11, "struct1", Types.StructType.of( + Types.NestedField.required(17, "field_" + type.typeId(), type)))); + return schema.accessorForField(17); + } + + private static Accessor<StructLike> nested2(Type type) { + Schema schema = new Schema(required(11, "s", Types.StructType.of( + Types.NestedField.required(22, "s2", Types.StructType.of( + Types.NestedField.required(17, "field_" + type.typeId(), type)))))); + return schema.accessorForField(17); + } + + private static Accessor<StructLike> nested3(Type type) { + Schema schema = new Schema(required(11, "s", Types.StructType.of( + Types.NestedField.required(22, "s2", Types.StructType.of( + Types.NestedField.required(33, "s3", Types.StructType.of( + Types.NestedField.required(17, "field_" + type.typeId(), type)))))))); + return schema.accessorForField(17); + } + + private static Accessor<StructLike> nested3optional(Type type) { + Schema schema = new Schema(optional(11, "s", Types.StructType.of( + Types.NestedField.optional(22, "s2", Types.StructType.of( + Types.NestedField.optional(33, "s3", Types.StructType.of( + Types.NestedField.optional(17, "field_" + type.typeId(), type)))))))); + return schema.accessorForField(17); + } + + private static Accessor<StructLike> nested4(Type type) { + Schema schema = new Schema(required(11, "s", Types.StructType.of( + Types.NestedField.required(22, "s2", Types.StructType.of( + Types.NestedField.required(33, "s3", Types.StructType.of( + Types.NestedField.required(44, "s4", Types.StructType.of( + Types.NestedField.required(17, "field_" + type.typeId(), type)))))))))); + return schema.accessorForField(17); + } + + private void assertAccessorReturns(Type type, Object value) { + Assert.assertEquals(value, direct(type).get(Row.of(value))); + + Assert.assertEquals(value, nested1(type).get(Row.of(Row.of(value)))); + Assert.assertEquals(value, nested2(type).get(Row.of(Row.of(Row.of(value))))); + Assert.assertEquals(value, nested3(type).get(Row.of(Row.of(Row.of(Row.of(value)))))); + Assert.assertEquals(value, nested4(type).get(Row.of(Row.of(Row.of(Row.of(Row.of(value))))))); + + Assert.assertEquals(value, nested3optional(type).get(Row.of(Row.of(Row.of(Row.of(value)))))); Review Comment: Only covers struct type. We can cover map and list type also? -- 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]
