Skip to content

Commit 32a7949

Browse files
authored
fix: allow UNIQUE INDEX in CREATE TABLE (#2457)
Accept an optional UNIQUE prefix on the INDEX branch of CreateTableConstraint, so MySQL-style CREATE TABLE ... UNIQUE INDEX name (cols) ... parses and round-trips. UNIQUE INDEX is disambiguated from UNIQUE KEY and the bare UNIQUE constraint by the LOOKAHEAD. Fixes #1893 Signed-off-by: 付典 <fudianchn@gmail.com>
1 parent d51ee1c commit 32a7949

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

‎src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10561,14 +10561,15 @@ Index CreateTableConstraint():
1056110561
}
1056210562
{
1056310563
(
10564-
LOOKAHEAD(3) (
10565-
{ idxSpec.clear(); }
10564+
LOOKAHEAD(4) (
10565+
{ idxSpec.clear(); tk3=null; }
10566+
[ tk3=<K_UNIQUE> ]
1056610567
tk=<K_INDEX>
1056710568
sk3=RelObjectName()
1056810569
colNames = IndexColumnsWithParamsList()
1056910570
( parameter=CreateParameter() { idxSpec.addAll(parameter); } )*
1057010571
{
10571-
index = new Index().withType(tk.image).withName(sk3).withColumns(colNames).withIndexSpec(new ArrayList<String>(idxSpec));
10572+
index = new Index().withType((tk3!=null ? tk3.image + " " : "") + tk.image).withName(sk3).withColumns(colNames).withIndexSpec(new ArrayList<String>(idxSpec));
1057210573
}
1057310574
)
1057410575
|

‎src/test/java/net/sf/jsqlparser/statement/create/CreateTableTest.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,4 +1206,15 @@ void testCreateTableQuotedColumnNamedVisible() throws JSQLParserException {
12061206
assertSqlCanBeParsedAndDeparsed(
12071207
"CREATE TABLE t1 (`visible` int, `invisible` varchar (10))", true);
12081208
}
1209+
1210+
@Test
1211+
void testUniqueIndexIssue1893() throws JSQLParserException {
1212+
// MySQL `UNIQUE INDEX name (cols) ...` was rejected (only INDEX / [UNIQUE] KEY were
1213+
// accepted).
1214+
assertSqlCanBeParsedAndDeparsed(
1215+
"CREATE TABLE t (a int, b int, UNIQUE INDEX idx (a, b) USING BTREE COMMENT 'unique index')",
1216+
true);
1217+
// A plain INDEX must still parse unchanged.
1218+
assertSqlCanBeParsedAndDeparsed("CREATE TABLE t (a int, INDEX idx (a))", true);
1219+
}
12091220
}

0 commit comments

Comments
 (0)