-
Notifications
You must be signed in to change notification settings - Fork 196
Fix Delta to Iceberg not working on column mapping enabled Delta source table #766
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
base: main
Are you sure you want to change the base?
Changes from all commits
e1e0ed0
210ea62
4526881
2d92688
fc763a9
6526ab4
baf9d33
88ac8c0
07dd0d1
2368c5a
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 |
|---|---|---|
|
|
@@ -20,9 +20,12 @@ | |
|
|
||
| import java.time.Instant; | ||
| import java.time.temporal.ChronoUnit; | ||
| import java.util.ArrayList; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
|
|
||
| import lombok.extern.log4j.Log4j2; | ||
|
|
||
|
|
@@ -39,6 +42,11 @@ | |
| import org.apache.iceberg.catalog.Namespace; | ||
| import org.apache.iceberg.catalog.TableIdentifier; | ||
| import org.apache.iceberg.exceptions.NotFoundException; | ||
| import org.apache.iceberg.mapping.MappedField; | ||
| import org.apache.iceberg.mapping.MappedFields; | ||
| import org.apache.iceberg.mapping.MappingUtil; | ||
| import org.apache.iceberg.mapping.NameMapping; | ||
| import org.apache.iceberg.mapping.NameMappingParser; | ||
|
|
||
| import org.apache.xtable.conversion.TargetTable; | ||
| import org.apache.xtable.model.InternalTable; | ||
|
|
@@ -161,12 +169,42 @@ private void initializeTableIfRequired(InternalTable internalTable) { | |
| } | ||
| } | ||
|
|
||
| private MappedFields updateNameMapping(MappedFields mapping, Map<Integer, String> updates) { | ||
| if (mapping == null) { | ||
|
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 mapping ever be null? If null is returned will
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. It's mainly used to end the recursive call; all nested fields of a given field were processed using this function as well. In that case, having |
||
| return null; | ||
| } | ||
| List<MappedField> fieldResults = new ArrayList<>(); | ||
| for (MappedField field : mapping.fields()) { | ||
| Set<String> fieldNames = new HashSet<>(field.names()); | ||
| if (updates.containsKey(field.id())) { | ||
| fieldNames.add(updates.get(field.id())); | ||
| } | ||
| MappedFields nestedMapping = updateNameMapping(field.nestedMapping(), updates); | ||
| fieldResults.add(MappedField.of(field.id(), fieldNames, nestedMapping)); | ||
| } | ||
| return MappedFields.of(fieldResults); | ||
| } | ||
|
|
||
| @Override | ||
| public void syncSchema(InternalSchema schema) { | ||
| Schema latestSchema = schemaExtractor.toIceberg(schema); | ||
| String mappingJson = transaction.table().properties().get(TableProperties.DEFAULT_NAME_MAPPING); | ||
| boolean hasFieldIds = | ||
| schema.getAllFields().stream().anyMatch(field -> field.getFieldId() != null); | ||
| // Recreate name mapping when field IDs were provided in the source schema to ensure every | ||
| // field in the mapping was assigned the same ID as what is in the source schema | ||
| NameMapping mapping = | ||
| mappingJson == null || hasFieldIds | ||
| ? MappingUtil.create(latestSchema) | ||
| : NameMappingParser.fromJson(mappingJson); | ||
| mapping = | ||
| NameMapping.of( | ||
| updateNameMapping(mapping.asMappedFields(), schemaExtractor.getIdToStorageName())); | ||
| transaction | ||
| .updateProperties() | ||
| .set(TableProperties.DEFAULT_NAME_MAPPING, NameMappingParser.toJson(mapping)) | ||
| .commit(); | ||
| if (!transaction.table().schema().sameSchema(latestSchema)) { | ||
| boolean hasFieldIds = | ||
| schema.getAllFields().stream().anyMatch(field -> field.getFieldId() != null); | ||
| if (hasFieldIds) { | ||
| // There is no clean way to sync the schema with the provided field IDs using the | ||
| // transaction API so we commit the current transaction and interact directly with | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ | |
| import java.util.stream.Collectors; | ||
|
|
||
| import lombok.AccessLevel; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.extern.log4j.Log4j2; | ||
|
|
||
|
|
@@ -53,18 +54,63 @@ | |
| @Log4j2 | ||
| @NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
| public class IcebergSchemaExtractor { | ||
| private static final IcebergSchemaExtractor INSTANCE = new IcebergSchemaExtractor(); | ||
| private static final String MAP_KEY_FIELD_NAME = "key"; | ||
| private static final String MAP_VALUE_FIELD_NAME = "value"; | ||
| private static final String LIST_ELEMENT_FIELD_NAME = "element"; | ||
| @Getter private final Map<Integer, String> idToStorageName = new HashMap<>(); | ||
|
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. @xr-chen This adds state to the class so we have to decide if we want to make an instance of this class per conversion or remove this state. Removing the state would require you to return the map as part of the response for the I don't have a strong opinion either way, but I would prefer that over 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. Good suggestion, this will avoid unexpected outcomes caused by adding states to a singleton object. I will update the code |
||
|
|
||
| public static IcebergSchemaExtractor getInstance() { | ||
| return INSTANCE; | ||
| return new IcebergSchemaExtractor(); | ||
| } | ||
|
|
||
| private void initializeFieldIdTracker(InternalSchema schema, AtomicInteger fieldIdTracker) { | ||
| schema.getFields().stream() | ||
| .forEach( | ||
| field -> { | ||
| if (field.getFieldId() != null) | ||
| fieldIdTracker.accumulateAndGet(field.getFieldId(), Math::max); | ||
| initializeFieldIdTracker(field, fieldIdTracker); | ||
| }); | ||
| } | ||
|
|
||
| private void initializeFieldIdTracker(InternalField field, AtomicInteger fieldIdTracker) { | ||
| switch (field.getSchema().getDataType()) { | ||
| case RECORD: | ||
| initializeFieldIdTracker(field.getSchema(), fieldIdTracker); | ||
| return; | ||
| case MAP: | ||
| field.getSchema().getFields().stream() | ||
| .filter( | ||
| mapField -> | ||
| InternalField.Constants.MAP_KEY_FIELD_NAME.equals(mapField.getName()) | ||
| || InternalField.Constants.MAP_VALUE_FIELD_NAME.equals(mapField.getName())) | ||
| .forEach( | ||
| mapField -> { | ||
| if (mapField.getFieldId() != null) | ||
| fieldIdTracker.accumulateAndGet(mapField.getFieldId(), Math::max); | ||
| initializeFieldIdTracker(mapField, fieldIdTracker); | ||
| }); | ||
| return; | ||
| case LIST: | ||
| field.getSchema().getFields().stream() | ||
| .filter( | ||
| arrayField -> | ||
| InternalField.Constants.ARRAY_ELEMENT_FIELD_NAME.equals(arrayField.getName())) | ||
| .forEach( | ||
| arrayField -> { | ||
| if (arrayField.getFieldId() != null) | ||
| fieldIdTracker.accumulateAndGet(arrayField.getFieldId(), Math::max); | ||
| initializeFieldIdTracker(arrayField, fieldIdTracker); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| public Schema toIceberg(InternalSchema internalSchema) { | ||
| // if field IDs are not assigned in the source, just use an incrementing integer | ||
| AtomicInteger fieldIdTracker = new AtomicInteger(0); | ||
| // traverse the schema before conversion to ensure fieldIdTracker won't return any | ||
| // fieldIds that are already present in the schema | ||
| initializeFieldIdTracker(internalSchema, fieldIdTracker); | ||
| List<Types.NestedField> nestedFields = convertFields(internalSchema, fieldIdTracker); | ||
| List<InternalField> recordKeyFields = internalSchema.getRecordKeyFields(); | ||
| boolean recordKeyFieldsAreNotRequired = | ||
|
|
@@ -154,6 +200,9 @@ private List<Types.NestedField> convertFields( | |
| List<Types.NestedField> nestedFields = new ArrayList<>(schema.getFields().size()); | ||
| for (int i = 0; i < schema.getFields().size(); i++) { | ||
| InternalField field = schema.getFields().get(i); | ||
| if (field.getStorageName() != null) { | ||
| idToStorageName.put(ids.get(i), field.getStorageName()); | ||
| } | ||
| nestedFields.add( | ||
| Types.NestedField.of( | ||
| ids.get(i), | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -746,6 +746,48 @@ public void testIcebergCorruptedSnapshotRecovery() throws Exception { | |||
| } | ||||
| } | ||||
|
|
||||
| @Test | ||||
| public void testColumnMappingEnabledDeltaToIceberg() { | ||||
|
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. @xr-chen to answer your question, yes this is exactly what I was looking for. Do you think we should also do some minor schema evolution in this case?
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. @the-other-tim-brown Yes, I think so, but the code actually can't pass this test case now, so it probably won't work on any rename column type of schema change. It seems to me that only populating fieldId doesn't work, and the converted Iceberg doesn't know which 'physical' column in the data file to read data from for a 'logic' column name in the table schema, and it returns null values for all columns if we read from the generated Iceberg table. This issue is probably due to:
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 added delta's physical column names into the Icerberg's
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. The filesystem is shared between the tests along with the hadoop and spark configurations
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. Ahh, it's due 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. Good catch on this, thanks for digging into the issue. I added a comment on that class.
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. Now that you have this working, should we add some schema evolution here? I think we should at least add a second commit as a sanity check that everything works as expected.
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. Is there an existing function I can use to change the schema of the source delta table for testing, or I should implement it by myself? By a second commit, you mean inserting more records by
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. We already have some helpers for this. Earlier in this test class you will see a test case that uses GenericTable.getInstanceWithAdditionalColumns. This has some helpers for creating the evolved Delta table schema under the hood that you should be able to build off of. And yes, just inserting or updating some more records is fine. I just want to ensure there isn't some unexpected side-effect when we set this table property multiple times for Iceberg. |
||||
| String tableName = getTableName(); | ||||
| ConversionSourceProvider<?> conversionSourceProvider = getConversionSourceProvider(DELTA); | ||||
| try (TestSparkDeltaTable table = | ||||
| TestSparkDeltaTable.forColumnMappingEnabled(tableName, tempDir, sparkSession, null)) { | ||||
| table.insertRows(20); | ||||
| ConversionController conversionController = | ||||
| new ConversionController(jsc.hadoopConfiguration()); | ||||
| ConversionConfig conversionConfig = | ||||
| getTableSyncConfig( | ||||
| DELTA, | ||||
| SyncMode.INCREMENTAL, | ||||
| tableName, | ||||
| table, | ||||
| Collections.singletonList(ICEBERG), | ||||
| null, | ||||
| null); | ||||
| conversionController.sync(conversionConfig, conversionSourceProvider); | ||||
| table.insertRows(10); | ||||
| conversionController.sync(conversionConfig, conversionSourceProvider); | ||||
| table.insertRows(10); | ||||
| conversionController.sync(conversionConfig, conversionSourceProvider); | ||||
| checkDatasetEquivalence(DELTA, table, Collections.singletonList(ICEBERG), 40); | ||||
|
|
||||
| table.dropColumn("long_field"); | ||||
| table.insertRows(10); | ||||
| conversionController.sync(conversionConfig, conversionSourceProvider); | ||||
| checkDatasetEquivalence(DELTA, table, Collections.singletonList(ICEBERG), 50); | ||||
|
|
||||
| table.renameColumn("double_field", "scores"); | ||||
| table.insertRows(10); | ||||
| conversionController.sync(conversionConfig, conversionSourceProvider); | ||||
| checkDatasetEquivalence(DELTA, table, Collections.singletonList(ICEBERG), 60); | ||||
|
|
||||
| table.addColumn(); | ||||
| table.insertRows(10); | ||||
| conversionController.sync(conversionConfig, conversionSourceProvider); | ||||
| checkDatasetEquivalence(DELTA, table, Collections.singletonList(ICEBERG), 70); | ||||
| } | ||||
| } | ||||
|
|
||||
| @Test | ||||
| public void testMetadataRetention() throws Exception { | ||||
| String tableName = getTableName(); | ||||
|
|
||||
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.
Can you add a comment for this field?
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.
sure, will add
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.
Comment added.
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.
@xr-chen "name mapping" is a delta specific concept. The comment should describe more generally what is happening here. Something like
The name of the column in the data file used to store this field when it differs from the name in table's definitionThe comment should also describe whether this will be
nullwhen the names are the same or if the string is expected to be populatedThere 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.
updated
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.
@xr-chen will this value be null if it is not different?
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.
Yes, it will be null
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.
Can you add that detail to the comment?