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

Skip to content

Commit 60c0c71

Browse files
author
Nikita Glukhov
committed
Add jsonpath @@ support
1 parent adece56 commit 60c0c71

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

jsonb_gin_ops.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ typedef struct
6161
#define JsonbNestedContainsStrategyNumber 13
6262
#define JsQueryMatchStrategyNumber 14
6363
#define JsonpathExistsStrategyNumber 15
64+
#define JsonpathMatchStrategyNumber 16
6465

6566
typedef struct
6667
{
@@ -587,7 +588,8 @@ gin_compare_partial_jsonb_value_path(PG_FUNCTION_ARGS)
587588
int32 result;
588589

589590
if (strategy == JsQueryMatchStrategyNumber ||
590-
strategy == JsonpathExistsStrategyNumber)
591+
strategy == JsonpathExistsStrategyNumber ||
592+
strategy == JsonpathMatchStrategyNumber)
591593
{
592594
KeyExtra *extra = (KeyExtra *)PG_GETARG_POINTER(3);
593595
ExtractedNode *node = extra->node;
@@ -809,8 +811,10 @@ gin_extract_jsonb_query_value_path(PG_FUNCTION_ARGS)
809811
case JsQueryMatchStrategyNumber:
810812
#ifndef NO_JSONPATH
811813
case JsonpathExistsStrategyNumber:
812-
if (strategy == JsonpathExistsStrategyNumber)
814+
case JsonpathMatchStrategyNumber:
815+
if (strategy != JsQueryMatchStrategyNumber)
813816
root = extractJsonPath(PG_GETARG_JSONPATH_P(0),
817+
strategy == JsonpathExistsStrategyNumber,
814818
make_value_path_entry_handler,
815819
check_value_path_entry_handler,
816820
(Pointer)&e);
@@ -878,6 +882,7 @@ gin_consistent_jsonb_value_path(PG_FUNCTION_ARGS)
878882

879883
case JsQueryMatchStrategyNumber:
880884
case JsonpathExistsStrategyNumber:
885+
case JsonpathMatchStrategyNumber:
881886
if (nkeys == 0)
882887
res = true;
883888
else
@@ -940,6 +945,7 @@ gin_triconsistent_jsonb_value_path(PG_FUNCTION_ARGS)
940945

941946
case JsQueryMatchStrategyNumber:
942947
case JsonpathExistsStrategyNumber:
948+
case JsonpathMatchStrategyNumber:
943949
if (nkeys == 0)
944950
res = GIN_MAYBE;
945951
else
@@ -1063,7 +1069,8 @@ gin_compare_partial_jsonb_path_value(PG_FUNCTION_ARGS)
10631069
result = (key->hash > partial_key->hash) ? 1 : -1;
10641070
}
10651071
else if (strategy == JsQueryMatchStrategyNumber ||
1066-
strategy == JsonpathExistsStrategyNumber)
1072+
strategy == JsonpathExistsStrategyNumber ||
1073+
strategy == JsonpathMatchStrategyNumber)
10671074
{
10681075
KeyExtra *extra = (KeyExtra *)PG_GETARG_POINTER(3);
10691076
ExtractedNode *node = extra->node;
@@ -1276,8 +1283,10 @@ gin_extract_jsonb_query_path_value_internal(FunctionCallInfo fcinfo, bool lax)
12761283
case JsQueryMatchStrategyNumber:
12771284
#ifndef NO_JSONPATH
12781285
case JsonpathExistsStrategyNumber:
1279-
if (strategy == JsonpathExistsStrategyNumber)
1286+
case JsonpathMatchStrategyNumber:
1287+
if (strategy != JsQueryMatchStrategyNumber)
12801288
root = extractJsonPath(PG_GETARG_JSONPATH_P(0),
1289+
strategy == JsonpathExistsStrategyNumber,
12811290
make_path_value_entry_handler,
12821291
check_path_value_entry_handler,
12831292
(Pointer) &extra);
@@ -1356,6 +1365,7 @@ gin_consistent_jsonb_path_value(PG_FUNCTION_ARGS)
13561365

13571366
case JsQueryMatchStrategyNumber:
13581367
case JsonpathExistsStrategyNumber:
1368+
case JsonpathMatchStrategyNumber:
13591369
if (nkeys == 0)
13601370
res = true;
13611371
else
@@ -1418,6 +1428,7 @@ gin_triconsistent_jsonb_path_value(PG_FUNCTION_ARGS)
14181428

14191429
case JsQueryMatchStrategyNumber:
14201430
case JsonpathExistsStrategyNumber:
1431+
case JsonpathMatchStrategyNumber:
14211432
if (nkeys == 0)
14221433
res = GIN_MAYBE;
14231434
else

jsquery--1.1.sql

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ CREATE OR REPLACE FUNCTION gin_debug_query_laxpath_value(jsquery)
330330
AS 'MODULE_PATHNAME'
331331
LANGUAGE C STRICT IMMUTABLE;
332332

333-
-- add support for operator @? (jsonb, jsonpath) if type jsonpath exists in catalog
333+
-- add support for operators @?, @@ (jsonb, jsonpath) if type jsonpath exists in catalog
334334
DO LANGUAGE plpgsql
335335
$$
336336
BEGIN
@@ -346,10 +346,16 @@ BEGIN
346346
IF FOUND THEN
347347
ALTER OPERATOR FAMILY jsonb_path_value_ops USING gin
348348
ADD OPERATOR 15 @? (jsonb, jsonpath);
349+
ALTER OPERATOR FAMILY jsonb_path_value_ops USING gin
350+
ADD OPERATOR 16 @@ (jsonb, jsonpath);
349351
ALTER OPERATOR FAMILY jsonb_laxpath_value_ops USING gin
350352
ADD OPERATOR 15 @? (jsonb, jsonpath);
353+
ALTER OPERATOR FAMILY jsonb_laxpath_value_ops USING gin
354+
ADD OPERATOR 16 @@ (jsonb, jsonpath);
351355
ALTER OPERATOR FAMILY jsonb_value_path_ops USING gin
352356
ADD OPERATOR 15 @? (jsonb, jsonpath);
357+
ALTER OPERATOR FAMILY jsonb_value_path_ops USING gin
358+
ADD OPERATOR 16 @@ (jsonb, jsonpath);
353359
END IF;
354360
END
355361
$$;

jsquery.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ bool isLogicalNodeType(ExtractedNodeType type);
245245
ExtractedNode *extractJsQuery(JsQuery *jq, MakeEntryHandler makeHandler,
246246
CheckEntryHandler checkHandler, Pointer extra);
247247
#ifndef NO_JSONPATH
248-
ExtractedNode *extractJsonPath(JsonPath *jp, MakeEntryHandler makeHandler,
248+
ExtractedNode *extractJsonPath(JsonPath *jp, bool exists,
249+
MakeEntryHandler makeHandler,
249250
CheckEntryHandler checkHandler, Pointer extra);
250251
#endif
251252
char *debugJsQuery(JsQuery *jq, MakeEntryHandler makeHandler,

jsquery_extract.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,15 +1346,17 @@ extractJsQuery(JsQuery *jq, MakeEntryHandler makeHandler,
13461346
* Turn jsonpath into tree of entries using user-provided handler.
13471347
*/
13481348
ExtractedNode *
1349-
extractJsonPath(JsonPath *jp, MakeEntryHandler makeHandler,
1349+
extractJsonPath(JsonPath *jp, bool exists, MakeEntryHandler makeHandler,
13501350
CheckEntryHandler checkHandler, Pointer extra)
13511351
{
13521352
ExtractedNode *root;
13531353
JsonPathItem jsp;
1354-
bool lax = (jp->header & JSONPATH_LAX) != 0;
1354+
bool lax = (jp->header & JSONPATH_LAX) != 0;
13551355

13561356
jspInit(&jsp, jp);
1357-
root = recursiveExtractJsonPathExpr(&jsp, lax, false, NULL);
1357+
root = exists
1358+
? extractJsonPathExists(&jsp, lax, NULL)
1359+
: recursiveExtractJsonPathExpr(&jsp, lax, false, NULL);
13581360
if (root)
13591361
{
13601362
flatternTree(root);

0 commit comments

Comments
 (0)