@@ -712,6 +712,7 @@ executeBinaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
712712 Datum ldatum ;
713713 Datum rdatum ;
714714 Datum res ;
715+ bool hasNext ;
715716
716717 jspGetLeftArg (jsp , & elem );
717718
@@ -743,7 +744,9 @@ executeBinaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
743744 if (rval -> type != jbvNumeric )
744745 return jperMakeError (ERRCODE_SINGLETON_JSON_ITEM_REQUIRED );
745746
746- if (!found )
747+ hasNext = jspGetNext (jsp , & elem );
748+
749+ if (!found && !hasNext )
747750 return jperOk ;
748751
749752 ldatum = NumericGetDatum (lval -> val .numeric );
@@ -774,6 +777,9 @@ executeBinaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
774777 lval -> type = jbvNumeric ;
775778 lval -> val .numeric = DatumGetNumeric (res );
776779
780+ if (hasNext )
781+ return recursiveExecute (cxt , & elem , lval , found );
782+
777783 * found = lappend (* found , lval );
778784
779785 return jperOk ;
@@ -787,6 +793,7 @@ executeUnaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
787793 JsonPathItem elem ;
788794 List * seq = NIL ;
789795 ListCell * lc ;
796+ bool hasNext ;
790797
791798 jspGetArg (jsp , & elem );
792799 jper = recursiveExecuteAndUnwrap (cxt , & elem , jb , & seq );
@@ -796,6 +803,8 @@ executeUnaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
796803
797804 jper = jperNotFound ;
798805
806+ hasNext = jspGetNext (jsp , & elem );
807+
799808 foreach (lc , seq )
800809 {
801810 JsonbValue * val = lfirst (lc );
@@ -806,12 +815,10 @@ executeUnaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
806815
807816 if (val -> type == jbvNumeric )
808817 {
809- jper = jperOk ;
810-
811- if (!found )
812- return jper ;
818+ if (!found && !hasNext )
819+ return jperOk ;
813820 }
814- else if (!found )
821+ else if (!found && ! hasNext )
815822 continue ; /* skip non-numerics processing */
816823
817824 if (val -> type != jbvNumeric )
@@ -832,7 +839,26 @@ executeUnaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
832839 elog (ERROR , "unknown jsonpath arithmetic operation %d" , jsp -> type );
833840 }
834841
835- * found = lappend (* found , val );
842+ if (hasNext )
843+ {
844+ JsonPathExecResult jper2 = recursiveExecute (cxt , & elem , val , found );
845+
846+ if (jperIsError (jper2 ))
847+ return jper2 ;
848+
849+ if (jper2 == jperOk )
850+ {
851+ if (!found )
852+ return jperOk ;
853+ jper = jperOk ;
854+ }
855+ }
856+ else
857+ {
858+ Assert (found );
859+ * found = lappend (* found , val );
860+ jper = jperOk ;
861+ }
836862 }
837863
838864 return jper ;
0 commit comments