Skip to content

Commit 5fe44db

Browse files
committed
output to_timestamp signature as ns
1 parent 830a4c8 commit 5fe44db

10 files changed

Lines changed: 40 additions & 35 deletions

File tree

datafusion-examples/examples/expr_api.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ fn simplify_demo() -> Result<()> {
8282
let expr = simplifier.simplify(expr)?;
8383

8484
// DataFusion has simplified the expression to a comparison with a constant
85-
// ts = 1599566400i64; Tada!
85+
// ts = 1599566400000000000; Tada!
8686
assert_eq!(
8787
expr,
88-
col("ts").eq(lit(ScalarValue::TimestampSecond(Some(1599566400i64), None)))
88+
col("ts").eq(lit_timestamp_nano(1599566400000000000i64))
8989
);
9090

9191
// here are some other examples of what DataFusion is capable of

datafusion/core/tests/sql/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ async fn test_uuid_expression() -> Result<()> {
639639
async fn test_extract_date_part() -> Result<()> {
640640
test_expression!("date_part('YEAR', CAST('2000-01-01' AS DATE))", "2000.0");
641641
test_expression!(
642-
"EXTRACT(year FROM to_timestamp('2020-09-08T12:00:00+00:00'))",
642+
"EXTRACT(year FROM timestamp '2020-09-08T12:00:00+00:00')",
643643
"2020.0"
644644
);
645645
test_expression!("date_part('QUARTER', CAST('2000-01-01' AS DATE))", "1.0");

datafusion/expr/src/built_in_function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ impl BuiltinScalarFunction {
748748
return plan_err!("The to_hex function can only accept integers.");
749749
}
750750
}),
751-
BuiltinScalarFunction::ToTimestamp => Ok(Timestamp(Second, None)),
751+
BuiltinScalarFunction::ToTimestamp => Ok(Timestamp(Nanosecond, None)),
752752
BuiltinScalarFunction::ToTimestampMillis => Ok(Timestamp(Millisecond, None)),
753753
BuiltinScalarFunction::ToTimestampMicros => Ok(Timestamp(Microsecond, None)),
754754
BuiltinScalarFunction::ToTimestampNanos => Ok(Timestamp(Nanosecond, None)),

datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,10 +1504,7 @@ mod tests {
15041504
// to_timestamp("2020-09-08T12:00:00+00:00") --> timestamp(1599566400i64)
15051505
let expr =
15061506
call_fn("to_timestamp", vec![lit("2020-09-08T12:00:00+00:00")]).unwrap();
1507-
test_evaluate(
1508-
expr,
1509-
lit(ScalarValue::TimestampSecond(Some(1599566400i64), None)),
1510-
);
1507+
test_evaluate(expr, lit_timestamp_nano(1599566400000000000i64));
15111508

15121509
// check that non foldable arguments are folded
15131510
// to_timestamp(a) --> to_timestamp(a) [no rewrite possible]
@@ -1543,10 +1540,10 @@ mod tests {
15431540
let expr = cast_to_int64_expr(now_expr()) + lit(100_i64);
15441541
test_evaluate_with_start_time(expr, lit(ts_nanos + 100), &time);
15451542

1546-
// CAST(now() as int64) < cast(to_timestamp(...) as int64) + 50000_i64 ---> false
1543+
// CAST(now() as int64) < cast(to_timestamp(...) as int64) + 50000_i64 ---> true
15471544
let expr = cast_to_int64_expr(now_expr())
15481545
.lt(cast_to_int64_expr(to_timestamp_expr(ts_string)) + lit(50000i64));
1549-
test_evaluate_with_start_time(expr, lit(false), &time);
1546+
test_evaluate_with_start_time(expr, lit(true), &time);
15501547
}
15511548

15521549
#[test]

datafusion/optimizer/src/simplify_expressions/simplify_exprs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ mod tests {
460460
.project(proj)?
461461
.build()?;
462462

463-
let expected = "Projection: TimestampSecond(1599566400, None) AS to_timestamp(Utf8(\"2020-09-08T12:00:00+00:00\"))\
463+
let expected = "Projection: TimestampNanosecond(1599566400000000000, None) AS to_timestamp(Utf8(\"2020-09-08T12:00:00+00:00\"))\
464464
\n TableScan: test"
465465
.to_string();
466466
let actual = get_optimized_plan_formatted(&plan, &Utc::now());
@@ -571,8 +571,8 @@ mod tests {
571571
.build()?;
572572

573573
// Note that constant folder runs and folds the entire
574-
// expression down to a single constant (false)
575-
let expected = "Filter: Boolean(false)\
574+
// expression down to a single constant (true)
575+
let expected = "Filter: Boolean(true)\
576576
\n TableScan: test";
577577
let actual = get_optimized_plan_formatted(&plan, &time);
578578

datafusion/physical-expr/src/datetime_expressions.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ fn string_to_timestamp_nanos_shim(s: &str) -> Result<i64> {
129129

130130
/// to_timestamp SQL function
131131
pub fn to_timestamp(args: &[ColumnarValue]) -> Result<ColumnarValue> {
132-
handle::<TimestampSecondType, _, TimestampSecondType>(
132+
handle::<TimestampNanosecondType, _, TimestampNanosecondType>(
133133
args,
134-
|s| string_to_timestamp_nanos_shim(s).map(|n| n / 1_000_000_000),
134+
string_to_timestamp_nanos_shim,
135135
"to_timestamp",
136136
)
137137
}
@@ -969,10 +969,10 @@ mod tests {
969969
// ensure that arrow array implementation is wired up and handles nulls correctly
970970

971971
let mut string_builder = StringBuilder::with_capacity(2, 1024);
972-
let mut ts_builder = TimestampSecondArray::builder(2);
972+
let mut ts_builder = TimestampNanosecondArray::builder(2);
973973

974-
string_builder.append_value("2020-09-08T13:42:29");
975-
ts_builder.append_value(1599572549);
974+
string_builder.append_value("2020-09-08T13:42:29.190855");
975+
ts_builder.append_value(1599572549190855000);
976976

977977
string_builder.append_null();
978978
ts_builder.append_null();

datafusion/physical-expr/src/functions.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,20 @@ pub fn create_physical_expr(
7474
// so we don't have to pay a per-array/batch cost.
7575
BuiltinScalarFunction::ToTimestamp => {
7676
Arc::new(match input_phy_exprs[0].data_type(input_schema) {
77-
Ok(DataType::Int64) | Ok(DataType::Timestamp(_, None)) => {
78-
|col_values: &[ColumnarValue]| {
79-
cast_column(
80-
&col_values[0],
81-
&DataType::Timestamp(TimeUnit::Second, None),
82-
None,
83-
)
84-
}
85-
}
77+
Ok(DataType::Int64) => |col_values: &[ColumnarValue]| {
78+
cast_column(
79+
&col_values[0],
80+
&DataType::Timestamp(TimeUnit::Second, None),
81+
None,
82+
)
83+
},
84+
Ok(DataType::Timestamp(_, None)) => |col_values: &[ColumnarValue]| {
85+
cast_column(
86+
&col_values[0],
87+
&DataType::Timestamp(TimeUnit::Nanosecond, None),
88+
None,
89+
)
90+
},
8691
Ok(DataType::Utf8) => datetime_expressions::to_timestamp,
8792
other => {
8893
return internal_err!(

datafusion/sql/tests/sql_integration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ Dml: op=[Insert Into] table=[test_decimal]
444444
)]
445445
#[case::type_mismatch(
446446
"INSERT INTO test_decimal SELECT '2022-01-01', to_timestamp('2022-01-01T12:00:00')",
447-
"Error during planning: Cannot automatically convert Timestamp(Second, None) to Decimal128(10, 2)"
447+
"Error during planning: Cannot automatically convert Timestamp(Nanosecond, None) to Decimal128(10, 2)"
448448
)]
449449
#[case::target_column_count_mismatch(
450450
"INSERT INTO person (id, first_name, last_name) VALUES ($1, $2)",

datafusion/sqllogictest/test_files/timestamps.slt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ SELECT to_timestamp_seconds(ts) FROM ts_data_micros LIMIT 3
249249
query P
250250
SELECT to_timestamp(ts) FROM ts_data_micros LIMIT 3
251251
----
252-
2020-09-08T13:42:29
253-
2020-09-08T12:42:29
254-
2020-09-08T11:42:29
252+
2020-09-08T13:42:29.190855
253+
2020-09-08T12:42:29.190855
254+
2020-09-08T11:42:29.190855
255255

256256
# query_cast_timestamp_from_unixtime
257257

@@ -266,7 +266,7 @@ SELECT from_unixtime(ts / 1000000000) FROM ts_data LIMIT 3;
266266
# to_timestamp
267267

268268
query I
269-
SELECT COUNT(*) FROM ts_data_nanos where ts > to_timestamp('2020-09-08T12:00:00+00:00')
269+
SELECT COUNT(*) FROM ts_data_nanos where ts > timestamp '2020-09-08T12:00:00+00:00'
270270
----
271271
2
272272

docs/source/user-guide/sql/scalar_functions.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,9 +1391,9 @@ extract(field FROM source)
13911391

13921392
### `to_timestamp`
13931393

1394-
Converts a value to RFC3339 second timestamp format (`YYYY-MM-DDT00:00:00Z`).
1394+
Converts a value to RFC3339 nanosecond timestamp format (`YYYY-MM-DDT00:00:00Z`).
13951395
Supports timestamp, integer, and unsigned integer types as input.
1396-
Integers and unsigned integers are parsed as Unix nanosecond timestamps and
1396+
Integers and unsigned integers are parsed as Unix second timestamps and
13971397
return the corresponding RFC3339 timestamp.
13981398

13991399
```
@@ -1450,7 +1450,10 @@ to_timestamp_nanos(expression)
14501450

14511451
### `to_timestamp_seconds`
14521452

1453-
Alias for `to_timestamp()` function.
1453+
Converts a value to RFC3339 second timestamp format (`YYYY-MM-DDT00:00:00Z`).
1454+
Supports timestamp, integer, and unsigned integer types as input.
1455+
Integers and unsigned integers are parsed as Unix nanosecond timestamps and
1456+
return the corresponding RFC3339 timestamp.
14541457

14551458
```
14561459
to_timestamp_seconds(expression)

0 commit comments

Comments
 (0)