Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: parse DEFAULT in VALUES clause into a Var #4448

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sqlglot/dialects/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down
1 change: 1 addition & 0 deletions sqlglot/dialects/presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both Presto and Trino treat DEFAULT as an identifier: I tried running the issue's INSERT and got an identifier resolution error back. Only those two out of the engines I tested had this behavior, the other engines treat DEFAULT as a special keyword in the context of a VALUES clause.


TIME_MAPPING = MySQL.TIME_MAPPING

Expand Down
7 changes: 6 additions & 1 deletion sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 7 additions & 0 deletions tests/dialects/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={
Expand Down