2008-05-17 03:28:26 +02:00
|
|
|
/*
|
2010-09-20 22:08:53 +02:00
|
|
|
* contrib/intarray/_int_bool.c
|
2008-05-17 03:28:26 +02:00
|
|
|
*/
|
2008-05-12 02:00:54 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2019-10-23 05:56:22 +02:00
|
|
|
#include "_int.h"
|
2011-01-09 06:39:21 +01:00
|
|
|
#include "miscadmin.h"
|
2008-05-12 02:00:54 +02:00
|
|
|
#include "utils/builtins.h"
|
|
|
|
|
2003-06-11 21:31:05 +02:00
|
|
|
PG_FUNCTION_INFO_V1(bqarr_in);
|
|
|
|
PG_FUNCTION_INFO_V1(bqarr_out);
|
|
|
|
PG_FUNCTION_INFO_V1(boolop);
|
|
|
|
PG_FUNCTION_INFO_V1(rboolop);
|
|
|
|
PG_FUNCTION_INFO_V1(querytree);
|
|
|
|
|
|
|
|
|
|
|
|
/* parser's states */
|
|
|
|
#define WAITOPERAND 1
|
|
|
|
#define WAITENDOPERAND 2
|
|
|
|
#define WAITOPERATOR 3
|
|
|
|
|
|
|
|
/*
|
|
|
|
* node of query tree, also used
|
|
|
|
* for storing polish notation in parser
|
|
|
|
*/
|
|
|
|
typedef struct NODE
|
|
|
|
{
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 type;
|
|
|
|
int32 val;
|
2003-06-11 21:31:05 +02:00
|
|
|
struct NODE *next;
|
2007-11-15 23:25:18 +01:00
|
|
|
} NODE;
|
2003-06-11 21:31:05 +02:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char *buf;
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 state;
|
|
|
|
int32 count;
|
2003-06-11 21:31:05 +02:00
|
|
|
/* reverse polish notation in list (for temporary usage) */
|
|
|
|
NODE *str;
|
|
|
|
/* number in str */
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 num;
|
2009-06-11 16:49:15 +02:00
|
|
|
} WORKSTATE;
|
2003-06-11 21:31:05 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* get token from query string
|
|
|
|
*/
|
2012-06-25 00:51:46 +02:00
|
|
|
static int32
|
|
|
|
gettoken(WORKSTATE *state, int32 *val)
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
2011-01-27 23:41:41 +01:00
|
|
|
char nnn[16];
|
|
|
|
int innn;
|
2003-06-11 21:31:05 +02:00
|
|
|
|
2005-09-25 01:07:18 +02:00
|
|
|
*val = 0; /* default result */
|
|
|
|
|
2011-01-27 23:41:41 +01:00
|
|
|
innn = 0;
|
2003-06-11 21:31:05 +02:00
|
|
|
while (1)
|
|
|
|
{
|
2011-01-27 23:41:41 +01:00
|
|
|
if (innn >= sizeof(nnn))
|
|
|
|
return ERR; /* buffer overrun => syntax error */
|
2003-06-11 21:31:05 +02:00
|
|
|
switch (state->state)
|
|
|
|
{
|
|
|
|
case WAITOPERAND:
|
2011-01-27 23:41:41 +01:00
|
|
|
innn = 0;
|
2003-06-11 21:31:05 +02:00
|
|
|
if ((*(state->buf) >= '0' && *(state->buf) <= '9') ||
|
|
|
|
*(state->buf) == '-')
|
|
|
|
{
|
|
|
|
state->state = WAITENDOPERAND;
|
2011-01-27 23:41:41 +01:00
|
|
|
nnn[innn++] = *(state->buf);
|
2003-06-11 21:31:05 +02:00
|
|
|
}
|
|
|
|
else if (*(state->buf) == '!')
|
|
|
|
{
|
|
|
|
(state->buf)++;
|
2012-06-25 00:51:46 +02:00
|
|
|
*val = (int32) '!';
|
2003-06-11 21:31:05 +02:00
|
|
|
return OPR;
|
|
|
|
}
|
|
|
|
else if (*(state->buf) == '(')
|
|
|
|
{
|
|
|
|
state->count++;
|
|
|
|
(state->buf)++;
|
|
|
|
return OPEN;
|
|
|
|
}
|
|
|
|
else if (*(state->buf) != ' ')
|
|
|
|
return ERR;
|
|
|
|
break;
|
|
|
|
case WAITENDOPERAND:
|
|
|
|
if (*(state->buf) >= '0' && *(state->buf) <= '9')
|
|
|
|
{
|
2011-01-27 23:41:41 +01:00
|
|
|
nnn[innn++] = *(state->buf);
|
2003-06-11 21:31:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
long lval;
|
2011-01-27 23:41:41 +01:00
|
|
|
|
|
|
|
nnn[innn] = '\0';
|
|
|
|
errno = 0;
|
|
|
|
lval = strtol(nnn, NULL, 0);
|
2012-06-25 00:51:46 +02:00
|
|
|
*val = (int32) lval;
|
2011-01-27 23:41:41 +01:00
|
|
|
if (errno != 0 || (long) *val != lval)
|
|
|
|
return ERR;
|
2003-06-11 21:31:05 +02:00
|
|
|
state->state = WAITOPERATOR;
|
|
|
|
return (state->count && *(state->buf) == '\0')
|
|
|
|
? ERR : VAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WAITOPERATOR:
|
|
|
|
if (*(state->buf) == '&' || *(state->buf) == '|')
|
|
|
|
{
|
|
|
|
state->state = WAITOPERAND;
|
2012-06-25 00:51:46 +02:00
|
|
|
*val = (int32) *(state->buf);
|
2003-06-11 21:31:05 +02:00
|
|
|
(state->buf)++;
|
|
|
|
return OPR;
|
|
|
|
}
|
|
|
|
else if (*(state->buf) == ')')
|
|
|
|
{
|
|
|
|
(state->buf)++;
|
|
|
|
state->count--;
|
|
|
|
return (state->count < 0) ? ERR : CLOSE;
|
|
|
|
}
|
|
|
|
else if (*(state->buf) == '\0')
|
|
|
|
return (state->count) ? ERR : END;
|
|
|
|
else if (*(state->buf) != ' ')
|
|
|
|
return ERR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return ERR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
(state->buf)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* push new one in polish notation reverse view
|
|
|
|
*/
|
|
|
|
static void
|
2012-06-25 00:51:46 +02:00
|
|
|
pushquery(WORKSTATE *state, int32 type, int32 val)
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
|
|
|
NODE *tmp = (NODE *) palloc(sizeof(NODE));
|
|
|
|
|
|
|
|
tmp->type = type;
|
|
|
|
tmp->val = val;
|
|
|
|
tmp->next = state->str;
|
|
|
|
state->str = tmp;
|
|
|
|
state->num++;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define STACKDEPTH 16
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make polish notation of query
|
|
|
|
*/
|
2012-06-25 00:51:46 +02:00
|
|
|
static int32
|
2009-06-11 16:49:15 +02:00
|
|
|
makepol(WORKSTATE *state)
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 val,
|
2003-06-11 21:31:05 +02:00
|
|
|
type;
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 stack[STACKDEPTH];
|
|
|
|
int32 lenstack = 0;
|
2003-06-11 21:31:05 +02:00
|
|
|
|
2011-01-09 06:39:21 +01:00
|
|
|
/* since this function recurses, it could be driven to stack overflow */
|
|
|
|
check_stack_depth();
|
|
|
|
|
2003-06-11 21:31:05 +02:00
|
|
|
while ((type = gettoken(state, &val)) != END)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case VAL:
|
|
|
|
pushquery(state, type, val);
|
2012-06-25 00:51:46 +02:00
|
|
|
while (lenstack && (stack[lenstack - 1] == (int32) '&' ||
|
|
|
|
stack[lenstack - 1] == (int32) '!'))
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
|
|
|
lenstack--;
|
|
|
|
pushquery(state, OPR, stack[lenstack]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OPR:
|
2012-06-25 00:51:46 +02:00
|
|
|
if (lenstack && val == (int32) '|')
|
2003-06-11 21:31:05 +02:00
|
|
|
pushquery(state, OPR, val);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (lenstack == STACKDEPTH)
|
2003-07-24 19:52:50 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_STATEMENT_TOO_COMPLEX),
|
|
|
|
errmsg("statement too complex")));
|
2003-06-11 21:31:05 +02:00
|
|
|
stack[lenstack] = val;
|
|
|
|
lenstack++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OPEN:
|
|
|
|
if (makepol(state) == ERR)
|
|
|
|
return ERR;
|
2012-06-25 00:51:46 +02:00
|
|
|
while (lenstack && (stack[lenstack - 1] == (int32) '&' ||
|
|
|
|
stack[lenstack - 1] == (int32) '!'))
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
|
|
|
lenstack--;
|
|
|
|
pushquery(state, OPR, stack[lenstack]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CLOSE:
|
|
|
|
while (lenstack)
|
|
|
|
{
|
|
|
|
lenstack--;
|
|
|
|
pushquery(state, OPR, stack[lenstack]);
|
|
|
|
};
|
|
|
|
return END;
|
|
|
|
break;
|
|
|
|
case ERR:
|
|
|
|
default:
|
2003-07-24 19:52:50 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("syntax error")));
|
2003-06-11 21:31:05 +02:00
|
|
|
return ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (lenstack)
|
|
|
|
{
|
|
|
|
lenstack--;
|
|
|
|
pushquery(state, OPR, stack[lenstack]);
|
|
|
|
};
|
|
|
|
return END;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 *arrb;
|
|
|
|
int32 *arre;
|
2007-11-15 23:25:18 +01:00
|
|
|
} CHKVAL;
|
2003-06-11 21:31:05 +02:00
|
|
|
|
|
|
|
/*
|
2011-01-09 06:39:21 +01:00
|
|
|
* is there value 'val' in (sorted) array or not ?
|
2003-06-11 21:31:05 +02:00
|
|
|
*/
|
|
|
|
static bool
|
Implement operator class parameters
PostgreSQL provides set of template index access methods, where opclasses have
much freedom in the semantics of indexing. These index AMs are GiST, GIN,
SP-GiST and BRIN. There opclasses define representation of keys, operations on
them and supported search strategies. So, it's natural that opclasses may be
faced some tradeoffs, which require user-side decision. This commit implements
opclass parameters allowing users to set some values, which tell opclass how to
index the particular dataset.
This commit doesn't introduce new storage in system catalog. Instead it uses
pg_attribute.attoptions, which is used for table column storage options but
unused for index attributes.
In order to evade changing signature of each opclass support function, we
implement unified way to pass options to opclass support functions. Options
are set to fn_expr as the constant bytea expression. It's possible due to the
fact that opclass support functions are executed outside of expressions, so
fn_expr is unused for them.
This commit comes with some examples of opclass options usage. We parametrize
signature length in GiST. That applies to multiple opclasses: tsvector_ops,
gist__intbig_ops, gist_ltree_ops, gist__ltree_ops, gist_trgm_ops and
gist_hstore_ops. Also we parametrize maximum number of integer ranges for
gist__int_ops. However, the main future usage of this feature is expected
to be json, where users would be able to specify which way to index particular
json parts.
Catversion is bumped.
Discussion: https://postgr.es/m/d22c3a18-31c7-1879-fc11-4c1ce2f5e5af%40postgrespro.ru
Author: Nikita Glukhov, revised by me
Reviwed-by: Nikolay Shaplov, Robert Haas, Tom Lane, Tomas Vondra, Alvaro Herrera
2020-03-30 18:17:11 +02:00
|
|
|
checkcondition_arr(void *checkval, ITEM *item, void *options)
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 *StopLow = ((CHKVAL *) checkval)->arrb;
|
|
|
|
int32 *StopHigh = ((CHKVAL *) checkval)->arre;
|
|
|
|
int32 *StopMiddle;
|
2003-06-11 21:31:05 +02:00
|
|
|
|
|
|
|
/* Loop invariant: StopLow <= val < StopHigh */
|
|
|
|
|
|
|
|
while (StopLow < StopHigh)
|
|
|
|
{
|
|
|
|
StopMiddle = StopLow + (StopHigh - StopLow) / 2;
|
2006-05-03 18:31:07 +02:00
|
|
|
if (*StopMiddle == item->val)
|
2017-08-17 18:39:20 +02:00
|
|
|
return true;
|
2006-05-03 18:31:07 +02:00
|
|
|
else if (*StopMiddle < item->val)
|
2003-06-11 21:31:05 +02:00
|
|
|
StopLow = StopMiddle + 1;
|
|
|
|
else
|
|
|
|
StopHigh = StopMiddle;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
Implement operator class parameters
PostgreSQL provides set of template index access methods, where opclasses have
much freedom in the semantics of indexing. These index AMs are GiST, GIN,
SP-GiST and BRIN. There opclasses define representation of keys, operations on
them and supported search strategies. So, it's natural that opclasses may be
faced some tradeoffs, which require user-side decision. This commit implements
opclass parameters allowing users to set some values, which tell opclass how to
index the particular dataset.
This commit doesn't introduce new storage in system catalog. Instead it uses
pg_attribute.attoptions, which is used for table column storage options but
unused for index attributes.
In order to evade changing signature of each opclass support function, we
implement unified way to pass options to opclass support functions. Options
are set to fn_expr as the constant bytea expression. It's possible due to the
fact that opclass support functions are executed outside of expressions, so
fn_expr is unused for them.
This commit comes with some examples of opclass options usage. We parametrize
signature length in GiST. That applies to multiple opclasses: tsvector_ops,
gist__intbig_ops, gist_ltree_ops, gist__ltree_ops, gist_trgm_ops and
gist_hstore_ops. Also we parametrize maximum number of integer ranges for
gist__int_ops. However, the main future usage of this feature is expected
to be json, where users would be able to specify which way to index particular
json parts.
Catversion is bumped.
Discussion: https://postgr.es/m/d22c3a18-31c7-1879-fc11-4c1ce2f5e5af%40postgrespro.ru
Author: Nikita Glukhov, revised by me
Reviwed-by: Nikolay Shaplov, Robert Haas, Tom Lane, Tomas Vondra, Alvaro Herrera
2020-03-30 18:17:11 +02:00
|
|
|
checkcondition_bit(void *checkval, ITEM *item, void *siglen)
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
2020-05-14 19:06:38 +02:00
|
|
|
return GETBIT(checkval, HASHVAL(item->val, (int) (intptr_t) siglen));
|
2003-06-11 21:31:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-01-09 06:39:21 +01:00
|
|
|
* evaluate boolean expression, using chkcond() to test the primitive cases
|
2003-06-11 21:31:05 +02:00
|
|
|
*/
|
|
|
|
static bool
|
Implement operator class parameters
PostgreSQL provides set of template index access methods, where opclasses have
much freedom in the semantics of indexing. These index AMs are GiST, GIN,
SP-GiST and BRIN. There opclasses define representation of keys, operations on
them and supported search strategies. So, it's natural that opclasses may be
faced some tradeoffs, which require user-side decision. This commit implements
opclass parameters allowing users to set some values, which tell opclass how to
index the particular dataset.
This commit doesn't introduce new storage in system catalog. Instead it uses
pg_attribute.attoptions, which is used for table column storage options but
unused for index attributes.
In order to evade changing signature of each opclass support function, we
implement unified way to pass options to opclass support functions. Options
are set to fn_expr as the constant bytea expression. It's possible due to the
fact that opclass support functions are executed outside of expressions, so
fn_expr is unused for them.
This commit comes with some examples of opclass options usage. We parametrize
signature length in GiST. That applies to multiple opclasses: tsvector_ops,
gist__intbig_ops, gist_ltree_ops, gist__ltree_ops, gist_trgm_ops and
gist_hstore_ops. Also we parametrize maximum number of integer ranges for
gist__int_ops. However, the main future usage of this feature is expected
to be json, where users would be able to specify which way to index particular
json parts.
Catversion is bumped.
Discussion: https://postgr.es/m/d22c3a18-31c7-1879-fc11-4c1ce2f5e5af%40postgrespro.ru
Author: Nikita Glukhov, revised by me
Reviwed-by: Nikolay Shaplov, Robert Haas, Tom Lane, Tomas Vondra, Alvaro Herrera
2020-03-30 18:17:11 +02:00
|
|
|
execute(ITEM *curitem, void *checkval, void *options, bool calcnot,
|
|
|
|
bool (*chkcond) (void *checkval, ITEM *item, void *options))
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
2011-01-09 06:39:21 +01:00
|
|
|
/* since this function recurses, it could be driven to stack overflow */
|
|
|
|
check_stack_depth();
|
2003-06-11 21:31:05 +02:00
|
|
|
|
|
|
|
if (curitem->type == VAL)
|
Implement operator class parameters
PostgreSQL provides set of template index access methods, where opclasses have
much freedom in the semantics of indexing. These index AMs are GiST, GIN,
SP-GiST and BRIN. There opclasses define representation of keys, operations on
them and supported search strategies. So, it's natural that opclasses may be
faced some tradeoffs, which require user-side decision. This commit implements
opclass parameters allowing users to set some values, which tell opclass how to
index the particular dataset.
This commit doesn't introduce new storage in system catalog. Instead it uses
pg_attribute.attoptions, which is used for table column storage options but
unused for index attributes.
In order to evade changing signature of each opclass support function, we
implement unified way to pass options to opclass support functions. Options
are set to fn_expr as the constant bytea expression. It's possible due to the
fact that opclass support functions are executed outside of expressions, so
fn_expr is unused for them.
This commit comes with some examples of opclass options usage. We parametrize
signature length in GiST. That applies to multiple opclasses: tsvector_ops,
gist__intbig_ops, gist_ltree_ops, gist__ltree_ops, gist_trgm_ops and
gist_hstore_ops. Also we parametrize maximum number of integer ranges for
gist__int_ops. However, the main future usage of this feature is expected
to be json, where users would be able to specify which way to index particular
json parts.
Catversion is bumped.
Discussion: https://postgr.es/m/d22c3a18-31c7-1879-fc11-4c1ce2f5e5af%40postgrespro.ru
Author: Nikita Glukhov, revised by me
Reviwed-by: Nikolay Shaplov, Robert Haas, Tom Lane, Tomas Vondra, Alvaro Herrera
2020-03-30 18:17:11 +02:00
|
|
|
return (*chkcond) (checkval, curitem, options);
|
2012-06-25 00:51:46 +02:00
|
|
|
else if (curitem->val == (int32) '!')
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
2017-08-17 18:39:20 +02:00
|
|
|
return calcnot ?
|
Implement operator class parameters
PostgreSQL provides set of template index access methods, where opclasses have
much freedom in the semantics of indexing. These index AMs are GiST, GIN,
SP-GiST and BRIN. There opclasses define representation of keys, operations on
them and supported search strategies. So, it's natural that opclasses may be
faced some tradeoffs, which require user-side decision. This commit implements
opclass parameters allowing users to set some values, which tell opclass how to
index the particular dataset.
This commit doesn't introduce new storage in system catalog. Instead it uses
pg_attribute.attoptions, which is used for table column storage options but
unused for index attributes.
In order to evade changing signature of each opclass support function, we
implement unified way to pass options to opclass support functions. Options
are set to fn_expr as the constant bytea expression. It's possible due to the
fact that opclass support functions are executed outside of expressions, so
fn_expr is unused for them.
This commit comes with some examples of opclass options usage. We parametrize
signature length in GiST. That applies to multiple opclasses: tsvector_ops,
gist__intbig_ops, gist_ltree_ops, gist__ltree_ops, gist_trgm_ops and
gist_hstore_ops. Also we parametrize maximum number of integer ranges for
gist__int_ops. However, the main future usage of this feature is expected
to be json, where users would be able to specify which way to index particular
json parts.
Catversion is bumped.
Discussion: https://postgr.es/m/d22c3a18-31c7-1879-fc11-4c1ce2f5e5af%40postgrespro.ru
Author: Nikita Glukhov, revised by me
Reviwed-by: Nikolay Shaplov, Robert Haas, Tom Lane, Tomas Vondra, Alvaro Herrera
2020-03-30 18:17:11 +02:00
|
|
|
((execute(curitem - 1, checkval, options, calcnot, chkcond)) ? false : true)
|
2003-06-11 21:31:05 +02:00
|
|
|
: true;
|
|
|
|
}
|
2012-06-25 00:51:46 +02:00
|
|
|
else if (curitem->val == (int32) '&')
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
Implement operator class parameters
PostgreSQL provides set of template index access methods, where opclasses have
much freedom in the semantics of indexing. These index AMs are GiST, GIN,
SP-GiST and BRIN. There opclasses define representation of keys, operations on
them and supported search strategies. So, it's natural that opclasses may be
faced some tradeoffs, which require user-side decision. This commit implements
opclass parameters allowing users to set some values, which tell opclass how to
index the particular dataset.
This commit doesn't introduce new storage in system catalog. Instead it uses
pg_attribute.attoptions, which is used for table column storage options but
unused for index attributes.
In order to evade changing signature of each opclass support function, we
implement unified way to pass options to opclass support functions. Options
are set to fn_expr as the constant bytea expression. It's possible due to the
fact that opclass support functions are executed outside of expressions, so
fn_expr is unused for them.
This commit comes with some examples of opclass options usage. We parametrize
signature length in GiST. That applies to multiple opclasses: tsvector_ops,
gist__intbig_ops, gist_ltree_ops, gist__ltree_ops, gist_trgm_ops and
gist_hstore_ops. Also we parametrize maximum number of integer ranges for
gist__int_ops. However, the main future usage of this feature is expected
to be json, where users would be able to specify which way to index particular
json parts.
Catversion is bumped.
Discussion: https://postgr.es/m/d22c3a18-31c7-1879-fc11-4c1ce2f5e5af%40postgrespro.ru
Author: Nikita Glukhov, revised by me
Reviwed-by: Nikolay Shaplov, Robert Haas, Tom Lane, Tomas Vondra, Alvaro Herrera
2020-03-30 18:17:11 +02:00
|
|
|
if (execute(curitem + curitem->left, checkval, options, calcnot, chkcond))
|
|
|
|
return execute(curitem - 1, checkval, options, calcnot, chkcond);
|
2003-06-11 21:31:05 +02:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* |-operator */
|
Implement operator class parameters
PostgreSQL provides set of template index access methods, where opclasses have
much freedom in the semantics of indexing. These index AMs are GiST, GIN,
SP-GiST and BRIN. There opclasses define representation of keys, operations on
them and supported search strategies. So, it's natural that opclasses may be
faced some tradeoffs, which require user-side decision. This commit implements
opclass parameters allowing users to set some values, which tell opclass how to
index the particular dataset.
This commit doesn't introduce new storage in system catalog. Instead it uses
pg_attribute.attoptions, which is used for table column storage options but
unused for index attributes.
In order to evade changing signature of each opclass support function, we
implement unified way to pass options to opclass support functions. Options
are set to fn_expr as the constant bytea expression. It's possible due to the
fact that opclass support functions are executed outside of expressions, so
fn_expr is unused for them.
This commit comes with some examples of opclass options usage. We parametrize
signature length in GiST. That applies to multiple opclasses: tsvector_ops,
gist__intbig_ops, gist_ltree_ops, gist__ltree_ops, gist_trgm_ops and
gist_hstore_ops. Also we parametrize maximum number of integer ranges for
gist__int_ops. However, the main future usage of this feature is expected
to be json, where users would be able to specify which way to index particular
json parts.
Catversion is bumped.
Discussion: https://postgr.es/m/d22c3a18-31c7-1879-fc11-4c1ce2f5e5af%40postgrespro.ru
Author: Nikita Glukhov, revised by me
Reviwed-by: Nikolay Shaplov, Robert Haas, Tom Lane, Tomas Vondra, Alvaro Herrera
2020-03-30 18:17:11 +02:00
|
|
|
if (execute(curitem + curitem->left, checkval, options, calcnot, chkcond))
|
2003-06-11 21:31:05 +02:00
|
|
|
return true;
|
|
|
|
else
|
Implement operator class parameters
PostgreSQL provides set of template index access methods, where opclasses have
much freedom in the semantics of indexing. These index AMs are GiST, GIN,
SP-GiST and BRIN. There opclasses define representation of keys, operations on
them and supported search strategies. So, it's natural that opclasses may be
faced some tradeoffs, which require user-side decision. This commit implements
opclass parameters allowing users to set some values, which tell opclass how to
index the particular dataset.
This commit doesn't introduce new storage in system catalog. Instead it uses
pg_attribute.attoptions, which is used for table column storage options but
unused for index attributes.
In order to evade changing signature of each opclass support function, we
implement unified way to pass options to opclass support functions. Options
are set to fn_expr as the constant bytea expression. It's possible due to the
fact that opclass support functions are executed outside of expressions, so
fn_expr is unused for them.
This commit comes with some examples of opclass options usage. We parametrize
signature length in GiST. That applies to multiple opclasses: tsvector_ops,
gist__intbig_ops, gist_ltree_ops, gist__ltree_ops, gist_trgm_ops and
gist_hstore_ops. Also we parametrize maximum number of integer ranges for
gist__int_ops. However, the main future usage of this feature is expected
to be json, where users would be able to specify which way to index particular
json parts.
Catversion is bumped.
Discussion: https://postgr.es/m/d22c3a18-31c7-1879-fc11-4c1ce2f5e5af%40postgrespro.ru
Author: Nikita Glukhov, revised by me
Reviwed-by: Nikolay Shaplov, Robert Haas, Tom Lane, Tomas Vondra, Alvaro Herrera
2020-03-30 18:17:11 +02:00
|
|
|
return execute(curitem - 1, checkval, options, calcnot, chkcond);
|
2003-06-11 21:31:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* signconsistent & execconsistent called by *_consistent
|
|
|
|
*/
|
|
|
|
bool
|
Implement operator class parameters
PostgreSQL provides set of template index access methods, where opclasses have
much freedom in the semantics of indexing. These index AMs are GiST, GIN,
SP-GiST and BRIN. There opclasses define representation of keys, operations on
them and supported search strategies. So, it's natural that opclasses may be
faced some tradeoffs, which require user-side decision. This commit implements
opclass parameters allowing users to set some values, which tell opclass how to
index the particular dataset.
This commit doesn't introduce new storage in system catalog. Instead it uses
pg_attribute.attoptions, which is used for table column storage options but
unused for index attributes.
In order to evade changing signature of each opclass support function, we
implement unified way to pass options to opclass support functions. Options
are set to fn_expr as the constant bytea expression. It's possible due to the
fact that opclass support functions are executed outside of expressions, so
fn_expr is unused for them.
This commit comes with some examples of opclass options usage. We parametrize
signature length in GiST. That applies to multiple opclasses: tsvector_ops,
gist__intbig_ops, gist_ltree_ops, gist__ltree_ops, gist_trgm_ops and
gist_hstore_ops. Also we parametrize maximum number of integer ranges for
gist__int_ops. However, the main future usage of this feature is expected
to be json, where users would be able to specify which way to index particular
json parts.
Catversion is bumped.
Discussion: https://postgr.es/m/d22c3a18-31c7-1879-fc11-4c1ce2f5e5af%40postgrespro.ru
Author: Nikita Glukhov, revised by me
Reviwed-by: Nikolay Shaplov, Robert Haas, Tom Lane, Tomas Vondra, Alvaro Herrera
2020-03-30 18:17:11 +02:00
|
|
|
signconsistent(QUERYTYPE *query, BITVECP sign, int siglen, bool calcnot)
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
2011-01-09 06:39:21 +01:00
|
|
|
return execute(GETQUERY(query) + query->size - 1,
|
2020-05-14 19:06:38 +02:00
|
|
|
(void *) sign, (void *) (intptr_t) siglen, calcnot,
|
2011-01-09 06:39:21 +01:00
|
|
|
checkcondition_bit);
|
2003-06-11 21:31:05 +02:00
|
|
|
}
|
|
|
|
|
2011-01-09 06:39:21 +01:00
|
|
|
/* Array must be sorted! */
|
2003-06-11 21:31:05 +02:00
|
|
|
bool
|
2009-06-11 16:49:15 +02:00
|
|
|
execconsistent(QUERYTYPE *query, ArrayType *array, bool calcnot)
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
|
|
|
CHKVAL chkval;
|
|
|
|
|
2005-11-19 04:00:09 +01:00
|
|
|
CHECKARRVALID(array);
|
2003-06-11 21:31:05 +02:00
|
|
|
chkval.arrb = ARRPTR(array);
|
|
|
|
chkval.arre = chkval.arrb + ARRNELEMS(array);
|
2011-01-09 06:39:21 +01:00
|
|
|
return execute(GETQUERY(query) + query->size - 1,
|
Implement operator class parameters
PostgreSQL provides set of template index access methods, where opclasses have
much freedom in the semantics of indexing. These index AMs are GiST, GIN,
SP-GiST and BRIN. There opclasses define representation of keys, operations on
them and supported search strategies. So, it's natural that opclasses may be
faced some tradeoffs, which require user-side decision. This commit implements
opclass parameters allowing users to set some values, which tell opclass how to
index the particular dataset.
This commit doesn't introduce new storage in system catalog. Instead it uses
pg_attribute.attoptions, which is used for table column storage options but
unused for index attributes.
In order to evade changing signature of each opclass support function, we
implement unified way to pass options to opclass support functions. Options
are set to fn_expr as the constant bytea expression. It's possible due to the
fact that opclass support functions are executed outside of expressions, so
fn_expr is unused for them.
This commit comes with some examples of opclass options usage. We parametrize
signature length in GiST. That applies to multiple opclasses: tsvector_ops,
gist__intbig_ops, gist_ltree_ops, gist__ltree_ops, gist_trgm_ops and
gist_hstore_ops. Also we parametrize maximum number of integer ranges for
gist__int_ops. However, the main future usage of this feature is expected
to be json, where users would be able to specify which way to index particular
json parts.
Catversion is bumped.
Discussion: https://postgr.es/m/d22c3a18-31c7-1879-fc11-4c1ce2f5e5af%40postgrespro.ru
Author: Nikita Glukhov, revised by me
Reviwed-by: Nikolay Shaplov, Robert Haas, Tom Lane, Tomas Vondra, Alvaro Herrera
2020-03-30 18:17:11 +02:00
|
|
|
(void *) &chkval, NULL, calcnot,
|
2011-01-09 06:39:21 +01:00
|
|
|
checkcondition_arr);
|
2003-06-11 21:31:05 +02:00
|
|
|
}
|
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
ITEM *first;
|
|
|
|
bool *mapped_check;
|
2007-11-15 23:25:18 +01:00
|
|
|
} GinChkVal;
|
2006-05-03 18:31:07 +02:00
|
|
|
|
|
|
|
static bool
|
Implement operator class parameters
PostgreSQL provides set of template index access methods, where opclasses have
much freedom in the semantics of indexing. These index AMs are GiST, GIN,
SP-GiST and BRIN. There opclasses define representation of keys, operations on
them and supported search strategies. So, it's natural that opclasses may be
faced some tradeoffs, which require user-side decision. This commit implements
opclass parameters allowing users to set some values, which tell opclass how to
index the particular dataset.
This commit doesn't introduce new storage in system catalog. Instead it uses
pg_attribute.attoptions, which is used for table column storage options but
unused for index attributes.
In order to evade changing signature of each opclass support function, we
implement unified way to pass options to opclass support functions. Options
are set to fn_expr as the constant bytea expression. It's possible due to the
fact that opclass support functions are executed outside of expressions, so
fn_expr is unused for them.
This commit comes with some examples of opclass options usage. We parametrize
signature length in GiST. That applies to multiple opclasses: tsvector_ops,
gist__intbig_ops, gist_ltree_ops, gist__ltree_ops, gist_trgm_ops and
gist_hstore_ops. Also we parametrize maximum number of integer ranges for
gist__int_ops. However, the main future usage of this feature is expected
to be json, where users would be able to specify which way to index particular
json parts.
Catversion is bumped.
Discussion: https://postgr.es/m/d22c3a18-31c7-1879-fc11-4c1ce2f5e5af%40postgrespro.ru
Author: Nikita Glukhov, revised by me
Reviwed-by: Nikolay Shaplov, Robert Haas, Tom Lane, Tomas Vondra, Alvaro Herrera
2020-03-30 18:17:11 +02:00
|
|
|
checkcondition_gin(void *checkval, ITEM *item, void *options)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
|
|
|
GinChkVal *gcv = (GinChkVal *) checkval;
|
2006-05-03 18:31:07 +02:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
return gcv->mapped_check[item - gcv->first];
|
2006-05-03 18:31:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2011-01-09 06:39:21 +01:00
|
|
|
gin_bool_consistent(QUERYTYPE *query, bool *check)
|
2006-10-04 02:30:14 +02:00
|
|
|
{
|
|
|
|
GinChkVal gcv;
|
|
|
|
ITEM *items = GETQUERY(query);
|
|
|
|
int i,
|
|
|
|
j = 0;
|
2006-05-03 18:31:07 +02:00
|
|
|
|
2011-01-09 06:39:21 +01:00
|
|
|
if (query->size <= 0)
|
2017-08-16 06:22:32 +02:00
|
|
|
return false;
|
2006-05-03 18:31:07 +02:00
|
|
|
|
2011-01-09 06:39:21 +01:00
|
|
|
/*
|
2014-05-06 18:12:18 +02:00
|
|
|
* Set up data for checkcondition_gin. This must agree with the query
|
2011-04-10 17:42:00 +02:00
|
|
|
* extraction code in ginint4_queryextract.
|
2011-01-09 06:39:21 +01:00
|
|
|
*/
|
2006-05-03 18:31:07 +02:00
|
|
|
gcv.first = items;
|
2006-10-04 02:30:14 +02:00
|
|
|
gcv.mapped_check = (bool *) palloc(sizeof(bool) * query->size);
|
|
|
|
for (i = 0; i < query->size; i++)
|
2011-01-09 06:39:21 +01:00
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
if (items[i].type == VAL)
|
|
|
|
gcv.mapped_check[i] = check[j++];
|
2011-01-09 06:39:21 +01:00
|
|
|
}
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2011-01-09 06:39:21 +01:00
|
|
|
return execute(GETQUERY(query) + query->size - 1,
|
Implement operator class parameters
PostgreSQL provides set of template index access methods, where opclasses have
much freedom in the semantics of indexing. These index AMs are GiST, GIN,
SP-GiST and BRIN. There opclasses define representation of keys, operations on
them and supported search strategies. So, it's natural that opclasses may be
faced some tradeoffs, which require user-side decision. This commit implements
opclass parameters allowing users to set some values, which tell opclass how to
index the particular dataset.
This commit doesn't introduce new storage in system catalog. Instead it uses
pg_attribute.attoptions, which is used for table column storage options but
unused for index attributes.
In order to evade changing signature of each opclass support function, we
implement unified way to pass options to opclass support functions. Options
are set to fn_expr as the constant bytea expression. It's possible due to the
fact that opclass support functions are executed outside of expressions, so
fn_expr is unused for them.
This commit comes with some examples of opclass options usage. We parametrize
signature length in GiST. That applies to multiple opclasses: tsvector_ops,
gist__intbig_ops, gist_ltree_ops, gist__ltree_ops, gist_trgm_ops and
gist_hstore_ops. Also we parametrize maximum number of integer ranges for
gist__int_ops. However, the main future usage of this feature is expected
to be json, where users would be able to specify which way to index particular
json parts.
Catversion is bumped.
Discussion: https://postgr.es/m/d22c3a18-31c7-1879-fc11-4c1ce2f5e5af%40postgrespro.ru
Author: Nikita Glukhov, revised by me
Reviwed-by: Nikolay Shaplov, Robert Haas, Tom Lane, Tomas Vondra, Alvaro Herrera
2020-03-30 18:17:11 +02:00
|
|
|
(void *) &gcv, NULL, true,
|
2011-01-09 06:39:21 +01:00
|
|
|
checkcondition_gin);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
contains_required_value(ITEM *curitem)
|
|
|
|
{
|
|
|
|
/* since this function recurses, it could be driven to stack overflow */
|
|
|
|
check_stack_depth();
|
|
|
|
|
|
|
|
if (curitem->type == VAL)
|
|
|
|
return true;
|
2012-06-25 00:51:46 +02:00
|
|
|
else if (curitem->val == (int32) '!')
|
2011-01-09 06:39:21 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Assume anything under a NOT is non-required. For some cases with
|
|
|
|
* nested NOTs, we could prove there's a required value, but it seems
|
|
|
|
* unlikely to be worth the trouble.
|
|
|
|
*/
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-25 00:51:46 +02:00
|
|
|
else if (curitem->val == (int32) '&')
|
2011-01-09 06:39:21 +01:00
|
|
|
{
|
|
|
|
/* If either side has a required value, we're good */
|
|
|
|
if (contains_required_value(curitem + curitem->left))
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return contains_required_value(curitem - 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* |-operator */
|
|
|
|
/* Both sides must have required values */
|
|
|
|
if (contains_required_value(curitem + curitem->left))
|
|
|
|
return contains_required_value(curitem - 1);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
query_has_required_values(QUERYTYPE *query)
|
|
|
|
{
|
|
|
|
if (query->size <= 0)
|
|
|
|
return false;
|
|
|
|
return contains_required_value(GETQUERY(query) + query->size - 1);
|
2006-05-03 18:31:07 +02:00
|
|
|
}
|
|
|
|
|
2003-06-11 21:31:05 +02:00
|
|
|
/*
|
|
|
|
* boolean operations
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
rboolop(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2011-01-09 06:39:21 +01:00
|
|
|
/* just reverse the operands */
|
|
|
|
return DirectFunctionCall2(boolop,
|
2003-06-11 21:31:05 +02:00
|
|
|
PG_GETARG_DATUM(1),
|
2011-01-09 06:39:21 +01:00
|
|
|
PG_GETARG_DATUM(0));
|
2003-06-11 21:31:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
|
|
|
boolop(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2011-01-09 06:39:21 +01:00
|
|
|
ArrayType *val = PG_GETARG_ARRAYTYPE_P_COPY(0);
|
|
|
|
QUERYTYPE *query = PG_GETARG_QUERYTYPE_P(1);
|
2003-06-11 21:31:05 +02:00
|
|
|
CHKVAL chkval;
|
|
|
|
bool result;
|
|
|
|
|
2005-11-19 04:00:09 +01:00
|
|
|
CHECKARRVALID(val);
|
2003-06-11 21:31:05 +02:00
|
|
|
PREPAREARR(val);
|
|
|
|
chkval.arrb = ARRPTR(val);
|
|
|
|
chkval.arre = chkval.arrb + ARRNELEMS(val);
|
2011-01-09 06:39:21 +01:00
|
|
|
result = execute(GETQUERY(query) + query->size - 1,
|
Implement operator class parameters
PostgreSQL provides set of template index access methods, where opclasses have
much freedom in the semantics of indexing. These index AMs are GiST, GIN,
SP-GiST and BRIN. There opclasses define representation of keys, operations on
them and supported search strategies. So, it's natural that opclasses may be
faced some tradeoffs, which require user-side decision. This commit implements
opclass parameters allowing users to set some values, which tell opclass how to
index the particular dataset.
This commit doesn't introduce new storage in system catalog. Instead it uses
pg_attribute.attoptions, which is used for table column storage options but
unused for index attributes.
In order to evade changing signature of each opclass support function, we
implement unified way to pass options to opclass support functions. Options
are set to fn_expr as the constant bytea expression. It's possible due to the
fact that opclass support functions are executed outside of expressions, so
fn_expr is unused for them.
This commit comes with some examples of opclass options usage. We parametrize
signature length in GiST. That applies to multiple opclasses: tsvector_ops,
gist__intbig_ops, gist_ltree_ops, gist__ltree_ops, gist_trgm_ops and
gist_hstore_ops. Also we parametrize maximum number of integer ranges for
gist__int_ops. However, the main future usage of this feature is expected
to be json, where users would be able to specify which way to index particular
json parts.
Catversion is bumped.
Discussion: https://postgr.es/m/d22c3a18-31c7-1879-fc11-4c1ce2f5e5af%40postgrespro.ru
Author: Nikita Glukhov, revised by me
Reviwed-by: Nikolay Shaplov, Robert Haas, Tom Lane, Tomas Vondra, Alvaro Herrera
2020-03-30 18:17:11 +02:00
|
|
|
&chkval, NULL, true,
|
2011-01-09 06:39:21 +01:00
|
|
|
checkcondition_arr);
|
2003-06-11 21:31:05 +02:00
|
|
|
pfree(val);
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
|
|
|
PG_RETURN_BOOL(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-06-25 00:51:46 +02:00
|
|
|
findoprnd(ITEM *ptr, int32 *pos)
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
2014-02-17 15:33:31 +01:00
|
|
|
/* since this function recurses, it could be driven to stack overflow. */
|
|
|
|
check_stack_depth();
|
|
|
|
|
2003-06-11 21:31:05 +02:00
|
|
|
#ifdef BS_DEBUG
|
|
|
|
elog(DEBUG3, (ptr[*pos].type == OPR) ?
|
2003-07-24 19:52:50 +02:00
|
|
|
"%d %c" : "%d %d", *pos, ptr[*pos].val);
|
2003-06-11 21:31:05 +02:00
|
|
|
#endif
|
|
|
|
if (ptr[*pos].type == VAL)
|
|
|
|
{
|
|
|
|
ptr[*pos].left = 0;
|
|
|
|
(*pos)--;
|
|
|
|
}
|
2012-06-25 00:51:46 +02:00
|
|
|
else if (ptr[*pos].val == (int32) '!')
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
|
|
|
ptr[*pos].left = -1;
|
|
|
|
(*pos)--;
|
|
|
|
findoprnd(ptr, pos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ITEM *curitem = &ptr[*pos];
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 tmp = *pos;
|
2003-06-11 21:31:05 +02:00
|
|
|
|
|
|
|
(*pos)--;
|
|
|
|
findoprnd(ptr, pos);
|
|
|
|
curitem->left = *pos - tmp;
|
|
|
|
findoprnd(ptr, pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* input
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
bqarr_in(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
char *buf = (char *) PG_GETARG_POINTER(0);
|
|
|
|
WORKSTATE state;
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 i;
|
2003-06-11 21:31:05 +02:00
|
|
|
QUERYTYPE *query;
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 commonlen;
|
2003-06-11 21:31:05 +02:00
|
|
|
ITEM *ptr;
|
|
|
|
NODE *tmp;
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 pos = 0;
|
2003-06-11 21:31:05 +02:00
|
|
|
|
|
|
|
#ifdef BS_DEBUG
|
|
|
|
StringInfoData pbuf;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
state.buf = buf;
|
|
|
|
state.state = WAITOPERAND;
|
|
|
|
state.count = 0;
|
|
|
|
state.num = 0;
|
|
|
|
state.str = NULL;
|
|
|
|
|
|
|
|
/* make polish notation (postfix, but in reverse order) */
|
|
|
|
makepol(&state);
|
|
|
|
if (!state.num)
|
2003-07-24 19:52:50 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("empty query")));
|
2003-06-11 21:31:05 +02:00
|
|
|
|
2014-02-17 15:33:31 +01:00
|
|
|
if (state.num > QUERYTYPEMAXITEMS)
|
|
|
|
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 query items (%d) exceeds the maximum allowed (%d)",
|
|
|
|
state.num, (int) QUERYTYPEMAXITEMS)));
|
2003-06-11 21:31:05 +02:00
|
|
|
commonlen = COMPUTESIZE(state.num);
|
2014-02-17 15:33:31 +01:00
|
|
|
|
2003-06-11 21:31:05 +02:00
|
|
|
query = (QUERYTYPE *) palloc(commonlen);
|
2007-02-28 23:44:38 +01:00
|
|
|
SET_VARSIZE(query, commonlen);
|
2003-06-11 21:31:05 +02:00
|
|
|
query->size = state.num;
|
|
|
|
ptr = GETQUERY(query);
|
|
|
|
|
|
|
|
for (i = state.num - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
ptr[i].type = state.str->type;
|
|
|
|
ptr[i].val = state.str->val;
|
|
|
|
tmp = state.str->next;
|
|
|
|
pfree(state.str);
|
|
|
|
state.str = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = query->size - 1;
|
|
|
|
findoprnd(ptr, &pos);
|
|
|
|
#ifdef BS_DEBUG
|
|
|
|
initStringInfo(&pbuf);
|
|
|
|
for (i = 0; i < query->size; i++)
|
|
|
|
{
|
|
|
|
if (ptr[i].type == OPR)
|
|
|
|
appendStringInfo(&pbuf, "%c(%d) ", ptr[i].val, ptr[i].left);
|
|
|
|
else
|
|
|
|
appendStringInfo(&pbuf, "%d ", ptr[i].val);
|
|
|
|
}
|
|
|
|
elog(DEBUG3, "POR: %s", pbuf.data);
|
|
|
|
pfree(pbuf.data);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
PG_RETURN_POINTER(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* out function
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
ITEM *curpol;
|
|
|
|
char *buf;
|
|
|
|
char *cur;
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 buflen;
|
2007-11-15 23:25:18 +01:00
|
|
|
} INFIX;
|
2003-06-11 21:31:05 +02:00
|
|
|
|
2005-05-25 23:40:43 +02:00
|
|
|
#define RESIZEBUF(inf,addsize) while( ( (inf)->cur - (inf)->buf ) + (addsize) + 1 >= (inf)->buflen ) { \
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 len = inf->cur - inf->buf; \
|
2003-06-11 21:31:05 +02:00
|
|
|
inf->buflen *= 2; \
|
|
|
|
inf->buf = (char*) repalloc( (void*)inf->buf, inf->buflen ); \
|
|
|
|
inf->cur = inf->buf + len; \
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-11-15 23:25:18 +01:00
|
|
|
infix(INFIX *in, bool first)
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
2015-10-05 16:06:30 +02:00
|
|
|
/* since this function recurses, it could be driven to stack overflow. */
|
|
|
|
check_stack_depth();
|
|
|
|
|
2003-06-11 21:31:05 +02:00
|
|
|
if (in->curpol->type == VAL)
|
|
|
|
{
|
|
|
|
RESIZEBUF(in, 11);
|
|
|
|
sprintf(in->cur, "%d", in->curpol->val);
|
|
|
|
in->cur = strchr(in->cur, '\0');
|
|
|
|
in->curpol--;
|
|
|
|
}
|
2012-06-25 00:51:46 +02:00
|
|
|
else if (in->curpol->val == (int32) '!')
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
|
|
|
bool isopr = false;
|
|
|
|
|
|
|
|
RESIZEBUF(in, 1);
|
|
|
|
*(in->cur) = '!';
|
|
|
|
in->cur++;
|
|
|
|
*(in->cur) = '\0';
|
|
|
|
in->curpol--;
|
|
|
|
if (in->curpol->type == OPR)
|
|
|
|
{
|
|
|
|
isopr = true;
|
|
|
|
RESIZEBUF(in, 2);
|
|
|
|
sprintf(in->cur, "( ");
|
|
|
|
in->cur = strchr(in->cur, '\0');
|
|
|
|
}
|
|
|
|
infix(in, isopr);
|
|
|
|
if (isopr)
|
|
|
|
{
|
|
|
|
RESIZEBUF(in, 2);
|
|
|
|
sprintf(in->cur, " )");
|
|
|
|
in->cur = strchr(in->cur, '\0');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-25 00:51:46 +02:00
|
|
|
int32 op = in->curpol->val;
|
2003-06-11 21:31:05 +02:00
|
|
|
INFIX nrm;
|
|
|
|
|
|
|
|
in->curpol--;
|
2012-06-25 00:51:46 +02:00
|
|
|
if (op == (int32) '|' && !first)
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
|
|
|
RESIZEBUF(in, 2);
|
|
|
|
sprintf(in->cur, "( ");
|
|
|
|
in->cur = strchr(in->cur, '\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
nrm.curpol = in->curpol;
|
|
|
|
nrm.buflen = 16;
|
|
|
|
nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);
|
|
|
|
|
|
|
|
/* get right operand */
|
|
|
|
infix(&nrm, false);
|
|
|
|
|
|
|
|
/* get & print left operand */
|
|
|
|
in->curpol = nrm.curpol;
|
|
|
|
infix(in, false);
|
|
|
|
|
|
|
|
/* print operator & right operand */
|
|
|
|
RESIZEBUF(in, 3 + (nrm.cur - nrm.buf));
|
|
|
|
sprintf(in->cur, " %c %s", op, nrm.buf);
|
|
|
|
in->cur = strchr(in->cur, '\0');
|
|
|
|
pfree(nrm.buf);
|
|
|
|
|
2012-06-25 00:51:46 +02:00
|
|
|
if (op == (int32) '|' && !first)
|
2003-06-11 21:31:05 +02:00
|
|
|
{
|
|
|
|
RESIZEBUF(in, 2);
|
|
|
|
sprintf(in->cur, " )");
|
|
|
|
in->cur = strchr(in->cur, '\0');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
|
|
|
bqarr_out(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2011-01-09 06:39:21 +01:00
|
|
|
QUERYTYPE *query = PG_GETARG_QUERYTYPE_P(0);
|
2003-06-11 21:31:05 +02:00
|
|
|
INFIX nrm;
|
|
|
|
|
|
|
|
if (query->size == 0)
|
2003-07-24 19:52:50 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("empty query")));
|
|
|
|
|
2003-06-11 21:31:05 +02:00
|
|
|
nrm.curpol = GETQUERY(query) + query->size - 1;
|
|
|
|
nrm.buflen = 32;
|
|
|
|
nrm.cur = nrm.buf = (char *) palloc(sizeof(char) * nrm.buflen);
|
|
|
|
*(nrm.cur) = '\0';
|
|
|
|
infix(&nrm, true);
|
|
|
|
|
|
|
|
PG_FREE_IF_COPY(query, 0);
|
|
|
|
PG_RETURN_POINTER(nrm.buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-09 06:39:21 +01:00
|
|
|
/* Useless old "debugging" function for a fundamentally wrong algorithm */
|
2003-06-11 21:31:05 +02:00
|
|
|
Datum
|
|
|
|
querytree(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2011-01-09 06:39:21 +01:00
|
|
|
elog(ERROR, "querytree is no longer implemented");
|
|
|
|
PG_RETURN_NULL();
|
2003-06-11 21:31:05 +02:00
|
|
|
}
|