improve_error_for_Nan_Infinity.patch.no
application/octet-stream
Filename: improve_error_for_Nan_Infinity.patch.no
Type: application/octet-stream
Part: 0
Message:
Re: More new SQL/JSON item methods
commit c1ca75abf0b77b9359b457441aefd3968d0b5a58
Author: Jeevan Chalke <jeevan.chalke@enterprisedb.com>
Date: Tue Jan 30 09:24:00 2024 +0530
Improve error message for NaN or Infinity inputs for jsonpath item.
Jeevan Chalke, per suggestion from Tom Lane.
diff --git a/src/backend/utils/adt/jsonpath_exec.c b/src/backend/utils/adt/jsonpath_exec.c
index 23d375d..0567b17 100644
--- a/src/backend/utils/adt/jsonpath_exec.c
+++ b/src/backend/utils/adt/jsonpath_exec.c
@@ -1049,11 +1049,16 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
tmp,
(Node *) &escontext);
- if (escontext.error_occurred || isinf(val) || isnan(val))
+ if (escontext.error_occurred)
RETURN_ERROR(ereport(ERROR,
(errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
errmsg("numeric argument of jsonpath item method .%s() is out of range for type double precision",
jspOperationName(jsp->type)))));
+ if (isinf(val) || isnan(val))
+ RETURN_ERROR(ereport(ERROR,
+ (errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
+ errmsg("NaN or Infinity is not allowed for jsonpath item method .%s()",
+ jspOperationName(jsp->type)))));
res = jperOk;
}
else if (jb->type == jbvString)
@@ -1070,11 +1075,16 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
tmp,
(Node *) &escontext);
- if (escontext.error_occurred || isinf(val) || isnan(val))
+ if (escontext.error_occurred)
RETURN_ERROR(ereport(ERROR,
(errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
errmsg("string argument of jsonpath item method .%s() is not a valid representation of a double precision number",
jspOperationName(jsp->type)))));
+ if (isinf(val) || isnan(val))
+ RETURN_ERROR(ereport(ERROR,
+ (errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
+ errmsg("NaN or Infinity is not allowed for jsonpath item method .%s()",
+ jspOperationName(jsp->type)))));
jb = &jbv;
jb->type = jbvNumeric;
@@ -1287,7 +1297,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
if (numeric_is_nan(num) || numeric_is_inf(num))
RETURN_ERROR(ereport(ERROR,
(errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
- errmsg("numeric argument of jsonpath item method .%s() is out of range for type decimal or number",
+ errmsg("NaN or Infinity is not allowed for jsonpath item method .%s()",
jspOperationName(jsp->type)))));
if (jsp->type == jpiDecimal)
@@ -1319,7 +1329,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
if (numeric_is_nan(num) || numeric_is_inf(num))
RETURN_ERROR(ereport(ERROR,
(errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
- errmsg("string argument of jsonpath item method .%s() is not a valid representation of a decimal or number",
+ errmsg("NaN or Infinity is not allowed for jsonpath item method .%s()",
jspOperationName(jsp->type)))));
res = jperOk;
diff --git a/src/test/regress/expected/jsonb_jsonpath.out b/src/test/regress/expected/jsonb_jsonpath.out
index eea2af3..95408ad 100644
--- a/src/test/regress/expected/jsonb_jsonpath.out
+++ b/src/test/regress/expected/jsonb_jsonpath.out
@@ -1500,13 +1500,13 @@ ERROR: string argument of jsonpath item method .double() is not a valid represe
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()');
-ERROR: string argument of jsonpath item method .double() is not a valid representation of a double precision number
+ERROR: NaN or Infinity is not allowed for jsonpath item method .double()
select jsonb_path_query('"NaN"', '$.double()');
-ERROR: string argument of jsonpath item method .double() is not a valid representation of a double precision number
+ERROR: NaN or Infinity is not allowed for jsonpath item method .double()
select jsonb_path_query('"inf"', '$.double()');
-ERROR: string argument of jsonpath item method .double() is not a valid representation of a double precision number
+ERROR: NaN or Infinity is not allowed for jsonpath item method .double()
select jsonb_path_query('"-inf"', '$.double()');
-ERROR: string argument of jsonpath item method .double() is not a valid representation of a double precision number
+ERROR: NaN or Infinity is not allowed for jsonpath item method .double()
select jsonb_path_query('"inf"', '$.double()', silent => true);
jsonb_path_query
------------------
@@ -2152,13 +2152,13 @@ select jsonb_path_query('1e1000', '$.decimal()');
(1 row)
select jsonb_path_query('"nan"', '$.decimal()');
-ERROR: string argument of jsonpath item method .decimal() is not a valid representation of a decimal or number
+ERROR: NaN or Infinity is not allowed for jsonpath item method .decimal()
select jsonb_path_query('"NaN"', '$.decimal()');
-ERROR: string argument of jsonpath item method .decimal() is not a valid representation of a decimal or number
+ERROR: NaN or Infinity is not allowed for jsonpath item method .decimal()
select jsonb_path_query('"inf"', '$.decimal()');
-ERROR: string argument of jsonpath item method .decimal() is not a valid representation of a decimal or number
+ERROR: NaN or Infinity is not allowed for jsonpath item method .decimal()
select jsonb_path_query('"-inf"', '$.decimal()');
-ERROR: string argument of jsonpath item method .decimal() is not a valid representation of a decimal or number
+ERROR: NaN or Infinity is not allowed for jsonpath item method .decimal()
select jsonb_path_query('"inf"', '$.decimal()', silent => true);
jsonb_path_query
------------------
@@ -2448,13 +2448,13 @@ select jsonb_path_query('1e1000', '$.number()');
(1 row)
select jsonb_path_query('"nan"', '$.number()');
-ERROR: string argument of jsonpath item method .number() is not a valid representation of a decimal or number
+ERROR: NaN or Infinity is not allowed for jsonpath item method .number()
select jsonb_path_query('"NaN"', '$.number()');
-ERROR: string argument of jsonpath item method .number() is not a valid representation of a decimal or number
+ERROR: NaN or Infinity is not allowed for jsonpath item method .number()
select jsonb_path_query('"inf"', '$.number()');
-ERROR: string argument of jsonpath item method .number() is not a valid representation of a decimal or number
+ERROR: NaN or Infinity is not allowed for jsonpath item method .number()
select jsonb_path_query('"-inf"', '$.number()');
-ERROR: string argument of jsonpath item method .number() is not a valid representation of a decimal or number
+ERROR: NaN or Infinity is not allowed for jsonpath item method .number()
select jsonb_path_query('"inf"', '$.number()', silent => true);
jsonb_path_query
------------------