Skip to content

Commit 856faa9

Browse files
Feat(mysql): add support for ALTER INDEX (#4800)
* Support MySQL's `ALTER INDEX`. MySQL 8.0 and above [support] altering an existinq index, `x`, using the syntax, `ALTER TABLE x ALTER INDEX {VISIBLE | INVISIBLE}`. This allows for the index to become either invisible _or_ visible. No other index changes are permitted. This commit adds support for such syntax. [support]: https://dev.mysql.com/doc/refman/8.0/en/alter-table.html * Move `None` initialization to `else` branch * Fix return type hint * Apply styling change --------- Co-authored-by: Jo <[email protected]>
1 parent 575c979 commit 856faa9

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

sqlglot/dialects/mysql.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,11 @@ class Parser(parser.Parser):
439439
"MODIFY": lambda self: self._parse_alter_table_alter(),
440440
}
441441

442+
ALTER_ALTER_PARSERS = {
443+
**parser.Parser.ALTER_ALTER_PARSERS,
444+
"INDEX": lambda self: self._parse_alter_table_alter_index(),
445+
}
446+
442447
SCHEMA_UNNAMED_CONSTRAINTS = {
443448
*parser.Parser.SCHEMA_UNNAMED_CONSTRAINTS,
444449
"FULLTEXT",
@@ -693,6 +698,18 @@ def _parse_json_value(self) -> exp.JSONValue:
693698
on_condition=self._parse_on_condition(),
694699
)
695700

701+
def _parse_alter_table_alter_index(self) -> exp.AlterIndex:
702+
index = self._parse_field(any_token=True)
703+
704+
if self._match_text_seq("VISIBLE"):
705+
visible = True
706+
elif self._match_text_seq("INVISIBLE"):
707+
visible = False
708+
else:
709+
visible = None
710+
711+
return self.expression(exp.AlterIndex, this=index, visible=visible)
712+
696713
class Generator(generator.Generator):
697714
INTERVAL_ALLOWS_PLURAL_FORM = False
698715
LOCKING_READS_SUPPORTED = True

sqlglot/expressions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,6 +1706,11 @@ class AlterColumn(Expression):
17061706
}
17071707

17081708

1709+
# https://dev.mysql.com/doc/refman/8.0/en/invisible-indexes.html
1710+
class AlterIndex(Expression):
1711+
arg_types = {"this": True, "visible": True}
1712+
1713+
17091714
# https://docs.aws.amazon.com/redshift/latest/dg/r_ALTER_TABLE.html
17101715
class AlterDistStyle(Expression):
17111716
pass

sqlglot/generator.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3304,6 +3304,14 @@ def altercolumn_sql(self, expression: exp.AlterColumn) -> str:
33043304

33053305
return f"ALTER COLUMN {this} DROP DEFAULT"
33063306

3307+
def alterindex_sql(self, expression: exp.AlterIndex) -> str:
3308+
this = self.sql(expression, "this")
3309+
3310+
visible = expression.args.get("visible")
3311+
visible_sql = "VISIBLE" if visible else "INVISIBLE"
3312+
3313+
return f"ALTER INDEX {this} {visible_sql}"
3314+
33073315
def alterdiststyle_sql(self, expression: exp.AlterDistStyle) -> str:
33083316
this = self.sql(expression, "this")
33093317
if not isinstance(expression.this, exp.Var):

tests/dialects/test_mysql.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ def test_ddl(self):
150150
"sqlite": "CREATE TABLE x (id INTEGER NOT NULL AUTOINCREMENT PRIMARY KEY)",
151151
},
152152
)
153+
self.validate_identity("ALTER TABLE t ALTER INDEX i INVISIBLE")
154+
self.validate_identity("ALTER TABLE t ALTER INDEX i VISIBLE")
153155

154156
def test_identity(self):
155157
self.validate_identity("SELECT HIGH_PRIORITY STRAIGHT_JOIN SQL_CALC_FOUND_ROWS * FROM t")

0 commit comments

Comments
 (0)