Skip to content

[SPARK-51556][SQL] Add the try_to_time function#50335

Closed
MaxGekk wants to merge 3 commits intoapache:masterfrom
MaxGekk:try_to_time
Closed

[SPARK-51556][SQL] Add the try_to_time function#50335
MaxGekk wants to merge 3 commits intoapache:masterfrom
MaxGekk:try_to_time

Conversation

@MaxGekk
Copy link
Copy Markdown
Member

@MaxGekk MaxGekk commented Mar 20, 2025

What changes were proposed in this pull request?

In the PR, I propose new SQL function try_to_time which is similar to to_time but returns NULL instead of raising an error in the cases of malformed or non-matched to the given pattern input strings.

Syntax

try_to_time(str[, format])

Arguments

  • str: A STRING expression representing a time.
  • format: An optional format STRING expression.

Returns

A TIME.

  • If format is supplied, it must conform with Datetime patterns.
  • If format is not supplied, the function is a synonym for cast(expr AS TIME).
  • If format is malformed or its application does not result in a well formed time, the function returns NULL.

To return an error instead of NULL in case of a malformed str use to_time.

Examples

> SELECT try_to_time('00:12:00');
 00:12:00

> SELECT try_to_time("02-69", "HH-mm");
 NULL

Why are the changes needed?

To improve user experience with Spark SQL. If an user needs to process wrongly formatted time values in a string format, and ignore errors, she/he can use try_to_time instead of to_time.

Does this PR introduce any user-facing change?

Yes, it extends Spark SQL by new function.

How was this patch tested?

By running the affected test suites:

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

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

No.

@MaxGekk MaxGekk changed the title [WIP][SQL] Add the try_to_time function [WIP][SPARK-51556][SQL] Add the try_to_time function Mar 20, 2025
@MaxGekk MaxGekk changed the title [WIP][SPARK-51556][SQL] Add the try_to_time function [SPARK-51556][SQL] Add the try_to_time function Mar 20, 2025
@MaxGekk MaxGekk marked this pull request as ready for review March 20, 2025 10:15
@the-sakthi
Copy link
Copy Markdown
Member

LGTM @MaxGekk !

Copy link
Copy Markdown
Member

@dongjoon-hyun dongjoon-hyun left a comment

Choose a reason for hiding this comment

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

+1, LGTM. Thank you, @MaxGekk .

@MaxGekk
Copy link
Copy Markdown
Member Author

MaxGekk commented Mar 21, 2025

Merging to master. Thank you, @the-sakthi @dongjoon-hyun @yaooqinn for review.

@MaxGekk MaxGekk closed this in 2822526 Mar 21, 2025
@LuciferYang
Copy link
Copy Markdown
Contributor

late LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants