diff --git a/core/src/main/resources/error/error-classes.json b/core/src/main/resources/error/error-classes.json index 34ccc63ff072c..655c07266dfc7 100644 --- a/core/src/main/resources/error/error-classes.json +++ b/core/src/main/resources/error/error-classes.json @@ -167,6 +167,15 @@ "message" : [ "The target JDBC server does not support transaction and can only support ALTER TABLE with a single action." ], "sqlState" : "0A000" }, + "UP_CAST_AS_ATTRIBUTE_UNSUPPORTED" : { + "message" : [ "Cannot up cast %s from %s to %s as it may truncate" ] + }, + "UP_CAST_FAILURE" : { + "message" : [ "Cannot up cast %s from %s to %s.\nThe type path of the target object is:\n%sYou can either add an explicit cast to the input data or choose a higher precision type of the field in the target object" ] + }, + "UP_CAST_UNSUPPORTED_ABSTRACT_DATA_TYPE" : { + "message" : [ "UpCast only support DecimalType as AbstractDataType yet, but got: %s" ] + }, "WRITING_JOB_ABORTED" : { "message" : [ "Writing job aborted" ], "sqlState" : "40000" diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala index 14f8053233d45..68cb1219dd6b6 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala @@ -158,16 +158,21 @@ object QueryCompilationErrors { def upCastFailureError( fromStr: String, from: Expression, to: DataType, walkedTypePath: Seq[String]): Throwable = { new AnalysisException( - s"Cannot up cast $fromStr from " + - s"${from.dataType.catalogString} to ${to.catalogString}.\n" + - s"The type path of the target object is:\n" + walkedTypePath.mkString("", "\n", "\n") + - "You can either add an explicit cast to the input data or choose a higher precision " + - "type of the field in the target object") + errorClass = "UP_CAST_FAILURE", + messageParameters = Array( + fromStr, + from.dataType.catalogString, + to.catalogString, + walkedTypePath.mkString("", "\n", "\n") + ) + ) } def unsupportedAbstractDataTypeForUpCastError(gotType: AbstractDataType): Throwable = { new AnalysisException( - s"UpCast only support DecimalType as AbstractDataType yet, but got: $gotType") + errorClass = "UP_CAST_UNSUPPORTED_ABSTRACT_DATA_TYPE", + messageParameters = Array(gotType.toString) + ) } def outerScopeFailureForNewInstanceError(className: String): Throwable = { @@ -417,9 +422,13 @@ object QueryCompilationErrors { def cannotUpCastAsAttributeError( fromAttr: Attribute, toAttr: Attribute): Throwable = { - new AnalysisException(s"Cannot up cast ${fromAttr.sql} from " + - s"${fromAttr.dataType.catalogString} to ${toAttr.dataType.catalogString} " + - "as it may truncate") + new AnalysisException( + errorClass = "UP_CAST_AS_ATTRIBUTE_UNSUPPORTED", + messageParameters = Array( + fromAttr.sql, + fromAttr.dataType.catalogString, + toAttr.dataType.catalogString) + ) } def functionUndefinedError(name: FunctionIdentifier): Throwable = {