-
Notifications
You must be signed in to change notification settings - Fork 198
Delta kernel conversion target #801
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
vaibhavk1992
wants to merge
58
commits into
apache:main
Choose a base branch
from
vaibhavk1992:test-4
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.
Open
Changes from all commits
Commits
Show all changes
58 commits
Select commit
Hold shift + click to select a range
dfea6a4
single commit
vaibhavk1992 b75bc7c
adding delta kernel
vaibhavk1992 16134b3
adding the test file
vaibhavk1992 3929e95
adding workable code for iteration over data
vaibhavk1992 c6379b5
adding Kernel 4.0 code
vaibhavk1992 6deb5f7
adding the working code with xtable that check getcurrenttable
vaibhavk1992 05d9984
Merge branch 'main' into test-4
vaibhavk1992 c7ba4b9
adding the dependecies
vaibhavk1992 0ff36a5
adding getcurrentsnapshot code
vaibhavk1992 18ab9d6
spotless fix
vaibhavk1992 e906091
spotless fix 2
vaibhavk1992 e00241c
spotless fix 2
vaibhavk1992 3fdfd31
fixed partitioned test case
vaibhavk1992 e0102e3
setting junit parallel execution to true
vaibhavk1992 381722a
testInsertsUpsertsAndDeletes test case addition,internal datatype add…
vaibhavk1992 809bfe8
added the fix for table basepath listing wrong paths
vaibhavk1992 40172f2
added the fix for table basepath listing wrong paths
vaibhavk1992 e0b7829
adding all tests
vaibhavk1992 9ac022a
adding refactored code
vaibhavk1992 73f33b6
spotless fix
vaibhavk1992 bee3e8a
fix change extraction
e75bb55
adding the commitbacklog test cases changes
vaibhavk1992 21044af
Merge branch 'apache:main' into test-4
vaibhavk1992 e212f52
adding a test case testConvertFromDeltaPartitionFormat
vaibhavk1992 988cda1
adding a test case testConvertFromDeltaPartitionFormat
vaibhavk1992 1705ce4
adding the KernelPartitionExtractor test under kernel
vaibhavk1992 8f81109
commiting schema extractor and stats extrator
vaibhavk1992 49ebf21
adding unit test cases with the request changes on the PR
vaibhavk1992 70fe0e3
spotless fix
vaibhavk1992 fba7e0e
spotless fix
vaibhavk1992 bb4dc4e
Merge branch 'main' into test-4
vaibhavk1992 6b1be2d
adding haddop common in xtable service POM
vaibhavk1992 2f46699
changed map type to java and removed print commands
vaibhavk1992 70469fb
changed map type to java and removed print commands
vaibhavk1992 ae61a28
removing hadoop common from xtable service
vaibhavk1992 a6f86ac
fixing POM
vaibhavk1992 cd30bab
resolving some minor comments from review
vaibhavk1992 cecf300
changing constructor for Datafile extractor
vaibhavk1992 253de3f
add exclusion hadoop-client-runtime in POM
vaibhavk1992 019855b
removing unused code and string comparison method
vaibhavk1992 977df2f
removing while True condition
vaibhavk1992 3a1df45
Merge branch 'apache:main' into test-4
vaibhavk1992 2ea30a8
Add Delta Kernel integration with disabled tests
vaibhavk1992 5d695cc
Merge remote-tracking branch 'upstream/main' into test-4
vaibhavk1992 10f7fbe
adding the conversion target files
vaibhavk1992 de57c88
corrected the test cases
vaibhavk1992 0345140
spotless fix
vaibhavk1992 b9f27af
adding read write integration test case
vaibhavk1992 2af1236
addressed comments over PR
vaibhavk1992 2d0e16e
addressed comments over PR
vaibhavk1992 ab0417c
addressed comments over PR
vaibhavk1992 935d835
adding data types
vaibhavk1992 23f6321
dummy commit to trigger actions
vaibhavk1992 cf5029f
Merge upstream/main into test-4
vaibhavk1992 d622ae7
Apply spotless formatting to remove wildcard imports
vaibhavk1992 66eb9df
Fix missing imports after spotless wildcard removal
vaibhavk1992 d405870
adding read write integration test case
vaibhavk1992 37caddf
Fix exception handling, Scala/Java mixing, and test quality in Delta …
vaibhavk1992 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
481 changes: 481 additions & 0 deletions
481
xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelConversionTarget.java
Large diffs are not rendered by default.
Oops, something went wrong.
230 changes: 230 additions & 0 deletions
230
xtable-core/src/main/java/org/apache/xtable/kernel/DeltaKernelDataFileUpdatesExtractor.java
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 |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.xtable.kernel; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import lombok.Builder; | ||
|
|
||
| import scala.collection.JavaConverters; | ||
| import scala.collection.Seq; | ||
|
|
||
| import io.delta.kernel.Snapshot; | ||
| import io.delta.kernel.Table; | ||
| import io.delta.kernel.data.MapValue; | ||
| import io.delta.kernel.data.Row; | ||
| import io.delta.kernel.engine.Engine; | ||
| import io.delta.kernel.internal.actions.AddFile; | ||
| import io.delta.kernel.internal.actions.RemoveFile; | ||
| import io.delta.kernel.internal.actions.RowBackedAction; | ||
| import io.delta.kernel.internal.util.VectorUtils; | ||
| import io.delta.kernel.types.StructType; | ||
|
|
||
| import org.apache.xtable.collectors.CustomCollectors; | ||
| import org.apache.xtable.exception.ReadException; | ||
| import org.apache.xtable.model.schema.InternalSchema; | ||
| import org.apache.xtable.model.storage.FilesDiff; | ||
| import org.apache.xtable.model.storage.InternalDataFile; | ||
| import org.apache.xtable.model.storage.InternalFile; | ||
| import org.apache.xtable.model.storage.InternalFilesDiff; | ||
| import org.apache.xtable.model.storage.PartitionFileGroup; | ||
| import org.apache.xtable.paths.PathUtils; | ||
| import org.apache.xtable.spi.extractor.DataFileIterator; | ||
|
|
||
| @Builder | ||
| public class DeltaKernelDataFileUpdatesExtractor { | ||
| @Builder.Default | ||
| private final DeltaKernelStatsExtractor deltaStatsExtractor = | ||
| DeltaKernelStatsExtractor.getInstance(); | ||
|
|
||
| @Builder.Default | ||
| private final DeltaKernelPartitionExtractor deltaKernelPartitionExtractor = | ||
| DeltaKernelPartitionExtractor.getInstance(); | ||
|
|
||
| @Builder.Default | ||
| private final DeltaKernelDataFileExtractor dataFileExtractor = | ||
| DeltaKernelDataFileExtractor.builder().build(); | ||
|
|
||
| private final Engine engine; | ||
| private final String basePath; | ||
| private final boolean includeColumnStats; | ||
|
|
||
| public Seq<RowBackedAction> applySnapshot( | ||
| Table table, List<PartitionFileGroup> partitionedDataFiles, InternalSchema tableSchema) { | ||
|
|
||
| // all files in the current delta snapshot are potential candidates for remove actions, i.e. if | ||
| // the file is not present in the new snapshot (addedFiles) then the file is considered removed | ||
| Map<String, RowBackedAction> previousFiles = new HashMap<>(); | ||
| StructType physicalSchema; | ||
|
|
||
| // Check if table exists by checking if _delta_log directory exists | ||
| boolean tableExists = checkTableExists(table); | ||
|
|
||
| if (tableExists) { | ||
| Snapshot snapshot = table.getLatestSnapshot(engine); | ||
|
|
||
| // Reuse DeltaKernelDataFileExtractor to iterate through existing files | ||
| // This avoids duplicating the scan logic for reading Delta files | ||
| try (DataFileIterator fileIterator = | ||
| dataFileExtractor.iterator(snapshot, table, engine, tableSchema)) { | ||
|
|
||
| while (fileIterator.hasNext()) { | ||
| InternalDataFile internalFile = fileIterator.next(); | ||
|
|
||
| // Convert InternalDataFile back to AddFile to create RemoveFile action | ||
| AddFile addFile = createAddFileFromInternalDataFile(internalFile, snapshot.getSchema()); | ||
| RemoveFile removeFile = | ||
| new RemoveFile(addFile.toRemoveFileRow(false, Optional.of(snapshot.getVersion()))); | ||
| String fullPath = | ||
| DeltaKernelActionsConverter.getFullPathToFile(removeFile.getPath(), table); | ||
| previousFiles.put(fullPath, removeFile); | ||
| } | ||
| } catch (Exception e) { | ||
| throw new ReadException("Failed to scan existing Delta files", e); | ||
| } | ||
|
|
||
| physicalSchema = snapshot.getSchema(); | ||
|
|
||
| } else { | ||
|
|
||
| // Table doesn't exist yet - no previous files to remove | ||
| // Convert InternalSchema to StructType for physical schema | ||
| DeltaKernelSchemaExtractor schemaExtractor = DeltaKernelSchemaExtractor.getInstance(); | ||
| physicalSchema = schemaExtractor.fromInternalSchema(tableSchema); | ||
| } | ||
|
|
||
| FilesDiff<InternalFile, RowBackedAction> diff = | ||
| InternalFilesDiff.findNewAndRemovedFiles(partitionedDataFiles, previousFiles); | ||
|
|
||
| return applyDiff( | ||
| diff.getFilesAdded(), | ||
| diff.getFilesRemoved(), | ||
| tableSchema, | ||
| table.getPath(engine), | ||
| physicalSchema); | ||
| } | ||
|
|
||
| private boolean checkTableExists(Table table) { | ||
vaibhavk1992 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return DeltaKernelUtils.tableExists(engine, table.getPath(engine)); | ||
| } | ||
|
|
||
| /** | ||
| * Converts an InternalDataFile back to Delta Kernel's AddFile action. This is needed to create | ||
| * RemoveFile actions from existing files. | ||
| */ | ||
| private AddFile createAddFileFromInternalDataFile( | ||
| InternalDataFile internalFile, StructType physicalSchema) { | ||
| // Extract partition values from InternalDataFile using existing logic | ||
| Map<String, String> partitionValuesMap = | ||
| deltaKernelPartitionExtractor.partitionValueSerialization(internalFile); | ||
| MapValue partitionValues = convertToMapValue(partitionValuesMap); | ||
|
|
||
| // Create AddFile Row using the same pattern as createAddFileAction | ||
| Row addFileRow = | ||
| AddFile.createAddFileRow( | ||
| physicalSchema, | ||
| PathUtils.getRelativePath(internalFile.getPhysicalPath(), basePath), | ||
| partitionValues, | ||
| internalFile.getFileSizeBytes(), | ||
| internalFile.getLastModified(), | ||
| true, // dataChange - assume true for existing files | ||
| Optional.empty(), // deletionVector | ||
| Optional.empty(), // tags | ||
| Optional.empty(), // baseRowId | ||
| Optional.empty(), // defaultRowCommitVersion | ||
| Optional.empty()); // stats - set to empty since we're creating RemoveFile | ||
|
|
||
| // Wrap the Row back into an AddFile object | ||
| return new AddFile(addFileRow); | ||
| } | ||
|
|
||
| public Seq<RowBackedAction> applyDiff( | ||
| InternalFilesDiff internalFilesDiff, | ||
| InternalSchema tableSchema, | ||
| String tableBasePath, | ||
| StructType physicalSchema) { | ||
| List<RowBackedAction> removeActions = | ||
| internalFilesDiff.dataFilesRemoved().stream() | ||
| .map(dFile -> createAddFileAction(dFile, tableBasePath, physicalSchema)) | ||
| .map(addFile -> new RemoveFile(addFile.toRemoveFileRow(false, Optional.empty()))) | ||
| .collect(CustomCollectors.toList(internalFilesDiff.dataFilesRemoved().size())); | ||
| return applyDiff( | ||
| internalFilesDiff.dataFilesAdded(), | ||
| removeActions, | ||
| tableSchema, | ||
| tableBasePath, | ||
| physicalSchema); | ||
| } | ||
|
|
||
| private Seq<RowBackedAction> applyDiff( | ||
| Set<? extends InternalFile> filesAdded, | ||
| Collection<RowBackedAction> removeFileActions, | ||
| InternalSchema tableSchema, | ||
| String tableBasePath, | ||
| StructType physicalSchema) { | ||
| Stream<RowBackedAction> addActions = | ||
| filesAdded.stream() | ||
| .filter(InternalDataFile.class::isInstance) | ||
| .map(file -> (InternalDataFile) file) | ||
| .map(dFile -> createAddFileAction(dFile, tableBasePath, physicalSchema)); | ||
| int totalActions = filesAdded.size() + removeFileActions.size(); | ||
| List<RowBackedAction> allActions = | ||
| Stream.concat(addActions, removeFileActions.stream()) | ||
| .collect(CustomCollectors.toList(totalActions)); | ||
| return JavaConverters.asScalaBuffer(allActions).toSeq(); | ||
| } | ||
|
|
||
| private AddFile createAddFileAction( | ||
| InternalDataFile dataFile, String tableBasePath, StructType physicalSchema) { | ||
| // Convert partition values from Map<String, String> to MapValue | ||
| Map<String, String> partitionValuesMap = | ||
| deltaKernelPartitionExtractor.partitionValueSerialization(dataFile); | ||
| MapValue partitionValues = convertToMapValue(partitionValuesMap); | ||
|
|
||
| Row addFileRow = | ||
| AddFile.createAddFileRow( | ||
| physicalSchema, | ||
| // Delta Lake supports relative and absolute paths in theory but relative paths seem | ||
| // more commonly supported by query engines in our testing | ||
| PathUtils.getRelativePath(dataFile.getPhysicalPath(), tableBasePath), | ||
| partitionValues, | ||
| dataFile.getFileSizeBytes(), | ||
| dataFile.getLastModified(), | ||
| true, // dataChange | ||
| Optional.empty(), // deletionVector | ||
| Optional.empty(), // tags | ||
| Optional.empty(), // baseRowId | ||
| Optional.empty(), // defaultRowCommitVersion | ||
| Optional.empty() // stats - TODO: convert column stats to DataFileStatistics | ||
| ); | ||
|
|
||
| // Wrap the Row back into an AddFile object so we can use its methods | ||
| return new AddFile(addFileRow); | ||
| } | ||
|
|
||
| private MapValue convertToMapValue(Map<String, String> map) { | ||
| return VectorUtils.stringStringMapValue(map); | ||
| } | ||
| } | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.