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


##########
sql/connect/client/jvm/src/test/scala/org/apache/spark/sql/connect/client/SparkConnectClientSuite.scala:
##########
@@ -393,6 +394,72 @@ class SparkConnectClientSuite extends ConnectFunSuite {
     }
   }
 
+  test("ParseException keeps the server message for a condition the client 
does not define") {
+    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"
+    }
+    Seq(
+      // A subcondition the client lacks, with a query context and the 
server's SQLSTATE.
+      (
+        "INVALID_SQL_SYNTAX.SUBCONDITION_NOT_DEFINED_BY_CLIENT",
+        Map("statement" -> "SELECT"),
+        Array(sqlContext),
+        Some("42000"),
+        "42000"),
+      // A main condition the client lacks, without a query context or 
SQLSTATE, as when only

Review Comment:
   **Nit (P3):** Only the missing query context comes from the truncated error 
status. `ErrorUtils.buildStatusFromThrowable` writes the SQLSTATE into the 
status metadata independently of the size check that can drop the condition and 
parameters (`ErrorUtils.scala` lines 236-248), and `errorInfoToThrowable` 
forwards it whenever a condition is present (`GrpcExceptionConverter.scala` 
lines 520-539). So that path normally delivers a main condition together with 
the server's SQLSTATE, a combination no case covers. A condition arrives 
without a SQLSTATE when the server's condition defines none, such as the parse 
condition `_LEGACY_ERROR_TEMP_0035`, and then on either path. Attributing the 
missing SQLSTATE to that cause, or giving this case a server SQLSTATE so that 
it models the truncated status, would make the comment accurate. This describes 
the server and client code at this PR's head; the review could not read the 
`ErrorUtils.scala` of released versions.
   
   See **Shared repair plan 1** in the review body.



##########
sql/connect/common/src/main/scala/org/apache/spark/sql/connect/client/GrpcExceptionConverter.scala:
##########
@@ -291,13 +291,26 @@ private[client] object GrpcExceptionConverter {
         getErrorClassOrFallback(params),
         errorParamsToMessageParameters(params),
         getSqlStateOrFallback(params))),
