Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Add tests for errors
  • Loading branch information
MaxGekk committed Aug 26, 2023
commit b7987e10a6c2d6685f6c2c28e02c3e0e6d7e2d1a
5 changes: 5 additions & 0 deletions common/utils/src/main/resources/error/error-classes.json
Original file line number Diff line number Diff line change
Expand Up @@ -1788,6 +1788,11 @@
"expects a binary value with 16, 24 or 32 bytes, but got <actualLength> bytes."
]
},
"BINARY_FORMAT" : {
"message" : [
"expects one of binary formats 'base64', 'hex', 'utf-8', but got <invalidFormat>."
]
},
"DATETIME_UNIT" : {
"message" : [
"expects one of the units without quotes YEAR, QUARTER, MONTH, WEEK, DAY, DAYOFYEAR, HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND, but got the string literal <invalidValue>."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ supports 16-byte CBC IVs and 12-byte GCM IVs, but got `<actualLength>` bytes for

expects a binary value with 16, 24 or 32 bytes, but got `<actualLength>` bytes.

## BINARY_FORMAT

expects one of binary formats 'base64', 'hex', 'utf-8', but got `<invalidFormat>`.

## DATETIME_UNIT

expects one of the units without quotes YEAR, QUARTER, MONTH, WEEK, DAY, DAYOFYEAR, HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND, but got the string literal `<invalidValue>`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ object ToCharacterBuilder extends ExpressionBuilder {
if (expressions.length == 2) {
val (inputExpr, format) = (expressions(0), expressions(1))
Copy link
Contributor

Choose a reason for hiding this comment

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

this is a bit weird, we can just write 2 lines

val inputExpr = expressions(0)
val format = expressions(1)

Copy link
Member Author

Choose a reason for hiding this comment

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

Don't think it is the right approach to split semantically one operation: "assign names".

inputExpr.dataType match {
case _: DatetimeType => DateFormatClass(inputExpr, expressions(1))
case _: DatetimeType => DateFormatClass(inputExpr, format)
case _: BinaryType =>
if (!(format.dataType == StringType && format.foldable)) {
throw QueryCompilationErrors.requireLiteralParameter(funcName, "format", "string")
Expand All @@ -253,8 +253,9 @@ object ToCharacterBuilder extends ExpressionBuilder {
case "base64" => Base64(inputExpr)
case "hex" => Hex(inputExpr)
case "utf-8" => new Decode(Seq(inputExpr, format))
case invalid => throw QueryCompilationErrors.binaryFormatError(funcName, invalid)
}
case _ => ToCharacter(inputExpr, expressions(1))
case _ => ToCharacter(inputExpr, format)
}
} else {
throw QueryCompilationErrors.wrongNumArgsError(funcName, Seq(2), numArgs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ private[sql] object QueryCompilationErrors extends QueryErrorsBase with Compilat
"functionName" -> toSQLId("format_string")))
}

def binaryFormatError(funcName: String, invalidFormat: String): Throwable = {
new AnalysisException(
Copy link
Contributor

Choose a reason for hiding this comment

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

My feeling is that, if the error is only thrown in one place, we don't need to add a method here, just throw new AnalysisException... in the caller side.

Copy link
Member Author

Choose a reason for hiding this comment

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

One of purposes of gathering exceptions to Query*Errors to don't depend on forming exceptions: errors classes, quoting, context and so on. The caller has to provide only valuable info and don't worry about any technical stuff.

Don't think that this PR is right place to begin don't do that.

errorClass = "INVALID_PARAMETER_VALUE.BINARY_FORMAT",
messageParameters = Map(
"parameter" -> toSQLId("format"),
"functionName" -> toSQLId(funcName),
"invalidFormat" -> toSQLValue(invalidFormat, StringType)))
}

def unorderablePivotColError(pivotCol: Expression): Throwable = {
new AnalysisException(
errorClass = "INCOMPARABLE_PIVOT_COLUMN",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,26 @@ class StringFunctionsSuite extends QueryTest with SharedSparkSession {
"argName" -> "format",
"funcName" -> "to_char",
"requiredType" -> "string"))
checkError(
exception = intercept[AnalysisException] {
df2.select(to_char(col("input"), lit("invalid_format"))).collect()
},
errorClass = "INVALID_PARAMETER_VALUE.BINARY_FORMAT",
parameters = Map(
"parameter" -> "`format`",
"functionName" -> "`to_char`",
"invalidFormat" -> "'invalid_format'"))
checkError(
exception = intercept[AnalysisException] {
sql("select to_char('a', 'b', 'c')")
},
errorClass = "WRONG_NUM_ARGS.WITHOUT_SUGGESTION",
parameters = Map(
"functionName" -> "`to_char`",
"expectedNum" -> "2",
"actualNum" -> "3",
"docroot" -> SPARK_DOC_ROOT),
context = ExpectedContext("", "", 7, 28, "to_char('a', 'b', 'c')"))
}

test("to_varchar") {
Expand Down