Skip to content

Commit

Permalink
Resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dengziming committed Oct 26, 2023
1 parent 9cb63e1 commit 4acf89e
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 99 deletions.
28 changes: 10 additions & 18 deletions common/utils/src/main/resources/error/error-classes.json
Original file line number Diff line number Diff line change
Expand Up @@ -1968,6 +1968,11 @@
"expects one of the units without quotes YEAR, QUARTER, MONTH, WEEK, DAY, DAYOFYEAR, HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND, but got the string literal <invalidValue>."
]
},
"LENGTH" : {
"message" : [
"Expects `length` greater than or equal to 0, but got <length>."
]
},
"NULL" : {
"message" : [
"expects a non-NULL value."
Expand All @@ -1983,6 +1988,11 @@
"Expects group index between 0 and <groupCount>, but got <groupIndex>."
]
},
"START" : {
"message" : [
"Expects `start` to start at 1 or start from the end if start is negative, but got <start>."
]
},
"ZERO_INDEX" : {
"message" : [
"expects %1$, %2$ and so on, but got %0$."
Expand Down Expand Up @@ -3029,18 +3039,6 @@
],
"sqlState" : "42846"
},
"UNEXPECTED_VALUE_FOR_LENGTH_IN_SLICE_FUNCTION" : {
"message" : [
"Unexpected value for length in function <prettyName>: length must be greater than or equal to 0."
],
"sqlState" : "22003"
},
"UNEXPECTED_VALUE_FOR_START_IN_SLICE_FUNCTION" : {
"message" : [
"Unexpected value for start in function <prettyName>: SQL array indices start at 1."
],
"sqlState" : "22003"
},
"UNKNOWN_PROTOBUF_MESSAGE_TYPE" : {
"message" : [
"Attempting to treat <descriptorName> as a Message, but it was <containingType>."
Expand Down Expand Up @@ -3225,12 +3223,6 @@
],
"sqlState" : "0A000"
},
"UNSUPPORTED_DATA_TYPE_FOR_SIZE_FUNCTION" : {
"message" : [
"The size function doesn't support the operand type <dataType>."
],
"sqlState" : "42K09"
},
"UNSUPPORTED_DEFAULT_VALUE" : {
"message" : [
"DEFAULT column values is not supported."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ expects an integer value in [0, `<upper>`), but got `<invalidValue>`.

expects one of the units without quotes YEAR, QUARTER, MONTH, WEEK, DAY, DAYOFYEAR, HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND, but got the string literal `<invalidValue>`.

## LENGTH

Expects `length` greater than or equal to 0, but got `<length>`.

## NULL

expects a non-NULL value.
Expand All @@ -61,6 +65,10 @@ expects a non-NULL value.

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>`.

## ZERO_INDEX

expects `%1$`, `%2$` and so on, but got `%0$`.
Expand Down
18 changes: 0 additions & 18 deletions docs/sql-error-conditions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1953,18 +1953,6 @@ Cannot invoke function `<functionName>` because it contains positional argument(

The class `<className>` has an unexpected expression serializer. Expects "STRUCT" or "IF" which returns "STRUCT" but found `<expr>`.

### UNEXPECTED_VALUE_FOR_LENGTH_IN_SLICE_FUNCTION

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

Unexpected value for length in function `<prettyName>`: length must be greater than or equal to 0.

### UNEXPECTED_VALUE_FOR_START_IN_SLICE_FUNCTION

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

Unexpected value for start in function `<prettyName>`: SQL array indices start at 1.

### UNKNOWN_PROTOBUF_MESSAGE_TYPE

[SQLSTATE: 42K0G](sql-error-conditions-sqlstates.html#class-42-syntax-error-or-access-rule-violation)
Expand Down Expand Up @@ -2109,12 +2097,6 @@ Unsupported data type `<typeName>`.

The `<format>` datasource doesn't support the column `<columnName>` of the type `<columnType>`.

### UNSUPPORTED_DATA_TYPE_FOR_SIZE_FUNCTION

[SQLSTATE: 42K09](sql-error-conditions-sqlstates.html#class-42-syntax-error-or-access-rule-violation)

The size function doesn't support the operand type `<dataType>`.

### [UNSUPPORTED_DEFAULT_VALUE](sql-error-conditions-unsupported-default-value-error-class.html)

[SQLSTATE: 0A000](sql-error-conditions-sqlstates.html#class-0A-feature-not-supported)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1761,14 +1761,14 @@ 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)
throw QueryExecutionErrors.unexpectedValueForStartInFunctionError(prettyName, startInt)
} else if (startInt < 0) {
startInt + arr.numElements()
} else {
startInt - 1
}
if (lengthInt < 0) {
throw QueryExecutionErrors.unexpectedValueForLengthInFunctionError(prettyName)
throw QueryExecutionErrors.unexpectedValueForLengthInFunctionError(prettyName, lengthInt)
}
// startIndex can be negative if start is negative and its absolute value is greater than the
// number of elements in the array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1368,25 +1368,29 @@ private[sql] object QueryExecutionErrors extends QueryErrorsBase with ExecutionE
}

def unsupportedOperandTypeForSizeFunctionError(
dataType: DataType): SparkUnsupportedOperationException = {
new SparkUnsupportedOperationException(
errorClass = "UNSUPPORTED_DATA_TYPE_FOR_SIZE_FUNCTION",
messageParameters = Map(
"dataType" -> dataType.getClass.getCanonicalName))
dataType: DataType): Throwable = {
SparkException.internalError(
s"The size function doesn't support the operand type ${toSQLType(dataType)}")
}

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

def unexpectedValueForLengthInFunctionError(prettyName: String): SparkRuntimeException = {
def unexpectedValueForLengthInFunctionError(
prettyName: String, length: Int): SparkRuntimeException = {
new SparkRuntimeException(
errorClass = "UNEXPECTED_VALUE_FOR_LENGTH_IN_SLICE_FUNCTION",
errorClass = "INVALID_PARAMETER_VALUE.LENGTH",
messageParameters = Map(
"prettyName" -> prettyName))
"parameter" -> "length",
"length" -> length.toString,
"functionName" -> toSQLId(prettyName)))
}

def invalidIndexOfZeroError(context: SQLQueryContext): RuntimeException = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.DataTypeMismatch
import org.apache.spark.sql.catalyst.util.{DateTimeTestUtils, DateTimeUtils}
import org.apache.spark.sql.catalyst.util.DateTimeTestUtils.{outstandingZoneIds, LA, UTC}
import org.apache.spark.sql.catalyst.util.IntervalUtils._
import org.apache.spark.sql.errors.DataTypeErrorsBase
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types._
import org.apache.spark.unsafe.array.ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH
import org.apache.spark.unsafe.types.UTF8String

class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
class CollectionExpressionsSuite
extends SparkFunSuite with ExpressionEvalHelper with DataTypeErrorsBase {

implicit def stringToUTF8Str(str: String): UTF8String = UTF8String.fromString(str)

Expand Down Expand Up @@ -87,14 +89,15 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
}

test("Unsupported data type for size()") {
val exception = intercept[org.apache.spark.SparkUnsupportedOperationException] {
val exception = intercept[org.apache.spark.SparkException] {
Size(Literal.create("str", StringType)).eval(EmptyRow)
}
checkError(
exception = exception,
errorClass = "UNSUPPORTED_DATA_TYPE_FOR_SIZE_FUNCTION",
errorClass = "INTERNAL_ERROR",
parameters = Map(
"dataType" -> StringType.getClass.getCanonicalName
"message" -> ("The size function doesn't support the operand type " +
toSQLType(StringType))
))
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import org.apache.spark.sql.catalyst.expressions.codegen.CodegenContext
import org.apache.spark.sql.catalyst.expressions.objects.InitializeJavaBean
import org.apache.spark.sql.catalyst.rules.RuleIdCollection
import org.apache.spark.sql.catalyst.util.BadRecordException
import org.apache.spark.sql.errors.DataTypeErrorsBase
import org.apache.spark.sql.execution.datasources.jdbc.{DriverRegistry, JDBCOptions}
import org.apache.spark.sql.execution.datasources.jdbc.connection.ConnectionProvider
import org.apache.spark.sql.execution.datasources.orc.OrcTest
Expand All @@ -55,7 +56,8 @@ class QueryExecutionErrorsSuite
extends QueryTest
with ParquetTest
with OrcTest
with SharedSparkSession {
with SharedSparkSession
with DataTypeErrorsBase {

import testImplicits._

Expand Down Expand Up @@ -1016,6 +1018,34 @@ class QueryExecutionErrorsSuite
)
}
}

test("Unexpected `start` for slice()") {
checkError(
exception = intercept[SparkRuntimeException] {
sql("select slice(array(1,2,3), 0, 1)").collect()
},
errorClass = "INVALID_PARAMETER_VALUE.START",
parameters = Map(
"parameter" -> "start",
"start" -> 0.toString,
"functionName" -> toSQLId("slice")
)
)
}

test("Unexpected `length` for slice()") {
checkError(
exception = intercept[SparkRuntimeException] {
sql("select slice(array(1,2,3), 1, -1)").collect()
},
errorClass = "INVALID_PARAMETER_VALUE.LENGTH",
parameters = Map(
"parameter" -> "length",
"start" -> (-1).toString,
"functionName" -> toSQLId("slice")
)
)
}
}

class FakeFileSystemSetPermission extends LocalFileSystem {
Expand Down

0 comments on commit 4acf89e

Please sign in to comment.