Skip to content

Commit f4ae676

Browse files
committed
Forbid numeric NaN in jsonpath
SQL standard doesn't define numeric Inf or NaN values. It appears even more ridiculous to support then in jsonpath assuming JSON doesn't support these values as well. This commit forbids returning NaN from .double(), which was previously allowed. NaN can't be result of inner-jsonpath computation over non-NaNs. So, we can not expect NaN in the jsonpath output. Reported-by: Tom Lane Discussion: https://postgr.es/m/203949.1591879542%40sss.pgh.pa.us Author: Alexander Korotkov Reviewed-by: Tom Lane Backpatch-through: 12
1 parent 3ec5f6b commit f4ae676

File tree

3 files changed

+10
-25
lines changed

3 files changed

+10
-25
lines changed

src/backend/utils/adt/jsonb_util.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,14 +1728,6 @@ convertJsonbScalar(StringInfo buffer, JEntry *jentry, JsonbValue *scalarVal)
17281728
break;
17291729

17301730
case jbvNumeric:
1731-
/* replace numeric NaN with string "NaN" */
1732-
if (numeric_is_nan(scalarVal->val.numeric))
1733-
{
1734-
appendToBuffer(buffer, "NaN", 3);
1735-
*jentry = 3;
1736-
break;
1737-
}
1738-
17391731
numlen = VARSIZE_ANY(scalarVal->val.numeric);
17401732
padlen = padBufferToInt(buffer);
17411733

src/backend/utils/adt/jsonpath_exec.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -975,15 +975,16 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
975975
{
976976
char *tmp = DatumGetCString(DirectFunctionCall1(numeric_out,
977977
NumericGetDatum(jb->val.numeric)));
978+
double val;
978979
bool have_error = false;
979980

980-
(void) float8in_internal_opt_error(tmp,
981-
NULL,
982-
"double precision",
983-
tmp,
984-
&have_error);
981+
val = float8in_internal_opt_error(tmp,
982+
NULL,
983+
"double precision",
984+
tmp,
985+
&have_error);
985986

986-
if (have_error)
987+
if (have_error || isinf(val) || isnan(val))
987988
RETURN_ERROR(ereport(ERROR,
988989
(errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
989990
errmsg("numeric argument of jsonpath item method .%s() is out of range for type double precision",
@@ -1004,7 +1005,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
10041005
tmp,
10051006
&have_error);
10061007

1007-
if (have_error || isinf(val))
1008+
if (have_error || isinf(val) || isnan(val))
10081009
RETURN_ERROR(ereport(ERROR,
10091010
(errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
10101011
errmsg("string argument of jsonpath item method .%s() is not a valid representation of a double precision number",

src/test/regress/expected/jsonb_jsonpath.out

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,17 +1500,9 @@ ERROR: string argument of jsonpath item method .double() is not a valid represe
15001500
select jsonb_path_query('1e1000', '$.double()');
15011501
ERROR: numeric argument of jsonpath item method .double() is out of range for type double precision
15021502
select jsonb_path_query('"nan"', '$.double()');
1503-
jsonb_path_query
1504-
------------------
1505-
"NaN"
1506-
(1 row)
1507-
1503+
ERROR: string argument of jsonpath item method .double() is not a valid representation of a double precision number
15081504
select jsonb_path_query('"NaN"', '$.double()');
1509-
jsonb_path_query
1510-
------------------
1511-
"NaN"
1512-
(1 row)
1513-
1505+
ERROR: string argument of jsonpath item method .double() is not a valid representation of a double precision number
15141506
select jsonb_path_query('"inf"', '$.double()');
15151507
ERROR: string argument of jsonpath item method .double() is not a valid representation of a double precision number
15161508
select jsonb_path_query('"-inf"', '$.double()');

0 commit comments

Comments
 (0)