Skip to content

Apache DataFusion SQL Query Engine

Fix date_trunc signature (#6632)

Andrew Lamb6df158eb4bb3
diff --git a/datafusion/core/tests/sqllogictests/test_files/timestamps.slt b/datafusion/core/tests/sqllogictests/test_files/timestamps.slt
index baf1f4d5b9..e90fb3bc7b 100644
--- a/datafusion/core/tests/sqllogictests/test_files/timestamps.slt
+++ b/datafusion/core/tests/sqllogictests/test_files/timestamps.slt
@@ -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
##########
@@ -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
@@ -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
##########
@@ -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)
@@ -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
diff --git a/datafusion/expr/src/built_in_function.rs b/datafusion/expr/src/built_in_function.rs
index b2cf075fa5..ef8892ee49 100644
--- a/datafusion/expr/src/built_in_function.rs
+++ b/datafusion/expr/src/built_in_function.rs
@@ -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")
}