@@ -751,6 +751,53 @@ FROM
751751 4 | 4 | [4]
752752(25 rows)
753753
754+ -- Conversion to record types
755+ CREATE TYPE sqljsonb_rec AS (a int, t text, js json, jb jsonb, jsa json[]);
756+ CREATE TYPE sqljsonb_reca AS (reca sqljsonb_rec[]);
757+ SELECT JSON_QUERY(jsonb '[{"a": 1, "b": "foo", "t": "aaa", "js": [1, "2", {}], "jb": {"x": [1, "2", {}]}}, {"a": 2}]', '$[0]' RETURNING sqljsonb_rec);
758+ ?column?
759+ -----------------------------------------------------
760+ (1,aaa,"[1, ""2"", {}]","{""x"": [1, ""2"", {}]}",)
761+ (1 row)
762+
763+ SELECT * FROM unnest((JSON_QUERY(jsonb '{"jsa": [{"a": 1, "b": ["foo"]}, {"a": 2, "c": {}}, 123]}', '$' RETURNING sqljsonb_rec)).jsa);
764+ unnest
765+ ------------------------
766+ {"a": 1, "b": ["foo"]}
767+ {"a": 2, "c": {}}
768+ 123
769+ (3 rows)
770+
771+ SELECT * FROM unnest((JSON_QUERY(jsonb '{"reca": [{"a": 1, "t": ["foo", []]}, {"a": 2, "jb": [{}, true]}]}', '$' RETURNING sqljsonb_reca)).reca);
772+ a | t | js | jb | jsa
773+ ---+-------------+----+------------+-----
774+ 1 | ["foo", []] | | |
775+ 2 | | | [{}, true] |
776+ (2 rows)
777+
778+ -- Conversion to array types
779+ SELECT JSON_QUERY(jsonb '[1,2,null,"3"]', '$[*]' RETURNING int[] WITH WRAPPER);
780+ ?column?
781+ --------------
782+ {1,2,NULL,3}
783+ (1 row)
784+
785+ SELECT * FROM unnest(JSON_QUERY(jsonb '[{"a": 1, "t": ["foo", []]}, {"a": 2, "jb": [{}, true]}]', '$' RETURNING sqljsonb_rec[]));
786+ a | t | js | jb | jsa
787+ ---+-------------+----+------------+-----
788+ 1 | ["foo", []] | | |
789+ 2 | | | [{}, true] |
790+ (2 rows)
791+
792+ -- Conversion to domain types
793+ SELECT JSON_QUERY(jsonb '{"a": 1}', '$.a' RETURNING sqljsonb_int_not_null);
794+ ?column?
795+ ----------
796+ 1
797+ (1 row)
798+
799+ SELECT JSON_QUERY(jsonb '{"a": 1}', '$.b' RETURNING sqljsonb_int_not_null);
800+ ERROR: domain sqljsonb_int_not_null does not allow null values
754801-- Test constraints
755802CREATE TABLE test_jsonb_constraints (
756803 js text,
0 commit comments