From 4a277c21ef7eeae9c0d0b25d12c7359abc1f6236 Mon Sep 17 00:00:00 2001 From: "Lei, HUANG" <6406592+v0y4g3r@users.noreply.github.com> Date: Mon, 3 Mar 2025 20:32:19 +0800 Subject: [PATCH] fix: properly display CJK characters in table/column comments (#5633) fix/comment-in-cjk: ### Update `OptionMap` Formatting and Add Tests - **Enhancements in `OptionMap`**: - Changed formatting from `escape_default` to `escape_debug` for better handling of special characters in `src/sql/src/statements/option_map.rs`. - Added unit tests to verify the new formatting behavior. - **Test Cases for CJK Comments**: - Added test cases for tables with comments in CJK (Chinese, Japanese, Korean) characters in `tests/cases/standalone/common/show/show_create.sql` and `show_create.result`. --- src/sql/src/statements/option_map.rs | 24 +++++++++++++-- .../standalone/common/show/show_create.result | 29 +++++++++++++++++++ .../standalone/common/show/show_create.sql | 11 +++++++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/sql/src/statements/option_map.rs b/src/sql/src/statements/option_map.rs index 6e758c19b2..2b4b4e771b 100644 --- a/src/sql/src/statements/option_map.rs +++ b/src/sql/src/statements/option_map.rs @@ -82,9 +82,9 @@ impl OptionMap { let mut result = Vec::with_capacity(self.options.len() + self.secrets.len()); for (k, v) in self.options.iter() { if k.contains(".") { - result.push(format!("'{k}' = '{}'", v.escape_default())); + result.push(format!("'{k}' = '{}'", v.escape_debug())); } else { - result.push(format!("{k} = '{}'", v.escape_default())); + result.push(format!("{k} = '{}'", v.escape_debug())); } } for (k, _) in self.secrets.iter() { @@ -154,3 +154,23 @@ impl VisitMut for OptionMap { ControlFlow::Continue(()) } } + +#[cfg(test)] +mod tests { + use crate::statements::OptionMap; + + #[test] + fn test_format() { + let mut map = OptionMap::default(); + map.insert("comment".to_string(), "中文comment".to_string()); + assert_eq!("comment = '中文comment'", map.kv_pairs()[0]); + + let mut map = OptionMap::default(); + map.insert("a.b".to_string(), "中文comment".to_string()); + assert_eq!("'a.b' = '中文comment'", map.kv_pairs()[0]); + + let mut map = OptionMap::default(); + map.insert("a.b".to_string(), "中文comment\n".to_string()); + assert_eq!("'a.b' = '中文comment\\n'", map.kv_pairs()[0]); + } +} diff --git a/tests/cases/standalone/common/show/show_create.result b/tests/cases/standalone/common/show/show_create.result index 47a14926a7..ddbdd4179a 100644 --- a/tests/cases/standalone/common/show/show_create.result +++ b/tests/cases/standalone/common/show/show_create.result @@ -392,3 +392,32 @@ drop table test_column_constrain_composite_indexes; Affected Rows: 0 +CREATE TABLE `table_comment_in_cjk` ( + `ts` TIMESTAMP(3) NOT NULL COMMENT '时间戳', + `val` DOUBLE NULL COMMENT '值', + TIME INDEX ("ts"), +) WITH (comment = '你好\nこんにちは\n안녕하세요'); + +Affected Rows: 0 + +show create table table_comment_in_cjk; + ++----------------------+-----------------------------------------------------+ +| Table | Create Table | ++----------------------+-----------------------------------------------------+ +| table_comment_in_cjk | CREATE TABLE IF NOT EXISTS "table_comment_in_cjk" ( | +| | "ts" TIMESTAMP(3) NOT NULL COMMENT '时间戳', | +| | "val" DOUBLE NULL COMMENT '值', | +| | TIME INDEX ("ts") | +| | ) | +| | | +| | ENGINE=mito | +| | WITH( | +| | comment = '你好\\nこんにちは\\n안녕하세요' | +| | ) | ++----------------------+-----------------------------------------------------+ + +drop table table_comment_in_cjk; + +Affected Rows: 0 + diff --git a/tests/cases/standalone/common/show/show_create.sql b/tests/cases/standalone/common/show/show_create.sql index a0791caa20..0521508d48 100644 --- a/tests/cases/standalone/common/show/show_create.sql +++ b/tests/cases/standalone/common/show/show_create.sql @@ -153,3 +153,14 @@ CREATE TABLE test_column_constrain_composite_indexes ( show create table test_column_constrain_composite_indexes; drop table test_column_constrain_composite_indexes; + + +CREATE TABLE `table_comment_in_cjk` ( + `ts` TIMESTAMP(3) NOT NULL COMMENT '时间戳', + `val` DOUBLE NULL COMMENT '值', + TIME INDEX ("ts"), +) WITH (comment = '你好\nこんにちは\n안녕하세요'); + +show create table table_comment_in_cjk; + +drop table table_comment_in_cjk;