Skip to content

Commit cf150d9

Browse files
authored
feat(query): add new function TO_TIMESTAMP<int, scale> (#16924)
feat(query): add new function TO_TIMESTAMP(int, scale) scale support [0,6] select TO_TIMESTAMP(1, 0), TO_TIMESTAMP(1, 1), TO_TIMESTAMP(1, 2), TO_TIMESTAMP(1, 3), TO_TIMESTAMP(1, 4), TO_TIMESTAMP(1, 5), TO_TIMESTAMP(1, 6); ---- 1970-01-01 00:00:01.000000 1970-01-01 00:00:00.100000 1970-01-01 00:00:00.010000 1970-01-01 00:00:00.001000 1970-01-01 00:00:00.000100 1970-01-01 00:00:00.000010 1970-01-01 00:00:00.000001
1 parent c6b23ae commit cf150d9

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,30 @@ fn register_number_to_timestamp(registry: &mut FunctionRegistry) {
541541
error_to_null(eval_number_to_timestamp),
542542
);
543543

544+
registry.register_passthrough_nullable_2_arg::<Int64Type, UInt64Type, TimestampType, _, _>(
545+
"to_timestamp",
546+
|_, _, _| FunctionDomain::Full,
547+
vectorize_with_builder_2_arg::<Int64Type, UInt64Type, TimestampType>(
548+
|val, scale, output, _| {
549+
let mut n = val * 10i64.pow(6 - scale.clamp(0, 6) as u32);
550+
clamp_timestamp(&mut n);
551+
output.push(n)
552+
},
553+
),
554+
);
555+
556+
registry.register_passthrough_nullable_2_arg::<Int64Type, UInt64Type, TimestampType, _, _>(
557+
"try_to_timestamp",
558+
|_, _, _| FunctionDomain::Full,
559+
vectorize_with_builder_2_arg::<Int64Type, UInt64Type, TimestampType>(
560+
|val, scale, output, _| {
561+
let mut n = val * 10i64.pow(6 - scale.clamp(0, 6) as u32);
562+
clamp_timestamp(&mut n);
563+
output.push(n);
564+
},
565+
),
566+
);
567+
544568
fn eval_number_to_timestamp(
545569
val: ValueRef<Int64Type>,
546570
ctx: &mut EvalContext,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4164,6 +4164,8 @@ Functions overloads:
41644164
7 to_timestamp(Date NULL) :: Timestamp NULL
41654165
8 to_timestamp(Int64) :: Timestamp
41664166
9 to_timestamp(Int64 NULL) :: Timestamp NULL
4167+
10 to_timestamp(Int64, UInt64) :: Timestamp
4168+
11 to_timestamp(Int64 NULL, UInt64 NULL) :: Timestamp NULL
41674169
0 to_uint16(Variant) :: UInt16
41684170
1 to_uint16(Variant NULL) :: UInt16 NULL
41694171
2 to_uint16(String) :: UInt16
@@ -4614,6 +4616,8 @@ Functions overloads:
46144616
7 try_to_timestamp(Date NULL) :: Timestamp NULL
46154617
8 try_to_timestamp(Int64) :: Timestamp NULL
46164618
9 try_to_timestamp(Int64 NULL) :: Timestamp NULL
4619+
10 try_to_timestamp(Int64, UInt64) :: Timestamp
4620+
11 try_to_timestamp(Int64 NULL, UInt64 NULL) :: Timestamp NULL
46174621
0 try_to_uint16(Variant) :: UInt16 NULL
46184622
1 try_to_uint16(Variant NULL) :: UInt16 NULL
46194623
2 try_to_uint16(String) :: UInt16 NULL

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,3 +1451,18 @@ query T
14511451
select to_timestamp('2022-03-27 07:54:31.12');
14521452
----
14531453
2022-03-27 07:54:31.120000
1454+
1455+
query T
1456+
select TO_TIMESTAMP(-7233803000000+1, 6), TO_TIMESTAMP(-7233803000, 3), TO_TIMESTAMP(-7233803000000-1, 6);
1457+
----
1458+
1969-10-09 06:36:37.000001 1969-10-09 06:36:37.000000 1969-10-09 06:36:36.999999
1459+
1460+
query T
1461+
select TO_TIMESTAMP(1, 0), TO_TIMESTAMP(1, 1), TO_TIMESTAMP(1, 2), TO_TIMESTAMP(1, 3), TO_TIMESTAMP(1, 4), TO_TIMESTAMP(1, 5), TO_TIMESTAMP(1, 6);
1462+
----
1463+
1970-01-01 00:00:01.000000 1970-01-01 00:00:00.100000 1970-01-01 00:00:00.010000 1970-01-01 00:00:00.001000 1970-01-01 00:00:00.000100 1970-01-01 00:00:00.000010 1970-01-01 00:00:00.000001
1464+
1465+
query T
1466+
select TRY_TO_TIMESTAMP(1, 0), TRY_TO_TIMESTAMP(1, null);
1467+
----
1468+
1970-01-01 00:00:01.000000 NULL

0 commit comments

Comments
 (0)