-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Support non-optional union types and column projection in complex union for Avro #5704
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
Changes from all commits
eca352a
6ef0ad8
ee12c60
3296222
72e8883
4c11e47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,11 +82,38 @@ private static <T> T visitRecord( | |
| private static <T> T visitUnion(Type type, Schema union, AvroSchemaWithTypeVisitor<T> visitor) { | ||
| List<Schema> types = union.getTypes(); | ||
| List<T> options = Lists.newArrayListWithExpectedSize(types.size()); | ||
| for (Schema branch : types) { | ||
| if (branch.getType() == Schema.Type.NULL) { | ||
| options.add(visit((Type) null, branch, visitor)); | ||
| } else { | ||
| options.add(visit(type, branch, visitor)); | ||
|
|
||
| // simple union case | ||
| if (AvroSchemaUtil.isOptionSchema(union)) { | ||
| for (Schema branch : types) { | ||
| if (branch.getType() == Schema.Type.NULL) { | ||
| options.add(visit((Type) null, branch, visitor)); | ||
| } else { | ||
| options.add(visit(type, branch, visitor)); | ||
| } | ||
| } | ||
| } else { // complex union case | ||
| Preconditions.checkArgument( | ||
| type instanceof Types.StructType, | ||
| "Cannot visit invalid Iceberg type: %s for Avro complex union type: %s", | ||
| type, | ||
| union); | ||
| for (Schema branch : types) { | ||
| if (branch.getType() == Schema.Type.NULL) { | ||
| options.add(visit((Type) null, branch, visitor)); | ||
| } else { | ||
| Types.NestedField expectedSchemaField = null; | ||
| String branchId = branch.getProp(AvroSchemaUtil.BRANCH_ID_PROP); | ||
| if (branchId != null) { | ||
| expectedSchemaField = type.asStructType().field(Integer.parseInt(branchId)); | ||
| } | ||
| if (expectedSchemaField != null) { | ||
| options.add(visit(expectedSchemaField.type(), branch, visitor)); | ||
| } else { | ||
| Type pseudoExpectedSchemaField = AvroSchemaUtil.convert(branch); | ||
| options.add(visit(pseudoExpectedSchemaField, branch, visitor)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return visitor.union(type, union, options); | ||
|
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. I think here you are returning all the readers and in the
Author
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. I am afraid not. The readers for all branch types need to pass into |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,8 @@ public class ProjectionDatumReader<D> implements DatumReader<D>, SupportsRowPosi | |
| private Schema fileSchema = null; | ||
| private DatumReader<D> wrapped = null; | ||
|
|
||
| private Map<String, Integer> avroSchemaNameToIcebergFieldId = null; | ||
|
|
||
| public ProjectionDatumReader( | ||
| Function<Schema, DatumReader<?>> getReader, | ||
| org.apache.iceberg.Schema expectedSchema, | ||
|
|
@@ -50,6 +52,16 @@ public ProjectionDatumReader( | |
| this.nameMapping = nameMapping; | ||
| } | ||
|
|
||
| public ProjectionDatumReader( | ||
| Function<Schema, DatumReader<?>> getReader, | ||
| org.apache.iceberg.Schema expectedSchema, | ||
| Map<String, String> renames, | ||
| NameMapping nameMapping, | ||
| Map<String, Integer> avroSchemaNameToIcebergFieldId) { | ||
| this(getReader, expectedSchema, renames, nameMapping); | ||
| this.avroSchemaNameToIcebergFieldId = avroSchemaNameToIcebergFieldId; | ||
| } | ||
|
|
||
| @Override | ||
| public void setRowPositionSupplier(Supplier<Long> posSupplier) { | ||
| if (wrapped instanceof SupportsRowPosition) { | ||
|
|
@@ -64,7 +76,9 @@ public void setSchema(Schema newFileSchema) { | |
| nameMapping = MappingUtil.create(expectedSchema); | ||
|
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. I was under the impression that we would add name/type name information to the
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. Yes, exactly. See the code I posted above. I think we need to add |
||
| } | ||
| Set<Integer> projectedIds = TypeUtil.getProjectedIds(expectedSchema); | ||
| Schema prunedSchema = AvroSchemaUtil.pruneColumns(newFileSchema, projectedIds, nameMapping); | ||
| Schema prunedSchema = | ||
| AvroSchemaUtil.pruneColumns( | ||
| newFileSchema, projectedIds, nameMapping, avroSchemaNameToIcebergFieldId); | ||
| this.readSchema = AvroSchemaUtil.buildAvroProjection(prunedSchema, expectedSchema, renames); | ||
| this.wrapped = newDatumReader(); | ||
| } | ||
|
|
||
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.
Terminology is a bit confusing (types, branches, options, etc). I would try to qualify them by the source, e.g., renaming
typestoavroUnionBranches.