Skip to content

Commit d418ed0

Browse files
committed
add some check
1 parent 1cee2cf commit d418ed0

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

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

+15-5
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pub fn register(registry: &mut FunctionRegistry) {
111111
// [date | timestamp] +/- number
112112
register_timestamp_add_sub(registry);
113113

114-
// convert_timezone( target_timezone, date, src_timezone)
114+
// convert_timezone( target_timezone, 'timestamp')
115115
register_convert_timezone(registry);
116116
}
117117

@@ -144,6 +144,12 @@ fn register_convert_timezone(registry: &mut FunctionRegistry) {
144144
|_, _, _| FunctionDomain::MayThrow,
145145
vectorize_with_builder_2_arg::<StringType, TimestampType, TimestampType>(
146146
|target_tz, src_timestamp, output, ctx| {
147+
if let Some(validity) = &ctx.validity {
148+
if !validity.get_bit(output.len()) {
149+
output.push(0);
150+
return;
151+
}
152+
}
147153
// Convert source timestamp from source timezone to target timezone
148154
let p_src_timestamp = src_timestamp.to_timestamp(ctx.func_ctx.tz.tz);
149155
let src_dst_from_utc = p_src_timestamp.offset().fix().local_minus_utc();
@@ -163,10 +169,14 @@ fn register_convert_timezone(registry: &mut FunctionRegistry) {
163169
.offset()
164170
.fix()
165171
.local_minus_utc();
166-
let offset_as_micros_sec =
167-
(target_dst_from_utc - src_dst_from_utc) as i64 * MICROS_PER_SEC;
168-
let res = result_timestamp.checked_add(offset_as_micros_sec).unwrap();
169-
output.push(res)
172+
let offset_as_micros_sec = (target_dst_from_utc - src_dst_from_utc) as i64;
173+
match offset_as_micros_sec.checked_mul(MICROS_PER_SEC) {
174+
Some(offset) => match result_timestamp.checked_add(offset) {
175+
Some(res) => output.push(res),
176+
None => ctx.set_error(output.len(), "calc final time error".to_string()),
177+
},
178+
None => ctx.set_error(output.len(), "calc time offset error".to_string()),
179+
}
170180
},
171181
),
172182
);

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -782,18 +782,22 @@ statement ok
782782
set timezone='UTC';
783783

784784
statement ok
785-
create or replace table t(c timestamp);
785+
create or replace table t(a string null, c timestamp null);
786786

787787
statement ok
788-
insert into t values('1970-01-01 00:00:00'), ('2024-10-31 22:21:15');
788+
insert into t values('America/Los_Angeles','1970-01-01 00:00:00'), ('America/Los_Angeles','2024-10-31 22:21:15'), (null, '1970-01-01 00:00:00'), ('Asia/Shanghai', '1970-01-01 00:00:00'), ('Asia/Shanghai', '2024-10-31 22:21:15'),('Asia/Shanghai', null), (null, null);
789789

790790
# UTC to America/Los_Angeles, 20240-03-10 ~ 2024-11-03 is UTC-7(dst), other is UTC-8
791791
query T
792-
select c, CONVERT_TIMEZONE('America/Los_Angeles', c) from t;
792+
select a, c, CONVERT_TIMEZONE(a, c) from t order by a,c;
793793
----
794-
1970-01-01 00:00:00.000000 1969-12-31 16:00:00.000000
795-
2024-10-31 22:21:15.000000 2024-10-31 15:21:15.000000
796-
794+
America/Los_Angeles 1970-01-01 00:00:00.000000 1969-12-31 16:00:00.000000
795+
America/Los_Angeles 2024-10-31 22:21:15.000000 2024-10-31 15:21:15.000000
796+
Asia/Shanghai 1970-01-01 00:00:00.000000 1970-01-01 08:00:00.000000
797+
Asia/Shanghai 2024-10-31 22:21:15.000000 2024-11-01 06:21:15.000000
798+
Asia/Shanghai NULL NULL
799+
NULL 1970-01-01 00:00:00.000000 NULL
800+
NULL NULL NULL
797801

798802
statement ok
799803
set timezone='Asia/Shanghai';

0 commit comments

Comments
 (0)