spark-test commented on code in PR #60736:
URL: https://github.com/apache/spark/pull/60736#discussion_r4647163047


##########
sql/connect/client/jvm/src/test/scala/org/apache/spark/sql/connect/client/SparkConnectClientSuite.scala:
##########
@@ -393,6 +395,117 @@ class SparkConnectClientSuite extends ConnectFunSuite {
     }
   }
 
+  // The errorFactory entries that rebuild a server error from this client's 
error definitions.
+  // StreamingQueryException and SparkException always keep the server's 
message.
+  private val renderingErrorFactory = 
GrpcExceptionConverter.errorFactory.filter {
+    case (name, _) =>
+      name != classOf[StreamingQueryException].getName && name != 
classOf[SparkException].getName
+  }
+
+  private def isInstanceOf(error: Throwable, className: String): Boolean =
+    Iterator
+      .iterate[Class[_]](error.getClass)(_.getSuperclass)
+      .takeWhile(_ != null)
+      .exists(_.getName == className)
+
+  // Entries whose exception type carries no query context.
+  private val withoutQueryContext = Set(
+    classOf[NamespaceAlreadyExistsException],
+    classOf[TableAlreadyExistsException],
+    classOf[TempTableAlreadyExistsException],
+    classOf[NoSuchDatabaseException],
+    classOf[NoSuchNamespaceException],
+    classOf[NoSuchTableException],
+    classOf[UnsupportedOperationException],
+    classOf[SparkUpgradeException]).map(_.getName)
+
+  test("Server errors the client cannot render keep the server's message") {
+    val sqlContext = new QueryContext {
+      override def contextType(): QueryContextType = QueryContextType.SQL
+      override def objectType(): String = ""
+      override def objectName(): String = ""
+      override def startIndex(): Int = 0
+      override def stopIndex(): Int = 5
+      override def fragment(): String = "SELECT"
+      override def callSite(): String = ""
+      override def summary(): String = "== SQL (line 1, position 1) 
==\nSELECT\n^^^^^^\n"
+    }
+    // (condition, message parameters, query context, server SQLSTATE, 
expected SQLSTATE)
+    val cases = Seq(
+      // A subcondition the client lacks. The server's SQLSTATE differs from 
the 42000 the
+      // client would derive from the main condition, and wins.
+      (
+        "INVALID_SQL_SYNTAX.SUBCONDITION_NOT_DEFINED_BY_CLIENT",
+        Map("statement" -> "SELECT"),
+        Array(sqlContext),
+        Some("42601"),
+        "42601"),
+      // The same without a server SQLSTATE, as for a server condition that 
defines none: the
+      // client falls back to the main condition's SQLSTATE.
+      (
+        "INVALID_SQL_SYNTAX.SUBCONDITION_NOT_DEFINED_BY_CLIENT",
+        Map("statement" -> "SELECT"),
+        Array.empty[QueryContext],
+        None,
+        "42000"),
+      // A main condition the client lacks, without a server SQLSTATE.
+      (
+        "CONDITION_NOT_DEFINED_BY_CLIENT",
+        Map.empty[String, String],
+        Array(sqlContext),
+        None,
+        "XXKCM"),
+      // A condition the client defines, but whose template uses a parameter 
the server did not
+      // send, as when the template gained a placeholder in the client's 
version.
+      ("DUPLICATE_KEY", Map("unrelated" -> "x"), Array.empty[QueryContext], 
None, "23505"))
+    for ((name, constructor) <- renderingErrorFactory) {
+      cases.foreach {
+        case (errorClass, messageParameters, queryContext, serverSqlState, 
expectedSqlState) =>
+          withClue(s"$name, $errorClass, $serverSqlState: ") {
+            val serverMessage =
+              s"[$errorClass] Server message. SQLSTATE: $expectedSqlState" +
+                queryContext.map("\n" + _.summary()).mkString
+            val error = constructor(
+              GrpcExceptionConverter.ErrorParams(
+                message = serverMessage,
+                cause = None,

Review Comment:
   **Nit (P3):** Every case of this test reaches this `ErrorParams` with `cause 
= None`, and no assertion reads `getCause`. Yet when the client cannot render a 
server error, eight entries now attach the cause that the converter rebuilt 
from the server's error chain: `AnalysisException`, 
`TableAlreadyExistsException`, `TempTableAlreadyExistsException`, 
`NoSuchDatabaseException`, `NoSuchTableException`, 
`SparkIllegalArgumentException`, `SparkRuntimeException` and 
`SparkUpgradeException` (`GrpcExceptionConverter.scala` lines 315, 361, 386, 
399, 412, 437, 471, 553 and 567). By the review's reading of the code, a later 
change that drops that cause in `renderOrKeepServerMessage`, in one of those 
entries' branches that keep the server's message, or in a `withRenderedMessage` 
factory would leave the whole suite passing. The other `ErrorParams` in this 
suite also use `cause = None`, and the end-to-end `cause exception` test in 
`ClientE2ETestSuite` talks to a server whose definitions match, so
  it never takes that branch. A Scala client talking to a server of another 
Spark version would then lose the server's cause chain on those exceptions with 
no test failing. The current code forwards the cause correctly, so only the 
test needs a case that sends a server cause and checks `getCause`.
   
   **Verification:**
   - **Behavior:** Convert a server error that the client cannot render and 
that carries a cause, once for each converter entry that renders from the 
client's definitions. getCause is that cause for AnalysisException, 
TableAlreadyExistsException, TempTableAlreadyExistsException, 
NoSuchDatabaseException, NoSuchTableException, SparkIllegalArgumentException, 
SparkRuntimeException and SparkUpgradeException, and null for the other eight 
entries, which is what each entry's rendering branch does with the same cause.
   - **Regression:** At least one case of the reworked converter test fails 
when the forwarded cause is replaced with None in any of these places: 
GrpcExceptionConverter.renderOrKeepServerMessage, the branch that keeps the 
server's message for any one of the eight entries that keep a cause, or, for 
the seven of those entries other than AnalysisException, the 
withRenderedMessage factory that the branch calls.



##########
sql/api/src/main/scala/org/apache/spark/sql/catalyst/parser/parsers.scala:
##########
@@ -278,6 +283,27 @@ object ParseException {
   def getQueryContext(): Array[QueryContext] = {
     Some(CurrentOrigin.get.context).collect { case b: SQLQueryContext if 
b.isValid => b }.toArray
   }
+
+  /**
+   * Creates a [[ParseException]] whose `getMessage` returns `renderedMessage` 
unchanged. The
+   * Spark Connect client uses it for a server error whose condition is 
missing from the client's
+   * own error definitions, which it therefore cannot render again.

Review Comment:
   **Nit (P3):** Since 4c681e9b963, the converter also calls this factory when 
the client defines the condition but its template uses a parameter the server 
did not send (`canRender` in `GrpcExceptionConverter.scala` lines 320-322), as 
the new `DUPLICATE_KEY` case in `SparkConnectClientSuite` shows. This sentence 
still ties the factory to a missing condition alone, so a reader may assume 
that every kept `ParseException` carries a condition the client does not 
define, for example that its `getDefaultMessageTemplate` returns null, which by 
the review's reading of `SparkThrowable` holds only for a missing condition. 
The other `withRenderedMessage` docs give the missing condition only as an 
example, and the `renderOrKeepServerMessage` doc names both cases, so only this 
sentence needs rewording, for example:
   
   ```suggestion
      * Spark Connect client uses it for a server error that its own 
definitions cannot render: they
      * lack the condition or subcondition, or the template uses a parameter 
the server did not send.
   ```
   
   **Verification:**
   - **Inspection:** The ParseException.withRenderedMessage scaladoc in 
sql/api/src/main/scala/org/apache/spark/sql/catalyst/parser/parsers.scala names 
both cases in which the Spark Connect client cannot render a server error: a 
condition or subcondition its definitions lack, and a message template that 
uses a parameter the server did not send. This matches the check that 
GrpcExceptionConverter applies before calling the factory.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to