Skip to content

Commit 9a09f20

Browse files
author
Nikita Glukhov
committed
Extract 'EXISTS(left) && EXISTS(right)' from jsonpath 'left OP right'
# Conflicts: # sql/jsquery.sql
1 parent e3d9a09 commit 9a09f20

File tree

3 files changed

+90
-5
lines changed

3 files changed

+90
-5
lines changed

expected/jsquery.out

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,6 +2823,24 @@ select gin_debug_jsonpath_value_path('$.a > 1');
28232823

28242824
(1 row)
28252825

2826+
SELECT gin_debug_jsonpath_value_path('lax $.a == $.b');
2827+
gin_debug_jsonpath_value_path
2828+
-------------------------------
2829+
AND +
2830+
a = * , entry 0 +
2831+
b = * , entry 1 +
2832+
2833+
(1 row)
2834+
2835+
SELECT gin_debug_jsonpath_value_path('strict $.a == $.b');
2836+
gin_debug_jsonpath_value_path
2837+
-------------------------------
2838+
AND +
2839+
a = * , entry 0 +
2840+
b = * , entry 1 +
2841+
2842+
(1 row)
2843+
28262844
SELECT gin_debug_jsonpath_path_value('lax $');
28272845
gin_debug_jsonpath_path_value
28282846
-------------------------------
@@ -3954,6 +3972,32 @@ SELECT gin_debug_jsonpath_path_value('strict $.a > 1 && $.a < 2 && $.b >= 3 && $
39543972

39553973
(1 row)
39563974

3975+
SELECT gin_debug_jsonpath_path_value('lax $.a == $.b');
3976+
gin_debug_jsonpath_path_value
3977+
-------------------------------
3978+
AND +
3979+
OR +
3980+
#.a = * , entry 0 +
3981+
#.a.# = * , entry 1 +
3982+
a = * , entry 2 +
3983+
a.# = * , entry 3 +
3984+
OR +
3985+
#.b = * , entry 4 +
3986+
#.b.# = * , entry 5 +
3987+
b = * , entry 6 +
3988+
b.# = * , entry 7 +
3989+
3990+
(1 row)
3991+
3992+
SELECT gin_debug_jsonpath_path_value('strict $.a == $.b');
3993+
gin_debug_jsonpath_path_value
3994+
-------------------------------
3995+
AND +
3996+
a = * , entry 0 +
3997+
b = * , entry 1 +
3998+
3999+
(1 row)
4000+
39574001
SELECT gin_debug_jsonpath_laxpath_value('lax $');
39584002
gin_debug_jsonpath_laxpath_value
39594003
----------------------------------
@@ -4232,6 +4276,24 @@ SELECT gin_debug_jsonpath_laxpath_value('strict $.a > 1 && $.a < 2 && $.b >= 3 &
42324276

42334277
(1 row)
42344278

4279+
SELECT gin_debug_jsonpath_laxpath_value('lax $.a == $.b');
4280+
gin_debug_jsonpath_laxpath_value
4281+
----------------------------------
4282+
AND +
4283+
a = * , entry 0 +
4284+
b = * , entry 1 +
4285+
4286+
(1 row)
4287+
4288+
SELECT gin_debug_jsonpath_laxpath_value('strict $.a == $.b');
4289+
gin_debug_jsonpath_laxpath_value
4290+
----------------------------------
4291+
AND +
4292+
a = * , entry 0 +
4293+
b = * , entry 1 +
4294+
4295+
(1 row)
4296+
42354297
---table and index
42364298
select count(*) from test_jsquery where (v->>'review_helpful_votes')::int4 > 0;
42374299
count

jsquery_extract.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -551,13 +551,14 @@ appendJsonPathExprNode(ExtractedNode *result, ExtractedNode *node, PathItem *pat
551551
}
552552

553553
static ExtractedNode *
554-
extractJsonPathExists(JsonPathItem *jpi, int flags, bool indirect, PathItem *path)
554+
extractJsonPathExists(JsonPathItem *jpi, int flags, bool indirect, bool unwrap,
555+
PathItem *path)
555556
{
556557
List *paths;
557558
ListCell *lc;
558559
ExtractedNode *result;
559560

560-
if (!(paths = extractJsonPath(jpi, flags, false, path)))
561+
if (!(paths = extractJsonPath(jpi, flags, unwrap, path)))
561562
return NULL;
562563

563564
result = NULL;
@@ -668,7 +669,20 @@ extractJsonPathExpr(JsonPathItem *jpi, int flags, bool not, bool indirect,
668669
greater = !greater;
669670
}
670671
else
671-
return NULL;
672+
{
673+
ExtractedNode *lnode;
674+
ExtractedNode *rnode;
675+
676+
lnode = extractJsonPathExists(&larg, flags, indirect, true, path);
677+
rnode = extractJsonPathExists(&rarg, flags, indirect, true, path);
678+
679+
if (lnode && rnode)
680+
return makeBinaryNode(eAnd, path, indirect, lnode, rnode);
681+
else if (lnode)
682+
return lnode;
683+
else
684+
return rnode;
685+
}
672686

673687
if (!(paths = extractJsonPath(patharg, flags, true, path)))
674688
return NULL;
@@ -755,7 +769,7 @@ extractJsonPathExpr(JsonPathItem *jpi, int flags, bool not, bool indirect,
755769

756770
jspGetArg(jpi, &elem);
757771

758-
return extractJsonPathExists(&elem, flags, indirect, path);
772+
return extractJsonPathExists(&elem, flags, indirect, false, path);
759773
}
760774

761775
default:
@@ -1374,7 +1388,7 @@ extractJsonPathQuery(JsonPath *jp, bool exists, bool arrayPathItems, int optimiz
13741388

13751389
jspInit(&jsp, jp);
13761390
root = exists
1377-
? extractJsonPathExists(&jsp, flags, false, NULL)
1391+
? extractJsonPathExists(&jsp, flags, false, false, NULL)
13781392
: extractJsonPathExpr(&jsp, flags, false, false, NULL);
13791393

13801394
return emitExtractedQuery(root, optimize, makeHandler, checkHandler, extra);

sql/jsquery.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,9 @@ select gin_debug_jsonpath_value_path('strict exists($.a.b ? (@.c.d == 1))');
510510

511511
select gin_debug_jsonpath_value_path('$.a > 1');
512512

513+
SELECT gin_debug_jsonpath_value_path('lax $.a == $.b');
514+
SELECT gin_debug_jsonpath_value_path('strict $.a == $.b');
515+
513516
SELECT gin_debug_jsonpath_path_value('lax $');
514517
SELECT gin_debug_jsonpath_path_value('strict $');
515518
SELECT gin_debug_jsonpath_path_value('lax $.a');
@@ -553,6 +556,9 @@ select gin_debug_jsonpath_path_value('lax $.a[*].b[*] == 1');
553556
SELECT gin_debug_jsonpath_path_value('lax $.a > 1 && $.a < 2 && $.b >= 3 && $.c <= "aaa"');
554557
SELECT gin_debug_jsonpath_path_value('strict $.a > 1 && $.a < 2 && $.b >= 3 && $.c <= "aaa"');
555558

559+
SELECT gin_debug_jsonpath_path_value('lax $.a == $.b');
560+
SELECT gin_debug_jsonpath_path_value('strict $.a == $.b');
561+
556562
SELECT gin_debug_jsonpath_laxpath_value('lax $');
557563
SELECT gin_debug_jsonpath_laxpath_value('strict $');
558564
SELECT gin_debug_jsonpath_laxpath_value('lax $.a');
@@ -595,6 +601,9 @@ select gin_debug_jsonpath_laxpath_value('lax $.a[*].b[*] == 1');
595601
SELECT gin_debug_jsonpath_laxpath_value('lax $.a > 1 && $.a < 2 && $.b >= 3 && $.c <= "aaa"');
596602
SELECT gin_debug_jsonpath_laxpath_value('strict $.a > 1 && $.a < 2 && $.b >= 3 && $.c <= "aaa"');
597603

604+
SELECT gin_debug_jsonpath_laxpath_value('lax $.a == $.b');
605+
SELECT gin_debug_jsonpath_laxpath_value('strict $.a == $.b');
606+
598607
---table and index
599608

600609
select count(*) from test_jsquery where (v->>'review_helpful_votes')::int4 > 0;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy