Skip to content

Commit ffb8771

Browse files
authored
fix(query): unsupport datetime format item should not return panic error (#17323)
fix(query): unsupport format item should not return panic error
1 parent 07034df commit ffb8771

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use std::io::Write;
1616

1717
use chrono::format::parse_and_remainder;
18+
use chrono::format::Item;
1819
use chrono::format::Parsed;
1920
use chrono::format::StrftimeItems;
2021
use chrono::prelude::*;
@@ -700,18 +701,22 @@ fn register_to_string(registry: &mut FunctionRegistry) {
700701
if format.is_empty() {
701702
output.push_null();
702703
} else {
703-
// Can't use `tz.timestamp_nanos(self.as_() * 1000)` directly, is may cause multiply with overflow.
704-
let (mut secs, mut nanos) =
705-
(micros / MICROS_PER_SEC, (micros % MICROS_PER_SEC) * 1_000);
706-
if nanos < 0 {
707-
secs -= 1;
708-
nanos += 1_000_000_000;
704+
let items = StrftimeItems::new(format);
705+
if items.clone().any(|item| matches!(item, Item::Error)) {
706+
ctx.set_error(output.len(), "Invalid format string".to_string());
707+
output.push_null();
708+
} else {
709+
// Can't use `tz.timestamp_nanos(self.as_() * 1000)` directly, is may cause multiply with overflow.
710+
let (mut secs, mut nanos) =
711+
(micros / MICROS_PER_SEC, (micros % MICROS_PER_SEC) * 1_000);
712+
if nanos < 0 {
713+
secs -= 1;
714+
nanos += 1_000_000_000;
715+
}
716+
let ts = ctx.func_ctx.tz.timestamp_opt(secs, nanos as u32).unwrap();
717+
let res = ts.format(format).to_string();
718+
output.push(&res);
709719
}
710-
let ts = ctx.func_ctx.tz.timestamp_opt(secs, nanos as u32).unwrap();
711-
// https://github.com/BurntSushi/jiff/issues/155
712-
// ASCII is currently required in jiff crate
713-
let res = ts.format(format).to_string();
714-
output.push(&res);
715720
}
716721
},
717722
),

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,10 @@ select to_date('精彩的2022年,美丽的02month,激动の02d', '精彩的%Y
13671367
----
13681368
2022-02-02
13691369

1370+
1371+
statement error 1006
1372+
select date_format('2022-2-04T03:58:59', '%i');
1373+
13701374
statement error 1006
13711375
select date_format('', '');
13721376

@@ -1486,3 +1490,5 @@ query T
14861490
SELECT add_hours(to_timestamp(710455), 2147483647);
14871491
----
14881492
1000-01-01 00:00:00.000000
1493+
1494+

0 commit comments

Comments
 (0)