You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "j1wonpark (via GitHub)" <gi...@apache.org> on 2026/07/05 12:49:17 UTC

[PR] [SPARK-57933][CONNECT] Map Spark Connect client failures to SQLException in the JDBC driver [spark]

j1wonpark opened a new pull request, #57009:
URL: https://github.com/apache/spark/pull/57009

   ### What changes were proposed in this pull request?
   
   Two commits:
   
   1. `GrpcExceptionConverter` now preserves the original `StatusRuntimeException`
      as the cause of fallback errors (errors without `ErrorInfo`), generalizing
      what the `DEADLINE_EXCEEDED` branch already did for the same reason.
   2. The Connect JDBC driver maps client failures to `SQLException` at the two
      points where they cross the JDBC boundary: `SparkConnectStatement.execute`
      (`executeQuery`/`executeUpdate` route through it) and
      `SparkConnectResultSet.next` (rows are fetched lazily, so errors during
      result streaming surface there).
   
   | Error | Mapped to | SQLState |
   |---|---|---|
   | `INVALID_HANDLE.SESSION_*` (session is gone) | `SQLNonTransientConnectionException` | `08003` |
   | gRPC `UNAVAILABLE` | `SQLTransientConnectionException` | `08006` |
   | gRPC `DEADLINE_EXCEEDED` | `SQLTimeoutException` | `HYT00` |
   | Anything else | `SQLException` | server-provided |
   
   The gRPC status is read only from the `StatusRuntimeException` preserved in
   the cause chain (commit 1), never from message text, so a server-side error
   that merely quotes a gRPC exception cannot be mistaken for a transport
   failure. The original exception is always kept as the cause.
   
   Notes on the mapping:
   - Operation-level `INVALID_HANDLE` subconditions (`OPERATION_*`, `FORMAT`)
     are not connection errors: the session is still healthy.
   - `DEADLINE_EXCEEDED` is a timeout, not a connection failure: a slow query
     fires it on a healthy connection, and the client's own `RetryPolicy`
     deliberately excludes it from retryable errors.
   
   Other JDBC entry points (`Connection.setCatalog`, `DatabaseMetaData` query
   methods, `Statement.cancel`) will be covered in a follow-up.
   
   ### Why are the changes needed?
   
   Currently unchecked exceptions (e.g. `SparkException`) leak across the JDBC
   boundary: standard JDBC code catching `SQLException` misses them, and there
   is no SQLState to act on. SQLState class 08 is how connection pools (e.g.
   HikariCP) and BI tools detect a dead connection and reconnect; without it
   they keep handing out a connection whose server-side session is gone.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, for the unreleased Connect JDBC driver:
   
       stmt.execute("SELECT * FROM nonexistent")
       // before: throws AnalysisException (unchecked)
       // after:  throws SQLException with getSQLState() == "42P01"
   
   For other Connect clients, the only observable change is that fallback
   `SparkException`s now carry the original `StatusRuntimeException` as their
   cause (previously often null); messages are unchanged.
   
   ### How was this patch tested?
   
   - `SparkConnectClientSuite`: new test that transport errors preserve the
     gRPC exception as the cause; existing deadline tests still pass.
   - New `JdbcErrorUtilsSuite`: 13 unit tests covering the mapping table,
     precedence, cause-chain traversal, and the quoted-gRPC-text negative case.
   - Integration tests: a failing query throws `SQLException`/`42P01` from
     `execute`; a mid-stream failure (ANSI divide-by-zero) throws
     `SQLException`/`22012` from `next`.
   
   Ran `build/sbt "connect-client-jdbc/test"` and scalastyle.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (claude-fable-5)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-57933][CONNECT] Map Spark Connect client failures to SQLException in the JDBC driver [spark]

Posted by "j1wonpark (via GitHub)" <gi...@apache.org>.
j1wonpark commented on code in PR #57009:
URL: https://github.com/apache/spark/pull/57009#discussion_r3695747364


##########
sql/connect/client/jdbc/src/main/scala/org/apache/spark/sql/connect/client/jdbc/util/JdbcErrorUtils.scala:
##########
@@ -53,4 +59,65 @@ private[jdbc] object JdbcErrorUtils {
     case _ =>
       throw new IllegalArgumentException(s"Invalid fetch direction: $direction")
   }
