jsonb.out 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511
  1. -- Strings.
  2. SELECT '""'::jsonb; -- OK.
  3. jsonb
  4. -------
  5. ""
  6. (1 row)
  7. SELECT $$''$$::jsonb; -- ERROR, single quotes are not allowed
  8. ERROR: invalid input syntax for type json
  9. LINE 1: SELECT $$''$$::jsonb;
  10. ^
  11. DETAIL: Token "'" is invalid.
  12. CONTEXT: JSON data, line 1: '...
  13. SELECT '"abc"'::jsonb; -- OK
  14. jsonb
  15. -------
  16. "abc"
  17. (1 row)
  18. SELECT '"abc'::jsonb; -- ERROR, quotes not closed
  19. ERROR: invalid input syntax for type json
  20. LINE 1: SELECT '"abc'::jsonb;
  21. ^
  22. DETAIL: Token ""abc" is invalid.
  23. CONTEXT: JSON data, line 1: "abc
  24. SELECT '"abc
  25. def"'::jsonb; -- ERROR, unescaped newline in string constant
  26. ERROR: invalid input syntax for type json
  27. LINE 1: SELECT '"abc
  28. ^
  29. DETAIL: Character with value 0x0a must be escaped.
  30. CONTEXT: JSON data, line 1: "abc
  31. SELECT '"\n\"\\"'::jsonb; -- OK, legal escapes
  32. jsonb
  33. ----------
  34. "\n\"\\"
  35. (1 row)
  36. SELECT '"\v"'::jsonb; -- ERROR, not a valid JSON escape
  37. ERROR: invalid input syntax for type json
  38. LINE 1: SELECT '"\v"'::jsonb;
  39. ^
  40. DETAIL: Escape sequence "\v" is invalid.
  41. CONTEXT: JSON data, line 1: "\v...
  42. -- see json_encoding test for input with unicode escapes
  43. -- Numbers.
  44. SELECT '1'::jsonb; -- OK
  45. jsonb
  46. -------
  47. 1
  48. (1 row)
  49. SELECT '0'::jsonb; -- OK
  50. jsonb
  51. -------
  52. 0
  53. (1 row)
  54. SELECT '01'::jsonb; -- ERROR, not valid according to JSON spec
  55. ERROR: invalid input syntax for type json
  56. LINE 1: SELECT '01'::jsonb;
  57. ^
  58. DETAIL: Token "01" is invalid.
  59. CONTEXT: JSON data, line 1: 01
  60. SELECT '0.1'::jsonb; -- OK
  61. jsonb
  62. -------
  63. 0.1
  64. (1 row)
  65. SELECT '9223372036854775808'::jsonb; -- OK, even though it's too large for int8
  66. jsonb
  67. ---------------------
  68. 9223372036854775808
  69. (1 row)
  70. SELECT '1e100'::jsonb; -- OK
  71. jsonb
  72. -------------------------------------------------------------------------------------------------------
  73. 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  74. (1 row)
  75. SELECT '1.3e100'::jsonb; -- OK
  76. jsonb
  77. -------------------------------------------------------------------------------------------------------
  78. 13000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  79. (1 row)
  80. SELECT '1f2'::jsonb; -- ERROR
  81. ERROR: invalid input syntax for type json
  82. LINE 1: SELECT '1f2'::jsonb;
  83. ^
  84. DETAIL: Token "1f2" is invalid.
  85. CONTEXT: JSON data, line 1: 1f2
  86. SELECT '0.x1'::jsonb; -- ERROR
  87. ERROR: invalid input syntax for type json
  88. LINE 1: SELECT '0.x1'::jsonb;
  89. ^
  90. DETAIL: Token "0.x1" is invalid.
  91. CONTEXT: JSON data, line 1: 0.x1
  92. SELECT '1.3ex100'::jsonb; -- ERROR
  93. ERROR: invalid input syntax for type json
  94. LINE 1: SELECT '1.3ex100'::jsonb;
  95. ^
  96. DETAIL: Token "1.3ex100" is invalid.
  97. CONTEXT: JSON data, line 1: 1.3ex100
  98. -- Arrays.
  99. SELECT '[]'::jsonb; -- OK
  100. jsonb
  101. -------
  102. []
  103. (1 row)
  104. SELECT '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'::jsonb; -- OK
  105. jsonb
  106. ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  107. [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
  108. (1 row)
  109. SELECT '[1,2]'::jsonb; -- OK
  110. jsonb
  111. --------
  112. [1, 2]
  113. (1 row)
  114. SELECT '[1,2,]'::jsonb; -- ERROR, trailing comma
  115. ERROR: invalid input syntax for type json
  116. LINE 1: SELECT '[1,2,]'::jsonb;
  117. ^
  118. DETAIL: Expected JSON value, but found "]".
  119. CONTEXT: JSON data, line 1: [1,2,]
  120. SELECT '[1,2'::jsonb; -- ERROR, no closing bracket
  121. ERROR: invalid input syntax for type json
  122. LINE 1: SELECT '[1,2'::jsonb;
  123. ^
  124. DETAIL: The input string ended unexpectedly.
  125. CONTEXT: JSON data, line 1: [1,2
  126. SELECT '[1,[2]'::jsonb; -- ERROR, no closing bracket
  127. ERROR: invalid input syntax for type json
  128. LINE 1: SELECT '[1,[2]'::jsonb;
  129. ^
  130. DETAIL: The input string ended unexpectedly.
  131. CONTEXT: JSON data, line 1: [1,[2]
  132. -- Objects.
  133. SELECT '{}'::jsonb; -- OK
  134. jsonb
  135. -------
  136. {}
  137. (1 row)
  138. SELECT '{"abc"}'::jsonb; -- ERROR, no value
  139. ERROR: invalid input syntax for type json
  140. LINE 1: SELECT '{"abc"}'::jsonb;
  141. ^
  142. DETAIL: Expected ":", but found "}".
  143. CONTEXT: JSON data, line 1: {"abc"}
  144. SELECT '{"abc":1}'::jsonb; -- OK
  145. jsonb
  146. ------------
  147. {"abc": 1}
  148. (1 row)
  149. SELECT '{1:"abc"}'::jsonb; -- ERROR, keys must be strings
  150. ERROR: invalid input syntax for type json
  151. LINE 1: SELECT '{1:"abc"}'::jsonb;
  152. ^
  153. DETAIL: Expected string or "}", but found "1".
  154. CONTEXT: JSON data, line 1: {1...
  155. SELECT '{"abc",1}'::jsonb; -- ERROR, wrong separator
  156. ERROR: invalid input syntax for type json
  157. LINE 1: SELECT '{"abc",1}'::jsonb;
  158. ^
  159. DETAIL: Expected ":", but found ",".
  160. CONTEXT: JSON data, line 1: {"abc",...
  161. SELECT '{"abc"=1}'::jsonb; -- ERROR, totally wrong separator
  162. ERROR: invalid input syntax for type json
  163. LINE 1: SELECT '{"abc"=1}'::jsonb;
  164. ^
  165. DETAIL: Token "=" is invalid.
  166. CONTEXT: JSON data, line 1: {"abc"=...
  167. SELECT '{"abc"::1}'::jsonb; -- ERROR, another wrong separator
  168. ERROR: invalid input syntax for type json
  169. LINE 1: SELECT '{"abc"::1}'::jsonb;
  170. ^
  171. DETAIL: Expected JSON value, but found ":".
  172. CONTEXT: JSON data, line 1: {"abc"::...
  173. SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::jsonb; -- OK
  174. jsonb
  175. --------------------------------------------------------------------
  176. {"abc": 1, "def": 2, "ghi": [3, 4], "hij": {"klm": 5, "nop": [6]}}
  177. (1 row)
  178. SELECT '{"abc":1:2}'::jsonb; -- ERROR, colon in wrong spot
  179. ERROR: invalid input syntax for type json
  180. LINE 1: SELECT '{"abc":1:2}'::jsonb;
  181. ^
  182. DETAIL: Expected "," or "}", but found ":".
  183. CONTEXT: JSON data, line 1: {"abc":1:...
  184. SELECT '{"abc":1,3}'::jsonb; -- ERROR, no value
  185. ERROR: invalid input syntax for type json
  186. LINE 1: SELECT '{"abc":1,3}'::jsonb;
  187. ^
  188. DETAIL: Expected string, but found "3".
  189. CONTEXT: JSON data, line 1: {"abc":1,3...
  190. -- Recursion.
  191. SET max_stack_depth = '100kB';
  192. SELECT repeat('[', 10000)::jsonb;
  193. ERROR: stack depth limit exceeded
  194. HINT: Increase the configuration parameter "max_stack_depth" (currently 100kB), after ensuring the platform's stack depth limit is adequate.
  195. SELECT repeat('{"a":', 10000)::jsonb;
  196. ERROR: stack depth limit exceeded
  197. HINT: Increase the configuration parameter "max_stack_depth" (currently 100kB), after ensuring the platform's stack depth limit is adequate.
  198. RESET max_stack_depth;
  199. -- Miscellaneous stuff.
  200. SELECT 'true'::jsonb; -- OK
  201. jsonb
  202. -------
  203. true
  204. (1 row)
  205. SELECT 'false'::jsonb; -- OK
  206. jsonb
  207. -------
  208. false
  209. (1 row)
  210. SELECT 'null'::jsonb; -- OK
  211. jsonb
  212. -------
  213. null
  214. (1 row)
  215. SELECT ' true '::jsonb; -- OK, even with extra whitespace
  216. jsonb
  217. -------
  218. true
  219. (1 row)
  220. SELECT 'true false'::jsonb; -- ERROR, too many values
  221. ERROR: invalid input syntax for type json
  222. LINE 1: SELECT 'true false'::jsonb;
  223. ^
  224. DETAIL: Expected end of input, but found "false".
  225. CONTEXT: JSON data, line 1: true false
  226. SELECT 'true, false'::jsonb; -- ERROR, too many values
  227. ERROR: invalid input syntax for type json
  228. LINE 1: SELECT 'true, false'::jsonb;
  229. ^
  230. DETAIL: Expected end of input, but found ",".
  231. CONTEXT: JSON data, line 1: true,...
  232. SELECT 'truf'::jsonb; -- ERROR, not a keyword
  233. ERROR: invalid input syntax for type json
  234. LINE 1: SELECT 'truf'::jsonb;
  235. ^
  236. DETAIL: Token "truf" is invalid.
  237. CONTEXT: JSON data, line 1: truf
  238. SELECT 'trues'::jsonb; -- ERROR, not a keyword
  239. ERROR: invalid input syntax for type json
  240. LINE 1: SELECT 'trues'::jsonb;
  241. ^
  242. DETAIL: Token "trues" is invalid.
  243. CONTEXT: JSON data, line 1: trues
  244. SELECT ''::jsonb; -- ERROR, no value
  245. ERROR: invalid input syntax for type json
  246. LINE 1: SELECT ''::jsonb;
  247. ^
  248. DETAIL: The input string ended unexpectedly.
  249. CONTEXT: JSON data, line 1:
  250. SELECT ' '::jsonb; -- ERROR, no value
  251. ERROR: invalid input syntax for type json
  252. LINE 1: SELECT ' '::jsonb;
  253. ^
  254. DETAIL: The input string ended unexpectedly.
  255. CONTEXT: JSON data, line 1:
  256. -- Multi-line JSON input to check ERROR reporting
  257. SELECT '{
  258. "one": 1,
  259. "two":"two",
  260. "three":
  261. true}'::jsonb; -- OK
  262. jsonb
  263. -----------------------------------------
  264. {"one": 1, "two": "two", "three": true}
  265. (1 row)
  266. SELECT '{
  267. "one": 1,
  268. "two":,"two", -- ERROR extraneous comma before field "two"
  269. "three":
  270. true}'::jsonb;
  271. ERROR: invalid input syntax for type json
  272. LINE 1: SELECT '{
  273. ^
  274. DETAIL: Expected JSON value, but found ",".
  275. CONTEXT: JSON data, line 3: "two":,...
  276. SELECT '{
  277. "one": 1,
  278. "two":"two",
  279. "averyveryveryveryveryveryveryveryveryverylongfieldname":}'::jsonb;
  280. ERROR: invalid input syntax for type json
  281. LINE 1: SELECT '{
  282. ^
  283. DETAIL: Expected JSON value, but found "}".
  284. CONTEXT: JSON data, line 4: ...yveryveryveryveryveryveryveryverylongfieldname":}
  285. -- ERROR missing value for last field
  286. -- make sure jsonb is passed through json generators without being escaped
  287. SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']);
  288. array_to_json
  289. --------------------------
  290. [{"a": 1},{"b": [2, 3]}]
  291. (1 row)
  292. -- anyarray column
  293. CREATE TEMP TABLE rows AS
  294. SELECT x, 'txt' || x as y
  295. FROM generate_series(1,3) AS x;
  296. analyze rows;
  297. select attname, to_jsonb(histogram_bounds) histogram_bounds
  298. from pg_stats
  299. where tablename = 'rows' and
  300. schemaname = pg_my_temp_schema()::regnamespace::text
  301. order by 1;
  302. attname | histogram_bounds
  303. ---------+--------------------------
  304. x | [1, 2, 3]
  305. y | ["txt1", "txt2", "txt3"]
  306. (2 rows)
  307. -- to_jsonb, timestamps
  308. select to_jsonb(timestamp '2014-05-28 12:22:35.614298');
  309. to_jsonb
  310. ------------------------------
  311. "2014-05-28T12:22:35.614298"
  312. (1 row)
  313. BEGIN;
  314. SET LOCAL TIME ZONE 10.5;
  315. select to_jsonb(timestamptz '2014-05-28 12:22:35.614298-04');
  316. to_jsonb
  317. ------------------------------------
  318. "2014-05-29T02:52:35.614298+10:30"
  319. (1 row)
  320. SET LOCAL TIME ZONE -8;
  321. select to_jsonb(timestamptz '2014-05-28 12:22:35.614298-04');
  322. to_jsonb
  323. ------------------------------------
  324. "2014-05-28T08:22:35.614298-08:00"
  325. (1 row)
  326. COMMIT;
  327. select to_jsonb(date '2014-05-28');
  328. to_jsonb
  329. --------------
  330. "2014-05-28"
  331. (1 row)
  332. select to_jsonb(date 'Infinity');
  333. to_jsonb
  334. ------------
  335. "infinity"
  336. (1 row)
  337. select to_jsonb(date '-Infinity');
  338. to_jsonb
  339. -------------
  340. "-infinity"
  341. (1 row)
  342. select to_jsonb(timestamp 'Infinity');
  343. to_jsonb
  344. ------------
  345. "infinity"
  346. (1 row)
  347. select to_jsonb(timestamp '-Infinity');
  348. to_jsonb
  349. -------------
  350. "-infinity"
  351. (1 row)
  352. select to_jsonb(timestamptz 'Infinity');
  353. to_jsonb
  354. ------------
  355. "infinity"
  356. (1 row)
  357. select to_jsonb(timestamptz '-Infinity');
  358. to_jsonb
  359. -------------
  360. "-infinity"
  361. (1 row)
  362. --jsonb_agg
  363. SELECT jsonb_agg(q)
  364. FROM ( SELECT $$a$$ || x AS b, y AS c,
  365. ARRAY[ROW(x.*,ARRAY[1,2,3]),
  366. ROW(y.*,ARRAY[4,5,6])] AS z
  367. FROM generate_series(1,2) x,
  368. generate_series(4,5) y) q;
  369. jsonb_agg
  370. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  371. [{"b": "a1", "c": 4, "z": [{"f1": 1, "f2": [1, 2, 3]}, {"f1": 4, "f2": [4, 5, 6]}]}, {"b": "a1", "c": 5, "z": [{"f1": 1, "f2": [1, 2, 3]}, {"f1": 5, "f2": [4, 5, 6]}]}, {"b": "a2", "c": 4, "z": [{"f1": 2, "f2": [1, 2, 3]}, {"f1": 4, "f2": [4, 5, 6]}]}, {"b": "a2", "c": 5, "z": [{"f1": 2, "f2": [1, 2, 3]}, {"f1": 5, "f2": [4, 5, 6]}]}]
  372. (1 row)
  373. SELECT jsonb_agg(q ORDER BY x, y)
  374. FROM rows q;
  375. jsonb_agg
  376. -----------------------------------------------------------------------
  377. [{"x": 1, "y": "txt1"}, {"x": 2, "y": "txt2"}, {"x": 3, "y": "txt3"}]
  378. (1 row)
  379. UPDATE rows SET x = NULL WHERE x = 1;
  380. SELECT jsonb_agg(q ORDER BY x NULLS FIRST, y)
  381. FROM rows q;
  382. jsonb_agg
  383. --------------------------------------------------------------------------
  384. [{"x": null, "y": "txt1"}, {"x": 2, "y": "txt2"}, {"x": 3, "y": "txt3"}]
  385. (1 row)
  386. -- jsonb extraction functions
  387. CREATE TEMP TABLE test_jsonb (
  388. json_type text,
  389. test_json jsonb
  390. );
  391. INSERT INTO test_jsonb VALUES
  392. ('scalar','"a scalar"'),
  393. ('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
  394. ('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
  395. SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'scalar';
  396. ?column?
  397. ----------
  398. (1 row)
  399. SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'array';
  400. ?column?
  401. ----------
  402. (1 row)
  403. SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'object';
  404. ?column?
  405. ----------
  406. (1 row)
  407. SELECT test_json -> 'field2' FROM test_jsonb WHERE json_type = 'object';
  408. ?column?
  409. ----------
  410. "val2"
  411. (1 row)
  412. SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'scalar';
  413. ?column?
  414. ----------
  415. (1 row)
  416. SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'array';
  417. ?column?
  418. ----------
  419. (1 row)
  420. SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'object';
  421. ?column?
  422. ----------
  423. val2
  424. (1 row)
  425. SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'scalar';
  426. ?column?
  427. ----------
  428. (1 row)
  429. SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'array';
  430. ?column?
  431. ----------
  432. "two"
  433. (1 row)
  434. SELECT test_json -> 9 FROM test_jsonb WHERE json_type = 'array';
  435. ?column?
  436. ----------
  437. (1 row)
  438. SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'object';
  439. ?column?
  440. ----------
  441. (1 row)
  442. SELECT test_json ->> 6 FROM test_jsonb WHERE json_type = 'array';
  443. ?column?
  444. -----------
  445. [1, 2, 3]
  446. (1 row)
  447. SELECT test_json ->> 7 FROM test_jsonb WHERE json_type = 'array';
  448. ?column?
  449. -----------
  450. {"f1": 9}
  451. (1 row)
  452. SELECT test_json ->> 'field4' FROM test_jsonb WHERE json_type = 'object';
  453. ?column?
  454. ----------
  455. 4
  456. (1 row)
  457. SELECT test_json ->> 'field5' FROM test_jsonb WHERE json_type = 'object';
  458. ?column?
  459. -----------
  460. [1, 2, 3]
  461. (1 row)
  462. SELECT test_json ->> 'field6' FROM test_jsonb WHERE json_type = 'object';
  463. ?column?
  464. -----------
  465. {"f1": 9}
  466. (1 row)
  467. SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'scalar';
  468. ?column?
  469. ----------
  470. (1 row)
  471. SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'array';
  472. ?column?
  473. ----------
  474. two
  475. (1 row)
  476. SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'object';
  477. ?column?
  478. ----------
  479. (1 row)
  480. SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'scalar';
  481. ERROR: cannot call jsonb_object_keys on a scalar
  482. SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'array';
  483. ERROR: cannot call jsonb_object_keys on an array
  484. SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'object';
  485. jsonb_object_keys
  486. -------------------
  487. field1
  488. field2
  489. field3
  490. field4
  491. field5
  492. field6
  493. (6 rows)
  494. -- nulls
  495. SELECT (test_json->'field3') IS NULL AS expect_false FROM test_jsonb WHERE json_type = 'object';
  496. expect_false
  497. --------------
  498. f
  499. (1 row)
  500. SELECT (test_json->>'field3') IS NULL AS expect_true FROM test_jsonb WHERE json_type = 'object';
  501. expect_true
  502. -------------
  503. t
  504. (1 row)
  505. SELECT (test_json->3) IS NULL AS expect_false FROM test_jsonb WHERE json_type = 'array';
  506. expect_false
  507. --------------
  508. f
  509. (1 row)
  510. SELECT (test_json->>3) IS NULL AS expect_true FROM test_jsonb WHERE json_type = 'array';
  511. expect_true
  512. -------------
  513. t
  514. (1 row)
  515. -- corner cases
  516. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::text;
  517. ?column?
  518. ----------
  519. (1 row)
  520. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::int;
  521. ?column?
  522. ----------
  523. (1 row)
  524. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 1;
  525. ?column?
  526. ----------
  527. (1 row)
  528. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 'z';
  529. ?column?
  530. ----------
  531. (1 row)
  532. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> '';
  533. ?column?
  534. ----------
  535. (1 row)
  536. select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 1;
  537. ?column?
  538. -------------
  539. {"b": "cc"}
  540. (1 row)
  541. select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 3;
  542. ?column?
  543. ----------
  544. (1 row)
  545. select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 'z';
  546. ?column?
  547. ----------
  548. (1 row)
  549. select '{"a": "c", "b": null}'::jsonb -> 'b';
  550. ?column?
  551. ----------
  552. null
  553. (1 row)
  554. select '"foo"'::jsonb -> 1;
  555. ?column?
  556. ----------
  557. (1 row)
  558. select '"foo"'::jsonb -> 'z';
  559. ?column?
  560. ----------
  561. (1 row)
  562. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::text;
  563. ?column?
  564. ----------
  565. (1 row)
  566. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::int;
  567. ?column?
  568. ----------
  569. (1 row)
  570. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 1;
  571. ?column?
  572. ----------
  573. (1 row)
  574. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 'z';
  575. ?column?
  576. ----------
  577. (1 row)
  578. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> '';
  579. ?column?
  580. ----------
  581. (1 row)
  582. select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 1;
  583. ?column?
  584. -------------
  585. {"b": "cc"}
  586. (1 row)
  587. select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 3;
  588. ?column?
  589. ----------
  590. (1 row)
  591. select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 'z';
  592. ?column?
  593. ----------
  594. (1 row)
  595. select '{"a": "c", "b": null}'::jsonb ->> 'b';
  596. ?column?
  597. ----------
  598. (1 row)
  599. select '"foo"'::jsonb ->> 1;
  600. ?column?
  601. ----------
  602. (1 row)
  603. select '"foo"'::jsonb ->> 'z';
  604. ?column?
  605. ----------
  606. (1 row)
  607. -- equality and inequality
  608. SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb;
  609. ?column?
  610. ----------
  611. t
  612. (1 row)
  613. SELECT '{"x":"y"}'::jsonb = '{"x":"z"}'::jsonb;
  614. ?column?
  615. ----------
  616. f
  617. (1 row)
  618. SELECT '{"x":"y"}'::jsonb <> '{"x":"y"}'::jsonb;
  619. ?column?
  620. ----------
  621. f
  622. (1 row)
  623. SELECT '{"x":"y"}'::jsonb <> '{"x":"z"}'::jsonb;
  624. ?column?
  625. ----------
  626. t
  627. (1 row)
  628. -- containment
  629. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b"}');
  630. jsonb_contains
  631. ----------------
  632. t
  633. (1 row)
  634. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "c":null}');
  635. jsonb_contains
  636. ----------------
  637. t
  638. (1 row)
  639. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "g":null}');
  640. jsonb_contains
  641. ----------------
  642. f
  643. (1 row)
  644. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"g":null}');
  645. jsonb_contains
  646. ----------------
  647. f
  648. (1 row)
  649. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"c"}');
  650. jsonb_contains
  651. ----------------
  652. f
  653. (1 row)
  654. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b"}');
  655. jsonb_contains
  656. ----------------
  657. t
  658. (1 row)
  659. SELECT jsonb_contains('{"a":"b", "b":1, "c":null}', '{"a":"b", "c":"q"}');
  660. jsonb_contains
  661. ----------------
  662. f
  663. (1 row)
  664. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b"}';
  665. ?column?
  666. ----------
  667. t
  668. (1 row)
  669. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "c":null}';
  670. ?column?
  671. ----------
  672. t
  673. (1 row)
  674. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "g":null}';
  675. ?column?
  676. ----------
  677. f
  678. (1 row)
  679. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"g":null}';
  680. ?column?
  681. ----------
  682. f
  683. (1 row)
  684. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"c"}';
  685. ?column?
  686. ----------
  687. f
  688. (1 row)
  689. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b"}';
  690. ?column?
  691. ----------
  692. t
  693. (1 row)
  694. SELECT '{"a":"b", "b":1, "c":null}'::jsonb @> '{"a":"b", "c":"q"}';
  695. ?column?
  696. ----------
  697. f
  698. (1 row)
  699. SELECT '[1,2]'::jsonb @> '[1,2,2]'::jsonb;
  700. ?column?
  701. ----------
  702. t
  703. (1 row)
  704. SELECT '[1,1,2]'::jsonb @> '[1,2,2]'::jsonb;
  705. ?column?
  706. ----------
  707. t
  708. (1 row)
  709. SELECT '[[1,2]]'::jsonb @> '[[1,2,2]]'::jsonb;
  710. ?column?
  711. ----------
  712. t
  713. (1 row)
  714. SELECT '[1,2,2]'::jsonb <@ '[1,2]'::jsonb;
  715. ?column?
  716. ----------
  717. t
  718. (1 row)
  719. SELECT '[1,2,2]'::jsonb <@ '[1,1,2]'::jsonb;
  720. ?column?
  721. ----------
  722. t
  723. (1 row)
  724. SELECT '[[1,2,2]]'::jsonb <@ '[[1,2]]'::jsonb;
  725. ?column?
  726. ----------
  727. t
  728. (1 row)
  729. SELECT jsonb_contained('{"a":"b"}', '{"a":"b", "b":1, "c":null}');
  730. jsonb_contained
  731. -----------------
  732. t
  733. (1 row)
  734. SELECT jsonb_contained('{"a":"b", "c":null}', '{"a":"b", "b":1, "c":null}');
  735. jsonb_contained
  736. -----------------
  737. t
  738. (1 row)
  739. SELECT jsonb_contained('{"a":"b", "g":null}', '{"a":"b", "b":1, "c":null}');
  740. jsonb_contained
  741. -----------------
  742. f
  743. (1 row)
  744. SELECT jsonb_contained('{"g":null}', '{"a":"b", "b":1, "c":null}');
  745. jsonb_contained
  746. -----------------
  747. f
  748. (1 row)
  749. SELECT jsonb_contained('{"a":"c"}', '{"a":"b", "b":1, "c":null}');
  750. jsonb_contained
  751. -----------------
  752. f
  753. (1 row)
  754. SELECT jsonb_contained('{"a":"b"}', '{"a":"b", "b":1, "c":null}');
  755. jsonb_contained
  756. -----------------
  757. t
  758. (1 row)
  759. SELECT jsonb_contained('{"a":"b", "c":"q"}', '{"a":"b", "b":1, "c":null}');
  760. jsonb_contained
  761. -----------------
  762. f
  763. (1 row)
  764. SELECT '{"a":"b"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  765. ?column?
  766. ----------
  767. t
  768. (1 row)
  769. SELECT '{"a":"b", "c":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  770. ?column?
  771. ----------
  772. t
  773. (1 row)
  774. SELECT '{"a":"b", "g":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  775. ?column?
  776. ----------
  777. f
  778. (1 row)
  779. SELECT '{"g":null}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  780. ?column?
  781. ----------
  782. f
  783. (1 row)
  784. SELECT '{"a":"c"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  785. ?column?
  786. ----------
  787. f
  788. (1 row)
  789. SELECT '{"a":"b"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  790. ?column?
  791. ----------
  792. t
  793. (1 row)
  794. SELECT '{"a":"b", "c":"q"}'::jsonb <@ '{"a":"b", "b":1, "c":null}';
  795. ?column?
  796. ----------
  797. f
  798. (1 row)
  799. -- Raw scalar may contain another raw scalar, array may contain a raw scalar
  800. SELECT '[5]'::jsonb @> '[5]';
  801. ?column?
  802. ----------
  803. t
  804. (1 row)
  805. SELECT '5'::jsonb @> '5';
  806. ?column?
  807. ----------
  808. t
  809. (1 row)
  810. SELECT '[5]'::jsonb @> '5';
  811. ?column?
  812. ----------
  813. t
  814. (1 row)
  815. -- But a raw scalar cannot contain an array
  816. SELECT '5'::jsonb @> '[5]';
  817. ?column?
  818. ----------
  819. f
  820. (1 row)
  821. -- In general, one thing should always contain itself. Test array containment:
  822. SELECT '["9", ["7", "3"], 1]'::jsonb @> '["9", ["7", "3"], 1]'::jsonb;
  823. ?column?
  824. ----------
  825. t
  826. (1 row)
  827. SELECT '["9", ["7", "3"], ["1"]]'::jsonb @> '["9", ["7", "3"], ["1"]]'::jsonb;
  828. ?column?
  829. ----------
  830. t
  831. (1 row)
  832. -- array containment string matching confusion bug
  833. SELECT '{ "name": "Bob", "tags": [ "enim", "qui"]}'::jsonb @> '{"tags":["qu"]}';
  834. ?column?
  835. ----------
  836. f
  837. (1 row)
  838. -- array length
  839. SELECT jsonb_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
  840. jsonb_array_length
  841. --------------------
  842. 5
  843. (1 row)
  844. SELECT jsonb_array_length('[]');
  845. jsonb_array_length
  846. --------------------
  847. 0
  848. (1 row)
  849. SELECT jsonb_array_length('{"f1":1,"f2":[5,6]}');
  850. ERROR: cannot get array length of a non-array
  851. SELECT jsonb_array_length('4');
  852. ERROR: cannot get array length of a scalar
  853. -- each
  854. SELECT jsonb_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null}');
  855. jsonb_each
  856. --------------------
  857. (f1,"[1, 2, 3]")
  858. (f2,"{""f3"": 1}")
  859. (f4,null)
  860. (3 rows)
  861. SELECT jsonb_each('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
  862. q
  863. ------------------------------------------------------
  864. (1,"""first""")
  865. (a,"{""1"": ""first"", ""b"": ""c"", ""c"": ""b""}")
  866. (b,"[1, 2]")
  867. (c,"""cc""")
  868. (n,null)
  869. (5 rows)
  870. SELECT * FROM jsonb_each('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
  871. key | value
  872. -----+-----------
  873. f1 | [1, 2, 3]
  874. f2 | {"f3": 1}
  875. f4 | null
  876. f5 | 99
  877. f6 | "stringy"
  878. (5 rows)
  879. SELECT * FROM jsonb_each('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
  880. key | value
  881. -----+------------------------------------
  882. 1 | "first"
  883. a | {"1": "first", "b": "c", "c": "b"}
  884. b | [1, 2]
  885. c | "cc"
  886. n | null
  887. (5 rows)
  888. SELECT jsonb_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":"null"}');
  889. jsonb_each_text
  890. --------------------
  891. (f1,"[1, 2, 3]")
  892. (f2,"{""f3"": 1}")
  893. (f4,)
  894. (f5,null)
  895. (4 rows)
  896. SELECT jsonb_each_text('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
  897. q
  898. ------------------------------------------------------
  899. (1,first)
  900. (a,"{""1"": ""first"", ""b"": ""c"", ""c"": ""b""}")
  901. (b,"[1, 2]")
  902. (c,cc)
  903. (n,)
  904. (5 rows)
  905. SELECT * FROM jsonb_each_text('{"f1":[1,2,3],"f2":{"f3":1},"f4":null,"f5":99,"f6":"stringy"}') q;
  906. key | value
  907. -----+-----------
  908. f1 | [1, 2, 3]
  909. f2 | {"f3": 1}
  910. f4 |
  911. f5 | 99
  912. f6 | stringy
  913. (5 rows)
  914. SELECT * FROM jsonb_each_text('{"a":{"b":"c","c":"b","1":"first"},"b":[1,2],"c":"cc","1":"first","n":null}'::jsonb) AS q;
  915. key | value
  916. -----+------------------------------------
  917. 1 | first
  918. a | {"1": "first", "b": "c", "c": "b"}
  919. b | [1, 2]
  920. c | cc
  921. n |
  922. (5 rows)
  923. -- exists
  924. SELECT jsonb_exists('{"a":null, "b":"qq"}', 'a');
  925. jsonb_exists
  926. --------------
  927. t
  928. (1 row)
  929. SELECT jsonb_exists('{"a":null, "b":"qq"}', 'b');
  930. jsonb_exists
  931. --------------
  932. t
  933. (1 row)
  934. SELECT jsonb_exists('{"a":null, "b":"qq"}', 'c');
  935. jsonb_exists
  936. --------------
  937. f
  938. (1 row)
  939. SELECT jsonb_exists('{"a":"null", "b":"qq"}', 'a');
  940. jsonb_exists
  941. --------------
  942. t
  943. (1 row)
  944. SELECT jsonb '{"a":null, "b":"qq"}' ? 'a';
  945. ?column?
  946. ----------
  947. t
  948. (1 row)
  949. SELECT jsonb '{"a":null, "b":"qq"}' ? 'b';
  950. ?column?
  951. ----------
  952. t
  953. (1 row)
  954. SELECT jsonb '{"a":null, "b":"qq"}' ? 'c';
  955. ?column?
  956. ----------
  957. f
  958. (1 row)
  959. SELECT jsonb '{"a":"null", "b":"qq"}' ? 'a';
  960. ?column?
  961. ----------
  962. t
  963. (1 row)
  964. -- array exists - array elements should behave as keys
  965. SELECT count(*) from testjsonb WHERE j->'array' ? 'bar';
  966. count
  967. -------
  968. 3
  969. (1 row)
  970. -- type sensitive array exists - should return no rows (since "exists" only
  971. -- matches strings that are either object keys or array elements)
  972. SELECT count(*) from testjsonb WHERE j->'array' ? '5'::text;
  973. count
  974. -------
  975. 0
  976. (1 row)
  977. -- However, a raw scalar is *contained* within the array
  978. SELECT count(*) from testjsonb WHERE j->'array' @> '5'::jsonb;
  979. count
  980. -------
  981. 1
  982. (1 row)
  983. SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['a','b']);
  984. jsonb_exists_any
  985. ------------------
  986. t
  987. (1 row)
  988. SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['b','a']);
  989. jsonb_exists_any
  990. ------------------
  991. t
  992. (1 row)
  993. SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['c','a']);
  994. jsonb_exists_any
  995. ------------------
  996. t
  997. (1 row)
  998. SELECT jsonb_exists_any('{"a":null, "b":"qq"}', ARRAY['c','d']);
  999. jsonb_exists_any
  1000. ------------------
  1001. f
  1002. (1 row)
  1003. SELECT jsonb_exists_any('{"a":null, "b":"qq"}', '{}'::text[]);
  1004. jsonb_exists_any
  1005. ------------------
  1006. f
  1007. (1 row)
  1008. SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['a','b'];
  1009. ?column?
  1010. ----------
  1011. t
  1012. (1 row)
  1013. SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['b','a'];
  1014. ?column?
  1015. ----------
  1016. t
  1017. (1 row)
  1018. SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['c','a'];
  1019. ?column?
  1020. ----------
  1021. t
  1022. (1 row)
  1023. SELECT jsonb '{"a":null, "b":"qq"}' ?| ARRAY['c','d'];
  1024. ?column?
  1025. ----------
  1026. f
  1027. (1 row)
  1028. SELECT jsonb '{"a":null, "b":"qq"}' ?| '{}'::text[];
  1029. ?column?
  1030. ----------
  1031. f
  1032. (1 row)
  1033. SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['a','b']);
  1034. jsonb_exists_all
  1035. ------------------
  1036. t
  1037. (1 row)
  1038. SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['b','a']);
  1039. jsonb_exists_all
  1040. ------------------
  1041. t
  1042. (1 row)
  1043. SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['c','a']);
  1044. jsonb_exists_all
  1045. ------------------
  1046. f
  1047. (1 row)
  1048. SELECT jsonb_exists_all('{"a":null, "b":"qq"}', ARRAY['c','d']);
  1049. jsonb_exists_all
  1050. ------------------
  1051. f
  1052. (1 row)
  1053. SELECT jsonb_exists_all('{"a":null, "b":"qq"}', '{}'::text[]);
  1054. jsonb_exists_all
  1055. ------------------
  1056. t
  1057. (1 row)
  1058. SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['a','b'];
  1059. ?column?
  1060. ----------
  1061. t
  1062. (1 row)
  1063. SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['b','a'];
  1064. ?column?
  1065. ----------
  1066. t
  1067. (1 row)
  1068. SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['c','a'];
  1069. ?column?
  1070. ----------
  1071. f
  1072. (1 row)
  1073. SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['c','d'];
  1074. ?column?
  1075. ----------
  1076. f
  1077. (1 row)
  1078. SELECT jsonb '{"a":null, "b":"qq"}' ?& ARRAY['a','a', 'b', 'b', 'b'];
  1079. ?column?
  1080. ----------
  1081. t
  1082. (1 row)
  1083. SELECT jsonb '{"a":null, "b":"qq"}' ?& '{}'::text[];
  1084. ?column?
  1085. ----------
  1086. t
  1087. (1 row)
  1088. -- typeof
  1089. SELECT jsonb_typeof('{}') AS object;
  1090. object
  1091. --------
  1092. object
  1093. (1 row)
  1094. SELECT jsonb_typeof('{"c":3,"p":"o"}') AS object;
  1095. object
  1096. --------
  1097. object
  1098. (1 row)
  1099. SELECT jsonb_typeof('[]') AS array;
  1100. array
  1101. -------
  1102. array
  1103. (1 row)
  1104. SELECT jsonb_typeof('["a", 1]') AS array;
  1105. array
  1106. -------
  1107. array
  1108. (1 row)
  1109. SELECT jsonb_typeof('null') AS "null";
  1110. null
  1111. ------
  1112. null
  1113. (1 row)
  1114. SELECT jsonb_typeof('1') AS number;
  1115. number
  1116. --------
  1117. number
  1118. (1 row)
  1119. SELECT jsonb_typeof('-1') AS number;
  1120. number
  1121. --------
  1122. number
  1123. (1 row)
  1124. SELECT jsonb_typeof('1.0') AS number;
  1125. number
  1126. --------
  1127. number
  1128. (1 row)
  1129. SELECT jsonb_typeof('1e2') AS number;
  1130. number
  1131. --------
  1132. number
  1133. (1 row)
  1134. SELECT jsonb_typeof('-1.0') AS number;
  1135. number
  1136. --------
  1137. number
  1138. (1 row)
  1139. SELECT jsonb_typeof('true') AS boolean;
  1140. boolean
  1141. ---------
  1142. boolean
  1143. (1 row)
  1144. SELECT jsonb_typeof('false') AS boolean;
  1145. boolean
  1146. ---------
  1147. boolean
  1148. (1 row)
  1149. SELECT jsonb_typeof('"hello"') AS string;
  1150. string
  1151. --------
  1152. string
  1153. (1 row)
  1154. SELECT jsonb_typeof('"true"') AS string;
  1155. string
  1156. --------
  1157. string
  1158. (1 row)
  1159. SELECT jsonb_typeof('"1.0"') AS string;
  1160. string
  1161. --------
  1162. string
  1163. (1 row)
  1164. -- jsonb_build_array, jsonb_build_object, jsonb_object_agg
  1165. SELECT jsonb_build_array('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
  1166. jsonb_build_array
  1167. -------------------------------------------------------------------------
  1168. ["a", 1, "b", 1.2, "c", true, "d", null, "e", {"x": 3, "y": [1, 2, 3]}]
  1169. (1 row)
  1170. SELECT jsonb_build_array('a', NULL); -- ok
  1171. jsonb_build_array
  1172. -------------------
  1173. ["a", null]
  1174. (1 row)
  1175. SELECT jsonb_build_array(VARIADIC NULL::text[]); -- ok
  1176. jsonb_build_array
  1177. -------------------
  1178. (1 row)
  1179. SELECT jsonb_build_array(VARIADIC '{}'::text[]); -- ok
  1180. jsonb_build_array
  1181. -------------------
  1182. []
  1183. (1 row)
  1184. SELECT jsonb_build_array(VARIADIC '{a,b,c}'::text[]); -- ok
  1185. jsonb_build_array
  1186. -------------------
  1187. ["a", "b", "c"]
  1188. (1 row)
  1189. SELECT jsonb_build_array(VARIADIC ARRAY['a', NULL]::text[]); -- ok
  1190. jsonb_build_array
  1191. -------------------
  1192. ["a", null]
  1193. (1 row)
  1194. SELECT jsonb_build_array(VARIADIC '{1,2,3,4}'::text[]); -- ok
  1195. jsonb_build_array
  1196. ----------------------
  1197. ["1", "2", "3", "4"]
  1198. (1 row)
  1199. SELECT jsonb_build_array(VARIADIC '{1,2,3,4}'::int[]); -- ok
  1200. jsonb_build_array
  1201. -------------------
  1202. [1, 2, 3, 4]
  1203. (1 row)
  1204. SELECT jsonb_build_array(VARIADIC '{{1,4},{2,5},{3,6}}'::int[][]); -- ok
  1205. jsonb_build_array
  1206. --------------------
  1207. [1, 4, 2, 5, 3, 6]
  1208. (1 row)
  1209. SELECT jsonb_build_object('a',1,'b',1.2,'c',true,'d',null,'e',json '{"x": 3, "y": [1,2,3]}');
  1210. jsonb_build_object
  1211. -------------------------------------------------------------------------
  1212. {"a": 1, "b": 1.2, "c": true, "d": null, "e": {"x": 3, "y": [1, 2, 3]}}
  1213. (1 row)
  1214. SELECT jsonb_build_object(
  1215. 'a', jsonb_build_object('b',false,'c',99),
  1216. 'd', jsonb_build_object('e',array[9,8,7]::int[],
  1217. 'f', (select row_to_json(r) from ( select relkind, oid::regclass as name from pg_class where relname = 'pg_class') r)));
  1218. jsonb_build_object
  1219. ------------------------------------------------------------------------------------------------
  1220. {"a": {"b": false, "c": 99}, "d": {"e": [9, 8, 7], "f": {"name": "pg_class", "relkind": "r"}}}
  1221. (1 row)
  1222. SELECT jsonb_build_object('{a,b,c}'::text[]); -- error
  1223. ERROR: argument list must have even number of elements
  1224. HINT: The arguments of jsonb_build_object() must consist of alternating keys and values.
  1225. SELECT jsonb_build_object('{a,b,c}'::text[], '{d,e,f}'::text[]); -- error, key cannot be array
  1226. ERROR: key value must be scalar, not array, composite, or json
  1227. SELECT jsonb_build_object('a', 'b', 'c'); -- error
  1228. ERROR: argument list must have even number of elements
  1229. HINT: The arguments of jsonb_build_object() must consist of alternating keys and values.
  1230. SELECT jsonb_build_object(NULL, 'a'); -- error, key cannot be NULL
  1231. ERROR: argument 1: key must not be null
  1232. SELECT jsonb_build_object('a', NULL); -- ok
  1233. jsonb_build_object
  1234. --------------------
  1235. {"a": null}
  1236. (1 row)
  1237. SELECT jsonb_build_object(VARIADIC NULL::text[]); -- ok
  1238. jsonb_build_object
  1239. --------------------
  1240. (1 row)
  1241. SELECT jsonb_build_object(VARIADIC '{}'::text[]); -- ok
  1242. jsonb_build_object
  1243. --------------------
  1244. {}
  1245. (1 row)
  1246. SELECT jsonb_build_object(VARIADIC '{a,b,c}'::text[]); -- error
  1247. ERROR: argument list must have even number of elements
  1248. HINT: The arguments of jsonb_build_object() must consist of alternating keys and values.
  1249. SELECT jsonb_build_object(VARIADIC ARRAY['a', NULL]::text[]); -- ok
  1250. jsonb_build_object
  1251. --------------------
  1252. {"a": null}
  1253. (1 row)
  1254. SELECT jsonb_build_object(VARIADIC ARRAY[NULL, 'a']::text[]); -- error, key cannot be NULL
  1255. ERROR: argument 1: key must not be null
  1256. SELECT jsonb_build_object(VARIADIC '{1,2,3,4}'::text[]); -- ok
  1257. jsonb_build_object
  1258. ----------------------
  1259. {"1": "2", "3": "4"}
  1260. (1 row)
  1261. SELECT jsonb_build_object(VARIADIC '{1,2,3,4}'::int[]); -- ok
  1262. jsonb_build_object
  1263. --------------------
  1264. {"1": 2, "3": 4}
  1265. (1 row)
  1266. SELECT jsonb_build_object(VARIADIC '{{1,4},{2,5},{3,6}}'::int[][]); -- ok
  1267. jsonb_build_object
  1268. --------------------------
  1269. {"1": 4, "2": 5, "3": 6}
  1270. (1 row)
  1271. -- empty objects/arrays
  1272. SELECT jsonb_build_array();
  1273. jsonb_build_array
  1274. -------------------
  1275. []
  1276. (1 row)
  1277. SELECT jsonb_build_object();
  1278. jsonb_build_object
  1279. --------------------
  1280. {}
  1281. (1 row)
  1282. -- make sure keys are quoted
  1283. SELECT jsonb_build_object(1,2);
  1284. jsonb_build_object
  1285. --------------------
  1286. {"1": 2}
  1287. (1 row)
  1288. -- keys must be scalar and not null
  1289. SELECT jsonb_build_object(null,2);
  1290. ERROR: argument 1: key must not be null
  1291. SELECT jsonb_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
  1292. ERROR: key value must be scalar, not array, composite, or json
  1293. SELECT jsonb_build_object(json '{"a":1,"b":2}', 3);
  1294. ERROR: key value must be scalar, not array, composite, or json
  1295. SELECT jsonb_build_object('{1,2,3}'::int[], 3);
  1296. ERROR: key value must be scalar, not array, composite, or json
  1297. -- handling of NULL values
  1298. SELECT jsonb_object_agg(1, NULL::jsonb);
  1299. jsonb_object_agg
  1300. ------------------
  1301. {"1": null}
  1302. (1 row)
  1303. SELECT jsonb_object_agg(NULL, '{"a":1}');
  1304. ERROR: field name must not be null
  1305. CREATE TEMP TABLE foo (serial_num int, name text, type text);
  1306. INSERT INTO foo VALUES (847001,'t15','GE1043');
  1307. INSERT INTO foo VALUES (847002,'t16','GE1043');
  1308. INSERT INTO foo VALUES (847003,'sub-alpha','GESS90');
  1309. SELECT jsonb_build_object('turbines',jsonb_object_agg(serial_num,jsonb_build_object('name',name,'type',type)))
  1310. FROM foo;
  1311. jsonb_build_object
  1312. -------------------------------------------------------------------------------------------------------------------------------------------------------------
  1313. {"turbines": {"847001": {"name": "t15", "type": "GE1043"}, "847002": {"name": "t16", "type": "GE1043"}, "847003": {"name": "sub-alpha", "type": "GESS90"}}}
  1314. (1 row)
  1315. SELECT jsonb_object_agg(name, type) FROM foo;
  1316. jsonb_object_agg
  1317. -----------------------------------------------------------
  1318. {"t15": "GE1043", "t16": "GE1043", "sub-alpha": "GESS90"}
  1319. (1 row)
  1320. INSERT INTO foo VALUES (999999, NULL, 'bar');
  1321. SELECT jsonb_object_agg(name, type) FROM foo;
  1322. ERROR: field name must not be null
  1323. -- jsonb_object
  1324. -- empty object, one dimension
  1325. SELECT jsonb_object('{}');
  1326. jsonb_object
  1327. --------------
  1328. {}
  1329. (1 row)
  1330. -- empty object, two dimensions
  1331. SELECT jsonb_object('{}', '{}');
  1332. jsonb_object
  1333. --------------
  1334. {}
  1335. (1 row)
  1336. -- one dimension
  1337. SELECT jsonb_object('{a,1,b,2,3,NULL,"d e f","a b c"}');
  1338. jsonb_object
  1339. ---------------------------------------------------
  1340. {"3": null, "a": "1", "b": "2", "d e f": "a b c"}
  1341. (1 row)
  1342. -- same but with two dimensions
  1343. SELECT jsonb_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
  1344. jsonb_object
  1345. ---------------------------------------------------
  1346. {"3": null, "a": "1", "b": "2", "d e f": "a b c"}
  1347. (1 row)
  1348. -- odd number error
  1349. SELECT jsonb_object('{a,b,c}');
  1350. ERROR: array must have even number of elements
  1351. -- one column error
  1352. SELECT jsonb_object('{{a},{b}}');
  1353. ERROR: array must have two columns
  1354. -- too many columns error
  1355. SELECT jsonb_object('{{a,b,c},{b,c,d}}');
  1356. ERROR: array must have two columns
  1357. -- too many dimensions error
  1358. SELECT jsonb_object('{{{a,b},{c,d}},{{b,c},{d,e}}}');
  1359. ERROR: wrong number of array subscripts
  1360. --two argument form of jsonb_object
  1361. select jsonb_object('{a,b,c,"d e f"}','{1,2,3,"a b c"}');
  1362. jsonb_object
  1363. --------------------------------------------------
  1364. {"a": "1", "b": "2", "c": "3", "d e f": "a b c"}
  1365. (1 row)
  1366. -- too many dimensions
  1367. SELECT jsonb_object('{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}', '{{a,1},{b,2},{3,NULL},{"d e f","a b c"}}');
  1368. ERROR: wrong number of array subscripts
  1369. -- mismatched dimensions
  1370. select jsonb_object('{a,b,c,"d e f",g}','{1,2,3,"a b c"}');
  1371. ERROR: mismatched array dimensions
  1372. select jsonb_object('{a,b,c,"d e f"}','{1,2,3,"a b c",g}');
  1373. ERROR: mismatched array dimensions
  1374. -- null key error
  1375. select jsonb_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
  1376. ERROR: null value not allowed for object key
  1377. -- empty key is allowed
  1378. select jsonb_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
  1379. jsonb_object
  1380. -------------------------------------------------
  1381. {"": "3", "a": "1", "b": "2", "d e f": "a b c"}
  1382. (1 row)
  1383. -- extract_path, extract_path_as_text
  1384. SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
  1385. jsonb_extract_path
  1386. --------------------
  1387. "stringy"
  1388. (1 row)
  1389. SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
  1390. jsonb_extract_path
  1391. --------------------
  1392. {"f3": 1}
  1393. (1 row)
  1394. SELECT jsonb_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
  1395. jsonb_extract_path
  1396. --------------------
  1397. "f3"
  1398. (1 row)
  1399. SELECT jsonb_extract_path('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
  1400. jsonb_extract_path
  1401. --------------------
  1402. 1
  1403. (1 row)
  1404. SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f4','f6');
  1405. jsonb_extract_path_text
  1406. -------------------------
  1407. stringy
  1408. (1 row)
  1409. SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}','f2');
  1410. jsonb_extract_path_text
  1411. -------------------------
  1412. {"f3": 1}
  1413. (1 row)
  1414. SELECT jsonb_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',0::text);
  1415. jsonb_extract_path_text
  1416. -------------------------
  1417. f3
  1418. (1 row)
  1419. SELECT jsonb_extract_path_text('{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}','f2',1::text);
  1420. jsonb_extract_path_text
  1421. -------------------------
  1422. 1
  1423. (1 row)
  1424. -- extract_path nulls
  1425. SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') IS NULL AS expect_false;
  1426. expect_false
  1427. --------------
  1428. f
  1429. (1 row)
  1430. SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":{"f5":null,"f6":"stringy"}}','f4','f5') IS NULL AS expect_true;
  1431. expect_true
  1432. -------------
  1433. t
  1434. (1 row)
  1435. SELECT jsonb_extract_path('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') IS NULL AS expect_false;
  1436. expect_false
  1437. --------------
  1438. f
  1439. (1 row)
  1440. SELECT jsonb_extract_path_text('{"f2":{"f3":1},"f4":[0,1,2,null]}','f4','3') IS NULL AS expect_true;
  1441. expect_true
  1442. -------------
  1443. t
  1444. (1 row)
  1445. -- extract_path operators
  1446. SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f4','f6'];
  1447. ?column?
  1448. -----------
  1449. "stringy"
  1450. (1 row)
  1451. SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2'];
  1452. ?column?
  1453. -----------
  1454. {"f3": 1}
  1455. (1 row)
  1456. SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2','0'];
  1457. ?column?
  1458. ----------
  1459. "f3"
  1460. (1 row)
  1461. SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>array['f2','1'];
  1462. ?column?
  1463. ----------
  1464. 1
  1465. (1 row)
  1466. SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f4','f6'];
  1467. ?column?
  1468. ----------
  1469. stringy
  1470. (1 row)
  1471. SELECT '{"f2":{"f3":1},"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2'];
  1472. ?column?
  1473. -----------
  1474. {"f3": 1}
  1475. (1 row)
  1476. SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','0'];
  1477. ?column?
  1478. ----------
  1479. f3
  1480. (1 row)
  1481. SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','1'];
  1482. ?column?
  1483. ----------
  1484. 1
  1485. (1 row)
  1486. -- corner cases for same
  1487. select '{"a": {"b":{"c": "foo"}}}'::jsonb #> '{}';
  1488. ?column?
  1489. ----------------------------
  1490. {"a": {"b": {"c": "foo"}}}
  1491. (1 row)
  1492. select '[1,2,3]'::jsonb #> '{}';
  1493. ?column?
  1494. -----------
  1495. [1, 2, 3]
  1496. (1 row)
  1497. select '"foo"'::jsonb #> '{}';
  1498. ?column?
  1499. ----------
  1500. "foo"
  1501. (1 row)
  1502. select '42'::jsonb #> '{}';
  1503. ?column?
  1504. ----------
  1505. 42
  1506. (1 row)
  1507. select 'null'::jsonb #> '{}';
  1508. ?column?
  1509. ----------
  1510. null
  1511. (1 row)
  1512. select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a'];
  1513. ?column?
  1514. ---------------------
  1515. {"b": {"c": "foo"}}
  1516. (1 row)
  1517. select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', null];
  1518. ?column?
  1519. ----------
  1520. (1 row)
  1521. select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', ''];
  1522. ?column?
  1523. ----------
  1524. (1 row)
  1525. select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b'];
  1526. ?column?
  1527. --------------
  1528. {"c": "foo"}
  1529. (1 row)
  1530. select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b','c'];
  1531. ?column?
  1532. ----------
  1533. "foo"
  1534. (1 row)
  1535. select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b','c','d'];
  1536. ?column?
  1537. ----------
  1538. (1 row)
  1539. select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','z','c'];
  1540. ?column?
  1541. ----------
  1542. (1 row)
  1543. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #> array['a','1','b'];
  1544. ?column?
  1545. ----------
  1546. "cc"
  1547. (1 row)
  1548. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #> array['a','z','b'];
  1549. ?column?
  1550. ----------
  1551. (1 row)
  1552. select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['1','b'];
  1553. ?column?
  1554. ----------
  1555. "cc"
  1556. (1 row)
  1557. select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['z','b'];
  1558. ?column?
  1559. ----------
  1560. (1 row)
  1561. select '[{"b": "c"}, {"b": null}]'::jsonb #> array['1','b'];
  1562. ?column?
  1563. ----------
  1564. null
  1565. (1 row)
  1566. select '"foo"'::jsonb #> array['z'];
  1567. ?column?
  1568. ----------
  1569. (1 row)
  1570. select '42'::jsonb #> array['f2'];
  1571. ?column?
  1572. ----------
  1573. (1 row)
  1574. select '42'::jsonb #> array['0'];
  1575. ?column?
  1576. ----------
  1577. (1 row)
  1578. select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> '{}';
  1579. ?column?
  1580. ----------------------------
  1581. {"a": {"b": {"c": "foo"}}}
  1582. (1 row)
  1583. select '[1,2,3]'::jsonb #>> '{}';
  1584. ?column?
  1585. -----------
  1586. [1, 2, 3]
  1587. (1 row)
  1588. select '"foo"'::jsonb #>> '{}';
  1589. ?column?
  1590. ----------
  1591. foo
  1592. (1 row)
  1593. select '42'::jsonb #>> '{}';
  1594. ?column?
  1595. ----------
  1596. 42
  1597. (1 row)
  1598. select 'null'::jsonb #>> '{}';
  1599. ?column?
  1600. ----------
  1601. (1 row)
  1602. select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a'];
  1603. ?column?
  1604. ---------------------
  1605. {"b": {"c": "foo"}}
  1606. (1 row)
  1607. select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', null];
  1608. ?column?
  1609. ----------
  1610. (1 row)
  1611. select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', ''];
  1612. ?column?
  1613. ----------
  1614. (1 row)
  1615. select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b'];
  1616. ?column?
  1617. --------------
  1618. {"c": "foo"}
  1619. (1 row)
  1620. select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b','c'];
  1621. ?column?
  1622. ----------
  1623. foo
  1624. (1 row)
  1625. select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b','c','d'];
  1626. ?column?
  1627. ----------
  1628. (1 row)
  1629. select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','z','c'];
  1630. ?column?
  1631. ----------
  1632. (1 row)
  1633. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #>> array['a','1','b'];
  1634. ?column?
  1635. ----------
  1636. cc
  1637. (1 row)
  1638. select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #>> array['a','z','b'];
  1639. ?column?
  1640. ----------
  1641. (1 row)
  1642. select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['1','b'];
  1643. ?column?
  1644. ----------
  1645. cc
  1646. (1 row)
  1647. select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['z','b'];
  1648. ?column?
  1649. ----------
  1650. (1 row)
  1651. select '[{"b": "c"}, {"b": null}]'::jsonb #>> array['1','b'];
  1652. ?column?
  1653. ----------
  1654. (1 row)
  1655. select '"foo"'::jsonb #>> array['z'];
  1656. ?column?
  1657. ----------
  1658. (1 row)
  1659. select '42'::jsonb #>> array['f2'];
  1660. ?column?
  1661. ----------
  1662. (1 row)
  1663. select '42'::jsonb #>> array['0'];
  1664. ?column?
  1665. ----------
  1666. (1 row)
  1667. -- array_elements
  1668. SELECT jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]');
  1669. jsonb_array_elements
  1670. ----------------------------
  1671. 1
  1672. true
  1673. [1, [2, 3]]
  1674. null
  1675. {"f1": 1, "f2": [7, 8, 9]}
  1676. false
  1677. (6 rows)
  1678. SELECT * FROM jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]') q;
  1679. value
  1680. ----------------------------
  1681. 1
  1682. true
  1683. [1, [2, 3]]
  1684. null
  1685. {"f1": 1, "f2": [7, 8, 9]}
  1686. false
  1687. (6 rows)
  1688. SELECT jsonb_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]');
  1689. jsonb_array_elements_text
  1690. ----------------------------
  1691. 1
  1692. true
  1693. [1, [2, 3]]
  1694. {"f1": 1, "f2": [7, 8, 9]}
  1695. false
  1696. stringy
  1697. (7 rows)
  1698. SELECT * FROM jsonb_array_elements_text('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false,"stringy"]') q;
  1699. value
  1700. ----------------------------
  1701. 1
  1702. true
  1703. [1, [2, 3]]
  1704. {"f1": 1, "f2": [7, 8, 9]}
  1705. false
  1706. stringy
  1707. (7 rows)
  1708. -- populate_record
  1709. CREATE TYPE jbpop AS (a text, b int, c timestamp);
  1710. CREATE DOMAIN jsb_int_not_null AS int NOT NULL;
  1711. CREATE DOMAIN jsb_int_array_1d AS int[] CHECK(array_length(VALUE, 1) = 3);
  1712. CREATE DOMAIN jsb_int_array_2d AS int[][] CHECK(array_length(VALUE, 2) = 3);
  1713. create type jb_unordered_pair as (x int, y int);
  1714. create domain jb_ordered_pair as jb_unordered_pair check((value).x <= (value).y);
  1715. CREATE TYPE jsbrec AS (
  1716. i int,
  1717. ia _int4,
  1718. ia1 int[],
  1719. ia2 int[][],
  1720. ia3 int[][][],
  1721. ia1d jsb_int_array_1d,
  1722. ia2d jsb_int_array_2d,
  1723. t text,
  1724. ta text[],
  1725. c char(10),
  1726. ca char(10)[],
  1727. ts timestamp,
  1728. js json,
  1729. jsb jsonb,
  1730. jsa json[],
  1731. rec jbpop,
  1732. reca jbpop[]
  1733. );
  1734. CREATE TYPE jsbrec_i_not_null AS (
  1735. i jsb_int_not_null
  1736. );
  1737. SELECT * FROM jsonb_populate_record(NULL::jbpop,'{"a":"blurfl","x":43.2}') q;
  1738. a | b | c
  1739. --------+---+---
  1740. blurfl | |
  1741. (1 row)
  1742. SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":"blurfl","x":43.2}') q;
  1743. a | b | c
  1744. --------+---+--------------------------
  1745. blurfl | 3 | Mon Dec 31 15:30:56 2012
  1746. (1 row)
  1747. SELECT * FROM jsonb_populate_record(NULL::jbpop,'{"a":"blurfl","x":43.2}') q;
  1748. a | b | c
  1749. --------+---+---
  1750. blurfl | |
  1751. (1 row)
  1752. SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":"blurfl","x":43.2}') q;
  1753. a | b | c
  1754. --------+---+--------------------------
  1755. blurfl | 3 | Mon Dec 31 15:30:56 2012
  1756. (1 row)
  1757. SELECT * FROM jsonb_populate_record(NULL::jbpop,'{"a":[100,200,false],"x":43.2}') q;
  1758. a | b | c
  1759. -------------------+---+---
  1760. [100, 200, false] | |
  1761. (1 row)
  1762. SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"a":[100,200,false],"x":43.2}') q;
  1763. a | b | c
  1764. -------------------+---+--------------------------
  1765. [100, 200, false] | 3 | Mon Dec 31 15:30:56 2012
  1766. (1 row)
  1767. SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop,'{"c":[100,200,false],"x":43.2}') q;
  1768. ERROR: invalid input syntax for type timestamp: "[100, 200, false]"
  1769. SELECT * FROM jsonb_populate_record(row('x',3,'2012-12-31 15:30:56')::jbpop, '{}') q;
  1770. a | b | c
  1771. ---+---+--------------------------
  1772. x | 3 | Mon Dec 31 15:30:56 2012
  1773. (1 row)
  1774. SELECT i FROM jsonb_populate_record(NULL::jsbrec_i_not_null, '{"x": 43.2}') q;
  1775. ERROR: domain jsb_int_not_null does not allow null values
  1776. SELECT i FROM jsonb_populate_record(NULL::jsbrec_i_not_null, '{"i": null}') q;
  1777. ERROR: domain jsb_int_not_null does not allow null values
  1778. SELECT i FROM jsonb_populate_record(NULL::jsbrec_i_not_null, '{"i": 12345}') q;
  1779. i
  1780. -------
  1781. 12345
  1782. (1 row)
  1783. SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": null}') q;
  1784. ia
  1785. ----
  1786. (1 row)
  1787. SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": 123}') q;
  1788. ERROR: expected JSON array
  1789. HINT: See the value of key "ia".
  1790. SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": [1, "2", null, 4]}') q;
  1791. ia
  1792. --------------
  1793. {1,2,NULL,4}
  1794. (1 row)
  1795. SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": [[1, 2], [3, 4]]}') q;
  1796. ia
  1797. ---------------
  1798. {{1,2},{3,4}}
  1799. (1 row)
  1800. SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": [[1], 2]}') q;
  1801. ERROR: expected JSON array
  1802. HINT: See the array element [1] of key "ia".
  1803. SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": [[1], [2, 3]]}') q;
  1804. ERROR: malformed JSON array
  1805. DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
  1806. SELECT ia FROM jsonb_populate_record(NULL::jsbrec, '{"ia": "{1,2,3}"}') q;
  1807. ia
  1808. ---------
  1809. {1,2,3}
  1810. (1 row)
  1811. SELECT ia1 FROM jsonb_populate_record(NULL::jsbrec, '{"ia1": null}') q;
  1812. ia1
  1813. -----
  1814. (1 row)
  1815. SELECT ia1 FROM jsonb_populate_record(NULL::jsbrec, '{"ia1": 123}') q;
  1816. ERROR: expected JSON array
  1817. HINT: See the value of key "ia1".
  1818. SELECT ia1 FROM jsonb_populate_record(NULL::jsbrec, '{"ia1": [1, "2", null, 4]}') q;
  1819. ia1
  1820. --------------
  1821. {1,2,NULL,4}
  1822. (1 row)
  1823. SELECT ia1 FROM jsonb_populate_record(NULL::jsbrec, '{"ia1": [[1, 2, 3]]}') q;
  1824. ia1
  1825. -----------
  1826. {{1,2,3}}
  1827. (1 row)
  1828. SELECT ia1d FROM jsonb_populate_record(NULL::jsbrec, '{"ia1d": null}') q;
  1829. ia1d
  1830. ------
  1831. (1 row)
  1832. SELECT ia1d FROM jsonb_populate_record(NULL::jsbrec, '{"ia1d": 123}') q;
  1833. ERROR: expected JSON array
  1834. HINT: See the value of key "ia1d".
  1835. SELECT ia1d FROM jsonb_populate_record(NULL::jsbrec, '{"ia1d": [1, "2", null, 4]}') q;
  1836. ERROR: value for domain jsb_int_array_1d violates check constraint "jsb_int_array_1d_check"
  1837. SELECT ia1d FROM jsonb_populate_record(NULL::jsbrec, '{"ia1d": [1, "2", null]}') q;
  1838. ia1d
  1839. ------------
  1840. {1,2,NULL}
  1841. (1 row)
  1842. SELECT ia2 FROM jsonb_populate_record(NULL::jsbrec, '{"ia2": [1, "2", null, 4]}') q;
  1843. ia2
  1844. --------------
  1845. {1,2,NULL,4}
  1846. (1 row)
  1847. SELECT ia2 FROM jsonb_populate_record(NULL::jsbrec, '{"ia2": [[1, 2], [null, 4]]}') q;
  1848. ia2
  1849. ------------------
  1850. {{1,2},{NULL,4}}
  1851. (1 row)
  1852. SELECT ia2 FROM jsonb_populate_record(NULL::jsbrec, '{"ia2": [[], []]}') q;
  1853. ia2
  1854. -----
  1855. {}
  1856. (1 row)
  1857. SELECT ia2 FROM jsonb_populate_record(NULL::jsbrec, '{"ia2": [[1, 2], [3]]}') q;
  1858. ERROR: malformed JSON array
  1859. DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
  1860. SELECT ia2 FROM jsonb_populate_record(NULL::jsbrec, '{"ia2": [[1, 2], 3, 4]}') q;
  1861. ERROR: expected JSON array
  1862. HINT: See the array element [1] of key "ia2".
  1863. SELECT ia2d FROM jsonb_populate_record(NULL::jsbrec, '{"ia2d": [[1, "2"], [null, 4]]}') q;
  1864. ERROR: value for domain jsb_int_array_2d violates check constraint "jsb_int_array_2d_check"
  1865. SELECT ia2d FROM jsonb_populate_record(NULL::jsbrec, '{"ia2d": [[1, "2", 3], [null, 5, 6]]}') q;
  1866. ia2d
  1867. ----------------------
  1868. {{1,2,3},{NULL,5,6}}
  1869. (1 row)
  1870. SELECT ia3 FROM jsonb_populate_record(NULL::jsbrec, '{"ia3": [1, "2", null, 4]}') q;
  1871. ia3
  1872. --------------
  1873. {1,2,NULL,4}
  1874. (1 row)
  1875. SELECT ia3 FROM jsonb_populate_record(NULL::jsbrec, '{"ia3": [[1, 2], [null, 4]]}') q;
  1876. ia3
  1877. ------------------
  1878. {{1,2},{NULL,4}}
  1879. (1 row)
  1880. SELECT ia3 FROM jsonb_populate_record(NULL::jsbrec, '{"ia3": [ [[], []], [[], []], [[], []] ]}') q;
  1881. ia3
  1882. -----
  1883. {}
  1884. (1 row)
  1885. SELECT ia3 FROM jsonb_populate_record(NULL::jsbrec, '{"ia3": [ [[1, 2]], [[3, 4]] ]}') q;
  1886. ia3
  1887. -------------------
  1888. {{{1,2}},{{3,4}}}
  1889. (1 row)
  1890. SELECT ia3 FROM jsonb_populate_record(NULL::jsbrec, '{"ia3": [ [[1, 2], [3, 4]], [[5, 6], [7, 8]] ]}') q;
  1891. ia3
  1892. -------------------------------
  1893. {{{1,2},{3,4}},{{5,6},{7,8}}}
  1894. (1 row)
  1895. SELECT ia3 FROM jsonb_populate_record(NULL::jsbrec, '{"ia3": [ [[1, 2], [3, 4]], [[5, 6], [7, 8], [9, 10]] ]}') q;
  1896. ERROR: malformed JSON array
  1897. DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
  1898. SELECT ta FROM jsonb_populate_record(NULL::jsbrec, '{"ta": null}') q;
  1899. ta
  1900. ----
  1901. (1 row)
  1902. SELECT ta FROM jsonb_populate_record(NULL::jsbrec, '{"ta": 123}') q;
  1903. ERROR: expected JSON array
  1904. HINT: See the value of key "ta".
  1905. SELECT ta FROM jsonb_populate_record(NULL::jsbrec, '{"ta": [1, "2", null, 4]}') q;
  1906. ta
  1907. --------------
  1908. {1,2,NULL,4}
  1909. (1 row)
  1910. SELECT ta FROM jsonb_populate_record(NULL::jsbrec, '{"ta": [[1, 2, 3], {"k": "v"}]}') q;
  1911. ERROR: expected JSON array
  1912. HINT: See the array element [1] of key "ta".
  1913. SELECT c FROM jsonb_populate_record(NULL::jsbrec, '{"c": null}') q;
  1914. c
  1915. ---
  1916. (1 row)
  1917. SELECT c FROM jsonb_populate_record(NULL::jsbrec, '{"c": "aaa"}') q;
  1918. c
  1919. ------------
  1920. aaa
  1921. (1 row)
  1922. SELECT c FROM jsonb_populate_record(NULL::jsbrec, '{"c": "aaaaaaaaaa"}') q;
  1923. c
  1924. ------------
  1925. aaaaaaaaaa
  1926. (1 row)
  1927. SELECT c FROM jsonb_populate_record(NULL::jsbrec, '{"c": "aaaaaaaaaaaaa"}') q;
  1928. ERROR: value too long for type character(10)
  1929. SELECT ca FROM jsonb_populate_record(NULL::jsbrec, '{"ca": null}') q;
  1930. ca
  1931. ----
  1932. (1 row)
  1933. SELECT ca FROM jsonb_populate_record(NULL::jsbrec, '{"ca": 123}') q;
  1934. ERROR: expected JSON array
  1935. HINT: See the value of key "ca".
  1936. SELECT ca FROM jsonb_populate_record(NULL::jsbrec, '{"ca": [1, "2", null, 4]}') q;
  1937. ca
  1938. -----------------------------------------------
  1939. {"1 ","2 ",NULL,"4 "}
  1940. (1 row)
  1941. SELECT ca FROM jsonb_populate_record(NULL::jsbrec, '{"ca": ["aaaaaaaaaaaaaaaa"]}') q;
  1942. ERROR: value too long for type character(10)
  1943. SELECT ca FROM jsonb_populate_record(NULL::jsbrec, '{"ca": [[1, 2, 3], {"k": "v"}]}') q;
  1944. ERROR: expected JSON array
  1945. HINT: See the array element [1] of key "ca".
  1946. SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": null}') q;
  1947. js
  1948. ----
  1949. (1 row)
  1950. SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": true}') q;
  1951. js
  1952. ------
  1953. true
  1954. (1 row)
  1955. SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": 123.45}') q;
  1956. js
  1957. --------
  1958. 123.45
  1959. (1 row)
  1960. SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": "123.45"}') q;
  1961. js
  1962. ----------
  1963. "123.45"
  1964. (1 row)
  1965. SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": "abc"}') q;
  1966. js
  1967. -------
  1968. "abc"
  1969. (1 row)
  1970. SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": [123, "123", null, {"key": "value"}]}') q;
  1971. js
  1972. --------------------------------------
  1973. [123, "123", null, {"key": "value"}]
  1974. (1 row)
  1975. SELECT js FROM jsonb_populate_record(NULL::jsbrec, '{"js": {"a": "bbb", "b": null, "c": 123.45}}') q;
  1976. js
  1977. --------------------------------------
  1978. {"a": "bbb", "b": null, "c": 123.45}
  1979. (1 row)
  1980. SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": null}') q;
  1981. jsb
  1982. -----
  1983. (1 row)
  1984. SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": true}') q;
  1985. jsb
  1986. ------
  1987. true
  1988. (1 row)
  1989. SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": 123.45}') q;
  1990. jsb
  1991. --------
  1992. 123.45
  1993. (1 row)
  1994. SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": "123.45"}') q;
  1995. jsb
  1996. ----------
  1997. "123.45"
  1998. (1 row)
  1999. SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": "abc"}') q;
  2000. jsb
  2001. -------
  2002. "abc"
  2003. (1 row)
  2004. SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": [123, "123", null, {"key": "value"}]}') q;
  2005. jsb
  2006. --------------------------------------
  2007. [123, "123", null, {"key": "value"}]
  2008. (1 row)
  2009. SELECT jsb FROM jsonb_populate_record(NULL::jsbrec, '{"jsb": {"a": "bbb", "b": null, "c": 123.45}}') q;
  2010. jsb
  2011. --------------------------------------
  2012. {"a": "bbb", "b": null, "c": 123.45}
  2013. (1 row)
  2014. SELECT jsa FROM jsonb_populate_record(NULL::jsbrec, '{"jsa": null}') q;
  2015. jsa
  2016. -----
  2017. (1 row)
  2018. SELECT jsa FROM jsonb_populate_record(NULL::jsbrec, '{"jsa": 123}') q;
  2019. ERROR: expected JSON array
  2020. HINT: See the value of key "jsa".
  2021. SELECT jsa FROM jsonb_populate_record(NULL::jsbrec, '{"jsa": [1, "2", null, 4]}') q;
  2022. jsa
  2023. --------------------
  2024. {1,"\"2\"",NULL,4}
  2025. (1 row)
  2026. SELECT jsa FROM jsonb_populate_record(NULL::jsbrec, '{"jsa": ["aaa", null, [1, 2, "3", {}], { "k" : "v" }]}') q;
  2027. jsa
  2028. -------------------------------------------------------
  2029. {"\"aaa\"",NULL,"[1, 2, \"3\", {}]","{\"k\": \"v\"}"}
  2030. (1 row)
  2031. SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": 123}') q;
  2032. ERROR: cannot call populate_composite on a scalar
  2033. SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": [1, 2]}') q;
  2034. ERROR: cannot call populate_composite on an array
  2035. SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}') q;
  2036. rec
  2037. -----------------------------------
  2038. (abc,,"Thu Jan 02 00:00:00 2003")
  2039. (1 row)
  2040. SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": "(abc,42,01.02.2003)"}') q;
  2041. rec
  2042. -------------------------------------
  2043. (abc,42,"Thu Jan 02 00:00:00 2003")
  2044. (1 row)
  2045. SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": 123}') q;
  2046. ERROR: expected JSON array
  2047. HINT: See the value of key "reca".
  2048. SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": [1, 2]}') q;
  2049. ERROR: cannot call populate_composite on a scalar
  2050. SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]}') q;
  2051. reca
  2052. --------------------------------------------------------
  2053. {"(abc,456,)",NULL,"(,,\"Thu Jan 02 00:00:00 2003\")"}
  2054. (1 row)
  2055. SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": ["(abc,42,01.02.2003)"]}') q;
  2056. reca
  2057. -------------------------------------------
  2058. {"(abc,42,\"Thu Jan 02 00:00:00 2003\")"}
  2059. (1 row)
  2060. SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": "{\"(abc,42,01.02.2003)\"}"}') q;
  2061. reca
  2062. -------------------------------------------
  2063. {"(abc,42,\"Thu Jan 02 00:00:00 2003\")"}
  2064. (1 row)
  2065. SELECT rec FROM jsonb_populate_record(
  2066. row(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
  2067. row('x',3,'2012-12-31 15:30:56')::jbpop,NULL)::jsbrec,
  2068. '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}'
  2069. ) q;
  2070. rec
  2071. ------------------------------------
  2072. (abc,3,"Thu Jan 02 00:00:00 2003")
  2073. (1 row)
  2074. -- anonymous record type
  2075. SELECT jsonb_populate_record(null::record, '{"x": 0, "y": 1}');
  2076. ERROR: could not determine row type for result of jsonb_populate_record
  2077. HINT: Provide a non-null record argument, or call the function in the FROM clause using a column definition list.
  2078. SELECT jsonb_populate_record(row(1,2), '{"f1": 0, "f2": 1}');
  2079. jsonb_populate_record
  2080. -----------------------
  2081. (0,1)
  2082. (1 row)
  2083. SELECT * FROM
  2084. jsonb_populate_record(null::record, '{"x": 776}') AS (x int, y int);
  2085. x | y
  2086. -----+---
  2087. 776 |
  2088. (1 row)
  2089. -- composite domain
  2090. SELECT jsonb_populate_record(null::jb_ordered_pair, '{"x": 0, "y": 1}');
  2091. jsonb_populate_record
  2092. -----------------------
  2093. (0,1)
  2094. (1 row)
  2095. SELECT jsonb_populate_record(row(1,2)::jb_ordered_pair, '{"x": 0}');
  2096. jsonb_populate_record
  2097. -----------------------
  2098. (0,2)
  2099. (1 row)
  2100. SELECT jsonb_populate_record(row(1,2)::jb_ordered_pair, '{"x": 1, "y": 0}');
  2101. ERROR: value for domain jb_ordered_pair violates check constraint "jb_ordered_pair_check"
  2102. -- populate_recordset
  2103. SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  2104. a | b | c
  2105. --------+---+--------------------------
  2106. blurfl | |
  2107. | 3 | Fri Jan 20 10:42:53 2012
  2108. (2 rows)
  2109. SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  2110. a | b | c
  2111. --------+----+--------------------------
  2112. blurfl | 99 |
  2113. def | 3 | Fri Jan 20 10:42:53 2012
  2114. (2 rows)
  2115. SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  2116. a | b | c
  2117. --------+---+--------------------------
  2118. blurfl | |
  2119. | 3 | Fri Jan 20 10:42:53 2012
  2120. (2 rows)
  2121. SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  2122. a | b | c
  2123. --------+----+--------------------------
  2124. blurfl | 99 |
  2125. def | 3 | Fri Jan 20 10:42:53 2012
  2126. (2 rows)
  2127. SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
  2128. a | b | c
  2129. -----------------+----+--------------------------
  2130. [100, 200, 300] | 99 |
  2131. {"z": true} | 3 | Fri Jan 20 10:42:53 2012
  2132. (2 rows)
  2133. SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"c":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
  2134. ERROR: invalid input syntax for type timestamp: "[100, 200, 300]"
  2135. SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  2136. a | b | c
  2137. --------+---+--------------------------
  2138. blurfl | |
  2139. | 3 | Fri Jan 20 10:42:53 2012
  2140. (2 rows)
  2141. SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q;
  2142. a | b | c
  2143. --------+----+--------------------------
  2144. blurfl | 99 |
  2145. def | 3 | Fri Jan 20 10:42:53 2012
  2146. (2 rows)
  2147. SELECT * FROM jsonb_populate_recordset(row('def',99,NULL)::jbpop,'[{"a":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]') q;
  2148. a | b | c
  2149. -----------------+----+--------------------------
  2150. [100, 200, 300] | 99 |
  2151. {"z": true} | 3 | Fri Jan 20 10:42:53 2012
  2152. (2 rows)
  2153. -- anonymous record type
  2154. SELECT jsonb_populate_recordset(null::record, '[{"x": 0, "y": 1}]');
  2155. ERROR: could not determine row type for result of jsonb_populate_recordset
  2156. HINT: Provide a non-null record argument, or call the function in the FROM clause using a column definition list.
  2157. SELECT jsonb_populate_recordset(row(1,2), '[{"f1": 0, "f2": 1}]');
  2158. jsonb_populate_recordset
  2159. --------------------------
  2160. (0,1)
  2161. (1 row)
  2162. SELECT i, jsonb_populate_recordset(row(i,50), '[{"f1":"42"},{"f2":"43"}]')
  2163. FROM (VALUES (1),(2)) v(i);
  2164. i | jsonb_populate_recordset
  2165. ---+--------------------------
  2166. 1 | (42,50)
  2167. 1 | (1,43)
  2168. 2 | (42,50)
  2169. 2 | (2,43)
  2170. (4 rows)
  2171. SELECT * FROM
  2172. jsonb_populate_recordset(null::record, '[{"x": 776}]') AS (x int, y int);
  2173. x | y
  2174. -----+---
  2175. 776 |
  2176. (1 row)
  2177. -- empty array is a corner case
  2178. SELECT jsonb_populate_recordset(null::record, '[]');
  2179. ERROR: could not determine row type for result of jsonb_populate_recordset
  2180. HINT: Provide a non-null record argument, or call the function in the FROM clause using a column definition list.
  2181. SELECT jsonb_populate_recordset(row(1,2), '[]');
  2182. jsonb_populate_recordset
  2183. --------------------------
  2184. (0 rows)
  2185. SELECT * FROM jsonb_populate_recordset(NULL::jbpop,'[]') q;
  2186. a | b | c
  2187. ---+---+---
  2188. (0 rows)
  2189. SELECT * FROM
  2190. jsonb_populate_recordset(null::record, '[]') AS (x int, y int);
  2191. x | y
  2192. ---+---
  2193. (0 rows)
  2194. -- composite domain
  2195. SELECT jsonb_populate_recordset(null::jb_ordered_pair, '[{"x": 0, "y": 1}]');
  2196. jsonb_populate_recordset
  2197. --------------------------
  2198. (0,1)
  2199. (1 row)
  2200. SELECT jsonb_populate_recordset(row(1,2)::jb_ordered_pair, '[{"x": 0}, {"y": 3}]');
  2201. jsonb_populate_recordset
  2202. --------------------------
  2203. (0,2)
  2204. (1,3)
  2205. (2 rows)
  2206. SELECT jsonb_populate_recordset(row(1,2)::jb_ordered_pair, '[{"x": 1, "y": 0}]');
  2207. ERROR: value for domain jb_ordered_pair violates check constraint "jb_ordered_pair_check"
  2208. -- negative cases where the wrong record type is supplied
  2209. select * from jsonb_populate_recordset(row(0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text);
  2210. ERROR: function return row and query-specified return row do not match
  2211. DETAIL: Returned row contains 1 attribute, but query expects 2.
  2212. select * from jsonb_populate_recordset(row(0::int,0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text);
  2213. ERROR: function return row and query-specified return row do not match
  2214. DETAIL: Returned type integer at ordinal position 1, but query expects text.
  2215. select * from jsonb_populate_recordset(row(0::int,0::int,0::int),'[{"a":"1","b":"2"},{"a":"3"}]') q (a text, b text);
  2216. ERROR: function return row and query-specified return row do not match
  2217. DETAIL: Returned row contains 3 attributes, but query expects 2.
  2218. select * from jsonb_populate_recordset(row(1000000000::int,50::int),'[{"b":"2"},{"a":"3"}]') q (a text, b text);
  2219. ERROR: function return row and query-specified return row do not match
  2220. DETAIL: Returned type integer at ordinal position 1, but query expects text.
  2221. -- jsonb_to_record and jsonb_to_recordset
  2222. select * from jsonb_to_record('{"a":1,"b":"foo","c":"bar"}')
  2223. as x(a int, b text, d text);
  2224. a | b | d
  2225. ---+-----+---
  2226. 1 | foo |
  2227. (1 row)
  2228. select * from jsonb_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]')
  2229. as x(a int, b text, c boolean);
  2230. a | b | c
  2231. ---+-----+---
  2232. 1 | foo |
  2233. 2 | bar | t
  2234. (2 rows)
  2235. select *, c is null as c_is_null
  2236. from jsonb_to_record('{"a":1, "b":{"c":16, "d":2}, "x":8, "ca": ["1 2", 3], "ia": [[1,2],[3,4]], "r": {"a": "aaa", "b": 123}}'::jsonb)
  2237. as t(a int, b jsonb, c text, x int, ca char(5)[], ia int[][], r jbpop);
  2238. a | b | c | x | ca | ia | r | c_is_null
  2239. ---+-------------------+---+---+-------------------+---------------+------------+-----------
  2240. 1 | {"c": 16, "d": 2} | | 8 | {"1 2 ","3 "} | {{1,2},{3,4}} | (aaa,123,) | t
  2241. (1 row)
  2242. select *, c is null as c_is_null
  2243. from jsonb_to_recordset('[{"a":1, "b":{"c":16, "d":2}, "x":8}]'::jsonb)
  2244. as t(a int, b jsonb, c text, x int);
  2245. a | b | c | x | c_is_null
  2246. ---+-------------------+---+---+-----------
  2247. 1 | {"c": 16, "d": 2} | | 8 | t
  2248. (1 row)
  2249. select * from jsonb_to_record('{"ia": null}') as x(ia _int4);
  2250. ia
  2251. ----
  2252. (1 row)
  2253. select * from jsonb_to_record('{"ia": 123}') as x(ia _int4);
  2254. ERROR: expected JSON array
  2255. HINT: See the value of key "ia".
  2256. select * from jsonb_to_record('{"ia": [1, "2", null, 4]}') as x(ia _int4);
  2257. ia
  2258. --------------
  2259. {1,2,NULL,4}
  2260. (1 row)
  2261. select * from jsonb_to_record('{"ia": [[1, 2], [3, 4]]}') as x(ia _int4);
  2262. ia
  2263. ---------------
  2264. {{1,2},{3,4}}
  2265. (1 row)
  2266. select * from jsonb_to_record('{"ia": [[1], 2]}') as x(ia _int4);
  2267. ERROR: expected JSON array
  2268. HINT: See the array element [1] of key "ia".
  2269. select * from jsonb_to_record('{"ia": [[1], [2, 3]]}') as x(ia _int4);
  2270. ERROR: malformed JSON array
  2271. DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
  2272. select * from jsonb_to_record('{"ia2": [1, 2, 3]}') as x(ia2 int[][]);
  2273. ia2
  2274. ---------
  2275. {1,2,3}
  2276. (1 row)
  2277. select * from jsonb_to_record('{"ia2": [[1, 2], [3, 4]]}') as x(ia2 int4[][]);
  2278. ia2
  2279. ---------------
  2280. {{1,2},{3,4}}
  2281. (1 row)
  2282. select * from jsonb_to_record('{"ia2": [[[1], [2], [3]]]}') as x(ia2 int4[][]);
  2283. ia2
  2284. -----------------
  2285. {{{1},{2},{3}}}
  2286. (1 row)
  2287. select * from jsonb_to_record('{"out": {"key": 1}}') as x(out json);
  2288. out
  2289. ------------
  2290. {"key": 1}
  2291. (1 row)
  2292. select * from jsonb_to_record('{"out": [{"key": 1}]}') as x(out json);
  2293. out
  2294. --------------
  2295. [{"key": 1}]
  2296. (1 row)
  2297. select * from jsonb_to_record('{"out": "{\"key\": 1}"}') as x(out json);
  2298. out
  2299. ----------------
  2300. "{\"key\": 1}"
  2301. (1 row)
  2302. select * from jsonb_to_record('{"out": {"key": 1}}') as x(out jsonb);
  2303. out
  2304. ------------
  2305. {"key": 1}
  2306. (1 row)
  2307. select * from jsonb_to_record('{"out": [{"key": 1}]}') as x(out jsonb);
  2308. out
  2309. --------------
  2310. [{"key": 1}]
  2311. (1 row)
  2312. select * from jsonb_to_record('{"out": "{\"key\": 1}"}') as x(out jsonb);
  2313. out
  2314. ----------------
  2315. "{\"key\": 1}"
  2316. (1 row)
  2317. -- test type info caching in jsonb_populate_record()
  2318. CREATE TEMP TABLE jsbpoptest (js jsonb);
  2319. INSERT INTO jsbpoptest
  2320. SELECT '{
  2321. "jsa": [1, "2", null, 4],
  2322. "rec": {"a": "abc", "c": "01.02.2003", "x": 43.2},
  2323. "reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]
  2324. }'::jsonb
  2325. FROM generate_series(1, 3);
  2326. SELECT (jsonb_populate_record(NULL::jsbrec, js)).* FROM jsbpoptest;
  2327. i | ia | ia1 | ia2 | ia3 | ia1d | ia2d | t | ta | c | ca | ts | js | jsb | jsa | rec | reca
  2328. ---+----+-----+-----+-----+------+------+---+----+---+----+----+----+-----+--------------------+-----------------------------------+--------------------------------------------------------
  2329. | | | | | | | | | | | | | | {1,"\"2\"",NULL,4} | (abc,,"Thu Jan 02 00:00:00 2003") | {"(abc,456,)",NULL,"(,,\"Thu Jan 02 00:00:00 2003\")"}
  2330. | | | | | | | | | | | | | | {1,"\"2\"",NULL,4} | (abc,,"Thu Jan 02 00:00:00 2003") | {"(abc,456,)",NULL,"(,,\"Thu Jan 02 00:00:00 2003\")"}
  2331. | | | | | | | | | | | | | | {1,"\"2\"",NULL,4} | (abc,,"Thu Jan 02 00:00:00 2003") | {"(abc,456,)",NULL,"(,,\"Thu Jan 02 00:00:00 2003\")"}
  2332. (3 rows)
  2333. DROP TYPE jsbrec;
  2334. DROP TYPE jsbrec_i_not_null;
  2335. DROP DOMAIN jsb_int_not_null;
  2336. DROP DOMAIN jsb_int_array_1d;
  2337. DROP DOMAIN jsb_int_array_2d;
  2338. DROP DOMAIN jb_ordered_pair;
  2339. DROP TYPE jb_unordered_pair;
  2340. -- indexing
  2341. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
  2342. count
  2343. -------
  2344. 1
  2345. (1 row)
  2346. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
  2347. count
  2348. -------
  2349. 15
  2350. (1 row)
  2351. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
  2352. count
  2353. -------
  2354. 2
  2355. (1 row)
  2356. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
  2357. count
  2358. -------
  2359. 2
  2360. (1 row)
  2361. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
  2362. count
  2363. -------
  2364. 2
  2365. (1 row)
  2366. SELECT count(*) FROM testjsonb WHERE j ? 'public';
  2367. count
  2368. -------
  2369. 194
  2370. (1 row)
  2371. SELECT count(*) FROM testjsonb WHERE j ? 'bar';
  2372. count
  2373. -------
  2374. 0
  2375. (1 row)
  2376. SELECT count(*) FROM testjsonb WHERE j ?| ARRAY['public','disabled'];
  2377. count
  2378. -------
  2379. 337
  2380. (1 row)
  2381. SELECT count(*) FROM testjsonb WHERE j ?& ARRAY['public','disabled'];
  2382. count
  2383. -------
  2384. 42
  2385. (1 row)
  2386. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == null';
  2387. count
  2388. -------
  2389. 1
  2390. (1 row)
  2391. SELECT count(*) FROM testjsonb WHERE j @@ '"CC" == $.wait';
  2392. count
  2393. -------
  2394. 15
  2395. (1 row)
  2396. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == "CC" && true == $.public';
  2397. count
  2398. -------
  2399. 2
  2400. (1 row)
  2401. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25';
  2402. count
  2403. -------
  2404. 2
  2405. (1 row)
  2406. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25.0';
  2407. count
  2408. -------
  2409. 2
  2410. (1 row)
  2411. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($)';
  2412. count
  2413. -------
  2414. 1012
  2415. (1 row)
  2416. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public)';
  2417. count
  2418. -------
  2419. 194
  2420. (1 row)
  2421. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.bar)';
  2422. count
  2423. -------
  2424. 0
  2425. (1 row)
  2426. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public) || exists($.disabled)';
  2427. count
  2428. -------
  2429. 337
  2430. (1 row)
  2431. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public) && exists($.disabled)';
  2432. count
  2433. -------
  2434. 42
  2435. (1 row)
  2436. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
  2437. count
  2438. -------
  2439. 1
  2440. (1 row)
  2441. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? ("CC" == @)';
  2442. count
  2443. -------
  2444. 15
  2445. (1 row)
  2446. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.wait == "CC" && true == @.public)';
  2447. count
  2448. -------
  2449. 2
  2450. (1 row)
  2451. SELECT count(*) FROM testjsonb WHERE j @? '$.age ? (@ == 25)';
  2452. count
  2453. -------
  2454. 2
  2455. (1 row)
  2456. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.age == 25.0)';
  2457. count
  2458. -------
  2459. 2
  2460. (1 row)
  2461. SELECT count(*) FROM testjsonb WHERE j @? '$';
  2462. count
  2463. -------
  2464. 1012
  2465. (1 row)
  2466. SELECT count(*) FROM testjsonb WHERE j @? '$.public';
  2467. count
  2468. -------
  2469. 194
  2470. (1 row)
  2471. SELECT count(*) FROM testjsonb WHERE j @? '$.bar';
  2472. count
  2473. -------
  2474. 0
  2475. (1 row)
  2476. CREATE INDEX jidx ON testjsonb USING gin (j);
  2477. SET enable_seqscan = off;
  2478. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
  2479. count
  2480. -------
  2481. 1
  2482. (1 row)
  2483. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
  2484. count
  2485. -------
  2486. 15
  2487. (1 row)
  2488. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
  2489. count
  2490. -------
  2491. 2
  2492. (1 row)
  2493. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
  2494. count
  2495. -------
  2496. 2
  2497. (1 row)
  2498. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
  2499. count
  2500. -------
  2501. 2
  2502. (1 row)
  2503. SELECT count(*) FROM testjsonb WHERE j @> '{"array":["foo"]}';
  2504. count
  2505. -------
  2506. 3
  2507. (1 row)
  2508. SELECT count(*) FROM testjsonb WHERE j @> '{"array":["bar"]}';
  2509. count
  2510. -------
  2511. 3
  2512. (1 row)
  2513. -- exercise GIN_SEARCH_MODE_ALL
  2514. SELECT count(*) FROM testjsonb WHERE j @> '{}';
  2515. count
  2516. -------
  2517. 1012
  2518. (1 row)
  2519. SELECT count(*) FROM testjsonb WHERE j ? 'public';
  2520. count
  2521. -------
  2522. 194
  2523. (1 row)
  2524. SELECT count(*) FROM testjsonb WHERE j ? 'bar';
  2525. count
  2526. -------
  2527. 0
  2528. (1 row)
  2529. SELECT count(*) FROM testjsonb WHERE j ?| ARRAY['public','disabled'];
  2530. count
  2531. -------
  2532. 337
  2533. (1 row)
  2534. SELECT count(*) FROM testjsonb WHERE j ?& ARRAY['public','disabled'];
  2535. count
  2536. -------
  2537. 42
  2538. (1 row)
  2539. EXPLAIN (COSTS OFF)
  2540. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == null';
  2541. QUERY PLAN
  2542. -----------------------------------------------------------------
  2543. Aggregate
  2544. -> Bitmap Heap Scan on testjsonb
  2545. Recheck Cond: (j @@ '($."wait" == null)'::jsonpath)
  2546. -> Bitmap Index Scan on jidx
  2547. Index Cond: (j @@ '($."wait" == null)'::jsonpath)
  2548. (5 rows)
  2549. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == null';
  2550. count
  2551. -------
  2552. 1
  2553. (1 row)
  2554. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($ ? (@.wait == null))';
  2555. count
  2556. -------
  2557. 1
  2558. (1 row)
  2559. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.wait ? (@ == null))';
  2560. count
  2561. -------
  2562. 1
  2563. (1 row)
  2564. SELECT count(*) FROM testjsonb WHERE j @@ '"CC" == $.wait';
  2565. count
  2566. -------
  2567. 15
  2568. (1 row)
  2569. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == "CC" && true == $.public';
  2570. count
  2571. -------
  2572. 2
  2573. (1 row)
  2574. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25';
  2575. count
  2576. -------
  2577. 2
  2578. (1 row)
  2579. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25.0';
  2580. count
  2581. -------
  2582. 2
  2583. (1 row)
  2584. SELECT count(*) FROM testjsonb WHERE j @@ '$.array[*] == "foo"';
  2585. count
  2586. -------
  2587. 3
  2588. (1 row)
  2589. SELECT count(*) FROM testjsonb WHERE j @@ '$.array[*] == "bar"';
  2590. count
  2591. -------
  2592. 3
  2593. (1 row)
  2594. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($ ? (@.array[*] == "bar"))';
  2595. count
  2596. -------
  2597. 3
  2598. (1 row)
  2599. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.array ? (@[*] == "bar"))';
  2600. count
  2601. -------
  2602. 3
  2603. (1 row)
  2604. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.array[*] ? (@ == "bar"))';
  2605. count
  2606. -------
  2607. 3
  2608. (1 row)
  2609. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($)';
  2610. count
  2611. -------
  2612. 1012
  2613. (1 row)
  2614. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public)';
  2615. count
  2616. -------
  2617. 194
  2618. (1 row)
  2619. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.bar)';
  2620. count
  2621. -------
  2622. 0
  2623. (1 row)
  2624. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public) || exists($.disabled)';
  2625. count
  2626. -------
  2627. 337
  2628. (1 row)
  2629. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.public) && exists($.disabled)';
  2630. count
  2631. -------
  2632. 42
  2633. (1 row)
  2634. EXPLAIN (COSTS OFF)
  2635. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
  2636. QUERY PLAN
  2637. -------------------------------------------------------------------
  2638. Aggregate
  2639. -> Bitmap Heap Scan on testjsonb
  2640. Recheck Cond: (j @? '$."wait"?(@ == null)'::jsonpath)
  2641. -> Bitmap Index Scan on jidx
  2642. Index Cond: (j @? '$."wait"?(@ == null)'::jsonpath)
  2643. (5 rows)
  2644. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
  2645. count
  2646. -------
  2647. 1
  2648. (1 row)
  2649. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? ("CC" == @)';
  2650. count
  2651. -------
  2652. 15
  2653. (1 row)
  2654. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.wait == "CC" && true == @.public)';
  2655. count
  2656. -------
  2657. 2
  2658. (1 row)
  2659. SELECT count(*) FROM testjsonb WHERE j @? '$.age ? (@ == 25)';
  2660. count
  2661. -------
  2662. 2
  2663. (1 row)
  2664. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.age == 25.0)';
  2665. count
  2666. -------
  2667. 2
  2668. (1 row)
  2669. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.array[*] == "bar")';
  2670. count
  2671. -------
  2672. 3
  2673. (1 row)
  2674. SELECT count(*) FROM testjsonb WHERE j @? '$.array ? (@[*] == "bar")';
  2675. count
  2676. -------
  2677. 3
  2678. (1 row)
  2679. SELECT count(*) FROM testjsonb WHERE j @? '$.array[*] ? (@ == "bar")';
  2680. count
  2681. -------
  2682. 3
  2683. (1 row)
  2684. SELECT count(*) FROM testjsonb WHERE j @? '$';
  2685. count
  2686. -------
  2687. 1012
  2688. (1 row)
  2689. SELECT count(*) FROM testjsonb WHERE j @? '$.public';
  2690. count
  2691. -------
  2692. 194
  2693. (1 row)
  2694. SELECT count(*) FROM testjsonb WHERE j @? '$.bar';
  2695. count
  2696. -------
  2697. 0
  2698. (1 row)
  2699. -- array exists - array elements should behave as keys (for GIN index scans too)
  2700. CREATE INDEX jidx_array ON testjsonb USING gin((j->'array'));
  2701. SELECT count(*) from testjsonb WHERE j->'array' ? 'bar';
  2702. count
  2703. -------
  2704. 3
  2705. (1 row)
  2706. -- type sensitive array exists - should return no rows (since "exists" only
  2707. -- matches strings that are either object keys or array elements)
  2708. SELECT count(*) from testjsonb WHERE j->'array' ? '5'::text;
  2709. count
  2710. -------
  2711. 0
  2712. (1 row)
  2713. -- However, a raw scalar is *contained* within the array
  2714. SELECT count(*) from testjsonb WHERE j->'array' @> '5'::jsonb;
  2715. count
  2716. -------
  2717. 1
  2718. (1 row)
  2719. RESET enable_seqscan;
  2720. SELECT count(*) FROM (SELECT (jsonb_each(j)).key FROM testjsonb) AS wow;
  2721. count
  2722. -------
  2723. 4791
  2724. (1 row)
  2725. SELECT key, count(*) FROM (SELECT (jsonb_each(j)).key FROM testjsonb) AS wow GROUP BY key ORDER BY count DESC, key;
  2726. key | count
  2727. -----------+-------
  2728. line | 884
  2729. query | 207
  2730. pos | 203
  2731. node | 202
  2732. space | 197
  2733. status | 195
  2734. public | 194
  2735. title | 190
  2736. wait | 190
  2737. org | 189
  2738. user | 189
  2739. coauthors | 188
  2740. disabled | 185
  2741. indexed | 184
  2742. cleaned | 180
  2743. bad | 179
  2744. date | 179
  2745. world | 176
  2746. state | 172
  2747. subtitle | 169
  2748. auth | 168
  2749. abstract | 161
  2750. array | 5
  2751. age | 2
  2752. foo | 2
  2753. fool | 1
  2754. (26 rows)
  2755. -- sort/hash
  2756. SELECT count(distinct j) FROM testjsonb;
  2757. count
  2758. -------
  2759. 894
  2760. (1 row)
  2761. SET enable_hashagg = off;
  2762. SELECT count(*) FROM (SELECT j FROM (SELECT * FROM testjsonb UNION ALL SELECT * FROM testjsonb) js GROUP BY j) js2;
  2763. count
  2764. -------
  2765. 894
  2766. (1 row)
  2767. SET enable_hashagg = on;
  2768. SET enable_sort = off;
  2769. SELECT count(*) FROM (SELECT j FROM (SELECT * FROM testjsonb UNION ALL SELECT * FROM testjsonb) js GROUP BY j) js2;
  2770. count
  2771. -------
  2772. 894
  2773. (1 row)
  2774. SELECT distinct * FROM (values (jsonb '{}' || ''::text),('{}')) v(j);
  2775. j
  2776. ----
  2777. {}
  2778. (1 row)
  2779. SET enable_sort = on;
  2780. RESET enable_hashagg;
  2781. RESET enable_sort;
  2782. DROP INDEX jidx;
  2783. DROP INDEX jidx_array;
  2784. -- btree
  2785. CREATE INDEX jidx ON testjsonb USING btree (j);
  2786. SET enable_seqscan = off;
  2787. SELECT count(*) FROM testjsonb WHERE j > '{"p":1}';
  2788. count
  2789. -------
  2790. 884
  2791. (1 row)
  2792. SELECT count(*) FROM testjsonb WHERE j = '{"pos":98, "line":371, "node":"CBA", "indexed":true}';
  2793. count
  2794. -------
  2795. 1
  2796. (1 row)
  2797. --gin path opclass
  2798. DROP INDEX jidx;
  2799. CREATE INDEX jidx ON testjsonb USING gin (j jsonb_path_ops);
  2800. SET enable_seqscan = off;
  2801. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
  2802. count
  2803. -------
  2804. 1
  2805. (1 row)
  2806. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';
  2807. count
  2808. -------
  2809. 15
  2810. (1 row)
  2811. SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC", "public":true}';
  2812. count
  2813. -------
  2814. 2
  2815. (1 row)
  2816. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25}';
  2817. count
  2818. -------
  2819. 2
  2820. (1 row)
  2821. SELECT count(*) FROM testjsonb WHERE j @> '{"age":25.0}';
  2822. count
  2823. -------
  2824. 2
  2825. (1 row)
  2826. -- exercise GIN_SEARCH_MODE_ALL
  2827. SELECT count(*) FROM testjsonb WHERE j @> '{}';
  2828. count
  2829. -------
  2830. 1012
  2831. (1 row)
  2832. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == null';
  2833. count
  2834. -------
  2835. 1
  2836. (1 row)
  2837. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($ ? (@.wait == null))';
  2838. count
  2839. -------
  2840. 1
  2841. (1 row)
  2842. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.wait ? (@ == null))';
  2843. count
  2844. -------
  2845. 1
  2846. (1 row)
  2847. SELECT count(*) FROM testjsonb WHERE j @@ '"CC" == $.wait';
  2848. count
  2849. -------
  2850. 15
  2851. (1 row)
  2852. SELECT count(*) FROM testjsonb WHERE j @@ '$.wait == "CC" && true == $.public';
  2853. count
  2854. -------
  2855. 2
  2856. (1 row)
  2857. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25';
  2858. count
  2859. -------
  2860. 2
  2861. (1 row)
  2862. SELECT count(*) FROM testjsonb WHERE j @@ '$.age == 25.0';
  2863. count
  2864. -------
  2865. 2
  2866. (1 row)
  2867. SELECT count(*) FROM testjsonb WHERE j @@ '$.array[*] == "foo"';
  2868. count
  2869. -------
  2870. 3
  2871. (1 row)
  2872. SELECT count(*) FROM testjsonb WHERE j @@ '$.array[*] == "bar"';
  2873. count
  2874. -------
  2875. 3
  2876. (1 row)
  2877. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($ ? (@.array[*] == "bar"))';
  2878. count
  2879. -------
  2880. 3
  2881. (1 row)
  2882. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.array ? (@[*] == "bar"))';
  2883. count
  2884. -------
  2885. 3
  2886. (1 row)
  2887. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($.array[*] ? (@ == "bar"))';
  2888. count
  2889. -------
  2890. 3
  2891. (1 row)
  2892. SELECT count(*) FROM testjsonb WHERE j @@ 'exists($)';
  2893. count
  2894. -------
  2895. 1012
  2896. (1 row)
  2897. EXPLAIN (COSTS OFF)
  2898. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
  2899. QUERY PLAN
  2900. -------------------------------------------------------------------
  2901. Aggregate
  2902. -> Bitmap Heap Scan on testjsonb
  2903. Recheck Cond: (j @? '$."wait"?(@ == null)'::jsonpath)
  2904. -> Bitmap Index Scan on jidx
  2905. Index Cond: (j @? '$."wait"?(@ == null)'::jsonpath)
  2906. (5 rows)
  2907. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? (@ == null)';
  2908. count
  2909. -------
  2910. 1
  2911. (1 row)
  2912. SELECT count(*) FROM testjsonb WHERE j @? '$.wait ? ("CC" == @)';
  2913. count
  2914. -------
  2915. 15
  2916. (1 row)
  2917. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.wait == "CC" && true == @.public)';
  2918. count
  2919. -------
  2920. 2
  2921. (1 row)
  2922. SELECT count(*) FROM testjsonb WHERE j @? '$.age ? (@ == 25)';
  2923. count
  2924. -------
  2925. 2
  2926. (1 row)
  2927. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.age == 25.0)';
  2928. count
  2929. -------
  2930. 2
  2931. (1 row)
  2932. SELECT count(*) FROM testjsonb WHERE j @? '$ ? (@.array[*] == "bar")';
  2933. count
  2934. -------
  2935. 3
  2936. (1 row)
  2937. SELECT count(*) FROM testjsonb WHERE j @? '$.array ? (@[*] == "bar")';
  2938. count
  2939. -------
  2940. 3
  2941. (1 row)
  2942. SELECT count(*) FROM testjsonb WHERE j @? '$.array[*] ? (@ == "bar")';
  2943. count
  2944. -------
  2945. 3
  2946. (1 row)
  2947. SELECT count(*) FROM testjsonb WHERE j @? '$';
  2948. count
  2949. -------
  2950. 1012
  2951. (1 row)
  2952. SELECT count(*) FROM testjsonb WHERE j @? '$.public';
  2953. count
  2954. -------
  2955. 194
  2956. (1 row)
  2957. SELECT count(*) FROM testjsonb WHERE j @? '$.bar';
  2958. count
  2959. -------
  2960. 0
  2961. (1 row)
  2962. RESET enable_seqscan;
  2963. DROP INDEX jidx;
  2964. -- nested tests
  2965. SELECT '{"ff":{"a":12,"b":16}}'::jsonb;
  2966. jsonb
  2967. ----------------------------
  2968. {"ff": {"a": 12, "b": 16}}
  2969. (1 row)
  2970. SELECT '{"ff":{"a":12,"b":16},"qq":123}'::jsonb;
  2971. jsonb
  2972. ---------------------------------------
  2973. {"ff": {"a": 12, "b": 16}, "qq": 123}
  2974. (1 row)
  2975. SELECT '{"aa":["a","aaa"],"qq":{"a":12,"b":16,"c":["c1","c2"],"d":{"d1":"d1","d2":"d2","d1":"d3"}}}'::jsonb;
  2976. jsonb
  2977. --------------------------------------------------------------------------------------------------
  2978. {"aa": ["a", "aaa"], "qq": {"a": 12, "b": 16, "c": ["c1", "c2"], "d": {"d1": "d3", "d2": "d2"}}}
  2979. (1 row)
  2980. SELECT '{"aa":["a","aaa"],"qq":{"a":"12","b":"16","c":["c1","c2"],"d":{"d1":"d1","d2":"d2"}}}'::jsonb;
  2981. jsonb
  2982. ------------------------------------------------------------------------------------------------------
  2983. {"aa": ["a", "aaa"], "qq": {"a": "12", "b": "16", "c": ["c1", "c2"], "d": {"d1": "d1", "d2": "d2"}}}
  2984. (1 row)
  2985. SELECT '{"aa":["a","aaa"],"qq":{"a":"12","b":"16","c":["c1","c2",["c3"],{"c4":4}],"d":{"d1":"d1","d2":"d2"}}}'::jsonb;
  2986. jsonb
  2987. -------------------------------------------------------------------------------------------------------------------------
  2988. {"aa": ["a", "aaa"], "qq": {"a": "12", "b": "16", "c": ["c1", "c2", ["c3"], {"c4": 4}], "d": {"d1": "d1", "d2": "d2"}}}
  2989. (1 row)
  2990. SELECT '{"ff":["a","aaa"]}'::jsonb;
  2991. jsonb
  2992. ----------------------
  2993. {"ff": ["a", "aaa"]}
  2994. (1 row)
  2995. SELECT
  2996. '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'ff',
  2997. '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'qq',
  2998. ('{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'Y') IS NULL AS f,
  2999. ('{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb ->> 'Y') IS NULL AS t,
  3000. '{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'x';
  3001. ?column? | ?column? | f | t | ?column?
  3002. --------------------+----------+---+---+----------
  3003. {"a": 12, "b": 16} | 123 | f | t | [1, 2]
  3004. (1 row)
  3005. -- nested containment
  3006. SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[1,2]}';
  3007. ?column?
  3008. ----------
  3009. t
  3010. (1 row)
  3011. SELECT '{"a":[2,1],"c":"b"}'::jsonb @> '{"a":[1,2]}';
  3012. ?column?
  3013. ----------
  3014. t
  3015. (1 row)
  3016. SELECT '{"a":{"1":2},"c":"b"}'::jsonb @> '{"a":[1,2]}';
  3017. ?column?
  3018. ----------
  3019. f
  3020. (1 row)
  3021. SELECT '{"a":{"2":1},"c":"b"}'::jsonb @> '{"a":[1,2]}';
  3022. ?column?
  3023. ----------
  3024. f
  3025. (1 row)
  3026. SELECT '{"a":{"1":2},"c":"b"}'::jsonb @> '{"a":{"1":2}}';
  3027. ?column?
  3028. ----------
  3029. t
  3030. (1 row)
  3031. SELECT '{"a":{"2":1},"c":"b"}'::jsonb @> '{"a":{"1":2}}';
  3032. ?column?
  3033. ----------
  3034. f
  3035. (1 row)
  3036. SELECT '["a","b"]'::jsonb @> '["a","b","c","b"]';
  3037. ?column?
  3038. ----------
  3039. f
  3040. (1 row)
  3041. SELECT '["a","b","c","b"]'::jsonb @> '["a","b"]';
  3042. ?column?
  3043. ----------
  3044. t
  3045. (1 row)
  3046. SELECT '["a","b","c",[1,2]]'::jsonb @> '["a",[1,2]]';
  3047. ?column?
  3048. ----------
  3049. t
  3050. (1 row)
  3051. SELECT '["a","b","c",[1,2]]'::jsonb @> '["b",[1,2]]';
  3052. ?column?
  3053. ----------
  3054. t
  3055. (1 row)
  3056. SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[1]}';
  3057. ?column?
  3058. ----------
  3059. t
  3060. (1 row)
  3061. SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[2]}';
  3062. ?column?
  3063. ----------
  3064. t
  3065. (1 row)
  3066. SELECT '{"a":[1,2],"c":"b"}'::jsonb @> '{"a":[3]}';
  3067. ?column?
  3068. ----------
  3069. f
  3070. (1 row)
  3071. SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"c":3}]}';
  3072. ?column?
  3073. ----------
  3074. t
  3075. (1 row)
  3076. SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4}]}';
  3077. ?column?
  3078. ----------
  3079. t
  3080. (1 row)
  3081. SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},3]}';
  3082. ?column?
  3083. ----------
  3084. f
  3085. (1 row)
  3086. SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},1]}';
  3087. ?column?
  3088. ----------
  3089. t
  3090. (1 row)
  3091. -- check some corner cases for indexed nested containment (bug #13756)
  3092. create temp table nestjsonb (j jsonb);
  3093. insert into nestjsonb (j) values ('{"a":[["b",{"x":1}],["b",{"x":2}]],"c":3}');
  3094. insert into nestjsonb (j) values ('[[14,2,3]]');
  3095. insert into nestjsonb (j) values ('[1,[14,2,3]]');
  3096. create index on nestjsonb using gin(j jsonb_path_ops);
  3097. set enable_seqscan = on;
  3098. set enable_bitmapscan = off;
  3099. select * from nestjsonb where j @> '{"a":[[{"x":2}]]}'::jsonb;
  3100. j
  3101. ---------------------------------------------------
  3102. {"a": [["b", {"x": 1}], ["b", {"x": 2}]], "c": 3}
  3103. (1 row)
  3104. select * from nestjsonb where j @> '{"c":3}';
  3105. j
  3106. ---------------------------------------------------
  3107. {"a": [["b", {"x": 1}], ["b", {"x": 2}]], "c": 3}
  3108. (1 row)
  3109. select * from nestjsonb where j @> '[[14]]';
  3110. j
  3111. -----------------
  3112. [[14, 2, 3]]
  3113. [1, [14, 2, 3]]
  3114. (2 rows)
  3115. set enable_seqscan = off;
  3116. set enable_bitmapscan = on;
  3117. select * from nestjsonb where j @> '{"a":[[{"x":2}]]}'::jsonb;
  3118. j
  3119. ---------------------------------------------------
  3120. {"a": [["b", {"x": 1}], ["b", {"x": 2}]], "c": 3}
  3121. (1 row)
  3122. select * from nestjsonb where j @> '{"c":3}';
  3123. j
  3124. ---------------------------------------------------
  3125. {"a": [["b", {"x": 1}], ["b", {"x": 2}]], "c": 3}
  3126. (1 row)
  3127. select * from nestjsonb where j @> '[[14]]';
  3128. j
  3129. -----------------
  3130. [[14, 2, 3]]
  3131. [1, [14, 2, 3]]
  3132. (2 rows)
  3133. reset enable_seqscan;
  3134. reset enable_bitmapscan;
  3135. -- nested object field / array index lookup
  3136. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'n';
  3137. ?column?
  3138. ----------
  3139. null
  3140. (1 row)
  3141. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'a';
  3142. ?column?
  3143. ----------
  3144. 1
  3145. (1 row)
  3146. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'b';
  3147. ?column?
  3148. ----------
  3149. [1, 2]
  3150. (1 row)
  3151. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'c';
  3152. ?column?
  3153. ----------
  3154. {"1": 2}
  3155. (1 row)
  3156. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'd';
  3157. ?column?
  3158. ---------------
  3159. {"1": [2, 3]}
  3160. (1 row)
  3161. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'd' -> '1';
  3162. ?column?
  3163. ----------
  3164. [2, 3]
  3165. (1 row)
  3166. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'e';
  3167. ?column?
  3168. ----------
  3169. (1 row)
  3170. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 0; --expecting error
  3171. ?column?
  3172. ----------
  3173. (1 row)
  3174. SELECT '["a","b","c",[1,2],null]'::jsonb -> 0;
  3175. ?column?
  3176. ----------
  3177. "a"
  3178. (1 row)
  3179. SELECT '["a","b","c",[1,2],null]'::jsonb -> 1;
  3180. ?column?
  3181. ----------
  3182. "b"
  3183. (1 row)
  3184. SELECT '["a","b","c",[1,2],null]'::jsonb -> 2;
  3185. ?column?
  3186. ----------
  3187. "c"
  3188. (1 row)
  3189. SELECT '["a","b","c",[1,2],null]'::jsonb -> 3;
  3190. ?column?
  3191. ----------
  3192. [1, 2]
  3193. (1 row)
  3194. SELECT '["a","b","c",[1,2],null]'::jsonb -> 3 -> 1;
  3195. ?column?
  3196. ----------
  3197. 2
  3198. (1 row)
  3199. SELECT '["a","b","c",[1,2],null]'::jsonb -> 4;
  3200. ?column?
  3201. ----------
  3202. null
  3203. (1 row)
  3204. SELECT '["a","b","c",[1,2],null]'::jsonb -> 5;
  3205. ?column?
  3206. ----------
  3207. (1 row)
  3208. SELECT '["a","b","c",[1,2],null]'::jsonb -> -1;
  3209. ?column?
  3210. ----------
  3211. null
  3212. (1 row)
  3213. SELECT '["a","b","c",[1,2],null]'::jsonb -> -5;
  3214. ?column?
  3215. ----------
  3216. "a"
  3217. (1 row)
  3218. SELECT '["a","b","c",[1,2],null]'::jsonb -> -6;
  3219. ?column?
  3220. ----------
  3221. (1 row)
  3222. --nested path extraction
  3223. SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{0}';
  3224. ?column?
  3225. ----------
  3226. (1 row)
  3227. SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{a}';
  3228. ?column?
  3229. ----------
  3230. "b"
  3231. (1 row)
  3232. SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c}';
  3233. ?column?
  3234. -----------
  3235. [1, 2, 3]
  3236. (1 row)
  3237. SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,0}';
  3238. ?column?
  3239. ----------
  3240. 1
  3241. (1 row)
  3242. SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,1}';
  3243. ?column?
  3244. ----------
  3245. 2
  3246. (1 row)
  3247. SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,2}';
  3248. ?column?
  3249. ----------
  3250. 3
  3251. (1 row)
  3252. SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,3}';
  3253. ?column?
  3254. ----------
  3255. (1 row)
  3256. SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,-1}';
  3257. ?column?
  3258. ----------
  3259. 3
  3260. (1 row)
  3261. SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,-3}';
  3262. ?column?
  3263. ----------
  3264. 1
  3265. (1 row)
  3266. SELECT '{"a":"b","c":[1,2,3]}'::jsonb #> '{c,-4}';
  3267. ?column?
  3268. ----------
  3269. (1 row)
  3270. SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{0}';
  3271. ?column?
  3272. ----------
  3273. 0
  3274. (1 row)
  3275. SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{3}';
  3276. ?column?
  3277. ----------
  3278. [3, 4]
  3279. (1 row)
  3280. SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{4}';
  3281. ?column?
  3282. ---------------
  3283. {"5": "five"}
  3284. (1 row)
  3285. SELECT '[0,1,2,[3,4],{"5":"five"}]'::jsonb #> '{4,5}';
  3286. ?column?
  3287. ----------
  3288. "five"
  3289. (1 row)
  3290. --nested exists
  3291. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'n';
  3292. ?column?
  3293. ----------
  3294. t
  3295. (1 row)
  3296. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'a';
  3297. ?column?
  3298. ----------
  3299. t
  3300. (1 row)
  3301. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'b';
  3302. ?column?
  3303. ----------
  3304. t
  3305. (1 row)
  3306. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'c';
  3307. ?column?
  3308. ----------
  3309. t
  3310. (1 row)
  3311. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'd';
  3312. ?column?
  3313. ----------
  3314. t
  3315. (1 row)
  3316. SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb ? 'e';
  3317. ?column?
  3318. ----------
  3319. f
  3320. (1 row)
  3321. -- jsonb_strip_nulls
  3322. select jsonb_strip_nulls(null);
  3323. jsonb_strip_nulls
  3324. -------------------
  3325. (1 row)
  3326. select jsonb_strip_nulls('1');
  3327. jsonb_strip_nulls
  3328. -------------------
  3329. 1
  3330. (1 row)
  3331. select jsonb_strip_nulls('"a string"');
  3332. jsonb_strip_nulls
  3333. -------------------
  3334. "a string"
  3335. (1 row)
  3336. select jsonb_strip_nulls('null');
  3337. jsonb_strip_nulls
  3338. -------------------
  3339. null
  3340. (1 row)
  3341. select jsonb_strip_nulls('[1,2,null,3,4]');
  3342. jsonb_strip_nulls
  3343. --------------------
  3344. [1, 2, null, 3, 4]
  3345. (1 row)
  3346. select jsonb_strip_nulls('{"a":1,"b":null,"c":[2,null,3],"d":{"e":4,"f":null}}');
  3347. jsonb_strip_nulls
  3348. --------------------------------------------
  3349. {"a": 1, "c": [2, null, 3], "d": {"e": 4}}
  3350. (1 row)
  3351. select jsonb_strip_nulls('[1,{"a":1,"b":null,"c":2},3]');
  3352. jsonb_strip_nulls
  3353. --------------------------
  3354. [1, {"a": 1, "c": 2}, 3]
  3355. (1 row)
  3356. -- an empty object is not null and should not be stripped
  3357. select jsonb_strip_nulls('{"a": {"b": null, "c": null}, "d": {} }');
  3358. jsonb_strip_nulls
  3359. --------------------
  3360. {"a": {}, "d": {}}
  3361. (1 row)
  3362. select jsonb_pretty('{"a": "test", "b": [1, 2, 3], "c": "test3", "d":{"dd": "test4", "dd2":{"ddd": "test5"}}}');
  3363. jsonb_pretty
  3364. ----------------------------
  3365. { +
  3366. "a": "test", +
  3367. "b": [ +
  3368. 1, +
  3369. 2, +
  3370. 3 +
  3371. ], +
  3372. "c": "test3", +
  3373. "d": { +
  3374. "dd": "test4", +
  3375. "dd2": { +
  3376. "ddd": "test5"+
  3377. } +
  3378. } +
  3379. }
  3380. (1 row)
  3381. select jsonb_pretty('[{"f1":1,"f2":null},2,null,[[{"x":true},6,7],8],3]');
  3382. jsonb_pretty
  3383. ---------------------------
  3384. [ +
  3385. { +
  3386. "f1": 1, +
  3387. "f2": null +
  3388. }, +
  3389. 2, +
  3390. null, +
  3391. [ +
  3392. [ +
  3393. { +
  3394. "x": true+
  3395. }, +
  3396. 6, +
  3397. 7 +
  3398. ], +
  3399. 8 +
  3400. ], +
  3401. 3 +
  3402. ]
  3403. (1 row)
  3404. select jsonb_pretty('{"a":["b", "c"], "d": {"e":"f"}}');
  3405. jsonb_pretty
  3406. ------------------
  3407. { +
  3408. "a": [ +
  3409. "b", +
  3410. "c" +
  3411. ], +
  3412. "d": { +
  3413. "e": "f"+
  3414. } +
  3415. }
  3416. (1 row)
  3417. select jsonb_concat('{"d": "test", "a": [1, 2]}', '{"g": "test2", "c": {"c1":1, "c2":2}}');
  3418. jsonb_concat
  3419. -------------------------------------------------------------------
  3420. {"a": [1, 2], "c": {"c1": 1, "c2": 2}, "d": "test", "g": "test2"}
  3421. (1 row)
  3422. select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"cq":"l", "b":"g", "fg":false}';
  3423. ?column?
  3424. ---------------------------------------------
  3425. {"b": "g", "aa": 1, "cq": "l", "fg": false}
  3426. (1 row)
  3427. select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"aq":"l"}';
  3428. ?column?
  3429. ---------------------------------------
  3430. {"b": 2, "aa": 1, "aq": "l", "cq": 3}
  3431. (1 row)
  3432. select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{"aa":"l"}';
  3433. ?column?
  3434. ------------------------------
  3435. {"b": 2, "aa": "l", "cq": 3}
  3436. (1 row)
  3437. select '{"aa":1 , "b":2, "cq":3}'::jsonb || '{}';
  3438. ?column?
  3439. ----------------------------
  3440. {"b": 2, "aa": 1, "cq": 3}
  3441. (1 row)
  3442. select '["a", "b"]'::jsonb || '["c"]';
  3443. ?column?
  3444. -----------------
  3445. ["a", "b", "c"]
  3446. (1 row)
  3447. select '["a", "b"]'::jsonb || '["c", "d"]';
  3448. ?column?
  3449. ----------------------
  3450. ["a", "b", "c", "d"]
  3451. (1 row)
  3452. select '["c"]' || '["a", "b"]'::jsonb;
  3453. ?column?
  3454. -----------------
  3455. ["c", "a", "b"]
  3456. (1 row)
  3457. select '["a", "b"]'::jsonb || '"c"';
  3458. ?column?
  3459. -----------------
  3460. ["a", "b", "c"]
  3461. (1 row)
  3462. select '"c"' || '["a", "b"]'::jsonb;
  3463. ?column?
  3464. -----------------
  3465. ["c", "a", "b"]
  3466. (1 row)
  3467. select '[]'::jsonb || '["a"]'::jsonb;
  3468. ?column?
  3469. ----------
  3470. ["a"]
  3471. (1 row)
  3472. select '[]'::jsonb || '"a"'::jsonb;
  3473. ?column?
  3474. ----------
  3475. ["a"]
  3476. (1 row)
  3477. select '"b"'::jsonb || '"a"'::jsonb;
  3478. ?column?
  3479. ------------
  3480. ["b", "a"]
  3481. (1 row)
  3482. select '{}'::jsonb || '{"a":"b"}'::jsonb;
  3483. ?column?
  3484. ------------
  3485. {"a": "b"}
  3486. (1 row)
  3487. select '[]'::jsonb || '{"a":"b"}'::jsonb;
  3488. ?column?
  3489. --------------
  3490. [{"a": "b"}]
  3491. (1 row)
  3492. select '{"a":"b"}'::jsonb || '[]'::jsonb;
  3493. ?column?
  3494. --------------
  3495. [{"a": "b"}]
  3496. (1 row)
  3497. select '"a"'::jsonb || '{"a":1}';
  3498. ?column?
  3499. -----------------
  3500. ["a", {"a": 1}]
  3501. (1 row)
  3502. select '{"a":1}' || '"a"'::jsonb;
  3503. ?column?
  3504. -----------------
  3505. [{"a": 1}, "a"]
  3506. (1 row)
  3507. select '[3]'::jsonb || '{}'::jsonb;
  3508. ?column?
  3509. ----------
  3510. [3, {}]
  3511. (1 row)
  3512. select '3'::jsonb || '[]'::jsonb;
  3513. ?column?
  3514. ----------
  3515. [3]
  3516. (1 row)
  3517. select '3'::jsonb || '4'::jsonb;
  3518. ?column?
  3519. ----------
  3520. [3, 4]
  3521. (1 row)
  3522. select '3'::jsonb || '{}'::jsonb;
  3523. ?column?
  3524. ----------
  3525. [3, {}]
  3526. (1 row)
  3527. select '["a", "b"]'::jsonb || '{"c":1}';
  3528. ?column?
  3529. ----------------------
  3530. ["a", "b", {"c": 1}]
  3531. (1 row)
  3532. select '{"c": 1}'::jsonb || '["a", "b"]';
  3533. ?column?
  3534. ----------------------
  3535. [{"c": 1}, "a", "b"]
  3536. (1 row)
  3537. select '{}'::jsonb || '{"cq":"l", "b":"g", "fg":false}';
  3538. ?column?
  3539. ------------------------------------
  3540. {"b": "g", "cq": "l", "fg": false}
  3541. (1 row)
  3542. select pg_column_size('{}'::jsonb || '{}'::jsonb) = pg_column_size('{}'::jsonb);
  3543. ?column?
  3544. ----------
  3545. t
  3546. (1 row)
  3547. select pg_column_size('{"aa":1}'::jsonb || '{"b":2}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
  3548. ?column?
  3549. ----------
  3550. t
  3551. (1 row)
  3552. select pg_column_size('{"aa":1, "b":2}'::jsonb || '{}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
  3553. ?column?
  3554. ----------
  3555. t
  3556. (1 row)
  3557. select pg_column_size('{}'::jsonb || '{"aa":1, "b":2}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb);
  3558. ?column?
  3559. ----------
  3560. t
  3561. (1 row)
  3562. select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'a');
  3563. jsonb_delete
  3564. ------------------
  3565. {"b": 2, "c": 3}
  3566. (1 row)
  3567. select jsonb_delete('{"a":null , "b":2, "c":3}'::jsonb, 'a');
  3568. jsonb_delete
  3569. ------------------
  3570. {"b": 2, "c": 3}
  3571. (1 row)
  3572. select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'b');
  3573. jsonb_delete
  3574. ------------------
  3575. {"a": 1, "c": 3}
  3576. (1 row)
  3577. select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'c');
  3578. jsonb_delete
  3579. ------------------
  3580. {"a": 1, "b": 2}
  3581. (1 row)
  3582. select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'd');
  3583. jsonb_delete
  3584. --------------------------
  3585. {"a": 1, "b": 2, "c": 3}
  3586. (1 row)
  3587. select '{"a":1 , "b":2, "c":3}'::jsonb - 'a';
  3588. ?column?
  3589. ------------------
  3590. {"b": 2, "c": 3}
  3591. (1 row)
  3592. select '{"a":null , "b":2, "c":3}'::jsonb - 'a';
  3593. ?column?
  3594. ------------------
  3595. {"b": 2, "c": 3}
  3596. (1 row)
  3597. select '{"a":1 , "b":2, "c":3}'::jsonb - 'b';
  3598. ?column?
  3599. ------------------
  3600. {"a": 1, "c": 3}
  3601. (1 row)
  3602. select '{"a":1 , "b":2, "c":3}'::jsonb - 'c';
  3603. ?column?
  3604. ------------------
  3605. {"a": 1, "b": 2}
  3606. (1 row)
  3607. select '{"a":1 , "b":2, "c":3}'::jsonb - 'd';
  3608. ?column?
  3609. --------------------------
  3610. {"a": 1, "b": 2, "c": 3}
  3611. (1 row)
  3612. select pg_column_size('{"a":1 , "b":2, "c":3}'::jsonb - 'b') = pg_column_size('{"a":1, "b":2}'::jsonb);
  3613. ?column?
  3614. ----------
  3615. t
  3616. (1 row)
  3617. select '["a","b","c"]'::jsonb - 3;
  3618. ?column?
  3619. -----------------
  3620. ["a", "b", "c"]
  3621. (1 row)
  3622. select '["a","b","c"]'::jsonb - 2;
  3623. ?column?
  3624. ------------
  3625. ["a", "b"]
  3626. (1 row)
  3627. select '["a","b","c"]'::jsonb - 1;
  3628. ?column?
  3629. ------------
  3630. ["a", "c"]
  3631. (1 row)
  3632. select '["a","b","c"]'::jsonb - 0;
  3633. ?column?
  3634. ------------
  3635. ["b", "c"]
  3636. (1 row)
  3637. select '["a","b","c"]'::jsonb - -1;
  3638. ?column?
  3639. ------------
  3640. ["a", "b"]
  3641. (1 row)
  3642. select '["a","b","c"]'::jsonb - -2;
  3643. ?column?
  3644. ------------
  3645. ["a", "c"]
  3646. (1 row)
  3647. select '["a","b","c"]'::jsonb - -3;
  3648. ?column?
  3649. ------------
  3650. ["b", "c"]
  3651. (1 row)
  3652. select '["a","b","c"]'::jsonb - -4;
  3653. ?column?
  3654. -----------------
  3655. ["a", "b", "c"]
  3656. (1 row)
  3657. select '{"a":1 , "b":2, "c":3}'::jsonb - '{b}'::text[];
  3658. ?column?
  3659. ------------------
  3660. {"a": 1, "c": 3}
  3661. (1 row)
  3662. select '{"a":1 , "b":2, "c":3}'::jsonb - '{c,b}'::text[];
  3663. ?column?
  3664. ----------
  3665. {"a": 1}
  3666. (1 row)
  3667. select '{"a":1 , "b":2, "c":3}'::jsonb - '{}'::text[];
  3668. ?column?
  3669. --------------------------
  3670. {"a": 1, "b": 2, "c": 3}
  3671. (1 row)
  3672. select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]');
  3673. jsonb_set
  3674. --------------------------------------------------------------------------
  3675. {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": [1, 2, 3]}
  3676. (1 row)
  3677. select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '[1,2,3]');
  3678. jsonb_set
  3679. -----------------------------------------------------------------------------
  3680. {"a": 1, "b": [1, [1, 2, 3]], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
  3681. (1 row)
  3682. select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '[1,2,3]');
  3683. jsonb_set
  3684. -----------------------------------------------------------------------------
  3685. {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [[1, 2, 3], 3]}, "n": null}
  3686. (1 row)
  3687. select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '[1,2,3]');
  3688. ERROR: path element at position 2 is null
  3689. select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '{"1": 2}');
  3690. jsonb_set
  3691. -------------------------------------------------------------------------
  3692. {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": {"1": 2}}
  3693. (1 row)
  3694. select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"1": 2}');
  3695. jsonb_set
  3696. ----------------------------------------------------------------------------
  3697. {"a": 1, "b": [1, {"1": 2}], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
  3698. (1 row)
  3699. select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '{"1": 2}');
  3700. jsonb_set
  3701. ----------------------------------------------------------------------------
  3702. {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [{"1": 2}, 3]}, "n": null}
  3703. (1 row)
  3704. select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '{"1": 2}');
  3705. ERROR: path element at position 2 is null
  3706. select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '"test"');
  3707. jsonb_set
  3708. --------------------------------------------------------------------------
  3709. {"a": 1, "b": [1, "test"], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
  3710. (1 row)
  3711. select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '{"f": "test"}');
  3712. jsonb_set
  3713. ---------------------------------------------------------------------------------
  3714. {"a": 1, "b": [1, {"f": "test"}], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
  3715. (1 row)
  3716. select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{n}');
  3717. jsonb_delete_path
  3718. ----------------------------------------------------------
  3719. {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}}
  3720. (1 row)
  3721. select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{b,-1}');
  3722. jsonb_delete_path
  3723. ------------------------------------------------------------------
  3724. {"a": 1, "b": [1], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
  3725. (1 row)
  3726. select jsonb_delete_path('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}', '{d,1,0}');
  3727. jsonb_delete_path
  3728. ------------------------------------------------------------------
  3729. {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [3]}, "n": null}
  3730. (1 row)
  3731. select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{n}';
  3732. ?column?
  3733. ----------------------------------------------------------
  3734. {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}}
  3735. (1 row)
  3736. select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{b,-1}';
  3737. ?column?
  3738. ------------------------------------------------------------------
  3739. {"a": 1, "b": [1], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null}
  3740. (1 row)
  3741. select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{b,-1e}'; -- invalid array subscript
  3742. ERROR: path element at position 2 is not an integer: "-1e"
  3743. select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{d,1,0}';
  3744. ?column?
  3745. ------------------------------------------------------------------
  3746. {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [3]}, "n": null}
  3747. (1 row)
  3748. -- empty structure and error conditions for delete and replace
  3749. select '"a"'::jsonb - 'a'; -- error
  3750. ERROR: cannot delete from scalar
  3751. select '{}'::jsonb - 'a';
  3752. ?column?
  3753. ----------
  3754. {}
  3755. (1 row)
  3756. select '[]'::jsonb - 'a';
  3757. ?column?
  3758. ----------
  3759. []
  3760. (1 row)
  3761. select '"a"'::jsonb - 1; -- error
  3762. ERROR: cannot delete from scalar
  3763. select '{}'::jsonb - 1; -- error
  3764. ERROR: cannot delete from object using integer index
  3765. select '[]'::jsonb - 1;
  3766. ?column?
  3767. ----------
  3768. []
  3769. (1 row)
  3770. select '"a"'::jsonb #- '{a}'; -- error
  3771. ERROR: cannot delete path in scalar
  3772. select '{}'::jsonb #- '{a}';
  3773. ?column?
  3774. ----------
  3775. {}
  3776. (1 row)
  3777. select '[]'::jsonb #- '{a}';
  3778. ?column?
  3779. ----------
  3780. []
  3781. (1 row)
  3782. select jsonb_set('"a"','{a}','"b"'); --error
  3783. ERROR: cannot set path in scalar
  3784. select jsonb_set('{}','{a}','"b"', false);
  3785. jsonb_set
  3786. -----------
  3787. {}
  3788. (1 row)
  3789. select jsonb_set('[]','{1}','"b"', false);
  3790. jsonb_set
  3791. -----------
  3792. []
  3793. (1 row)
  3794. select jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{0}','[2,3,4]', false);
  3795. jsonb_set
  3796. -------------------------
  3797. [[2, 3, 4], 2, null, 3]
  3798. (1 row)
  3799. -- jsonb_set adding instead of replacing
  3800. -- prepend to array
  3801. select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{b,-33}','{"foo":123}');
  3802. jsonb_set
  3803. -------------------------------------------------------
  3804. {"a": 1, "b": [{"foo": 123}, 0, 1, 2], "c": {"d": 4}}
  3805. (1 row)
  3806. -- append to array
  3807. select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{b,33}','{"foo":123}');
  3808. jsonb_set
  3809. -------------------------------------------------------
  3810. {"a": 1, "b": [0, 1, 2, {"foo": 123}], "c": {"d": 4}}
  3811. (1 row)
  3812. -- check nesting levels addition
  3813. select jsonb_set('{"a":1,"b":[4,5,[0,1,2],6,7],"c":{"d":4}}','{b,2,33}','{"foo":123}');
  3814. jsonb_set
  3815. ---------------------------------------------------------------------
  3816. {"a": 1, "b": [4, 5, [0, 1, 2, {"foo": 123}], 6, 7], "c": {"d": 4}}
  3817. (1 row)
  3818. -- add new key
  3819. select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{c,e}','{"foo":123}');
  3820. jsonb_set
  3821. ------------------------------------------------------------
  3822. {"a": 1, "b": [0, 1, 2], "c": {"d": 4, "e": {"foo": 123}}}
  3823. (1 row)
  3824. -- adding doesn't do anything if elements before last aren't present
  3825. select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{x,-33}','{"foo":123}');
  3826. jsonb_set
  3827. -----------------------------------------
  3828. {"a": 1, "b": [0, 1, 2], "c": {"d": 4}}
  3829. (1 row)
  3830. select jsonb_set('{"a":1,"b":[0,1,2],"c":{"d":4}}','{x,y}','{"foo":123}');
  3831. jsonb_set
  3832. -----------------------------------------
  3833. {"a": 1, "b": [0, 1, 2], "c": {"d": 4}}
  3834. (1 row)
  3835. -- add to empty object
  3836. select jsonb_set('{}','{x}','{"foo":123}');
  3837. jsonb_set
  3838. ---------------------
  3839. {"x": {"foo": 123}}
  3840. (1 row)
  3841. --add to empty array
  3842. select jsonb_set('[]','{0}','{"foo":123}');
  3843. jsonb_set
  3844. ----------------
  3845. [{"foo": 123}]
  3846. (1 row)
  3847. select jsonb_set('[]','{99}','{"foo":123}');
  3848. jsonb_set
  3849. ----------------
  3850. [{"foo": 123}]
  3851. (1 row)
  3852. select jsonb_set('[]','{-99}','{"foo":123}');
  3853. jsonb_set
  3854. ----------------
  3855. [{"foo": 123}]
  3856. (1 row)
  3857. select jsonb_set('{"a": [1, 2, 3]}', '{a, non_integer}', '"new_value"');
  3858. ERROR: path element at position 2 is not an integer: "non_integer"
  3859. select jsonb_set('{"a": {"b": [1, 2, 3]}}', '{a, b, non_integer}', '"new_value"');
  3860. ERROR: path element at position 3 is not an integer: "non_integer"
  3861. select jsonb_set('{"a": {"b": [1, 2, 3]}}', '{a, b, NULL}', '"new_value"');
  3862. ERROR: path element at position 3 is null
  3863. -- jsonb_set_lax
  3864. \pset null NULL
  3865. -- pass though non nulls to jsonb_set
  3866. select jsonb_set_lax('{"a":1,"b":2}','{b}','5') ;
  3867. jsonb_set_lax
  3868. ------------------
  3869. {"a": 1, "b": 5}
  3870. (1 row)
  3871. select jsonb_set_lax('{"a":1,"b":2}','{d}','6', true) ;
  3872. jsonb_set_lax
  3873. --------------------------
  3874. {"a": 1, "b": 2, "d": 6}
  3875. (1 row)
  3876. -- using the default treatment
  3877. select jsonb_set_lax('{"a":1,"b":2}','{b}',null);
  3878. jsonb_set_lax
  3879. ---------------------
  3880. {"a": 1, "b": null}
  3881. (1 row)
  3882. select jsonb_set_lax('{"a":1,"b":2}','{d}',null,true);
  3883. jsonb_set_lax
  3884. -----------------------------
  3885. {"a": 1, "b": 2, "d": null}
  3886. (1 row)
  3887. -- errors
  3888. select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, true, null);
  3889. ERROR: null_value_treatment must be "delete_key", "return_target", "use_json_null", or "raise_exception"
  3890. select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, true, 'no_such_treatment');
  3891. ERROR: null_value_treatment must be "delete_key", "return_target", "use_json_null", or "raise_exception"
  3892. -- explicit treatments
  3893. select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, null_value_treatment => 'raise_exception') as raise_exception;
  3894. ERROR: JSON value must not be null
  3895. DETAIL: Exception was raised because null_value_treatment is "raise_exception".
  3896. HINT: To avoid, either change the null_value_treatment argument or ensure that an SQL NULL is not passed.
  3897. select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, null_value_treatment => 'return_target') as return_target;
  3898. return_target
  3899. ------------------
  3900. {"a": 1, "b": 2}
  3901. (1 row)
  3902. select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, null_value_treatment => 'delete_key') as delete_key;
  3903. delete_key
  3904. ------------
  3905. {"a": 1}
  3906. (1 row)
  3907. select jsonb_set_lax('{"a":1,"b":2}', '{b}', null, null_value_treatment => 'use_json_null') as use_json_null;
  3908. use_json_null
  3909. ---------------------
  3910. {"a": 1, "b": null}
  3911. (1 row)
  3912. \pset null ''
  3913. -- jsonb_insert
  3914. select jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"');
  3915. jsonb_insert
  3916. -------------------------------
  3917. {"a": [0, "new_value", 1, 2]}
  3918. (1 row)
  3919. select jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"', true);
  3920. jsonb_insert
  3921. -------------------------------
  3922. {"a": [0, 1, "new_value", 2]}
  3923. (1 row)
  3924. select jsonb_insert('{"a": {"b": {"c": [0, 1, "test1", "test2"]}}}', '{a, b, c, 2}', '"new_value"');
  3925. jsonb_insert
  3926. ------------------------------------------------------------
  3927. {"a": {"b": {"c": [0, 1, "new_value", "test1", "test2"]}}}
  3928. (1 row)
  3929. select jsonb_insert('{"a": {"b": {"c": [0, 1, "test1", "test2"]}}}', '{a, b, c, 2}', '"new_value"', true);
  3930. jsonb_insert
  3931. ------------------------------------------------------------
  3932. {"a": {"b": {"c": [0, 1, "test1", "new_value", "test2"]}}}
  3933. (1 row)
  3934. select jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '{"b": "value"}');
  3935. jsonb_insert
  3936. ----------------------------------
  3937. {"a": [0, {"b": "value"}, 1, 2]}
  3938. (1 row)
  3939. select jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '["value1", "value2"]');
  3940. jsonb_insert
  3941. ----------------------------------------
  3942. {"a": [0, ["value1", "value2"], 1, 2]}
  3943. (1 row)
  3944. -- edge cases
  3945. select jsonb_insert('{"a": [0,1,2]}', '{a, 0}', '"new_value"');
  3946. jsonb_insert
  3947. -------------------------------
  3948. {"a": ["new_value", 0, 1, 2]}
  3949. (1 row)
  3950. select jsonb_insert('{"a": [0,1,2]}', '{a, 0}', '"new_value"', true);
  3951. jsonb_insert
  3952. -------------------------------
  3953. {"a": [0, "new_value", 1, 2]}
  3954. (1 row)
  3955. select jsonb_insert('{"a": [0,1,2]}', '{a, 2}', '"new_value"');
  3956. jsonb_insert
  3957. -------------------------------
  3958. {"a": [0, 1, "new_value", 2]}
  3959. (1 row)
  3960. select jsonb_insert('{"a": [0,1,2]}', '{a, 2}', '"new_value"', true);
  3961. jsonb_insert
  3962. -------------------------------
  3963. {"a": [0, 1, 2, "new_value"]}
  3964. (1 row)
  3965. select jsonb_insert('{"a": [0,1,2]}', '{a, -1}', '"new_value"');
  3966. jsonb_insert
  3967. -------------------------------
  3968. {"a": [0, 1, "new_value", 2]}
  3969. (1 row)
  3970. select jsonb_insert('{"a": [0,1,2]}', '{a, -1}', '"new_value"', true);
  3971. jsonb_insert
  3972. -------------------------------
  3973. {"a": [0, 1, 2, "new_value"]}
  3974. (1 row)
  3975. select jsonb_insert('[]', '{1}', '"new_value"');
  3976. jsonb_insert
  3977. ---------------
  3978. ["new_value"]
  3979. (1 row)
  3980. select jsonb_insert('[]', '{1}', '"new_value"', true);
  3981. jsonb_insert
  3982. ---------------
  3983. ["new_value"]
  3984. (1 row)
  3985. select jsonb_insert('{"a": []}', '{a, 1}', '"new_value"');
  3986. jsonb_insert
  3987. ----------------------
  3988. {"a": ["new_value"]}
  3989. (1 row)
  3990. select jsonb_insert('{"a": []}', '{a, 1}', '"new_value"', true);
  3991. jsonb_insert
  3992. ----------------------
  3993. {"a": ["new_value"]}
  3994. (1 row)
  3995. select jsonb_insert('{"a": [0,1,2]}', '{a, 10}', '"new_value"');
  3996. jsonb_insert
  3997. -------------------------------
  3998. {"a": [0, 1, 2, "new_value"]}
  3999. (1 row)
  4000. select jsonb_insert('{"a": [0,1,2]}', '{a, -10}', '"new_value"');
  4001. jsonb_insert
  4002. -------------------------------
  4003. {"a": ["new_value", 0, 1, 2]}
  4004. (1 row)
  4005. -- jsonb_insert should be able to insert new value for objects, but not to replace
  4006. select jsonb_insert('{"a": {"b": "value"}}', '{a, c}', '"new_value"');
  4007. jsonb_insert
  4008. -----------------------------------------
  4009. {"a": {"b": "value", "c": "new_value"}}
  4010. (1 row)
  4011. select jsonb_insert('{"a": {"b": "value"}}', '{a, c}', '"new_value"', true);
  4012. jsonb_insert
  4013. -----------------------------------------
  4014. {"a": {"b": "value", "c": "new_value"}}
  4015. (1 row)
  4016. select jsonb_insert('{"a": {"b": "value"}}', '{a, b}', '"new_value"');
  4017. ERROR: cannot replace existing key
  4018. HINT: Try using the function jsonb_set to replace key value.
  4019. select jsonb_insert('{"a": {"b": "value"}}', '{a, b}', '"new_value"', true);
  4020. ERROR: cannot replace existing key
  4021. HINT: Try using the function jsonb_set to replace key value.
  4022. -- jsonb subscript
  4023. select ('123'::jsonb)['a'];
  4024. jsonb
  4025. -------
  4026. (1 row)
  4027. select ('123'::jsonb)[0];
  4028. jsonb
  4029. -------
  4030. (1 row)
  4031. select ('123'::jsonb)[NULL];
  4032. jsonb
  4033. -------
  4034. (1 row)
  4035. select ('{"a": 1}'::jsonb)['a'];
  4036. jsonb
  4037. -------
  4038. 1
  4039. (1 row)
  4040. select ('{"a": 1}'::jsonb)[0];
  4041. jsonb
  4042. -------
  4043. (1 row)
  4044. select ('{"a": 1}'::jsonb)['not_exist'];
  4045. jsonb
  4046. -------
  4047. (1 row)
  4048. select ('{"a": 1}'::jsonb)[NULL];
  4049. jsonb
  4050. -------
  4051. (1 row)
  4052. select ('[1, "2", null]'::jsonb)['a'];
  4053. jsonb
  4054. -------
  4055. (1 row)
  4056. select ('[1, "2", null]'::jsonb)[0];
  4057. jsonb
  4058. -------
  4059. 1
  4060. (1 row)
  4061. select ('[1, "2", null]'::jsonb)['1'];
  4062. jsonb
  4063. -------
  4064. "2"
  4065. (1 row)
  4066. select ('[1, "2", null]'::jsonb)[1.0];
  4067. ERROR: subscript type numeric is not supported
  4068. LINE 1: select ('[1, "2", null]'::jsonb)[1.0];
  4069. ^
  4070. HINT: jsonb subscript must be coercible to either integer or text.
  4071. select ('[1, "2", null]'::jsonb)[2];
  4072. jsonb
  4073. -------
  4074. null
  4075. (1 row)
  4076. select ('[1, "2", null]'::jsonb)[3];
  4077. jsonb
  4078. -------
  4079. (1 row)
  4080. select ('[1, "2", null]'::jsonb)[-2];
  4081. jsonb
  4082. -------
  4083. "2"
  4084. (1 row)
  4085. select ('[1, "2", null]'::jsonb)[1]['a'];
  4086. jsonb
  4087. -------
  4088. (1 row)
  4089. select ('[1, "2", null]'::jsonb)[1][0];
  4090. jsonb
  4091. -------
  4092. (1 row)
  4093. select ('{"a": 1, "b": "c", "d": [1, 2, 3]}'::jsonb)['b'];
  4094. jsonb
  4095. -------
  4096. "c"
  4097. (1 row)
  4098. select ('{"a": 1, "b": "c", "d": [1, 2, 3]}'::jsonb)['d'];
  4099. jsonb
  4100. -----------
  4101. [1, 2, 3]
  4102. (1 row)
  4103. select ('{"a": 1, "b": "c", "d": [1, 2, 3]}'::jsonb)['d'][1];
  4104. jsonb
  4105. -------
  4106. 2
  4107. (1 row)
  4108. select ('{"a": 1, "b": "c", "d": [1, 2, 3]}'::jsonb)['d']['a'];
  4109. jsonb
  4110. -------
  4111. (1 row)
  4112. select ('{"a": {"a1": {"a2": "aaa"}}, "b": "bbb", "c": "ccc"}'::jsonb)['a']['a1'];
  4113. jsonb
  4114. ---------------
  4115. {"a2": "aaa"}
  4116. (1 row)
  4117. select ('{"a": {"a1": {"a2": "aaa"}}, "b": "bbb", "c": "ccc"}'::jsonb)['a']['a1']['a2'];
  4118. jsonb
  4119. -------
  4120. "aaa"
  4121. (1 row)
  4122. select ('{"a": {"a1": {"a2": "aaa"}}, "b": "bbb", "c": "ccc"}'::jsonb)['a']['a1']['a2']['a3'];
  4123. jsonb
  4124. -------
  4125. (1 row)
  4126. select ('{"a": ["a1", {"b1": ["aaa", "bbb", "ccc"]}], "b": "bb"}'::jsonb)['a'][1]['b1'];
  4127. jsonb
  4128. -----------------------
  4129. ["aaa", "bbb", "ccc"]
  4130. (1 row)
  4131. select ('{"a": ["a1", {"b1": ["aaa", "bbb", "ccc"]}], "b": "bb"}'::jsonb)['a'][1]['b1'][2];
  4132. jsonb
  4133. -------
  4134. "ccc"
  4135. (1 row)
  4136. -- slices are not supported
  4137. select ('{"a": 1}'::jsonb)['a':'b'];
  4138. ERROR: jsonb subscript does not support slices
  4139. LINE 1: select ('{"a": 1}'::jsonb)['a':'b'];
  4140. ^
  4141. select ('[1, "2", null]'::jsonb)[1:2];
  4142. ERROR: jsonb subscript does not support slices
  4143. LINE 1: select ('[1, "2", null]'::jsonb)[1:2];
  4144. ^
  4145. select ('[1, "2", null]'::jsonb)[:2];
  4146. ERROR: jsonb subscript does not support slices
  4147. LINE 1: select ('[1, "2", null]'::jsonb)[:2];
  4148. ^
  4149. select ('[1, "2", null]'::jsonb)[1:];
  4150. ERROR: jsonb subscript does not support slices
  4151. LINE 1: select ('[1, "2", null]'::jsonb)[1:];
  4152. ^
  4153. select ('[1, "2", null]'::jsonb)[:];
  4154. ERROR: jsonb subscript does not support slices
  4155. create TEMP TABLE test_jsonb_subscript (
  4156. id int,
  4157. test_json jsonb
  4158. );
  4159. insert into test_jsonb_subscript values
  4160. (1, '{}'), -- empty jsonb
  4161. (2, '{"key": "value"}'); -- jsonb with data
  4162. -- update empty jsonb
  4163. update test_jsonb_subscript set test_json['a'] = '1' where id = 1;
  4164. select * from test_jsonb_subscript;
  4165. id | test_json
  4166. ----+------------------
  4167. 2 | {"key": "value"}
  4168. 1 | {"a": 1}
  4169. (2 rows)
  4170. -- update jsonb with some data
  4171. update test_jsonb_subscript set test_json['a'] = '1' where id = 2;
  4172. select * from test_jsonb_subscript;
  4173. id | test_json
  4174. ----+--------------------------
  4175. 1 | {"a": 1}
  4176. 2 | {"a": 1, "key": "value"}
  4177. (2 rows)
  4178. -- replace jsonb
  4179. update test_jsonb_subscript set test_json['a'] = '"test"';
  4180. select * from test_jsonb_subscript;
  4181. id | test_json
  4182. ----+-------------------------------
  4183. 1 | {"a": "test"}
  4184. 2 | {"a": "test", "key": "value"}
  4185. (2 rows)
  4186. -- replace by object
  4187. update test_jsonb_subscript set test_json['a'] = '{"b": 1}'::jsonb;
  4188. select * from test_jsonb_subscript;
  4189. id | test_json
  4190. ----+---------------------------------
  4191. 1 | {"a": {"b": 1}}
  4192. 2 | {"a": {"b": 1}, "key": "value"}
  4193. (2 rows)
  4194. -- replace by array
  4195. update test_jsonb_subscript set test_json['a'] = '[1, 2, 3]'::jsonb;
  4196. select * from test_jsonb_subscript;
  4197. id | test_json
  4198. ----+----------------------------------
  4199. 1 | {"a": [1, 2, 3]}
  4200. 2 | {"a": [1, 2, 3], "key": "value"}
  4201. (2 rows)
  4202. -- use jsonb subscription in where clause
  4203. select * from test_jsonb_subscript where test_json['key'] = '"value"';
  4204. id | test_json
  4205. ----+----------------------------------
  4206. 2 | {"a": [1, 2, 3], "key": "value"}
  4207. (1 row)
  4208. select * from test_jsonb_subscript where test_json['key_doesnt_exists'] = '"value"';
  4209. id | test_json
  4210. ----+-----------
  4211. (0 rows)
  4212. select * from test_jsonb_subscript where test_json['key'] = '"wrong_value"';
  4213. id | test_json
  4214. ----+-----------
  4215. (0 rows)
  4216. -- NULL
  4217. update test_jsonb_subscript set test_json[NULL] = '1';
  4218. ERROR: jsonb subscript in assignment must not be null
  4219. update test_jsonb_subscript set test_json['another_key'] = NULL;
  4220. select * from test_jsonb_subscript;
  4221. id | test_json
  4222. ----+-------------------------------------------------------
  4223. 1 | {"a": [1, 2, 3], "another_key": null}
  4224. 2 | {"a": [1, 2, 3], "key": "value", "another_key": null}
  4225. (2 rows)
  4226. -- NULL as jsonb source
  4227. insert into test_jsonb_subscript values (3, NULL);
  4228. update test_jsonb_subscript set test_json['a'] = '1' where id = 3;
  4229. select * from test_jsonb_subscript;
  4230. id | test_json
  4231. ----+-------------------------------------------------------
  4232. 1 | {"a": [1, 2, 3], "another_key": null}
  4233. 2 | {"a": [1, 2, 3], "key": "value", "another_key": null}
  4234. 3 | {"a": 1}
  4235. (3 rows)
  4236. update test_jsonb_subscript set test_json = NULL where id = 3;
  4237. update test_jsonb_subscript set test_json[0] = '1';
  4238. select * from test_jsonb_subscript;
  4239. id | test_json
  4240. ----+---------------------------------------------------------------
  4241. 1 | {"0": 1, "a": [1, 2, 3], "another_key": null}
  4242. 2 | {"0": 1, "a": [1, 2, 3], "key": "value", "another_key": null}
  4243. 3 | [1]
  4244. (3 rows)
  4245. -- Fill the gaps logic
  4246. delete from test_jsonb_subscript;
  4247. insert into test_jsonb_subscript values (1, '[0]');
  4248. update test_jsonb_subscript set test_json[5] = '1';
  4249. select * from test_jsonb_subscript;
  4250. id | test_json
  4251. ----+--------------------------------
  4252. 1 | [0, null, null, null, null, 1]
  4253. (1 row)
  4254. update test_jsonb_subscript set test_json[-4] = '1';
  4255. select * from test_jsonb_subscript;
  4256. id | test_json
  4257. ----+-----------------------------
  4258. 1 | [0, null, 1, null, null, 1]
  4259. (1 row)
  4260. update test_jsonb_subscript set test_json[-8] = '1';
  4261. ERROR: path element at position 1 is out of range: -8
  4262. select * from test_jsonb_subscript;
  4263. id | test_json
  4264. ----+-----------------------------
  4265. 1 | [0, null, 1, null, null, 1]
  4266. (1 row)
  4267. -- keep consistent values position
  4268. delete from test_jsonb_subscript;
  4269. insert into test_jsonb_subscript values (1, '[]');
  4270. update test_jsonb_subscript set test_json[5] = '1';
  4271. select * from test_jsonb_subscript;
  4272. id | test_json
  4273. ----+-----------------------------------
  4274. 1 | [null, null, null, null, null, 1]
  4275. (1 row)
  4276. -- create the whole path
  4277. delete from test_jsonb_subscript;
  4278. insert into test_jsonb_subscript values (1, '{}');
  4279. update test_jsonb_subscript set test_json['a'][0]['b'][0]['c'] = '1';
  4280. select * from test_jsonb_subscript;
  4281. id | test_json
  4282. ----+----------------------------
  4283. 1 | {"a": [{"b": [{"c": 1}]}]}
  4284. (1 row)
  4285. delete from test_jsonb_subscript;
  4286. insert into test_jsonb_subscript values (1, '{}');
  4287. update test_jsonb_subscript set test_json['a'][2]['b'][2]['c'][2] = '1';
  4288. select * from test_jsonb_subscript;
  4289. id | test_json
  4290. ----+------------------------------------------------------------------
  4291. 1 | {"a": [null, null, {"b": [null, null, {"c": [null, null, 1]}]}]}
  4292. (1 row)
  4293. -- create the whole path with already existing keys
  4294. delete from test_jsonb_subscript;
  4295. insert into test_jsonb_subscript values (1, '{"b": 1}');
  4296. update test_jsonb_subscript set test_json['a'][0] = '2';
  4297. select * from test_jsonb_subscript;
  4298. id | test_json
  4299. ----+--------------------
  4300. 1 | {"a": [2], "b": 1}
  4301. (1 row)
  4302. -- the start jsonb is an object, first subscript is treated as a key
  4303. delete from test_jsonb_subscript;
  4304. insert into test_jsonb_subscript values (1, '{}');
  4305. update test_jsonb_subscript set test_json[0]['a'] = '1';
  4306. select * from test_jsonb_subscript;
  4307. id | test_json
  4308. ----+-----------------
  4309. 1 | {"0": {"a": 1}}
  4310. (1 row)
  4311. -- the start jsonb is an array
  4312. delete from test_jsonb_subscript;
  4313. insert into test_jsonb_subscript values (1, '[]');
  4314. update test_jsonb_subscript set test_json[0]['a'] = '1';
  4315. update test_jsonb_subscript set test_json[2]['b'] = '2';
  4316. select * from test_jsonb_subscript;
  4317. id | test_json
  4318. ----+----------------------------
  4319. 1 | [{"a": 1}, null, {"b": 2}]
  4320. (1 row)
  4321. -- overwriting an existing path
  4322. delete from test_jsonb_subscript;
  4323. insert into test_jsonb_subscript values (1, '{}');
  4324. update test_jsonb_subscript set test_json['a']['b'][1] = '1';
  4325. update test_jsonb_subscript set test_json['a']['b'][10] = '1';
  4326. select * from test_jsonb_subscript;
  4327. id | test_json
  4328. ----+----------------------------------------------------------------------------
  4329. 1 | {"a": {"b": [null, 1, null, null, null, null, null, null, null, null, 1]}}
  4330. (1 row)
  4331. delete from test_jsonb_subscript;
  4332. insert into test_jsonb_subscript values (1, '[]');
  4333. update test_jsonb_subscript set test_json[0][0][0] = '1';
  4334. update test_jsonb_subscript set test_json[0][0][1] = '1';
  4335. select * from test_jsonb_subscript;
  4336. id | test_json
  4337. ----+------------
  4338. 1 | [[[1, 1]]]
  4339. (1 row)
  4340. delete from test_jsonb_subscript;
  4341. insert into test_jsonb_subscript values (1, '{}');
  4342. update test_jsonb_subscript set test_json['a']['b'][10] = '1';
  4343. update test_jsonb_subscript set test_json['a'][10][10] = '1';
  4344. select * from test_jsonb_subscript;
  4345. id | test_json
  4346. ----+------------------------------------------------------------------------------------------------------------------------------------------------------
  4347. 1 | {"a": {"b": [null, null, null, null, null, null, null, null, null, null, 1], "10": [null, null, null, null, null, null, null, null, null, null, 1]}}
  4348. (1 row)
  4349. -- an empty sub element
  4350. delete from test_jsonb_subscript;
  4351. insert into test_jsonb_subscript values (1, '{"a": {}}');
  4352. update test_jsonb_subscript set test_json['a']['b']['c'][2] = '1';
  4353. select * from test_jsonb_subscript;
  4354. id | test_json
  4355. ----+--------------------------------------
  4356. 1 | {"a": {"b": {"c": [null, null, 1]}}}
  4357. (1 row)
  4358. delete from test_jsonb_subscript;
  4359. insert into test_jsonb_subscript values (1, '{"a": []}');
  4360. update test_jsonb_subscript set test_json['a'][1]['c'][2] = '1';
  4361. select * from test_jsonb_subscript;
  4362. id | test_json
  4363. ----+---------------------------------------
  4364. 1 | {"a": [null, {"c": [null, null, 1]}]}
  4365. (1 row)
  4366. -- trying replace assuming a composite object, but it's an element or a value
  4367. delete from test_jsonb_subscript;
  4368. insert into test_jsonb_subscript values (1, '{"a": 1}');
  4369. update test_jsonb_subscript set test_json['a']['b'] = '1';
  4370. ERROR: cannot replace existing key
  4371. DETAIL: The path assumes key is a composite object, but it is a scalar value.
  4372. update test_jsonb_subscript set test_json['a']['b']['c'] = '1';
  4373. ERROR: cannot replace existing key
  4374. DETAIL: The path assumes key is a composite object, but it is a scalar value.
  4375. update test_jsonb_subscript set test_json['a'][0] = '1';
  4376. ERROR: cannot replace existing key
  4377. DETAIL: The path assumes key is a composite object, but it is a scalar value.
  4378. update test_jsonb_subscript set test_json['a'][0]['c'] = '1';
  4379. ERROR: cannot replace existing key
  4380. DETAIL: The path assumes key is a composite object, but it is a scalar value.
  4381. update test_jsonb_subscript set test_json['a'][0][0] = '1';
  4382. ERROR: cannot replace existing key
  4383. DETAIL: The path assumes key is a composite object, but it is a scalar value.
  4384. -- trying replace assuming a composite object, but it's a raw scalar
  4385. delete from test_jsonb_subscript;
  4386. insert into test_jsonb_subscript values (1, 'null');
  4387. update test_jsonb_subscript set test_json[0] = '1';
  4388. ERROR: cannot replace existing key
  4389. DETAIL: The path assumes key is a composite object, but it is a scalar value.
  4390. update test_jsonb_subscript set test_json[0][0] = '1';
  4391. ERROR: cannot replace existing key
  4392. DETAIL: The path assumes key is a composite object, but it is a scalar value.
  4393. -- jsonb to tsvector
  4394. select to_tsvector('{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::jsonb);
  4395. to_tsvector
  4396. ---------------------------------------------------------------------------
  4397. 'aaa':1 'bbb':2 'ccc':4 'ddd':3 'eee':6 'fff':7 'ggg':8 'hhh':10 'iii':11
  4398. (1 row)
  4399. -- jsonb to tsvector with config
  4400. select to_tsvector('simple', '{"a": "aaa bbb ddd ccc", "b": ["eee fff ggg"], "c": {"d": "hhh iii"}}'::jsonb);
  4401. to_tsvector
  4402. ---------------------------------------------------------------------------
  4403. 'aaa':1 'bbb':2 'ccc':4 'ddd':3 'eee':6 'fff':7 'ggg':8 'hhh':10 'iii':11
  4404. (1 row)
  4405. -- jsonb to tsvector with stop words
  4406. select to_tsvector('english', '{"a": "aaa in bbb ddd ccc", "b": ["the eee fff ggg"], "c": {"d": "hhh. iii"}}'::jsonb);
  4407. to_tsvector
  4408. ----------------------------------------------------------------------------
  4409. 'aaa':1 'bbb':3 'ccc':5 'ddd':4 'eee':8 'fff':9 'ggg':10 'hhh':12 'iii':13
  4410. (1 row)
  4411. -- jsonb to tsvector with numeric values
  4412. select to_tsvector('english', '{"a": "aaa in bbb ddd ccc", "b": 123, "c": 456}'::jsonb);
  4413. to_tsvector
  4414. ---------------------------------
  4415. 'aaa':1 'bbb':3 'ccc':5 'ddd':4
  4416. (1 row)
  4417. -- jsonb_to_tsvector
  4418. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"all"');
  4419. jsonb_to_tsvector
  4420. ----------------------------------------------------------------------------------------
  4421. '123':8 '456':12 'aaa':2 'b':6 'bbb':4 'c':10 'd':14 'f':18 'fals':20 'g':22 'true':16
  4422. (1 row)
  4423. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"key"');
  4424. jsonb_to_tsvector
  4425. --------------------------------
  4426. 'b':2 'c':4 'd':6 'f':8 'g':10
  4427. (1 row)
  4428. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"string"');
  4429. jsonb_to_tsvector
  4430. -------------------
  4431. 'aaa':1 'bbb':3
  4432. (1 row)
  4433. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"numeric"');
  4434. jsonb_to_tsvector
  4435. -------------------
  4436. '123':1 '456':3
  4437. (1 row)
  4438. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"boolean"');
  4439. jsonb_to_tsvector
  4440. -------------------
  4441. 'fals':3 'true':1
  4442. (1 row)
  4443. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '["string", "numeric"]');
  4444. jsonb_to_tsvector
  4445. ---------------------------------
  4446. '123':5 '456':7 'aaa':1 'bbb':3
  4447. (1 row)
  4448. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"all"');
  4449. jsonb_to_tsvector
  4450. ----------------------------------------------------------------------------------------
  4451. '123':8 '456':12 'aaa':2 'b':6 'bbb':4 'c':10 'd':14 'f':18 'fals':20 'g':22 'true':16
  4452. (1 row)
  4453. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"key"');
  4454. jsonb_to_tsvector
  4455. --------------------------------
  4456. 'b':2 'c':4 'd':6 'f':8 'g':10
  4457. (1 row)
  4458. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"string"');
  4459. jsonb_to_tsvector
  4460. -------------------
  4461. 'aaa':1 'bbb':3
  4462. (1 row)
  4463. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"numeric"');
  4464. jsonb_to_tsvector
  4465. -------------------
  4466. '123':1 '456':3
  4467. (1 row)
  4468. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '"boolean"');
  4469. jsonb_to_tsvector
  4470. -------------------
  4471. 'fals':3 'true':1
  4472. (1 row)
  4473. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '["string", "numeric"]');
  4474. jsonb_to_tsvector
  4475. ---------------------------------
  4476. '123':5 '456':7 'aaa':1 'bbb':3
  4477. (1 row)
  4478. -- to_tsvector corner cases
  4479. select to_tsvector('""'::jsonb);
  4480. to_tsvector
  4481. -------------
  4482. (1 row)
  4483. select to_tsvector('{}'::jsonb);
  4484. to_tsvector
  4485. -------------
  4486. (1 row)
  4487. select to_tsvector('[]'::jsonb);
  4488. to_tsvector
  4489. -------------
  4490. (1 row)
  4491. select to_tsvector('null'::jsonb);
  4492. to_tsvector
  4493. -------------
  4494. (1 row)
  4495. -- jsonb_to_tsvector corner cases
  4496. select jsonb_to_tsvector('""'::jsonb, '"all"');
  4497. jsonb_to_tsvector
  4498. -------------------
  4499. (1 row)
  4500. select jsonb_to_tsvector('{}'::jsonb, '"all"');
  4501. jsonb_to_tsvector
  4502. -------------------
  4503. (1 row)
  4504. select jsonb_to_tsvector('[]'::jsonb, '"all"');
  4505. jsonb_to_tsvector
  4506. -------------------
  4507. (1 row)
  4508. select jsonb_to_tsvector('null'::jsonb, '"all"');
  4509. jsonb_to_tsvector
  4510. -------------------
  4511. (1 row)
  4512. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '""');
  4513. ERROR: wrong flag in flag array: ""
  4514. HINT: Possible values are: "string", "numeric", "boolean", "key", and "all".
  4515. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '{}');
  4516. ERROR: wrong flag type, only arrays and scalars are allowed
  4517. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '[]');
  4518. jsonb_to_tsvector
  4519. -------------------
  4520. (1 row)
  4521. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, 'null');
  4522. ERROR: flag array element is not a string
  4523. HINT: Possible values are: "string", "numeric", "boolean", "key", and "all".
  4524. select jsonb_to_tsvector('english', '{"a": "aaa in bbb", "b": 123, "c": 456, "d": true, "f": false, "g": null}'::jsonb, '["all", null]');
  4525. ERROR: flag array element is not a string
  4526. HINT: Possible values are: "string", "numeric", "boolean", "key", and "all".
  4527. -- ts_headline for jsonb
  4528. select ts_headline('{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::jsonb, tsquery('bbb & ddd & hhh'));
  4529. ts_headline
  4530. ------------------------------------------------------------------------------------------------------------------
  4531. {"a": "aaa <b>bbb</b>", "b": {"c": "ccc <b>ddd</b> fff", "c1": "ccc1 ddd1"}, "d": ["ggg <b>hhh</b>", "iii jjj"]}
  4532. (1 row)
  4533. select ts_headline('english', '{"a": "aaa bbb", "b": {"c": "ccc ddd fff"}, "d": ["ggg hhh", "iii jjj"]}'::jsonb, tsquery('bbb & ddd & hhh'));
  4534. ts_headline
  4535. -----------------------------------------------------------------------------------------------
  4536. {"a": "aaa <b>bbb</b>", "b": {"c": "ccc <b>ddd</b> fff"}, "d": ["ggg <b>hhh</b>", "iii jjj"]}
  4537. (1 row)
  4538. select ts_headline('{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::jsonb, tsquery('bbb & ddd & hhh'), 'StartSel = <, StopSel = >');
  4539. ts_headline
  4540. ---------------------------------------------------------------------------------------------------
  4541. {"a": "aaa <bbb>", "b": {"c": "ccc <ddd> fff", "c1": "ccc1 ddd1"}, "d": ["ggg <hhh>", "iii jjj"]}
  4542. (1 row)
  4543. select ts_headline('english', '{"a": "aaa bbb", "b": {"c": "ccc ddd fff", "c1": "ccc1 ddd1"}, "d": ["ggg hhh", "iii jjj"]}'::jsonb, tsquery('bbb & ddd & hhh'), 'StartSel = <, StopSel = >');
  4544. ts_headline
  4545. ---------------------------------------------------------------------------------------------------
  4546. {"a": "aaa <bbb>", "b": {"c": "ccc <ddd> fff", "c1": "ccc1 ddd1"}, "d": ["ggg <hhh>", "iii jjj"]}
  4547. (1 row)
  4548. -- corner cases for ts_headline with jsonb
  4549. select ts_headline('null'::jsonb, tsquery('aaa & bbb'));
  4550. ts_headline
  4551. -------------
  4552. null
  4553. (1 row)
  4554. select ts_headline('{}'::jsonb, tsquery('aaa & bbb'));
  4555. ts_headline
  4556. -------------
  4557. {}
  4558. (1 row)
  4559. select ts_headline('[]'::jsonb, tsquery('aaa & bbb'));
  4560. ts_headline
  4561. -------------
  4562. []
  4563. (1 row)
  4564. -- casts
  4565. select 'true'::jsonb::bool;
  4566. bool
  4567. ------
  4568. t
  4569. (1 row)
  4570. select '[]'::jsonb::bool;
  4571. ERROR: cannot cast jsonb array to type boolean
  4572. select '1.0'::jsonb::float;
  4573. float8
  4574. --------
  4575. 1
  4576. (1 row)
  4577. select '[1.0]'::jsonb::float;
  4578. ERROR: cannot cast jsonb array to type double precision
  4579. select '12345'::jsonb::int4;
  4580. int4
  4581. -------
  4582. 12345
  4583. (1 row)
  4584. select '"hello"'::jsonb::int4;
  4585. ERROR: cannot cast jsonb string to type integer
  4586. select '12345'::jsonb::numeric;
  4587. numeric
  4588. ---------
  4589. 12345
  4590. (1 row)
  4591. select '{}'::jsonb::numeric;
  4592. ERROR: cannot cast jsonb object to type numeric
  4593. select '12345.05'::jsonb::numeric;
  4594. numeric
  4595. ----------
  4596. 12345.05
  4597. (1 row)
  4598. select '12345.05'::jsonb::float4;
  4599. float4
  4600. ----------
  4601. 12345.05
  4602. (1 row)
  4603. select '12345.05'::jsonb::float8;
  4604. float8
  4605. ----------
  4606. 12345.05
  4607. (1 row)
  4608. select '12345.05'::jsonb::int2;
  4609. int2
  4610. -------
  4611. 12345
  4612. (1 row)
  4613. select '12345.05'::jsonb::int4;
  4614. int4
  4615. -------
  4616. 12345
  4617. (1 row)
  4618. select '12345.05'::jsonb::int8;
  4619. int8
  4620. -------
  4621. 12345
  4622. (1 row)
  4623. select '12345.0000000000000000000000000000000000000000000005'::jsonb::numeric;
  4624. numeric
  4625. ------------------------------------------------------
  4626. 12345.0000000000000000000000000000000000000000000005
  4627. (1 row)
  4628. select '12345.0000000000000000000000000000000000000000000005'::jsonb::float4;
  4629. float4
  4630. --------
  4631. 12345
  4632. (1 row)
  4633. select '12345.0000000000000000000000000000000000000000000005'::jsonb::float8;
  4634. float8
  4635. --------
  4636. 12345
  4637. (1 row)
  4638. select '12345.0000000000000000000000000000000000000000000005'::jsonb::int2;
  4639. int2
  4640. -------
  4641. 12345
  4642. (1 row)
  4643. select '12345.0000000000000000000000000000000000000000000005'::jsonb::int4;
  4644. int4
  4645. -------
  4646. 12345
  4647. (1 row)
  4648. select '12345.0000000000000000000000000000000000000000000005'::jsonb::int8;
  4649. int8
  4650. -------
  4651. 12345
  4652. (1 row)