Skip to content

Commit 4012393

Browse files
committed
Correct fallback if not positive quals.
1 parent 1cfc4b1 commit 4012393

File tree

2 files changed

+66
-14
lines changed

2 files changed

+66
-14
lines changed

jsonb_gin_ops.c

+28-14
Original file line numberDiff line numberDiff line change
@@ -657,13 +657,20 @@ gin_extract_jsonb_query_bloom_value(PG_FUNCTION_ARGS)
657657
case JsQueryMatchStrategyNumber:
658658
jq = PG_GETARG_JSQUERY(0);
659659
root = extractJsQuery(jq, make_bloom_entry_handler, (Pointer)&e);
660-
661-
*nentries = e.count;
662-
entries = e.entries;
663-
*pmatch = e.partial_match;
664-
*extra_data = e.extra_data;
665-
for (i = 0; i < e.count; i++)
666-
((KeyExtra *)e.extra_data[i])->root = root;
660+
if (root)
661+
{
662+
*nentries = e.count;
663+
entries = e.entries;
664+
*pmatch = e.partial_match;
665+
*extra_data = e.extra_data;
666+
for (i = 0; i < e.count; i++)
667+
((KeyExtra *)e.extra_data[i])->root = root;
668+
}
669+
else
670+
{
671+
entries = NULL;
672+
*nentries = 0;
673+
}
667674
break;
668675

669676
default:
@@ -1071,13 +1078,20 @@ gin_extract_jsonb_query_hash_value(PG_FUNCTION_ARGS)
10711078
case JsQueryMatchStrategyNumber:
10721079
jq = PG_GETARG_JSQUERY(0);
10731080
root = extractJsQuery(jq, make_hash_entry_handler, (Pointer)&e);
1074-
1075-
*nentries = e.count;
1076-
entries = e.entries;
1077-
*pmatch = e.partial_match;
1078-
*extra_data = e.extra_data;
1079-
for (i = 0; i < e.count; i++)
1080-
((KeyExtra *)e.extra_data[i])->root = root;
1081+
if (root)
1082+
{
1083+
*nentries = e.count;
1084+
entries = e.entries;
1085+
*pmatch = e.partial_match;
1086+
*extra_data = e.extra_data;
1087+
for (i = 0; i < e.count; i++)
1088+
((KeyExtra *)e.extra_data[i])->root = root;
1089+
}
1090+
else
1091+
{
1092+
entries = NULL;
1093+
*nentries = 0;
1094+
}
10811095
break;
10821096

10831097
default:

jsquery_extract.c

+38
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,42 @@ makeEntries(ExtractedNode *node, MakeEntryHandler handler, Pointer extra)
568568
}
569569
}
570570

571+
static bool
572+
queryHasPositive(ExtractedNode *node)
573+
{
574+
int i;
575+
bool result;
576+
switch(node->type)
577+
{
578+
case eAnd:
579+
result = false;
580+
for (i = 0; i < node->args.count; i++)
581+
{
582+
if (queryHasPositive(node->args.items[i]))
583+
{
584+
result = true;
585+
break;
586+
}
587+
}
588+
return result;
589+
case eOr:
590+
result = true;
591+
for (i = 0; i < node->args.count; i++)
592+
{
593+
if (!queryHasPositive(node->args.items[i]))
594+
{
595+
result = false;
596+
break;
597+
}
598+
}
599+
return result;
600+
case eNot:
601+
return !queryHasPositive(node->args.items[0]);
602+
case eScalar:
603+
return true;
604+
}
605+
}
606+
571607
ExtractedNode *
572608
extractJsQuery(JsQuery *jq, MakeEntryHandler handler, Pointer extra)
573609
{
@@ -580,6 +616,8 @@ extractJsQuery(JsQuery *jq, MakeEntryHandler handler, Pointer extra)
580616
simplifyRecursive(root);
581617
root = makeEntries(root, handler, extra);
582618
}
619+
if (root && !queryHasPositive(root))
620+
root = NULL;
583621
return root;
584622
}
585623

0 commit comments

Comments
 (0)