New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
#include "postgres.h"
|
|
|
|
#include "fmgr.h"
|
|
|
|
|
|
|
|
#include "query_util.h"
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(tsquery_numnode);
|
2005-11-23 02:17:34 +08:00
|
|
|
Datum tsquery_numnode(PG_FUNCTION_ARGS);
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
|
|
|
|
Datum
|
2005-11-23 02:17:34 +08:00
|
|
|
tsquery_numnode(PG_FUNCTION_ARGS)
|
|
|
|
{
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
QUERYTYPE *query = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0)));
|
2005-11-23 02:17:34 +08:00
|
|
|
int nnode = query->size;
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(query, 0);
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
PG_RETURN_INT32(nnode);
|
|
|
|
}
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
static QTNode *
|
|
|
|
join_tsqueries(QUERYTYPE * a, QUERYTYPE * b)
|
|
|
|
{
|
|
|
|
QTNode *res = (QTNode *) palloc0(sizeof(QTNode));
|
2005-11-21 20:27:57 +08:00
|
|
|
|
|
|
|
res->flags |= QTN_NEEDFREE;
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
res->valnode = (ITEM *) palloc0(sizeof(ITEM));
|
2005-11-21 20:27:57 +08:00
|
|
|
res->valnode->type = OPR;
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
res->child = (QTNode **) palloc0(sizeof(QTNode *) * 2);
|
|
|
|
res->child[0] = QT2QTN(GETQUERY(b), GETOPERAND(b));
|
|
|
|
res->child[1] = QT2QTN(GETQUERY(a), GETOPERAND(a));
|
2005-11-21 20:27:57 +08:00
|
|
|
res->nchild = 2;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(tsquery_and);
|
2005-11-23 02:17:34 +08:00
|
|
|
Datum tsquery_and(PG_FUNCTION_ARGS);
|
2005-11-21 20:27:57 +08:00
|
|
|
|
|
|
|
Datum
|
2005-11-23 02:17:34 +08:00
|
|
|
tsquery_and(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2005-11-21 20:27:57 +08:00
|
|
|
QUERYTYPE *a = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0)));
|
|
|
|
QUERYTYPE *b = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(1)));
|
2005-11-23 02:17:34 +08:00
|
|
|
QTNode *res;
|
2005-11-21 20:27:57 +08:00
|
|
|
QUERYTYPE *query;
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
if (a->size == 0)
|
|
|
|
{
|
|
|
|
PG_FREE_IF_COPY(a, 1);
|
2005-11-21 20:27:57 +08:00
|
|
|
PG_RETURN_POINTER(b);
|
2005-11-23 02:17:34 +08:00
|
|
|
}
|
|
|
|
else if (b->size == 0)
|
|
|
|
{
|
|
|
|
PG_FREE_IF_COPY(b, 1);
|
2005-11-21 20:27:57 +08:00
|
|
|
PG_RETURN_POINTER(a);
|
2005-11-23 02:17:34 +08:00
|
|
|
}
|
2005-11-21 20:27:57 +08:00
|
|
|
|
|
|
|
res = join_tsqueries(a, b);
|
|
|
|
|
|
|
|
res->valnode->val = '&';
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
query = QTN2QT(res, PlainMemory);
|
2005-11-21 20:27:57 +08:00
|
|
|
|
|
|
|
QTNFree(res);
|
2005-11-23 02:17:34 +08:00
|
|
|
PG_FREE_IF_COPY(a, 0);
|
|
|
|
PG_FREE_IF_COPY(b, 1);
|
2005-11-21 20:27:57 +08:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(tsquery_or);
|
2005-11-23 02:17:34 +08:00
|
|
|
Datum tsquery_or(PG_FUNCTION_ARGS);
|
2005-11-21 20:27:57 +08:00
|
|
|
|
|
|
|
Datum
|
2005-11-23 02:17:34 +08:00
|
|
|
tsquery_or(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2005-11-21 20:27:57 +08:00
|
|
|
QUERYTYPE *a = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0)));
|
|
|
|
QUERYTYPE *b = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(1)));
|
2005-11-23 02:17:34 +08:00
|
|
|
QTNode *res;
|
2005-11-21 20:27:57 +08:00
|
|
|
QUERYTYPE *query;
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
if (a->size == 0)
|
|
|
|
{
|
|
|
|
PG_FREE_IF_COPY(a, 1);
|
2005-11-21 20:27:57 +08:00
|
|
|
PG_RETURN_POINTER(b);
|
2005-11-23 02:17:34 +08:00
|
|
|
}
|
|
|
|
else if (b->size == 0)
|
|
|
|
{
|
|
|
|
PG_FREE_IF_COPY(b, 1);
|
2005-11-21 20:27:57 +08:00
|
|
|
PG_RETURN_POINTER(a);
|
2005-11-23 02:17:34 +08:00
|
|
|
}
|
2005-11-21 20:27:57 +08:00
|
|
|
|
|
|
|
res = join_tsqueries(a, b);
|
|
|
|
|
|
|
|
res->valnode->val = '|';
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
query = QTN2QT(res, PlainMemory);
|
2005-11-21 20:27:57 +08:00
|
|
|
|
|
|
|
QTNFree(res);
|
2005-11-23 02:17:34 +08:00
|
|
|
PG_FREE_IF_COPY(a, 0);
|
|
|
|
PG_FREE_IF_COPY(b, 1);
|
2005-11-21 20:27:57 +08:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(tsquery_not);
|
2005-11-23 02:17:34 +08:00
|
|
|
Datum tsquery_not(PG_FUNCTION_ARGS);
|
2005-11-21 20:27:57 +08:00
|
|
|
|
|
|
|
Datum
|
2005-11-23 02:17:34 +08:00
|
|
|
tsquery_not(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2005-11-21 20:27:57 +08:00
|
|
|
QUERYTYPE *a = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0)));
|
2005-11-23 02:17:34 +08:00
|
|
|
QTNode *res;
|
2005-11-21 20:27:57 +08:00
|
|
|
QUERYTYPE *query;
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
if (a->size == 0)
|
2005-11-21 20:27:57 +08:00
|
|
|
PG_RETURN_POINTER(a);
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
res = (QTNode *) palloc0(sizeof(QTNode));
|
2005-11-21 20:27:57 +08:00
|
|
|
|
|
|
|
res->flags |= QTN_NEEDFREE;
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
res->valnode = (ITEM *) palloc0(sizeof(ITEM));
|
2005-11-21 20:27:57 +08:00
|
|
|
res->valnode->type = OPR;
|
|
|
|
res->valnode->val = '!';
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
res->child = (QTNode **) palloc0(sizeof(QTNode *));
|
|
|
|
res->child[0] = QT2QTN(GETQUERY(a), GETOPERAND(a));
|
2005-11-21 20:27:57 +08:00
|
|
|
res->nchild = 1;
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
query = QTN2QT(res, PlainMemory);
|
2005-11-21 20:27:57 +08:00
|
|
|
|
|
|
|
QTNFree(res);
|
2005-11-23 02:17:34 +08:00
|
|
|
PG_FREE_IF_COPY(a, 0);
|
2005-11-21 20:27:57 +08:00
|
|
|
|
|
|
|
PG_RETURN_POINTER(query);
|
|
|
|
}
|
|
|
|
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
static int
|
2005-11-23 02:17:34 +08:00
|
|
|
CompareTSQ(QUERYTYPE * a, QUERYTYPE * b)
|
|
|
|
{
|
|
|
|
if (a->size != b->size)
|
|
|
|
{
|
|
|
|
return (a->size < b->size) ? -1 : 1;
|
|
|
|
}
|
|
|
|
else if (a->len != b->len)
|
|
|
|
{
|
|
|
|
return (a->len < b->len) ? -1 : 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QTNode *an = QT2QTN(GETQUERY(a), GETOPERAND(a));
|
|
|
|
QTNode *bn = QT2QTN(GETQUERY(b), GETOPERAND(b));
|
|
|
|
int res = QTNodeCompare(an, bn);
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
|
|
|
|
QTNFree(an);
|
|
|
|
QTNFree(bn);
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
return res;
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
PG_FUNCTION_INFO_V1(tsquery_cmp);
|
|
|
|
\
|
|
|
|
Datum tsquery_cmp(PG_FUNCTION_ARGS);
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
|
|
|
|
Datum
|
2005-11-23 02:17:34 +08:00
|
|
|
tsquery_cmp(PG_FUNCTION_ARGS)
|
|
|
|
{
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
QUERYTYPE *a = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0)));
|
|
|
|
QUERYTYPE *b = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(1)));
|
2005-11-23 02:17:34 +08:00
|
|
|
int res = CompareTSQ(a, b);
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
PG_FREE_IF_COPY(a, 0);
|
|
|
|
PG_FREE_IF_COPY(b, 1);
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
|
|
|
|
PG_RETURN_INT32(res);
|
|
|
|
}
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
#define CMPFUNC( NAME, ACTION ) \
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
PG_FUNCTION_INFO_V1(NAME); \
|
|
|
|
Datum NAME(PG_FUNCTION_ARGS); \
|
|
|
|
\
|
|
|
|
Datum \
|
2005-11-23 02:17:34 +08:00
|
|
|
NAME(PG_FUNCTION_ARGS) { \
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
QUERYTYPE *a = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0))); \
|
|
|
|
QUERYTYPE *b = (QUERYTYPE *) DatumGetPointer(PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(1))); \
|
|
|
|
int res = CompareTSQ(a,b); \
|
|
|
|
\
|
|
|
|
PG_FREE_IF_COPY(a,0); \
|
|
|
|
PG_FREE_IF_COPY(b,1); \
|
|
|
|
\
|
|
|
|
PG_RETURN_BOOL( ACTION ); \
|
|
|
|
}
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
CMPFUNC(tsquery_lt, res < 0);
|
|
|
|
CMPFUNC(tsquery_le, res <= 0);
|
|
|
|
CMPFUNC(tsquery_eq, res == 0);
|
|
|
|
CMPFUNC(tsquery_ge, res >= 0);
|
|
|
|
CMPFUNC(tsquery_gt, res > 0);
|
|
|
|
CMPFUNC(tsquery_ne, res != 0);
|