Skip to content

Commit

Permalink
feat(query): add new function TO_TIMESTAMP<int, scale> (#16924)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
TCeason authored Nov 25, 2024
1 parent c6b23ae commit cf150d9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/query/functions/src/scalars/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,30 @@ fn register_number_to_timestamp(registry: &mut FunctionRegistry) {
error_to_null(eval_number_to_timestamp),
);

registry.register_passthrough_nullable_2_arg::<Int64Type, UInt64Type, TimestampType, _, _>(
"to_timestamp",
|_, _, _| FunctionDomain::Full,
vectorize_with_builder_2_arg::<Int64Type, UInt64Type, TimestampType>(
|val, scale, output, _| {
let mut n = val * 10i64.pow(6 - scale.clamp(0, 6) as u32);
clamp_timestamp(&mut n);
output.push(n)
},
),
);

registry.register_passthrough_nullable_2_arg::<Int64Type, UInt64Type, TimestampType, _, _>(
"try_to_timestamp",
|_, _, _| FunctionDomain::Full,
vectorize_with_builder_2_arg::<Int64Type, UInt64Type, TimestampType>(
|val, scale, output, _| {
let mut n = val * 10i64.pow(6 - scale.clamp(0, 6) as u32);
clamp_timestamp(&mut n);
output.push(n);
},
),
);

fn eval_number_to_timestamp(
val: ValueRef<Int64Type>,
ctx: &mut EvalContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4164,6 +4164,8 @@ Functions overloads:
7 to_timestamp(Date NULL) :: Timestamp NULL
8 to_timestamp(Int64) :: Timestamp
9 to_timestamp(Int64 NULL) :: Timestamp NULL
10 to_timestamp(Int64, UInt64) :: Timestamp
11 to_timestamp(Int64 NULL, UInt64 NULL) :: Timestamp NULL
0 to_uint16(Variant) :: UInt16
1 to_uint16(Variant NULL) :: UInt16 NULL
2 to_uint16(String) :: UInt16
Expand Down Expand Up @@ -4614,6 +4616,8 @@ Functions overloads:
7 try_to_timestamp(Date NULL) :: Timestamp NULL
8 try_to_timestamp(Int64) :: Timestamp NULL
9 try_to_timestamp(Int64 NULL) :: Timestamp NULL
10 try_to_timestamp(Int64, UInt64) :: Timestamp
11 try_to_timestamp(Int64 NULL, UInt64 NULL) :: Timestamp NULL
0 try_to_uint16(Variant) :: UInt16 NULL
1 try_to_uint16(Variant NULL) :: UInt16 NULL
2 try_to_uint16(String) :: UInt16 NULL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1451,3 +1451,18 @@ query T
select to_timestamp('2022-03-27 07:54:31.12');
----
2022-03-27 07:54:31.120000

query T
select TO_TIMESTAMP(-7233803000000+1, 6), TO_TIMESTAMP(-7233803000, 3), TO_TIMESTAMP(-7233803000000-1, 6);
----
1969-10-09 06:36:37.000001 1969-10-09 06:36:37.000000 1969-10-09 06:36:36.999999

query T
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

query T
select TRY_TO_TIMESTAMP(1, 0), TRY_TO_TIMESTAMP(1, null);
----
1970-01-01 00:00:01.000000 NULL

0 comments on commit cf150d9

Please sign in to comment.