Skip to content

Commit 79d8329

Browse files
TCeasonsundy-li
andauthored
feat(query): to_timestamp support parser string to ts without timezone (#15131)
feat(query): add to_timestamp support parser string to ts without timezone ```sql select to_timestamp('2022/02/04/ 8-58-59', '%Y/%m/%d/ %H-%M-%S') as dt; +----------------------------+ | dt | +----------------------------+ | 2022-02-04 08:58:59.000000 | +----------------------------+ ``` Co-authored-by: sundyli <543950155@qq.com>
1 parent f8494c8 commit 79d8329

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,30 @@ fn register_string_to_timestamp(registry: &mut FunctionRegistry) {
169169
if format.is_empty() {
170170
output.push_null();
171171
} else {
172-
// date need has timezone info.
173-
if let Ok(res) = DateTime::parse_from_str(timestamp, format) {
174-
output.push(res.with_timezone(&ctx.func_ctx.tz.tz).timestamp_micros());
172+
//%Z ACST Local time zone name. Skips all non-whitespace characters during parsing. Identical to %:z when formatting. 6
173+
// %z +0930 Offset from the local time to UTC (with UTC being +0000).
174+
// %:z +09:30 Same as %z but with a colon.
175+
// %::z +09:30:00 Offset from the local time to UTC with seconds.
176+
// %:::z +09 Offset from the local time to UTC without minutes.
177+
// %#z +09 Parsing only: Same as %z but allows minutes to be missing or present.
178+
let timezone_strftime = ["%Z", "%z", "%:z", "%::z", "%:::z", "%#z"];
179+
if timezone_strftime
180+
.iter()
181+
.any(|&pattern| format.contains(pattern))
182+
{
183+
if let Ok(res) = DateTime::parse_from_str(timestamp, format) {
184+
// date need has timezone info.
185+
output.push(res.with_timezone(&ctx.func_ctx.tz.tz).timestamp_micros());
186+
} else {
187+
output.push_null();
188+
}
175189
} else {
176-
output.push_null();
190+
// Parse as unix timestamp
191+
if let Ok(res) = NaiveDateTime::parse_from_str(timestamp, format) {
192+
output.push(res.timestamp_micros());
193+
} else {
194+
output.push_null();
195+
}
177196
}
178197
}
179198
},

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ select date_format('2022-02-04T03:58:59', '%Y年%m月%d日,%H时%M分%S秒');
10581058
query T
10591059
select str_to_timestamp('2022年02月04日,03时58分59秒', '%Y年%m月%d日,%H时%M分%S秒');
10601060
----
1061-
NULL
1061+
2022-02-04 03:58:59.000000
10621062

10631063
query T
10641064
select str_to_timestamp('2022年02月04日,8时58分59秒,时区:+0000', '%Y年%m月%d日,%H时%M分%S秒,时区:%z');
@@ -1070,6 +1070,21 @@ select to_timestamp('2022年02月04日,8时58分59秒,时区:+0800', '%Y年%
10701070
----
10711071
2022-02-04 00:58:59.000000
10721072

1073+
query T
1074+
select to_timestamp('2022年02月04日,8时58分59秒', '%Y年%m月%d日,%H时%M分%S秒');
1075+
----
1076+
2022-02-04 08:58:59.000000
1077+
1078+
query T
1079+
select to_timestamp('2022年02月04日,8时58分59秒', '');
1080+
----
1081+
NULL
1082+
1083+
query T
1084+
select to_timestamp('10000-09-09 01:46:39', '%Y-%m-%d %H:%M:%S');
1085+
----
1086+
NULL
1087+
10731088
query I
10741089
select week('2017-01-01');
10751090
----

0 commit comments

Comments
 (0)