Skip to content

[SPARK-52867][SQL] Remove redundant GetTimestamp #51556

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

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
Original file line number Diff line number Diff line change
Expand Up @@ -2110,11 +2110,12 @@ case class ParseToDate(
extends RuntimeReplaceable with ImplicitCastInputTypes with TimeZoneAwareExpression {

override lazy val replacement: Expression = withOrigin(origin) {
format.map { f =>
Cast(GetTimestamp(left, f, TimestampType, "try_to_date", timeZoneId, ansiEnabled), DateType,
timeZoneId, EvalMode.fromBoolean(ansiEnabled))
}.getOrElse(Cast(left, DateType, timeZoneId,
EvalMode.fromBoolean(ansiEnabled))) // backwards compatibility
if (left.dataType == TimestampType || format.isEmpty) {
Cast(left, DateType, timeZoneId, EvalMode.fromBoolean(ansiEnabled))
} else {
Cast(GetTimestamp(left, format.get, TimestampType, "try_to_date", timeZoneId, ansiEnabled),
DateType, timeZoneId, EvalMode.fromBoolean(ansiEnabled))
}
}

def this(left: Expression, format: Expression) = {
Expand Down Expand Up @@ -2191,9 +2192,13 @@ case class ParseToTimestamp(
extends RuntimeReplaceable with ImplicitCastInputTypes with TimeZoneAwareExpression {

override lazy val replacement: Expression = withOrigin(origin) {
format.map { f =>
GetTimestamp(left, f, dataType, "try_to_timestamp", timeZoneId, failOnError = failOnError)
}.getOrElse(Cast(left, dataType, timeZoneId, ansiEnabled = failOnError))
if (left.dataType == dataType) {
left
} else {
format.map { f =>
GetTimestamp(left, f, dataType, "try_to_timestamp", timeZoneId, failOnError = failOnError)
}.getOrElse(Cast(left, dataType, timeZoneId, ansiEnabled = failOnError))
}
}

def this(left: Expression, format: Expression) = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,16 @@ class DateFunctionsSuite extends QueryTest with SharedSparkSession {
assert(e.getCause.isInstanceOf[IllegalArgumentException])
assert(e.getMessage.contains("You may get a different result due to the upgrading to Spark"))

// Format is invalid when input is not StringType.
checkAnswer(
df.select(to_date(col("t"), "yyyy-MM")),
Seq(Row(Date.valueOf("2015-07-22")), Row(Date.valueOf("2014-12-31")),
Row(Date.valueOf("2014-12-31"))))
checkAnswer(
df.select(to_date(col("d"), "yyyy-MM")),
Seq(Row(Date.valueOf("2015-07-22")), Row(Date.valueOf("2015-07-01")),
Row(Date.valueOf("2014-12-31"))))

// February
val x1 = "2016-02-29"
val x2 = "2017-02-29"
Expand Down Expand Up @@ -1014,6 +1024,10 @@ class DateFunctionsSuite extends QueryTest with SharedSparkSession {
Row(ts1), Row(ts2)))
checkAnswer(df.select(to_timestamp(col("d"), "yyyy-MM-dd")), Seq(
Row(ts_date1), Row(ts_date2)))

// Format is invalid when input is not StringType.
checkAnswer(df.select(to_timestamp(col("ts"), "yyyy-MM-dd")), Seq(
Row(ts1), Row(ts2)))
}
}
}
Expand Down