diff --git a/src/utils/pgwire/src/lib.rs b/src/utils/pgwire/src/lib.rs index d4018373db005..96d439b8b94ff 100644 --- a/src/utils/pgwire/src/lib.rs +++ b/src/utils/pgwire/src/lib.rs @@ -18,6 +18,7 @@ #![feature(trusted_len)] #![feature(lazy_cell)] #![feature(buf_read_has_data_left)] +#![feature(round_char_boundary)] #![expect(clippy::doc_markdown, reason = "FIXME: later")] pub mod error; diff --git a/src/utils/pgwire/src/pg_protocol.rs b/src/utils/pgwire/src/pg_protocol.rs index 98ad88ffcce25..ebffb24d81118 100644 --- a/src/utils/pgwire/src/pg_protocol.rs +++ b/src/utils/pgwire/src/pg_protocol.rs @@ -1139,8 +1139,9 @@ pub mod truncated_fmt { } if self.remaining < s.len() { - self.f.write_str(&s[0..self.remaining])?; - self.remaining = 0; + let actual = s.floor_char_boundary(self.remaining); + self.f.write_str(&s[0..actual])?; + self.remaining -= actual; self.f.write_str("...(truncated)")?; self.finished = true; // so that ...(truncated) is printed exactly once } else { @@ -1180,4 +1181,17 @@ pub mod truncated_fmt { .write_fmt(format_args!("{}", self.0)) } } + + #[cfg(test)] + mod tests { + use super::*; + + #[test] + fn test_trunc_utf8() { + assert_eq!( + format!("{}", TruncatedFmt(&"select '🌊';", 10)), + "select '...(truncated)", + ); + } + } }