Skip to content

[SPARK-51562][SQL] Add the time() function #50557

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

Conversation

senthh
Copy link
Contributor

@senthh senthh commented Apr 10, 2025

What changes were proposed in this pull request?

This PR adds new function time() which should cast an expr to TIME. This function is a synonym for CAST(expr AS TIME)

Examples

SELECT time('12:25:13.45');
12:25:13.45
SELECT time(timestamp'2020-04-30 12:25:13.45');
12:25:13.45
SELECT time(123);
00:02:03

Why are the changes needed?

This function is a synonym for CAST(expr AS TIME)

Does this PR introduce any user-facing change?

Yes

How was this patch tested?

By running the related test suites:

$ build/sbt "test:testOnly *ExpressionInfoSuite"
$ build/sbt "test:testOnly *TimeExpressionsSuite"
$ build/sbt "sql/testOnly org.apache.spark.sql.SQLQueryTestSuite -- -z time.sql"

and also with spark-shell

scala> spark.sql("select time(123) as col").show()
+--------+
|     col|
+--------+
|00:02:03|
+--------+


scala> spark.sql("select time(timestamp'2020-04-30 00:25:13.45') as col").show()
+-----------+
|        col|
+-----------+
|00:25:13.45|
+-----------+


scala> spark.sql("select time('13:33:13.45') as col").show()
+-----------+
|        col|
+-----------+
|13:33:13.45|
+-----------+

Was this patch authored or co-authored using generative AI tooling?

No

@github-actions github-actions bot added the SQL label Apr 10, 2025
@senthh
Copy link
Contributor Author

senthh commented Apr 10, 2025

@MaxGekk Could you please review this PR?

@@ -740,6 +744,28 @@ case class Cast(
} else {
buildCast[UTF8String](_, s => DateTimeUtils.stringToTime(s).orNull)
}

case TimestampType =>
Copy link
Member

Choose a reason for hiding this comment

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

The scope of this PR should be adding the time() function alias for CAST. Let's implement CAST and test it separately, please.

}

override protected def nullSafeEval(input: Any): Any = {
Cast(child, TimeType()).eval(null)
Copy link
Member

Choose a reason for hiding this comment

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

This is not right way of implementing a replacement. Please, take a look at

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))
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants