RussellSpitzer commented on a change in pull request #2952:
URL: https://github.com/apache/iceberg/pull/2952#discussion_r696178975



##########
File path: api/src/test/java/org/apache/iceberg/types/TestTypeUtil.java
##########
@@ -103,6 +105,267 @@ public void testAssignIncreasingFreshIdNewIdentifier() {
         Sets.newHashSet(sourceSchema.findField("a").fieldId()), 
actualSchema.identifierFieldIds());
   }
 
+  @Test
+  public void testProject() {
+    Schema schema = new Schema(
+        Lists.newArrayList(
+            required(10, "a", Types.IntegerType.get()),
+            required(11, "A", Types.IntegerType.get()),
+            required(12, "someStruct", Types.StructType.of(
+                required(13, "b", Types.IntegerType.get()),
+                required(14, "B", Types.IntegerType.get()),
+                required(15, "anotherStruct", Types.StructType.of(
+                    required(16, "c", Types.IntegerType.get()),
+                    required(17, "C", Types.IntegerType.get()))
+                )))));
+
+    Schema expectedTop = new Schema(
+        Lists.newArrayList(
+            required(11, "A", Types.IntegerType.get())));
+
+    Schema actualTop = TypeUtil.project(schema, Sets.newHashSet(11));
+    Assert.assertEquals(expectedTop.asStruct(), actualTop.asStruct());
+
+    Schema expectedDepthOne = new Schema(
+        Lists.newArrayList(
+            required(10, "a", Types.IntegerType.get()),
+            required(12, "someStruct", Types.StructType.of(
+                required(13, "b", Types.IntegerType.get())))));
+
+    Schema actualDepthOne = TypeUtil.project(schema, Sets.newHashSet(10, 12, 
13));
+    Assert.assertEquals(expectedDepthOne.asStruct(), 
actualDepthOne.asStruct());
+
+    Schema expectedDepthTwo = new Schema(
+        Lists.newArrayList(
+            required(11, "A", Types.IntegerType.get()),
+            required(12, "someStruct", Types.StructType.of(
+                required(15, "anotherStruct", Types.StructType.of(
+                    required(17, "C", Types.IntegerType.get()))
+                )))));
+
+    Schema actualDepthTwo = TypeUtil.project(schema, Sets.newHashSet(11, 12, 
15, 17));
+    Schema actualDepthTwoChildren = TypeUtil.project(schema, 
Sets.newHashSet(11, 17));
+    Assert.assertEquals(expectedDepthTwo.asStruct(), 
actualDepthTwo.asStruct());
+    Assert.assertEquals(expectedDepthTwo.asStruct(), 
actualDepthTwoChildren.asStruct());
+  }
+
+  @Test
+  public void testProjectNaturallyEmpty() {
+    Schema schema = new Schema(
+        Lists.newArrayList(
+            required(12, "someStruct", Types.StructType.of(
+                required(15, "anotherStruct", Types.StructType.of(
+                    required(20, "empty", Types.StructType.of())
+                ))))));
+
+    Schema expectedDepthOne = new Schema(
+        Lists.newArrayList(
+            required(12, "someStruct", Types.StructType.of())));
+
+    Schema actualDepthOne = TypeUtil.project(schema, Sets.newHashSet(12));
+    Assert.assertEquals(expectedDepthOne.asStruct(), 
actualDepthOne.asStruct());
+
+    Schema expectedDepthTwo = new Schema(
+        Lists.newArrayList(
+            required(12, "someStruct", Types.StructType.of(
+                required(15, "anotherStruct", Types.StructType.of())))));
+
+    Schema actualDepthTwo = TypeUtil.project(schema, Sets.newHashSet(12, 15));
+    Assert.assertEquals(expectedDepthTwo.asStruct(), 
actualDepthTwo.asStruct());
+
+    Schema expectedDepthThree =  new Schema(
+        Lists.newArrayList(
+            required(12, "someStruct", Types.StructType.of(
+                required(15, "anotherStruct", Types.StructType.of(
+                    required(20, "empty", Types.StructType.of())
+                ))))));
+
+    Schema actualDepthThree = TypeUtil.project(schema, Sets.newHashSet(12, 15, 
20));
+    Schema actualDepthThreeChildren = TypeUtil.project(schema, 
Sets.newHashSet(20));
+    Assert.assertEquals(expectedDepthThree.asStruct(), 
actualDepthThree.asStruct());
+    Assert.assertEquals(expectedDepthThree.asStruct(), 
actualDepthThreeChildren.asStruct());
+  }
+
+  @Test
+  public void testProjectEmpty() {
+    Schema schema = new Schema(
+        Lists.newArrayList(
+            required(10, "a", Types.IntegerType.get()),
+            required(11, "A", Types.IntegerType.get()),
+            required(12, "someStruct", Types.StructType.of(
+                required(13, "b", Types.IntegerType.get()),
+                required(14, "B", Types.IntegerType.get()),
+                required(15, "anotherStruct", Types.StructType.of(
+                    required(16, "c", Types.IntegerType.get()),
+                    required(17, "C", Types.IntegerType.get()))
+            )))));
+
+    Schema expectedDepthOne = new Schema(
+        Lists.newArrayList(
+            required(12, "someStruct", Types.StructType.of())));
+
+    Schema actualDepthOne = TypeUtil.project(schema, Sets.newHashSet(12));
+    Assert.assertEquals(expectedDepthOne.asStruct(), 
actualDepthOne.asStruct());
+
+    Schema expectedDepthTwo = new Schema(
+        Lists.newArrayList(
+            required(12, "someStruct", Types.StructType.of(
+                required(15, "anotherStruct", Types.StructType.of())))));
+
+    Schema actualDepthTwo = TypeUtil.project(schema, Sets.newHashSet(12, 15));
+    Assert.assertEquals(expectedDepthTwo.asStruct(), 
actualDepthTwo.asStruct());
+  }
+
+  @Test
+  public void testSelect() {
+    Schema schema = new Schema(
+        Lists.newArrayList(
+            required(10, "a", Types.IntegerType.get()),
+            required(11, "A", Types.IntegerType.get()),
+            required(12, "someStruct", Types.StructType.of(
+                required(13, "b", Types.IntegerType.get()),
+                required(14, "B", Types.IntegerType.get()),
+                required(15, "anotherStruct", Types.StructType.of(
+                    required(16, "c", Types.IntegerType.get()),
+                    required(17, "C", Types.IntegerType.get()))
+                )))));
+
+    Schema expectedTop = new Schema(
+        Lists.newArrayList(
+            required(11, "A", Types.IntegerType.get())));
+
+    Schema actualTop = TypeUtil.select(schema, Sets.newHashSet(11));
+    Assert.assertEquals(expectedTop.asStruct(), actualTop.asStruct());
+
+    Schema expectedDepthOne = new Schema(
+        Lists.newArrayList(
+            required(10, "a", Types.IntegerType.get()),
+            required(12, "someStruct", Types.StructType.of(
+                required(13, "b", Types.IntegerType.get()),
+                required(14, "B", Types.IntegerType.get()),
+                required(15, "anotherStruct", Types.StructType.of(
+                    required(16, "c", Types.IntegerType.get()),
+                    required(17, "C", Types.IntegerType.get())))))));
+
+    Schema actualDepthOne = TypeUtil.select(schema, Sets.newHashSet(10, 12));
+    Assert.assertEquals(expectedDepthOne.asStruct(), 
actualDepthOne.asStruct());
+
+    Schema expectedDepthTwo = new Schema(
+        Lists.newArrayList(
+            required(11, "A", Types.IntegerType.get()),
+            required(12, "someStruct", Types.StructType.of(
+                required(15, "anotherStruct", Types.StructType.of(
+                    required(17, "C", Types.IntegerType.get()))
+                )))));
+
+    Schema actualDepthTwo = TypeUtil.select(schema, Sets.newHashSet(11, 17));
+    Assert.assertEquals(expectedDepthTwo.asStruct(), 
actualDepthTwo.asStruct());
+  }
+
+  @Test
+  public void testProjectMap() {
+    // We can't partially project keys because it changes key equality
+    Schema schema = new Schema(
+        Lists.newArrayList(
+            required(10, "a", Types.IntegerType.get()),
+            required(11, "A", Types.IntegerType.get()),
+            required(12, "map", Types.MapType.ofRequired(13, 14,
+                Types.StructType.of(
+                    optional(100, "x", Types.IntegerType.get()),
+                    optional(101, "y", Types.IntegerType.get())),
+                Types.StructType.of(
+                    required(200, "z", Types.IntegerType.get()),
+                    optional(201, "innerMap", Types.MapType.ofOptional(202, 
203,
+                        Types.IntegerType.get(),
+                        Types.StructType.of(
+                            required(300, "foo", Types.IntegerType.get()),
+                            required(301, "bar", 
Types.IntegerType.get())))))))));
+
+    Assert.assertThrows("Cannot project maps explicitly", 
IllegalArgumentException.class,
+        () -> TypeUtil.project(schema, Sets.newHashSet(12)));
+
+    Assert.assertThrows("Cannot project maps explicitly", 
IllegalArgumentException.class,
+        () -> TypeUtil.project(schema, Sets.newHashSet(201)));
+
+    Schema expectedTopLevel = new Schema(
+        Lists.newArrayList(required(10, "a", Types.IntegerType.get())));
+    Schema actualTopLevel = TypeUtil.project(schema, Sets.newHashSet(10));
+    Assert.assertEquals(expectedTopLevel.asStruct(), 
actualTopLevel.asStruct());
+
+    Schema expectedDepthOne = new Schema(
+        Lists.newArrayList(
+            required(10, "a", Types.IntegerType.get()),
+            required(12, "map", Types.MapType.ofRequired(13, 14,
+                Types.StructType.of(
+                    optional(100, "x", Types.IntegerType.get()),
+                    optional(101, "y", Types.IntegerType.get())),
+                Types.StructType.of()))));
+    Schema actualDepthOne = TypeUtil.project(schema, Sets.newHashSet(10, 13, 
14, 100, 101));
+    Schema actualDepthOneNoKeys = TypeUtil.project(schema, Sets.newHashSet(10, 
13, 14));

Review comment:
       Implicit projection of keys here




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