nested hstore
[hstore.git] / expected / hstore.out
1 CREATE EXTENSION hstore;
2 set escape_string_warning=off;
3 --hstore;
4 select ''::hstore;
5  hstore 
6 --------
7  
8 (1 row)
9
10 select 'a=>b'::hstore;
11   hstore  
12 ----------
13  "a"=>"b"
14 (1 row)
15
16 select ' a=>b'::hstore;
17   hstore  
18 ----------
19  "a"=>"b"
20 (1 row)
21
22 select 'a =>b'::hstore;
23   hstore  
24 ----------
25  "a"=>"b"
26 (1 row)
27
28 select 'a=>b '::hstore;
29   hstore  
30 ----------
31  "a"=>"b"
32 (1 row)
33
34 select 'a=> b'::hstore;
35   hstore  
36 ----------
37  "a"=>"b"
38 (1 row)
39
40 select '"a"=>"b"'::hstore;
41   hstore  
42 ----------
43  "a"=>"b"
44 (1 row)
45
46 select ' "a"=>"b"'::hstore;
47   hstore  
48 ----------
49  "a"=>"b"
50 (1 row)
51
52 select '"a" =>"b"'::hstore;
53   hstore  
54 ----------
55  "a"=>"b"
56 (1 row)
57
58 select '"a"=>"b" '::hstore;
59   hstore  
60 ----------
61  "a"=>"b"
62 (1 row)
63
64 select '"a"=> "b"'::hstore;
65   hstore  
66 ----------
67  "a"=>"b"
68 (1 row)
69
70 select 'aa=>bb'::hstore;
71    hstore   
72 ------------
73  "aa"=>"bb"
74 (1 row)
75
76 select ' aa=>bb'::hstore;
77    hstore   
78 ------------
79  "aa"=>"bb"
80 (1 row)
81
82 select 'aa =>bb'::hstore;
83    hstore   
84 ------------
85  "aa"=>"bb"
86 (1 row)
87
88 select 'aa=>bb '::hstore;
89    hstore   
90 ------------
91  "aa"=>"bb"
92 (1 row)
93
94 select 'aa=> bb'::hstore;
95    hstore   
96 ------------
97  "aa"=>"bb"
98 (1 row)
99
100 select '"aa"=>"bb"'::hstore;
101    hstore   
102 ------------
103  "aa"=>"bb"
104 (1 row)
105
106 select ' "aa"=>"bb"'::hstore;
107    hstore   
108 ------------
109  "aa"=>"bb"
110 (1 row)
111
112 select '"aa" =>"bb"'::hstore;
113    hstore   
114 ------------
115  "aa"=>"bb"
116 (1 row)
117
118 select '"aa"=>"bb" '::hstore;
119    hstore   
120 ------------
121  "aa"=>"bb"
122 (1 row)
123
124 select '"aa"=> "bb"'::hstore;
125    hstore   
126 ------------
127  "aa"=>"bb"
128 (1 row)
129
130 select 'aa=>bb, cc=>dd'::hstore;
131          hstore         
132 ------------------------
133  "aa"=>"bb", "cc"=>"dd"
134 (1 row)
135
136 select 'aa=>bb , cc=>dd'::hstore;
137          hstore         
138 ------------------------
139  "aa"=>"bb", "cc"=>"dd"
140 (1 row)
141
142 select 'aa=>bb ,cc=>dd'::hstore;
143          hstore         
144 ------------------------
145  "aa"=>"bb", "cc"=>"dd"
146 (1 row)
147
148 select 'aa=>bb, "cc"=>dd'::hstore;
149          hstore         
150 ------------------------
151  "aa"=>"bb", "cc"=>"dd"
152 (1 row)
153
154 select 'aa=>bb , "cc"=>dd'::hstore;
155          hstore         
156 ------------------------
157  "aa"=>"bb", "cc"=>"dd"
158 (1 row)
159
160 select 'aa=>bb ,"cc"=>dd'::hstore;
161          hstore         
162 ------------------------
163  "aa"=>"bb", "cc"=>"dd"
164 (1 row)
165
166 select 'aa=>"bb", cc=>dd'::hstore;
167          hstore         
168 ------------------------
169  "aa"=>"bb", "cc"=>"dd"
170 (1 row)
171
172 select 'aa=>"bb" , cc=>dd'::hstore;
173          hstore         
174 ------------------------
175  "aa"=>"bb", "cc"=>"dd"
176 (1 row)
177
178 select 'aa=>"bb" ,cc=>dd'::hstore;
179          hstore         
180 ------------------------
181  "aa"=>"bb", "cc"=>"dd"
182 (1 row)
183
184 select 'aa=>null'::hstore;
185    hstore   
186 ------------
187  "aa"=>NULL
188 (1 row)
189
190 select 'aa=>NuLl'::hstore;
191    hstore   
192 ------------
193  "aa"=>NULL
194 (1 row)
195
196 select 'aa=>"NuLl"'::hstore;
197     hstore    
198 --------------
199  "aa"=>"NuLl"
200 (1 row)
201
202 select 'aa=>nul'::hstore;
203    hstore    
204 -------------
205  "aa"=>"nul"
206 (1 row)
207
208 select 'aa=>NuL'::hstore;
209    hstore    
210 -------------
211  "aa"=>"NuL"
212 (1 row)
213
214 select 'aa=>"NuL"'::hstore;
215    hstore    
216 -------------
217  "aa"=>"NuL"
218 (1 row)
219
220 select e'\\=a=>q=w'::hstore;
221    hstore    
222 -------------
223  "=a"=>"q=w"
224 (1 row)
225
226 select e'"=a"=>q\\=w'::hstore;
227    hstore    
228 -------------
229  "=a"=>"q=w"
230 (1 row)
231
232 select e'"\\"a"=>q>w'::hstore;
233     hstore    
234 --------------
235  "\"a"=>"q>w"
236 (1 row)
237
238 select e'\\"a=>q"w'::hstore;
239     hstore     
240 ---------------
241  "\"a"=>"q\"w"
242 (1 row)
243
244 select ''::hstore;
245  hstore 
246 --------
247  
248 (1 row)
249
250 select '        '::hstore;
251  hstore 
252 --------
253  
254 (1 row)
255
256 -- -> operator
257 select 'aa=>b, c=>d , b=>16'::hstore->'c';
258  ?column? 
259 ----------
260  d
261 (1 row)
262
263 select 'aa=>b, c=>d , b=>16'::hstore->'b';
264  ?column? 
265 ----------
266  16
267 (1 row)
268
269 select 'aa=>b, c=>d , b=>16'::hstore->'aa';
270  ?column? 
271 ----------
272  b
273 (1 row)
274
275 select ('aa=>b, c=>d , b=>16'::hstore->'gg') is null;
276  ?column? 
277 ----------
278  t
279 (1 row)
280
281 select ('aa=>NULL, c=>d , b=>16'::hstore->'aa') is null;
282  ?column? 
283 ----------
284  t
285 (1 row)
286
287 select ('aa=>"NULL", c=>d , b=>16'::hstore->'aa') is null;
288  ?column? 
289 ----------
290  f
291 (1 row)
292
293 -- -> array operator
294 select 'aa=>"NULL", c=>d , b=>16'::hstore -> ARRAY['aa','c'];
295   ?column?  
296 ------------
297  {"NULL",d}
298 (1 row)
299
300 select 'aa=>"NULL", c=>d , b=>16'::hstore -> ARRAY['c','aa'];
301   ?column?  
302 ------------
303  {d,"NULL"}
304 (1 row)
305
306 select 'aa=>NULL, c=>d , b=>16'::hstore -> ARRAY['aa','c',null];
307    ?column?    
308 ---------------
309  {NULL,d,NULL}
310 (1 row)
311
312 select 'aa=>1, c=>3, b=>2, d=>4'::hstore -> ARRAY[['b','d'],['aa','c']];
313    ?column?    
314 ---------------
315  {{2,4},{1,3}}
316 (1 row)
317
318 -- exists/defined
319 select exist('a=>NULL, b=>qq', 'a');
320  exist 
321 -------
322  t
323 (1 row)
324
325 select exist('a=>NULL, b=>qq', 'b');
326  exist 
327 -------
328  t
329 (1 row)
330
331 select exist('a=>NULL, b=>qq', 'c');
332  exist 
333 -------
334  f
335 (1 row)
336
337 select exist('a=>"NULL", b=>qq', 'a');
338  exist 
339 -------
340  t
341 (1 row)
342
343 select defined('a=>NULL, b=>qq', 'a');
344  defined 
345 ---------
346  f
347 (1 row)
348
349 select defined('a=>NULL, b=>qq', 'b');
350  defined 
351 ---------
352  t
353 (1 row)
354
355 select defined('a=>NULL, b=>qq', 'c');
356  defined 
357 ---------
358  f
359 (1 row)
360
361 select defined('a=>"NULL", b=>qq', 'a');
362  defined 
363 ---------
364  t
365 (1 row)
366
367 select hstore 'a=>NULL, b=>qq' ? 'a';
368  ?column? 
369 ----------
370  t
371 (1 row)
372
373 select hstore 'a=>NULL, b=>qq' ? 'b';
374  ?column? 
375 ----------
376  t
377 (1 row)
378
379 select hstore 'a=>NULL, b=>qq' ? 'c';
380  ?column? 
381 ----------
382  f
383 (1 row)
384
385 select hstore 'a=>"NULL", b=>qq' ? 'a';
386  ?column? 
387 ----------
388  t
389 (1 row)
390
391 select hstore 'a=>NULL, b=>qq' ?| ARRAY['a','b'];
392  ?column? 
393 ----------
394  t
395 (1 row)
396
397 select hstore 'a=>NULL, b=>qq' ?| ARRAY['b','a'];
398  ?column? 
399 ----------
400  t
401 (1 row)
402
403 select hstore 'a=>NULL, b=>qq' ?| ARRAY['c','a'];
404  ?column? 
405 ----------
406  t
407 (1 row)
408
409 select hstore 'a=>NULL, b=>qq' ?| ARRAY['c','d'];
410  ?column? 
411 ----------
412  f
413 (1 row)
414
415 select hstore 'a=>NULL, b=>qq' ?| '{}'::text[];
416  ?column? 
417 ----------
418  f
419 (1 row)
420
421 select hstore 'a=>NULL, b=>qq' ?& ARRAY['a','b'];
422  ?column? 
423 ----------
424  t
425 (1 row)
426
427 select hstore 'a=>NULL, b=>qq' ?& ARRAY['b','a'];
428  ?column? 
429 ----------
430  t
431 (1 row)
432
433 select hstore 'a=>NULL, b=>qq' ?& ARRAY['c','a'];
434  ?column? 
435 ----------
436  f
437 (1 row)
438
439 select hstore 'a=>NULL, b=>qq' ?& ARRAY['c','d'];
440  ?column? 
441 ----------
442  f
443 (1 row)
444
445 select hstore 'a=>NULL, b=>qq' ?& '{}'::text[];
446  ?column? 
447 ----------
448  t
449 (1 row)
450
451 -- delete
452 select delete('a=>1 , b=>2, c=>3'::hstore, 'a');
453      delete     
454 ----------------
455  "b"=>2, "c"=>3
456 (1 row)
457
458 select delete('a=>null , b=>2, c=>3'::hstore, 'a');
459      delete     
460 ----------------
461  "b"=>2, "c"=>3
462 (1 row)
463
464 select delete('a=>1 , b=>2, c=>3'::hstore, 'b');
465      delete     
466 ----------------
467  "a"=>1, "c"=>3
468 (1 row)
469
470 select delete('a=>1 , b=>2, c=>3'::hstore, 'c');
471      delete     
472 ----------------
473  "a"=>1, "b"=>2
474 (1 row)
475
476 select delete('a=>1 , b=>2, c=>3'::hstore, 'd');
477          delete         
478 ------------------------
479  "a"=>1, "b"=>2, "c"=>3
480 (1 row)
481
482 select 'a=>1 , b=>2, c=>3'::hstore - 'a'::text;
483     ?column?    
484 ----------------
485  "b"=>2, "c"=>3
486 (1 row)
487
488 select 'a=>null , b=>2, c=>3'::hstore - 'a'::text;
489     ?column?    
490 ----------------
491  "b"=>2, "c"=>3
492 (1 row)
493
494 select 'a=>1 , b=>2, c=>3'::hstore - 'b'::text;
495     ?column?    
496 ----------------
497  "a"=>1, "c"=>3
498 (1 row)
499
500 select 'a=>1 , b=>2, c=>3'::hstore - 'c'::text;
501     ?column?    
502 ----------------
503  "a"=>1, "b"=>2
504 (1 row)
505
506 select 'a=>1 , b=>2, c=>3'::hstore - 'd'::text;
507         ?column?        
508 ------------------------
509  "a"=>1, "b"=>2, "c"=>3
510 (1 row)
511
512 select pg_column_size('a=>1 , b=>2, c=>3'::hstore - 'b'::text)
513          = pg_column_size('a=>1, b=>2'::hstore);
514  ?column? 
515 ----------
516  t
517 (1 row)
518
519 -- delete (array)
520 select delete('a=>1 , b=>2, c=>3'::hstore, ARRAY['d','e']);
521          delete         
522 ------------------------
523  "a"=>1, "b"=>2, "c"=>3
524 (1 row)
525
526 select delete('a=>1 , b=>2, c=>3'::hstore, ARRAY['d','b']);
527      delete     
528 ----------------
529  "a"=>1, "c"=>3
530 (1 row)
531
532 select delete('a=>1 , b=>2, c=>3'::hstore, ARRAY['a','c']);
533  delete 
534 --------
535  "b"=>2
536 (1 row)
537
538 select delete('a=>1 , b=>2, c=>3'::hstore, ARRAY[['b'],['c'],['a']]);
539  delete 
540 --------
541  
542 (1 row)
543
544 select delete('a=>1 , b=>2, c=>3'::hstore, '{}'::text[]);
545          delete         
546 ------------------------
547  "a"=>1, "b"=>2, "c"=>3
548 (1 row)
549
550 select 'a=>1 , b=>2, c=>3'::hstore - ARRAY['d','e'];
551         ?column?        
552 ------------------------
553  "a"=>1, "b"=>2, "c"=>3
554 (1 row)
555
556 select 'a=>1 , b=>2, c=>3'::hstore - ARRAY['d','b'];
557     ?column?    
558 ----------------
559  "a"=>1, "c"=>3
560 (1 row)
561
562 select 'a=>1 , b=>2, c=>3'::hstore - ARRAY['a','c'];
563  ?column? 
564 ----------
565  "b"=>2
566 (1 row)
567
568 select 'a=>1 , b=>2, c=>3'::hstore - ARRAY[['b'],['c'],['a']];
569  ?column? 
570 ----------
571  
572 (1 row)
573
574 select 'a=>1 , b=>2, c=>3'::hstore - '{}'::text[];
575         ?column?        
576 ------------------------
577  "a"=>1, "b"=>2, "c"=>3
578 (1 row)
579
580 select pg_column_size('a=>1 , b=>2, c=>3'::hstore - ARRAY['a','c'])
581          = pg_column_size('b=>2'::hstore);
582  ?column? 
583 ----------
584  t
585 (1 row)
586
587 select pg_column_size('a=>1 , b=>2, c=>3'::hstore - '{}'::text[])
588          = pg_column_size('a=>1, b=>2, c=>3'::hstore);
589  ?column? 
590 ----------
591  t
592 (1 row)
593
594 -- delete (hstore)
595 select delete('aa=>1 , b=>2, c=>3'::hstore, 'aa=>4, b=>2'::hstore);
596      delete      
597 -----------------
598  "c"=>3, "aa"=>1
599 (1 row)
600
601 select delete('aa=>1 , b=>2, c=>3'::hstore, 'aa=>NULL, c=>3'::hstore);
602      delete      
603 -----------------
604  "b"=>2, "aa"=>1
605 (1 row)
606
607 select delete('aa=>1 , b=>2, c=>3'::hstore, 'aa=>1, b=>2, c=>3'::hstore);
608  delete 
609 --------
610  
611 (1 row)
612
613 select delete('aa=>1 , b=>2, c=>3'::hstore, 'b=>2'::hstore);
614      delete      
615 -----------------
616  "c"=>3, "aa"=>1
617 (1 row)
618
619 select delete('aa=>1 , b=>2, c=>3'::hstore, ''::hstore);
620          delete          
621 -------------------------
622  "b"=>2, "c"=>3, "aa"=>1
623 (1 row)
624
625 select 'aa=>1 , b=>2, c=>3'::hstore - 'aa=>4, b=>2'::hstore;
626     ?column?     
627 -----------------
628  "c"=>3, "aa"=>1
629 (1 row)
630
631 select 'aa=>1 , b=>2, c=>3'::hstore - 'aa=>NULL, c=>3'::hstore;
632     ?column?     
633 -----------------
634  "b"=>2, "aa"=>1
635 (1 row)
636
637 select 'aa=>1 , b=>2, c=>3'::hstore - 'aa=>1, b=>2, c=>3'::hstore;
638  ?column? 
639 ----------
640  
641 (1 row)
642
643 select 'aa=>1 , b=>2, c=>3'::hstore - 'b=>2'::hstore;
644     ?column?     
645 -----------------
646  "c"=>3, "aa"=>1
647 (1 row)
648
649 select 'aa=>1 , b=>2, c=>3'::hstore - ''::hstore;
650         ?column?         
651 -------------------------
652  "b"=>2, "c"=>3, "aa"=>1
653 (1 row)
654
655 select pg_column_size('a=>1 , b=>2, c=>3'::hstore - 'b=>2'::hstore)
656          = pg_column_size('a=>1, c=>3'::hstore);
657  ?column? 
658 ----------
659  t
660 (1 row)
661
662 select pg_column_size('a=>1 , b=>2, c=>3'::hstore - ''::hstore)
663          = pg_column_size('a=>1, b=>2, c=>3'::hstore);
664  ?column? 
665 ----------
666  t
667 (1 row)
668
669 -- ||
670 select 'aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f';
671                ?column?                
672 ---------------------------------------
673  "b"=>"g", "aa"=>1, "cq"=>"l", "fg"=>f
674 (1 row)
675
676 select 'aa=>1 , b=>2, cq=>3'::hstore || 'aq=>l';
677               ?column?               
678 -------------------------------------
679  "b"=>2, "aa"=>1, "aq"=>"l", "cq"=>3
680 (1 row)
681
682 select 'aa=>1 , b=>2, cq=>3'::hstore || 'aa=>l';
683           ?column?          
684 ----------------------------
685  "b"=>2, "aa"=>"l", "cq"=>3
686 (1 row)
687
688 select 'aa=>1 , b=>2, cq=>3'::hstore || '';
689          ?column?         
690 --------------------------
691  "b"=>2, "aa"=>1, "cq"=>3
692 (1 row)
693
694 select ''::hstore || 'cq=>l, b=>g, fg=>f';
695            ?column?           
696 ------------------------------
697  "b"=>"g", "cq"=>"l", "fg"=>f
698 (1 row)
699
700 select pg_column_size(''::hstore || ''::hstore) = pg_column_size(''::hstore);
701  ?column? 
702 ----------
703  t
704 (1 row)
705
706 select pg_column_size('aa=>1'::hstore || 'b=>2'::hstore)
707          = pg_column_size('aa=>1, b=>2'::hstore);
708  ?column? 
709 ----------
710  t
711 (1 row)
712
713 select pg_column_size('aa=>1, b=>2'::hstore || ''::hstore)
714          = pg_column_size('aa=>1, b=>2'::hstore);
715  ?column? 
716 ----------
717  t
718 (1 row)
719
720 select pg_column_size(''::hstore || 'aa=>1, b=>2'::hstore)
721          = pg_column_size('aa=>1, b=>2'::hstore);
722  ?column? 
723 ----------
724  t
725 (1 row)
726
727 -- hstore(text,text)
728 select 'a=>g, b=>c'::hstore || hstore('asd', 'gf');
729             ?column?             
730 ---------------------------------
731  "a"=>"g", "b"=>"c", "asd"=>"gf"
732 (1 row)
733
734 select 'a=>g, b=>c'::hstore || hstore('b', 'gf');
735       ?column?       
736 ---------------------
737  "a"=>"g", "b"=>"gf"
738 (1 row)
739
740 select 'a=>g, b=>c'::hstore || hstore('b', 'NULL');
741        ?column?        
742 -----------------------
743  "a"=>"g", "b"=>"NULL"
744 (1 row)
745
746 select 'a=>g, b=>c'::hstore || hstore('b', NULL);
747       ?column?       
748 ---------------------
749  "a"=>"g", "b"=>NULL
750 (1 row)
751
752 select ('a=>g, b=>c'::hstore || hstore(NULL, 'b')) is null;
753  ?column? 
754 ----------
755  t
756 (1 row)
757
758 select pg_column_size(hstore('b', 'gf'))
759          = pg_column_size('b=>gf'::hstore);
760  ?column? 
761 ----------
762  t
763 (1 row)
764
765 select pg_column_size('a=>g, b=>c'::hstore || hstore('b', 'gf'))
766          = pg_column_size('a=>g, b=>gf'::hstore);
767  ?column? 
768 ----------
769  t
770 (1 row)
771
772 -- slice()
773 select slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['g','h','i']);
774  slice 
775 -------
776  
777 (1 row)
778
779 select slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['c','b']);
780      slice      
781 ----------------
782  "b"=>2, "c"=>3
783 (1 row)
784
785 select slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['aa','b']);
786       slice      
787 -----------------
788  "b"=>2, "aa"=>1
789 (1 row)
790
791 select slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['c','b','aa']);
792           slice          
793 -------------------------
794  "b"=>2, "c"=>3, "aa"=>1
795 (1 row)
796
797 select pg_column_size(slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['c','b']))
798          = pg_column_size('b=>2, c=>3'::hstore);
799  ?column? 
800 ----------
801  t
802 (1 row)
803
804 select pg_column_size(slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['c','b','aa']))
805          = pg_column_size('aa=>1, b=>2, c=>3'::hstore);
806  ?column? 
807 ----------
808  t
809 (1 row)
810
811 -- array input
812 select '{}'::text[]::hstore;
813  hstore 
814 --------
815  
816 (1 row)
817
818 select ARRAY['a','g','b','h','asd']::hstore;
819 ERROR:  array must have even number of elements
820 select ARRAY['a','g','b','h','asd','i']::hstore;
821              array              
822 --------------------------------
823  "a"=>"g", "b"=>"h", "asd"=>"i"
824 (1 row)
825
826 select ARRAY[['a','g'],['b','h'],['asd','i']]::hstore;
827              array              
828 --------------------------------
829  "a"=>"g", "b"=>"h", "asd"=>"i"
830 (1 row)
831
832 select ARRAY[['a','g','b'],['h','asd','i']]::hstore;
833 ERROR:  array must have two columns
834 select ARRAY[[['a','g'],['b','h'],['asd','i']]]::hstore;
835 ERROR:  wrong number of array subscripts
836 select hstore('{}'::text[]);
837  hstore 
838 --------
839  
840 (1 row)
841
842 select hstore(ARRAY['a','g','b','h','asd']);
843 ERROR:  array must have even number of elements
844 select hstore(ARRAY['a','g','b','h','asd','i']);
845              hstore             
846 --------------------------------
847  "a"=>"g", "b"=>"h", "asd"=>"i"
848 (1 row)
849
850 select hstore(ARRAY[['a','g'],['b','h'],['asd','i']]);
851              hstore             
852 --------------------------------
853  "a"=>"g", "b"=>"h", "asd"=>"i"
854 (1 row)
855
856 select hstore(ARRAY[['a','g','b'],['h','asd','i']]);
857 ERROR:  array must have two columns
858 select hstore(ARRAY[[['a','g'],['b','h'],['asd','i']]]);
859 ERROR:  wrong number of array subscripts
860 select hstore('[0:5]={a,g,b,h,asd,i}'::text[]);
861              hstore             
862 --------------------------------
863  "a"=>"g", "b"=>"h", "asd"=>"i"
864 (1 row)
865
866 select hstore('[0:2][1:2]={{a,g},{b,h},{asd,i}}'::text[]);
867              hstore             
868 --------------------------------
869  "a"=>"g", "b"=>"h", "asd"=>"i"
870 (1 row)
871
872 -- pairs of arrays
873 select hstore(ARRAY['a','b','asd'], ARRAY['g','h','i']);
874              hstore             
875 --------------------------------
876  "a"=>"g", "b"=>"h", "asd"=>"i"
877 (1 row)
878
879 select hstore(ARRAY['a','b','asd'], ARRAY['g','h',NULL]);
880              hstore              
881 ---------------------------------
882  "a"=>"g", "b"=>"h", "asd"=>NULL
883 (1 row)
884
885 select hstore(ARRAY['z','y','x'], ARRAY['1','2','3']);
886             hstore            
887 ------------------------------
888  "x"=>"3", "y"=>"2", "z"=>"1"
889 (1 row)
890
891 select hstore(ARRAY['aaa','bb','c','d'], ARRAY[null::text,null,null,null]);
892                     hstore                     
893 -----------------------------------------------
894  "c"=>NULL, "d"=>NULL, "bb"=>NULL, "aaa"=>NULL
895 (1 row)
896
897 select hstore(ARRAY['aaa','bb','c','d'], null);
898                     hstore                     
899 -----------------------------------------------
900  "c"=>NULL, "d"=>NULL, "bb"=>NULL, "aaa"=>NULL
901 (1 row)
902
903 select quote_literal(hstore('{}'::text[], '{}'::text[]));
904  quote_literal 
905 ---------------
906  ''
907 (1 row)
908
909 select quote_literal(hstore('{}'::text[], null));
910  quote_literal 
911 ---------------
912  ''
913 (1 row)
914
915 select hstore(ARRAY['a'], '{}'::text[]);  -- error
916 ERROR:  arrays must have same bounds
917 select hstore('{}'::text[], ARRAY['a']);  -- error
918 ERROR:  arrays must have same bounds
919 select pg_column_size(hstore(ARRAY['a','b','asd'], ARRAY['g','h','i']))
920          = pg_column_size('a=>g, b=>h, asd=>i'::hstore);
921  ?column? 
922 ----------
923  t
924 (1 row)
925
926 -- records
927 select hstore(v) from (values (1, 'foo', 1.2, 3::float8)) v(a,b,c,d);
928                 hstore                
929 --------------------------------------
930  "a"=>1, "b"=>"foo", "c"=>1.2, "d"=>3
931 (1 row)
932
933 create domain hstestdom1 as integer not null default 0;
934 create table testhstore0 (a integer, b text, c numeric, d float8);
935 create table testhstore1 (a integer, b text, c numeric, d float8, e hstestdom1);
936 insert into testhstore0 values (1, 'foo', 1.2, 3::float8);
937 insert into testhstore1 values (1, 'foo', 1.2, 3::float8);
938 select hstore(v) from testhstore1 v;
939                     hstore                    
940 ----------------------------------------------
941  "a"=>1, "b"=>"foo", "c"=>1.2, "d"=>3, "e"=>0
942 (1 row)
943
944 select hstore(null::testhstore0);
945                    hstore                   
946 --------------------------------------------
947  "a"=>NULL, "b"=>NULL, "c"=>NULL, "d"=>NULL
948 (1 row)
949
950 select hstore(null::testhstore1);
951                         hstore                         
952 -------------------------------------------------------
953  "a"=>NULL, "b"=>NULL, "c"=>NULL, "d"=>NULL, "e"=>NULL
954 (1 row)
955
956 select pg_column_size(hstore(v))
957          = pg_column_size('a=>1, b=>"foo", c=>1.2, d=>3, e=>0'::hstore)
958   from testhstore1 v;
959  ?column? 
960 ----------
961  t
962 (1 row)
963
964 select populate_record(v, hstore('c', '3.45')) from testhstore1 v;
965  populate_record  
966 ------------------
967  (1,foo,3.45,3,0)
968 (1 row)
969
970 select populate_record(v, hstore('d', '3.45')) from testhstore1 v;
971   populate_record   
972 --------------------
973  (1,foo,1.2,3.45,0)
974 (1 row)
975
976 select populate_record(v, hstore('e', '123')) from testhstore1 v;
977   populate_record  
978 -------------------
979  (1,foo,1.2,3,123)
980 (1 row)
981
982 select populate_record(v, hstore('e', null)) from testhstore1 v;
983 ERROR:  domain hstestdom1 does not allow null values
984 select populate_record(v, hstore('c', null)) from testhstore1 v;
985  populate_record 
986 -----------------
987  (1,foo,,3,0)
988 (1 row)
989
990 select populate_record(v, hstore('b', 'foo') || hstore('a', '123')) from testhstore1 v;
991   populate_record  
992 -------------------
993  (123,foo,1.2,3,0)
994 (1 row)
995
996 select populate_record(v, hstore('b', 'foo') || hstore('e', null)) from testhstore0 v;
997  populate_record 
998 -----------------
999  (1,foo,1.2,3)
1000 (1 row)
1001
1002 select populate_record(v, hstore('b', 'foo') || hstore('e', null)) from testhstore1 v;
1003 ERROR:  domain hstestdom1 does not allow null values
1004 select populate_record(v, '') from testhstore0 v;
1005  populate_record 
1006 -----------------
1007  (1,foo,1.2,3)
1008 (1 row)
1009
1010 select populate_record(v, '') from testhstore1 v;
1011  populate_record 
1012 -----------------
1013  (1,foo,1.2,3,0)
1014 (1 row)
1015
1016 select populate_record(null::testhstore1, hstore('c', '3.45') || hstore('a', '123'));
1017 ERROR:  domain hstestdom1 does not allow null values
1018 select populate_record(null::testhstore1, hstore('c', '3.45') || hstore('e', '123'));
1019  populate_record 
1020 -----------------
1021  (,,3.45,,123)
1022 (1 row)
1023
1024 select populate_record(null::testhstore0, '');
1025  populate_record 
1026 -----------------
1027  (,,,)
1028 (1 row)
1029
1030 select populate_record(null::testhstore1, '');
1031 ERROR:  domain hstestdom1 does not allow null values
1032 select v #= hstore('c', '3.45') from testhstore1 v;
1033      ?column?     
1034 ------------------
1035  (1,foo,3.45,3,0)
1036 (1 row)
1037
1038 select v #= hstore('d', '3.45') from testhstore1 v;
1039       ?column?      
1040 --------------------
1041  (1,foo,1.2,3.45,0)
1042 (1 row)
1043
1044 select v #= hstore('e', '123') from testhstore1 v;
1045      ?column?      
1046 -------------------
1047  (1,foo,1.2,3,123)
1048 (1 row)
1049
1050 select v #= hstore('c', null) from testhstore1 v;
1051    ?column?   
1052 --------------
1053  (1,foo,,3,0)
1054 (1 row)
1055
1056 select v #= hstore('e', null) from testhstore0 v;
1057    ?column?    
1058 ---------------
1059  (1,foo,1.2,3)
1060 (1 row)
1061
1062 select v #= hstore('e', null) from testhstore1 v;
1063 ERROR:  domain hstestdom1 does not allow null values
1064 select v #= (hstore('b', 'foo') || hstore('a', '123')) from testhstore1 v;
1065      ?column?      
1066 -------------------
1067  (123,foo,1.2,3,0)
1068 (1 row)
1069
1070 select v #= (hstore('b', 'foo') || hstore('e', '123')) from testhstore1 v;
1071      ?column?      
1072 -------------------
1073  (1,foo,1.2,3,123)
1074 (1 row)
1075
1076 select v #= hstore '' from testhstore0 v;
1077    ?column?    
1078 ---------------
1079  (1,foo,1.2,3)
1080 (1 row)
1081
1082 select v #= hstore '' from testhstore1 v;
1083     ?column?     
1084 -----------------
1085  (1,foo,1.2,3,0)
1086 (1 row)
1087
1088 select null::testhstore1 #= (hstore('c', '3.45') || hstore('a', '123'));
1089 ERROR:  domain hstestdom1 does not allow null values
1090 select null::testhstore1 #= (hstore('c', '3.45') || hstore('e', '123'));
1091    ?column?    
1092 ---------------
1093  (,,3.45,,123)
1094 (1 row)
1095
1096 select null::testhstore0 #= hstore '';
1097  ?column? 
1098 ----------
1099  (,,,)
1100 (1 row)
1101
1102 select null::testhstore1 #= hstore '';
1103 ERROR:  domain hstestdom1 does not allow null values
1104 select v #= h from testhstore1 v, (values (hstore 'a=>123',1),('b=>foo,c=>3.21',2),('a=>null',3),('e=>123',4),('f=>blah',5)) x(h,i) order by i;
1105      ?column?      
1106 -------------------
1107  (123,foo,1.2,3,0)
1108  (1,foo,3.21,3,0)
1109  (,foo,1.2,3,0)
1110  (1,foo,1.2,3,123)
1111  (1,foo,1.2,3,0)
1112 (5 rows)
1113
1114 -- keys/values
1115 select akeys('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
1116     akeys     
1117 --------------
1118  {b,aa,cq,fg}
1119 (1 row)
1120
1121 select akeys('""=>1');
1122  akeys 
1123 -------
1124  {""}
1125 (1 row)
1126
1127 select akeys('');
1128  akeys 
1129 -------
1130  {}
1131 (1 row)
1132
1133 select avals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
1134    avals   
1135 -----------
1136  {g,1,l,f}
1137 (1 row)
1138
1139 select avals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>NULL');
1140     avals     
1141 --------------
1142  {g,1,l,NULL}
1143 (1 row)
1144
1145 select avals('""=>1');
1146  avals 
1147 -------
1148  {1}
1149 (1 row)
1150
1151 select avals('');
1152  avals 
1153 -------
1154  {}
1155 (1 row)
1156
1157 select hstore_to_array('aa=>1, cq=>l, b=>g, fg=>NULL'::hstore);
1158      hstore_to_array     
1159 -------------------------
1160  {b,g,aa,1,cq,l,fg,NULL}
1161 (1 row)
1162
1163 select %% 'aa=>1, cq=>l, b=>g, fg=>NULL';
1164         ?column?         
1165 -------------------------
1166  {b,g,aa,1,cq,l,fg,NULL}
1167 (1 row)
1168
1169 select hstore_to_matrix('aa=>1, cq=>l, b=>g, fg=>NULL'::hstore);
1170         hstore_to_matrix         
1171 ---------------------------------
1172  {{b,g},{aa,1},{cq,l},{fg,NULL}}
1173 (1 row)
1174
1175 select %# 'aa=>1, cq=>l, b=>g, fg=>NULL';
1176             ?column?             
1177 ---------------------------------
1178  {{b,g},{aa,1},{cq,l},{fg,NULL}}
1179 (1 row)
1180
1181 select * from skeys('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
1182  skeys 
1183 -------
1184  b
1185  aa
1186  cq
1187  fg
1188 (4 rows)
1189
1190 select * from skeys('""=>1');
1191  skeys 
1192 -------
1193  
1194 (1 row)
1195
1196 select * from skeys('');
1197  skeys 
1198 -------
1199 (0 rows)
1200
1201 select * from svals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
1202  svals 
1203 -------
1204  g
1205  1
1206  l
1207  f
1208 (4 rows)
1209
1210 select *, svals is null from svals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>NULL');
1211  svals | ?column? 
1212 -------+----------
1213  g     | f
1214  1     | f
1215  l     | f
1216        | t
1217 (4 rows)
1218
1219 select * from svals('""=>1');
1220  svals 
1221 -------
1222  1
1223 (1 row)
1224
1225 select * from svals('');
1226  svals 
1227 -------
1228 (0 rows)
1229
1230 select * from each('aaa=>bq, b=>NULL, ""=>1 ');
1231  key | value 
1232 -----+-------
1233      | 1
1234  b   | 
1235  aaa | bq
1236 (3 rows)
1237
1238 -- @>
1239 select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b';
1240  ?column? 
1241 ----------
1242  t
1243 (1 row)
1244
1245 select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b, c=>NULL';
1246  ?column? 
1247 ----------
1248  t
1249 (1 row)
1250
1251 select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b, g=>NULL';
1252  ?column? 
1253 ----------
1254  f
1255 (1 row)
1256
1257 select 'a=>b, b=>1, c=>NULL'::hstore @> 'g=>NULL';
1258  ?column? 
1259 ----------
1260  f
1261 (1 row)
1262
1263 select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>c';
1264  ?column? 
1265 ----------
1266  f
1267 (1 row)
1268
1269 select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b';
1270  ?column? 
1271 ----------
1272  t
1273 (1 row)
1274
1275 select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b, c=>q';
1276  ?column? 
1277 ----------
1278  f
1279 (1 row)
1280
1281 CREATE TABLE testhstore (h hstore);
1282 \copy testhstore from 'data/hstore.data'
1283 select count(*) from testhstore where h @> 'wait=>NULL';
1284  count 
1285 -------
1286      1
1287 (1 row)
1288
1289 select count(*) from testhstore where h @> 'wait=>CC';
1290  count 
1291 -------
1292     15
1293 (1 row)
1294
1295 select count(*) from testhstore where h @> 'wait=>CC, public=>t';
1296  count 
1297 -------
1298      2
1299 (1 row)
1300
1301 select count(*) from testhstore where h @> 'age=>25';
1302  count 
1303 -------
1304      2
1305 (1 row)
1306
1307 select count(*) from testhstore where h @> 'age=>25.0';
1308  count 
1309 -------
1310      2
1311 (1 row)
1312
1313 select count(*) from testhstore where h @> 'age=>+25.00';
1314  count 
1315 -------
1316      2
1317 (1 row)
1318
1319 select count(*) from testhstore where h ? 'public';
1320  count 
1321 -------
1322    194
1323 (1 row)
1324
1325 select count(*) from testhstore where h ?| ARRAY['public','disabled'];
1326  count 
1327 -------
1328    337
1329 (1 row)
1330
1331 select count(*) from testhstore where h ?& ARRAY['public','disabled'];
1332  count 
1333 -------
1334     42
1335 (1 row)
1336
1337 create index hidx on testhstore using gist(h);
1338 set enable_seqscan=off;
1339 select count(*) from testhstore where h @> 'wait=>NULL';
1340  count 
1341 -------
1342      1
1343 (1 row)
1344
1345 select count(*) from testhstore where h @> 'wait=>CC';
1346  count 
1347 -------
1348     15
1349 (1 row)
1350
1351 select count(*) from testhstore where h @> 'wait=>CC, public=>t';
1352  count 
1353 -------
1354      2
1355 (1 row)
1356
1357 select count(*) from testhstore where h @> 'age=>25';
1358  count 
1359 -------
1360      2
1361 (1 row)
1362
1363 select count(*) from testhstore where h @> 'age=>25.0';
1364  count 
1365 -------
1366      2
1367 (1 row)
1368
1369 select count(*) from testhstore where h @> 'age=>+25.00';
1370  count 
1371 -------
1372      2
1373 (1 row)
1374
1375 select count(*) from testhstore where h ? 'public';
1376  count 
1377 -------
1378    194
1379 (1 row)
1380
1381 select count(*) from testhstore where h ?| ARRAY['public','disabled'];
1382  count 
1383 -------
1384    337
1385 (1 row)
1386
1387 select count(*) from testhstore where h ?& ARRAY['public','disabled'];
1388  count 
1389 -------
1390     42
1391 (1 row)
1392
1393 RESET enable_seqscan;
1394 drop index hidx;
1395 create index hidx on testhstore using gin (h);
1396 set enable_seqscan=off;
1397 select count(*) from testhstore where h @> 'wait=>NULL';
1398  count 
1399 -------
1400      1
1401 (1 row)
1402
1403 select count(*) from testhstore where h @> 'wait=>CC';
1404  count 
1405 -------
1406     15
1407 (1 row)
1408
1409 select count(*) from testhstore where h @> 'wait=>CC, public=>t';
1410  count 
1411 -------
1412      2
1413 (1 row)
1414
1415 select count(*) from testhstore where h @> 'age=>25';
1416  count 
1417 -------
1418      2
1419 (1 row)
1420
1421 select count(*) from testhstore where h @> 'age=>25.0';
1422  count 
1423 -------
1424      2
1425 (1 row)
1426
1427 select count(*) from testhstore where h @> 'age=>+25.00';
1428  count 
1429 -------
1430      2
1431 (1 row)
1432
1433 select count(*) from testhstore where h ? 'public';
1434  count 
1435 -------
1436    194
1437 (1 row)
1438
1439 select count(*) from testhstore where h ?| ARRAY['public','disabled'];
1440  count 
1441 -------
1442    337
1443 (1 row)
1444
1445 select count(*) from testhstore where h ?& ARRAY['public','disabled'];
1446  count 
1447 -------
1448     42
1449 (1 row)
1450
1451 RESET enable_seqscan;
1452 select count(*) from (select (each(h)).key from testhstore) as wow ;
1453  count 
1454 -------
1455   4783
1456 (1 row)
1457
1458 select key, count(*) from (select (each(h)).key from testhstore) as wow group by key order by count desc, key;
1459     key    | count 
1460 -----------+-------
1461  line      |   884
1462  query     |   207
1463  pos       |   203
1464  node      |   202
1465  space     |   197
1466  status    |   195
1467  public    |   194
1468  title     |   190
1469  wait      |   190
1470  org       |   189
1471  user      |   189
1472  coauthors |   188
1473  disabled  |   185
1474  indexed   |   184
1475  cleaned   |   180
1476  bad       |   179
1477  date      |   179
1478  world     |   176
1479  state     |   172
1480  subtitle  |   169
1481  auth      |   168
1482  abstract  |   161
1483  age       |     2
1484 (23 rows)
1485
1486 -- sort/hash
1487 select count(distinct h) from testhstore;
1488  count 
1489 -------
1490    886
1491 (1 row)
1492
1493 set enable_hashagg = false;
1494 select count(*) from (select h from (select * from testhstore union all select * from testhstore) hs group by h) hs2;
1495  count 
1496 -------
1497    886
1498 (1 row)
1499
1500 set enable_hashagg = true;
1501 set enable_sort = false;
1502 select count(*) from (select h from (select * from testhstore union all select * from testhstore) hs group by h) hs2;
1503  count 
1504 -------
1505    886
1506 (1 row)
1507
1508 select distinct * from (values (hstore '' || ''),('')) v(h);
1509  h 
1510 ---
1511  
1512 (1 row)
1513
1514 set enable_sort = true;
1515 RESET enable_hashagg;
1516 RESET enable_sort;
1517 -- btree
1518 drop index hidx;
1519 create index hidx on testhstore using btree (h);
1520 set enable_seqscan=off;
1521 select count(*) from testhstore where h #># 'p=>1';
1522  count 
1523 -------
1524    886
1525 (1 row)
1526
1527 select count(*) from testhstore where h = 'pos=>98, line=>371, node=>CBA, indexed=>t';
1528  count 
1529 -------
1530      1
1531 (1 row)
1532
1533 --gin hash
1534 drop index hidx;
1535 create index hidx on testhstore using gin (h gin_hstore_hash_ops);
1536 set enable_seqscan=off;
1537 select count(*) from testhstore where h @> 'wait=>NULL';
1538  count 
1539 -------
1540      1
1541 (1 row)
1542
1543 select count(*) from testhstore where h @> 'wait=>CC';
1544  count 
1545 -------
1546     15
1547 (1 row)
1548
1549 select count(*) from testhstore where h @> 'wait=>CC, public=>t';
1550  count 
1551 -------
1552      2
1553 (1 row)
1554
1555 select count(*) from testhstore where h @> 'age=>25';
1556  count 
1557 -------
1558      2
1559 (1 row)
1560
1561 select count(*) from testhstore where h @> 'age=>25.0';
1562  count 
1563 -------
1564      2
1565 (1 row)
1566
1567 select count(*) from testhstore where h @> 'age=>+25.00';
1568  count 
1569 -------
1570      2
1571 (1 row)
1572
1573 RESET enable_seqscan;
1574 drop index hidx;
1575 -- json
1576 select hstore_to_json('"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4');
1577                                    hstore_to_json                                   
1578 ------------------------------------------------------------------------------------
1579  {"b": true, "c": null, "d": 12345, "e": 12345, "f": 1.234, "g": 23450, "a key": 1}
1580 (1 row)
1581
1582 select cast( hstore  '"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4' as json);
1583                                         json                                        
1584 ------------------------------------------------------------------------------------
1585  {"b": true, "c": null, "d": 12345, "e": 12345, "f": 1.234, "g": 23450, "a key": 1}
1586 (1 row)
1587
1588 select hstore_to_json_loose('"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4');
1589                                 hstore_to_json_loose                                
1590 ------------------------------------------------------------------------------------
1591  {"b": true, "c": null, "d": 12345, "e": 12345, "f": 1.234, "g": 23450, "a key": 1}
1592 (1 row)
1593
1594 create table test_json_agg (f1 text, f2 hstore);
1595 insert into test_json_agg values ('rec1','"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4'),
1596        ('rec2','"a key" =>2, b => f, c => "null", d=> -12345, e => 012345.6, f=> -1.234, g=> 0.345e-4');
1597 select json_agg(q) from test_json_agg q;
1598                                                       json_agg                                                      
1599 --------------------------------------------------------------------------------------------------------------------
1600  [{"f1":"rec1","f2":{"b": true, "c": null, "d": 12345, "e": 12345, "f": 1.234, "g": 23450, "a key": 1}},           +
1601   {"f1":"rec2","f2":{"b": false, "c": "null", "d": -12345, "e": 12345.6, "f": -1.234, "g": 0.0000345, "a key": 2}}]
1602 (1 row)
1603
1604 select json_agg(q) from (select f1, hstore_to_json_loose(f2) as f2 from test_json_agg) q;
1605                                                       json_agg                                                      
1606 --------------------------------------------------------------------------------------------------------------------
1607  [{"f1":"rec1","f2":{"b": true, "c": null, "d": 12345, "e": 12345, "f": 1.234, "g": 23450, "a key": 1}},           +
1608   {"f1":"rec2","f2":{"b": false, "c": "null", "d": -12345, "e": 12345.6, "f": -1.234, "g": 0.0000345, "a key": 2}}]
1609 (1 row)
1610