Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ object DeltaV2SourceSuite {
"streaming read preserves percent-literal string partition value",
"initial snapshot: checkpoint resume produces all rows without duplicates",
"initial snapshot: Trigger.AvailableNow processes all data and terminates",
"initial snapshot: checkpoint resume after new commits produces all rows"
"initial snapshot: checkpoint resume after new commits produces all rows",
"no schema should throw an exception",
"Delta sources should verify the protocol reader version"
)

val FailingTests: Set[String] = Set(
Expand All @@ -181,10 +183,6 @@ object DeltaV2SourceSuite {
"incremental: commit file gap between versions, failOnDataLoss=false succeeds",

// === Misc ===
// TODO(#5900): fix exception mismatch
"no schema should throw an exception",
// TODO(#5900): fix exception mismatch
"Delta sources should verify the protocol reader version",
// TODO(#5895): gracefully handle corrupt checkpoint
"start from corrupt checkpoint",

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
import io.delta.kernel.Snapshot;
import io.delta.kernel.defaults.engine.DefaultEngine;
import io.delta.kernel.engine.Engine;
import io.delta.kernel.exceptions.KernelException;
import io.delta.kernel.internal.SnapshotImpl;
import io.delta.kernel.internal.rowtracking.RowTracking;
import io.delta.spark.internal.v2.exception.KernelExceptionConverter;
import io.delta.spark.internal.v2.exception.Operation;
import io.delta.spark.internal.v2.read.MetadataEvolutionHandler;
import io.delta.spark.internal.v2.read.SparkScanBuilder;
import io.delta.spark.internal.v2.read.cdc.CDCSchemaContext;
Expand Down Expand Up @@ -197,8 +200,14 @@ private DeltaV2Table(
SparkSession.active().sessionState().newHadoopConfWithOptions(toScalaMap(options));
this.kernelEngine = DefaultEngine.create(this.hadoopConf);
this.snapshotManager = SnapshotManagerFactory.create(tablePath, kernelEngine, catalogTable);
// Load the initial snapshot through the manager
this.initialSnapshot = snapshotManager.loadLatestSnapshot();
Snapshot loadedSnapshot;
try {
loadedSnapshot = snapshotManager.loadLatestSnapshot();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI Generated] DeltaV2Table is shared by batch and streaming reads, but this TABLE_RESOLUTION path maps every Kernel TableNotFoundException to schemaNotSetException. Kernel also throws TableNotFoundException when _delta_log is missing, while DSv1 batch reads still surface DELTA_PATH_DOES_NOT_EXIST for a missing path. Can we distinguish the batch vs streaming/table-state cases here, or add a missing-path batch regression test under STRICT mode? Otherwise this fixes the empty-log streaming test by changing the missing-path batch error.

} catch (KernelException e) {
// Surface the same exceptions the DSv1 connector raises (delta-io/delta#5900).
throw KernelExceptionConverter.translateAndThrow(e, tablePath, Operation.TABLE_RESOLUTION);
}
this.initialSnapshot = loadedSnapshot;

this.isCDCRead = CDCReader.isCDCRead(new CaseInsensitiveStringMap(this.options));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (2026) 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.spark.internal.v2.exception;

import io.delta.kernel.exceptions.KernelException;
import io.delta.kernel.exceptions.TableNotFoundException;
import io.delta.kernel.exceptions.UnsupportedProtocolVersionException;
import java.util.HashMap;
import java.util.Map;
import org.apache.spark.sql.delta.DeltaErrors;
import org.apache.spark.sql.delta.InvalidProtocolVersionException;
import org.apache.spark.sql.delta.actions.Action$;

/**
* Translates Delta Kernel exceptions into the Spark/Delta exceptions.
*
* <p>Mappings live in a class-to-handler registry, lookup walks the exception's class hierarchy and
* kernel exceptions without a registered handler are returned unchanged. To add a mapping, register
* one more handler below.
*/
public final class KernelExceptionConverter {

private KernelExceptionConverter() {}

@FunctionalInterface
private interface Handler {
Throwable translate(KernelException e, String tableNameOrPath, Operation op);
}

private static final Map<Class<? extends KernelException>, Handler> HANDLERS = new HashMap<>();

static {
HANDLERS.put(TableNotFoundException.class, KernelExceptionConverter::translateTableNotFound);
HANDLERS.put(
UnsupportedProtocolVersionException.class,
KernelExceptionConverter::translateUnsupportedProtocolVersion);
}

/** Returns the Spark/Delta equivalent of {@code e}, or {@code e} unchanged. */
public static Throwable convert(KernelException e, String tableNameOrPath, Operation op) {
for (Class<?> c = e.getClass();
c != null && KernelException.class.isAssignableFrom(c);
c = c.getSuperclass()) {
Handler handler = HANDLERS.get(c);
if (handler != null) {
return handler.translate(e, tableNameOrPath, op);
}
}
return e;
}

/** Converts {@code e} and throws the result. */
public static RuntimeException translateAndThrow(
KernelException e, String tableNameOrPath, Operation op) {
throw sneakyThrow(convert(e, tableNameOrPath, op));
}

private static Throwable translateTableNotFound(
KernelException e, String tableNameOrPath, Operation op) {
return op == Operation.TABLE_RESOLUTION
? DeltaErrors.schemaNotSetException()
: DeltaErrors.pathNotExistsException(tableNameOrPath);
}

private static Throwable translateUnsupportedProtocolVersion(
KernelException e, String tableNameOrPath, Operation op) {
UnsupportedProtocolVersionException source = (UnsupportedProtocolVersionException) e;
boolean isReader =
source.getVersionType() == UnsupportedProtocolVersionException.ProtocolVersionType.READER;
return new InvalidProtocolVersionException(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AI Generated] Passing 0 for whichever protocol side Kernel did not flag makes the Spark/Delta error parameters inaccurate. DSv1 constructs InvalidProtocolVersionException from the full table protocol, so an unsupported reader protocol still reports the table's actual minWriterVersion in the user-facing DELTA_INVALID_PROTOCOL_VERSION message. Can we carry both required protocol versions through Kernel, or avoid this translation until the converter can preserve both values? The unit tests should also assert both readerRequiredVersion and writerRequiredVersion so this does not regress silently.

tableNameOrPath,
isReader ? source.getVersion() : 0,
isReader ? 0 : source.getVersion(),
Action$.MODULE$.supportedReaderVersionNumbers().toList(),
Action$.MODULE$.supportedWriterVersionNumbers().toList());
}

@SuppressWarnings("unchecked")
private static <E extends Throwable> E sneakyThrow(Throwable t) throws E {
throw (E) t;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (2026) 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.spark.internal.v2.exception;

/**
* The connector operation during which a Kernel exception was raised. Lets {@link
* KernelExceptionConverter} map the same Kernel exception to different Spark/Delta exceptions per
* operation.
*/
public enum Operation {
/** Resolving a table reference. */
TABLE_RESOLUTION,
/** Streaming micro-batch execution. */
STREAMING_MICROBATCH
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.delta.kernel.data.FilteredColumnarBatch;
import io.delta.kernel.defaults.engine.DefaultEngine;
import io.delta.kernel.engine.Engine;
import io.delta.kernel.exceptions.KernelException;
import io.delta.kernel.exceptions.UnsupportedTableFeatureException;
import io.delta.kernel.internal.DeltaHistoryManager;
import io.delta.kernel.internal.DeltaLogActionUtils.DeltaAction;
Expand All @@ -44,6 +45,8 @@
import io.delta.kernel.internal.util.VectorUtils;
import io.delta.kernel.utils.CloseableIterator;
import io.delta.spark.internal.v2.adapters.KernelMetadataAdapter;
import io.delta.spark.internal.v2.exception.KernelExceptionConverter;
import io.delta.spark.internal.v2.exception.Operation;
import io.delta.spark.internal.v2.snapshot.DeltaSnapshotManager;
import io.delta.spark.internal.v2.utils.PartitionUtils;
import io.delta.spark.internal.v2.utils.ScalaUtils;
Expand Down Expand Up @@ -499,6 +502,10 @@ public Offset latestOffset(Offset startOffset, ReadLimit limit) {
if (interruptCause.isPresent()) {
throw new UncheckedIOException(interruptCause.get());
}
if (e instanceof KernelException) {
throw KernelExceptionConverter.translateAndThrow(
(KernelException) e, tablePath, Operation.STREAMING_MICROBATCH);
}
throw e;
}
}
Expand Down Expand Up @@ -664,6 +671,10 @@ public InputPartition[] planInputPartitions(Offset start, Offset end) {
if (interruptCause.isPresent()) {
throw new UncheckedIOException(interruptCause.get());
}
if (e instanceof KernelException) {
throw KernelExceptionConverter.translateAndThrow(
(KernelException) e, tablePath, Operation.STREAMING_MICROBATCH);
}
throw e;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (2026) 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.spark.internal.v2.exception;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.delta.kernel.exceptions.KernelException;
import io.delta.kernel.exceptions.TableNotFoundException;
import io.delta.kernel.exceptions.UnsupportedProtocolVersionException;
import org.apache.spark.sql.AnalysisException;
import org.apache.spark.sql.delta.InvalidProtocolVersionException;
import org.junit.jupiter.api.Test;

/** Unit tests for {@link KernelExceptionConverter}. */
public class KernelExceptionConverterTest {

private static final String PATH = "/tmp/some/table";

@Test
public void tableNotFound_tableResolution_mapsToSchemaNotSet() {
Throwable translated =
KernelExceptionConverter.convert(
new TableNotFoundException(PATH), PATH, Operation.TABLE_RESOLUTION);

AnalysisException ae = assertInstanceOf(AnalysisException.class, translated);
assertTrue(
ae.getMessage().contains("Table schema is not set"),
"expected 'Table schema is not set', got: " + ae.getMessage());
assertTrue(
ae.getMessage().contains("CREATE TABLE"),
"expected 'CREATE TABLE', got: " + ae.getMessage());
}

@Test
public void tableNotFound_microBatch_mapsToPathDoesNotExist() {
Throwable translated =
KernelExceptionConverter.convert(
new TableNotFoundException(PATH), PATH, Operation.STREAMING_MICROBATCH);

AnalysisException ae = assertInstanceOf(AnalysisException.class, translated);
assertTrue(
ae.getMessage().contains(PATH) && ae.getMessage().contains("doesn't exist"),
"expected a path-does-not-exist message, got: " + ae.getMessage());
}

@Test
public void unsupportedReaderProtocol_mapsToInvalidProtocolVersion() {
int badVersion = Integer.MAX_VALUE;
Throwable translated =
KernelExceptionConverter.convert(
new UnsupportedProtocolVersionException(
PATH, badVersion, UnsupportedProtocolVersionException.ProtocolVersionType.READER),
PATH,
Operation.STREAMING_MICROBATCH);

InvalidProtocolVersionException ipve =
assertInstanceOf(InvalidProtocolVersionException.class, translated);
assertEquals(PATH, ipve.tableNameOrPath());
assertEquals(badVersion, ipve.readerRequiredVersion());
assertTrue(
ipve.supportedReaderVersions().nonEmpty(), "expected non-empty supported reader versions");
}

@Test
public void unregisteredKernelException_returnedUnchanged() {
KernelException original = new KernelException("some unmapped kernel failure");
Throwable translated =
KernelExceptionConverter.convert(original, PATH, Operation.TABLE_RESOLUTION);

assertSame(original, translated, "unmapped Kernel exceptions must pass through unchanged");
}

@Test
public void translateAndThrow_throwsTranslatedException() {
assertThrows(
InvalidProtocolVersionException.class,
() ->
KernelExceptionConverter.translateAndThrow(
new UnsupportedProtocolVersionException(
PATH,
Integer.MAX_VALUE,
UnsupportedProtocolVersionException.ProtocolVersionType.WRITER),
PATH,
Operation.STREAMING_MICROBATCH));
}
}