Skip to content

Commit

Permalink
minors
Browse files Browse the repository at this point in the history
  • Loading branch information
comphead committed Nov 22, 2023
1 parent 4806bde commit 7ee9f9b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
3 changes: 2 additions & 1 deletion datafusion/sql/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
let expr =
self.sql_expr_to_logical_expr(*expr, schema, planner_context)?;

// int/floats input should be treated as seconds rather as nanoseconds
// numeric constants are treated as seconds (rather as nanoseconds)
// to align with postgres / duckdb semantics
let expr = match &dt {
DataType::Timestamp(TimeUnit::Nanosecond, tz)
if expr.get_type(schema)? == DataType::Int64 =>
Expand Down
8 changes: 6 additions & 2 deletions datafusion/sql/tests/sql_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,14 +597,18 @@ fn select_neg_filter() {
fn select_compound_filter() {
let sql = "SELECT id, first_name, last_name \
FROM person WHERE state = 'CO' AND age >= 21 AND age <= 65";
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";
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";
quick_test(sql, expected);
}

#[test]
fn test_timestamp_filter() {
let sql = "SELECT state FROM person WHERE birth_date < CAST (158412331400600000 as timestamp)";
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";
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";
quick_test(sql, expected);
}

Expand Down
48 changes: 26 additions & 22 deletions datafusion/sqllogictest/test_files/timestamps.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1794,17 +1794,23 @@ SELECT to_timestamp(null), to_timestamp(0), to_timestamp(1926632005), to_timesta
----
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

# verify timestamp cast with integer input timestamp literal syntax
query PPPPPP
SELECT null::timestamp, 0::timestamp, 1926632005::timestamp, 1::timestamp, -1::timestamp, (0-1)::timestamp
----
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

# verify timestamp cast with integer input timestamp literal syntax using CAST syntax
query PPPPPP
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)
----
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
# verify timestamp syntax stlyes are consistent
query BBBBBBBBBBBBB
SELECT to_timestamp(null) is null as c1,
null::timestamp is null as c2,
cast(null as timestamp) is null as c3,
to_timestamp(0) = 0::timestamp as c4,
to_timestamp(1926632005) = 1926632005::timestamp as c5,
to_timestamp(1) = 1::timestamp as c6,
to_timestamp(-1) = -1::timestamp as c7,
to_timestamp(0-1) = (0-1)::timestamp as c8,
to_timestamp(0) = cast(0 as timestamp) as c9,
to_timestamp(1926632005) = cast(1926632005 as timestamp) as c10,
to_timestamp(1) = cast(1 as timestamp) as c11,
to_timestamp(-1) = cast(-1 as timestamp) as c12,
to_timestamp(0-1) = cast(0-1 as timestamp) as c13
----
true true true true true true true true true true true true true

# verify timestamp output types
query TTT
Expand All @@ -1813,17 +1819,15 @@ SELECT arrow_typeof(to_timestamp(1)), arrow_typeof(to_timestamp(null)), arrow_ty
Timestamp(Nanosecond, None) Timestamp(Nanosecond, None) Timestamp(Nanosecond, None)

# verify timestamp output types using timestamp literal syntax
query TTT
SELECT arrow_typeof(1::timestamp), arrow_typeof(null::timestamp), arrow_typeof('2023-01-10 12:34:56.000'::timestamp)
----
Timestamp(Nanosecond, None) Timestamp(Nanosecond, None) Timestamp(Nanosecond, None)

# verify timestamp output types using CAST syntax
query TTT
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))
----
Timestamp(Nanosecond, None) Timestamp(Nanosecond, None) Timestamp(Nanosecond, None)

query BBBBBB
SELECT arrow_typeof(to_timestamp(1)) = arrow_typeof(1::timestamp) as c1,
arrow_typeof(to_timestamp(null)) = arrow_typeof(null::timestamp) as c2,
arrow_typeof(to_timestamp('2023-01-10 12:34:56.000')) = arrow_typeof('2023-01-10 12:34:56.000'::timestamp) as c3,
arrow_typeof(to_timestamp(1)) = arrow_typeof(cast(1 as timestamp)) as c4,
arrow_typeof(to_timestamp(null)) = arrow_typeof(cast(null as timestamp)) as c5,
arrow_typeof(to_timestamp('2023-01-10 12:34:56.000')) = arrow_typeof(cast('2023-01-10 12:34:56.000' as timestamp)) as c6
----
true true true true true true

# known issues. currently overflows (expects default precision to be microsecond instead of nanoseconds. Work pending)
#verify extreme values
Expand Down

0 comments on commit 7ee9f9b

Please sign in to comment.