mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-19 21:50:19 +00:00
Support COLLATION
keywork on CREATE TABLE
(#424)
* support table definition's collate * add table collate test
This commit is contained in:
parent
b5f37118f1
commit
2ebe18a94e
3 changed files with 43 additions and 0 deletions
|
@ -758,6 +758,7 @@ pub enum Statement {
|
||||||
like: Option<ObjectName>,
|
like: Option<ObjectName>,
|
||||||
engine: Option<String>,
|
engine: Option<String>,
|
||||||
default_charset: Option<String>,
|
default_charset: Option<String>,
|
||||||
|
collation: Option<String>,
|
||||||
},
|
},
|
||||||
/// SQLite's `CREATE VIRTUAL TABLE .. USING <module_name> (<module_args>)`
|
/// SQLite's `CREATE VIRTUAL TABLE .. USING <module_name> (<module_args>)`
|
||||||
CreateVirtualTable {
|
CreateVirtualTable {
|
||||||
|
@ -1208,6 +1209,7 @@ impl fmt::Display for Statement {
|
||||||
like,
|
like,
|
||||||
default_charset,
|
default_charset,
|
||||||
engine,
|
engine,
|
||||||
|
collation,
|
||||||
} => {
|
} => {
|
||||||
// We want to allow the following options
|
// We want to allow the following options
|
||||||
// Empty column list, allowed by PostgreSQL:
|
// Empty column list, allowed by PostgreSQL:
|
||||||
|
@ -1339,6 +1341,9 @@ impl fmt::Display for Statement {
|
||||||
if let Some(default_charset) = default_charset {
|
if let Some(default_charset) = default_charset {
|
||||||
write!(f, " DEFAULT CHARSET={}", default_charset)?;
|
write!(f, " DEFAULT CHARSET={}", default_charset)?;
|
||||||
}
|
}
|
||||||
|
if let Some(collation) = collation {
|
||||||
|
write!(f, " COLLATE={}", collation)?;
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
Statement::CreateVirtualTable {
|
Statement::CreateVirtualTable {
|
||||||
|
|
|
@ -1560,6 +1560,7 @@ impl<'a> Parser<'a> {
|
||||||
like: None,
|
like: None,
|
||||||
default_charset: None,
|
default_charset: None,
|
||||||
engine: None,
|
engine: None,
|
||||||
|
collation: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1754,6 +1755,16 @@ impl<'a> Parser<'a> {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let collation = if self.parse_keywords(&[Keyword::COLLATE]) {
|
||||||
|
self.expect_token(&Token::Eq)?;
|
||||||
|
match self.next_token() {
|
||||||
|
Token::Word(w) => Some(w.value),
|
||||||
|
unexpected => self.expected("identifier", unexpected)?,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
Ok(Statement::CreateTable {
|
Ok(Statement::CreateTable {
|
||||||
name: table_name,
|
name: table_name,
|
||||||
temporary,
|
temporary,
|
||||||
|
@ -1773,6 +1784,7 @@ impl<'a> Parser<'a> {
|
||||||
like,
|
like,
|
||||||
engine,
|
engine,
|
||||||
default_charset,
|
default_charset,
|
||||||
|
collation,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -211,6 +211,32 @@ fn parse_create_table_engine_default_charset() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_create_table_collate() {
|
||||||
|
let sql = "CREATE TABLE foo (id INT(11)) COLLATE=utf8mb4_0900_ai_ci";
|
||||||
|
match mysql().verified_stmt(sql) {
|
||||||
|
Statement::CreateTable {
|
||||||
|
name,
|
||||||
|
columns,
|
||||||
|
collation,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
assert_eq!(name.to_string(), "foo");
|
||||||
|
assert_eq!(
|
||||||
|
vec![ColumnDef {
|
||||||
|
name: Ident::new("id"),
|
||||||
|
data_type: DataType::Int(Some(11)),
|
||||||
|
collation: None,
|
||||||
|
options: vec![],
|
||||||
|
},],
|
||||||
|
columns
|
||||||
|
);
|
||||||
|
assert_eq!(collation, Some("utf8mb4_0900_ai_ci".to_string()));
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_create_table_comment_character_set() {
|
fn parse_create_table_comment_character_set() {
|
||||||
let sql = "CREATE TABLE foo (s TEXT CHARACTER SET utf8mb4 COMMENT 'comment')";
|
let sql = "CREATE TABLE foo (s TEXT CHARACTER SET utf8mb4 COMMENT 'comment')";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue