Improve warning messages from tsearch trigger function; clean up some
casting infelicities. Allow char(n) fields to be indexed. Per Bjoern Metzdorf.
This commit is contained in:
parent
89caf56b86
commit
3e8c4c7a73
@ -207,7 +207,7 @@ gettoken_txtidx(TI_IN_STATE * state)
|
|||||||
Datum
|
Datum
|
||||||
txtidx_in(PG_FUNCTION_ARGS)
|
txtidx_in(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
char *buf = (char *) PG_GETARG_POINTER(0);
|
char *buf = PG_GETARG_CSTRING(0);
|
||||||
TI_IN_STATE state;
|
TI_IN_STATE state;
|
||||||
WordEntry *arr;
|
WordEntry *arr;
|
||||||
int4 len = 0,
|
int4 len = 0,
|
||||||
@ -276,7 +276,7 @@ txtidx_in(PG_FUNCTION_ARGS)
|
|||||||
Datum
|
Datum
|
||||||
txtidxsize(PG_FUNCTION_ARGS)
|
txtidxsize(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
txtidx *in = (txtidx *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
|
txtidx *in = (txtidx *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
|
||||||
int4 ret = in->size;
|
int4 ret = in->size;
|
||||||
|
|
||||||
PG_FREE_IF_COPY(in, 0);
|
PG_FREE_IF_COPY(in, 0);
|
||||||
@ -286,7 +286,7 @@ txtidxsize(PG_FUNCTION_ARGS)
|
|||||||
Datum
|
Datum
|
||||||
txtidx_out(PG_FUNCTION_ARGS)
|
txtidx_out(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
txtidx *out = (txtidx *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
|
txtidx *out = (txtidx *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
|
||||||
char *outbuf;
|
char *outbuf;
|
||||||
int4 i,
|
int4 i,
|
||||||
j,
|
j,
|
||||||
@ -475,7 +475,7 @@ makevalue(PRSTEXT * prs)
|
|||||||
Datum
|
Datum
|
||||||
txt2txtidx(PG_FUNCTION_ARGS)
|
txt2txtidx(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
text *in = (text *) DatumGetPointer(PG_DETOAST_DATUM(PG_GETARG_DATUM(0)));
|
text *in = PG_GETARG_TEXT_P(0);
|
||||||
PRSTEXT prs;
|
PRSTEXT prs;
|
||||||
txtidx *out = NULL;
|
txtidx *out = NULL;
|
||||||
|
|
||||||
@ -511,7 +511,6 @@ tsearch(PG_FUNCTION_ARGS)
|
|||||||
PRSTEXT prs;
|
PRSTEXT prs;
|
||||||
Datum datum = (Datum) 0;
|
Datum datum = (Datum) 0;
|
||||||
|
|
||||||
|
|
||||||
if (!CALLED_AS_TRIGGER(fcinfo))
|
if (!CALLED_AS_TRIGGER(fcinfo))
|
||||||
elog(ERROR, "TSearch: Not fired by trigger manager");
|
elog(ERROR, "TSearch: Not fired by trigger manager");
|
||||||
|
|
||||||
@ -535,7 +534,7 @@ tsearch(PG_FUNCTION_ARGS)
|
|||||||
elog(ERROR, "TSearch: format tsearch(txtidx_field, text_field1,...)");
|
elog(ERROR, "TSearch: format tsearch(txtidx_field, text_field1,...)");
|
||||||
|
|
||||||
numidxattr = SPI_fnumber(rel->rd_att, trigger->tgargs[0]);
|
numidxattr = SPI_fnumber(rel->rd_att, trigger->tgargs[0]);
|
||||||
if (numidxattr < 0)
|
if (numidxattr == SPI_ERROR_NOATTRIBUTE)
|
||||||
elog(ERROR, "TSearch: Can not find txtidx_field");
|
elog(ERROR, "TSearch: Can not find txtidx_field");
|
||||||
|
|
||||||
prs.lenwords = 32;
|
prs.lenwords = 32;
|
||||||
@ -546,27 +545,35 @@ tsearch(PG_FUNCTION_ARGS)
|
|||||||
/* find all words in indexable column */
|
/* find all words in indexable column */
|
||||||
for (i = 1; i < trigger->tgnargs; i++)
|
for (i = 1; i < trigger->tgnargs; i++)
|
||||||
{
|
{
|
||||||
int4 numattr;
|
int numattr;
|
||||||
text *txt_toasted,
|
|
||||||
*txt;
|
|
||||||
bool isnull;
|
|
||||||
Oid oidtype;
|
Oid oidtype;
|
||||||
|
Datum txt_datum;
|
||||||
|
bool isnull;
|
||||||
|
text *txt;
|
||||||
|
|
||||||
numattr = SPI_fnumber(rel->rd_att, trigger->tgargs[i]);
|
numattr = SPI_fnumber(rel->rd_att, trigger->tgargs[i]);
|
||||||
oidtype = SPI_gettypeid(rel->rd_att, numattr);
|
if (numattr == SPI_ERROR_NOATTRIBUTE)
|
||||||
if (numattr < 0 || (!(oidtype == TEXTOID || oidtype == VARCHAROID)))
|
|
||||||
{
|
{
|
||||||
elog(WARNING, "TSearch: can not find field '%s'", trigger->tgargs[i]);
|
elog(WARNING, "TSearch: can not find field '%s'",
|
||||||
|
trigger->tgargs[i]);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
txt_toasted = (text *) DatumGetPointer(SPI_getbinval(rettuple, rel->rd_att, numattr, &isnull));
|
oidtype = SPI_gettypeid(rel->rd_att, numattr);
|
||||||
|
/* We assume char() and varchar() are binary-equivalent to text */
|
||||||
|
if (!(oidtype == TEXTOID ||
|
||||||
|
oidtype == VARCHAROID ||
|
||||||
|
oidtype == BPCHAROID))
|
||||||
|
{
|
||||||
|
elog(WARNING, "TSearch: '%s' is not of character type",
|
||||||
|
trigger->tgargs[i]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
txt_datum = SPI_getbinval(rettuple, rel->rd_att, numattr, &isnull);
|
||||||
if (isnull)
|
if (isnull)
|
||||||
continue;
|
continue;
|
||||||
txt = (text *) DatumGetPointer(PG_DETOAST_DATUM(PointerGetDatum(txt_toasted)));
|
txt = DatumGetTextP(txt_datum);
|
||||||
|
|
||||||
parsetext(&prs, VARDATA(txt), VARSIZE(txt) - VARHDRSZ);
|
parsetext(&prs, VARDATA(txt), VARSIZE(txt) - VARHDRSZ);
|
||||||
if (txt != txt_toasted)
|
|
||||||
pfree(txt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* make txtidx value */
|
/* make txtidx value */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user