Skip to content

[SPARK-39007][SQL] Use double quotes for SQL configs in error messages #36335

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 5 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
2 changes: 1 addition & 1 deletion core/src/main/resources/error/error-classes.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
"message" : [ "The operation is not supported: <operation>" ]
},
"UNTYPED_SCALA_UDF" : {
"message" : [ "You're using untyped Scala UDF, which does not have the input type information. Spark may blindly pass null to the Scala closure with primitive-type argument, and the closure will see the default value of the Java type for the null argument, e.g. `udf((x: Int) => x, IntegerType)`, the result is 0 for null input. To get rid of this error, you could:\n1. use typed Scala UDF APIs(without return type parameter), e.g. `udf((x: Int) => x)`\n2. use Java UDF APIs, e.g. `udf(new UDF1[String, Integer] { override def call(s: String): Integer = s.length() }, IntegerType)`, if input types are all non primitive\n3. set spark.sql.legacy.allowUntypedScalaUDF to true and use this API with caution" ]
"message" : [ "You're using untyped Scala UDF, which does not have the input type information. Spark may blindly pass null to the Scala closure with primitive-type argument, and the closure will see the default value of the Java type for the null argument, e.g. `udf((x: Int) => x, IntegerType)`, the result is 0 for null input. To get rid of this error, you could:\n1. use typed Scala UDF APIs(without return type parameter), e.g. `udf((x: Int) => x)`\n2. use Java UDF APIs, e.g. `udf(new UDF1[String, Integer] { override def call(s: String): Integer = s.length() }, IntegerType)`, if input types are all non primitive\n3. set \"spark.sql.legacy.allowUntypedScalaUDF\" to true and use this API with caution" ]
},
"WRITING_JOB_ABORTED" : {
"message" : [ "Writing job aborted" ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ trait QueryErrorsBase {
litToErrorValue(Literal.create(v, t))
}

private def quoteByDefault(elem: String): String = {
"\"" + elem + "\""
}

// Quote sql statements in error messages.
def toSQLStmt(text: String): String = {
"\"" + text.toUpperCase(Locale.ROOT) + "\""
quoteByDefault(text.toUpperCase(Locale.ROOT))
}

def toSQLId(parts: Seq[String]): String = {
Expand All @@ -62,6 +66,10 @@ trait QueryErrorsBase {
}

def toSQLType(t: DataType): String = {
"\"" + t.sql + "\""
quoteByDefault(t.sql)
}

def toSQLConf(conf: String): String = {
quoteByDefault(conf)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,23 @@ object QueryExecutionErrors extends QueryErrorsBase {

def castingCauseOverflowError(t: Any, dataType: DataType): ArithmeticException = {
new SparkArithmeticException(errorClass = "CAST_CAUSES_OVERFLOW",
messageParameters = Array(toSQLValue(t), toSQLType(dataType), SQLConf.ANSI_ENABLED.key))
messageParameters = Array(
toSQLValue(t), toSQLType(dataType), toSQLConf(SQLConf.ANSI_ENABLED.key)))
}

def cannotChangeDecimalPrecisionError(
value: Decimal,
decimalPrecision: Int,
decimalScale: Int,
context: String): ArithmeticException = {
new SparkArithmeticException(errorClass = "CANNOT_CHANGE_DECIMAL_PRECISION",
messageParameters = Array(value.toDebugString,
decimalPrecision.toString, decimalScale.toString, SQLConf.ANSI_ENABLED.key, context))
new SparkArithmeticException(
errorClass = "CANNOT_CHANGE_DECIMAL_PRECISION",
messageParameters = Array(
value.toDebugString,
decimalPrecision.toString,
decimalScale.toString,
toSQLConf(SQLConf.ANSI_ENABLED.key),
context))
}

def invalidInputSyntaxForNumericError(
Expand Down Expand Up @@ -148,7 +154,8 @@ object QueryExecutionErrors extends QueryErrorsBase {

def divideByZeroError(context: String): ArithmeticException = {
new SparkArithmeticException(
errorClass = "DIVIDE_BY_ZERO", messageParameters = Array(SQLConf.ANSI_ENABLED.key, context))
errorClass = "DIVIDE_BY_ZERO",
messageParameters = Array(toSQLConf(SQLConf.ANSI_ENABLED.key), context))
}

def invalidArrayIndexError(index: Int, numElements: Int): ArrayIndexOutOfBoundsException = {
Expand All @@ -163,8 +170,9 @@ object QueryExecutionErrors extends QueryErrorsBase {
index: Int,
numElements: Int,
key: String): ArrayIndexOutOfBoundsException = {
new SparkArrayIndexOutOfBoundsException(errorClass = "INVALID_ARRAY_INDEX",
messageParameters = Array(toSQLValue(index), toSQLValue(numElements), key))
new SparkArrayIndexOutOfBoundsException(
errorClass = "INVALID_ARRAY_INDEX",
messageParameters = Array(toSQLValue(index), toSQLValue(numElements), toSQLConf(key)))
}

def invalidElementAtIndexError(
Expand All @@ -173,7 +181,7 @@ object QueryExecutionErrors extends QueryErrorsBase {
new SparkArrayIndexOutOfBoundsException(
errorClass = "INVALID_ARRAY_INDEX_IN_ELEMENT_AT",
messageParameters =
Array(toSQLValue(index), toSQLValue(numElements), SQLConf.ANSI_ENABLED.key))
Array(toSQLValue(index), toSQLValue(numElements), toSQLConf(SQLConf.ANSI_ENABLED.key)))
}

def mapKeyNotExistError(key: Any, context: String): NoSuchElementException = {
Expand All @@ -182,8 +190,9 @@ object QueryExecutionErrors extends QueryErrorsBase {
}

def invalidFractionOfSecondError(): DateTimeException = {
new SparkDateTimeException(errorClass = "INVALID_FRACTION_OF_SECOND",
Array(SQLConf.ANSI_ENABLED.key))
new SparkDateTimeException(
errorClass = "INVALID_FRACTION_OF_SECOND",
Array(toSQLConf(SQLConf.ANSI_ENABLED.key)))
}

def ansiDateTimeParseError(e: DateTimeParseException): DateTimeParseException = {
Expand Down Expand Up @@ -521,10 +530,10 @@ object QueryExecutionErrors extends QueryErrorsBase {
|from $format files can be ambiguous, as the files may be written by
|Spark 2.x or legacy versions of Hive, which uses a legacy hybrid calendar
|that is different from Spark 3.0+'s Proleptic Gregorian calendar.
|See more details in SPARK-31404. You can set the SQL config '$config' or
|See more details in SPARK-31404. You can set the SQL config ${toSQLConf(config)} or
|the datasource option '$option' to 'LEGACY' to rebase the datetime values
|w.r.t. the calendar difference during reading. To read the datetime values
|as it is, set the SQL config '$config' or the datasource option '$option'
|as it is, set the SQL config ${toSQLConf(config)} or the datasource option '$option'
|to 'CORRECTED'.
|""".stripMargin),
cause = null
Expand All @@ -541,10 +550,10 @@ object QueryExecutionErrors extends QueryErrorsBase {
|into $format files can be dangerous, as the files may be read by Spark 2.x
|or legacy versions of Hive later, which uses a legacy hybrid calendar that
|is different from Spark 3.0+'s Proleptic Gregorian calendar. See more
|details in SPARK-31404. You can set $config to 'LEGACY' to rebase the
|details in SPARK-31404. You can set ${toSQLConf(config)} to 'LEGACY' to rebase the
|datetime values w.r.t. the calendar difference during writing, to get maximum
|interoperability. Or set $config to 'CORRECTED' to write the datetime values
|as it is, if you are 100% sure that the written files will only be read by
|interoperability. Or set ${toSQLConf(config)} to 'CORRECTED' to write the datetime
|values as it is, if you are 100% sure that the written files will only be read by
|Spark 3.0+ or other systems that use Proleptic Gregorian calendar.
|""".stripMargin),
cause = null
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
[INVALID_ARRAY_INDEX_IN_ELEMENT_AT] Invalid index: 5, numElements: 3. To return NULL instead, use 'try_element_at'. If necessary set spark.sql.ansi.enabled to false to bypass this error.
[INVALID_ARRAY_INDEX_IN_ELEMENT_AT] Invalid index: 5, numElements: 3. To return NULL instead, use 'try_element_at'. 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
[INVALID_ARRAY_INDEX_IN_ELEMENT_AT] Invalid index: -5, numElements: 3. To return NULL instead, use 'try_element_at'. If necessary set spark.sql.ansi.enabled to false to bypass this error.
[INVALID_ARRAY_INDEX_IN_ELEMENT_AT] Invalid index: -5, numElements: 3. To return NULL instead, use 'try_element_at'. 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
[INVALID_ARRAY_INDEX] Invalid index: 4, numElements: 2. If necessary set spark.sql.ansi.enabled to false to bypass this error.
[INVALID_ARRAY_INDEX] Invalid index: 4, numElements: 2. 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
[INVALID_ARRAY_INDEX] Invalid index: 0, numElements: 2. If necessary set spark.sql.ansi.enabled to false to bypass this error.
[INVALID_ARRAY_INDEX] Invalid index: 0, numElements: 2. 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
[INVALID_ARRAY_INDEX] Invalid index: -1, numElements: 2. If necessary set spark.sql.ansi.enabled to false to bypass this error.
[INVALID_ARRAY_INDEX] Invalid index: -1, numElements: 2. 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
[INVALID_ARRAY_INDEX] Invalid index: 5, numElements: 3. If necessary set spark.sql.ansi.enabled to false to bypass this error.
[INVALID_ARRAY_INDEX] Invalid index: 5, numElements: 3. 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
[INVALID_ARRAY_INDEX] Invalid index: -1, numElements: 3. If necessary set spark.sql.ansi.enabled to false to bypass this error.
[INVALID_ARRAY_INDEX] Invalid index: -1, numElements: 3. 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
[INVALID_ARRAY_INDEX_IN_ELEMENT_AT] Invalid index: 5, numElements: 3. To return NULL instead, use 'try_element_at'. If necessary set spark.sql.ansi.enabled to false to bypass this error.
[INVALID_ARRAY_INDEX_IN_ELEMENT_AT] Invalid index: 5, numElements: 3. To return NULL instead, use 'try_element_at'. 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
[INVALID_ARRAY_INDEX_IN_ELEMENT_AT] Invalid index: -5, numElements: 3. To return NULL instead, use 'try_element_at'. If necessary set spark.sql.ansi.enabled to false to bypass this error.
[INVALID_ARRAY_INDEX_IN_ELEMENT_AT] Invalid index: -5, numElements: 3. To return NULL instead, use 'try_element_at'. 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
[INVALID_ARRAY_INDEX] Invalid index: 4, numElements: 2. If necessary set spark.sql.ansi.enabled to false to bypass this error.
[INVALID_ARRAY_INDEX] Invalid index: 4, numElements: 2. 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
[INVALID_ARRAY_INDEX] Invalid index: 0, numElements: 2. If necessary set spark.sql.ansi.enabled to false to bypass this error.
[INVALID_ARRAY_INDEX] Invalid index: 0, numElements: 2. 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
[INVALID_ARRAY_INDEX] Invalid index: -1, numElements: 2. If necessary set spark.sql.ansi.enabled to false to bypass this error.
[INVALID_ARRAY_INDEX] Invalid index: -1, numElements: 2. If necessary set "spark.sql.ansi.enabled" to false to bypass this error.
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ select cast('123.45' as decimal(4, 2))
struct<>
-- !query output
org.apache.spark.SparkArithmeticException
[CANNOT_CHANGE_DECIMAL_PRECISION] Decimal(expanded,123.45,5,2}) cannot be represented as Decimal(4, 2). If necessary set spark.sql.ansi.enabled to false to bypass this error.
[CANNOT_CHANGE_DECIMAL_PRECISION] Decimal(expanded,123.45,5,2}) cannot be represented as Decimal(4, 2). If necessary set "spark.sql.ansi.enabled" to false to bypass this error.
== SQL(line 1, position 7) ==
select cast('123.45' as decimal(4, 2))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ select (5e36BD + 0.1) + 5e36BD
struct<>
-- !query output
org.apache.spark.SparkArithmeticException
[CANNOT_CHANGE_DECIMAL_PRECISION] Decimal(expanded,10000000000000000000000000000000000000.1,39,1}) cannot be represented as Decimal(38, 1). If necessary set spark.sql.ansi.enabled to false to bypass this error.
[CANNOT_CHANGE_DECIMAL_PRECISION] Decimal(expanded,10000000000000000000000000000000000000.1,39,1}) cannot be represented as Decimal(38, 1). If necessary set "spark.sql.ansi.enabled" to false to bypass this error.
== SQL(line 1, position 7) ==
select (5e36BD + 0.1) + 5e36BD
^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -88,7 +88,7 @@ select (-4e36BD - 0.1) - 7e36BD
struct<>
-- !query output
org.apache.spark.SparkArithmeticException
[CANNOT_CHANGE_DECIMAL_PRECISION] Decimal(expanded,-11000000000000000000000000000000000000.1,39,1}) cannot be represented as Decimal(38, 1). If necessary set spark.sql.ansi.enabled to false to bypass this error.
[CANNOT_CHANGE_DECIMAL_PRECISION] Decimal(expanded,-11000000000000000000000000000000000000.1,39,1}) cannot be represented as Decimal(38, 1). If necessary set "spark.sql.ansi.enabled" to false to bypass this error.
== SQL(line 1, position 7) ==
select (-4e36BD - 0.1) - 7e36BD
^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -100,7 +100,7 @@ select 12345678901234567890.0 * 12345678901234567890.0
struct<>
-- !query output
org.apache.spark.SparkArithmeticException
[CANNOT_CHANGE_DECIMAL_PRECISION] Decimal(expanded,152415787532388367501905199875019052100,39,0}) cannot be represented as Decimal(38, 2). If necessary set spark.sql.ansi.enabled to false to bypass this error.
[CANNOT_CHANGE_DECIMAL_PRECISION] Decimal(expanded,152415787532388367501905199875019052100,39,0}) cannot be represented as Decimal(38, 2). If necessary set "spark.sql.ansi.enabled" to false to bypass this error.
== SQL(line 1, position 7) ==
select 12345678901234567890.0 * 12345678901234567890.0
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -112,7 +112,7 @@ select 1e35BD / 0.1
struct<>
-- !query output
org.apache.spark.SparkArithmeticException
[CANNOT_CHANGE_DECIMAL_PRECISION] Decimal(expanded,1000000000000000000000000000000000000,37,0}) cannot be represented as Decimal(38, 6). If necessary set spark.sql.ansi.enabled to false to bypass this error.
[CANNOT_CHANGE_DECIMAL_PRECISION] Decimal(expanded,1000000000000000000000000000000000000,37,0}) cannot be represented as Decimal(38, 6). If necessary set "spark.sql.ansi.enabled" to false to bypass this error.
== SQL(line 1, position 7) ==
select 1e35BD / 0.1
^^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ select interval '2 seconds' / 0
struct<>
-- !query output
org.apache.spark.SparkArithmeticException
[DIVIDE_BY_ZERO] divide by zero. To return NULL instead, use 'try_divide'. If necessary set spark.sql.ansi.enabled to false (except for ANSI interval type) to bypass this error.
[DIVIDE_BY_ZERO] divide by zero. To return NULL instead, use 'try_divide'. If necessary set "spark.sql.ansi.enabled" to false (except for ANSI interval type) to bypass this error.
== SQL(line 1, position 7) ==
select interval '2 seconds' / 0
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -264,7 +264,7 @@ select interval '2' year / 0
struct<>
-- !query output
org.apache.spark.SparkArithmeticException
[DIVIDE_BY_ZERO] divide by zero. To return NULL instead, use 'try_divide'. If necessary set spark.sql.ansi.enabled to false (except for ANSI interval type) to bypass this error.
[DIVIDE_BY_ZERO] divide by zero. To return NULL instead, use 'try_divide'. If necessary set "spark.sql.ansi.enabled" to false (except for ANSI interval type) to bypass this error.
== SQL(line 1, position 7) ==
select interval '2' year / 0
^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -664,7 +664,7 @@ select make_interval(0, 0, 0, 0, 0, 0, 1234567890123456789)
struct<>
-- !query output
org.apache.spark.SparkArithmeticException
[CANNOT_CHANGE_DECIMAL_PRECISION] Decimal(expanded,1234567890123456789,20,0}) cannot be represented as Decimal(18, 6). If necessary set spark.sql.ansi.enabled to false to bypass this error.
[CANNOT_CHANGE_DECIMAL_PRECISION] Decimal(expanded,1234567890123456789,20,0}) cannot be represented as Decimal(18, 6). If necessary set "spark.sql.ansi.enabled" to false to bypass this error.
== SQL(line 1, position 7) ==
select make_interval(0, 0, 0, 0, 0, 0, 1234567890123456789)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ SELECT make_timestamp(2021, 07, 11, 6, 30, 60.007)
struct<>
-- !query output
org.apache.spark.SparkDateTimeException
[INVALID_FRACTION_OF_SECOND] The fraction of sec must be zero. Valid range is [0, 60]. If necessary set spark.sql.ansi.enabled to false to bypass this error.
[INVALID_FRACTION_OF_SECOND] The fraction of sec must be zero. Valid range is [0, 60]. If necessary set "spark.sql.ansi.enabled" to false to bypass this error.


-- !query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ select interval '2 seconds' / 0
struct<>
-- !query output
org.apache.spark.SparkArithmeticException
[DIVIDE_BY_ZERO] divide by zero. To return NULL instead, use 'try_divide'. If necessary set spark.sql.ansi.enabled to false (except for ANSI interval type) to bypass this error.
[DIVIDE_BY_ZERO] divide by zero. To return NULL instead, use 'try_divide'. If necessary set "spark.sql.ansi.enabled" to false (except for ANSI interval type) to bypass this error.
== SQL(line 1, position 7) ==
select interval '2 seconds' / 0
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -240,7 +240,7 @@ select interval '2' year / 0
struct<>
-- !query output
org.apache.spark.SparkArithmeticException
[DIVIDE_BY_ZERO] divide by zero. To return NULL instead, use 'try_divide'. If necessary set spark.sql.ansi.enabled to false (except for ANSI interval type) to bypass this error.
[DIVIDE_BY_ZERO] divide by zero. To return NULL instead, use 'try_divide'. If necessary set "spark.sql.ansi.enabled" to false (except for ANSI interval type) to bypass this error.
== SQL(line 1, position 7) ==
select interval '2' year / 0
^^^^^^^^^^^^^^^^^^^^^
Expand Down
Loading