jsonb.sql 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. -- Strings.
  2. SELECT '""'::jsonb; -- OK.
  3. SELECT $$''$$::jsonb; -- ERROR, single quotes are not allowed
  4. SELECT '"abc"'::jsonb; -- OK
  5. SELECT '"abc'::jsonb; -- ERROR, quotes not closed
  6. SELECT '"abc
  7. def"'::jsonb; -- ERROR, unescaped newline in string constant
  8. SELECT '"\n\"\\"'::jsonb; -- OK, legal escapes
  9. SELECT '"\v"'::jsonb; -- ERROR, not a valid JSON escape
  10. -- see json_encoding test for input with unicode escapes
  11. -- Numbers.
  12. SELECT '1'::jsonb; -- OK
  13. SELECT '0'::jsonb; -- OK
  14. SELECT '01'::jsonb; -- ERROR, not valid according to JSON spec
  15. SELECT '0.1'::jsonb; -- OK
  16. SELECT '9223372036854775808'::jsonb; -- OK, even though it's too large for int8
  17. SELECT '1f2'::jsonb; -- ERROR
  18. SELECT '0.x1'::jsonb; -- ERROR
  19. SELECT '1.3ex100'::jsonb; -- ERROR
  20. -- Arrays.
  21. SELECT '[]'::jsonb; -- OK
  22. SELECT '[1,2]'::jsonb; -- OK
  23. SELECT '[1,2,]'::jsonb; -- ERROR, trailing comma
  24. SELECT '[1,2'::jsonb; -- ERROR, no closing bracket
  25. SELECT '[1,[2]'::jsonb; -- ERROR, no closing bracket
  26. -- Objects.
  27. SELECT '{}'::jsonb; -- OK
  28. SELECT '{"abc"}'::jsonb; -- ERROR, no value
  29. SELECT '{"abc":1}'::jsonb; -- OK
  30. SELECT '{1:"abc"}'::jsonb; -- ERROR, keys must be strings
  31. SELECT '{"abc",1}'::jsonb; -- ERROR, wrong separator
  32. SELECT '{"abc"=1}'::jsonb; -- ERROR, totally wrong separator
  33. SELECT '{"abc"::1}'::jsonb; -- ERROR, another wrong separator
  34. SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::jsonb; -- OK
  35. SELECT '{"abc":1:2}'::jsonb; -- ERROR, colon in wrong spot
  36. SELECT '{"abc":1,3}'::jsonb; -- ERROR, no value
  37. -- Miscellaneous stuff.
  38. SELECT 'true'::jsonb; -- OK
  39. SELECT 'false'::jsonb; -- OK
  40. SELECT 'null'::jsonb; -- OK
  41. SELECT ' true '::jsonb; -- OK, even with extra whitespace
  42. SELECT 'true false'::jsonb; -- ERROR, too many values
  43. SELECT 'true, false'::jsonb; -- ERROR, too many values
  44. SELECT 'truf'::jsonb; -- ERROR, not a keyword
  45. SELECT 'trues'::jsonb; -- ERROR, not a keyword
  46. SELECT ''::jsonb; -- ERROR, no value
  47. SELECT ' '::jsonb; -- ERROR, no value
  48. -- Multi-line JSON input to check ERROR reporting
  49. SELECT '{
  50. "one": 1,
  51. "two":"two",
  52. "three":
  53. true}'::jsonb; -- OK
  54. SELECT '{
  55. "one": 1,
  56. "two":,"two", -- ERROR extraneous comma before field "two"
  57. "three":
  58. true}'::jsonb;
  59. SELECT '{
  60. "one": 1,
  61. "two":"two",
  62. "averyveryveryveryveryveryveryveryveryverylongfieldname":}'::jsonb;
  63. -- ERROR missing value for last field
  64. -- make sure jsonb is passed through json generators without being escaped
  65. SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']);
  66. BEGIN;
  67. SET LOCAL TIME ZONE 10.5;
  68. SET LOCAL TIME ZONE -8;
  69. COMMIT;
  70. -- jsonb extraction functions
  71. CREATE TEMP TABLE test_jsonb (
  72. json_type text,
  73. test_json jsonb
  74. );
  75. INSERT INTO test_jsonb VALUES
  76. ('scalar','"a scalar"'),
  77. ('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
  78. ('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
  79. SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'scalar';
  80. SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'array';
  81. SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'object';
  82. SELECT test_json -> 'field2' FROM test_jsonb WHERE json_type = 'object';
  83. SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'scalar';
  84. SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'array';
  85. SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'object';
  86. SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'scalar';
  87. SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'array';
  88. SELECT test_json -> 9 FROM test_jsonb WHERE json_type = 'array';
  89. SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'object';
  90. SELECT test_json ->> 6 FROM test_jsonb WHERE json_type = 'array';
  91. SELECT test_json ->> 7 FROM test_jsonb WHERE json_type = 'array';
  92. SELECT test_json ->> 'field4' FROM test_jsonb WHERE json_type = 'object';
  93. SELECT test_json ->> 'field5' FROM test_jsonb WHERE json_type = 'object';
  94. SELECT test_json ->> 'field6' FROM test_jsonb WHERE json_type = 'object';
  95. SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'scalar';
  96. SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'array';
  97. SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'object';
  98. -- nulls
  99. SELECT (test_json->'field3') IS NULL AS expect_false FROM test_jsonb WHERE json_type = 'object';
  100. SELECT (test_json->>'field3') IS NULL AS expect_true FROM test_jsonb WHERE json_type = 'object';
  101. SELECT (test_json->3) IS NULL AS expect_false FROM test_jsonb WHERE json_type = 'array';
  102. SELECT (test_json->>3) IS NULL AS expect_true FROM test_jsonb WHERE json_type = 'array';
  103. -- corner cases
  104. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::text;
  105. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::int;
  106. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 1;
  107. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 'z';
  108. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> '';
  109. select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 1;
  110. select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 3;
  111. select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 'z';
  112. select '{"a": "c", "b": null}'::jsonb -> 'b';
  113. select '"foo"'::jsonb -> 1;
  114. select '"foo"'::jsonb -> 'z';
  115. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::text;
  116. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::int;
  117. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 1;
  118. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 'z';
  119. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> '';
  120. select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 1;
  121. select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 3;
  122. select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 'z';
  123. select '{"a": "c", "b": null}'::jsonb ->> 'b';
  124. select '"foo"'::jsonb ->> 1;
  125. select '"foo"'::jsonb ->> 'z';
  126. -- equality and inequality
  127. SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb;
  128. SELECT '{"x":"y"}'::jsonb = '{"x":"z"}'::jsonb;
  129. SELECT '{"x":"y"}'::jsonb <> '{"x":"y"}'::jsonb;
  130. SELECT '{"x":"y"}'::jsonb <> '{"x":"z"}'::jsonb;
  131. -- containment
  132. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b"}');
  133. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "c":null}');
  134. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "g":null}');
  135. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"g":null}');
  136. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"c"}');
  137. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b"}');
  138. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "c":"q"}');
  139. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b"}';
  140. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "c":null}';
  141. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "g":null}';
  142. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"g":null}';
  143. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"c"}';
  144. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b"}';
  145. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "c":"q"}';
  146. SELECT '[1,2]'::jsonb @> '[1,2,2]'::jsonb;
  147. SELECT '[1,1,2]'::jsonb @> '[1,2,2]'::jsonb;
  148. SELECT '[[1,2]]'::jsonb @> '[[1,2,2]]'::jsonb;
  149. SELECT '[1,2,2]'::jsonb <@ '[1,2]'::jsonb;
  150. SELECT '[1,2,2]'::jsonb <@ '[1,1,2]'::jsonb;
  151. SELECT '[[1,2,2]]'::jsonb <@ '[[1,2]]'::jsonb;
  152. SELECT jsonb_contained('{"a":"b"}', '{"a":"b", "b":1, "c":null}');
  153. SELECT jsonb_contained('{"a":"b", "c":null}', '{"a":"b", "b":1, "c":null}');
  154. SELECT jsonb_contained('{"a":"b", "g":null}', '{"a":"b", "b":1, "c":null}');
  155. SELECT jsonb_contained('{"g":null}', '{"a":"b", "b":1, "c":null}');
  156. SELECT jsonb_contained('{"a":"c"}', '{"a":"b", "b":1, "c":null}');
  157. SELECT jsonb_contained('{"a":"b"}', '{"a":"b", "b":1, "c":null}');
  158. SELECT jsonb_contained('{"a":"b", "c":"q"}', '{"a":"b", "b":1, "c":null}');
  159. SELECT '{"a":"b"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  160. SELECT '{"a":"b", "c":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  161. SELECT '{"a":"b", "g":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  162. SELECT '{"g":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  163. SELECT '{"a":"c"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  164. SELECT '{"a":"b"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  165. SELECT '{"a":"b", "c":"q"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  166. -- Raw scalar may contain another raw scalar, array may contain a raw scalar
  167. SELECT '[5]'::jsonb @> '[5]';
  168. SELECT '5'::jsonb @> '5';
  169. SELECT '[5]'::jsonb @> '5';
  170. -- But a raw scalar cannot contain an array
  171. SELECT '5'::jsonb @> '[5]';
  172. -- In general, one thing should always contain itself. Test array containment:
  173. SELECT '["9", ["7", "3"], 1]'::jsonb @> '["9", ["7", "3"], 1]'::jsonb;
  174. SELECT '["9", ["7", "3"], ["1"]]'::jsonb @> '["9", ["7", "3"], ["1"]]'::jsonb;
  175. -- array containment string matching confusion bug
  176. SELECT '{ "name": "Bob", "tags": [ "enim", "qui"]}'::jsonb @> '{"tags":["qu"]}';
  177. -- array length
  178. SELECT jsonb_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
  179. SELECT jsonb_array_length('[]');
  180. SELECT jsonb_array_length('{"f1":1,"f2":[5,6]}');
  181. SELECT jsonb_array_length('4');
  182. SELECT * FROM jsonb_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
  183. SELECT * FROM jsonb_each('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
  184. SELECT * FROM jsonb_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
  185. SELECT * FROM jsonb_each_text('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
  186. -- exists
  187. SELECT jsonb_exists('{"a":null, "b":"qq"}', 'a');
  188. SELECT jsonb_exists('{"a":null, "b":"qq"}', 'b');
  189. SELECT jsonb_exists('{"a":null, "b":"qq"}', 'c');
  190. SELECT jsonb_exists('{"a":"null", "b":"qq"}', 'a');
  191. SELECT jsonb '{"a":null, "b":"qq"}' ? 'a';
  192. SELECT jsonb '{"a":null, "b":"qq"}' ? 'b';
  193. SELECT jsonb '{"a":null, "b":"qq"}' ? 'c';
  194. SELECT jsonb '{"a":"null", "b":"qq"}' ? 'a';
  195. -- array exists - array elements should behave as keys
  196. SELECT count(*) from testjsonb WHERE j->'array' ? 'bar';
  197. -- type sensitive array exists - should return no rows (since "exists" only
  198. -- matches strings that are either object keys or array elements)
  199. SELECT count(*) from testjsonb WHERE j->'array' ? '5'::text;
  200. -- However, a raw scalar is *contained* within the array
  201. SELECT count(*) from testjsonb WHERE j->'array' @> '5'::jsonb;
  202. SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['a','b']);
  203. SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['b','a']);
  204. SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['c','a']);
  205. SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['c','d']);
  206. SELECT jsonb_exists_any('{"a":null, "b":"qq"}', '{}'::text[]);
  207. SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['a','b'];
  208. SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['b','a'];
  209. SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['c','a'];
  210. SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['c','d'];
  211. SELECT jsonb '{"a":null, "b":"qq"}' ?| '{}'::text[];
  212. SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['a','b']);
  213. SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['b','a']);
  214. SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['c','a']);
  215. SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['c','d']);
  216. SELECT jsonb_exists_all('{"a":null, "b":"qq"}', '{}'::text[]);
  217. SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['a','b'];
  218. SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['b','a'];
  219. SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['c','a'];
  220. SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['c','d'];
  221. SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['a','a', 'b', 'b', 'b'];
  222. SELECT jsonb '{"a":null, "b":"qq"}' ?& '{}'::text[];
  223. -- typeof
  224. SELECT jsonb_typeof('{}') AS object;
  225. SELECT jsonb_typeof('{"c":3,"p":"o"}') AS object;
  226. SELECT jsonb_typeof('[]') AS array;
  227. SELECT jsonb_typeof('["a", 1]') AS array;
  228. SELECT jsonb_typeof('null') AS "null";
  229. SELECT jsonb_typeof('1') AS number;
  230. SELECT jsonb_typeof('-1') AS number;
  231. SELECT jsonb_typeof('1.0') AS number;
  232. SELECT jsonb_typeof('1e2') AS number;
  233. SELECT jsonb_typeof('-1.0') AS number;
  234. SELECT jsonb_typeof('true') AS boolean;
  235. SELECT jsonb_typeof('false') AS boolean;
  236. SELECT jsonb_typeof('"hello"') AS string;
  237. SELECT jsonb_typeof('"true"') AS string;
  238. SELECT jsonb_typeof('"1.0"') AS string;
  239. SELECT jsonb_build_object('{a,b,c}'::text[]); -- error
  240. SELECT jsonb_build_object('{a,b,c}'::text[], '{d,e,f}'::text[]); -- error, key cannot be array
  241. -- empty objects/arrays
  242. SELECT jsonb_build_array();
  243. SELECT jsonb_build_object();
  244. -- make sure keys are quoted
  245. SELECT jsonb_build_object(1,2);
  246. SELECT jsonb_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
  247. SELECT jsonb_build_object(json '{"a":1,"b":2}', 3);
  248. SELECT jsonb_build_object('{1,2,3}'::int[], 3);
  249. -- handling of NULL values
  250. SELECT jsonb_object_agg(1, NULL::jsonb);
  251. SELECT jsonb_object_agg(NULL, '{"a":1}');
  252. CREATE TEMP TABLE foo (serial_num int, name text, type text);
  253. INSERT INTO foo VALUES (847001,'t15','GE1043');
  254. INSERT INTO foo VALUES (847002,'t16','GE1043');
  255. INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
  256. SELECT jsonb_object_agg(name, type) FROM foo;
  257. INSERT INTO foo VALUES (999999, NULL, 'bar');
  258. SELECT jsonb_object_agg(name, type) FROM foo;
  259. -- jsonb_object
  260. -- empty object, one dimension
  261. SELECT jsonb_object('{}');
  262. -- empty object, two dimensions
  263. SELECT jsonb_object('{}', '{}');
  264. -- one dimension
  265. SELECT jsonb_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
  266. -- same but with two dimensions
  267. SELECT jsonb_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
  268. -- odd number error
  269. SELECT jsonb_object('{a,b,c}');
  270. -- one column error
  271. SELECT jsonb_object('{{a},{b}}');
  272. -- too many columns error
  273. SELECT jsonb_object('{{a,b,c},{b,c,d}}');
  274. -- too many dimensions error
  275. SELECT jsonb_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
  276. --two argument form of jsonb_object
  277. select jsonb_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
  278. -- too many dimensions
  279. SELECT jsonb_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}', '{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
  280. -- mismatched dimensions
  281. select jsonb_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
  282. select jsonb_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
  283. -- null key error
  284. select jsonb_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
  285. -- empty key is allowed
  286. select jsonb_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
  287. -- extract_path, extract_path_as_text
  288. SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
  289. SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
  290. SELECT jsonb_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
  291. SELECT jsonb_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
  292. SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
  293. SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
  294. SELECT jsonb_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
  295. SELECT jsonb_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
  296. -- extract_path nulls
  297. SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') IS NULL AS expect_false;
  298. SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') IS NULL AS expect_true;
  299. SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') IS NULL AS expect_false;
  300. SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') IS NULL AS expect_true;
  301. SELECT * FROM jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]') q;
  302. SELECT * FROM jsonb_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
  303. -- test type info caching in jsonb_populate_record()
  304. CREATE TEMP TABLE jsbpoptest (js jsonb);
  305. INSERT INTO jsbpoptest
  306. SELECT '{
  307. "jsa": [1, "2", null, 4],
  308. "rec": {"a": "abc", "c": "01.02.2003", "x": 43.2},
  309. "reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]
  310. }'::jsonb
  311. FROM generate_series(1, 3);
  312. -- indexing
  313. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
  314. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
  315. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
  316. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
  317. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
  318. SELECT count(*) FROM testjsonb WHERE j ? 'public';
  319. SELECT count(*) FROM testjsonb WHERE j ? 'bar';
  320. SELECT count(*) FROM testjsonb WHERE j ?| ARRAY['public','disabled'];
  321. SELECT count(*) FROM testjsonb WHERE j ?& ARRAY['public','disabled'];
  322. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == null';
  323. SELECT count(*) FROM testjsonb WHERE j @@ '"CC" == $.wait';
  324. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == "CC" && true == $.public';
  325. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25';
  326. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25.0';
  327. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($)';
  328. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public)';
  329. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.bar)';
  330. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public) || exists($.disabled)';
  331. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public) && exists($.disabled)';
  332. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
  333. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? ("CC" == @)';
  334. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.wait == "CC" && true == @.public)';
  335. SELECT count(*) FROM testjsonb WHERE j @? '$.age ? (@ == 25)';
  336. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.age == 25.0)';
  337. SELECT count(*) FROM testjsonb WHERE j @? '$';
  338. SELECT count(*) FROM testjsonb WHERE j @? '$.public';
  339. SELECT count(*) FROM testjsonb WHERE j @? '$.bar';
  340. CREATE INDEX jidx ON testjsonb USING gin (j);
  341. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
  342. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
  343. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
  344. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
  345. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
  346. SELECT count(*) FROM testjsonb WHERE j @> '{"array":["foo"]}';
  347. SELECT count(*) FROM testjsonb WHERE j @> '{"array":["bar"]}';
  348. -- exercise GIN_SEARCH_MODE_ALL
  349. SELECT count(*) FROM testjsonb WHERE j @> '{}';
  350. SELECT count(*) FROM testjsonb WHERE j ? 'public';
  351. SELECT count(*) FROM testjsonb WHERE j ? 'bar';
  352. SELECT count(*) FROM testjsonb WHERE j ?| ARRAY['public','disabled'];
  353. SELECT count(*) FROM testjsonb WHERE j ?& ARRAY['public','disabled'];
  354. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == null';
  355. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($ ? (@.wait == null))';
  356. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.wait ? (@ == null))';
  357. SELECT count(*) FROM testjsonb WHERE j @@ '"CC" == $.wait';
  358. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == "CC" && true == $.public';
  359. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25';
  360. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25.0';
  361. SELECT count(*) FROM testjsonb WHERE j @@ '$.array[*] == "foo"';
  362. SELECT count(*) FROM testjsonb WHERE j @@ '$.array[*] == "bar"';
  363. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($ ? (@.array[*] == "bar"))';
  364. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.array ? (@[*] == "bar"))';
  365. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.array[*] ? (@ == "bar"))';
  366. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($)';
  367. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public)';
  368. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.bar)';
  369. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public) || exists($.disabled)';
  370. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public) && exists($.disabled)';
  371. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
  372. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? ("CC" == @)';
  373. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.wait == "CC" && true == @.public)';
  374. SELECT count(*) FROM testjsonb WHERE j @? '$.age ? (@ == 25)';
  375. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.age == 25.0)';
  376. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.array[*] == "bar")';
  377. SELECT count(*) FROM testjsonb WHERE j @? '$.array ? (@[*] == "bar")';
  378. SELECT count(*) FROM testjsonb WHERE j @? '$.array[*] ? (@ == "bar")';
  379. SELECT count(*) FROM testjsonb WHERE j @? '$';
  380. SELECT count(*) FROM testjsonb WHERE j @? '$.public';
  381. SELECT count(*) FROM testjsonb WHERE j @? '$.bar';
  382. SELECT count(*) from testjsonb WHERE j->'array' ? 'bar';
  383. -- type sensitive array exists - should return no rows (since "exists" only
  384. -- matches strings that are either object keys or array elements)
  385. SELECT count(*) from testjsonb WHERE j->'array' ? '5'::text;
  386. -- However, a raw scalar is *contained* within the array
  387. SELECT count(*) from testjsonb WHERE j->'array' @> '5'::jsonb;
  388. -- btree
  389. CREATE INDEX jidx ON testjsonb USING btree (j);
  390. SELECT count(*) FROM testjsonb WHERE j > '{"p":1}';
  391. SELECT count(*) FROM testjsonb WHERE j = '{"pos":98, "line":371, "node":"CBA", "indexed":true}';
  392. CREATE INDEX jidx ON testjsonb USING gin (j jsonb_path_ops);
  393. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
  394. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
  395. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
  396. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
  397. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
  398. -- exercise GIN_SEARCH_MODE_ALL
  399. SELECT count(*) FROM testjsonb WHERE j @> '{}';
  400. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == null';
  401. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($ ? (@.wait == null))';
  402. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.wait ? (@ == null))';
  403. SELECT count(*) FROM testjsonb WHERE j @@ '"CC" == $.wait';
  404. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == "CC" && true == $.public';
  405. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25';
  406. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25.0';
  407. SELECT count(*) FROM testjsonb WHERE j @@ '$.array[*] == "foo"';
  408. SELECT count(*) FROM testjsonb WHERE j @@ '$.array[*] == "bar"';
  409. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($ ? (@.array[*] == "bar"))';
  410. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.array ? (@[*] == "bar"))';
  411. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.array[*] ? (@ == "bar"))';
  412. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($)';
  413. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
  414. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? ("CC" == @)';
  415. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.wait == "CC" && true == @.public)';
  416. SELECT count(*) FROM testjsonb WHERE j @? '$.age ? (@ == 25)';
  417. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.age == 25.0)';
  418. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.array[*] == "bar")';
  419. SELECT count(*) FROM testjsonb WHERE j @? '$.array ? (@[*] == "bar")';
  420. SELECT count(*) FROM testjsonb WHERE j @? '$.array[*] ? (@ == "bar")';
  421. SELECT count(*) FROM testjsonb WHERE j @? '$';
  422. SELECT count(*) FROM testjsonb WHERE j @? '$.public';
  423. SELECT count(*) FROM testjsonb WHERE j @? '$.bar';
  424. -- nested tests
  425. SELECT '{"ff":{"a":12,"b":16}}'::jsonb;
  426. SELECT '{"ff":{"a":12,"b":16},"qq":123}'::jsonb;
  427. SELECT '{"aa":["a","aaa"],"qq":{"a":12,"b":16,"c":["c1","c2"],"d":{"d1":"d1","d2":"d2","d1":"d3"}}}'::jsonb;
  428. SELECT '{"ff":["a","aaa"]}'::jsonb;
  429. SELECT
  430. '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'ff',
  431. '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'qq',
  432. ('{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'Y') IS NULL AS f,
  433. ('{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb ->> 'Y') IS NULL AS t,
  434. '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'x';
  435. -- nested containment
  436. SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[1,2]}';
  437. SELECT '{"a":[2,1],"c":"b"}'::jsonb @> '{"a":[1,2]}';
  438. SELECT '{"a":{"1":2},"c":"b"}'::jsonb @> '{"a":[1,2]}';
  439. SELECT '{"a":{"2":1},"c":"b"}'::jsonb @> '{"a":[1,2]}';
  440. SELECT '{"a":{"1":2},"c":"b"}'::jsonb @> '{"a":{"1":2}}';
  441. SELECT '{"a":{"2":1},"c":"b"}'::jsonb @> '{"a":{"1":2}}';
  442. SELECT '["a","b"]'::jsonb @> '["a","b","c","b"]';
  443. SELECT '["a","b","c","b"]'::jsonb @> '["a","b"]';
  444. SELECT '["a","b","c",[1,2]]'::jsonb @> '["a",[1,2]]';
  445. SELECT '["a","b","c",[1,2]]'::jsonb @> '["b",[1,2]]';
  446. SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[1]}';
  447. SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[2]}';
  448. SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[3]}';
  449. SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"c":3}]}';
  450. SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4}]}';
  451. SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},3]}';
  452. SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},1]}';
  453. -- check some corner cases for indexed nested containment (bug #13756)
  454. create temp table nestjsonb (j jsonb);
  455. insert into nestjsonb (j) values ('{"a":[["b",{"x":1}],["b",{"x":2}]],"c":3}');
  456. insert into nestjsonb (j) values ('[[14,2,3]]');
  457. insert into nestjsonb (j) values ('[1,[14,2,3]]');
  458. create index on nestjsonb using gin(j jsonb_path_ops);
  459. select * from nestjsonb where j @> '{"a":[[{"x":2}]]}'::jsonb;
  460. select * from nestjsonb where j @> '{"c":3}';
  461. select * from nestjsonb where j @> '[[14]]';
  462. select * from nestjsonb where j @> '{"a":[[{"x":2}]]}'::jsonb;
  463. select * from nestjsonb where j @> '{"c":3}';
  464. select * from nestjsonb where j @> '[[14]]';
  465. -- nested object field / array index lookup
  466. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'n';
  467. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'a';
  468. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'b';
  469. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'c';
  470. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'd';
  471. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'd' -> '1';
  472. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'e';
  473. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 0; --expecting error
  474. SELECT '["a","b","c",[1,2],null]'::jsonb -> 0;
  475. SELECT '["a","b","c",[1,2],null]'::jsonb -> 1;
  476. SELECT '["a","b","c",[1,2],null]'::jsonb -> 2;
  477. SELECT '["a","b","c",[1,2],null]'::jsonb -> 3;
  478. SELECT '["a","b","c",[1,2],null]'::jsonb -> 3 -> 1;
  479. SELECT '["a","b","c",[1,2],null]'::jsonb -> 4;
  480. SELECT '["a","b","c",[1,2],null]'::jsonb -> 5;
  481. SELECT '["a","b","c",[1,2],null]'::jsonb -> -1;
  482. SELECT '["a","b","c",[1,2],null]'::jsonb -> -5;
  483. SELECT '["a","b","c",[1,2],null]'::jsonb -> -6;
  484. --nested exists
  485. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'n';
  486. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'a';
  487. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'b';
  488. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'c';
  489. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'd';
  490. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'e';
  491. -- jsonb_strip_nulls
  492. select jsonb_strip_nulls(null);
  493. select jsonb_strip_nulls('1');
  494. select jsonb_strip_nulls('"a string"');
  495. select jsonb_strip_nulls('null');
  496. select jsonb_strip_nulls('[1,2,null,3,4]');
  497. select jsonb_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
  498. select jsonb_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
  499. -- an empty object is not null and should not be stripped
  500. select jsonb_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
  501. select jsonb_concat('{"d": "test", "a": [1, 2]}', '{"g": "test2", "c": {"c1":1, "c2":2}}');
  502. select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"cq":"l", "b":"g", "fg":false}';
  503. select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"aq":"l"}';
  504. select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"aa":"l"}';
  505. select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{}';
  506. select '["a", "b"]'::jsonb || '["c"]';
  507. select '["a", "b"]'::jsonb || '["c", "d"]';
  508. select '["c"]' || '["a", "b"]'::jsonb;
  509. select '["a", "b"]'::jsonb || '"c"';
  510. select '"c"' || '["a", "b"]'::jsonb;
  511. select '[]'::jsonb || '["a"]'::jsonb;
  512. select '[]'::jsonb || '"a"'::jsonb;
  513. select '"b"'::jsonb || '"a"'::jsonb;
  514. select '{}'::jsonb || '{"a":"b"}'::jsonb;
  515. select '[]'::jsonb || '{"a":"b"}'::jsonb;
  516. select '{"a":"b"}'::jsonb || '[]'::jsonb;
  517. select '"a"'::jsonb || '{"a":1}';
  518. select '{"a":1}' || '"a"'::jsonb;
  519. select '[3]'::jsonb || '{}'::jsonb;
  520. select '3'::jsonb || '[]'::jsonb;
  521. select '3'::jsonb || '4'::jsonb;
  522. select '3'::jsonb || '{}'::jsonb;
  523. select '["a", "b"]'::jsonb || '{"c":1}';
  524. select '{"c": 1}'::jsonb || '["a", "b"]';
  525. select '{}'::jsonb || '{"cq":"l", "b":"g", "fg":false}';
  526. select pg_column_size('{}'::jsonb || '{}'::jsonb) = pg_column_size('{}'::jsonb);
  527. select pg_column_size('{"aa":1}'::jsonb || '{"b":2}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
  528. select pg_column_size('{"aa":1, "b":2}'::jsonb || '{}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
  529. select pg_column_size('{}'::jsonb || '{"aa":1, "b":2}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
  530. select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'a');
  531. select jsonb_delete('{"a":null , "b":2, "c":3}'::jsonb, 'a');
  532. select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'b');
  533. select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'c');
  534. select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'd');
  535. select '{"a":1 , "b":2, "c":3}'::jsonb - 'a';
  536. select '{"a":null , "b":2, "c":3}'::jsonb - 'a';
  537. select '{"a":1 , "b":2, "c":3}'::jsonb - 'b';
  538. select '{"a":1 , "b":2, "c":3}'::jsonb - 'c';
  539. select '{"a":1 , "b":2, "c":3}'::jsonb - 'd';
  540. select pg_column_size('{"a":1 , "b":2, "c":3}'::jsonb - 'b') = pg_column_size('{"a":1, "b":2}'::jsonb);
  541. select '["a","b","c"]'::jsonb - 3;
  542. select '["a","b","c"]'::jsonb - 2;
  543. select '["a","b","c"]'::jsonb - 1;
  544. select '["a","b","c"]'::jsonb - 0;
  545. select '["a","b","c"]'::jsonb - -1;
  546. select '["a","b","c"]'::jsonb - -2;
  547. select '["a","b","c"]'::jsonb - -3;
  548. select '["a","b","c"]'::jsonb - -4;
  549. select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{n}');
  550. select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{b,-1}');
  551. select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{d,1,0}');
  552. select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{n}';
  553. select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{b,-1}';
  554. select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{b,-1e}'; -- invalid array subscript
  555. select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{d,1,0}';
  556. -- empty structure and error conditions for delete and replace
  557. select '"a"'::jsonb - 'a'; -- error
  558. select '{}'::jsonb - 'a';
  559. select '[]'::jsonb - 'a';
  560. select '"a"'::jsonb - 1; -- error
  561. select '{}'::jsonb - 1; -- error
  562. select '[]'::jsonb - 1;
  563. select '"a"'::jsonb #- '{a}'; -- error
  564. select '{}'::jsonb #- '{a}';
  565. select '[]'::jsonb #- '{a}';
  566. select jsonb_set('{}','{a}','"b"', false);
  567. select jsonb_set('[]','{1}','"b"', false);
  568. select jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{0}','[2,3,4]', false);
  569. -- jsonb_set_lax
  570. \pset null NULL
  571. -- errors
  572. select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, true, null);
  573. select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, true, 'no_such_treatment');
  574. \pset null ''
  575. select jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"', true);
  576. select jsonb_insert('{"a": {"b": {"c": [0, 1, "test1", "test2"]}}}', '{a, b, c, 2}', '"new_value"', true);
  577. select jsonb_insert('{"a": [0,1,2]}', '{a, 0}', '"new_value"', true);
  578. select jsonb_insert('{"a": [0,1,2]}', '{a, 2}', '"new_value"', true);
  579. select jsonb_insert('{"a": [0,1,2]}', '{a, -1}', '"new_value"', true);
  580. select jsonb_insert('[]', '{1}', '"new_value"', true);
  581. select jsonb_insert('{"a": []}', '{a, 1}', '"new_value"', true);
  582. select jsonb_insert('{"a": {"b": "value"}}', '{a, c}', '"new_value"', true);
  583. select jsonb_insert('{"a": {"b": "value"}}', '{a, b}', '"new_value"', true);
  584. create TEMP TABLE test_jsonb_subscript (
  585. id int,
  586. test_json jsonb
  587. );
  588. insert into test_jsonb_subscript values
  589. (1, '{}'), -- empty jsonb
  590. (2, '{"key": "value"}'); -- jsonb with data
  591. -- NULL as jsonb source
  592. insert into test_jsonb_subscript values (3, NULL);
  593. insert into test_jsonb_subscript values (1, '[0]');
  594. insert into test_jsonb_subscript values (1, '[]');
  595. insert into test_jsonb_subscript values (1, '{}');
  596. insert into test_jsonb_subscript values (1, '{}');
  597. insert into test_jsonb_subscript values (1, '{"b": 1}');
  598. insert into test_jsonb_subscript values (1, '{}');
  599. insert into test_jsonb_subscript values (1, '[]');
  600. insert into test_jsonb_subscript values (1, '{}');
  601. insert into test_jsonb_subscript values (1, '[]');
  602. insert into test_jsonb_subscript values (1, '{}');
  603. insert into test_jsonb_subscript values (1, '{"a": {}}');
  604. insert into test_jsonb_subscript values (1, '{"a": []}');
  605. insert into test_jsonb_subscript values (1, '{"a": 1}');
  606. insert into test_jsonb_subscript values (1, 'null');
  607. -- casts
  608. select 'true'::jsonb::bool;
  609. select '[]'::jsonb::bool;
  610. select '1.0'::jsonb::float;
  611. select '[1.0]'::jsonb::float;
  612. select '12345'::jsonb::int4;
  613. select '"hello"'::jsonb::int4;
  614. select '12345'::jsonb::numeric;
  615. select '{}'::jsonb::numeric;
  616. select '12345.05'::jsonb::numeric;
  617. select '12345.05'::jsonb::float4;
  618. select '12345.05'::jsonb::float8;
  619. select '12345.05'::jsonb::int2;
  620. select '12345.05'::jsonb::int4;
  621. select '12345.05'::jsonb::int8;
  622. select '12345.0000000000000000000000000000000000000000000005'::jsonb::numeric;
  623. select '12345.0000000000000000000000000000000000000000000005'::jsonb::float4;
  624. select '12345.0000000000000000000000000000000000000000000005'::jsonb::float8;
  625. select '12345.0000000000000000000000000000000000000000000005'::jsonb::int2;
  626. select '12345.0000000000000000000000000000000000000000000005'::jsonb::int4;
  627. select '12345.0000000000000000000000000000000000000000000005'::jsonb::int8;