dtenedor commented on code in PR #60736:
URL: https://github.com/apache/spark/pull/60736#discussion_r4655210305


##########
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:
   Done in e457e0e65f. The test now sends a server cause in every case and 
checks that `getCause` is that same object for the eight entries whose 
exception type keeps a cause (`AnalysisException`, 
`TableAlreadyExistsException`, `TempTableAlreadyExistsException`, 
`NoSuchDatabaseException`, `NoSuchTableException`, 
`SparkIllegalArgumentException`, `SparkRuntimeException`, 
`SparkUpgradeException`) and null for the others. I checked that replacing the 
forwarded cause with `None` in `renderOrKeepServerMessage` makes the test fail.



##########
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:
   Done in e457e0e65f, using the suggested wording.



-- 
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