Skip to content

Conversation

@MaxGekk
Copy link
Member

@MaxGekk MaxGekk commented Mar 15, 2025

What changes were proposed in this pull request?

In the PR, I propose to add new function to_time(). It casts a STRING input value to TIME using an optional formatting.

Syntax

to_time(expr[, fmt])

Arguments

Returns

A TIME(n) where n is always 6 in the proposed implementation.

Examples

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

> SELECT to_time('12.10.05', 'HH.mm.ss');
 12:10:05

Why are the changes needed?

  1. To improve user experience with Spark SQL, and allow to construct values of the new data type TIME from strings.
  2. To simplify migration from other systems where to_time is supported. For instance:
  3. For consistency: Spark SQL has already to_timestamp() for TIMESTAMP and to_date() for DATE.

Does this PR introduce any user-facing change?

No.

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"

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

No.

@github-actions github-actions bot added the SQL label Mar 15, 2025
@MaxGekk MaxGekk changed the title [WIP][SQL] Add the to_time function [WIP][SPARK-51456][SQL] Add the to_time function Mar 16, 2025
@MaxGekk MaxGekk changed the title [WIP][SPARK-51456][SQL] Add the to_time function [SPARK-51456][SQL] Add the to_time function Mar 17, 2025
@MaxGekk MaxGekk marked this pull request as ready for review March 17, 2025 04:44
@ExpressionDescription(
usage = """
_FUNC_(str[, format]) - Parses the `str` expression with the `format` expression to
a time. Returns null with invalid input. By default, it follows casting rules to a time if
Copy link
Member

Choose a reason for hiding this comment

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

Returns null with invalid input

Need this behavior to follow [try_]to_timestamp?

Copy link
Member Author

Choose a reason for hiding this comment

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

I will re-formulate it something like If format is malformed or its application does not result in a well formed time, the function raises an error.


override lazy val replacement: Expression = format match {
case None => invokeParser()
case Some(expr) if expr.foldable => invokeParser(Some(expr.eval().toString), Seq(str))
Copy link
Member

Choose a reason for hiding this comment

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

Will it be possible to hit NPE? e.g. Literal.create(null).eval().toString.

Copy link
Member Author

Choose a reason for hiding this comment

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

Let me check and write a test.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed and added new checks for nulls.

Copy link
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 .

I guess we had better a final sign-off from @yaooqinn . I'll leave this to you and him.

Copy link
Member

@yaooqinn yaooqinn left a comment

Choose a reason for hiding this comment

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

LGTM

@yaooqinn yaooqinn closed this in 76a4239 Mar 18, 2025
@yaooqinn
Copy link
Member

Thank you @MaxGekk @dongjoon-hyun, merged to master

baibaichen added a commit to baibaichen/gluten that referenced this pull request Jan 12, 2026
baibaichen added a commit to baibaichen/gluten that referenced this pull request Jan 13, 2026
baibaichen added a commit to baibaichen/gluten that referenced this pull request Jan 13, 2026
baibaichen added a commit to baibaichen/gluten that referenced this pull request Jan 13, 2026
baibaichen added a commit to baibaichen/gluten that referenced this pull request Jan 13, 2026
baibaichen added a commit to apache/incubator-gluten that referenced this pull request Jan 13, 2026
* Move GlutenStreamingQuerySuite to correct package

* Add Spark 4.1 new test suites for Gluten

* Enable new and existing Gluten test suites for Spark 4.1 UT

* Update workflow trigger paths to exclude Spark 4.0 and 4.1 shims directories for clickhouse backend

* Add support for Spark 4.1 in build script

* Merge Spark 4.1.0 sql-tests into Gluten Spark 4.1 (three-way merge)

Three-way merge performed using Git:
- Base: Spark 4.0.1 (29434ea766b)
- Left: Spark 4.1.0 (e221b56be7b)
- Right: Gluten Spark 4.1 backends-velox

Summary:
- Auto-merged: 165 files
- New tests added: 31 files (collations, edge cases, recursion, spatial, etc.)
- Modified tests: 134 files
- Deleted tests: 2 files (collations.sql -> split into 4 files, timestamp-ntz.sql)

Conflicts resolved:
- inputs/timestamp-ntz.sql: Right deleted + Left modified -> DELETED (per resolution rule)

New test suites from Spark 4.1.0:
- Collations (4 files): aliases, basic, padding-trim, string-functions
- Edge cases (6 files): alias-resolution, extract-value, join-resolution, etc.
- Advanced features: cte-recursion, generators, kllquantiles, thetasketch, time
- Name resolution: order-by-alias, session-variable-precedence, runtime-replaceable
- Spatial functions: st-functions (ANSI and non-ANSI variants)
- Various resolution edge cases

Total files after merge: 671 (up from 613)

* Enable additional Spark 4.1 SQL tests by resolving TODOs

* Add new Spark 4.1 test files to VeloxSQLQueryTestSettings

* [Fix] Replace `RuntimeReplaceable` with its `replacement` to fix UT.

see apache/spark#50287

* [4.1.0] Exclude "infer shredding with mixed scale"

see apache/spark#52406

* [Fix] Implement Kryo serialization for CachedColumnarBatch

see apache/spark#50599

* [4.1.0] Exclude GlutenMapStatusEndToEndSuite and configure parallelism

see apache/spark#50230

* [4.1.0] Exclude Spark Structure Steaming tests in Gluten

see
 - apache/spark#52473
 - apache/spark#52870
 - apache/spark#52891

* [4.1.0] Exclude failing SQL tests on Spark 4.1

* Replace SparkException.require with standard require in ColumnarCachedBatchSerializer to work across different spark versions

* [Fix] Replace `RuntimeReplaceable` with its `replacement` to fix UT.

see apache/spark#50287

* Exclude Spark 4.0 and 4.1 paths in clickhouse_be_trigger using `!` prefix

* [Fix] Update GlutenShowNamespacesParserSuite to use GlutenSQLTestsBaseTrait
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.

3 participants