Skip to content

Commit

Permalink
[SPARK-44987][SQL] Assign a name to the error class `_LEGACY_ERROR_TE…
Browse files Browse the repository at this point in the history
…MP_1100`

### What changes were proposed in this pull request?
In the PR, I propose to assign the name `NON_FOLDABLE_ARGUMENT` to the legacy error class `_LEGACY_ERROR_TEMP_1100`, and improve the error message format: make it less restrictive.

### Why are the changes needed?
1. To don't confuse users by slightly restrictive error message about literals.
2. To assign proper name as a part of activity in SPARK-37935

### Does this PR introduce _any_ user-facing change?
No. Only if user's code depends on error class name and message parameters.

### How was this patch tested?
By running the modified and affected tests:
```
$ build/sbt "test:testOnly *.StringFunctionsSuite"
$ PYSPARK_PYTHON=python3 build/sbt "sql/testOnly org.apache.spark.sql.SQLQueryTestSuite"
$ build/sbt "core/testOnly *SparkThrowableSuite"
```

### Was this patch authored or co-authored using generative AI tooling?
No.

Closes #42737 from MaxGekk/assign-name-_LEGACY_ERROR_TEMP_1100.

Authored-by: Max Gekk <[email protected]>
Signed-off-by: Max Gekk <[email protected]>
  • Loading branch information
