From 5c2a0ca449465548107af2daf597dfe1e8b70f29 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Fri, 14 Jun 2019 15:11:24 +0900
Subject: [PATCH 3/3] Separate two distinctive json errors.

The error message "right/left operand of jsonpath operator %s is not a
single numeric value" is not user-friendly since the two different
causes are not easy to identify for users. Differenciate the two cases
for users's convenience.
---
 src/backend/utils/adt/jsonpath_exec.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/src/backend/utils/adt/jsonpath_exec.c b/src/backend/utils/adt/jsonpath_exec.c
index 873d64b630..4afc4b517f 100644
--- a/src/backend/utils/adt/jsonpath_exec.c
+++ b/src/backend/utils/adt/jsonpath_exec.c
@@ -1484,6 +1484,7 @@ executeBinaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
 	JsonValueList rseq = {0};
 	JsonbValue *lval;
 	JsonbValue *rval;
+	int			llen;
 	Numeric		res;
 
 	jspGetLeftArg(jsp, &elem);
@@ -1502,19 +1503,27 @@ executeBinaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
 	if (jperIsError(jper))
 		return jper;
 
-	if (JsonValueListLength(&lseq) != 1 ||
+	llen = JsonValueListLength(&lseq);
+	if (llen != 1 ||
 		!(lval = getScalar(JsonValueListHead(&lseq), jbvNumeric)))
 		RETURN_ERROR(ereport(ERROR,
 							 (errcode(ERRCODE_SINGLETON_JSON_ITEM_REQUIRED),
 							  errmsg("left operand of jsonpath operator %s is not a single numeric value",
-									 jspOperationName(jsp->type)))));
+									 jspOperationName(jsp->type)),
+							  (llen != 1 ?
+							   errdetail("It was an array with %d elements.", llen):
+							   errdetail("The only element was not a numeric.")))));
 
-	if (JsonValueListLength(&rseq) != 1 ||
+	llen = JsonValueListLength(&rseq);
+	if (llen != 1 ||
 		!(rval = getScalar(JsonValueListHead(&rseq), jbvNumeric)))
 		RETURN_ERROR(ereport(ERROR,
 							 (errcode(ERRCODE_SINGLETON_JSON_ITEM_REQUIRED),
 							  errmsg("right operand of jsonpath operator %s is not a single numeric value",
-									 jspOperationName(jsp->type)))));
+									 jspOperationName(jsp->type)),
+							  (llen != 1 ?
+							   errdetail("It was an array with %d elements.", llen):
+							   errdetail("The only element was not a numeric.")))));
 
 	if (jspThrowErrors(cxt))
 	{
-- 
2.16.3

