Skip to content

Commit 2e4ec7f

Browse files
committed
MySQL: Support comma-separated CREATE TABLE options
In [MySQL], options for `CREATE TABLE` following the body can be optionally separated by commas. I'm not aware of any cases where this affects parsing (e.g. eliminating ambiguity or anything), so we just optionally eat comma tokens after each option is parsed. [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/create-table.html
1 parent dd650b8 commit 2e4ec7f

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

src/dialect/generic.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,8 @@ impl Dialect for GenericDialect {
187187
fn supports_data_type_signed_suffix(&self) -> bool {
188188
true
189189
}
190+
191+
fn supports_comma_separated_create_table_options(&self) -> bool {
192+
true
193+
}
190194
}

src/dialect/mod.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ pub trait Dialect: Debug + Any {
590590
false
591591
}
592592

593-
/// Returne true if the dialect supports specifying multiple options
593+
/// Return true if the dialect supports specifying multiple options
594594
/// in a `CREATE TABLE` statement for the structure of the new table. For example:
595595
/// `CREATE TABLE t (a INT, b INT) AS SELECT 1 AS b, 2 AS a`
596596
fn supports_create_table_multi_schema_info_sources(&self) -> bool {
@@ -1148,6 +1148,21 @@ pub trait Dialect: Debug + Any {
11481148
fn supports_data_type_signed_suffix(&self) -> bool {
11491149
false
11501150
}
1151+
1152+
/// Returns true if this dialect allows table-level options for `CREATE TABLE` to be comma
1153+
/// separated, as does [MySQL].
1154+
///
1155+
/// Example:
1156+
/// ```sql
1157+
/// CREATE TABLE t (x INT) ENGINE=InnoDB, AUTO_INCREMENT=100;
1158+
/// -- equivalent to:
1159+
/// CREATE TABLE t (x INT) ENGINE=InnoDB AUTO_INCREMENT=100;
1160+
/// ```
1161+
///
1162+
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/create-table.html
1163+
fn supports_comma_separated_create_table_options(&self) -> bool {
1164+
false
1165+
}
11511166
}
11521167

11531168
/// This represents the operators for which precedence must be defined

src/dialect/mysql.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ impl Dialect for MySqlDialect {
158158
fn supports_data_type_signed_suffix(&self) -> bool {
159159
true
160160
}
161+
162+
fn supports_comma_separated_create_table_options(&self) -> bool {
163+
true
164+
}
161165
}
162166

163167
/// `LOCK TABLES`

src/parser/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7708,6 +7708,9 @@ impl<'a> Parser<'a> {
77087708

77097709
while let Some(option) = self.parse_plain_option()? {
77107710
options.push(option);
7711+
if self.dialect.supports_comma_separated_create_table_options() {
7712+
let _ = self.consume_token(&Token::Comma);
7713+
}
77117714
}
77127715

77137716
Ok(options)

tests/sqlparser_mysql.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,13 @@ fn parse_create_table_gencol() {
13611361
mysql_and_generic().verified_stmt("CREATE TABLE t1 (a INT, b INT AS (a * 2) STORED)");
13621362
}
13631363

1364+
#[test]
1365+
fn parse_create_table_options_comma_separated() {
1366+
let sql = "CREATE TABLE t (x INT) DEFAULT CHARSET = utf8mb4, ENGINE = InnoDB , AUTO_INCREMENT 1 DATA DIRECTORY '/var/lib/mysql/data'";
1367+
let canonical = "CREATE TABLE t (x INT) DEFAULT CHARSET = utf8mb4 ENGINE = InnoDB AUTO_INCREMENT = 1 DATA DIRECTORY = '/var/lib/mysql/data'";
1368+
mysql_and_generic().one_statement_parses_to(sql, canonical);
1369+
}
1370+
13641371
#[test]
13651372
fn parse_quote_identifiers() {
13661373
let sql = "CREATE TABLE `PRIMARY` (`BEGIN` INT PRIMARY KEY)";

0 commit comments

Comments
 (0)