Skip to content

Commit 48bbbb2

Browse files
committed
[SPARK-52216] Make InvalidCommandInput a user-facing error
1 parent 7de318e commit 48bbbb2

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

common/utils/src/main/resources/error/error-conditions.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2386,6 +2386,12 @@
23862386
],
23872387
"sqlState" : "42601"
23882388
},
2389+
"INVALID_BUCKET_COUNT" : {
2390+
"message" : [
2391+
"BucketBy must specify a bucket count > 0, received <numBuckets> instead."
2392+
],
2393+
"sqlState" : "22003"
2394+
},
23892395
"INVALID_BUCKET_FILE" : {
23902396
"message" : [
23912397
"Invalid bucket file: <path>."

sql/connect/common/src/main/scala/org/apache/spark/sql/connect/common/InvalidCommandInput.scala

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,21 @@
1616
*/
1717
package org.apache.spark.sql.connect.common
1818

19+
import scala.jdk.CollectionConverters._
20+
21+
import org.apache.spark.{SparkThrowable, SparkThrowableHelper}
22+
1923
/**
2024
* Error thrown when a connect command is not valid.
2125
*/
2226
final case class InvalidCommandInput(
23-
private val message: String = "",
27+
private val errorCondition: String,
28+
private val messageParameters: Map[String, String] = Map.empty,
2429
private val cause: Throwable = null)
25-
extends Exception(message, cause)
30+
extends Exception(SparkThrowableHelper.getMessage(errorCondition, messageParameters), cause)
31+
with SparkThrowable {
32+
33+
override def getCondition: String = errorCondition
34+
35+
override def getMessageParameters: java.util.Map[String, String] = messageParameters.asJava
36+
}

sql/connect/server/src/main/scala/org/apache/spark/sql/connect/planner/InvalidInputErrors.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ object InvalidInputErrors {
211211
InvalidPlanInput("UnionByName `allowMissingCol` can be true only if `byName` is true.")
212212

213213
def invalidBucketCount(numBuckets: Int): InvalidCommandInput =
214-
InvalidCommandInput("BucketBy must specify a bucket count > 0, received $numBuckets instead.")
214+
InvalidCommandInput("INVALID_BUCKET_COUNT", Map("numBuckets" -> numBuckets.toString))
215215

216216
def invalidPythonUdtfReturnType(actualType: String): InvalidPlanInput =
217217
InvalidPlanInput(

sql/connect/server/src/test/scala/org/apache/spark/sql/connect/planner/SparkConnectProtoSuite.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -658,9 +658,12 @@ class SparkConnectProtoSuite extends PlanTest with SparkConnectPlanTest {
658658

659659
test("Write with invalid bucketBy configuration") {
660660
val cmd = localRelation.write(bucketByCols = Seq("id"), numBuckets = Some(0))
661-
assertThrows[InvalidCommandInput] {
662-
transform(cmd)
663-
}
661+
checkError(
662+
exception = intercept[InvalidCommandInput] {
663+
transform(cmd)
664+
},
665+
condition = "INVALID_BUCKET_COUNT",
666+
parameters = Map("numBuckets" -> "0"))
664667
}
665668

666669
test("Write to Path") {

0 commit comments

Comments
 (0)