Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty struct type in selectNot call #59

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public Set<Integer> schema(Schema schema, Set<Integer> structResult) {

@Override
public Set<Integer> struct(Types.StructType struct, List<Set<Integer>> fieldResults) {
// if the struct is an empty struct, we should consider itself as 'internal', thus including its id.
if (struct.fields().size() == 0) {
// since returning null is signaling the call to field() to add the field ID.
return null;
}
return fieldIds;
}

Expand Down
7 changes: 6 additions & 1 deletion api/src/main/java/org/apache/iceberg/types/TypeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.iceberg.relocated.com.google.common.collect.Iterables;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.relocated.com.google.common.collect.Sets;

public class TypeUtil {

Expand Down Expand Up @@ -82,7 +83,11 @@ public static Set<Integer> getProjectedIds(Type type) {
}

private static Set<Integer> getIdsInternal(Type type) {
return visit(type, new GetProjectedIds());
Set<Integer> res = visit(type, new GetProjectedIds());
if (res == null) {
return Sets.newHashSet();
}
return res;
}

public static Types.StructType selectNot(Types.StructType struct, Set<Integer> fieldIds) {
Expand Down
24 changes: 24 additions & 0 deletions api/src/test/java/org/apache/iceberg/types/TestTypeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@

package org.apache.iceberg.types;

import java.util.Collections;
import org.apache.iceberg.Schema;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet;
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;


Expand Down Expand Up @@ -55,5 +58,26 @@ public void testValidateSchemaViaIndexByName() {

TypeUtil.indexByName(Types.StructType.of(nestedType));
}

@Test
public void testSelectNot() {
// iceberg schema which has an empty struct column.
Schema schema = new Schema(
required(1, "a", Types.IntegerType.get()),
optional(2, "b", Types.StructType.of(
required(3, "c", Types.StringType.get()),
optional(4, "d", Types.StructType.of(Collections.emptyList()))))
);
Schema filteredSchema = TypeUtil.selectNot(schema, ImmutableSet.of());
Assert.assertEquals(schema.toString(), filteredSchema.toString());

filteredSchema = TypeUtil.selectNot(schema, ImmutableSet.of(4));
Schema expected = new Schema(
required(1, "a", Types.IntegerType.get()),
optional(2, "b", Types.StructType.of(
required(3, "c", Types.StringType.get())))
);
Assert.assertEquals(expected.toString(), filteredSchema.toString());
}
}