+
+  // SQLState class 08 is "connection exception"; HYT00 is the conventional
+  // (ODBC-derived) state for an elapsed timeout.
+  private val CONNECTION_DOES_NOT_EXIST = "08003"
+  private val CONNECTION_FAILURE = "08006"
+  private val TIMEOUT_EXPIRED = "HYT00"
+
+  private val SESSION_GONE_CONDITION_PREFIX = "INVALID_HANDLE.SESSION_"
+
+  /**
+   * Maps the unchecked exceptions raised by the Spark Connect client (a
+   * [[SparkThrowable]] once GrpcExceptionConverter has converted the gRPC error) to
+   * the [[SQLException]] a JDBC method is required to throw:
+   *
+   *  - `INVALID_HANDLE.SESSION_*` means the server-side session is gone (e.g. it
+   *    timed out); the connection is unusable and retrying on it is pointless, so it
+   *    maps to a [[SQLNonTransientConnectionException]] with SQLState 08003
+   *    ("connection does not exist"). The operation-level subconditions
+   *    (`OPERATION_*`, `FORMAT`) concern a single operation on a healthy session
+   *    and are deliberately not treated as connection errors.
+   *  - a gRPC UNAVAILABLE (e.g. a server restart or a network blip) maps to a
+   *    [[SQLTransientConnectionException]] with SQLState 08006 ("connection
+   *    failure"), since a fresh connection can succeed.
+   *  - a gRPC DEADLINE_EXCEEDED means the RPC deadline elapsed, which a slow query
+   *    fires on a perfectly healthy connection, so it maps to a
+   *    [[SQLTimeoutException]] rather than a connection error.
+   *  - any other error keeps the server-provided SQLState when one is available.
+   *
+   * The gRPC status is read from the [[StatusRuntimeException]] that
+   * GrpcExceptionConverter preserves in the cause chain, never from message text,
+   * so a server-side error merely quoting a gRPC exception cannot be mistaken for
+   * a transport failure. SQLState class 08 is how connection pools and BI tools
+   * detect a dead connection and reconnect.
+   */

Review Comment:
   Thanks for raising this. I looked into whether we could assign `08003` directly in `error-conditions.json`.
   
   The current error framework cannot assign a SQLSTATE to an individual subclass. `ErrorSubInfo` has no `sqlState` field, and `ErrorClassesJsonReader.getSqlState` resolves only the top-level condition. Therefore, assigning `08003` only to `INVALID_HANDLE.SESSION_*` while preserving the existing condition names would require a broader extension to the common error framework.
   
   Moving the session subclasses to a new top-level condition would avoid that limitation, but it would be a compatibility-breaking rename. Existing Scala and Python Connect clients match identifiers such as `INVALID_HANDLE.SESSION_NOT_FOUND` to retry `ExecutePlan` transparently, and `ResponseValidator` matches `INVALID_HANDLE.SESSION_CHANGED` to invalidate the session. Updating the clients in this repository would not protect already released clients communicating with a newer server.
   
   For those reasons, would it make sense to keep the JDBC-layer mapping in this PR and consider subclass-level SQLSTATE support as a follow-up? When an `INVALID_HANDLE.SESSION_*` condition propagates to JDBC, `08003` is more specific than the inherited `HY000`. The JDBC adapter would still be needed to select the appropriate `SQLException` subtype and handle transport-level `UNAVAILABLE` and `DEADLINE_EXCEEDED` errors.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-57933][CONNECT] Map Spark Connect client failures to SQLException in the JDBC driver [spark]

Posted by "j1wonpark (via GitHub)" <gi...@apache.org>.
j1wonpark commented on code in PR #57009:
URL: https://github.com/apache/spark/pull/57009#discussion_r3789494694


##########
sql/connect/client/jdbc/src/main/scala/org/apache/spark/sql/connect/client/jdbc/util/JdbcErrorUtils.scala:
##########
@@ -53,4 +59,65 @@ private[jdbc] object JdbcErrorUtils {
     case _ =>
       throw new IllegalArgumentException(s"Invalid fetch direction: $direction")
   }
