Skip to content

[SPARK-52617][SQL]Cast TIME to/from TIMESTAMP_NTZ #51381

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 40 commits into
base: master
Choose a base branch
from

Conversation

SubhamSinghal
Copy link

@SubhamSinghal SubhamSinghal commented Jul 5, 2025

What changes were proposed in this pull request?

This PR adds support for casting TIME to/from TIMESTAMP_NTZ type

Why are the changes needed?

Adds casting support between 2 types.

Does this PR introduce any user-facing change?

yes

How was this patch tested?

By running the related test suites: CastSuiteBase

By manual tests:

timestamp_ntz to time conversion:

scala> spark.sql(
     |   """
     |     |SELECT CAST(CAST('2025-07-05 12:34:56.123456' AS TIMESTAMP_NTZ) AS TIME(6))
     |   """.stripMargin
     | ).show(false)

+------------------------------------------------------------------+
|CAST(CAST(2025-07-05 12:34:56.123456 AS TIMESTAMP_NTZ) AS TIME(6))|
+------------------------------------------------------------------+
|12:34:56.123456                                                   |
+------------------------------------------------------------------+

time to timestamp_ntz conversion:

scala> spark.sql("""
     |   SELECT CAST(CAST('12:34:56.123456' AS TIME(6)) AS TIMESTAMP_NTZ)
     | """).show(false)
+-------------------------------------------------------+
|CAST(CAST(12:34:56.123456 AS TIME(6)) AS TIMESTAMP_NTZ)|
+-------------------------------------------------------+
|2025-07-05 12:34:56.123456                             |
+-------------------------------------------------------+

logical plan analysis:

scala> spark.sql("SELECT CAST(make_time(12, 30, 40) AS TIMESTAMP_NTZ)").explain(true)

== Analyzed Logical Plan ==
make_timestamp_ntz(current_date(), make_time(12, 30, 40)): timestamp_ntz
Project [make_timestamp_ntz(current_date(Some(Asia/Kolkata)), make_time(12, 30, cast(40 as decimal(16,6)))) AS make_timestamp_ntz(current_date(), make_time(12, 30, 40))#0]
+- OneRowRelation

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

yes
Generated / corrected with AI assistance: chatGPT

@github-actions github-actions bot added the SQL label Jul 5, 2025
@SubhamSinghal SubhamSinghal changed the title [SPARK-52617][SQL]Cast TIME to/from TIMESTAMP_NTZ [WIP][SPARK-52617][SQL]Cast TIME to/from TIMESTAMP_NTZ Jul 5, 2025
@SubhamSinghal SubhamSinghal changed the title [WIP][SPARK-52617][SQL]Cast TIME to/from TIMESTAMP_NTZ [SPARK-52617][SQL]Cast TIME to/from TIMESTAMP_NTZ Jul 5, 2025
@SubhamSinghal SubhamSinghal force-pushed the SPARK-52617_cast_time_to/from_TIMESTAMP_NTZ branch from 4167bf1 to 828d214 Compare July 6, 2025 16:05
Copy link
Contributor

@mihailotim-db mihailotim-db left a comment

Choose a reason for hiding this comment

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

On a separate note, is the new rule necessary? Should we try and place this in existing type coercion rules?

@SubhamSinghal SubhamSinghal force-pushed the SPARK-52617_cast_time_to/from_TIMESTAMP_NTZ branch from fa1daa9 to fd1aef3 Compare July 8, 2025 07:42
@SubhamSinghal SubhamSinghal requested a review from MaxGekk July 9, 2025 11:25
@subham611
Copy link
Contributor

On a separate note, is the new rule necessary? Should we try and place this in existing type coercion rules?

@mihailotim-db Should I make RewriteTimeCastToTimestampNTZ extend TypeCoercionRule instead of Rule[LogicalPlan] and place RewriteTimeCastToTimestampNTZ inside CombinedTypeCoercionRule?

@SubhamSinghal SubhamSinghal force-pushed the SPARK-52617_cast_time_to/from_TIMESTAMP_NTZ branch from f7d1226 to fd5e76e Compare July 10, 2025 13:51
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.

4 participants