Govindarajan-D commented on code in PR #15984:
URL: https://github.com/apache/iceberg/pull/15984#discussion_r3203825807


##########
api/src/test/java/org/apache/iceberg/util/TestStructProjection.java:
##########
@@ -0,0 +1,505 @@
+/*
+ * 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.util;
+
+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.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.StructLike;
+import org.apache.iceberg.TestHelpers;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.types.Types.StructType;
+import org.junit.jupiter.api.Test;
+
+public class TestStructProjection {
+  @Test
+  public void projectSubsetOfSchemaFields() {
+    Schema dataSchema =
+        new Schema(
+            required(10, "id", Types.LongType.get()),
+            required(20, "name", Types.StringType.get()),
+            required(30, "value", Types.IntegerType.get()));
+
+    Schema projectedSchema = new Schema(required(30, "value", 
Types.IntegerType.get()));
+
+    StructProjection projection = StructProjection.create(dataSchema, 
projectedSchema);
+
+    assertThat(projection.get(0, Integer.class)).isNull();
+
+    TestHelpers.Row row = TestHelpers.Row.of(1L, "Batman", 42);
+    projection.wrap(row);
+
+    assertThat(projection.size()).isEqualTo(1);
+    assertThat(projection.projectedFields()).isEqualTo(1);
+    assertThat(projection.get(0, Integer.class)).isEqualTo(42);
+  }
+
+  @Test
+  public void getThrowsClassCastExceptionForWrongType() {
+    Schema dataSchema =
+        new Schema(
+            required(10, "id", Types.LongType.get()),
+            required(20, "name", Types.StringType.get()),
+            required(30, "value", Types.IntegerType.get()));
+
+    Schema projectedSchema = new Schema(required(30, "value", 
Types.IntegerType.get()));
+
+    StructProjection projection = StructProjection.create(dataSchema, 
projectedSchema);
+
+    assertThat(projection.get(0, Integer.class)).isNull();
+
+    TestHelpers.Row row = TestHelpers.Row.of(1L, "Batman", 42);
+    projection.wrap(row);
+
+    assertThatThrownBy(() -> projection.get(0, String.class))
+        .isInstanceOf(ClassCastException.class)
+        .hasMessage("Cannot cast java.lang.Integer to java.lang.String");
+  }
+
+  @Test
+  public void getThrowsForOutOfBoundsIndex() {
+    Schema dataSchema =
+        new Schema(
+            required(10, "id", Types.LongType.get()),
+            required(20, "name", Types.StringType.get()),
+            required(30, "value", Types.IntegerType.get()));
+
+    Schema projectedSchema = new Schema(required(30, "value", 
Types.IntegerType.get()));
+
+    StructProjection projection = StructProjection.create(dataSchema, 
projectedSchema);
+
+    TestHelpers.Row row = TestHelpers.Row.of(1L, "Batman", 42);
+    projection.wrap(row);
+
+    assertThatThrownBy(() -> projection.get(1, Integer.class))
+        .isInstanceOf(ArrayIndexOutOfBoundsException.class)
+        .hasMessage("Index 1 out of bounds for length 1");
+  }
+
+  @Test
+  public void setOnProjectionThrows() {
+    Schema dataSchema =
+        new Schema(
+            required(10, "id", Types.LongType.get()),
+            required(20, "name", Types.StringType.get()),
+            required(30, "value", Types.IntegerType.get()));
+
+    Schema projectedSchema = new Schema(required(30, "value", 
Types.IntegerType.get()));
+
+    StructProjection projection = StructProjection.create(dataSchema, 
projectedSchema);
+
+    assertThat(projection.get(0, Integer.class)).isNull();
+
+    TestHelpers.Row row = TestHelpers.Row.of(1L, "Batman", 42);
+    projection.wrap(row);
+
+    assertThatThrownBy(() -> projection.set(0, 5))
+        .isInstanceOf(UnsupportedOperationException.class)
+        .hasMessage("Cannot set fields in a TypeProjection");
+  }
+
+  @Test
+  public void projectNestedStructSubfields() {
+    Schema dataSchema =
+        new Schema(
+            required(1, "id", Types.LongType.get()),
+            required(
+                2,
+                "address",
+                StructType.of(
+                    required(10, "street", Types.StringType.get()),
+                    required(
+                        20,
+                        "coordinates",
+                        StructType.of(
+                            required(100, "latitude", Types.DoubleType.get()),
+                            required(200, "longitude", 
Types.DoubleType.get()))))));
+
+    Schema projectedSchema =
+        new Schema(
+            required(
+                2,
+                "address",
+                StructType.of(
+                    required(
+                        20,
+                        "coordinates",
+                        StructType.of(required(200, "longitude", 
Types.DoubleType.get()))))));
+
+    StructProjection projection = StructProjection.create(dataSchema, 
projectedSchema);
+
+    TestHelpers.Row coordinates = TestHelpers.Row.of(42.00, 24.00);
+    TestHelpers.Row address = TestHelpers.Row.of("221B Baker Street", 
coordinates);
+    TestHelpers.Row row = TestHelpers.Row.of(1L, address);
+
+    projection.wrap(row);
+
+    StructLike addressProjection = projection.get(0, StructLike.class);
+    assertThat(addressProjection).isNotNull();
+    assertThat(addressProjection.size()).isEqualTo(1);
+
+    StructLike coordinatesProjection = addressProjection.get(0, 
StructLike.class);
+    assertThat(coordinatesProjection).isNotNull();
+    assertThat(coordinatesProjection.size()).isEqualTo(1);
+    assertThat(projection.projectedFields()).isEqualTo(1);
+    assertThat(coordinatesProjection.get(0, Double.class)).isEqualTo(24.00);
+  }
+
+  @Test
+  public void createAllowMissingWithAbsentOptionalFieldReturnsNull() {
+    Schema dataSchema = new Schema(required(10, "id", Types.LongType.get()));
+
+    StructType projectedStructType =
+        StructType.of(
+            required(10, "id", Types.LongType.get()),
+            Types.NestedField.optional(20, "name", Types.StringType.get()));
+
+    StructProjection projection =
+        StructProjection.createAllowMissing(dataSchema.asStruct(), 
projectedStructType);
+
+    TestHelpers.Row row = TestHelpers.Row.of(1L);
+    projection.wrap(row);
+
+    assertThat(projection.size()).isEqualTo(2);
+    assertThat(projection.projectedFields()).isEqualTo(1);
+    assertThat(projection.get(0, Long.class)).isEqualTo(1L);
+    assertThat(projection.get(1, String.class)).isNull();
+  }
+
+  @Test
+  public void createThrowsWhenOptionalFieldAbsent() {
+    Schema dataSchema = new Schema(required(10, "id", Types.LongType.get()));
+
+    StructType projectedStructType =
+        StructType.of(
+            required(10, "id", Types.LongType.get()),
+            Types.NestedField.optional(20, "name", Types.StringType.get()));
+
+    assertThatThrownBy(() -> StructProjection.create(dataSchema.asStruct(), 
projectedStructType))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot find field 20: name: optional string in struct<10: 
id: required long>");
+  }
+
+  @Test
+  public void createAllowMissingThrowsWhenRequiredFieldAbsent() {
+    Schema dataSchema = new Schema(required(10, "id", Types.LongType.get()));
+
+    StructType projectedStructType =
+        StructType.of(
+            required(10, "id", Types.LongType.get()), required(20, "name", 
Types.StringType.get()));
+
+    assertThatThrownBy(
+            () -> StructProjection.createAllowMissing(dataSchema.asStruct(), 
projectedStructType))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot find field 20: name: required string in struct<10: 
id: required long>");
+  }
+
+  @Test
+  public void createThrowsWhenRequiredFieldAbsent() {
+    Schema dataSchema = new Schema(required(10, "id", Types.LongType.get()));
+
+    StructType projectedStructType =
+        StructType.of(
+            required(10, "id", Types.LongType.get()), required(20, "name", 
Types.StringType.get()));
+
+    assertThatThrownBy(() -> StructProjection.create(dataSchema.asStruct(), 
projectedStructType))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Cannot find field 20: name: required string in struct<10: 
id: required long>");
+  }
+
+  @Test
+  @SuppressWarnings("unchecked")
+  public void projectMapWithNestedStructAndPrimitiveValues() {
+    StructType coordinatesStruct =
+        StructType.of(
+            required(100, "latitude", Types.DoubleType.get()),
+            required(200, "longitude", Types.DoubleType.get()));
+
+    Schema dataSchema =
+        new Schema(
+            required(1, "id", Types.LongType.get()),
+            required(
+                2,
+                "address",
+                Types.MapType.ofRequired(3, 4, Types.StringType.get(), 
coordinatesStruct)),
+            required(
+                5,
+                "metadata",
+                Types.MapType.ofRequired(6, 7, Types.StringType.get(), 
Types.StringType.get())));
+    Schema projectedSchema =
+        new Schema(
+            required(
+                2,
+                "address",
+                Types.MapType.ofRequired(3, 4, Types.StringType.get(), 
coordinatesStruct)),
+            required(
+                5,
+                "metadata",
+                Types.MapType.ofRequired(6, 7, Types.StringType.get(), 
Types.StringType.get())));

Review Comment:
   But we do have tests that are not exact subset of dataSchema right. So I 
would have to use a mix of select/selectNot with normal definition which would 
not be uniform. Let me know what you think. 



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