Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-51583] [SQL] Improve error message when to_timestamp function has arguments of wrong type #50347

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions common/utils/src/main/resources/error/error-conditions.json
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,11 @@
"class <className> not found."
]
},
"UNEXPECTED_FUNCTION_ARGUMENT_TYPE" : {
"message" : [
"Argument type must be a <requiredType>, but it's <argumentType>."
]
},
"UNEXPECTED_INPUT_TYPE" : {
"message" : [
"The <paramIndex> parameter requires the <requiredType> type, however <inputSql> has the type <inputType>."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,13 @@ trait CheckAnalysis extends LookupCatalog with QueryErrorsBase with PlanToString
e.invalidFormat(checkRes)
}

case parseToTimestamp: ParseToTimestamp if parseToTimestamp.left.dataType != StringType =>
Copy link
Member

Choose a reason for hiding this comment

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

ParseToTimestamp should support numeric, see #35887. cc @HyukjinKwon

And I am not sure that checking parameters of particular expression here in checkAnalysis is right approach. IMHO, ParseToTimestamp should care of them.

Copy link
Member

Choose a reason for hiding this comment

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

Can we do it in checkInputDataTypes or inputTypes in ParseToTimestamp?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I just wonder what's the semantics here: should we support NumericTypes here or no? cc @srielau
Per docs, it seems that we should support STRING type only.

Copy link
Member

Choose a reason for hiding this comment

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

I think the problem is that it is a breaking change .. I would leave it to @srielau tho.

throw QueryCompilationErrors.unexpectedFunctionArgumentDatatype(
expression = parseToTimestamp,
requiredType = StringType,
argumentType = parseToTimestamp.left.dataType
)

case c: Cast if !c.resolved =>
throw SparkException.internalError(
msg = s"Found the unresolved Cast: ${c.simpleString(SQLConf.get.maxToStringFields)}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4334,4 +4334,18 @@ private[sql] object QueryCompilationErrors extends QueryErrorsBase with Compilat
origin = origin
)
}

def unexpectedFunctionArgumentDatatype(
expression: Expression,
requiredType: DataType,
argumentType: DataType): Throwable = {
new AnalysisException(
errorClass = "DATATYPE_MISMATCH.UNEXPECTED_FUNCTION_ARGUMENT_TYPE",
messageParameters = Map(
"sqlExpr" -> toSQLExpr(expression),
"requiredType" -> toSQLType(requiredType),
"argumentType" -> toSQLType(argumentType)
)
)
}
}
31 changes: 31 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4941,6 +4941,37 @@ class SQLQuerySuite extends QueryTest with SharedSparkSession with AdaptiveSpark
Row(Array(0), Array(0)), Row(Array(1), Array(1)), Row(Array(2), Array(2)))
checkAnswer(df, expectedAnswer)
}

test("SPARK-51583: Function to_timestamp can't have non-string arguments") {
checkAnswer(
sql("SELECT to_timestamp('2016-12-31 00:12:00');"),
Row(Timestamp.valueOf("2016-12-31 00:12:00"))
)
checkAnswer(
sql("SELECT to_timestamp('2016-12-31', 'yyyy-MM-dd');"),
Row(Timestamp.valueOf("2016-12-31 00:00:00"))
)
checkError(
exception = intercept[AnalysisException](
sql("SELECT to_timestamp(1, 'yyyy-MM-dd HH:mm:ss')")
),
condition = "DATATYPE_MISMATCH.UNEXPECTED_FUNCTION_ARGUMENT_TYPE",
parameters = Map(
"sqlExpr" -> "\"to_timestamp(1, yyyy-MM-dd HH:mm:ss)\"",
"requiredType" -> "\"STRING\"",
"argumentType" -> "\"INT\"")
)
checkError(
exception = intercept[AnalysisException](
sql("SELECT to_timestamp(1.0, 'yyyy-MM-dd HH:mm:ss')")
),
condition = "DATATYPE_MISMATCH.UNEXPECTED_FUNCTION_ARGUMENT_TYPE",
parameters = Map(
"sqlExpr" -> "\"to_timestamp(1.0, yyyy-MM-dd HH:mm:ss)\"",
"requiredType" -> "\"STRING\"",
"argumentType" -> "\"DECIMAL(2,1)\"")
)
}
}

case class Foo(bar: Option[String])