diff --git a/fuzz/fuzz_seeds/mysql_index_option_comment_escape.sql b/fuzz/fuzz_seeds/mysql_index_option_comment_escape.sql new file mode 100644 index 0000000000..5aaa2af716 --- /dev/null +++ b/fuzz/fuzz_seeds/mysql_index_option_comment_escape.sql @@ -0,0 +1 @@ +CREATE TABLE t (a INT, INDEX idx (a) COMMENT 'a''b'); diff --git a/src/ast/ddl.rs b/src/ast/ddl.rs index 28d498f8aa..baf9faa962 100644 --- a/src/ast/ddl.rs +++ b/src/ast/ddl.rs @@ -1684,7 +1684,7 @@ impl fmt::Display for IndexOption { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Self::Using(index_type) => write!(f, "USING {index_type}"), - Self::Comment(s) => write!(f, "COMMENT '{s}'"), + Self::Comment(s) => write!(f, "COMMENT '{}'", escape_single_quote_string(s)), } } } diff --git a/tests/sqlparser_mysql.rs b/tests/sqlparser_mysql.rs index 3b274aed89..1577e99d06 100644 --- a/tests/sqlparser_mysql.rs +++ b/tests/sqlparser_mysql.rs @@ -5254,3 +5254,45 @@ fn parse_key_constraint_index_type_before_columns() { ); } } + +#[test] +fn test_index_option_comment_escape() { + match mysql_and_generic() + .verified_stmt("CREATE TABLE t (a INT, INDEX idx (a) COMMENT 'foo''bar')") + { + Statement::CreateTable(CreateTable { constraints, .. }) => match &constraints[..] { + [TableConstraint::Index(c)] => { + assert_eq!( + c.index_options, + vec![IndexOption::Comment("foo'bar".to_string())] + ); + } + _ => unreachable!(), + }, + _ => unreachable!(), + } + + match mysql_and_generic() + .verified_stmt("CREATE TABLE t (a INT, UNIQUE KEY uk (a) COMMENT 'it''s an ''index''')") + { + Statement::CreateTable(CreateTable { constraints, .. }) => match &constraints[..] { + [TableConstraint::Unique(c)] => { + assert_eq!( + c.index_options, + vec![IndexOption::Comment("it's an 'index'".to_string())] + ); + } + _ => unreachable!(), + }, + _ => unreachable!(), + } + + assert_eq!( + mysql_and_generic() + .parse_sql_statements("CREATE TABLE t (a INT, INDEX idx (a) COMMENT 'unterminated)") + .unwrap_err(), + ParserError::TokenizerError( + "Unterminated string literal at Line: 1, Column: 46".to_string() + ) + ); +}