Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5c8c00e

Browse files
author
Nikita Glukhov
committed
Introduce struct JsonPathExecContext
1 parent f5e9589 commit 5c8c00e

File tree

1 file changed

+46
-38
lines changed

1 file changed

+46
-38
lines changed

src/backend/utils/adt/jsonpath_exec.c

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@
1919
#include "utils/json.h"
2020
#include "utils/jsonpath.h"
2121

22-
static JsonPathExecResult
23-
recursiveExecute(JsonPathItem *jsp, List *vars, JsonbValue *jb, List **found);
22+
typedef struct JsonPathExecContext
23+
{
24+
List *vars;
25+
bool lax;
26+
} JsonPathExecContext;
27+
28+
static JsonPathExecResult recursiveExecute(JsonPathExecContext *cxt,
29+
JsonPathItem *jsp, JsonbValue *jb,
30+
List **found);
2431

2532
/********************Execute functions for JsonPath***************************/
2633

@@ -126,7 +133,7 @@ computeJsonPathVariable(JsonPathItem *variable, List *vars, JsonbValue *value)
126133
}
127134

128135
static void
129-
computeJsonPathItem(JsonPathItem *item, List *vars, JsonbValue *value)
136+
computeJsonPathItem(JsonPathExecContext *cxt, JsonPathItem *item, JsonbValue *value)
130137
{
131138
switch(item->type)
132139
{
@@ -146,7 +153,7 @@ computeJsonPathItem(JsonPathItem *item, List *vars, JsonbValue *value)
146153
value->val.string.val = jspGetString(item, &value->val.string.len);
147154
break;
148155
case jpiVariable:
149-
computeJsonPathVariable(item, vars, value);
156+
computeJsonPathVariable(item, cxt->vars, value);
150157
break;
151158
default:
152159
elog(ERROR, "Wrong type");
@@ -302,25 +309,24 @@ makeCompare(int32 op, JsonbValue *jb1, JsonbValue *jb2)
302309
}
303310

304311
static JsonPathExecResult
305-
executeExpr(JsonPathItem *jsp, List *vars, JsonbValue *jb)
312+
executeExpr(JsonPathExecContext *cxt, JsonPathItem *jsp, JsonbValue *jb)
306313
{
307314
JsonPathExecResult res;
308315
JsonPathItem elem;
309316
List *lseq = NIL;
310317
List *rseq = NIL;
311318
ListCell *llc;
312319
ListCell *rlc;
313-
bool strict = true; /* FIXME pass */
314320
bool error = false;
315321
bool found = false;
316322

317323
jspGetLeftArg(jsp, &elem);
318-
res = recursiveExecute(&elem, vars, jb, &lseq);
324+
res = recursiveExecute(cxt, &elem, jb, &lseq);
319325
if (res != jperOk)
320326
return res;
321327

322328
jspGetRightArg(jsp, &elem);
323-
res = recursiveExecute(&elem, vars, jb, &rseq);
329+
res = recursiveExecute(cxt, &elem, jb, &rseq);
324330
if (res != jperOk)
325331
return res;
326332

@@ -352,14 +358,14 @@ executeExpr(JsonPathItem *jsp, List *vars, JsonbValue *jb)
352358

353359
if (res == jperOk)
354360
{
355-
if (!strict)
361+
if (cxt->lax)
356362
return jperOk;
357363

358364
found = true;
359365
}
360366
else if (res == jperError)
361367
{
362-
if (strict)
368+
if (!cxt->lax)
363369
return jperError;
364370

365371
error = true;
@@ -387,10 +393,7 @@ copyJsonbValue(JsonbValue *src)
387393
}
388394

389395
static JsonPathExecResult
390-
recursiveExecute(JsonPathItem *jsp, List *vars, JsonbValue *jb, List **found);
391-
392-
static JsonPathExecResult
393-
recursiveAny(JsonPathItem *jsp, List *vars, JsonbValue *jb,
396+
recursiveAny(JsonPathExecContext *cxt, JsonPathItem *jsp, JsonbValue *jb,
394397
List **found, uint32 level, uint32 first, uint32 last)
395398
{
396399
JsonPathExecResult res = jperNotFound;
@@ -421,7 +424,7 @@ recursiveAny(JsonPathItem *jsp, List *vars, JsonbValue *jb,
421424
/* check expression */
422425
if (jsp)
423426
{
424-
res = recursiveExecute(jsp, vars, &v, found);
427+
res = recursiveExecute(cxt, jsp, &v, found);
425428
if (res == jperOk && !found)
426429
break;
427430
}
@@ -436,7 +439,7 @@ recursiveAny(JsonPathItem *jsp, List *vars, JsonbValue *jb,
436439

437440
if (level < last && v.type == jbvBinary)
438441
{
439-
res = recursiveAny(jsp, vars, &v, found, level + 1, first, last);
442+
res = recursiveAny(cxt, jsp, &v, found, level + 1, first, last);
440443

441444
if (res == jperOk && found == NULL)
442445
break;
@@ -448,7 +451,8 @@ recursiveAny(JsonPathItem *jsp, List *vars, JsonbValue *jb,
448451
}
449452

450453
static JsonPathExecResult
451-
recursiveExecute(JsonPathItem *jsp, List *vars, JsonbValue *jb, List **found)
454+
recursiveExecute(JsonPathExecContext *cxt, JsonPathItem *jsp, JsonbValue *jb,
455+
List **found)
452456
{
453457
JsonPathItem elem;
454458
JsonPathExecResult res = jperNotFound;
@@ -458,33 +462,33 @@ recursiveExecute(JsonPathItem *jsp, List *vars, JsonbValue *jb, List **found)
458462
switch(jsp->type) {
459463
case jpiAnd:
460464
jspGetLeftArg(jsp, &elem);
461-
res = recursiveExecute(&elem, vars, jb, NULL);
465+
res = recursiveExecute(cxt, &elem, jb, NULL);
462466
if (res != jperNotFound)
463467
{
464468
JsonPathExecResult res2;
465469

466470
jspGetRightArg(jsp, &elem);
467-
res2 = recursiveExecute(&elem, vars, jb, NULL);
471+
res2 = recursiveExecute(cxt, &elem, jb, NULL);
468472

469473
res = res2 == jperOk ? res : res2;
470474
}
471475
break;
472476
case jpiOr:
473477
jspGetLeftArg(jsp, &elem);
474-
res = recursiveExecute(&elem, vars, jb, NULL);
478+
res = recursiveExecute(cxt, &elem, jb, NULL);
475479
if (res != jperOk)
476480
{
477481
JsonPathExecResult res2;
478482

479483
jspGetRightArg(jsp, &elem);
480-
res2 = recursiveExecute(&elem, vars, jb, NULL);
484+
res2 = recursiveExecute(cxt, &elem, jb, NULL);
481485

482486
res = res2 == jperNotFound ? res : res2;
483487
}
484488
break;
485489
case jpiNot:
486490
jspGetArg(jsp, &elem);
487-
switch((res = recursiveExecute(&elem, vars, jb, NULL)))
491+
switch((res = recursiveExecute(cxt, &elem, jb, NULL)))
488492
{
489493
case jperOk:
490494
res = jperNotFound;
@@ -498,7 +502,7 @@ recursiveExecute(JsonPathItem *jsp, List *vars, JsonbValue *jb, List **found)
498502
break;
499503
case jpiIsUnknown:
500504
jspGetArg(jsp, &elem);
501-
res = recursiveExecute(&elem, vars, jb, NULL);
505+
res = recursiveExecute(cxt, &elem, jb, NULL);
502506
res = res == jperError ? jperOk : jperNotFound;
503507
break;
504508
case jpiKey:
@@ -515,7 +519,7 @@ recursiveExecute(JsonPathItem *jsp, List *vars, JsonbValue *jb, List **found)
515519
{
516520
if (jspGetNext(jsp, &elem))
517521
{
518-
res = recursiveExecute(&elem, vars, v, found);
522+
res = recursiveExecute(cxt, &elem, v, found);
519523
pfree(v);
520524
}
521525
else
@@ -552,11 +556,11 @@ recursiveExecute(JsonPathItem *jsp, List *vars, JsonbValue *jb, List **found)
552556

553557
JsonbExtractScalar(jb->val.binary.data, &v);
554558

555-
res = recursiveExecute(&elem, vars, &v, found);
559+
res = recursiveExecute(cxt, &elem, &v, found);
556560
}
557561
else
558562
{
559-
res = recursiveExecute(&elem, vars, jb, found);
563+
res = recursiveExecute(cxt, &elem, jb, found);
560564
}
561565
break;
562566
case jpiAnyArray:
@@ -576,7 +580,7 @@ recursiveExecute(JsonPathItem *jsp, List *vars, JsonbValue *jb, List **found)
576580
{
577581
if (hasNext == true)
578582
{
579-
res = recursiveExecute(&elem, vars, &v, found);
583+
res = recursiveExecute(cxt, &elem, &v, found);
580584

581585
if (res == jperError)
582586
break;
@@ -618,7 +622,7 @@ recursiveExecute(JsonPathItem *jsp, List *vars, JsonbValue *jb, List **found)
618622

619623
if (hasNext == true)
620624
{
621-
res = recursiveExecute(&elem, vars, v, found);
625+
res = recursiveExecute(cxt, &elem, v, found);
622626

623627
if (res == jperError || found == NULL)
624628
break;
@@ -655,7 +659,7 @@ recursiveExecute(JsonPathItem *jsp, List *vars, JsonbValue *jb, List **found)
655659
{
656660
if (hasNext == true)
657661
{
658-
res = recursiveExecute(&elem, vars, &v, found);
662+
res = recursiveExecute(cxt, &elem, &v, found);
659663

660664
if (res == jperError)
661665
break;
@@ -682,12 +686,12 @@ recursiveExecute(JsonPathItem *jsp, List *vars, JsonbValue *jb, List **found)
682686
case jpiGreater:
683687
case jpiLessOrEqual:
684688
case jpiGreaterOrEqual:
685-
res = executeExpr(jsp, vars, jb);
689+
res = executeExpr(cxt, jsp, jb);
686690
break;
687691
case jpiRoot:
688692
if (jspGetNext(jsp, &elem))
689693
{
690-
res = recursiveExecute(&elem, vars, jb, found);
694+
res = recursiveExecute(cxt, &elem, jb, found);
691695
}
692696
else
693697
{
@@ -699,11 +703,11 @@ recursiveExecute(JsonPathItem *jsp, List *vars, JsonbValue *jb, List **found)
699703
break;
700704
case jpiFilter:
701705
jspGetArg(jsp, &elem);
702-
res = recursiveExecute(&elem, vars, jb, NULL);
706+
res = recursiveExecute(cxt, &elem, jb, NULL);
703707
if (res != jperOk)
704708
res = jperNotFound;
705709
else if (jspGetNext(jsp, &elem))
706-
res = recursiveExecute(&elem, vars, jb, found);
710+
res = recursiveExecute(cxt, &elem, jb, found);
707711
else if (found)
708712
*found = lappend(*found, copyJsonbValue(jb));
709713
break;
@@ -716,7 +720,7 @@ recursiveExecute(JsonPathItem *jsp, List *vars, JsonbValue *jb, List **found)
716720
{
717721
if (hasNext)
718722
{
719-
res = recursiveExecute(&elem, vars, jb, found);
723+
res = recursiveExecute(cxt, &elem, jb, found);
720724
if (res == jperOk && !found)
721725
break;
722726
}
@@ -730,15 +734,15 @@ recursiveExecute(JsonPathItem *jsp, List *vars, JsonbValue *jb, List **found)
730734
}
731735

732736
if (jb->type == jbvBinary)
733-
res = recursiveAny(hasNext ? &elem : NULL, vars, jb, found,
737+
res = recursiveAny(cxt, hasNext ? &elem : NULL, jb, found,
734738
1,
735739
jsp->anybounds.first,
736740
jsp->anybounds.last);
737741
break;
738742
}
739743
case jpiExists:
740744
jspGetArg(jsp, &elem);
741-
res = recursiveExecute(&elem, vars, jb, NULL);
745+
res = recursiveExecute(cxt, &elem, jb, NULL);
742746
break;
743747
case jpiNull:
744748
case jpiBool:
@@ -749,7 +753,7 @@ recursiveExecute(JsonPathItem *jsp, List *vars, JsonbValue *jb, List **found)
749753
if (found)
750754
{
751755
JsonbValue *jbv = palloc(sizeof(*jbv));
752-
computeJsonPathItem(jsp, vars, jbv);
756+
computeJsonPathItem(cxt, jsp, jbv);
753757
*found = lappend(*found, jbv);
754758
}
755759
break;
@@ -763,16 +767,20 @@ recursiveExecute(JsonPathItem *jsp, List *vars, JsonbValue *jb, List **found)
763767
JsonPathExecResult
764768
executeJsonPath(JsonPath *path, List *vars, Jsonb *json, List **foundJson)
765769
{
770+
JsonPathExecContext cxt;
766771
JsonPathItem jsp;
767772
JsonbValue jbv;
768773

774+
cxt.vars = vars;
775+
cxt.lax = false; /* FIXME */
776+
769777
jbv.type = jbvBinary;
770778
jbv.val.binary.data = &json->root;
771779
jbv.val.binary.len = VARSIZE_ANY_EXHDR(json);
772780

773781
jspInit(&jsp, path);
774782

775-
return recursiveExecute(&jsp, vars, &jbv, foundJson);
783+
return recursiveExecute(&cxt, &jsp, &jbv, foundJson);
776784
}
777785

778786
static Datum

0 commit comments

Comments
 (0)