From 222913d652d39eedec4b698d44963dbc2b486622 Mon Sep 17 00:00:00 2001 From: LucaCappelletti94 Date: Sat, 31 Oct 2026 13:05:18 +0100 Subject: [PATCH] PostgreSQL: Escape quotes in COMMENT ON values --- src/ast/mod.rs | 2 +- tests/sqlparser_common.rs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 7dfd13d6b1..47cdfe56c7 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -6280,7 +6280,7 @@ impl fmt::Display for Statement { }; write!(f, "ON {object_type} {object_name} IS ")?; if let Some(c) = comment { - write!(f, "'{c}'") + write!(f, "'{}'", value::escape_single_quote_string(c)) } else { write!(f, "NULL") } diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 74693a468b..d267378747 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -23411,3 +23411,17 @@ fn display_create_procedure_begin_atomic_propagates_write_errors() { assert_eq!(write!(writer, "{stmt}"), Err(fmt::Error), "{rejected:?}"); } } + +#[test] +fn parse_comment_on_escaped_comment_value() { + let dialects = all_dialects_where(|d| d.supports_comment_on()); + + match dialects.verified_stmt("COMMENT ON COLUMN t.id IS 'Column ''id'' on t'") { + Statement::Comment { + comment: Some(comment), + .. + } => assert_eq!("Column 'id' on t", comment), + _ => unreachable!(), + } + dialects.verified_stmt("COMMENT ON SEQUENCE asdf IS 'won''t work'"); +}