From e62c50c449e557f84fa3970c783fa72c44e10080 Mon Sep 17 00:00:00 2001 From: Jo <46752250+GeorgeSittas@users.noreply.github.com> Date: Mon, 19 Jun 2023 16:44:11 +0300 Subject: [PATCH] Feat: add support for LIMIT clause in DELETE statement (#1804) --- sqlglot/expressions.py | 9 ++++++++- sqlglot/generator.py | 3 ++- sqlglot/parser.py | 1 + tests/dialects/test_mysql.py | 1 + 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/sqlglot/expressions.py b/sqlglot/expressions.py index 805a09dc2e..9660a099d3 100644 --- a/sqlglot/expressions.py +++ b/sqlglot/expressions.py @@ -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, diff --git a/sqlglot/generator.py b/sqlglot/generator.py index e9094ac7db..a63a5433d6 100644 --- a/sqlglot/generator.py +++ b/sqlglot/generator.py @@ -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: diff --git a/sqlglot/parser.py b/sqlglot/parser.py index ac562c745c..c6dd38e9f5 100644 --- a/sqlglot/parser.py +++ b/sqlglot/parser.py @@ -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: diff --git a/tests/dialects/test_mysql.py b/tests/dialects/test_mysql.py index 0b9c8b7269..2276743cca 100644 --- a/tests/dialects/test_mysql.py +++ b/tests/dialects/test_mysql.py @@ -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" )