Skip to content

Commit 77b87d5

Browse files
author
lin.zhang
committed
fix(starrocks): exp.Unnest transpilation
1 parent c1ac987 commit 77b87d5

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

sqlglot/dialects/starrocks.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ def _parse_unnest(self, with_alias: bool = True) -> t.Optional[exp.Unnest]:
3535
if unnest:
3636
alias = unnest.args.get("alias")
3737

38+
if not alias:
39+
# Starrocks defaults to naming the table alias as "unnest"
40+
alias = exp.TableAlias(
41+
this=exp.to_identifier("unnest"), columns=[exp.to_identifier("unnest")]
42+
)
43+
unnest.set("alias", alias)
3844
if alias and not alias.args.get("columns"):
3945
# Starrocks defaults to naming the UNNEST column as "unnest"
4046
# if it's not otherwise specified

tests/dialects/test_starrocks.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,29 @@ def test_unnest(self):
3535
"SELECT student, score, t.unnest FROM tests CROSS JOIN LATERAL UNNEST(scores) AS t",
3636
"SELECT student, score, t.unnest FROM tests CROSS JOIN LATERAL UNNEST(scores) AS t(unnest)",
3737
)
38+
self.validate_identity(
39+
"SELECT student, score, unnest.unnest FROM tests CROSS JOIN LATERAL UNNEST(scores)",
40+
"SELECT student, score, unnest.unnest FROM tests CROSS JOIN LATERAL UNNEST(scores) AS unnest(unnest)",
41+
)
42+
self.validate_all(
43+
r"""SELECT * FROM UNNEST(array['John','Jane','Jim','Jamie'], array[24,25,26,27]) AS t(name, age)""",
44+
write={
45+
"postgres": "SELECT * FROM UNNEST(ARRAY['John', 'Jane', 'Jim', 'Jamie'], ARRAY[24, 25, 26, 27]) AS t(name, age)",
46+
"spark": "SELECT * FROM EXPLODE(ARRAY('John', 'Jane', 'Jim', 'Jamie'), ARRAY(24, 25, 26, 27)) AS "
47+
"t(name, age)",
48+
},
49+
)
50+
# Use UNNEST to convert into multiple columns
51+
# see: https://docs.starrocks.io/docs/sql-reference/sql-functions/array-functions/unnest/
52+
self.validate_all(
53+
r"""SELECT id, t.type, t.scores FROM example_table, unnest(split(type, ";"), scores) AS t(type,scores)""",
54+
write={
55+
"postgres": "SELECT id, t.type, t.scores FROM example_table, UNNEST(SPLIT(type, ';'), scores) AS "
56+
"t(type, scores)",
57+
"spark": "SELECT id, t.type, t.scores FROM example_table LATERAL VIEW EXPLODE(SPLIT(type, CONCAT"
58+
r"""('\\Q', ';'))) t AS type LATERAL VIEW EXPLODE(scores) t AS scores""",
59+
},
60+
)
3861

3962
lateral_explode_sqls = [
4063
"SELECT id, t.col FROM tbl, UNNEST(scores) AS t(col)",

0 commit comments

Comments
 (0)