|
| 1 | +#include "postgres.h" |
| 2 | + |
| 3 | +#include <float.h> |
| 4 | + |
| 5 | +#include "access/gist.h" |
| 6 | +#include "access/itup.h" |
| 7 | +#include "access/tuptoaster.h" |
| 8 | +#include "storage/bufpage.h" |
| 9 | +#include "utils/array.h" |
| 10 | +#include "utils/builtins.h" |
| 11 | + |
| 12 | +#include "tsvector.h" |
| 13 | +#include "query.h" |
| 14 | +#include "query_cleanup.h" |
| 15 | + |
| 16 | +PG_FUNCTION_INFO_V1(gin_extract_tsvector); |
| 17 | +Datum gin_extract_tsvector(PG_FUNCTION_ARGS); |
| 18 | + |
| 19 | +Datum |
| 20 | +gin_extract_tsvector(PG_FUNCTION_ARGS) { |
| 21 | + tsvector *vector = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); |
| 22 | + uint32 *nentries = (uint32*)PG_GETARG_POINTER(1); |
| 23 | + Datum *entries = NULL; |
| 24 | + |
| 25 | + *nentries = 0; |
| 26 | + if ( vector->size > 0 ) { |
| 27 | + int i; |
| 28 | + WordEntry *we = ARRPTR( vector ); |
| 29 | + |
| 30 | + *nentries = (uint32)vector->size; |
| 31 | + entries = (Datum*)palloc( sizeof(Datum) * vector->size ); |
| 32 | + |
| 33 | + for(i=0;i<vector->size;i++) { |
| 34 | + text *txt = (text*)palloc( VARHDRSZ + we->len ); |
| 35 | + |
| 36 | + VARATT_SIZEP(txt) = VARHDRSZ + we->len; |
| 37 | + memcpy( VARDATA(txt), STRPTR( vector ) + we->pos, we->len ); |
| 38 | + |
| 39 | + entries[i] = PointerGetDatum( txt ); |
| 40 | + |
| 41 | + we++; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + PG_FREE_IF_COPY(vector, 0); |
| 46 | + PG_RETURN_POINTER(entries); |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +PG_FUNCTION_INFO_V1(gin_extract_tsquery); |
| 51 | +Datum gin_extract_tsquery(PG_FUNCTION_ARGS); |
| 52 | + |
| 53 | +Datum |
| 54 | +gin_extract_tsquery(PG_FUNCTION_ARGS) { |
| 55 | + QUERYTYPE *query = (QUERYTYPE*) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); |
| 56 | + uint32 *nentries = (uint32*)PG_GETARG_POINTER(1); |
| 57 | + StrategyNumber strategy = DatumGetUInt16( PG_GETARG_DATUM(2) ); |
| 58 | + Datum *entries = NULL; |
| 59 | + |
| 60 | + *nentries = 0; |
| 61 | + if ( query->size > 0 ) { |
| 62 | + int4 i, j=0, len; |
| 63 | + ITEM *item; |
| 64 | + |
| 65 | + item = clean_NOT_v2(GETQUERY(query), &len); |
| 66 | + if ( !item ) |
| 67 | + elog(ERROR,"Query requires full scan, GIN doesn't support it"); |
| 68 | + |
| 69 | + item = GETQUERY(query); |
| 70 | + |
| 71 | + for(i=0; i<query->size; i++) |
| 72 | + if ( item[i].type == VAL ) |
| 73 | + (*nentries)++; |
| 74 | + |
| 75 | + entries = (Datum*)palloc( sizeof(Datum) * (*nentries) ); |
| 76 | + |
| 77 | + for(i=0; i<query->size; i++) |
| 78 | + if ( item[i].type == VAL ) { |
| 79 | + text *txt; |
| 80 | + |
| 81 | + txt = (text*)palloc( VARHDRSZ + item[i].length ); |
| 82 | + |
| 83 | + VARATT_SIZEP(txt) = VARHDRSZ + item[i].length; |
| 84 | + memcpy( VARDATA(txt), GETOPERAND( query ) + item[i].distance, item[i].length ); |
| 85 | + |
| 86 | + entries[j++] = PointerGetDatum( txt ); |
| 87 | + |
| 88 | + if ( strategy == 1 && item[i].weight != 0 ) |
| 89 | + elog(ERROR,"With class of lexeme restrictions use @@@ operation"); |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + PG_FREE_IF_COPY(query, 0); |
| 95 | + PG_RETURN_POINTER(entries); |
| 96 | +} |
| 97 | + |
| 98 | +typedef struct { |
| 99 | + ITEM *frst; |
| 100 | + bool *mapped_check; |
| 101 | +} GinChkVal; |
| 102 | + |
| 103 | +static bool |
| 104 | +checkcondition_gin(void *checkval, ITEM * val) { |
| 105 | + GinChkVal *gcv = (GinChkVal*)checkval; |
| 106 | + |
| 107 | + return gcv->mapped_check[ val - gcv->frst ]; |
| 108 | +} |
| 109 | + |
| 110 | +PG_FUNCTION_INFO_V1(gin_ts_consistent); |
| 111 | +Datum gin_ts_consistent(PG_FUNCTION_ARGS); |
| 112 | + |
| 113 | +Datum |
| 114 | +gin_ts_consistent(PG_FUNCTION_ARGS) { |
| 115 | + bool *check = (bool*)PG_GETARG_POINTER(0); |
| 116 | + QUERYTYPE *query = (QUERYTYPE*) PG_DETOAST_DATUM(PG_GETARG_DATUM(2)); |
| 117 | + bool res = FALSE; |
| 118 | + |
| 119 | + if ( query->size > 0 ) { |
| 120 | + int4 i, j=0; |
| 121 | + ITEM *item; |
| 122 | + GinChkVal gcv; |
| 123 | + |
| 124 | + gcv.frst = item = GETQUERY(query); |
| 125 | + gcv.mapped_check= (bool*)palloc( sizeof(bool) * query->size ); |
| 126 | + |
| 127 | + for(i=0; i<query->size; i++) |
| 128 | + if ( item[i].type == VAL ) |
| 129 | + gcv.mapped_check[ i ] = check[ j++ ]; |
| 130 | + |
| 131 | + |
| 132 | + res = TS_execute( |
| 133 | + GETQUERY(query), |
| 134 | + &gcv, |
| 135 | + true, |
| 136 | + checkcondition_gin |
| 137 | + ); |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | + PG_FREE_IF_COPY(query, 2); |
| 142 | + PG_RETURN_BOOL(res); |
| 143 | +} |
| 144 | + |
| 145 | + |
0 commit comments