Skip to content

Commit 4123f60

Browse files
aparajonclaude
andcommitted
fix: accept VISIBLE/INVISIBLE index and column attributes in CREATE TABLE and CREATE INDEX
MySQL 8.0 invisible indexes (KEY idx (col) INVISIBLE) and invisible columns failed to parse inside CREATE TABLE, and CREATE INDEX ... INVISIBLE failed too, because CreateParameter() did not accept the K_VISIBLE/K_INVISIBLE tokens. The ALTER TABLE productions already support them (#2076 was fixed for ALTER TABLE only, via #2234); this covers the CREATE statements, which share CreateParameter(). Tests cover index and column visibility, mixing visibility with other index options in either order, and quoted columns named visible/invisible. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent f0ba123 commit 4123f60

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11046,6 +11046,7 @@ List<String> CreateParameter():
1104611046
| tk=<K_DESC> | tk=<K_TRUE> | tk=<K_FALSE> | tk=<K_PARALLEL> | tk=<K_BINARY> | tk=<K_START> | tk=<K_ORDER>
1104711047
| tk=<K_TIME_KEY_EXPR> | tk=<K_RAW> | tk=<K_HASH> | tk=<K_FIRST> | tk=<K_LAST> | tk = <K_SIGNED> | tk = <K_UNSIGNED>
1104811048
| tk=<K_ENGINE> | tk=<K_IDENTITY> | tk=<K_MATERIALIZED> | tk=<K_SAMPLE> | tk=<K_ALWAYS>
11049+
| tk=<K_VISIBLE> | tk=<K_INVISIBLE>
1104911050
| tk="="
1105011051
)
1105111052
{ param.add(tk.image); }

src/test/java/net/sf/jsqlparser/statement/create/CreateIndexTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,10 @@ public void testCreateIndexWithFunctionalKeyParts() throws JSQLParserException {
168168

169169
assertSqlCanBeParsedAndDeparsed(statement);
170170
}
171+
172+
@Test
173+
public void testCreateIndexVisibility() throws JSQLParserException {
174+
assertSqlCanBeParsedAndDeparsed("CREATE INDEX idx_a ON t1 (a) INVISIBLE", true);
175+
assertSqlCanBeParsedAndDeparsed("CREATE INDEX idx_a ON t1 (a) VISIBLE", true);
176+
}
171177
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,4 +1162,48 @@ void testWithCatalog() throws JSQLParserException {
11621162
assertEquals("session1", t.getSchemaName());
11631163
assertEquals("a", t.getUnquotedName());
11641164
}
1165+
1166+
@Test
1167+
void testCreateTableIndexVisibility() throws JSQLParserException {
1168+
String sqlStr = "CREATE TABLE `orders` (" +
1169+
"`id` bigint NOT NULL AUTO_INCREMENT" +
1170+
", `status` varchar (32) NOT NULL" +
1171+
", `quote_id` varchar (191) NOT NULL" +
1172+
", PRIMARY KEY (`id`)" +
1173+
", KEY `idx_status` (`status`) INVISIBLE" +
1174+
", UNIQUE KEY `idx_quote_id` (`quote_id`) VISIBLE" +
1175+
") ENGINE = InnoDB";
1176+
CreateTable createTable = (CreateTable) assertSqlCanBeParsedAndDeparsed(sqlStr, true);
1177+
1178+
assertEquals(Arrays.asList("INVISIBLE"), createTable.getIndexes().get(1).getIndexSpec());
1179+
assertEquals(Arrays.asList("VISIBLE"), createTable.getIndexes().get(2).getIndexSpec());
1180+
}
1181+
1182+
@Test
1183+
void testCreateTableColumnVisibility() throws JSQLParserException {
1184+
String sqlStr =
1185+
"CREATE TABLE t1 (id bigint NOT NULL VISIBLE, secret varchar (10) INVISIBLE)";
1186+
CreateTable createTable = (CreateTable) assertSqlCanBeParsedAndDeparsed(sqlStr, true);
1187+
1188+
assertEquals(Arrays.asList("NOT", "NULL", "VISIBLE"),
1189+
createTable.getColumnDefinitions().get(0).getColumnSpecs());
1190+
assertEquals(Arrays.asList("INVISIBLE"),
1191+
createTable.getColumnDefinitions().get(1).getColumnSpecs());
1192+
}
1193+
1194+
@Test
1195+
void testCreateTableIndexVisibilityWithOtherIndexOptions() throws JSQLParserException {
1196+
assertSqlCanBeParsedAndDeparsed(
1197+
"CREATE TABLE t1 (a int, KEY idx_a (a) INVISIBLE COMMENT 'retiring')", true);
1198+
assertSqlCanBeParsedAndDeparsed(
1199+
"CREATE TABLE t1 (a int, KEY idx_a (a) COMMENT 'retiring' INVISIBLE)", true);
1200+
assertSqlCanBeParsedAndDeparsed(
1201+
"CREATE TABLE t1 (a int, KEY idx_a (a) USING BTREE INVISIBLE)", true);
1202+
}
1203+
1204+
@Test
1205+
void testCreateTableQuotedColumnNamedVisible() throws JSQLParserException {
1206+
assertSqlCanBeParsedAndDeparsed(
1207+
"CREATE TABLE t1 (`visible` int, `invisible` varchar (10))", true);
1208+
}
11651209
}

0 commit comments

Comments
 (0)