From 3118d352b2188e58c3c8b214d9cc6bf1a52d466d Mon Sep 17 00:00:00 2001 From: dengziming Date: Thu, 26 Oct 2023 20:06:31 +0800 Subject: [PATCH] optimize sqlId and msg --- .../main/resources/error/error-classes.json | 2 +- ...ons-invalid-parameter-value-error-class.md | 2 +- .../expressions/collectionOperations.scala | 5 ++--- .../sql/errors/QueryExecutionErrors.scala | 8 +++----- .../CollectionExpressionsSuite.scala | 19 +++++++++++++++---- .../errors/QueryExecutionErrorsSuite.scala | 5 ++--- 6 files changed, 24 insertions(+), 17 deletions(-) diff --git a/common/utils/src/main/resources/error/error-classes.json b/common/utils/src/main/resources/error/error-classes.json index 121635a164236..90b1f863c866f 100644 --- a/common/utils/src/main/resources/error/error-classes.json +++ b/common/utils/src/main/resources/error/error-classes.json @@ -1990,7 +1990,7 @@ }, "START" : { "message" : [ - "Expects `start` to start at 1 or start from the end if start is negative, but got ." + "Expects a positive or a negative value for `start`, but got 0." ] }, "ZERO_INDEX" : { diff --git a/docs/sql-error-conditions-invalid-parameter-value-error-class.md b/docs/sql-error-conditions-invalid-parameter-value-error-class.md index 0db1c360524be..d58d4fc2f5992 100644 --- a/docs/sql-error-conditions-invalid-parameter-value-error-class.md +++ b/docs/sql-error-conditions-invalid-parameter-value-error-class.md @@ -67,7 +67,7 @@ Expects group index between 0 and ``, but got ``. ## START -Expects `start` to start at 1 or start from the end if start is negative, but got ``. +Expects a positive or a negative value for `start`, but got 0. ## ZERO_INDEX diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala index 05507a0b9f683..0a080423b10fe 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala @@ -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 { @@ -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 { diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala index 50a8aea3babc3..afc244509c41d 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala @@ -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))) } @@ -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))) } diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CollectionExpressionsSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CollectionExpressionsSuite.scala index d552deff46c1a..b3a21bd9565f7 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CollectionExpressionsSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CollectionExpressionsSuite.scala @@ -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) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/errors/QueryExecutionErrorsSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/errors/QueryExecutionErrorsSuite.scala index 07a4a3e627d30..345ca6e8930ba 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/errors/QueryExecutionErrorsSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/errors/QueryExecutionErrorsSuite.scala @@ -1026,8 +1026,7 @@ class QueryExecutionErrorsSuite }, errorClass = "INVALID_PARAMETER_VALUE.START", parameters = Map( - "parameter" -> "start", - "start" -> 0.toString, + "parameter" -> toSQLId("start"), "functionName" -> toSQLId("slice") ) ) @@ -1040,7 +1039,7 @@ class QueryExecutionErrorsSuite }, errorClass = "INVALID_PARAMETER_VALUE.LENGTH", parameters = Map( - "parameter" -> "length", + "parameter" -> toSQLId("length"), "length" -> (-1).toString, "functionName" -> toSQLId("slice") )