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 43 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
@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
@MaxGekk MaxGekk changed the title [SPARK-52617][SQL]Cast TIME to/from TIMESTAMP_NTZ [SPARK-52617][SQL] Cast TIME to/from TIMESTAMP_NTZ Jul 15, 2025
@MaxGekk
Copy link
Member

MaxGekk commented Jul 16, 2025

@SubhamSinghal Could you resolve conflicts, please.

Comment on lines +68 to +69
case Cast(child, TimestampNTZType, _, _) if child.dataType.isInstanceOf[TimeType] =>
MakeTimestampNTZ(CurrentDate(), child)
Copy link
Contributor

Choose a reason for hiding this comment

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

There is no need to have this if we have a type coercion rule. Type coercion rules run implicitly in single-pass

Copy link
Author

Choose a reason for hiding this comment

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

@mihailotim-db are you suggesting to revert all change in TimeZoneAwareExpressionResolver class and type coercion rule will take care of single pass and fixed point analyzer?

@@ -93,6 +93,7 @@ object AnsiTypeCoercion extends TypeCoercionBase {
StackCoercion ::
Division ::
IntegralDivision ::
RewriteTimeCastToTimestampNTZ ::
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this a separate rule? I think we should be able to make it part of one of the existing rule, maybe DateTimeOperations @MaxGekk ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually is this Cast coming from internal type coercion or user specified casts? If former, we should perform this change in the place where we add a cast. If latter, we should have an analysis rule for rewrite and not a type coercion one, since there is no coercion going on here

Copy link
Author

Choose a reason for hiding this comment

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

@MaxGekk Can you help here?

@SubhamSinghal SubhamSinghal force-pushed the SPARK-52617_cast_time_to/from_TIMESTAMP_NTZ branch from e15a217 to 68dc4a9 Compare July 16, 2025 10:34
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