@@ -58,31 +58,37 @@ func convertToBool(j json.JSON) jsonpathBool {
5858
5959func (ctx * jsonpathCtx ) evalOperation (
6060 op jsonpath.Operation , jsonValue json.JSON ,
61- ) (json.JSON , error ) {
61+ ) ([] json.JSON , error ) {
6262 switch op .Type {
6363 case jsonpath .OpLogicalAnd , jsonpath .OpLogicalOr , jsonpath .OpLogicalNot :
6464 res , err := ctx .evalLogical (op , jsonValue )
6565 if err != nil {
66- return convertFromBool (jsonpathBoolUnknown ), err
66+ return []json. JSON { convertFromBool (jsonpathBoolUnknown )} , err
6767 }
68- return convertFromBool (res ), nil
68+ return []json. JSON { convertFromBool (res )} , nil
6969 case jsonpath .OpCompEqual , jsonpath .OpCompNotEqual ,
7070 jsonpath .OpCompLess , jsonpath .OpCompLessEqual ,
7171 jsonpath .OpCompGreater , jsonpath .OpCompGreaterEqual :
7272 res , err := ctx .evalComparison (op , jsonValue )
7373 if err != nil {
74- return convertFromBool (jsonpathBoolUnknown ), err
74+ return []json. JSON { convertFromBool (jsonpathBoolUnknown )} , err
7575 }
76- return convertFromBool (res ), nil
76+ return []json. JSON { convertFromBool (res )} , nil
7777 case jsonpath .OpAdd , jsonpath .OpSub , jsonpath .OpMult ,
7878 jsonpath .OpDiv , jsonpath .OpMod :
79- return ctx .evalArithmetic (op , jsonValue )
79+ results , err := ctx .evalArithmetic (op , jsonValue )
80+ if err != nil {
81+ return nil , err
82+ }
83+ return []json.JSON {results }, nil
8084 case jsonpath .OpLikeRegex :
8185 res , err := ctx .evalRegex (op , jsonValue )
8286 if err != nil {
83- return convertFromBool (jsonpathBoolUnknown ), err
87+ return []json. JSON { convertFromBool (jsonpathBoolUnknown )} , err
8488 }
85- return convertFromBool (res ), nil
89+ return []json.JSON {convertFromBool (res )}, nil
90+ case jsonpath .OpPlus , jsonpath .OpMinus :
91+ return ctx .evalUnaryArithmetic (op , jsonValue )
8692 default :
8793 panic (errors .AssertionFailedf ("unhandled operation type" ))
8894 }
@@ -286,6 +292,30 @@ func execComparison(l, r json.JSON, op jsonpath.OperationType) (jsonpathBool, er
286292 return jsonpathBoolFalse , nil
287293}
288294
295+ func (ctx * jsonpathCtx ) evalUnaryArithmetic (
296+ op jsonpath.Operation , jsonValue json.JSON ,
297+ ) ([]json.JSON , error ) {
298+ operands , err := ctx .evalAndUnwrapResult (op .Left , jsonValue , true /* unwrap */ )
299+ if err != nil {
300+ return nil , err
301+ }
302+
303+ for i := range len (operands ) {
304+ if operands [i ].Type () != json .NumberJSONType {
305+ return nil , pgerror .Newf (pgcode .SingletonSQLJSONItemRequired ,
306+ "operand of unary jsonpath operator %s is not a numeric value" ,
307+ jsonpath .OperationTypeStrings [op .Type ])
308+ }
309+
310+ if op .Type == jsonpath .OpMinus {
311+ leftNum , _ := operands [i ].AsDecimal ()
312+ leftNum .Neg (leftNum )
313+ operands [i ] = json .FromDecimal (* leftNum )
314+ }
315+ }
316+ return operands , nil
317+ }
318+
289319func (ctx * jsonpathCtx ) evalArithmetic (
290320 op jsonpath.Operation , jsonValue json.JSON ,
291321) (json.JSON , error ) {
0 commit comments