Repository: databendlabs/databend (language: rust)

Issue:
Bug: `date_format` function with invalid `format` argument panic
### Search before asking

- [x] I had searched in the [issues](https://github.com/databendlabs/databend/issues) and found no similar issues.


### Version

main

### What's Wrong?

`date_format` function with invalid `format` argument panic, seems caused by `chrono` bug
this bug is found by Tidb test
```
panicked at /home/b41sh/.rustup/toolchains/nightly-2024-11-28-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/string.rs:2688:14:
a Display implementation returned an error unexpectedly: Error
   0: backtrace::backtrace::libunwind::trace
             at /home/b41sh/.cargo/git/checkouts/backtrace-rs-fb1f822361417489/72265be/src/backtrace/libunwind.rs:116:5
      backtrace::backtrace::trace_unsynchronized
             at /home/b41sh/.cargo/git/checkouts/backtrace-rs-fb1f822361417489/72265be/src/backtrace/mod.rs:66:5
      backtrace::backtrace::trace
```

### How to Reproduce?

```sql
root@0.0.0.0:48000/default> SELECT date_format(now(), '%i');
error: APIError: QueryFailed: [1104]a Display implementation returned an error unexpectedly: Error

root@0.0.0.0:48000/default> SELECT date_format(now(), '%O');
error: APIError: QueryFailed: [1104]a Display implementation returned an error unexpectedly: Error
```

### Are you willing to submit PR?

- [ ] Yes I am willing to submit a PR!


Discussion / hints:

Fix patch:
diff --git a/src/query/functions/src/scalars/timestamp/src/datetime.rs b/src/query/functions/src/scalars/timestamp/src/datetime.rs
index 32ad99f1f273f..0c77e7404b2e6 100644
--- a/src/query/functions/src/scalars/timestamp/src/datetime.rs
+++ b/src/query/functions/src/scalars/timestamp/src/datetime.rs
@@ -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::*;
@@ -700,18 +701,22 @@ 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 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);
                     }
-                    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);
                 }
             },
         ),


Test patch:
diff --git a/tests/sqllogictests/suites/query/functions/02_0012_function_datetimes.test b/tests/sqllogictests/suites/query/functions/02_0012_function_datetimes.test
index e76537179a32b..ad7668efcedb0 100644
--- a/tests/sqllogictests/suites/query/functions/02_0012_function_datetimes.test
+++ b/tests/sqllogictests/suites/query/functions/02_0012_function_datetimes.test
@@ -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('', '');
 
@@ -1486,3 +1490,5 @@ query T
 SELECT add_hours(to_timestamp(710455), 2147483647);
 ----
 1000-01-01 00:00:00.000000
+
+