Skip to content

Commit 35f946c

Browse files
committed
fix and add index support for = array
1 parent e44bec7 commit 35f946c

File tree

3 files changed

+73
-11
lines changed

3 files changed

+73
-11
lines changed

expected/jsquery.out

+50
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,16 @@ SELECT gin_debug_query_value_path('x <@ [1,2,3]');
13031303

13041304
(1 row)
13051305

1306+
SELECT gin_debug_query_value_path('x = [1,2,3]');
1307+
gin_debug_query_value_path
1308+
----------------------------
1309+
AND +
1310+
x.# = 1 , entry 0 +
1311+
x.# = 2 , entry 1 +
1312+
x.# = 3 , entry 2 +
1313+
1314+
(1 row)
1315+
13061316
---table and index
13071317
select count(*) from test_jsquery where (v->>'review_helpful_votes')::int4 > 0;
13081318
count
@@ -1469,6 +1479,12 @@ select v from test_jsquery where v @@ 'array @> [2,3]' order by v;
14691479
{"array": [2, 3, 4]}
14701480
(3 rows)
14711481

1482+
select v from test_jsquery where v @@ 'array = [2,3]' order by v;
1483+
v
1484+
-------------------
1485+
{"array": [2, 3]}
1486+
(1 row)
1487+
14721488
create index t_idx on test_jsquery using gin (v jsonb_value_path_ops);
14731489
set enable_seqscan = off;
14741490
explain (costs off) select count(*) from test_jsquery where v @@ 'review_helpful_votes > 0';
@@ -1611,6 +1627,17 @@ explain (costs off) select v from test_jsquery where v @@ 'array @> [2,3]' order
16111627
Index Cond: (v @@ '"array" @> [2, 3]'::jsquery)
16121628
(6 rows)
16131629

1630+
explain (costs off) select v from test_jsquery where v @@ 'array = [2,3]' order by v;
1631+
QUERY PLAN
1632+
--------------------------------------------------------------
1633+
Sort
1634+
Sort Key: v
1635+
-> Bitmap Heap Scan on test_jsquery
1636+
Recheck Cond: (v @@ '"array" = [2, 3]'::jsquery)
1637+
-> Bitmap Index Scan on t_idx
1638+
Index Cond: (v @@ '"array" = [2, 3]'::jsquery)
1639+
(6 rows)
1640+
16141641
select v from test_jsquery where v @@ 'array <@ [2,3]' order by v;
16151642
v
16161643
-------------------
@@ -1636,6 +1663,12 @@ select v from test_jsquery where v @@ 'array @> [2,3]' order by v;
16361663
{"array": [2, 3, 4]}
16371664
(3 rows)
16381665

1666+
select v from test_jsquery where v @@ 'array = [2,3]' order by v;
1667+
v
1668+
-------------------
1669+
{"array": [2, 3]}
1670+
(1 row)
1671+
16391672
drop index t_idx;
16401673
create index t_idx on test_jsquery using gin (v jsonb_path_value_ops);
16411674
set enable_seqscan = off;
@@ -1779,6 +1812,17 @@ explain (costs off) select v from test_jsquery where v @@ 'array @> [2,3]' order
17791812
Index Cond: (v @@ '"array" @> [2, 3]'::jsquery)
17801813
(6 rows)
17811814

1815+
explain (costs off) select v from test_jsquery where v @@ 'array = [2,3]' order by v;
1816+
QUERY PLAN
1817+
--------------------------------------------------------------
1818+
Sort
1819+
Sort Key: v
1820+
-> Bitmap Heap Scan on test_jsquery
1821+
Recheck Cond: (v @@ '"array" = [2, 3]'::jsquery)
1822+
-> Bitmap Index Scan on t_idx
1823+
Index Cond: (v @@ '"array" = [2, 3]'::jsquery)
1824+
(6 rows)
1825+
17821826
select v from test_jsquery where v @@ 'array <@ [2,3]' order by v;
17831827
v
17841828
-------------------
@@ -1804,4 +1848,10 @@ select v from test_jsquery where v @@ 'array @> [2,3]' order by v;
18041848
{"array": [2, 3, 4]}
18051849
(3 rows)
18061850

1851+
select v from test_jsquery where v @@ 'array = [2,3]' order by v;
1852+
v
1853+
-------------------
1854+
{"array": [2, 3]}
1855+
(1 row)
1856+
18071857
RESET enable_seqscan;

jsquery_extract.c

