Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions datafusion/expr/src/built_in_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,7 @@ impl BuiltinScalarFunction {
1,
vec![
Int64,
Float64,
Timestamp(Nanosecond, None),
Timestamp(Microsecond, None),
Timestamp(Millisecond, None),
Expand Down
18 changes: 18 additions & 0 deletions datafusion/physical-expr/src/datetime_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,24 @@ pub fn to_timestamp_invoke(args: &[ColumnarValue]) -> Result<ColumnarValue> {
DataType::Int64 => {
cast_column(&args[0], &DataType::Timestamp(TimeUnit::Second, None), None)
}
DataType::Float64 => {
if let ColumnarValue::Scalar(ScalarValue::Float64(Some(float_ts))) = &args[0]
{
cast_column(
&ColumnarValue::Scalar(ScalarValue::Int64(Some(
(float_ts * 1_000_000_000_f64).trunc() as i64,
))),
&DataType::Timestamp(TimeUnit::Nanosecond, None),
None,
)
} else {
cast_column(
&args[0],
&DataType::Timestamp(TimeUnit::Nanosecond, None),
None,
)
}
}
DataType::Timestamp(_, None) => cast_column(
&args[0],
&DataType::Timestamp(TimeUnit::Nanosecond, None),
Expand Down
29 changes: 29 additions & 0 deletions datafusion/sqllogictest/test_files/timestamps.slt
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,35 @@ SELECT COUNT(*) FROM ts_data_secs where ts > to_timestamp_seconds('2020-09-08T12
----
2


# to_timestamp float inputs

query P
SELECT to_timestamp(1.1);
----
1970-01-01T00:00:01.100

query P
SELECT to_timestamp(-1.1);
----
1969-12-31T23:59:58.900

query P
SELECT to_timestamp(0.0);
----
1970-01-01T00:00:00

query P
SELECT to_timestamp(1.23456789);
----
1970-01-01T00:00:01.234567890

query P
SELECT to_timestamp(123456789.123456789);
----
1973-11-29T21:33:09.123456784


# from_unixtime

# 1599566400 is '2020-09-08T12:00:00+00:00'
Expand Down
4 changes: 2 additions & 2 deletions docs/source/user-guide/sql/scalar_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1392,9 +1392,9 @@ extract(field FROM source)
### `to_timestamp`

Converts a value to a timestamp (`YYYY-MM-DDT00:00:00Z`).
Supports strings, integer, and unsigned integer types as input.
Supports strings, integer, unsigned integer, and double types as input.
Strings are parsed as RFC3339 (e.g. '2023-07-20T05:44:00')
Integers and unsigned integers are interpreted as seconds since the unix epoch (`1970-01-01T00:00:00Z`)
Integers, unsigned integers, and doubles are interpreted as seconds since the unix epoch (`1970-01-01T00:00:00Z`)
return the corresponding timestamp.

```
Expand Down