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(parser)!: Parse VALUES & query modifiers in wrapped FROM clause #4135

Merged
merged 2 commits into from
Sep 19, 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: 2 additions & 1 deletion sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2002,7 +2002,8 @@ def values_sql(self, expression: exp.Values, values_as_table: bool = True) -> st
values = f"VALUES{self.seg('')}{args}"
values = (
f"({values})"
if self.WRAP_DERIVED_VALUES and (alias or isinstance(expression.parent, exp.From))
if self.WRAP_DERIVED_VALUES
and (alias or isinstance(expression.parent, (exp.From, exp.Table)))
else values
)
return f"{values} AS {alias}" if alias else values
Expand Down
7 changes: 7 additions & 0 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2984,6 +2984,13 @@ def _parse_select(
if table
else self._parse_select(nested=True, parse_set_operation=False)
)

# Transform exp.Values into a exp.Table to pass through parse_query_modifiers
# in case a modifier (e.g. join) is following
if table and isinstance(this, exp.Values) and this.alias:
alias = this.args["alias"].pop()
this = exp.Table(this=this, alias=alias)
tobymao marked this conversation as resolved.
Show resolved Hide resolved

this = self._parse_query_modifiers(self._parse_set_operations(this))

self._match_r_paren()
Expand Down
3 changes: 3 additions & 0 deletions tests/dialects/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,9 @@ def test_postgres(self):
self.validate_identity(
"MERGE INTO target_table USING source_table AS source ON target.id = source.id WHEN MATCHED THEN DO NOTHING WHEN NOT MATCHED THEN DO NOTHING RETURNING MERGE_ACTION(), *"
)
self.validate_identity(
"SELECT 1 FROM ((VALUES (1)) AS vals(id) LEFT OUTER JOIN tbl ON vals.id = tbl.id)"
)

def test_ddl(self):
# Checks that user-defined types are parsed into DataType instead of Identifier
Expand Down
Loading