2002-07-30 18:40:34 +02:00
|
|
|
/*
|
2002-09-04 22:31:48 +02:00
|
|
|
* op function for ltree
|
2002-07-30 18:40:34 +02:00
|
|
|
* Teodor Sigaev <teodor@stack.net>
|
2010-09-20 22:08:53 +02:00
|
|
|
* contrib/ltree/ltree_op.c
|
2002-07-30 18:40:34 +02:00
|
|
|
*/
|
2008-05-12 02:00:54 +02:00
|
|
|
#include "postgres.h"
|
2006-04-27 20:24:35 +02:00
|
|
|
|
2002-07-30 18:40:34 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2012-08-30 22:15:44 +02:00
|
|
|
#include "access/htup_details.h"
|
2006-09-20 21:50:21 +02:00
|
|
|
#include "catalog/pg_statistic.h"
|
2019-10-23 05:56:22 +02:00
|
|
|
#include "ltree.h"
|
2008-05-12 02:00:54 +02:00
|
|
|
#include "utils/builtins.h"
|
2006-04-27 00:33:36 +02:00
|
|
|
#include "utils/lsyscache.h"
|
|
|
|
#include "utils/selfuncs.h"
|
|
|
|
|
2006-05-31 00:12:16 +02:00
|
|
|
PG_MODULE_MAGIC;
|
2006-04-27 20:24:35 +02:00
|
|
|
|
2002-07-30 18:40:34 +02:00
|
|
|
/* compare functions */
|
|
|
|
PG_FUNCTION_INFO_V1(ltree_cmp);
|
|
|
|
PG_FUNCTION_INFO_V1(ltree_lt);
|
|
|
|
PG_FUNCTION_INFO_V1(ltree_le);
|
|
|
|
PG_FUNCTION_INFO_V1(ltree_eq);
|
|
|
|
PG_FUNCTION_INFO_V1(ltree_ne);
|
|
|
|
PG_FUNCTION_INFO_V1(ltree_ge);
|
|
|
|
PG_FUNCTION_INFO_V1(ltree_gt);
|
|
|
|
PG_FUNCTION_INFO_V1(nlevel);
|
|
|
|
PG_FUNCTION_INFO_V1(ltree_isparent);
|
|
|
|
PG_FUNCTION_INFO_V1(ltree_risparent);
|
|
|
|
PG_FUNCTION_INFO_V1(subltree);
|
|
|
|
PG_FUNCTION_INFO_V1(subpath);
|
2003-03-31 22:53:45 +02:00
|
|
|
PG_FUNCTION_INFO_V1(ltree_index);
|
2002-07-30 18:40:34 +02:00
|
|
|
PG_FUNCTION_INFO_V1(ltree_addltree);
|
|
|
|
PG_FUNCTION_INFO_V1(ltree_addtext);
|
|
|
|
PG_FUNCTION_INFO_V1(ltree_textadd);
|
2002-08-04 07:02:50 +02:00
|
|
|
PG_FUNCTION_INFO_V1(lca);
|
2003-03-31 22:53:45 +02:00
|
|
|
PG_FUNCTION_INFO_V1(ltree2text);
|
|
|
|
PG_FUNCTION_INFO_V1(text2ltree);
|
2006-04-27 20:24:35 +02:00
|
|
|
PG_FUNCTION_INFO_V1(ltreeparentsel);
|
|
|
|
|
2002-07-30 18:40:34 +02:00
|
|
|
int
|
2009-06-11 16:49:15 +02:00
|
|
|
ltree_compare(const ltree *a, const ltree *b)
|
2002-09-04 22:31:48 +02:00
|
|
|
{
|
2002-07-30 18:40:34 +02:00
|
|
|
ltree_level *al = LTREE_FIRST(a);
|
|
|
|
ltree_level *bl = LTREE_FIRST(b);
|
2002-09-04 22:31:48 +02:00
|
|
|
int an = a->numlevel;
|
|
|
|
int bn = b->numlevel;
|
|
|
|
|
|
|
|
while (an > 0 && bn > 0)
|
|
|
|
{
|
Allow btree comparison functions to return INT_MIN.
Historically we forbade datatype-specific comparison functions from
returning INT_MIN, so that it would be safe to invert the sort order
just by negating the comparison result. However, this was never
really safe for comparison functions that directly return the result
of memcmp(), strcmp(), etc, as POSIX doesn't place any such restriction
on those library functions. Buildfarm results show that at least on
recent Linux on s390x, memcmp() actually does return INT_MIN sometimes,
causing sort failures.
The agreed-on answer is to remove this restriction and fix relevant
call sites to not make such an assumption; code such as "res = -res"
should be replaced by "INVERT_COMPARE_RESULT(res)". The same is needed
in a few places that just directly negated the result of memcmp or
strcmp.
To help find places having this problem, I've also added a compile option
to nbtcompare.c that causes some of the commonly used comparators to
return INT_MIN/INT_MAX instead of their usual -1/+1. It'd likely be
a good idea to have at least one buildfarm member running with
"-DSTRESS_SORT_INT_MIN". That's far from a complete test of course,
but it should help to prevent fresh introductions of such bugs.
This is a longstanding portability hazard, so back-patch to all supported
branches.
Discussion: https://postgr.es/m/20180928185215.ffoq2xrq5d3pafna@alap3.anarazel.de
2018-10-05 22:01:29 +02:00
|
|
|
int res;
|
|
|
|
|
2010-12-22 04:11:40 +01:00
|
|
|
if ((res = memcmp(al->name, bl->name, Min(al->len, bl->len))) == 0)
|
2002-09-04 22:31:48 +02:00
|
|
|
{
|
|
|
|
if (al->len != bl->len)
|
|
|
|
return (al->len - bl->len) * 10 * (an + 1);
|
|
|
|
}
|
|
|
|
else
|
Allow btree comparison functions to return INT_MIN.
Historically we forbade datatype-specific comparison functions from
returning INT_MIN, so that it would be safe to invert the sort order
just by negating the comparison result. However, this was never
really safe for comparison functions that directly return the result
of memcmp(), strcmp(), etc, as POSIX doesn't place any such restriction
on those library functions. Buildfarm results show that at least on
recent Linux on s390x, memcmp() actually does return INT_MIN sometimes,
causing sort failures.
The agreed-on answer is to remove this restriction and fix relevant
call sites to not make such an assumption; code such as "res = -res"
should be replaced by "INVERT_COMPARE_RESULT(res)". The same is needed
in a few places that just directly negated the result of memcmp or
strcmp.
To help find places having this problem, I've also added a compile option
to nbtcompare.c that causes some of the commonly used comparators to
return INT_MIN/INT_MAX instead of their usual -1/+1. It'd likely be
a good idea to have at least one buildfarm member running with
"-DSTRESS_SORT_INT_MIN". That's far from a complete test of course,
but it should help to prevent fresh introductions of such bugs.
This is a longstanding portability hazard, so back-patch to all supported
branches.
Discussion: https://postgr.es/m/20180928185215.ffoq2xrq5d3pafna@alap3.anarazel.de
2018-10-05 22:01:29 +02:00
|
|
|
{
|
|
|
|
if (res < 0)
|
|
|
|
res = -1;
|
|
|
|
else
|
|
|
|
res = 1;
|
2002-09-04 22:31:48 +02:00
|
|
|
return res * 10 * (an + 1);
|
Allow btree comparison functions to return INT_MIN.
Historically we forbade datatype-specific comparison functions from
returning INT_MIN, so that it would be safe to invert the sort order
just by negating the comparison result. However, this was never
really safe for comparison functions that directly return the result
of memcmp(), strcmp(), etc, as POSIX doesn't place any such restriction
on those library functions. Buildfarm results show that at least on
recent Linux on s390x, memcmp() actually does return INT_MIN sometimes,
causing sort failures.
The agreed-on answer is to remove this restriction and fix relevant
call sites to not make such an assumption; code such as "res = -res"
should be replaced by "INVERT_COMPARE_RESULT(res)". The same is needed
in a few places that just directly negated the result of memcmp or
strcmp.
To help find places having this problem, I've also added a compile option
to nbtcompare.c that causes some of the commonly used comparators to
return INT_MIN/INT_MAX instead of their usual -1/+1. It'd likely be
a good idea to have at least one buildfarm member running with
"-DSTRESS_SORT_INT_MIN". That's far from a complete test of course,
but it should help to prevent fresh introductions of such bugs.
This is a longstanding portability hazard, so back-patch to all supported
branches.
Discussion: https://postgr.es/m/20180928185215.ffoq2xrq5d3pafna@alap3.anarazel.de
2018-10-05 22:01:29 +02:00
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
|
|
|
|
an--;
|
|
|
|
bn--;
|
|
|
|
al = LEVEL_NEXT(al);
|
|
|
|
bl = LEVEL_NEXT(bl);
|
2002-07-30 18:40:34 +02:00
|
|
|
}
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
return (a->numlevel - b->numlevel) * 10 * (an + 1);
|
|
|
|
}
|
2002-07-30 18:40:34 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
#define RUNCMP \
|
2017-09-18 21:21:23 +02:00
|
|
|
ltree *a = PG_GETARG_LTREE_P(0); \
|
|
|
|
ltree *b = PG_GETARG_LTREE_P(1); \
|
|
|
|
int res = ltree_compare(a,b); \
|
|
|
|
PG_FREE_IF_COPY(a,0); \
|
|
|
|
PG_FREE_IF_COPY(b,1)
|
2002-07-30 18:40:34 +02:00
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
ltree_cmp(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
RUNCMP;
|
|
|
|
PG_RETURN_INT32(res);
|
2002-07-30 18:40:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
ltree_lt(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
RUNCMP;
|
|
|
|
PG_RETURN_BOOL((res < 0) ? true : false);
|
2002-07-30 18:40:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
ltree_le(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
RUNCMP;
|
|
|
|
PG_RETURN_BOOL((res <= 0) ? true : false);
|
2002-07-30 18:40:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
ltree_eq(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
RUNCMP;
|
|
|
|
PG_RETURN_BOOL((res == 0) ? true : false);
|
2002-07-30 18:40:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
ltree_ge(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
RUNCMP;
|
|
|
|
PG_RETURN_BOOL((res >= 0) ? true : false);
|
2002-07-30 18:40:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
ltree_gt(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
RUNCMP;
|
|
|
|
PG_RETURN_BOOL((res > 0) ? true : false);
|
2002-07-30 18:40:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
ltree_ne(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
RUNCMP;
|
|
|
|
PG_RETURN_BOOL((res != 0) ? true : false);
|
2002-07-30 18:40:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
nlevel(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
ltree *a = PG_GETARG_LTREE_P(0);
|
2002-09-04 22:31:48 +02:00
|
|
|
int res = a->numlevel;
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(a, 0);
|
2002-07-30 18:40:34 +02:00
|
|
|
PG_RETURN_INT32(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2009-06-11 16:49:15 +02:00
|
|
|
inner_isparent(const ltree *c, const ltree *p)
|
2002-09-04 22:31:48 +02:00
|
|
|
{
|
2002-07-30 18:40:34 +02:00
|
|
|
ltree_level *cl = LTREE_FIRST(c);
|
|
|
|
ltree_level *pl = LTREE_FIRST(p);
|
2002-09-04 22:31:48 +02:00
|
|
|
int pn = p->numlevel;
|
2002-07-30 18:40:34 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (pn > c->numlevel)
|
2002-07-30 18:40:34 +02:00
|
|
|
return false;
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
while (pn > 0)
|
|
|
|
{
|
|
|
|
if (cl->len != pl->len)
|
2002-07-30 18:40:34 +02:00
|
|
|
return false;
|
Allow btree comparison functions to return INT_MIN.
Historically we forbade datatype-specific comparison functions from
returning INT_MIN, so that it would be safe to invert the sort order
just by negating the comparison result. However, this was never
really safe for comparison functions that directly return the result
of memcmp(), strcmp(), etc, as POSIX doesn't place any such restriction
on those library functions. Buildfarm results show that at least on
recent Linux on s390x, memcmp() actually does return INT_MIN sometimes,
causing sort failures.
The agreed-on answer is to remove this restriction and fix relevant
call sites to not make such an assumption; code such as "res = -res"
should be replaced by "INVERT_COMPARE_RESULT(res)". The same is needed
in a few places that just directly negated the result of memcmp or
strcmp.
To help find places having this problem, I've also added a compile option
to nbtcompare.c that causes some of the commonly used comparators to
return INT_MIN/INT_MAX instead of their usual -1/+1. It'd likely be
a good idea to have at least one buildfarm member running with
"-DSTRESS_SORT_INT_MIN". That's far from a complete test of course,
but it should help to prevent fresh introductions of such bugs.
This is a longstanding portability hazard, so back-patch to all supported
branches.
Discussion: https://postgr.es/m/20180928185215.ffoq2xrq5d3pafna@alap3.anarazel.de
2018-10-05 22:01:29 +02:00
|
|
|
if (memcmp(cl->name, pl->name, cl->len) != 0)
|
2002-07-30 18:40:34 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
pn--;
|
2002-09-04 22:31:48 +02:00
|
|
|
cl = LEVEL_NEXT(cl);
|
|
|
|
pl = LEVEL_NEXT(pl);
|
2002-07-30 18:40:34 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
Datum
|
|
|
|
ltree_isparent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
ltree *c = PG_GETARG_LTREE_P(1);
|
|
|
|
ltree *p = PG_GETARG_LTREE_P(0);
|
2002-09-04 22:31:48 +02:00
|
|
|
bool res = inner_isparent(c, p);
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(c, 1);
|
|
|
|
PG_FREE_IF_COPY(p, 0);
|
|
|
|
PG_RETURN_BOOL(res);
|
2002-07-30 18:40:34 +02:00
|
|
|
}
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
Datum
|
|
|
|
ltree_risparent(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
ltree *c = PG_GETARG_LTREE_P(0);
|
|
|
|
ltree *p = PG_GETARG_LTREE_P(1);
|
2002-09-04 22:31:48 +02:00
|
|
|
bool res = inner_isparent(c, p);
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(c, 0);
|
|
|
|
PG_FREE_IF_COPY(p, 1);
|
|
|
|
PG_RETURN_BOOL(res);
|
2002-07-30 18:40:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
static ltree *
|
2012-06-25 00:51:46 +02:00
|
|
|
inner_subltree(ltree *t, int32 startpos, int32 endpos)
|
2002-09-04 22:31:48 +02:00
|
|
|
{
|
|
|
|
char *start = NULL,
|
|
|
|
*end = NULL;
|
2002-07-30 18:40:34 +02:00
|
|
|
ltree_level *ptr = LTREE_FIRST(t);
|
2002-09-04 22:31:48 +02:00
|
|
|
ltree *res;
|
|
|
|
int i;
|
2002-07-30 18:40:34 +02:00
|
|
|
|
2003-07-18 15:27:43 +02:00
|
|
|
if (startpos < 0 || endpos < 0 || startpos >= t->numlevel || startpos > endpos)
|
2003-07-24 19:52:50 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("invalid positions")));
|
2002-07-30 18:40:34 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (endpos > t->numlevel)
|
2002-07-30 18:40:34 +02:00
|
|
|
endpos = t->numlevel;
|
|
|
|
|
2003-07-18 15:27:43 +02:00
|
|
|
start = end = (char *) ptr;
|
2002-09-04 22:31:48 +02:00
|
|
|
for (i = 0; i < endpos; i++)
|
|
|
|
{
|
|
|
|
if (i == startpos)
|
|
|
|
start = (char *) ptr;
|
|
|
|
if (i == endpos - 1)
|
|
|
|
{
|
|
|
|
end = (char *) LEVEL_NEXT(ptr);
|
2002-07-30 18:40:34 +02:00
|
|
|
break;
|
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
ptr = LEVEL_NEXT(ptr);
|
2002-07-30 18:40:34 +02:00
|
|
|
}
|
|
|
|
|
2016-03-08 23:59:29 +01:00
|
|
|
res = (ltree *) palloc0(LTREE_HDRSIZE + (end - start));
|
2007-02-28 23:44:38 +01:00
|
|
|
SET_VARSIZE(res, LTREE_HDRSIZE + (end - start));
|
2002-09-04 22:31:48 +02:00
|
|
|
res->numlevel = endpos - startpos;
|
|
|
|
|
|
|
|
memcpy(LTREE_FIRST(res), start, end - start);
|
2002-07-30 18:40:34 +02:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
subltree(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
ltree *t = PG_GETARG_LTREE_P(0);
|
2002-09-04 22:31:48 +02:00
|
|
|
ltree *res = inner_subltree(t, PG_GETARG_INT32(1), PG_GETARG_INT32(2));
|
2002-07-30 18:40:34 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_FREE_IF_COPY(t, 0);
|
2002-07-30 18:40:34 +02:00
|
|
|
PG_RETURN_POINTER(res);
|
|
|
|
}
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
Datum
|
|
|
|
subpath(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
ltree *t = PG_GETARG_LTREE_P(0);
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 start = PG_GETARG_INT32(1);
|
|
|
|
int32 len = (fcinfo->nargs == 3) ? PG_GETARG_INT32(2) : 0;
|
|
|
|
int32 end;
|
2002-09-04 22:31:48 +02:00
|
|
|
ltree *res;
|
|
|
|
|
|
|
|
end = start + len;
|
|
|
|
|
|
|
|
if (start < 0)
|
|
|
|
{
|
2002-07-30 18:40:34 +02:00
|
|
|
start = t->numlevel + start;
|
2002-09-04 22:31:48 +02:00
|
|
|
end = start + len;
|
2002-07-30 18:40:34 +02:00
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
if (start < 0)
|
|
|
|
{ /* start > t->numlevel */
|
2002-07-30 18:40:34 +02:00
|
|
|
start = t->numlevel + start;
|
2002-09-04 22:31:48 +02:00
|
|
|
end = start + len;
|
2002-07-30 18:40:34 +02:00
|
|
|
}
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (len < 0)
|
2002-07-30 18:40:34 +02:00
|
|
|
end = t->numlevel + len;
|
2002-09-04 22:31:48 +02:00
|
|
|
else if (len == 0)
|
2003-07-18 15:27:43 +02:00
|
|
|
end = (fcinfo->nargs == 3) ? start : 0xffff;
|
2002-07-30 18:40:34 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
res = inner_subltree(t, start, end);
|
2002-07-30 18:40:34 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_FREE_IF_COPY(t, 0);
|
2002-07-30 18:40:34 +02:00
|
|
|
PG_RETURN_POINTER(res);
|
|
|
|
}
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
static ltree *
|
2009-06-11 16:49:15 +02:00
|
|
|
ltree_concat(ltree *a, ltree *b)
|
2002-09-04 22:31:48 +02:00
|
|
|
{
|
|
|
|
ltree *r;
|
2020-03-28 22:09:51 +01:00
|
|
|
int numlevel = (int) a->numlevel + b->numlevel;
|
|
|
|
|
|
|
|
if (numlevel > LTREE_MAX_LEVELS)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
|
|
|
errmsg("number of ltree levels (%d) exceeds the maximum allowed (%d)",
|
|
|
|
numlevel, LTREE_MAX_LEVELS)));
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2016-03-08 23:59:29 +01:00
|
|
|
r = (ltree *) palloc0(VARSIZE(a) + VARSIZE(b) - LTREE_HDRSIZE);
|
2007-02-28 23:44:38 +01:00
|
|
|
SET_VARSIZE(r, VARSIZE(a) + VARSIZE(b) - LTREE_HDRSIZE);
|
2020-03-28 22:09:51 +01:00
|
|
|
r->numlevel = (uint16) numlevel;
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2007-02-28 23:44:38 +01:00
|
|
|
memcpy(LTREE_FIRST(r), LTREE_FIRST(a), VARSIZE(a) - LTREE_HDRSIZE);
|
|
|
|
memcpy(((char *) LTREE_FIRST(r)) + VARSIZE(a) - LTREE_HDRSIZE,
|
|
|
|
LTREE_FIRST(b),
|
|
|
|
VARSIZE(b) - LTREE_HDRSIZE);
|
2002-09-04 22:31:48 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
ltree_addltree(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
ltree *a = PG_GETARG_LTREE_P(0);
|
|
|
|
ltree *b = PG_GETARG_LTREE_P(1);
|
2002-09-04 22:31:48 +02:00
|
|
|
ltree *r;
|
2002-07-30 18:40:34 +02:00
|
|
|
|
|
|
|
r = ltree_concat(a, b);
|
2002-09-04 22:31:48 +02:00
|
|
|
PG_FREE_IF_COPY(a, 0);
|
|
|
|
PG_FREE_IF_COPY(b, 1);
|
2002-07-30 18:40:34 +02:00
|
|
|
PG_RETURN_POINTER(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
ltree_addtext(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
ltree *a = PG_GETARG_LTREE_P(0);
|
2008-03-25 23:42:46 +01:00
|
|
|
text *b = PG_GETARG_TEXT_PP(1);
|
2002-09-04 22:31:48 +02:00
|
|
|
char *s;
|
|
|
|
ltree *r,
|
|
|
|
*tmp;
|
|
|
|
|
2008-03-25 23:42:46 +01:00
|
|
|
s = text_to_cstring(b);
|
2002-07-30 18:40:34 +02:00
|
|
|
|
2008-03-25 23:42:46 +01:00
|
|
|
tmp = (ltree *) DatumGetPointer(DirectFunctionCall1(ltree_in,
|
|
|
|
PointerGetDatum(s)));
|
2002-07-30 18:40:34 +02:00
|
|
|
|
|
|
|
pfree(s);
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
r = ltree_concat(a, tmp);
|
2002-07-30 18:40:34 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
pfree(tmp);
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(a, 0);
|
|
|
|
PG_FREE_IF_COPY(b, 1);
|
2002-07-30 18:40:34 +02:00
|
|
|
PG_RETURN_POINTER(r);
|
|
|
|
}
|
|
|
|
|
2003-03-31 22:53:45 +02:00
|
|
|
Datum
|
|
|
|
ltree_index(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
ltree *a = PG_GETARG_LTREE_P(0);
|
|
|
|
ltree *b = PG_GETARG_LTREE_P(1);
|
2003-08-04 02:43:34 +02:00
|
|
|
int start = (fcinfo->nargs == 3) ? PG_GETARG_INT32(2) : 0;
|
|
|
|
int i,
|
|
|
|
j;
|
|
|
|
ltree_level *startptr,
|
|
|
|
*aptr,
|
|
|
|
*bptr;
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
if (start < 0)
|
|
|
|
{
|
|
|
|
if (-start >= a->numlevel)
|
|
|
|
start = 0;
|
|
|
|
else
|
|
|
|
start = (int) (a->numlevel) + start;
|
2003-03-31 22:53:45 +02:00
|
|
|
}
|
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
if (a->numlevel - start < b->numlevel || a->numlevel == 0 || b->numlevel == 0)
|
|
|
|
{
|
2003-03-31 22:53:45 +02:00
|
|
|
PG_FREE_IF_COPY(a, 0);
|
|
|
|
PG_FREE_IF_COPY(b, 1);
|
|
|
|
PG_RETURN_INT32(-1);
|
|
|
|
}
|
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
startptr = LTREE_FIRST(a);
|
|
|
|
for (i = 0; i <= a->numlevel - b->numlevel; i++)
|
|
|
|
{
|
|
|
|
if (i >= start)
|
|
|
|
{
|
|
|
|
aptr = startptr;
|
|
|
|
bptr = LTREE_FIRST(b);
|
|
|
|
for (j = 0; j < b->numlevel; j++)
|
|
|
|
{
|
2010-12-22 04:11:40 +01:00
|
|
|
if (!(aptr->len == bptr->len && memcmp(aptr->name, bptr->name, aptr->len) == 0))
|
2003-08-04 02:43:34 +02:00
|
|
|
break;
|
|
|
|
aptr = LEVEL_NEXT(aptr);
|
|
|
|
bptr = LEVEL_NEXT(bptr);
|
2003-03-31 22:53:45 +02:00
|
|
|
}
|
2003-08-04 02:43:34 +02:00
|
|
|
|
|
|
|
if (j == b->numlevel)
|
|
|
|
{
|
|
|
|
found = true;
|
2003-03-31 22:53:45 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-08-04 02:43:34 +02:00
|
|
|
startptr = LEVEL_NEXT(startptr);
|
2003-03-31 22:53:45 +02:00
|
|
|
}
|
2003-08-04 02:43:34 +02:00
|
|
|
|
|
|
|
if (!found)
|
|
|
|
i = -1;
|
2003-03-31 22:53:45 +02:00
|
|
|
|
|
|
|
PG_FREE_IF_COPY(a, 0);
|
|
|
|
PG_FREE_IF_COPY(b, 1);
|
|
|
|
PG_RETURN_INT32(i);
|
|
|
|
}
|
|
|
|
|
2002-07-30 18:40:34 +02:00
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
ltree_textadd(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
ltree *a = PG_GETARG_LTREE_P(1);
|
2008-03-25 23:42:46 +01:00
|
|
|
text *b = PG_GETARG_TEXT_PP(0);
|
2002-09-04 22:31:48 +02:00
|
|
|
char *s;
|
|
|
|
ltree *r,
|
|
|
|
*tmp;
|
|
|
|
|
2008-03-25 23:42:46 +01:00
|
|
|
s = text_to_cstring(b);
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2008-03-25 23:42:46 +01:00
|
|
|
tmp = (ltree *) DatumGetPointer(DirectFunctionCall1(ltree_in,
|
|
|
|
PointerGetDatum(s)));
|
2002-09-04 22:31:48 +02:00
|
|
|
|
|
|
|
pfree(s);
|
|
|
|
|
|
|
|
r = ltree_concat(tmp, a);
|
|
|
|
|
|
|
|
pfree(tmp);
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(a, 1);
|
|
|
|
PG_FREE_IF_COPY(b, 0);
|
|
|
|
PG_RETURN_POINTER(r);
|
2002-07-30 18:40:34 +02:00
|
|
|
}
|
2002-08-04 07:02:50 +02:00
|
|
|
|
2018-07-14 00:45:30 +02:00
|
|
|
/*
|
|
|
|
* Common code for variants of lca(), find longest common ancestor of inputs
|
|
|
|
*
|
|
|
|
* Returns NULL if there is no common ancestor, ie, the longest common
|
|
|
|
* prefix is empty.
|
|
|
|
*/
|
2002-09-04 22:31:48 +02:00
|
|
|
ltree *
|
2009-06-11 16:49:15 +02:00
|
|
|
lca_inner(ltree **a, int len)
|
2002-09-04 22:31:48 +02:00
|
|
|
{
|
|
|
|
int tmp,
|
2018-07-14 00:45:30 +02:00
|
|
|
num,
|
|
|
|
i,
|
|
|
|
reslen;
|
|
|
|
ltree **ptr;
|
2002-09-04 22:31:48 +02:00
|
|
|
ltree_level *l1,
|
|
|
|
*l2;
|
|
|
|
ltree *res;
|
|
|
|
|
2018-07-14 00:45:30 +02:00
|
|
|
if (len <= 0)
|
|
|
|
return NULL; /* no inputs? */
|
2002-09-04 22:31:48 +02:00
|
|
|
if ((*a)->numlevel == 0)
|
2018-07-14 00:45:30 +02:00
|
|
|
return NULL; /* any empty input means NULL result */
|
|
|
|
|
|
|
|
/* num is the length of the longest common ancestor so far */
|
|
|
|
num = (*a)->numlevel - 1;
|
2002-08-04 07:02:50 +02:00
|
|
|
|
2018-07-14 00:45:30 +02:00
|
|
|
/* Compare each additional input to *a */
|
|
|
|
ptr = a + 1;
|
2002-09-04 22:31:48 +02:00
|
|
|
while (ptr - a < len)
|
|
|
|
{
|
|
|
|
if ((*ptr)->numlevel == 0)
|
2002-08-04 07:02:50 +02:00
|
|
|
return NULL;
|
2002-09-04 22:31:48 +02:00
|
|
|
else if ((*ptr)->numlevel == 1)
|
|
|
|
num = 0;
|
|
|
|
else
|
|
|
|
{
|
2002-08-04 07:02:50 +02:00
|
|
|
l1 = LTREE_FIRST(*a);
|
|
|
|
l2 = LTREE_FIRST(*ptr);
|
2018-07-14 00:45:30 +02:00
|
|
|
tmp = Min(num, (*ptr)->numlevel - 1);
|
2002-09-04 22:31:48 +02:00
|
|
|
num = 0;
|
2018-07-14 00:45:30 +02:00
|
|
|
for (i = 0; i < tmp; i++)
|
2002-09-04 22:31:48 +02:00
|
|
|
{
|
2018-07-14 00:45:30 +02:00
|
|
|
if (l1->len == l2->len &&
|
|
|
|
memcmp(l1->name, l2->name, l1->len) == 0)
|
2002-09-04 22:31:48 +02:00
|
|
|
num = i + 1;
|
2002-08-04 07:02:50 +02:00
|
|
|
else
|
|
|
|
break;
|
2002-09-04 22:31:48 +02:00
|
|
|
l1 = LEVEL_NEXT(l1);
|
|
|
|
l2 = LEVEL_NEXT(l2);
|
2002-08-04 07:02:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
2018-07-14 00:45:30 +02:00
|
|
|
/* Now compute size of result ... */
|
|
|
|
reslen = LTREE_HDRSIZE;
|
2002-08-04 07:02:50 +02:00
|
|
|
l1 = LTREE_FIRST(*a);
|
2002-09-04 22:31:48 +02:00
|
|
|
for (i = 0; i < num; i++)
|
|
|
|
{
|
2002-08-04 07:02:50 +02:00
|
|
|
reslen += MAXALIGN(l1->len + LEVEL_HDRSIZE);
|
2002-09-04 22:31:48 +02:00
|
|
|
l1 = LEVEL_NEXT(l1);
|
2002-08-04 07:02:50 +02:00
|
|
|
}
|
|
|
|
|
2018-07-14 00:45:30 +02:00
|
|
|
/* ... and construct it by copying from *a */
|
2016-03-08 23:59:29 +01:00
|
|
|
res = (ltree *) palloc0(reslen);
|
2007-02-28 23:44:38 +01:00
|
|
|
SET_VARSIZE(res, reslen);
|
2002-08-04 07:02:50 +02:00
|
|
|
res->numlevel = num;
|
|
|
|
|
|
|
|
l1 = LTREE_FIRST(*a);
|
|
|
|
l2 = LTREE_FIRST(res);
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
for (i = 0; i < num; i++)
|
|
|
|
{
|
|
|
|
memcpy(l2, l1, MAXALIGN(l1->len + LEVEL_HDRSIZE));
|
|
|
|
l1 = LEVEL_NEXT(l1);
|
|
|
|
l2 = LEVEL_NEXT(l2);
|
|
|
|
}
|
2002-08-04 07:02:50 +02:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-04 22:31:48 +02:00
|
|
|
lca(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
ltree **a,
|
|
|
|
*res;
|
|
|
|
|
|
|
|
a = (ltree **) palloc(sizeof(ltree *) * fcinfo->nargs);
|
|
|
|
for (i = 0; i < fcinfo->nargs; i++)
|
2017-09-18 21:21:23 +02:00
|
|
|
a[i] = PG_GETARG_LTREE_P(i);
|
2002-09-04 22:31:48 +02:00
|
|
|
res = lca_inner(a, (int) fcinfo->nargs);
|
|
|
|
for (i = 0; i < fcinfo->nargs; i++)
|
|
|
|
PG_FREE_IF_COPY(a[i], i);
|
2002-08-04 07:02:50 +02:00
|
|
|
pfree(a);
|
2002-09-04 22:31:48 +02:00
|
|
|
|
|
|
|
if (res)
|
2002-08-04 07:02:50 +02:00
|
|
|
PG_RETURN_POINTER(res);
|
|
|
|
else
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
}
|
2003-03-31 22:53:45 +02:00
|
|
|
|
|
|
|
Datum
|
|
|
|
text2ltree(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2008-03-25 23:42:46 +01:00
|
|
|
text *in = PG_GETARG_TEXT_PP(0);
|
|
|
|
char *s;
|
2003-08-04 02:43:34 +02:00
|
|
|
ltree *out;
|
2003-03-31 22:53:45 +02:00
|
|
|
|
2008-03-25 23:42:46 +01:00
|
|
|
s = text_to_cstring(in);
|
2003-03-31 22:53:45 +02:00
|
|
|
|
2008-03-25 23:42:46 +01:00
|
|
|
out = (ltree *) DatumGetPointer(DirectFunctionCall1(ltree_in,
|
|
|
|
PointerGetDatum(s)));
|
2003-03-31 22:53:45 +02:00
|
|
|
pfree(s);
|
2003-08-04 02:43:34 +02:00
|
|
|
PG_FREE_IF_COPY(in, 0);
|
2003-03-31 22:53:45 +02:00
|
|
|
PG_RETURN_POINTER(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
ltree2text(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-18 21:21:23 +02:00
|
|
|
ltree *in = PG_GETARG_LTREE_P(0);
|
2003-08-04 02:43:34 +02:00
|
|
|
char *ptr;
|
|
|
|
int i;
|
2003-03-31 22:53:45 +02:00
|
|
|
ltree_level *curlevel;
|
2003-08-04 02:43:34 +02:00
|
|
|
text *out;
|
|
|
|
|
2007-02-28 23:44:38 +01:00
|
|
|
out = (text *) palloc(VARSIZE(in) + VARHDRSZ);
|
2003-08-04 02:43:34 +02:00
|
|
|
ptr = VARDATA(out);
|
2003-03-31 22:53:45 +02:00
|
|
|
curlevel = LTREE_FIRST(in);
|
2003-08-04 02:43:34 +02:00
|
|
|
for (i = 0; i < in->numlevel; i++)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
{
|
2003-03-31 22:53:45 +02:00
|
|
|
*ptr = '.';
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
memcpy(ptr, curlevel->name, curlevel->len);
|
|
|
|
ptr += curlevel->len;
|
|
|
|
curlevel = LEVEL_NEXT(curlevel);
|
|
|
|
}
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2007-02-28 00:48:10 +01:00
|
|
|
SET_VARSIZE(out, ptr - ((char *) out));
|
2003-03-31 22:53:45 +02:00
|
|
|
PG_FREE_IF_COPY(in, 0);
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2003-03-31 22:53:45 +02:00
|
|
|
PG_RETURN_POINTER(out);
|
|
|
|
}
|
2006-04-27 00:33:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ltreeparentsel - Selectivity of parent relationship for ltree data types.
|
Improve selectivity estimation for assorted match-style operators.
Quite a few matching operators such as JSONB's @> used "contsel" and
"contjoinsel" as their selectivity estimators. That was a bad idea,
because (a) contsel is only a stub, yielding a fixed default estimate,
and (b) that default is 0.001, meaning we estimate these operators as
five times more selective than equality, which is surely pretty silly.
There's a good model for improving this in ltree's ltreeparentsel():
for any "var OP constant" query, we can try applying the operator
to all of the column's MCV and histogram values, taking the latter
as being a random sample of the non-MCV values. That code is
actually 100% generic, except for the question of exactly what
default selectivity ought to be plugged in when we don't have stats.
Hence, migrate the guts of ltreeparentsel() into the core code, provide
wrappers "matchingsel" and "matchingjoinsel" with a more-appropriate
default estimate, and use those for the non-geometric operators that
formerly used contsel (mostly JSONB containment operators and tsquery
matching).
Also apply this code to some match-like operators in hstore, ltree, and
pg_trgm, including the former users of ltreeparentsel as well as ones
that improperly used contsel. Since commit 911e70207 just created new
versions of those extensions that we haven't released yet, we can sneak
this change into those new versions instead of having to create an
additional generation of update scripts.
Patch by me, reviewed by Alexey Bashtanov
Discussion: https://postgr.es/m/12237.1582833074@sss.pgh.pa.us
2020-04-01 16:32:33 +02:00
|
|
|
*
|
|
|
|
* This function is not used anymore, if the ltree extension has been
|
|
|
|
* updated to 1.2 or later.
|
2006-04-27 00:33:36 +02:00
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
ltreeparentsel(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
|
|
|
|
Oid operator = PG_GETARG_OID(1);
|
|
|
|
List *args = (List *) PG_GETARG_POINTER(2);
|
|
|
|
int varRelid = PG_GETARG_INT32(3);
|
2006-04-27 20:24:35 +02:00
|
|
|
double selec;
|
2006-04-27 00:33:36 +02:00
|
|
|
|
Improve selectivity estimation for assorted match-style operators.
Quite a few matching operators such as JSONB's @> used "contsel" and
"contjoinsel" as their selectivity estimators. That was a bad idea,
because (a) contsel is only a stub, yielding a fixed default estimate,
and (b) that default is 0.001, meaning we estimate these operators as
five times more selective than equality, which is surely pretty silly.
There's a good model for improving this in ltree's ltreeparentsel():
for any "var OP constant" query, we can try applying the operator
to all of the column's MCV and histogram values, taking the latter
as being a random sample of the non-MCV values. That code is
actually 100% generic, except for the question of exactly what
default selectivity ought to be plugged in when we don't have stats.
Hence, migrate the guts of ltreeparentsel() into the core code, provide
wrappers "matchingsel" and "matchingjoinsel" with a more-appropriate
default estimate, and use those for the non-geometric operators that
formerly used contsel (mostly JSONB containment operators and tsquery
matching).
Also apply this code to some match-like operators in hstore, ltree, and
pg_trgm, including the former users of ltreeparentsel as well as ones
that improperly used contsel. Since commit 911e70207 just created new
versions of those extensions that we haven't released yet, we can sneak
this change into those new versions instead of having to create an
additional generation of update scripts.
Patch by me, reviewed by Alexey Bashtanov
Discussion: https://postgr.es/m/12237.1582833074@sss.pgh.pa.us
2020-04-01 16:32:33 +02:00
|
|
|
/* Use generic restriction selectivity logic, with default 0.001. */
|
|
|
|
selec = generic_restriction_selectivity(root, operator,
|
|
|
|
args, varRelid,
|
|
|
|
0.001);
|
2006-04-27 00:33:36 +02:00
|
|
|
|
|
|
|
PG_RETURN_FLOAT8((float8) selec);
|
|
|
|
}
|