-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[WIP][Spark][kernel] Translate Kernel exceptions to DSv1 Spark/Delta exceptions #5900 #7025
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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( | ||
|
Collaborator
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. [AI Generated] Passing |
||
| 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 |
|---|---|---|
| @@ -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)); | ||
| } | ||
| } |
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.
[AI Generated]
DeltaV2Tableis shared by batch and streaming reads, but thisTABLE_RESOLUTIONpath maps every KernelTableNotFoundExceptiontoschemaNotSetException. Kernel also throwsTableNotFoundExceptionwhen_delta_logis missing, while DSv1 batch reads still surfaceDELTA_PATH_DOES_NOT_EXISTfor 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.