+
+  // SQLState class 08 is "connection exception"; HYT00 is the conventional
+  // (ODBC-derived) state for an elapsed timeout.
+  private val CONNECTION_DOES_NOT_EXIST = "08003"
+  private val CONNECTION_FAILURE = "08006"
+  private val TIMEOUT_EXPIRED = "HYT00"
+
+  private val SESSION_GONE_CONDITION_PREFIX = "INVALID_HANDLE.SESSION_"
+
+  /**
+   * Maps the unchecked exceptions raised by the Spark Connect client (a
+   * [[SparkThrowable]] once GrpcExceptionConverter has converted the gRPC error) to
+   * the [[SQLException]] a JDBC method is required to throw:
+   *
+   *  - `INVALID_HANDLE.SESSION_*` means the server-side session is gone (e.g. it
+   *    timed out); the connection is unusable and retrying on it is pointless, so it
+   *    maps to a [[SQLNonTransientConnectionException]] with SQLState 08003
+   *    ("connection does not exist"). The operation-level subconditions
+   *    (`OPERATION_*`, `FORMAT`) concern a single operation on a healthy session
+   *    and are deliberately not treated as connection errors.
+   *  - a gRPC UNAVAILABLE (e.g. a server restart or a network blip) maps to a
+   *    [[SQLTransientConnectionException]] with SQLState 08006 ("connection
+   *    failure"), since a fresh connection can succeed.
+   *  - a gRPC DEADLINE_EXCEEDED means the RPC deadline elapsed, which a slow query
+   *    fires on a perfectly healthy connection, so it maps to a
+   *    [[SQLTimeoutException]] rather than a connection error.
+   *  - any other error keeps the server-provided SQLState when one is available.
+   *
+   * The gRPC status is read from the [[StatusRuntimeException]] that
+   * GrpcExceptionConverter preserves in the cause chain, never from message text,
+   * so a server-side error merely quoting a gRPC exception cannot be mistaken for
+   * a transport failure. SQLState class 08 is how connection pools and BI tools
+   * detect a dead connection and reconnect.
+   */

Review Comment:
   Opened #58022.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-57933][CONNECT] Map Spark Connect client failures to SQLException in the JDBC driver [spark]

Posted by "j1wonpark (via GitHub)" <gi...@apache.org>.
j1wonpark commented on code in PR #57009:
URL: https://github.com/apache/spark/pull/57009#discussion_r3732882661


##########
sql/connect/client/jdbc/src/main/scala/org/apache/spark/sql/connect/client/jdbc/util/JdbcErrorUtils.scala:
##########
@@ -53,4 +59,65 @@ private[jdbc] object JdbcErrorUtils {
     case _ =>
       throw new IllegalArgumentException(s"Invalid fetch direction: $direction")
   }
+
+  // SQLState class 08 is "connection exception"; HYT00 is the conventional
+  // (ODBC-derived) state for an elapsed timeout.
+  private val CONNECTION_DOES_NOT_EXIST = "08003"
+  private val CONNECTION_FAILURE = "08006"
+  private val TIMEOUT_EXPIRED = "HYT00"
+
+  private val SESSION_GONE_CONDITION_PREFIX = "INVALID_HANDLE.SESSION_"
+
+  /**
+   * Maps the unchecked exceptions raised by the Spark Connect client (a
+   * [[SparkThrowable]] once GrpcExceptionConverter has converted the gRPC error) to
+   * the [[SQLException]] a JDBC method is required to throw:
+   *
+   *  - `INVALID_HANDLE.SESSION_*` means the server-side session is gone (e.g. it
+   *    timed out); the connection is unusable and retrying on it is pointless, so it
+   *    maps to a [[SQLNonTransientConnectionException]] with SQLState 08003
+   *    ("connection does not exist"). The operation-level subconditions
+   *    (`OPERATION_*`, `FORMAT`) concern a single operation on a healthy session
+   *    and are deliberately not treated as connection errors.
+   *  - a gRPC UNAVAILABLE (e.g. a server restart or a network blip) maps to a
+   *    [[SQLTransientConnectionException]] with SQLState 08006 ("connection
+   *    failure"), since a fresh connection can succeed.
+   *  - a gRPC DEADLINE_EXCEEDED means the RPC deadline elapsed, which a slow query
+   *    fires on a perfectly healthy connection, so it maps to a
+   *    [[SQLTimeoutException]] rather than a connection error.
+   *  - any other error keeps the server-provided SQLState when one is available.
+   *
+   * The gRPC status is read from the [[StatusRuntimeException]] that
+   * GrpcExceptionConverter preserves in the cause chain, never from message text,
+   * so a server-side error merely quoting a gRPC exception cannot be mistaken for
+   * a transport failure. SQLState class 08 is how connection pools and BI tools
+   * detect a dead connection and reconnect.
+   */

