Skip to content

Commit 83c9c5e

Browse files
committed
restore to_timestamp(double) behavior
the function was not meant to be changed
1 parent 838eeda commit 83c9c5e

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

datafusion/functions/src/datetime/to_timestamp.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ use std::any::Any;
1919
use std::sync::Arc;
2020

2121
use crate::datetime::common::*;
22+
use arrow::array::Float64Array;
2223
use arrow::datatypes::DataType::*;
2324
use arrow::datatypes::TimeUnit::{Microsecond, Millisecond, Nanosecond, Second};
2425
use arrow::datatypes::{
2526
ArrowTimestampType, DataType, TimeUnit, TimestampMicrosecondType,
2627
TimestampMillisecondType, TimestampNanosecondType, TimestampSecondType,
2728
};
29+
use datafusion_common::format::DEFAULT_CAST_OPTIONS;
2830
use datafusion_common::{exec_err, Result, ScalarType, ScalarValue};
2931
use datafusion_expr::{
3032
ColumnarValue, Documentation, ScalarUDFImpl, Signature, Volatility,
@@ -319,9 +321,20 @@ impl ScalarUDFImpl for ToTimestampFunc {
319321
Int32 | Int64 => args[0]
320322
.cast_to(&Timestamp(Second, None), None)?
321323
.cast_to(&Timestamp(Nanosecond, None), None),
322-
Null | Float64 | Timestamp(_, None) => {
324+
Null | Timestamp(_, None) => {
323325
args[0].cast_to(&Timestamp(Nanosecond, None), None)
324326
}
327+
Float64 => {
328+
let rescaled = arrow::compute::kernels::numeric::mul(
329+
&args[0].to_array(1)?,
330+
&arrow::array::Scalar::new(Float64Array::from(vec![1_000_000_000f64])),
331+
)?;
332+
Ok(ColumnarValue::Array(arrow::compute::cast_with_options(
333+
&rescaled,
334+
&Timestamp(Nanosecond, None),
335+
&DEFAULT_CAST_OPTIONS,
336+
)?))
337+
}
325338
Timestamp(_, Some(tz)) => {
326339
args[0].cast_to(&Timestamp(Nanosecond, Some(tz)), None)
327340
}

datafusion/sqllogictest/test_files/timestamps.slt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,12 +503,12 @@ SELECT COUNT(*) FROM ts_data_secs where ts > to_timestamp_seconds('2020-09-08 12
503503
query PPP
504504
SELECT to_timestamp(1.1) as c1, cast(1.1 as timestamp) as c2, 1.1::timestamp as c3;
505505
----
506-
1970-01-01T00:00:00.000000001 1970-01-01T00:00:00.000000001 1970-01-01T00:00:00.000000001
506+
1970-01-01T00:00:01.100 1970-01-01T00:00:00.000000001 1970-01-01T00:00:00.000000001
507507

508508
query PPP
509509
SELECT to_timestamp(-1.1) as c1, cast(-1.1 as timestamp) as c2, (-1.1)::timestamp as c3;
510510
----
511-
1969-12-31T23:59:59.999999999 1969-12-31T23:59:59.999999999 1969-12-31T23:59:59.999999999
511+
1969-12-31T23:59:58.900 1969-12-31T23:59:59.999999999 1969-12-31T23:59:59.999999999
512512

513513
query PPP
514514
SELECT to_timestamp(0.0) as c1, cast(0.0 as timestamp) as c2, 0.0::timestamp as c3;
@@ -518,12 +518,12 @@ SELECT to_timestamp(0.0) as c1, cast(0.0 as timestamp) as c2, 0.0::timestamp as
518518
query PPP
519519
SELECT to_timestamp(1.23456789) as c1, cast(1.23456789 as timestamp) as c2, 1.23456789::timestamp as c3;
520520
----
521-
1970-01-01T00:00:00.000000001 1970-01-01T00:00:00.000000001 1970-01-01T00:00:00.000000001
521+
1970-01-01T00:00:01.234567890 1970-01-01T00:00:00.000000001 1970-01-01T00:00:00.000000001
522522

523523
query PPP
524524
SELECT to_timestamp(123456789.123456789) as c1, cast(123456789.123456789 as timestamp) as c2, 123456789.123456789::timestamp as c3;
525525
----
526-
1970-01-01T00:00:00.123456789 1970-01-01T00:00:00.123456789 1970-01-01T00:00:00.123456789
526+
1973-11-29T21:33:09.123456784 1970-01-01T00:00:00.123456789 1970-01-01T00:00:00.123456789
527527

528528
# to_timestamp Decimal128 inputs
529529

0 commit comments

Comments
 (0)