LCOV - code coverage report
Current view: top level - src/backend/utils/adt - tsginidx.c (source / functions) Hit Total Coverage
Test: PostgreSQL 14devel Lines: 104 120 86.7 %
Date: 2020-11-27 11:06:40 Functions: 7 12 58.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * tsginidx.c
       4             :  *   GIN support functions for tsvector_ops
       5             :  *
       6             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
       7             :  *
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *    src/backend/utils/adt/tsginidx.c
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : #include "postgres.h"
      15             : 
      16             : #include "access/gin.h"
      17             : #include "access/stratnum.h"
      18             : #include "miscadmin.h"
      19             : #include "tsearch/ts_type.h"
      20             : #include "tsearch/ts_utils.h"
      21             : #include "utils/builtins.h"
      22             : 
      23             : 
      24             : Datum
      25     1206536 : gin_cmp_tslexeme(PG_FUNCTION_ARGS)
      26             : {
      27     1206536 :     text       *a = PG_GETARG_TEXT_PP(0);
      28     1206536 :     text       *b = PG_GETARG_TEXT_PP(1);
      29             :     int         cmp;
      30             : 
      31     2413072 :     cmp = tsCompareString(VARDATA_ANY(a), VARSIZE_ANY_EXHDR(a),
      32     2413072 :                           VARDATA_ANY(b), VARSIZE_ANY_EXHDR(b),
      33             :                           false);
      34             : 
      35     1206536 :     PG_FREE_IF_COPY(a, 0);
      36     1206536 :     PG_FREE_IF_COPY(b, 1);
      37     1206536 :     PG_RETURN_INT32(cmp);
      38             : }
      39             : 
      40             : Datum
      41         296 : gin_cmp_prefix(PG_FUNCTION_ARGS)
      42             : {
      43         296 :     text       *a = PG_GETARG_TEXT_PP(0);
      44         296 :     text       *b = PG_GETARG_TEXT_PP(1);
      45             : 
      46             : #ifdef NOT_USED
      47             :     StrategyNumber strategy = PG_GETARG_UINT16(2);
      48             :     Pointer     extra_data = PG_GETARG_POINTER(3);
      49             : #endif
      50             :     int         cmp;
      51             : 
      52         592 :     cmp = tsCompareString(VARDATA_ANY(a), VARSIZE_ANY_EXHDR(a),
      53         592 :                           VARDATA_ANY(b), VARSIZE_ANY_EXHDR(b),
      54             :                           true);
      55             : 
      56         296 :     if (cmp < 0)
      57           8 :         cmp = 1;                /* prevent continue scan */
      58             : 
      59         296 :     PG_FREE_IF_COPY(a, 0);
      60         296 :     PG_FREE_IF_COPY(b, 1);
      61         296 :     PG_RETURN_INT32(cmp);
      62             : }
      63             : 
      64             : Datum
      65        2064 : gin_extract_tsvector(PG_FUNCTION_ARGS)
      66             : {
      67        2064 :     TSVector    vector = PG_GETARG_TSVECTOR(0);
      68        2064 :     int32      *nentries = (int32 *) PG_GETARG_POINTER(1);
      69        2064 :     Datum      *entries = NULL;
      70             : 
      71        2064 :     *nentries = vector->size;
      72        2064 :     if (vector->size > 0)
      73             :     {
      74             :         int         i;
      75        2028 :         WordEntry  *we = ARRPTR(vector);
      76             : 
      77        2028 :         entries = (Datum *) palloc(sizeof(Datum) * vector->size);
      78             : 
      79      117316 :         for (i = 0; i < vector->size; i++)
      80             :         {
      81             :             text       *txt;
      82             : 
      83      115288 :             txt = cstring_to_text_with_len(STRPTR(vector) + we->pos, we->len);
      84      115288 :             entries[i] = PointerGetDatum(txt);
      85             : 
      86      115288 :             we++;
      87             :         }
      88             :     }
      89             : 
      90        2064 :     PG_FREE_IF_COPY(vector, 0);
      91        2064 :     PG_RETURN_POINTER(entries);
      92             : }
      93             : 
      94             : Datum
      95         300 : gin_extract_tsquery(PG_FUNCTION_ARGS)
      96             : {
      97         300 :     TSQuery     query = PG_GETARG_TSQUERY(0);
      98         300 :     int32      *nentries = (int32 *) PG_GETARG_POINTER(1);
      99             : 
     100             :     /* StrategyNumber strategy = PG_GETARG_UINT16(2); */
     101         300 :     bool      **ptr_partialmatch = (bool **) PG_GETARG_POINTER(3);
     102         300 :     Pointer   **extra_data = (Pointer **) PG_GETARG_POINTER(4);
     103             : 
     104             :     /* bool   **nullFlags = (bool **) PG_GETARG_POINTER(5); */
     105         300 :     int32      *searchMode = (int32 *) PG_GETARG_POINTER(6);
     106         300 :     Datum      *entries = NULL;
     107             : 
     108         300 :     *nentries = 0;
     109             : 
     110         300 :     if (query->size > 0)
     111             :     {
     112         300 :         QueryItem  *item = GETQUERY(query);
     113             :         int32       i,
     114             :                     j;
     115             :         bool       *partialmatch;
     116             :         int        *map_item_operand;
     117             : 
     118             :         /*
     119             :          * If the query doesn't have any required positive matches (for
     120             :          * instance, it's something like '! foo'), we have to do a full index
     121             :          * scan.
     122             :          */
     123         300 :         if (tsquery_requires_match(item))
     124         220 :             *searchMode = GIN_SEARCH_MODE_DEFAULT;
     125             :         else
     126          80 :             *searchMode = GIN_SEARCH_MODE_ALL;
     127             : 
     128             :         /* count number of VAL items */
     129         300 :         j = 0;
     130        1136 :         for (i = 0; i < query->size; i++)
     131             :         {
     132         836 :             if (item[i].type == QI_VAL)
     133         512 :                 j++;
     134             :         }
     135         300 :         *nentries = j;
     136             : 
     137         300 :         entries = (Datum *) palloc(sizeof(Datum) * j);
     138         300 :         partialmatch = *ptr_partialmatch = (bool *) palloc(sizeof(bool) * j);
     139             : 
     140             :         /*
     141             :          * Make map to convert item's number to corresponding operand's (the
     142             :          * same, entry's) number. Entry's number is used in check array in
     143             :          * consistent method. We use the same map for each entry.
     144             :          */
     145         300 :         *extra_data = (Pointer *) palloc(sizeof(Pointer) * j);
     146         300 :         map_item_operand = (int *) palloc0(sizeof(int) * query->size);
     147             : 
     148             :         /* Now rescan the VAL items and fill in the arrays */
     149         300 :         j = 0;
     150        1136 :         for (i = 0; i < query->size; i++)
     151             :         {
     152         836 :             if (item[i].type == QI_VAL)
     153             :             {
     154         512 :                 QueryOperand *val = &item[i].qoperand;
     155             :                 text       *txt;
     156             : 
     157         512 :                 txt = cstring_to_text_with_len(GETOPERAND(query) + val->distance,
     158         512 :                                                val->length);
     159         512 :                 entries[j] = PointerGetDatum(txt);
     160         512 :                 partialmatch[j] = val->prefix;
     161         512 :                 (*extra_data)[j] = (Pointer) map_item_operand;
     162         512 :                 map_item_operand[i] = j;
     163         512 :                 j++;
     164             :             }
     165             :         }
     166             :     }
     167             : 
     168         300 :     PG_FREE_IF_COPY(query, 0);
     169             : 
     170         300 :     PG_RETURN_POINTER(entries);
     171             : }
     172             : 
     173             : typedef struct
     174             : {
     175             :     QueryItem  *first_item;
     176             :     GinTernaryValue *check;
     177             :     int        *map_item_operand;
     178             :     bool       *need_recheck;
     179             : } GinChkVal;
     180             : 
     181             : /*
     182             :  * TS_execute callback for matching a tsquery operand to GIN index data
     183             :  */
     184             : static TSTernaryValue
     185       32244 : checkcondition_gin(void *checkval, QueryOperand *val, ExecPhraseData *data)
     186             : {
     187       32244 :     GinChkVal  *gcv = (GinChkVal *) checkval;
     188             :     int         j;
     189             : 
     190             :     /*
     191             :      * if any val requiring a weight is used or caller needs position
     192             :      * information then set recheck flag
     193             :      */
     194       32244 :     if (val->weight != 0 || data != NULL)
     195       19264 :         *(gcv->need_recheck) = true;
     196             : 
     197             :     /* convert item's number to corresponding entry's (operand's) number */
     198       32244 :     j = gcv->map_item_operand[((QueryItem *) val) - gcv->first_item];
     199             : 
     200             :     /*
     201             :      * return presence of current entry in indexed value; but TRUE becomes
     202             :      * MAYBE in the presence of a query requiring recheck
     203             :      */
     204       32244 :     if (gcv->check[j] == GIN_TRUE)
     205             :     {
     206       10436 :         if (val->weight != 0 || data != NULL)
     207        4332 :             return TS_MAYBE;
     208             :     }
     209             : 
     210             :     /*
     211             :      * We rely on GinTernaryValue and TSTernaryValue using equivalent value
     212             :      * assignments.  We could use a switch statement to map the values if that
     213             :      * ever stops being true, but it seems unlikely to happen.
     214             :      */
     215       27912 :     return (TSTernaryValue) gcv->check[j];
     216             : }
     217             : 
     218             : Datum
     219          16 : gin_tsquery_consistent(PG_FUNCTION_ARGS)
     220             : {
     221          16 :     bool       *check = (bool *) PG_GETARG_POINTER(0);
     222             : 
     223             :     /* StrategyNumber strategy = PG_GETARG_UINT16(1); */
     224          16 :     TSQuery     query = PG_GETARG_TSQUERY(2);
     225             : 
     226             :     /* int32    nkeys = PG_GETARG_INT32(3); */
     227          16 :     Pointer    *extra_data = (Pointer *) PG_GETARG_POINTER(4);
     228          16 :     bool       *recheck = (bool *) PG_GETARG_POINTER(5);
     229          16 :     bool        res = false;
     230             : 
     231             :     /* Initially assume query doesn't require recheck */
     232          16 :     *recheck = false;
     233             : 
     234          16 :     if (query->size > 0)
     235             :     {
     236             :         GinChkVal   gcv;
     237             : 
     238             :         /*
     239             :          * check-parameter array has one entry for each value (operand) in the
     240             :          * query.
     241             :          */
     242          16 :         gcv.first_item = GETQUERY(query);
     243             :         StaticAssertStmt(sizeof(GinTernaryValue) == sizeof(bool),
     244             :                          "sizes of GinTernaryValue and bool are not equal");
     245          16 :         gcv.check = (GinTernaryValue *) check;
     246          16 :         gcv.map_item_operand = (int *) (extra_data[0]);
     247          16 :         gcv.need_recheck = recheck;
     248             : 
     249          16 :         res = TS_execute(GETQUERY(query),
     250             :                          &gcv,
     251             :                          TS_EXEC_PHRASE_NO_POS,
     252             :                          checkcondition_gin);
     253             :     }
     254             : 
     255          16 :     PG_RETURN_BOOL(res);
     256             : }
     257             : 
     258             : Datum
     259       24612 : gin_tsquery_triconsistent(PG_FUNCTION_ARGS)
     260             : {
     261       24612 :     GinTernaryValue *check = (GinTernaryValue *) PG_GETARG_POINTER(0);
     262             : 
     263             :     /* StrategyNumber strategy = PG_GETARG_UINT16(1); */
     264       24612 :     TSQuery     query = PG_GETARG_TSQUERY(2);
     265             : 
     266             :     /* int32    nkeys = PG_GETARG_INT32(3); */
     267       24612 :     Pointer    *extra_data = (Pointer *) PG_GETARG_POINTER(4);
     268       24612 :     GinTernaryValue res = GIN_FALSE;
     269             :     bool        recheck;
     270             : 
     271             :     /* Initially assume query doesn't require recheck */
     272       24612 :     recheck = false;
     273             : 
     274       24612 :     if (query->size > 0)
     275             :     {
     276             :         GinChkVal   gcv;
     277             : 
     278             :         /*
     279             :          * check-parameter array has one entry for each value (operand) in the
     280             :          * query.
     281             :          */
     282       24612 :         gcv.first_item = GETQUERY(query);
     283       24612 :         gcv.check = check;
     284       24612 :         gcv.map_item_operand = (int *) (extra_data[0]);
     285       24612 :         gcv.need_recheck = &recheck;
     286             : 
     287       24612 :         if (TS_execute(GETQUERY(query),
     288             :                        &gcv,
     289             :                        TS_EXEC_PHRASE_NO_POS,
     290             :                        checkcondition_gin))
     291       21928 :             res = recheck ? GIN_MAYBE : GIN_TRUE;
     292             :     }
     293             : 
     294       24612 :     PG_RETURN_GIN_TERNARY_VALUE(res);
     295             : }
     296             : 
     297             : /*
     298             :  * Formerly, gin_extract_tsvector had only two arguments.  Now it has three,
     299             :  * but we still need a pg_proc entry with two args to support reloading
     300             :  * pre-9.1 contrib/tsearch2 opclass declarations.  This compatibility
     301             :  * function should go away eventually.  (Note: you might say "hey, but the
     302             :  * code above is only *using* two args, so let's just declare it that way".
     303             :  * If you try that you'll find the opr_sanity regression test complains.)
     304             :  */
     305             : Datum
     306           0 : gin_extract_tsvector_2args(PG_FUNCTION_ARGS)
     307             : {
     308           0 :     if (PG_NARGS() < 3)          /* should not happen */
     309           0 :         elog(ERROR, "gin_extract_tsvector requires three arguments");
     310           0 :     return gin_extract_tsvector(fcinfo);
     311             : }
     312             : 
     313             : /*
     314             :  * Likewise, we need a stub version of gin_extract_tsquery declared with
     315             :  * only five arguments.
     316             :  */
     317             : Datum
     318           0 : gin_extract_tsquery_5args(PG_FUNCTION_ARGS)
     319             : {
     320           0 :     if (PG_NARGS() < 7)          /* should not happen */
     321           0 :         elog(ERROR, "gin_extract_tsquery requires seven arguments");
     322           0 :     return gin_extract_tsquery(fcinfo);
     323             : }
     324             : 
     325             : /*
     326             :  * Likewise, we need a stub version of gin_tsquery_consistent declared with
     327             :  * only six arguments.
     328             :  */
     329             : Datum
     330           0 : gin_tsquery_consistent_6args(PG_FUNCTION_ARGS)
     331             : {
     332           0 :     if (PG_NARGS() < 8)          /* should not happen */
     333           0 :         elog(ERROR, "gin_tsquery_consistent requires eight arguments");
     334           0 :     return gin_tsquery_consistent(fcinfo);
     335             : }
     336             : 
     337             : /*
     338             :  * Likewise, a stub version of gin_extract_tsquery declared with argument
     339             :  * types that are no longer considered appropriate.
     340             :  */
     341             : Datum
     342           0 : gin_extract_tsquery_oldsig(PG_FUNCTION_ARGS)
     343             : {
     344           0 :     return gin_extract_tsquery(fcinfo);
     345             : }
     346             : 
     347             : /*
     348             :  * Likewise, a stub version of gin_tsquery_consistent declared with argument
     349             :  * types that are no longer considered appropriate.
     350             :  */
     351             : Datum
     352           0 : gin_tsquery_consistent_oldsig(PG_FUNCTION_ARGS)
     353             : {
     354           0 :     return gin_tsquery_consistent(fcinfo);
     355             : }

Generated by: LCOV version 1.13