Skip to content

Commit

Permalink
feat(trino): implement existing json functionality (#8963)
Browse files Browse the repository at this point in the history
Implement existing JSON functionality for the Trino backend.
  • Loading branch information
cpcloud authored Apr 15, 2024
1 parent ff2f73e commit 964ac3e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ibis/backends/tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_json_getitem_array(json_t):
@pytest.mark.notyet(["bigquery", "sqlite"], reason="doesn't support maps")
@pytest.mark.notyet(["postgres"], reason="only supports map<string, string>")
@pytest.mark.notyet(
["pyspark", "trino", "flink"], reason="should work but doesn't deserialize JSON"
["pyspark", "flink"], reason="should work but doesn't deserialize JSON"
)
def test_json_map(backend, json_t):
expr = json_t.js.map.name("res")
Expand All @@ -87,7 +87,7 @@ def test_json_map(backend, json_t):
@pytest.mark.notimpl(["dask", "mysql", "pandas", "risingwave"])
@pytest.mark.notyet(["sqlite"], reason="doesn't support arrays")
@pytest.mark.notyet(
["pyspark", "trino", "flink"], reason="should work but doesn't deserialize JSON"
["pyspark", "flink"], reason="should work but doesn't deserialize JSON"
)
@pytest.mark.notyet(["bigquery"], reason="doesn't allow null in arrays")
def test_json_array(backend, json_t):
Expand Down
20 changes: 20 additions & 0 deletions ibis/backends/trino/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,23 @@ def visit_RegexpExtract(self, op, *, arg, pattern, index):
# sqlglot doesn't support the third `group` argument for trino so work
# around that limitation using an anonymous function
return self.f.anon.regexp_extract(arg, pattern, index)

def visit_ToJSONMap(self, op, *, arg):
return self.cast(
self.f.json_parse(
self.f.json_query(
self.f.json_format(arg), 'strict $?($.type() == "object")'
)
),
dt.Map(dt.string, dt.json),
)

def visit_ToJSONArray(self, op, *, arg):
return self.cast(
self.f.json_parse(
self.f.json_query(
self.f.json_format(arg), 'strict $?($.type() == "array")'
)
),
dt.Array(dt.json),
)

0 comments on commit 964ac3e

Please sign in to comment.