Commit 642194ba authored by Teodor Sigaev's avatar Teodor Sigaev

Add hstore contrib module.

Per discussion
http://archives.postgresql.org/pgsql-hackers/2006-08/msg01409.php
parent af7d257e
# $PostgreSQL: pgsql/contrib/Makefile,v 1.68 2006/09/05 17:20:26 tgl Exp $
# $PostgreSQL: pgsql/contrib/Makefile,v 1.69 2006/09/05 18:00:57 teodor Exp $
subdir = contrib
top_builddir = ..
......@@ -12,6 +12,7 @@ WANTED_DIRS = \
dblink \
earthdistance \
fuzzystrmatch \
hstore \
intagg \
intarray \
isbn_issn \
......
......@@ -52,6 +52,10 @@ fuzzystrmatch -
Levenshtein, metaphone, and soundex fuzzy string matching
by Joe Conway <mail@joeconway.com>, Joel Burton <jburton@scw.org>
hstore -
Hstore - module for storing (key,value) pairs
by Oleg Bartunov <oleg@sai.msu.su> and Teodor Sigaev <teodor@sigaev.ru>
intagg -
Integer aggregator
by mlw <markw@mohawksoft.com>
......
subdir = contrib/hstore
top_builddir = ../..
include $(top_builddir)/src/Makefile.global
override CPPFLAGS := -I. $(CPPFLAGS)
MODULE_big = hstore
OBJS = hstore_io.o hstore_op.o hstore_gist.o crc32.o
DATA_built = hstore.sql
DOCS = README.hstore
REGRESS = hstore
include $(top_srcdir)/contrib/contrib-global.mk
# DO NOT DELETE
Hstore - contrib module for storing (key,value) pairs
[Online version] (http://www.sai.msu.su/~megera/oddmuse/index.cgi?Hstore)
Motivation
Many attributes rarely searched, semistructural data, lazy DBA
Authors
* Oleg Bartunov <oleg@sai.msu.su>, Moscow, Moscow University, Russia
* Teodor Sigaev <teodor@sigaev.ru>, Moscow, Delta-Soft Ltd.,Russia
License
Stable version, included into PostgreSQL distribution, released under BSD license. Development version, available from this site, released under the GNU General Public License, version 2 (June 1991).
Operations
* hstore -> text - get value , perl analogy $h{key}
select 'a=>q, b=>g'->'a';
?
------
q
* hstore || hstore - concatenation, perl analogy %a=( %b, %c );
regression=# select 'a=>b'::hstore || 'c=>d'::hstore;
?column?
--------------------
"a"=>"b", "c"=>"d"
(1 row)
but, notice
regression=# select 'a=>b'::hstore || 'a=>d'::hstore;
?column?
----------
"a"=>"d"
(1 row)
* text => text - creates hstore type from two text strings
select 'a'=>'b';
?column?
----------
"a"=>"b"
* hstore @ hstore - contains operation, check if left operand contains right.
regression=# select 'a=>b, b=>1, c=>NULL'::hstore @ 'a=>c';
?column?
----------
f
(1 row)
regression=# select 'a=>b, b=>1, c=>NULL'::hstore @ 'b=>1';
?column?
----------
t
(1 row)
* hstore ~ hstore - contained operation, check if left operand is contained in right
Functions
* akeys(hstore) - returns all keys from hstore as array
regression=# select akeys('a=>1,b=>2');
akeys
-------
{a,b}
* skeys(hstore) - returns all keys from hstore as strings
regression=# select skeys('a=>1,b=>2');
skeys
-------
a
b
* avals(hstore) - returns all values from hstore as array
regression=# select avals('a=>1,b=>2');
avals
-------
{1,2}
* svals(hstore) - returns all values from hstore as strings
regression=# select svals('a=>1,b=>2');
svals
-------
1
2
* delete (hstore,text) - delete (key,value) from hstore if key matches argument.
regression=# select delete('a=>1,b=>2','b');
delete
----------
"a"=>"1"
* each(hstore) return (key, value) pairs
regression=# select * from each('a=>1,b=>2');
key | value
-----+-------
a | 1
b | 2
* isexists (hstore,text) - returns 'true if key is exists in hstore and false otherwise.
regression=# select isexists('a=>1','a');
isexists
----------
t
* isdefined (hstore,text) - returns true if key is exists in hstore and its value is not NULL.
regression=# select isdefined('a=>NULL','a');
isdefined
-----------
f
Indices
Module provides index support for '@' and '~' operations.
create index hidx on testhstore using gist(h);
Note
Use parenthesis in select below, because priority of 'is' is higher than that of '->'
select id from entrants where (info->'education_period') is not null;
Examples
* add key
update tt set h=h||'c=>3';
* delete key
update tt set h=delete(h,'k1');
* Statistics
hstore type, because of its intrinsic liberality, could contain a lot of different keys. Checking for valid keys is the task of application. Examples below demonstrate several techniques how to check keys statistics.
o simple example
select * from each('aaa=>bq, b=>NULL, ""=>1 ');
o using table
select (each(h)).key, (each(h)).value into stat from testhstore ;
o online stat
select key, count(*) from (select (each(h)).key from testhstore) as stat group by key order by count desc, key;
key | count
-----------+-------
line | 883
query | 207
pos | 203
node | 202
space | 197
status | 195
public | 194
title | 190
org | 189
...................
\ No newline at end of file
/* Both POSIX and CRC32 checksums */
#include <sys/types.h>
#include <stdio.h>
#include <sys/types.h>
#include "crc32.h"
/*
* This code implements the AUTODIN II polynomial
* The variable corresponding to the macro argument "crc" should
* be an unsigned long.
* Oroginal code by Spencer Garrett <srg@quick.com>
*/
#define _CRC32_(crc, ch) (crc = (crc >> 8) ^ crc32tab[(crc ^ (ch)) & 0xff])
/* generated using the AUTODIN II polynomial
* x^32 + x^26 + x^23 + x^22 + x^16 +
* x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x^1 + 1
*/
static const unsigned int crc32tab[256] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,
0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,
0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a,
0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818,
0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c,
0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086,
0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4,
0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe,
0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252,
0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60,
0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,
0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,
0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,
0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,
0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
};
unsigned int
crc32_sz(char *buf, int size)
{
unsigned int crc = ~0;
char *p;
int len,
nr;
len = 0;
nr = size;
for (len += nr, p = buf; nr--; ++p)
_CRC32_(crc, *p);
return ~crc;
}
#ifndef _CRC32_H
#define _CRC32_H
/* Returns crc32 of data block */
extern unsigned int crc32_sz(char *buf, int size);
/* Returns crc32 of null-terminated string */
#define crc32(buf) crc32_sz((buf),strlen(buf))
#endif
This diff is collapsed.
\set ECHO none
psql:hstore.sql:8: NOTICE: type "hstore" is not yet defined
DETAIL: Creating a shell type definition.
psql:hstore.sql:13: NOTICE: argument type hstore is only a shell
psql:hstore.sql:132: NOTICE: type "ghstore" is not yet defined
DETAIL: Creating a shell type definition.
psql:hstore.sql:137: NOTICE: argument type ghstore is only a shell
--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)
-- exists/defined
select isexists('a=>NULL, b=>qq', 'a');
isexists
----------
t
(1 row)
select isexists('a=>NULL, b=>qq', 'b');
isexists
----------
t
(1 row)
select isexists('a=>NULL, b=>qq', 'c');
isexists
----------
f
(1 row)
select isdefined('a=>NULL, b=>qq', 'a');
isdefined
-----------
f
(1 row)
select isdefined('a=>NULL, b=>qq', 'b');
isdefined
-----------
t
(1 row)
select isdefined('a=>NULL, b=>qq', 'c');
isdefined
-----------
f
(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)
-- 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=>NULL';
?column?
----------
t
(1 row)
select 'a=>b, b=>1, c=>NULL'::hstore @ 'a=>NULL, c=>NULL';
?column?
----------
t
(1 row)
select 'a=>b, b=>1, c=>NULL'::hstore @ 'a=>NULL, 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=>NULL';
?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
-------
189
(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)
create index hidx on testhstore using gist(h);
set enable_seqscan=off;
select count(*) from testhstore where h @ 'wait=>NULL';
count
-------
189
(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 (select (each(h)).key from testhstore) as wow ;
count
-------
4779
(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 | 883
query | 207
pos | 203
node | 202
space | 197
status | 195
public | 194
title | 190
org | 189
user | 189
wait | 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)
#ifndef __HSTORE_H__
#define __HSTORE_H__
#include "postgres.h"
#include "funcapi.h"
#include "access/gist.h"
#include "access/itup.h"
#include "utils/elog.h"
#include "utils/palloc.h"
#include "utils/builtins.h"
#include "storage/bufpage.h"
typedef struct {
uint16 keylen;
uint16 vallen;
uint32
valisnull:1,
pos:31;
} HEntry;
typedef struct {
int4 len;
int4 size;
char data[1];
} HStore;
#define HSHRDSIZE (2*sizeof(int4))
#define CALCDATASIZE(x, lenstr) ( (x) * sizeof(HEntry) + HSHRDSIZE + (lenstr) )
#define ARRPTR(x) ( (HEntry*) ( (char*)(x) + HSHRDSIZE ) )
#define STRPTR(x) ( (char*)(x) + HSHRDSIZE + ( sizeof(HEntry) * ((HStore*)x)->size ) )
#define PG_GETARG_HS(x) ((HStore*)PG_DETOAST_DATUM(PG_GETARG_DATUM(x)))
typedef struct {
char *key;
char *val;
uint16 keylen;
uint16 vallen;
bool isnull;
bool needfree;
} Pairs;
int comparePairs(const void *a, const void *b);
int uniquePairs(Pairs * a, int4 l, int4 *buflen);
#endif
SET search_path = public;
BEGIN;
CREATE FUNCTION hstore_in(cstring)
RETURNS hstore
AS 'MODULE_PATHNAME'
LANGUAGE 'C' with (isstrict);
CREATE FUNCTION hstore_out(hstore)
RETURNS cstring
AS 'MODULE_PATHNAME'
LANGUAGE 'C' with (isstrict);
CREATE TYPE hstore (
INTERNALLENGTH = -1,
INPUT = hstore_in,
OUTPUT = hstore_out,
STORAGE = extended
);
CREATE FUNCTION fetchval(hstore,text)
RETURNS text
AS 'MODULE_PATHNAME'
LANGUAGE 'C' with (isstrict,iscachable);
CREATE OPERATOR -> (
LEFTARG = hstore,
RIGHTARG = text,
PROCEDURE = fetchval
);
CREATE FUNCTION isexists(hstore,text)
RETURNS bool
AS 'MODULE_PATHNAME','exists'
LANGUAGE 'C' with (isstrict,iscachable);
CREATE FUNCTION isdefined(hstore,text)
RETURNS bool
AS 'MODULE_PATHNAME','defined'
LANGUAGE 'C' with (isstrict,iscachable);
CREATE FUNCTION delete(hstore,text)
RETURNS hstore
AS 'MODULE_PATHNAME','delete'
LANGUAGE 'C' with (isstrict,iscachable);
CREATE FUNCTION hs_concat(hstore,hstore)
RETURNS hstore
AS 'MODULE_PATHNAME'
LANGUAGE 'C' with (isstrict,iscachable);
CREATE OPERATOR || (
LEFTARG = hstore,
RIGHTARG = hstore,
PROCEDURE = hs_concat
);
CREATE FUNCTION hs_contains(hstore,hstore)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' with (isstrict,iscachable);
CREATE OPERATOR @ (
LEFTARG = hstore,
RIGHTARG = hstore,
PROCEDURE = hs_contains,
COMMUTATOR = '~',
RESTRICT = contsel,
JOIN = contjoinsel
);
CREATE FUNCTION hs_contained(hstore,hstore)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C' with (isstrict,iscachable);
CREATE OPERATOR ~ (
LEFTARG = hstore,
RIGHTARG = hstore,
PROCEDURE = hs_contained,
COMMUTATOR = '@',
RESTRICT = contsel,
JOIN = contjoinsel
);
CREATE FUNCTION tconvert(text,text)
RETURNS hstore
AS 'MODULE_PATHNAME'
LANGUAGE 'C' with (isstrict,iscachable);
CREATE OPERATOR => (
LEFTARG = text,
RIGHTARG = text,
PROCEDURE = tconvert
);
CREATE FUNCTION akeys(hstore)
RETURNS _text
AS 'MODULE_PATHNAME'
LANGUAGE 'C' with (isstrict,iscachable);
CREATE FUNCTION avals(hstore)
RETURNS _text
AS 'MODULE_PATHNAME'
LANGUAGE 'C' with (isstrict,iscachable);
CREATE FUNCTION skeys(hstore)
RETURNS setof text
AS 'MODULE_PATHNAME'
LANGUAGE 'C' with (isstrict,iscachable);
CREATE FUNCTION svals(hstore)
RETURNS setof text
AS 'MODULE_PATHNAME'
LANGUAGE 'C' with (isstrict,iscachable);
create type hs_each as (key text, value text);
CREATE FUNCTION each(hstore)
RETURNS setof hs_each
AS 'MODULE_PATHNAME'
LANGUAGE 'C' with (isstrict,iscachable);
-- define the GiST support methods
CREATE FUNCTION ghstore_in(cstring)
RETURNS ghstore
AS 'MODULE_PATHNAME'
LANGUAGE 'C' with (isstrict);
CREATE FUNCTION ghstore_out(ghstore)
RETURNS cstring
AS 'MODULE_PATHNAME'
LANGUAGE 'C' with (isstrict);
CREATE TYPE ghstore (
INTERNALLENGTH = -1,
INPUT = ghstore_in,
OUTPUT = ghstore_out
);
CREATE FUNCTION ghstore_compress(internal)
RETURNS internal
AS 'MODULE_PATHNAME'
LANGUAGE 'C';
CREATE FUNCTION ghstore_decompress(internal)
RETURNS internal
AS 'MODULE_PATHNAME'
LANGUAGE 'C';
CREATE FUNCTION ghstore_penalty(internal,internal,internal)
RETURNS internal
AS 'MODULE_PATHNAME'
LANGUAGE 'C' WITH (isstrict);
CREATE FUNCTION ghstore_picksplit(internal, internal)
RETURNS internal
AS 'MODULE_PATHNAME'
LANGUAGE 'C';
CREATE FUNCTION ghstore_union(internal, internal)
RETURNS internal
AS 'MODULE_PATHNAME'
LANGUAGE 'C';
CREATE FUNCTION ghstore_same(internal, internal, internal)
RETURNS internal
AS 'MODULE_PATHNAME'
LANGUAGE 'C';
CREATE FUNCTION ghstore_consistent(internal,internal,int4)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE 'C';
-- register the opclass for indexing (not as default)
CREATE OPERATOR CLASS gist_hstore_ops
DEFAULT FOR TYPE hstore USING gist
AS
OPERATOR 7 @ RECHECK,
--OPERATOR 8 ~ RECHECK,
FUNCTION 1 ghstore_consistent (internal, internal, int4),
FUNCTION 2 ghstore_union (internal, internal),
FUNCTION 3 ghstore_compress (internal),
FUNCTION 4 ghstore_decompress (internal),
FUNCTION 5 ghstore_penalty (internal, internal, internal),
FUNCTION 6 ghstore_picksplit (internal, internal),
FUNCTION 7 ghstore_same (internal, internal, internal),
STORAGE ghstore;
END;
This diff is collapsed.
#include "hstore.h"
#include <ctype.h>
PG_MODULE_MAGIC;
typedef struct {
char *begin;
char *ptr;
char *cur;
char *word;
int wordlen;
Pairs *pairs;
int pcur;
int plen;
} HSParser;
#define RESIZEPRSBUF \
do { \
if ( state->cur - state->word + 1 >= state->wordlen ) \
{ \
int4 clen = state->cur - state->word; \
state->wordlen *= 2; \
state->word = (char*)repalloc( (void*)state->word, state->wordlen ); \
state->cur = state->word + clen; \
} \
} while (0)
#define GV_WAITVAL 0
#define GV_INVAL 1
#define GV_INESCVAL 2
#define GV_WAITESCIN 3
#define GV_WAITESCESCIN 4
static bool
get_val( HSParser *state, bool ignoreeq, bool *escaped ) {
int st = GV_WAITVAL;
state->wordlen=32;
state->cur = state->word = palloc( state->wordlen );
*escaped=false;
while(1) {
if ( st == GV_WAITVAL ) {
if ( *(state->ptr) == '"' ) {
*escaped=true;
st = GV_INESCVAL;
} else if ( *(state->ptr) == '\0' ) {
return false;
} else if ( *(state->ptr) == '=' && !ignoreeq ) {
elog(ERROR,"Syntax error near '%c' at postion %d", *(state->ptr), state->ptr-state->begin);
} else if ( *(state->ptr) == '\\' ) {
st = GV_WAITESCIN;
} else if ( !isspace(*(state->ptr)) ) {
*(state->cur) = *(state->ptr);
state->cur++;
st = GV_INVAL;
}
} else if ( st == GV_INVAL ) {
if ( *(state->ptr) == '\\' ) {
st = GV_WAITESCIN;
} else if ( *(state->ptr) == '=' && !ignoreeq ) {
state->ptr--;
return true;
} else if ( *(state->ptr) == ',' && ignoreeq ) {
state->ptr--;
return true;
} else if ( isspace(*(state->ptr)) ) {
return true;
} else if ( *(state->ptr) == '\0' ) {
state->ptr--;
return true;
} else {
RESIZEPRSBUF;
*(state->cur) = *(state->ptr);
state->cur++;
}
} else if ( st == GV_INESCVAL ) {
if ( *(state->ptr) == '\\' ) {
st = GV_WAITESCESCIN;
} else if ( *(state->ptr) == '"' ) {
return true;
} else if ( *(state->ptr) == '\0' ) {
elog(ERROR,"Unexpected end of string");
} else {
RESIZEPRSBUF;
*(state->cur) = *(state->ptr);
state->cur++;
}
} else if ( st == GV_WAITESCIN ) {
if ( *(state->ptr) == '\0' )
elog(ERROR,"Unexpected end of string");
RESIZEPRSBUF;
*(state->cur) = *(state->ptr);
state->cur++;
st = GV_INVAL;
} else if ( st == GV_WAITESCESCIN ) {
if ( *(state->ptr) == '\0' )
elog(ERROR,"Unexpected end of string");
RESIZEPRSBUF;
*(state->cur) = *(state->ptr);
state->cur++;
st = GV_INESCVAL;
} else
elog(ERROR,"Unknown state %d at postion line %d in file '%s'", st, __LINE__, __FILE__);
state->ptr++;
}
return false;
}
#define WKEY 0
#define WVAL 1
#define WEQ 2
#define WGT 3
#define WDEL 4
static void
parse_hstore( HSParser *state ) {
int st = WKEY;
bool escaped=false;
state->plen=16;
state->pairs = (Pairs*)palloc( sizeof(Pairs) * state->plen );
state->pcur=0;
state->ptr = state->begin;
state->word=NULL;
while(1) {
if (st == WKEY) {
if ( !get_val(state, false, &escaped) )
return;
if ( state->pcur >= state->plen ) {
state->plen *= 2;
state->pairs = (Pairs*)repalloc( state->pairs, sizeof(Pairs) * state->plen );
}
state->pairs[ state->pcur ].key = state->word;
state->pairs[ state->pcur ].keylen = state->cur - state->word;
state->pairs[ state->pcur ].val=NULL;
state->word=NULL;
st = WEQ;
} else if ( st == WEQ ) {
if ( *(state->ptr) == '=' ) {
st = WGT;
} else if ( *(state->ptr) == '\0' ) {
elog(ERROR,"Unexpectd end of string");
} else if (!isspace(*(state->ptr))) {
elog(ERROR,"Syntax error near '%c' at postion %d", *(state->ptr), state->ptr-state->begin);
}
} else if ( st == WGT ) {
if ( *(state->ptr) == '>' ) {
st = WVAL;
} else if ( *(state->ptr) == '\0' ) {
elog(ERROR,"Unexpectd end of string");
} else {
elog(ERROR,"Syntax error near '%c' at postion %d", *(state->ptr), state->ptr-state->begin);
}
} else if ( st == WVAL ) {
if ( !get_val(state, true, &escaped) )
elog(ERROR,"Unexpected end of string");
state->pairs[ state->pcur ].val = state->word;
state->pairs[ state->pcur ].vallen = state->cur - state->word;
state->pairs[ state->pcur ].isnull = false;
state->pairs[ state->pcur ].needfree = true;
if ( state->cur - state->word == 4 && !escaped) {
state->word[4] = '\0';
if ( 0==strcasecmp(state->word, "null") )
state->pairs[ state->pcur ].isnull=true;
}
state->word=NULL;
state->pcur++;
st = WDEL;
} else if ( st == WDEL ) {
if ( *(state->ptr) == ',' ) {
st = WKEY;
} else if ( *(state->ptr) == '\0' ) {
return;
} else if (!isspace(*(state->ptr))) {
elog(ERROR,"Syntax error near '%c' at postion %d", *(state->ptr), state->ptr-state->begin);
}
} else
elog(ERROR,"Unknown state %d at line %d in file '%s'", st, __LINE__, __FILE__);
state->ptr++;
}
}
int
comparePairs(const void *a, const void *b) {
if ( ((Pairs*)a)->keylen == ((Pairs*)b)->keylen ) {
int res = strncmp(
((Pairs*)a)->key,
((Pairs*)b)->key,
((Pairs*)a)->keylen
);
if ( res )
return res;
/* guarantee that neddfree willl be later */
if ( ((Pairs*)b)->needfree == ((Pairs*)a)->needfree )
return 0;
else if ( ((Pairs*)a)->needfree )
return 1;
else
return -1;
}
return ( ((Pairs*)a)->keylen > ((Pairs*)b)->keylen ) ? 1 : -1;
}
int
uniquePairs(Pairs * a, int4 l, int4 *buflen) {
Pairs *ptr, *res;
*buflen=0;
if ( l < 2 ) {
if ( l==1 )
*buflen = a->keylen + ((a->isnull) ? 0 : a->vallen) ;
return l;
}
qsort((void *) a, l, sizeof(Pairs), comparePairs);
ptr=a+1;
res=a;
while( ptr - a < l ) {
if ( ptr->keylen == res->keylen && strncmp( ptr->key, res->key, res->keylen )==0 ) {
if ( ptr->needfree ) {
pfree(ptr->key);
pfree(ptr->val);
}
} else {
*buflen += res->keylen + (( res->isnull ) ? 0 : res->vallen);
res++;
memcpy(res,ptr,sizeof(Pairs));
}
ptr++;
}
*buflen += res->keylen + (( res->isnull ) ? 0 : res->vallen);
return res + 1 - a;
}
static void
freeHSParse(HSParser *state) {
int i;
if ( state->word ) pfree( state->word );
for (i=0;i<state->pcur;i++)
if ( state->pairs[i].needfree ) {
if (state->pairs[i].key) pfree(state->pairs[i].key);
if (state->pairs[i].val) pfree(state->pairs[i].val);
}
pfree( state->pairs );
}
PG_FUNCTION_INFO_V1(hstore_in);
Datum hstore_in(PG_FUNCTION_ARGS);
Datum
hstore_in(PG_FUNCTION_ARGS) {
HSParser state;
int4 len,buflen,i;
HStore *out;
HEntry *entries;
char *ptr;
state.begin = PG_GETARG_CSTRING(0);
parse_hstore(&state);
if ( state.pcur == 0 ) {
freeHSParse(&state);
len = CALCDATASIZE(0,0);
out = palloc(len);
out->len=len;
out->size=0;
PG_RETURN_POINTER(out);
}
state.pcur = uniquePairs(state.pairs, state.pcur, &buflen);
len=CALCDATASIZE(state.pcur, buflen);
out = palloc(len);
out->len=len;
out->size=state.pcur;
entries=ARRPTR(out);
ptr = STRPTR(out);
for(i=0;i<out->size;i++) {
entries[i].keylen = state.pairs[i].keylen;
entries[i].pos = ptr - STRPTR(out);
memcpy(ptr, state.pairs[i].key, state.pairs[i].keylen);
ptr+=entries[i].keylen;
entries[i].valisnull = state.pairs[i].isnull;
if ( entries[i].valisnull )
entries[i].vallen=4; /* null */
else {
entries[i].vallen = state.pairs[i].vallen;
memcpy(ptr, state.pairs[i].val,state.pairs[i].vallen);
ptr+=entries[i].vallen;
}
}
freeHSParse(&state);
PG_RETURN_POINTER(out);
}
static char*
cpw(char *dst, char *src, int len) {
char *ptr = src;
while(ptr-src<len) {
if ( *ptr == '"' || *ptr == '\\' )
*dst++='\\';
*dst++ = *ptr++;
}
return dst;
}
PG_FUNCTION_INFO_V1(hstore_out);
Datum hstore_out(PG_FUNCTION_ARGS);
Datum
hstore_out(PG_FUNCTION_ARGS) {
HStore *in = PG_GETARG_HS(0);
int buflen,i;
char *out,*ptr;
char *base = STRPTR(in);
HEntry *entries = ARRPTR(in);
if ( in->size==0 ) {
out=palloc(1);
*out='\0';
PG_FREE_IF_COPY(in,0);
PG_RETURN_CSTRING(out);
}
buflen = ( 4 /* " */ + 2 /* => */ + 2 /*, */ )*in->size +
2 /* esc */ * ( in->len - CALCDATASIZE(in->size,0) );
out=ptr=palloc(buflen);
for(i=0;i<in->size;i++) {
*ptr++='"';
ptr = cpw( ptr, base + entries[i].pos, entries[i].keylen );
*ptr++='"';
*ptr++='=';
*ptr++='>';
if ( entries[i].valisnull ) {
*ptr++='N';
*ptr++='U';
*ptr++='L';
*ptr++='L';
} else {
*ptr++='"';
ptr = cpw( ptr, base + entries[i].pos + entries[i].keylen, entries[i].vallen );
*ptr++='"';
}
if ( i+1 != in->size ) {
*ptr++=',';
*ptr++=' ';
}
}
*ptr='\0';
PG_FREE_IF_COPY(in,0);
PG_RETURN_CSTRING(out);
}
This diff is collapsed.
\set ECHO none
\i hstore.sql
set escape_string_warning=off;
\set ECHO all
--hstore;
select ''::hstore;
select 'a=>b'::hstore;
select ' a=>b'::hstore;
select 'a =>b'::hstore;
select 'a=>b '::hstore;
select 'a=> b'::hstore;
select '"a"=>"b"'::hstore;
select ' "a"=>"b"'::hstore;
select '"a" =>"b"'::hstore;
select '"a"=>"b" '::hstore;
select '"a"=> "b"'::hstore;
select 'aa=>bb'::hstore;
select ' aa=>bb'::hstore;
select 'aa =>bb'::hstore;
select 'aa=>bb '::hstore;
select 'aa=> bb'::hstore;
select '"aa"=>"bb"'::hstore;
select ' "aa"=>"bb"'::hstore;
select '"aa" =>"bb"'::hstore;
select '"aa"=>"bb" '::hstore;
select '"aa"=> "bb"'::hstore;
select 'aa=>bb, cc=>dd'::hstore;
select 'aa=>bb , cc=>dd'::hstore;
select 'aa=>bb ,cc=>dd'::hstore;
select 'aa=>bb, "cc"=>dd'::hstore;
select 'aa=>bb , "cc"=>dd'::hstore;
select 'aa=>bb ,"cc"=>dd'::hstore;
select 'aa=>"bb", cc=>dd'::hstore;
select 'aa=>"bb" , cc=>dd'::hstore;
select 'aa=>"bb" ,cc=>dd'::hstore;
select 'aa=>null'::hstore;
select 'aa=>NuLl'::hstore;
select 'aa=>"NuLl"'::hstore;
select '\\=a=>q=w'::hstore;
select '"=a"=>q\\=w'::hstore;
select '"\\"a"=>q>w'::hstore;
select '\\"a=>q"w'::hstore;
select ''::hstore;
select ' '::hstore;
-- -> operator
select 'aa=>b, c=>d , b=>16'::hstore->'c';
select 'aa=>b, c=>d , b=>16'::hstore->'b';
select 'aa=>b, c=>d , b=>16'::hstore->'aa';
select ('aa=>b, c=>d , b=>16'::hstore->'gg') is null;
select ('aa=>NULL, c=>d , b=>16'::hstore->'aa') is null;
-- exists/defined
select isexists('a=>NULL, b=>qq', 'a');
select isexists('a=>NULL, b=>qq', 'b');
select isexists('a=>NULL, b=>qq', 'c');
select isdefined('a=>NULL, b=>qq', 'a');
select isdefined('a=>NULL, b=>qq', 'b');
select isdefined('a=>NULL, b=>qq', 'c');
-- delete
select delete('a=>1 , b=>2, c=>3'::hstore, 'a');
select delete('a=>null , b=>2, c=>3'::hstore, 'a');
select delete('a=>1 , b=>2, c=>3'::hstore, 'b');
select delete('a=>1 , b=>2, c=>3'::hstore, 'c');
select delete('a=>1 , b=>2, c=>3'::hstore, 'd');
-- ||
select 'aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f';
select 'aa=>1 , b=>2, cq=>3'::hstore || 'aq=>l';
select 'aa=>1 , b=>2, cq=>3'::hstore || 'aa=>l';
select 'aa=>1 , b=>2, cq=>3'::hstore || '';
select ''::hstore || 'cq=>l, b=>g, fg=>f';
-- =>
select 'a=>g, b=>c'::hstore || ( 'asd'=>'gf' );
select 'a=>g, b=>c'::hstore || ( 'b'=>'gf' );
-- keys/values
select akeys('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
select akeys('""=>1');
select akeys('');
select avals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
select avals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>NULL');
select avals('""=>1');
select avals('');
select * from skeys('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
select * from skeys('""=>1');
select * from skeys('');
select * from svals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
select *, svals is null from svals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>NULL');
select * from svals('""=>1');
select * from svals('');
select * from each('aaa=>bq, b=>NULL, ""=>1 ');
-- @
select 'a=>b, b=>1, c=>NULL'::hstore @ 'a=>NULL';
select 'a=>b, b=>1, c=>NULL'::hstore @ 'a=>NULL, c=>NULL';
select 'a=>b, b=>1, c=>NULL'::hstore @ 'a=>NULL, g=>NULL';
select 'a=>b, b=>1, c=>NULL'::hstore @ 'g=>NULL';
select 'a=>b, b=>1, c=>NULL'::hstore @ 'a=>c';
select 'a=>b, b=>1, c=>NULL'::hstore @ 'a=>b';
select 'a=>b, b=>1, c=>NULL'::hstore @ 'a=>b, c=>NULL';
select 'a=>b, b=>1, c=>NULL'::hstore @ 'a=>b, c=>q';
CREATE TABLE testhstore (h hstore);
\copy testhstore from 'data/hstore.data'
select count(*) from testhstore where h @ 'wait=>NULL';
select count(*) from testhstore where h @ 'wait=>CC';
select count(*) from testhstore where h @ 'wait=>CC, public=>t';
create index hidx on testhstore using gist(h);
set enable_seqscan=off;
select count(*) from testhstore where h @ 'wait=>NULL';
select count(*) from testhstore where h @ 'wait=>CC';
select count(*) from testhstore where h @ 'wait=>CC, public=>t';
select count(*) from (select (each(h)).key from testhstore) as wow ;
select key, count(*) from (select (each(h)).key from testhstore) as wow group by key order by count desc, key;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment