--
-- first, define the datatype.  Turn off echoing so that expected file
-- does not depend on contents of hstore.sql.
--
SET client_min_messages = warning;
\set ECHO none
RESET client_min_messages;
set escape_string_warning=off;
--hstore;
select ''::hstore;
 hstore 
--------
 
(1 row)

select 'a=>b'::hstore;
  hstore  
----------
 "a"=>"b"
(1 row)

select ' a=>b'::hstore;
  hstore  
----------
 "a"=>"b"
(1 row)

select 'a =>b'::hstore;
  hstore  
----------
 "a"=>"b"
(1 row)

select 'a=>b '::hstore;
  hstore  
----------
 "a"=>"b"
(1 row)

select 'a=> b'::hstore;
  hstore  
----------
 "a"=>"b"
(1 row)

select '"a"=>"b"'::hstore;
  hstore  
----------
 "a"=>"b"
(1 row)

select ' "a"=>"b"'::hstore;
  hstore  
----------
 "a"=>"b"
(1 row)

select '"a" =>"b"'::hstore;
  hstore  
----------
 "a"=>"b"
(1 row)

select '"a"=>"b" '::hstore;
  hstore  
----------
 "a"=>"b"
(1 row)

select '"a"=> "b"'::hstore;
  hstore  
----------
 "a"=>"b"
(1 row)

select 'aa=>bb'::hstore;
   hstore   
------------
 "aa"=>"bb"
(1 row)

select ' aa=>bb'::hstore;
   hstore   
------------
 "aa"=>"bb"
(1 row)

select 'aa =>bb'::hstore;
   hstore   
------------
 "aa"=>"bb"
(1 row)

select 'aa=>bb '::hstore;
   hstore   
------------
 "aa"=>"bb"
(1 row)

select 'aa=> bb'::hstore;
   hstore   
------------
 "aa"=>"bb"
(1 row)

select '"aa"=>"bb"'::hstore;
   hstore   
------------
 "aa"=>"bb"
(1 row)

select ' "aa"=>"bb"'::hstore;
   hstore   
------------
 "aa"=>"bb"
(1 row)

select '"aa" =>"bb"'::hstore;
   hstore   
------------
 "aa"=>"bb"
(1 row)

select '"aa"=>"bb" '::hstore;
   hstore   
------------
 "aa"=>"bb"
(1 row)

select '"aa"=> "bb"'::hstore;
   hstore   
------------
 "aa"=>"bb"
(1 row)

select 'aa=>bb, cc=>dd'::hstore;
         hstore         
------------------------
 "aa"=>"bb", "cc"=>"dd"
(1 row)

select 'aa=>bb , cc=>dd'::hstore;
         hstore         
------------------------
 "aa"=>"bb", "cc"=>"dd"
(1 row)

select 'aa=>bb ,cc=>dd'::hstore;
         hstore         
------------------------
 "aa"=>"bb", "cc"=>"dd"
(1 row)

select 'aa=>bb, "cc"=>dd'::hstore;
         hstore         
------------------------
 "aa"=>"bb", "cc"=>"dd"
(1 row)

select 'aa=>bb , "cc"=>dd'::hstore;
         hstore         
------------------------
 "aa"=>"bb", "cc"=>"dd"
(1 row)

select 'aa=>bb ,"cc"=>dd'::hstore;
         hstore         
------------------------
 "aa"=>"bb", "cc"=>"dd"
(1 row)

select 'aa=>"bb", cc=>dd'::hstore;
         hstore         
------------------------
 "aa"=>"bb", "cc"=>"dd"
(1 row)

select 'aa=>"bb" , cc=>dd'::hstore;
         hstore         
------------------------
 "aa"=>"bb", "cc"=>"dd"
(1 row)

select 'aa=>"bb" ,cc=>dd'::hstore;
         hstore         
------------------------
 "aa"=>"bb", "cc"=>"dd"
(1 row)

select 'aa=>null'::hstore;
   hstore   
------------
 "aa"=>NULL
(1 row)

select 'aa=>NuLl'::hstore;
   hstore   
------------
 "aa"=>NULL
(1 row)

select 'aa=>"NuLl"'::hstore;
    hstore    
--------------
 "aa"=>"NuLl"
(1 row)

select '\\=a=>q=w'::hstore;
   hstore    
-------------
 "=a"=>"q=w"
(1 row)

