Skip to content

Commit

Permalink
fix(query): unsupport format item should not return panic error
Browse files Browse the repository at this point in the history
  • Loading branch information
TCeason committed Jan 18, 2025
1 parent 3a32c18 commit fd2f24a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/query/functions/src/scalars/timestamp/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use std::io::Write;

use chrono::format::parse_and_remainder;
use chrono::format::Item;
use chrono::format::Parsed;
use chrono::format::StrftimeItems;
use chrono::prelude::*;
Expand Down Expand Up @@ -700,18 +701,29 @@ fn register_to_string(registry: &mut FunctionRegistry) {
if format.is_empty() {
output.push_null();
} else {
// Can't use `tz.timestamp_nanos(self.as_() * 1000)` directly, is may cause multiply with overflow.
let (mut secs, mut nanos) =
(micros / MICROS_PER_SEC, (micros % MICROS_PER_SEC) * 1_000);
if nanos < 0 {
secs -= 1;
nanos += 1_000_000_000;
}
let ts = ctx.func_ctx.tz.timestamp_opt(secs, nanos as u32).unwrap();
// https://github.com/BurntSushi/jiff/issues/155
// ASCII is currently required in jiff crate
let res = ts.format(format).to_string();
output.push(&res);
// Consider use jiff crate 0.1.24 version.
// let datetime = micros.to_timestamp(ctx.func_ctx.jiff_tz.clone());
// match strtime::format(&datetime, format) {
// Ok(res) => output.push(&res),
// Err(e) => { ctx.set_error(output.len(), e.into());output.push_null() },
// }
let items = StrftimeItems::new(format);
if items.clone().any(|item| matches!(item, Item::Error)) {
ctx.set_error(output.len(), "Invalid format string".to_string());
output.push_null();
} else {
// Can't use `tz.timestamp_nanos(self.as_() * 1000)` directly, is may cause multiply with overflow.
let (mut secs, mut nanos) =
(micros / MICROS_PER_SEC, (micros % MICROS_PER_SEC) * 1_000);
if nanos < 0 {
secs -= 1;
nanos += 1_000_000_000;
}
let ts = ctx.func_ctx.tz.timestamp_opt(secs, nanos as u32).unwrap();
let res = ts.format(format).to_string();
output.push(&res);
}
}
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,10 @@ select to_date('精彩的2022年,美丽的02month,激动の02d', '精彩的%Y
----
2022-02-02


statement error 1006
select date_format('2022-2-04T03:58:59', '%i');

statement error 1006
select date_format('', '');

Expand Down Expand Up @@ -1486,3 +1490,5 @@ query T
SELECT add_hours(to_timestamp(710455), 2147483647);
----
1000-01-01 00:00:00.000000


0 comments on commit fd2f24a

Please sign in to comment.