Datum gin_extract_tsquery(PG_FUNCTION_ARGS) { TSQuery query = PG_GETARG_TSQUERY(0); int32 *nentries = (int32 *) PG_GETARG_POINTER(1); StrategyNumber strategy = PG_GETARG_UINT16(2); Datum *entries = NULL; *nentries = 0; if (query->size > 0) { int4 i, j = 0, len; QueryItem *item; item = clean_NOT(GETQUERY(query), &len); if (!item) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("query requires full scan, which is not supported by GIN indexes"))); item = GETQUERY(query); for (i = 0; i < query->size; i++) if (item[i].type == QI_VAL) (*nentries)++; entries = (Datum *) palloc(sizeof(Datum) * (*nentries)); for (i = 0; i < query->size; i++) if (item[i].type == QI_VAL) { text *txt; QueryOperand *val = &item[i].operand; txt = (text *) palloc(VARHDRSZ + val->length); SET_VARSIZE(txt, VARHDRSZ + val->length); memcpy(VARDATA(txt), GETOPERAND(query) + val->distance, val->length); entries[j++] = PointerGetDatum(txt); if (strategy != TSearchWithClassStrategyNumber && val->weight != 0) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("@@ operator does not support lexeme weight restrictions in GIN index searches"), errhint("Use the @@@ operator instead."))); } } else *nentries = -1; /* nothing can be found */ PG_FREE_IF_COPY(query, 0); PG_RETURN_POINTER(entries); }
Datum gin_extract_tsquery(PG_FUNCTION_ARGS) { TSQuery query = PG_GETARG_TSQUERY(0); int32 *nentries = (int32 *) PG_GETARG_POINTER(1); /* StrategyNumber strategy = PG_GETARG_UINT16(2); */ bool **ptr_partialmatch = (bool **) PG_GETARG_POINTER(3); Pointer **extra_data = (Pointer **) PG_GETARG_POINTER(4); Datum *entries = NULL; bool *partialmatch; *nentries = 0; if (query->size > 0) { int4 i, j = 0, len; QueryItem *item; bool use_fullscan = false; int *map_item_operand; item = clean_NOT(GETQUERY(query), &len); if (!item) { use_fullscan = true; *nentries = 1; } item = GETQUERY(query); for (i = 0; i < query->size; i++) if (item[i].type == QI_VAL) (*nentries)++; entries = (Datum *) palloc(sizeof(Datum) * (*nentries)); partialmatch = *ptr_partialmatch = (bool *) palloc(sizeof(bool) * (*nentries)); /* * Make map to convert item's number to corresponding operand's (the * same, entry's) number. Entry's number is used in check array in * consistent method. We use the same map for each entry. */ *extra_data = (Pointer *) palloc0(sizeof(Pointer) * (*nentries)); map_item_operand = palloc0(sizeof(int) * (query->size + 1)); for (i = 0; i < query->size; i++) if (item[i].type == QI_VAL) { text *txt; QueryOperand *val = &item[i].qoperand; txt = cstring_to_text_with_len(GETOPERAND(query) + val->distance, val->length); (*extra_data)[j] = (Pointer) map_item_operand; map_item_operand[i] = j; partialmatch[j] = val->prefix; entries[j++] = PointerGetDatum(txt); } if (use_fullscan) { (*extra_data)[j] = (Pointer) map_item_operand; map_item_operand[i] = j; entries[j++] = PointerGetDatum(cstring_to_text_with_len("", 0)); } } else *nentries = -1; /* nothing can be found */ PG_FREE_IF_COPY(query, 0); PG_RETURN_POINTER(entries); }