From 6c5e6a2735d952fcac6d615079f7cc783a870e92 Mon Sep 17 00:00:00 2001 From: George Sittas Date: Tue, 26 Nov 2024 16:58:29 +0200 Subject: [PATCH] Fix: parse DEFALUT in VALUES clause into a Var --- sqlglot/dialects/dialect.py | 3 +++ sqlglot/dialects/presto.py | 1 + sqlglot/parser.py | 7 ++++++- tests/dialects/test_mysql.py | 7 +++++++ 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/sqlglot/dialects/dialect.py b/sqlglot/dialects/dialect.py index 5b12f56c0..eca2bf592 100644 --- a/sqlglot/dialects/dialect.py +++ b/sqlglot/dialects/dialect.py @@ -411,6 +411,9 @@ class Dialect(metaclass=_Dialect): is cast to x's type to match it instead. """ + SUPPORTS_VALUES_DEFAULT = True + """Whether the DEFAULT keyword is supported in the VALUES clause.""" + REGEXP_EXTRACT_DEFAULT_GROUP = 0 """The default value for the capturing group.""" diff --git a/sqlglot/dialects/presto.py b/sqlglot/dialects/presto.py index ec702a097..056d974c2 100644 --- a/sqlglot/dialects/presto.py +++ b/sqlglot/dialects/presto.py @@ -198,6 +198,7 @@ class Presto(Dialect): TYPED_DIVISION = True TABLESAMPLE_SIZE_IS_PERCENT = True LOG_BASE_FIRST: t.Optional[bool] = None + SUPPORTS_VALUES_DEFAULT = False TIME_MAPPING = MySQL.TIME_MAPPING diff --git a/sqlglot/parser.py b/sqlglot/parser.py index 2a477d9b9..12419e8c2 100644 --- a/sqlglot/parser.py +++ b/sqlglot/parser.py @@ -2946,8 +2946,13 @@ def _parse_partition(self) -> t.Optional[exp.Partition]: ) def _parse_value(self) -> t.Optional[exp.Tuple]: + def _parse_value_expression() -> t.Optional[exp.Expression]: + if self.dialect.SUPPORTS_VALUES_DEFAULT and self._match(TokenType.DEFAULT): + return exp.var(self._prev.text.upper()) + return self._parse_expression() + if self._match(TokenType.L_PAREN): - expressions = self._parse_csv(self._parse_expression) + expressions = self._parse_csv(_parse_value_expression) self._match_r_paren() return self.expression(exp.Tuple, expressions=expressions) diff --git a/tests/dialects/test_mysql.py b/tests/dialects/test_mysql.py index fd6b36f1b..9e5b74e0b 100644 --- a/tests/dialects/test_mysql.py +++ b/tests/dialects/test_mysql.py @@ -118,6 +118,13 @@ def test_ddl(self): "CREATE TABLE `foo` (a VARCHAR(10), INDEX idx_a (a DESC))", ) + self.validate_all( + "insert into t(i) values (default)", + write={ + "duckdb": "INSERT INTO t (i) VALUES (DEFAULT)", + "mysql": "INSERT INTO t (i) VALUES (DEFAULT)", + }, + ) self.validate_all( "CREATE TABLE t (id INT UNSIGNED)", write={