-
Notifications
You must be signed in to change notification settings - Fork 3.4k
AWS: fix Iceberg to Glue schema conversion #3887
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 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,6 @@ | |
| import java.util.Set; | ||
| import java.util.regex.Pattern; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.iceberg.PartitionField; | ||
| import org.apache.iceberg.TableMetadata; | ||
| import org.apache.iceberg.catalog.Namespace; | ||
| import org.apache.iceberg.catalog.TableIdentifier; | ||
|
|
@@ -37,7 +36,6 @@ | |
| import org.apache.iceberg.relocated.com.google.common.collect.Maps; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Sets; | ||
| import org.apache.iceberg.types.Type; | ||
| import org.apache.iceberg.types.TypeUtil; | ||
| import org.apache.iceberg.types.Types; | ||
| import org.apache.iceberg.types.Types.NestedField; | ||
| import org.slf4j.Logger; | ||
|
|
@@ -58,17 +56,8 @@ private IcebergToGlueConverter() { | |
| private static final Pattern GLUE_TABLE_PATTERN = Pattern.compile("^[a-z0-9_]{1,255}$"); | ||
| public static final String GLUE_DB_LOCATION_KEY = "location"; | ||
| public static final String GLUE_DB_DESCRIPTION_KEY = "comment"; | ||
| public static final String ICEBERG_FIELD_USAGE = "iceberg.field.usage"; | ||
| public static final String ICEBERG_FIELD_TYPE_TYPE_ID = "iceberg.field.type.typeid"; | ||
| public static final String ICEBERG_FIELD_TYPE_STRING = "iceberg.field.type.string"; | ||
| public static final String ICEBERG_FIELD_ID = "iceberg.field.id"; | ||
| public static final String ICEBERG_FIELD_OPTIONAL = "iceberg.field.optional"; | ||
| public static final String ICEBERG_PARTITION_TRANSFORM = "iceberg.partition.transform"; | ||
| public static final String ICEBERG_PARTITION_FIELD_ID = "iceberg.partition.field-id"; | ||
| public static final String ICEBERG_PARTITION_SOURCE_ID = "iceberg.partition.source-id"; | ||
| public static final String SCHEMA_COLUMN = "schema-column"; | ||
| public static final String SCHEMA_SUBFIELD = "schema-subfield"; | ||
| public static final String PARTITION_FIELD = "partition-field"; | ||
|
|
||
| /** | ||
| * A Glue database name cannot be longer than 252 characters. | ||
|
|
@@ -252,59 +241,32 @@ private static String toTypeString(Type type) { | |
|
|
||
| private static List<Column> toColumns(TableMetadata metadata) { | ||
| List<Column> columns = Lists.newArrayList(); | ||
| Set<NestedField> rootColumnSet = Sets.newHashSet(); | ||
| // Add schema-column fields | ||
| Set<String> addedNames = Sets.newHashSet(); | ||
|
|
||
| for (NestedField field : metadata.schema().columns()) { | ||
| rootColumnSet.add(field); | ||
| addColumnWithDedupe(columns, addedNames, field, field.name()); | ||
| } | ||
|
|
||
| return columns; | ||
| } | ||
|
|
||
| private static void addColumnWithDedupe(List<Column> columns, Set<String> dedupe, | ||
| NestedField field, String fieldName) { | ||
| if (!dedupe.contains(fieldName)) { | ||
| columns.add(Column.builder() | ||
| .name(field.name()) | ||
| .name(fieldName) | ||
| .type(toTypeString(field.type())) | ||
| .comment(field.doc()) | ||
| .parameters(convertToParameters(SCHEMA_COLUMN, field)) | ||
| .build()); | ||
| } | ||
| // Add schema-subfield | ||
| for (NestedField field : TypeUtil.indexById(metadata.schema().asStruct()).values()) { | ||
| if (!rootColumnSet.contains(field)) { | ||
| columns.add(Column.builder() | ||
| .name(field.name()) | ||
| .type(toTypeString(field.type())) | ||
| .comment(field.doc()) | ||
| .parameters(convertToParameters(SCHEMA_SUBFIELD, field)) | ||
| .build()); | ||
| } | ||
| } | ||
| // Add partition-field | ||
| for (PartitionField partitionField : metadata.spec().fields()) { | ||
| Type type = partitionField.transform() | ||
| .getResultType(metadata.schema().findField(partitionField.sourceId()).type()); | ||
| columns.add(Column.builder() | ||
| .name(partitionField.name()) | ||
| .type(toTypeString(type)) | ||
| .parameters(convertToPartitionFieldParameters(type, partitionField)) | ||
| .parameters(convertToParameters(field)) | ||
| .build()); | ||
| dedupe.add(fieldName); | ||
| } | ||
| return columns; | ||
| } | ||
|
|
||
| private static Map<String, String> convertToParameters(String fieldUsage, NestedField field) { | ||
| return ImmutableMap.of(ICEBERG_FIELD_USAGE, fieldUsage, | ||
| ICEBERG_FIELD_TYPE_TYPE_ID, field.type().typeId().toString(), | ||
| ICEBERG_FIELD_TYPE_STRING, toTypeString(field.type()), | ||
| private static Map<String, String> convertToParameters(NestedField field) { | ||
|
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. this method can be combined to |
||
| return ImmutableMap.of( | ||
| ICEBERG_FIELD_ID, Integer.toString(field.fieldId()), | ||
| ICEBERG_FIELD_OPTIONAL, Boolean.toString(field.isOptional()) | ||
| ); | ||
| } | ||
|
|
||
| private static Map<String, String> convertToPartitionFieldParameters(Type type, PartitionField partitionField) { | ||
| return ImmutableMap.<String, String>builder() | ||
| .put(ICEBERG_FIELD_USAGE, PARTITION_FIELD) | ||
| .put(ICEBERG_FIELD_TYPE_TYPE_ID, type.typeId().toString()) | ||
| .put(ICEBERG_FIELD_TYPE_STRING, toTypeString(type)) | ||
| .put(ICEBERG_FIELD_ID, Integer.toString(partitionField.fieldId())) | ||
| .put(ICEBERG_PARTITION_TRANSFORM, partitionField.transform().toString()) | ||
| .put(ICEBERG_PARTITION_FIELD_ID, Integer.toString(partitionField.fieldId())) | ||
| .put(ICEBERG_PARTITION_SOURCE_ID, Integer.toString(partitionField.sourceId())) | ||
| .build(); | ||
| } | ||
| } | ||
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
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.
the
fieldNameparameter is not necessary, it's alwaysfield.name()