create schema test_json_storage_size;
set current_schema to 'test_json_storage_size';
set enable_set_variable_b_format to on;
SELECT JSON_STORAGE_SIZE('0');
 json_storage_size 
-------------------
                 2
(1 row)

SELECT JSON_STORAGE_SIZE('1');
 json_storage_size 
-------------------
                 2
(1 row)

SELECT JSON_STORAGE_SIZE('1000');
 json_storage_size 
-------------------
                 5
(1 row)

SELECT JSON_STORAGE_SIZE('100000');
 json_storage_size 
-------------------
                 7
(1 row)

SELECT JSON_STORAGE_SIZE('"a"');
 json_storage_size 
-------------------
                 4
(1 row)

SELECT JSON_STORAGE_SIZE('"Hello World"');
 json_storage_size 
-------------------
                14
(1 row)

SELECT JSON_STORAGE_SIZE('true');
 json_storage_size 
-------------------
                 5
(1 row)

SELECT JSON_STORAGE_SIZE('false');
 json_storage_size 
-------------------
                 6
(1 row)

SELECT JSON_STORAGE_SIZE('null');
 json_storage_size 
-------------------
                 5
(1 row)

SELECT JSON_STORAGE_SIZE('[1, "abc", null, true, "10:27:06.000000", {"id": 1}]');
 json_storage_size 
-------------------
                53
(1 row)

SELECT JSON_STORAGE_SIZE('{"db": ["mysql", "oracle"], "id": 123, "info": {"age": 20}}');
 json_storage_size 
-------------------
                60
(1 row)

SELECT JSON_STORAGE_SIZE('[1, 2]');
 json_storage_size 
-------------------
                 7
(1 row)

SELECT JSON_STORAGE_SIZE('[1, 2, 3]');
 json_storage_size 
-------------------
                10
(1 row)

SELECT JSON_STORAGE_SIZE('{"x": 1}');
 json_storage_size 
-------------------
                 9
(1 row)

SELECT JSON_STORAGE_SIZE('{"x": 1, "y": 2}');
 json_storage_size 
-------------------
                17
(1 row)

SET @data = '{
  "Person": {
    "Age": 10,
    "Name": "Bart",
    "Friends": [
      "Bart",
      "Milhouse"
    ]
  }
}';
SELECT JSON_STORAGE_SIZE(@data);
 json_storage_size 
-------------------
               114
(1 row)

DROP TABLE IF EXISTS test_json_storage_size;
NOTICE:  table "test_json_storage_size" does not exist, skipping
CREATE TABLE test_json_storage_size (
    json_col JSON NOT NULL
);
INSERT INTO test_json_storage_size
VALUES ('{"x": 1, "y": 2}');
SELECT
    json_col,
    JSON_STORAGE_SIZE(json_col)
FROM
    test_json_storage_size;
     json_col     | json_storage_size 
------------------+-------------------
 {"x": 1, "y": 2} |                17
(1 row)

UPDATE test_json_storage_size
SET json_col = '{"x": 1, "y": 2, "z": 3}';
SELECT
    json_col,
    JSON_STORAGE_SIZE(json_col)
FROM
    test_json_storage_size;
         json_col         | json_storage_size 
--------------------------+-------------------
 {"x": 1, "y": 2, "z": 3} |                25
(1 row)

SELECT JSON_STORAGE_SIZE('{0,1}');
ERROR:  Invalid JSON text in argument 1 to function json_storage_size.
CONTEXT:  referenced column: json_storage_size
set dolphin.b_compatibility_mode = on;
create table test_json_table
(
   `char` char(100),
   `varchar` varchar(100),
   `json` json,
   `int1` tinyint,
   `binary` binary(100)
);
insert into test_json_table values ('[1,2,3,4,5]', '[1,2,3,4,5]', json_object('a', 1, 'b', 2), 1, '[1,2,3,4,5]');
select json_storage_size(`char`), json_storage_size(`varchar`), json_storage_size(`json`), pg_typeof(json_storage_size(`char`)), pg_typeof(json_storage_size(`varchar`)), pg_typeof(json_storage_size(`json`)) from test_json_table;
 json_storage_size | json_storage_size | json_storage_size | pg_typeof | pg_typeof | pg_typeof 
-------------------+-------------------+-------------------+-----------+-----------+-----------
               101 |                12 |                17 | bigint    | bigint    | bigint
(1 row)

create table test1 as select json_storage_size(`char`) as c1, json_storage_size(`varchar`) as c2, json_storage_size(`json`) as c3 from test_json_table;
-- expect error
select json_storage_size(`int1`) from test_json_table;
ERROR:  Invalid data type for JSON data in argument 1 to function json_storage_size
CONTEXT:  referenced column: json_storage_size
select json_storage_size(`binary`) from test_json_table;
ERROR:  Invalid data type for JSON data in argument 1 to function json_storage_size
CONTEXT:  referenced column: json_storage_size
drop table test1;
drop table test_json_table;
drop schema test_json_storage_size cascade;
NOTICE:  drop cascades to table test_json_storage_size
reset current_schema;