Skip to content

Commit 38b19c7

Browse files
tswastcpcloud
andauthored
feat: support JSON literal values (#11728)
## Description of changes Adds support for JSON literals to SQLGlot-based backends. ## Issues closed * Resolves #11727 --------- Co-authored-by: Phillip Cloud <417981+cpcloud@users.noreply.github.com>
1 parent 16a8f0f commit 38b19c7

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

ibis/backends/sql/compilers/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,8 @@ def visit_DefaultLiteral(self, op, *, value, dtype):
792792
)
793793
elif dtype.is_boolean():
794794
return sge.Boolean(this=bool(value))
795+
elif dtype.is_json():
796+
return sge.JSON(this=sge.convert(str(value)))
795797
elif dtype.is_string():
796798
return sge.convert(value)
797799
elif dtype.is_inet() or dtype.is_macaddr():

ibis/backends/tests/test_json.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import pytest
88
from packaging.version import parse as vparse
99

10+
import ibis
11+
import ibis.expr.datatypes as dt
1012
import ibis.expr.types as ir
1113
from ibis.backends.tests.conftest import NO_JSON_SUPPORT_MARKS
1214
from ibis.backends.tests.errors import PySparkPythonException
@@ -62,6 +64,21 @@ def test_json_getitem_array(json_t):
6264
assert result == expected
6365

6466

67+
@pytest.mark.notimpl(["polars"])
68+
@pytest.mark.notyet(
69+
["sqlite", "mysql", "pyspark"],
70+
reason="Syntax error near 'AS'",
71+
)
72+
@pytest.mark.notyet(
73+
["flink"],
74+
reason="Expecting alias, found character literal",
75+
)
76+
def test_json_literal(con):
77+
expr = ibis.literal('{"scale": 100}', dt.json).name("some_literal")
78+
result = con.execute(expr)
79+
assert result == {"scale": 100}
80+
81+
6582
@pytest.mark.notimpl(["mysql", "risingwave"])
6683
@pytest.mark.notyet(["bigquery", "sqlite"], reason="doesn't support maps")
6784
@pytest.mark.notyet(["postgres"], reason="only supports map<string, string>")

0 commit comments

Comments
 (0)