select '"=a"=>q\\=w'::hstore;
   hstore    
-------------
 "=a"=>"q=w"
(1 row)

select '"\\"a"=>q>w'::hstore;
    hstore    
--------------
 "\"a"=>"q>w"
(1 row)

select '\\"a=>q"w'::hstore;
    hstore     
---------------
 "\"a"=>"q\"w"
(1 row)

select ''::hstore;
 hstore 
--------
 
(1 row)

select '	'::hstore;
 hstore 
--------
 
(1 row)

-- -> operator
select 'aa=>b, c=>d , b=>16'::hstore->'c';
 ?column? 
----------
 d
(1 row)

select 'aa=>b, c=>d , b=>16'::hstore->'b';
 ?column? 
----------
 16
(1 row)

select 'aa=>b, c=>d , b=>16'::hstore->'aa';
 ?column? 
----------
 b
(1 row)

select ('aa=>b, c=>d , b=>16'::hstore->'gg') is null;
 ?column? 
----------
 t
(1 row)

select ('aa=>NULL, c=>d , b=>16'::hstore->'aa') is null;
 ?column? 
----------
 t
(1 row)

select ('aa=>"NULL", c=>d , b=>16'::hstore->'aa') is null;
 ?column? 
----------
 f
(1 row)

-- exists/defined
select exist('a=>NULL, b=>qq', 'a');
 exist 
-------
 t
(1 row)

select exist('a=>NULL, b=>qq', 'b');
 exist 
-------
 t
(1 row)

select exist('a=>NULL, b=>qq', 'c');
 exist 
-------
 f
(1 row)

select exist('a=>"NULL", b=>qq', 'a');
 exist 
-------
 t
(1 row)

select defined('a=>NULL, b=>qq', 'a');
 defined 
---------
 f
(1 row)

select defined('a=>NULL, b=>qq', 'b');
 defined 
---------
 t
(1 row)

select defined('a=>NULL, b=>qq', 'c');
 defined 
---------
 f
(1 row)

select defined('a=>"NULL", b=>qq', 'a');
 defined 
---------
 t
(1 row)

-- delete 
select delete('a=>1 , b=>2, c=>3'::hstore, 'a');
       delete       
--------------------
 "b"=>"2", "c"=>"3"
(1 row)

select delete('a=>null , b=>2, c=>3'::hstore, 'a');
       delete       
--------------------
 "b"=>"2", "c"=>"3"
(1 row)

select delete('a=>1 , b=>2, c=>3'::hstore, 'b');
       delete       
--------------------
 "a"=>"1", "c"=>"3"
(1 row)

select delete('a=>1 , b=>2, c=>3'::hstore, 'c');
       delete       
--------------------
 "a"=>"1", "b"=>"2"
(1 row)

select delete('a=>1 , b=>2, c=>3'::hstore, 'd');
            delete            
------------------------------
 "a"=>"1", "b"=>"2", "c"=>"3"
(1 row)

-- ||
select 'aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f';
                 ?column?                  
-------------------------------------------
 "b"=>"g", "aa"=>"1", "cq"=>"l", "fg"=>"f"
(1 row)

select 'aa=>1 , b=>2, cq=>3'::hstore || 'aq=>l';
                 ?column?                  
-------------------------------------------
 "b"=>"2", "aa"=>"1", "aq"=>"l", "cq"=>"3"
(1 row)

select 'aa=>1 , b=>2, cq=>3'::hstore || 'aa=>l';
            ?column?            
--------------------------------
 "b"=>"2", "aa"=>"l", "cq"=>"3"
(1 row)

select 'aa=>1 , b=>2, cq=>3'::hstore || '';
            ?column?            
--------------------------------
 "b"=>"2", "aa"=>"1", "cq"=>"3"
(1 row)

select ''::hstore || 'cq=>l, b=>g, fg=>f';
            ?column?            
--------------------------------
 "b"=>"g", "cq"=>"l", "fg"=>"f"
(1 row)

-- =>
select 'a=>g, b=>c'::hstore || ( 'asd'=>'gf' );
            ?column?             
---------------------------------
 "a"=>"g", "b"=>"c", "asd"=>"gf"
(1 row)

select 'a=>g, b=>c'::hstore || ( 'b'=>'gf' );
      ?column?       
---------------------
 "a"=>"g", "b"=>"gf"
(1 row)

