2002-07-31 00:40:34 +08:00
|
|
|
/*
|
2002-09-05 04:31:48 +08:00
|
|
|
* op function for ltree and lquery
|
2002-07-31 00:40:34 +08:00
|
|
|
* Teodor Sigaev <teodor@stack.net>
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/ltree/lquery_op.c
|
2002-07-31 00:40:34 +08:00
|
|
|
*/
|
2008-05-12 08:00:54 +08:00
|
|
|
#include "postgres.h"
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
#include <ctype.h>
|
2008-05-12 08:00:54 +08:00
|
|
|
|
2011-02-09 05:04:18 +08:00
|
|
|
#include "catalog/pg_collation.h"
|
2008-07-01 02:30:48 +08:00
|
|
|
#include "utils/formatting.h"
|
2008-05-12 08:00:54 +08:00
|
|
|
#include "ltree.h"
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(ltq_regex);
|
|
|
|
PG_FUNCTION_INFO_V1(ltq_rregex);
|
|
|
|
|
2003-02-19 11:50:09 +08:00
|
|
|
PG_FUNCTION_INFO_V1(lt_q_regex);
|
|
|
|
PG_FUNCTION_INFO_V1(lt_q_rregex);
|
|
|
|
|
|
|
|
#define NEXTVAL(x) ( (lquery*)( (char*)(x) + INTALIGN( VARSIZE(x) ) ) )
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
lquery_level *q;
|
|
|
|
int nq;
|
|
|
|
ltree_level *t;
|
|
|
|
int nt;
|
|
|
|
int posq;
|
|
|
|
int post;
|
2009-06-11 22:49:15 +08:00
|
|
|
} FieldNot;
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
static char *
|
2006-03-01 14:30:32 +08:00
|
|
|
getlexeme(char *start, char *end, int *len)
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
|
|
|
char *ptr;
|
2009-06-11 22:49:15 +08:00
|
|
|
int charlen;
|
|
|
|
|
|
|
|
while (start < end && (charlen = pg_mblen(start)) == 1 && t_iseq(start, '_'))
|
2008-07-01 02:30:48 +08:00
|
|
|
start += charlen;
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
ptr = start;
|
2008-07-01 02:30:48 +08:00
|
|
|
if (ptr >= end)
|
2002-07-31 00:40:34 +08:00
|
|
|
return NULL;
|
|
|
|
|
2009-06-11 22:49:15 +08:00
|
|
|
while (ptr < end && !((charlen = pg_mblen(ptr)) == 1 && t_iseq(ptr, '_')))
|
2008-07-01 02:30:48 +08:00
|
|
|
ptr += charlen;
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
*len = ptr - start;
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2009-06-11 22:49:15 +08:00
|
|
|
compare_subnode(ltree_level *t, char *qn, int len, int (*cmpptr) (const char *, const char *, size_t), bool anyend)
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
|
|
|
char *endt = t->name + t->len;
|
|
|
|
char *endq = qn + len;
|
|
|
|
char *tn;
|
|
|
|
int lent,
|
|
|
|
lenq;
|
|
|
|
bool isok;
|
|
|
|
|
2006-03-01 14:30:32 +08:00
|
|
|
while ((qn = getlexeme(qn, endq, &lenq)) != NULL)
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
|
|
|
tn = t->name;
|
2002-07-31 00:40:34 +08:00
|
|
|
isok = false;
|
2006-03-01 14:30:32 +08:00
|
|
|
while ((tn = getlexeme(tn, endt, &lent)) != NULL)
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
|
|
|
if (
|
2002-07-31 00:40:34 +08:00
|
|
|
(
|
2002-09-05 04:31:48 +08:00
|
|
|
lent == lenq ||
|
|
|
|
(lent > lenq && anyend)
|
|
|
|
) &&
|
|
|
|
(*cmpptr) (qn, tn, lenq) == 0)
|
|
|
|
{
|
|
|
|
|
|
|
|
isok = true;
|
2002-07-31 00:40:34 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
tn += lent;
|
|
|
|
}
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
if (!isok)
|
2002-07-31 00:40:34 +08:00
|
|
|
return false;
|
|
|
|
qn += lenq;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-07-01 02:30:48 +08:00
|
|
|
int
|
|
|
|
ltree_strncasecmp(const char *a, const char *b, size_t s)
|
|
|
|
{
|
2011-02-09 05:04:18 +08:00
|
|
|
char *al = str_tolower(a, s, DEFAULT_COLLATION_OID);
|
|
|
|
char *bl = str_tolower(b, s, DEFAULT_COLLATION_OID);
|
2009-06-11 22:49:15 +08:00
|
|
|
int res;
|
|
|
|
|
|
|
|
res = strncmp(al, bl, s);
|
2008-07-01 02:30:48 +08:00
|
|
|
|
|
|
|
pfree(al);
|
|
|
|
pfree(bl);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-07-31 00:40:34 +08:00
|
|
|
static bool
|
2009-06-11 22:49:15 +08:00
|
|
|
checkLevel(lquery_level *curq, ltree_level *curt)
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
|
|
|
int (*cmpptr) (const char *, const char *, size_t);
|
2002-07-31 00:40:34 +08:00
|
|
|
lquery_variant *curvar = LQL_FIRST(curq);
|
2002-09-05 04:31:48 +08:00
|
|
|
int i;
|
2002-07-31 00:40:34 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
for (i = 0; i < curq->numvar; i++)
|
|
|
|
{
|
2008-07-01 02:30:48 +08:00
|
|
|
cmpptr = (curvar->flag & LVAR_INCASE) ? ltree_strncasecmp : strncmp;
|
2002-09-05 04:31:48 +08:00
|
|
|
|
2006-03-01 14:30:32 +08:00
|
|
|
if (curvar->flag & LVAR_SUBLEXEME)
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
|
|
|
if (compare_subnode(curt, curvar->name, curvar->len, cmpptr, (curvar->flag & LVAR_ANYEND)))
|
2002-07-31 00:40:34 +08:00
|
|
|
return true;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else if (
|
|
|
|
(
|
|
|
|
curvar->len == curt->len ||
|
2005-10-15 10:49:52 +08:00
|
|
|
(curt->len > curvar->len && (curvar->flag & LVAR_ANYEND))
|
2002-09-05 04:31:48 +08:00
|
|
|
) &&
|
|
|
|
(*cmpptr) (curvar->name, curt->name, curvar->len) == 0)
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
curvar = LVAR_NEXT(curvar);
|
2002-07-31 00:40:34 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
void
|
|
|
|
printFieldNot(FieldNot *fn ) {
|
|
|
|
while(fn->q) {
|
|
|
|
elog(NOTICE,"posQ:%d lenQ:%d posT:%d lenT:%d", fn->posq,fn->nq,fn->post,fn->nt);
|
|
|
|
fn++;
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
2002-07-31 00:40:34 +08:00
|
|
|
*/
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
static struct
|
|
|
|
{
|
|
|
|
bool muse;
|
|
|
|
uint32 high_pos;
|
2017-06-22 02:39:04 +08:00
|
|
|
} SomeStack =
|
2003-08-04 08:43:34 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
false, 0,
|
|
|
|
};
|
2003-02-19 11:50:09 +08:00
|
|
|
|
2002-07-31 00:40:34 +08:00
|
|
|
static bool
|
2009-06-11 22:49:15 +08:00
|
|
|
checkCond(lquery_level *curq, int query_numlevel, ltree_level *curt, int tree_numlevel, FieldNot *ptr)
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
|
|
|
uint32 low_pos = 0,
|
|
|
|
high_pos = 0,
|
|
|
|
cur_tpos = 0;
|
|
|
|
int tlen = tree_numlevel,
|
|
|
|
qlen = query_numlevel;
|
|
|
|
int isok;
|
|
|
|
lquery_level *prevq = NULL;
|
|
|
|
ltree_level *prevt = NULL;
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (SomeStack.muse)
|
|
|
|
{
|
2003-02-19 11:50:09 +08:00
|
|
|
high_pos = SomeStack.high_pos;
|
|
|
|
qlen--;
|
|
|
|
prevq = curq;
|
|
|
|
curq = LQL_NEXT(curq);
|
|
|
|
SomeStack.muse = false;
|
|
|
|
}
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
while (tlen > 0 && qlen > 0)
|
|
|
|
{
|
|
|
|
if (curq->numvar)
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
prevt = curt;
|
2002-09-05 04:31:48 +08:00
|
|
|
while (cur_tpos < low_pos)
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
curt = LEVEL_NEXT(curt);
|
|
|
|
tlen--;
|
|
|
|
cur_tpos++;
|
2002-09-05 04:31:48 +08:00
|
|
|
if (tlen == 0)
|
2002-07-31 00:40:34 +08:00
|
|
|
return false;
|
2002-09-05 04:31:48 +08:00
|
|
|
if (ptr && ptr->q)
|
2002-07-31 00:40:34 +08:00
|
|
|
ptr->nt++;
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
|
|
|
|
if (ptr && curq->flag & LQL_NOT)
|
|
|
|
{
|
|
|
|
if (!(prevq && prevq->numvar == 0))
|
2002-07-31 00:40:34 +08:00
|
|
|
prevq = curq;
|
2002-09-05 04:31:48 +08:00
|
|
|
if (ptr->q == NULL)
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
ptr->t = prevt;
|
|
|
|
ptr->q = prevq;
|
2002-09-05 04:31:48 +08:00
|
|
|
ptr->nt = 1;
|
|
|
|
ptr->nq = 1 + ((prevq == curq) ? 0 : 1);
|
|
|
|
ptr->posq = query_numlevel - qlen - ((prevq == curq) ? 0 : 1);
|
2002-07-31 00:40:34 +08:00
|
|
|
ptr->post = cur_tpos;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
ptr->nt++;
|
|
|
|
ptr->nq++;
|
|
|
|
}
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
if (qlen == 1 && ptr->q->numvar == 0)
|
|
|
|
ptr->nt = tree_numlevel - ptr->post;
|
2002-07-31 00:40:34 +08:00
|
|
|
curt = LEVEL_NEXT(curt);
|
|
|
|
tlen--;
|
|
|
|
cur_tpos++;
|
2002-09-05 04:31:48 +08:00
|
|
|
if (high_pos < cur_tpos)
|
2002-07-31 00:40:34 +08:00
|
|
|
high_pos++;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
isok = false;
|
2002-09-05 04:31:48 +08:00
|
|
|
while (cur_tpos <= high_pos && tlen > 0 && !isok)
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
isok = checkLevel(curq, curt);
|
|
|
|
curt = LEVEL_NEXT(curt);
|
|
|
|
tlen--;
|
|
|
|
cur_tpos++;
|
2003-08-04 08:43:34 +08:00
|
|
|
if (isok && prevq && prevq->numvar == 0 && tlen > 0 && cur_tpos <= high_pos)
|
|
|
|
{
|
|
|
|
FieldNot tmpptr;
|
|
|
|
|
|
|
|
if (ptr)
|
|
|
|
memcpy(&tmpptr, ptr, sizeof(FieldNot));
|
|
|
|
SomeStack.high_pos = high_pos - cur_tpos;
|
2003-02-19 11:50:09 +08:00
|
|
|
SomeStack.muse = true;
|
2003-08-04 08:43:34 +08:00
|
|
|
if (checkCond(prevq, qlen + 1, curt, tlen, (ptr) ? &tmpptr : NULL))
|
2003-02-19 11:50:09 +08:00
|
|
|
return true;
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
if (!isok && ptr)
|
2002-07-31 00:40:34 +08:00
|
|
|
ptr->nt++;
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
if (!isok)
|
2002-07-31 00:40:34 +08:00
|
|
|
return false;
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
if (ptr && ptr->q)
|
|
|
|
{
|
|
|
|
if (checkCond(ptr->q, ptr->nq, ptr->t, ptr->nt, NULL))
|
2002-07-31 00:40:34 +08:00
|
|
|
return false;
|
|
|
|
ptr->q = NULL;
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
low_pos = cur_tpos;
|
|
|
|
high_pos = cur_tpos;
|
2002-07-31 00:40:34 +08:00
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
low_pos = cur_tpos + curq->low;
|
|
|
|
high_pos = cur_tpos + curq->high;
|
2002-09-05 04:31:48 +08:00
|
|
|
if (ptr && ptr->q)
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
ptr->nq++;
|
2002-09-05 04:31:48 +08:00
|
|
|
if (qlen == 1)
|
2002-07-31 00:40:34 +08:00
|
|
|
ptr->nt = tree_numlevel - ptr->post;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prevq = curq;
|
|
|
|
curq = LQL_NEXT(curq);
|
|
|
|
qlen--;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
2002-07-31 00:40:34 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
if (low_pos > tree_numlevel || tree_numlevel > high_pos)
|
2002-07-31 00:40:34 +08:00
|
|
|
return false;
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
while (qlen > 0)
|
|
|
|
{
|
|
|
|
if (curq->numvar)
|
|
|
|
{
|
|
|
|
if (!(curq->flag & LQL_NOT))
|
2002-07-31 00:40:34 +08:00
|
|
|
return false;
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
low_pos = cur_tpos + curq->low;
|
|
|
|
high_pos = cur_tpos + curq->high;
|
|
|
|
}
|
|
|
|
|
|
|
|
curq = LQL_NEXT(curq);
|
|
|
|
qlen--;
|
|
|
|
}
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
if (low_pos > tree_numlevel || tree_numlevel > high_pos)
|
2002-07-31 00:40:34 +08:00
|
|
|
return false;
|
2002-09-05 04:31:48 +08:00
|
|
|
|
|
|
|
if (ptr && ptr->q && checkCond(ptr->q, ptr->nq, ptr->t, ptr->nt, NULL))
|
2002-07-31 00:40:34 +08:00
|
|
|
return false;
|
2002-09-05 04:31:48 +08:00
|
|
|
|
2002-07-31 00:40:34 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2002-09-05 04:31:48 +08:00
|
|
|
ltq_regex(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-19 03:21:23 +08:00
|
|
|
ltree *tree = PG_GETARG_LTREE_P(0);
|
|
|
|
lquery *query = PG_GETARG_LQUERY_P(1);
|
2002-09-05 04:31:48 +08:00
|
|
|
bool res = false;
|
2002-07-31 00:40:34 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
if (query->flag & LQUERY_HASNOT)
|
|
|
|
{
|
2002-07-31 00:40:34 +08:00
|
|
|
FieldNot fn;
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
fn.q = NULL;
|
2002-07-31 00:40:34 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
res = checkCond(LQUERY_FIRST(query), query->numlevel,
|
|
|
|
LTREE_FIRST(tree), tree->numlevel, &fn);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res = checkCond(LQUERY_FIRST(query), query->numlevel,
|
|
|
|
LTREE_FIRST(tree), tree->numlevel, NULL);
|
2002-07-31 00:40:34 +08:00
|
|
|
}
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
PG_FREE_IF_COPY(tree, 0);
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
|
|
|
PG_RETURN_BOOL(res);
|
2002-07-31 00:40:34 +08:00
|
|
|
}
|
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
Datum
|
|
|
|
ltq_rregex(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PG_RETURN_DATUM(DirectFunctionCall2(ltq_regex,
|
|
|
|
PG_GETARG_DATUM(1),
|
|
|
|
PG_GETARG_DATUM(0)
|
|
|
|
));
|
2002-07-31 00:40:34 +08:00
|
|
|
}
|
2003-02-19 11:50:09 +08:00
|
|
|
|
|
|
|
Datum
|
|
|
|
lt_q_regex(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-09-19 03:21:23 +08:00
|
|
|
ltree *tree = PG_GETARG_LTREE_P(0);
|
2003-08-04 08:43:34 +08:00
|
|
|
ArrayType *_query = PG_GETARG_ARRAYTYPE_P(1);
|
|
|
|
lquery *query = (lquery *) ARR_DATA_PTR(_query);
|
|
|
|
bool res = false;
|
|
|
|
int num = ArrayGetNItems(ARR_NDIM(_query), ARR_DIMS(_query));
|
2003-02-19 11:50:09 +08:00
|
|
|
|
2010-02-25 02:02:24 +08:00
|
|
|
if (ARR_NDIM(_query) > 1)
|
2003-08-04 08:43:34 +08:00
|
|
|
ereport(ERROR,
|
2003-07-25 01:52:50 +08:00
|
|
|
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
|
|
|
|
errmsg("array must be one-dimensional")));
|
2011-01-10 02:09:07 +08:00
|
|
|
if (array_contains_nulls(_query))
|
2005-11-19 10:08:45 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
|
|
|
|
errmsg("array must not contain nulls")));
|
2003-02-19 11:50:09 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
while (num > 0)
|
|
|
|
{
|
2003-02-19 11:50:09 +08:00
|
|
|
if (DatumGetBool(DirectFunctionCall2(ltq_regex,
|
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-22 03:35:54 +08:00
|
|
|
PointerGetDatum(tree), PointerGetDatum(query))))
|
2003-08-04 08:43:34 +08:00
|
|
|
{
|
2003-02-19 11:50:09 +08:00
|
|
|
|
|
|
|
res = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
num--;
|
|
|
|
query = NEXTVAL(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(tree, 0);
|
|
|
|
PG_FREE_IF_COPY(_query, 1);
|
|
|
|
PG_RETURN_BOOL(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
lt_q_rregex(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PG_RETURN_DATUM(DirectFunctionCall2(lt_q_regex,
|
|
|
|
PG_GETARG_DATUM(1),
|
|
|
|
PG_GETARG_DATUM(0)
|
|
|
|
));
|
|
|
|
}
|