jsonb.sql 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  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 '1e100'::jsonb; -- OK
  18. SELECT '1.3e100'::jsonb; -- OK
  19. SELECT '1f2'::jsonb; -- ERROR
  20. SELECT '0.x1'::jsonb; -- ERROR
  21. SELECT '1.3ex100'::jsonb; -- ERROR
  22. -- Arrays.
  23. SELECT '[]'::jsonb; -- OK
  24. SELECT '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'::jsonb; -- OK
  25. SELECT '[1,2]'::jsonb; -- OK
  26. SELECT '[1,2,]'::jsonb; -- ERROR, trailing comma
  27. SELECT '[1,2'::jsonb; -- ERROR, no closing bracket
  28. SELECT '[1,[2]'::jsonb; -- ERROR, no closing bracket
  29. -- Objects.
  30. SELECT '{}'::jsonb; -- OK
  31. SELECT '{"abc"}'::jsonb; -- ERROR, no value
  32. SELECT '{"abc":1}'::jsonb; -- OK
  33. SELECT '{1:"abc"}'::jsonb; -- ERROR, keys must be strings
  34. SELECT '{"abc",1}'::jsonb; -- ERROR, wrong separator
  35. SELECT '{"abc"=1}'::jsonb; -- ERROR, totally wrong separator
  36. SELECT '{"abc"::1}'::jsonb; -- ERROR, another wrong separator
  37. SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::jsonb; -- OK
  38. SELECT '{"abc":1:2}'::jsonb; -- ERROR, colon in wrong spot
  39. SELECT '{"abc":1,3}'::jsonb; -- ERROR, no value
  40. -- Recursion.
  41. SET max_stack_depth = '100kB';
  42. SELECT repeat('[', 10000)::jsonb;
  43. SELECT repeat('{"a":', 10000)::jsonb;
  44. RESET max_stack_depth;
  45. -- Miscellaneous stuff.
  46. SELECT 'true'::jsonb; -- OK
  47. SELECT 'false'::jsonb; -- OK
  48. SELECT 'null'::jsonb; -- OK
  49. SELECT ' true '::jsonb; -- OK, even with extra whitespace
  50. SELECT 'true false'::jsonb; -- ERROR, too many values
  51. SELECT 'true, false'::jsonb; -- ERROR, too many values
  52. SELECT 'truf'::jsonb; -- ERROR, not a keyword
  53. SELECT 'trues'::jsonb; -- ERROR, not a keyword
  54. SELECT ''::jsonb; -- ERROR, no value
  55. SELECT ' '::jsonb; -- ERROR, no value
  56. -- Multi-line JSON input to check ERROR reporting
  57. SELECT '{
  58. "one": 1,
  59. "two":"two",
  60. "three":
  61. true}'::jsonb; -- OK
  62. SELECT '{
  63. "one": 1,
  64. "two":,"two", -- ERROR extraneous comma before field "two"
  65. "three":
  66. true}'::jsonb;
  67. SELECT '{
  68. "one": 1,
  69. "two":"two",
  70. "averyveryveryveryveryveryveryveryveryverylongfieldname":}'::jsonb;
  71. -- ERROR missing value for last field
  72. -- make sure jsonb is passed through json generators without being escaped
  73. SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']);
  74. -- anyarray column
  75. CREATE TEMP TABLE rows AS
  76. SELECT x, 'txt' || x as y
  77. FROM generate_series(1,3) AS x;
  78. analyze rows;
  79. select attname, to_jsonb(histogram_bounds) histogram_bounds
  80. from pg_stats
  81. where tablename = 'rows' and
  82. schemaname = pg_my_temp_schema()::regnamespace::text
  83. order by 1;
  84. -- to_jsonb, timestamps
  85. select to_jsonb(timestamp '2014-05-28 12:22:35.614298');
  86. BEGIN;
  87. SET LOCAL TIME ZONE 10.5;
  88. select to_jsonb(timestamptz '2014-05-28 12:22:35.614298-04');
  89. SET LOCAL TIME ZONE -8;
  90. select to_jsonb(timestamptz '2014-05-28 12:22:35.614298-04');
  91. COMMIT;
  92. select to_jsonb(date '2014-05-28');
  93. select to_jsonb(date 'Infinity');
  94. select to_jsonb(date '-Infinity');
  95. select to_jsonb(timestamp 'Infinity');
  96. select to_jsonb(timestamp '-Infinity');
  97. select to_jsonb(timestamptz 'Infinity');
  98. select to_jsonb(timestamptz '-Infinity');
  99. --jsonb_agg
  100. SELECT jsonb_agg(q)
  101. FROM ( SELECT $$a$$ || x AS b, y AS c,
  102. ARRAY[ROW(x.*,ARRAY[1,2,3]),
  103. ROW(y.*,ARRAY[4,5,6])] AS z
  104. FROM generate_series(1,2) x,
  105. generate_series(4,5) y) q;
  106. SELECT jsonb_agg(q ORDER BY x, y)
  107. FROM rows q;
  108. UPDATE rows SET x = NULL WHERE x = 1;
  109. SELECT jsonb_agg(q ORDER BY x NULLS FIRST, y)
  110. FROM rows q;
  111. -- jsonb extraction functions
  112. CREATE TEMP TABLE test_jsonb (
  113. json_type text,
  114. test_json jsonb
  115. );
  116. INSERT INTO test_jsonb VALUES
  117. ('scalar','"a scalar"'),
  118. ('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
  119. ('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
  120. SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'scalar';
  121. SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'array';
  122. SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'object';
  123. SELECT test_json -> 'field2' FROM test_jsonb WHERE json_type = 'object';
  124. SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'scalar';
  125. SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'array';
  126. SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'object';
  127. SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'scalar';
  128. SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'array';
  129. SELECT test_json -> 9 FROM test_jsonb WHERE json_type = 'array';
  130. SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'object';
  131. SELECT test_json ->> 6 FROM test_jsonb WHERE json_type = 'array';
  132. SELECT test_json ->> 7 FROM test_jsonb WHERE json_type = 'array';
  133. SELECT test_json ->> 'field4' FROM test_jsonb WHERE json_type = 'object';
  134. SELECT test_json ->> 'field5' FROM test_jsonb WHERE json_type = 'object';
  135. SELECT test_json ->> 'field6' FROM test_jsonb WHERE json_type = 'object';
  136. SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'scalar';
  137. SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'array';
  138. SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'object';
  139. SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'scalar';
  140. SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'array';
  141. SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'object';
  142. -- nulls
  143. SELECT (test_json->'field3') IS NULL AS expect_false FROM test_jsonb WHERE json_type = 'object';
  144. SELECT (test_json->>'field3') IS NULL AS expect_true FROM test_jsonb WHERE json_type = 'object';
  145. SELECT (test_json->3) IS NULL AS expect_false FROM test_jsonb WHERE json_type = 'array';
  146. SELECT (test_json->>3) IS NULL AS expect_true FROM test_jsonb WHERE json_type = 'array';
  147. -- corner cases
  148. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::text;
  149. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::int;
  150. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 1;
  151. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 'z';
  152. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> '';
  153. select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 1;
  154. select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 3;
  155. select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 'z';
  156. select '{"a": "c", "b": null}'::jsonb -> 'b';
  157. select '"foo"'::jsonb -> 1;
  158. select '"foo"'::jsonb -> 'z';
  159. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::text;
  160. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::int;
  161. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 1;
  162. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 'z';
  163. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> '';
  164. select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 1;
  165. select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 3;
  166. select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 'z';
  167. select '{"a": "c", "b": null}'::jsonb ->> 'b';
  168. select '"foo"'::jsonb ->> 1;
  169. select '"foo"'::jsonb ->> 'z';
  170. -- equality and inequality
  171. SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb;
  172. SELECT '{"x":"y"}'::jsonb = '{"x":"z"}'::jsonb;
  173. SELECT '{"x":"y"}'::jsonb <> '{"x":"y"}'::jsonb;
  174. SELECT '{"x":"y"}'::jsonb <> '{"x":"z"}'::jsonb;
  175. -- containment
  176. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b"}');
  177. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "c":null}');
  178. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "g":null}');
  179. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"g":null}');
  180. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"c"}');
  181. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b"}');
  182. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "c":"q"}');
  183. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b"}';
  184. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "c":null}';
  185. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "g":null}';
  186. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"g":null}';
  187. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"c"}';
  188. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b"}';
  189. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "c":"q"}';
  190. SELECT '[1,2]'::jsonb @> '[1,2,2]'::jsonb;
  191. SELECT '[1,1,2]'::jsonb @> '[1,2,2]'::jsonb;
  192. SELECT '[[1,2]]'::jsonb @> '[[1,2,2]]'::jsonb;
  193. SELECT '[1,2,2]'::jsonb <@ '[1,2]'::jsonb;
  194. SELECT '[1,2,2]'::jsonb <@ '[1,1,2]'::jsonb;
  195. SELECT '[[1,2,2]]'::jsonb <@ '[[1,2]]'::jsonb;
  196. SELECT jsonb_contained('{"a":"b"}', '{"a":"b", "b":1, "c":null}');
  197. SELECT jsonb_contained('{"a":"b", "c":null}', '{"a":"b", "b":1, "c":null}');
  198. SELECT jsonb_contained('{"a":"b", "g":null}', '{"a":"b", "b":1, "c":null}');
  199. SELECT jsonb_contained('{"g":null}', '{"a":"b", "b":1, "c":null}');
  200. SELECT jsonb_contained('{"a":"c"}', '{"a":"b", "b":1, "c":null}');
  201. SELECT jsonb_contained('{"a":"b"}', '{"a":"b", "b":1, "c":null}');
  202. SELECT jsonb_contained('{"a":"b", "c":"q"}', '{"a":"b", "b":1, "c":null}');
  203. SELECT '{"a":"b"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  204. SELECT '{"a":"b", "c":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  205. SELECT '{"a":"b", "g":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  206. SELECT '{"g":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  207. SELECT '{"a":"c"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  208. SELECT '{"a":"b"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  209. SELECT '{"a":"b", "c":"q"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  210. -- Raw scalar may contain another raw scalar, array may contain a raw scalar
  211. SELECT '[5]'::jsonb @> '[5]';
  212. SELECT '5'::jsonb @> '5';
  213. SELECT '[5]'::jsonb @> '5';
  214. -- But a raw scalar cannot contain an array
  215. SELECT '5'::jsonb @> '[5]';
  216. -- In general, one thing should always contain itself. Test array containment:
  217. SELECT '["9", ["7", "3"], 1]'::jsonb @> '["9", ["7", "3"], 1]'::jsonb;
  218. SELECT '["9", ["7", "3"], ["1"]]'::jsonb @> '["9", ["7", "3"], ["1"]]'::jsonb;
  219. -- array containment string matching confusion bug
  220. SELECT '{ "name": "Bob", "tags": [ "enim", "qui"]}'::jsonb @> '{"tags":["qu"]}';
  221. -- array length
  222. SELECT jsonb_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
  223. SELECT jsonb_array_length('[]');
  224. SELECT jsonb_array_length('{"f1":1,"f2":[5,6]}');
  225. SELECT jsonb_array_length('4');
  226. -- each
  227. SELECT jsonb_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}');
  228. SELECT jsonb_each('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
  229. SELECT * FROM jsonb_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
  230. SELECT * FROM jsonb_each('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
  231. SELECT jsonb_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}');
  232. SELECT jsonb_each_text('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
  233. SELECT * FROM jsonb_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
  234. SELECT * FROM jsonb_each_text('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
  235. -- exists
  236. SELECT jsonb_exists('{"a":null, "b":"qq"}', 'a');
  237. SELECT jsonb_exists('{"a":null, "b":"qq"}', 'b');
  238. SELECT jsonb_exists('{"a":null, "b":"qq"}', 'c');
  239. SELECT jsonb_exists('{"a":"null", "b":"qq"}', 'a');
  240. SELECT jsonb '{"a":null, "b":"qq"}' ? 'a';
  241. SELECT jsonb '{"a":null, "b":"qq"}' ? 'b';
  242. SELECT jsonb '{"a":null, "b":"qq"}' ? 'c';
  243. SELECT jsonb '{"a":"null", "b":"qq"}' ? 'a';
  244. -- array exists - array elements should behave as keys
  245. SELECT count(*) from testjsonb WHERE j->'array' ? 'bar';
  246. -- type sensitive array exists - should return no rows (since "exists" only
  247. -- matches strings that are either object keys or array elements)
  248. SELECT count(*) from testjsonb WHERE j->'array' ? '5'::text;
  249. -- However, a raw scalar is *contained* within the array
  250. SELECT count(*) from testjsonb WHERE j->'array' @> '5'::jsonb;
  251. SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['a','b']);
  252. SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['b','a']);
  253. SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['c','a']);
  254. SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['c','d']);
  255. SELECT jsonb_exists_any('{"a":null, "b":"qq"}', '{}'::text[]);
  256. SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['a','b'];
  257. SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['b','a'];
  258. SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['c','a'];
  259. SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['c','d'];
  260. SELECT jsonb '{"a":null, "b":"qq"}' ?| '{}'::text[];
  261. SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['a','b']);
  262. SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['b','a']);
  263. SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['c','a']);
  264. SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['c','d']);
  265. SELECT jsonb_exists_all('{"a":null, "b":"qq"}', '{}'::text[]);
  266. SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['a','b'];
  267. SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['b','a'];
  268. SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['c','a'];
  269. SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['c','d'];
  270. SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['a','a', 'b', 'b', 'b'];
  271. SELECT jsonb '{"a":null, "b":"qq"}' ?& '{}'::text[];
  272. -- typeof
  273. SELECT jsonb_typeof('{}') AS object;
  274. SELECT jsonb_typeof('{"c":3,"p":"o"}') AS object;
  275. SELECT jsonb_typeof('[]') AS array;
  276. SELECT jsonb_typeof('["a", 1]') AS array;
  277. SELECT jsonb_typeof('null') AS "null";
  278. SELECT jsonb_typeof('1') AS number;
  279. SELECT jsonb_typeof('-1') AS number;
  280. SELECT jsonb_typeof('1.0') AS number;
  281. SELECT jsonb_typeof('1e2') AS number;
  282. SELECT jsonb_typeof('-1.0') AS number;
  283. SELECT jsonb_typeof('true') AS boolean;
  284. SELECT jsonb_typeof('false') AS boolean;
  285. SELECT jsonb_typeof('"hello"') AS string;
  286. SELECT jsonb_typeof('"true"') AS string;
  287. SELECT jsonb_typeof('"1.0"') AS string;
  288. -- jsonb_build_array, jsonb_build_object, jsonb_object_agg
  289. SELECT jsonb_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
  290. SELECT jsonb_build_array('a', NULL); -- ok
  291. SELECT jsonb_build_array(VARIADIC NULL::text[]); -- ok
  292. SELECT jsonb_build_array(VARIADIC '{}'::text[]); -- ok
  293. SELECT jsonb_build_array(VARIADIC '{a,b,c}'::text[]); -- ok
  294. SELECT jsonb_build_array(VARIADIC ARRAY['a', NULL]::text[]); -- ok
  295. SELECT jsonb_build_array(VARIADIC '{1,2,3,4}'::text[]); -- ok
  296. SELECT jsonb_build_array(VARIADIC '{1,2,3,4}'::int[]); -- ok
  297. SELECT jsonb_build_array(VARIADIC '{{1,4},{2,5},{3,6}}'::int[][]); -- ok
  298. SELECT jsonb_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
  299. SELECT jsonb_build_object(
  300. 'a', jsonb_build_object('b',false,'c',99),
  301. 'd', jsonb_build_object('e',array[9,8,7]::int[],
  302. 'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r)));
  303. SELECT jsonb_build_object('{a,b,c}'::text[]); -- error
  304. SELECT jsonb_build_object('{a,b,c}'::text[], '{d,e,f}'::text[]); -- error, key cannot be array
  305. SELECT jsonb_build_object('a', 'b', 'c'); -- error
  306. SELECT jsonb_build_object(NULL, 'a'); -- error, key cannot be NULL
  307. SELECT jsonb_build_object('a', NULL); -- ok
  308. SELECT jsonb_build_object(VARIADIC NULL::text[]); -- ok
  309. SELECT jsonb_build_object(VARIADIC '{}'::text[]); -- ok
  310. SELECT jsonb_build_object(VARIADIC '{a,b,c}'::text[]); -- error
  311. SELECT jsonb_build_object(VARIADIC ARRAY['a', NULL]::text[]); -- ok
  312. SELECT jsonb_build_object(VARIADIC ARRAY[NULL, 'a']::text[]); -- error, key cannot be NULL
  313. SELECT jsonb_build_object(VARIADIC '{1,2,3,4}'::text[]); -- ok
  314. SELECT jsonb_build_object(VARIADIC '{1,2,3,4}'::int[]); -- ok
  315. SELECT jsonb_build_object(VARIADIC '{{1,4},{2,5},{3,6}}'::int[][]); -- ok
  316. -- empty objects/arrays
  317. SELECT jsonb_build_array();
  318. SELECT jsonb_build_object();
  319. -- make sure keys are quoted
  320. SELECT jsonb_build_object(1,2);
  321. -- keys must be scalar and not null
  322. SELECT jsonb_build_object(null,2);
  323. SELECT jsonb_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
  324. SELECT jsonb_build_object(json '{"a":1,"b":2}', 3);
  325. SELECT jsonb_build_object('{1,2,3}'::int[], 3);
  326. -- handling of NULL values
  327. SELECT jsonb_object_agg(1, NULL::jsonb);
  328. SELECT jsonb_object_agg(NULL, '{"a":1}');
  329. CREATE TEMP TABLE foo (serial_num int, name text, type text);
  330. INSERT INTO foo VALUES (847001,'t15','GE1043');
  331. INSERT INTO foo VALUES (847002,'t16','GE1043');
  332. INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
  333. SELECT jsonb_build_object('turbines',jsonb_object_agg(serial_num,jsonb_build_object('name',name,'type',type)))
  334. FROM foo;
  335. SELECT jsonb_object_agg(name, type) FROM foo;
  336. INSERT INTO foo VALUES (999999, NULL, 'bar');
  337. SELECT jsonb_object_agg(name, type) FROM foo;
  338. -- jsonb_object
  339. -- empty object, one dimension
  340. SELECT jsonb_object('{}');
  341. -- empty object, two dimensions
  342. SELECT jsonb_object('{}', '{}');
  343. -- one dimension
  344. SELECT jsonb_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
  345. -- same but with two dimensions
  346. SELECT jsonb_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
  347. -- odd number error
  348. SELECT jsonb_object('{a,b,c}');
  349. -- one column error
  350. SELECT jsonb_object('{{a},{b}}');
  351. -- too many columns error
  352. SELECT jsonb_object('{{a,b,c},{b,c,d}}');
  353. -- too many dimensions error
  354. SELECT jsonb_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
  355. --two argument form of jsonb_object
  356. select jsonb_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
  357. -- too many dimensions
  358. 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"}}');
  359. -- mismatched dimensions
  360. select jsonb_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
  361. select jsonb_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
  362. -- null key error
  363. select jsonb_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
  364. -- empty key is allowed
  365. select jsonb_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
  366. -- extract_path, extract_path_as_text
  367. SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
  368. SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
  369. SELECT jsonb_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
  370. SELECT jsonb_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
  371. SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
  372. SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
  373. SELECT jsonb_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
  374. SELECT jsonb_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
  375. -- extract_path nulls
  376. SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') IS NULL AS expect_false;
  377. SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') IS NULL AS expect_true;
  378. SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') IS NULL AS expect_false;
  379. SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') IS NULL AS expect_true;
  380. -- extract_path operators
  381. SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f4','f6'];
  382. SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2'];
  383. SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2','0'];
  384. SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2','1'];
  385. SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f4','f6'];
  386. SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2'];
  387. SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','0'];
  388. SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','1'];
  389. -- corner cases for same
  390. select '{"a": {"b":{"c": "foo"}}}'::jsonb #> '{}';
  391. select '[1,2,3]'::jsonb #> '{}';
  392. select '"foo"'::jsonb #> '{}';
  393. select '42'::jsonb #> '{}';
  394. select 'null'::jsonb #> '{}';
  395. select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a'];
  396. select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', null];
  397. select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', ''];
  398. select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b'];
  399. select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b','c'];
  400. select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b','c','d'];
  401. select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','z','c'];
  402. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #> array['a','1','b'];
  403. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #> array['a','z','b'];
  404. select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['1','b'];
  405. select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['z','b'];
  406. select '[{"b": "c"}, {"b": null}]'::jsonb #> array['1','b'];
  407. select '"foo"'::jsonb #> array['z'];
  408. select '42'::jsonb #> array['f2'];
  409. select '42'::jsonb #> array['0'];
  410. select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> '{}';
  411. select '[1,2,3]'::jsonb #>> '{}';
  412. select '"foo"'::jsonb #>> '{}';
  413. select '42'::jsonb #>> '{}';
  414. select 'null'::jsonb #>> '{}';
  415. select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a'];
  416. select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', null];
  417. select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', ''];
  418. select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b'];
  419. select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b','c'];
  420. select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b','c','d'];
  421. select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','z','c'];
  422. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #>> array['a','1','b'];
  423. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #>> array['a','z','b'];
  424. select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['1','b'];
  425. select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['z','b'];
  426. select '[{"b": "c"}, {"b": null}]'::jsonb #>> array['1','b'];
  427. select '"foo"'::jsonb #>> array['z'];
  428. select '42'::jsonb #>> array['f2'];
  429. select '42'::jsonb #>> array['0'];
  430. -- array_elements
  431. SELECT jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]');
  432. SELECT * FROM jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]') q;
  433. SELECT jsonb_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
  434. SELECT * FROM jsonb_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
  435. -- populate_record
  436. CREATE TYPE jbpop AS (a text, b int, c timestamp);
  437. CREATE DOMAIN jsb_int_not_null AS int NOT NULL;
  438. CREATE DOMAIN jsb_int_array_1d AS int[] CHECK(array_length(VALUE, 1) = 3);
  439. CREATE DOMAIN jsb_int_array_2d AS int[][] CHECK(array_length(VALUE, 2) = 3);
  440. create type jb_unordered_pair as (x int, y int);
  441. create domain jb_ordered_pair as jb_unordered_pair check((value).x <= (value).y);
  442. CREATE TYPE jsbrec AS (
  443. i int,
  444. ia _int4,
  445. ia1 int[],
  446. ia2 int[][],
  447. ia3 int[][][],
  448. ia1d jsb_int_array_1d,
  449. ia2d jsb_int_array_2d,
  450. t text,
  451. ta text[],
  452. c char(10),
  453. ca char(10)[],
  454. ts timestamp,
  455. js json,
  456. jsb jsonb,
  457. jsa json[],
  458. rec jbpop,
  459. reca jbpop[]
  460. );
  461. CREATE TYPE jsbrec_i_not_null AS (
  462. i jsb_int_not_null
  463. );
  464. SELECT * FROM jsonb_populate_record(NULL::jbpop,'{"a":"blurfl","x":43.2}') q;
  465. SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":"blurfl","x":43.2}') q;
  466. SELECT * FROM jsonb_populate_record(NULL::jbpop,'{"a":"blurfl","x":43.2}') q;
  467. SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":"blurfl","x":43.2}') q;
  468. SELECT * FROM jsonb_populate_record(NULL::jbpop,'{"a":[100,200,false],"x":43.2}') q;
  469. SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":[100,200,false],"x":43.2}') q;
  470. SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"c":[100,200,false],"x":43.2}') q;
  471. SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop, '{}') q;
  472. SELECT i FROM jsonb_populate_record(NULL::jsbrec_i_not_null, '{"x": 43.2}') q;
  473. SELECT i FROM jsonb_populate_record(NULL::jsbrec_i_not_null, '{"i": null}') q;
  474. SELECT i FROM jsonb_populate_record(NULL::jsbrec_i_not_null, '{"i": 12345}') q;
  475. SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": null}') q;
  476. SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": 123}') q;
  477. SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": [1, "2", null, 4]}') q;
  478. SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": [[1, 2], [3, 4]]}') q;
  479. SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": [[1], 2]}') q;
  480. SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": [[1], [2, 3]]}') q;
  481. SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": "{1,2,3}"}') q;
  482. SELECT ia1 FROM jsonb_populate_record(NULL::jsbrec, '{"ia1": null}') q;
  483. SELECT ia1 FROM jsonb_populate_record(NULL::jsbrec, '{"ia1": 123}') q;
  484. SELECT ia1 FROM jsonb_populate_record(NULL::jsbrec, '{"ia1": [1, "2", null, 4]}') q;
  485. SELECT ia1 FROM jsonb_populate_record(NULL::jsbrec, '{"ia1": [[1, 2, 3]]}') q;
  486. SELECT ia1d FROM jsonb_populate_record(NULL::jsbrec, '{"ia1d": null}') q;
  487. SELECT ia1d FROM jsonb_populate_record(NULL::jsbrec, '{"ia1d": 123}') q;
  488. SELECT ia1d FROM jsonb_populate_record(NULL::jsbrec, '{"ia1d": [1, "2", null, 4]}') q;
  489. SELECT ia1d FROM jsonb_populate_record(NULL::jsbrec, '{"ia1d": [1, "2", null]}') q;
  490. SELECT ia2 FROM jsonb_populate_record(NULL::jsbrec, '{"ia2": [1, "2", null, 4]}') q;
  491. SELECT ia2 FROM jsonb_populate_record(NULL::jsbrec, '{"ia2": [[1, 2], [null, 4]]}') q;
  492. SELECT ia2 FROM jsonb_populate_record(NULL::jsbrec, '{"ia2": [[], []]}') q;
  493. SELECT ia2 FROM jsonb_populate_record(NULL::jsbrec, '{"ia2": [[1, 2], [3]]}') q;
  494. SELECT ia2 FROM jsonb_populate_record(NULL::jsbrec, '{"ia2": [[1, 2], 3, 4]}') q;
  495. SELECT ia2d FROM jsonb_populate_record(NULL::jsbrec, '{"ia2d": [[1, "2"], [null, 4]]}') q;
  496. SELECT ia2d FROM jsonb_populate_record(NULL::jsbrec, '{"ia2d": [[1, "2", 3], [null, 5, 6]]}') q;
  497. SELECT ia3 FROM jsonb_populate_record(NULL::jsbrec, '{"ia3": [1, "2", null, 4]}') q;
  498. SELECT ia3 FROM jsonb_populate_record(NULL::jsbrec, '{"ia3": [[1, 2], [null, 4]]}') q;
  499. SELECT ia3 FROM jsonb_populate_record(NULL::jsbrec, '{"ia3": [ [[], []], [[], []], [[], []] ]}') q;
  500. SELECT ia3 FROM jsonb_populate_record(NULL::jsbrec, '{"ia3": [ [[1, 2]], [[3, 4]] ]}') q;
  501. SELECT ia3 FROM jsonb_populate_record(NULL::jsbrec, '{"ia3": [ [[1, 2], [3, 4]], [[5, 6], [7, 8]] ]}') q;
  502. SELECT ia3 FROM jsonb_populate_record(NULL::jsbrec, '{"ia3": [ [[1, 2], [3, 4]], [[5, 6], [7, 8], [9, 10]] ]}') q;
  503. SELECT ta FROM jsonb_populate_record(NULL::jsbrec, '{"ta": null}') q;
  504. SELECT ta FROM jsonb_populate_record(NULL::jsbrec, '{"ta": 123}') q;
  505. SELECT ta FROM jsonb_populate_record(NULL::jsbrec, '{"ta": [1, "2", null, 4]}') q;
  506. SELECT ta FROM jsonb_populate_record(NULL::jsbrec, '{"ta": [[1, 2, 3], {"k": "v"}]}') q;
  507. SELECT c FROM jsonb_populate_record(NULL::jsbrec, '{"c": null}') q;
  508. SELECT c FROM jsonb_populate_record(NULL::jsbrec, '{"c": "aaa"}') q;
  509. SELECT c FROM jsonb_populate_record(NULL::jsbrec, '{"c": "aaaaaaaaaa"}') q;
  510. SELECT c FROM jsonb_populate_record(NULL::jsbrec, '{"c": "aaaaaaaaaaaaa"}') q;
  511. SELECT ca FROM jsonb_populate_record(NULL::jsbrec, '{"ca": null}') q;
  512. SELECT ca FROM jsonb_populate_record(NULL::jsbrec, '{"ca": 123}') q;
  513. SELECT ca FROM jsonb_populate_record(NULL::jsbrec, '{"ca": [1, "2", null, 4]}') q;
  514. SELECT ca FROM jsonb_populate_record(NULL::jsbrec, '{"ca": ["aaaaaaaaaaaaaaaa"]}') q;
  515. SELECT ca FROM jsonb_populate_record(NULL::jsbrec, '{"ca": [[1, 2, 3], {"k": "v"}]}') q;
  516. SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": null}') q;
  517. SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": true}') q;
  518. SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": 123.45}') q;
  519. SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": "123.45"}') q;
  520. SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": "abc"}') q;
  521. SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": [123, "123", null, {"key": "value"}]}') q;
  522. SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": {"a": "bbb", "b": null, "c": 123.45}}') q;
  523. SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": null}') q;
  524. SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": true}') q;
  525. SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": 123.45}') q;
  526. SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": "123.45"}') q;
  527. SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": "abc"}') q;
  528. SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": [123, "123", null, {"key": "value"}]}') q;
  529. SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": {"a": "bbb", "b": null, "c": 123.45}}') q;
  530. SELECT jsa FROM jsonb_populate_record(NULL::jsbrec, '{"jsa": null}') q;
  531. SELECT jsa FROM jsonb_populate_record(NULL::jsbrec, '{"jsa": 123}') q;
  532. SELECT jsa FROM jsonb_populate_record(NULL::jsbrec, '{"jsa": [1, "2", null, 4]}') q;
  533. SELECT jsa FROM jsonb_populate_record(NULL::jsbrec, '{"jsa": ["aaa", null, [1, 2, "3", {}], { "k" : "v" }]}') q;
  534. SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": 123}') q;
  535. SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": [1, 2]}') q;
  536. SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}') q;
  537. SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": "(abc,42,01.02.2003)"}') q;
  538. SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": 123}') q;
  539. SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": [1, 2]}') q;
  540. SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]}') q;
  541. SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": ["(abc,42,01.02.2003)"]}') q;
  542. SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": "{\"(abc,42,01.02.2003)\"}"}') q;
  543. SELECT rec FROM jsonb_populate_record(
  544. row(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  545. row('x',3,'2012-12-31 15:30:56')::jbpop,NULL)::jsbrec,
  546. '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}'
  547. ) q;
  548. -- anonymous record type
  549. SELECT jsonb_populate_record(null::record, '{"x": 0, "y": 1}');
  550. SELECT jsonb_populate_record(row(1,2), '{"f1": 0, "f2": 1}');
  551. SELECT * FROM
  552. jsonb_populate_record(null::record, '{"x": 776}') AS (x int, y int);
  553. -- composite domain
  554. SELECT jsonb_populate_record(null::jb_ordered_pair, '{"x": 0, "y": 1}');
  555. SELECT jsonb_populate_record(row(1,2)::jb_ordered_pair, '{"x": 0}');
  556. SELECT jsonb_populate_record(row(1,2)::jb_ordered_pair, '{"x": 1, "y": 0}');
  557. -- populate_recordset
  558. SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  559. SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  560. SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  561. SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  562. SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
  563. SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"c":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
  564. SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  565. SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  566. SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
  567. -- anonymous record type
  568. SELECT jsonb_populate_recordset(null::record, '[{"x": 0, "y": 1}]');
  569. SELECT jsonb_populate_recordset(row(1,2), '[{"f1": 0, "f2": 1}]');
  570. SELECT i, jsonb_populate_recordset(row(i,50), '[{"f1":"42"},{"f2":"43"}]')
  571. FROM (VALUES (1),(2)) v(i);
  572. SELECT * FROM
  573. jsonb_populate_recordset(null::record, '[{"x": 776}]') AS (x int, y int);
  574. -- empty array is a corner case
  575. SELECT jsonb_populate_recordset(null::record, '[]');
  576. SELECT jsonb_populate_recordset(row(1,2), '[]');
  577. SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[]') q;
  578. SELECT * FROM
  579. jsonb_populate_recordset(null::record, '[]') AS (x int, y int);
  580. -- composite domain
  581. SELECT jsonb_populate_recordset(null::jb_ordered_pair, '[{"x": 0, "y": 1}]');
  582. SELECT jsonb_populate_recordset(row(1,2)::jb_ordered_pair, '[{"x": 0}, {"y": 3}]');
  583. SELECT jsonb_populate_recordset(row(1,2)::jb_ordered_pair, '[{"x": 1, "y": 0}]');
  584. -- negative cases where the wrong record type is supplied
  585. select * from jsonb_populate_recordset(row(0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text);
  586. select * from jsonb_populate_recordset(row(0::int,0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text);
  587. select * from jsonb_populate_recordset(row(0::int,0::int,0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text);
  588. select * from jsonb_populate_recordset(row(1000000000::int,50::int),'[{"b":"2"},{"a":"3"}]') q (a text, b text);
  589. -- jsonb_to_record and jsonb_to_recordset
  590. select * from jsonb_to_record('{"a":1,"b":"foo","c":"bar"}')
  591. as x(a int, b text, d text);
  592. select * from jsonb_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]')
  593. as x(a int, b text, c boolean);
  594. select *, c is null as c_is_null
  595. from jsonb_to_record('{"a":1, "b":{"c":16, "d":2}, "x":8, "ca": ["1 2", 3], "ia": [[1,2],[3,4]], "r": {"a": "aaa", "b": 123}}'::jsonb)
  596. as t(a int, b jsonb, c text, x int, ca char(5)[], ia int[][], r jbpop);
  597. select *, c is null as c_is_null
  598. from jsonb_to_recordset('[{"a":1, "b":{"c":16, "d":2}, "x":8}]'::jsonb)
  599. as t(a int, b jsonb, c text, x int);
  600. select * from jsonb_to_record('{"ia": null}') as x(ia _int4);
  601. select * from jsonb_to_record('{"ia": 123}') as x(ia _int4);
  602. select * from jsonb_to_record('{"ia": [1, "2", null, 4]}') as x(ia _int4);
  603. select * from jsonb_to_record('{"ia": [[1, 2], [3, 4]]}') as x(ia _int4);
  604. select * from jsonb_to_record('{"ia": [[1], 2]}') as x(ia _int4);
  605. select * from jsonb_to_record('{"ia": [[1], [2, 3]]}') as x(ia _int4);
  606. select * from jsonb_to_record('{"ia2": [1, 2, 3]}') as x(ia2 int[][]);
  607. select * from jsonb_to_record('{"ia2": [[1, 2], [3, 4]]}') as x(ia2 int4[][]);
  608. select * from jsonb_to_record('{"ia2": [[[1], [2], [3]]]}') as x(ia2 int4[][]);
  609. select * from jsonb_to_record('{"out": {"key": 1}}') as x(out json);
  610. select * from jsonb_to_record('{"out": [{"key": 1}]}') as x(out json);
  611. select * from jsonb_to_record('{"out": "{\"key\": 1}"}') as x(out json);
  612. select * from jsonb_to_record('{"out": {"key": 1}}') as x(out jsonb);
  613. select * from jsonb_to_record('{"out": [{"key": 1}]}') as x(out jsonb);
  614. select * from jsonb_to_record('{"out": "{\"key\": 1}"}') as x(out jsonb);
  615. -- test type info caching in jsonb_populate_record()
  616. CREATE TEMP TABLE jsbpoptest (js jsonb);
  617. INSERT INTO jsbpoptest
  618. SELECT '{
  619. "jsa": [1, "2", null, 4],
  620. "rec": {"a": "abc", "c": "01.02.2003", "x": 43.2},
  621. "reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]
  622. }'::jsonb
  623. FROM generate_series(1, 3);
  624. SELECT (jsonb_populate_record(NULL::jsbrec, js)).* FROM jsbpoptest;
  625. DROP TYPE jsbrec;
  626. DROP TYPE jsbrec_i_not_null;
  627. DROP DOMAIN jsb_int_not_null;
  628. DROP DOMAIN jsb_int_array_1d;
  629. DROP DOMAIN jsb_int_array_2d;
  630. DROP DOMAIN jb_ordered_pair;
  631. DROP TYPE jb_unordered_pair;
  632. -- indexing
  633. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
  634. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
  635. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
  636. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
  637. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
  638. SELECT count(*) FROM testjsonb WHERE j ? 'public';
  639. SELECT count(*) FROM testjsonb WHERE j ? 'bar';
  640. SELECT count(*) FROM testjsonb WHERE j ?| ARRAY['public','disabled'];
  641. SELECT count(*) FROM testjsonb WHERE j ?& ARRAY['public','disabled'];
  642. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == null';
  643. SELECT count(*) FROM testjsonb WHERE j @@ '"CC" == $.wait';
  644. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == "CC" && true == $.public';
  645. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25';
  646. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25.0';
  647. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($)';
  648. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public)';
  649. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.bar)';
  650. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public) || exists($.disabled)';
  651. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public) && exists($.disabled)';
  652. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
  653. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? ("CC" == @)';
  654. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.wait == "CC" && true == @.public)';
  655. SELECT count(*) FROM testjsonb WHERE j @? '$.age ? (@ == 25)';
  656. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.age == 25.0)';
  657. SELECT count(*) FROM testjsonb WHERE j @? '$';
  658. SELECT count(*) FROM testjsonb WHERE j @? '$.public';
  659. SELECT count(*) FROM testjsonb WHERE j @? '$.bar';
  660. CREATE INDEX jidx ON testjsonb USING gin (j);
  661. SET enable_seqscan = off;
  662. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
  663. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
  664. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
  665. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
  666. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
  667. SELECT count(*) FROM testjsonb WHERE j @> '{"array":["foo"]}';
  668. SELECT count(*) FROM testjsonb WHERE j @> '{"array":["bar"]}';
  669. -- exercise GIN_SEARCH_MODE_ALL
  670. SELECT count(*) FROM testjsonb WHERE j @> '{}';
  671. SELECT count(*) FROM testjsonb WHERE j ? 'public';
  672. SELECT count(*) FROM testjsonb WHERE j ? 'bar';
  673. SELECT count(*) FROM testjsonb WHERE j ?| ARRAY['public','disabled'];
  674. SELECT count(*) FROM testjsonb WHERE j ?& ARRAY['public','disabled'];
  675. EXPLAIN (COSTS OFF)
  676. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == null';
  677. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == null';
  678. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($ ? (@.wait == null))';
  679. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.wait ? (@ == null))';
  680. SELECT count(*) FROM testjsonb WHERE j @@ '"CC" == $.wait';
  681. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == "CC" && true == $.public';
  682. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25';
  683. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25.0';
  684. SELECT count(*) FROM testjsonb WHERE j @@ '$.array[*] == "foo"';
  685. SELECT count(*) FROM testjsonb WHERE j @@ '$.array[*] == "bar"';
  686. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($ ? (@.array[*] == "bar"))';
  687. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.array ? (@[*] == "bar"))';
  688. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.array[*] ? (@ == "bar"))';
  689. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($)';
  690. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public)';
  691. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.bar)';
  692. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public) || exists($.disabled)';
  693. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public) && exists($.disabled)';
  694. EXPLAIN (COSTS OFF)
  695. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
  696. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
  697. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? ("CC" == @)';
  698. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.wait == "CC" && true == @.public)';
  699. SELECT count(*) FROM testjsonb WHERE j @? '$.age ? (@ == 25)';
  700. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.age == 25.0)';
  701. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.array[*] == "bar")';
  702. SELECT count(*) FROM testjsonb WHERE j @? '$.array ? (@[*] == "bar")';
  703. SELECT count(*) FROM testjsonb WHERE j @? '$.array[*] ? (@ == "bar")';
  704. SELECT count(*) FROM testjsonb WHERE j @? '$';
  705. SELECT count(*) FROM testjsonb WHERE j @? '$.public';
  706. SELECT count(*) FROM testjsonb WHERE j @? '$.bar';
  707. -- array exists - array elements should behave as keys (for GIN index scans too)
  708. CREATE INDEX jidx_array ON testjsonb USING gin((j->'array'));
  709. SELECT count(*) from testjsonb WHERE j->'array' ? 'bar';
  710. -- type sensitive array exists - should return no rows (since "exists" only
  711. -- matches strings that are either object keys or array elements)
  712. SELECT count(*) from testjsonb WHERE j->'array' ? '5'::text;
  713. -- However, a raw scalar is *contained* within the array
  714. SELECT count(*) from testjsonb WHERE j->'array' @> '5'::jsonb;
  715. RESET enable_seqscan;
  716. SELECT count(*) FROM (SELECT (jsonb_each(j)).key FROM testjsonb) AS wow;
  717. SELECT key, count(*) FROM (SELECT (jsonb_each(j)).key FROM testjsonb) AS wow GROUP BY key ORDER BY count DESC, key;
  718. -- sort/hash
  719. SELECT count(distinct j) FROM testjsonb;
  720. SET enable_hashagg = off;
  721. SELECT count(*) FROM (SELECT j FROM (SELECT * FROM testjsonb UNION ALL SELECT * FROM testjsonb) js GROUP BY j) js2;
  722. SET enable_hashagg = on;
  723. SET enable_sort = off;
  724. SELECT count(*) FROM (SELECT j FROM (SELECT * FROM testjsonb UNION ALL SELECT * FROM testjsonb) js GROUP BY j) js2;
  725. SELECT distinct * FROM (values (jsonb '{}' || ''::text),('{}')) v(j);
  726. SET enable_sort = on;
  727. RESET enable_hashagg;
  728. RESET enable_sort;
  729. DROP INDEX jidx;
  730. DROP INDEX jidx_array;
  731. -- btree
  732. CREATE INDEX jidx ON testjsonb USING btree (j);
  733. SET enable_seqscan = off;
  734. SELECT count(*) FROM testjsonb WHERE j > '{"p":1}';
  735. SELECT count(*) FROM testjsonb WHERE j = '{"pos":98, "line":371, "node":"CBA", "indexed":true}';
  736. --gin path opclass
  737. DROP INDEX jidx;
  738. CREATE INDEX jidx ON testjsonb USING gin (j jsonb_path_ops);
  739. SET enable_seqscan = off;
  740. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
  741. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
  742. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
  743. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
  744. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
  745. -- exercise GIN_SEARCH_MODE_ALL
  746. SELECT count(*) FROM testjsonb WHERE j @> '{}';
  747. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == null';
  748. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($ ? (@.wait == null))';
  749. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.wait ? (@ == null))';
  750. SELECT count(*) FROM testjsonb WHERE j @@ '"CC" == $.wait';
  751. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == "CC" && true == $.public';
  752. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25';
  753. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25.0';
  754. SELECT count(*) FROM testjsonb WHERE j @@ '$.array[*] == "foo"';
  755. SELECT count(*) FROM testjsonb WHERE j @@ '$.array[*] == "bar"';
  756. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($ ? (@.array[*] == "bar"))';
  757. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.array ? (@[*] == "bar"))';
  758. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.array[*] ? (@ == "bar"))';
  759. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($)';
  760. EXPLAIN (COSTS OFF)
  761. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
  762. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
  763. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? ("CC" == @)';
  764. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.wait == "CC" && true == @.public)';
  765. SELECT count(*) FROM testjsonb WHERE j @? '$.age ? (@ == 25)';
  766. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.age == 25.0)';
  767. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.array[*] == "bar")';
  768. SELECT count(*) FROM testjsonb WHERE j @? '$.array ? (@[*] == "bar")';
  769. SELECT count(*) FROM testjsonb WHERE j @? '$.array[*] ? (@ == "bar")';
  770. SELECT count(*) FROM testjsonb WHERE j @? '$';
  771. SELECT count(*) FROM testjsonb WHERE j @? '$.public';
  772. SELECT count(*) FROM testjsonb WHERE j @? '$.bar';
  773. RESET enable_seqscan;
  774. DROP INDEX jidx;
  775. -- nested tests
  776. SELECT '{"ff":{"a":12,"b":16}}'::jsonb;
  777. SELECT '{"ff":{"a":12,"b":16},"qq":123}'::jsonb;
  778. SELECT '{"aa":["a","aaa"],"qq":{"a":12,"b":16,"c":["c1","c2"],"d":{"d1":"d1","d2":"d2","d1":"d3"}}}'::jsonb;
  779. SELECT '{"aa":["a","aaa"],"qq":{"a":"12","b":"16","c":["c1","c2"],"d":{"d1":"d1","d2":"d2"}}}'::jsonb;
  780. SELECT '{"aa":["a","aaa"],"qq":{"a":"12","b":"16","c":["c1","c2",["c3"],{"c4":4}],"d":{"d1":"d1","d2":"d2"}}}'::jsonb;
  781. SELECT '{"ff":["a","aaa"]}'::jsonb;
  782. SELECT
  783. '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'ff',
  784. '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'qq',
  785. ('{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'Y') IS NULL AS f,
  786. ('{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb ->> 'Y') IS NULL AS t,
  787. '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'x';
  788. -- nested containment
  789. SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[1,2]}';
  790. SELECT '{"a":[2,1],"c":"b"}'::jsonb @> '{"a":[1,2]}';
  791. SELECT '{"a":{"1":2},"c":"b"}'::jsonb @> '{"a":[1,2]}';
  792. SELECT '{"a":{"2":1},"c":"b"}'::jsonb @> '{"a":[1,2]}';
  793. SELECT '{"a":{"1":2},"c":"b"}'::jsonb @> '{"a":{"1":2}}';
  794. SELECT '{"a":{"2":1},"c":"b"}'::jsonb @> '{"a":{"1":2}}';
  795. SELECT '["a","b"]'::jsonb @> '["a","b","c","b"]';
  796. SELECT '["a","b","c","b"]'::jsonb @> '["a","b"]';
  797. SELECT '["a","b","c",[1,2]]'::jsonb @> '["a",[1,2]]';
  798. SELECT '["a","b","c",[1,2]]'::jsonb @> '["b",[1,2]]';
  799. SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[1]}';
  800. SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[2]}';
  801. SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[3]}';
  802. SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"c":3}]}';
  803. SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4}]}';
  804. SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},3]}';
  805. SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},1]}';
  806. -- check some corner cases for indexed nested containment (bug #13756)
  807. create temp table nestjsonb (j jsonb);
  808. insert into nestjsonb (j) values ('{"a":[["b",{"x":1}],["b",{"x":2}]],"c":3}');
  809. insert into nestjsonb (j) values ('[[14,2,3]]');
  810. insert into nestjsonb (j) values ('[1,[14,2,3]]');
  811. create index on nestjsonb using gin(j jsonb_path_ops);
  812. set enable_seqscan = on;
  813. set enable_bitmapscan = off;
  814. select * from nestjsonb where j @> '{"a":[[{"x":2}]]}'::jsonb;
  815. select * from nestjsonb where j @> '{"c":3}';
  816. select * from nestjsonb where j @> '[[14]]';
  817. set enable_seqscan = off;
  818. set enable_bitmapscan = on;
  819. select * from nestjsonb where j @> '{"a":[[{"x":2}]]}'::jsonb;
  820. select * from nestjsonb where j @> '{"c":3}';
  821. select * from nestjsonb where j @> '[[14]]';
  822. reset enable_seqscan;
  823. reset enable_bitmapscan;
  824. -- nested object field / array index lookup
  825. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'n';
  826. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'a';
  827. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'b';
  828. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'c';
  829. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'd';
  830. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'd' -> '1';
  831. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'e';
  832. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 0; --expecting error
  833. SELECT '["a","b","c",[1,2],null]'::jsonb -> 0;
  834. SELECT '["a","b","c",[1,2],null]'::jsonb -> 1;
  835. SELECT '["a","b","c",[1,2],null]'::jsonb -> 2;
  836. SELECT '["a","b","c",[1,2],null]'::jsonb -> 3;
  837. SELECT '["a","b","c",[1,2],null]'::jsonb -> 3 -> 1;
  838. SELECT '["a","b","c",[1,2],null]'::jsonb -> 4;
  839. SELECT '["a","b","c",[1,2],null]'::jsonb -> 5;
  840. SELECT '["a","b","c",[1,2],null]'::jsonb -> -1;
  841. SELECT '["a","b","c",[1,2],null]'::jsonb -> -5;
  842. SELECT '["a","b","c",[1,2],null]'::jsonb -> -6;
  843. --nested path extraction
  844. SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{0}';
  845. SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{a}';
  846. SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c}';
  847. SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,0}';
  848. SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,1}';
  849. SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,2}';
  850. SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,3}';
  851. SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,-1}';
  852. SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,-3}';
  853. SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,-4}';
  854. SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{0}';
  855. SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{3}';
  856. SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{4}';
  857. SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{4,5}';
  858. --nested exists
  859. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'n';
  860. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'a';
  861. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'b';
  862. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'c';
  863. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'd';
  864. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'e';
  865. -- jsonb_strip_nulls
  866. select jsonb_strip_nulls(null);
  867. select jsonb_strip_nulls('1');
  868. select jsonb_strip_nulls('"a string"');
  869. select jsonb_strip_nulls('null');
  870. select jsonb_strip_nulls('[1,2,null,3,4]');
  871. select jsonb_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
  872. select jsonb_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
  873. -- an empty object is not null and should not be stripped
  874. select jsonb_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
  875. select jsonb_pretty('{"a": "test", "b": [1, 2, 3], "c": "test3", "d":{"dd": "test4", "dd2":{"ddd": "test5"}}}');
  876. select jsonb_pretty('[{"f1":1,"f2":null},2,null,[[{"x":true},6,7],8],3]');
  877. select jsonb_pretty('{"a":["b", "c"], "d": {"e":"f"}}');
  878. select jsonb_concat('{"d": "test", "a": [1, 2]}', '{"g": "test2", "c": {"c1":1, "c2":2}}');
  879. select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"cq":"l", "b":"g", "fg":false}';
  880. select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"aq":"l"}';
  881. select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"aa":"l"}';
  882. select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{}';
  883. select '["a", "b"]'::jsonb || '["c"]';
  884. select '["a", "b"]'::jsonb || '["c", "d"]';
  885. select '["c"]' || '["a", "b"]'::jsonb;
  886. select '["a", "b"]'::jsonb || '"c"';
  887. select '"c"' || '["a", "b"]'::jsonb;
  888. select '[]'::jsonb || '["a"]'::jsonb;
  889. select '[]'::jsonb || '"a"'::jsonb;
  890. select '"b"'::jsonb || '"a"'::jsonb;
  891. select '{}'::jsonb || '{"a":"b"}'::jsonb;
  892. select '[]'::jsonb || '{"a":"b"}'::jsonb;
  893. select '{"a":"b"}'::jsonb || '[]'::jsonb;
  894. select '"a"'::jsonb || '{"a":1}';
  895. select '{"a":1}' || '"a"'::jsonb;
  896. select '[3]'::jsonb || '{}'::jsonb;
  897. select '3'::jsonb || '[]'::jsonb;
  898. select '3'::jsonb || '4'::jsonb;
  899. select '3'::jsonb || '{}'::jsonb;
  900. select '["a", "b"]'::jsonb || '{"c":1}';
  901. select '{"c": 1}'::jsonb || '["a", "b"]';
  902. select '{}'::jsonb || '{"cq":"l", "b":"g", "fg":false}';
  903. select pg_column_size('{}'::jsonb || '{}'::jsonb) = pg_column_size('{}'::jsonb);
  904. select pg_column_size('{"aa":1}'::jsonb || '{"b":2}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
  905. select pg_column_size('{"aa":1, "b":2}'::jsonb || '{}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
  906. select pg_column_size('{}'::jsonb || '{"aa":1, "b":2}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
  907. select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'a');
  908. select jsonb_delete('{"a":null , "b":2, "c":3}'::jsonb, 'a');
  909. select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'b');
  910. select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'c');
  911. select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'd');
  912. select '{"a":1 , "b":2, "c":3}'::jsonb - 'a';
  913. select '{"a":null , "b":2, "c":3}'::jsonb - 'a';
  914. select '{"a":1 , "b":2, "c":3}'::jsonb - 'b';
  915. select '{"a":1 , "b":2, "c":3}'::jsonb - 'c';
  916. select '{"a":1 , "b":2, "c":3}'::jsonb - 'd';
  917. select pg_column_size('{"a":1 , "b":2, "c":3}'::jsonb - 'b') = pg_column_size('{"a":1, "b":2}'::jsonb);
  918. select '["a","b","c"]'::jsonb - 3;
  919. select '["a","b","c"]'::jsonb - 2;
  920. select '["a","b","c"]'::jsonb - 1;
  921. select '["a","b","c"]'::jsonb - 0;
  922. select '["a","b","c"]'::jsonb - -1;
  923. select '["a","b","c"]'::jsonb - -2;
  924. select '["a","b","c"]'::jsonb - -3;
  925. select '["a","b","c"]'::jsonb - -4;
  926. select '{"a":1 , "b":2, "c":3}'::jsonb - '{b}'::text[];
  927. select '{"a":1 , "b":2, "c":3}'::jsonb - '{c,b}'::text[];
  928. select '{"a":1 , "b":2, "c":3}'::jsonb - '{}'::text[];
  929. select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]');
  930. select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '[1,2,3]');
  931. select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '[1,2,3]');
  932. select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '[1,2,3]');
  933. select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '{"1": 2}');
  934. select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"1": 2}');
  935. select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '{"1": 2}');
  936. select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '{"1": 2}');
  937. select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '"test"');
  938. select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"f": "test"}');
  939. select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{n}');
  940. select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{b,-1}');
  941. select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{d,1,0}');
  942. select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{n}';
  943. select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{b,-1}';
  944. select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{b,-1e}'; -- invalid array subscript
  945. select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{d,1,0}';
  946. -- empty structure and error conditions for delete and replace
  947. select '"a"'::jsonb - 'a'; -- error
  948. select '{}'::jsonb - 'a';
  949. select '[]'::jsonb - 'a';
  950. select '"a"'::jsonb - 1; -- error
  951. select '{}'::jsonb - 1; -- error
  952. select '[]'::jsonb - 1;
  953. select '"a"'::jsonb #- '{a}'; -- error
  954. select '{}'::jsonb #- '{a}';
  955. select '[]'::jsonb #- '{a}';
  956. select jsonb_set('"a"','{a}','"b"'); --error
  957. select jsonb_set('{}','{a}','"b"', false);
  958. select jsonb_set('[]','{1}','"b"', false);
  959. select jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{0}','[2,3,4]', false);
  960. -- jsonb_set adding instead of replacing
  961. -- prepend to array
  962. select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{b,-33}','{"foo":123}');
  963. -- append to array
  964. select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{b,33}','{"foo":123}');
  965. -- check nesting levels addition
  966. select jsonb_set('{"a":1,"b":[4,5,[0,1,2],6,7],"c":{"d":4}}','{b,2,33}','{"foo":123}');
  967. -- add new key
  968. select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{c,e}','{"foo":123}');
  969. -- adding doesn't do anything if elements before last aren't present
  970. select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{x,-33}','{"foo":123}');
  971. select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{x,y}','{"foo":123}');
  972. -- add to empty object
  973. select jsonb_set('{}','{x}','{"foo":123}');
  974. --add to empty array
  975. select jsonb_set('[]','{0}','{"foo":123}');
  976. select jsonb_set('[]','{99}','{"foo":123}');
  977. select jsonb_set('[]','{-99}','{"foo":123}');
  978. select jsonb_set('{"a": [1, 2, 3]}', '{a, non_integer}', '"new_value"');
  979. select jsonb_set('{"a": {"b": [1, 2, 3]}}', '{a, b, non_integer}', '"new_value"');
  980. select jsonb_set('{"a": {"b": [1, 2, 3]}}', '{a, b, NULL}', '"new_value"');
  981. -- jsonb_set_lax
  982. \pset null NULL
  983. -- pass though non nulls to jsonb_set
  984. select jsonb_set_lax('{"a":1,"b":2}','{b}','5') ;
  985. select jsonb_set_lax('{"a":1,"b":2}','{d}','6', true) ;
  986. -- using the default treatment
  987. select jsonb_set_lax('{"a":1,"b":2}','{b}',null);
  988. select jsonb_set_lax('{"a":1,"b":2}','{d}',null,true);
  989. -- errors
  990. select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, true, null);
  991. select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, true, 'no_such_treatment');
  992. -- explicit treatments
  993. select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, null_value_treatment => 'raise_exception') as raise_exception;
  994. select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, null_value_treatment => 'return_target') as return_target;
  995. select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, null_value_treatment => 'delete_key') as delete_key;
  996. select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, null_value_treatment => 'use_json_null') as use_json_null;
  997. \pset null ''
  998. -- jsonb_insert
  999. select jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"');
  1000. select jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"', true);
  1001. select jsonb_insert('{"a": {"b": {"c": [0, 1, "test1", "test2"]}}}', '{a, b, c, 2}', '"new_value"');
  1002. select jsonb_insert('{"a": {"b": {"c": [0, 1, "test1", "test2"]}}}', '{a, b, c, 2}', '"new_value"', true);
  1003. select jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '{"b": "value"}');
  1004. select jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '["value1", "value2"]');
  1005. -- edge cases
  1006. select jsonb_insert('{"a": [0,1,2]}', '{a, 0}', '"new_value"');
  1007. select jsonb_insert('{"a": [0,1,2]}', '{a, 0}', '"new_value"', true);
  1008. select jsonb_insert('{"a": [0,1,2]}', '{a, 2}', '"new_value"');
  1009. select jsonb_insert('{"a": [0,1,2]}', '{a, 2}', '"new_value"', true);
  1010. select jsonb_insert('{"a": [0,1,2]}', '{a, -1}', '"new_value"');
  1011. select jsonb_insert('{"a": [0,1,2]}', '{a, -1}', '"new_value"', true);
  1012. select jsonb_insert('[]', '{1}', '"new_value"');
  1013. select jsonb_insert('[]', '{1}', '"new_value"', true);
  1014. select jsonb_insert('{"a": []}', '{a, 1}', '"new_value"');
  1015. select jsonb_insert('{"a": []}', '{a, 1}', '"new_value"', true);
  1016. select jsonb_insert('{"a": [0,1,2]}', '{a, 10}', '"new_value"');
  1017. select jsonb_insert('{"a": [0,1,2]}', '{a, -10}', '"new_value"');
  1018. -- jsonb_insert should be able to insert new value for objects, but not to replace
  1019. select jsonb_insert('{"a": {"b": "value"}}', '{a, c}', '"new_value"');
  1020. select jsonb_insert('{"a": {"b": "value"}}', '{a, c}', '"new_value"', true);
  1021. select jsonb_insert('{"a": {"b": "value"}}', '{a, b}', '"new_value"');
  1022. select jsonb_insert('{"a": {"b": "value"}}', '{a, b}', '"new_value"', true);
  1023. -- jsonb subscript
  1024. select ('123'::jsonb)['a'];
  1025. select ('123'::jsonb)[0];
  1026. select ('123'::jsonb)[NULL];
  1027. select ('{"a": 1}'::jsonb)['a'];
  1028. select ('{"a": 1}'::jsonb)[0];
  1029. select ('{"a": 1}'::jsonb)['not_exist'];
  1030. select ('{"a": 1}'::jsonb)[NULL];
  1031. select ('[1, "2", null]'::jsonb)['a'];
  1032. select ('[1, "2", null]'::jsonb)[0];
  1033. select ('[1, "2", null]'::jsonb)['1'];
  1034. select ('[1, "2", null]'::jsonb)[1.0];
  1035. select ('[1, "2", null]'::jsonb)[2];
  1036. select ('[1, "2", null]'::jsonb)[3];
  1037. select ('[1, "2", null]'::jsonb)[-2];
  1038. select ('[1, "2", null]'::jsonb)[1]['a'];
  1039. select ('[1, "2", null]'::jsonb)[1][0];
  1040. select ('{"a": 1, "b": "c", "d": [1, 2, 3]}'::jsonb)['b'];
  1041. select ('{"a": 1, "b": "c", "d": [1, 2, 3]}'::jsonb)['d'];
  1042. select ('{"a": 1, "b": "c", "d": [1, 2, 3]}'::jsonb)['d'][1];
  1043. select ('{"a": 1, "b": "c", "d": [1, 2, 3]}'::jsonb)['d']['a'];
  1044. select ('{"a": {"a1": {"a2": "aaa"}}, "b": "bbb", "c": "ccc"}'::jsonb)['a']['a1'];
  1045. select ('{"a": {"a1": {"a2": "aaa"}}, "b": "bbb", "c": "ccc"}'::jsonb)['a']['a1']['a2'];
  1046. select ('{"a": {"a1": {"a2": "aaa"}}, "b": "bbb", "c": "ccc"}'::jsonb)['a']['a1']['a2']['a3'];
  1047. select ('{"a": ["a1", {"b1": ["aaa", "bbb", "ccc"]}], "b": "bb"}'::jsonb)['a'][1]['b1'];
  1048. select ('{"a": ["a1", {"b1": ["aaa", "bbb", "ccc"]}], "b": "bb"}'::jsonb)['a'][1]['b1'][2];
  1049. -- slices are not supported
  1050. select ('{"a": 1}'::jsonb)['a':'b'];
  1051. select ('[1, "2", null]'::jsonb)[1:2];
  1052. select ('[1, "2", null]'::jsonb)[:2];
  1053. select ('[1, "2", null]'::jsonb)[1:];
  1054. select ('[1, "2", null]'::jsonb)[:];
  1055. create TEMP TABLE test_jsonb_subscript (
  1056. id int,
  1057. test_json jsonb
  1058. );
  1059. insert into test_jsonb_subscript values
  1060. (1, '{}'), -- empty jsonb
  1061. (2, '{"key": "value"}'); -- jsonb with data
  1062. -- update empty jsonb
  1063. update test_jsonb_subscript set test_json['a'] = '1' where id = 1;
  1064. select * from test_jsonb_subscript;
  1065. -- update jsonb with some data
  1066. update test_jsonb_subscript set test_json['a'] = '1' where id = 2;
  1067. select * from test_jsonb_subscript;
  1068. -- replace jsonb
  1069. update test_jsonb_subscript set test_json['a'] = '"test"';
  1070. select * from test_jsonb_subscript;
  1071. -- replace by object
  1072. update test_jsonb_subscript set test_json['a'] = '{"b": 1}'::jsonb;
  1073. select * from test_jsonb_subscript;
  1074. -- replace by array
  1075. update test_jsonb_subscript set test_json['a'] = '[1, 2, 3]'::jsonb;
  1076. select * from test_jsonb_subscript;
  1077. -- use jsonb subscription in where clause
  1078. select * from test_jsonb_subscript where test_json['key'] = '"value"';
  1079. select * from test_jsonb_subscript where test_json['key_doesnt_exists'] = '"value"';
  1080. select * from test_jsonb_subscript where test_json['key'] = '"wrong_value"';
  1081. -- NULL
  1082. update test_jsonb_subscript set test_json[NULL] = '1';
  1083. update test_jsonb_subscript set test_json['another_key'] = NULL;
  1084. select * from test_jsonb_subscript;
  1085. -- NULL as jsonb source
  1086. insert into test_jsonb_subscript values (3, NULL);
  1087. update test_jsonb_subscript set test_json['a'] = '1' where id = 3;
  1088. select * from test_jsonb_subscript;
  1089. update test_jsonb_subscript set test_json = NULL where id = 3;
  1090. update test_jsonb_subscript set test_json[0] = '1';
  1091. select * from test_jsonb_subscript;
  1092. -- Fill the gaps logic
  1093. delete from test_jsonb_subscript;
  1094. insert into test_jsonb_subscript values (1, '[0]');
  1095. update test_jsonb_subscript set test_json[5] = '1';
  1096. select * from test_jsonb_subscript;
  1097. update test_jsonb_subscript set test_json[-4] = '1';
  1098. select * from test_jsonb_subscript;
  1099. update test_jsonb_subscript set test_json[-8] = '1';
  1100. select * from test_jsonb_subscript;
  1101. -- keep consistent values position
  1102. delete from test_jsonb_subscript;
  1103. insert into test_jsonb_subscript values (1, '[]');
  1104. update test_jsonb_subscript set test_json[5] = '1';
  1105. select * from test_jsonb_subscript;
  1106. -- create the whole path
  1107. delete from test_jsonb_subscript;
  1108. insert into test_jsonb_subscript values (1, '{}');
  1109. update test_jsonb_subscript set test_json['a'][0]['b'][0]['c'] = '1';
  1110. select * from test_jsonb_subscript;
  1111. delete from test_jsonb_subscript;
  1112. insert into test_jsonb_subscript values (1, '{}');
  1113. update test_jsonb_subscript set test_json['a'][2]['b'][2]['c'][2] = '1';
  1114. select * from test_jsonb_subscript;
  1115. -- create the whole path with already existing keys
  1116. delete from test_jsonb_subscript;
  1117. insert into test_jsonb_subscript values (1, '{"b": 1}');
  1118. update test_jsonb_subscript set test_json['a'][0] = '2';
  1119. select * from test_jsonb_subscript;
  1120. -- the start jsonb is an object, first subscript is treated as a key
  1121. delete from test_jsonb_subscript;
  1122. insert into test_jsonb_subscript values (1, '{}');
  1123. update test_jsonb_subscript set test_json[0]['a'] = '1';
  1124. select * from test_jsonb_subscript;
  1125. -- the start jsonb is an array
  1126. delete from test_jsonb_subscript;
  1127. insert into test_jsonb_subscript values (1, '[]');
  1128. update test_jsonb_subscript set test_json[0]['a'] = '1';
  1129. update test_jsonb_subscript set test_json[2]['b'] = '2';
  1130. select * from test_jsonb_subscript;
  1131. -- overwriting an existing path
  1132. delete from test_jsonb_subscript;
  1133. insert into test_jsonb_subscript values (1, '{}');
  1134. update test_jsonb_subscript set test_json['a']['b'][1] = '1';
  1135. update test_jsonb_subscript set test_json['a']['b'][10] = '1';
  1136. select * from test_jsonb_subscript;
  1137. delete from test_jsonb_subscript;
  1138. insert into test_jsonb_subscript values (1, '[]');
  1139. update test_jsonb_subscript set test_json[0][0][0] = '1';
  1140. update test_jsonb_subscript set test_json[0][0][1] = '1';
  1141. select * from test_jsonb_subscript;
  1142. delete from test_jsonb_subscript;
  1143. insert into test_jsonb_subscript values (1, '{}');
  1144. update test_jsonb_subscript set test_json['a']['b'][10] = '1';
  1145. update test_jsonb_subscript set test_json['a'][10][10] = '1';
  1146. select * from test_jsonb_subscript;
  1147. -- an empty sub element
  1148. delete from test_jsonb_subscript;
  1149. insert into test_jsonb_subscript values (1, '{"a": {}}');
  1150. update test_jsonb_subscript set test_json['a']['b']['c'][2] = '1';
  1151. select * from test_jsonb_subscript;
  1152. delete from test_jsonb_subscript;
  1153. insert into test_jsonb_subscript values (1, '{"a": []}');
  1154. update test_jsonb_subscript set test_json['a'][1]['c'][2] = '1';
  1155. select * from test_jsonb_subscript;
  1156. -- trying replace assuming a composite object, but it's an element or a value
  1157. delete from test_jsonb_subscript;
  1158. insert into test_jsonb_subscript values (1, '{"a": 1}');
  1159. update test_jsonb_subscript set test_json['a']['b'] = '1';
  1160. update test_jsonb_subscript set test_json['a']['b']['c'] = '1';
  1161. update test_jsonb_subscript set test_json['a'][0] = '1';
  1162. update test_jsonb_subscript set test_json['a'][0]['c'] = '1';
  1163. update test_jsonb_subscript set test_json['a'][0][0] = '1';
  1164. -- trying replace assuming a composite object, but it's a raw scalar
  1165. delete from test_jsonb_subscript;
  1166. insert into test_jsonb_subscript values (1, 'null');
  1167. update test_jsonb_subscript set test_json[0] = '1';
  1168. update test_jsonb_subscript set test_json[0][0] = '1';
  1169. -- jsonb to tsvector
  1170. select to_tsvector('{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::jsonb);
  1171. -- jsonb to tsvector with config
  1172. select to_tsvector('simple', '{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::jsonb);
  1173. -- jsonb to tsvector with stop words
  1174. select to_tsvector('english', '{"a": "aaa in bbb ddd ccc", "b": ["the eee fff ggg"], "c": {"d": "hhh. iii"}}'::jsonb);
  1175. -- jsonb to tsvector with numeric values
  1176. select to_tsvector('english', '{"a": "aaa in bbb ddd ccc", "b": 123, "c": 456}'::jsonb);
  1177. -- jsonb_to_tsvector
  1178. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"all"');
  1179. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"key"');
  1180. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"string"');
  1181. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"numeric"');
  1182. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"boolean"');
  1183. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '["string", "numeric"]');
  1184. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"all"');
  1185. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"key"');
  1186. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"string"');
  1187. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"numeric"');
  1188. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"boolean"');
  1189. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '["string", "numeric"]');
  1190. -- to_tsvector corner cases
  1191. select to_tsvector('""'::jsonb);
  1192. select to_tsvector('{}'::jsonb);
  1193. select to_tsvector('[]'::jsonb);
  1194. select to_tsvector('null'::jsonb);
  1195. -- jsonb_to_tsvector corner cases
  1196. select jsonb_to_tsvector('""'::jsonb, '"all"');
  1197. select jsonb_to_tsvector('{}'::jsonb, '"all"');
  1198. select jsonb_to_tsvector('[]'::jsonb, '"all"');
  1199. select jsonb_to_tsvector('null'::jsonb, '"all"');
  1200. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '""');
  1201. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '{}');
  1202. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '[]');
  1203. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, 'null');
  1204. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '["all", null]');
  1205. -- ts_headline for jsonb
  1206. select ts_headline('{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::jsonb, tsquery('bbb & ddd & hhh'));
  1207. select ts_headline('english', '{"a": "aaa bbb", "b": {"c": "ccc ddd fff"}, "d": ["ggg hhh", "iii jjj"]}'::jsonb, tsquery('bbb & ddd & hhh'));
  1208. select ts_headline('{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::jsonb, tsquery('bbb & ddd & hhh'), 'StartSel = <, StopSel = >');
  1209. select ts_headline('english', '{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::jsonb, tsquery('bbb & ddd & hhh'), 'StartSel = <, StopSel = >');
  1210. -- corner cases for ts_headline with jsonb
  1211. select ts_headline('null'::jsonb, tsquery('aaa & bbb'));
  1212. select ts_headline('{}'::jsonb, tsquery('aaa & bbb'));
  1213. select ts_headline('[]'::jsonb, tsquery('aaa & bbb'));
  1214. -- casts
  1215. select 'true'::jsonb::bool;
  1216. select '[]'::jsonb::bool;
  1217. select '1.0'::jsonb::float;
  1218. select '[1.0]'::jsonb::float;
  1219. select '12345'::jsonb::int4;
  1220. select '"hello"'::jsonb::int4;
  1221. select '12345'::jsonb::numeric;
  1222. select '{}'::jsonb::numeric;
  1223. select '12345.05'::jsonb::numeric;
  1224. select '12345.05'::jsonb::float4;
  1225. select '12345.05'::jsonb::float8;
  1226. select '12345.05'::jsonb::int2;
  1227. select '12345.05'::jsonb::int4;
  1228. select '12345.05'::jsonb::int8;
  1229. select '12345.0000000000000000000000000000000000000000000005'::jsonb::numeric;
  1230. select '12345.0000000000000000000000000000000000000000000005'::jsonb::float4;
  1231. select '12345.0000000000000000000000000000000000000000000005'::jsonb::float8;
  1232. select '12345.0000000000000000000000000000000000000000000005'::jsonb::int2;
  1233. select '12345.0000000000000000000000000000000000000000000005'::jsonb::int4;
  1234. select '12345.0000000000000000000000000000000000000000000005'::jsonb::int8;