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
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2746,7 +2746,13 @@ 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.
_FUNC_(date, time[, timezone]) - Create timestamp from date, time and time zone.
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.
""",
arguments = """
Arguments:
* year - the year to represent, from 1 to 9999
Expand All @@ -2758,6 +2764,8 @@ object TryMakeTimestampLTZExpressionBuilder extends ExpressionBuilder {
The value can be either an integer like 13 , or a fraction like 13.123.
If the sec argument equals to 60, the seconds field is set
to 0 and 1 minute is added to the final timestamp.
* date - a date to represent, from 0001-01-01 to 9999-12-31
* time - a local time to represent, from 00:00:00 to 23:59:59.999999
* timezone - the time zone identifier. For example, CET, UTC and etc.
""",
examples = """
Expand All @@ -2772,6 +2780,10 @@ object TryMakeTimestampLTZExpressionBuilder extends ExpressionBuilder {
2019-06-30 23:59:01
> SELECT _FUNC_(null, 7, 22, 15, 30, 0);
NULL
> SELECT _FUNC_(make_date(2014, 12, 28), make_time(6, 30, 45.887));
2014-12-28 06:30:45.887
> SELECT _FUNC_(DATE'2014-12-28', TIME'6:30:45.887', 'CET');
2014-12-27 21:30:45.887
""",
group = "datetime_funcs",
since = "3.0.0")
Expand Down Expand Up @@ -2813,6 +2825,17 @@ case class MakeTimestamp(
SQLConf.get.timestampType)
}

def this(date: Expression, time: Expression) = {
this(Year(date), Month(date), DayOfMonth(date),
HoursOfTime(time), MinutesOfTime(time), SecondsOfTimeWithFraction(time))
}

def this(date: Expression, time: Expression, timezone: Expression) = {
this(Year(date), Month(date), DayOfMonth(date),
HoursOfTime(time), MinutesOfTime(time), SecondsOfTimeWithFraction(time),
timezone)
}

override def children: Seq[Expression] = Seq(year, month, day, hour, min, sec) ++ timezone
// Accept `sec` as DecimalType to avoid loosing precision of microseconds while converting
// them to the fractional part of `sec`. For accepts IntegerType as `sec` and integer can be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1899,3 +1899,54 @@ org.apache.spark.sql.catalyst.parser.ParseException
"fragment" : "timediff('YEAR', date'2020-02-15', date'2023-02-15')"
} ]
}


-- !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



-- !query
SELECT make_timestamp(DATE'2025-06-14', TIME'17:25:30.5', 'CET')
-- !query analysis
[Analyzer test output redacted due to nondeterminism]


-- !query
SELECT make_timestamp(DATE'9999-12-31', TIME'23:59:59.123456')
-- !query analysis
[Analyzer test output redacted due to nondeterminism]


-- !query
SELECT make_timestamp(NULL, TIME'0:0:0.000001')
-- !query analysis
Project [make_timestamp(year(cast(null as date)), month(cast(null as date)), dayofmonth(cast(null as date)), hour(00:00:00.000001), minute(00:00:00.000001), cast(secondsoftimewithfraction(00:00:00.000001) as decimal(16,6)), None, Some(America/Los_Angeles), true, TimestampType) AS make_timestamp(year(NULL), month(NULL), dayofmonth(NULL), hour(TIME '00:00:00.000001'), minute(TIME '00:00:00.000001'), secondsoftimewithfraction(TIME '00:00:00.000001'))#x]
+- OneRowRelation


-- !query
SELECT make_timestamp(DATE'1970-01-01', NULL)
-- !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.

"sqlState" : "38000",
"messageParameters" : {
"funcName" : "`make_timestamp`"
},
"queryContext" : [ {
"objectType" : "",
"objectName" : "",
"startIndex" : 8,
"stopIndex" : 45,
"fragment" : "make_timestamp(DATE'1970-01-01', NULL)"
} ]
}


-- !query
SELECT make_timestamp(DATE'1970-01-01', TIME'12:00:00', NULL)
-- !query analysis
[Analyzer test output redacted due to nondeterminism]
Original file line number Diff line number Diff line change
Expand Up @@ -1015,3 +1015,54 @@ org.apache.spark.sql.catalyst.parser.ParseException
"fragment" : "timediff('YEAR', date'2020-02-15', date'2023-02-15')"
} ]
}


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


-- !query
SELECT make_timestamp(DATE'2025-06-14', TIME'17:25:30.5', 'CET')
-- !query analysis
[Analyzer test output redacted due to nondeterminism]


-- !query
SELECT make_timestamp(DATE'9999-12-31', TIME'23:59:59.123456')
-- !query analysis
[Analyzer test output redacted due to nondeterminism]


-- !query
SELECT make_timestamp(NULL, TIME'0:0:0.000001')
-- !query analysis
Project [make_timestamp(year(cast(null as date)), month(cast(null as date)), dayofmonth(cast(null as date)), hour(00:00:00.000001), minute(00:00:00.000001), cast(secondsoftimewithfraction(00:00:00.000001) as decimal(16,6)), None, Some(America/Los_Angeles), false, TimestampType) AS make_timestamp(year(NULL), month(NULL), dayofmonth(NULL), hour(TIME '00:00:00.000001'), minute(TIME '00:00:00.000001'), secondsoftimewithfraction(TIME '00:00:00.000001'))#x]
+- OneRowRelation


-- !query
SELECT make_timestamp(DATE'1970-01-01', NULL)
-- !query analysis
org.apache.spark.sql.AnalysisException
{
"errorClass" : "FAILED_FUNCTION_CALL",
"sqlState" : "38000",
"messageParameters" : {
"funcName" : "`make_timestamp`"
},
"queryContext" : [ {
"objectType" : "",
"objectName" : "",
"startIndex" : 8,
"stopIndex" : 45,
"fragment" : "make_timestamp(DATE'1970-01-01', NULL)"
} ]
}


-- !query
SELECT make_timestamp(DATE'1970-01-01', TIME'12:00:00', NULL)
-- !query analysis
[Analyzer test output redacted due to nondeterminism]
Original file line number Diff line number Diff line change
Expand Up @@ -943,3 +943,54 @@ org.apache.spark.sql.catalyst.parser.ParseException
"fragment" : "timediff('YEAR', date'2020-02-15', date'2023-02-15')"
} ]
}


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


-- !query
SELECT make_timestamp(DATE'2025-06-14', TIME'17:25:30.5', 'CET')
-- !query analysis
[Analyzer test output redacted due to nondeterminism]


-- !query
SELECT make_timestamp(DATE'9999-12-31', TIME'23:59:59.123456')
-- !query analysis
[Analyzer test output redacted due to nondeterminism]


-- !query
SELECT make_timestamp(NULL, TIME'0:0:0.000001')
-- !query analysis
Project [make_timestamp(year(cast(null as date)), month(cast(null as date)), dayofmonth(cast(null as date)), hour(00:00:00.000001), minute(00:00:00.000001), cast(secondsoftimewithfraction(00:00:00.000001) as decimal(16,6)), None, Some(America/Los_Angeles), true, TimestampType) AS make_timestamp(year(NULL), month(NULL), dayofmonth(NULL), hour(TIME '00:00:00.000001'), minute(TIME '00:00:00.000001'), secondsoftimewithfraction(TIME '00:00:00.000001'))#x]
+- OneRowRelation


-- !query
SELECT make_timestamp(DATE'1970-01-01', NULL)
-- !query analysis
org.apache.spark.sql.AnalysisException
{
"errorClass" : "FAILED_FUNCTION_CALL",
"sqlState" : "38000",
"messageParameters" : {
"funcName" : "`make_timestamp`"
},
"queryContext" : [ {
"objectType" : "",
"objectName" : "",
"startIndex" : 8,
"stopIndex" : 45,
"fragment" : "make_timestamp(DATE'1970-01-01', NULL)"
} ]
}


-- !query
SELECT make_timestamp(DATE'1970-01-01', TIME'12:00:00', NULL)
-- !query analysis
[Analyzer test output redacted due to nondeterminism]
Original file line number Diff line number Diff line change
Expand Up @@ -964,3 +964,54 @@ org.apache.spark.sql.catalyst.parser.ParseException
"fragment" : "timediff('YEAR', date'2020-02-15', date'2023-02-15')"
} ]
}


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


-- !query
SELECT make_timestamp(DATE'2025-06-14', TIME'17:25:30.5', 'CET')
-- !query analysis
[Analyzer test output redacted due to nondeterminism]


-- !query
SELECT make_timestamp(DATE'9999-12-31', TIME'23:59:59.123456')
-- !query analysis
[Analyzer test output redacted due to nondeterminism]


-- !query
SELECT make_timestamp(NULL, TIME'0:0:0.000001')
-- !query analysis
Project [make_timestamp(year(cast(null as date)), month(cast(null as date)), dayofmonth(cast(null as date)), hour(00:00:00.000001), minute(00:00:00.000001), cast(secondsoftimewithfraction(00:00:00.000001) as decimal(16,6)), None, Some(America/Los_Angeles), true, TimestampNTZType) AS make_timestamp(year(NULL), month(NULL), dayofmonth(NULL), hour(TIME '00:00:00.000001'), minute(TIME '00:00:00.000001'), secondsoftimewithfraction(TIME '00:00:00.000001'))#x]
+- OneRowRelation


-- !query
SELECT make_timestamp(DATE'1970-01-01', NULL)
-- !query analysis
org.apache.spark.sql.AnalysisException
{
"errorClass" : "FAILED_FUNCTION_CALL",
"sqlState" : "38000",
"messageParameters" : {
"funcName" : "`make_timestamp`"
},
"queryContext" : [ {
"objectType" : "",
"objectName" : "",
"startIndex" : 8,
"stopIndex" : 45,
"fragment" : "make_timestamp(DATE'1970-01-01', NULL)"
} ]
}


-- !query
SELECT make_timestamp(DATE'1970-01-01', TIME'12:00:00', NULL)
-- !query analysis
[Analyzer test output redacted due to nondeterminism]
Original file line number Diff line number Diff line change
Expand Up @@ -1024,3 +1024,54 @@ org.apache.spark.sql.catalyst.parser.ParseException
"fragment" : "timediff('YEAR', date'2020-02-15', date'2023-02-15')"
} ]
}


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


-- !query
SELECT make_timestamp(DATE'2025-06-14', TIME'17:25:30.5', 'CET')
-- !query analysis
[Analyzer test output redacted due to nondeterminism]


-- !query
SELECT make_timestamp(DATE'9999-12-31', TIME'23:59:59.123456')
-- !query analysis
[Analyzer test output redacted due to nondeterminism]


-- !query
SELECT make_timestamp(NULL, TIME'0:0:0.000001')
-- !query analysis
Project [make_timestamp(year(cast(null as date)), month(cast(null as date)), dayofmonth(cast(null as date)), hour(00:00:00.000001), minute(00:00:00.000001), cast(secondsoftimewithfraction(00:00:00.000001) as decimal(16,6)), None, Some(America/Los_Angeles), false, TimestampNTZType) AS make_timestamp(year(NULL), month(NULL), dayofmonth(NULL), hour(TIME '00:00:00.000001'), minute(TIME '00:00:00.000001'), secondsoftimewithfraction(TIME '00:00:00.000001'))#x]
+- OneRowRelation


-- !query
SELECT make_timestamp(DATE'1970-01-01', NULL)
-- !query analysis
org.apache.spark.sql.AnalysisException
{
"errorClass" : "FAILED_FUNCTION_CALL",
"sqlState" : "38000",
"messageParameters" : {
"funcName" : "`make_timestamp`"
},
"queryContext" : [ {
"objectType" : "",
"objectName" : "",
"startIndex" : 8,
"stopIndex" : 45,
"fragment" : "make_timestamp(DATE'1970-01-01', NULL)"
} ]
}


-- !query
SELECT make_timestamp(DATE'1970-01-01', TIME'12:00:00', NULL)
-- !query analysis
[Analyzer test output redacted due to nondeterminism]
8 changes: 8 additions & 0 deletions sql/core/src/test/resources/sql-tests/inputs/timestamp.sql
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,11 @@ 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.

SELECT make_timestamp(DATE'0001-01-01', TIME'0:0:0');
SELECT make_timestamp(DATE'2025-06-14', TIME'17:25:30.5', 'CET');
SELECT make_timestamp(DATE'9999-12-31', TIME'23:59:59.123456');
SELECT make_timestamp(NULL, TIME'0:0:0.000001');
SELECT make_timestamp(DATE'1970-01-01', NULL);
SELECT make_timestamp(DATE'1970-01-01', TIME'12:00:00', NULL);
Loading