Skip to content

Commit 1c57f68

Browse files
authored
feat(query): support timestamp diff return interval type (#17381)
timestamp_diff(TIMESTAMP, TIMESTAMP) -> INTERVAL
1 parent c5e7355 commit 1c57f68

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/query/functions/src/scalars/timestamp/src/datetime.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use std::borrow::Cow;
1616
use std::io::Write;
1717

18+
use databend_common_column::types::months_days_micros;
1819
use databend_common_exception::ErrorCode;
1920
use databend_common_expression::error_to_null;
2021
use databend_common_expression::types::date::clamp_date;
@@ -40,6 +41,7 @@ use databend_common_expression::types::Bitmap;
4041
use databend_common_expression::types::DateType;
4142
use databend_common_expression::types::Float64Type;
4243
use databend_common_expression::types::Int32Type;
44+
use databend_common_expression::types::IntervalType;
4345
use databend_common_expression::types::NullableType;
4446
use databend_common_expression::types::NumberType;
4547
use databend_common_expression::types::StringType;
@@ -1183,6 +1185,12 @@ fn register_diff_functions(registry: &mut FunctionRegistry) {
11831185
|a, b, _| a - b,
11841186
);
11851187

1188+
registry.register_2_arg::<TimestampType, TimestampType, IntervalType, _, _>(
1189+
"timestamp_diff",
1190+
|_, _, _| FunctionDomain::MayThrow,
1191+
|a, b, _| months_days_micros::new(0, 0, a - b),
1192+
);
1193+
11861194
registry.register_2_arg::<TimestampType, TimestampType, Int64Type, _, _>(
11871195
"minus",
11881196
|_, lhs, rhs| {

src/query/functions/tests/it/scalars/testdata/function_list.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3492,6 +3492,8 @@ Functions overloads:
34923492
1 tan(Float64 NULL) :: Float64 NULL
34933493
0 time_slot(Timestamp) :: Timestamp
34943494
1 time_slot(Timestamp NULL) :: Timestamp NULL
3495+
0 timestamp_diff(Timestamp, Timestamp) :: Interval
3496+
1 timestamp_diff(Timestamp NULL, Timestamp NULL) :: Interval NULL
34953497
0 to_base64(Binary) :: String
34963498
1 to_base64(Binary NULL) :: String NULL
34973499
0 to_binary(Variant) :: Binary

tests/sqllogictests/suites/query/functions/02_0079_function_interval.test

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,33 @@ query T
154154
select to_interval('120000000000 months');
155155
----
156156
00:00:00
157+
158+
onlyif http
159+
query T
160+
select '2022-01-01'::timestamp - '2021-01-01'::timestamp
161+
----
162+
31536000000000
163+
164+
onlyif http
165+
query T
166+
select timestamp_diff('2022-01-01'::timestamp,'2021-01-01'::timestamp);
167+
----
168+
8760:00:00
169+
170+
onlyif http
171+
query T
172+
select timestamp_diff('2021-01-01'::timestamp, '2022-01-01'::timestamp);
173+
----
174+
-8760:00:00
175+
176+
onlyif http
177+
query T
178+
select timestamp_diff('2022-01-01'::timestamp,'2021-12-01'::timestamp);
179+
----
180+
744:00:00
181+
182+
onlyif http
183+
query T
184+
select '2022-01-01'::timestamp-'2021-12-01'::timestamp;
185+
----
186+
2678400000000

0 commit comments

Comments
 (0)