Add hstore contrib module.

Per discussion
http://archives.postgresql.org/pgsql-hackers/2006-08/msg01409.php
This commit is contained in:
Teodor Sigaev 2006-09-05 18:00:58 +00:00
parent af7d257e21
commit 642194ba0c
14 changed files with 3701 additions and 1 deletions

View File

@ -1,4 +1,4 @@
# $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 \

View File

@ -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>

17
contrib/hstore/Makefile Normal file
View File

@ -0,0 +1,17 @@
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

View File

@ -0,0 +1,175 @@
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
...................

103
contrib/hstore/crc32.c Normal file
View File

@ -0,0 +1,103 @@
/* 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;
}

10
contrib/hstore/crc32.h Normal file
View File

@ -0,0 +1,10 @@
#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

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,607 @@
\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)

49
contrib/hstore/hstore.h Normal file
View File

@ -0,0 +1,49 @@
#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

View File

@ -0,0 +1,195 @@
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;

View File

@ -0,0 +1,471 @@
#include "hstore.h"
#include "access/gist.h"
#include "access/itup.h"
/*#include "access/rtree.h"*/
#include "crc32.h"
/* bigint defines */
#define BITBYTE 8
#define SIGLENINT 4 /* >122 => key will toast, so very slow!!! */
#define SIGLEN ( sizeof(int)*SIGLENINT )
#define SIGLENBIT (SIGLEN*BITBYTE)
typedef char BITVEC[SIGLEN];
typedef char *BITVECP;
#define SIGPTR(x) ( (BITVECP) ARR_DATA_PTR(x) )
#define LOOPBYTE(a) \
for(i=0;i<SIGLEN;i++) {\
a;\
}
#define LOOPBIT(a) \
for(i=0;i<SIGLENBIT;i++) {\
a;\
}
/* beware of multiple evaluation of arguments to these macros! */
#define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) )
#define GETBITBYTE(x,i) ( (*((char*)(x)) >> (i)) & 0x01 )
#define CLRBIT(x,i) GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITBYTE ) )
#define SETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITBYTE ) )
#define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITBYTE )) & 0x01 )
#define HASHVAL(val) (((unsigned int)(val)) % SIGLENBIT)
#define HASH(sign, val) SETBIT((sign), HASHVAL(val))
typedef struct {
int4 len;
int4 flag;
char data[1];
} GISTTYPE;
#define ALLISTRUE 0x04
#define ISALLTRUE(x) ( ((GISTTYPE*)x)->flag & ALLISTRUE )
#define GTHDRSIZE ( sizeof(int4)*2 )
#define CALCGTSIZE(flag) ( GTHDRSIZE+(((flag) & ALLISTRUE) ? 0 : SIGLEN) )
#define GETSIGN(x) ( (BITVECP)( (char*)x+GTHDRSIZE ) )
#define SUMBIT(val) ( \
GETBITBYTE((val),0) + \
GETBITBYTE((val),1) + \
GETBITBYTE((val),2) + \
GETBITBYTE((val),3) + \
GETBITBYTE((val),4) + \
GETBITBYTE((val),5) + \
GETBITBYTE((val),6) + \
GETBITBYTE((val),7) \
)
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
PG_FUNCTION_INFO_V1(ghstore_in);
Datum ghstore_in(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(ghstore_out);
Datum ghstore_out(PG_FUNCTION_ARGS);
Datum
ghstore_in(PG_FUNCTION_ARGS) {
elog(ERROR, "Not implemented");
PG_RETURN_DATUM(0);
}
Datum
ghstore_out(PG_FUNCTION_ARGS) {
elog(ERROR, "Not implemented");
PG_RETURN_DATUM(0);
}
PG_FUNCTION_INFO_V1(ghstore_consistent);
PG_FUNCTION_INFO_V1(ghstore_compress);
PG_FUNCTION_INFO_V1(ghstore_decompress);
PG_FUNCTION_INFO_V1(ghstore_penalty);
PG_FUNCTION_INFO_V1(ghstore_picksplit);
PG_FUNCTION_INFO_V1(ghstore_union);
PG_FUNCTION_INFO_V1(ghstore_same);
Datum ghstore_consistent(PG_FUNCTION_ARGS);
Datum ghstore_compress(PG_FUNCTION_ARGS);
Datum ghstore_decompress(PG_FUNCTION_ARGS);
Datum ghstore_penalty(PG_FUNCTION_ARGS);
Datum ghstore_picksplit(PG_FUNCTION_ARGS);
Datum ghstore_union(PG_FUNCTION_ARGS);
Datum ghstore_same(PG_FUNCTION_ARGS);
Datum
ghstore_compress(PG_FUNCTION_ARGS) {
GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
GISTENTRY *retval = entry;
if (entry->leafkey) {
GISTTYPE *res = (GISTTYPE*)palloc(CALCGTSIZE(0));
HStore *toastedval = (HStore *) DatumGetPointer(entry->key);
HStore *val = (HStore *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));
HEntry *ptr = ARRPTR(val);
char *words = STRPTR(val);
memset(res,0,CALCGTSIZE(0));
res->len=CALCGTSIZE(0);
while(ptr-ARRPTR(val) < val->size) {
int h;
h = crc32_sz((char*)(words+ptr->pos), ptr->keylen);
HASH( GETSIGN(res), h);
if ( !ptr->valisnull ) {
h = crc32_sz((char *)(words+ptr->pos+ptr->keylen), ptr->vallen);
HASH( GETSIGN(res), h);
}
ptr++;
}
if (val != toastedval)
pfree(val);
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
gistentryinit(*retval, PointerGetDatum(res),
entry->rel, entry->page,
entry->offset,
FALSE);
} else if ( !ISALLTRUE(DatumGetPointer(entry->key)) ) {
int4 i;
GISTTYPE *res;
BITVECP sign = GETSIGN(DatumGetPointer(entry->key));
LOOPBYTE(
if ((sign[i] & 0xff) != 0xff)
PG_RETURN_POINTER(retval);
);
res = (GISTTYPE *) palloc(CALCGTSIZE(ALLISTRUE));
res->len = CALCGTSIZE(ALLISTRUE);
res->flag = ALLISTRUE;
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
gistentryinit(*retval, PointerGetDatum(res),
entry->rel, entry->page,
entry->offset,
FALSE);
}
PG_RETURN_POINTER(retval);
}
Datum
ghstore_decompress(PG_FUNCTION_ARGS) {
PG_RETURN_DATUM(PG_GETARG_DATUM(0));
}
Datum
ghstore_same(PG_FUNCTION_ARGS) {
GISTTYPE *a = (GISTTYPE *) PG_GETARG_POINTER(0);
GISTTYPE *b = (GISTTYPE *) PG_GETARG_POINTER(1);
bool *result = (bool *) PG_GETARG_POINTER(2);
if (ISALLTRUE(a) && ISALLTRUE(b))
*result = true;
else if (ISALLTRUE(a))
*result = false;
else if (ISALLTRUE(b))
*result = false;
else {
int4 i;
BITVECP sa = GETSIGN(a),
sb = GETSIGN(b);
*result = true;
LOOPBYTE(
if (sa[i] != sb[i]) {
*result = false;
break;
}
);
}
PG_RETURN_POINTER(result);
}
static int4
sizebitvec(BITVECP sign) {
int4 size = 0, i;
LOOPBYTE(
size += SUMBIT(sign);
sign = (BITVECP) (((char *) sign) + 1);
);
return size;
}
static int
hemdistsign(BITVECP a, BITVECP b) {
int i,dist=0;
LOOPBIT(
if ( GETBIT(a,i) != GETBIT(b,i) )
dist++;
);
return dist;
}
static int
hemdist(GISTTYPE *a, GISTTYPE *b) {
if ( ISALLTRUE(a) ) {
if (ISALLTRUE(b))
return 0;
else
return SIGLENBIT-sizebitvec(GETSIGN(b));
} else if (ISALLTRUE(b))
return SIGLENBIT-sizebitvec(GETSIGN(a));
return hemdistsign( GETSIGN(a), GETSIGN(b) );
}
static int4
unionkey(BITVECP sbase, GISTTYPE * add)
{
int4 i;
BITVECP sadd = GETSIGN(add);
if (ISALLTRUE(add))
return 1;
LOOPBYTE(
sbase[i] |= sadd[i];
);
return 0;
}
Datum
ghstore_union(PG_FUNCTION_ARGS) {
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
int4 len = entryvec->n;
int *size = (int *) PG_GETARG_POINTER(1);
BITVEC base;
int4 i;
int4 flag = 0;
GISTTYPE *result;
MemSet((void *) base, 0, sizeof(BITVEC));
for (i = 0; i < len; i++) {
if (unionkey(base, GETENTRY(entryvec, i))) {
flag = ALLISTRUE;
break;
}
}
len = CALCGTSIZE(flag);
result = (GISTTYPE *) palloc(len);
*size = result->len = len;
result->flag = flag;
if (!ISALLTRUE(result))
memcpy((void *) GETSIGN(result), (void *) base, sizeof(BITVEC));
PG_RETURN_POINTER(result);
}
Datum
ghstore_penalty(PG_FUNCTION_ARGS) {
GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
float *penalty = (float *) PG_GETARG_POINTER(2);
GISTTYPE *origval = (GISTTYPE *) DatumGetPointer(origentry->key);
GISTTYPE *newval = (GISTTYPE *) DatumGetPointer(newentry->key);
*penalty=hemdist(origval,newval);
PG_RETURN_POINTER(penalty);
}
typedef struct {
OffsetNumber pos;
int4 cost;
} SPLITCOST;
static int
comparecost(const void *a, const void *b) {
return ((SPLITCOST *) a)->cost - ((SPLITCOST *) b)->cost;
}
Datum
ghstore_picksplit(PG_FUNCTION_ARGS) {
GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
OffsetNumber maxoff = entryvec->n - 2;
GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
OffsetNumber k,
j;
GISTTYPE *datum_l,
*datum_r;
BITVECP union_l,
union_r;
int4 size_alpha, size_beta;
int4 size_waste,
waste = -1;
int4 nbytes;
OffsetNumber seed_1 = 0,
seed_2 = 0;
OffsetNumber *left,
*right;
BITVECP ptr;
int i;
SPLITCOST *costvector;
GISTTYPE *_k,
*_j;
nbytes = (maxoff + 2) * sizeof(OffsetNumber);
v->spl_left = (OffsetNumber *) palloc(nbytes);
v->spl_right = (OffsetNumber *) palloc(nbytes);
for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k)) {
_k = GETENTRY(entryvec, k);
for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j)) {
size_waste=hemdist(_k, GETENTRY(entryvec, j));
if (size_waste > waste ) {
waste = size_waste;
seed_1 = k;
seed_2 = j;
}
}
}
left = v->spl_left;
v->spl_nleft = 0;
right = v->spl_right;
v->spl_nright = 0;
if (seed_1 == 0 || seed_2 == 0)
{
seed_1 = 1;
seed_2 = 2;
}
/* form initial .. */
if (ISALLTRUE(GETENTRY(entryvec, seed_1))) {
datum_l = (GISTTYPE *) palloc(GTHDRSIZE);
datum_l->len = GTHDRSIZE;
datum_l->flag = ALLISTRUE;
} else {
datum_l = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
datum_l->len = GTHDRSIZE + SIGLEN;
datum_l->flag = 0;
memcpy((void *) GETSIGN(datum_l), (void *) GETSIGN(GETENTRY(entryvec, seed_1)), sizeof(BITVEC))
;
}
if (ISALLTRUE(GETENTRY(entryvec, seed_2))) {
datum_r = (GISTTYPE *) palloc(GTHDRSIZE);
datum_r->len = GTHDRSIZE;
datum_r->flag = ALLISTRUE;
} else {
datum_r = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
datum_r->len = GTHDRSIZE + SIGLEN;
datum_r->flag = 0;
memcpy((void *) GETSIGN(datum_r), (void *) GETSIGN(GETENTRY(entryvec, seed_2)), sizeof(BITVEC)) ;
}
maxoff = OffsetNumberNext(maxoff);
/* sort before ... */
costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
{
costvector[j - 1].pos = j;
_j = GETENTRY(entryvec, j);
size_alpha = hemdist(datum_l,_j);
size_beta = hemdist(datum_r,_j);
costvector[j - 1].cost = abs(size_alpha - size_beta);
}
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
union_l=GETSIGN(datum_l);
union_r=GETSIGN(datum_r);
for (k = 0; k < maxoff; k++) {
j = costvector[k].pos;
if (j == seed_1) {
*left++ = j;
v->spl_nleft++;
continue;
} else if (j == seed_2) {
*right++ = j;
v->spl_nright++;
continue;
}
_j = GETENTRY(entryvec, j);
size_alpha = hemdist(datum_l,_j);
size_beta = hemdist(datum_r,_j);
if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.0001)) {
if (ISALLTRUE(datum_l) || ISALLTRUE(_j) ) {
if (!ISALLTRUE(datum_l))
MemSet((void *) union_l, 0xff, sizeof(BITVEC));
} else {
ptr=GETSIGN(_j);
LOOPBYTE(
union_l[i] |= ptr[i];
);
}
*left++ = j;
v->spl_nleft++;
} else {
if (ISALLTRUE(datum_r) || ISALLTRUE(_j) ) {
if (!ISALLTRUE(datum_r))
MemSet((void *) union_r, 0xff, sizeof(BITVEC));
} else {
ptr=GETSIGN(_j);
LOOPBYTE(
union_r[i] |= ptr[i];
);
}
*right++ = j;
v->spl_nright++;
}
}
*right = *left = FirstOffsetNumber;
pfree(costvector);
v->spl_ldatum = PointerGetDatum(datum_l);
v->spl_rdatum = PointerGetDatum(datum_r);
PG_RETURN_POINTER(v);
}
Datum
ghstore_consistent(PG_FUNCTION_ARGS) {
GISTTYPE *entry = (GISTTYPE*) DatumGetPointer( ((GISTENTRY *) PG_GETARG_POINTER(0))->key );
HStore *query=PG_GETARG_HS(1);
bool res=true;
HEntry *qe = ARRPTR(query);
char *qv = STRPTR(query);
BITVECP sign;
if ( ISALLTRUE(entry) ) {
PG_FREE_IF_COPY(query,1);
PG_RETURN_BOOL(true);
}
sign=GETSIGN(entry);
while(res && qe-ARRPTR(query) < query->size) {
int crc = crc32_sz((char *)(qv + qe->pos), qe->keylen);
if (GETBIT(sign,HASHVAL(crc))) {
if ( !qe->valisnull ) {
crc = crc32_sz((char *)(qv + qe->pos + qe->keylen), qe->vallen);
if ( !GETBIT(sign,HASHVAL(crc)) )
res=false;
}
} else
res=false;
qe++;
}
PG_FREE_IF_COPY(query,1);
PG_RETURN_BOOL(res);
}