+17-11
Original file line numberDiff line numberDiff line change
@@ -121,29 +121,35 @@ recursiveExtract(JsQueryItem *jsq, bool not, bool indirect, PathItem *path)
121121
return recursiveExtract(&elem, not, indirect, path);
122122
case jqiEqual:
123123
if (not) return NULL;
124-
result = (ExtractedNode *)palloc(sizeof(ExtractedNode));
125-
result->type = eScalar;
126-
result->hint = jsq->hint;
127-
result->path = path;
128-
result->indirect = indirect;
129-
result->bounds.inequality = false;
130-
result->bounds.exact = (JsQueryItem *)palloc(sizeof(JsQueryItem));
131-
jsqGetArg(jsq, result->bounds.exact);
132-
return result;
124+
jsqGetArg(jsq, &e);
125+
if (e.type != jqiArray)
126+
{
127+
result = (ExtractedNode *)palloc(sizeof(ExtractedNode));
128+
result->type = eScalar;
129+
result->hint = jsq->hint;
130+
result->path = path;
131+
result->indirect = indirect;
132+
result->bounds.inequality = false;
133+
result->bounds.exact = (JsQueryItem *)palloc(sizeof(JsQueryItem));
134+
*result->bounds.exact = e;
135+
return result;
136+
}
137+
/* jqiEqual with jqiArray follows */
133138
case jqiIn:
134139
case jqiOverlap:
135140
case jqiContains:
136141
case jqiContained:
137142
if (not) return NULL;
138143
result = (ExtractedNode *)palloc(sizeof(ExtractedNode));
139-
result->type = (jsq->type == jqiContains) ? eAnd : eOr;
144+
result->type = (jsq->type == jqiContains || jsq->type == jqiEqual) ? eAnd : eOr;
140145
jsqGetArg(jsq, &elem);
141146
Assert(elem.type == jqiArray);
142147
result->path = path;
143148
result->indirect = indirect;
144149
result->args.items = (ExtractedNode **)palloc(elem.array.nelems * sizeof(ExtractedNode *));
145150
result->args.count = 0;
146-
if (jsq->type == jqiContains || jsq->type == jqiOverlap || jsq->type == jqiContained)
151+
if (jsq->type == jqiContains || jsq->type == jqiOverlap || jsq->type == jqiContained ||
152+
jsq->type == jqiEqual)
147153
{
148154
pathItem = (PathItem *)palloc(sizeof(PathItem));
149155
pathItem->type = iAnyArray;

sql/jsquery.sql

+6
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ SELECT gin_debug_query_value_path('*.x = "b"');
248248
SELECT gin_debug_query_value_path('x && [1,2,3]');
249249
SELECT gin_debug_query_value_path('x @> [1,2,3]');
250250
SELECT gin_debug_query_value_path('x <@ [1,2,3]');
251+
SELECT gin_debug_query_value_path('x = [1,2,3]');
251252

252253
---table and index
253254

@@ -282,6 +283,7 @@ select count(*) from test_jsquery where v @@ 'product_group = false';
282283
select v from test_jsquery where v @@ 'array <@ [2,3]' order by v;
283284
select v from test_jsquery where v @@ 'array && [2,3]' order by v;
284285
select v from test_jsquery where v @@ 'array @> [2,3]' order by v;
286+
select v from test_jsquery where v @@ 'array = [2,3]' order by v;
285287

286288
create index t_idx on test_jsquery using gin (v jsonb_value_path_ops);
287289
set enable_seqscan = off;
@@ -309,10 +311,12 @@ select count(*) from test_jsquery where v @@ 'product_group = false';
309311
explain (costs off) select v from test_jsquery where v @@ 'array <@ [2,3]' order by v;
310312
explain (costs off) select v from test_jsquery where v @@ 'array && [2,3]' order by v;
311313
explain (costs off) select v from test_jsquery where v @@ 'array @> [2,3]' order by v;
314+
explain (costs off) select v from test_jsquery where v @@ 'array = [2,3]' order by v;
312315

313316
select v from test_jsquery where v @@ 'array <@ [2,3]' order by v;
314317
select v from test_jsquery where v @@ 'array && [2,3]' order by v;
315318
select v from test_jsquery where v @@ 'array @> [2,3]' order by v;
319+
select v from test_jsquery where v @@ 'array = [2,3]' order by v;
316320

317321
drop index t_idx;
318322

@@ -342,9 +346,11 @@ select count(*) from test_jsquery where v @@ 'product_group = false';
342346
explain (costs off) select v from test_jsquery where v @@ 'array <@ [2,3]' order by v;
343347
explain (costs off) select v from test_jsquery where v @@ 'array && [2,3]' order by v;
344348
explain (costs off) select v from test_jsquery where v @@ 'array @> [2,3]' order by v;
349+
explain (costs off) select v from test_jsquery where v @@ 'array = [2,3]' order by v;
345350

346351
select v from test_jsquery where v @@ 'array <@ [2,3]' order by v;
347352
select v from test_jsquery where v @@ 'array && [2,3]' order by v;
348353
select v from test_jsquery where v @@ 'array @> [2,3]' order by v;
354+
select v from test_jsquery where v @@ 'array = [2,3]' order by v;
349355

350356
RESET enable_seqscan;

0 commit comments

Comments
 (0)