select 'a=>g, b=>c'::hstore || ( 'b'=>'NULL' );
       ?column?        
-----------------------
 "a"=>"g", "b"=>"NULL"
(1 row)

select 'a=>g, b=>c'::hstore || ( 'b'=>NULL );
      ?column?       
---------------------
 "a"=>"g", "b"=>NULL
(1 row)

-- keys/values
select akeys('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
    akeys     
--------------
 {b,aa,cq,fg}
(1 row)

select akeys('""=>1');
 akeys 
-------
 {""}
(1 row)

select akeys('');
 akeys 
-------
 {}
(1 row)

select avals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
   avals   
-----------
 {g,1,l,f}
(1 row)

select avals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>NULL');
   avals    
------------
 {g,1,l,""}
(1 row)

select avals('""=>1');
 avals 
-------
 {1}
(1 row)

select avals('');
 avals 
-------
 {}
(1 row)

select * from skeys('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
 skeys 
-------
 b
 aa
 cq
 fg
(4 rows)

select * from skeys('""=>1');
 skeys 
-------
 
(1 row)

select * from skeys('');
 skeys 
-------
(0 rows)

select * from svals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
 svals 
-------
 g
 1
 l
 f
(4 rows)

select *, svals is null from svals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>NULL');
 svals | ?column? 
-------+----------
 g     | f
 1     | f
 l     | f
       | t
(4 rows)

select * from svals('""=>1');
 svals 
-------
 1
(1 row)

select * from svals('');
 svals 
-------
(0 rows)

select * from each('aaa=>bq, b=>NULL, ""=>1 ');
 key | value 
-----+-------
     | 1
 b   | 
 aaa | bq
(3 rows)

-- @>
select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b';
 ?column? 
----------
 t
(1 row)

select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b, c=>NULL';
 ?column? 
----------
 t
(1 row)

select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b, g=>NULL';
 ?column? 
----------
 f
(1 row)

select 'a=>b, b=>1, c=>NULL'::hstore @> 'g=>NULL';
 ?column? 
----------
 f
(1 row)

select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>c';
 ?column? 
----------
 f
(1 row)

select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b';
 ?column? 
----------
 t
(1 row)

select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b, c=>q';
 ?column? 
----------
 f
(1 row)

CREATE TABLE testhstore (h hstore);
\copy testhstore from 'data/hstore.data'
select count(*) from testhstore where h @> 'wait=>NULL';
 count 
-------
     1
(1 row)

select count(*) from testhstore where h @> 'wait=>CC';
 count 
-------
    15
(1 row)

select count(*) from testhstore where h @> 'wait=>CC, public=>t';
 count 
-------
     2
(1 row)

select count(*) from testhstore where h ? 'public';
 count 
-------
   194
(1 row)

create index hidx on testhstore using gist(h);
set enable_seqscan=off;
select count(*) from testhstore where h @> 'wait=>NULL';
 count 
-------
     1
(1 row)

select count(*) from testhstore where h @> 'wait=>CC';
 count 
-------
    15
(1 row)

select count(*) from testhstore where h @> 'wait=>CC, public=>t';
 count 
-------
     2
(1 row)

select count(*) from testhstore where h ? 'public';
 count 
-------
   194
(1 row)

drop index hidx;
create index hidx on testhstore using gin (h);
set enable_seqscan=off;
select count(*) from testhstore where h @> 'wait=>NULL';
 count 
-------
     1
(1 row)

select count(*) from testhstore where h @> 'wait=>CC';
 count 
-------
    15
(1 row)

select count(*) from testhstore where h @> 'wait=>CC, public=>t';
 count 
-------
     2
(1 row)

select count(*) from testhstore where h ? 'public';
 count 
-------
   194
(1 row)

select count(*) from (select (each(h)).key from testhstore) as wow ;
 count 
-------
  4781
(1 row)

select key, count(*) from (select (each(h)).key from testhstore) as wow group by key order by count desc, key;
    key    | count 
-----------+-------
 line      |   884
 query     |   207
 pos       |   203
 node      |   202
 space     |   197
 status    |   195
 public    |   194
 title     |   190
 wait      |   190
 org       |   189
 user      |   189
 coauthors |   188
 disabled  |   185
 indexed   |   184
 cleaned   |   180
 bad       |   179
 date      |   179
 world     |   176
 state     |   172
 subtitle  |   169
 auth      |   168
 abstract  |   161
(22 rows)

