mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-01-06 05:12:54 +00:00
feat: add respective json_is UDFs for JSON type (#4726)
* feat: add respective json_is UDFs * refactor: rename to_json to parse_json * chore: happy clippy * chore: some rename * fix: small fixes
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
-- json_get functions --
|
||||
SELECT json_get_int(to_json('{"a": {"b": {"c": 1}}}'), 'a.b.c');
|
||||
|
||||
SELECT json_get_float(to_json('{"a": {"b": {"c": 1.234}}}'), 'a:b.c');
|
||||
|
||||
SELECT json_get_string(to_json('{"a": {"b": {"c": "foo"}}}'), 'a.b:c');
|
||||
|
||||
SELECT json_get_bool(to_json('{"a": {"b": {"c": true}}}'), 'a.b["c"]');
|
||||
|
||||
SELECT json_get_int(to_json('{"a": {"b": {"c": {"d": 1}}}}'), 'a.b');
|
||||
|
||||
SELECT json_get_string(to_json('{"a": {"b": {"c": {"d": 1}}}}'), 'a.b');
|
||||
|
||||
-- test functions with table rows --
|
||||
CREATE TABLE jsons(j JSON, ts timestamp time index);
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('{"a": {"b": {"c": 1}}}'), 1);
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('{"a": {"b": {"c": 1.234}}}'), 2);
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('{"a": {"b": {"c": "foo"}}}'), 3);
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('{"a": {"b": {"c": true}}}'), 4);
|
||||
|
||||
SELECT json_get_int(j, 'a.b.c') FROM jsons;
|
||||
|
||||
SELECT json_get_float(j, 'a["b"].c') FROM jsons;
|
||||
|
||||
SELECT json_get_string(j, 'a.b.c?(@ == 1)') FROM jsons;
|
||||
|
||||
SELECT json_get_bool(j, 'a.b.c') FROM jsons;
|
||||
|
||||
SELECT json_get_int(j, 'a.b["c"]') FROM jsons;
|
||||
|
||||
DROP TABLE jsons;
|
||||
|
||||
-- test functions with arrays --
|
||||
CREATE TABLE jsons(j JSON, ts timestamp time index);
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('["a", "bcde", "", "Long time ago, there is a little pig flying in the sky"]'), 1);
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('[true, false, false, false]'), 2);
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('[1, 0, -2147483649, 2147483648]'), 3);
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('[1.2, 3.1415926535897932384626, -3e123, 1e100]'), 4);
|
||||
|
||||
SELECT json_get_int(j, '[0]') FROM jsons;
|
||||
|
||||
SELECT json_get_float(j, '[1]') FROM jsons;
|
||||
|
||||
SELECT json_get_bool(j, '[2]') FROM jsons;
|
||||
|
||||
SELECT json_get_string(j, '[3]') FROM jsons;
|
||||
|
||||
DROP TABLE jsons;
|
||||
@@ -1,70 +1,70 @@
|
||||
-- json_get functions --
|
||||
SELECT json_get_int(to_json('{"a": {"b": {"c": 1}}}'), 'a.b.c');
|
||||
SELECT json_get_int(parse_json('{"a": {"b": {"c": 1}}}'), 'a.b.c');
|
||||
|
||||
+---------------------------------------------------------------------+
|
||||
| json_get_int(to_json(Utf8("{"a": {"b": {"c": 1}}}")),Utf8("a.b.c")) |
|
||||
+---------------------------------------------------------------------+
|
||||
| 1 |
|
||||
+---------------------------------------------------------------------+
|
||||
+------------------------------------------------------------------------+
|
||||
| json_get_int(parse_json(Utf8("{"a": {"b": {"c": 1}}}")),Utf8("a.b.c")) |
|
||||
+------------------------------------------------------------------------+
|
||||
| 1 |
|
||||
+------------------------------------------------------------------------+
|
||||
|
||||
SELECT json_get_float(to_json('{"a": {"b": {"c": 1.234}}}'), 'a:b.c');
|
||||
SELECT json_get_float(parse_json('{"a": {"b": {"c": 1.234}}}'), 'a:b.c');
|
||||
|
||||
+---------------------------------------------------------------------------+
|
||||
| json_get_float(to_json(Utf8("{"a": {"b": {"c": 1.234}}}")),Utf8("a:b.c")) |
|
||||
+---------------------------------------------------------------------------+
|
||||
| 1.234 |
|
||||
+---------------------------------------------------------------------------+
|
||||
+------------------------------------------------------------------------------+
|
||||
| json_get_float(parse_json(Utf8("{"a": {"b": {"c": 1.234}}}")),Utf8("a:b.c")) |
|
||||
+------------------------------------------------------------------------------+
|
||||
| 1.234 |
|
||||
+------------------------------------------------------------------------------+
|
||||
|
||||
SELECT json_get_string(to_json('{"a": {"b": {"c": "foo"}}}'), 'a.b:c');
|
||||
SELECT json_get_string(parse_json('{"a": {"b": {"c": "foo"}}}'), 'a.b:c');
|
||||
|
||||
+----------------------------------------------------------------------------+
|
||||
| json_get_string(to_json(Utf8("{"a": {"b": {"c": "foo"}}}")),Utf8("a.b:c")) |
|
||||
+----------------------------------------------------------------------------+
|
||||
| foo |
|
||||
+----------------------------------------------------------------------------+
|
||||
+-------------------------------------------------------------------------------+
|
||||
| json_get_string(parse_json(Utf8("{"a": {"b": {"c": "foo"}}}")),Utf8("a.b:c")) |
|
||||
+-------------------------------------------------------------------------------+
|
||||
| foo |
|
||||
+-------------------------------------------------------------------------------+
|
||||
|
||||
SELECT json_get_bool(to_json('{"a": {"b": {"c": true}}}'), 'a.b["c"]');
|
||||
SELECT json_get_bool(parse_json('{"a": {"b": {"c": true}}}'), 'a.b["c"]');
|
||||
|
||||
+----------------------------------------------------------------------------+
|
||||
| json_get_bool(to_json(Utf8("{"a": {"b": {"c": true}}}")),Utf8("a.b["c"]")) |
|
||||
+----------------------------------------------------------------------------+
|
||||
| true |
|
||||
+----------------------------------------------------------------------------+
|
||||
+-------------------------------------------------------------------------------+
|
||||
| json_get_bool(parse_json(Utf8("{"a": {"b": {"c": true}}}")),Utf8("a.b["c"]")) |
|
||||
+-------------------------------------------------------------------------------+
|
||||
| true |
|
||||
+-------------------------------------------------------------------------------+
|
||||
|
||||
SELECT json_get_int(to_json('{"a": {"b": {"c": {"d": 1}}}}'), 'a.b');
|
||||
|
||||
+--------------------------------------------------------------------------+
|
||||
| json_get_int(to_json(Utf8("{"a": {"b": {"c": {"d": 1}}}}")),Utf8("a.b")) |
|
||||
+--------------------------------------------------------------------------+
|
||||
| |
|
||||
+--------------------------------------------------------------------------+
|
||||
|
||||
SELECT json_get_string(to_json('{"a": {"b": {"c": {"d": 1}}}}'), 'a.b');
|
||||
SELECT json_get_int(parse_json('{"a": {"b": {"c": {"d": 1}}}}'), 'a.b');
|
||||
|
||||
+-----------------------------------------------------------------------------+
|
||||
| json_get_string(to_json(Utf8("{"a": {"b": {"c": {"d": 1}}}}")),Utf8("a.b")) |
|
||||
| json_get_int(parse_json(Utf8("{"a": {"b": {"c": {"d": 1}}}}")),Utf8("a.b")) |
|
||||
+-----------------------------------------------------------------------------+
|
||||
| |
|
||||
+-----------------------------------------------------------------------------+
|
||||
|
||||
SELECT json_get_string(parse_json('{"a": {"b": {"c": {"d": 1}}}}'), 'a.b');
|
||||
|
||||
+--------------------------------------------------------------------------------+
|
||||
| json_get_string(parse_json(Utf8("{"a": {"b": {"c": {"d": 1}}}}")),Utf8("a.b")) |
|
||||
+--------------------------------------------------------------------------------+
|
||||
| |
|
||||
+--------------------------------------------------------------------------------+
|
||||
|
||||
-- test functions with table rows --
|
||||
CREATE TABLE jsons(j JSON, ts timestamp time index);
|
||||
|
||||
Affected Rows: 0
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('{"a": {"b": {"c": 1}}}'), 1);
|
||||
INSERT INTO jsons VALUES(parse_json('{"a": {"b": {"c": 1}}}'), 1);
|
||||
|
||||
Affected Rows: 1
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('{"a": {"b": {"c": 1.234}}}'), 2);
|
||||
INSERT INTO jsons VALUES(parse_json('{"a": {"b": {"c": 1.234}}}'), 2);
|
||||
|
||||
Affected Rows: 1
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('{"a": {"b": {"c": "foo"}}}'), 3);
|
||||
INSERT INTO jsons VALUES(parse_json('{"a": {"b": {"c": "foo"}}}'), 3);
|
||||
|
||||
Affected Rows: 1
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('{"a": {"b": {"c": true}}}'), 4);
|
||||
INSERT INTO jsons VALUES(parse_json('{"a": {"b": {"c": true}}}'), 4);
|
||||
|
||||
Affected Rows: 1
|
||||
|
||||
@@ -132,19 +132,19 @@ CREATE TABLE jsons(j JSON, ts timestamp time index);
|
||||
|
||||
Affected Rows: 0
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('["a", "bcde", "", "Long time ago, there is a little pig flying in the sky"]'), 1);
|
||||
INSERT INTO jsons VALUES(parse_json('["a", "bcde", "", "Long time ago, there is a little pig flying in the sky"]'), 1);
|
||||
|
||||
Affected Rows: 1
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('[true, false, false, false]'), 2);
|
||||
INSERT INTO jsons VALUES(parse_json('[true, false, false, false]'), 2);
|
||||
|
||||
Affected Rows: 1
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('[1, 0, -2147483649, 2147483648]'), 3);
|
||||
INSERT INTO jsons VALUES(parse_json('[1, 0, -2147483649, 2147483648]'), 3);
|
||||
|
||||
Affected Rows: 1
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('[1.2, 3.1415926535897932384626, -3e123, 1e100]'), 4);
|
||||
INSERT INTO jsons VALUES(parse_json('[1.2, 3.1415926535897932384626, -3e123, 1e100]'), 4);
|
||||
|
||||
Affected Rows: 1
|
||||
|
||||
@@ -196,3 +196,70 @@ DROP TABLE jsons;
|
||||
|
||||
Affected Rows: 0
|
||||
|
||||
-- test functions in WHERE clause --
|
||||
CREATE TABLE jsons(j JSON, ts timestamp time index);
|
||||
|
||||
Affected Rows: 0
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('{"a": {"b": {"c": 1}}}'), 1);
|
||||
|
||||
Affected Rows: 1
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('{"a": {"b": {"c": 1.234}}}'), 2);
|
||||
|
||||
Affected Rows: 1
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('{"a": {"b": {"c": "foo"}}}'), 3);
|
||||
|
||||
Affected Rows: 1
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('{"a": {"b": {"c": true}}}'), 4);
|
||||
|
||||
Affected Rows: 1
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_get_int(j, 'a.b.c') = 1;
|
||||
|
||||
+-------------------------+
|
||||
| json_to_string(jsons.j) |
|
||||
+-------------------------+
|
||||
| {"a":{"b":{"c":1}}} |
|
||||
| {"a":{"b":{"c":true}}} |
|
||||
+-------------------------+
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_get_float(j, 'a.b.c') = 1.234;
|
||||
|
||||
+-------------------------+
|
||||
| json_to_string(jsons.j) |
|
||||
+-------------------------+
|
||||
| {"a":{"b":{"c":1.234}}} |
|
||||
+-------------------------+
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_get_string(j, 'a.b.c') = 'foo';
|
||||
|
||||
+-------------------------+
|
||||
| json_to_string(jsons.j) |
|
||||
+-------------------------+
|
||||
| {"a":{"b":{"c":"foo"}}} |
|
||||
+-------------------------+
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_get_bool(j, 'a.b.c') = true;
|
||||
|
||||
+-------------------------+
|
||||
| json_to_string(jsons.j) |
|
||||
+-------------------------+
|
||||
| {"a":{"b":{"c":true}}} |
|
||||
+-------------------------+
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE CAST(json_get_int(j, 'a.b.c') AS BOOLEAN);
|
||||
|
||||
+-------------------------+
|
||||
| json_to_string(jsons.j) |
|
||||
+-------------------------+
|
||||
| {"a":{"b":{"c":1}}} |
|
||||
| {"a":{"b":{"c":true}}} |
|
||||
+-------------------------+
|
||||
|
||||
DROP TABLE jsons;
|
||||
|
||||
Affected Rows: 0
|
||||
|
||||
79
tests/cases/standalone/common/function/json/json_get.sql
Normal file
79
tests/cases/standalone/common/function/json/json_get.sql
Normal file
@@ -0,0 +1,79 @@
|
||||
-- json_get functions --
|
||||
SELECT json_get_int(parse_json('{"a": {"b": {"c": 1}}}'), 'a.b.c');
|
||||
|
||||
SELECT json_get_float(parse_json('{"a": {"b": {"c": 1.234}}}'), 'a:b.c');
|
||||
|
||||
SELECT json_get_string(parse_json('{"a": {"b": {"c": "foo"}}}'), 'a.b:c');
|
||||
|
||||
SELECT json_get_bool(parse_json('{"a": {"b": {"c": true}}}'), 'a.b["c"]');
|
||||
|
||||
SELECT json_get_int(parse_json('{"a": {"b": {"c": {"d": 1}}}}'), 'a.b');
|
||||
|
||||
SELECT json_get_string(parse_json('{"a": {"b": {"c": {"d": 1}}}}'), 'a.b');
|
||||
|
||||
-- test functions with table rows --
|
||||
CREATE TABLE jsons(j JSON, ts timestamp time index);
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('{"a": {"b": {"c": 1}}}'), 1);
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('{"a": {"b": {"c": 1.234}}}'), 2);
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('{"a": {"b": {"c": "foo"}}}'), 3);
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('{"a": {"b": {"c": true}}}'), 4);
|
||||
|
||||
SELECT json_get_int(j, 'a.b.c') FROM jsons;
|
||||
|
||||
SELECT json_get_float(j, 'a["b"].c') FROM jsons;
|
||||
|
||||
SELECT json_get_string(j, 'a.b.c?(@ == 1)') FROM jsons;
|
||||
|
||||
SELECT json_get_bool(j, 'a.b.c') FROM jsons;
|
||||
|
||||
SELECT json_get_int(j, 'a.b["c"]') FROM jsons;
|
||||
|
||||
DROP TABLE jsons;
|
||||
|
||||
-- test functions with arrays --
|
||||
CREATE TABLE jsons(j JSON, ts timestamp time index);
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('["a", "bcde", "", "Long time ago, there is a little pig flying in the sky"]'), 1);
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('[true, false, false, false]'), 2);
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('[1, 0, -2147483649, 2147483648]'), 3);
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('[1.2, 3.1415926535897932384626, -3e123, 1e100]'), 4);
|
||||
|
||||
SELECT json_get_int(j, '[0]') FROM jsons;
|
||||
|
||||
SELECT json_get_float(j, '[1]') FROM jsons;
|
||||
|
||||
SELECT json_get_bool(j, '[2]') FROM jsons;
|
||||
|
||||
SELECT json_get_string(j, '[3]') FROM jsons;
|
||||
|
||||
DROP TABLE jsons;
|
||||
|
||||
-- test functions in WHERE clause --
|
||||
CREATE TABLE jsons(j JSON, ts timestamp time index);
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('{"a": {"b": {"c": 1}}}'), 1);
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('{"a": {"b": {"c": 1.234}}}'), 2);
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('{"a": {"b": {"c": "foo"}}}'), 3);
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('{"a": {"b": {"c": true}}}'), 4);
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_get_int(j, 'a.b.c') = 1;
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_get_float(j, 'a.b.c') = 1.234;
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_get_string(j, 'a.b.c') = 'foo';
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_get_bool(j, 'a.b.c') = true;
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE CAST(json_get_int(j, 'a.b.c') AS BOOLEAN);
|
||||
|
||||
DROP TABLE jsons;
|
||||
175
tests/cases/standalone/common/function/json/json_is.result
Normal file
175
tests/cases/standalone/common/function/json/json_is.result
Normal file
@@ -0,0 +1,175 @@
|
||||
-- json_is functions --
|
||||
SELECT json_is_object(parse_json('{"a": 1}'));
|
||||
|
||||
+----------------------------------------------+
|
||||
| json_is_object(parse_json(Utf8("{"a": 1}"))) |
|
||||
+----------------------------------------------+
|
||||
| true |
|
||||
+----------------------------------------------+
|
||||
|
||||
SELECT json_is_array(parse_json('[1, 2, 3]'));
|
||||
|
||||
+----------------------------------------------+
|
||||
| json_is_array(parse_json(Utf8("[1, 2, 3]"))) |
|
||||
+----------------------------------------------+
|
||||
| true |
|
||||
+----------------------------------------------+
|
||||
|
||||
SELECT json_is_int(parse_json('1'));
|
||||
|
||||
+------------------------------------+
|
||||
| json_is_int(parse_json(Utf8("1"))) |
|
||||
+------------------------------------+
|
||||
| true |
|
||||
+------------------------------------+
|
||||
|
||||
SELECT json_is_bool(parse_json('true'));
|
||||
|
||||
+----------------------------------------+
|
||||
| json_is_bool(parse_json(Utf8("true"))) |
|
||||
+----------------------------------------+
|
||||
| true |
|
||||
+----------------------------------------+
|
||||
|
||||
SELECT json_is_null(parse_json('null'));
|
||||
|
||||
+----------------------------------------+
|
||||
| json_is_null(parse_json(Utf8("null"))) |
|
||||
+----------------------------------------+
|
||||
| true |
|
||||
+----------------------------------------+
|
||||
|
||||
SELECT json_is_float(parse_json('1.2'));
|
||||
|
||||
+----------------------------------------+
|
||||
| json_is_float(parse_json(Utf8("1.2"))) |
|
||||
+----------------------------------------+
|
||||
| true |
|
||||
+----------------------------------------+
|
||||
|
||||
SELECT json_is_string(parse_json('"foo"'));
|
||||
|
||||
+-------------------------------------------+
|
||||
| json_is_string(parse_json(Utf8(""foo""))) |
|
||||
+-------------------------------------------+
|
||||
| true |
|
||||
+-------------------------------------------+
|
||||
|
||||
SELECT json_is_null(parse_json('{"a": 1}'));
|
||||
|
||||
+--------------------------------------------+
|
||||
| json_is_null(parse_json(Utf8("{"a": 1}"))) |
|
||||
+--------------------------------------------+
|
||||
| false |
|
||||
+--------------------------------------------+
|
||||
|
||||
SELECT json_is_string(parse_json('[1, 2, 3]'));
|
||||
|
||||
+-----------------------------------------------+
|
||||
| json_is_string(parse_json(Utf8("[1, 2, 3]"))) |
|
||||
+-----------------------------------------------+
|
||||
| false |
|
||||
+-----------------------------------------------+
|
||||
|
||||
SELECT json_is_float(parse_json('1'));
|
||||
|
||||
+--------------------------------------+
|
||||
| json_is_float(parse_json(Utf8("1"))) |
|
||||
+--------------------------------------+
|
||||
| true |
|
||||
+--------------------------------------+
|
||||
|
||||
-- test json_is functions in table rows and WHERE clause --
|
||||
CREATE TABLE jsons(j JSON, ts timestamp time index);
|
||||
|
||||
Affected Rows: 0
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('{"a": 1}'), 1);
|
||||
|
||||
Affected Rows: 1
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('[1, 2, 3]'), 2);
|
||||
|
||||
Affected Rows: 1
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('1'), 3);
|
||||
|
||||
Affected Rows: 1
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('true'), 4);
|
||||
|
||||
Affected Rows: 1
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('null'), 5);
|
||||
|
||||
Affected Rows: 1
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('1.2'), 6);
|
||||
|
||||
Affected Rows: 1
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('"foo"'), 7);
|
||||
|
||||
Affected Rows: 1
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_is_object(j);
|
||||
|
||||
+-------------------------+
|
||||
| json_to_string(jsons.j) |
|
||||
+-------------------------+
|
||||
| {"a":1} |
|
||||
+-------------------------+
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_is_array(j);
|
||||
|
||||
+-------------------------+
|
||||
| json_to_string(jsons.j) |
|
||||
+-------------------------+
|
||||
| [1,2,3] |
|
||||
+-------------------------+
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_is_int(j);
|
||||
|
||||
+-------------------------+
|
||||
| json_to_string(jsons.j) |
|
||||
+-------------------------+
|
||||
| 1 |
|
||||
+-------------------------+
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_is_bool(j);
|
||||
|
||||
+-------------------------+
|
||||
| json_to_string(jsons.j) |
|
||||
+-------------------------+
|
||||
| true |
|
||||
+-------------------------+
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_is_null(j);
|
||||
|
||||
+-------------------------+
|
||||
| json_to_string(jsons.j) |
|
||||
+-------------------------+
|
||||
| null |
|
||||
+-------------------------+
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_is_float(j);
|
||||
|
||||
+-------------------------+
|
||||
| json_to_string(jsons.j) |
|
||||
+-------------------------+
|
||||
| 1 |
|
||||
| 1.2 |
|
||||
+-------------------------+
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_is_string(j);
|
||||
|
||||
+-------------------------+
|
||||
| json_to_string(jsons.j) |
|
||||
+-------------------------+
|
||||
| "foo" |
|
||||
+-------------------------+
|
||||
|
||||
DROP TABLE jsons;
|
||||
|
||||
Affected Rows: 0
|
||||
|
||||
53
tests/cases/standalone/common/function/json/json_is.sql
Normal file
53
tests/cases/standalone/common/function/json/json_is.sql
Normal file
@@ -0,0 +1,53 @@
|
||||
-- json_is functions --
|
||||
SELECT json_is_object(parse_json('{"a": 1}'));
|
||||
|
||||
SELECT json_is_array(parse_json('[1, 2, 3]'));
|
||||
|
||||
SELECT json_is_int(parse_json('1'));
|
||||
|
||||
SELECT json_is_bool(parse_json('true'));
|
||||
|
||||
SELECT json_is_null(parse_json('null'));
|
||||
|
||||
SELECT json_is_float(parse_json('1.2'));
|
||||
|
||||
SELECT json_is_string(parse_json('"foo"'));
|
||||
|
||||
SELECT json_is_null(parse_json('{"a": 1}'));
|
||||
|
||||
SELECT json_is_string(parse_json('[1, 2, 3]'));
|
||||
|
||||
SELECT json_is_float(parse_json('1'));
|
||||
|
||||
-- test json_is functions in table rows and WHERE clause --
|
||||
CREATE TABLE jsons(j JSON, ts timestamp time index);
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('{"a": 1}'), 1);
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('[1, 2, 3]'), 2);
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('1'), 3);
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('true'), 4);
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('null'), 5);
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('1.2'), 6);
|
||||
|
||||
INSERT INTO jsons VALUES(parse_json('"foo"'), 7);
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_is_object(j);
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_is_array(j);
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_is_int(j);
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_is_bool(j);
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_is_null(j);
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_is_float(j);
|
||||
|
||||
SELECT json_to_string(j) FROM jsons WHERE json_is_string(j);
|
||||
|
||||
DROP TABLE jsons;
|
||||
@@ -41,19 +41,19 @@ INSERT INTO jsons VALUES('[null]', 0),
|
||||
|
||||
Affected Rows: 12
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('[null]'), 12),
|
||||
(to_json('[true]'), 13),
|
||||
(to_json('[false]'), 14),
|
||||
(to_json('[0]'), 15),
|
||||
(to_json('["foo"]'), 16),
|
||||
(to_json('[]'), 17),
|
||||
(to_json('{}'), 18),
|
||||
(to_json('[0,1]'), 19),
|
||||
(to_json('{"foo":"bar"}'), 20),
|
||||
(to_json('{"a":null,"foo":"bar"}'), 21),
|
||||
(to_json('[-1]'), 22),
|
||||
(to_json('[-2147483648]'), 23),
|
||||
(to_json('{"entities": {
|
||||
INSERT INTO jsons VALUES(parse_json('[null]'), 12),
|
||||
(parse_json('[true]'), 13),
|
||||
(parse_json('[false]'), 14),
|
||||
(parse_json('[0]'), 15),
|
||||
(parse_json('["foo"]'), 16),
|
||||
(parse_json('[]'), 17),
|
||||
(parse_json('{}'), 18),
|
||||
(parse_json('[0,1]'), 19),
|
||||
(parse_json('{"foo":"bar"}'), 20),
|
||||
(parse_json('{"a":null,"foo":"bar"}'), 21),
|
||||
(parse_json('[-1]'), 22),
|
||||
(parse_json('[-2147483648]'), 23),
|
||||
(parse_json('{"entities": {
|
||||
"description": {
|
||||
"urls": [
|
||||
{
|
||||
@@ -117,11 +117,11 @@ DELETE FROM jsons;
|
||||
|
||||
Affected Rows: 25
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('{"a":1, "b":2, "c":3'), 4);
|
||||
INSERT INTO jsons VALUES(parse_json('{"a":1, "b":2, "c":3'), 4);
|
||||
|
||||
Error: 3001(EngineExecuteQuery), DataFusion error: Invalid function args: Cannot convert the string to json, have: {"a":1, "b":2, "c":3
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('Morning my friends, have a nice day :)'), 5);
|
||||
INSERT INTO jsons VALUES(parse_json('Morning my friends, have a nice day :)'), 5);
|
||||
|
||||
Error: 3001(EngineExecuteQuery), DataFusion error: Invalid function args: Cannot convert the string to json, have: Morning my friends, have a nice day :)
|
||||
|
||||
|
||||
@@ -37,19 +37,19 @@ INSERT INTO jsons VALUES('[null]', 0),
|
||||
}
|
||||
}}', 11);
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('[null]'), 12),
|
||||
(to_json('[true]'), 13),
|
||||
(to_json('[false]'), 14),
|
||||
(to_json('[0]'), 15),
|
||||
(to_json('["foo"]'), 16),
|
||||
(to_json('[]'), 17),
|
||||
(to_json('{}'), 18),
|
||||
(to_json('[0,1]'), 19),
|
||||
(to_json('{"foo":"bar"}'), 20),
|
||||
(to_json('{"a":null,"foo":"bar"}'), 21),
|
||||
(to_json('[-1]'), 22),
|
||||
(to_json('[-2147483648]'), 23),
|
||||
(to_json('{"entities": {
|
||||
INSERT INTO jsons VALUES(parse_json('[null]'), 12),
|
||||
(parse_json('[true]'), 13),
|
||||
(parse_json('[false]'), 14),
|
||||
(parse_json('[0]'), 15),
|
||||
(parse_json('["foo"]'), 16),
|
||||
(parse_json('[]'), 17),
|
||||
(parse_json('{}'), 18),
|
||||
(parse_json('[0,1]'), 19),
|
||||
(parse_json('{"foo":"bar"}'), 20),
|
||||
(parse_json('{"a":null,"foo":"bar"}'), 21),
|
||||
(parse_json('[-1]'), 22),
|
||||
(parse_json('[-2147483648]'), 23),
|
||||
(parse_json('{"entities": {
|
||||
"description": {
|
||||
"urls": [
|
||||
{
|
||||
@@ -79,9 +79,9 @@ SELECT json_to_string(j), t FROM jsons;
|
||||
--Insert invalid json strings--
|
||||
DELETE FROM jsons;
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('{"a":1, "b":2, "c":3'), 4);
|
||||
INSERT INTO jsons VALUES(parse_json('{"a":1, "b":2, "c":3'), 4);
|
||||
|
||||
INSERT INTO jsons VALUES(to_json('Morning my friends, have a nice day :)'), 5);
|
||||
INSERT INTO jsons VALUES(parse_json('Morning my friends, have a nice day :)'), 5);
|
||||
|
||||
SELECT json_to_string(j), t FROM jsons;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user