forked from delta-io/delta
-
Notifications
You must be signed in to change notification settings - Fork 0
#5 Introducing Deletion Vector support #1
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
murali-db
wants to merge
2
commits into
refactor-validator
Choose a base branch
from
introducing-dvs
base: refactor-validator
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.
Open
Changes from all commits
Commits
Show all changes
2 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 |
|---|---|---|
|
|
@@ -72,7 +72,7 @@ public class AddFile extends RowBackedAction { | |
|
|
||
| /** | ||
| * Utility to generate {@link AddFile} action instance from the given {@link DataFileStatus} and | ||
| * partition values. | ||
| * partition values. Use null DV descriptor when not present. | ||
| */ | ||
| public static AddFile convertDataFileStatus( | ||
| StructType physicalSchema, | ||
|
|
@@ -81,6 +81,33 @@ public static AddFile convertDataFileStatus( | |
| Map<String, Literal> partitionValues, | ||
| boolean dataChange, | ||
| Map<String, String> tags) { | ||
|
|
||
| return convertDataFileStatus( | ||
| physicalSchema, | ||
| tableRoot, | ||
| dataFileStatus, | ||
| partitionValues, | ||
| dataChange, | ||
| tags, | ||
| null); // DeletionVectorDescriptor is absent | ||
| } | ||
|
|
||
| /** | ||
| * Utility to generate {@link AddFile} action instance from the given {@link DataFileStatus} and | ||
| * partition values. | ||
| */ | ||
| public static AddFile convertDataFileStatus( | ||
| StructType physicalSchema, | ||
| URI tableRoot, | ||
| DataFileStatus dataFileStatus, | ||
| Map<String, Literal> partitionValues, | ||
| boolean dataChange, | ||
| Map<String, String> tags, | ||
| DeletionVectorDescriptor deletionVectorDescriptor) { | ||
|
|
||
| Optional<DeletionVectorDescriptor> deletionVectorOpt = | ||
| deletionVectorDescriptor != null ? Optional.of(deletionVectorDescriptor) : Optional.empty(); | ||
|
|
||
|
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. do we need to do any validations when a DV is added? for example deletion vectors feature is enabled and active? |
||
| Optional<MapValue> tagMapValue = | ||
| !tags.isEmpty() ? Optional.of(VectorUtils.stringStringMapValue(tags)) : Optional.empty(); | ||
| Row row = | ||
|
|
@@ -91,8 +118,8 @@ public static AddFile convertDataFileStatus( | |
| dataFileStatus.getSize(), | ||
| dataFileStatus.getModificationTime(), | ||
| dataChange, | ||
| Optional.empty(), // deletionVector | ||
| tagMapValue, // tags | ||
| deletionVectorOpt, | ||
| tagMapValue, | ||
| Optional.empty(), // baseRowId | ||
| Optional.empty(), // defaultRowCommitVersion | ||
| dataFileStatus.getStatistics()); | ||
|
|
@@ -116,8 +143,6 @@ public static Row createAddFileRow( | |
|
|
||
| checkArgument(path != null, "path is not nullable"); | ||
| checkArgument(partitionValues != null, "partitionValues is not nullable"); | ||
| // TODO - Add support for DeletionVectorDescriptor | ||
| checkArgument(!deletionVector.isPresent(), "DeletionVectorDescriptor is unsupported"); | ||
|
|
||
| Map<Integer, Object> fieldMap = new HashMap<>(); | ||
| fieldMap.put(FULL_SCHEMA.indexOf("path"), path); | ||
|
|
@@ -131,7 +156,20 @@ public static Row createAddFileRow( | |
| version -> fieldMap.put(FULL_SCHEMA.indexOf("defaultRowCommitVersion"), version)); | ||
| stats.ifPresent( | ||
| stat -> fieldMap.put(FULL_SCHEMA.indexOf("stats"), stat.serializeAsJson(physicalSchema))); | ||
|
|
||
| deletionVector.ifPresent( | ||
| dv -> { | ||
| Map<Integer, Object> dvFieldMap = new HashMap<>(); | ||
| StructType dvSchema = DeletionVectorDescriptor.READ_SCHEMA; | ||
|
|
||
| dvFieldMap.put(dvSchema.indexOf("storageType"), dv.getStorageType()); | ||
| dvFieldMap.put(dvSchema.indexOf("pathOrInlineDv"), dv.getPathOrInlineDv()); | ||
| dv.getOffset().ifPresent(offset -> dvFieldMap.put(dvSchema.indexOf("offset"), offset)); | ||
| dvFieldMap.put(dvSchema.indexOf("sizeInBytes"), dv.getSizeInBytes()); | ||
| dvFieldMap.put(dvSchema.indexOf("cardinality"), dv.getCardinality()); | ||
|
|
||
| Row dvRow = new GenericRow(dvSchema, dvFieldMap); | ||
| fieldMap.put(FULL_SCHEMA.indexOf("deletionVector"), dvRow); | ||
| }); | ||
| return new GenericRow(FULL_SCHEMA, fieldMap); | ||
| } | ||
|
|
||
|
|
||
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 |
|---|---|---|
|
|
@@ -34,6 +34,7 @@ | |
| import io.delta.kernel.internal.data.TransactionStateRow; | ||
| import io.delta.kernel.internal.fs.Path; | ||
| import io.delta.kernel.internal.icebergcompat.IcebergCompatV2MetadataValidatorAndUpdater; | ||
| import io.delta.kernel.internal.icebergcompat.IcebergCompatV3MetadataValidatorAndUpdater; | ||
| import io.delta.kernel.statistics.DataFileStatistics; | ||
| import io.delta.kernel.types.StructType; | ||
| import io.delta.kernel.utils.CloseableIterable; | ||
|
|
@@ -107,6 +108,66 @@ public static Row generateIcebergCompatWriterV1AddAction( | |
| transactionState, fileStatus, partitionValues, dataChange, Collections.emptyMap()); | ||
| } | ||
|
|
||
| /** | ||
| * Create an add action {@link Row} that can be passed to {@link Transaction#commit(Engine, | ||
| * CloseableIterable)} from an Iceberg add. | ||
| * | ||
| * @param transactionState the transaction state from the built transaction | ||
| * @param fileStatus the file status to create the add with (contains path, time, size, and stats) | ||
| * @param partitionValues the partition values for the add | ||
| * @param dataChange whether or not the add constitutes a dataChange (i.e. append vs. compaction) | ||
| * @param tags key-value metadata to be attached to the add action | ||
| * @return add action row that can be included in the transaction | ||
| * @throws UnsupportedOperationException if icebergWriterCompatV3 is not enabled | ||
| * @throws UnsupportedOperationException if maxRetries != 0 in the transaction | ||
| * @throws KernelException if stats are not present (required for icebergCompatV3) | ||
| * @throws UnsupportedOperationException if the table is partitioned (currently unsupported) | ||
| */ | ||
| public static Row generateIcebergCompatWriterV3AddAction( | ||
| Row transactionState, | ||
| DataFileStatus fileStatus, | ||
| Map<String, Literal> partitionValues, | ||
| boolean dataChange, | ||
| Map<String, String> tags) { | ||
| Map<String, String> configuration = TransactionStateRow.getConfiguration(transactionState); | ||
|
|
||
| /* ----- Validate that this is a valid usage of this API ----- */ | ||
| validateIcebergWriterCompatV3Enabled(configuration); | ||
| validateMaxRetriesSetToZero(transactionState); | ||
|
|
||
| /* ----- Validate this is valid write given the table's protocol & configurations ----- */ | ||
| checkState( | ||
| TableConfig.ICEBERG_COMPAT_V3_ENABLED.fromMetadata(configuration), | ||
| "icebergCompatV3 not enabled despite icebergWriterCompatV3 enabled"); | ||
| // We require field `numRecords` when icebergCompatV3 is enabled | ||
| IcebergCompatV3MetadataValidatorAndUpdater.validateDataFileStatus(fileStatus); | ||
|
|
||
| /* --- Validate and update partitionValues ---- */ | ||
| // Currently we don't support partitioned tables; fail here | ||
| blockPartitionedTables(transactionState, partitionValues); | ||
|
|
||
| URI tableRoot = new Path(TransactionStateRow.getTablePath(transactionState)).toUri(); | ||
| // This takes care of relativizing the file path and serializing the file statistics | ||
| AddFile addFile = | ||
| AddFile.convertDataFileStatus( | ||
| TransactionStateRow.getPhysicalSchema(transactionState), | ||
| tableRoot, | ||
| fileStatus, | ||
| partitionValues, | ||
| dataChange, | ||
| tags); | ||
| return SingleAction.createAddFileSingleAction(addFile.toRow()); | ||
| } | ||
|
|
||
| public static Row generateIcebergCompatWriterV3AddAction( | ||
|
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 for this API. |
||
| Row transactionState, | ||
| DataFileStatus fileStatus, | ||
| Map<String, Literal> partitionValues, | ||
| boolean dataChange) { | ||
| return generateIcebergCompatWriterV3AddAction( | ||
| transactionState, fileStatus, partitionValues, dataChange, Collections.emptyMap()); | ||
| } | ||
|
|
||
| /** | ||
| * Create a remove action {@link Row} that can be passed to {@link Transaction#commit(Engine, | ||
| * CloseableIterable)} from an Iceberg remove. | ||
|
|
@@ -157,6 +218,56 @@ public static Row generateIcebergCompatWriterV1RemoveAction( | |
| return SingleAction.createRemoveFileSingleAction(removeFileRow); | ||
| } | ||
|
|
||
| /** | ||
| * Create a remove action {@link Row} that can be passed to {@link Transaction#commit(Engine, | ||
| * CloseableIterable)} from an Iceberg remove. | ||
| * | ||
| * @param transactionState the transaction state from the built transaction | ||
| * @param fileStatus the file status to create the remove with (contains path, time, size, and | ||
| * stats) | ||
| * @param partitionValues the partition values for the remove | ||
| * @param dataChange whether or not the remove constitutes a dataChange (i.e. delete vs. | ||
| * compaction) | ||
| * @return remove action row that can be committed to the transaction | ||
| * @throws UnsupportedOperationException if icebergWriterCompatV3 is not enabled | ||
| * @throws UnsupportedOperationException if maxRetries != 0 in the transaction | ||
| * @throws KernelException if the table is an append-only table and dataChange=true | ||
| * @throws UnsupportedOperationException if the table is partitioned (currently unsupported) | ||
| */ | ||
| public static Row generateIcebergCompatWriterV3RemoveAction( | ||
| Row transactionState, | ||
| DataFileStatus fileStatus, | ||
| Map<String, Literal> partitionValues, | ||
| boolean dataChange) { | ||
| Map<String, String> config = TransactionStateRow.getConfiguration(transactionState); | ||
|
|
||
| /* ----- Validate that this is a valid usage of this API ----- */ | ||
| validateIcebergWriterCompatV3Enabled(config); | ||
| validateMaxRetriesSetToZero(transactionState); | ||
|
|
||
| /* ----- Validate this is valid write given the table's protocol & configurations ----- */ | ||
| // We only allow removes with dataChange=false when appendOnly=true | ||
| if (dataChange && TableConfig.APPEND_ONLY_ENABLED.fromMetadata(config)) { | ||
| throw DeltaErrors.cannotModifyAppendOnlyTable( | ||
| TransactionStateRow.getTablePath(transactionState)); | ||
| } | ||
|
|
||
| /* --- Validate and update partitionValues ---- */ | ||
| // Currently we don't support partitioned tables; fail here | ||
| blockPartitionedTables(transactionState, partitionValues); | ||
|
|
||
| URI tableRoot = new Path(TransactionStateRow.getTablePath(transactionState)).toUri(); | ||
| // This takes care of relativizing the file path and serializing the file statistics | ||
| Row removeFileRow = | ||
| convertRemoveDataFileStatus( | ||
| TransactionStateRow.getPhysicalSchema(transactionState), | ||
| tableRoot, | ||
| fileStatus, | ||
| partitionValues, | ||
| dataChange); | ||
| return SingleAction.createRemoveFileSingleAction(removeFileRow); | ||
| } | ||
|
|
||
| ///////////////////// | ||
| // Private helpers // | ||
| ///////////////////// | ||
|
|
@@ -177,6 +288,21 @@ private static void validateIcebergWriterCompatV1Enabled(Map<String, String> con | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validates that table feature `icebergWriterCompatV3` is enabled. We restrict usage of these | ||
| * APIs to require that this table feature is enabled to prevent any unsafe usage due to the table | ||
| * features that are blocked via `icebergWriterCompatV3`. | ||
| */ | ||
| private static void validateIcebergWriterCompatV3Enabled(Map<String, String> config) { | ||
| if (!TableConfig.ICEBERG_WRITER_COMPAT_V3_ENABLED.fromMetadata(config)) { | ||
| throw new UnsupportedOperationException( | ||
| String.format( | ||
| "APIs within GenerateIcebergCompatActionUtils are only supported on tables with" | ||
| + " '%s' set to true", | ||
| TableConfig.ICEBERG_WRITER_COMPAT_V3_ENABLED.getKey())); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Throws an exception if `maxRetries` was not set to 0 in the transaction. We restrict these APIs | ||
| * to require `maxRetries = 0` since conflict resolution is not supported for operations other | ||
|
|
||
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
Oops, something went wrong.
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.
make the existing API take the deletionVectorDescription Optional. Given this is not a public API, we don't need to worry about the existing users and migration is simple anyways.