Review Comment:
   Opened https://github.com/apache/spark/pull/57831 (SPARK-58619) as the follow-up discussed here: it adds sub-condition level SQLSTATE support to the error framework and assigns `08003` to the `INVALID_HANDLE.SESSION_*` sub-conditions.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-57933][CONNECT] Map Spark Connect client failures to SQLException in the JDBC driver [spark]

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 closed pull request #57009: [SPARK-57933][CONNECT] Map Spark Connect client failures to SQLException in the JDBC driver
URL: https://github.com/apache/spark/pull/57009


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-57933][CONNECT] Map Spark Connect client failures to SQLException in the JDBC driver [spark]

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #57009:
URL: https://github.com/apache/spark/pull/57009#discussion_r3674458143


##########
sql/connect/client/jdbc/src/main/scala/org/apache/spark/sql/connect/client/jdbc/util/JdbcErrorUtils.scala:
##########
@@ -53,4 +59,65 @@ private[jdbc] object JdbcErrorUtils {
     case _ =>
       throw new IllegalArgumentException(s"Invalid fetch direction: $direction")
   }
+
+  // SQLState class 08 is "connection exception"; HYT00 is the conventional
+  // (ODBC-derived) state for an elapsed timeout.
+  private val CONNECTION_DOES_NOT_EXIST = "08003"
+  private val CONNECTION_FAILURE = "08006"
+  private val TIMEOUT_EXPIRED = "HYT00"
+
+  private val SESSION_GONE_CONDITION_PREFIX = "INVALID_HANDLE.SESSION_"
+
+  /**
+   * Maps the unchecked exceptions raised by the Spark Connect client (a
+   * [[SparkThrowable]] once GrpcExceptionConverter has converted the gRPC error) to
+   * the [[SQLException]] a JDBC method is required to throw:
+   *
+   *  - `INVALID_HANDLE.SESSION_*` means the server-side session is gone (e.g. it
+   *    timed out); the connection is unusable and retrying on it is pointless, so it
+   *    maps to a [[SQLNonTransientConnectionException]] with SQLState 08003
+   *    ("connection does not exist"). The operation-level subconditions
+   *    (`OPERATION_*`, `FORMAT`) concern a single operation on a healthy session
+   *    and are deliberately not treated as connection errors.
+   *  - a gRPC UNAVAILABLE (e.g. a server restart or a network blip) maps to a
+   *    [[SQLTransientConnectionException]] with SQLState 08006 ("connection
+   *    failure"), since a fresh connection can succeed.
+   *  - a gRPC DEADLINE_EXCEEDED means the RPC deadline elapsed, which a slow query
+   *    fires on a perfectly healthy connection, so it maps to a
+   *    [[SQLTimeoutException]] rather than a connection error.
+   *  - any other error keeps the server-provided SQLState when one is available.
+   *
+   * The gRPC status is read from the [[StatusRuntimeException]] that
+   * GrpcExceptionConverter preserves in the cause chain, never from message text,
+   * so a server-side error merely quoting a gRPC exception cannot be mistaken for
+   * a transport failure. SQLState class 08 is how connection pools and BI tools
+   * detect a dead connection and reconnect.
+   */

Review Comment:
   this looks reasonable to me, but does it mean that the current `error-conditions.json` defines those errors in the wrong sqlState ?



##########
sql/connect/common/src/main/scala/org/apache/spark/sql/connect/client/GrpcExceptionConverter.scala:
##########
@@ -170,23 +170,22 @@ private[client] class GrpcExceptionConverter(
     }
 
     // If no ErrorInfo is found, create a SparkException based on the StatusRuntimeException.
