Skip to content

[SPARK-38996][SQL] Use double quotes for types in error massages #36324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
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 @@ -62,6 +62,6 @@ trait QueryErrorsBase {
}

def toSQLType(t: DataType): String = {
t.sql
"\"" + t.sql + "\""
}
}
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_DATATYPE] 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 @@ -212,7 +212,7 @@ class EncoderResolutionSuite extends PlanTest {
val attrs = Seq(attr)
assert(intercept[AnalysisException](encoder.resolveAndBind(attrs)).message ==
s"""
|[CANNOT_UP_CAST_DATATYPE] 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 @@ -226,7 +226,7 @@ class EncoderResolutionSuite extends PlanTest {
}.message
assert(msg1 ==
s"""
|[CANNOT_UP_CAST_DATATYPE] 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 @@ -239,7 +239,7 @@ class EncoderResolutionSuite extends PlanTest {
}.message
assert(msg2 ==
s"""
|[CANNOT_UP_CAST_DATATYPE] 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 @@ -176,29 +176,29 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase {
// cast to IntegerType
Seq(IntegerType, ShortType, ByteType, LongType).foreach { dataType =>
checkExceptionInExpression[NumberFormatException](cast("string", dataType),
s"Invalid input syntax for type ${dataType.sql}: 'string'")
s"""Invalid input syntax for type "${dataType.sql}": 'string'""")
checkExceptionInExpression[NumberFormatException](cast("123-string", dataType),
s"Invalid input syntax for type ${dataType.sql}: '123-string'")
s"""Invalid input syntax for type "${dataType.sql}": '123-string'""")
checkExceptionInExpression[NumberFormatException](cast("2020-07-19", dataType),
s"Invalid input syntax for type ${dataType.sql}: '2020-07-19'")
s"""Invalid input syntax for type "${dataType.sql}": '2020-07-19'""")
checkExceptionInExpression[NumberFormatException](cast("1.23", dataType),
s"Invalid input syntax for type ${dataType.sql}: '1.23'")
s"""Invalid input syntax for type "${dataType.sql}": '1.23'""")
}

Seq(DoubleType, FloatType, DecimalType.USER_DEFAULT).foreach { dataType =>
checkExceptionInExpression[NumberFormatException](cast("string", dataType),
s"Invalid input syntax for type ${dataType.sql}: 'string'")
s"""Invalid input syntax for type "${dataType.sql}": 'string'""")
checkExceptionInExpression[NumberFormatException](cast("123.000.00", dataType),
s"Invalid input syntax for type ${dataType.sql}: '123.000.00'")
s"""Invalid input syntax for type "${dataType.sql}": '123.000.00'""")
checkExceptionInExpression[NumberFormatException](cast("abc.com", dataType),
s"Invalid input syntax for type ${dataType.sql}: 'abc.com'")
s"""Invalid input syntax for type "${dataType.sql}": 'abc.com'""")
}
}

protected def checkCastToNumericError(l: Literal, to: DataType,
expectedDataTypeInErrorMsg: DataType, tryCastResult: Any): Unit = {
checkExceptionInExpression[NumberFormatException](
cast(l, to), s"Invalid input syntax for type ${expectedDataTypeInErrorMsg.sql}: 'true'")
cast(l, to), s"""Invalid input syntax for type "${expectedDataTypeInErrorMsg.sql}": 'true'""")
}

test("cast from invalid string array to numeric array should throw NumberFormatException") {
Expand Down Expand Up @@ -245,7 +245,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase {

checkExceptionInExpression[NumberFormatException](
cast("abcd", DecimalType(38, 1)),
s"Invalid input syntax for type ${DecimalType(38, 1).sql}: 'abcd'")
s"""Invalid input syntax for type "${DecimalType(38, 1).sql}": 'abcd'""")
}

protected def checkCastToBooleanError(l: Literal, to: DataType, tryCastResult: Any): Unit = {
Expand All @@ -260,7 +260,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase {

protected def checkCastToTimestampError(l: Literal, to: DataType): Unit = {
checkExceptionInExpression[DateTimeException](
cast(l, to), s"Invalid input syntax for type TIMESTAMP: ${toSQLValue(l)}")
cast(l, to), s"""Invalid input syntax for type "TIMESTAMP": ${toSQLValue(l)}""")
}

test("cast from timestamp II") {
Expand All @@ -281,7 +281,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase {
assert(negativeTs.getTime < 0)
Seq(ByteType, ShortType, IntegerType).foreach { dt =>
checkExceptionInExpression[SparkArithmeticException](
cast(negativeTs, dt), s"to ${dt.sql} causes overflow")
cast(negativeTs, dt), s"""to "${dt.sql}" causes overflow""")
}
}
}
Expand All @@ -292,7 +292,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase {
assert(negativeTs.getTime < 0)
Seq(ByteType, ShortType, IntegerType).foreach { dt =>
checkExceptionInExpression[SparkArithmeticException](
cast(negativeTs, dt), s"to ${dt.sql} causes overflow")
cast(negativeTs, dt), s"""to "${dt.sql}" causes overflow""")
}
val expectedSecs = Math.floorDiv(negativeTs.getTime, MILLIS_PER_SECOND)
checkEvaluation(cast(negativeTs, LongType), expectedSecs)
Expand Down Expand Up @@ -371,7 +371,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase {
assert(ret.resolved == !isTryCast)
if (!isTryCast) {
checkExceptionInExpression[NumberFormatException](
ret, s"Invalid input syntax for type ${IntegerType.sql}")
ret, s"""Invalid input syntax for type "${IntegerType.sql}"""")
}
}

Expand All @@ -389,7 +389,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase {
assert(ret.resolved == !isTryCast)
if (!isTryCast) {
checkExceptionInExpression[NumberFormatException](
ret, s"Invalid input syntax for type ${IntegerType.sql}")
ret, s"""Invalid input syntax for type "${IntegerType.sql}"""")
}
}
}
Expand Down Expand Up @@ -514,7 +514,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase {
assert(ret.resolved === !isTryCast)
if (!isTryCast) {
checkExceptionInExpression[NumberFormatException](
ret, s"Invalid input syntax for type ${IntegerType.sql}")
ret, s"""Invalid input syntax for type "${IntegerType.sql}"""")
}
}

Expand All @@ -523,7 +523,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase {
def checkCastWithParseError(str: String): Unit = {
checkExceptionInExpression[DateTimeException](
cast(Literal(str), TimestampType, Option(zid.getId)),
s"Invalid input syntax for type TIMESTAMP: '$str'")
s"""Invalid input syntax for type "TIMESTAMP": '$str'""")
}

checkCastWithParseError("123")
Expand All @@ -544,7 +544,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase {
def checkCastWithParseError(str: String): Unit = {
checkExceptionInExpression[DateTimeException](
cast(Literal(str), DateType, Option(zid.getId)),
s"Invalid input syntax for type DATE: '$str'")
s"""Invalid input syntax for type "DATE": '$str'""")
}

checkCastWithParseError("2015-13-18")
Expand Down Expand Up @@ -572,7 +572,7 @@ abstract class AnsiCastSuiteBase extends CastSuiteBase {
"2021-06-17 00:00:00ABC").foreach { invalidInput =>
checkExceptionInExpression[DateTimeException](
cast(invalidInput, TimestampNTZType),
s"Invalid input syntax for type TIMESTAMP_NTZ: '$invalidInput'")
s"""Invalid input syntax for type "TIMESTAMP_NTZ": '$invalidInput'""")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,15 +592,15 @@ class CastSuite extends CastSuiteBase {
val e1 = intercept[ArithmeticException] {
Cast(Literal(Byte.MaxValue + 1), ByteType).eval()
}.getMessage
assert(e1.contains("Casting 128 to TINYINT causes overflow"))
assert(e1.contains("Casting 128 to \"TINYINT\" causes overflow"))
val e2 = intercept[ArithmeticException] {
Cast(Literal(Short.MaxValue + 1), ShortType).eval()
}.getMessage
assert(e2.contains("Casting 32768 to SMALLINT causes overflow"))
assert(e2.contains("Casting 32768 to \"SMALLINT\" causes overflow"))
val e3 = intercept[ArithmeticException] {
Cast(Literal(Int.MaxValue + 1L), IntegerType).eval()
}.getMessage
assert(e3.contains("Casting 2147483648L to INT causes overflow"))
assert(e3.contains("Casting 2147483648L to \"INT\" causes overflow"))
}
}

Expand Down Expand Up @@ -642,15 +642,15 @@ class CastSuite extends CastSuiteBase {
checkEvaluation(cast(v2, LongType), 25L)
case MINUTE =>
checkExceptionInExpression[ArithmeticException](cast(v2, ByteType),
s"Casting $v2 to TINYINT causes overflow")
s"""Casting $v2 to "TINYINT" causes overflow""")
checkEvaluation(cast(v2, ShortType), (MINUTES_PER_HOUR * 25 + 1).toShort)
checkEvaluation(cast(v2, IntegerType), (MINUTES_PER_HOUR * 25 + 1).toInt)
checkEvaluation(cast(v2, LongType), MINUTES_PER_HOUR * 25 + 1)
case SECOND =>
checkExceptionInExpression[ArithmeticException](cast(v2, ByteType),
s"Casting $v2 to TINYINT causes overflow")
s"""Casting $v2 to "TINYINT" causes overflow""")
checkExceptionInExpression[ArithmeticException](cast(v2, ShortType),
s"Casting $v2 to SMALLINT causes overflow")
s"""Casting $v2 to "SMALLINT" causes overflow""")
checkEvaluation(cast(v2, IntegerType), num.toInt)
checkEvaluation(cast(v2, LongType), num)
}
Expand All @@ -659,69 +659,69 @@ class CastSuite extends CastSuiteBase {
dt.endField match {
case DAY =>
checkExceptionInExpression[ArithmeticException](cast(v3, ByteType),
s"Casting $v3 to TINYINT causes overflow")
s"""Casting $v3 to "TINYINT" causes overflow""")
checkExceptionInExpression[ArithmeticException](cast(v3, ShortType),
s"Casting $v3 to SMALLINT causes overflow")
s"""Casting $v3 to "SMALLINT" causes overflow""")
checkEvaluation(cast(v3, IntegerType), (Long.MaxValue / MICROS_PER_DAY).toInt)
checkEvaluation(cast(v3, LongType), Long.MaxValue / MICROS_PER_DAY)
case HOUR =>
checkExceptionInExpression[ArithmeticException](cast(v3, ByteType),
s"Casting $v3 to TINYINT causes overflow")
s"""Casting $v3 to "TINYINT" causes overflow""")
checkExceptionInExpression[ArithmeticException](cast(v3, ShortType),
s"Casting $v3 to SMALLINT causes overflow")
s"""Casting $v3 to "SMALLINT" causes overflow""")
checkExceptionInExpression[ArithmeticException](cast(v3, IntegerType),
s"Casting $v3 to INT causes overflow")
s"""Casting $v3 to "INT" causes overflow""")
checkEvaluation(cast(v3, LongType), Long.MaxValue / MICROS_PER_HOUR)
case MINUTE =>
checkExceptionInExpression[ArithmeticException](cast(v3, ByteType),
s"Casting $v3 to TINYINT causes overflow")
s"""Casting $v3 to "TINYINT" causes overflow""")
checkExceptionInExpression[ArithmeticException](cast(v3, ShortType),
s"Casting $v3 to SMALLINT causes overflow")
s"""Casting $v3 to "SMALLINT" causes overflow""")
checkExceptionInExpression[ArithmeticException](cast(v3, IntegerType),
s"Casting $v3 to INT causes overflow")
s"""Casting $v3 to "INT" causes overflow""")
checkEvaluation(cast(v3, LongType), Long.MaxValue / MICROS_PER_MINUTE)
case SECOND =>
checkExceptionInExpression[ArithmeticException](cast(v3, ByteType),
s"Casting $v3 to TINYINT causes overflow")
s"""Casting $v3 to "TINYINT" causes overflow""")
checkExceptionInExpression[ArithmeticException](cast(v3, ShortType),
s"Casting $v3 to SMALLINT causes overflow")
s"""Casting $v3 to "SMALLINT" causes overflow""")
checkExceptionInExpression[ArithmeticException](cast(v3, IntegerType),
s"Casting $v3 to INT causes overflow")
s"""Casting $v3 to "INT" causes overflow""")
checkEvaluation(cast(v3, LongType), Long.MaxValue / MICROS_PER_SECOND)
}

val v4 = Literal.create(Duration.of(Long.MinValue, ChronoUnit.MICROS), dt)
dt.endField match {
case DAY =>
checkExceptionInExpression[ArithmeticException](cast(v4, ByteType),
s"Casting $v4 to TINYINT causes overflow")
s"""Casting $v4 to "TINYINT" causes overflow""")
checkExceptionInExpression[ArithmeticException](cast(v4, ShortType),
s"Casting $v4 to SMALLINT causes overflow")
s"""Casting $v4 to "SMALLINT" causes overflow""")
checkEvaluation(cast(v4, IntegerType), (Long.MinValue / MICROS_PER_DAY).toInt)
checkEvaluation(cast(v4, LongType), Long.MinValue / MICROS_PER_DAY)
case HOUR =>
checkExceptionInExpression[ArithmeticException](cast(v4, ByteType),
s"Casting $v4 to TINYINT causes overflow")
s"""Casting $v4 to "TINYINT" causes overflow""")
checkExceptionInExpression[ArithmeticException](cast(v4, ShortType),
s"Casting $v4 to SMALLINT causes overflow")
s"""Casting $v4 to "SMALLINT" causes overflow""")
checkExceptionInExpression[ArithmeticException](cast(v4, IntegerType),
s"Casting $v4 to INT causes overflow")
s"""Casting $v4 to "INT" causes overflow""")
checkEvaluation(cast(v4, LongType), Long.MinValue / MICROS_PER_HOUR)
case MINUTE =>
checkExceptionInExpression[ArithmeticException](cast(v4, ByteType),
s"Casting $v4 to TINYINT causes overflow")
s"""Casting $v4 to "TINYINT" causes overflow""")
checkExceptionInExpression[ArithmeticException](cast(v4, ShortType),
s"Casting $v4 to SMALLINT causes overflow")
s"""Casting $v4 to "SMALLINT" causes overflow""")
checkExceptionInExpression[ArithmeticException](cast(v4, IntegerType),
s"Casting $v4 to INT causes overflow")
s"""Casting $v4 to "INT" causes overflow""")
checkEvaluation(cast(v4, LongType), Long.MinValue / MICROS_PER_MINUTE)
case SECOND =>
checkExceptionInExpression[ArithmeticException](cast(v4, ByteType),
s"Casting $v4 to TINYINT causes overflow")
s"""Casting $v4 to "TINYINT" causes overflow""")
checkExceptionInExpression[ArithmeticException](cast(v4, ShortType),
s"Casting $v4 to SMALLINT causes overflow")
s"""Casting $v4 to "SMALLINT" causes overflow""")
checkExceptionInExpression[ArithmeticException](cast(v4, IntegerType),
s"Casting $v4 to INT causes overflow")
s"""Casting $v4 to "INT" causes overflow""")
checkEvaluation(cast(v4, LongType), Long.MinValue / MICROS_PER_SECOND)
}
}
Expand Down Expand Up @@ -777,7 +777,7 @@ class CastSuite extends CastSuiteBase {
).foreach {
case (v, toType) =>
checkExceptionInExpression[ArithmeticException](cast(v, toType),
s"Casting $v to ${toType.sql} causes overflow")
s"""Casting $v to "${toType.sql}" causes overflow""")
}

Seq(
Expand All @@ -792,7 +792,7 @@ class CastSuite extends CastSuiteBase {
).foreach {
case (v, toType) =>
checkExceptionInExpression[ArithmeticException](cast(v, toType),
s"Casting ${v}L to ${toType.sql} causes overflow")
s"""Casting ${v}L to "${toType.sql}" causes overflow""")
}
}

Expand Down Expand Up @@ -829,7 +829,7 @@ class CastSuite extends CastSuiteBase {
case (v, dt, toType) =>
val value = Literal.create(v, dt)
checkExceptionInExpression[ArithmeticException](cast(value, toType),
s"Casting $value to ${toType.sql} causes overflow")
s"""Casting $value to "${toType.sql}" causes overflow""")
}

Seq(
Expand Down Expand Up @@ -887,7 +887,7 @@ class CastSuite extends CastSuiteBase {
).foreach {
case (v, toType) =>
checkExceptionInExpression[ArithmeticException](cast(v, toType),
s"Casting $v to ${toType.sql} causes overflow")
s"""Casting $v to "${toType.sql}" causes overflow""")
}

Seq(
Expand All @@ -898,7 +898,7 @@ class CastSuite extends CastSuiteBase {
).foreach {
case (v, toType) =>
checkExceptionInExpression[ArithmeticException](cast(v, toType),
s"Casting ${v}L to ${toType.sql} causes overflow")
s"""Casting ${v}L to "${toType.sql}" causes overflow""")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,6 @@ class DateFormatterSuite extends DatetimeFormatterSuite {
val errMsg = intercept[DateTimeException] {
formatter.parse("x123")
}.getMessage
assert(errMsg.contains("Invalid input syntax for type DATE: 'x123'"))
assert(errMsg.contains("""Invalid input syntax for type "DATE": 'x123'"""))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ class TimestampFormatterSuite extends DatetimeFormatterSuite {
val errMsg = intercept[DateTimeException] {
formatter.parse("x123")
}.getMessage
assert(errMsg.contains("Invalid input syntax for type TIMESTAMP: 'x123'"))
assert(errMsg.contains("""Invalid input syntax for type "TIMESTAMP": 'x123'"""))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class DecimalSuite extends SparkFunSuite with PrivateMethodTester with SQLHelper
assert(Decimal.fromString(UTF8String.fromString("str")) === null)
val e = intercept[NumberFormatException](Decimal.fromStringANSI(UTF8String.fromString("str")))
assert(e.getMessage.contains("Invalid input syntax for type " +
s"${DecimalType.USER_DEFAULT.sql}: 'str'"))
s""""${DecimalType.USER_DEFAULT.sql}": 'str'"""))
}

test("SPARK-35841: Casting string to decimal type doesn't work " +
Expand Down
Loading