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

[WIP][SPARK-51423][SQL] Add the current_time() function for TIME datatype #50336

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

the-sakthi
Copy link
Member

What changes were proposed in this pull request?

This PR adds support for a new function current_time() which returns the current time at the start of query evaluation.

scala> spark.sql("SELECT current_time(0);").show()
+--------------+
|current_time()|
+--------------+
|      04:48:47|
+--------------+


scala> spark.sql("SELECT current_time();").show()
+---------------+
| current_time()|
+---------------+
|04:48:51.609168|
+---------------+


scala> spark.sql("SELECT current_time(3);").show()
+--------------+
|current_time()|
+--------------+
|  04:48:54.846|
+--------------+


scala> spark.sql("SELECT current_time(6);").show()
+---------------+
| current_time()|
+---------------+
|04:48:59.222348|
+---------------+

scala> spark.sql("SELECT current_time(7);").show()
org.apache.spark.sql.catalyst.ExtendedAnalysisException: [DATATYPE_MISMATCH.TYPE_CHECK_FAILURE_WITH_HINT] Cannot resolve "current_time()" due to data type mismatch: Invalid precision 7. Must be between 0 and 6.. SQLSTATE: 42K09; line 1 pos 7;
'Project [unresolvedalias(current_time(7))]
+- OneRowRelation

  at org.apache.spark.sql.catalyst.analysis.package$AnalysisErrorAt.failAnalysis(package.scala:52)
  ...

Why are the changes needed?

Adds a built-in current_time([n]) function returning just the time portion (in a TIME(n) type). This aligns Spark with other SQL systems offering a native time function, improves convenience for time-only queries, and complements existing functions like current_date and current_timestamp.

Does this PR introduce any user-facing change?

Yes, adds a new function. Users can now get the current time using this function.

How was this patch tested?

Manual testing.

Pending

  • Support for current_time [i.e. without any braces]
  • Implementation of codegen (currently extending CodegenFallback)
  • Addition of UTs.
  • Use inbuilt functions for computeMicrosAtPrecision

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

No

@github-actions github-actions bot added the SQL label Mar 20, 2025
Copy link
Member

@MaxGekk MaxGekk left a comment

Choose a reason for hiding this comment

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

... which returns the current time at the start of query evaluation.

I think the current implementation doesn't satisfy the requirement, and every call in a query returns new current time. Could you look at the CurrentTimestampLike, and the rule:

/**
* Computes the current date and time to make sure we return the same result in a single query.
*/
object ComputeCurrentTime extends Rule[LogicalPlan] {

@the-sakthi
Copy link
Member Author

Ahh sure! Thanks for the early review, Max. Will refer CurrentTimestampLike to better cater to the requirement.

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