From 17f85f61eb4fcc2270aba21dfc2a3f20a87298d6 Mon Sep 17 00:00:00 2001
From: Alexander Korotkov <akorotkov@postgresql.org>
Date: Mon, 6 Jul 2020 14:58:00 +0300
Subject: [PATCH 1/2] Improve error reporting for jsonpath .double() method

---
 src/backend/utils/adt/jsonpath_exec.c        |  4 ++--
 src/test/regress/expected/jsonb_jsonpath.out | 20 +++++++-------------
 src/test/regress/sql/jsonb_jsonpath.sql      |  1 +
 3 files changed, 10 insertions(+), 15 deletions(-)

diff --git a/src/backend/utils/adt/jsonpath_exec.c b/src/backend/utils/adt/jsonpath_exec.c
index 135025cf570..e4c1bcadb2e 100644
--- a/src/backend/utils/adt/jsonpath_exec.c
+++ b/src/backend/utils/adt/jsonpath_exec.c
@@ -1055,7 +1055,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 					if (have_error)
 						RETURN_ERROR(ereport(ERROR,
 											 (errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
-											  errmsg("jsonpath item method .%s() can only be applied to a numeric value",
+											  errmsg("jsonpath item method .%s() applied to a numeric value out of range for double precision type",
 													 jspOperationName(jsp->type)))));
 					res = jperOk;
 				}
@@ -1076,7 +1076,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 					if (have_error || isinf(val))
 						RETURN_ERROR(ereport(ERROR,
 											 (errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
-											  errmsg("jsonpath item method .%s() can only be applied to a numeric value",
+											  errmsg("jsonpath item method .%s() can only be applied to a valid string representation of double precision value",
 													 jspOperationName(jsp->type)))));
 
 					jb = &jbv;
diff --git a/src/test/regress/expected/jsonb_jsonpath.out b/src/test/regress/expected/jsonb_jsonpath.out
index 83a050d3e29..36660589afa 100644
--- a/src/test/regress/expected/jsonb_jsonpath.out
+++ b/src/test/regress/expected/jsonb_jsonpath.out
@@ -1496,23 +1496,17 @@ select jsonb_path_query('"1.23"', '$.double()');
 (1 row)
 
 select jsonb_path_query('"1.23aaa"', '$.double()');
-ERROR:  jsonpath item method .double() can only be applied to a numeric value
+ERROR:  jsonpath item method .double() can only be applied to a valid string representation of double precision value
+select jsonb_path_query('1e1000', '$.double()');
+ERROR:  jsonpath item method .double() applied to a numeric value out of range for double precision type
 select jsonb_path_query('"nan"', '$.double()');
- jsonb_path_query 
-------------------
- "NaN"
-(1 row)
-
+ERROR:  jsonpath item method .double() can only be applied to a valid string representation of double precision value
 select jsonb_path_query('"NaN"', '$.double()');
- jsonb_path_query 
-------------------
- "NaN"
-(1 row)
-
+ERROR:  jsonpath item method .double() can only be applied to a valid string representation of double precision value
 select jsonb_path_query('"inf"', '$.double()');
-ERROR:  jsonpath item method .double() can only be applied to a numeric value
+ERROR:  jsonpath item method .double() can only be applied to a valid string representation of double precision value
 select jsonb_path_query('"-inf"', '$.double()');
-ERROR:  jsonpath item method .double() can only be applied to a numeric value
+ERROR:  jsonpath item method .double() can only be applied to a valid string representation of double precision value
 select jsonb_path_query('"inf"', '$.double()', silent => true);
  jsonb_path_query 
 ------------------
diff --git a/src/test/regress/sql/jsonb_jsonpath.sql b/src/test/regress/sql/jsonb_jsonpath.sql
index 731b4d444cc..a50abed95da 100644
--- a/src/test/regress/sql/jsonb_jsonpath.sql
+++ b/src/test/regress/sql/jsonb_jsonpath.sql
@@ -312,6 +312,7 @@ select jsonb_path_query('{}', '$.double()', silent => true);
 select jsonb_path_query('1.23', '$.double()');
 select jsonb_path_query('"1.23"', '$.double()');
 select jsonb_path_query('"1.23aaa"', '$.double()');
+select jsonb_path_query('1e1000', '$.double()');
 select jsonb_path_query('"nan"', '$.double()');
 select jsonb_path_query('"NaN"', '$.double()');
 select jsonb_path_query('"inf"', '$.double()');
-- 
2.14.3

