Coverage Report

Created: 2026-12-01 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/postgres/src/backend/tsearch/dict_thesaurus.c
Line
Count
Source
1
/*-------------------------------------------------------------------------
2
 *
3
 * dict_thesaurus.c
4
 *    Thesaurus dictionary: phrase to phrase substitution
5
 *
6
 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7
 *
8
 *
9
 * IDENTIFICATION
10
 *    src/backend/tsearch/dict_thesaurus.c
11
 *
12
 *-------------------------------------------------------------------------
13
 */
14
#include "postgres.h"
15
16
#include "catalog/namespace.h"
17
#include "commands/defrem.h"
18
#include "tsearch/ts_cache.h"
19
#include "tsearch/ts_locale.h"
20
#include "tsearch/ts_public.h"
21
#include "utils/fmgrprotos.h"
22
#include "utils/regproc.h"
23
24
25
/*
26
 * Temporary we use TSLexeme.flags for inner use...
27
 */
28
0
#define DT_USEASIS    0x1000
29
30
typedef struct LexemeInfo
31
{
32
  uint32    idsubst;    /* entry's number in DictThesaurus->subst */
33
  uint16    posinsubst;   /* pos info in entry */
34
  uint16    tnvariant;    /* total num lexemes in one variant */
35
  struct LexemeInfo *nextentry;
36
  struct LexemeInfo *nextvariant;
37
} LexemeInfo;
38
39
typedef struct
40
{
41
  char     *lexeme;
42
  LexemeInfo *entries;
43
} TheLexeme;
44
45
typedef struct
46
{
47
  uint16    lastlexeme;   /* number lexemes to substitute */
48
  uint16    reslen;
49
  TSLexeme   *res;      /* prepared substituted result */
50
} TheSubstitute;
51
52
typedef struct
53
{
54
  /* subdictionary to normalize lexemes */
55
  Oid     subdictOid;
56
  TSDictionaryCacheEntry *subdict;
57
58
  /* Array to search lexeme by exact match */
59
  TheLexeme  *wrds;
60
  int     nwrds;      /* current number of words */
61
  int     ntwrds;     /* allocated array length */
62
63
  /*
64
   * Storage of substituted result, n-th element is for n-th expression
65
   */
66
  TheSubstitute *subst;
67
  int     nsubst;
68
} DictThesaurus;
69
70
71
static void
72
newLexeme(DictThesaurus *d, char *b, char *e, uint32 idsubst, uint16 posinsubst)
73
0
{
74
0
  TheLexeme  *ptr;
75
76
0
  if (d->nwrds >= d->ntwrds)
77
0
  {
78
0
    if (d->ntwrds == 0)
79
0
    {
80
0
      d->ntwrds = 16;
81
0
      d->wrds = palloc_array(TheLexeme, d->ntwrds);
82
0
    }
83
0
    else
84
0
    {
85
0
      d->ntwrds *= 2;
86
0
      d->wrds = repalloc_array(d->wrds, TheLexeme, d->ntwrds);
87
0
    }
88
0
  }
89
90
0
  ptr = d->wrds + d->nwrds;
91
0
  d->nwrds++;
92
93
0
  ptr->lexeme = palloc(e - b + 1);
94
95
0
  memcpy(ptr->lexeme, b, e - b);
96
0
  ptr->lexeme[e - b] = '\0';
97
98
0
  ptr->entries = palloc_object(LexemeInfo);
99
100
0
  ptr->entries->nextentry = NULL;
101
0
  ptr->entries->idsubst = idsubst;
102
0
  ptr->entries->posinsubst = posinsubst;
103
0
}
104
105
static void
106
addWrd(DictThesaurus *d, char *b, char *e, uint32 idsubst, uint16 nwrd, uint16 posinsubst, bool useasis)
107
0
{
108
0
  static int  nres = 0;
109
0
  static int  ntres = 0;
110
0
  TheSubstitute *ptr;
111
112
0
  if (nwrd == 0)
113
0
  {
114
0
    nres = ntres = 0;
115
116
0
    if (idsubst >= d->nsubst)
117
0
    {
118
0
      if (d->nsubst == 0)
119
0
      {
120
0
        d->nsubst = 16;
121
0
        d->subst = palloc_array(TheSubstitute, d->nsubst);
122
0
      }
123
0
      else
124
0
      {
125
0
        d->nsubst *= 2;
126
0
        d->subst = repalloc_array(d->subst, TheSubstitute, d->nsubst);
127
0
      }
128
0
    }
129
0
  }
130
131
0
  ptr = d->subst + idsubst;
132
133
0
  ptr->lastlexeme = posinsubst - 1;
134
135
0
  if (nres + 1 >= ntres)
136
0
  {
137
0
    if (ntres == 0)
138
0
    {
139
0
      ntres = 2;
140
0
      ptr->res = palloc_array(TSLexeme, ntres);
141
0
    }
142
0
    else
143
0
    {
144
0
      ntres *= 2;
145
0
      ptr->res = repalloc_array(ptr->res, TSLexeme, ntres);
146
0
    }
147
0
  }
148
149
0
  ptr->res[nres].lexeme = palloc(e - b + 1);
150
0
  memcpy(ptr->res[nres].lexeme, b, e - b);
151
0
  ptr->res[nres].lexeme[e - b] = '\0';
152
153
0
  ptr->res[nres].nvariant = nwrd;
154
0
  if (useasis)
155
0
    ptr->res[nres].flags = DT_USEASIS;
156
0
  else
157
0
    ptr->res[nres].flags = 0;
158
159
0
  ptr->res[++nres].lexeme = NULL;
160
0
}
161
162
0
#define TR_WAITLEX  1
163
0
#define TR_INLEX  2
164
0
#define TR_WAITSUBS 3
165
0
#define TR_INSUBS 4
166
167
static void
168
thesaurusRead(const char *filename, DictThesaurus *d)
169
0
{
170
0
  char     *real_filename = get_tsearch_config_filename(filename, "ths");
171
0
  tsearch_readline_state trst;
172
0
  uint32    idsubst = 0;
173
0
  bool    useasis = false;
174
0
  char     *line;
175
176
0
  if (!tsearch_readline_begin(&trst, real_filename))
177
0
    ereport(ERROR,
178
0
        (errcode(ERRCODE_CONFIG_FILE_ERROR),
179
0
         errmsg("could not open thesaurus file \"%s\": %m",
180
0
            real_filename)));
181
182
0
  while ((line = tsearch_readline(&trst)) != NULL)
183
0
  {
184
0
    char     *ptr;
185
0
    int     state = TR_WAITLEX;
186
0
    char     *beginwrd = NULL;
187
0
    uint32    posinsubst = 0;
188
0
    uint32    nwrd = 0;
189
190
0
    ptr = line;
191
192
    /* is it a comment? */
193
0
    while (*ptr && isspace((unsigned char) *ptr))
194
0
      ptr += pg_mblen_cstr(ptr);
195
196
0
    if (t_iseq(ptr, '#') || *ptr == '\0' ||
197
0
      t_iseq(ptr, '\n') || t_iseq(ptr, '\r'))
198
0
    {
199
0
      pfree(line);
200
0
      continue;
201
0
    }
202
203
0
    while (*ptr)
204
0
    {
205
0
      if (state == TR_WAITLEX)
206
0
      {
207
0
        if (t_iseq(ptr, ':'))
208
0
        {
209
0
          if (posinsubst == 0)
210
0
            ereport(ERROR,
211
0
                (errcode(ERRCODE_CONFIG_FILE_ERROR),
212
0
                 errmsg("unexpected delimiter")));
213
0
          state = TR_WAITSUBS;
214
0
        }
215
0
        else if (!isspace((unsigned char) *ptr))
216
0
        {
217
0
          beginwrd = ptr;
218
0
          state = TR_INLEX;
219
0
        }
220
0
      }
221
0
      else if (state == TR_INLEX)
222
0
      {
223
0
        if (t_iseq(ptr, ':'))
224
0
        {
225
0
          newLexeme(d, beginwrd, ptr, idsubst, posinsubst++);
226
0
          state = TR_WAITSUBS;
227
0
        }
228
0
        else if (isspace((unsigned char) *ptr))
229
0
        {
230
0
          newLexeme(d, beginwrd, ptr, idsubst, posinsubst++);
231
0
          state = TR_WAITLEX;
232
0
        }
233
0
      }
234
0
      else if (state == TR_WAITSUBS)
235
0
      {
236
0
        if (t_iseq(ptr, '*'))
237
0
        {
238
0
          useasis = true;
239
0
          state = TR_INSUBS;
240
0
          beginwrd = ptr + pg_mblen_cstr(ptr);
241
0
        }
242
0
        else if (t_iseq(ptr, '\\'))
243
0
        {
244
0
          useasis = false;
245
0
          state = TR_INSUBS;
246
0
          beginwrd = ptr + pg_mblen_cstr(ptr);
247
0
        }
248
0
        else if (!isspace((unsigned char) *ptr))
249
0
        {
250
0
          useasis = false;
251
0
          beginwrd = ptr;
252
0
          state = TR_INSUBS;
253
0
        }
254
0
      }
255
0
      else if (state == TR_INSUBS)
256
0
      {
257
0
        if (isspace((unsigned char) *ptr))
258
0
        {
259
0
          if (ptr == beginwrd)
260
0
            ereport(ERROR,
261
0
                (errcode(ERRCODE_CONFIG_FILE_ERROR),
262
0
                 errmsg("unexpected end of line or lexeme")));
263
0
          addWrd(d, beginwrd, ptr, idsubst, nwrd++, posinsubst, useasis);
264
0
          state = TR_WAITSUBS;
265
0
        }
266
0
      }
267
0
      else
268
0
        elog(ERROR, "unrecognized thesaurus state: %d", state);
269
270
0
      ptr += pg_mblen_cstr(ptr);
271
0
    }
272
273
0
    if (state == TR_INSUBS)
274
0
    {
275
0
      if (ptr == beginwrd)
276
0
        ereport(ERROR,
277
0
            (errcode(ERRCODE_CONFIG_FILE_ERROR),
278
0
             errmsg("unexpected end of line or lexeme")));
279
0
      addWrd(d, beginwrd, ptr, idsubst, nwrd++, posinsubst, useasis);
280
0
    }
281
282
0
    idsubst++;
283
284
0
    if (!(nwrd && posinsubst))
285
0
      ereport(ERROR,
286
0
          (errcode(ERRCODE_CONFIG_FILE_ERROR),
287
0
           errmsg("unexpected end of line")));
288
289
0
    if (nwrd != (uint16) nwrd || posinsubst != (uint16) posinsubst)
290
0
      ereport(ERROR,
291
0
          (errcode(ERRCODE_CONFIG_FILE_ERROR),
292
0
           errmsg("too many lexemes in thesaurus entry")));
293
294
0
    pfree(line);
295
0
  }
296
297
0
  d->nsubst = idsubst;
298
299
0
  tsearch_readline_end(&trst);
300
0
  pfree(real_filename);
301
0
}
302
303
static TheLexeme *
304
addCompiledLexeme(TheLexeme *newwrds, int *nnw, int *tnm, TSLexeme *lexeme, LexemeInfo *src, uint16 tnvariant)
305
0
{
306
0
  if (*nnw >= *tnm)
307
0
  {
308
0
    *tnm *= 2;
309
0
    newwrds = repalloc_array(newwrds, TheLexeme, *tnm);
310
0
  }
311
312
0
  newwrds[*nnw].entries = palloc_object(LexemeInfo);
313
314
0
  if (lexeme && lexeme->lexeme)
315
0
  {
316
0
    newwrds[*nnw].lexeme = pstrdup(lexeme->lexeme);
317
0
    newwrds[*nnw].entries->tnvariant = tnvariant;
318
0
  }
319
0
  else
320
0
  {
321
0
    newwrds[*nnw].lexeme = NULL;
322
0
    newwrds[*nnw].entries->tnvariant = 1;
323
0
  }
324
325
0
  newwrds[*nnw].entries->idsubst = src->idsubst;
326
0
  newwrds[*nnw].entries->posinsubst = src->posinsubst;
327
328
0
  newwrds[*nnw].entries->nextentry = NULL;
329
330
0
  (*nnw)++;
331
0
  return newwrds;
332
0
}
333
334
static int
335
cmpLexemeInfo(LexemeInfo *a, LexemeInfo *b)
336
0
{
337
0
  if (a == NULL || b == NULL)
338
0
    return 0;
339
340
0
  if (a->idsubst == b->idsubst)
341
0
  {
342
0
    if (a->posinsubst == b->posinsubst)
343
0
    {
344
0
      if (a->tnvariant == b->tnvariant)
345
0
        return 0;
346
347
0
      return (a->tnvariant > b->tnvariant) ? 1 : -1;
348
0
    }
349
350
0
    return (a->posinsubst > b->posinsubst) ? 1 : -1;
351
0
  }
352
353
0
  return (a->idsubst > b->idsubst) ? 1 : -1;
354
0
}
355
356
static int
357
cmpLexeme(const TheLexeme *a, const TheLexeme *b)
358
0
{
359
0
  if (a->lexeme == NULL)
360
0
  {
361
0
    if (b->lexeme == NULL)
362
0
      return 0;
363
0
    else
364
0
      return 1;
365
0
  }
366
0
  else if (b->lexeme == NULL)
367
0
    return -1;
368
369
0
  return strcmp(a->lexeme, b->lexeme);
370
0
}
371
372
static int
373
cmpLexemeQ(const void *a, const void *b)
374
0
{
375
0
  return cmpLexeme((const TheLexeme *) a, (const TheLexeme *) b);
376
0
}
377
378
static int
379
cmpTheLexeme(const void *a, const void *b)
380
0
{
381
0
  const TheLexeme *la = (const TheLexeme *) a;
382
0
  const TheLexeme *lb = (const TheLexeme *) b;
383
0
  int     res;
384
385
0
  if ((res = cmpLexeme(la, lb)) != 0)
386
0
    return res;
387
388
0
  return -cmpLexemeInfo(la->entries, lb->entries);
389
0
}
390
391
static void
392
compileTheLexeme(DictThesaurus *d)
393
0
{
394
0
  int     i,
395
0
        nnw = 0,
396
0
        tnm = 16;
397
0
  TheLexeme  *newwrds = palloc_array(TheLexeme, tnm),
398
0
         *ptrwrds;
399
400
0
  for (i = 0; i < d->nwrds; i++)
401
0
  {
402
0
    TSLexeme   *ptr;
403
404
0
    if (strcmp(d->wrds[i].lexeme, "?") == 0) /* Is stop word marker? */
405
0
      newwrds = addCompiledLexeme(newwrds, &nnw, &tnm, NULL, d->wrds[i].entries, 0);
406
0
    else
407
0
    {
408
0
      ptr = (TSLexeme *) DatumGetPointer(FunctionCall4(&(d->subdict->lexize),
409
0
                               PointerGetDatum(d->subdict->dictData),
410
0
                               PointerGetDatum(d->wrds[i].lexeme),
411
0
                               Int32GetDatum(strlen(d->wrds[i].lexeme)),
412
0
                               PointerGetDatum(NULL)));
413
414
0
      if (!ptr)
415
0
        ereport(ERROR,
416
0
            (errcode(ERRCODE_CONFIG_FILE_ERROR),
417
0
             errmsg("thesaurus sample word \"%s\" isn't recognized by subdictionary (rule %d)",
418
0
                d->wrds[i].lexeme,
419
0
                d->wrds[i].entries->idsubst + 1)));
420
0
      else if (!(ptr->lexeme))
421
0
        ereport(ERROR,
422
0
            (errcode(ERRCODE_CONFIG_FILE_ERROR),
423
0
             errmsg("thesaurus sample word \"%s\" is a stop word (rule %d)",
424
0
                d->wrds[i].lexeme,
425
0
                d->wrds[i].entries->idsubst + 1),
426
0
             errhint("Use \"?\" to represent a stop word within a sample phrase.")));
427
0
      else
428
0
      {
429
0
        while (ptr->lexeme)
430
0
        {
431
0
          TSLexeme   *remptr = ptr + 1;
432
0
          int     tnvar = 1;
433
0
          int     curvar = ptr->nvariant;
434
435
          /* compute n words in one variant */
436
0
          while (remptr->lexeme)
437
0
          {
438
0
            if (remptr->nvariant != (remptr - 1)->nvariant)
439
0
              break;
440
0
            tnvar++;
441
0
            remptr++;
442
0
          }
443
444
0
          remptr = ptr;
445
0
          while (remptr->lexeme && remptr->nvariant == curvar)
446
0
          {
447
0
            newwrds = addCompiledLexeme(newwrds, &nnw, &tnm, remptr, d->wrds[i].entries, tnvar);
448
0
            remptr++;
449
0
          }
450
451
0
          ptr = remptr;
452
0
        }
453
0
      }
454
0
    }
455
456
0
    pfree(d->wrds[i].lexeme);
457
0
    pfree(d->wrds[i].entries);
458
0
  }
459
460
0
  if (d->wrds)
461
0
    pfree(d->wrds);
462
0
  d->wrds = newwrds;
463
0
  d->nwrds = nnw;
464
0
  d->ntwrds = tnm;
465
466
0
  if (d->nwrds > 1)
467
0
  {
468
0
    qsort(d->wrds, d->nwrds, sizeof(TheLexeme), cmpTheLexeme);
469
470
    /* uniq */
471
0
    newwrds = d->wrds;
472
0
    ptrwrds = d->wrds + 1;
473
0
    while (ptrwrds - d->wrds < d->nwrds)
474
0
    {
475
0
      if (cmpLexeme(ptrwrds, newwrds) == 0)
476
0
      {
477
0
        if (cmpLexemeInfo(ptrwrds->entries, newwrds->entries))
478
0
        {
479
0
          ptrwrds->entries->nextentry = newwrds->entries;
480
0
          newwrds->entries = ptrwrds->entries;
481
0
        }
482
0
        else
483
0
          pfree(ptrwrds->entries);
484
485
0
        if (ptrwrds->lexeme)
486
0
          pfree(ptrwrds->lexeme);
487
0
      }
488
0
      else
489
0
      {
490
0
        newwrds++;
491
0
        *newwrds = *ptrwrds;
492
0
      }
493
494
0
      ptrwrds++;
495
0
    }
496
497
0
    d->nwrds = newwrds - d->wrds + 1;
498
0
    d->wrds = repalloc_array(d->wrds, TheLexeme, d->nwrds);
499
0
  }
500
0
}
501
502
static void
503
compileTheSubstitute(DictThesaurus *d)
504
0
{
505
0
  int     i;
506
507
0
  for (i = 0; i < d->nsubst; i++)
508
0
  {
509
0
    TSLexeme   *rem = d->subst[i].res,
510
0
           *outptr,
511
0
           *inptr;
512
0
    int     n = 2;
513
514
0
    outptr = d->subst[i].res = palloc_array(TSLexeme, n);
515
0
    outptr->lexeme = NULL;
516
0
    inptr = rem;
517
518
0
    while (inptr && inptr->lexeme)
519
0
    {
520
0
      TSLexeme   *lexized,
521
0
            tmplex[2];
522
523
0
      if (inptr->flags & DT_USEASIS)
524
0
      {         /* do not lexize */
525
0
        tmplex[0] = *inptr;
526
0
        tmplex[0].flags = 0;
527
0
        tmplex[1].lexeme = NULL;
528
0
        lexized = tmplex;
529
0
      }
530
0
      else
531
0
      {
532
0
        lexized = (TSLexeme *) DatumGetPointer(FunctionCall4(&(d->subdict->lexize),
533
0
                                   PointerGetDatum(d->subdict->dictData),
534
0
                                   PointerGetDatum(inptr->lexeme),
535
0
                                   Int32GetDatum(strlen(inptr->lexeme)),
536
0
                                   PointerGetDatum(NULL)));
537
0
      }
538
539
0
      if (lexized && lexized->lexeme)
540
0
      {
541
0
        int     toset = (lexized->lexeme && outptr != d->subst[i].res) ? (outptr - d->subst[i].res) : -1;
542
543
0
        while (lexized->lexeme)
544
0
        {
545
0
          if (outptr - d->subst[i].res + 1 >= n)
546
0
          {
547
0
            int     diff = outptr - d->subst[i].res;
548
549
0
            n *= 2;
550
0
            d->subst[i].res = repalloc_array(d->subst[i].res, TSLexeme, n);
551
0
            outptr = d->subst[i].res + diff;
552
0
          }
553
554
0
          *outptr = *lexized;
555
0
          outptr->lexeme = pstrdup(lexized->lexeme);
556
557
0
          outptr++;
558
0
          lexized++;
559
0
        }
560
561
0
        if (toset > 0)
562
0
          d->subst[i].res[toset].flags |= TSL_ADDPOS;
563
0
      }
564
0
      else if (lexized)
565
0
      {
566
0
        ereport(ERROR,
567
0
            (errcode(ERRCODE_CONFIG_FILE_ERROR),
568
0
             errmsg("thesaurus substitute word \"%s\" is a stop word (rule %d)",
569
0
                inptr->lexeme, i + 1)));
570
0
      }
571
0
      else
572
0
      {
573
0
        ereport(ERROR,
574
0
            (errcode(ERRCODE_CONFIG_FILE_ERROR),
575
0
             errmsg("thesaurus substitute word \"%s\" isn't recognized by subdictionary (rule %d)",
576
0
                inptr->lexeme, i + 1)));
577
0
      }
578
579
0
      if (inptr->lexeme)
580
0
        pfree(inptr->lexeme);
581
0
      inptr++;
582
0
    }
583
584
0
    if (outptr == d->subst[i].res)
585
0
      ereport(ERROR,
586
0
          (errcode(ERRCODE_CONFIG_FILE_ERROR),
587
0
           errmsg("thesaurus substitute phrase is empty (rule %d)",
588
0
              i + 1)));
589
590
0
    d->subst[i].reslen = outptr - d->subst[i].res;
591
592
0
    pfree(rem);
593
0
  }
594
0
}
595
596
Datum
597
thesaurus_init(PG_FUNCTION_ARGS)
598
0
{
599
0
  List     *dictoptions = (List *) PG_GETARG_POINTER(0);
600
0
  DictThesaurus *d;
601
0
  char     *subdictname = NULL;
602
0
  bool    fileloaded = false;
603
0
  List     *namelist;
604
0
  ListCell   *l;
605
606
0
  d = palloc0_object(DictThesaurus);
607
608
0
  foreach(l, dictoptions)
609
0
  {
610
0
    DefElem    *defel = (DefElem *) lfirst(l);
611
612
0
    if (strcmp(defel->defname, "dictfile") == 0)
613
0
    {
614
0
      if (fileloaded)
615
0
        ereport(ERROR,
616
0
            (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
617
0
             errmsg("multiple DictFile parameters")));
618
0
      thesaurusRead(defGetString(defel), d);
619
0
      fileloaded = true;
620
0
    }
621
0
    else if (strcmp(defel->defname, "dictionary") == 0)
622
0
    {
623
0
      if (subdictname)
624
0
        ereport(ERROR,
625
0
            (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
626
0
             errmsg("multiple Dictionary parameters")));
627
0
      subdictname = pstrdup(defGetString(defel));
628
0
    }
629
0
    else
630
0
    {
631
0
      ereport(ERROR,
632
0
          (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
633
0
           errmsg("unrecognized Thesaurus parameter: \"%s\"",
634
0
              defel->defname)));
635
0
    }
636
0
  }
637
638
0
  if (!fileloaded)
639
0
    ereport(ERROR,
640
0
        (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
641
0
         errmsg("missing DictFile parameter")));
642
0
  if (!subdictname)
643
0
    ereport(ERROR,
644
0
        (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
645
0
         errmsg("missing Dictionary parameter")));
646
647
0
  namelist = stringToQualifiedNameList(subdictname, NULL);
648
0
  d->subdictOid = get_ts_dict_oid(namelist, false);
649
0
  d->subdict = lookup_ts_dictionary_cache(d->subdictOid);
650
651
0
  compileTheLexeme(d);
652
0
  compileTheSubstitute(d);
653
654
0
  PG_RETURN_POINTER(d);
655
0
}
656
657
static LexemeInfo *
658
findTheLexeme(DictThesaurus *d, char *lexeme)
659
0
{
660
0
  TheLexeme key,
661
0
         *res;
662
663
0
  if (d->nwrds == 0)
664
0
    return NULL;
665
666
0
  key.lexeme = lexeme;
667
0
  key.entries = NULL;
668
669
0
  res = bsearch(&key, d->wrds, d->nwrds, sizeof(TheLexeme), cmpLexemeQ);
670
671
0
  if (res == NULL)
672
0
    return NULL;
673
0
  return res->entries;
674
0
}
675
676
static bool
677
matchIdSubst(LexemeInfo *stored, uint32 idsubst)
678
0
{
679
0
  bool    res = true;
680
681
0
  if (stored)
682
0
  {
683
0
    res = false;
684
685
0
    for (; stored; stored = stored->nextvariant)
686
0
      if (stored->idsubst == idsubst)
687
0
      {
688
0
        res = true;
689
0
        break;
690
0
      }
691
0
  }
692
693
0
  return res;
694
0
}
695
696
static LexemeInfo *
697
findVariant(LexemeInfo *in, LexemeInfo *stored, uint16 curpos, LexemeInfo **newin, int newn)
698
0
{
699
0
  for (;;)
700
0
  {
701
0
    int     i;
702
0
    LexemeInfo *ptr = newin[0];
703
704
0
    for (i = 0; i < newn; i++)
705
0
    {
706
0
      while (newin[i] && newin[i]->idsubst < ptr->idsubst)
707
0
        newin[i] = newin[i]->nextentry;
708
709
0
      if (newin[i] == NULL)
710
0
        return in;
711
712
0
      if (newin[i]->idsubst > ptr->idsubst)
713
0
      {
714
0
        ptr = newin[i];
715
0
        i = -1;
716
0
        continue;
717
0
      }
718
719
0
      while (newin[i]->idsubst == ptr->idsubst)
720
0
      {
721
0
        if (newin[i]->posinsubst == curpos && newin[i]->tnvariant == newn)
722
0
        {
723
0
          ptr = newin[i];
724
0
          break;
725
0
        }
726
727
0
        newin[i] = newin[i]->nextentry;
728
0
        if (newin[i] == NULL)
729
0
          return in;
730
0
      }
731
732
0
      if (newin[i]->idsubst != ptr->idsubst)
733
0
      {
734
0
        ptr = newin[i];
735
0
        i = -1;
736
0
        continue;
737
0
      }
738
0
    }
739
740
0
    if (i == newn && matchIdSubst(stored, ptr->idsubst) && (in == NULL || !matchIdSubst(in, ptr->idsubst)))
741
0
    {           /* found */
742
743
0
      ptr->nextvariant = in;
744
0
      in = ptr;
745
0
    }
746
747
    /* step forward */
748
0
    for (i = 0; i < newn; i++)
749
0
      newin[i] = newin[i]->nextentry;
750
0
  }
751
0
}
752
753
static TSLexeme *
754
copyTSLexeme(TheSubstitute *ts)
755
0
{
756
0
  TSLexeme   *res;
757
0
  uint16    i;
758
759
0
  res = palloc_array(TSLexeme, ts->reslen + 1);
760
0
  for (i = 0; i < ts->reslen; i++)
761
0
  {
762
0
    res[i] = ts->res[i];
763
0
    res[i].lexeme = pstrdup(ts->res[i].lexeme);
764
0
  }
765
766
0
  res[ts->reslen].lexeme = NULL;
767
768
0
  return res;
769
0
}
770
771
static TSLexeme *
772
checkMatch(DictThesaurus *d, LexemeInfo *info, uint16 curpos, bool *moreres)
773
0
{
774
0
  *moreres = false;
775
0
  while (info)
776
0
  {
777
0
    Assert(info->idsubst < d->nsubst);
778
0
    if (info->nextvariant)
779
0
      *moreres = true;
780
0
    if (d->subst[info->idsubst].lastlexeme == curpos)
781
0
      return copyTSLexeme(d->subst + info->idsubst);
782
0
    info = info->nextvariant;
783
0
  }
784
785
0
  return NULL;
786
0
}
787
788
Datum
789
thesaurus_lexize(PG_FUNCTION_ARGS)
790
0
{
791
0
  DictThesaurus *d = (DictThesaurus *) PG_GETARG_POINTER(0);
792
0
  DictSubState *dstate = (DictSubState *) PG_GETARG_POINTER(3);
793
0
  TSLexeme   *res = NULL;
794
0
  LexemeInfo *stored,
795
0
         *info = NULL;
796
0
  uint16    curpos = 0;
797
0
  bool    moreres = false;
798
799
0
  if (PG_NARGS() != 4 || dstate == NULL)
800
0
    elog(ERROR, "forbidden call of thesaurus or nested call");
801
802
0
  if (dstate->isend)
803
0
    PG_RETURN_POINTER(NULL);
804
0
  stored = (LexemeInfo *) dstate->private_state;
805
806
0
  if (stored)
807
0
    curpos = stored->posinsubst + 1;
808
809
0
  if (!d->subdict->isvalid)
810
0
    d->subdict = lookup_ts_dictionary_cache(d->subdictOid);
811
812
0
  res = (TSLexeme *) DatumGetPointer(FunctionCall4(&(d->subdict->lexize),
813
0
                           PointerGetDatum(d->subdict->dictData),
814
0
                           PG_GETARG_DATUM(1),
815
0
                           PG_GETARG_DATUM(2),
816
0
                           PointerGetDatum(NULL)));
817
818
0
  if (res && res->lexeme)
819
0
  {
820
0
    TSLexeme   *ptr = res,
821
0
           *basevar;
822
823
0
    while (ptr->lexeme)
824
0
    {
825
0
      uint16    nv = ptr->nvariant;
826
0
      uint16    i,
827
0
            nlex = 0;
828
0
      LexemeInfo **infos;
829
830
0
      basevar = ptr;
831
0
      while (ptr->lexeme && nv == ptr->nvariant)
832
0
      {
833
0
        nlex++;
834
0
        ptr++;
835
0
      }
836
837
0
      infos = palloc_array(LexemeInfo *, nlex);
838
0
      for (i = 0; i < nlex; i++)
839
0
        if ((infos[i] = findTheLexeme(d, basevar[i].lexeme)) == NULL)
840
0
          break;
841
842
0
      if (i < nlex)
843
0
      {
844
        /* no chance to find */
845
0
        pfree(infos);
846
0
        continue;
847
0
      }
848
849
0
      info = findVariant(info, stored, curpos, infos, nlex);
850
0
    }
851
0
  }
852
0
  else if (res)
853
0
  {             /* stop-word */
854
0
    LexemeInfo *infos = findTheLexeme(d, NULL);
855
856
0
    info = findVariant(NULL, stored, curpos, &infos, 1);
857
0
  }
858
0
  else
859
0
  {
860
0
    info = NULL;     /* word isn't recognized */
861
0
  }
862
863
0
  dstate->private_state = info;
864
865
0
  if (!info)
866
0
  {
867
0
    dstate->getnext = false;
868
0
    PG_RETURN_POINTER(NULL);
869
0
  }
870
871
0
  if ((res = checkMatch(d, info, curpos, &moreres)) != NULL)
872
0
  {
873
0
    dstate->getnext = moreres;
874
0
    PG_RETURN_POINTER(res);
875
0
  }
876
877
0
  dstate->getnext = true;
878
879
  PG_RETURN_POINTER(NULL);
880
0
}