Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
af96306
poc
huan233usc Oct 2, 2025
63b0bb6
poc
huan233usc Oct 2, 2025
49a55e2
reuse code
huan233usc Oct 14, 2025
c2479bb
Merge branch 'master' into kernel-api
huan233usc Oct 14, 2025
b3861fe
merge master
huan233usc Oct 14, 2025
5368f96
fix build
huan233usc Oct 14, 2025
0991c4e
fix build
huan233usc Oct 14, 2025
0829218
fmt
huan233usc Oct 14, 2025
64f9725
add test
huan233usc Oct 14, 2025
2d1dc9f
fix
huan233usc Oct 14, 2025
7bd9d76
add doc
huan233usc Oct 14, 2025
ad5ccec
fmt
huan233usc Oct 14, 2025
35b03da
doc
huan233usc Oct 14, 2025
c22cae7
fix
huan233usc Oct 14, 2025
74fc278
clean up
huan233usc Oct 14, 2025
a3e4347
fix test
huan233usc Oct 14, 2025
bd7cd34
refactor
huan233usc Oct 20, 2025
2c1cd78
refactor
huan233usc Oct 20, 2025
5d078dc
refactor
huan233usc Oct 20, 2025
811c3cc
simp
huan233usc Oct 21, 2025
4afbe1f
fix
huan233usc Oct 21, 2025
7223a13
fix
huan233usc Oct 21, 2025
e2c2350
save
huan233usc Oct 23, 2025
231cfda
Address code review feedback for flatMap implementation
huan233usc Oct 23, 2025
a280387
simplify
huan233usc Oct 23, 2025
ba4a572
Address code review comments: rename flatMap to flatten and enhance d…
huan233usc Oct 23, 2025
683b6b8
Update flatMap to flatten in TableChangesUtils
huan233usc Oct 23, 2025
b3bad95
Merge branch 'master' into kernel-api
huan233usc Oct 23, 2025
6b01de6
empty
huan233usc Oct 23, 2025
a676055
resourcr
huan233usc Oct 23, 2025
9086d87
simplify
huan233usc Oct 23, 2025
e79d41f
simplify
huan233usc Oct 23, 2025
b8c0d90
Merge remote-tracking branch 'origin/master' into kernel-api
huan233usc Oct 23, 2025
f37120d
define static
huan233usc Oct 23, 2025
c866e52
fix
huan233usc Oct 24, 2025
cbfaa61
fix
huan233usc Oct 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions kernel/kernel-api/src/main/java/io/delta/kernel/CommitActions.java
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();
Copy link
Collaborator

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.

Copy link
Collaborator Author

@huan233usc huan233usc Oct 17, 2025

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

  • For large commits with many actions, this could be problematic
  • The memory is held even if caller only needs one pass
    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?

Copy link
Collaborator

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.

}
18 changes: 18 additions & 0 deletions kernel/kernel-api/src/main/java/io/delta/kernel/CommitRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,22 @@ public interface CommitRange {
*/
CloseableIterator<ColumnarBatch> getActions(
Engine engine, Snapshot startSnapshot, Set<DeltaLogActionUtils.DeltaAction> actionSet);

/**
* Returns an iterator of commits in this commit range, where each commit is represented as a
* {@link CommitActions} object.
*
* @param engine the {@link Engine} to use for reading the Delta log files
* @param startSnapshot the snapshot for startVersion, required to ensure the table is readable by
* Kernel at startVersion
* @param actionSet the set of action types to include in the results. Only actions of these types
* will be returned in each commit's actions iterator
* @return a {@link CloseableIterator} over {@link CommitActions}, one per commit version in this
* range
* @throws IllegalArgumentException if startSnapshot.getVersion() != startVersion
* @throws KernelException if the version range contains a version with reader protocol that is
* unsupported by Kernel
*/
CloseableIterator<CommitActions> getCommitActions(
Engine engine, Snapshot startSnapshot, Set<DeltaLogActionUtils.DeltaAction> actionSet);
}
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;
}
}
Loading
Loading