@@ -149,6 +149,8 @@ uniqueWORD(ParsedWord *a, int32 l)
149
149
150
150
/*
151
151
* make value of tsvector, given parsed text
152
+ *
153
+ * Note: frees prs->words and subsidiary data.
152
154
*/
153
155
TSVector
154
156
make_tsvector (ParsedText * prs )
@@ -162,7 +164,11 @@ make_tsvector(ParsedText *prs)
162
164
char * str ;
163
165
int stroff ;
164
166
165
- prs -> curwords = uniqueWORD (prs -> words , prs -> curwords );
167
+ /* Merge duplicate words */
168
+ if (prs -> curwords > 0 )
169
+ prs -> curwords = uniqueWORD (prs -> words , prs -> curwords );
170
+
171
+ /* Determine space needed */
166
172
for (i = 0 ; i < prs -> curwords ; i ++ )
167
173
{
168
174
lenstr += prs -> words [i ].len ;
@@ -217,7 +223,10 @@ make_tsvector(ParsedText *prs)
217
223
ptr -> haspos = 0 ;
218
224
ptr ++ ;
219
225
}
220
- pfree (prs -> words );
226
+
227
+ if (prs -> words )
228
+ pfree (prs -> words );
229
+
221
230
return in ;
222
231
}
223
232
@@ -231,26 +240,19 @@ to_tsvector_byid(PG_FUNCTION_ARGS)
231
240
232
241
prs .lenwords = VARSIZE_ANY_EXHDR (in ) / 6 ; /* just estimation of word's
233
242
* number */
234
- if (prs .lenwords == 0 )
243
+ if (prs .lenwords < 2 )
235
244
prs .lenwords = 2 ;
236
245
prs .curwords = 0 ;
237
246
prs .pos = 0 ;
238
247
prs .words = (ParsedWord * ) palloc (sizeof (ParsedWord ) * prs .lenwords );
239
248
240
249
parsetext (cfgId , & prs , VARDATA_ANY (in ), VARSIZE_ANY_EXHDR (in ));
250
+
241
251
PG_FREE_IF_COPY (in , 1 );
242
252
243
- if (prs .curwords )
244
- out = make_tsvector (& prs );
245
- else
246
- {
247
- pfree (prs .words );
248
- out = palloc (CALCDATASIZE (0 , 0 ));
249
- SET_VARSIZE (out , CALCDATASIZE (0 , 0 ));
250
- out -> size = 0 ;
251
- }
253
+ out = make_tsvector (& prs );
252
254
253
- PG_RETURN_POINTER (out );
255
+ PG_RETURN_TSVECTOR (out );
254
256
}
255
257
256
258
Datum
@@ -281,21 +283,10 @@ jsonb_to_tsvector_byid(PG_FUNCTION_ARGS)
281
283
282
284
iterate_jsonb_string_values (jb , & state , add_to_tsvector );
283
285
284
- if (prs .curwords > 0 )
285
- result = make_tsvector (& prs );
286
- else
287
- {
288
- /*
289
- * There weren't any string elements in jsonb, so we need to return an
290
- * empty vector
291
- */
292
- result = palloc (CALCDATASIZE (0 , 0 ));
293
- SET_VARSIZE (result , CALCDATASIZE (0 , 0 ));
294
- result -> size = 0 ;
295
- }
296
-
297
286
PG_FREE_IF_COPY (jb , 1 );
298
287
288
+ result = make_tsvector (& prs );
289
+
299
290
PG_RETURN_TSVECTOR (result );
300
291
}
301
292
@@ -327,21 +318,10 @@ json_to_tsvector_byid(PG_FUNCTION_ARGS)
327
318
328
319
iterate_json_string_values (json , & state , add_to_tsvector );
329
320
330
- if (prs .curwords > 0 )
331
- result = make_tsvector (& prs );
332
- else
333
- {
334
- /*
335
- * There weren't any string elements in json, so we need to return an
336
- * empty vector
337
- */
338
- result = palloc (CALCDATASIZE (0 , 0 ));
339
- SET_VARSIZE (result , CALCDATASIZE (0 , 0 ));
340
- result -> size = 0 ;
341
- }
342
-
343
321
PG_FREE_IF_COPY (json , 1 );
344
322
323
+ result = make_tsvector (& prs );
324
+
345
325
PG_RETURN_TSVECTOR (result );
346
326
}
347
327
0 commit comments