Skip to content

Commit

Permalink
Fix date_trunc signature (#6632)
Browse files Browse the repository at this point in the history
* Fix date_trunc signature

* fix
  • Loading branch information
alamb authored Jun 12, 2023
1 parent 2d8a42e commit 6df158e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 49 deletions.
116 changes: 78 additions & 38 deletions datafusion/core/tests/sqllogictests/test_files/timestamps.slt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,38 @@
# specific language governing permissions and limitations
# under the License.

##########
## Common timestamp data
#
# ts_data: Int64 nanosecods
# ts_data_nanos: Timestamp(Nanosecond, None)
# ts_data_micros: Timestamp(Microsecond, None)
# ts_data_millis: Timestamp(Millisecond, None)
# ts_data_secs: Timestamp(Second, None)
##########

# Create timestamp tables with different precisions but the same logical values

statement ok
create table ts_data(ts bigint, value int) as values
(1599572549190855000, 1),
(1599568949190855000, 2),
(1599565349190855000, 3);

statement ok
create table ts_data_nanos as select arrow_cast(ts, 'Timestamp(Nanosecond, None)') as ts, value from ts_data;

statement ok
create table ts_data_micros as select arrow_cast(ts / 1000, 'Timestamp(Microsecond, None)') as ts, value from ts_data;

statement ok
create table ts_data_millis as select arrow_cast(ts / 1000000, 'Timestamp(Millisecond, None)') as ts, value from ts_data;

statement ok
create table ts_data_secs as select arrow_cast(ts / 1000000000, 'Timestamp(Second, None)') as ts, value from ts_data;



##########
## Timestamp Handling Tests
##########
Expand Down Expand Up @@ -105,29 +137,6 @@ SELECT to_timestamp_seconds(ts / 1000) FROM t1 LIMIT 3
2009-03-01T00:01:00
2009-04-01T00:00:00

statement error DataFusion error: Execution error: Table 'ts' doesn't exist\.
drop table ts;

# Create timestamp tables with different precisions but the same logical values

statement ok
create table ts_data(ts bigint, value int) as values
(1599572549190855000, 1),
(1599568949190855000, 2),
(1599565349190855000, 3);

statement ok
create table ts_data_nanos as select arrow_cast(ts, 'Timestamp(Nanosecond, None)') as ts, value from ts_data;

statement ok
create table ts_data_micros as select arrow_cast(ts / 1000, 'Timestamp(Microsecond, None)') as ts, value from ts_data;

statement ok
create table ts_data_millis as select arrow_cast(ts / 1000000, 'Timestamp(Millisecond, None)') as ts, value from ts_data;

statement ok
create table ts_data_secs as select arrow_cast(ts / 1000000000, 'Timestamp(Second, None)') as ts, value from ts_data;



# query_cast_timestamp_nanos_to_others
Expand Down Expand Up @@ -377,21 +386,6 @@ select to_timestamp_seconds(cast (1 as int));
----
1970-01-01T00:00:01

statement ok
drop table ts_data

statement ok
drop table ts_data_nanos

statement ok
drop table ts_data_micros

statement ok
drop table ts_data_millis

statement ok
drop table ts_data_secs

##########
## test date_bin function
##########
Expand Down Expand Up @@ -913,6 +907,31 @@ SELECT DATE_TRUNC('SECOND', TIMESTAMP '2022-08-03 14:38:50Z');
----
2022-08-03T14:38:50

# Test date trunc on different timestamp types
query TP rowsort
SELECT 'ts_data_nanos', DATE_TRUNC('day', ts) FROM ts_data_nanos
UNION ALL
SELECT 'ts_data_micros', DATE_TRUNC('day', ts) FROM ts_data_micros
UNION ALL
SELECT 'ts_data_millis', DATE_TRUNC('day', ts) FROM ts_data_millis
UNION ALL
SELECT 'ts_data_secs', DATE_TRUNC('day', ts) FROM ts_data_secs
----
ts_data_micros 2020-09-08T00:00:00
ts_data_micros 2020-09-08T00:00:00
ts_data_micros 2020-09-08T00:00:00
ts_data_millis 2020-09-08T00:00:00
ts_data_millis 2020-09-08T00:00:00
ts_data_millis 2020-09-08T00:00:00
ts_data_nanos 2020-09-08T00:00:00
ts_data_nanos 2020-09-08T00:00:00
ts_data_nanos 2020-09-08T00:00:00
ts_data_secs 2020-09-08T00:00:00
ts_data_secs 2020-09-08T00:00:00
ts_data_secs 2020-09-08T00:00:00




# Demonstrate that strings are automatically coerced to timestamps (don't use TIMESTAMP)

Expand Down Expand Up @@ -1078,3 +1097,24 @@ SELECT
;
----
true false true true



##########
## Common timestamp data
##########

statement ok
drop table ts_data

statement ok
drop table ts_data_nanos

statement ok
drop table ts_data_micros

statement ok
drop table ts_data_millis

statement ok
drop table ts_data_secs
22 changes: 11 additions & 11 deletions datafusion/expr/src/built_in_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,17 +548,17 @@ impl BuiltinScalarFunction {
BuiltinScalarFunction::Concat => Ok(Utf8),
BuiltinScalarFunction::ConcatWithSeparator => Ok(Utf8),
BuiltinScalarFunction::DatePart => Ok(Float64),
BuiltinScalarFunction::DateTrunc | BuiltinScalarFunction::DateBin => {
match input_expr_types[1] {
Timestamp(Nanosecond, _) | Utf8 => Ok(Timestamp(Nanosecond, None)),
Timestamp(Microsecond, _) => Ok(Timestamp(Microsecond, None)),
Timestamp(Millisecond, _) => Ok(Timestamp(Millisecond, None)),
Timestamp(Second, _) => Ok(Timestamp(Second, None)),
_ => Err(DataFusionError::Internal(format!(
"The {self} function can only accept timestamp as the second arg."
))),
}
}
// DateTrunc always makes nanosecond timestamps
BuiltinScalarFunction::DateTrunc => Ok(Timestamp(Nanosecond, None)),
BuiltinScalarFunction::DateBin => match input_expr_types[1] {
Timestamp(Nanosecond, _) | Utf8 => Ok(Timestamp(Nanosecond, None)),
Timestamp(Microsecond, _) => Ok(Timestamp(Microsecond, None)),
Timestamp(Millisecond, _) => Ok(Timestamp(Millisecond, None)),
Timestamp(Second, _) => Ok(Timestamp(Second, None)),
_ => Err(DataFusionError::Internal(format!(
"The {self} function can only accept timestamp as the second arg."
))),
},
BuiltinScalarFunction::InitCap => {
utf8_to_str_type(&input_expr_types[0], "initcap")
}
Expand Down

0 comments on commit 6df158e

Please sign in to comment.