jsonb_jsonpath.out 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539
  1. select jsonb '{"a": 12}' @? '$';
  2. ?column?
  3. ----------
  4. t
  5. (1 row)
  6. select jsonb '{"a": 12}' @? '1';
  7. ?column?
  8. ----------
  9. t
  10. (1 row)
  11. select jsonb '{"a": 12}' @? '$.a.b';
  12. ?column?
  13. ----------
  14. f
  15. (1 row)
  16. select jsonb '{"a": 12}' @? '$.b';
  17. ?column?
  18. ----------
  19. f
  20. (1 row)
  21. select jsonb '{"a": 12}' @? '$.a + 2';
  22. ?column?
  23. ----------
  24. t
  25. (1 row)
  26. select jsonb '{"a": 12}' @? '$.b + 2';
  27. ?column?
  28. ----------
  29. (1 row)
  30. select jsonb '{"a": {"a": 12}}' @? '$.a.a';
  31. ?column?
  32. ----------
  33. t
  34. (1 row)
  35. select jsonb '{"a": {"a": 12}}' @? '$.*.a';
  36. ?column?
  37. ----------
  38. t
  39. (1 row)
  40. select jsonb '{"b": {"a": 12}}' @? '$.*.a';
  41. ?column?
  42. ----------
  43. t
  44. (1 row)
  45. select jsonb '{"b": {"a": 12}}' @? '$.*.b';
  46. ?column?
  47. ----------
  48. f
  49. (1 row)
  50. select jsonb '{"b": {"a": 12}}' @? 'strict $.*.b';
  51. ?column?
  52. ----------
  53. (1 row)
  54. select jsonb '{}' @? '$.*';
  55. ?column?
  56. ----------
  57. f
  58. (1 row)
  59. select jsonb '{"a": 1}' @? '$.*';
  60. ?column?
  61. ----------
  62. t
  63. (1 row)
  64. select jsonb '{"a": {"b": 1}}' @? 'lax $.**{1}';
  65. ?column?
  66. ----------
  67. t
  68. (1 row)
  69. select jsonb '{"a": {"b": 1}}' @? 'lax $.**{2}';
  70. ?column?
  71. ----------
  72. t
  73. (1 row)
  74. select jsonb '{"a": {"b": 1}}' @? 'lax $.**{3}';
  75. ?column?
  76. ----------
  77. f
  78. (1 row)
  79. select jsonb '[]' @? '$[*]';
  80. ?column?
  81. ----------
  82. f
  83. (1 row)
  84. select jsonb '[1]' @? '$[*]';
  85. ?column?
  86. ----------
  87. t
  88. (1 row)
  89. select jsonb '[1]' @? '$[1]';
  90. ?column?
  91. ----------
  92. f
  93. (1 row)
  94. select jsonb '[1]' @? 'strict $[1]';
  95. ?column?
  96. ----------
  97. (1 row)
  98. select jsonb_path_query('[1]', 'strict $[1]');
  99. ERROR: jsonpath array subscript is out of bounds
  100. select jsonb_path_query('[1]', 'strict $[1]', silent => true);
  101. jsonb_path_query
  102. ------------------
  103. (0 rows)
  104. select jsonb '[1]' @? 'lax $[10000000000000000]';
  105. ?column?
  106. ----------
  107. (1 row)
  108. select jsonb '[1]' @? 'strict $[10000000000000000]';
  109. ?column?
  110. ----------
  111. (1 row)
  112. select jsonb_path_query('[1]', 'lax $[10000000000000000]');
  113. ERROR: jsonpath array subscript is out of integer range
  114. select jsonb_path_query('[1]', 'strict $[10000000000000000]');
  115. ERROR: jsonpath array subscript is out of integer range
  116. select jsonb '[1]' @? '$[0]';
  117. ?column?
  118. ----------
  119. t
  120. (1 row)
  121. select jsonb '[1]' @? '$[0.3]';
  122. ?column?
  123. ----------
  124. t
  125. (1 row)
  126. select jsonb '[1]' @? '$[0.5]';
  127. ?column?
  128. ----------
  129. t
  130. (1 row)
  131. select jsonb '[1]' @? '$[0.9]';
  132. ?column?
  133. ----------
  134. t
  135. (1 row)
  136. select jsonb '[1]' @? '$[1.2]';
  137. ?column?
  138. ----------
  139. f
  140. (1 row)
  141. select jsonb '[1]' @? 'strict $[1.2]';
  142. ?column?
  143. ----------
  144. (1 row)
  145. select jsonb '{"a": [1,2,3], "b": [3,4,5]}' @? '$ ? (@.a[*] > @.b[*])';
  146. ?column?
  147. ----------
  148. f
  149. (1 row)
  150. select jsonb '{"a": [1,2,3], "b": [3,4,5]}' @? '$ ? (@.a[*] >= @.b[*])';
  151. ?column?
  152. ----------
  153. t
  154. (1 row)
  155. select jsonb '{"a": [1,2,3], "b": [3,4,"5"]}' @? '$ ? (@.a[*] >= @.b[*])';
  156. ?column?
  157. ----------
  158. t
  159. (1 row)
  160. select jsonb '{"a": [1,2,3], "b": [3,4,"5"]}' @? 'strict $ ? (@.a[*] >= @.b[*])';
  161. ?column?
  162. ----------
  163. f
  164. (1 row)
  165. select jsonb '{"a": [1,2,3], "b": [3,4,null]}' @? '$ ? (@.a[*] >= @.b[*])';
  166. ?column?
  167. ----------
  168. t
  169. (1 row)
  170. select jsonb '1' @? '$ ? ((@ == "1") is unknown)';
  171. ?column?
  172. ----------
  173. t
  174. (1 row)
  175. select jsonb '1' @? '$ ? ((@ == 1) is unknown)';
  176. ?column?
  177. ----------
  178. f
  179. (1 row)
  180. select jsonb '[{"a": 1}, {"a": 2}]' @? '$[0 to 1] ? (@.a > 1)';
  181. ?column?
  182. ----------
  183. t
  184. (1 row)
  185. select jsonb_path_exists('[{"a": 1}, {"a": 2}, 3]', 'lax $[*].a', silent => false);
  186. jsonb_path_exists
  187. -------------------
  188. t
  189. (1 row)
  190. select jsonb_path_exists('[{"a": 1}, {"a": 2}, 3]', 'lax $[*].a', silent => true);
  191. jsonb_path_exists
  192. -------------------
  193. t
  194. (1 row)
  195. select jsonb_path_exists('[{"a": 1}, {"a": 2}, 3]', 'strict $[*].a', silent => false);
  196. ERROR: jsonpath member accessor can only be applied to an object
  197. select jsonb_path_exists('[{"a": 1}, {"a": 2}, 3]', 'strict $[*].a', silent => true);
  198. jsonb_path_exists
  199. -------------------
  200. (1 row)
  201. select jsonb_path_query('1', 'lax $.a');
  202. jsonb_path_query
  203. ------------------
  204. (0 rows)
  205. select jsonb_path_query('1', 'strict $.a');
  206. ERROR: jsonpath member accessor can only be applied to an object
  207. select jsonb_path_query('1', 'strict $.*');
  208. ERROR: jsonpath wildcard member accessor can only be applied to an object
  209. select jsonb_path_query('1', 'strict $.a', silent => true);
  210. jsonb_path_query
  211. ------------------
  212. (0 rows)
  213. select jsonb_path_query('1', 'strict $.*', silent => true);
  214. jsonb_path_query
  215. ------------------
  216. (0 rows)
  217. select jsonb_path_query('[]', 'lax $.a');
  218. jsonb_path_query
  219. ------------------
  220. (0 rows)
  221. select jsonb_path_query('[]', 'strict $.a');
  222. ERROR: jsonpath member accessor can only be applied to an object
  223. select jsonb_path_query('[]', 'strict $.a', silent => true);
  224. jsonb_path_query
  225. ------------------
  226. (0 rows)
  227. select jsonb_path_query('{}', 'lax $.a');
  228. jsonb_path_query
  229. ------------------
  230. (0 rows)
  231. select jsonb_path_query('{}', 'strict $.a');
  232. ERROR: JSON object does not contain key "a"
  233. select jsonb_path_query('{}', 'strict $.a', silent => true);
  234. jsonb_path_query
  235. ------------------
  236. (0 rows)
  237. select jsonb_path_query('1', 'strict $[1]');
  238. ERROR: jsonpath array accessor can only be applied to an array
  239. select jsonb_path_query('1', 'strict $[*]');
  240. ERROR: jsonpath wildcard array accessor can only be applied to an array
  241. select jsonb_path_query('[]', 'strict $[1]');
  242. ERROR: jsonpath array subscript is out of bounds
  243. select jsonb_path_query('[]', 'strict $["a"]');
  244. ERROR: jsonpath array subscript is not a single numeric value
  245. select jsonb_path_query('1', 'strict $[1]', silent => true);
  246. jsonb_path_query
  247. ------------------
  248. (0 rows)
  249. select jsonb_path_query('1', 'strict $[*]', silent => true);
  250. jsonb_path_query
  251. ------------------
  252. (0 rows)
  253. select jsonb_path_query('[]', 'strict $[1]', silent => true);
  254. jsonb_path_query
  255. ------------------
  256. (0 rows)
  257. select jsonb_path_query('[]', 'strict $["a"]', silent => true);
  258. jsonb_path_query
  259. ------------------
  260. (0 rows)
  261. select jsonb_path_query('{"a": 12, "b": {"a": 13}}', '$.a');
  262. jsonb_path_query
  263. ------------------
  264. 12
  265. (1 row)
  266. select jsonb_path_query('{"a": 12, "b": {"a": 13}}', '$.b');
  267. jsonb_path_query
  268. ------------------
  269. {"a": 13}
  270. (1 row)
  271. select jsonb_path_query('{"a": 12, "b": {"a": 13}}', '$.*');
  272. jsonb_path_query
  273. ------------------
  274. 12
  275. {"a": 13}
  276. (2 rows)
  277. select jsonb_path_query('{"a": 12, "b": {"a": 13}}', 'lax $.*.a');
  278. jsonb_path_query
  279. ------------------
  280. 13
  281. (1 row)
  282. select jsonb_path_query('[12, {"a": 13}, {"b": 14}]', 'lax $[*].a');
  283. jsonb_path_query
  284. ------------------
  285. 13
  286. (1 row)
  287. select jsonb_path_query('[12, {"a": 13}, {"b": 14}]', 'lax $[*].*');
  288. jsonb_path_query
  289. ------------------
  290. 13
  291. 14
  292. (2 rows)
  293. select jsonb_path_query('[12, {"a": 13}, {"b": 14}]', 'lax $[0].a');
  294. jsonb_path_query
  295. ------------------
  296. (0 rows)
  297. select jsonb_path_query('[12, {"a": 13}, {"b": 14}]', 'lax $[1].a');
  298. jsonb_path_query
  299. ------------------
  300. 13
  301. (1 row)
  302. select jsonb_path_query('[12, {"a": 13}, {"b": 14}]', 'lax $[2].a');
  303. jsonb_path_query
  304. ------------------
  305. (0 rows)
  306. select jsonb_path_query('[12, {"a": 13}, {"b": 14}]', 'lax $[0,1].a');
  307. jsonb_path_query
  308. ------------------
  309. 13
  310. (1 row)
  311. select jsonb_path_query('[12, {"a": 13}, {"b": 14}]', 'lax $[0 to 10].a');
  312. jsonb_path_query
  313. ------------------
  314. 13
  315. (1 row)
  316. select jsonb_path_query('[12, {"a": 13}, {"b": 14}]', 'lax $[0 to 10 / 0].a');
  317. ERROR: division by zero
  318. select jsonb_path_query('[12, {"a": 13}, {"b": 14}, "ccc", true]', '$[2.5 - 1 to $.size() - 2]');
  319. jsonb_path_query
  320. ------------------
  321. {"a": 13}
  322. {"b": 14}
  323. "ccc"
  324. (3 rows)
  325. select jsonb_path_query('1', 'lax $[0]');
  326. jsonb_path_query
  327. ------------------
  328. 1
  329. (1 row)
  330. select jsonb_path_query('1', 'lax $[*]');
  331. jsonb_path_query
  332. ------------------
  333. 1
  334. (1 row)
  335. select jsonb_path_query('[1]', 'lax $[0]');
  336. jsonb_path_query
  337. ------------------
  338. 1
  339. (1 row)
  340. select jsonb_path_query('[1]', 'lax $[*]');
  341. jsonb_path_query
  342. ------------------
  343. 1
  344. (1 row)
  345. select jsonb_path_query('[1,2,3]', 'lax $[*]');
  346. jsonb_path_query
  347. ------------------
  348. 1
  349. 2
  350. 3
  351. (3 rows)
  352. select jsonb_path_query('[1,2,3]', 'strict $[*].a');
  353. ERROR: jsonpath member accessor can only be applied to an object
  354. select jsonb_path_query('[1,2,3]', 'strict $[*].a', silent => true);
  355. jsonb_path_query
  356. ------------------
  357. (0 rows)
  358. select jsonb_path_query('[]', '$[last]');
  359. jsonb_path_query
  360. ------------------
  361. (0 rows)
  362. select jsonb_path_query('[]', '$[last ? (exists(last))]');
  363. jsonb_path_query
  364. ------------------
  365. (0 rows)
  366. select jsonb_path_query('[]', 'strict $[last]');
  367. ERROR: jsonpath array subscript is out of bounds
  368. select jsonb_path_query('[]', 'strict $[last]', silent => true);
  369. jsonb_path_query
  370. ------------------
  371. (0 rows)
  372. select jsonb_path_query('[1]', '$[last]');
  373. jsonb_path_query
  374. ------------------
  375. 1
  376. (1 row)
  377. select jsonb_path_query('[1,2,3]', '$[last]');
  378. jsonb_path_query
  379. ------------------
  380. 3
  381. (1 row)
  382. select jsonb_path_query('[1,2,3]', '$[last - 1]');
  383. jsonb_path_query
  384. ------------------
  385. 2
  386. (1 row)
  387. select jsonb_path_query('[1,2,3]', '$[last ? (@.type() == "number")]');
  388. jsonb_path_query
  389. ------------------
  390. 3
  391. (1 row)
  392. select jsonb_path_query('[1,2,3]', '$[last ? (@.type() == "string")]');
  393. ERROR: jsonpath array subscript is not a single numeric value
  394. select jsonb_path_query('[1,2,3]', '$[last ? (@.type() == "string")]', silent => true);
  395. jsonb_path_query
  396. ------------------
  397. (0 rows)
  398. select * from jsonb_path_query('{"a": 10}', '$');
  399. jsonb_path_query
  400. ------------------
  401. {"a": 10}
  402. (1 row)
  403. select * from jsonb_path_query('{"a": 10}', '$ ? (@.a < $value)');
  404. ERROR: could not find jsonpath variable "value"
  405. select * from jsonb_path_query('{"a": 10}', '$ ? (@.a < $value)', '1');
  406. ERROR: "vars" argument is not an object
  407. DETAIL: Jsonpath parameters should be encoded as key-value pairs of "vars" object.
  408. select * from jsonb_path_query('{"a": 10}', '$ ? (@.a < $value)', '[{"value" : 13}]');
  409. ERROR: "vars" argument is not an object
  410. DETAIL: Jsonpath parameters should be encoded as key-value pairs of "vars" object.
  411. select * from jsonb_path_query('{"a": 10}', '$ ? (@.a < $value)', '{"value" : 13}');
  412. jsonb_path_query
  413. ------------------
  414. {"a": 10}
  415. (1 row)
  416. select * from jsonb_path_query('{"a": 10}', '$ ? (@.a < $value)', '{"value" : 8}');
  417. jsonb_path_query
  418. ------------------
  419. (0 rows)
  420. select * from jsonb_path_query('{"a": 10}', '$.a ? (@ < $value)', '{"value" : 13}');
  421. jsonb_path_query
  422. ------------------
  423. 10
  424. (1 row)
  425. select * from jsonb_path_query('[10,11,12,13,14,15]', '$[*] ? (@ < $value)', '{"value" : 13}');
  426. jsonb_path_query
  427. ------------------
  428. 10
  429. 11
  430. 12
  431. (3 rows)
  432. select * from jsonb_path_query('[10,11,12,13,14,15]', '$[0,1] ? (@ < $x.value)', '{"x": {"value" : 13}}');
  433. jsonb_path_query
  434. ------------------
  435. 10
  436. 11
  437. (2 rows)
  438. select * from jsonb_path_query('[10,11,12,13,14,15]', '$[0 to 2] ? (@ < $value)', '{"value" : 15}');
  439. jsonb_path_query
  440. ------------------
  441. 10
  442. 11
  443. 12
  444. (3 rows)
  445. select * from jsonb_path_query('[1,"1",2,"2",null]', '$[*] ? (@ == "1")');
  446. jsonb_path_query
  447. ------------------
  448. "1"
  449. (1 row)
  450. select * from jsonb_path_query('[1,"1",2,"2",null]', '$[*] ? (@ == $value)', '{"value" : "1"}');
  451. jsonb_path_query
  452. ------------------
  453. "1"
  454. (1 row)
  455. select * from jsonb_path_query('[1,"1",2,"2",null]', '$[*] ? (@ == $value)', '{"value" : null}');
  456. jsonb_path_query
  457. ------------------
  458. null
  459. (1 row)
  460. select * from jsonb_path_query('[1, "2", null]', '$[*] ? (@ != null)');
  461. jsonb_path_query
  462. ------------------
  463. 1
  464. "2"
  465. (2 rows)
  466. select * from jsonb_path_query('[1, "2", null]', '$[*] ? (@ == null)');
  467. jsonb_path_query
  468. ------------------
  469. null
  470. (1 row)
  471. select * from jsonb_path_query('{}', '$ ? (@ == @)');
  472. jsonb_path_query
  473. ------------------
  474. (0 rows)
  475. select * from jsonb_path_query('[]', 'strict $ ? (@ == @)');
  476. jsonb_path_query
  477. ------------------
  478. (0 rows)
  479. select jsonb_path_query('{"a": {"b": 1}}', 'lax $.**');
  480. jsonb_path_query
  481. ------------------
  482. {"a": {"b": 1}}
  483. {"b": 1}
  484. 1
  485. (3 rows)
  486. select jsonb_path_query('{"a": {"b": 1}}', 'lax $.**{0}');
  487. jsonb_path_query
  488. ------------------
  489. {"a": {"b": 1}}
  490. (1 row)
  491. select jsonb_path_query('{"a": {"b": 1}}', 'lax $.**{0 to last}');
  492. jsonb_path_query
  493. ------------------
  494. {"a": {"b": 1}}
  495. {"b": 1}
  496. 1
  497. (3 rows)
  498. select jsonb_path_query('{"a": {"b": 1}}', 'lax $.**{1}');
  499. jsonb_path_query
  500. ------------------
  501. {"b": 1}
  502. (1 row)
  503. select jsonb_path_query('{"a": {"b": 1}}', 'lax $.**{1 to last}');
  504. jsonb_path_query
  505. ------------------
  506. {"b": 1}
  507. 1
  508. (2 rows)
  509. select jsonb_path_query('{"a": {"b": 1}}', 'lax $.**{2}');
  510. jsonb_path_query
  511. ------------------
  512. 1
  513. (1 row)
  514. select jsonb_path_query('{"a": {"b": 1}}', 'lax $.**{2 to last}');
  515. jsonb_path_query
  516. ------------------
  517. 1
  518. (1 row)
  519. select jsonb_path_query('{"a": {"b": 1}}', 'lax $.**{3 to last}');
  520. jsonb_path_query
  521. ------------------
  522. (0 rows)
  523. select jsonb_path_query('{"a": {"b": 1}}', 'lax $.**{last}');
  524. jsonb_path_query
  525. ------------------
  526. 1
  527. (1 row)
  528. select jsonb_path_query('{"a": {"b": 1}}', 'lax $.**.b ? (@ > 0)');
  529. jsonb_path_query
  530. ------------------
  531. 1
  532. (1 row)
  533. select jsonb_path_query('{"a": {"b": 1}}', 'lax $.**{0}.b ? (@ > 0)');
  534. jsonb_path_query
  535. ------------------
  536. (0 rows)
  537. select jsonb_path_query('{"a": {"b": 1}}', 'lax $.**{1}.b ? (@ > 0)');
  538. jsonb_path_query
  539. ------------------
  540. 1
  541. (1 row)
  542. select jsonb_path_query('{"a": {"b": 1}}', 'lax $.**{0 to last}.b ? (@ > 0)');
  543. jsonb_path_query
  544. ------------------
  545. 1
  546. (1 row)
  547. select jsonb_path_query('{"a": {"b": 1}}', 'lax $.**{1 to last}.b ? (@ > 0)');
  548. jsonb_path_query
  549. ------------------
  550. 1
  551. (1 row)
  552. select jsonb_path_query('{"a": {"b": 1}}', 'lax $.**{1 to 2}.b ? (@ > 0)');
  553. jsonb_path_query
  554. ------------------
  555. 1
  556. (1 row)
  557. select jsonb_path_query('{"a": {"c": {"b": 1}}}', 'lax $.**.b ? (@ > 0)');
  558. jsonb_path_query
  559. ------------------
  560. 1
  561. (1 row)
  562. select jsonb_path_query('{"a": {"c": {"b": 1}}}', 'lax $.**{0}.b ? (@ > 0)');
  563. jsonb_path_query
  564. ------------------
  565. (0 rows)
  566. select jsonb_path_query('{"a": {"c": {"b": 1}}}', 'lax $.**{1}.b ? (@ > 0)');
  567. jsonb_path_query
  568. ------------------
  569. (0 rows)
  570. select jsonb_path_query('{"a": {"c": {"b": 1}}}', 'lax $.**{0 to last}.b ? (@ > 0)');
  571. jsonb_path_query
  572. ------------------
  573. 1
  574. (1 row)
  575. select jsonb_path_query('{"a": {"c": {"b": 1}}}', 'lax $.**{1 to last}.b ? (@ > 0)');
  576. jsonb_path_query
  577. ------------------
  578. 1
  579. (1 row)
  580. select jsonb_path_query('{"a": {"c": {"b": 1}}}', 'lax $.**{1 to 2}.b ? (@ > 0)');
  581. jsonb_path_query
  582. ------------------
  583. 1
  584. (1 row)
  585. select jsonb_path_query('{"a": {"c": {"b": 1}}}', 'lax $.**{2 to 3}.b ? (@ > 0)');
  586. jsonb_path_query
  587. ------------------
  588. 1
  589. (1 row)
  590. select jsonb '{"a": {"b": 1}}' @? '$.**.b ? ( @ > 0)';
  591. ?column?
  592. ----------
  593. t
  594. (1 row)
  595. select jsonb '{"a": {"b": 1}}' @? '$.**{0}.b ? ( @ > 0)';
  596. ?column?
  597. ----------
  598. f
  599. (1 row)
  600. select jsonb '{"a": {"b": 1}}' @? '$.**{1}.b ? ( @ > 0)';
  601. ?column?
  602. ----------
  603. t
  604. (1 row)
  605. select jsonb '{"a": {"b": 1}}' @? '$.**{0 to last}.b ? ( @ > 0)';
  606. ?column?
  607. ----------
  608. t
  609. (1 row)
  610. select jsonb '{"a": {"b": 1}}' @? '$.**{1 to last}.b ? ( @ > 0)';
  611. ?column?
  612. ----------
  613. t
  614. (1 row)
  615. select jsonb '{"a": {"b": 1}}' @? '$.**{1 to 2}.b ? ( @ > 0)';
  616. ?column?
  617. ----------
  618. t
  619. (1 row)
  620. select jsonb '{"a": {"c": {"b": 1}}}' @? '$.**.b ? ( @ > 0)';
  621. ?column?
  622. ----------
  623. t
  624. (1 row)
  625. select jsonb '{"a": {"c": {"b": 1}}}' @? '$.**{0}.b ? ( @ > 0)';
  626. ?column?
  627. ----------
  628. f
  629. (1 row)
  630. select jsonb '{"a": {"c": {"b": 1}}}' @? '$.**{1}.b ? ( @ > 0)';
  631. ?column?
  632. ----------
  633. f
  634. (1 row)
  635. select jsonb '{"a": {"c": {"b": 1}}}' @? '$.**{0 to last}.b ? ( @ > 0)';
  636. ?column?
  637. ----------
  638. t
  639. (1 row)
  640. select jsonb '{"a": {"c": {"b": 1}}}' @? '$.**{1 to last}.b ? ( @ > 0)';
  641. ?column?
  642. ----------
  643. t
  644. (1 row)
  645. select jsonb '{"a": {"c": {"b": 1}}}' @? '$.**{1 to 2}.b ? ( @ > 0)';
  646. ?column?
  647. ----------
  648. t
  649. (1 row)
  650. select jsonb '{"a": {"c": {"b": 1}}}' @? '$.**{2 to 3}.b ? ( @ > 0)';
  651. ?column?
  652. ----------
  653. t
  654. (1 row)
  655. select jsonb_path_query('{"g": {"x": 2}}', '$.g ? (exists (@.x))');
  656. jsonb_path_query
  657. ------------------
  658. {"x": 2}
  659. (1 row)
  660. select jsonb_path_query('{"g": {"x": 2}}', '$.g ? (exists (@.y))');
  661. jsonb_path_query
  662. ------------------
  663. (0 rows)
  664. select jsonb_path_query('{"g": {"x": 2}}', '$.g ? (exists (@.x ? (@ >= 2) ))');
  665. jsonb_path_query
  666. ------------------
  667. {"x": 2}
  668. (1 row)
  669. select jsonb_path_query('{"g": [{"x": 2}, {"y": 3}]}', 'lax $.g ? (exists (@.x))');
  670. jsonb_path_query
  671. ------------------
  672. {"x": 2}
  673. (1 row)
  674. select jsonb_path_query('{"g": [{"x": 2}, {"y": 3}]}', 'lax $.g ? (exists (@.x + "3"))');
  675. jsonb_path_query
  676. ------------------
  677. (0 rows)
  678. select jsonb_path_query('{"g": [{"x": 2}, {"y": 3}]}', 'lax $.g ? ((exists (@.x + "3")) is unknown)');
  679. jsonb_path_query
  680. ------------------
  681. {"x": 2}
  682. {"y": 3}
  683. (2 rows)
  684. select jsonb_path_query('{"g": [{"x": 2}, {"y": 3}]}', 'strict $.g[*] ? (exists (@.x))');
  685. jsonb_path_query
  686. ------------------
  687. {"x": 2}
  688. (1 row)
  689. select jsonb_path_query('{"g": [{"x": 2}, {"y": 3}]}', 'strict $.g[*] ? ((exists (@.x)) is unknown)');
  690. jsonb_path_query
  691. ------------------
  692. {"y": 3}
  693. (1 row)
  694. select jsonb_path_query('{"g": [{"x": 2}, {"y": 3}]}', 'strict $.g ? (exists (@[*].x))');
  695. jsonb_path_query
  696. ------------------
  697. (0 rows)
  698. select jsonb_path_query('{"g": [{"x": 2}, {"y": 3}]}', 'strict $.g ? ((exists (@[*].x)) is unknown)');
  699. jsonb_path_query
  700. ----------------------
  701. [{"x": 2}, {"y": 3}]
  702. (1 row)
  703. --test ternary logic
  704. select
  705. x, y,
  706. jsonb_path_query(
  707. '[true, false, null]',
  708. '$[*] ? (@ == true && ($x == true && $y == true) ||
  709. @ == false && !($x == true && $y == true) ||
  710. @ == null && ($x == true && $y == true) is unknown)',
  711. jsonb_build_object('x', x, 'y', y)
  712. ) as "x && y"
  713. from
  714. (values (jsonb 'true'), ('false'), ('"null"')) x(x),
  715. (values (jsonb 'true'), ('false'), ('"null"')) y(y);
  716. x | y | x && y
  717. --------+--------+--------
  718. true | true | true
  719. true | false | false
  720. true | "null" | null
  721. false | true | false
  722. false | false | false
  723. false | "null" | false
  724. "null" | true | null
  725. "null" | false | false
  726. "null" | "null" | null
  727. (9 rows)
  728. select
  729. x, y,
  730. jsonb_path_query(
  731. '[true, false, null]',
  732. '$[*] ? (@ == true && ($x == true || $y == true) ||
  733. @ == false && !($x == true || $y == true) ||
  734. @ == null && ($x == true || $y == true) is unknown)',
  735. jsonb_build_object('x', x, 'y', y)
  736. ) as "x || y"
  737. from
  738. (values (jsonb 'true'), ('false'), ('"null"')) x(x),
  739. (values (jsonb 'true'), ('false'), ('"null"')) y(y);
  740. x | y | x || y
  741. --------+--------+--------
  742. true | true | true
  743. true | false | true
  744. true | "null" | true
  745. false | true | true
  746. false | false | false
  747. false | "null" | null
  748. "null" | true | true
  749. "null" | false | null
  750. "null" | "null" | null
  751. (9 rows)
  752. select jsonb '{"a": 1, "b":1}' @? '$ ? (@.a == @.b)';
  753. ?column?
  754. ----------
  755. t
  756. (1 row)
  757. select jsonb '{"c": {"a": 1, "b":1}}' @? '$ ? (@.a == @.b)';
  758. ?column?
  759. ----------
  760. f
  761. (1 row)
  762. select jsonb '{"c": {"a": 1, "b":1}}' @? '$.c ? (@.a == @.b)';
  763. ?column?
  764. ----------
  765. t
  766. (1 row)
  767. select jsonb '{"c": {"a": 1, "b":1}}' @? '$.c ? ($.c.a == @.b)';
  768. ?column?
  769. ----------
  770. t
  771. (1 row)
  772. select jsonb '{"c": {"a": 1, "b":1}}' @? '$.* ? (@.a == @.b)';
  773. ?column?
  774. ----------
  775. t
  776. (1 row)
  777. select jsonb '{"a": 1, "b":1}' @? '$.** ? (@.a == @.b)';
  778. ?column?
  779. ----------
  780. t
  781. (1 row)
  782. select jsonb '{"c": {"a": 1, "b":1}}' @? '$.** ? (@.a == @.b)';
  783. ?column?
  784. ----------
  785. t
  786. (1 row)
  787. select jsonb_path_query('{"c": {"a": 2, "b":1}}', '$.** ? (@.a == 1 + 1)');
  788. jsonb_path_query
  789. ------------------
  790. {"a": 2, "b": 1}
  791. (1 row)
  792. select jsonb_path_query('{"c": {"a": 2, "b":1}}', '$.** ? (@.a == (1 + 1))');
  793. jsonb_path_query
  794. ------------------
  795. {"a": 2, "b": 1}
  796. (1 row)
  797. select jsonb_path_query('{"c": {"a": 2, "b":1}}', '$.** ? (@.a == @.b + 1)');
  798. jsonb_path_query
  799. ------------------
  800. {"a": 2, "b": 1}
  801. (1 row)
  802. select jsonb_path_query('{"c": {"a": 2, "b":1}}', '$.** ? (@.a == (@.b + 1))');
  803. jsonb_path_query
  804. ------------------
  805. {"a": 2, "b": 1}
  806. (1 row)
  807. select jsonb '{"c": {"a": -1, "b":1}}' @? '$.** ? (@.a == - 1)';
  808. ?column?
  809. ----------
  810. t
  811. (1 row)
  812. select jsonb '{"c": {"a": -1, "b":1}}' @? '$.** ? (@.a == -1)';
  813. ?column?
  814. ----------
  815. t
  816. (1 row)
  817. select jsonb '{"c": {"a": -1, "b":1}}' @? '$.** ? (@.a == -@.b)';
  818. ?column?
  819. ----------
  820. t
  821. (1 row)
  822. select jsonb '{"c": {"a": -1, "b":1}}' @? '$.** ? (@.a == - @.b)';
  823. ?column?
  824. ----------
  825. t
  826. (1 row)
  827. select jsonb '{"c": {"a": 0, "b":1}}' @? '$.** ? (@.a == 1 - @.b)';
  828. ?column?
  829. ----------
  830. t
  831. (1 row)
  832. select jsonb '{"c": {"a": 2, "b":1}}' @? '$.** ? (@.a == 1 - - @.b)';
  833. ?column?
  834. ----------
  835. t
  836. (1 row)
  837. select jsonb '{"c": {"a": 0, "b":1}}' @? '$.** ? (@.a == 1 - +@.b)';
  838. ?column?
  839. ----------
  840. t
  841. (1 row)
  842. select jsonb '[1,2,3]' @? '$ ? (+@[*] > +2)';
  843. ?column?
  844. ----------
  845. t
  846. (1 row)
  847. select jsonb '[1,2,3]' @? '$ ? (+@[*] > +3)';
  848. ?column?
  849. ----------
  850. f
  851. (1 row)
  852. select jsonb '[1,2,3]' @? '$ ? (-@[*] < -2)';
  853. ?column?
  854. ----------
  855. t
  856. (1 row)
  857. select jsonb '[1,2,3]' @? '$ ? (-@[*] < -3)';
  858. ?column?
  859. ----------
  860. f
  861. (1 row)
  862. select jsonb '1' @? '$ ? ($ > 0)';
  863. ?column?
  864. ----------
  865. t
  866. (1 row)
  867. -- arithmetic errors
  868. select jsonb_path_query('[1,2,0,3]', '$[*] ? (2 / @ > 0)');
  869. jsonb_path_query
  870. ------------------
  871. 1
  872. 2
  873. 3
  874. (3 rows)
  875. select jsonb_path_query('[1,2,0,3]', '$[*] ? ((2 / @ > 0) is unknown)');
  876. jsonb_path_query
  877. ------------------
  878. 0
  879. (1 row)
  880. select jsonb_path_query('0', '1 / $');
  881. ERROR: division by zero
  882. select jsonb_path_query('0', '1 / $ + 2');
  883. ERROR: division by zero
  884. select jsonb_path_query('0', '-(3 + 1 % $)');
  885. ERROR: division by zero
  886. select jsonb_path_query('1', '$ + "2"');
  887. ERROR: right operand of jsonpath operator + is not a single numeric value
  888. select jsonb_path_query('[1, 2]', '3 * $');
  889. ERROR: right operand of jsonpath operator * is not a single numeric value
  890. select jsonb_path_query('"a"', '-$');
  891. ERROR: operand of unary jsonpath operator - is not a numeric value
  892. select jsonb_path_query('[1,"2",3]', '+$');
  893. ERROR: operand of unary jsonpath operator + is not a numeric value
  894. select jsonb_path_query('1', '$ + "2"', silent => true);
  895. jsonb_path_query
  896. ------------------
  897. (0 rows)
  898. select jsonb_path_query('[1, 2]', '3 * $', silent => true);
  899. jsonb_path_query
  900. ------------------
  901. (0 rows)
  902. select jsonb_path_query('"a"', '-$', silent => true);
  903. jsonb_path_query
  904. ------------------
  905. (0 rows)
  906. select jsonb_path_query('[1,"2",3]', '+$', silent => true);
  907. jsonb_path_query
  908. ------------------
  909. 1
  910. (1 row)
  911. select jsonb '["1",2,0,3]' @? '-$[*]';
  912. ?column?
  913. ----------
  914. t
  915. (1 row)
  916. select jsonb '[1,"2",0,3]' @? '-$[*]';
  917. ?column?
  918. ----------
  919. t
  920. (1 row)
  921. select jsonb '["1",2,0,3]' @? 'strict -$[*]';
  922. ?column?
  923. ----------
  924. (1 row)
  925. select jsonb '[1,"2",0,3]' @? 'strict -$[*]';
  926. ?column?
  927. ----------
  928. (1 row)
  929. -- unwrapping of operator arguments in lax mode
  930. select jsonb_path_query('{"a": [2]}', 'lax $.a * 3');
  931. jsonb_path_query
  932. ------------------
  933. 6
  934. (1 row)
  935. select jsonb_path_query('{"a": [2]}', 'lax $.a + 3');
  936. jsonb_path_query
  937. ------------------
  938. 5
  939. (1 row)
  940. select jsonb_path_query('{"a": [2, 3, 4]}', 'lax -$.a');
  941. jsonb_path_query
  942. ------------------
  943. -2
  944. -3
  945. -4
  946. (3 rows)
  947. -- should fail
  948. select jsonb_path_query('{"a": [1, 2]}', 'lax $.a * 3');
  949. ERROR: left operand of jsonpath operator * is not a single numeric value
  950. select jsonb_path_query('{"a": [1, 2]}', 'lax $.a * 3', silent => true);
  951. jsonb_path_query
  952. ------------------
  953. (0 rows)
  954. -- extension: boolean expressions
  955. select jsonb_path_query('2', '$ > 1');
  956. jsonb_path_query
  957. ------------------
  958. true
  959. (1 row)
  960. select jsonb_path_query('2', '$ <= 1');
  961. jsonb_path_query
  962. ------------------
  963. false
  964. (1 row)
  965. select jsonb_path_query('2', '$ == "2"');
  966. jsonb_path_query
  967. ------------------
  968. null
  969. (1 row)
  970. select jsonb '2' @? '$ == "2"';
  971. ?column?
  972. ----------
  973. t
  974. (1 row)
  975. select jsonb '2' @@ '$ > 1';
  976. ?column?
  977. ----------
  978. t
  979. (1 row)
  980. select jsonb '2' @@ '$ <= 1';
  981. ?column?
  982. ----------
  983. f
  984. (1 row)
  985. select jsonb '2' @@ '$ == "2"';
  986. ?column?
  987. ----------
  988. (1 row)
  989. select jsonb '2' @@ '1';
  990. ?column?
  991. ----------
  992. (1 row)
  993. select jsonb '{}' @@ '$';
  994. ?column?
  995. ----------
  996. (1 row)
  997. select jsonb '[]' @@ '$';
  998. ?column?
  999. ----------
  1000. (1 row)
  1001. select jsonb '[1,2,3]' @@ '$[*]';
  1002. ?column?
  1003. ----------
  1004. (1 row)
  1005. select jsonb '[]' @@ '$[*]';
  1006. ?column?
  1007. ----------
  1008. (1 row)
  1009. select jsonb_path_match('[[1, true], [2, false]]', 'strict $[*] ? (@[0] > $x) [1]', '{"x": 1}');
  1010. jsonb_path_match
  1011. ------------------
  1012. f
  1013. (1 row)
  1014. select jsonb_path_match('[[1, true], [2, false]]', 'strict $[*] ? (@[0] < $x) [1]', '{"x": 2}');
  1015. jsonb_path_match
  1016. ------------------
  1017. t
  1018. (1 row)
  1019. select jsonb_path_match('[{"a": 1}, {"a": 2}, 3]', 'lax exists($[*].a)', silent => false);
  1020. jsonb_path_match
  1021. ------------------
  1022. t
  1023. (1 row)
  1024. select jsonb_path_match('[{"a": 1}, {"a": 2}, 3]', 'lax exists($[*].a)', silent => true);
  1025. jsonb_path_match
  1026. ------------------
  1027. t
  1028. (1 row)
  1029. select jsonb_path_match('[{"a": 1}, {"a": 2}, 3]', 'strict exists($[*].a)', silent => false);
  1030. jsonb_path_match
  1031. ------------------
  1032. (1 row)
  1033. select jsonb_path_match('[{"a": 1}, {"a": 2}, 3]', 'strict exists($[*].a)', silent => true);
  1034. jsonb_path_match
  1035. ------------------
  1036. (1 row)
  1037. select jsonb_path_query('[null,1,true,"a",[],{}]', '$.type()');
  1038. jsonb_path_query
  1039. ------------------
  1040. "array"
  1041. (1 row)
  1042. select jsonb_path_query('[null,1,true,"a",[],{}]', 'lax $.type()');
  1043. jsonb_path_query
  1044. ------------------
  1045. "array"
  1046. (1 row)
  1047. select jsonb_path_query('[null,1,true,"a",[],{}]', '$[*].type()');
  1048. jsonb_path_query
  1049. ------------------
  1050. "null"
  1051. "number"
  1052. "boolean"
  1053. "string"
  1054. "array"
  1055. "object"
  1056. (6 rows)
  1057. select jsonb_path_query('null', 'null.type()');
  1058. jsonb_path_query
  1059. ------------------
  1060. "null"
  1061. (1 row)
  1062. select jsonb_path_query('null', 'true.type()');
  1063. jsonb_path_query
  1064. ------------------
  1065. "boolean"
  1066. (1 row)
  1067. select jsonb_path_query('null', '(123).type()');
  1068. jsonb_path_query
  1069. ------------------
  1070. "number"
  1071. (1 row)
  1072. select jsonb_path_query('null', '"123".type()');
  1073. jsonb_path_query
  1074. ------------------
  1075. "string"
  1076. (1 row)
  1077. select jsonb_path_query('{"a": 2}', '($.a - 5).abs() + 10');
  1078. jsonb_path_query
  1079. ------------------
  1080. 13
  1081. (1 row)
  1082. select jsonb_path_query('{"a": 2.5}', '-($.a * $.a).floor() % 4.3');
  1083. jsonb_path_query
  1084. ------------------
  1085. -1.7
  1086. (1 row)
  1087. select jsonb_path_query('[1, 2, 3]', '($[*] > 2) ? (@ == true)');
  1088. jsonb_path_query
  1089. ------------------
  1090. true
  1091. (1 row)
  1092. select jsonb_path_query('[1, 2, 3]', '($[*] > 3).type()');
  1093. jsonb_path_query
  1094. ------------------
  1095. "boolean"
  1096. (1 row)
  1097. select jsonb_path_query('[1, 2, 3]', '($[*].a > 3).type()');
  1098. jsonb_path_query
  1099. ------------------
  1100. "boolean"
  1101. (1 row)
  1102. select jsonb_path_query('[1, 2, 3]', 'strict ($[*].a > 3).type()');
  1103. jsonb_path_query
  1104. ------------------
  1105. "null"
  1106. (1 row)
  1107. select jsonb_path_query('[1,null,true,"11",[],[1],[1,2,3],{},{"a":1,"b":2}]', 'strict $[*].size()');
  1108. ERROR: jsonpath item method .size() can only be applied to an array
  1109. select jsonb_path_query('[1,null,true,"11",[],[1],[1,2,3],{},{"a":1,"b":2}]', 'strict $[*].size()', silent => true);
  1110. jsonb_path_query
  1111. ------------------
  1112. (0 rows)
  1113. select jsonb_path_query('[1,null,true,"11",[],[1],[1,2,3],{},{"a":1,"b":2}]', 'lax $[*].size()');
  1114. jsonb_path_query
  1115. ------------------
  1116. 1
  1117. 1
  1118. 1
  1119. 1
  1120. 0
  1121. 1
  1122. 3
  1123. 1
  1124. 1
  1125. (9 rows)
  1126. select jsonb_path_query('[0, 1, -2, -3.4, 5.6]', '$[*].abs()');
  1127. jsonb_path_query
  1128. ------------------
  1129. 0
  1130. 1
  1131. 2
  1132. 3.4
  1133. 5.6
  1134. (5 rows)
  1135. select jsonb_path_query('[0, 1, -2, -3.4, 5.6]', '$[*].floor()');
  1136. jsonb_path_query
  1137. ------------------
  1138. 0
  1139. 1
  1140. -2
  1141. -4
  1142. 5
  1143. (5 rows)
  1144. select jsonb_path_query('[0, 1, -2, -3.4, 5.6]', '$[*].ceiling()');
  1145. jsonb_path_query
  1146. ------------------
  1147. 0
  1148. 1
  1149. -2
  1150. -3
  1151. 6
  1152. (5 rows)
  1153. select jsonb_path_query('[0, 1, -2, -3.4, 5.6]', '$[*].ceiling().abs()');
  1154. jsonb_path_query
  1155. ------------------
  1156. 0
  1157. 1
  1158. 2
  1159. 3
  1160. 6
  1161. (5 rows)
  1162. select jsonb_path_query('[0, 1, -2, -3.4, 5.6]', '$[*].ceiling().abs().type()');
  1163. jsonb_path_query
  1164. ------------------
  1165. "number"
  1166. "number"
  1167. "number"
  1168. "number"
  1169. "number"
  1170. (5 rows)
  1171. select jsonb_path_query('[{},1]', '$[*].keyvalue()');
  1172. ERROR: jsonpath item method .keyvalue() can only be applied to an object
  1173. select jsonb_path_query('[{},1]', '$[*].keyvalue()', silent => true);
  1174. jsonb_path_query
  1175. ------------------
  1176. (0 rows)
  1177. select jsonb_path_query('{}', '$.keyvalue()');
  1178. jsonb_path_query
  1179. ------------------
  1180. (0 rows)
  1181. select jsonb_path_query('{"a": 1, "b": [1, 2], "c": {"a": "bbb"}}', '$.keyvalue()');
  1182. jsonb_path_query
  1183. ----------------------------------------------
  1184. {"id": 0, "key": "a", "value": 1}
  1185. {"id": 0, "key": "b", "value": [1, 2]}
  1186. {"id": 0, "key": "c", "value": {"a": "bbb"}}
  1187. (3 rows)
  1188. select jsonb_path_query('[{"a": 1, "b": [1, 2]}, {"c": {"a": "bbb"}}]', '$[*].keyvalue()');
  1189. jsonb_path_query
  1190. -----------------------------------------------
  1191. {"id": 12, "key": "a", "value": 1}
  1192. {"id": 12, "key": "b", "value": [1, 2]}
  1193. {"id": 72, "key": "c", "value": {"a": "bbb"}}
  1194. (3 rows)
  1195. select jsonb_path_query('[{"a": 1, "b": [1, 2]}, {"c": {"a": "bbb"}}]', 'strict $.keyvalue()');
  1196. ERROR: jsonpath item method .keyvalue() can only be applied to an object
  1197. select jsonb_path_query('[{"a": 1, "b": [1, 2]}, {"c": {"a": "bbb"}}]', 'lax $.keyvalue()');
  1198. jsonb_path_query
  1199. -----------------------------------------------
  1200. {"id": 12, "key": "a", "value": 1}
  1201. {"id": 12, "key": "b", "value": [1, 2]}
  1202. {"id": 72, "key": "c", "value": {"a": "bbb"}}
  1203. (3 rows)
  1204. select jsonb_path_query('[{"a": 1, "b": [1, 2]}, {"c": {"a": "bbb"}}]', 'strict $.keyvalue().a');
  1205. ERROR: jsonpath item method .keyvalue() can only be applied to an object
  1206. select jsonb '{"a": 1, "b": [1, 2]}' @? 'lax $.keyvalue()';
  1207. ?column?
  1208. ----------
  1209. t
  1210. (1 row)
  1211. select jsonb '{"a": 1, "b": [1, 2]}' @? 'lax $.keyvalue().key';
  1212. ?column?
  1213. ----------
  1214. t
  1215. (1 row)
  1216. select jsonb_path_query('null', '$.double()');
  1217. ERROR: jsonpath item method .double() can only be applied to a string or numeric value
  1218. select jsonb_path_query('true', '$.double()');
  1219. ERROR: jsonpath item method .double() can only be applied to a string or numeric value
  1220. select jsonb_path_query('null', '$.double()', silent => true);
  1221. jsonb_path_query
  1222. ------------------
  1223. (0 rows)
  1224. select jsonb_path_query('true', '$.double()', silent => true);
  1225. jsonb_path_query
  1226. ------------------
  1227. (0 rows)
  1228. select jsonb_path_query('[]', '$.double()');
  1229. jsonb_path_query
  1230. ------------------
  1231. (0 rows)
  1232. select jsonb_path_query('[]', 'strict $.double()');
  1233. ERROR: jsonpath item method .double() can only be applied to a string or numeric value
  1234. select jsonb_path_query('{}', '$.double()');
  1235. ERROR: jsonpath item method .double() can only be applied to a string or numeric value
  1236. select jsonb_path_query('[]', 'strict $.double()', silent => true);
  1237. jsonb_path_query
  1238. ------------------
  1239. (0 rows)
  1240. select jsonb_path_query('{}', '$.double()', silent => true);
  1241. jsonb_path_query
  1242. ------------------
  1243. (0 rows)
  1244. select jsonb_path_query('1.23', '$.double()');
  1245. jsonb_path_query
  1246. ------------------
  1247. 1.23
  1248. (1 row)
  1249. select jsonb_path_query('"1.23"', '$.double()');
  1250. jsonb_path_query
  1251. ------------------
  1252. 1.23
  1253. (1 row)
  1254. select jsonb_path_query('"1.23aaa"', '$.double()');
  1255. ERROR: string argument of jsonpath item method .double() is not a valid representation of a double precision number
  1256. select jsonb_path_query('1e1000', '$.double()');
  1257. ERROR: numeric argument of jsonpath item method .double() is out of range for type double precision
  1258. select jsonb_path_query('"nan"', '$.double()');
  1259. ERROR: string argument of jsonpath item method .double() is not a valid representation of a double precision number
  1260. select jsonb_path_query('"NaN"', '$.double()');
  1261. ERROR: string argument of jsonpath item method .double() is not a valid representation of a double precision number
  1262. select jsonb_path_query('"inf"', '$.double()');
  1263. ERROR: string argument of jsonpath item method .double() is not a valid representation of a double precision number
  1264. select jsonb_path_query('"-inf"', '$.double()');
  1265. ERROR: string argument of jsonpath item method .double() is not a valid representation of a double precision number
  1266. select jsonb_path_query('"inf"', '$.double()', silent => true);
  1267. jsonb_path_query
  1268. ------------------
  1269. (0 rows)
  1270. select jsonb_path_query('"-inf"', '$.double()', silent => true);
  1271. jsonb_path_query
  1272. ------------------
  1273. (0 rows)
  1274. select jsonb_path_query('{}', '$.abs()');
  1275. ERROR: jsonpath item method .abs() can only be applied to a numeric value
  1276. select jsonb_path_query('true', '$.floor()');
  1277. ERROR: jsonpath item method .floor() can only be applied to a numeric value
  1278. select jsonb_path_query('"1.2"', '$.ceiling()');
  1279. ERROR: jsonpath item method .ceiling() can only be applied to a numeric value
  1280. select jsonb_path_query('{}', '$.abs()', silent => true);
  1281. jsonb_path_query
  1282. ------------------
  1283. (0 rows)
  1284. select jsonb_path_query('true', '$.floor()', silent => true);
  1285. jsonb_path_query
  1286. ------------------
  1287. (0 rows)
  1288. select jsonb_path_query('"1.2"', '$.ceiling()', silent => true);
  1289. jsonb_path_query
  1290. ------------------
  1291. (0 rows)
  1292. select jsonb_path_query('["", "a", "abc", "abcabc"]', '$[*] ? (@ starts with "abc")');
  1293. jsonb_path_query
  1294. ------------------
  1295. "abc"
  1296. "abcabc"
  1297. (2 rows)
  1298. select jsonb_path_query('["", "a", "abc", "abcabc"]', 'strict $ ? (@[*] starts with "abc")');
  1299. jsonb_path_query
  1300. ----------------------------
  1301. ["", "a", "abc", "abcabc"]
  1302. (1 row)
  1303. select jsonb_path_query('["", "a", "abd", "abdabc"]', 'strict $ ? (@[*] starts with "abc")');
  1304. jsonb_path_query
  1305. ------------------
  1306. (0 rows)
  1307. select jsonb_path_query('["abc", "abcabc", null, 1]', 'strict $ ? (@[*] starts with "abc")');
  1308. jsonb_path_query
  1309. ------------------
  1310. (0 rows)
  1311. select jsonb_path_query('["abc", "abcabc", null, 1]', 'strict $ ? ((@[*] starts with "abc") is unknown)');
  1312. jsonb_path_query
  1313. ----------------------------
  1314. ["abc", "abcabc", null, 1]
  1315. (1 row)
  1316. select jsonb_path_query('[[null, 1, "abc", "abcabc"]]', 'lax $ ? (@[*] starts with "abc")');
  1317. jsonb_path_query
  1318. ----------------------------
  1319. [null, 1, "abc", "abcabc"]
  1320. (1 row)
  1321. select jsonb_path_query('[[null, 1, "abd", "abdabc"]]', 'lax $ ? ((@[*] starts with "abc") is unknown)');
  1322. jsonb_path_query
  1323. ----------------------------
  1324. [null, 1, "abd", "abdabc"]
  1325. (1 row)
  1326. select jsonb_path_query('[null, 1, "abd", "abdabc"]', 'lax $[*] ? ((@ starts with "abc") is unknown)');
  1327. jsonb_path_query
  1328. ------------------
  1329. null
  1330. 1
  1331. (2 rows)
  1332. select jsonb_path_query('[null, 1, "abc", "abd", "aBdC", "abdacb", "babc", "adc\nabc", "ab\nadc"]', 'lax $[*] ? (@ like_regex "^ab.*c")');
  1333. jsonb_path_query
  1334. ------------------
  1335. "abc"
  1336. "abdacb"
  1337. (2 rows)
  1338. select jsonb_path_query('[null, 1, "abc", "abd", "aBdC", "abdacb", "babc", "adc\nabc", "ab\nadc"]', 'lax $[*] ? (@ like_regex "^ab.*c" flag "i")');
  1339. jsonb_path_query
  1340. ------------------
  1341. "abc"
  1342. "aBdC"
  1343. "abdacb"
  1344. (3 rows)
  1345. select jsonb_path_query('[null, 1, "abc", "abd", "aBdC", "abdacb", "babc", "adc\nabc", "ab\nadc"]', 'lax $[*] ? (@ like_regex "^ab.*c" flag "m")');
  1346. jsonb_path_query
  1347. ------------------
  1348. "abc"
  1349. "abdacb"
  1350. "adc\nabc"
  1351. (3 rows)
  1352. select jsonb_path_query('[null, 1, "abc", "abd", "aBdC", "abdacb", "babc", "adc\nabc", "ab\nadc"]', 'lax $[*] ? (@ like_regex "^ab.*c" flag "s")');
  1353. jsonb_path_query
  1354. ------------------
  1355. "abc"
  1356. "abdacb"
  1357. "ab\nadc"
  1358. (3 rows)
  1359. select jsonb_path_query('[null, 1, "a\b", "a\\b", "^a\\b$"]', 'lax $[*] ? (@ like_regex "a\\b" flag "q")');
  1360. jsonb_path_query
  1361. ------------------
  1362. "a\\b"
  1363. "^a\\b$"
  1364. (2 rows)
  1365. select jsonb_path_query('[null, 1, "a\b", "a\\b", "^a\\b$"]', 'lax $[*] ? (@ like_regex "a\\b" flag "")');
  1366. jsonb_path_query
  1367. ------------------
  1368. "a\b"
  1369. (1 row)
  1370. select jsonb_path_query('[null, 1, "a\b", "a\\b", "^a\\b$"]', 'lax $[*] ? (@ like_regex "^a\\b$" flag "q")');
  1371. jsonb_path_query
  1372. ------------------
  1373. "^a\\b$"
  1374. (1 row)
  1375. select jsonb_path_query('[null, 1, "a\b", "a\\b", "^a\\b$"]', 'lax $[*] ? (@ like_regex "^a\\B$" flag "q")');
  1376. jsonb_path_query
  1377. ------------------
  1378. (0 rows)
  1379. select jsonb_path_query('[null, 1, "a\b", "a\\b", "^a\\b$"]', 'lax $[*] ? (@ like_regex "^a\\B$" flag "iq")');
  1380. jsonb_path_query
  1381. ------------------
  1382. "^a\\b$"
  1383. (1 row)
  1384. select jsonb_path_query('[null, 1, "a\b", "a\\b", "^a\\b$"]', 'lax $[*] ? (@ like_regex "^a\\b$" flag "")');
  1385. jsonb_path_query
  1386. ------------------
  1387. "a\b"
  1388. (1 row)
  1389. select jsonb_path_query('null', '$.datetime()');
  1390. ERROR: jsonpath item method .datetime() can only be applied to a string
  1391. select jsonb_path_query('true', '$.datetime()');
  1392. ERROR: jsonpath item method .datetime() can only be applied to a string
  1393. select jsonb_path_query('1', '$.datetime()');
  1394. ERROR: jsonpath item method .datetime() can only be applied to a string
  1395. select jsonb_path_query('[]', '$.datetime()');
  1396. jsonb_path_query
  1397. ------------------
  1398. (0 rows)
  1399. select jsonb_path_query('[]', 'strict $.datetime()');
  1400. ERROR: jsonpath item method .datetime() can only be applied to a string
  1401. select jsonb_path_query('{}', '$.datetime()');
  1402. ERROR: jsonpath item method .datetime() can only be applied to a string
  1403. select jsonb_path_query('"bogus"', '$.datetime()');
  1404. ERROR: datetime format is not recognized: "bogus"
  1405. HINT: Use a datetime template argument to specify the input data format.
  1406. select jsonb_path_query('"12:34"', '$.datetime("aaa")');
  1407. ERROR: invalid datetime format separator: "a"
  1408. select jsonb_path_query('"aaaa"', '$.datetime("HH24")');
  1409. ERROR: invalid value "aa" for "HH24"
  1410. DETAIL: Value must be an integer.
  1411. select jsonb '"10-03-2017"' @? '$.datetime("dd-mm-yyyy")';
  1412. ?column?
  1413. ----------
  1414. t
  1415. (1 row)
  1416. select jsonb_path_query('"10-03-2017"', '$.datetime("dd-mm-yyyy")');
  1417. jsonb_path_query
  1418. ------------------
  1419. "2017-03-10"
  1420. (1 row)
  1421. select jsonb_path_query('"10-03-2017"', '$.datetime("dd-mm-yyyy").type()');
  1422. jsonb_path_query
  1423. ------------------
  1424. "date"
  1425. (1 row)
  1426. select jsonb_path_query('"10-03-2017 12:34"', '$.datetime("dd-mm-yyyy")');
  1427. ERROR: trailing characters remain in input string after datetime format
  1428. select jsonb_path_query('"10-03-2017 12:34"', '$.datetime("dd-mm-yyyy").type()');
  1429. ERROR: trailing characters remain in input string after datetime format
  1430. select jsonb_path_query('"10-03-2017 12:34"', ' $.datetime("dd-mm-yyyy HH24:MI").type()');
  1431. jsonb_path_query
  1432. -------------------------------
  1433. "timestamp without time zone"
  1434. (1 row)
  1435. select jsonb_path_query('"10-03-2017 12:34 +05:20"', '$.datetime("dd-mm-yyyy HH24:MI TZH:TZM").type()');
  1436. jsonb_path_query
  1437. ----------------------------
  1438. "timestamp with time zone"
  1439. (1 row)
  1440. select jsonb_path_query('"12:34:56"', '$.datetime("HH24:MI:SS").type()');
  1441. jsonb_path_query
  1442. --------------------------
  1443. "time without time zone"
  1444. (1 row)
  1445. select jsonb_path_query('"12:34:56 +05:20"', '$.datetime("HH24:MI:SS TZH:TZM").type()');
  1446. jsonb_path_query
  1447. -----------------------
  1448. "time with time zone"
  1449. (1 row)
  1450. select jsonb_path_query('"10-03-2017T12:34:56"', '$.datetime("dd-mm-yyyy\"T\"HH24:MI:SS")');
  1451. jsonb_path_query
  1452. -----------------------
  1453. "2017-03-10T12:34:56"
  1454. (1 row)
  1455. select jsonb_path_query('"10-03-2017t12:34:56"', '$.datetime("dd-mm-yyyy\"T\"HH24:MI:SS")');
  1456. ERROR: unmatched format character "T"
  1457. select jsonb_path_query('"10-03-2017 12:34:56"', '$.datetime("dd-mm-yyyy\"T\"HH24:MI:SS")');
  1458. ERROR: unmatched format character "T"
  1459. set time zone '+00';
  1460. select jsonb_path_query('"10-03-2017 12:34"', '$.datetime("dd-mm-yyyy HH24:MI")');
  1461. jsonb_path_query
  1462. -----------------------
  1463. "2017-03-10T12:34:00"
  1464. (1 row)
  1465. select jsonb_path_query('"10-03-2017 12:34"', '$.datetime("dd-mm-yyyy HH24:MI TZH")');
  1466. ERROR: input string is too short for datetime format
  1467. select jsonb_path_query('"10-03-2017 12:34 +05"', '$.datetime("dd-mm-yyyy HH24:MI TZH")');
  1468. jsonb_path_query
  1469. -----------------------------
  1470. "2017-03-10T12:34:00+05:00"
  1471. (1 row)
  1472. select jsonb_path_query('"10-03-2017 12:34 -05"', '$.datetime("dd-mm-yyyy HH24:MI TZH")');
  1473. jsonb_path_query
  1474. -----------------------------
  1475. "2017-03-10T12:34:00-05:00"
  1476. (1 row)
  1477. select jsonb_path_query('"10-03-2017 12:34 +05:20"', '$.datetime("dd-mm-yyyy HH24:MI TZH:TZM")');
  1478. jsonb_path_query
  1479. -----------------------------
  1480. "2017-03-10T12:34:00+05:20"
  1481. (1 row)
  1482. select jsonb_path_query('"10-03-2017 12:34 -05:20"', '$.datetime("dd-mm-yyyy HH24:MI TZH:TZM")');
  1483. jsonb_path_query
  1484. -----------------------------
  1485. "2017-03-10T12:34:00-05:20"
  1486. (1 row)
  1487. select jsonb_path_query('"12:34"', '$.datetime("HH24:MI")');
  1488. jsonb_path_query
  1489. ------------------
  1490. "12:34:00"
  1491. (1 row)
  1492. select jsonb_path_query('"12:34"', '$.datetime("HH24:MI TZH")');
  1493. ERROR: input string is too short for datetime format
  1494. select jsonb_path_query('"12:34 +05"', '$.datetime("HH24:MI TZH")');
  1495. jsonb_path_query
  1496. ------------------
  1497. "12:34:00+05:00"
  1498. (1 row)
  1499. select jsonb_path_query('"12:34 -05"', '$.datetime("HH24:MI TZH")');
  1500. jsonb_path_query
  1501. ------------------
  1502. "12:34:00-05:00"
  1503. (1 row)
  1504. select jsonb_path_query('"12:34 +05:20"', '$.datetime("HH24:MI TZH:TZM")');
  1505. jsonb_path_query
  1506. ------------------
  1507. "12:34:00+05:20"
  1508. (1 row)
  1509. select jsonb_path_query('"12:34 -05:20"', '$.datetime("HH24:MI TZH:TZM")');
  1510. jsonb_path_query
  1511. ------------------
  1512. "12:34:00-05:20"
  1513. (1 row)
  1514. set time zone '+10';
  1515. select jsonb_path_query('"10-03-2017 12:34"', '$.datetime("dd-mm-yyyy HH24:MI")');
  1516. jsonb_path_query
  1517. -----------------------
  1518. "2017-03-10T12:34:00"
  1519. (1 row)
  1520. select jsonb_path_query('"10-03-2017 12:34"', '$.datetime("dd-mm-yyyy HH24:MI TZH")');
  1521. ERROR: input string is too short for datetime format
  1522. select jsonb_path_query('"10-03-2017 12:34 +05"', '$.datetime("dd-mm-yyyy HH24:MI TZH")');
  1523. jsonb_path_query
  1524. -----------------------------
  1525. "2017-03-10T12:34:00+05:00"
  1526. (1 row)
  1527. select jsonb_path_query('"10-03-2017 12:34 -05"', '$.datetime("dd-mm-yyyy HH24:MI TZH")');
  1528. jsonb_path_query
  1529. -----------------------------
  1530. "2017-03-10T12:34:00-05:00"
  1531. (1 row)
  1532. select jsonb_path_query('"10-03-2017 12:34 +05:20"', '$.datetime("dd-mm-yyyy HH24:MI TZH:TZM")');
  1533. jsonb_path_query
  1534. -----------------------------
  1535. "2017-03-10T12:34:00+05:20"
  1536. (1 row)
  1537. select jsonb_path_query('"10-03-2017 12:34 -05:20"', '$.datetime("dd-mm-yyyy HH24:MI TZH:TZM")');
  1538. jsonb_path_query
  1539. -----------------------------
  1540. "2017-03-10T12:34:00-05:20"
  1541. (1 row)
  1542. select jsonb_path_query('"12:34"', '$.datetime("HH24:MI")');
  1543. jsonb_path_query
  1544. ------------------
  1545. "12:34:00"
  1546. (1 row)
  1547. select jsonb_path_query('"12:34"', '$.datetime("HH24:MI TZH")');
  1548. ERROR: input string is too short for datetime format
  1549. select jsonb_path_query('"12:34 +05"', '$.datetime("HH24:MI TZH")');
  1550. jsonb_path_query
  1551. ------------------
  1552. "12:34:00+05:00"
  1553. (1 row)
  1554. select jsonb_path_query('"12:34 -05"', '$.datetime("HH24:MI TZH")');
  1555. jsonb_path_query
  1556. ------------------
  1557. "12:34:00-05:00"
  1558. (1 row)
  1559. select jsonb_path_query('"12:34 +05:20"', '$.datetime("HH24:MI TZH:TZM")');
  1560. jsonb_path_query
  1561. ------------------
  1562. "12:34:00+05:20"
  1563. (1 row)
  1564. select jsonb_path_query('"12:34 -05:20"', '$.datetime("HH24:MI TZH:TZM")');
  1565. jsonb_path_query
  1566. ------------------
  1567. "12:34:00-05:20"
  1568. (1 row)
  1569. set time zone default;
  1570. select jsonb_path_query('"2017-03-10"', '$.datetime().type()');
  1571. jsonb_path_query
  1572. ------------------
  1573. "date"
  1574. (1 row)
  1575. select jsonb_path_query('"2017-03-10"', '$.datetime()');
  1576. jsonb_path_query
  1577. ------------------
  1578. "2017-03-10"
  1579. (1 row)
  1580. select jsonb_path_query('"2017-03-10 12:34:56"', '$.datetime().type()');
  1581. jsonb_path_query
  1582. -------------------------------
  1583. "timestamp without time zone"
  1584. (1 row)
  1585. select jsonb_path_query('"2017-03-10 12:34:56"', '$.datetime()');
  1586. jsonb_path_query
  1587. -----------------------
  1588. "2017-03-10T12:34:56"
  1589. (1 row)
  1590. select jsonb_path_query('"2017-03-10 12:34:56+3"', '$.datetime().type()');
  1591. jsonb_path_query
  1592. ----------------------------
  1593. "timestamp with time zone"
  1594. (1 row)
  1595. select jsonb_path_query('"2017-03-10 12:34:56+3"', '$.datetime()');
  1596. jsonb_path_query
  1597. -----------------------------
  1598. "2017-03-10T12:34:56+03:00"
  1599. (1 row)
  1600. select jsonb_path_query('"2017-03-10 12:34:56+3:10"', '$.datetime().type()');
  1601. jsonb_path_query
  1602. ----------------------------
  1603. "timestamp with time zone"
  1604. (1 row)
  1605. select jsonb_path_query('"2017-03-10 12:34:56+3:10"', '$.datetime()');
  1606. jsonb_path_query
  1607. -----------------------------
  1608. "2017-03-10T12:34:56+03:10"
  1609. (1 row)
  1610. select jsonb_path_query('"2017-03-10T12:34:56+3:10"', '$.datetime()');
  1611. jsonb_path_query
  1612. -----------------------------
  1613. "2017-03-10T12:34:56+03:10"
  1614. (1 row)
  1615. select jsonb_path_query('"2017-03-10t12:34:56+3:10"', '$.datetime()');
  1616. ERROR: datetime format is not recognized: "2017-03-10t12:34:56+3:10"
  1617. HINT: Use a datetime template argument to specify the input data format.
  1618. select jsonb_path_query('"12:34:56"', '$.datetime().type()');
  1619. jsonb_path_query
  1620. --------------------------
  1621. "time without time zone"
  1622. (1 row)
  1623. select jsonb_path_query('"12:34:56"', '$.datetime()');
  1624. jsonb_path_query
  1625. ------------------
  1626. "12:34:56"
  1627. (1 row)
  1628. select jsonb_path_query('"12:34:56+3"', '$.datetime().type()');
  1629. jsonb_path_query
  1630. -----------------------
  1631. "time with time zone"
  1632. (1 row)
  1633. select jsonb_path_query('"12:34:56+3"', '$.datetime()');
  1634. jsonb_path_query
  1635. ------------------
  1636. "12:34:56+03:00"
  1637. (1 row)
  1638. select jsonb_path_query('"12:34:56+3:10"', '$.datetime().type()');
  1639. jsonb_path_query
  1640. -----------------------
  1641. "time with time zone"
  1642. (1 row)
  1643. select jsonb_path_query('"12:34:56+3:10"', '$.datetime()');
  1644. jsonb_path_query
  1645. ------------------
  1646. "12:34:56+03:10"
  1647. (1 row)
  1648. set time zone '+00';
  1649. -- date comparison
  1650. select jsonb_path_query(
  1651. '["2017-03-10", "2017-03-11", "2017-03-09", "12:34:56", "01:02:03+04", "2017-03-10 00:00:00", "2017-03-10 12:34:56", "2017-03-10 01:02:03+04", "2017-03-10 03:00:00+03"]',
  1652. '$[*].datetime() ? (@ == "10.03.2017".datetime("dd.mm.yyyy"))');
  1653. ERROR: cannot convert value from date to timestamptz without time zone usage
  1654. HINT: Use *_tz() function for time zone support.
  1655. select jsonb_path_query(
  1656. '["2017-03-10", "2017-03-11", "2017-03-09", "12:34:56", "01:02:03+04", "2017-03-10 00:00:00", "2017-03-10 12:34:56", "2017-03-10 01:02:03+04", "2017-03-10 03:00:00+03"]',
  1657. '$[*].datetime() ? (@ >= "10.03.2017".datetime("dd.mm.yyyy"))');
  1658. ERROR: cannot convert value from date to timestamptz without time zone usage
  1659. HINT: Use *_tz() function for time zone support.
  1660. select jsonb_path_query(
  1661. '["2017-03-10", "2017-03-11", "2017-03-09", "12:34:56", "01:02:03+04", "2017-03-10 00:00:00", "2017-03-10 12:34:56", "2017-03-10 01:02:03+04", "2017-03-10 03:00:00+03"]',
  1662. '$[*].datetime() ? (@ < "10.03.2017".datetime("dd.mm.yyyy"))');
  1663. ERROR: cannot convert value from date to timestamptz without time zone usage
  1664. HINT: Use *_tz() function for time zone support.
  1665. select jsonb_path_query_tz(
  1666. '["2017-03-10", "2017-03-11", "2017-03-09", "12:34:56", "01:02:03+04", "2017-03-10 00:00:00", "2017-03-10 12:34:56", "2017-03-10 01:02:03+04", "2017-03-10 03:00:00+03"]',
  1667. '$[*].datetime() ? (@ == "10.03.2017".datetime("dd.mm.yyyy"))');
  1668. jsonb_path_query_tz
  1669. -----------------------------
  1670. "2017-03-10"
  1671. "2017-03-10T00:00:00"
  1672. "2017-03-10T03:00:00+03:00"
  1673. (3 rows)
  1674. select jsonb_path_query_tz(
  1675. '["2017-03-10", "2017-03-11", "2017-03-09", "12:34:56", "01:02:03+04", "2017-03-10 00:00:00", "2017-03-10 12:34:56", "2017-03-10 01:02:03+04", "2017-03-10 03:00:00+03"]',
  1676. '$[*].datetime() ? (@ >= "10.03.2017".datetime("dd.mm.yyyy"))');
  1677. jsonb_path_query_tz
  1678. -----------------------------
  1679. "2017-03-10"
  1680. "2017-03-11"
  1681. "2017-03-10T00:00:00"
  1682. "2017-03-10T12:34:56"
  1683. "2017-03-10T03:00:00+03:00"
  1684. (5 rows)
  1685. select jsonb_path_query_tz(
  1686. '["2017-03-10", "2017-03-11", "2017-03-09", "12:34:56", "01:02:03+04", "2017-03-10 00:00:00", "2017-03-10 12:34:56", "2017-03-10 01:02:03+04", "2017-03-10 03:00:00+03"]',
  1687. '$[*].datetime() ? (@ < "10.03.2017".datetime("dd.mm.yyyy"))');
  1688. jsonb_path_query_tz
  1689. -----------------------------
  1690. "2017-03-09"
  1691. "2017-03-10T01:02:03+04:00"
  1692. (2 rows)
  1693. -- time comparison
  1694. select jsonb_path_query(
  1695. '["12:34:00", "12:35:00", "12:36:00", "12:35:00+00", "12:35:00+01", "13:35:00+01", "2017-03-10", "2017-03-10 12:35:00", "2017-03-10 12:35:00+01"]',
  1696. '$[*].datetime() ? (@ == "12:35".datetime("HH24:MI"))');
  1697. ERROR: cannot convert value from time to timetz without time zone usage
  1698. HINT: Use *_tz() function for time zone support.
  1699. select jsonb_path_query(
  1700. '["12:34:00", "12:35:00", "12:36:00", "12:35:00+00", "12:35:00+01", "13:35:00+01", "2017-03-10", "2017-03-10 12:35:00", "2017-03-10 12:35:00+01"]',
  1701. '$[*].datetime() ? (@ >= "12:35".datetime("HH24:MI"))');
  1702. ERROR: cannot convert value from time to timetz without time zone usage
  1703. HINT: Use *_tz() function for time zone support.
  1704. select jsonb_path_query(
  1705. '["12:34:00", "12:35:00", "12:36:00", "12:35:00+00", "12:35:00+01", "13:35:00+01", "2017-03-10", "2017-03-10 12:35:00", "2017-03-10 12:35:00+01"]',
  1706. '$[*].datetime() ? (@ < "12:35".datetime("HH24:MI"))');
  1707. ERROR: cannot convert value from time to timetz without time zone usage
  1708. HINT: Use *_tz() function for time zone support.
  1709. select jsonb_path_query_tz(
  1710. '["12:34:00", "12:35:00", "12:36:00", "12:35:00+00", "12:35:00+01", "13:35:00+01", "2017-03-10", "2017-03-10 12:35:00", "2017-03-10 12:35:00+01"]',
  1711. '$[*].datetime() ? (@ == "12:35".datetime("HH24:MI"))');
  1712. jsonb_path_query_tz
  1713. ---------------------
  1714. "12:35:00"
  1715. "12:35:00+00:00"
  1716. (2 rows)
  1717. select jsonb_path_query_tz(
  1718. '["12:34:00", "12:35:00", "12:36:00", "12:35:00+00", "12:35:00+01", "13:35:00+01", "2017-03-10", "2017-03-10 12:35:00", "2017-03-10 12:35:00+01"]',
  1719. '$[*].datetime() ? (@ >= "12:35".datetime("HH24:MI"))');
  1720. jsonb_path_query_tz
  1721. ---------------------
  1722. "12:35:00"
  1723. "12:36:00"
  1724. "12:35:00+00:00"
  1725. (3 rows)
  1726. select jsonb_path_query_tz(
  1727. '["12:34:00", "12:35:00", "12:36:00", "12:35:00+00", "12:35:00+01", "13:35:00+01", "2017-03-10", "2017-03-10 12:35:00", "2017-03-10 12:35:00+01"]',
  1728. '$[*].datetime() ? (@ < "12:35".datetime("HH24:MI"))');
  1729. jsonb_path_query_tz
  1730. ---------------------
  1731. "12:34:00"
  1732. "12:35:00+01:00"
  1733. "13:35:00+01:00"
  1734. (3 rows)
  1735. -- timetz comparison
  1736. select jsonb_path_query(
  1737. '["12:34:00+01", "12:35:00+01", "12:36:00+01", "12:35:00+02", "12:35:00-02", "10:35:00", "11:35:00", "12:35:00", "2017-03-10", "2017-03-10 12:35:00", "2017-03-10 12:35:00 +1"]',
  1738. '$[*].datetime() ? (@ == "12:35 +1".datetime("HH24:MI TZH"))');
  1739. ERROR: cannot convert value from time to timetz without time zone usage
  1740. HINT: Use *_tz() function for time zone support.
  1741. select jsonb_path_query(
  1742. '["12:34:00+01", "12:35:00+01", "12:36:00+01", "12:35:00+02", "12:35:00-02", "10:35:00", "11:35:00", "12:35:00", "2017-03-10", "2017-03-10 12:35:00", "2017-03-10 12:35:00 +1"]',
  1743. '$[*].datetime() ? (@ >= "12:35 +1".datetime("HH24:MI TZH"))');
  1744. ERROR: cannot convert value from time to timetz without time zone usage
  1745. HINT: Use *_tz() function for time zone support.
  1746. select jsonb_path_query(
  1747. '["12:34:00+01", "12:35:00+01", "12:36:00+01", "12:35:00+02", "12:35:00-02", "10:35:00", "11:35:00", "12:35:00", "2017-03-10", "2017-03-10 12:35:00", "2017-03-10 12:35:00 +1"]',
  1748. '$[*].datetime() ? (@ < "12:35 +1".datetime("HH24:MI TZH"))');
  1749. ERROR: cannot convert value from time to timetz without time zone usage
  1750. HINT: Use *_tz() function for time zone support.
  1751. select jsonb_path_query_tz(
  1752. '["12:34:00+01", "12:35:00+01", "12:36:00+01", "12:35:00+02", "12:35:00-02", "10:35:00", "11:35:00", "12:35:00", "2017-03-10", "2017-03-10 12:35:00", "2017-03-10 12:35:00 +1"]',
  1753. '$[*].datetime() ? (@ == "12:35 +1".datetime("HH24:MI TZH"))');
  1754. jsonb_path_query_tz
  1755. ---------------------
  1756. "12:35:00+01:00"
  1757. (1 row)
  1758. select jsonb_path_query_tz(
  1759. '["12:34:00+01", "12:35:00+01", "12:36:00+01", "12:35:00+02", "12:35:00-02", "10:35:00", "11:35:00", "12:35:00", "2017-03-10", "2017-03-10 12:35:00", "2017-03-10 12:35:00 +1"]',
  1760. '$[*].datetime() ? (@ >= "12:35 +1".datetime("HH24:MI TZH"))');
  1761. jsonb_path_query_tz
  1762. ---------------------
  1763. "12:35:00+01:00"
  1764. "12:36:00+01:00"
  1765. "12:35:00-02:00"
  1766. "11:35:00"
  1767. "12:35:00"
  1768. (5 rows)
  1769. select jsonb_path_query_tz(
  1770. '["12:34:00+01", "12:35:00+01", "12:36:00+01", "12:35:00+02", "12:35:00-02", "10:35:00", "11:35:00", "12:35:00", "2017-03-10", "2017-03-10 12:35:00", "2017-03-10 12:35:00 +1"]',
  1771. '$[*].datetime() ? (@ < "12:35 +1".datetime("HH24:MI TZH"))');
  1772. jsonb_path_query_tz
  1773. ---------------------
  1774. "12:34:00+01:00"
  1775. "12:35:00+02:00"
  1776. "10:35:00"
  1777. (3 rows)
  1778. -- timestamp comparison
  1779. select jsonb_path_query(
  1780. '["2017-03-10 12:34:00", "2017-03-10 12:35:00", "2017-03-10 12:36:00", "2017-03-10 12:35:00+01", "2017-03-10 13:35:00+01", "2017-03-10 12:35:00-01", "2017-03-10", "2017-03-11", "12:34:56", "12:34:56+01"]',
  1781. '$[*].datetime() ? (@ == "10.03.2017 12:35".datetime("dd.mm.yyyy HH24:MI"))');
  1782. ERROR: cannot convert value from timestamp to timestamptz without time zone usage
  1783. HINT: Use *_tz() function for time zone support.
  1784. select jsonb_path_query(
  1785. '["2017-03-10 12:34:00", "2017-03-10 12:35:00", "2017-03-10 12:36:00", "2017-03-10 12:35:00+01", "2017-03-10 13:35:00+01", "2017-03-10 12:35:00-01", "2017-03-10", "2017-03-11", "12:34:56", "12:34:56+01"]',
  1786. '$[*].datetime() ? (@ >= "10.03.2017 12:35".datetime("dd.mm.yyyy HH24:MI"))');
  1787. ERROR: cannot convert value from timestamp to timestamptz without time zone usage
  1788. HINT: Use *_tz() function for time zone support.
  1789. select jsonb_path_query(
  1790. '["2017-03-10 12:34:00", "2017-03-10 12:35:00", "2017-03-10 12:36:00", "2017-03-10 12:35:00+01", "2017-03-10 13:35:00+01", "2017-03-10 12:35:00-01", "2017-03-10", "2017-03-11", "12:34:56", "12:34:56+01"]',
  1791. '$[*].datetime() ? (@ < "10.03.2017 12:35".datetime("dd.mm.yyyy HH24:MI"))');
  1792. ERROR: cannot convert value from timestamp to timestamptz without time zone usage
  1793. HINT: Use *_tz() function for time zone support.
  1794. select jsonb_path_query_tz(
  1795. '["2017-03-10 12:34:00", "2017-03-10 12:35:00", "2017-03-10 12:36:00", "2017-03-10 12:35:00+01", "2017-03-10 13:35:00+01", "2017-03-10 12:35:00-01", "2017-03-10", "2017-03-11", "12:34:56", "12:34:56+01"]',
  1796. '$[*].datetime() ? (@ == "10.03.2017 12:35".datetime("dd.mm.yyyy HH24:MI"))');
  1797. jsonb_path_query_tz
  1798. -----------------------------
  1799. "2017-03-10T12:35:00"
  1800. "2017-03-10T13:35:00+01:00"
  1801. (2 rows)
  1802. select jsonb_path_query_tz(
  1803. '["2017-03-10 12:34:00", "2017-03-10 12:35:00", "2017-03-10 12:36:00", "2017-03-10 12:35:00+01", "2017-03-10 13:35:00+01", "2017-03-10 12:35:00-01", "2017-03-10", "2017-03-11", "12:34:56", "12:34:56+01"]',
  1804. '$[*].datetime() ? (@ >= "10.03.2017 12:35".datetime("dd.mm.yyyy HH24:MI"))');
  1805. jsonb_path_query_tz
  1806. -----------------------------
  1807. "2017-03-10T12:35:00"
  1808. "2017-03-10T12:36:00"
  1809. "2017-03-10T13:35:00+01:00"
  1810. "2017-03-10T12:35:00-01:00"
  1811. "2017-03-11"
  1812. (5 rows)
  1813. select jsonb_path_query_tz(
  1814. '["2017-03-10 12:34:00", "2017-03-10 12:35:00", "2017-03-10 12:36:00", "2017-03-10 12:35:00+01", "2017-03-10 13:35:00+01", "2017-03-10 12:35:00-01", "2017-03-10", "2017-03-11", "12:34:56", "12:34:56+01"]',
  1815. '$[*].datetime() ? (@ < "10.03.2017 12:35".datetime("dd.mm.yyyy HH24:MI"))');
  1816. jsonb_path_query_tz
  1817. -----------------------------
  1818. "2017-03-10T12:34:00"
  1819. "2017-03-10T12:35:00+01:00"
  1820. "2017-03-10"
  1821. (3 rows)
  1822. -- timestamptz comparison
  1823. select jsonb_path_query(
  1824. '["2017-03-10 12:34:00+01", "2017-03-10 12:35:00+01", "2017-03-10 12:36:00+01", "2017-03-10 12:35:00+02", "2017-03-10 12:35:00-02", "2017-03-10 10:35:00", "2017-03-10 11:35:00", "2017-03-10 12:35:00", "2017-03-10", "2017-03-11", "12:34:56", "12:34:56+01"]',
  1825. '$[*].datetime() ? (@ == "10.03.2017 12:35 +1".datetime("dd.mm.yyyy HH24:MI TZH"))');
  1826. ERROR: cannot convert value from timestamp to timestamptz without time zone usage
  1827. HINT: Use *_tz() function for time zone support.
  1828. select jsonb_path_query(
  1829. '["2017-03-10 12:34:00+01", "2017-03-10 12:35:00+01", "2017-03-10 12:36:00+01", "2017-03-10 12:35:00+02", "2017-03-10 12:35:00-02", "2017-03-10 10:35:00", "2017-03-10 11:35:00", "2017-03-10 12:35:00", "2017-03-10", "2017-03-11", "12:34:56", "12:34:56+01"]',
  1830. '$[*].datetime() ? (@ >= "10.03.2017 12:35 +1".datetime("dd.mm.yyyy HH24:MI TZH"))');
  1831. ERROR: cannot convert value from timestamp to timestamptz without time zone usage
  1832. HINT: Use *_tz() function for time zone support.
  1833. select jsonb_path_query(
  1834. '["2017-03-10 12:34:00+01", "2017-03-10 12:35:00+01", "2017-03-10 12:36:00+01", "2017-03-10 12:35:00+02", "2017-03-10 12:35:00-02", "2017-03-10 10:35:00", "2017-03-10 11:35:00", "2017-03-10 12:35:00", "2017-03-10", "2017-03-11", "12:34:56", "12:34:56+01"]',
  1835. '$[*].datetime() ? (@ < "10.03.2017 12:35 +1".datetime("dd.mm.yyyy HH24:MI TZH"))');
  1836. ERROR: cannot convert value from timestamp to timestamptz without time zone usage
  1837. HINT: Use *_tz() function for time zone support.
  1838. select jsonb_path_query_tz(
  1839. '["2017-03-10 12:34:00+01", "2017-03-10 12:35:00+01", "2017-03-10 12:36:00+01", "2017-03-10 12:35:00+02", "2017-03-10 12:35:00-02", "2017-03-10 10:35:00", "2017-03-10 11:35:00", "2017-03-10 12:35:00", "2017-03-10", "2017-03-11", "12:34:56", "12:34:56+01"]',
  1840. '$[*].datetime() ? (@ == "10.03.2017 12:35 +1".datetime("dd.mm.yyyy HH24:MI TZH"))');
  1841. jsonb_path_query_tz
  1842. -----------------------------
  1843. "2017-03-10T12:35:00+01:00"
  1844. "2017-03-10T11:35:00"
  1845. (2 rows)
  1846. select jsonb_path_query_tz(
  1847. '["2017-03-10 12:34:00+01", "2017-03-10 12:35:00+01", "2017-03-10 12:36:00+01", "2017-03-10 12:35:00+02", "2017-03-10 12:35:00-02", "2017-03-10 10:35:00", "2017-03-10 11:35:00", "2017-03-10 12:35:00", "2017-03-10", "2017-03-11", "12:34:56", "12:34:56+01"]',
  1848. '$[*].datetime() ? (@ >= "10.03.2017 12:35 +1".datetime("dd.mm.yyyy HH24:MI TZH"))');
  1849. jsonb_path_query_tz
  1850. -----------------------------
  1851. "2017-03-10T12:35:00+01:00"
  1852. "2017-03-10T12:36:00+01:00"
  1853. "2017-03-10T12:35:00-02:00"
  1854. "2017-03-10T11:35:00"
  1855. "2017-03-10T12:35:00"
  1856. "2017-03-11"
  1857. (6 rows)
  1858. select jsonb_path_query_tz(
  1859. '["2017-03-10 12:34:00+01", "2017-03-10 12:35:00+01", "2017-03-10 12:36:00+01", "2017-03-10 12:35:00+02", "2017-03-10 12:35:00-02", "2017-03-10 10:35:00", "2017-03-10 11:35:00", "2017-03-10 12:35:00", "2017-03-10", "2017-03-11", "12:34:56", "12:34:56+01"]',
  1860. '$[*].datetime() ? (@ < "10.03.2017 12:35 +1".datetime("dd.mm.yyyy HH24:MI TZH"))');
  1861. jsonb_path_query_tz
  1862. -----------------------------
  1863. "2017-03-10T12:34:00+01:00"
  1864. "2017-03-10T12:35:00+02:00"
  1865. "2017-03-10T10:35:00"
  1866. "2017-03-10"
  1867. (4 rows)
  1868. -- overflow during comparison
  1869. select jsonb_path_query('"1000000-01-01"', '$.datetime() > "2020-01-01 12:00:00".datetime()'::jsonpath);
  1870. jsonb_path_query
  1871. ------------------
  1872. true
  1873. (1 row)
  1874. set time zone default;
  1875. -- jsonpath operators
  1876. SELECT jsonb_path_query('[{"a": 1}, {"a": 2}]', '$[*]');
  1877. jsonb_path_query
  1878. ------------------
  1879. {"a": 1}
  1880. {"a": 2}
  1881. (2 rows)
  1882. SELECT jsonb_path_query('[{"a": 1}, {"a": 2}]', '$[*] ? (@.a > 10)');
  1883. jsonb_path_query
  1884. ------------------
  1885. (0 rows)
  1886. SELECT jsonb_path_query_array('[{"a": 1}, {"a": 2}, {}]', 'strict $[*].a');
  1887. ERROR: JSON object does not contain key "a"
  1888. SELECT jsonb_path_query_array('[{"a": 1}, {"a": 2}]', '$[*].a');
  1889. jsonb_path_query_array
  1890. ------------------------
  1891. [1, 2]
  1892. (1 row)
  1893. SELECT jsonb_path_query_array('[{"a": 1}, {"a": 2}]', '$[*].a ? (@ == 1)');
  1894. jsonb_path_query_array
  1895. ------------------------
  1896. [1]
  1897. (1 row)
  1898. SELECT jsonb_path_query_array('[{"a": 1}, {"a": 2}]', '$[*].a ? (@ > 10)');
  1899. jsonb_path_query_array
  1900. ------------------------
  1901. []
  1902. (1 row)
  1903. SELECT jsonb_path_query_array('[{"a": 1}, {"a": 2}, {"a": 3}, {"a": 5}]', '$[*].a ? (@ > $min && @ < $max)', vars => '{"min": 1, "max": 4}');
  1904. jsonb_path_query_array
  1905. ------------------------
  1906. [2, 3]
  1907. (1 row)
  1908. SELECT jsonb_path_query_array('[{"a": 1}, {"a": 2}, {"a": 3}, {"a": 5}]', '$[*].a ? (@ > $min && @ < $max)', vars => '{"min": 3, "max": 4}');
  1909. jsonb_path_query_array
  1910. ------------------------
  1911. []
  1912. (1 row)
  1913. SELECT jsonb_path_query_first('[{"a": 1}, {"a": 2}, {}]', 'strict $[*].a');
  1914. ERROR: JSON object does not contain key "a"
  1915. SELECT jsonb_path_query_first('[{"a": 1}, {"a": 2}, {}]', 'strict $[*].a', silent => true);
  1916. jsonb_path_query_first
  1917. ------------------------
  1918. 1
  1919. (1 row)
  1920. SELECT jsonb_path_query_first('[{"a": 1}, {"a": 2}]', '$[*].a');
  1921. jsonb_path_query_first
  1922. ------------------------
  1923. 1
  1924. (1 row)
  1925. SELECT jsonb_path_query_first('[{"a": 1}, {"a": 2}]', '$[*].a ? (@ == 1)');
  1926. jsonb_path_query_first
  1927. ------------------------
  1928. 1
  1929. (1 row)
  1930. SELECT jsonb_path_query_first('[{"a": 1}, {"a": 2}]', '$[*].a ? (@ > 10)');
  1931. jsonb_path_query_first
  1932. ------------------------
  1933. (1 row)
  1934. SELECT jsonb_path_query_first('[{"a": 1}, {"a": 2}, {"a": 3}, {"a": 5}]', '$[*].a ? (@ > $min && @ < $max)', vars => '{"min": 1, "max": 4}');
  1935. jsonb_path_query_first
  1936. ------------------------
  1937. 2
  1938. (1 row)
  1939. SELECT jsonb_path_query_first('[{"a": 1}, {"a": 2}, {"a": 3}, {"a": 5}]', '$[*].a ? (@ > $min && @ < $max)', vars => '{"min": 3, "max": 4}');
  1940. jsonb_path_query_first
  1941. ------------------------
  1942. (1 row)
  1943. SELECT jsonb '[{"a": 1}, {"a": 2}]' @? '$[*].a ? (@ > 1)';
  1944. ?column?
  1945. ----------
  1946. t
  1947. (1 row)
  1948. SELECT jsonb '[{"a": 1}, {"a": 2}]' @? '$[*] ? (@.a > 2)';
  1949. ?column?
  1950. ----------
  1951. f
  1952. (1 row)
  1953. SELECT jsonb_path_exists('[{"a": 1}, {"a": 2}]', '$[*].a ? (@ > 1)');
  1954. jsonb_path_exists
  1955. -------------------
  1956. t
  1957. (1 row)
  1958. SELECT jsonb_path_exists('[{"a": 1}, {"a": 2}, {"a": 3}, {"a": 5}]', '$[*] ? (@.a > $min && @.a < $max)', vars => '{"min": 1, "max": 4}');
  1959. jsonb_path_exists
  1960. -------------------
  1961. t
  1962. (1 row)
  1963. SELECT jsonb_path_exists('[{"a": 1}, {"a": 2}, {"a": 3}, {"a": 5}]', '$[*] ? (@.a > $min && @.a < $max)', vars => '{"min": 3, "max": 4}');
  1964. jsonb_path_exists
  1965. -------------------
  1966. f
  1967. (1 row)
  1968. SELECT jsonb_path_match('true', '$', silent => false);
  1969. jsonb_path_match
  1970. ------------------
  1971. t
  1972. (1 row)
  1973. SELECT jsonb_path_match('false', '$', silent => false);
  1974. jsonb_path_match
  1975. ------------------
  1976. f
  1977. (1 row)
  1978. SELECT jsonb_path_match('null', '$', silent => false);
  1979. jsonb_path_match
  1980. ------------------
  1981. (1 row)
  1982. SELECT jsonb_path_match('1', '$', silent => true);
  1983. jsonb_path_match
  1984. ------------------
  1985. (1 row)
  1986. SELECT jsonb_path_match('1', '$', silent => false);
  1987. ERROR: single boolean result is expected
  1988. SELECT jsonb_path_match('"a"', '$', silent => false);
  1989. ERROR: single boolean result is expected
  1990. SELECT jsonb_path_match('{}', '$', silent => false);
  1991. ERROR: single boolean result is expected
  1992. SELECT jsonb_path_match('[true]', '$', silent => false);
  1993. ERROR: single boolean result is expected
  1994. SELECT jsonb_path_match('{}', 'lax $.a', silent => false);
  1995. ERROR: single boolean result is expected
  1996. SELECT jsonb_path_match('{}', 'strict $.a', silent => false);
  1997. ERROR: JSON object does not contain key "a"
  1998. SELECT jsonb_path_match('{}', 'strict $.a', silent => true);
  1999. jsonb_path_match
  2000. ------------------
  2001. (1 row)
  2002. SELECT jsonb_path_match('[true, true]', '$[*]', silent => false);
  2003. ERROR: single boolean result is expected
  2004. SELECT jsonb '[{"a": 1}, {"a": 2}]' @@ '$[*].a > 1';
  2005. ?column?
  2006. ----------
  2007. t
  2008. (1 row)
  2009. SELECT jsonb '[{"a": 1}, {"a": 2}]' @@ '$[*].a > 2';
  2010. ?column?
  2011. ----------
  2012. f
  2013. (1 row)
  2014. SELECT jsonb_path_match('[{"a": 1}, {"a": 2}]', '$[*].a > 1');
  2015. jsonb_path_match
  2016. ------------------
  2017. t
  2018. (1 row)
  2019. -- test string comparison (Unicode codepoint collation)
  2020. WITH str(j, num) AS
  2021. (
  2022. SELECT jsonb_build_object('s', s), num
  2023. FROM unnest('{"", "a", "ab", "abc", "abcd", "b", "A", "AB", "ABC", "ABc", "ABcD", "B"}'::text[]) WITH ORDINALITY AS a(s, num)
  2024. )
  2025. SELECT
  2026. s1.j, s2.j,
  2027. jsonb_path_query_first(s1.j, '$.s < $s', vars => s2.j) lt,
  2028. jsonb_path_query_first(s1.j, '$.s <= $s', vars => s2.j) le,
  2029. jsonb_path_query_first(s1.j, '$.s == $s', vars => s2.j) eq,
  2030. jsonb_path_query_first(s1.j, '$.s >= $s', vars => s2.j) ge,
  2031. jsonb_path_query_first(s1.j, '$.s > $s', vars => s2.j) gt
  2032. FROM str s1, str s2
  2033. ORDER BY s1.num, s2.num;
  2034. j | j | lt | le | eq | ge | gt
  2035. ---------------+---------------+-------+-------+-------+-------+-------
  2036. {"s": ""} | {"s": ""} | false | true | true | true | false
  2037. {"s": ""} | {"s": "a"} | true | true | false | false | false
  2038. {"s": ""} | {"s": "ab"} | true | true | false | false | false
  2039. {"s": ""} | {"s": "abc"} | true | true | false | false | false
  2040. {"s": ""} | {"s": "abcd"} | true | true | false | false | false
  2041. {"s": ""} | {"s": "b"} | true | true | false | false | false
  2042. {"s": ""} | {"s": "A"} | true | true | false | false | false
  2043. {"s": ""} | {"s": "AB"} | true | true | false | false | false
  2044. {"s": ""} | {"s": "ABC"} | true | true | false | false | false
  2045. {"s": ""} | {"s": "ABc"} | true | true | false | false | false
  2046. {"s": ""} | {"s": "ABcD"} | true | true | false | false | false
  2047. {"s": ""} | {"s": "B"} | true | true | false | false | false
  2048. {"s": "a"} | {"s": ""} | false | false | false | true | true
  2049. {"s": "a"} | {"s": "a"} | false | true | true | true | false
  2050. {"s": "a"} | {"s": "ab"} | true | true | false | false | false
  2051. {"s": "a"} | {"s": "abc"} | true | true | false | false | false
  2052. {"s": "a"} | {"s": "abcd"} | true | true | false | false | false
  2053. {"s": "a"} | {"s": "b"} | true | true | false | false | false
  2054. {"s": "a"} | {"s": "A"} | false | false | false | true | true
  2055. {"s": "a"} | {"s": "AB"} | false | false | false | true | true
  2056. {"s": "a"} | {"s": "ABC"} | false | false | false | true | true
  2057. {"s": "a"} | {"s": "ABc"} | false | false | false | true | true
  2058. {"s": "a"} | {"s": "ABcD"} | false | false | false | true | true
  2059. {"s": "a"} | {"s": "B"} | false | false | false | true | true
  2060. {"s": "ab"} | {"s": ""} | false | false | false | true | true
  2061. {"s": "ab"} | {"s": "a"} | false | false | false | true | true
  2062. {"s": "ab"} | {"s": "ab"} | false | true | true | true | false
  2063. {"s": "ab"} | {"s": "abc"} | true | true | false | false | false
  2064. {"s": "ab"} | {"s": "abcd"} | true | true | false | false | false
  2065. {"s": "ab"} | {"s": "b"} | true | true | false | false | false
  2066. {"s": "ab"} | {"s": "A"} | false | false | false | true | true
  2067. {"s": "ab"} | {"s": "AB"} | false | false | false | true | true
  2068. {"s": "ab"} | {"s": "ABC"} | false | false | false | true | true
  2069. {"s": "ab"} | {"s": "ABc"} | false | false | false | true | true
  2070. {"s": "ab"} | {"s": "ABcD"} | false | false | false | true | true
  2071. {"s": "ab"} | {"s": "B"} | false | false | false | true | true
  2072. {"s": "abc"} | {"s": ""} | false | false | false | true | true
  2073. {"s": "abc"} | {"s": "a"} | false | false | false | true | true
  2074. {"s": "abc"} | {"s": "ab"} | false | false | false | true | true
  2075. {"s": "abc"} | {"s": "abc"} | false | true | true | true | false
  2076. {"s": "abc"} | {"s": "abcd"} | true | true | false | false | false
  2077. {"s": "abc"} | {"s": "b"} | true | true | false | false | false
  2078. {"s": "abc"} | {"s": "A"} | false | false | false | true | true
  2079. {"s": "abc"} | {"s": "AB"} | false | false | false | true | true
  2080. {"s": "abc"} | {"s": "ABC"} | false | false | false | true | true
  2081. {"s": "abc"} | {"s": "ABc"} | false | false | false | true | true
  2082. {"s": "abc"} | {"s": "ABcD"} | false | false | false | true | true
  2083. {"s": "abc"} | {"s": "B"} | false | false | false | true | true
  2084. {"s": "abcd"} | {"s": ""} | false | false | false | true | true
  2085. {"s": "abcd"} | {"s": "a"} | false | false | false | true | true
  2086. {"s": "abcd"} | {"s": "ab"} | false | false | false | true | true
  2087. {"s": "abcd"} | {"s": "abc"} | false | false | false | true | true
  2088. {"s": "abcd"} | {"s": "abcd"} | false | true | true | true | false
  2089. {"s": "abcd"} | {"s": "b"} | true | true | false | false | false
  2090. {"s": "abcd"} | {"s": "A"} | false | false | false | true | true
  2091. {"s": "abcd"} | {"s": "AB"} | false | false | false | true | true
  2092. {"s": "abcd"} | {"s": "ABC"} | false | false | false | true | true
  2093. {"s": "abcd"} | {"s": "ABc"} | false | false | false | true | true
  2094. {"s": "abcd"} | {"s": "ABcD"} | false | false | false | true | true
  2095. {"s": "abcd"} | {"s": "B"} | false | false | false | true | true
  2096. {"s": "b"} | {"s": ""} | false | false | false | true | true
  2097. {"s": "b"} | {"s": "a"} | false | false | false | true | true
  2098. {"s": "b"} | {"s": "ab"} | false | false | false | true | true
  2099. {"s": "b"} | {"s": "abc"} | false | false | false | true | true
  2100. {"s": "b"} | {"s": "abcd"} | false | false | false | true | true
  2101. {"s": "b"} | {"s": "b"} | false | true | true | true | false
  2102. {"s": "b"} | {"s": "A"} | false | false | false | true | true
  2103. {"s": "b"} | {"s": "AB"} | false | false | false | true | true
  2104. {"s": "b"} | {"s": "ABC"} | false | false | false | true | true
  2105. {"s": "b"} | {"s": "ABc"} | false | false | false | true | true
  2106. {"s": "b"} | {"s": "ABcD"} | false | false | false | true | true
  2107. {"s": "b"} | {"s": "B"} | false | false | false | true | true
  2108. {"s": "A"} | {"s": ""} | false | false | false | true | true
  2109. {"s": "A"} | {"s": "a"} | true | true | false | false | false
  2110. {"s": "A"} | {"s": "ab"} | true | true | false | false | false
  2111. {"s": "A"} | {"s": "abc"} | true | true | false | false | false
  2112. {"s": "A"} | {"s": "abcd"} | true | true | false | false | false
  2113. {"s": "A"} | {"s": "b"} | true | true | false | false | false
  2114. {"s": "A"} | {"s": "A"} | false | true | true | true | false
  2115. {"s": "A"} | {"s": "AB"} | true | true | false | false | false
  2116. {"s": "A"} | {"s": "ABC"} | true | true | false | false | false
  2117. {"s": "A"} | {"s": "ABc"} | true | true | false | false | false
  2118. {"s": "A"} | {"s": "ABcD"} | true | true | false | false | false
  2119. {"s": "A"} | {"s": "B"} | true | true | false | false | false
  2120. {"s": "AB"} | {"s": ""} | false | false | false | true | true
  2121. {"s": "AB"} | {"s": "a"} | true | true | false | false | false
  2122. {"s": "AB"} | {"s": "ab"} | true | true | false | false | false
  2123. {"s": "AB"} | {"s": "abc"} | true | true | false | false | false
  2124. {"s": "AB"} | {"s": "abcd"} | true | true | false | false | false
  2125. {"s": "AB"} | {"s": "b"} | true | true | false | false | false
  2126. {"s": "AB"} | {"s": "A"} | false | false | false | true | true
  2127. {"s": "AB"} | {"s": "AB"} | false | true | true | true | false
  2128. {"s": "AB"} | {"s": "ABC"} | true | true | false | false | false
  2129. {"s": "AB"} | {"s": "ABc"} | true | true | false | false | false
  2130. {"s": "AB"} | {"s": "ABcD"} | true | true | false | false | false
  2131. {"s": "AB"} | {"s": "B"} | true | true | false | false | false
  2132. {"s": "ABC"} | {"s": ""} | false | false | false | true | true
  2133. {"s": "ABC"} | {"s": "a"} | true | true | false | false | false
  2134. {"s": "ABC"} | {"s": "ab"} | true | true | false | false | false
  2135. {"s": "ABC"} | {"s": "abc"} | true | true | false | false | false
  2136. {"s": "ABC"} | {"s": "abcd"} | true | true | false | false | false
  2137. {"s": "ABC"} | {"s": "b"} | true | true | false | false | false
  2138. {"s": "ABC"} | {"s": "A"} | false | false | false | true | true
  2139. {"s": "ABC"} | {"s": "AB"} | false | false | false | true | true
  2140. {"s": "ABC"} | {"s": "ABC"} | false | true | true | true | false
  2141. {"s": "ABC"} | {"s": "ABc"} | true | true | false | false | false
  2142. {"s": "ABC"} | {"s": "ABcD"} | true | true | false | false | false
  2143. {"s": "ABC"} | {"s": "B"} | true | true | false | false | false
  2144. {"s": "ABc"} | {"s": ""} | false | false | false | true | true
  2145. {"s": "ABc"} | {"s": "a"} | true | true | false | false | false
  2146. {"s": "ABc"} | {"s": "ab"} | true | true | false | false | false
  2147. {"s": "ABc"} | {"s": "abc"} | true | true | false | false | false
  2148. {"s": "ABc"} | {"s": "abcd"} | true | true | false | false | false
  2149. {"s": "ABc"} | {"s": "b"} | true | true | false | false | false
  2150. {"s": "ABc"} | {"s": "A"} | false | false | false | true | true
  2151. {"s": "ABc"} | {"s": "AB"} | false | false | false | true | true
  2152. {"s": "ABc"} | {"s": "ABC"} | false | false | false | true | true
  2153. {"s": "ABc"} | {"s": "ABc"} | false | true | true | true | false
  2154. {"s": "ABc"} | {"s": "ABcD"} | true | true | false | false | false
  2155. {"s": "ABc"} | {"s": "B"} | true | true | false | false | false
  2156. {"s": "ABcD"} | {"s": ""} | false | false | false | true | true
  2157. {"s": "ABcD"} | {"s": "a"} | true | true | false | false | false
  2158. {"s": "ABcD"} | {"s": "ab"} | true | true | false | false | false
  2159. {"s": "ABcD"} | {"s": "abc"} | true | true | false | false | false
  2160. {"s": "ABcD"} | {"s": "abcd"} | true | true | false | false | false
  2161. {"s": "ABcD"} | {"s": "b"} | true | true | false | false | false
  2162. {"s": "ABcD"} | {"s": "A"} | false | false | false | true | true
  2163. {"s": "ABcD"} | {"s": "AB"} | false | false | false | true | true
  2164. {"s": "ABcD"} | {"s": "ABC"} | false | false | false | true | true
  2165. {"s": "ABcD"} | {"s": "ABc"} | false | false | false | true | true
  2166. {"s": "ABcD"} | {"s": "ABcD"} | false | true | true | true | false
  2167. {"s": "ABcD"} | {"s": "B"} | true | true | false | false | false
  2168. {"s": "B"} | {"s": ""} | false | false | false | true | true
  2169. {"s": "B"} | {"s": "a"} | true | true | false | false | false
  2170. {"s": "B"} | {"s": "ab"} | true | true | false | false | false
  2171. {"s": "B"} | {"s": "abc"} | true | true | false | false | false
  2172. {"s": "B"} | {"s": "abcd"} | true | true | false | false | false
  2173. {"s": "B"} | {"s": "b"} | true | true | false | false | false
  2174. {"s": "B"} | {"s": "A"} | false | false | false | true | true
  2175. {"s": "B"} | {"s": "AB"} | false | false | false | true | true
  2176. {"s": "B"} | {"s": "ABC"} | false | false | false | true | true
  2177. {"s": "B"} | {"s": "ABc"} | false | false | false | true | true
  2178. {"s": "B"} | {"s": "ABcD"} | false | false | false | true | true
  2179. {"s": "B"} | {"s": "B"} | false | true | true | true | false
  2180. (144 rows)