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



##########
File path: api/src/main/java/org/apache/iceberg/types/GetEmptyStructIds.java
##########
@@ -0,0 +1,76 @@
+/*
+ * 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 java.util.List;
+import java.util.Set;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.relocated.com.google.common.collect.Sets;
+
+class GetEmptyStructIds extends TypeUtil.SchemaVisitor<Set<Integer>> {
+  private final Set<Integer> fieldIds = Sets.newHashSet();
+
+  @Override
+  public Set<Integer> schema(Schema schema, Set<Integer> structResult) {
+    return fieldIds;
+  }
+
+  @Override
+  public Set<Integer> struct(Types.StructType struct, List<Set<Integer>> 
fieldResults) {
+    return fieldIds;
+  }
+
+  private boolean isEmptyStruct(Types.NestedField field) {
+    return field.type().isStructType() && ((Types.StructType) 
field.type()).fields().isEmpty();
+  }
+
+  /**
+   * We don't know at this point whether the fields contained within a struct 
are Metadata (and don't exist in the file)
+   * or have been projected in as optional and don't exist in the file. Here 
we check if all the fields are a
+   * combination of those two states.
+   * @param field a nested field which may be an empty struct
+   * @return true if we need to preserve this struct as it may be needed for 
its child fields
+   */
+  private boolean isPotentiallyEmpty(Types.NestedField field) {
+    if (!field.type().isStructType()) {
+      return false;
+    }
+    List<Types.NestedField> fields = ((Types.StructType) 
field.type()).fields();
+    return fields.stream().allMatch(f -> f.fieldId() > Integer.MAX_VALUE - 201 
|| f.isOptional());
+  }
+
+  @Override
+  public Set<Integer> field(Types.NestedField field, Set<Integer> fieldResult) 
{
+    if (isEmptyStruct(field) || isPotentiallyEmpty(field)) {

Review comment:
       Ok so I did a brief look at the buildAvroProjection code to see any 
possible issues, the main difficulty is that we would need to simultaneously 
traverse the real avro schema along with the one passed as "pruned". If we 
don't do this we have no way of determining if a required field is actually in 
the avro schema or not so we can't do proper correctness checks for whether an 
expected required field should actually be there.
   
   Other than that I think the implementation should be fairly straight 
forward, if we find a record that should be there but isn't, we build a new 
record based on the actual field then call "visit" on that field. We will end 
up breaking the 
   
   Let me see how difficult this would be.




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

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