-    errorConstructor(params =>
-      new ParseException(
-        None,
-        Origin(),
-        errorClass = getErrorClassOrFallback(params),
-        messageParameters = errorParamsToMessageParameters(params),
-        queryContext = params.queryContext)),
+    errorConstructor { params =>

Review Comment:
   **Non-blocking (P2):** This fallback covers only the `ParseException` entry. 
The other 15 typed entries in `errorFactory` (`AnalysisException`; the six 
catalog exceptions, such as `NoSuchTableException`; and 
`SparkNumberFormatException`, `SparkIllegalArgumentException`, 
`SparkArithmeticException`, `SparkUnsupportedOperationException`, 
`SparkArrayIndexOutOfBoundsException`, `SparkDateTimeException`, 
`SparkRuntimeException` and `SparkUpgradeException`) still build their 
exception through constructors that render the server's condition from the 
client's bundled `error-conditions.json` with 
`SparkThrowableHelper.getMessage`. That call throws 
`SparkException.internalError` when the client lacks the condition or 
subcondition. Only the `StreamingQueryException` and `SparkException` entries 
keep `params.message`.
   
   So the failure this PR fixes for parse errors remains for analysis, catalog 
and runtime errors in mixed-version deployments of the Scala client. Reading 
the code, a client built from this PR that talks to a Spark 4.3.0 server and 
runs `df.write.partitionBy("a").bucketBy(2, "a").saveAsTable("t")` receives an 
`AnalysisException` with `_LEGACY_ERROR_TEMP_1166`, which SPARK-59040 renamed 
to `BUCKET_COLUMN_IN_PARTITION_COLUMNS` on the base branch in a change that 
4.3.0 does not contain. The client then throws `INTERNAL_ERROR`, saying it 
cannot find main error class `_LEGACY_ERROR_TEMP_1166`, instead of an 
`AnalysisException`, so code that catches `AnalysisException` or checks its 
condition or SQLSTATE does not match it. A newer server's new analysis, catalog 
or runtime conditions fail the same way. The statement fails either way, so no 
result is wrong, and PySpark Connect is not affected. Neither a TODO here nor 
the PR description defers the other entries.
   
   **Recommended change:** Extend the fallback this PR adds for parse errors to 
every errorFactory entry in 
sql/connect/common/src/main/scala/org/apache/spark/sql/connect/client/GrpcExceptionConverter.scala
 that rebuilds its exception from the client's error definitions. That covers 
AnalysisException; the six catalog exceptions (NamespaceAlreadyExistsException, 
TableAlreadyExistsException, TempTableAlreadyExistsException, 
NoSuchDatabaseException, NoSuchNamespaceException and NoSuchTableException); 
and SparkNumberFormatException, SparkIllegalArgumentException, 
SparkArithmeticException, SparkUnsupportedOperationException, 
SparkArrayIndexOutOfBoundsException, SparkDateTimeException, 
SparkRuntimeException and SparkUpgradeException. When the client's definitions 
lack the server's condition or subcondition, each entry builds its own 
exception type from the server's rendered message, condition, message 
parameters and SQLSTATE, plus the query context where the type has one, instead 
of render
 ing and failing with INTERNAL_ERROR. The SQLSTATE is the one the converter 
already resolves. When the client defines the condition, the entry builds the 
exception exactly as it does today. Cover the new branch in the Spark Connect 
Scala client converter tests.
   
   **Why this works:** Move the check that the ParseException entry makes at 
GrpcExceptionConverter.scala line 296 into one helper. Every typed entry calls 
the helper with two constructors: its existing one, which renders from the 
client's definitions, and one that keeps the server's message. The helper 
resolves the condition once and picks the second constructor only when the 
condition is non-null and SparkThrowableHelper.isValidErrorClass rejects it, so 
all entries share one trigger. AnalysisException can be built from a verbatim 
message and the SQLSTATE through its existing constructor that accepts them 
(AnalysisException.scala lines 86-101), with copy to attach the query context. 
Each catalog exception in alreadyExistException.scala and 
noSuchItemsExceptions.scala, and each Spark exception type in 
SparkException.scala, gets a non-public factory beside its existing 
constructors that, like ParseException.withRenderedMessage, passes the given 
message to the class's private primary c
 onstructor without rendering. The Spark exception types' private primary 
constructors already take the SQLSTATE and, where the type has one, the query 
context. The catalog exceptions' private primary constructors take no SQLSTATE, 
so, as this PR does for ParseException, each gains a SQLSTATE parameter that 
defaults to None and is passed on to AnalysisException; 
NoSuchNamespaceException passes it through NoSuchDatabaseException's. The 
ParseException entry then goes through the same helper.
   
   **Compatibility:** Server errors whose condition the client defines are 
rebuilt from the client's definitions exactly as on the base branch, and server 
errors without a condition keep their legacy fallback conditions.
   
   **Risks:** Each new factory widens the internal construction surface of its 
exception class. Keeping it private[sql] or private[spark], as 
ParseException.withRenderedMessage is, keeps it out of the public API and out 
of reach of external connectors, which the source comments of 
NamespaceAlreadyExistsException, TableAlreadyExistsException, 
NoSuchNamespaceException and NoSuchTableException name as possible users of 
those classes. The new SQLSTATE parameter on the catalog exceptions' private 
primary constructors leaves the public constructors those comments protect 
unchanged, and org.apache.spark.sql.catalyst is excluded from MiMa 
(project/MimaExcludes.scala line 193). For these inputs, getMessage returns the 
server's wording, which can differ from what this client would render for the 
same condition. This PR already makes that trade-off for parse errors, and it 
applies only to conditions the client cannot render at all.
   
   **Constraints:** Conditions the client defines keep the base branch's 
constructor, message and SQLSTATE in every entry. Every entry, the 
ParseException entry included, uses the same trigger, so whatever is decided 
about conditions whose templates differ between versions applies to all entries 
at once. The SQLSTATE comes from the converter's existing resolution: a 
SQLSTATE the server sent wins, and otherwise the documented fallback applies 
(the client's SQLSTATE for the main condition, then XXKCM). No public 
constructor is added to or removed from the catalog exception classes; four of 
them carry source comments saying external connectors may use them.
   
   **Exclusions:**
   - A server condition that the client defines but whose template needs a 
parameter the server did not send stays on the rendering path until the open 
question about drifted templates is settled.
   - A null condition that arrives with only a SQLSTATE stays on the existing 
path; that behavior predates this change and this repair does not affect it.
   - PySpark Connect needs no change, because it already keeps the server's 
message for every exception type.
   
   **Verification:**
   - **Behavior:** For each of the fifteen entries, convert one server error 
whose main condition the client lacks and one whose subcondition it lacks. Each 
conversion yields that entry's exception type with the server's condition, 
message parameters, SQLSTATE and, where the type carries one, query context, 
and getMessage equals the server's message.
   - **Regression:** Converting a server AnalysisException whose condition the 
client no longer defines, such as _LEGACY_ERROR_TEMP_1166 (renamed on the base 
branch by SPARK-59040 and still raised by Spark 4.3.0), yields an 
AnalysisException with that condition and the server's message instead of a 
SparkException with INTERNAL_ERROR.
   - **Compatibility:** For a condition the client defines, every entry still 
renders its message from the client's definitions and reports the client's 
SQLSTATE, as on the base branch, and a server error without a condition still 
gets its legacy fallback condition.



##########
sql/connect/client/jvm/src/test/scala/org/apache/spark/sql/connect/client/SparkConnectClientSuite.scala:
##########
@@ -393,6 +394,72 @@ class SparkConnectClientSuite extends ConnectFunSuite {
     }
   }
 
+  test("ParseException keeps the server message for a condition the client 
does not define") {
+    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"
+    }
+    Seq(
+      // A subcondition the client lacks, with a query context and the 
server's SQLSTATE.
+      (
+        "INVALID_SQL_SYNTAX.SUBCONDITION_NOT_DEFINED_BY_CLIENT",
+        Map("statement" -> "SELECT"),
+        Array(sqlContext),
+        Some("42000"),

Review Comment:
   **Nit (P3):** This SQLSTATE is the one the client derives on its own: 
`ErrorClassesJsonReader.getSqlState` falls back from the undefined subcondition 
to `INVALID_SQL_SYNTAX`, whose SQLSTATE is 42000. The other case sends no 
SQLSTATE and expects `XXKCM`. So the assertion on `getSqlState` (line 440) 
cannot tell the server's SQLSTATE from the client's own lookup: replacing 
`getSqlStateOrFallback(params)` in `GrpcExceptionConverter.scala` (line 304) 
with 
`Option(SparkThrowableHelper.getSqlState(errorClass)).orElse(Some("XXKCM"))`, 
or with `params.sqlState.orElse(Some("XXKCM"))`, leaves both cases passing.
   
   The first replacement would matter in practice. The server sends its 
SQLSTATE with the condition on both conversion paths, so a main parse condition 
added after the client was built would be reported as `XXKCM` instead of its 
own SQLSTATE; the base branch keeps adding them, as SPARK-34586 did with 
`SPECIFY_DISTRIBUTED_BY_PARTITION_WITHOUT_PARTITIONING_IS_NOT_ALLOWED` 
(SQLSTATE 42908). A case whose server SQLSTATE differs from what the client 
derives, and a case for an undefined subcondition without a server SQLSTATE, 
which should report the main condition's 42000, would pin the behavior.
   
   See **Shared repair plan 1** in the review body.



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