Skip to content

Commit cb91d91

Browse files
burnisonVaggelisD
authored andcommitted
Feat(mysql): support ALTER TABLE ... RENAME INDEX. (#7511)
* Feat(mysql): support `ALTER TABLE ... RENAME INDEX`. MySQL 5.7 introduced `ALTER TABLE ... RENAME INDEX old TO new` (and the `RENAME KEY` synonym) as a metadata-only operation that renames an index without rebuilding it. * Update tests/dialects/test_mysql.py --------- Co-authored-by: Vaggelis Danias <daniasevangelos@gmail.com>
1 parent 930ff33 commit cb91d91

4 files changed

Lines changed: 22 additions & 0 deletions

File tree

sqlglot/expressions/ddl.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ class AlterRename(Expression):
279279
pass
280280

281281

282+
class RenameIndex(Expression):
283+
arg_types = {"this": True, "to": True}
284+
285+
282286
class AlterModifySqlSecurity(Expression):
283287
arg_types = {"expressions": True}
284288

sqlglot/generator.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5920,3 +5920,8 @@ def altermodifysqlsecurity_sql(self, expression: exp.AlterModifySqlSecurity) ->
59205920
def usingproperty_sql(self, expression: exp.UsingProperty) -> str:
59215921
kind = expression.args.get("kind")
59225922
return f"USING {kind} {self.sql(expression, 'this')}"
5923+
5924+
def renameindex_sql(self, expression: exp.RenameIndex) -> str:
5925+
this = self.sql(expression, "this")
5926+
to = self.sql(expression, "to")
5927+
return f"RENAME INDEX {this} TO {to}"

sqlglot/parsers/mysql.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,14 @@ class MySQLParser(parser.Parser):
301301
VALUES_FOLLOWED_BY_PAREN = False
302302
SUPPORTS_PARTITION_SELECTION = True
303303

304+
def _parse_alter_table_rename(self):
305+
if self._match_texts(("INDEX", "KEY")):
306+
old = self._parse_field(any_token=True)
307+
self._match_text_seq("TO")
308+
new = self._parse_field(any_token=True)
309+
return self.expression(exp.RenameIndex(this=old, to=new))
310+
return super()._parse_alter_table_rename()
311+
304312
def _parse_generated_as_identity(
305313
self,
306314
) -> (

tests/dialects/test_mysql.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ def test_ddl(self):
196196
self.validate_identity("ALTER TABLE t ALTER INDEX i VISIBLE")
197197
self.validate_identity("ALTER TABLE t ALTER COLUMN c SET INVISIBLE")
198198
self.validate_identity("ALTER TABLE t ALTER COLUMN c SET VISIBLE")
199+
self.validate_identity("ALTER TABLE t RENAME INDEX a TO b")
200+
self.validate_identity(
201+
"ALTER TABLE t RENAME KEY a TO b",
202+
"ALTER TABLE t RENAME INDEX a TO b",
203+
)
199204
self.validate_identity(
200205
"UPDATE foo JOIN bar ON TRUE SET foo.a = bar.a WHERE foo.id = bar.id"
201206
)

0 commit comments

Comments
 (0)