Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions core/src/main/resources/error/error-classes.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@
"UNSUPPORTED_OPERATION" : {
"message" : [ "The operation is not supported: <operation>" ]
},
"UNSUPPORTED_SAVE_MODE" : {
"message" : [ "The save mode <saveMode> is not supported for:" ],
"subClass" : {
"EXISTENT_PATH" : {
"message" : [ "an existent path." ]
},
"NON_EXISTENT_PATH" : {
"message" : [ "a not existent path." ]
}
}
},
"WRITING_JOB_ABORTED" : {
"message" : [ "Writing job aborted" ],
"sqlState" : "40000"
Expand Down
34 changes: 31 additions & 3 deletions core/src/main/scala/org/apache/spark/ErrorInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,30 @@ import com.fasterxml.jackson.module.scala.DefaultScalaModule

import org.apache.spark.util.Utils

/**
* Information associated with an error subclass.
*
* @param subClass SubClass associated with this class.
* @param message C-style message format compatible with printf.
* The error message is constructed by concatenating the lines with newlines.
*/
private[spark] case class ErrorSubInfo(message: Seq[String]) {
// For compatibility with multi-line error messages
@JsonIgnore
val messageFormat: String = message.mkString("\n")
}

/**
* Information associated with an error class.
*
* @param sqlState SQLSTATE associated with this class.
* @param subClass A sequence of subclasses
* @param message C-style message format compatible with printf.
* The error message is constructed by concatenating the lines with newlines.
*/
private[spark] case class ErrorInfo(message: Seq[String], sqlState: Option[String]) {
private[spark] case class ErrorInfo(message: Seq[String],
subClass: Option[Map[String, ErrorSubInfo]],
sqlState: Option[String]) {
// For compatibility with multi-line error messages
@JsonIgnore
val messageFormat: String = message.mkString("\n")
Expand All @@ -61,13 +77,25 @@ private[spark] object SparkThrowableHelper {
queryContext: String = ""): String = {
val errorInfo = errorClassToInfoMap.getOrElse(errorClass,
throw new IllegalArgumentException(s"Cannot find error class '$errorClass'"))
val (displayClass, displayMessageParameters, displayFormat) = if (errorInfo.subClass.isEmpty) {
(errorClass, messageParameters, errorInfo.messageFormat)
} else {
val subClass = errorInfo.subClass.get
val subErrorClass = messageParameters.head
val errorSubInfo = subClass.getOrElse(subErrorClass,
throw new IllegalArgumentException(s"Cannot find sub error class '$subErrorClass'"))
(errorClass + "." + subErrorClass, messageParameters.tail,
errorInfo.messageFormat + " " + errorSubInfo.messageFormat)
}
val displayMessage = String.format(
displayFormat.replaceAll("<[a-zA-Z0-9_-]+>", "%s"),
displayMessageParameters : _*)
val displayQueryContext = if (queryContext.isEmpty) {
Comment on lines +80 to 93
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you not include the changes into the PR. If you you changes strongly require them, let's don't backport it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, i will close it and open new pr for it!
new pr: #36852

""
} else {
s"\n$queryContext"
}
String.format(errorInfo.messageFormat.replaceAll("<[a-zA-Z0-9_-]+>", "%s"),
messageParameters: _*) + displayQueryContext
s"[$displayClass] $displayMessage$displayQueryContext"
}

def getSqlState(errorClass: String): String = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ class SparkThrowableSuite extends SparkFunSuite {

// Does not fail with too many args (expects 0 args)
assert(getMessage("DIVIDE_BY_ZERO", Array("foo", "bar", "baz")) ==
"Division by zero. Use `try_divide` to tolerate divisor being 0 and return NULL instead. " +
"If necessary set foo " +
"to \"false\" (except for ANSI interval type) to bypass this error.")
"[DIVIDE_BY_ZERO] Division by zero. Use `try_divide` to tolerate divisor being 0 and " +
"return NULL instead. If necessary set foo " +
"to \"false\" (except for ANSI interval type) to bypass this error.")
}

test("Error message is formatted") {
assert(getMessage("MISSING_COLUMN", Array("foo", "bar, baz")) ==
"Column 'foo' does not exist. Did you mean one of the following? [bar, baz]")
"[MISSING_COLUMN] Column 'foo' does not exist. Did you mean one of the following? [bar, baz]")
}

test("Try catching legacy SparkError") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ class GraphiteSinkSuite extends SparkFunSuite {
new GraphiteSink(props, registry)
}
assert(e.getErrorClass === "GRAPHITE_SINK_PROPERTY_MISSING")
assert(e.getMessage === "Graphite sink requires 'host' property.")
assert(e.getMessage === "[GRAPHITE_SINK_PROPERTY_MISSING] " +
"Graphite sink requires 'host' property.")
}

test("GraphiteSink without port") {
Expand All @@ -101,7 +102,8 @@ class GraphiteSinkSuite extends SparkFunSuite {
new GraphiteSink(props, registry)
}
assert(e.getErrorClass === "GRAPHITE_SINK_PROPERTY_MISSING")
assert(e.getMessage === "Graphite sink requires 'port' property.")
assert(e.getMessage === "[GRAPHITE_SINK_PROPERTY_MISSING] " +
"Graphite sink requires 'port' property.")
}

