Skip to content

Commit 6f6fb17

Browse files
goldmedalbgjackma
authored andcommitted
support date32 for date_trunc (apache#12603)
1 parent 350e3f8 commit 6f6fb17

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

datafusion/functions/src/datetime/date_trunc.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ impl ScalarUDFImpl for DateTruncFunc {
123123

124124
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
125125
match &arg_types[1] {
126-
Timestamp(Nanosecond, None) | Utf8 | Null => Ok(Timestamp(Nanosecond, None)),
126+
Timestamp(Nanosecond, None) | Utf8 | DataType::Date32 | Null => {
127+
Ok(Timestamp(Nanosecond, None))
128+
}
127129
Timestamp(Nanosecond, tz_opt) => Ok(Timestamp(Nanosecond, tz_opt.clone())),
128130
Timestamp(Microsecond, tz_opt) => Ok(Timestamp(Microsecond, tz_opt.clone())),
129131
Timestamp(Millisecond, tz_opt) => Ok(Timestamp(Millisecond, tz_opt.clone())),

datafusion/sqllogictest/test_files/dates.slt

+36
Original file line numberDiff line numberDiff line change
@@ -238,5 +238,41 @@ select to_date('2022-01-23', '%Y-%m-%d');
238238
----
239239
2022-01-23
240240

241+
query PPPP
242+
select
243+
date_trunc('YEAR', to_date('2022-02-23', '%Y-%m-%d')),
244+
date_trunc('MONTH', to_date('2022-02-23', '%Y-%m-%d')),
245+
date_trunc('DAY', to_date('2022-02-23', '%Y-%m-%d')),
246+
date_trunc('WEEK', to_date('2022-02-23', '%Y-%m-%d'));
247+
----
248+
2022-01-01T00:00:00 2022-02-01T00:00:00 2022-02-23T00:00:00 2022-02-21T00:00:00
249+
250+
query PPPPP
251+
select
252+
date_trunc('HOUR', to_date('2022-02-23', '%Y-%m-%d')),
253+
date_trunc('MINUTE', to_date('2022-02-23', '%Y-%m-%d')),
254+
date_trunc('SECOND', to_date('2022-02-23', '%Y-%m-%d')),
255+
date_trunc('MILLISECOND', to_date('2022-02-23', '%Y-%m-%d')),
256+
date_trunc('MICROSECOND', to_date('2022-02-23', '%Y-%m-%d'));
257+
----
258+
2022-02-23T00:00:00 2022-02-23T00:00:00 2022-02-23T00:00:00 2022-02-23T00:00:00 2022-02-23T00:00:00
259+
260+
query PPPP
261+
select
262+
date_trunc('YEAR', d2_date),
263+
date_trunc('MONTH', d2_date),
264+
date_trunc('DAY', d2_date),
265+
date_trunc('WEEK', d2_date)
266+
FROM test
267+
----
268+
2022-01-01T00:00:00 2022-12-01T00:00:00 2022-12-12T00:00:00 2022-12-12T00:00:00
269+
2022-01-01T00:00:00 2022-12-01T00:00:00 2022-12-11T00:00:00 2022-12-05T00:00:00
270+
2022-01-01T00:00:00 2022-12-01T00:00:00 2022-12-10T00:00:00 2022-12-05T00:00:00
271+
2022-01-01T00:00:00 2022-12-01T00:00:00 2022-12-09T00:00:00 2022-12-05T00:00:00
272+
2022-01-01T00:00:00 2022-12-01T00:00:00 2022-12-08T00:00:00 2022-12-05T00:00:00
273+
2022-01-01T00:00:00 2022-12-01T00:00:00 2022-12-07T00:00:00 2022-12-05T00:00:00
274+
2022-01-01T00:00:00 2022-12-01T00:00:00 2022-12-06T00:00:00 2022-12-05T00:00:00
275+
2022-01-01T00:00:00 2022-12-01T00:00:00 2022-12-05T00:00:00 2022-12-05T00:00:00
276+
241277
statement ok
242278
drop table ts_utf8_data

0 commit comments

Comments
 (0)