Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2040,7 +2040,7 @@ impl fmt::Display for ColumnOption {
Ok(())
}
Unique(constraint) => {
write!(f, "UNIQUE")?;
write!(f, "UNIQUE{:>}", constraint.index_type_display)?;
if let Some(characteristics) = &constraint.characteristics {
write!(f, " {characteristics}")?;
}
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,8 @@ impl Dialect for GenericDialect {
fn supports_constraint_keyword_without_name(&self) -> bool {
true
}

fn supports_key_column_option(&self) -> bool {
true
}
}
12 changes: 12 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,18 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if the dialect supports the `KEY` keyword as part of
/// column-level constraints in a `CREATE TABLE` statement.
///
/// When enabled, the parser accepts these MySQL-specific column options:
/// - `UNIQUE [KEY]` — optional `KEY` after `UNIQUE`
/// - `[PRIMARY] KEY` — standalone `KEY` as shorthand for `PRIMARY KEY`
///
/// <https://dev.mysql.com/doc/refman/8.4/en/create-table.html>
fn supports_key_column_option(&self) -> bool {
false
}

/// Returns true if the specified keyword is reserved and cannot be
/// used as an identifier without special handling like quoting.
fn is_reserved_for_identifier(&self, kw: Keyword) -> bool {
Expand Down
5 changes: 5 additions & 0 deletions src/dialect/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ impl Dialect for MySqlDialect {
fn supports_constraint_keyword_without_name(&self) -> bool {
true
}

/// See: <https://dev.mysql.com/doc/refman/8.4/en/create-table.html>
fn supports_key_column_option(&self) -> bool {
true
}
}

/// `LOCK TABLES`
Expand Down
23 changes: 22 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9048,12 +9048,18 @@ impl<'a> Parser<'a> {
.into(),
))
} else if self.parse_keyword(Keyword::UNIQUE) {
let index_type_display =
if self.dialect.supports_key_column_option() && self.parse_keyword(Keyword::KEY) {
KeyOrIndexDisplay::Key
} else {
KeyOrIndexDisplay::None
};
let characteristics = self.parse_constraint_characteristics()?;
Ok(Some(
UniqueConstraint {
name: None,
index_name: None,
index_type_display: KeyOrIndexDisplay::None,
index_type_display,
index_type: None,
columns: vec![],
index_options: vec![],
Expand All @@ -9062,6 +9068,21 @@ impl<'a> Parser<'a> {
}
.into(),
))
} else if self.dialect.supports_key_column_option() && self.parse_keyword(Keyword::KEY) {
// In MySQL, `KEY` in a column definition is shorthand for `PRIMARY KEY`.
// See: https://dev.mysql.com/doc/refman/8.4/en/create-table.html
let characteristics = self.parse_constraint_characteristics()?;
Ok(Some(
PrimaryKeyConstraint {
name: None,
index_name: None,
index_type: None,
columns: vec![],
index_options: vec![],
characteristics,
}
.into(),
))
} else if self.parse_keyword(Keyword::REFERENCES) {
let foreign_table = self.parse_object_name(false)?;
// PostgreSQL allows omitting the column list and
Expand Down
9 changes: 9 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,15 @@ fn parse_create_table_primary_and_unique_key_characteristic_test() {
}
}

#[test]
fn parse_create_table_column_key_options() {
mysql_and_generic().verified_stmt("CREATE TABLE foo (x INT UNIQUE KEY)");
mysql_and_generic().one_statement_parses_to(
"CREATE TABLE foo (x INT KEY)",
"CREATE TABLE foo (x INT PRIMARY KEY)",
);
}

#[test]
fn parse_create_table_comment() {
let without_equal = "CREATE TABLE foo (bar INT) COMMENT 'baz'";
Expand Down