test("GraphiteSink with invalid protocol") {
Expand All @@ -115,6 +117,6 @@ class GraphiteSinkSuite extends SparkFunSuite {
new GraphiteSink(props, registry)
}
assert(e.getErrorClass === "GRAPHITE_SINK_INVALID_PROTOCOL")
assert(e.getMessage === "Invalid Graphite protocol: http")
assert(e.getMessage === "[GRAPHITE_SINK_INVALID_PROTOCOL] Invalid Graphite protocol: http")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ class ShuffleExternalSorterSuite extends SparkFunSuite with LocalSparkContext wi
val e = intercept[SparkOutOfMemoryError] {
sorter.insertRecord(bytes, Platform.BYTE_ARRAY_OFFSET, 1, 0)
}
assert(e.getMessage == "Unable to acquire 800 bytes of memory, got 400")
assert(e.getMessage == "[UNABLE_TO_ACQUIRE_MEMORY] " +
"Unable to acquire 800 bytes of memory, got 400")
assert(e.getErrorClass == "UNABLE_TO_ACQUIRE_MEMORY")
assert(e.getSqlState == null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,13 @@ private[sql] object QueryExecutionErrors extends QueryErrorsBase {
""".stripMargin)
}

def unsupportedSaveModeError(saveMode: String, pathExists: Boolean): Throwable = {
new IllegalStateException(s"unsupported save mode $saveMode ($pathExists)")
def saveModeUnsupportedError(saveMode: Any, pathExists: Boolean): Throwable = {
pathExists match {
case true => new SparkIllegalArgumentException(errorClass = "UNSUPPORTED_SAVE_MODE",
messageParameters = Array("EXISTENT_PATH", toSQLValue(saveMode, StringType)))
case _ => new SparkIllegalArgumentException(errorClass = "UNSUPPORTED_SAVE_MODE",
messageParameters = Array("NON_EXISTENT_PATH", toSQLValue(saveMode, StringType)))
}
}

def cannotClearOutputDirectoryError(staticPrefixPath: Path): Throwable = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class EncoderResolutionSuite extends PlanTest {
val attrs = Seq('arr.array(StringType))
assert(intercept[AnalysisException](encoder.resolveAndBind(attrs)).message ==
s"""
|Cannot up cast array element from "STRING" to "BIGINT".
|[CANNOT_UP_CAST_DATATYPE] Cannot up cast array element from "STRING" to "BIGINT".
|The type path of the target object is:
|- array element class: "scala.Long"
|- field (class: "scala.Array", name: "arr")
Expand Down Expand Up @@ -211,7 +211,7 @@ class EncoderResolutionSuite extends PlanTest {
val attrs = Seq(attr)
assert(intercept[AnalysisException](encoder.resolveAndBind(attrs)).message ==
s"""
|Cannot up cast a from "${attr.dataType.sql}" to "STRING".
|[CANNOT_UP_CAST_DATATYPE] Cannot up cast a from "${attr.dataType.sql}" to "STRING".
|The type path of the target object is:
|- root class: "java.lang.String"
|You can either add an explicit cast to the input data or choose a higher precision type
Expand All @@ -225,7 +225,7 @@ class EncoderResolutionSuite extends PlanTest {
}.message
assert(msg1 ==
s"""
|Cannot up cast b from "BIGINT" to "INT".
|[CANNOT_UP_CAST_DATATYPE] Cannot up cast b from "BIGINT" to "INT".
|The type path of the target object is:
|- field (class: "scala.Int", name: "b")
|- root class: "org.apache.spark.sql.catalyst.encoders.StringIntClass"
Expand All @@ -238,7 +238,7 @@ class EncoderResolutionSuite extends PlanTest {
}.message
assert(msg2 ==
s"""
|Cannot up cast b.`b` from "DECIMAL(38,18)" to "BIGINT".
|[CANNOT_UP_CAST_DATATYPE] Cannot up cast b.`b` from "DECIMAL(38,18)" to "BIGINT".
|The type path of the target object is:
|- field (class: "scala.Long", name: "b")
|- field (class: "org.apache.spark.sql.catalyst.encoders.StringLongClass", name: "b")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class ExtractPythonUDFFromJoinConditionSuite extends PlanTest {
Optimize.execute(query.analyze)
}
assert(e.message ==
"The feature is not supported: " +
"[UNSUPPORTED_FEATURE] The feature is not supported: " +
s"""Using PythonUDF in join condition of join type ${joinType.sql} is not supported.""")

val query2 = testRelationLeft.join(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ case class InsertIntoHadoopFsRelationCommand(
case (SaveMode.Ignore, exists) =>
!exists
case (s, exists) =>
throw QueryExecutionErrors.unsupportedSaveModeError(s.toString, exists)
throw QueryExecutionErrors.saveModeUnsupportedError(s, exists)
}
}

Expand Down
24 changes: 12 additions & 12 deletions sql/core/src/test/resources/sql-tests/results/ansi/array.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ select element_at(array(1, 2, 3), 5)
struct<>
-- !query output
org.apache.spark.SparkArrayIndexOutOfBoundsException
The index 5 is out of bounds. The array has 3 elements. Use `try_element_at` to tolerate accessing element at invalid index and return NULL instead. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.
[INVALID_ARRAY_INDEX_IN_ELEMENT_AT] The index 5 is out of bounds. The array has 3 elements. Use `try_element_at` to tolerate accessing element at invalid index and return NULL instead. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.


-- !query
Expand All @@ -177,7 +177,7 @@ select element_at(array(1, 2, 3), -5)
struct<>
-- !query output
org.apache.spark.SparkArrayIndexOutOfBoundsException
The index -5 is out of bounds. The array has 3 elements. Use `try_element_at` to tolerate accessing element at invalid index and return NULL instead. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.
[INVALID_ARRAY_INDEX_IN_ELEMENT_AT] The index -5 is out of bounds. The array has 3 elements. Use `try_element_at` to tolerate accessing element at invalid index and return NULL instead. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.


-- !query
Expand All @@ -195,7 +195,7 @@ select elt(4, '123', '456')
struct<>
-- !query output
org.apache.spark.SparkArrayIndexOutOfBoundsException
The index 4 is out of bounds. The array has 2 elements. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.
[INVALID_ARRAY_INDEX] The index 4 is out of bounds. The array has 2 elements. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.


-- !query
Expand All @@ -204,7 +204,7 @@ select elt(0, '123', '456')
struct<>
-- !query output
org.apache.spark.SparkArrayIndexOutOfBoundsException
The index 0 is out of bounds. The array has 2 elements. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.
[INVALID_ARRAY_INDEX] The index 0 is out of bounds. The array has 2 elements. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.


-- !query
Expand All @@ -213,7 +213,7 @@ select elt(-1, '123', '456')
struct<>
-- !query output
org.apache.spark.SparkArrayIndexOutOfBoundsException
The index -1 is out of bounds. The array has 2 elements. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.
[INVALID_ARRAY_INDEX] The index -1 is out of bounds. The array has 2 elements. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.


-- !query
Expand Down Expand Up @@ -254,7 +254,7 @@ select array(1, 2, 3)[5]
struct<>
-- !query output
org.apache.spark.SparkArrayIndexOutOfBoundsException
The index 5 is out of bounds. The array has 3 elements. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.
[INVALID_ARRAY_INDEX] The index 5 is out of bounds. The array has 3 elements. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.


-- !query
Expand All @@ -263,7 +263,7 @@ select array(1, 2, 3)[-1]
struct<>
-- !query output
org.apache.spark.SparkArrayIndexOutOfBoundsException
The index -1 is out of bounds. The array has 3 elements. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.
[INVALID_ARRAY_INDEX] The index -1 is out of bounds. The array has 3 elements. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.


-- !query
Expand Down Expand Up @@ -337,7 +337,7 @@ select element_at(array(1, 2, 3), 5)
struct<>
-- !query output
org.apache.spark.SparkArrayIndexOutOfBoundsException
The index 5 is out of bounds. The array has 3 elements. Use `try_element_at` to tolerate accessing element at invalid index and return NULL instead. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.
[INVALID_ARRAY_INDEX_IN_ELEMENT_AT] The index 5 is out of bounds. The array has 3 elements. Use `try_element_at` to tolerate accessing element at invalid index and return NULL instead. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.


-- !query
Expand All @@ -346,7 +346,7 @@ select element_at(array(1, 2, 3), -5)
struct<>
-- !query output
org.apache.spark.SparkArrayIndexOutOfBoundsException
The index -5 is out of bounds. The array has 3 elements. Use `try_element_at` to tolerate accessing element at invalid index and return NULL instead. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.
[INVALID_ARRAY_INDEX_IN_ELEMENT_AT] The index -5 is out of bounds. The array has 3 elements. Use `try_element_at` to tolerate accessing element at invalid index and return NULL instead. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.


-- !query
Expand All @@ -364,7 +364,7 @@ select elt(4, '123', '456')
struct<>
-- !query output
org.apache.spark.SparkArrayIndexOutOfBoundsException
The index 4 is out of bounds. The array has 2 elements. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.
[INVALID_ARRAY_INDEX] The index 4 is out of bounds. The array has 2 elements. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.


-- !query
Expand All @@ -373,7 +373,7 @@ select elt(0, '123', '456')
struct<>
-- !query output
org.apache.spark.SparkArrayIndexOutOfBoundsException
The index 0 is out of bounds. The array has 2 elements. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.
[INVALID_ARRAY_INDEX] The index 0 is out of bounds. The array has 2 elements. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.


-- !query
Expand All @@ -382,4 +382,4 @@ select elt(-1, '123', '456')
struct<>
-- !query output
org.apache.spark.SparkArrayIndexOutOfBoundsException
The index -1 is out of bounds. The array has 2 elements. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.
[INVALID_ARRAY_INDEX] The index -1 is out of bounds. The array has 2 elements. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error.
Loading