2008-05-12 02:00:54 +02:00
|
|
|
/*
|
2010-09-20 22:08:53 +02:00
|
|
|
* contrib/hstore/hstore_op.c
|
2008-05-12 02:00:54 +02:00
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2012-08-30 22:15:44 +02:00
|
|
|
#include "access/htup_details.h"
|
2006-09-05 20:00:58 +02:00
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "funcapi.h"
|
2008-05-12 02:00:54 +02:00
|
|
|
#include "utils/builtins.h"
|
2019-03-11 17:17:50 +01:00
|
|
|
#include "utils/hashutils.h"
|
2014-02-17 15:33:31 +01:00
|
|
|
#include "utils/memutils.h"
|
2008-05-12 02:00:54 +02:00
|
|
|
|
|
|
|
#include "hstore.h"
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
/* old names for C functions */
|
2010-02-26 03:01:40 +01:00
|
|
|
HSTORE_POLLUTE(hstore_fetchval, fetchval);
|
|
|
|
HSTORE_POLLUTE(hstore_exists, exists);
|
|
|
|
HSTORE_POLLUTE(hstore_defined, defined);
|
|
|
|
HSTORE_POLLUTE(hstore_delete, delete);
|
|
|
|
HSTORE_POLLUTE(hstore_concat, hs_concat);
|
|
|
|
HSTORE_POLLUTE(hstore_contains, hs_contains);
|
|
|
|
HSTORE_POLLUTE(hstore_contained, hs_contained);
|
|
|
|
HSTORE_POLLUTE(hstore_akeys, akeys);
|
|
|
|
HSTORE_POLLUTE(hstore_avals, avals);
|
|
|
|
HSTORE_POLLUTE(hstore_skeys, skeys);
|
|
|
|
HSTORE_POLLUTE(hstore_svals, svals);
|
|
|
|
HSTORE_POLLUTE(hstore_each, each);
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We're often finding a sequence of keys in ascending order. The
|
|
|
|
* "lowbound" parameter is used to cache lower bounds of searches
|
|
|
|
* between calls, based on this assumption. Pass NULL for it for
|
|
|
|
* one-off or unordered searches.
|
|
|
|
*/
|
|
|
|
int
|
2010-02-26 03:01:40 +01:00
|
|
|
hstoreFindKey(HStore *hs, int *lowbound, char *key, int keylen)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2009-09-30 21:50:22 +02:00
|
|
|
HEntry *entries = ARRPTR(hs);
|
2010-02-26 03:01:40 +01:00
|
|
|
int stopLow = lowbound ? *lowbound : 0;
|
|
|
|
int stopHigh = HS_COUNT(hs);
|
|
|
|
int stopMiddle;
|
2006-10-04 02:30:14 +02:00
|
|
|
char *base = STRPTR(hs);
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
while (stopLow < stopHigh)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
int difference;
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
stopMiddle = stopLow + (stopHigh - stopLow) / 2;
|
|
|
|
|
2015-11-19 20:54:05 +01:00
|
|
|
if (HSTORE_KEYLEN(entries, stopMiddle) == keylen)
|
|
|
|
difference = memcmp(HSTORE_KEY(entries, base, stopMiddle), key, keylen);
|
2006-09-05 20:00:58 +02:00
|
|
|
else
|
2015-11-19 20:54:05 +01:00
|
|
|
difference = (HSTORE_KEYLEN(entries, stopMiddle) > keylen) ? 1 : -1;
|
2006-09-05 20:00:58 +02:00
|
|
|
|
|
|
|
if (difference == 0)
|
2009-09-30 21:50:22 +02:00
|
|
|
{
|
|
|
|
if (lowbound)
|
|
|
|
*lowbound = stopMiddle + 1;
|
|
|
|
return stopMiddle;
|
|
|
|
}
|
2006-09-05 20:00:58 +02:00
|
|
|
else if (difference < 0)
|
2009-09-30 21:50:22 +02:00
|
|
|
stopLow = stopMiddle + 1;
|
2006-09-05 20:00:58 +02:00
|
|
|
else
|
2009-09-30 21:50:22 +02:00
|
|
|
stopHigh = stopMiddle;
|
2006-09-05 20:00:58 +02:00
|
|
|
}
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
if (lowbound)
|
|
|
|
*lowbound = stopLow;
|
|
|
|
return -1;
|
2006-09-05 20:00:58 +02:00
|
|
|
}
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
Pairs *
|
|
|
|
hstoreArrayToPairs(ArrayType *a, int *npairs)
|
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
Datum *key_datums;
|
|
|
|
bool *key_nulls;
|
|
|
|
int key_count;
|
|
|
|
Pairs *key_pairs;
|
|
|
|
int bufsiz;
|
|
|
|
int i,
|
|
|
|
j;
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
deconstruct_array(a,
|
|
|
|
TEXTOID, -1, false, 'i',
|
|
|
|
&key_datums, &key_nulls, &key_count);
|
|
|
|
|
|
|
|
if (key_count == 0)
|
|
|
|
{
|
|
|
|
*npairs = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-02-17 15:33:31 +01:00
|
|
|
/*
|
|
|
|
* A text array uses at least eight bytes per element, so any overflow in
|
|
|
|
* "key_count * sizeof(Pairs)" is small enough for palloc() to catch.
|
|
|
|
* However, credible improvements to the array format could invalidate
|
|
|
|
* that assumption. Therefore, use an explicit check rather than relying
|
|
|
|
* on palloc() to complain.
|
|
|
|
*/
|
|
|
|
if (key_count > MaxAllocSize / sizeof(Pairs))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
errmsg("number of pairs (%d) exceeds the maximum allowed (%d)",
|
|
|
|
key_count, (int) (MaxAllocSize / sizeof(Pairs)))));
|
2014-02-17 15:33:31 +01:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
key_pairs = palloc(sizeof(Pairs) * key_count);
|
|
|
|
|
|
|
|
for (i = 0, j = 0; i < key_count; i++)
|
|
|
|
{
|
|
|
|
if (!key_nulls[i])
|
|
|
|
{
|
|
|
|
key_pairs[j].key = VARDATA(key_datums[i]);
|
|
|
|
key_pairs[j].keylen = VARSIZE(key_datums[i]) - VARHDRSZ;
|
|
|
|
key_pairs[j].val = NULL;
|
|
|
|
key_pairs[j].vallen = 0;
|
|
|
|
key_pairs[j].needfree = 0;
|
|
|
|
key_pairs[j].isnull = 1;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*npairs = hstoreUniquePairs(key_pairs, j, &bufsiz);
|
|
|
|
|
|
|
|
return key_pairs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_fetchval);
|
2006-09-05 20:00:58 +02:00
|
|
|
Datum
|
2009-09-30 21:50:22 +02:00
|
|
|
hstore_fetchval(PG_FUNCTION_ARGS)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
HStore *hs = PG_GETARG_HSTORE_P(0);
|
2009-09-30 21:50:22 +02:00
|
|
|
text *key = PG_GETARG_TEXT_PP(1);
|
|
|
|
HEntry *entries = ARRPTR(hs);
|
2006-10-04 02:30:14 +02:00
|
|
|
text *out;
|
2010-02-26 03:01:40 +01:00
|
|
|
int idx = hstoreFindKey(hs, NULL,
|
2009-09-30 21:50:22 +02:00
|
|
|
VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key));
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2015-11-19 20:54:05 +01:00
|
|
|
if (idx < 0 || HSTORE_VALISNULL(entries, idx))
|
2006-09-05 20:00:58 +02:00
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2015-11-19 20:54:05 +01:00
|
|
|
out = cstring_to_text_with_len(HSTORE_VAL(entries, STRPTR(hs), idx),
|
|
|
|
HSTORE_VALLEN(entries, idx));
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2008-03-25 23:42:46 +01:00
|
|
|
PG_RETURN_TEXT_P(out);
|
2006-09-05 20:00:58 +02:00
|
|
|
}
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_exists);
|
2006-09-05 20:00:58 +02:00
|
|
|
Datum
|
2009-09-30 21:50:22 +02:00
|
|
|
hstore_exists(PG_FUNCTION_ARGS)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
HStore *hs = PG_GETARG_HSTORE_P(0);
|
2009-09-30 21:50:22 +02:00
|
|
|
text *key = PG_GETARG_TEXT_PP(1);
|
2010-02-26 03:01:40 +01:00
|
|
|
int idx = hstoreFindKey(hs, NULL,
|
2009-09-30 21:50:22 +02:00
|
|
|
VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key));
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
PG_RETURN_BOOL(idx >= 0);
|
|
|
|
}
|
2006-09-05 20:00:58 +02:00
|
|
|
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
PG_FUNCTION_INFO_V1(hstore_exists_any);
|
|
|
|
Datum
|
|
|
|
hstore_exists_any(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
HStore *hs = PG_GETARG_HSTORE_P(0);
|
2009-09-30 21:50:22 +02:00
|
|
|
ArrayType *keys = PG_GETARG_ARRAYTYPE_P(1);
|
2010-02-26 03:01:40 +01:00
|
|
|
int nkeys;
|
|
|
|
Pairs *key_pairs = hstoreArrayToPairs(keys, &nkeys);
|
|
|
|
int i;
|
|
|
|
int lowbound = 0;
|
|
|
|
bool res = false;
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* we exploit the fact that the pairs list is already sorted into strictly
|
|
|
|
* increasing order to narrow the hstoreFindKey search; each search can
|
|
|
|
* start one entry past the previous "found" entry, or at the lower bound
|
|
|
|
* of the last search.
|
2009-09-30 21:50:22 +02:00
|
|
|
*/
|
2011-01-09 22:43:56 +01:00
|
|
|
for (i = 0; i < nkeys; i++)
|
2009-09-30 21:50:22 +02:00
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
int idx = hstoreFindKey(hs, &lowbound,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
key_pairs[i].key, key_pairs[i].keylen);
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
if (idx >= 0)
|
2011-01-09 22:43:56 +01:00
|
|
|
{
|
2009-09-30 21:50:22 +02:00
|
|
|
res = true;
|
2011-01-09 22:43:56 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-09-30 21:50:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(res);
|
2006-09-05 20:00:58 +02:00
|
|
|
}
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_exists_all);
|
2006-09-05 20:00:58 +02:00
|
|
|
Datum
|
2009-09-30 21:50:22 +02:00
|
|
|
hstore_exists_all(PG_FUNCTION_ARGS)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
HStore *hs = PG_GETARG_HSTORE_P(0);
|
2009-09-30 21:50:22 +02:00
|
|
|
ArrayType *keys = PG_GETARG_ARRAYTYPE_P(1);
|
2010-02-26 03:01:40 +01:00
|
|
|
int nkeys;
|
|
|
|
Pairs *key_pairs = hstoreArrayToPairs(keys, &nkeys);
|
|
|
|
int i;
|
|
|
|
int lowbound = 0;
|
2011-01-09 22:43:56 +01:00
|
|
|
bool res = true;
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* we exploit the fact that the pairs list is already sorted into strictly
|
|
|
|
* increasing order to narrow the hstoreFindKey search; each search can
|
|
|
|
* start one entry past the previous "found" entry, or at the lower bound
|
|
|
|
* of the last search.
|
2009-09-30 21:50:22 +02:00
|
|
|
*/
|
2011-01-09 22:43:56 +01:00
|
|
|
for (i = 0; i < nkeys; i++)
|
2009-09-30 21:50:22 +02:00
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
int idx = hstoreFindKey(hs, &lowbound,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
key_pairs[i].key, key_pairs[i].keylen);
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
if (idx < 0)
|
2011-01-09 22:43:56 +01:00
|
|
|
{
|
2009-09-30 21:50:22 +02:00
|
|
|
res = false;
|
2011-01-09 22:43:56 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-09-30 21:50:22 +02:00
|
|
|
}
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
PG_RETURN_BOOL(res);
|
|
|
|
}
|
2006-09-05 20:00:58 +02:00
|
|
|
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
PG_FUNCTION_INFO_V1(hstore_defined);
|
|
|
|
Datum
|
|
|
|
hstore_defined(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
HStore *hs = PG_GETARG_HSTORE_P(0);
|
2009-09-30 21:50:22 +02:00
|
|
|
text *key = PG_GETARG_TEXT_PP(1);
|
|
|
|
HEntry *entries = ARRPTR(hs);
|
2010-02-26 03:01:40 +01:00
|
|
|
int idx = hstoreFindKey(hs, NULL,
|
2009-09-30 21:50:22 +02:00
|
|
|
VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key));
|
2015-11-19 20:54:05 +01:00
|
|
|
bool res = (idx >= 0 && !HSTORE_VALISNULL(entries, idx));
|
2006-09-05 20:00:58 +02:00
|
|
|
|
|
|
|
PG_RETURN_BOOL(res);
|
|
|
|
}
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_delete);
|
2006-09-05 20:00:58 +02:00
|
|
|
Datum
|
2009-09-30 21:50:22 +02:00
|
|
|
hstore_delete(PG_FUNCTION_ARGS)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
HStore *hs = PG_GETARG_HSTORE_P(0);
|
2009-09-30 21:50:22 +02:00
|
|
|
text *key = PG_GETARG_TEXT_PP(1);
|
2010-02-26 03:01:40 +01:00
|
|
|
char *keyptr = VARDATA_ANY(key);
|
|
|
|
int keylen = VARSIZE_ANY_EXHDR(key);
|
2007-02-28 23:44:38 +01:00
|
|
|
HStore *out = palloc(VARSIZE(hs));
|
2009-09-30 21:50:22 +02:00
|
|
|
char *bufs,
|
2010-02-26 03:01:40 +01:00
|
|
|
*bufd,
|
2006-10-04 02:30:14 +02:00
|
|
|
*ptrd;
|
|
|
|
HEntry *es,
|
|
|
|
*ed;
|
2010-02-26 03:01:40 +01:00
|
|
|
int i;
|
|
|
|
int count = HS_COUNT(hs);
|
|
|
|
int outcount = 0;
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2007-02-28 23:44:38 +01:00
|
|
|
SET_VARSIZE(out, VARSIZE(hs));
|
2010-02-26 03:01:40 +01:00
|
|
|
HS_SETCOUNT(out, count); /* temporary! */
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
bufs = STRPTR(hs);
|
2006-10-04 02:30:14 +02:00
|
|
|
es = ARRPTR(hs);
|
2009-09-30 21:50:22 +02:00
|
|
|
bufd = ptrd = STRPTR(out);
|
2006-10-04 02:30:14 +02:00
|
|
|
ed = ARRPTR(out);
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
for (i = 0; i < count; ++i)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2015-11-19 20:54:05 +01:00
|
|
|
int len = HSTORE_KEYLEN(es, i);
|
|
|
|
char *ptrs = HSTORE_KEY(es, bufs, i);
|
2009-09-30 21:50:22 +02:00
|
|
|
|
2010-12-22 04:11:40 +01:00
|
|
|
if (!(len == keylen && memcmp(ptrs, keyptr, keylen) == 0))
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2015-11-19 20:54:05 +01:00
|
|
|
int vallen = HSTORE_VALLEN(es, i);
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2015-11-19 20:54:05 +01:00
|
|
|
HS_COPYITEM(ed, bufd, ptrd, ptrs, len, vallen,
|
|
|
|
HSTORE_VALISNULL(es, i));
|
2009-09-30 21:50:22 +02:00
|
|
|
++outcount;
|
2006-09-05 20:00:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-26 03:01:40 +01:00
|
|
|
HS_FINALIZE(out, outcount, bufd, ptrd);
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_delete_array);
|
|
|
|
Datum
|
|
|
|
hstore_delete_array(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
HStore *hs = PG_GETARG_HSTORE_P(0);
|
2009-09-30 21:50:22 +02:00
|
|
|
HStore *out = palloc(VARSIZE(hs));
|
2010-02-26 03:01:40 +01:00
|
|
|
int hs_count = HS_COUNT(hs);
|
2009-09-30 21:50:22 +02:00
|
|
|
char *ps,
|
2010-02-26 03:01:40 +01:00
|
|
|
*bufd,
|
2009-09-30 21:50:22 +02:00
|
|
|
*pd;
|
|
|
|
HEntry *es,
|
|
|
|
*ed;
|
2010-02-26 03:01:40 +01:00
|
|
|
int i,
|
|
|
|
j;
|
|
|
|
int outcount = 0;
|
2009-09-30 21:50:22 +02:00
|
|
|
ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(1);
|
2010-02-26 03:01:40 +01:00
|
|
|
int nkeys;
|
|
|
|
Pairs *key_pairs = hstoreArrayToPairs(key_array, &nkeys);
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
SET_VARSIZE(out, VARSIZE(hs));
|
2010-02-26 03:01:40 +01:00
|
|
|
HS_SETCOUNT(out, hs_count); /* temporary! */
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
ps = STRPTR(hs);
|
|
|
|
es = ARRPTR(hs);
|
|
|
|
bufd = pd = STRPTR(out);
|
|
|
|
ed = ARRPTR(out);
|
|
|
|
|
|
|
|
if (nkeys == 0)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2009-09-30 21:50:22 +02:00
|
|
|
/* return a copy of the input, unchanged */
|
|
|
|
memcpy(out, hs, VARSIZE(hs));
|
|
|
|
HS_FIXSIZE(out, hs_count);
|
|
|
|
HS_SETCOUNT(out, hs_count);
|
|
|
|
PG_RETURN_POINTER(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* this is in effect a merge between hs and key_pairs, both of which are
|
|
|
|
* already sorted by (keylen,key); we take keys from hs only
|
2009-09-30 21:50:22 +02:00
|
|
|
*/
|
|
|
|
|
2010-02-26 03:01:40 +01:00
|
|
|
for (i = j = 0; i < hs_count;)
|
2009-09-30 21:50:22 +02:00
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
int difference;
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
if (j >= nkeys)
|
|
|
|
difference = -1;
|
|
|
|
else
|
|
|
|
{
|
2015-11-19 20:54:05 +01:00
|
|
|
int skeylen = HSTORE_KEYLEN(es, i);
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
if (skeylen == key_pairs[j].keylen)
|
2015-11-19 20:54:05 +01:00
|
|
|
difference = memcmp(HSTORE_KEY(es, ps, i),
|
2010-12-22 04:11:40 +01:00
|
|
|
key_pairs[j].key,
|
|
|
|
key_pairs[j].keylen);
|
2009-09-30 21:50:22 +02:00
|
|
|
else
|
|
|
|
difference = (skeylen > key_pairs[j].keylen) ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (difference > 0)
|
|
|
|
++j;
|
|
|
|
else if (difference == 0)
|
|
|
|
++i, ++j;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HS_COPYITEM(ed, bufd, pd,
|
2015-11-19 20:54:05 +01:00
|
|
|
HSTORE_KEY(es, ps, i), HSTORE_KEYLEN(es, i),
|
|
|
|
HSTORE_VALLEN(es, i), HSTORE_VALISNULL(es, i));
|
2009-09-30 21:50:22 +02:00
|
|
|
++outcount;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-26 03:01:40 +01:00
|
|
|
HS_FINALIZE(out, outcount, bufd, pd);
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(out);
|
|
|
|
}
|
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
PG_FUNCTION_INFO_V1(hstore_delete_hstore);
|
|
|
|
Datum
|
|
|
|
hstore_delete_hstore(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
HStore *hs = PG_GETARG_HSTORE_P(0);
|
|
|
|
HStore *hs2 = PG_GETARG_HSTORE_P(1);
|
2009-09-30 21:50:22 +02:00
|
|
|
HStore *out = palloc(VARSIZE(hs));
|
2010-02-26 03:01:40 +01:00
|
|
|
int hs_count = HS_COUNT(hs);
|
|
|
|
int hs2_count = HS_COUNT(hs2);
|
2009-09-30 21:50:22 +02:00
|
|
|
char *ps,
|
2010-02-26 03:01:40 +01:00
|
|
|
*ps2,
|
|
|
|
*bufd,
|
2009-09-30 21:50:22 +02:00
|
|
|
*pd;
|
|
|
|
HEntry *es,
|
2010-02-26 03:01:40 +01:00
|
|
|
*es2,
|
2009-09-30 21:50:22 +02:00
|
|
|
*ed;
|
2010-02-26 03:01:40 +01:00
|
|
|
int i,
|
|
|
|
j;
|
|
|
|
int outcount = 0;
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
SET_VARSIZE(out, VARSIZE(hs));
|
2010-02-26 03:01:40 +01:00
|
|
|
HS_SETCOUNT(out, hs_count); /* temporary! */
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
ps = STRPTR(hs);
|
|
|
|
es = ARRPTR(hs);
|
|
|
|
ps2 = STRPTR(hs2);
|
|
|
|
es2 = ARRPTR(hs2);
|
|
|
|
bufd = pd = STRPTR(out);
|
|
|
|
ed = ARRPTR(out);
|
|
|
|
|
|
|
|
if (hs2_count == 0)
|
|
|
|
{
|
|
|
|
/* return a copy of the input, unchanged */
|
|
|
|
memcpy(out, hs, VARSIZE(hs));
|
|
|
|
HS_FIXSIZE(out, hs_count);
|
|
|
|
HS_SETCOUNT(out, hs_count);
|
|
|
|
PG_RETURN_POINTER(out);
|
2006-09-05 20:00:58 +02:00
|
|
|
}
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* this is in effect a merge between hs and hs2, both of which are already
|
|
|
|
* sorted by (keylen,key); we take keys from hs only; for equal keys, we
|
|
|
|
* take the value from hs unless the values are equal
|
2009-09-30 21:50:22 +02:00
|
|
|
*/
|
|
|
|
|
2010-02-26 03:01:40 +01:00
|
|
|
for (i = j = 0; i < hs_count;)
|
2009-09-30 21:50:22 +02:00
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
int difference;
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
if (j >= hs2_count)
|
|
|
|
difference = -1;
|
|
|
|
else
|
|
|
|
{
|
2015-11-19 20:54:05 +01:00
|
|
|
int skeylen = HSTORE_KEYLEN(es, i);
|
|
|
|
int s2keylen = HSTORE_KEYLEN(es2, j);
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
if (skeylen == s2keylen)
|
2015-11-19 20:54:05 +01:00
|
|
|
difference = memcmp(HSTORE_KEY(es, ps, i),
|
|
|
|
HSTORE_KEY(es2, ps2, j),
|
2010-12-22 04:11:40 +01:00
|
|
|
skeylen);
|
2009-09-30 21:50:22 +02:00
|
|
|
else
|
|
|
|
difference = (skeylen > s2keylen) ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (difference > 0)
|
|
|
|
++j;
|
|
|
|
else if (difference == 0)
|
|
|
|
{
|
2015-11-19 20:54:05 +01:00
|
|
|
int svallen = HSTORE_VALLEN(es, i);
|
|
|
|
int snullval = HSTORE_VALISNULL(es, i);
|
|
|
|
|
|
|
|
if (snullval != HSTORE_VALISNULL(es2, j) ||
|
|
|
|
(!snullval && (svallen != HSTORE_VALLEN(es2, j) ||
|
|
|
|
memcmp(HSTORE_VAL(es, ps, i),
|
|
|
|
HSTORE_VAL(es2, ps2, j),
|
|
|
|
svallen) != 0)))
|
2009-09-30 21:50:22 +02:00
|
|
|
{
|
|
|
|
HS_COPYITEM(ed, bufd, pd,
|
2015-11-19 20:54:05 +01:00
|
|
|
HSTORE_KEY(es, ps, i), HSTORE_KEYLEN(es, i),
|
2009-09-30 21:50:22 +02:00
|
|
|
svallen, snullval);
|
|
|
|
++outcount;
|
|
|
|
}
|
|
|
|
++i, ++j;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HS_COPYITEM(ed, bufd, pd,
|
2015-11-19 20:54:05 +01:00
|
|
|
HSTORE_KEY(es, ps, i), HSTORE_KEYLEN(es, i),
|
|
|
|
HSTORE_VALLEN(es, i), HSTORE_VALISNULL(es, i));
|
2009-09-30 21:50:22 +02:00
|
|
|
++outcount;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2010-02-26 03:01:40 +01:00
|
|
|
HS_FINALIZE(out, outcount, bufd, pd);
|
2006-09-05 20:00:58 +02:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(out);
|
|
|
|
}
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_concat);
|
2006-09-05 20:00:58 +02:00
|
|
|
Datum
|
2009-09-30 21:50:22 +02:00
|
|
|
hstore_concat(PG_FUNCTION_ARGS)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
HStore *s1 = PG_GETARG_HSTORE_P(0);
|
|
|
|
HStore *s2 = PG_GETARG_HSTORE_P(1);
|
2007-02-28 23:44:38 +01:00
|
|
|
HStore *out = palloc(VARSIZE(s1) + VARSIZE(s2));
|
2006-10-04 02:30:14 +02:00
|
|
|
char *ps1,
|
|
|
|
*ps2,
|
2010-02-26 03:01:40 +01:00
|
|
|
*bufd,
|
2006-10-04 02:30:14 +02:00
|
|
|
*pd;
|
|
|
|
HEntry *es1,
|
|
|
|
*es2,
|
|
|
|
*ed;
|
2010-02-26 03:01:40 +01:00
|
|
|
int s1idx;
|
|
|
|
int s2idx;
|
|
|
|
int s1count = HS_COUNT(s1);
|
|
|
|
int s2count = HS_COUNT(s2);
|
|
|
|
int outcount = 0;
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
SET_VARSIZE(out, VARSIZE(s1) + VARSIZE(s2) - HSHRDSIZE);
|
|
|
|
HS_SETCOUNT(out, s1count + s2count);
|
|
|
|
|
|
|
|
if (s1count == 0)
|
|
|
|
{
|
|
|
|
/* return a copy of the input, unchanged */
|
|
|
|
memcpy(out, s2, VARSIZE(s2));
|
|
|
|
HS_FIXSIZE(out, s2count);
|
|
|
|
HS_SETCOUNT(out, s2count);
|
|
|
|
PG_RETURN_POINTER(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s2count == 0)
|
|
|
|
{
|
|
|
|
/* return a copy of the input, unchanged */
|
|
|
|
memcpy(out, s1, VARSIZE(s1));
|
|
|
|
HS_FIXSIZE(out, s1count);
|
|
|
|
HS_SETCOUNT(out, s1count);
|
|
|
|
PG_RETURN_POINTER(out);
|
|
|
|
}
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
ps1 = STRPTR(s1);
|
|
|
|
ps2 = STRPTR(s2);
|
2009-09-30 21:50:22 +02:00
|
|
|
bufd = pd = STRPTR(out);
|
2006-10-04 02:30:14 +02:00
|
|
|
es1 = ARRPTR(s1);
|
|
|
|
es2 = ARRPTR(s2);
|
|
|
|
ed = ARRPTR(out);
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* this is in effect a merge between s1 and s2, both of which are already
|
|
|
|
* sorted by (keylen,key); we take s2 for equal keys
|
2009-09-30 21:50:22 +02:00
|
|
|
*/
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
for (s1idx = s2idx = 0; s1idx < s1count || s2idx < s2count; ++outcount)
|
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
int difference;
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
if (s1idx >= s1count)
|
|
|
|
difference = 1;
|
|
|
|
else if (s2idx >= s2count)
|
|
|
|
difference = -1;
|
2006-09-05 20:00:58 +02:00
|
|
|
else
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2015-11-19 20:54:05 +01:00
|
|
|
int s1keylen = HSTORE_KEYLEN(es1, s1idx);
|
|
|
|
int s2keylen = HSTORE_KEYLEN(es2, s2idx);
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
if (s1keylen == s2keylen)
|
2015-11-19 20:54:05 +01:00
|
|
|
difference = memcmp(HSTORE_KEY(es1, ps1, s1idx),
|
|
|
|
HSTORE_KEY(es2, ps2, s2idx),
|
2010-12-22 04:11:40 +01:00
|
|
|
s1keylen);
|
2009-09-30 21:50:22 +02:00
|
|
|
else
|
|
|
|
difference = (s1keylen > s2keylen) ? 1 : -1;
|
2006-10-04 02:30:14 +02:00
|
|
|
}
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
if (difference >= 0)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2009-09-30 21:50:22 +02:00
|
|
|
HS_COPYITEM(ed, bufd, pd,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
HSTORE_KEY(es2, ps2, s2idx), HSTORE_KEYLEN(es2, s2idx),
|
|
|
|
HSTORE_VALLEN(es2, s2idx), HSTORE_VALISNULL(es2, s2idx));
|
2009-09-30 21:50:22 +02:00
|
|
|
++s2idx;
|
|
|
|
if (difference == 0)
|
|
|
|
++s1idx;
|
2006-10-04 02:30:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-30 21:50:22 +02:00
|
|
|
HS_COPYITEM(ed, bufd, pd,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
HSTORE_KEY(es1, ps1, s1idx), HSTORE_KEYLEN(es1, s1idx),
|
|
|
|
HSTORE_VALLEN(es1, s1idx), HSTORE_VALISNULL(es1, s1idx));
|
2009-09-30 21:50:22 +02:00
|
|
|
++s1idx;
|
2006-09-05 20:00:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-26 03:01:40 +01:00
|
|
|
HS_FINALIZE(out, outcount, bufd, pd);
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
PG_RETURN_POINTER(out);
|
|
|
|
}
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
PG_FUNCTION_INFO_V1(hstore_slice_to_array);
|
|
|
|
Datum
|
|
|
|
hstore_slice_to_array(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
HStore *hs = PG_GETARG_HSTORE_P(0);
|
2009-09-30 21:50:22 +02:00
|
|
|
HEntry *entries = ARRPTR(hs);
|
2010-02-26 03:01:40 +01:00
|
|
|
char *ptr = STRPTR(hs);
|
2009-09-30 21:50:22 +02:00
|
|
|
ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(1);
|
|
|
|
ArrayType *aout;
|
2010-02-26 03:01:40 +01:00
|
|
|
Datum *key_datums;
|
|
|
|
bool *key_nulls;
|
|
|
|
Datum *out_datums;
|
|
|
|
bool *out_nulls;
|
|
|
|
int key_count;
|
|
|
|
int i;
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
deconstruct_array(key_array,
|
|
|
|
TEXTOID, -1, false, 'i',
|
|
|
|
&key_datums, &key_nulls, &key_count);
|
|
|
|
|
|
|
|
if (key_count == 0)
|
|
|
|
{
|
|
|
|
aout = construct_empty_array(TEXTOID);
|
|
|
|
PG_RETURN_POINTER(aout);
|
2006-09-05 20:00:58 +02:00
|
|
|
}
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
out_datums = palloc(sizeof(Datum) * key_count);
|
|
|
|
out_nulls = palloc(sizeof(bool) * key_count);
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
for (i = 0; i < key_count; ++i)
|
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
text *key = (text *) DatumGetPointer(key_datums[i]);
|
|
|
|
int idx;
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
if (key_nulls[i])
|
|
|
|
idx = -1;
|
|
|
|
else
|
|
|
|
idx = hstoreFindKey(hs, NULL, VARDATA(key), VARSIZE(key) - VARHDRSZ);
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2015-11-19 20:54:05 +01:00
|
|
|
if (idx < 0 || HSTORE_VALISNULL(entries, idx))
|
2009-09-30 21:50:22 +02:00
|
|
|
{
|
|
|
|
out_nulls[i] = true;
|
|
|
|
out_datums[i] = (Datum) 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out_datums[i] = PointerGetDatum(
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
cstring_to_text_with_len(HSTORE_VAL(entries, ptr, idx),
|
|
|
|
HSTORE_VALLEN(entries, idx)));
|
2009-09-30 21:50:22 +02:00
|
|
|
out_nulls[i] = false;
|
|
|
|
}
|
2006-09-05 20:00:58 +02:00
|
|
|
}
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
aout = construct_md_array(out_datums, out_nulls,
|
|
|
|
ARR_NDIM(key_array),
|
|
|
|
ARR_DIMS(key_array),
|
|
|
|
ARR_LBOUND(key_array),
|
2010-02-26 03:01:40 +01:00
|
|
|
TEXTOID, -1, false, 'i');
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
PG_RETURN_POINTER(aout);
|
2006-09-05 20:00:58 +02:00
|
|
|
}
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_slice_to_hstore);
|
2006-09-05 20:00:58 +02:00
|
|
|
Datum
|
2009-09-30 21:50:22 +02:00
|
|
|
hstore_slice_to_hstore(PG_FUNCTION_ARGS)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
HStore *hs = PG_GETARG_HSTORE_P(0);
|
2009-09-30 21:50:22 +02:00
|
|
|
HEntry *entries = ARRPTR(hs);
|
2010-02-26 03:01:40 +01:00
|
|
|
char *ptr = STRPTR(hs);
|
2009-09-30 21:50:22 +02:00
|
|
|
ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(1);
|
2010-02-26 03:01:40 +01:00
|
|
|
HStore *out;
|
|
|
|
int nkeys;
|
|
|
|
Pairs *key_pairs = hstoreArrayToPairs(key_array, &nkeys);
|
|
|
|
Pairs *out_pairs;
|
|
|
|
int bufsiz;
|
|
|
|
int lastidx = 0;
|
|
|
|
int i;
|
|
|
|
int out_count = 0;
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
if (nkeys == 0)
|
|
|
|
{
|
|
|
|
out = hstorePairs(NULL, 0, 0);
|
|
|
|
PG_RETURN_POINTER(out);
|
|
|
|
}
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2014-02-17 15:33:31 +01:00
|
|
|
/* hstoreArrayToPairs() checked overflow */
|
2009-09-30 21:50:22 +02:00
|
|
|
out_pairs = palloc(sizeof(Pairs) * nkeys);
|
|
|
|
bufsiz = 0;
|
2007-03-14 15:21:53 +01:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* we exploit the fact that the pairs list is already sorted into strictly
|
|
|
|
* increasing order to narrow the hstoreFindKey search; each search can
|
|
|
|
* start one entry past the previous "found" entry, or at the lower bound
|
|
|
|
* of the last search.
|
2009-09-30 21:50:22 +02:00
|
|
|
*/
|
2007-03-14 15:21:53 +01:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
for (i = 0; i < nkeys; ++i)
|
2007-03-14 15:21:53 +01:00
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
int idx = hstoreFindKey(hs, &lastidx,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
key_pairs[i].key, key_pairs[i].keylen);
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
if (idx >= 0)
|
|
|
|
{
|
|
|
|
out_pairs[out_count].key = key_pairs[i].key;
|
|
|
|
bufsiz += (out_pairs[out_count].keylen = key_pairs[i].keylen);
|
2015-11-19 20:54:05 +01:00
|
|
|
out_pairs[out_count].val = HSTORE_VAL(entries, ptr, idx);
|
|
|
|
bufsiz += (out_pairs[out_count].vallen = HSTORE_VALLEN(entries, idx));
|
|
|
|
out_pairs[out_count].isnull = HSTORE_VALISNULL(entries, idx);
|
2009-09-30 21:50:22 +02:00
|
|
|
out_pairs[out_count].needfree = false;
|
|
|
|
++out_count;
|
|
|
|
}
|
2007-03-14 15:21:53 +01:00
|
|
|
}
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
/*
|
2019-08-19 09:21:39 +02:00
|
|
|
* we don't use hstoreUniquePairs here because we know that the pairs list
|
|
|
|
* is already sorted and uniq'ed.
|
2009-09-30 21:50:22 +02:00
|
|
|
*/
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
out = hstorePairs(out_pairs, out_count, bufsiz);
|
2006-09-05 20:00:58 +02:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(out);
|
|
|
|
}
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_akeys);
|
2006-09-05 20:00:58 +02:00
|
|
|
Datum
|
2009-09-30 21:50:22 +02:00
|
|
|
hstore_akeys(PG_FUNCTION_ARGS)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
HStore *hs = PG_GETARG_HSTORE_P(0);
|
2006-10-04 02:30:14 +02:00
|
|
|
Datum *d;
|
|
|
|
ArrayType *a;
|
2009-09-30 21:50:22 +02:00
|
|
|
HEntry *entries = ARRPTR(hs);
|
2006-10-04 02:30:14 +02:00
|
|
|
char *base = STRPTR(hs);
|
2010-02-26 03:01:40 +01:00
|
|
|
int count = HS_COUNT(hs);
|
|
|
|
int i;
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
if (count == 0)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2009-09-30 21:50:22 +02:00
|
|
|
a = construct_empty_array(TEXTOID);
|
|
|
|
PG_RETURN_POINTER(a);
|
2006-09-05 20:00:58 +02:00
|
|
|
}
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
d = (Datum *) palloc(sizeof(Datum) * count);
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
for (i = 0; i < count; ++i)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2015-11-19 20:54:05 +01:00
|
|
|
text *t = cstring_to_text_with_len(HSTORE_KEY(entries, base, i),
|
|
|
|
HSTORE_KEYLEN(entries, i));
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2015-11-19 20:54:05 +01:00
|
|
|
d[i] = PointerGetDatum(t);
|
2006-09-05 20:00:58 +02:00
|
|
|
}
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
a = construct_array(d, count,
|
2010-02-26 03:01:40 +01:00
|
|
|
TEXTOID, -1, false, 'i');
|
2006-09-05 20:00:58 +02:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(a);
|
|
|
|
}
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_avals);
|
2006-09-05 20:00:58 +02:00
|
|
|
Datum
|
2009-09-30 21:50:22 +02:00
|
|
|
hstore_avals(PG_FUNCTION_ARGS)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
HStore *hs = PG_GETARG_HSTORE_P(0);
|
2006-10-04 02:30:14 +02:00
|
|
|
Datum *d;
|
2010-02-26 03:01:40 +01:00
|
|
|
bool *nulls;
|
2006-10-04 02:30:14 +02:00
|
|
|
ArrayType *a;
|
2009-09-30 21:50:22 +02:00
|
|
|
HEntry *entries = ARRPTR(hs);
|
2006-10-04 02:30:14 +02:00
|
|
|
char *base = STRPTR(hs);
|
2010-02-26 03:01:40 +01:00
|
|
|
int count = HS_COUNT(hs);
|
|
|
|
int lb = 1;
|
|
|
|
int i;
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
if (count == 0)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2009-09-30 21:50:22 +02:00
|
|
|
a = construct_empty_array(TEXTOID);
|
|
|
|
PG_RETURN_POINTER(a);
|
2006-09-05 20:00:58 +02:00
|
|
|
}
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
d = (Datum *) palloc(sizeof(Datum) * count);
|
|
|
|
nulls = (bool *) palloc(sizeof(bool) * count);
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
for (i = 0; i < count; ++i)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2015-11-19 20:54:05 +01:00
|
|
|
if (HSTORE_VALISNULL(entries, i))
|
2009-09-30 21:50:22 +02:00
|
|
|
{
|
|
|
|
d[i] = (Datum) 0;
|
|
|
|
nulls[i] = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-19 20:54:05 +01:00
|
|
|
text *item = cstring_to_text_with_len(HSTORE_VAL(entries, base, i),
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
HSTORE_VALLEN(entries, i));
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
d[i] = PointerGetDatum(item);
|
|
|
|
nulls[i] = false;
|
|
|
|
}
|
2006-09-05 20:00:58 +02:00
|
|
|
}
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
a = construct_md_array(d, nulls, 1, &count, &lb,
|
2010-02-26 03:01:40 +01:00
|
|
|
TEXTOID, -1, false, 'i');
|
2006-09-05 20:00:58 +02:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(a);
|
|
|
|
}
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
static ArrayType *
|
|
|
|
hstore_to_array_internal(HStore *hs, int ndims)
|
|
|
|
{
|
|
|
|
HEntry *entries = ARRPTR(hs);
|
|
|
|
char *base = STRPTR(hs);
|
2010-02-26 03:01:40 +01:00
|
|
|
int count = HS_COUNT(hs);
|
|
|
|
int out_size[2] = {0, 2};
|
|
|
|
int lb[2] = {1, 1};
|
2009-09-30 21:50:22 +02:00
|
|
|
Datum *out_datums;
|
|
|
|
bool *out_nulls;
|
2010-02-26 03:01:40 +01:00
|
|
|
int i;
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
Assert(ndims < 3);
|
|
|
|
|
|
|
|
if (count == 0 || ndims == 0)
|
|
|
|
return construct_empty_array(TEXTOID);
|
|
|
|
|
|
|
|
out_size[0] = count * 2 / ndims;
|
|
|
|
out_datums = palloc(sizeof(Datum) * count * 2);
|
|
|
|
out_nulls = palloc(sizeof(bool) * count * 2);
|
|
|
|
|
|
|
|
for (i = 0; i < count; ++i)
|
|
|
|
{
|
2015-11-19 20:54:05 +01:00
|
|
|
text *key = cstring_to_text_with_len(HSTORE_KEY(entries, base, i),
|
|
|
|
HSTORE_KEYLEN(entries, i));
|
2010-02-26 03:01:40 +01:00
|
|
|
|
|
|
|
out_datums[i * 2] = PointerGetDatum(key);
|
|
|
|
out_nulls[i * 2] = false;
|
2009-09-30 21:50:22 +02:00
|
|
|
|
2015-11-19 20:54:05 +01:00
|
|
|
if (HSTORE_VALISNULL(entries, i))
|
2009-09-30 21:50:22 +02:00
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
out_datums[i * 2 + 1] = (Datum) 0;
|
|
|
|
out_nulls[i * 2 + 1] = true;
|
2009-09-30 21:50:22 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-19 20:54:05 +01:00
|
|
|
text *item = cstring_to_text_with_len(HSTORE_VAL(entries, base, i),
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
HSTORE_VALLEN(entries, i));
|
2010-02-26 03:01:40 +01:00
|
|
|
|
|
|
|
out_datums[i * 2 + 1] = PointerGetDatum(item);
|
|
|
|
out_nulls[i * 2 + 1] = false;
|
2009-09-30 21:50:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return construct_md_array(out_datums, out_nulls,
|
|
|
|
ndims, out_size, lb,
|
|
|
|
TEXTOID, -1, false, 'i');
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_to_array);
|
|
|
|
Datum
|
|
|
|
hstore_to_array(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
HStore *hs = PG_GETARG_HSTORE_P(0);
|
2009-09-30 21:50:22 +02:00
|
|
|
ArrayType *out = hstore_to_array_internal(hs, 1);
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_to_matrix);
|
|
|
|
Datum
|
|
|
|
hstore_to_matrix(PG_FUNCTION_ARGS)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
HStore *hs = PG_GETARG_HSTORE_P(0);
|
2009-09-30 21:50:22 +02:00
|
|
|
ArrayType *out = hstore_to_array_internal(hs, 2);
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Common initialization function for the various set-returning
|
|
|
|
* funcs. fcinfo is only passed if the function is to return a
|
|
|
|
* composite; it will be used to look up the return tupledesc.
|
|
|
|
* we stash a copy of the hstore in the multi-call context in
|
|
|
|
* case it was originally toasted. (At least I assume that's why;
|
|
|
|
* there was no explanatory comment in the original code. --AG)
|
|
|
|
*/
|
2006-09-05 20:00:58 +02:00
|
|
|
|
|
|
|
static void
|
2010-02-26 03:01:40 +01:00
|
|
|
setup_firstcall(FuncCallContext *funcctx, HStore *hs,
|
Change function call information to be variable length.
Before this change FunctionCallInfoData, the struct arguments etc for
V1 function calls are stored in, always had space for
FUNC_MAX_ARGS/100 arguments, storing datums and their nullness in two
arrays. For nearly every function call 100 arguments is far more than
needed, therefore wasting memory. Arg and argnull being two separate
arrays also guarantees that to access a single argument, two
cachelines have to be touched.
Change the layout so there's a single variable-length array with pairs
of value / isnull. That drastically reduces memory consumption for
most function calls (on x86-64 a two argument function now uses
64bytes, previously 936 bytes), and makes it very likely that argument
value and its nullness are on the same cacheline.
Arguments are stored in a new NullableDatum struct, which, due to
padding, needs more memory per argument than before. But as usually
far fewer arguments are stored, and individual arguments are cheaper
to access, that's still a clear win. It's likely that there's other
places where conversion to NullableDatum arrays would make sense,
e.g. TupleTableSlots, but that's for another commit.
Because the function call information is now variable-length
allocations have to take the number of arguments into account. For
heap allocations that can be done with SizeForFunctionCallInfoData(),
for on-stack allocations there's a new LOCAL_FCINFO(name, nargs) macro
that helps to allocate an appropriately sized and aligned variable.
Some places with stack allocation function call information don't know
the number of arguments at compile time, and currently variably sized
stack allocations aren't allowed in postgres. Therefore allow for
FUNC_MAX_ARGS space in these cases. They're not that common, so for
now that seems acceptable.
Because of the need to allocate FunctionCallInfo of the appropriate
size, older extensions may need to update their code. To avoid subtle
breakages, the FunctionCallInfoData struct has been renamed to
FunctionCallInfoBaseData. Most code only references FunctionCallInfo,
so that shouldn't cause much collateral damage.
This change is also a prerequisite for more efficient expression JIT
compilation (by allocating the function call information on the stack,
allowing LLVM to optimize it away); previously the size of the call
information caused problems inside LLVM's optimizer.
Author: Andres Freund
Reviewed-By: Tom Lane
Discussion: https://postgr.es/m/20180605172952.x34m5uz6ju6enaem@alap3.anarazel.de
2019-01-26 23:17:52 +01:00
|
|
|
FunctionCallInfo fcinfo)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
|
|
|
MemoryContext oldcontext;
|
2010-02-26 03:01:40 +01:00
|
|
|
HStore *st;
|
2006-09-05 20:00:58 +02:00
|
|
|
|
|
|
|
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
st = (HStore *) palloc(VARSIZE(hs));
|
|
|
|
memcpy(st, hs, VARSIZE(hs));
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
funcctx->user_fctx = (void *) st;
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
if (fcinfo)
|
|
|
|
{
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
|
|
|
|
/* Build a tuple descriptor for our result type */
|
|
|
|
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
|
|
|
|
elog(ERROR, "return type must be a row type");
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
funcctx->tuple_desc = BlessTupleDesc(tupdesc);
|
|
|
|
}
|
|
|
|
|
2006-09-05 20:00:58 +02:00
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
}
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_skeys);
|
2006-09-05 20:00:58 +02:00
|
|
|
Datum
|
2009-09-30 21:50:22 +02:00
|
|
|
hstore_skeys(PG_FUNCTION_ARGS)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
|
|
|
FuncCallContext *funcctx;
|
2010-02-26 03:01:40 +01:00
|
|
|
HStore *hs;
|
|
|
|
int i;
|
2006-10-04 02:30:14 +02:00
|
|
|
|
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
hs = PG_GETARG_HSTORE_P(0);
|
2006-09-05 20:00:58 +02:00
|
|
|
funcctx = SRF_FIRSTCALL_INIT();
|
2009-09-30 21:50:22 +02:00
|
|
|
setup_firstcall(funcctx, hs, NULL);
|
2006-09-05 20:00:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
funcctx = SRF_PERCALL_SETUP();
|
2009-09-30 21:50:22 +02:00
|
|
|
hs = (HStore *) funcctx->user_fctx;
|
|
|
|
i = funcctx->call_cntr;
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
if (i < HS_COUNT(hs))
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
HEntry *entries = ARRPTR(hs);
|
2008-03-25 23:42:46 +01:00
|
|
|
text *item;
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2015-11-19 20:54:05 +01:00
|
|
|
item = cstring_to_text_with_len(HSTORE_KEY(entries, STRPTR(hs), i),
|
|
|
|
HSTORE_KEYLEN(entries, i));
|
2006-09-05 20:00:58 +02:00
|
|
|
|
|
|
|
SRF_RETURN_NEXT(funcctx, PointerGetDatum(item));
|
|
|
|
}
|
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
SRF_RETURN_DONE(funcctx);
|
2006-09-05 20:00:58 +02:00
|
|
|
}
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_svals);
|
2006-09-05 20:00:58 +02:00
|
|
|
Datum
|
2009-09-30 21:50:22 +02:00
|
|
|
hstore_svals(PG_FUNCTION_ARGS)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
|
|
|
FuncCallContext *funcctx;
|
2010-02-26 03:01:40 +01:00
|
|
|
HStore *hs;
|
|
|
|
int i;
|
2006-10-04 02:30:14 +02:00
|
|
|
|
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
hs = PG_GETARG_HSTORE_P(0);
|
2006-09-05 20:00:58 +02:00
|
|
|
funcctx = SRF_FIRSTCALL_INIT();
|
2009-09-30 21:50:22 +02:00
|
|
|
setup_firstcall(funcctx, hs, NULL);
|
2006-09-05 20:00:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
funcctx = SRF_PERCALL_SETUP();
|
2009-09-30 21:50:22 +02:00
|
|
|
hs = (HStore *) funcctx->user_fctx;
|
|
|
|
i = funcctx->call_cntr;
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
if (i < HS_COUNT(hs))
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
HEntry *entries = ARRPTR(hs);
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2015-11-19 20:54:05 +01:00
|
|
|
if (HSTORE_VALISNULL(entries, i))
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
|
|
|
ReturnSetInfo *rsi;
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
/* ugly ugly ugly. why no macro for this? */
|
2006-09-05 20:00:58 +02:00
|
|
|
(funcctx)->call_cntr++;
|
|
|
|
rsi = (ReturnSetInfo *) fcinfo->resultinfo;
|
|
|
|
rsi->isDone = ExprMultipleResult;
|
|
|
|
PG_RETURN_NULL();
|
2006-10-04 02:30:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-03-25 23:42:46 +01:00
|
|
|
text *item;
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2015-11-19 20:54:05 +01:00
|
|
|
item = cstring_to_text_with_len(HSTORE_VAL(entries, STRPTR(hs), i),
|
|
|
|
HSTORE_VALLEN(entries, i));
|
2006-09-05 20:00:58 +02:00
|
|
|
|
|
|
|
SRF_RETURN_NEXT(funcctx, PointerGetDatum(item));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
SRF_RETURN_DONE(funcctx);
|
2006-09-05 20:00:58 +02:00
|
|
|
}
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_contains);
|
2006-09-05 20:00:58 +02:00
|
|
|
Datum
|
2009-09-30 21:50:22 +02:00
|
|
|
hstore_contains(PG_FUNCTION_ARGS)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
HStore *val = PG_GETARG_HSTORE_P(0);
|
|
|
|
HStore *tmpl = PG_GETARG_HSTORE_P(1);
|
2006-10-04 02:30:14 +02:00
|
|
|
bool res = true;
|
|
|
|
HEntry *te = ARRPTR(tmpl);
|
2009-09-30 21:50:22 +02:00
|
|
|
char *tstr = STRPTR(tmpl);
|
|
|
|
HEntry *ve = ARRPTR(val);
|
|
|
|
char *vstr = STRPTR(val);
|
2010-02-26 03:01:40 +01:00
|
|
|
int tcount = HS_COUNT(tmpl);
|
|
|
|
int lastidx = 0;
|
|
|
|
int i;
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* we exploit the fact that keys in "tmpl" are in strictly increasing
|
|
|
|
* order to narrow the hstoreFindKey search; each search can start one
|
|
|
|
* entry past the previous "found" entry, or at the lower bound of the
|
|
|
|
* search
|
2009-09-30 21:50:22 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; res && i < tcount; ++i)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
int idx = hstoreFindKey(val, &lastidx,
|
2015-11-19 20:54:05 +01:00
|
|
|
HSTORE_KEY(te, tstr, i),
|
|
|
|
HSTORE_KEYLEN(te, i));
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
if (idx >= 0)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2015-11-19 20:54:05 +01:00
|
|
|
bool nullval = HSTORE_VALISNULL(te, i);
|
|
|
|
int vallen = HSTORE_VALLEN(te, i);
|
|
|
|
|
|
|
|
if (nullval != HSTORE_VALISNULL(ve, idx) ||
|
|
|
|
(!nullval && (vallen != HSTORE_VALLEN(ve, idx) ||
|
|
|
|
memcmp(HSTORE_VAL(te, tstr, i),
|
|
|
|
HSTORE_VAL(ve, vstr, idx),
|
|
|
|
vallen) != 0)))
|
2007-11-15 22:14:46 +01:00
|
|
|
res = false;
|
2006-10-04 02:30:14 +02:00
|
|
|
}
|
|
|
|
else
|
2006-09-05 20:00:58 +02:00
|
|
|
res = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(res);
|
|
|
|
}
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_contained);
|
2006-09-05 20:00:58 +02:00
|
|
|
Datum
|
2009-09-30 21:50:22 +02:00
|
|
|
hstore_contained(PG_FUNCTION_ARGS)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2009-09-30 21:50:22 +02:00
|
|
|
PG_RETURN_DATUM(DirectFunctionCall2(hstore_contains,
|
2006-10-04 02:30:14 +02:00
|
|
|
PG_GETARG_DATUM(1),
|
|
|
|
PG_GETARG_DATUM(0)
|
|
|
|
));
|
2006-09-05 20:00:58 +02:00
|
|
|
}
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_each);
|
2006-09-05 20:00:58 +02:00
|
|
|
Datum
|
2009-09-30 21:50:22 +02:00
|
|
|
hstore_each(PG_FUNCTION_ARGS)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
|
|
|
FuncCallContext *funcctx;
|
2010-02-26 03:01:40 +01:00
|
|
|
HStore *hs;
|
|
|
|
int i;
|
2006-09-05 20:00:58 +02:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
hs = PG_GETARG_HSTORE_P(0);
|
2006-09-05 20:00:58 +02:00
|
|
|
funcctx = SRF_FIRSTCALL_INIT();
|
2009-09-30 21:50:22 +02:00
|
|
|
setup_firstcall(funcctx, hs, fcinfo);
|
2006-09-05 20:00:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
funcctx = SRF_PERCALL_SETUP();
|
2009-09-30 21:50:22 +02:00
|
|
|
hs = (HStore *) funcctx->user_fctx;
|
|
|
|
i = funcctx->call_cntr;
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
if (i < HS_COUNT(hs))
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
2009-09-30 21:50:22 +02:00
|
|
|
HEntry *entries = ARRPTR(hs);
|
2010-02-26 03:01:40 +01:00
|
|
|
char *ptr = STRPTR(hs);
|
2006-10-04 02:30:14 +02:00
|
|
|
Datum res,
|
|
|
|
dvalues[2];
|
2008-11-02 02:45:28 +01:00
|
|
|
bool nulls[2] = {false, false};
|
2006-10-04 02:30:14 +02:00
|
|
|
text *item;
|
|
|
|
HeapTuple tuple;
|
|
|
|
|
2015-11-19 20:54:05 +01:00
|
|
|
item = cstring_to_text_with_len(HSTORE_KEY(entries, ptr, i),
|
|
|
|
HSTORE_KEYLEN(entries, i));
|
2006-09-05 20:00:58 +02:00
|
|
|
dvalues[0] = PointerGetDatum(item);
|
|
|
|
|
2015-11-19 20:54:05 +01:00
|
|
|
if (HSTORE_VALISNULL(entries, i))
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
|
|
|
dvalues[1] = (Datum) 0;
|
2008-11-02 02:45:28 +01:00
|
|
|
nulls[1] = true;
|
2006-10-04 02:30:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-19 20:54:05 +01:00
|
|
|
item = cstring_to_text_with_len(HSTORE_VAL(entries, ptr, i),
|
|
|
|
HSTORE_VALLEN(entries, i));
|
2006-09-05 20:00:58 +02:00
|
|
|
dvalues[1] = PointerGetDatum(item);
|
|
|
|
}
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
tuple = heap_form_tuple(funcctx->tuple_desc, dvalues, nulls);
|
2007-08-27 01:36:32 +02:00
|
|
|
res = HeapTupleGetDatum(tuple);
|
2006-09-05 20:00:58 +02:00
|
|
|
|
|
|
|
SRF_RETURN_NEXT(funcctx, PointerGetDatum(res));
|
|
|
|
}
|
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
SRF_RETURN_DONE(funcctx);
|
|
|
|
}
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* btree sort order for hstores isn't intended to be useful; we really only
|
|
|
|
* care about equality versus non-equality. we compare the entire string
|
|
|
|
* buffer first, then the entry pos array.
|
|
|
|
*/
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_cmp);
|
|
|
|
Datum
|
|
|
|
hstore_cmp(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
HStore *hs1 = PG_GETARG_HSTORE_P(0);
|
|
|
|
HStore *hs2 = PG_GETARG_HSTORE_P(1);
|
2010-02-26 03:01:40 +01:00
|
|
|
int hcount1 = HS_COUNT(hs1);
|
|
|
|
int hcount2 = HS_COUNT(hs2);
|
|
|
|
int res = 0;
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
if (hcount1 == 0 || hcount2 == 0)
|
|
|
|
{
|
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* if either operand is empty, and the other is nonempty, the nonempty
|
|
|
|
* one is larger. If both are empty they are equal.
|
2009-09-30 21:50:22 +02:00
|
|
|
*/
|
|
|
|
if (hcount1 > 0)
|
|
|
|
res = 1;
|
|
|
|
else if (hcount2 > 0)
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* here we know both operands are nonempty */
|
2010-02-26 03:01:40 +01:00
|
|
|
char *str1 = STRPTR(hs1);
|
|
|
|
char *str2 = STRPTR(hs2);
|
|
|
|
HEntry *ent1 = ARRPTR(hs1);
|
|
|
|
HEntry *ent2 = ARRPTR(hs2);
|
|
|
|
size_t len1 = HSE_ENDPOS(ent1[2 * hcount1 - 1]);
|
|
|
|
size_t len2 = HSE_ENDPOS(ent2[2 * hcount2 - 1]);
|
2009-09-30 21:50:22 +02:00
|
|
|
|
2010-02-26 03:01:40 +01:00
|
|
|
res = memcmp(str1, str2, Min(len1, len2));
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
if (res == 0)
|
|
|
|
{
|
|
|
|
if (len1 > len2)
|
|
|
|
res = 1;
|
|
|
|
else if (len1 < len2)
|
|
|
|
res = -1;
|
|
|
|
else if (hcount1 > hcount2)
|
|
|
|
res = 1;
|
|
|
|
else if (hcount2 > hcount1)
|
|
|
|
res = -1;
|
|
|
|
else
|
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
int count = hcount1 * 2;
|
|
|
|
int i;
|
2009-09-30 21:50:22 +02:00
|
|
|
|
|
|
|
for (i = 0; i < count; ++i)
|
|
|
|
if (HSE_ENDPOS(ent1[i]) != HSE_ENDPOS(ent2[i]) ||
|
|
|
|
HSE_ISNULL(ent1[i]) != HSE_ISNULL(ent2[i]))
|
|
|
|
break;
|
|
|
|
if (i < count)
|
|
|
|
{
|
|
|
|
if (HSE_ENDPOS(ent1[i]) < HSE_ENDPOS(ent2[i]))
|
|
|
|
res = -1;
|
|
|
|
else if (HSE_ENDPOS(ent1[i]) > HSE_ENDPOS(ent2[i]))
|
|
|
|
res = 1;
|
|
|
|
else if (HSE_ISNULL(ent1[i]))
|
|
|
|
res = 1;
|
|
|
|
else if (HSE_ISNULL(ent2[i]))
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res = (res > 0) ? 1 : -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* this is a btree support function; this is one of the few places where
|
|
|
|
* memory needs to be explicitly freed.
|
2009-09-30 21:50:22 +02:00
|
|
|
*/
|
2010-02-26 03:01:40 +01:00
|
|
|
PG_FREE_IF_COPY(hs1, 0);
|
|
|
|
PG_FREE_IF_COPY(hs2, 1);
|
2009-09-30 21:50:22 +02:00
|
|
|
PG_RETURN_INT32(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_eq);
|
|
|
|
Datum
|
|
|
|
hstore_eq(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
int res = DatumGetInt32(DirectFunctionCall2(hstore_cmp,
|
|
|
|
PG_GETARG_DATUM(0),
|
|
|
|
PG_GETARG_DATUM(1)));
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
PG_RETURN_BOOL(res == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_ne);
|
|
|
|
Datum
|
|
|
|
hstore_ne(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
int res = DatumGetInt32(DirectFunctionCall2(hstore_cmp,
|
|
|
|
PG_GETARG_DATUM(0),
|
|
|
|
PG_GETARG_DATUM(1)));
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
PG_RETURN_BOOL(res != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_gt);
|
|
|
|
Datum
|
|
|
|
hstore_gt(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
int res = DatumGetInt32(DirectFunctionCall2(hstore_cmp,
|
|
|
|
PG_GETARG_DATUM(0),
|
|
|
|
PG_GETARG_DATUM(1)));
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
PG_RETURN_BOOL(res > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_ge);
|
|
|
|
Datum
|
|
|
|
hstore_ge(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
int res = DatumGetInt32(DirectFunctionCall2(hstore_cmp,
|
|
|
|
PG_GETARG_DATUM(0),
|
|
|
|
PG_GETARG_DATUM(1)));
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
PG_RETURN_BOOL(res >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_lt);
|
|
|
|
Datum
|
|
|
|
hstore_lt(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
int res = DatumGetInt32(DirectFunctionCall2(hstore_cmp,
|
|
|
|
PG_GETARG_DATUM(0),
|
|
|
|
PG_GETARG_DATUM(1)));
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
PG_RETURN_BOOL(res < 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_le);
|
|
|
|
Datum
|
|
|
|
hstore_le(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
int res = DatumGetInt32(DirectFunctionCall2(hstore_cmp,
|
|
|
|
PG_GETARG_DATUM(0),
|
|
|
|
PG_GETARG_DATUM(1)));
|
|
|
|
|
2009-09-30 21:50:22 +02:00
|
|
|
PG_RETURN_BOOL(res <= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_hash);
|
|
|
|
Datum
|
|
|
|
hstore_hash(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
HStore *hs = PG_GETARG_HSTORE_P(0);
|
2010-02-26 03:01:40 +01:00
|
|
|
Datum hval = hash_any((unsigned char *) VARDATA(hs),
|
2009-09-30 21:50:22 +02:00
|
|
|
VARSIZE(hs) - VARHDRSZ);
|
|
|
|
|
|
|
|
/*
|
2018-11-23 19:37:34 +01:00
|
|
|
* This (along with hstore_hash_extended) is the only place in the code
|
|
|
|
* that cares whether the overall varlena size exactly matches the true
|
|
|
|
* data size; this assertion should be maintained by all the other code,
|
|
|
|
* but we make it explicit here.
|
2009-09-30 21:50:22 +02:00
|
|
|
*/
|
|
|
|
Assert(VARSIZE(hs) ==
|
2009-09-30 23:26:17 +02:00
|
|
|
(HS_COUNT(hs) != 0 ?
|
|
|
|
CALCDATASIZE(HS_COUNT(hs),
|
2010-02-26 03:01:40 +01:00
|
|
|
HSE_ENDPOS(ARRPTR(hs)[2 * HS_COUNT(hs) - 1])) :
|
2009-09-30 23:26:17 +02:00
|
|
|
HSHRDSIZE));
|
2009-09-30 21:50:22 +02:00
|
|
|
|
2010-02-26 03:01:40 +01:00
|
|
|
PG_FREE_IF_COPY(hs, 0);
|
2009-09-30 21:50:22 +02:00
|
|
|
PG_RETURN_DATUM(hval);
|
|
|
|
}
|
2018-11-23 19:37:34 +01:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(hstore_hash_extended);
|
|
|
|
Datum
|
|
|
|
hstore_hash_extended(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
HStore *hs = PG_GETARG_HSTORE_P(0);
|
|
|
|
uint64 seed = PG_GETARG_INT64(1);
|
|
|
|
Datum hval;
|
|
|
|
|
|
|
|
hval = hash_any_extended((unsigned char *) VARDATA(hs),
|
|
|
|
VARSIZE(hs) - VARHDRSZ,
|
|
|
|
seed);
|
|
|
|
|
|
|
|
/* See comment in hstore_hash */
|
|
|
|
Assert(VARSIZE(hs) ==
|
|
|
|
(HS_COUNT(hs) != 0 ?
|
|
|
|
CALCDATASIZE(HS_COUNT(hs),
|
|
|
|
HSE_ENDPOS(ARRPTR(hs)[2 * HS_COUNT(hs) - 1])) :
|
|
|
|
HSHRDSIZE));
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(hs, 0);
|
|
|
|
PG_RETURN_DATUM(hval);
|
|
|
|
}
|