Improve error reporting for jsonpath .double() method
authorAlexander Korotkov <akorotkov@postgresql.org>
Sat, 11 Jul 2020 00:20:46 +0000 (03:20 +0300)
committerAlexander Korotkov <akorotkov@postgresql.org>
Sat, 11 Jul 2020 00:20:46 +0000 (03:20 +0300)
When jsonpath .double() method detects that numeric or string can't be
converted to double precision, it throws an error.  This commit makes these
errors explicitly express the reason of failure.

Discussion: https://postgr.es/m/CAPpHfdtqJtiSXkP7tOXez18NxhLUH_-75bL8%3DOce4Ki%2Bbv7V6Q%40mail.gmail.com
Author: Alexander Korotkov
Reviewed-by: Tom Lane
Backpatch-through: 12

src/backend/utils/adt/jsonpath_exec.c
src/test/regress/expected/jsonb_jsonpath.out
src/test/regress/sql/jsonb_jsonpath.sql

index 135025cf570f1d8c3dc1606e4bf9b04571151acb..25ec3dcd8029d09cfc00e43aefbeecaa58acd52d 100644 (file)
@@ -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("numeric argument of jsonpath item method .%s() is out of range for type double precision",
                                                     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("string argument of jsonpath item method .%s() is not a valid representation of a double precision number",
                                                     jspOperationName(jsp->type)))));
 
                    jb = &jbv;
index 83a050d3e29d6f2a22883bdeb678d535649656d8..f8a5deb2b9955bdf88af780e57595e26f71d7a70 100644 (file)
@@ -1496,7 +1496,9 @@ 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:  string argument of jsonpath item method .double() is not a valid representation of a double precision number
+select jsonb_path_query('1e1000', '$.double()');
+ERROR:  numeric argument of jsonpath item method .double() is out of range for type double precision
 select jsonb_path_query('"nan"', '$.double()');
  jsonb_path_query 
 ------------------
@@ -1510,9 +1512,9 @@ select jsonb_path_query('"NaN"', '$.double()');
 (1 row)
 
 select jsonb_path_query('"inf"', '$.double()');
-ERROR:  jsonpath item method .double() can only be applied to a numeric value
+ERROR:  string argument of jsonpath item method .double() is not a valid representation of a double precision number
 select jsonb_path_query('"-inf"', '$.double()');
-ERROR:  jsonpath item method .double() can only be applied to a numeric value
+ERROR:  string argument of jsonpath item method .double() is not a valid representation of a double precision number
 select jsonb_path_query('"inf"', '$.double()', silent => true);
  jsonb_path_query 
 ------------------
index 731b4d444cc5dd2d7f4ba2f43068400fcca30dea..a50abed95da7ce5cfa5ae05bf1fe66b20a943d21 100644 (file)
@@ -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()');