Skip to content

MySQL create table options: optional DEFAULT keyword, CHARACTER SET longhand #1783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6946,8 +6946,12 @@ impl<'a> Parser<'a> {

let create_table_config = self.parse_optional_create_table_config()?;

let default_charset = if self.parse_keywords(&[Keyword::DEFAULT, Keyword::CHARSET]) {
self.expect_token(&Token::Eq)?;
let default_charset = if self.parse_keywords(&[Keyword::DEFAULT, Keyword::CHARSET])
|| self.parse_keywords(&[Keyword::DEFAULT, Keyword::CHARACTER, Keyword::SET])
|| self.parse_keywords(&[Keyword::CHARSET])
|| self.parse_keywords(&[Keyword::CHARACTER, Keyword::SET])
{
let _ = self.consume_token(&Token::Eq);
let next_token = self.next_token();
match next_token.token {
Token::Word(w) => Some(w.value),
Expand All @@ -6957,8 +6961,10 @@ impl<'a> Parser<'a> {
None
};

let collation = if self.parse_keywords(&[Keyword::COLLATE]) {
self.expect_token(&Token::Eq)?;
let collation = if self.parse_keywords(&[Keyword::COLLATE])
|| self.parse_keywords(&[Keyword::DEFAULT, Keyword::COLLATE])
{
let _ = self.consume_token(&Token::Eq);
let next_token = self.next_token();
match next_token.token {
Token::Word(w) => Some(w.value),
Expand Down
30 changes: 30 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,36 @@ fn parse_create_table_engine_default_charset() {
}
}

#[test]
fn parse_create_table_charset_collate() {
let sql =
"CREATE TABLE foo (id INT(11)) CHARACTER SET utf8mb3 DEFAULT COLLATE utf8mb4_0900_ai_ci";
let expected =
"CREATE TABLE foo (id INT(11)) DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb4_0900_ai_ci";
match mysql().one_statement_parses_to(sql, expected) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to switch this to use verified_stmt instead, we will need to replace both collate and charset fields in CreateTable with structs to capture the exact syntax used (was default keyword and/or assignment operator used? shorthand CHARSET instead of CHARACTER SET?).

Statement::CreateTable(CreateTable {
name,
columns,
collation,
default_charset,
..
}) => {
assert_eq!(name.to_string(), "foo");
assert_eq!(
vec![ColumnDef {
name: Ident::new("id"),
data_type: DataType::Int(Some(11)),
options: vec![],
},],
columns
);
assert_eq!(default_charset, Some("utf8mb3".to_string()));
assert_eq!(collation, Some("utf8mb4_0900_ai_ci".to_string()));
}
_ => unreachable!(),
}
}

#[test]
fn parse_create_table_collate() {
let sql = "CREATE TABLE foo (id INT(11)) COLLATE=utf8mb4_0900_ai_ci";
Expand Down