1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-15 19:21:59 +03:00

Fix incorrect translation of minus-infinity datetimes for json/jsonb.

Commit bda76c1c8c caused both plus and
minus infinity to be rendered as "infinity", which is not only wrong
but inconsistent with the pre-9.4 behavior of to_json().  Fix that by
duplicating the coding in date_out/timestamp_out/timestamptz_out more
closely.  Per bug #13687 from Stepan Perlov.  Back-patch to 9.4, like
the previous commit.

In passing, also re-pgindent json.c, since it had gotten a bit messed up by
recent patches (and I was already annoyed by indentation-related problems
in back-patching this fix ...)
This commit is contained in:
Tom Lane
2015-10-20 11:06:24 -07:00
parent 984ae04a2c
commit d435542583
10 changed files with 90 additions and 86 deletions

View File

@ -28,14 +28,6 @@
#include "utils/syscache.h"
#include "utils/typcache.h"
/*
* String to output for infinite dates and timestamps.
* Note the we don't use embedded quotes, unlike for json, because
* we store jsonb strings dequoted.
*/
#define DT_INFINITY "infinity"
typedef struct JsonbInState
{
JsonbParseState *parseState;
@ -798,21 +790,18 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
char buf[MAXDATELEN + 1];
date = DatumGetDateADT(val);
jb.type = jbvString;
/* Same as date_out(), but forcing DateStyle */
if (DATE_NOT_FINITE(date))
{
jb.val.string.len = strlen(DT_INFINITY);
jb.val.string.val = pstrdup(DT_INFINITY);
}
EncodeSpecialDate(date, buf);
else
{
j2date(date + POSTGRES_EPOCH_JDATE,
&(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
EncodeDateOnly(&tm, USE_XSD_DATES, buf);
jb.val.string.len = strlen(buf);
jb.val.string.val = pstrdup(buf);
}
jb.type = jbvString;
jb.val.string.len = strlen(buf);
jb.val.string.val = pstrdup(buf);
}
break;
case JSONBTYPE_TIMESTAMP:
@ -823,24 +812,18 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
char buf[MAXDATELEN + 1];
timestamp = DatumGetTimestamp(val);
jb.type = jbvString;
/* Same as timestamp_out(), but forcing DateStyle */
if (TIMESTAMP_NOT_FINITE(timestamp))
{
jb.val.string.len = strlen(DT_INFINITY);
jb.val.string.val = pstrdup(DT_INFINITY);
}
EncodeSpecialTimestamp(timestamp, buf);
else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
{
EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
jb.val.string.len = strlen(buf);
jb.val.string.val = pstrdup(buf);
}
else
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
jb.type = jbvString;
jb.val.string.len = strlen(buf);
jb.val.string.val = pstrdup(buf);
}
break;
case JSONBTYPE_TIMESTAMPTZ:
@ -852,24 +835,19 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
const char *tzn = NULL;
char buf[MAXDATELEN + 1];
timestamp = DatumGetTimestamp(val);
jb.type = jbvString;
timestamp = DatumGetTimestampTz(val);
/* Same as timestamptz_out(), but forcing DateStyle */
if (TIMESTAMP_NOT_FINITE(timestamp))
{
jb.val.string.len = strlen(DT_INFINITY);
jb.val.string.val = pstrdup(DT_INFINITY);
}
EncodeSpecialTimestamp(timestamp, buf);
else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
{
EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
jb.val.string.len = strlen(buf);
jb.val.string.val = pstrdup(buf);
}
else
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
jb.type = jbvString;
jb.val.string.len = strlen(buf);
jb.val.string.val = pstrdup(buf);
}
break;
case JSONBTYPE_JSONCAST: