-
Couldn't load subscription status.
- Fork 1.9k
[Kernel]Provide api to return per commit action iterator in get change api #5290
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
huan233usc
wants to merge
36
commits into
delta-io:master
Choose a base branch
from
huan233usc:kernel-api
base: master
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
36 commits
Select commit
Hold shift + click to select a range
af96306
poc
huan233usc 63b0bb6
poc
huan233usc 49a55e2
reuse code
huan233usc c2479bb
Merge branch 'master' into kernel-api
huan233usc b3861fe
merge master
huan233usc 5368f96
fix build
huan233usc 0991c4e
fix build
huan233usc 0829218
fmt
huan233usc 64f9725
add test
huan233usc 2d1dc9f
fix
huan233usc 7bd9d76
add doc
huan233usc ad5ccec
fmt
huan233usc 35b03da
doc
huan233usc c22cae7
fix
huan233usc 74fc278
clean up
huan233usc a3e4347
fix test
huan233usc bd7cd34
refactor
huan233usc 2c1cd78
refactor
huan233usc 5d078dc
refactor
huan233usc 811c3cc
simp
huan233usc 4afbe1f
fix
huan233usc 7223a13
fix
huan233usc e2c2350
save
huan233usc 231cfda
Address code review feedback for flatMap implementation
huan233usc a280387
simplify
huan233usc ba4a572
Address code review comments: rename flatMap to flatten and enhance d…
huan233usc 683b6b8
Update flatMap to flatten in TableChangesUtils
huan233usc b3bad95
Merge branch 'master' into kernel-api
huan233usc 6b01de6
empty
huan233usc a676055
resourcr
huan233usc 9086d87
simplify
huan233usc e79d41f
simplify
huan233usc b8c0d90
Merge remote-tracking branch 'origin/master' into kernel-api
huan233usc f37120d
define static
huan233usc c866e52
fix
huan233usc cbfaa61
fix
huan233usc 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
65 changes: 65 additions & 0 deletions
65
kernel/kernel-api/src/main/java/io/delta/kernel/CommitActions.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,65 @@ | ||
| /* | ||
| * Copyright (2025) The Delta Lake Project Authors. | ||
| * | ||
| * Licensed 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 io.delta.kernel; | ||
|
|
||
| import io.delta.kernel.annotation.Evolving; | ||
| import io.delta.kernel.data.ColumnarBatch; | ||
| import io.delta.kernel.utils.CloseableIterator; | ||
|
|
||
| /** | ||
| * Represents all actions from a single commit version in a table. | ||
| * | ||
| * <p><b>Important:</b> Each iterator returned by {@link #getActions()} must be closed after use to | ||
| * release underlying resources. | ||
| * | ||
| * @since 4.1.0 | ||
| */ | ||
| @Evolving | ||
| public interface CommitActions { | ||
|
|
||
| /** | ||
| * Returns the commit version number. | ||
| * | ||
| * @return the version number of this commit | ||
| */ | ||
| long getVersion(); | ||
|
|
||
| /** | ||
| * Returns the commit timestamp in milliseconds since Unix epoch. | ||
| * | ||
| * @return the timestamp of this commit | ||
| */ | ||
| long getTimestamp(); | ||
|
|
||
| /** | ||
| * Returns an iterator over the action batches for this commit. | ||
| * | ||
| * <p>Each {@link ColumnarBatch} contains actions from this commit only. | ||
| * | ||
| * <p>Note: All rows within all batches have the same version (returned by {@link #getVersion()}). | ||
| * | ||
| * <p>This method can be called multiple times, and each call returns a new iterator over the same | ||
| * set of batches. This supports use cases like two-pass processing (e.g., validation pass | ||
| * followed by processing pass). | ||
| * | ||
| * <p><b>Callers are responsible for closing each iterator returned by this method.</b> Each | ||
| * iterator must be closed after use to release underlying resources. | ||
| * | ||
| * @return a {@link CloseableIterator} over columnar batches containing this commit's actions | ||
| */ | ||
| CloseableIterator<ColumnarBatch> getActions(); | ||
| } | ||
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
196 changes: 196 additions & 0 deletions
196
kernel/kernel-api/src/main/java/io/delta/kernel/internal/CommitActionsImpl.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,196 @@ | ||
| /* | ||
| * Copyright (2025) The Delta Lake Project Authors. | ||
| * | ||
| * Licensed 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 io.delta.kernel.internal; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| import io.delta.kernel.CommitActions; | ||
| import io.delta.kernel.data.ColumnVector; | ||
| import io.delta.kernel.data.ColumnarBatch; | ||
| import io.delta.kernel.engine.Engine; | ||
| import io.delta.kernel.internal.actions.Protocol; | ||
| import io.delta.kernel.internal.fs.Path; | ||
| import io.delta.kernel.internal.replay.ActionWrapper; | ||
| import io.delta.kernel.internal.replay.ActionsIterator; | ||
| import io.delta.kernel.internal.tablefeatures.TableFeatures; | ||
| import io.delta.kernel.internal.util.FileNames; | ||
| import io.delta.kernel.internal.util.Preconditions; | ||
| import io.delta.kernel.types.StructField; | ||
| import io.delta.kernel.types.StructType; | ||
| import io.delta.kernel.utils.CloseableIterator; | ||
| import io.delta.kernel.utils.FileStatus; | ||
| import io.delta.kernel.utils.PeekableIterator; | ||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Implementation of {@link CommitActions}. | ||
| * | ||
| * <p>This implementation owns the commit file and supports multiple calls to {@link #getActions()}. | ||
| * The first call reuses initially-read data to avoid double I/O, while subsequent calls re-read the | ||
| * commit file for memory efficiency. | ||
| * | ||
| * <p><b>Resource Management:</b> | ||
| * | ||
| * <ul> | ||
| * <li>Calling {@link #getActions()} transfers resource ownership to the returned iterator. | ||
| * <li>Callers MUST close the returned iterator (preferably using try-with-resources) to release | ||
| * file handles and other resources. | ||
| * <li>If {@link #getActions()} is never called, internal resources will be released when the | ||
| * object is garbage collected (though explicit cleanup via {@link #getActions()} followed by | ||
| * iterator closure is preferred). | ||
| * </ul> | ||
| */ | ||
| public class CommitActionsImpl implements CommitActions { | ||
|
|
||
| private final Engine engine; | ||
| private final FileStatus commitFile; | ||
| private final StructType readSchema; | ||
| private final String tablePath; | ||
| private final boolean shouldDropProtocolColumn; | ||
| private final boolean shouldDropCommitInfoColumn; | ||
| private final long version; | ||
| private final long timestamp; | ||
|
|
||
| /** | ||
| * Peekable iterator for ActionWrappers. Supports peeking at the first element for metadata | ||
| * extraction. Used for the first getActions() call, then set to null. | ||
| */ | ||
| private PeekableIterator<ActionWrapper> peekableIterator; | ||
|
|
||
| /** | ||
| * Creates a CommitActions from a commit file. | ||
| * | ||
| * @param engine the engine for file I/O | ||
| * @param commitFile the commit file to read | ||
| * @param tablePath the table path for error messages | ||
| * @param actionSet the set of actions to read from the commit file | ||
| */ | ||
| public CommitActionsImpl( | ||
| Engine engine, | ||
| FileStatus commitFile, | ||
| String tablePath, | ||
| Set<DeltaLogActionUtils.DeltaAction> actionSet) { | ||
| requireNonNull(engine, "engine cannot be null"); | ||
| this.commitFile = requireNonNull(commitFile, "commitFile cannot be null"); | ||
| this.tablePath = requireNonNull(tablePath, "tablePath cannot be null"); | ||
|
|
||
| // Create a new action set which is a super set of the requested actions. | ||
| // The extra actions are needed either for checks or to extract | ||
| // extra information. We will strip out the extra actions before | ||
| // returning the result. | ||
| Set<DeltaLogActionUtils.DeltaAction> copySet = new HashSet<>(actionSet); | ||
| copySet.add(DeltaLogActionUtils.DeltaAction.PROTOCOL); | ||
| // commitInfo is needed to extract the inCommitTimestamp of delta files, this is used in | ||
| // ActionsIterator to resolve the timestamp when available | ||
| copySet.add(DeltaLogActionUtils.DeltaAction.COMMITINFO); | ||
| // Determine whether the additional actions were in the original set. | ||
| this.shouldDropProtocolColumn = !actionSet.contains(DeltaLogActionUtils.DeltaAction.PROTOCOL); | ||
| this.shouldDropCommitInfoColumn = | ||
| !actionSet.contains(DeltaLogActionUtils.DeltaAction.COMMITINFO); | ||
|
|
||
| this.readSchema = | ||
| new StructType( | ||
| copySet.stream() | ||
| .map(action -> new StructField(action.colName, action.schema, true)) | ||
| .collect(Collectors.toList())); | ||
| this.engine = engine; | ||
| this.peekableIterator = getNewIterator(); | ||
|
|
||
| // Extract version and timestamp from first action (or use fallback) | ||
| Optional<ActionWrapper> firstWrapper = peekableIterator.peek(); | ||
| if (firstWrapper.isPresent()) { | ||
| this.version = firstWrapper.get().getVersion(); | ||
| this.timestamp = | ||
| firstWrapper | ||
| .get() | ||
| .getTimestamp() | ||
| .orElseThrow( | ||
| () -> new RuntimeException("timestamp should always exist for Delta File")); | ||
| } else { | ||
| // Empty commit file - from file | ||
| this.version = FileNames.deltaVersion(new Path(commitFile.getPath())); | ||
| this.timestamp = commitFile.getModificationTime(); | ||
| } | ||
| } | ||
|
|
||
| private PeekableIterator<ActionWrapper> getNewIterator() { | ||
| CloseableIterator<ActionWrapper> actionsIter = | ||
| new ActionsIterator( | ||
| engine, Collections.singletonList(commitFile), readSchema, Optional.empty()); | ||
| return new PeekableIterator<>(actionsIter); | ||
| } | ||
|
|
||
| @Override | ||
| public long getVersion() { | ||
| return version; | ||
| } | ||
|
|
||
| @Override | ||
| public long getTimestamp() { | ||
| return timestamp; | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized CloseableIterator<ColumnarBatch> getActions() { | ||
| CloseableIterator<ColumnarBatch> result = | ||
| peekableIterator.map( | ||
| wrapper -> | ||
| validateProtocolAndDropInternalColumns( | ||
| wrapper.getColumnarBatch(), | ||
| tablePath, | ||
| shouldDropProtocolColumn, | ||
| shouldDropCommitInfoColumn)); | ||
| peekableIterator = getNewIterator(); | ||
| return result; | ||
| } | ||
|
|
||
| /** Validates protocol and drops protocol/commitInfo columns if not requested. */ | ||
| private static ColumnarBatch validateProtocolAndDropInternalColumns( | ||
| ColumnarBatch batch, | ||
| String tablePath, | ||
| boolean shouldDropProtocolColumn, | ||
| boolean shouldDropCommitInfoColumn) { | ||
|
|
||
| // Validate protocol if present in the batch. | ||
| int protocolIdx = batch.getSchema().indexOf("protocol"); | ||
| Preconditions.checkState(protocolIdx >= 0, "protocol column must be present in readSchema"); | ||
| ColumnVector protocolVector = batch.getColumnVector(protocolIdx); | ||
| for (int rowId = 0; rowId < protocolVector.getSize(); rowId++) { | ||
| if (!protocolVector.isNullAt(rowId)) { | ||
| Protocol protocol = Protocol.fromColumnVector(protocolVector, rowId); | ||
| TableFeatures.validateKernelCanReadTheTable(protocol, tablePath); | ||
| } | ||
| } | ||
|
|
||
| // Drop columns if not requested | ||
| ColumnarBatch result = batch; | ||
| if (shouldDropProtocolColumn && protocolIdx >= 0) { | ||
| result = result.withDeletedColumnAt(protocolIdx); | ||
| } | ||
|
|
||
| int commitInfoIdx = result.getSchema().indexOf("commitInfo"); | ||
| if (shouldDropCommitInfoColumn && commitInfoIdx >= 0) { | ||
| result = result.withDeletedColumnAt(commitInfoIdx); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } |
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.
I think it needs to be clear whether you can re-call this multiple times (new iterator each time) or if it always returns the same one. I think maybe this is a potential design decision? From an implementation standpoint either should be do-able.
Uh oh!
There was an error while loading. Please reload this page.
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 this is a good point, I rethink a bit what will be the best to serve the use case ---
The pseudo code of the actual use case is
try (CommitActions commitActions = ...) {
// First pass: validate and decide
try (CloseableIterator iter = commitActions.getActions()) {
boolean shouldSkip = validateCommitAndDecideSkipping(iter);
}
// Second pass: process the data (re-reads from file)
if (!shouldSkip) {
try (CloseableIterator iter = commitActions.getActions()) {
processActions(iter);
}
}
}
So basically three options
Only allow 1 time call (this implementation)
+Simple in kernel
-Difficult in connector: Connector needs to create a list to collect all add files so that they could do a second pass. "Create a list to collect all add files" may lead to memory pressure if commit file is large and there's no way to get around it.
-The complexity/memory pressure is just pushed to the connector layer
2.1 Allow multiple time call, implementation: kernel collects all batches in a List in CommitAction
+Simple in connector
-Memory pressure in kernel: All batches for a commit must be kept in memory
2.2 Allow multiple time call, implementation: kernel re-reads the commit file on each getActions() call
+Memory efficient: No need to cache batches in memory
+Flexible: Caller can choose to call once or multiple times based on their needs
+Supports the two-pass use case naturally
-Slight I/O overhead: Re-reads file if getActions() called multiple times
-More code complex implementation in kernel
Slight I/O overhead could actually be further improved by connector implementing a cache or if needed, we could add a hint parameter like
CommitActions.Builder.withCaching(boolean) or size threshold to let kernel
decide whether to cache in memory or re-read from file.
With those --- seems that 2.2 is the preferred one?
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.
I think 2.2 is preferred. We don't want to hold memory in Kernel, that's something the connector should implement.
In theory, 2.2 should be easy to support, but i'm not sure if it is actually that way considering we reuse internal classes that maybe don't make that sort of practice easy.