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
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public SparkOutOfMemoryError(OutOfMemoryError e) {
}

public SparkOutOfMemoryError(String errorClass, String[] messageParameters) {
super(SparkThrowableHelper.getMessage(errorClass, messageParameters));
super(SparkThrowableHelper.getMessage(errorClass, messageParameters, ""));
this.errorClass = errorClass;
this.messageParameters = messageParameters;
}
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/resources/error/error-classes.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
"sqlState" : "42000"
},
"ARITHMETIC_OVERFLOW" : {
"message" : [ "<message>.<alternative> If necessary set <config> to \"false\" (except for ANSI interval type) to bypass this error.<context>" ],
"message" : [ "<message>.<alternative> If necessary set <config> to \"false\" (except for ANSI interval type) to bypass this error." ],
"sqlState" : "22003"
},
"CANNOT_CAST_DATATYPE" : {
"message" : [ "Cannot cast <sourceType> to <targetType>." ],
"sqlState" : "22005"
},
"CANNOT_CHANGE_DECIMAL_PRECISION" : {
"message" : [ "<value> cannot be represented as Decimal(<precision>, <scale>). If necessary set <config> to \"false\" to bypass this error.<details>" ],
"message" : [ "<value> cannot be represented as Decimal(<precision>, <scale>). If necessary set <config> to \"false\" to bypass this error." ],
"sqlState" : "22005"
},
"CANNOT_PARSE_DECIMAL" : {
Expand All @@ -23,7 +23,7 @@
"message" : [ "Cannot up cast <value> from <sourceType> to <targetType>.\n<details>" ]
},
"CAST_INVALID_INPUT" : {
"message" : [ "The value <value> of the type <sourceType> cannot be cast to <targetType> because it is malformed. To return NULL instead, use `try_cast`. If necessary set <config> to \"false\" to bypass this error.<details>" ],
"message" : [ "The value <value> of the type <sourceType> cannot be cast to <targetType> because it is malformed. To return NULL instead, use `try_cast`. If necessary set <config> to \"false\" to bypass this error." ],
"sqlState" : "42000"
},
"CAST_OVERFLOW" : {
Expand All @@ -38,7 +38,7 @@
"sqlState" : "22008"
},
"DIVIDE_BY_ZERO" : {
"message" : [ "Division by zero. To return NULL instead, use `try_divide`. If necessary set <config> to \"false\" (except for ANSI interval type) to bypass this error.<details>" ],
"message" : [ "Division by zero. To return NULL instead, use `try_divide`. If necessary set <config> to \"false\" (except for ANSI interval type) to bypass this error." ],
"sqlState" : "22012"
},
"DUPLICATE_KEY" : {
Expand Down Expand Up @@ -138,7 +138,7 @@
"sqlState" : "42000"
},
"MAP_KEY_DOES_NOT_EXIST" : {
"message" : [ "Key <keyValue> does not exist. To return NULL instead, use `try_element_at`. If necessary set <config> to \"false\" to bypass this error.<details>" ]
"message" : [ "Key <keyValue> does not exist. To return NULL instead, use `try_element_at`. If necessary set <config> to \"false\" to bypass this error." ]
},
"MISSING_COLUMN" : {
"message" : [ "Column '<columnName>' does not exist. Did you mean one of the following? [<proposal>]" ],
Expand Down
9 changes: 6 additions & 3 deletions core/src/main/scala/org/apache/spark/ErrorInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ private[spark] object SparkThrowableHelper {
mapper.readValue(errorClassesUrl, new TypeReference[SortedMap[String, ErrorInfo]]() {})
}

def getMessage(errorClass: String, messageParameters: Array[String]): String = {
def getMessage(
errorClass: String,
messageParameters: Array[String],
queryContext: String = ""): String = {
val errorInfo = errorClassToInfoMap.getOrElse(errorClass,
throw new IllegalArgumentException(s"Cannot find error class '$errorClass'"))
if (errorInfo.subClass.isDefined) {
Expand All @@ -82,11 +85,11 @@ private[spark] object SparkThrowableHelper {
val subMessageParameters = messageParameters.tail
"[" + errorClass + "." + subErrorClass + "] " + String.format((errorInfo.messageFormat +
errorSubInfo.messageFormat).replaceAll("<[a-zA-Z0-9_-]+>", "%s"),
subMessageParameters: _*)
subMessageParameters: _*) + queryContext

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I know it's causing diffs, but we really should have a space.

Suggested change
subMessageParameters: _*) + queryContext
subMessageParameters: _*) + " " + queryContext

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Actually, there is a "\n" at the beginning of queryContext.
Probably I should move it from queryContext to the method here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I tried moving the "\n" out of the query context. It requires updates on the starting and ending position of "^^^^^^" too. I will do it in another PR to make this one clean.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Opened #36612 for this.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
messageParameters: _*) + queryContext
messageParameters: _*) + " " + queryContext

}
}