-    val (message, cause) = if (ex.getStatus.getCode == Status.Code.DEADLINE_EXCEEDED) {
-      val msg = s"${ex.toString}: RPC deadline exceeded. Deadlines can be configured via " +
+    val message = if (ex.getStatus.getCode == Status.Code.DEADLINE_EXCEEDED) {
+      s"${ex.toString}: RPC deadline exceeded. Deadlines can be configured via " +
         "SparkConnectClient.Builder.rpcDeadlines(). To disable all deadlines: " +
         "SparkConnectClient.builder().rpcDeadlines(RpcDeadlines.disabled).build()"
-      // For DEADLINE_EXCEEDED, we pass `ex` itself as the cause rather than `ex.getCause`.
-      // StatusRuntimeException.getCause() returns status.getCause(), which is always null for
-      // client-side deadline fires (gRPC constructs the status without a wrapped cause). Using
-      // ex.getCause would produce a SparkException with cause = null, losing the gRPC status
-      // code and description from the exception chain. Passing ex preserves full context and
-      // allows callers to programmatically inspect the status code via getCause().getStatus().
-      (msg, ex)
     } else {
-      (ex.toString, ex.getCause)
+      ex.toString
     }
+    // Pass `ex` itself as the cause rather than `ex.getCause`. StatusRuntimeException.getCause()
+    // returns status.getCause(), which is often null (e.g. always for client-side deadline
+    // fires, since gRPC constructs the status without a wrapped cause). Using ex.getCause
+    // would produce a SparkException with cause = null, losing the gRPC status code and
+    // description from the exception chain. Passing ex preserves full context and allows
+    // callers to programmatically inspect the status code via getCause().getStatus().
     new SparkException(
       message = message,
-      cause = cause,
+      cause = ex,

Review Comment:
   the change here looks fine to me:
   
   1. Only the fallback path (no ErrorInfo) is changed. Server errors with structured ErrorInfo return early at line 158-170, never reaching the changed code. The vast majority of Spark Connect server errors carry ErrorInfo.
   
   2. Only the cause field changes — from null to the SRE. The message, error class, and parameters are identical. This is strictly more information, not a semantic change.



##########
sql/connect/client/jvm/src/test/scala/org/apache/spark/sql/connect/client/SparkConnectClientSuite.scala:
##########
@@ -963,9 +991,8 @@ class SparkConnectClientSuite extends ConnectFunSuite {
         scala.concurrent.Await.result(resultPromise.future, FiniteDuration(15, TimeUnit.SECONDS))
       }
       // scalastyle:on awaitresult
-      // A keepalive-triggered UNAVAILABLE carries no wrapped cause (same as DEADLINE_EXCEEDED,
-      // see GrpcExceptionConverter.toThrowable), so the status code/description is only in the
-      // message.
+      // The status code/description of a keepalive-triggered UNAVAILABLE is part of the
+      // message (see GrpcExceptionConverter.toThrowable).

Review Comment:
   please update the identical comment in SparkConnectServiceKeepAliveSuite.scala:102-104



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-57933][CONNECT] Map Spark Connect client failures to SQLException in the JDBC driver [spark]

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on PR #57009:
URL: https://github.com/apache/spark/pull/57009#issuecomment-5163954574

   **Merge Summary:**
   - merged into master https://github.com/apache/spark/commit/3d9d7df61d4c39563c783bc538fb42fadb2c16b4
   - merged into branch-4.x https://github.com/apache/spark/commit/17bef863f7436718e2bf3dc8f41dd7f7eafb3131
   
   *Posted by `merge_spark_pr.py`*


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-57933][CONNECT] Map Spark Connect client failures to SQLException in the JDBC driver [spark]

Posted by "j1wonpark (via GitHub)" <gi...@apache.org>.
j1wonpark commented on PR #57009:
URL: https://github.com/apache/spark/pull/57009#issuecomment-5086048406

   Hi @pan3793, gentle ping when you have a chance. The latest revision is conflict-free and all CI checks are passing. I’d appreciate your review. Thanks!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-57933][CONNECT] Map Spark Connect client failures to SQLException in the JDBC driver [spark]

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #57009:
URL: https://github.com/apache/spark/pull/57009#discussion_r3674458143


##########
sql/connect/client/jdbc/src/main/scala/org/apache/spark/sql/connect/client/jdbc/util/JdbcErrorUtils.scala:
##########
@@ -53,4 +59,65 @@ private[jdbc] object JdbcErrorUtils {
     case _ =>
       throw new IllegalArgumentException(s"Invalid fetch direction: $direction")
   }
