diff --git a/src/ast/ddl.rs b/src/ast/ddl.rs index 7b583ca746..7a44154958 100644 --- a/src/ast/ddl.rs +++ b/src/ast/ddl.rs @@ -2170,6 +2170,8 @@ pub enum ColumnOption { CharacterSet(ObjectName), /// `COLLATE ` column option Collation(ObjectName), + /// `BINARY` column attribute, such as in MySQL's `CHAR(1) BINARY` + Binary, /// `COMMENT ''` column option Comment(String), /// `ON UPDATE ` column option @@ -2327,6 +2329,7 @@ impl fmt::Display for ColumnOption { DialectSpecific(val) => write!(f, "{}", display_separated(val, " ")), CharacterSet(n) => write!(f, "CHARACTER SET {n}"), Collation(n) => write!(f, "COLLATE {n}"), + Binary => write!(f, "BINARY"), Comment(v) => write!(f, "COMMENT '{}'", escape_single_quote_string(v)), OnUpdate(expr) => write!(f, "ON UPDATE {expr}"), Generated { diff --git a/src/ast/spans.rs b/src/ast/spans.rs index 2472a3a6a3..69b7e7d0b0 100644 --- a/src/ast/spans.rs +++ b/src/ast/spans.rs @@ -905,6 +905,7 @@ impl Spanned for ColumnOption { ColumnOption::DialectSpecific(_) => Span::empty(), ColumnOption::CharacterSet(object_name) => object_name.span(), ColumnOption::Collation(object_name) => object_name.span(), + ColumnOption::Binary => Span::empty(), ColumnOption::Comment(_) => Span::empty(), ColumnOption::OnUpdate(expr) => expr.span(), ColumnOption::Generated { .. } => Span::empty(), diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs index 5f936951bd..0128e4f8f4 100644 --- a/src/dialect/mod.rs +++ b/src/dialect/mod.rs @@ -1914,6 +1914,16 @@ pub trait Dialect: Debug + Any { false } + /// Returns true if the dialect supports the `BINARY` attribute after a + /// character column type. + /// + /// For example: ```CREATE TABLE t (c CHAR(1) BINARY);```. + /// + /// [MySQL](https://dev.mysql.com/doc/refman/8.0/en/string-type-syntax.html) + fn supports_binary_column_attribute(&self) -> bool { + false + } + /// Returns true if the dialect supports PostgreSQL-style ordering operators: /// `ORDER BY expr USING `. /// diff --git a/src/dialect/mysql.rs b/src/dialect/mysql.rs index 95bd27168e..2f5efcec8e 100644 --- a/src/dialect/mysql.rs +++ b/src/dialect/mysql.rs @@ -38,6 +38,10 @@ const RESERVED_FOR_TABLE_ALIAS_MYSQL: &[Keyword] = &[ pub struct MySqlDialect {} impl Dialect for MySqlDialect { + fn supports_binary_column_attribute(&self) -> bool { + true + } + /// Only select-list aliases may use string quoting, see /// fn supports_string_literal_table_alias(&self, _explicit: bool) -> bool { diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 07e6a03c86..1ac9226089 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -10547,6 +10547,10 @@ impl<'a> Parser<'a> { Ok(Some(ColumnOption::Collation( self.parse_object_name(false)?, ))) + } else if self.dialect.supports_binary_column_attribute() + && self.parse_keyword(Keyword::BINARY) + { + Ok(Some(ColumnOption::Binary)) } else if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) { Ok(Some(ColumnOption::NotNull)) } else if self.parse_keywords(&[Keyword::COMMENT]) { diff --git a/tests/sqlparser_mysql.rs b/tests/sqlparser_mysql.rs index 3dc17d8feb..61c5a751d1 100644 --- a/tests/sqlparser_mysql.rs +++ b/tests/sqlparser_mysql.rs @@ -5372,3 +5372,25 @@ fn test_index_constraint_multiple_index_options() { ParserError::ParserError("Expected: a data type name, found: 'lookup'".to_string()) ); } + +#[test] +fn parse_create_table_column_binary_attribute() { + match mysql().verified_stmt("CREATE TABLE t (c CHAR(1) BINARY)") { + Statement::CreateTable(CreateTable { columns, .. }) => { + assert_eq!( + columns[0].data_type, + DataType::Char(Some(CharacterLength::IntegerLength { + length: 1, + unit: None, + })) + ); + assert_eq!(columns[0].options[0].option, ColumnOption::Binary); + } + _ => unreachable!(), + } + mysql().verified_stmt("CREATE TABLE t (c VARCHAR(10) BINARY NOT NULL)"); + mysql().verified_stmt("CREATE TABLE t (c TEXT CHARACTER SET utf8mb4 BINARY)"); + all_dialects_where(|d| !d.supports_binary_column_attribute()) + .parse_sql_statements("CREATE TABLE t (c CHAR(1) BINARY)") + .unwrap_err(); +}