Skip to content

Commit 2ec94d4

Browse files
committed
sync signature
1 parent cb3cec8 commit 2ec94d4

File tree

7 files changed

+64
-34
lines changed

7 files changed

+64
-34
lines changed

datafusion/common/src/scalar.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,15 @@ impl ScalarValue {
962962
ScalarValue::TimestampSecond(Some(v), tz) => {
963963
Ok(ScalarValue::TimestampSecond(Some(-v), tz.clone()))
964964
}
965+
ScalarValue::TimestampNanosecond(Some(v), tz) => {
966+
Ok(ScalarValue::TimestampNanosecond(Some(-v), tz.clone()))
967+
}
968+
ScalarValue::TimestampMicrosecond(Some(v), tz) => {
969+
Ok(ScalarValue::TimestampMicrosecond(Some(-v), tz.clone()))
970+
}
971+
ScalarValue::TimestampMillisecond(Some(v), tz) => {
972+
Ok(ScalarValue::TimestampMillisecond(Some(-v), tz.clone()))
973+
}
965974
value => _internal_err!(
966975
"Can not run arithmetic negative on scalar value {value:?}"
967976
),

datafusion/core/tests/sql/timestamp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ async fn test_arrow_typeof() -> Result<()> {
742742
"+-----------------------------------------------------------------------+",
743743
"| arrow_typeof(date_trunc(Utf8(\"microsecond\"),to_timestamp(Int64(61)))) |",
744744
"+-----------------------------------------------------------------------+",
745-
"| Timestamp(Second, None) |",
745+
"| Timestamp(Nanosecond, None) |",
746746
"+-----------------------------------------------------------------------+",
747747
];
748748
assert_batches_eq!(expected, &actual);

datafusion/expr/src/built_in_function.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -750,13 +750,10 @@ impl BuiltinScalarFunction {
750750
return plan_err!("The to_hex function can only accept integers.");
751751
}
752752
}),
753-
BuiltinScalarFunction::ToTimestamp => Ok(match &input_expr_types[0] {
754-
Int64 => Timestamp(Second, None),
755-
_ => Timestamp(Nanosecond, None),
756-
}),
753+
BuiltinScalarFunction::ToTimestamp
754+
| BuiltinScalarFunction::ToTimestampNanos => Ok(Timestamp(Nanosecond, None)),
757755
BuiltinScalarFunction::ToTimestampMillis => Ok(Timestamp(Millisecond, None)),
758756
BuiltinScalarFunction::ToTimestampMicros => Ok(Timestamp(Microsecond, None)),
759-
BuiltinScalarFunction::ToTimestampNanos => Ok(Timestamp(Nanosecond, None)),
760757
BuiltinScalarFunction::ToTimestampSeconds => Ok(Timestamp(Second, None)),
761758
BuiltinScalarFunction::FromUnixtime => Ok(Timestamp(Second, None)),
762759
BuiltinScalarFunction::Now => {

datafusion/physical-expr/src/datetime_expressions.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -966,9 +966,11 @@ pub fn to_timestamp_invoke(args: &[ColumnarValue]) -> Result<ColumnarValue> {
966966
}
967967

968968
match args[0].data_type() {
969-
DataType::Int64 => {
970-
cast_column(&args[0], &DataType::Timestamp(TimeUnit::Second, None), None)
971-
}
969+
DataType::Int64 => cast_column(
970+
&cast_column(&args[0], &DataType::Timestamp(TimeUnit::Second, None), None)?,
971+
&DataType::Timestamp(TimeUnit::Nanosecond, None),
972+
None,
973+
),
972974
DataType::Timestamp(_, None) => cast_column(
973975
&args[0],
974976
&DataType::Timestamp(TimeUnit::Nanosecond, None),

datafusion/sql/src/expr/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,18 +226,21 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
226226
SQLExpr::Cast {
227227
expr, data_type, ..
228228
} => {
229-
let mut dt = self.convert_data_type(&data_type)?;
229+
let dt = self.convert_data_type(&data_type)?;
230230
let expr =
231231
self.sql_expr_to_logical_expr(*expr, schema, planner_context)?;
232232

233-
// int/floats should come as seconds rather as nanoseconds
234-
dt = match &dt {
233+
// int/floats input should be treated as seconds rather as nanoseconds
234+
let expr = match &dt {
235235
DataType::Timestamp(TimeUnit::Nanosecond, tz)
236236
if expr.get_type(schema)? == DataType::Int64 =>
237237
{
238-
DataType::Timestamp(TimeUnit::Second, tz.clone())
238+
Expr::Cast(Cast::new(
239+
Box::new(expr),
240+
DataType::Timestamp(TimeUnit::Second, tz.clone()),
241+
))
239242
}
240-
_ => dt,
243+
_ => expr,
241244
};
242245

243246
Ok(Expr::Cast(Cast::new(Box::new(expr), dt)))

datafusion/sql/tests/sql_integration.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -597,20 +597,14 @@ fn select_neg_filter() {
597597
fn select_compound_filter() {
598598
let sql = "SELECT id, first_name, last_name \
599599
FROM person WHERE state = 'CO' AND age >= 21 AND age <= 65";
600-
let expected = "Projection: person.id, person.first_name, person.last_name\
601-
\n Filter: person.state = Utf8(\"CO\") AND person.age >= Int64(21) AND person.age <= Int64(65)\
602-
\n TableScan: person";
600+
let expected = "Projection: person.id, person.first_name, person.last_name\n Filter: person.state = Utf8(\"CO\") AND person.age >= Int64(21) AND person.age <= Int64(65)\n TableScan: person";
603601
quick_test(sql, expected);
604602
}
605603

606604
#[test]
607605
fn test_timestamp_filter() {
608606
let sql = "SELECT state FROM person WHERE birth_date < CAST (158412331400600000 as timestamp)";
609-
610-
let expected = "Projection: person.state\
611-
\n Filter: person.birth_date < CAST(Int64(158412331400600000) AS Timestamp(Second, None))\
612-
\n TableScan: person";
613-
607+
let expected = "Projection: person.state\n Filter: person.birth_date < CAST(CAST(Int64(158412331400600000) AS Timestamp(Second, None)) AS Timestamp(Nanosecond, None))\n TableScan: person";
614608
quick_test(sql, expected);
615609
}
616610

datafusion/sqllogictest/test_files/timestamps.slt

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,20 +1788,45 @@ SELECT TIMESTAMPTZ '2020-01-01 00:00:00Z' = TIMESTAMP '2020-01-01'
17881788
----
17891789
true
17901790

1791-
# verify to_timestamp edge cases to be in sync with postgresql
1792-
query PPPPPPPP
1793-
SELECT to_timestamp(null), to_timestamp(-62125747200), to_timestamp(0), to_timestamp(1926632005177), to_timestamp(1926632005), to_timestamp(1), to_timestamp(-1), to_timestamp(0-1)
1791+
# verify timestamp cast with integer input
1792+
query PPPPPP
1793+
SELECT to_timestamp(null), to_timestamp(0), to_timestamp(1926632005), to_timestamp(1), to_timestamp(-1), to_timestamp(0-1)
17941794
----
1795-
NULL 0001-04-25T00:00:00 1970-01-01T00:00:00 +63022-07-16T12:59:37 2031-01-19T23:33:25 1970-01-01T00:00:01 1969-12-31T23:59:59 1969-12-31T23:59:59
1795+
NULL 1970-01-01T00:00:00 2031-01-19T23:33:25 1970-01-01T00:00:01 1969-12-31T23:59:59 1969-12-31T23:59:59
17961796

1797-
# verify timestamp cast from i64 is in sync with to_timestamp(i64)
1798-
query PPPPPPPP
1799-
SELECT null::timestamp, -62125747200::timestamp, 0::timestamp, 1926632005177::timestamp, 1926632005::timestamp, 1::timestamp, -1::timestamp, (0-1)::timestamp
1797+
# verify timestamp cast with integer input timestamp literal syntax
1798+
query PPPPPP
1799+
SELECT null::timestamp, 0::timestamp, 1926632005::timestamp, 1::timestamp, -1::timestamp, (0-1)::timestamp
18001800
----
1801-
NULL 0001-04-25T00:00:00 1970-01-01T00:00:00 +63022-07-16T12:59:37 2031-01-19T23:33:25 1970-01-01T00:00:01 1969-12-31T23:59:59 1969-12-31T23:59:59
1801+
NULL 1970-01-01T00:00:00 2031-01-19T23:33:25 1970-01-01T00:00:01 1969-12-31T23:59:59 1969-12-31T23:59:59
18021802

1803-
# verify timestamp cast from i64 is in sync with to_timestamp(i64) using CAST syntax
1804-
query PPPPPPPP
1805-
SELECT cast(null as timestamp), cast(-62125747200 as timestamp), cast(0 as timestamp), cast(1926632005177 as timestamp), cast(1926632005 as timestamp), cast(1 as timestamp), cast(-1 as timestamp), cast(0-1 as timestamp)
1803+
# verify timestamp cast with integer input timestamp literal syntax using CAST syntax
1804+
query PPPPPP
1805+
SELECT cast(null as timestamp), cast(0 as timestamp), cast(1926632005 as timestamp), cast(1 as timestamp), cast(-1 as timestamp), cast(0-1 as timestamp)
18061806
----
1807-
NULL 0001-04-25T00:00:00 1970-01-01T00:00:00 +63022-07-16T12:59:37 2031-01-19T23:33:25 1970-01-01T00:00:01 1969-12-31T23:59:59 1969-12-31T23:59:59
1807+
NULL 1970-01-01T00:00:00 2031-01-19T23:33:25 1970-01-01T00:00:01 1969-12-31T23:59:59 1969-12-31T23:59:59
1808+
1809+
# verify timestamp output types
1810+
query TTT
1811+
SELECT arrow_typeof(to_timestamp(1)), arrow_typeof(to_timestamp(null)), arrow_typeof(to_timestamp('2023-01-10 12:34:56.000'))
1812+
----
1813+
Timestamp(Nanosecond, None) Timestamp(Nanosecond, None) Timestamp(Nanosecond, None)
1814+
1815+
# verify timestamp output types using timestamp literal syntax
1816+
query TTT
1817+
SELECT arrow_typeof(1::timestamp), arrow_typeof(null::timestamp), arrow_typeof('2023-01-10 12:34:56.000'::timestamp)
1818+
----
1819+
Timestamp(Nanosecond, None) Timestamp(Nanosecond, None) Timestamp(Nanosecond, None)
1820+
1821+
# verify timestamp output types using CAST syntax
1822+
query TTT
1823+
SELECT arrow_typeof(cast(1 as timestamp)), arrow_typeof(cast(null as timestamp)), arrow_typeof(cast('2023-01-10 12:34:56.000' as timestamp))
1824+
----
1825+
Timestamp(Nanosecond, None) Timestamp(Nanosecond, None) Timestamp(Nanosecond, None)
1826+
1827+
1828+
# verify extreme values (expects default precision to be microsecond instead of nanoseconds. Work pending)
1829+
#query PPPPPPPP
1830+
#SELECT to_timestamp(-62125747200), to_timestamp(1926632005177), -62125747200::timestamp, 1926632005177::timestamp, cast(-62125747200 as timestamp), cast(1926632005177 as timestamp)
1831+
#----
1832+
#0001-04-25T00:00:00 +63022-07-16T12:59:37 0001-04-25T00:00:00 +63022-07-16T12:59:37 0001-04-25T00:00:00 +63022-07-16T12:59:37

0 commit comments

Comments
 (0)