@@ -149,6 +149,41 @@ calc_wraps(QueryItemWrap *wrap, int *num)
149
149
return result ;
150
150
}
151
151
152
+ static bool
153
+ check_allnegative (QueryItemWrap * wrap )
154
+ {
155
+ if (wrap -> type == QI_VAL )
156
+ {
157
+ return wrap -> not ;
158
+ }
159
+ else if (wrap -> oper == OP_AND )
160
+ {
161
+ int i ;
162
+ for (i = 0 ; i < wrap -> operandsCount ; i ++ )
163
+ {
164
+ if (!check_allnegative (& wrap -> operands [i ]))
165
+ return false;
166
+ }
167
+ return true;
168
+ }
169
+ else if (wrap -> oper == OP_OR )
170
+ {
171
+ int i ;
172
+ for (i = 0 ; i < wrap -> operandsCount ; i ++ )
173
+ {
174
+ if (check_allnegative (& wrap -> operands [i ]))
175
+ return true;
176
+ }
177
+ return false;
178
+ }
179
+ else
180
+ {
181
+ elog (ERROR , "check_allnegative: invalid node" );
182
+ return false;
183
+ }
184
+
185
+ }
186
+
152
187
#define MAX_ENCODED_LEN 5
153
188
154
189
/*
@@ -221,12 +256,12 @@ extract_wraps(QueryItemWrap *wrap, ExtractContext *context, int level)
221
256
{
222
257
if (wrap -> type == QI_VAL )
223
258
{
224
- bytea * addinfo = (bytea * ) palloc (VARHDRSZ + level * MAX_ENCODED_LEN );
259
+ bytea * addinfo = (bytea * ) palloc (VARHDRSZ + Max ( level , 1 ) * MAX_ENCODED_LEN );
225
260
unsigned char * ptr = (unsigned char * )VARDATA (addinfo );
226
261
int index = context -> index ;
227
262
228
263
context -> entries [index ] = PointerGetDatum (cstring_to_text_with_len (context -> operand + wrap -> distance , wrap -> length ));
229
- elog (NOTICE , "%s" , text_to_cstring (DatumGetPointer (context -> entries [index ])));
264
+ elog (NOTICE , "%s" , text_to_cstring (DatumGetTextP (context -> entries [index ])));
230
265
231
266
while (wrap -> parent )
232
267
{
@@ -243,6 +278,11 @@ extract_wraps(QueryItemWrap *wrap, ExtractContext *context, int level)
243
278
encode_varbyte (sum , & ptr );
244
279
wrap = parent ;
245
280
}
281
+ if (level == 0 && wrap -> not )
282
+ {
283
+ encode_varbyte (1 , & ptr );
284
+ encode_varbyte (4 | 1 , & ptr );
285
+ }
246
286
SET_VARSIZE (addinfo , ptr - (unsigned char * )addinfo );
247
287
248
288
context -> addInfo [index ] = PointerGetDatum (addinfo );
@@ -278,20 +318,34 @@ ruminv_extract_tsquery(PG_FUNCTION_ARGS)
278
318
{
279
319
TSQuery query = PG_GETARG_TSQUERY (0 );
280
320
int32 * nentries = (int32 * ) PG_GETARG_POINTER (1 );
321
+ bool * * nullFlags = (bool * * ) PG_GETARG_POINTER (2 );
281
322
Datum * * addInfo = (Datum * * ) PG_GETARG_POINTER (3 );
282
323
bool * * addInfoIsNull = (bool * * ) PG_GETARG_POINTER (4 );
283
324
Datum * entries = NULL ;
284
325
QueryItem * item = GETQUERY (query );
285
326
QueryItemWrap * wrap ;
286
327
ExtractContext context ;
287
328
int num = 1 ;
329
+ bool extractNull ;
288
330
289
331
wrap = make_query_item_wrap (item , NULL , false);
290
332
* nentries = calc_wraps (wrap , & num );
333
+ extractNull = check_allnegative (wrap );
334
+ if (extractNull )
335
+ (* nentries )++ ;
291
336
292
337
entries = (Datum * ) palloc (sizeof (Datum ) * (* nentries ));
293
338
* addInfo = (Datum * ) palloc (sizeof (Datum ) * (* nentries ));
294
339
* addInfoIsNull = (bool * ) palloc (sizeof (bool ) * (* nentries ));
340
+ if (extractNull )
341
+ {
342
+ int i ;
343
+ * nullFlags = (bool * ) palloc (sizeof (bool ) * (* nentries ));
344
+ for (i = 0 ; i < * nentries - 1 ; i ++ )
345
+ (* nullFlags )[i ] = false;
346
+ (* nullFlags )[* nentries - 1 ] = true;
347
+ (* addInfoIsNull )[* nentries - 1 ] = true;
348
+ }
295
349
296
350
context .addInfo = * addInfo ;
297
351
context .addInfoIsNull = * addInfoIsNull ;
@@ -322,29 +376,32 @@ ruminv_extract_tsvector(PG_FUNCTION_ARGS)
322
376
bool * * ptr_partialmatch = (bool * * ) PG_GETARG_POINTER (3 );
323
377
Pointer * * extra_data = (Pointer * * ) PG_GETARG_POINTER (4 );
324
378
325
- /* bool **nullFlags = (bool **) PG_GETARG_POINTER(5); */
379
+ bool * * nullFlags = (bool * * ) PG_GETARG_POINTER (5 );
326
380
int32 * searchMode = (int32 * ) PG_GETARG_POINTER (6 );
327
381
Datum * entries = NULL ;
328
382
329
- * nentries = vector -> size ;
383
+ * nentries = vector -> size + 1 ;
384
+ * extra_data = NULL ;
385
+ * ptr_partialmatch = NULL ;
386
+ * searchMode = GIN_SEARCH_MODE_DEFAULT ;
387
+
388
+ entries = (Datum * ) palloc (sizeof (Datum ) * (* nentries ));
389
+ * nullFlags = (bool * ) palloc (sizeof (bool ) * (* nentries ));
330
390
if (vector -> size > 0 )
331
391
{
332
392
int i ;
333
393
WordEntry * we = ARRPTR (vector );
334
394
335
- * extra_data = NULL ;
336
- * ptr_partialmatch = NULL ;
337
- * searchMode = GIN_SEARCH_MODE_DEFAULT ;
338
-
339
- entries = (Datum * ) palloc (sizeof (Datum ) * vector -> size );
340
395
for (i = 0 ; i < vector -> size ; i ++ )
341
396
{
342
397
text * txt ;
343
398
344
399
txt = cstring_to_text_with_len (STRPTR (vector ) + we [i ].pos , we [i ].len );
345
400
entries [i ] = PointerGetDatum (txt );
401
+ (* nullFlags )[i ] = false;
346
402
}
347
403
}
404
+ (* nullFlags )[* nentries - 1 ] = true;
348
405
PG_FREE_IF_COPY (vector , 0 );
349
406
PG_RETURN_POINTER (entries );
350
407
}
@@ -368,14 +425,15 @@ ruminv_tsvector_consistent(PG_FUNCTION_ARGS)
368
425
bool * recheck = (bool * ) PG_GETARG_POINTER (5 );
369
426
Datum * addInfo = (Datum * ) PG_GETARG_POINTER (8 );
370
427
bool * addInfoIsNull = (bool * ) PG_GETARG_POINTER (9 );
371
- bool res = false;
428
+ bool res = false,
429
+ allFalse = true;
372
430
int i ,
373
431
lastIndex = 0 ;
374
432
TmpNode nodes [256 ];
375
433
376
434
* recheck = false;
377
435
378
- for (i = 0 ; i < nkeys ; i ++ )
436
+ for (i = 0 ; i < nkeys - 1 ; i ++ )
379
437
{
380
438
unsigned char * ptr ,
381
439
* ptrEnd ;
@@ -385,6 +443,8 @@ ruminv_tsvector_consistent(PG_FUNCTION_ARGS)
385
443
if (!check [i ])
386
444
continue ;
387
445
446
+ allFalse = false;
447
+
388
448
if (addInfoIsNull [i ])
389
449
elog (ERROR , "Unexpected addInfoIsNull" );
390
450
@@ -445,26 +505,33 @@ ruminv_tsvector_consistent(PG_FUNCTION_ARGS)
445
505
}
446
506
}
447
507
448
- /* for (i = 0; i < lastIndex; i++ )
508
+ if ( allFalse && check [ nkeys - 1 ] )
449
509
{
450
- elog(NOTICE, "s %d %d %d %d", i, nodes[i].sum, nodes[i].parent, nodes[i].not);
451
- }*/
452
-
453
- for (i = lastIndex - 1 ; i >= 0 ; i -- )
510
+ res = true;
511
+ }
512
+ else
454
513
{
455
- if ( nodes [ i ]. parent != -2 )
514
+ /* for (i = 0; i < lastIndex; i++ )
456
515
{
457
- if (nodes [i ].sum > 0 )
516
+ elog(NOTICE, "s %d %d %d %d", i, nodes[i].sum, nodes[i].parent, nodes[i].not);
517
+ }*/
518
+
519
+ for (i = lastIndex - 1 ; i >= 0 ; i -- )
520
+ {
521
+ if (nodes [i ].parent != -2 )
458
522
{
459
- if (nodes [i ].parent == -1 )
460
- {
461
- res = true;
462
- break ;
463
- }
464
- else
523
+ if (nodes [i ].sum > 0 )
465
524
{
466
- int parent = nodes [i ].parent ;
467
- nodes [parent ].sum += nodes [i ].not ? -1 : 1 ;
525
+ if (nodes [i ].parent == -1 )
526
+ {
527
+ res = true;
528
+ break ;
529
+ }
530
+ else
531
+ {
532
+ int parent = nodes [i ].parent ;
533
+ nodes [parent ].sum += nodes [i ].not ? -1 : 1 ;
534
+ }
468
535
}
469
536
}
470
537
}
0 commit comments