Skip to content

Commit 2aff4ae

Browse files
authored
fix(duckdb): support parentheses with FROM-First syntax (#4569)
1 parent 2495508 commit 2aff4ae

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

sqlglot/parser.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3068,9 +3068,14 @@ def _parse_select(
30683068
is_unpivot=self._prev.token_type == TokenType.UNPIVOT
30693069
)
30703070
elif self._match(TokenType.FROM):
3071-
this = exp.select("*").from_(
3072-
t.cast(exp.From, self._parse_from(skip_from_token=True))
3073-
)
3071+
from_ = self._parse_from(skip_from_token=True)
3072+
# Support parentheses for duckdb FROM-first syntax
3073+
select = self._parse_select()
3074+
if select:
3075+
select.set("from", from_)
3076+
this = select
3077+
else:
3078+
this = exp.select("*").from_(t.cast(exp.From, from_))
30743079
else:
30753080
this = (
30763081
self._parse_table()

tests/dialects/test_duckdb.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,3 +1434,23 @@ def test_simplified_pivot_unpivot(self):
14341434
self.validate_identity(
14351435
"SELECT * FROM (UNPIVOT monthly_sales ON COLUMNS(* EXCLUDE (empid, dept)) INTO NAME month VALUE sales) AS unpivot_alias"
14361436
)
1437+
1438+
def test_from_first_with_parentheses(self):
1439+
self.validate_all(
1440+
"CREATE TABLE t1 AS (FROM t2 SELECT foo1, foo2)",
1441+
write={
1442+
"duckdb": "CREATE TABLE t1 AS (SELECT foo1, foo2 FROM t2)",
1443+
},
1444+
)
1445+
self.validate_all(
1446+
"FROM (FROM t1 SELECT foo1, foo2)",
1447+
write={
1448+
"duckdb": "SELECT * FROM (SELECT foo1, foo2 FROM t1)",
1449+
},
1450+
)
1451+
self.validate_all(
1452+
"WITH t1 AS (FROM (FROM t2 SELECT foo1, foo2)) FROM t1",
1453+
write={
1454+
"duckdb": "WITH t1 AS (SELECT * FROM (SELECT foo1, foo2 FROM t2)) SELECT * FROM t1",
1455+
},
1456+
)

0 commit comments

Comments
 (0)