Expand Down
34 changes: 24 additions & 10 deletions core/src/main/scala/org/apache/spark/SparkException.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ private[spark] class SparkUpgradeException(
/**
* Arithmetic exception thrown from Spark with an error class.
*/
private[spark] class SparkArithmeticException(errorClass: String, messageParameters: Array[String])
extends ArithmeticException(SparkThrowableHelper.getMessage(errorClass, messageParameters))
private[spark] class SparkArithmeticException(
errorClass: String,
messageParameters: Array[String],
queryContext: String = "")
extends ArithmeticException(
SparkThrowableHelper.getMessage(errorClass, messageParameters, queryContext))
with SparkThrowable {

override def getErrorClass: String = errorClass
Expand Down Expand Up @@ -132,9 +136,13 @@ private[spark] class SparkConcurrentModificationException(
/**
* Datetime exception thrown from Spark with an error class.
*/
private[spark] class SparkDateTimeException(errorClass: String, messageParameters: Array[String])
private[spark] class SparkDateTimeException(
errorClass: String,
messageParameters: Array[String],
queryContext: String = "")
extends DateTimeException(
SparkThrowableHelper.getMessage(errorClass, messageParameters)) with SparkThrowable {
SparkThrowableHelper.getMessage(errorClass, messageParameters, queryContext))
with SparkThrowable {

override def getErrorClass: String = errorClass
}
Expand Down Expand Up @@ -168,9 +176,11 @@ private[spark] class SparkFileNotFoundException(
*/
private[spark] class SparkNumberFormatException(
errorClass: String,
messageParameters: Array[String])
messageParameters: Array[String],
queryContext: String)
extends NumberFormatException(
SparkThrowableHelper.getMessage(errorClass, messageParameters)) with SparkThrowable {
SparkThrowableHelper.getMessage(errorClass, messageParameters, queryContext))
with SparkThrowable {

override def getErrorClass: String = errorClass
}
Expand Down Expand Up @@ -226,9 +236,11 @@ private[spark] class SparkIOException(
private[spark] class SparkRuntimeException(
errorClass: String,
messageParameters: Array[String],
cause: Throwable = null)
cause: Throwable = null,
queryContext: String = "")
extends RuntimeException(
SparkThrowableHelper.getMessage(errorClass, messageParameters), cause) with SparkThrowable {
SparkThrowableHelper.getMessage(errorClass, messageParameters, queryContext), cause)
with SparkThrowable {

override def getErrorClass: String = errorClass
}
Expand Down Expand Up @@ -274,9 +286,11 @@ private[spark] class SparkSQLException(
*/
private[spark] class SparkNoSuchElementException(
errorClass: String,
messageParameters: Array[String])
messageParameters: Array[String],
queryContext: String)
extends NoSuchElementException(
SparkThrowableHelper.getMessage(errorClass, messageParameters)) with SparkThrowable {
SparkThrowableHelper.getMessage(errorClass, messageParameters, queryContext))
with SparkThrowable {

override def getErrorClass: String = errorClass
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class SparkThrowableSuite extends SparkFunSuite {
assert(getMessage("DIVIDE_BY_ZERO", Array("foo", "bar", "baz")) ==
"[DIVIDE_BY_ZERO] Division by zero. " +
"To return NULL instead, use `try_divide`. If necessary set foo to \"false\" " +
"(except for ANSI interval type) to bypass this error.bar")
"(except for ANSI interval type) to bypass this error.")
}

test("Error message is formatted") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ object QueryExecutionErrors extends QueryErrorsBase {
value.toDebugString,
decimalPrecision.toString,
decimalScale.toString,
toSQLConf(SQLConf.ANSI_ENABLED.key),
context))
toSQLConf(SQLConf.ANSI_ENABLED.key)),
queryContext = context)
}

