-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Core: Add TypeUtil.project() for Explicit Projection #2952
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aa3b6e7
Core: Add TypeUtil.project() for Explicit Projection
RussellSpitzer 6375737
Handle projection of structs within Maps and Lists
RussellSpitzer ffc149a
Reviewer Comments
RussellSpitzer 0372d16
Fix issue with nested lists and maps
RussellSpitzer 35a768f
Error message change
RussellSpitzer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,13 +24,26 @@ | |
| import org.apache.iceberg.Schema; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
| import org.apache.iceberg.types.Types.ListType; | ||
| import org.apache.iceberg.types.Types.MapType; | ||
| import org.apache.iceberg.types.Types.StructType; | ||
|
|
||
| class PruneColumns extends TypeUtil.SchemaVisitor<Type> { | ||
| private final Set<Integer> selected; | ||
| private final boolean selectFullTypes; | ||
|
|
||
| PruneColumns(Set<Integer> selected) { | ||
| /** | ||
| * Visits a schema and returns only the fields selected by the id set. | ||
| * <p> | ||
| * When selectFullTypes is false selecting list or map types is undefined and forbidden. | ||
| * | ||
| * @param selected ids of elements to return | ||
| * @param selectFullTypes whether to select all subfields of a selected nested type | ||
| */ | ||
| PruneColumns(Set<Integer> selected, boolean selectFullTypes) { | ||
| Preconditions.checkNotNull(selected, "Selected field ids cannot be null"); | ||
| this.selected = selected; | ||
| this.selectFullTypes = selectFullTypes; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -77,10 +90,19 @@ public Type struct(Types.StructType struct, List<Type> fieldResults) { | |
| @Override | ||
| public Type field(Types.NestedField field, Type fieldResult) { | ||
| if (selected.contains(field.fieldId())) { | ||
| return field.type(); | ||
| if (selectFullTypes) { | ||
| return field.type(); | ||
| } else if (field.type().isStructType()) { | ||
| return projectSelectedStruct(fieldResult); | ||
| } else { | ||
| Preconditions.checkArgument(!field.type().isNestedType(), | ||
| "Cannot explicitly project List or Map types, %s:%s of type %s was selected", | ||
| field.fieldId(), field.name(), field.type()); | ||
| // Selected non-struct field | ||
| return field.type(); | ||
| } | ||
| } else if (fieldResult != null) { | ||
| // this isn't necessarily the same as field.type() because a struct may not have all | ||
| // fields selected. | ||
| // This field wasn't selected but a subfield was so include that | ||
| return fieldResult; | ||
| } | ||
| return null; | ||
|
|
@@ -89,31 +111,39 @@ public Type field(Types.NestedField field, Type fieldResult) { | |
| @Override | ||
| public Type list(Types.ListType list, Type elementResult) { | ||
| if (selected.contains(list.elementId())) { | ||
| return list; | ||
| } else if (elementResult != null) { | ||
| if (list.elementType() == elementResult) { | ||
| if (selectFullTypes) { | ||
| return list; | ||
| } else if (list.isElementOptional()) { | ||
| return Types.ListType.ofOptional(list.elementId(), elementResult); | ||
| } else if (list.elementType().isStructType()) { | ||
| StructType projectedStruct = projectSelectedStruct(elementResult); | ||
| return projectList(list, projectedStruct); | ||
| } else { | ||
| return Types.ListType.ofRequired(list.elementId(), elementResult); | ||
| Preconditions.checkArgument(list.elementType().isPrimitiveType(), | ||
| "Cannot explicitly project List or Map types, Field %s of type %s was selected", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really a "field" so much as a "list element" |
||
| list.elementId(), list.elementType()); | ||
| return list; | ||
| } | ||
| } else if (elementResult != null) { | ||
| return projectList(list, elementResult); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Type map(Types.MapType map, Type ignored, Type valueResult) { | ||
| if (selected.contains(map.valueId())) { | ||
| return map; | ||
| } else if (valueResult != null) { | ||
| if (map.valueType() == valueResult) { | ||
| if (selectFullTypes) { | ||
| return map; | ||
| } else if (map.isValueOptional()) { | ||
| return Types.MapType.ofOptional(map.keyId(), map.valueId(), map.keyType(), valueResult); | ||
| } else if (map.valueType().isStructType()) { | ||
| Type projectedStruct = projectSelectedStruct(valueResult); | ||
| return projectMap(map, projectedStruct); | ||
| } else { | ||
| return Types.MapType.ofRequired(map.keyId(), map.valueId(), map.keyType(), valueResult); | ||
| Preconditions.checkArgument(map.valueType().isPrimitiveType(), | ||
| "Cannot explicitly project List or Map types, Field %s of type %s was selected", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I'd replace "field" with "map value" |
||
| map.valueId(), map.valueType()); | ||
| return map; | ||
| } | ||
| } else if (valueResult != null) { | ||
| return projectMap(map, valueResult); | ||
| } else if (selected.contains(map.keyId())) { | ||
| // right now, maps can't be selected without values | ||
| return map; | ||
|
|
@@ -125,4 +155,44 @@ public Type map(Types.MapType map, Type ignored, Type valueResult) { | |
| public Type primitive(Type.PrimitiveType primitive) { | ||
| return null; | ||
| } | ||
|
|
||
| private ListType projectList(ListType list, Type elementResult) { | ||
| Preconditions.checkArgument(elementResult != null, "Cannot project a list when the element result is null"); | ||
| if (list.elementType() == elementResult) { | ||
| return list; | ||
| } else if (list.isElementOptional()) { | ||
| return Types.ListType.ofOptional(list.elementId(), elementResult); | ||
| } else { | ||
| return Types.ListType.ofRequired(list.elementId(), elementResult); | ||
| } | ||
| } | ||
|
|
||
| private MapType projectMap(MapType map, Type valueResult) { | ||
| Preconditions.checkArgument(valueResult != null, "Attempted to project a map without a defined map value type"); | ||
| if (map.valueType() == valueResult) { | ||
| return map; | ||
| } else if (map.isValueOptional()) { | ||
| return Types.MapType.ofOptional(map.keyId(), map.valueId(), map.keyType(), valueResult); | ||
| } else { | ||
| return Types.MapType.ofRequired(map.keyId(), map.valueId(), map.keyType(), valueResult); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * If select full types is disabled we need to recreate the struct with only the selected | ||
| * subfields. If no subfields are selected we return an empty struct. | ||
| * @param projectedField subfields already selected in this projection | ||
| * @return projected struct | ||
| */ | ||
| private StructType projectSelectedStruct(Type projectedField) { | ||
| Preconditions.checkArgument(projectedField == null || projectedField.isStructType()); | ||
| // the struct was selected, ensure at least an empty struct is returned | ||
| if (projectedField == null) { | ||
| // no sub-fields were selected but the struct was, return an empty struct | ||
| return Types.StructType.of(); | ||
| } else { | ||
| // sub-fields were selected so return the projected struct | ||
| return projectedField.asStructType(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.