Skip to content

Commit 68861d9

Browse files
authored
feat(mysql)!: support DROP PRIMARY KEY. (#7530)
* feat(mysql): support `DROP PRIMARY KEY`. This commit adds support for `ALTER TABLE t DROP PRIMARY KEY` for MySQL. I followed the general pattern of `DropPartition`, as it looked most extensible to other database vendors (eg. BigQuery, Oracle, Snowflake, Databricks) based on a sampling of syntax. * fix(parser): Move `DROP PRIMARY KEY` into MySQL. In my previous commit, I put the `DROP PRIMARY KEY` into the general parser, but that caused regressions for non-MySQL dialects. This commit tries to bridge the gap: keep the extensibility open for other dialects to use the `DropPrimaryKey`, but only when implemented.
1 parent f3bf070 commit 68861d9

6 files changed

Lines changed: 22 additions & 1 deletion

File tree

sqlglot/expressions/ddl.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ def kind(self) -> str | None:
354354
return kind and kind.upper()
355355

356356

357+
class DropPrimaryKey(Expression):
358+
arg_types = {}
359+
360+
357361
class Command(Expression):
358362
arg_types = {"this": True, "expression": False}
359363

sqlglot/generator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4041,6 +4041,9 @@ def droppartition_sql(self, expression: exp.DropPartition) -> str:
40414041
exists = " IF EXISTS " if expression.args.get("exists") else " "
40424042
return f"DROP{exists}{expressions}"
40434043

4044+
def dropprimarykey_sql(self, expression: exp.DropPrimaryKey) -> str:
4045+
return "DROP PRIMARY KEY"
4046+
40444047
def addconstraint_sql(self, expression: exp.AddConstraint) -> str:
40454048
return f"ADD {self.expressions(expression, indent=False)}"
40464049

sqlglot/parser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8503,6 +8503,9 @@ def _parse_drop_column(self) -> exp.Drop | exp.Command | None:
85038503
drop.set("kind", drop.args.get("kind", "COLUMN"))
85048504
return drop
85058505

8506+
def _parse_alter_drop_action(self) -> exp.Expr | None:
8507+
return self._parse_drop_column()
8508+
85068509
# https://docs.aws.amazon.com/athena/latest/ug/alter-table-drop-partition.html
85078510
def _parse_drop_partition(self, exists: bool | None = None) -> exp.DropPartition:
85088511
return self.expression(
@@ -8613,7 +8616,7 @@ def _parse_alter_table_drop(self) -> list[exp.Expr]:
86138616
return self._parse_csv(lambda: self._parse_drop_partition(exists=partition_exists))
86148617

86158618
self._retreat(index)
8616-
return self._parse_csv(self._parse_drop_column)
8619+
return self._parse_csv(self._parse_alter_drop_action)
86178620

86188621
def _parse_alter_table_rename(self) -> exp.AlterRename | exp.RenameColumn | None:
86198622
if self._match(TokenType.COLUMN) or not self.ALTER_RENAME_REQUIRES_COLUMN:

sqlglot/parsers/mysql.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ def _parse_alter_table_rename(self):
310310
return self.expression(exp.RenameIndex(this=old, to=new))
311311
return super()._parse_alter_table_rename()
312312

313+
def _parse_alter_drop_action(self) -> exp.Expr | None:
314+
if self._match_pair(TokenType.DROP, TokenType.PRIMARY_KEY):
315+
return self.expression(exp.DropPrimaryKey())
316+
return super()._parse_alter_drop_action()
317+
313318
def _parse_generated_as_identity(
314319
self,
315320
) -> (

tests/dialects/test_bigquery.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ def test_bigquery(self):
202202
self.validate_identity("BEGIN DECLARE y INT64", check_command_warning=True)
203203
self.validate_identity("LOOP SET x = x + 1", check_command_warning=True)
204204
self.validate_identity("REPEAT SET x = x + 1", check_command_warning=True)
205+
self.validate_identity(
206+
"ALTER TABLE foo DROP PRIMARY KEY IF EXISTS",
207+
check_command_warning=True,
208+
)
205209
self.validate_identity("SELECT MAKE_INTERVAL(100, 11, 1, 12, 30, 10)")
206210
self.validate_identity(
207211
"WHILE i < ARRAY_LENGTH(batches) DO SET x = batches[OFFSET(i)]",

tests/dialects/test_mysql.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def test_ddl(self):
3535
self.validate_identity("ALTER TABLE t1 ADD COLUMN x INT, ALGORITHM=INPLACE, LOCK=EXCLUSIVE")
3636
self.validate_identity("ALTER TABLE t ADD INDEX `i` (`c`)")
3737
self.validate_identity("ALTER TABLE t ADD UNIQUE `i` (`c`)")
38+
self.validate_identity("ALTER TABLE t DROP PRIMARY KEY")
39+
self.validate_identity("ALTER TABLE t DROP COLUMN c, DROP PRIMARY KEY, DROP INDEX `i`")
3840
self.validate_identity("ALTER TABLE test_table MODIFY COLUMN test_column LONGTEXT")
3941
self.validate_identity("ALTER TABLE t AUTO_INCREMENT=3000000000")
4042
self.validate_identity("ALTER VIEW v AS SELECT a, b, c, d FROM foo")

0 commit comments

Comments
 (0)