+
+  // SQLState class 08 is "connection exception"; HYT00 is the conventional
+  // (ODBC-derived) state for an elapsed timeout.
+  private val CONNECTION_DOES_NOT_EXIST = "08003"
+  private val CONNECTION_FAILURE = "08006"
+  private val TIMEOUT_EXPIRED = "HYT00"
+
+  private val SESSION_GONE_CONDITION_PREFIX = "INVALID_HANDLE.SESSION_"
+
+  /**
+   * Maps the unchecked exceptions raised by the Spark Connect client (a
+   * [[SparkThrowable]] once GrpcExceptionConverter has converted the gRPC error) to
+   * the [[SQLException]] a JDBC method is required to throw:
+   *
+   *  - `INVALID_HANDLE.SESSION_*` means the server-side session is gone (e.g. it
+   *    timed out); the connection is unusable and retrying on it is pointless, so it
+   *    maps to a [[SQLNonTransientConnectionException]] with SQLState 08003
+   *    ("connection does not exist"). The operation-level subconditions
+   *    (`OPERATION_*`, `FORMAT`) concern a single operation on a healthy session
+   *    and are deliberately not treated as connection errors.
+   *  - a gRPC UNAVAILABLE (e.g. a server restart or a network blip) maps to a
+   *    [[SQLTransientConnectionException]] with SQLState 08006 ("connection
+   *    failure"), since a fresh connection can succeed.
+   *  - a gRPC DEADLINE_EXCEEDED means the RPC deadline elapsed, which a slow query
+   *    fires on a perfectly healthy connection, so it maps to a
+   *    [[SQLTimeoutException]] rather than a connection error.
+   *  - any other error keeps the server-provided SQLState when one is available.
+   *
+   * The gRPC status is read from the [[StatusRuntimeException]] that
+   * GrpcExceptionConverter preserves in the cause chain, never from message text,
+   * so a server-side error merely quoting a gRPC exception cannot be mistaken for
+   * a transport failure. SQLState class 08 is how connection pools and BI tools
+   * detect a dead connection and reconnect.
+   */

Review Comment:
   this looks reasonable to me, but does it mean that the current `error-conditions.json` defines those `INVALID_HANDLE.SESSION_*` errors in the wrong sqlState ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-57933][CONNECT] Map Spark Connect client failures to SQLException in the JDBC driver [spark]

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #57009:
URL: https://github.com/apache/spark/pull/57009#discussion_r3786757968


##########
sql/connect/client/jdbc/src/main/scala/org/apache/spark/sql/connect/client/jdbc/util/JdbcErrorUtils.scala:
##########
@@ -53,4 +59,65 @@ private[jdbc] object JdbcErrorUtils {
     case _ =>
       throw new IllegalArgumentException(s"Invalid fetch direction: $direction")
   }
+
+  // SQLState class 08 is "connection exception"; HYT00 is the conventional
+  // (ODBC-derived) state for an elapsed timeout.
+  private val CONNECTION_DOES_NOT_EXIST = "08003"
+  private val CONNECTION_FAILURE = "08006"
+  private val TIMEOUT_EXPIRED = "HYT00"
+
+  private val SESSION_GONE_CONDITION_PREFIX = "INVALID_HANDLE.SESSION_"
+
+  /**
+   * Maps the unchecked exceptions raised by the Spark Connect client (a
+   * [[SparkThrowable]] once GrpcExceptionConverter has converted the gRPC error) to
+   * the [[SQLException]] a JDBC method is required to throw:
+   *
+   *  - `INVALID_HANDLE.SESSION_*` means the server-side session is gone (e.g. it
+   *    timed out); the connection is unusable and retrying on it is pointless, so it
+   *    maps to a [[SQLNonTransientConnectionException]] with SQLState 08003
+   *    ("connection does not exist"). The operation-level subconditions
+   *    (`OPERATION_*`, `FORMAT`) concern a single operation on a healthy session
+   *    and are deliberately not treated as connection errors.
+   *  - a gRPC UNAVAILABLE (e.g. a server restart or a network blip) maps to a
+   *    [[SQLTransientConnectionException]] with SQLState 08006 ("connection
+   *    failure"), since a fresh connection can succeed.
+   *  - a gRPC DEADLINE_EXCEEDED means the RPC deadline elapsed, which a slow query
+   *    fires on a perfectly healthy connection, so it maps to a
+   *    [[SQLTimeoutException]] rather than a connection error.
+   *  - any other error keeps the server-provided SQLState when one is available.
+   *
+   * The gRPC status is read from the [[StatusRuntimeException]] that
+   * GrpcExceptionConverter preserves in the cause chain, never from message text,
+   * so a server-side error merely quoting a gRPC exception cannot be mistaken for
+   * a transport failure. SQLState class 08 is how connection pools and BI tools
+   * detect a dead connection and reconnect.
+   */

Review Comment:
   @j1wonpark, https://github.com/apache/spark/pull/57831 ([SPARK-58619](https://issues.apache.org/jira/browse/SPARK-58619)) has landed, could you open a follow-up (no need for a new JIRA ticket) to remove this workaround?
   
   also cc @nchammas



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-57933][CONNECT] Map Spark Connect client failures to SQLException in the JDBC driver [spark]

Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on PR #57009:
URL: https://github.com/apache/spark/pull/57009#issuecomment-5163962614

   thank you @j1wonpark 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org