Skip to content

Commit

Permalink
optimize sqlId and msg
Browse files Browse the repository at this point in the history
  • Loading branch information
dengziming committed Oct 26, 2023
1 parent ccac57a commit 3118d35
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion common/utils/src/main/resources/error/error-classes.json
Original file line number Diff line number Diff line change
Expand Up @@ -1990,7 +1990,7 @@
},
"START" : {
"message" : [
"Expects `start` to start at 1 or start from the end if start is negative, but got <start>."
"Expects a positive or a negative value for `start`, but got 0."
]
},
"ZERO_INDEX" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Expects group index between 0 and `<groupCount>`, but got `<groupIndex>`.

## START

Expects `start` to start at 1 or start from the end if start is negative, but got `<start>`.
Expects a positive or a negative value for `start`, but got 0.

## ZERO_INDEX

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1761,7 +1761,7 @@ case class Slice(x: Expression, start: Expression, length: Expression)
val lengthInt = lengthVal.asInstanceOf[Int]
val arr = xVal.asInstanceOf[ArrayData]
val startIndex = if (startInt == 0) {
throw QueryExecutionErrors.unexpectedValueForStartInFunctionError(prettyName, startInt)
throw QueryExecutionErrors.unexpectedValueForStartInFunctionError(prettyName)
} else if (startInt < 0) {
startInt + arr.numElements()
} else {
Expand All @@ -1788,8 +1788,7 @@ case class Slice(x: Expression, start: Expression, length: Expression)
|${CodeGenerator.JAVA_INT} $startIdx = $defaultIntValue;
|${CodeGenerator.JAVA_INT} $resLength = $defaultIntValue;
|if ($start == 0) {
| throw QueryExecutionErrors.unexpectedValueForStartInFunctionError(
| "$prettyName", $start);
| throw QueryExecutionErrors.unexpectedValueForStartInFunctionError("$prettyName");
|} else if ($start < 0) {
| $startIdx = $start + $x.numElements();
|} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1373,13 +1373,11 @@ private[sql] object QueryExecutionErrors extends QueryErrorsBase with ExecutionE
s"The size function doesn't support the operand type ${toSQLType(dataType)}")
}

def unexpectedValueForStartInFunctionError(
prettyName: String, start: Int): SparkRuntimeException = {
def unexpectedValueForStartInFunctionError(prettyName: String): SparkRuntimeException = {
new SparkRuntimeException(
errorClass = "INVALID_PARAMETER_VALUE.START",
messageParameters = Map(
"parameter" -> "start",
"start" -> start.toString,
"parameter" -> toSQLId("start"),
"functionName" -> toSQLId(prettyName)))
}

Expand All @@ -1388,7 +1386,7 @@ private[sql] object QueryExecutionErrors extends QueryErrorsBase with ExecutionE
new SparkRuntimeException(
errorClass = "INVALID_PARAMETER_VALUE.LENGTH",
messageParameters = Map(
"parameter" -> "length",
"parameter" -> toSQLId("length"),
"length" -> length.toString,
"functionName" -> toSQLId(prettyName)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,10 +615,21 @@ class CollectionExpressionsSuite
checkEvaluation(Slice(a0, Literal(-3), Literal(2)), Seq(4, 5))
checkEvaluation(Slice(a0, Literal(4), Literal(10)), Seq(4, 5, 6))
checkEvaluation(Slice(a0, Literal(-1), Literal(2)), Seq(6))
checkExceptionInExpression[RuntimeException](Slice(a0, Literal(1), Literal(-1)),
"Expects `length` greater than or equal to 0, but got -1.")
checkExceptionInExpression[RuntimeException](Slice(a0, Literal(0), Literal(1)),
"Expects `start` to start at 1 or start from the end if start is negative, but got 0.")
checkErrorInExpression[SparkRuntimeException](
expression = Slice(a0, Literal(1), Literal(-1)),
errorClass = "INVALID_PARAMETER_VALUE.LENGTH",
parameters = Map(
"parameter" -> toSQLId("length"),
"length" -> (-1).toString,
"functionName" -> toSQLId("slice")
))
checkErrorInExpression[SparkRuntimeException](
expression = Slice(a0, Literal(0), Literal(1)),
errorClass = "INVALID_PARAMETER_VALUE.START",
parameters = Map(
"parameter" -> toSQLId("start"),
"functionName" -> toSQLId("slice")
))
checkEvaluation(Slice(a0, Literal(-20), Literal(1)), Seq.empty[Int])
checkEvaluation(Slice(a1, Literal(-20), Literal(1)), Seq.empty[String])
checkEvaluation(Slice(a0, Literal.create(null, IntegerType), Literal(2)), null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1026,8 +1026,7 @@ class QueryExecutionErrorsSuite
},
errorClass = "INVALID_PARAMETER_VALUE.START",
parameters = Map(
"parameter" -> "start",
"start" -> 0.toString,
"parameter" -> toSQLId("start"),
"functionName" -> toSQLId("slice")
)
)
Expand All @@ -1040,7 +1039,7 @@ class QueryExecutionErrorsSuite
},
errorClass = "INVALID_PARAMETER_VALUE.LENGTH",
parameters = Map(
"parameter" -> "length",
"parameter" -> toSQLId("length"),
"length" -> (-1).toString,
"functionName" -> toSQLId("slice")
)
Expand Down

0 comments on commit 3118d35

Please sign in to comment.