2001-10-13 03:19:09 +04:00
|
|
|
/*
|
|
|
|
* Rewrite routines of query tree
|
|
|
|
* Teodor Sigaev <teodor@stack.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include <float.h>
|
|
|
|
|
|
|
|
#include "access/gist.h"
|
|
|
|
#include "access/itup.h"
|
|
|
|
#include "access/rtree.h"
|
|
|
|
#include "utils/elog.h"
|
|
|
|
#include "utils/palloc.h"
|
|
|
|
#include "utils/array.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "storage/bufpage.h"
|
|
|
|
|
|
|
|
#include "query.h"
|
|
|
|
#include "rewrite.h"
|
|
|
|
|
2001-10-25 09:50:21 +04:00
|
|
|
typedef struct NODE
|
|
|
|
{
|
|
|
|
struct NODE *left;
|
2001-10-13 03:19:09 +04:00
|
|
|
struct NODE *right;
|
2001-10-25 09:50:21 +04:00
|
|
|
ITEM *valnode;
|
|
|
|
} NODE;
|
2001-10-13 03:19:09 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* make query tree from plain view of query
|
|
|
|
*/
|
2001-10-25 09:50:21 +04:00
|
|
|
static NODE *
|
|
|
|
maketree(ITEM * in)
|
|
|
|
{
|
|
|
|
NODE *node = (NODE *) palloc(sizeof(NODE));
|
|
|
|
|
2001-10-13 03:19:09 +04:00
|
|
|
node->valnode = in;
|
|
|
|
node->right = node->left = NULL;
|
2001-10-25 09:50:21 +04:00
|
|
|
if (in->type == OPR)
|
|
|
|
{
|
|
|
|
node->right = maketree(in + 1);
|
|
|
|
if (in->val != (int4) '!')
|
|
|
|
node->left = maketree(in + in->left);
|
2001-10-13 03:19:09 +04:00
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2001-10-25 09:50:21 +04:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
ITEM *ptr;
|
|
|
|
int4 len;
|
|
|
|
int4 cur;
|
|
|
|
} PLAINTREE;
|
2001-10-13 03:19:09 +04:00
|
|
|
|
|
|
|
static void
|
2001-10-25 09:50:21 +04:00
|
|
|
plainnode(PLAINTREE * state, NODE * node)
|
|
|
|
{
|
|
|
|
if (state->cur == state->len)
|
|
|
|
{
|
2001-10-13 03:19:09 +04:00
|
|
|
state->len *= 2;
|
2001-10-25 09:50:21 +04:00
|
|
|
state->ptr = (ITEM *) repalloc((void *) state->ptr, state->len * sizeof(ITEM));
|
2001-10-13 03:19:09 +04:00
|
|
|
}
|
2001-10-25 09:50:21 +04:00
|
|
|
memcpy((void *) &(state->ptr[state->cur]), (void *) node->valnode, sizeof(ITEM));
|
|
|
|
if (node->valnode->type == VAL)
|
2001-10-13 03:19:09 +04:00
|
|
|
state->cur++;
|
2001-10-25 09:50:21 +04:00
|
|
|
else if (node->valnode->val == (int4) '!')
|
|
|
|
{
|
|
|
|
state->ptr[state->cur].left = 1;
|
2001-10-13 03:19:09 +04:00
|
|
|
state->cur++;
|
|
|
|
plainnode(state, node->right);
|
2001-10-25 09:50:21 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int4 cur = state->cur;
|
|
|
|
|
2001-10-13 03:19:09 +04:00
|
|
|
state->cur++;
|
|
|
|
plainnode(state, node->right);
|
2001-10-25 09:50:21 +04:00
|
|
|
state->ptr[cur].left = state->cur - cur;
|
2001-10-13 03:19:09 +04:00
|
|
|
plainnode(state, node->left);
|
|
|
|
}
|
|
|
|
pfree(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make plain view of tree from 'normal' view of tree
|
|
|
|
*/
|
2001-10-25 09:50:21 +04:00
|
|
|
static ITEM *
|
|
|
|
plaintree(NODE * root, int4 *len)
|
|
|
|
{
|
2001-10-13 03:19:09 +04:00
|
|
|
PLAINTREE pl;
|
2001-10-25 09:50:21 +04:00
|
|
|
|
|
|
|
pl.cur = 0;
|
|
|
|
pl.len = 16;
|
|
|
|
if (root && (root->valnode->type == VAL || root->valnode->type == OPR))
|
|
|
|
{
|
|
|
|
pl.ptr = (ITEM *) palloc(pl.len * sizeof(ITEM));
|
2001-10-13 03:19:09 +04:00
|
|
|
plainnode(&pl, root);
|
|
|
|
}
|
2001-10-25 09:50:21 +04:00
|
|
|
else
|
|
|
|
pl.ptr = NULL;
|
2001-10-13 03:19:09 +04:00
|
|
|
*len = pl.cur;
|
2001-10-25 09:50:21 +04:00
|
|
|
return pl.ptr;
|
2001-10-13 03:19:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-10-25 09:50:21 +04:00
|
|
|
freetree(NODE * node)
|
|
|
|
{
|
|
|
|
if (!node)
|
|
|
|
return;
|
|
|
|
if (node->left)
|
|
|
|
freetree(node->left);
|
|
|
|
if (node->right)
|
|
|
|
freetree(node->right);
|
|
|
|
pfree(node);
|
2001-10-13 03:19:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* clean tree for ! operator.
|
2001-10-25 09:50:21 +04:00
|
|
|
* It's usefull for debug, but in
|
2001-10-13 03:19:09 +04:00
|
|
|
* other case, such view is used with search in index.
|
|
|
|
* Operator ! always return TRUE
|
|
|
|
*/
|
2001-10-25 09:50:21 +04:00
|
|
|
static NODE *
|
|
|
|
clean_NOT_intree(NODE * node)
|
|
|
|
{
|
|
|
|
if (node->valnode->type == VAL)
|
2001-10-13 03:19:09 +04:00
|
|
|
return node;
|
|
|
|
|
2001-10-25 09:50:21 +04:00
|
|
|
if (node->valnode->val == (int4) '!')
|
|
|
|
{
|
2001-10-13 03:19:09 +04:00
|
|
|
freetree(node);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* operator & or | */
|
2001-10-25 09:50:21 +04:00
|
|
|
if (node->valnode->val == (int4) '|')
|
|
|
|
{
|
|
|
|
if ((node->left = clean_NOT_intree(node->left)) == NULL ||
|
|
|
|
(node->right = clean_NOT_intree(node->right)) == NULL)
|
|
|
|
{
|
2001-10-13 03:19:09 +04:00
|
|
|
freetree(node);
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-10-25 09:50:21 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NODE *res = node;
|
|
|
|
|
|
|
|
node->left = clean_NOT_intree(node->left);
|
|
|
|
node->right = clean_NOT_intree(node->right);
|
|
|
|
if (node->left == NULL && node->right == NULL)
|
|
|
|
{
|
2001-10-13 03:19:09 +04:00
|
|
|
pfree(node);
|
|
|
|
res = NULL;
|
2001-10-25 09:50:21 +04:00
|
|
|
}
|
|
|
|
else if (node->left == NULL)
|
|
|
|
{
|
2001-10-13 03:19:09 +04:00
|
|
|
res = node->right;
|
|
|
|
pfree(node);
|
2001-10-25 09:50:21 +04:00
|
|
|
}
|
|
|
|
else if (node->right == NULL)
|
|
|
|
{
|
2001-10-13 03:19:09 +04:00
|
|
|
res = node->left;
|
|
|
|
pfree(node);
|
2001-10-25 09:50:21 +04:00
|
|
|
}
|
2001-10-13 03:19:09 +04:00
|
|
|
return res;
|
|
|
|
}
|
2001-10-25 09:50:21 +04:00
|
|
|
return node;
|
2001-10-13 03:19:09 +04:00
|
|
|
}
|
|
|
|
|
2001-10-25 09:50:21 +04:00
|
|
|
ITEM *
|
|
|
|
clean_NOT(ITEM * ptr, int4 *len)
|
|
|
|
{
|
|
|
|
NODE *root = maketree(ptr);
|
|
|
|
|
|
|
|
return plaintree(clean_NOT_intree(root), len);
|
2001-10-13 03:19:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define V_UNKNOWN 0
|
|
|
|
#define V_TRUE 1
|
|
|
|
#define V_FALSE 2
|
|
|
|
|
|
|
|
/*
|
2001-10-25 09:50:21 +04:00
|
|
|
* Clean query tree from values which is always in
|
2001-10-13 03:19:09 +04:00
|
|
|
* text (stopword)
|
2001-10-25 09:50:21 +04:00
|
|
|
*/
|
|
|
|
static NODE *
|
|
|
|
clean_fakeval_intree(NODE * node, char *result)
|
|
|
|
{
|
|
|
|
char lresult = V_UNKNOWN,
|
|
|
|
rresult = V_UNKNOWN;
|
|
|
|
|
|
|
|
if (node->valnode->type == VAL)
|
2001-10-13 03:19:09 +04:00
|
|
|
return node;
|
2001-10-25 09:50:21 +04:00
|
|
|
else if (node->valnode->type == VALTRUE)
|
|
|
|
{
|
|
|
|
pfree(node);
|
2001-10-13 03:19:09 +04:00
|
|
|
*result = V_TRUE;
|
|
|
|
return NULL;
|
2001-10-25 09:50:21 +04:00
|
|
|
}
|
|
|
|
|
2001-10-13 03:19:09 +04:00
|
|
|
|
2001-10-25 09:50:21 +04:00
|
|
|
if (node->valnode->val == (int4) '!')
|
|
|
|
{
|
|
|
|
node->right = clean_fakeval_intree(node->right, &rresult);
|
|
|
|
if (!node->right)
|
|
|
|
{
|
|
|
|
*result = (rresult == V_TRUE) ? V_FALSE : V_TRUE;
|
2001-10-13 03:19:09 +04:00
|
|
|
freetree(node);
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-10-25 09:50:21 +04:00
|
|
|
}
|
|
|
|
else if (node->valnode->val == (int4) '|')
|
|
|
|
{
|
|
|
|
NODE *res = node;
|
|
|
|
|
|
|
|
node->left = clean_fakeval_intree(node->left, &lresult);
|
|
|
|
node->right = clean_fakeval_intree(node->right, &rresult);
|
|
|
|
if (lresult == V_TRUE || rresult == V_TRUE)
|
|
|
|
{
|
2001-10-13 03:19:09 +04:00
|
|
|
freetree(node);
|
2001-10-25 09:50:21 +04:00
|
|
|
*result = V_TRUE;
|
2001-10-13 03:19:09 +04:00
|
|
|
return NULL;
|
2001-10-25 09:50:21 +04:00
|
|
|
}
|
|
|
|
else if (lresult == V_FALSE && rresult == V_FALSE)
|
|
|
|
{
|
2001-10-13 03:19:09 +04:00
|
|
|
freetree(node);
|
2001-10-25 09:50:21 +04:00
|
|
|
*result = V_FALSE;
|
2001-10-13 03:19:09 +04:00
|
|
|
return NULL;
|
2001-10-25 09:50:21 +04:00
|
|
|
}
|
|
|
|
else if (lresult == V_FALSE)
|
|
|
|
{
|
2001-10-13 03:19:09 +04:00
|
|
|
res = node->right;
|
|
|
|
pfree(node);
|
2001-10-25 09:50:21 +04:00
|
|
|
}
|
|
|
|
else if (rresult == V_FALSE)
|
|
|
|
{
|
2001-10-13 03:19:09 +04:00
|
|
|
res = node->left;
|
|
|
|
pfree(node);
|
|
|
|
}
|
2001-10-25 09:50:21 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NODE *res = node;
|
|
|
|
|
|
|
|
node->left = clean_fakeval_intree(node->left, &lresult);
|
|
|
|
node->right = clean_fakeval_intree(node->right, &rresult);
|
|
|
|
if (lresult == V_FALSE || rresult == V_FALSE)
|
|
|
|
{
|
2001-10-13 03:19:09 +04:00
|
|
|
freetree(node);
|
2001-10-25 09:50:21 +04:00
|
|
|
*result = V_FALSE;
|
2001-10-13 03:19:09 +04:00
|
|
|
return NULL;
|
2001-10-25 09:50:21 +04:00
|
|
|
}
|
|
|
|
else if (lresult == V_TRUE && rresult == V_TRUE)
|
|
|
|
{
|
2001-10-13 03:19:09 +04:00
|
|
|
freetree(node);
|
2001-10-25 09:50:21 +04:00
|
|
|
*result = V_TRUE;
|
2001-10-13 03:19:09 +04:00
|
|
|
return NULL;
|
2001-10-25 09:50:21 +04:00
|
|
|
}
|
|
|
|
else if (lresult == V_TRUE)
|
|
|
|
{
|
2001-10-13 03:19:09 +04:00
|
|
|
res = node->right;
|
|
|
|
pfree(node);
|
2001-10-25 09:50:21 +04:00
|
|
|
}
|
|
|
|
else if (rresult == V_TRUE)
|
|
|
|
{
|
2001-10-13 03:19:09 +04:00
|
|
|
res = node->left;
|
|
|
|
pfree(node);
|
|
|
|
}
|
2001-10-25 09:50:21 +04:00
|
|
|
return res;
|
2001-10-13 03:19:09 +04:00
|
|
|
}
|
2001-10-25 09:50:21 +04:00
|
|
|
return node;
|
2001-10-13 03:19:09 +04:00
|
|
|
}
|
|
|
|
|
2001-10-25 09:50:21 +04:00
|
|
|
ITEM *
|
|
|
|
clean_fakeval(ITEM * ptr, int4 *len)
|
|
|
|
{
|
|
|
|
NODE *root = maketree(ptr);
|
|
|
|
char result = V_UNKNOWN;
|
|
|
|
NODE *resroot;
|
|
|
|
|
2001-10-13 03:19:09 +04:00
|
|
|
resroot = clean_fakeval_intree(root, &result);
|
2001-10-25 09:50:21 +04:00
|
|
|
if (result != V_UNKNOWN)
|
|
|
|
{
|
|
|
|
elog(ERROR, "Your query contained only stopword(s), ignored");
|
2001-10-13 03:19:09 +04:00
|
|
|
*len = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-10-25 09:50:21 +04:00
|
|
|
return plaintree(resroot, len);
|
|
|
|
}
|