MaxGekk committed Aug 31, 2023
1 parent 723a0aa commit e72ce91
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 67 deletions.
11 changes: 6 additions & 5 deletions common/utils/src/main/resources/error/error-classes.json
Original file line number Diff line number Diff line change
Expand Up @@ -2215,6 +2215,12 @@
],
"sqlState" : "42607"
},
"NON_FOLDABLE_ARGUMENT" : {
"message" : [
"The function <funcName> requires the parameter <paramName> to be a foldable expression of the type <paramType>, but the actual argument is a non-foldable."
],
"sqlState" : "22024"
},
"NON_LAST_MATCHED_CLAUSE_OMIT_CONDITION" : {
"message" : [
"When there are more than one MATCHED clauses in a MERGE statement, only the last MATCHED clause can omit the condition."
Expand Down Expand Up @@ -4029,11 +4035,6 @@
"<funcName>() doesn't support the <mode> mode. Acceptable modes are <permissiveMode> and <failFastMode>."
]
},
"_LEGACY_ERROR_TEMP_1100" : {
"message" : [
"The '<argName>' parameter of function '<funcName>' needs to be a <requiredType> literal."
]
},
"_LEGACY_ERROR_TEMP_1103" : {
"message" : [
"Unsupported component type <clz> in arrays."
Expand Down
6 changes: 6 additions & 0 deletions docs/sql-error-conditions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,12 @@ Cannot call function `<functionName>` because named argument references are not

It is not allowed to use an aggregate function in the argument of another aggregate function. Please use the inner aggregate function in a sub-query.

### NON_FOLDABLE_ARGUMENT

[SQLSTATE: 22024](sql-error-conditions-sqlstates.html#class-22-data-exception)

The function `<funcName>` requires the parameter `<paramName>` to be a foldable expression of the type `<paramType>`, but the actual argument is a non-foldable.

### NON_LAST_MATCHED_CLAUSE_OMIT_CONDITION

[SQLSTATE: 42613](sql-error-conditions-sqlstates.html#class-42-syntax-error-or-access-rule-violation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2934,7 +2934,7 @@ object Extract {
}
}
} else {
throw QueryCompilationErrors.requireLiteralParameter(funcName, "field", "string")
throw QueryCompilationErrors.nonFoldableArgumentError(funcName, "field", StringType)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,10 @@ trait CeilFloorExpressionBuilderBase extends ExpressionBuilder {
} else if (numArgs == 2) {
val scale = expressions(1)
if (!(scale.foldable && scale.dataType == IntegerType)) {
throw QueryCompilationErrors.requireLiteralParameter(funcName, "scale", "int")
throw QueryCompilationErrors.nonFoldableArgumentError(funcName, "scale", IntegerType)
}
if (scale.eval() == null) {
throw QueryCompilationErrors.requireLiteralParameter(funcName, "scale", "int")
throw QueryCompilationErrors.nonFoldableArgumentError(funcName, "scale", IntegerType)
}
buildWithTwoParams(expressions(0), scale)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ object ToCharacterBuilder extends ExpressionBuilder {
case _: DatetimeType => DateFormatClass(inputExpr, format)
case _: BinaryType =>
if (!(format.dataType == StringType && format.foldable)) {
throw QueryCompilationErrors.requireLiteralParameter(funcName, "format", "string")
throw QueryCompilationErrors.nonFoldableArgumentError(funcName, "format", StringType)
}
format.eval().asInstanceOf[UTF8String].toString.toLowerCase(Locale.ROOT).trim match {
case "base64" => Base64(inputExpr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1207,14 +1207,16 @@ private[sql] object QueryCompilationErrors extends QueryErrorsBase with Compilat
"failFastMode" -> FailFastMode.name))
}

def requireLiteralParameter(
funcName: String, argName: String, requiredType: String): Throwable = {
def nonFoldableArgumentError(
funcName: String,
paramName: String,
paramType: DataType): Throwable = {
new AnalysisException(
errorClass = "_LEGACY_ERROR_TEMP_1100",
errorClass = "NON_FOLDABLE_ARGUMENT",
messageParameters = Map(
"argName" -> argName,
"funcName" -> funcName,
"requiredType" -> requiredType))
"funcName" -> toSQLId(funcName),
"paramName" -> toSQLId(paramName),
"paramType" -> toSQLType(paramType)))
}

def literalTypeUnsupportedForSourceTypeError(field: String, source: Expression): Throwable = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ SELECT CEIL(2.5, null)
-- !query analysis
org.apache.spark.sql.AnalysisException
{
"errorClass" : "_LEGACY_ERROR_TEMP_1100",
"errorClass" : "NON_FOLDABLE_ARGUMENT",
"sqlState" : "22024",
"messageParameters" : {
"argName" : "scale",
"funcName" : "ceil",
"requiredType" : "int"
"funcName" : "`ceil`",
"paramName" : "`scale`",
"paramType" : "\"INT\""
},
"queryContext" : [ {
"objectType" : "",
Expand All @@ -102,11 +103,12 @@ SELECT CEIL(2.5, 'a')
-- !query analysis
org.apache.spark.sql.AnalysisException
{
"errorClass" : "_LEGACY_ERROR_TEMP_1100",
"errorClass" : "NON_FOLDABLE_ARGUMENT",
"sqlState" : "22024",
"messageParameters" : {
"argName" : "scale",
"funcName" : "ceil",
"requiredType" : "int"
"funcName" : "`ceil`",
"paramName" : "`scale`",
"paramType" : "\"INT\""
},
"queryContext" : [ {
"objectType" : "",
Expand Down Expand Up @@ -223,11 +225,12 @@ SELECT FLOOR(2.5, null)
-- !query analysis
org.apache.spark.sql.AnalysisException
{
"errorClass" : "_LEGACY_ERROR_TEMP_1100",
"errorClass" : "NON_FOLDABLE_ARGUMENT",
"sqlState" : "22024",
"messageParameters" : {
"argName" : "scale",
"funcName" : "floor",
"requiredType" : "int"
"funcName" : "`floor`",
"paramName" : "`scale`",
"paramType" : "\"INT\""
},
"queryContext" : [ {
"objectType" : "",
Expand All @@ -244,11 +247,12 @@ SELECT FLOOR(2.5, 'a')
-- !query analysis
org.apache.spark.sql.AnalysisException
{
"errorClass" : "_LEGACY_ERROR_TEMP_1100",
"errorClass" : "NON_FOLDABLE_ARGUMENT",
"sqlState" : "22024",
"messageParameters" : {
"argName" : "scale",
"funcName" : "floor",
"requiredType" : "int"
"funcName" : "`floor`",
"paramName" : "`scale`",
"paramType" : "\"INT\""
},
"queryContext" : [ {
"objectType" : "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -932,11 +932,12 @@ select date_part(c, c) from t
-- !query analysis
org.apache.spark.sql.AnalysisException
{
"errorClass" : "_LEGACY_ERROR_TEMP_1100",
"errorClass" : "NON_FOLDABLE_ARGUMENT",
"sqlState" : "22024",
"messageParameters" : {
"argName" : "field",
"funcName" : "date_part",
"requiredType" : "string"
"funcName" : "`date_part`",
"paramName" : "`field`",
"paramType" : "\"STRING\""
},
"queryContext" : [ {
"objectType" : "",
Expand Down Expand Up @@ -964,11 +965,12 @@ select date_part(i, i) from t
-- !query analysis
org.apache.spark.sql.AnalysisException
{
"errorClass" : "_LEGACY_ERROR_TEMP_1100",
"errorClass" : "NON_FOLDABLE_ARGUMENT",
"sqlState" : "22024",
"messageParameters" : {
"argName" : "field",
"funcName" : "date_part",
"requiredType" : "string"
"funcName" : "`date_part`",
"paramName" : "`field`",
"paramType" : "\"STRING\""
},
"queryContext" : [ {
"objectType" : "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ struct<>
-- !query output
org.apache.spark.sql.AnalysisException
{
"errorClass" : "_LEGACY_ERROR_TEMP_1100",
"errorClass" : "NON_FOLDABLE_ARGUMENT",
"sqlState" : "22024",
"messageParameters" : {
"argName" : "scale",
"funcName" : "ceil",
"requiredType" : "int"
"funcName" : "`ceil`",
"paramName" : "`scale`",
"paramType" : "\"INT\""
},
"queryContext" : [ {
"objectType" : "",
Expand All @@ -117,11 +118,12 @@ struct<>
-- !query output
org.apache.spark.sql.AnalysisException
{
"errorClass" : "_LEGACY_ERROR_TEMP_1100",
"errorClass" : "NON_FOLDABLE_ARGUMENT",
"sqlState" : "22024",
"messageParameters" : {
"argName" : "scale",
"funcName" : "ceil",
"requiredType" : "int"
"funcName" : "`ceil`",
"paramName" : "`scale`",
"paramType" : "\"INT\""
},
"queryContext" : [ {
"objectType" : "",
Expand Down Expand Up @@ -253,11 +255,12 @@ struct<>
-- !query output
org.apache.spark.sql.AnalysisException
{
"errorClass" : "_LEGACY_ERROR_TEMP_1100",
"errorClass" : "NON_FOLDABLE_ARGUMENT",
"sqlState" : "22024",
"messageParameters" : {
"argName" : "scale",
"funcName" : "floor",
"requiredType" : "int"
"funcName" : "`floor`",
"paramName" : "`scale`",
"paramType" : "\"INT\""
},
"queryContext" : [ {
"objectType" : "",
Expand All @@ -276,11 +279,12 @@ struct<>
-- !query output
org.apache.spark.sql.AnalysisException
{
"errorClass" : "_LEGACY_ERROR_TEMP_1100",
"errorClass" : "NON_FOLDABLE_ARGUMENT",
"sqlState" : "22024",
"messageParameters" : {
"argName" : "scale",
"funcName" : "floor",
"requiredType" : "int"
"funcName" : "`floor`",
"paramName" : "`scale`",
"paramType" : "\"INT\""
},
"queryContext" : [ {
"objectType" : "",
Expand Down
18 changes: 10 additions & 8 deletions sql/core/src/test/resources/sql-tests/results/extract.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -714,11 +714,12 @@ struct<>
-- !query output
org.apache.spark.sql.AnalysisException
{
"errorClass" : "_LEGACY_ERROR_TEMP_1100",
"errorClass" : "NON_FOLDABLE_ARGUMENT",
"sqlState" : "22024",
"messageParameters" : {
"argName" : "field",
"funcName" : "date_part",
"requiredType" : "string"
"funcName" : "`date_part`",
"paramName" : "`field`",
"paramType" : "\"STRING\""
},
"queryContext" : [ {
"objectType" : "",
Expand All @@ -745,11 +746,12 @@ struct<>
-- !query output
org.apache.spark.sql.AnalysisException
{
"errorClass" : "_LEGACY_ERROR_TEMP_1100",
"errorClass" : "NON_FOLDABLE_ARGUMENT",
"sqlState" : "22024",
"messageParameters" : {
"argName" : "field",
"funcName" : "date_part",
"requiredType" : "string"
"funcName" : "`date_part`",
"paramName" : "`field`",
"paramType" : "\"STRING\""
},
"queryContext" : [ {
"objectType" : "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -875,11 +875,11 @@ class StringFunctionsSuite extends QueryTest with SharedSparkSession {
exception = intercept[AnalysisException] {
df2.select(func(col("input"), col("format"))).collect()
},
errorClass = "_LEGACY_ERROR_TEMP_1100",
errorClass = "NON_FOLDABLE_ARGUMENT",
parameters = Map(
"argName" -> "format",
"funcName" -> funcName,
"requiredType" -> "string"))
"funcName" -> s"`$funcName`",
"paramName" -> "`format`",
"paramType" -> "\"STRING\""))
checkError(
exception = intercept[AnalysisException] {
df2.select(func(col("input"), lit("invalid_format"))).collect()
Expand Down

0 comments on commit e72ce91

Please sign in to comment.