Skip to content
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
bcd53d2
[SPARK-38692][SQL] Use error classes in the compilation errors of fun…
lvshaokang Apr 22, 2022
9602765
[SPARK-38692][SQL] Use error classes in the compilation errors of fun…
lvshaokang Apr 24, 2022
8e59f84
Merge branch 'master' into SPARK-38692
lvshaokang Apr 24, 2022
bea3fe5
[SPARK-38692][SQL] Use error classes in the compilation errors of fun…
lvshaokang Apr 24, 2022
efa8dac
[SPARK-38692][SQL] Use error classes in the compilation errors of fun…
lvshaokang Apr 24, 2022
85e4c96
[SPARK-38692][SQL] Use error classes in the compilation errors of fun…
lvshaokang Apr 25, 2022
e7c6a68
style(): code style format
lvshaokang Apr 26, 2022
bcd5a50
Merge branch 'master' into SPARK-38692
lvshaokang Apr 26, 2022
c397177
Merge branch 'master' into SPARK-38692
lvshaokang Apr 27, 2022
7c92a85
[SPARK-38692][SQL] Use error classes in the compilation errors of fun…
lvshaokang May 1, 2022
cb81ea5
Merge branch 'master' into SPARK-38692
lvshaokang May 1, 2022
e21b47a
style(): code style format
lvshaokang May 5, 2022
ddccd54
Merge branch 'master' into SPARK-38692
lvshaokang May 5, 2022
aaa0760
Merge branch 'master' into SPARK-38692
lvshaokang May 6, 2022
1106961
fix(): unify `INVALID_PARAMETER_VALUE` class and format message
lvshaokang Aug 17, 2022
ffc521b
Merge branch 'master' into SPARK-38692
lvshaokang Aug 17, 2022
c522bb8
fix(): format message
lvshaokang Aug 18, 2022
cd91055
Merge branch 'master' into SPARK-38692
lvshaokang Aug 18, 2022
82a6fd7
fix(): format message
lvshaokang Aug 18, 2022
acad322
fix(): format message
lvshaokang Aug 18, 2022
e84cbec
Merge branch 'master' into SPARK-38692
lvshaokang Aug 19, 2022
43ac9a0
format message with json
lvshaokang Aug 19, 2022
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
27 changes: 27 additions & 0 deletions core/src/main/resources/error/error-classes.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,33 @@
"message" : [ "The fraction of sec must be zero. Valid range is [0, 60]. If necessary set <config> to false to bypass this error. " ],
"sqlState" : "22023"
},
"INVALID_FUNCTION_ARGUMENTS" : {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would it be possible to use the existing error class INVALID_PARAMETER_VALUE?

"message" : [ "The function arguments invalid: " ],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As I can see, every sub-class repeats function name. I would propose to add the function name to the base class, like:

  "message" : [ "Arguments of the <funcName> function are invalid: " ],

"subClass" : {
"APPROX_COUNT_DISTINCT" : {
"message" : [ "The second argument in <functionName> should be a double literal" ]
},
"CAST_ALIAS" : {
"message" : [ "Function <functionName> accepts only one argument" ]
},
"EMPTY_NUMBER_OF_ARGUMENTS" : {
"message" : [ "Invalid number of arguments for function <functionName>" ]
},
"FIRST_LAST" : {
"message" : [ "The second argument in <functionName> should be a boolean literal" ]
},
"INVALID_NUMBER_OF_ARGUMENTS" : {
"message" : [ "Invalid number of arguments for function <functionName>. Expected: <expected>; Found: <found>" ]
},
"INVALID_NUMBER_OF_ARGUMENTS_FOR_V2FUNCTION" : {
"message" : [ "There are <expected> arguments in V2Function <functionName>, but <found> parameters returned from 'inputTypes()'" ]
},
"INVALID_OPERATION_FOR_V2FUNCTION" : {
"message" : [ "V2Function <functionName> cannot process input: (<type>): <message>" ]
}
},
"sqlState" : "22023"
},
"INVALID_JSON_SCHEMA_MAP_TYPE" : {
"message" : [ "Input schema <jsonSchema> can only contain STRING as a key type for a MAP." ]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ object FunctionRegistryBase {
} catch {
// the exception is an invocation exception. To get a meaningful message, we need the
// cause.
case e: Exception => throw new AnalysisException(e.getCause.getMessage)
case e: Exception =>
e.getCause match {
case ae: AnalysisException if ae.errorClass.isDefined => throw ae;
case other => throw new AnalysisException(other.getMessage)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ case class ApproxCountDistinctForIntervals(
this(
child = child,
endpointsExpression = endpointsExpression,
relativeSD = HyperLogLogPlusPlus.validateDoubleLiteral(relativeSD),
relativeSD = HyperLogLogPlusPlus
.validateDoubleLiteral("ApproxCountDistinctForIntervals", relativeSD),
mutableAggBufferOffset = 0,
inputAggBufferOffset = 0)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ case class HyperLogLogPlusPlus(
def this(child: Expression, relativeSD: Expression) = {
this(
child = child,
relativeSD = HyperLogLogPlusPlus.validateDoubleLiteral(relativeSD),
relativeSD = HyperLogLogPlusPlus.validateDoubleLiteral("approx_count_distinct", relativeSD),
mutableAggBufferOffset = 0,
inputAggBufferOffset = 0)
}
Expand Down Expand Up @@ -144,10 +144,10 @@ case class HyperLogLogPlusPlus(
}

object HyperLogLogPlusPlus {
def validateDoubleLiteral(exp: Expression): Double = exp match {
def validateDoubleLiteral(name: String, exp: Expression): Double = exp match {
case Literal(d: Double, DoubleType) => d
case Literal(dec: Decimal, _) => dec.toDouble
case _ =>
throw QueryCompilationErrors.secondArgumentNotDoubleLiteralError
throw QueryCompilationErrors.secondArgumentNotDoubleLiteralError(name)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -442,14 +442,18 @@ object QueryCompilationErrors extends QueryErrorsBase {

def invalidFunctionArgumentsError(
name: String, expectedInfo: String, actualNumber: Int): Throwable = {
new AnalysisException(s"Invalid number of arguments for function $name. " +
s"Expected: $expectedInfo; Found: $actualNumber")
new AnalysisException(
errorClass = "INVALID_FUNCTION_ARGUMENTS",
messageParameters = Array("INVALID_NUMBER_OF_ARGUMENTS",
name, expectedInfo, s"$actualNumber"))
}

def invalidFunctionArgumentNumberError(
validParametersCount: Seq[Int], name: String, actualNumber: Int): Throwable = {
if (validParametersCount.length == 0) {
new AnalysisException(s"Invalid arguments for function $name")
if (validParametersCount.isEmpty) {
new AnalysisException(
errorClass = "INVALID_FUNCTION_ARGUMENTS",
messageParameters = Array("EMPTY_NUMBER_OF_ARGUMENTS", name))
} else {
val expectedNumberOfParameters = if (validParametersCount.length == 1) {
validParametersCount.head.toString
Expand All @@ -462,7 +466,9 @@ object QueryCompilationErrors extends QueryErrorsBase {
}

def functionAcceptsOnlyOneArgumentError(name: String): Throwable = {
new AnalysisException(s"Function $name accepts only one argument")
new AnalysisException(
errorClass = "INVALID_FUNCTION_ARGUMENTS",
messageParameters = Array("CAST_ALIAS", name))
}

def alterV2TableSetLocationWithPartitionNotSupportedError(): Throwable = {
Expand Down Expand Up @@ -765,8 +771,11 @@ object QueryCompilationErrors extends QueryErrorsBase {
new AnalysisException(s"Unsupported component type $clz in arrays")
}

def secondArgumentNotDoubleLiteralError(): Throwable = {
new AnalysisException("The second argument should be a double literal.")
def secondArgumentNotDoubleLiteralError(name: String): Throwable = {
new AnalysisException(
errorClass = "INVALID_FUNCTION_ARGUMENTS",
messageParameters = Array("APPROX_COUNT_DISTINCT", name)
)
}

def dataTypeUnsupportedByExtractValueError(
Expand Down Expand Up @@ -1479,16 +1488,23 @@ object QueryCompilationErrors extends QueryErrorsBase {
unbound: UnboundFunction,
arguments: Seq[Expression],
unsupported: UnsupportedOperationException): Throwable = {
new AnalysisException(s"Function '${unbound.name}' cannot process " +
s"input: (${arguments.map(_.dataType.simpleString).mkString(", ")}): " +
unsupported.getMessage, cause = Some(unsupported))
new AnalysisException(
errorClass = "INVALID_FUNCTION_ARGUMENTS",
messageParameters = Array("INVALID_OPERATION_FOR_V2FUNCTION",
unbound.name, arguments.map(x => toSQLType(x.dataType)).mkString(", "),
unsupported.getMessage),
cause = Some(unsupported))
}

def v2FunctionInvalidInputTypeLengthError(
bound: BoundFunction,
args: Seq[Expression]): Throwable = {
new AnalysisException(s"Invalid bound function '${bound.name()}: there are ${args.length} " +
s"arguments but ${bound.inputTypes().length} parameters returned from 'inputTypes()'")
new AnalysisException(
errorClass = "INVALID_FUNCTION_ARGUMENTS",
messageParameters = Array("INVALID_NUMBER_OF_ARGUMENTS_FOR_V2FUNCTION",
s"${args.length}", bound.name(),
s"${bound.inputTypes().length}")
)
}

def ambiguousRelationAliasNameInNestedCTEError(name: String): Throwable = {
Expand Down Expand Up @@ -1568,7 +1584,10 @@ object QueryCompilationErrors extends QueryErrorsBase {
}

def secondArgumentInFunctionIsNotBooleanLiteralError(funcName: String): Throwable = {
new AnalysisException(s"The second argument in $funcName should be a boolean literal.")
new AnalysisException(
errorClass = "INVALID_FUNCTION_ARGUMENTS",
messageParameters = Array("FIRST_LAST", funcName)
)
}

def joinConditionMissingOrTrivialError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ select decode()
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
Invalid number of arguments for function decode. Expected: 2; Found: 0; line 1 pos 7
[INVALID_FUNCTION_ARGUMENTS.INVALID_NUMBER_OF_ARGUMENTS] The function arguments invalid: Invalid number of arguments for function decode. Expected: 2; Found: 0; line 1 pos 7


-- !query
Expand All @@ -676,7 +676,7 @@ select decode(encode('abc', 'utf-8'))
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
Invalid number of arguments for function decode. Expected: 2; Found: 1; line 1 pos 7
[INVALID_FUNCTION_ARGUMENTS.INVALID_NUMBER_OF_ARGUMENTS] The function arguments invalid: Invalid number of arguments for function decode. Expected: 2; Found: 1; line 1 pos 7


-- !query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ SELECT CEIL(2.5, 0, 0)
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
Invalid number of arguments for function ceil. Expected: 2; Found: 3; line 1 pos 7
[INVALID_FUNCTION_ARGUMENTS.INVALID_NUMBER_OF_ARGUMENTS] The function arguments invalid: Invalid number of arguments for function ceil. Expected: 2; Found: 3; line 1 pos 7


-- !query
Expand Down Expand Up @@ -197,4 +197,4 @@ SELECT FLOOR(2.5, 0, 0)
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
Invalid number of arguments for function floor. Expected: 2; Found: 3; line 1 pos 7
[INVALID_FUNCTION_ARGUMENTS.INVALID_NUMBER_OF_ARGUMENTS] The function arguments invalid: Invalid number of arguments for function floor. Expected: 2; Found: 3; line 1 pos 7
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ select from_csv()
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
Invalid number of arguments for function from_csv. Expected: one of 2 and 3; Found: 0; line 1 pos 7
[INVALID_FUNCTION_ARGUMENTS.INVALID_NUMBER_OF_ARGUMENTS] The function arguments invalid: Invalid number of arguments for function from_csv. Expected: one of 2 and 3; Found: 0; line 1 pos 7


-- !query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ select to_json()
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
Invalid number of arguments for function to_json. Expected: one of 1 and 2; Found: 0; line 1 pos 7
[INVALID_FUNCTION_ARGUMENTS.INVALID_NUMBER_OF_ARGUMENTS] The function arguments invalid: Invalid number of arguments for function to_json. Expected: one of 1 and 2; Found: 0; line 1 pos 7


-- !query
Expand Down Expand Up @@ -164,7 +164,7 @@ select from_json()
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
Invalid number of arguments for function from_json. Expected: one of 2 and 3; Found: 0; line 1 pos 7
[INVALID_FUNCTION_ARGUMENTS.INVALID_NUMBER_OF_ARGUMENTS] The function arguments invalid: Invalid number of arguments for function from_json. Expected: one of 2 and 3; Found: 0; line 1 pos 7


-- !query
Expand Down Expand Up @@ -437,7 +437,7 @@ select json_array_length()
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
Invalid number of arguments for function json_array_length. Expected: 1; Found: 0; line 1 pos 7
[INVALID_FUNCTION_ARGUMENTS.INVALID_NUMBER_OF_ARGUMENTS] The function arguments invalid: Invalid number of arguments for function json_array_length. Expected: 1; Found: 0; line 1 pos 7


-- !query
Expand Down Expand Up @@ -510,7 +510,7 @@ select json_object_keys()
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
Invalid number of arguments for function json_object_keys. Expected: 1; Found: 0; line 1 pos 7
[INVALID_FUNCTION_ARGUMENTS.INVALID_NUMBER_OF_ARGUMENTS] The function arguments invalid: Invalid number of arguments for function json_object_keys. Expected: 1; Found: 0; line 1 pos 7


-- !query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ SELECT string(1, 2)
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
Function string accepts only one argument; line 1 pos 7
[INVALID_FUNCTION_ARGUMENTS.CAST_ALIAS] The function arguments invalid: Function string accepts only one argument; line 1 pos 7


-- !query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ select decode()
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
Invalid number of arguments for function decode. Expected: 2; Found: 0; line 1 pos 7
[INVALID_FUNCTION_ARGUMENTS.INVALID_NUMBER_OF_ARGUMENTS] The function arguments invalid: Invalid number of arguments for function decode. Expected: 2; Found: 0; line 1 pos 7


-- !query
Expand All @@ -660,7 +660,7 @@ select decode(encode('abc', 'utf-8'))
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
Invalid number of arguments for function decode. Expected: 2; Found: 1; line 1 pos 7
[INVALID_FUNCTION_ARGUMENTS.INVALID_NUMBER_OF_ARGUMENTS] The function arguments invalid: Invalid number of arguments for function decode. Expected: 2; Found: 1; line 1 pos 7


-- !query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Table-valued function range with alternatives:
range(start: long, end: long, step: long)
range(start: long, end: long)
range(end: long)
cannot be applied to (integer, integer, integer, integer, integer): Invalid number of arguments for function range. Expected: one of 1, 2, 3 and 4; Found: 5; line 1 pos 14
cannot be applied to (integer, integer, integer, integer, integer): [INVALID_FUNCTION_ARGUMENTS.INVALID_NUMBER_OF_ARGUMENTS] The function arguments invalid: Invalid number of arguments for function range. Expected: one of 1, 2, 3 and 4; Found: 5; line 1 pos 14


-- !query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ SELECT make_timestamp_ntz(2021, 07, 11, 6, 30, 45.678, 'CET')
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
Invalid number of arguments for function make_timestamp_ntz. Expected: 6; Found: 7; line 1 pos 7
[INVALID_FUNCTION_ARGUMENTS.INVALID_NUMBER_OF_ARGUMENTS] The function arguments invalid: Invalid number of arguments for function make_timestamp_ntz. Expected: 6; Found: 7; line 1 pos 7


-- !query
Expand Down
2 changes: 1 addition & 1 deletion sql/core/src/test/resources/sql-tests/results/udaf.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ SELECT default.myDoubleAvg(int_col1, 3) as my_avg from t1
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
Invalid number of arguments for function default.myDoubleAvg. Expected: 1; Found: 2; line 1 pos 7
[INVALID_FUNCTION_ARGUMENTS.INVALID_NUMBER_OF_ARGUMENTS] The function arguments invalid: Invalid number of arguments for function default.myDoubleAvg. Expected: 1; Found: 2; line 1 pos 7


-- !query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ SELECT default.myDoubleAvg(udf(int_col1), udf(3)) as my_avg from t1
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
Invalid number of arguments for function default.myDoubleAvg. Expected: 1; Found: 2; line 1 pos 7
[INVALID_FUNCTION_ARGUMENTS.INVALID_NUMBER_OF_ARGUMENTS] The function arguments invalid: Invalid number of arguments for function default.myDoubleAvg. Expected: 1; Found: 2; line 1 pos 7


-- !query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ class UDFSuite extends QueryTest with SharedSparkSession {
val e = intercept[AnalysisException] {
spark.sql("SELECT CAST(1)")
}
assert(e.getMessage.contains("Invalid arguments for function cast"))
assert(e.getMessage.contains("Invalid number of arguments for function cast"))
}

test("only one case class parameter") {
Expand Down
Loading