-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add variant type support to ParquetTypeVisitor #14588
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
Open
tmater
wants to merge
1
commit into
apache:main
Choose a base branch
from
tmater:variant_metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+92
−4
Open
Changes from all commits
Commits
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
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 |
|---|---|---|
|
|
@@ -112,7 +112,20 @@ public void testAssignIdsByNameMapping() { | |
| 27, | ||
| "m2", | ||
| Types.MapType.ofOptional( | ||
| 28, 29, Types.StringType.get(), SUPPORTED_PRIMITIVES)))))); | ||
| 28, 29, Types.StringType.get(), SUPPORTED_PRIMITIVES))))), | ||
| optional(30, "variant_col", Types.VariantType.get()), | ||
| required( | ||
| 31, "list_of_variants", Types.ListType.ofOptional(32, Types.VariantType.get())), | ||
| optional( | ||
| 33, | ||
| "struct_with_variant", | ||
| Types.StructType.of( | ||
| Types.NestedField.required(34, "id", Types.IntegerType.get()), | ||
| Types.NestedField.optional(35, "data", Types.VariantType.get()))), | ||
| required( | ||
| 36, | ||
| "map_with_variant_value", | ||
| Types.MapType.ofOptional(37, 38, Types.StringType.get(), Types.VariantType.get()))); | ||
|
|
||
| Schema schema = | ||
| new Schema( | ||
|
|
@@ -224,7 +237,16 @@ public void testSchemaConversionWithoutAssigningIds() { | |
| "map_col_5", | ||
| Repetition.REQUIRED, | ||
| primitive(28, "k", PrimitiveTypeName.INT32, Repetition.REQUIRED), | ||
| primitive(29, "v", PrimitiveTypeName.INT32, Repetition.REQUIRED))); | ||
| primitive(29, "v", PrimitiveTypeName.INT32, Repetition.REQUIRED)), | ||
| variant(30, "variant_col_1", Repetition.OPTIONAL), | ||
| variant(null, "variant_col_2", Repetition.REQUIRED), | ||
| list(31, "list_col_6", Repetition.OPTIONAL, variant(32, "v", Repetition.OPTIONAL)), | ||
|
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. Can we also add a test with a variant in map? |
||
| struct( | ||
| 33, | ||
| "struct_col_3", | ||
| Repetition.REQUIRED, | ||
| primitive(34, "n1", PrimitiveTypeName.INT32, Repetition.REQUIRED), | ||
| variant(null, "variant_field", Repetition.OPTIONAL))); | ||
|
|
||
| Schema expectedSchema = | ||
| new Schema( | ||
|
|
@@ -255,8 +277,13 @@ public void testSchemaConversionWithoutAssigningIds() { | |
| required( | ||
| 27, | ||
| "map_col_5", | ||
| Types.MapType.ofRequired( | ||
| 28, 29, Types.IntegerType.get(), Types.IntegerType.get()))); | ||
| Types.MapType.ofRequired(28, 29, Types.IntegerType.get(), Types.IntegerType.get())), | ||
| optional(30, "variant_col_1", Types.VariantType.get()), | ||
| optional(31, "list_col_6", Types.ListType.ofOptional(32, Types.VariantType.get())), | ||
| required( | ||
| 33, | ||
| "struct_col_3", | ||
| Types.StructType.of(required(34, "n1", Types.IntegerType.get())))); | ||
|
|
||
| Schema actualSchema = ParquetSchemaUtil.convertAndPrune(messageType); | ||
| assertThat(actualSchema.asStruct()) | ||
|
|
@@ -427,6 +454,17 @@ public void testLegacyTwoLevelListGenByParquetThrift1() { | |
| .isEqualTo(expectedSchema.asStruct()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testVariantTypeConversion() { | ||
| MessageType messageType = | ||
| new MessageType("test", variant(1, "variant_field", Repetition.OPTIONAL)); | ||
| Schema expectedSchema = new Schema(optional(1, "variant_field", Types.VariantType.get())); | ||
| Schema actualSchema = ParquetSchemaUtil.convertAndPrune(messageType); | ||
| assertThat(actualSchema.asStruct()) | ||
| .as("Schema must match") | ||
| .isEqualTo(expectedSchema.asStruct()); | ||
| } | ||
|
|
||
| private Type primitive( | ||
| Integer id, String name, PrimitiveTypeName typeName, Repetition repetition) { | ||
| PrimitiveBuilder<PrimitiveType> builder = | ||
|
|
@@ -464,4 +502,18 @@ private Type map(Integer id, String name, Repetition repetition, Type keyType, T | |
| } | ||
| return builder.named(name); | ||
| } | ||
|
|
||
| private Type variant(Integer id, String name, Repetition repetition) { | ||
| GroupBuilder<GroupType> builder = | ||
| org.apache.parquet.schema.Types.buildGroup(repetition) | ||
| .as(org.apache.parquet.schema.LogicalTypeAnnotation.variantType((byte) 1)) | ||
|
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. Same here. Use |
||
| .primitive(PrimitiveTypeName.BINARY, Repetition.REQUIRED) | ||
| .named("metadata") | ||
| .primitive(PrimitiveTypeName.BINARY, Repetition.REQUIRED) | ||
| .named("value"); | ||
| if (id != null) { | ||
| builder.id(id); | ||
| } | ||
| return builder.named(name); | ||
| } | ||
| } | ||
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.
nit: use
Variant.VARIANT_SPEC_VERSIONinstead of hardcoded 1.