-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Use ID-based mapping for struct fields in Iceberg Parquet reader vol. 2 #9124
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9132141
Fix missing defensive copy in ColumnIdentity constructor
findepi e788831
Code cleanup in ParquetColumnIOConverter
findepi c5cb830
Split ParquetColumnIOConverter for Hive and Iceberg
findepi 5adcb12
Use ID-based mapping for struct fields in Iceberg Parquet reader
findepi 5971a3b
Add dereference pushdown smoke test coverage
findepi 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
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
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
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
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
138 changes: 138 additions & 0 deletions
138
.../trino-iceberg/src/main/java/io/trino/plugin/iceberg/IcebergParquetColumnIOConverter.java
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 |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /* | ||
| * Licensed 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 io.trino.plugin.iceberg; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import io.trino.parquet.Field; | ||
| import io.trino.parquet.GroupField; | ||
| import io.trino.parquet.PrimitiveField; | ||
| import io.trino.parquet.RichColumnDescriptor; | ||
| import io.trino.spi.type.ArrayType; | ||
| import io.trino.spi.type.MapType; | ||
| import io.trino.spi.type.RowType; | ||
| import io.trino.spi.type.Type; | ||
| import org.apache.parquet.io.ColumnIO; | ||
| import org.apache.parquet.io.GroupColumnIO; | ||
| import org.apache.parquet.io.PrimitiveColumnIO; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import static com.google.common.base.MoreObjects.toStringHelper; | ||
| import static com.google.common.base.Preconditions.checkArgument; | ||
| import static com.google.common.collect.Iterables.getOnlyElement; | ||
| import static io.trino.parquet.ParquetTypeUtils.getArrayElementColumn; | ||
| import static io.trino.parquet.ParquetTypeUtils.getMapKeyValueColumn; | ||
| import static io.trino.parquet.ParquetTypeUtils.lookupColumnById; | ||
| import static java.util.Objects.requireNonNull; | ||
| import static org.apache.parquet.io.ColumnIOUtil.columnDefinitionLevel; | ||
| import static org.apache.parquet.io.ColumnIOUtil.columnRepetitionLevel; | ||
| import static org.apache.parquet.schema.Type.Repetition.OPTIONAL; | ||
|
|
||
| public final class IcebergParquetColumnIOConverter | ||
| { | ||
| private IcebergParquetColumnIOConverter() {} | ||
|
|
||
| public static Optional<Field> constructField(FieldContext context, ColumnIO columnIO) | ||
| { | ||
| requireNonNull(context, "context is null"); | ||
| if (columnIO == null) { | ||
| return Optional.empty(); | ||
| } | ||
| boolean required = columnIO.getType().getRepetition() != OPTIONAL; | ||
| int repetitionLevel = columnRepetitionLevel(columnIO); | ||
| int definitionLevel = columnDefinitionLevel(columnIO); | ||
| Type type = context.getType(); | ||
| if (type instanceof RowType) { | ||
| RowType rowType = (RowType) type; | ||
| GroupColumnIO groupColumnIO = (GroupColumnIO) columnIO; | ||
| ImmutableList.Builder<Optional<Field>> fieldsBuilder = ImmutableList.builder(); | ||
| List<RowType.Field> fields = rowType.getFields(); | ||
| boolean structHasParameters = false; | ||
| for (int i = 0; i < fields.size(); i++) { | ||
| RowType.Field rowField = fields.get(i); | ||
| ColumnIdentity fieldIdentity = context.getColumnIdentity().getChildren().get(i); | ||
| Optional<Field> field = constructField(new FieldContext(rowField.getType(), fieldIdentity), lookupColumnById(groupColumnIO, fieldIdentity.getId())); | ||
| structHasParameters |= field.isPresent(); | ||
| fieldsBuilder.add(field); | ||
| } | ||
| if (structHasParameters) { | ||
| return Optional.of(new GroupField(type, repetitionLevel, definitionLevel, required, fieldsBuilder.build())); | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
| if (type instanceof MapType) { | ||
| MapType mapType = (MapType) type; | ||
| GroupColumnIO groupColumnIO = (GroupColumnIO) columnIO; | ||
| GroupColumnIO keyValueColumnIO = getMapKeyValueColumn(groupColumnIO); | ||
| if (keyValueColumnIO.getChildrenCount() != 2) { | ||
| return Optional.empty(); | ||
| } | ||
| checkArgument(context.getColumnIdentity().getChildren().size() == 2, "Not a map: %s", context); | ||
| ColumnIdentity keyIdentity = context.getColumnIdentity().getChildren().get(0); | ||
| ColumnIdentity valueIdentity = context.getColumnIdentity().getChildren().get(1); | ||
| // TODO validate column ID | ||
| Optional<Field> keyField = constructField(new FieldContext(mapType.getKeyType(), keyIdentity), keyValueColumnIO.getChild(0)); | ||
| // TODO validate column ID | ||
| Optional<Field> valueField = constructField(new FieldContext(mapType.getValueType(), valueIdentity), keyValueColumnIO.getChild(1)); | ||
| return Optional.of(new GroupField(type, repetitionLevel, definitionLevel, required, ImmutableList.of(keyField, valueField))); | ||
| } | ||
| if (type instanceof ArrayType) { | ||
| ArrayType arrayType = (ArrayType) type; | ||
| GroupColumnIO groupColumnIO = (GroupColumnIO) columnIO; | ||
| if (groupColumnIO.getChildrenCount() != 1) { | ||
| return Optional.empty(); | ||
| } | ||
| checkArgument(context.getColumnIdentity().getChildren().size() == 1, "Not an array: %s", context); | ||
| ColumnIdentity elementIdentity = getOnlyElement(context.getColumnIdentity().getChildren()); | ||
| // TODO validate column ID | ||
| Optional<Field> field = constructField(new FieldContext(arrayType.getElementType(), elementIdentity), getArrayElementColumn(groupColumnIO.getChild(0))); | ||
| return Optional.of(new GroupField(type, repetitionLevel, definitionLevel, required, ImmutableList.of(field))); | ||
| } | ||
| PrimitiveColumnIO primitiveColumnIO = (PrimitiveColumnIO) columnIO; | ||
| RichColumnDescriptor column = new RichColumnDescriptor(primitiveColumnIO.getColumnDescriptor(), columnIO.getType().asPrimitiveType()); | ||
| return Optional.of(new PrimitiveField(type, repetitionLevel, definitionLevel, required, column, primitiveColumnIO.getId())); | ||
| } | ||
|
|
||
| public static class FieldContext | ||
| { | ||
| private final Type type; | ||
| private final ColumnIdentity columnIdentity; | ||
|
|
||
| public FieldContext(Type type, ColumnIdentity columnIdentity) | ||
| { | ||
| this.type = requireNonNull(type, "type is null"); | ||
| this.columnIdentity = requireNonNull(columnIdentity, "columnIdentity is null"); | ||
| } | ||
|
|
||
| public Type getType() | ||
| { | ||
| return type; | ||
| } | ||
|
|
||
| public ColumnIdentity getColumnIdentity() | ||
| { | ||
| return columnIdentity; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() | ||
| { | ||
| return toStringHelper(this) | ||
| .add("type", type) | ||
| .add("columnIdentity", columnIdentity) | ||
| .toString(); | ||
| } | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not pretty, but consistent with other methods in this file. A change to Optional can be a follow up, if needed.