Skip to content

Commit

Permalink
Feat: add support for LIMIT clause in DELETE statement (#1804)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas authored Jun 19, 2023
1 parent 6945b28 commit e62c50c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
9 changes: 8 additions & 1 deletion sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,14 @@ class Constraint(Expression):


class Delete(Expression):
arg_types = {"with": False, "this": False, "using": False, "where": False, "returning": False}
arg_types = {
"with": False,
"this": False,
"using": False,
"where": False,
"returning": False,
"limit": False,
}

def delete(
self,
Expand Down
3 changes: 2 additions & 1 deletion sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,8 @@ def delete_sql(self, expression: exp.Delete) -> str:
)
where_sql = self.sql(expression, "where")
returning = self.sql(expression, "returning")
sql = f"DELETE{this}{using_sql}{where_sql}{returning}"
limit = self.sql(expression, "limit")
sql = f"DELETE{this}{using_sql}{where_sql}{returning}{limit}"
return self.prepend_ctes(expression, sql)

def drop_sql(self, expression: exp.Drop) -> str:
Expand Down
1 change: 1 addition & 0 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,7 @@ def _parse_delete(self) -> exp.Delete:
using=self._parse_csv(lambda: self._match(TokenType.USING) and self._parse_table()),
where=self._parse_where(),
returning=self._parse_returning(),
limit=self._parse_limit(),
)

def _parse_update(self) -> exp.Update:
Expand Down
1 change: 1 addition & 0 deletions tests/dialects/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class TestMySQL(Validator):
dialect = "mysql"

def test_ddl(self):
self.validate_identity("DELETE FROM t WHERE a <= 10 LIMIT 10")
self.validate_identity(
"INSERT INTO x VALUES (1, 'a', 2.0) ON DUPLICATE KEY UPDATE SET x.id = 1"
)
Expand Down

0 comments on commit e62c50c

Please sign in to comment.