Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8362a5e

Browse files
author
Alexander Korotkov
committed
Handle filters in indexes
From index perspective, filter is basically the same as AND operator.
1 parent 5bf859c commit 8362a5e

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

jsquery_extract.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,25 @@ recursiveExtract(JsQueryItem *jsq, bool not, bool indirect, PathItem *path)
5656
{
5757
case jqiAnd:
5858
case jqiOr:
59-
type = ((jsq->type == jqiAnd) == not) ? eOr : eAnd;
59+
case jqiFilter:
60+
type = ((jsq->type == jqiAnd || jsq->type == jqiFilter) == not) ? eOr : eAnd;
6061

61-
jsqGetLeftArg(jsq, &elem);
62-
leftNode = recursiveExtract(&elem, not, false, path);
63-
jsqGetRightArg(jsq, &elem);
64-
rightNode = recursiveExtract(&elem, not, false, path);
62+
if (jsq->type == jqiFilter)
63+
{
64+
jsqGetArg(jsq, &elem);
65+
leftNode = recursiveExtract(&elem, not, false, path);
66+
if (jsqGetNext(jsq, &elem))
67+
rightNode = recursiveExtract(&elem, not, false, path);
68+
else
69+
rightNode = makeAnyNode(not, false, path);
70+
}
71+
else
72+
{
73+
jsqGetLeftArg(jsq, &elem);
74+
leftNode = recursiveExtract(&elem, not, false, path);
75+
jsqGetRightArg(jsq, &elem);
76+
rightNode = recursiveExtract(&elem, not, false, path);
77+
}
6578

6679
if (!leftNode || !rightNode)
6780
{
@@ -137,8 +150,6 @@ recursiveExtract(JsQueryItem *jsq, bool not, bool indirect, PathItem *path)
137150
if (!jsqGetNext(jsq, &elem))
138151
return makeAnyNode(not, indirect, pathItem);
139152
return recursiveExtract(&elem, not, true, pathItem);
140-
case jqiFilter:
141-
/* ignore filter for now */
142153
case jqiCurrent:
143154
if (!jsqGetNext(jsq, &elem))
144155
return makeAnyNode(not, indirect, path);

sql/jsquery.sql

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ SELECT gin_debug_query_path_value('#:(x=1) AND %:(y=1) AND *:(z=1)');
418418
SELECT gin_debug_query_path_value('#:(NOT x=1) AND %:(NOT y=1) AND *:(NOT z=1)');
419419
SELECT gin_debug_query_path_value('NOT #:(NOT x=1) AND NOT %:(NOT y=1) AND NOT *:(NOT z=1)');
420420
SELECT gin_debug_query_path_value('$ = true');
421+
SELECT gin_debug_query_path_value('$ . ? (review_votes > 10) . review_rating < 7');
422+
SELECT gin_debug_query_path_value('similar_product_ids . ? (# = "B0002W4TL2") . $');
421423

422424
SELECT gin_debug_query_value_path('NOT NOT NOT x(y(NOT (a=1) and NOT (b=2)) OR NOT NOT (c=3)) and z = 5');
423425
SELECT gin_debug_query_value_path('NOT #(x=1) and NOT *(y=1) and NOT %(z=1) ');
@@ -445,7 +447,9 @@ SELECT gin_debug_query_value_path('NOT #:(NOT x=1) AND NOT %:(NOT y=1) AND NOT *
445447
SELECT gin_debug_query_value_path('(@# > 0 and #: = 16)');
446448
SELECT gin_debug_query_value_path('*.@# ($ = 4 or $ = 2)');
447449
SELECT gin_debug_query_value_path('tags.#.term. ? ( # = "NYC").x > 0');
448-
SELECT gin_debug_query_path_value('$ = true');
450+
SELECT gin_debug_query_value_path('$ = true');
451+
SELECT gin_debug_query_value_path('$ . ? (review_votes > 10) . review_rating < 7');
452+
SELECT gin_debug_query_value_path('similar_product_ids . ? (# = "B0002W4TL2") . $');
449453

450454
---table and index
451455

@@ -497,6 +501,8 @@ select count(*) from test_jsquery where v @@ '$ = false';
497501
select count(*) from test_jsquery where v @@ 't';
498502
select count(*) from test_jsquery where v @@ '$';
499503
select count(*) from test_jsquery where v @@ 'similar_product_ids.#';
504+
select count(*) from test_jsquery where v @@ '$ . ? (review_votes > 10) . review_rating < 7';
505+
select count(*) from test_jsquery where v @@ 'similar_product_ids . ? (# = "B0002W4TL2") . $';
500506

501507
select v from test_jsquery where v @@ 'array <@ [2,3]' order by v;
502508
select v from test_jsquery where v @@ 'array && [2,3]' order by v;
@@ -546,6 +552,8 @@ select count(*) from test_jsquery where v @@ '$ = false';
546552
select count(*) from test_jsquery where v @@ 't';
547553
select count(*) from test_jsquery where v @@ '$';
548554
select count(*) from test_jsquery where v @@ 'similar_product_ids.#';
555+
select count(*) from test_jsquery where v @@ '$ . ? (review_votes > 10) . review_rating < 7';
556+
select count(*) from test_jsquery where v @@ 'similar_product_ids . ? (# = "B0002W4TL2") . $';
549557

550558
explain (costs off) select v from test_jsquery where v @@ 'array <@ [2,3]' order by v;
551559
explain (costs off) select v from test_jsquery where v @@ 'array && [2,3]' order by v;
@@ -602,6 +610,8 @@ select count(*) from test_jsquery where v @@ '$ = false';
602610
select count(*) from test_jsquery where v @@ 't';
603611
select count(*) from test_jsquery where v @@ '$';
604612
select count(*) from test_jsquery where v @@ 'similar_product_ids.#';
613+
select count(*) from test_jsquery where v @@ '$ . ? (review_votes > 10) . review_rating < 7';
614+
select count(*) from test_jsquery where v @@ 'similar_product_ids . ? (# = "B0002W4TL2") . $';
605615

606616
explain (costs off) select v from test_jsquery where v @@ 'array <@ [2,3]' order by v;
607617
explain (costs off) select v from test_jsquery where v @@ 'array && [2,3]' order by v;

0 commit comments

Comments
 (0)