@@ -691,6 +691,7 @@ executeBinaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
691691 Datum ldatum ;
692692 Datum rdatum ;
693693 Datum res ;
694+ bool hasNext ;
694695
695696 jspGetLeftArg (jsp , & elem );
696697
@@ -722,7 +723,9 @@ executeBinaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
722723 if (rval -> type != jbvNumeric )
723724 return jperMakeError (ERRCODE_SINGLETON_JSON_ITEM_REQUIRED );
724725
725- if (!found )
726+ hasNext = jspGetNext (jsp , & elem );
727+
728+ if (!found && !hasNext )
726729 return jperOk ;
727730
728731 ldatum = NumericGetDatum (lval -> val .numeric );
@@ -753,6 +756,9 @@ executeBinaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
753756 lval -> type = jbvNumeric ;
754757 lval -> val .numeric = DatumGetNumeric (res );
755758
759+ if (hasNext )
760+ return recursiveExecute (cxt , & elem , lval , found );
761+
756762 * found = lappend (* found , lval );
757763
758764 return jperOk ;
@@ -766,6 +772,7 @@ executeUnaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
766772 JsonPathItem elem ;
767773 List * seq = NIL ;
768774 ListCell * lc ;
775+ bool hasNext ;
769776
770777 jspGetArg (jsp , & elem );
771778 jper = recursiveExecuteAndUnwrap (cxt , & elem , jb , & seq );
@@ -775,6 +782,8 @@ executeUnaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
775782
776783 jper = jperNotFound ;
777784
785+ hasNext = jspGetNext (jsp , & elem );
786+
778787 foreach (lc , seq )
779788 {
780789 JsonbValue * val = lfirst (lc );
@@ -785,12 +794,10 @@ executeUnaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
785794
786795 if (val -> type == jbvNumeric )
787796 {
788- jper = jperOk ;
789-
790- if (!found )
791- return jper ;
797+ if (!found && !hasNext )
798+ return jperOk ;
792799 }
793- else if (!found )
800+ else if (!found && ! hasNext )
794801 continue ; /* skip non-numerics processing */
795802
796803 if (val -> type != jbvNumeric )
@@ -811,7 +818,26 @@ executeUnaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
811818 elog (ERROR , "unknown jsonpath arithmetic operation %d" , jsp -> type );
812819 }
813820
814- * found = lappend (* found , val );
821+ if (hasNext )
822+ {
823+ JsonPathExecResult jper2 = recursiveExecute (cxt , & elem , val , found );
824+
825+ if (jperIsError (jper2 ))
826+ return jper2 ;
827+
828+ if (jper2 == jperOk )
829+ {
830+ if (!found )
831+ return jperOk ;
832+ jper = jperOk ;
833+ }
834+ }
835+ else
836+ {
837+ Assert (found );
838+ * found = lappend (* found , val );
839+ jper = jperOk ;
840+ }
815841 }
816842
817843 return jper ;
0 commit comments