def invalidInputInCastToDatetimeError(
Expand All @@ -119,8 +119,8 @@ object QueryExecutionErrors extends QueryErrorsBase {
toSQLValue(value, from),
toSQLType(from),
toSQLType(to),
toSQLConf(SQLConf.ANSI_ENABLED.key),
errorContext))
toSQLConf(SQLConf.ANSI_ENABLED.key)),
queryContext = errorContext)
}

def invalidInputSyntaxForBooleanError(
Expand All @@ -132,8 +132,8 @@ object QueryExecutionErrors extends QueryErrorsBase {
toSQLValue(s, StringType),
toSQLType(StringType),
toSQLType(BooleanType),
toSQLConf(SQLConf.ANSI_ENABLED.key),
errorContext))
toSQLConf(SQLConf.ANSI_ENABLED.key)),
queryContext = errorContext)
}

def invalidInputInCastToNumberError(
Expand All @@ -146,8 +146,8 @@ object QueryExecutionErrors extends QueryErrorsBase {
toSQLValue(s, StringType),
toSQLType(StringType),
toSQLType(to),
toSQLConf(SQLConf.ANSI_ENABLED.key),
errorContext))
toSQLConf(SQLConf.ANSI_ENABLED.key)),
queryContext = errorContext)
}

def cannotCastFromNullTypeError(to: DataType): Throwable = {
Expand Down Expand Up @@ -180,7 +180,8 @@ object QueryExecutionErrors extends QueryErrorsBase {
def divideByZeroError(context: String): ArithmeticException = {
new SparkArithmeticException(
errorClass = "DIVIDE_BY_ZERO",
messageParameters = Array(toSQLConf(SQLConf.ANSI_ENABLED.key), context))
messageParameters = Array(toSQLConf(SQLConf.ANSI_ENABLED.key)),
queryContext = context)
}

def invalidArrayIndexError(index: Int, numElements: Int): ArrayIndexOutOfBoundsException = {
Expand Down Expand Up @@ -218,8 +219,8 @@ object QueryExecutionErrors extends QueryErrorsBase {
errorClass = "MAP_KEY_DOES_NOT_EXIST",
messageParameters = Array(
toSQLValue(key, dataType),
toSQLConf(SQLConf.ANSI_ENABLED.key),
context))
toSQLConf(SQLConf.ANSI_ENABLED.key)),
queryContext = context)
}

def invalidFractionOfSecondError(): DateTimeException = {
Expand Down Expand Up @@ -477,8 +478,10 @@ object QueryExecutionErrors extends QueryErrorsBase {
hint: String = "",
errorContext: String = ""): ArithmeticException = {
val alternative = if (hint.nonEmpty) s" To return NULL instead, use '$hint'." else ""
new SparkArithmeticException("ARITHMETIC_OVERFLOW",
Array(message, alternative, SQLConf.ANSI_ENABLED.key, errorContext))
new SparkArithmeticException(
errorClass = "ARITHMETIC_OVERFLOW",
messageParameters = Array(message, alternative, SQLConf.ANSI_ENABLED.key),
queryContext = errorContext)
}

def unaryMinusCauseOverflowError(originValue: Int): ArithmeticException = {
Expand Down