-
Notifications
You must be signed in to change notification settings - Fork 763
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(Postgres): Support UNNEST #1761
Conversation
Is it possible to just map |
Oh, interesting, will try that! |
|
You have to map |
Thanks! |
@georgesittas That was neat! Thanks for the explanation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks way cleaner :)
Ah I just noticed something in postgres's docs: SELECT * FROM UNNEST(ARRAY[1,2], ARRAY['foo','bar','baz']) AS x(a,b) So this is actually breaking, because before we'd just parse this into an cc: @tobymao |
sure, please fix |
Ah, good catch! What does it entail to let it take more than one argument? Changing explode from having "this" to "expressions"? I won't have time to do that tonight, so feel free to do it if you have time. Otherwise happy to try tomorrow! |
No worries, I'll play around with it locally and let you know when I have something. |
Ha, this is actually already taken care of -- there might be no problem with this approach!
def _parse_table(
self, schema: bool = False, alias_tokens: t.Optional[t.Collection[TokenType]] = None
) -> t.Optional[exp.Expression]:
lateral = self._parse_lateral()
if lateral:
return lateral
unnest = self._parse_unnest()
if unnest:
return unnest
... This means that with this PR the parsing of multi-argument >>> import sqlglot
>>> sqlglot.parse_one("SELECT * FROM UNNEST(ARRAY[1, 2], ARRAY['foo', 'bar', 'baz']) AS x(a, b)")
(SELECT expressions:
(STAR ), from:
(FROM this:
(UNNEST expressions:
(ARRAY expressions:
(LITERAL this: 1, is_string: False),
(LITERAL this: 2, is_string: False)),
(ARRAY expressions:
(LITERAL this: foo, is_string: True),
(LITERAL this: bar, is_string: True),
(LITERAL this: baz, is_string: True)), alias:
(TABLEALIAS this:
(IDENTIFIER this: x, quoted: False), columns:
(IDENTIFIER this: a, quoted: False),
(IDENTIFIER this: b, quoted: False)))))
>>> sqlglot.parse_one("SELECT UNNEST(c) FROM t", "postgres")
(SELECT expressions:
(EXPLODE this:
(COLUMN this:
(IDENTIFIER this: c, quoted: False))), from:
(FROM this:
(TABLE this:
(IDENTIFIER this: t, quoted: False)))) There's only a (minor) issue, where Note: added a couple more tests. |
This was solved by tobymao/sqlglot#1761, which is included in SQLGlot 16.1.
* Add failing test * Add support for Postgres' unnest * Instead, map unnest to explode and generate appropriately * Remove outdated * Add more tests --------- Co-authored-by: Jo <[email protected]>
Fixes #1760.