Skip to content

Commit

Permalink
Fix: parse struct(...)[] type properly (#4123)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas authored Sep 14, 2024
1 parent bd8e050 commit 21f5bcd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4796,7 +4796,11 @@ def _parse_types(

if self._match_set((TokenType.L_BRACKET, TokenType.L_PAREN)):
values = self._parse_csv(self._parse_assignment)
self._match_set((TokenType.R_BRACKET, TokenType.R_PAREN))
if not values and is_struct:
values = None
self._retreat(self._index - 1)
else:
self._match_set((TokenType.R_BRACKET, TokenType.R_PAREN))

if type_token in self.TIMESTAMPS:
if self._match_text_seq("WITH", "TIME", "ZONE"):
Expand Down
10 changes: 10 additions & 0 deletions tests/dialects/test_duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ def test_duckdb(self):
"WITH _data AS (SELECT [STRUCT(1 AS a, 2 AS b), STRUCT(2 AS a, 3 AS b)] AS col) SELECT col.b FROM _data, UNNEST(_data.col) AS col WHERE col.a = 1",
)

struct_array_type = exp.maybe_parse(
"STRUCT(k TEXT, v STRUCT(v_str TEXT, v_int INT, v_int_arr INT[]))[]",
into=exp.DataType,
dialect="duckdb",
)
self.assertEqual(
struct_array_type.sql("duckdb"),
"STRUCT(k TEXT, v STRUCT(v_str TEXT, v_int INT, v_int_arr INT[]))[]",
)

self.validate_all(
"CAST(x AS UUID)",
write={
Expand Down

0 comments on commit 21f5bcd

Please sign in to comment.