-
Notifications
You must be signed in to change notification settings - Fork 592
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
Refactor!: improve transpilation of JSON paths across dialects #2883
Conversation
A possible improvement is to consolidate multiple >>> import sqlglot
>>> sqlglot.parse_one("x -> 'a' -> 'b' -> 'z'", "postgres")
JSONExtract(
this=JSONExtract(
this=JSONExtract(
this=Column(
this=Identifier(this=x, quoted=False)),
expression=JSONPath(
this=[
{'kind': 'root'},
{'kind': 'key', 'value': 'a'}])),
expression=JSONPath(
this=[
{'kind': 'root'},
{'kind': 'key', 'value': 'b'}])),
expression=JSONPath(
this=[
{'kind': 'root'},
{'kind': 'key', 'value': 'z'}]))
>>> sqlglot.parse_one("x -> 'a' -> 'b' -> 'z'", "postgres").sql("bigquery")
"JSON_EXTRACT(JSON_EXTRACT(JSON_EXTRACT(x, '$.a'), '$.b'), '$.z')"
>>> sqlglot.parse_one("x -> 'a' -> 'b' -> 'z'", "postgres").sql("duckdb")
"x -> '$.a' -> '$.b' -> '$.z'" |
FYI the only remaining dialect I need to refactor is Snowflake, but their path syntax is a bit weirder than the rest of the dialects that don't support the JSON path syntax as-is. DuckDB doesn't support the full spec of JSON path too and they seem to have augmented the path syntax with their own, for example the SELECT json_extract('{"duck": [1, 2, 3]}', '$.duck[#-1]'); Refer to the linked docs for more info. You can also have JSON Pointer syntax for DuckDB paths which will be left as-is. |
87182f0
to
a89aac8
Compare
be5216d
to
61a14fb
Compare
… JSONExtract instead
c6ae80a
to
a52a54a
Compare
Fixes #2839, still a WIP; opening for some feedback
References: