Skip to content

Commit b70cf0f

Browse files
author
Nikita Glukhov
committed
Fix indexing of raw scalar values
1 parent 7a30b56 commit b70cf0f

File tree

4 files changed

+172
-2
lines changed

4 files changed

+172
-2
lines changed

Diff for: data/test_jsquery.data

+17
Original file line numberDiff line numberDiff line change
@@ -1015,3 +1015,20 @@
10151015
{"t": "a"}
10161016
{"t": true}
10171017
{"t": false}
1018+
[1, 2, 3]
1019+
["a", "b", "c"]
1020+
1
1021+
2
1022+
3
1023+
4
1024+
5
1025+
null
1026+
null
1027+
null
1028+
false
1029+
false
1030+
true
1031+
"aaa"
1032+
"bbb"
1033+
"ccc"
1034+
"ddd"

Diff for: expected/jsquery.out

+127-1
Original file line numberDiff line numberDiff line change
@@ -2237,6 +2237,36 @@ select count(*) from test_jsquery where v @@ 't is object';
22372237
2
22382238
(1 row)
22392239

2240+
select count(*) from test_jsquery where v @@ '$ is boolean';
2241+
count
2242+
-------
2243+
3
2244+
(1 row)
2245+
2246+
select count(*) from test_jsquery where v @@ '$ is string';
2247+
count
2248+
-------
2249+
4
2250+
(1 row)
2251+
2252+
select count(*) from test_jsquery where v @@ '$ is numeric';
2253+
count
2254+
-------
2255+
5
2256+
(1 row)
2257+
2258+
select count(*) from test_jsquery where v @@ '$ is array';
2259+
count
2260+
-------
2261+
2
2262+
(1 row)
2263+
2264+
select count(*) from test_jsquery where v @@ '$ is object';
2265+
count
2266+
-------
2267+
1017
2268+
(1 row)
2269+
22402270
select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric';
22412271
count
22422272
-------
@@ -2252,7 +2282,19 @@ select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is string';
22522282
select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $ = "0440180295")';
22532283
count
22542284
-------
2255-
23
2285+
40
2286+
(1 row)
2287+
2288+
select count(*) from test_jsquery where v @@ '$ > 2';
2289+
count
2290+
-------
2291+
3
2292+
(1 row)
2293+
2294+
select count(*) from test_jsquery where v @@ '$ = false';
2295+
count
2296+
-------
2297+
2
22562298
(1 row)
22572299

22582300
select v from test_jsquery where v @@ 'array <@ [2,3]' order by v;
@@ -2443,6 +2485,36 @@ select count(*) from test_jsquery where v @@ 't is object';
24432485
2
24442486
(1 row)
24452487

2488+
select count(*) from test_jsquery where v @@ '$ is boolean';
2489+
count
2490+
-------
2491+
3
2492+
(1 row)
2493+
2494+
select count(*) from test_jsquery where v @@ '$ is string';
2495+
count
2496+
-------
2497+
4
2498+
(1 row)
2499+
2500+
select count(*) from test_jsquery where v @@ '$ is numeric';
2501+
count
2502+
-------
2503+
5
2504+
(1 row)
2505+
2506+
select count(*) from test_jsquery where v @@ '$ is array';
2507+
count
2508+
-------
2509+
2
2510+
(1 row)
2511+
2512+
select count(*) from test_jsquery where v @@ '$ is object';
2513+
count
2514+
-------
2515+
1017
2516+
(1 row)
2517+
24462518
select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric';
24472519
count
24482520
-------
@@ -2461,6 +2533,18 @@ select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $
24612533
7
24622534
(1 row)
24632535

2536+
select count(*) from test_jsquery where v @@ '$ > 2';
2537+
count
2538+
-------
2539+
3
2540+
(1 row)
2541+
2542+
select count(*) from test_jsquery where v @@ '$ = false';
2543+
count
2544+
-------
2545+
2
2546+
(1 row)
2547+
24642548
explain (costs off) select v from test_jsquery where v @@ 'array <@ [2,3]' order by v;
24652549
QUERY PLAN
24662550
---------------------------------------------------------------
@@ -2694,6 +2778,36 @@ select count(*) from test_jsquery where v @@ 't is object';
26942778
2
26952779
(1 row)
26962780

2781+
select count(*) from test_jsquery where v @@ '$ is boolean';
2782+
count
2783+
-------
2784+
3
2785+
(1 row)
2786+
2787+
select count(*) from test_jsquery where v @@ '$ is string';
2788+
count
2789+
-------
2790+
4
2791+
(1 row)
2792+
2793+
select count(*) from test_jsquery where v @@ '$ is numeric';
2794+
count
2795+
-------
2796+
5
2797+
(1 row)
2798+
2799+
select count(*) from test_jsquery where v @@ '$ is array';
2800+
count
2801+
-------
2802+
2
2803+
(1 row)
2804+
2805+
select count(*) from test_jsquery where v @@ '$ is object';
2806+
count
2807+
-------
2808+
1017
2809+
(1 row)
2810+
26972811
select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric';
26982812
count
26992813
-------
@@ -2712,6 +2826,18 @@ select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $
27122826
7
27132827
(1 row)
27142828

2829+
select count(*) from test_jsquery where v @@ '$ > 2';
2830+
count
2831+
-------
2832+
3
2833+
(1 row)
2834+
2835+
select count(*) from test_jsquery where v @@ '$ = false';
2836+
count
2837+
-------
2838+
2
2839+
(1 row)
2840+
27152841
explain (costs off) select v from test_jsquery where v @@ 'array <@ [2,3]' order by v;
27162842
QUERY PLAN
27172843
---------------------------------------------------------------

Diff for: jsonb_gin_ops.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,8 @@ gin_extract_jsonb_value_path_internal(Jsonb *jb, int32 *nentries, uint32 **bloom
681681
switch (r)
682682
{
683683
case WJB_BEGIN_ARRAY:
684-
entries[i++] = PointerGetDatum(make_gin_key(&v, get_path_bloom(stack)));
684+
if (!v.val.array.rawScalar)
685+
entries[i++] = PointerGetDatum(make_gin_key(&v, get_path_bloom(stack)));
685686
break;
686687
case WJB_BEGIN_OBJECT:
687688
entries[i++] = PointerGetDatum(make_gin_key(&v, get_path_bloom(stack)));
@@ -1111,6 +1112,8 @@ gin_extract_jsonb_path_value_internal(Jsonb *jb, int32 *nentries)
11111112
switch (r)
11121113
{
11131114
case WJB_BEGIN_ARRAY:
1115+
if (v.val.array.rawScalar)
1116+
break;
11141117
entries[i++] = PointerGetDatum(make_gin_key(&v, stack->hash));
11151118
tmp = stack;
11161119
stack = (PathHashStack *) palloc(sizeof(PathHashStack));
@@ -1137,6 +1140,9 @@ gin_extract_jsonb_path_value_internal(Jsonb *jb, int32 *nentries)
11371140
entries[i++] = PointerGetDatum(make_gin_key(&v, stack->hash));
11381141
break;
11391142
case WJB_END_ARRAY:
1143+
if (!stack->parent)
1144+
break; /* raw scalar array */
1145+
/* fall through */
11401146
case WJB_END_OBJECT:
11411147
/* Pop the stack */
11421148
tmp = stack->parent;

Diff for: sql/jsquery.sql

+21
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,16 @@ select count(*) from test_jsquery where v @@ 't is string';
423423
select count(*) from test_jsquery where v @@ 't is numeric';
424424
select count(*) from test_jsquery where v @@ 't is array';
425425
select count(*) from test_jsquery where v @@ 't is object';
426+
select count(*) from test_jsquery where v @@ '$ is boolean';
427+
select count(*) from test_jsquery where v @@ '$ is string';
428+
select count(*) from test_jsquery where v @@ '$ is numeric';
429+
select count(*) from test_jsquery where v @@ '$ is array';
430+
select count(*) from test_jsquery where v @@ '$ is object';
426431
select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric';
427432
select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is string';
428433
select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $ = "0440180295")';
434+
select count(*) from test_jsquery where v @@ '$ > 2';
435+
select count(*) from test_jsquery where v @@ '$ = false';
429436

430437
select v from test_jsquery where v @@ 'array <@ [2,3]' order by v;
431438
select v from test_jsquery where v @@ 'array && [2,3]' order by v;
@@ -462,9 +469,16 @@ select count(*) from test_jsquery where v @@ 't is string';
462469
select count(*) from test_jsquery where v @@ 't is numeric';
463470
select count(*) from test_jsquery where v @@ 't is array';
464471
select count(*) from test_jsquery where v @@ 't is object';
472+
select count(*) from test_jsquery where v @@ '$ is boolean';
473+
select count(*) from test_jsquery where v @@ '$ is string';
474+
select count(*) from test_jsquery where v @@ '$ is numeric';
475+
select count(*) from test_jsquery where v @@ '$ is array';
476+
select count(*) from test_jsquery where v @@ '$ is object';
465477
select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric';
466478
select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is string';
467479
select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $ = "0440180295")';
480+
select count(*) from test_jsquery where v @@ '$ > 2';
481+
select count(*) from test_jsquery where v @@ '$ = false';
468482

469483
explain (costs off) select v from test_jsquery where v @@ 'array <@ [2,3]' order by v;
470484
explain (costs off) select v from test_jsquery where v @@ 'array && [2,3]' order by v;
@@ -508,9 +522,16 @@ select count(*) from test_jsquery where v @@ 't is string';
508522
select count(*) from test_jsquery where v @@ 't is numeric';
509523
select count(*) from test_jsquery where v @@ 't is array';
510524
select count(*) from test_jsquery where v @@ 't is object';
525+
select count(*) from test_jsquery where v @@ '$ is boolean';
526+
select count(*) from test_jsquery where v @@ '$ is string';
527+
select count(*) from test_jsquery where v @@ '$ is numeric';
528+
select count(*) from test_jsquery where v @@ '$ is array';
529+
select count(*) from test_jsquery where v @@ '$ is object';
511530
select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is numeric';
512531
select count(*) from test_jsquery where v @@ 'similar_product_ids.#: is string';
513532
select count(*) from test_jsquery where v @@ 'NOT similar_product_ids.#: (NOT $ = "0440180295")';
533+
select count(*) from test_jsquery where v @@ '$ > 2';
534+
select count(*) from test_jsquery where v @@ '$ = false';
514535

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

0 commit comments

Comments
 (0)