LCOV - code coverage report
Current view: top level - src/backend/tsearch - wparser.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 96.4 % 281 271
Test Date: 2026-01-26 10:56:24 Functions: 90.5 % 21 19
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 59.4 % 128 76

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * wparser.c
       4                 :             :  *              Standard interface to word parser
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  *
       8                 :             :  *
       9                 :             :  * IDENTIFICATION
      10                 :             :  *        src/backend/tsearch/wparser.c
      11                 :             :  *
      12                 :             :  *-------------------------------------------------------------------------
      13                 :             :  */
      14                 :             : #include "postgres.h"
      15                 :             : 
      16                 :             : #include "catalog/namespace.h"
      17                 :             : #include "commands/defrem.h"
      18                 :             : #include "funcapi.h"
      19                 :             : #include "tsearch/ts_cache.h"
      20                 :             : #include "tsearch/ts_utils.h"
      21                 :             : #include "utils/fmgrprotos.h"
      22                 :             : #include "utils/jsonfuncs.h"
      23                 :             : #include "utils/varlena.h"
      24                 :             : 
      25                 :             : /******sql-level interface******/
      26                 :             : 
      27                 :             : typedef struct
      28                 :             : {
      29                 :             :         int                     cur;
      30                 :             :         LexDescr   *list;
      31                 :             : } TSTokenTypeStorage;
      32                 :             : 
      33                 :             : /* state for ts_headline_json_* */
      34                 :             : typedef struct HeadlineJsonState
      35                 :             : {
      36                 :             :         HeadlineParsedText *prs;
      37                 :             :         TSConfigCacheEntry *cfg;
      38                 :             :         TSParserCacheEntry *prsobj;
      39                 :             :         TSQuery         query;
      40                 :             :         List       *prsoptions;
      41                 :             :         bool            transformed;
      42                 :             : } HeadlineJsonState;
      43                 :             : 
      44                 :             : static text *headline_json_value(void *_state, char *elem_value, int elem_len);
      45                 :             : 
      46                 :             : static void
      47                 :          39 : tt_setup_firstcall(FuncCallContext *funcctx, FunctionCallInfo fcinfo,
      48                 :             :                                    Oid prsid)
      49                 :             : {
      50                 :          39 :         TupleDesc       tupdesc;
      51                 :          39 :         MemoryContext oldcontext;
      52                 :          39 :         TSTokenTypeStorage *st;
      53                 :          39 :         TSParserCacheEntry *prs = lookup_ts_parser_cache(prsid);
      54                 :             : 
      55         [ +  - ]:          39 :         if (!OidIsValid(prs->lextypeOid))
      56   [ #  #  #  # ]:           0 :                 elog(ERROR, "method lextype isn't defined for text search parser %u",
      57                 :             :                          prsid);
      58                 :             : 
      59                 :          39 :         oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
      60                 :             : 
      61                 :          39 :         st = palloc_object(TSTokenTypeStorage);
      62                 :          39 :         st->cur = 0;
      63                 :             :         /* lextype takes one dummy argument */
      64                 :          39 :         st->list = (LexDescr *) DatumGetPointer(OidFunctionCall1(prs->lextypeOid,
      65                 :             :                                                                                                                          (Datum) 0));
      66                 :          39 :         funcctx->user_fctx = st;
      67                 :             : 
      68         [ +  - ]:          39 :         if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
      69   [ #  #  #  # ]:           0 :                 elog(ERROR, "return type must be a row type");
      70                 :          39 :         funcctx->tuple_desc = tupdesc;
      71                 :          39 :         funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
      72                 :             : 
      73                 :          39 :         MemoryContextSwitchTo(oldcontext);
      74                 :          39 : }
      75                 :             : 
      76                 :             : static Datum
      77                 :         936 : tt_process_call(FuncCallContext *funcctx)
      78                 :             : {
      79                 :         936 :         TSTokenTypeStorage *st;
      80                 :             : 
      81                 :         936 :         st = (TSTokenTypeStorage *) funcctx->user_fctx;
      82   [ +  -  +  + ]:         936 :         if (st->list && st->list[st->cur].lexid)
      83                 :             :         {
      84                 :         897 :                 Datum           result;
      85                 :         897 :                 char       *values[3];
      86                 :         897 :                 char            txtid[16];
      87                 :         897 :                 HeapTuple       tuple;
      88                 :             : 
      89                 :         897 :                 sprintf(txtid, "%d", st->list[st->cur].lexid);
      90                 :         897 :                 values[0] = txtid;
      91                 :         897 :                 values[1] = st->list[st->cur].alias;
      92                 :         897 :                 values[2] = st->list[st->cur].descr;
      93                 :             : 
      94                 :         897 :                 tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
      95                 :         897 :                 result = HeapTupleGetDatum(tuple);
      96                 :             : 
      97                 :         897 :                 pfree(values[1]);
      98                 :         897 :                 pfree(values[2]);
      99                 :         897 :                 st->cur++;
     100                 :         897 :                 return result;
     101                 :         897 :         }
     102                 :          39 :         return (Datum) 0;
     103                 :         936 : }
     104                 :             : 
     105                 :             : Datum
     106                 :         912 : ts_token_type_byid(PG_FUNCTION_ARGS)
     107                 :             : {
     108                 :         912 :         FuncCallContext *funcctx;
     109                 :         912 :         Datum           result;
     110                 :             : 
     111         [ +  + ]:         912 :         if (SRF_IS_FIRSTCALL())
     112                 :             :         {
     113                 :          38 :                 funcctx = SRF_FIRSTCALL_INIT();
     114                 :          38 :                 tt_setup_firstcall(funcctx, fcinfo, PG_GETARG_OID(0));
     115                 :          38 :         }
     116                 :             : 
     117                 :         912 :         funcctx = SRF_PERCALL_SETUP();
     118                 :             : 
     119         [ +  + ]:         912 :         if ((result = tt_process_call(funcctx)) != (Datum) 0)
     120                 :         874 :                 SRF_RETURN_NEXT(funcctx, result);
     121         [ +  - ]:          38 :         SRF_RETURN_DONE(funcctx);
     122         [ -  + ]:         912 : }
     123                 :             : 
     124                 :             : Datum
     125                 :          24 : ts_token_type_byname(PG_FUNCTION_ARGS)
     126                 :             : {
     127                 :          24 :         FuncCallContext *funcctx;
     128                 :          24 :         Datum           result;
     129                 :             : 
     130         [ +  + ]:          24 :         if (SRF_IS_FIRSTCALL())
     131                 :             :         {
     132                 :           1 :                 text       *prsname = PG_GETARG_TEXT_PP(0);
     133                 :           1 :                 Oid                     prsId;
     134                 :             : 
     135                 :           1 :                 funcctx = SRF_FIRSTCALL_INIT();
     136                 :           1 :                 prsId = get_ts_parser_oid(textToQualifiedNameList(prsname), false);
     137                 :           1 :                 tt_setup_firstcall(funcctx, fcinfo, prsId);
     138                 :           1 :         }
     139                 :             : 
     140                 :          24 :         funcctx = SRF_PERCALL_SETUP();
     141                 :             : 
     142         [ +  + ]:          24 :         if ((result = tt_process_call(funcctx)) != (Datum) 0)
     143                 :          23 :                 SRF_RETURN_NEXT(funcctx, result);
     144         [ +  - ]:           1 :         SRF_RETURN_DONE(funcctx);
     145         [ -  + ]:          24 : }
     146                 :             : 
     147                 :             : typedef struct
     148                 :             : {
     149                 :             :         int                     type;
     150                 :             :         char       *lexeme;
     151                 :             : } LexemeEntry;
     152                 :             : 
     153                 :             : typedef struct
     154                 :             : {
     155                 :             :         int                     cur;
     156                 :             :         int                     len;
     157                 :             :         LexemeEntry *list;
     158                 :             : } PrsStorage;
     159                 :             : 
     160                 :             : 
     161                 :             : static void
     162                 :           7 : prs_setup_firstcall(FuncCallContext *funcctx, FunctionCallInfo fcinfo,
     163                 :             :                                         Oid prsid, text *txt)
     164                 :             : {
     165                 :           7 :         TupleDesc       tupdesc;
     166                 :           7 :         MemoryContext oldcontext;
     167                 :           7 :         PrsStorage *st;
     168                 :           7 :         TSParserCacheEntry *prs = lookup_ts_parser_cache(prsid);
     169                 :           7 :         char       *lex = NULL;
     170                 :           7 :         int                     llen = 0,
     171                 :           7 :                                 type = 0;
     172                 :           7 :         void       *prsdata;
     173                 :             : 
     174                 :           7 :         oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
     175                 :             : 
     176                 :           7 :         st = palloc_object(PrsStorage);
     177                 :           7 :         st->cur = 0;
     178                 :           7 :         st->len = 16;
     179                 :           7 :         st->list = palloc_array(LexemeEntry, st->len);
     180                 :             : 
     181                 :           7 :         prsdata = DatumGetPointer(FunctionCall2(&prs->prsstart,
     182                 :             :                                                                                         PointerGetDatum(VARDATA_ANY(txt)),
     183                 :             :                                                                                         Int32GetDatum(VARSIZE_ANY_EXHDR(txt))));
     184                 :             : 
     185   [ +  +  +  + ]:         175 :         while ((type = DatumGetInt32(FunctionCall3(&prs->prstoken,
     186                 :             :                                                                                            PointerGetDatum(prsdata),
     187                 :             :                                                                                            PointerGetDatum(&lex),
     188                 :         175 :                                                                                            PointerGetDatum(&llen)))) != 0)
     189                 :             :         {
     190         [ +  + ]:         168 :                 if (st->cur >= st->len)
     191                 :             :                 {
     192                 :           4 :                         st->len = 2 * st->len;
     193                 :           4 :                         st->list = (LexemeEntry *) repalloc(st->list, sizeof(LexemeEntry) * st->len);
     194                 :           4 :                 }
     195                 :         168 :                 st->list[st->cur].lexeme = palloc(llen + 1);
     196                 :         168 :                 memcpy(st->list[st->cur].lexeme, lex, llen);
     197                 :         168 :                 st->list[st->cur].lexeme[llen] = '\0';
     198                 :         168 :                 st->list[st->cur].type = type;
     199                 :         168 :                 st->cur++;
     200                 :             :         }
     201                 :             : 
     202                 :           7 :         FunctionCall1(&prs->prsend, PointerGetDatum(prsdata));
     203                 :             : 
     204                 :           7 :         st->len = st->cur;
     205                 :           7 :         st->cur = 0;
     206                 :             : 
     207                 :           7 :         funcctx->user_fctx = st;
     208         [ +  - ]:           7 :         if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
     209   [ #  #  #  # ]:           0 :                 elog(ERROR, "return type must be a row type");
     210                 :           7 :         funcctx->tuple_desc = tupdesc;
     211                 :           7 :         funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
     212                 :           7 :         MemoryContextSwitchTo(oldcontext);
     213                 :           7 : }
     214                 :             : 
     215                 :             : static Datum
     216                 :         175 : prs_process_call(FuncCallContext *funcctx)
     217                 :             : {
     218                 :         175 :         PrsStorage *st;
     219                 :             : 
     220                 :         175 :         st = (PrsStorage *) funcctx->user_fctx;
     221         [ +  + ]:         175 :         if (st->cur < st->len)
     222                 :             :         {
     223                 :         168 :                 Datum           result;
     224                 :         168 :                 char       *values[2];
     225                 :         168 :                 char            tid[16];
     226                 :         168 :                 HeapTuple       tuple;
     227                 :             : 
     228                 :         168 :                 values[0] = tid;
     229                 :         168 :                 sprintf(tid, "%d", st->list[st->cur].type);
     230                 :         168 :                 values[1] = st->list[st->cur].lexeme;
     231                 :         168 :                 tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
     232                 :         168 :                 result = HeapTupleGetDatum(tuple);
     233                 :             : 
     234                 :         168 :                 pfree(values[1]);
     235                 :         168 :                 st->cur++;
     236                 :         168 :                 return result;
     237                 :         168 :         }
     238                 :           7 :         return (Datum) 0;
     239                 :         175 : }
     240                 :             : 
     241                 :             : Datum
     242                 :          35 : ts_parse_byid(PG_FUNCTION_ARGS)
     243                 :             : {
     244                 :          35 :         FuncCallContext *funcctx;
     245                 :          35 :         Datum           result;
     246                 :             : 
     247         [ +  + ]:          35 :         if (SRF_IS_FIRSTCALL())
     248                 :             :         {
     249                 :           6 :                 text       *txt = PG_GETARG_TEXT_PP(1);
     250                 :             : 
     251                 :           6 :                 funcctx = SRF_FIRSTCALL_INIT();
     252                 :           6 :                 prs_setup_firstcall(funcctx, fcinfo, PG_GETARG_OID(0), txt);
     253         [ +  - ]:           6 :                 PG_FREE_IF_COPY(txt, 1);
     254                 :           6 :         }
     255                 :             : 
     256                 :          35 :         funcctx = SRF_PERCALL_SETUP();
     257                 :             : 
     258         [ +  + ]:          35 :         if ((result = prs_process_call(funcctx)) != (Datum) 0)
     259                 :          29 :                 SRF_RETURN_NEXT(funcctx, result);
     260         [ +  - ]:           6 :         SRF_RETURN_DONE(funcctx);
     261         [ -  + ]:          35 : }
     262                 :             : 
     263                 :             : Datum
     264                 :         140 : ts_parse_byname(PG_FUNCTION_ARGS)
     265                 :             : {
     266                 :         140 :         FuncCallContext *funcctx;
     267                 :         140 :         Datum           result;
     268                 :             : 
     269         [ +  + ]:         140 :         if (SRF_IS_FIRSTCALL())
     270                 :             :         {
     271                 :           1 :                 text       *prsname = PG_GETARG_TEXT_PP(0);
     272                 :           1 :                 text       *txt = PG_GETARG_TEXT_PP(1);
     273                 :           1 :                 Oid                     prsId;
     274                 :             : 
     275                 :           1 :                 funcctx = SRF_FIRSTCALL_INIT();
     276                 :           1 :                 prsId = get_ts_parser_oid(textToQualifiedNameList(prsname), false);
     277                 :           1 :                 prs_setup_firstcall(funcctx, fcinfo, prsId, txt);
     278                 :           1 :         }
     279                 :             : 
     280                 :         140 :         funcctx = SRF_PERCALL_SETUP();
     281                 :             : 
     282         [ +  + ]:         140 :         if ((result = prs_process_call(funcctx)) != (Datum) 0)
     283                 :         139 :                 SRF_RETURN_NEXT(funcctx, result);
     284         [ +  - ]:           1 :         SRF_RETURN_DONE(funcctx);
     285         [ -  + ]:         140 : }
     286                 :             : 
     287                 :             : Datum
     288                 :          24 : ts_headline_byid_opt(PG_FUNCTION_ARGS)
     289                 :             : {
     290                 :          24 :         Oid                     tsconfig = PG_GETARG_OID(0);
     291                 :          24 :         text       *in = PG_GETARG_TEXT_PP(1);
     292                 :          24 :         TSQuery         query = PG_GETARG_TSQUERY(2);
     293   [ +  +  -  + ]:          24 :         text       *opt = (PG_NARGS() > 3 && PG_GETARG_POINTER(3)) ? PG_GETARG_TEXT_PP(3) : NULL;
     294                 :          24 :         HeadlineParsedText prs;
     295                 :          24 :         List       *prsoptions;
     296                 :          24 :         text       *out;
     297                 :          24 :         TSConfigCacheEntry *cfg;
     298                 :          24 :         TSParserCacheEntry *prsobj;
     299                 :             : 
     300                 :          24 :         cfg = lookup_ts_config_cache(tsconfig);
     301                 :          24 :         prsobj = lookup_ts_parser_cache(cfg->prsId);
     302                 :             : 
     303         [ +  - ]:          24 :         if (!OidIsValid(prsobj->headlineOid))
     304   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     305                 :             :                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     306                 :             :                                  errmsg("text search parser does not support headline creation")));
     307                 :             : 
     308                 :          24 :         memset(&prs, 0, sizeof(HeadlineParsedText));
     309                 :          24 :         prs.lenwords = 32;
     310                 :          24 :         prs.words = palloc_array(HeadlineWordEntry, prs.lenwords);
     311                 :             : 
     312                 :          48 :         hlparsetext(cfg->cfgId, &prs, query,
     313                 :          24 :                                 VARDATA_ANY(in), VARSIZE_ANY_EXHDR(in));
     314                 :             : 
     315         [ +  + ]:          24 :         if (opt)
     316                 :          11 :                 prsoptions = deserialize_deflist(PointerGetDatum(opt));
     317                 :             :         else
     318                 :          13 :                 prsoptions = NIL;
     319                 :             : 
     320                 :          24 :         FunctionCall3(&(prsobj->prsheadline),
     321                 :             :                                   PointerGetDatum(&prs),
     322                 :             :                                   PointerGetDatum(prsoptions),
     323                 :             :                                   PointerGetDatum(query));
     324                 :             : 
     325                 :          24 :         out = generateHeadline(&prs);
     326                 :             : 
     327         [ +  - ]:          24 :         PG_FREE_IF_COPY(in, 1);
     328         [ +  - ]:          24 :         PG_FREE_IF_COPY(query, 2);
     329         [ +  + ]:          24 :         if (opt)
     330         [ +  - ]:          11 :                 PG_FREE_IF_COPY(opt, 3);
     331                 :          24 :         pfree(prs.words);
     332                 :          24 :         pfree(prs.startsel);
     333                 :          24 :         pfree(prs.stopsel);
     334                 :             : 
     335                 :          48 :         PG_RETURN_POINTER(out);
     336                 :          24 : }
     337                 :             : 
     338                 :             : Datum
     339                 :          13 : ts_headline_byid(PG_FUNCTION_ARGS)
     340                 :             : {
     341                 :          13 :         PG_RETURN_DATUM(DirectFunctionCall3(ts_headline_byid_opt,
     342                 :             :                                                                                 PG_GETARG_DATUM(0),
     343                 :             :                                                                                 PG_GETARG_DATUM(1),
     344                 :             :                                                                                 PG_GETARG_DATUM(2)));
     345                 :             : }
     346                 :             : 
     347                 :             : Datum
     348                 :           0 : ts_headline(PG_FUNCTION_ARGS)
     349                 :             : {
     350                 :           0 :         PG_RETURN_DATUM(DirectFunctionCall3(ts_headline_byid_opt,
     351                 :             :                                                                                 ObjectIdGetDatum(getTSCurrentConfig(true)),
     352                 :             :                                                                                 PG_GETARG_DATUM(0),
     353                 :             :                                                                                 PG_GETARG_DATUM(1)));
     354                 :             : }
     355                 :             : 
     356                 :             : Datum
     357                 :           0 : ts_headline_opt(PG_FUNCTION_ARGS)
     358                 :             : {
     359                 :           0 :         PG_RETURN_DATUM(DirectFunctionCall4(ts_headline_byid_opt,
     360                 :             :                                                                                 ObjectIdGetDatum(getTSCurrentConfig(true)),
     361                 :             :                                                                                 PG_GETARG_DATUM(0),
     362                 :             :                                                                                 PG_GETARG_DATUM(1),
     363                 :             :                                                                                 PG_GETARG_DATUM(2)));
     364                 :             : }
     365                 :             : 
     366                 :             : Datum
     367                 :           7 : ts_headline_jsonb_byid_opt(PG_FUNCTION_ARGS)
     368                 :             : {
     369                 :           7 :         Oid                     tsconfig = PG_GETARG_OID(0);
     370                 :           7 :         Jsonb      *jb = PG_GETARG_JSONB_P(1);
     371                 :           7 :         TSQuery         query = PG_GETARG_TSQUERY(2);
     372   [ +  +  -  + ]:           7 :         text       *opt = (PG_NARGS() > 3 && PG_GETARG_POINTER(3)) ? PG_GETARG_TEXT_P(3) : NULL;
     373                 :           7 :         Jsonb      *out;
     374                 :           7 :         JsonTransformStringValuesAction action = (JsonTransformStringValuesAction) headline_json_value;
     375                 :           7 :         HeadlineParsedText prs;
     376                 :           7 :         HeadlineJsonState *state = palloc0_object(HeadlineJsonState);
     377                 :             : 
     378                 :           7 :         memset(&prs, 0, sizeof(HeadlineParsedText));
     379                 :           7 :         prs.lenwords = 32;
     380                 :           7 :         prs.words = palloc_array(HeadlineWordEntry, prs.lenwords);
     381                 :             : 
     382                 :           7 :         state->prs = &prs;
     383                 :           7 :         state->cfg = lookup_ts_config_cache(tsconfig);
     384                 :           7 :         state->prsobj = lookup_ts_parser_cache(state->cfg->prsId);
     385                 :           7 :         state->query = query;
     386         [ +  + ]:           7 :         if (opt)
     387                 :           2 :                 state->prsoptions = deserialize_deflist(PointerGetDatum(opt));
     388                 :             :         else
     389                 :           5 :                 state->prsoptions = NIL;
     390                 :             : 
     391         [ +  - ]:           7 :         if (!OidIsValid(state->prsobj->headlineOid))
     392   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     393                 :             :                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     394                 :             :                                  errmsg("text search parser does not support headline creation")));
     395                 :             : 
     396                 :           7 :         out = transform_jsonb_string_values(jb, state, action);
     397                 :             : 
     398         [ +  - ]:           7 :         PG_FREE_IF_COPY(jb, 1);
     399         [ +  - ]:           7 :         PG_FREE_IF_COPY(query, 2);
     400         [ +  + ]:           7 :         if (opt)
     401         [ +  - ]:           2 :                 PG_FREE_IF_COPY(opt, 3);
     402                 :             : 
     403                 :           7 :         pfree(prs.words);
     404                 :             : 
     405         [ +  + ]:           7 :         if (state->transformed)
     406                 :             :         {
     407                 :           4 :                 pfree(prs.startsel);
     408                 :           4 :                 pfree(prs.stopsel);
     409                 :           4 :         }
     410                 :             : 
     411                 :          14 :         PG_RETURN_JSONB_P(out);
     412                 :           7 : }
     413                 :             : 
     414                 :             : Datum
     415                 :           4 : ts_headline_jsonb(PG_FUNCTION_ARGS)
     416                 :             : {
     417                 :           4 :         PG_RETURN_DATUM(DirectFunctionCall3(ts_headline_jsonb_byid_opt,
     418                 :             :                                                                                 ObjectIdGetDatum(getTSCurrentConfig(true)),
     419                 :             :                                                                                 PG_GETARG_DATUM(0),
     420                 :             :                                                                                 PG_GETARG_DATUM(1)));
     421                 :             : }
     422                 :             : 
     423                 :             : Datum
     424                 :           1 : ts_headline_jsonb_byid(PG_FUNCTION_ARGS)
     425                 :             : {
     426                 :           1 :         PG_RETURN_DATUM(DirectFunctionCall3(ts_headline_jsonb_byid_opt,
     427                 :             :                                                                                 PG_GETARG_DATUM(0),
     428                 :             :                                                                                 PG_GETARG_DATUM(1),
     429                 :             :                                                                                 PG_GETARG_DATUM(2)));
     430                 :             : }
     431                 :             : 
     432                 :             : Datum
     433                 :           1 : ts_headline_jsonb_opt(PG_FUNCTION_ARGS)
     434                 :             : {
     435                 :           1 :         PG_RETURN_DATUM(DirectFunctionCall4(ts_headline_jsonb_byid_opt,
     436                 :             :                                                                                 ObjectIdGetDatum(getTSCurrentConfig(true)),
     437                 :             :                                                                                 PG_GETARG_DATUM(0),
     438                 :             :                                                                                 PG_GETARG_DATUM(1),
     439                 :             :                                                                                 PG_GETARG_DATUM(2)));
     440                 :             : }
     441                 :             : 
     442                 :             : Datum
     443                 :           7 : ts_headline_json_byid_opt(PG_FUNCTION_ARGS)
     444                 :             : {
     445                 :           7 :         Oid                     tsconfig = PG_GETARG_OID(0);
     446                 :           7 :         text       *json = PG_GETARG_TEXT_P(1);
     447                 :           7 :         TSQuery         query = PG_GETARG_TSQUERY(2);
     448   [ +  +  -  + ]:           7 :         text       *opt = (PG_NARGS() > 3 && PG_GETARG_POINTER(3)) ? PG_GETARG_TEXT_P(3) : NULL;
     449                 :           7 :         text       *out;
     450                 :           7 :         JsonTransformStringValuesAction action = (JsonTransformStringValuesAction) headline_json_value;
     451                 :             : 
     452                 :           7 :         HeadlineParsedText prs;
     453                 :           7 :         HeadlineJsonState *state = palloc0_object(HeadlineJsonState);
     454                 :             : 
     455                 :           7 :         memset(&prs, 0, sizeof(HeadlineParsedText));
     456                 :           7 :         prs.lenwords = 32;
     457                 :           7 :         prs.words = palloc_array(HeadlineWordEntry, prs.lenwords);
     458                 :             : 
     459                 :           7 :         state->prs = &prs;
     460                 :           7 :         state->cfg = lookup_ts_config_cache(tsconfig);
     461                 :           7 :         state->prsobj = lookup_ts_parser_cache(state->cfg->prsId);
     462                 :           7 :         state->query = query;
     463         [ +  + ]:           7 :         if (opt)
     464                 :           2 :                 state->prsoptions = deserialize_deflist(PointerGetDatum(opt));
     465                 :             :         else
     466                 :           5 :                 state->prsoptions = NIL;
     467                 :             : 
     468         [ +  - ]:           7 :         if (!OidIsValid(state->prsobj->headlineOid))
     469   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     470                 :             :                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     471                 :             :                                  errmsg("text search parser does not support headline creation")));
     472                 :             : 
     473                 :           7 :         out = transform_json_string_values(json, state, action);
     474                 :             : 
     475         [ +  - ]:           7 :         PG_FREE_IF_COPY(json, 1);
     476         [ +  - ]:           7 :         PG_FREE_IF_COPY(query, 2);
     477         [ +  + ]:           7 :         if (opt)
     478         [ +  - ]:           2 :                 PG_FREE_IF_COPY(opt, 3);
     479                 :           7 :         pfree(prs.words);
     480                 :             : 
     481         [ +  + ]:           7 :         if (state->transformed)
     482                 :             :         {
     483                 :           4 :                 pfree(prs.startsel);
     484                 :           4 :                 pfree(prs.stopsel);
     485                 :           4 :         }
     486                 :             : 
     487                 :          14 :         PG_RETURN_TEXT_P(out);
     488                 :           7 : }
     489                 :             : 
     490                 :             : Datum
     491                 :           4 : ts_headline_json(PG_FUNCTION_ARGS)
     492                 :             : {
     493                 :           4 :         PG_RETURN_DATUM(DirectFunctionCall3(ts_headline_json_byid_opt,
     494                 :             :                                                                                 ObjectIdGetDatum(getTSCurrentConfig(true)),
     495                 :             :                                                                                 PG_GETARG_DATUM(0),
     496                 :             :                                                                                 PG_GETARG_DATUM(1)));
     497                 :             : }
     498                 :             : 
     499                 :             : Datum
     500                 :           1 : ts_headline_json_byid(PG_FUNCTION_ARGS)
     501                 :             : {
     502                 :           1 :         PG_RETURN_DATUM(DirectFunctionCall3(ts_headline_json_byid_opt,
     503                 :             :                                                                                 PG_GETARG_DATUM(0),
     504                 :             :                                                                                 PG_GETARG_DATUM(1),
     505                 :             :                                                                                 PG_GETARG_DATUM(2)));
     506                 :             : }
     507                 :             : 
     508                 :             : Datum
     509                 :           1 : ts_headline_json_opt(PG_FUNCTION_ARGS)
     510                 :             : {
     511                 :           1 :         PG_RETURN_DATUM(DirectFunctionCall4(ts_headline_json_byid_opt,
     512                 :             :                                                                                 ObjectIdGetDatum(getTSCurrentConfig(true)),
     513                 :             :                                                                                 PG_GETARG_DATUM(0),
     514                 :             :                                                                                 PG_GETARG_DATUM(1),
     515                 :             :                                                                                 PG_GETARG_DATUM(2)));
     516                 :             : }
     517                 :             : 
     518                 :             : 
     519                 :             : /*
     520                 :             :  * Return headline in text from, generated from a json(b) element
     521                 :             :  */
     522                 :             : static text *
     523                 :          38 : headline_json_value(void *_state, char *elem_value, int elem_len)
     524                 :             : {
     525                 :          38 :         HeadlineJsonState *state = (HeadlineJsonState *) _state;
     526                 :             : 
     527                 :          38 :         HeadlineParsedText *prs = state->prs;
     528                 :          38 :         TSConfigCacheEntry *cfg = state->cfg;
     529                 :          38 :         TSParserCacheEntry *prsobj = state->prsobj;
     530                 :          38 :         TSQuery         query = state->query;
     531                 :          38 :         List       *prsoptions = state->prsoptions;
     532                 :             : 
     533                 :          38 :         prs->curwords = 0;
     534                 :          38 :         hlparsetext(cfg->cfgId, prs, query, elem_value, elem_len);
     535                 :          38 :         FunctionCall3(&(prsobj->prsheadline),
     536                 :             :                                   PointerGetDatum(prs),
     537                 :             :                                   PointerGetDatum(prsoptions),
     538                 :             :                                   PointerGetDatum(query));
     539                 :             : 
     540                 :          38 :         state->transformed = true;
     541                 :          76 :         return generateHeadline(prs);
     542                 :          38 : }
        

Generated by: LCOV version 2.3.2-1