370
contrib/hstore/hstore_io.c Normal file
View File

@ -0,0 +1,370 @@
#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);
}

567
contrib/hstore/hstore_op.c Normal file
View File

@ -0,0 +1,567 @@
#include "hstore.h"
#include "utils/array.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include <access/heapam.h>
#include <fmgr.h>
static HEntry *
findkey(HStore *hs, char *key, int keylen) {
HEntry *StopLow = ARRPTR(hs);
HEntry *StopHigh = StopLow + hs->size;
HEntry *StopMiddle;
int difference;
char *base = STRPTR(hs);
while (StopLow < StopHigh) {
StopMiddle = StopLow + (StopHigh - StopLow) / 2;
if ( StopMiddle->keylen == keylen )
difference=strncmp(base+StopMiddle->pos, key, StopMiddle->keylen);
else
difference=(StopMiddle->keylen > keylen) ? 1 : -1;
if (difference == 0)
return StopMiddle;
else if (difference < 0)
StopLow = StopMiddle + 1;
else
StopHigh = StopMiddle;
}
return NULL;
}
PG_FUNCTION_INFO_V1(fetchval);
Datum fetchval(PG_FUNCTION_ARGS);
Datum
fetchval(PG_FUNCTION_ARGS) {
HStore *hs = PG_GETARG_HS(0);
text *key = PG_GETARG_TEXT_P(1);
HEntry *entry;
text *out;
if ((entry=findkey(hs,VARDATA(key), VARSIZE(key)-VARHDRSZ))==NULL || entry->valisnull) {
PG_FREE_IF_COPY(hs,0);
PG_FREE_IF_COPY(key,1);
PG_RETURN_NULL();
}
out=palloc(VARHDRSZ+entry->vallen);
memcpy(VARDATA(out),STRPTR(hs) + entry->pos + entry->keylen, entry->vallen);
VARATT_SIZEP(out) = VARHDRSZ+entry->vallen;
PG_FREE_IF_COPY(hs,0);
PG_FREE_IF_COPY(key,1);
PG_RETURN_POINTER(out);
}
PG_FUNCTION_INFO_V1(exists);
Datum exists(PG_FUNCTION_ARGS);
Datum
exists(PG_FUNCTION_ARGS) {
HStore *hs = PG_GETARG_HS(0);
text *key = PG_GETARG_TEXT_P(1);
HEntry *entry;
entry=findkey(hs,VARDATA(key), VARSIZE(key)-VARHDRSZ);
PG_FREE_IF_COPY(hs,0);
PG_FREE_IF_COPY(key,1);
PG_RETURN_BOOL(entry);
}
PG_FUNCTION_INFO_V1(defined);
Datum defined(PG_FUNCTION_ARGS);
Datum
defined(PG_FUNCTION_ARGS) {
HStore *hs = PG_GETARG_HS(0);
text *key = PG_GETARG_TEXT_P(1);
HEntry *entry;
bool res;
entry=findkey(hs,VARDATA(key), VARSIZE(key)-VARHDRSZ);
res = ( entry && !entry->valisnull ) ? true : false;
PG_FREE_IF_COPY(hs,0);
PG_FREE_IF_COPY(key,1);
PG_RETURN_BOOL(res);
}
PG_FUNCTION_INFO_V1(delete);
Datum delete(PG_FUNCTION_ARGS);
Datum
delete(PG_FUNCTION_ARGS) {
HStore *hs = PG_GETARG_HS(0);
text *key = PG_GETARG_TEXT_P(1);
HStore *out = palloc(hs->len);
char *ptrs, *ptrd;
HEntry *es, *ed;
out->len=hs->len;
out->size=hs->size; /* temprorary! */
ptrs=STRPTR(hs);
es =ARRPTR(hs);
ptrd=STRPTR(out);
ed =ARRPTR(out);
while( es - ARRPTR(hs) < hs->size ) {
if ( !(es->keylen == VARSIZE(key) - VARHDRSZ && strncmp(ptrs, VARDATA(key), es->keylen)==0) ) {
memcpy( ed, es, sizeof(HEntry) );
memcpy( ptrd, ptrs, es->keylen + ( (es->valisnull) ? 0 : es->vallen ) );
ed->pos = ptrd - STRPTR(out);
ptrd += es->keylen + ( (es->valisnull) ? 0 : es->vallen );
ed++;
}
ptrs += es->keylen + ( (es->valisnull) ? 0 : es->vallen );
es++;
}
if ( ed - ARRPTR(out) != out->size ) {
int buflen=ptrd-STRPTR(out);
ptrd = STRPTR(out);
out->size = ed - ARRPTR(out);
memmove( STRPTR(out), ptrd, buflen);
out->len = CALCDATASIZE(out->size, buflen);
}
PG_FREE_IF_COPY(hs,0);
PG_FREE_IF_COPY(key,1);
PG_RETURN_POINTER(out);
}
PG_FUNCTION_INFO_V1(hs_concat);
Datum hs_concat(PG_FUNCTION_ARGS);
Datum
hs_concat(PG_FUNCTION_ARGS) {
HStore *s1 = PG_GETARG_HS(0);
HStore *s2 = PG_GETARG_HS(1);
HStore *out = palloc( s1->len + s2->len );
char *ps1, *ps2, *pd;
HEntry *es1, *es2, *ed;
out->len = s1->len + s2->len;
out->size = s1->size + s2->size;
ps1=STRPTR(s1);
ps2=STRPTR(s2);
pd=STRPTR(out);
es1=ARRPTR(s1);
es2=ARRPTR(s2);
ed=ARRPTR(out);
while( es1 - ARRPTR(s1) < s1->size && es2 - ARRPTR(s2) < s2->size ) {
int difference;
if ( es1->keylen == es2->keylen )
difference=strncmp(ps1, ps2, es1->keylen);
else
difference=(es1->keylen > es2->keylen) ? 1 : -1;
if ( difference == 0 ) {
memcpy( ed, es2, sizeof(HEntry) );
memcpy( pd, ps2, es2->keylen + ( (es2->valisnull) ? 0 : es2->vallen ) );
ed->pos = pd - STRPTR(out);
pd += es2->keylen + ( (es2->valisnull) ? 0 : es2->vallen );
ed++;
ps1 += es1->keylen + ( (es1->valisnull) ? 0 : es1->vallen );
es1++;
ps2 += es2->keylen + ( (es2->valisnull) ? 0 : es2->vallen );
es2++;
} else if ( difference > 0 ) {
memcpy( ed, es2, sizeof(HEntry) );
memcpy( pd, ps2, es2->keylen + ( (es2->valisnull) ? 0 : es2->vallen ) );
ed->pos = pd - STRPTR(out);
pd += es2->keylen + ( (es2->valisnull) ? 0 : es2->vallen );
ed++;
ps2 += es2->keylen + ( (es2->valisnull) ? 0 : es2->vallen );
es2++;
} else {
memcpy( ed, es1, sizeof(HEntry) );
memcpy( pd, ps1, es1->keylen + ( (es1->valisnull) ? 0 : es1->vallen ) );
ed->pos = pd - STRPTR(out);
pd += es1->keylen + ( (es1->valisnull) ? 0 : es1->vallen );
ed++;
ps1 += es1->keylen + ( (es1->valisnull) ? 0 : es1->vallen );
es1++;
}
}
while( es1 - ARRPTR(s1) < s1->size ) {
memcpy( ed, es1, sizeof(HEntry) );
memcpy( pd, ps1, es1->keylen + ( (es1->valisnull) ? 0 : es1->vallen ) );
ed->pos = pd - STRPTR(out);
pd += es1->keylen + ( (es1->valisnull) ? 0 : es1->vallen );
ed++;
ps1 += es1->keylen + ( (es1->valisnull) ? 0 : es1->vallen );
es1++;
}
while( es2 - ARRPTR(s2) < s2->size ) {
memcpy( ed, es2, sizeof(HEntry) );
memcpy( pd, ps2, es2->keylen + ( (es2->valisnull) ? 0 : es2->vallen ) );
ed->pos = pd - STRPTR(out);
pd += es2->keylen + ( (es2->valisnull) ? 0 : es2->vallen );
ed++;
ps2 += es2->keylen + ( (es2->valisnull) ? 0 : es2->vallen );
es2++;
}
if ( ed - ARRPTR(out) != out->size ) {
int buflen=pd-STRPTR(out);
pd = STRPTR(out);
out->size = ed - ARRPTR(out);
memmove( STRPTR(out), pd, buflen);
out->len = CALCDATASIZE(out->size, buflen);
}
PG_FREE_IF_COPY(s1,0);
PG_FREE_IF_COPY(s2,1);
PG_RETURN_POINTER(out);
}
PG_FUNCTION_INFO_V1(tconvert);
Datum tconvert(PG_FUNCTION_ARGS);
Datum
tconvert(PG_FUNCTION_ARGS) {
text *key = PG_GETARG_TEXT_P(0);
text *val = PG_GETARG_TEXT_P(1);
int len;
HStore *out;
len=CALCDATASIZE(1, VARSIZE(key) + VARSIZE(val) - 2*VARHDRSZ);
out = palloc(len);
out->len=len;
out->size=1;
ARRPTR(out)->keylen = VARSIZE(key) - VARHDRSZ;
ARRPTR(out)->vallen = VARSIZE(val) - VARHDRSZ;
ARRPTR(out)->valisnull = false;
ARRPTR(out)->pos=0;
memcpy( STRPTR(out), VARDATA(key), ARRPTR(out)->keylen );
memcpy( STRPTR(out) + ARRPTR(out)->keylen, VARDATA(val), ARRPTR(out)->vallen );
PG_FREE_IF_COPY(key,0);
PG_FREE_IF_COPY(val,1);
PG_RETURN_POINTER(out);
}
PG_FUNCTION_INFO_V1(akeys);
Datum akeys(PG_FUNCTION_ARGS);
Datum
akeys(PG_FUNCTION_ARGS) {
HStore *hs = PG_GETARG_HS(0);
Datum *d;
ArrayType *a;
HEntry *ptr=ARRPTR(hs);
char *base=STRPTR(hs);
d=(Datum*)palloc(sizeof(Datum)*(hs->size+1));
while( ptr-ARRPTR(hs) < hs->size ) {
text *item=(text*)palloc(VARHDRSZ + ptr->keylen);
VARATT_SIZEP(item) = VARHDRSZ+ptr->keylen;
memcpy(VARDATA(item), base + ptr->pos, ptr->keylen);
d[ ptr-ARRPTR(hs) ] = PointerGetDatum(item);
ptr++;
}
a = construct_array(
d,
hs->size,
TEXTOID,
-1,
false,
'i'
);
ptr=ARRPTR(hs);
while( ptr-ARRPTR(hs) < hs->size ) {
pfree(DatumGetPointer(d[ ptr-ARRPTR(hs) ]));
ptr++;
}
pfree(d);
PG_FREE_IF_COPY(hs,0);
PG_RETURN_POINTER(a);
}
PG_FUNCTION_INFO_V1(avals);
Datum avals(PG_FUNCTION_ARGS);
Datum
avals(PG_FUNCTION_ARGS) {
HStore *hs = PG_GETARG_HS(0);
Datum *d;
ArrayType *a;
HEntry *ptr=ARRPTR(hs);
char *base=STRPTR(hs);
d=(Datum*)palloc(sizeof(Datum)*(hs->size+1));
while( ptr-ARRPTR(hs) < hs->size ) {
int vallen = (ptr->valisnull) ? 0 : ptr->vallen;
text *item=(text*)palloc(VARHDRSZ + vallen);
VARATT_SIZEP(item) = VARHDRSZ+vallen;
memcpy(VARDATA(item), base + ptr->pos + ptr->keylen, vallen);
d[ ptr-ARRPTR(hs) ] = PointerGetDatum(item);
ptr++;
}
a = construct_array(
d,
hs->size,
TEXTOID,
-1,
false,
'i'
);
ptr=ARRPTR(hs);
while( ptr-ARRPTR(hs) < hs->size ) {
pfree(DatumGetPointer(d[ ptr-ARRPTR(hs) ]));
ptr++;
}
pfree(d);
PG_FREE_IF_COPY(hs,0);
PG_RETURN_POINTER(a);
}
typedef struct {
HStore *hs;
int i;
} AKStore;
static void
setup_firstcall(FuncCallContext *funcctx, HStore *hs) {
MemoryContext oldcontext;
AKStore *st;
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
st=(AKStore*)palloc( sizeof(AKStore) );
st->i=0;
st->hs = (HStore*)palloc(hs->len);
memcpy( st->hs, hs, hs->len );
funcctx->user_fctx = (void*)st;
MemoryContextSwitchTo(oldcontext);
}
PG_FUNCTION_INFO_V1(skeys);
Datum skeys(PG_FUNCTION_ARGS);
Datum
skeys(PG_FUNCTION_ARGS) {
FuncCallContext *funcctx;
AKStore *st;
if (SRF_IS_FIRSTCALL()) {
HStore *hs = PG_GETARG_HS(0);
funcctx = SRF_FIRSTCALL_INIT();
setup_firstcall(funcctx, hs);
PG_FREE_IF_COPY(hs,0);
}
funcctx = SRF_PERCALL_SETUP();
st = (AKStore*)funcctx->user_fctx;
if ( st->i < st->hs->size ) {
HEntry *ptr = &(ARRPTR(st->hs)[st->i]);
text *item=(text*)palloc(VARHDRSZ + ptr->keylen);
VARATT_SIZEP(item) = VARHDRSZ+ptr->keylen;
memcpy(VARDATA(item), STRPTR(st->hs) + ptr->pos, ptr->keylen);
st->i++;
SRF_RETURN_NEXT(funcctx, PointerGetDatum(item));
}
pfree( st->hs );
pfree( st );
SRF_RETURN_DONE(funcctx);
}
PG_FUNCTION_INFO_V1(svals);
Datum svals(PG_FUNCTION_ARGS);
Datum
svals(PG_FUNCTION_ARGS) {
FuncCallContext *funcctx;
AKStore *st;
if (SRF_IS_FIRSTCALL()) {
HStore *hs = PG_GETARG_HS(0);
funcctx = SRF_FIRSTCALL_INIT();
setup_firstcall(funcctx, hs);
PG_FREE_IF_COPY(hs,0);
}
funcctx = SRF_PERCALL_SETUP();
st = (AKStore*)funcctx->user_fctx;
if ( st->i < st->hs->size ) {
HEntry *ptr = &(ARRPTR(st->hs)[st->i]);
if ( ptr->valisnull ) {
ReturnSetInfo *rsi;
st->i++;
(funcctx)->call_cntr++;
rsi = (ReturnSetInfo *) fcinfo->resultinfo;
rsi->isDone = ExprMultipleResult;
PG_RETURN_NULL();
} else {
int vallen = ptr->vallen;
text *item=(text*)palloc(VARHDRSZ + vallen);
VARATT_SIZEP(item) = VARHDRSZ+vallen;
memcpy(VARDATA(item), STRPTR(st->hs) + ptr->pos + ptr->keylen, vallen);
st->i++;
SRF_RETURN_NEXT(funcctx, PointerGetDatum(item));
}
}
pfree( st->hs );
pfree( st );
SRF_RETURN_DONE(funcctx);
}
PG_FUNCTION_INFO_V1(hs_contains);
Datum hs_contains(PG_FUNCTION_ARGS);
Datum
hs_contains(PG_FUNCTION_ARGS) {
HStore *val = PG_GETARG_HS(0);
HStore *tmpl = PG_GETARG_HS(1);
bool res = true;
HEntry *te = ARRPTR(tmpl);
char *vv = STRPTR(val);
char *tv = STRPTR(tmpl);
while(res && te-ARRPTR(tmpl) < tmpl->size) {
HEntry *entry = findkey(val, tv + te->pos, te->keylen);
if ( entry ) {
if ( ! te->valisnull ) {
if ( entry->valisnull || !(
te->vallen==entry->vallen &&
strncmp(
vv + entry->pos + entry->keylen,
tv + te->pos + te->keylen,
te->vallen ) == 0
) )
res=false;
}
} else
res = false;
te++;
}
PG_FREE_IF_COPY(val,0);
PG_FREE_IF_COPY(tmpl,1);
PG_RETURN_BOOL(res);
}
PG_FUNCTION_INFO_V1(hs_contained);
Datum hs_contained(PG_FUNCTION_ARGS);
Datum
hs_contained(PG_FUNCTION_ARGS) {
PG_RETURN_DATUM( DirectFunctionCall2(
hs_contains,
PG_GETARG_DATUM(1),
PG_GETARG_DATUM(0)
));
}
PG_FUNCTION_INFO_V1(each);
Datum each(PG_FUNCTION_ARGS);
Datum
each(PG_FUNCTION_ARGS) {
FuncCallContext *funcctx;
AKStore *st;
if (SRF_IS_FIRSTCALL()) {
TupleDesc tupdesc;
MemoryContext oldcontext;
HStore *hs = PG_GETARG_HS(0);
funcctx = SRF_FIRSTCALL_INIT();
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
st=(AKStore*)palloc( sizeof(AKStore) );
st->i=0;
st->hs = (HStore*)palloc(hs->len);
memcpy( st->hs, hs, hs->len );
funcctx->user_fctx = (void*)st;
tupdesc = RelationNameGetTupleDesc("hs_each");
funcctx->slot = TupleDescGetSlot(tupdesc);
funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
MemoryContextSwitchTo(oldcontext);
PG_FREE_IF_COPY(hs,0);
}
funcctx = SRF_PERCALL_SETUP();
st = (AKStore*)funcctx->user_fctx;
if ( st->i < st->hs->size ) {
HEntry *ptr = &(ARRPTR(st->hs)[st->i]);
Datum res, dvalues[2];
char nulls[] = {' ', ' '};
text *item;
HeapTuple tuple;
item=(text*)palloc(VARHDRSZ + ptr->keylen);
VARATT_SIZEP(item) = VARHDRSZ+ptr->keylen;
memcpy(VARDATA(item), STRPTR(st->hs) + ptr->pos, ptr->keylen);
dvalues[0] = PointerGetDatum(item);
if ( ptr->valisnull ) {
dvalues[1]=(Datum)0;
nulls[1]='n';
} else {
int vallen = ptr->vallen;
item=(text*)palloc(VARHDRSZ + vallen);
VARATT_SIZEP(item) = VARHDRSZ+vallen;
memcpy(VARDATA(item), STRPTR(st->hs) + ptr->pos + ptr->keylen, vallen);
dvalues[1] = PointerGetDatum(item);
}
st->i++;
tuple = heap_formtuple(funcctx->attinmeta->tupdesc, dvalues, nulls);
res = TupleGetDatum(funcctx->slot, tuple);
pfree( DatumGetPointer(dvalues[0]) );
if ( nulls[1] != 'n' )
pfree( DatumGetPointer(dvalues[1]) );
SRF_RETURN_NEXT(funcctx, PointerGetDatum(res));
}
pfree( st->hs );
pfree( st );
SRF_RETURN_DONE(funcctx);
}

View File

@ -0,0 +1,131 @@
\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;