Skip to content

[SPARK-51415][SQL] Make timestamp from date and time #51179

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

Conversation

MaxGekk
Copy link
Member

@MaxGekk MaxGekk commented Jun 14, 2025

What changes were proposed in this pull request?

In the PR, I propose to extend the make_timestamp function, and accept a date and time field + optional time zone.

Syntax

make_timestamp(date[, time[, timezone]])

Arguments

  • date: A date expression
  • time: A time expression

Returns

A TIMESTAMP.

Examples

> SELECT make_timestamp(DATE'2014-12-28', TIME'6:30:45.887');
 2014-12-28 06:30:45.887

Why are the changes needed?

Users will be able to create a timestamp by combining a time and a date.

Does this PR introduce any user-facing change?

No, it just extends the existing API.

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 timestamp.sql"

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

No.

@github-actions github-actions bot added the SQL label Jun 14, 2025
@MaxGekk MaxGekk changed the title [WIP][SQL] Make timestamp from date and time [WIP][SPARK-51415][SQL] Make timestamp from date and time Jun 14, 2025
@@ -2746,7 +2746,11 @@ object TryMakeTimestampLTZExpressionBuilder extends ExpressionBuilder {

// scalastyle:off line.size.limit
@ExpressionDescription(
usage = "_FUNC_(year, month, day, hour, min, sec[, timezone]) - Create timestamp from year, month, day, hour, min, sec and timezone fields. The result data type is consistent with the value of configuration `spark.sql.timestampType`. If the configuration `spark.sql.ansi.enabled` is false, the function returns NULL on invalid inputs. Otherwise, it will throw an error instead.",
usage = """
_FUNC_(year, month, day, hour, min, sec[, timezone]) - Create timestamp from year, month, day, hour, min, sec and timezone fields. The result data type is consistent with the value of configuration `spark.sql.timestampType`. If the configuration `spark.sql.ansi.enabled` is false, the function returns NULL on invalid inputs. Otherwise, it will throw an error instead.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Should we move the output type and error out comments after the second declaration of the function?

Copy link
Member Author

Choose a reason for hiding this comment

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

SGTM

@MaxGekk MaxGekk changed the title [WIP][SPARK-51415][SQL] Make timestamp from date and time [SPARK-51415][SQL] Make timestamp from date and time Jun 17, 2025
@MaxGekk MaxGekk marked this pull request as ready for review June 17, 2025 12:25
@MaxGekk
Copy link
Member Author

MaxGekk commented Jun 17, 2025

@srielau @uros-db @mihailom-db Please, review the PR.

@srielau
Copy link
Contributor

srielau commented Jun 17, 2025

What is the exact type? Since both DATE and TIME are WITHOUT TIMEZONE , presumably the result should be without timezone.
Do we have a separate make_timestamp_ntz() version?

-- !query
SELECT make_timestamp(DATE'0001-01-01', TIME'0:0:0')
-- !query analysis
[Analyzer test output redacted due to nondeterminism]
Copy link
Contributor

Choose a reason for hiding this comment

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

What's exactly non-deterministic here?

Copy link
Member Author

@MaxGekk MaxGekk Jun 18, 2025

Choose a reason for hiding this comment

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

Not clear to me too. I have leaved a question in the PR: https://github.com/apache/spark/pull/40496/files#r2154235184

Copy link
Contributor

Choose a reason for hiding this comment

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

I would assume that we might call current_timestamp or similar functions that could produce different literals. But if this is the case, I would say this is the bug in the new system where we do not filter on the specific expressions, but on literals in general. cc: @dtenedor on this PR, for context

@@ -169,3 +169,8 @@ select timediff(SECOND, date'2022-02-15', timestamp'2022-02-14 23:59:59');

select timediff('MINUTE', timestamp'2023-02-14 01:02:03', timestamp'2023-02-14 02:00:03');
select timediff('YEAR', date'2020-02-15', date'2023-02-15');

-- Construct timestamp from date and time
Copy link
Contributor

Choose a reason for hiding this comment

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

How do we handle null values? Can we add tests for this?

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 added tests. Thanks.

@mihailom-db
Copy link
Contributor

@srielau We do have make_timestamp_ntz(). This is the base function which takes timezone from the config or from the input. When it comes to date/time not having zone, I would say neither do hour, minute, second, day, year, month from which the original make_timestamp was done. Should we maybe just improve the docs around this?

Copy link
Contributor

@mihailom-db mihailom-db left a comment

Choose a reason for hiding this comment

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

LGTM, apart from few comments from @uros-db

@MaxGekk
Copy link
Member Author

MaxGekk commented Jun 18, 2025

presumably the result should be without timezone

It depends on the config as the function doc says: "The result data type is consistent with the value of configuration spark.sql.timestampType.". If the result type is TIMESTAMP_NTZ, return local timestamp otherwise non-local using provided time zone or from the SQL config.

New behaviour of two parameters date and time is consistent to existing one with the year, month, ... seconds.

-- !query analysis
org.apache.spark.sql.AnalysisException
{
"errorClass" : "FAILED_FUNCTION_CALL",
Copy link
Member Author

Choose a reason for hiding this comment

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

Seems like we don't properly handle NULLs in time expressions, at least by HoursOfTime. I will recheck this.

Copy link
Contributor

@uros-db uros-db left a comment

Choose a reason for hiding this comment

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

Thanks for addressing the comments Max!

StaticInvoke(
classOf[DateTimeUtils.type],
DecimalType(8, 6),
"getSecondsOfTimeWithFraction",
Seq(child, Literal(precision)),
Seq(child.dataType, IntegerType))
}
private val precision: Int = child.dataType.asInstanceOf[TimeType].precision
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 made the fix here in the PR because the bug is triggered when the expression is invoked from MakeTimestamp with NullType directly (without checking inputTypes).

@MaxGekk MaxGekk requested a review from cloud-fan June 18, 2025 20:09
@MaxGekk
Copy link
Member Author

MaxGekk commented Jun 19, 2025

@LuciferYang @yaooqinn @dongjoon-hyun Could you review this PR, please.

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