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

@ -40,7 +40,6 @@
#endif #endif
static void EncodeSpecialDate(DateADT dt, char *str);
static int time2tm(TimeADT time, struct pg_tm * tm, fsec_t *fsec); static int time2tm(TimeADT time, struct pg_tm * tm, fsec_t *fsec);
static int timetz2tm(TimeTzADT *time, struct pg_tm * tm, fsec_t *fsec, int *tzp); static int timetz2tm(TimeTzADT *time, struct pg_tm * tm, fsec_t *fsec, int *tzp);
static int tm2time(struct pg_tm * tm, fsec_t fsec, TimeADT *result); static int tm2time(struct pg_tm * tm, fsec_t fsec, TimeADT *result);
@ -273,7 +272,7 @@ make_date(PG_FUNCTION_ARGS)
/* /*
* Convert reserved date values to string. * Convert reserved date values to string.
*/ */
static void void
EncodeSpecialDate(DateADT dt, char *str) EncodeSpecialDate(DateADT dt, char *str)
{ {
if (DATE_IS_NOBEGIN(dt)) if (DATE_IS_NOBEGIN(dt))

View File

@ -32,9 +32,6 @@
#include "utils/typcache.h" #include "utils/typcache.h"
#include "utils/syscache.h" #include "utils/syscache.h"
/* String to output for infinite dates and timestamps */
#define DT_INFINITY "\"infinity\""
/* /*
* The context of the parser is maintained by the recursive descent * The context of the parser is maintained by the recursive descent
* mechanism, but is passed explicitly to the error reporting routine * mechanism, but is passed explicitly to the error reporting routine
@ -70,11 +67,11 @@ typedef enum /* type categories for datum_to_json */
typedef struct JsonAggState typedef struct JsonAggState
{ {
StringInfo str; StringInfo str;
JsonTypeCategory key_category; JsonTypeCategory key_category;
Oid key_output_func; Oid key_output_func;
JsonTypeCategory val_category; JsonTypeCategory val_category;
Oid val_output_func; Oid val_output_func;
} JsonAggState; } JsonAggState;
static inline void json_lex(JsonLexContext *lex); static inline void json_lex(JsonLexContext *lex);
@ -360,16 +357,16 @@ pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
int int
json_count_array_elements(JsonLexContext *lex) json_count_array_elements(JsonLexContext *lex)
{ {
JsonLexContext copylex; JsonLexContext copylex;
int count; int count;
/* /*
* It's safe to do this with a shallow copy because the lexical routines * It's safe to do this with a shallow copy because the lexical routines
* don't scribble on the input. They do scribble on the other pointers etc, * don't scribble on the input. They do scribble on the other pointers
* so doing this with a copy makes that safe. * etc, so doing this with a copy makes that safe.
*/ */
memcpy(&copylex, lex, sizeof(JsonLexContext)); memcpy(&copylex, lex, sizeof(JsonLexContext));
copylex.strval = NULL; /* not interested in values here */ copylex.strval = NULL; /* not interested in values here */
copylex.lex_level++; copylex.lex_level++;
count = 0; count = 0;
@ -1492,19 +1489,16 @@ datum_to_json(Datum val, bool is_null, StringInfo result,
char buf[MAXDATELEN + 1]; char buf[MAXDATELEN + 1];
date = DatumGetDateADT(val); date = DatumGetDateADT(val);
/* Same as date_out(), but forcing DateStyle */
if (DATE_NOT_FINITE(date)) if (DATE_NOT_FINITE(date))
{ EncodeSpecialDate(date, buf);
/* we have to format infinity ourselves */
appendStringInfoString(result, DT_INFINITY);
}
else else
{ {
j2date(date + POSTGRES_EPOCH_JDATE, j2date(date + POSTGRES_EPOCH_JDATE,
&(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday)); &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
EncodeDateOnly(&tm, USE_XSD_DATES, buf); EncodeDateOnly(&tm, USE_XSD_DATES, buf);
appendStringInfo(result, "\"%s\"", buf);
} }
appendStringInfo(result, "\"%s\"", buf);
} }
break; break;
case JSONTYPE_TIMESTAMP: case JSONTYPE_TIMESTAMP:
@ -1515,21 +1509,16 @@ datum_to_json(Datum val, bool is_null, StringInfo result,
char buf[MAXDATELEN + 1]; char buf[MAXDATELEN + 1];
timestamp = DatumGetTimestamp(val); timestamp = DatumGetTimestamp(val);
/* Same as timestamp_out(), but forcing DateStyle */
if (TIMESTAMP_NOT_FINITE(timestamp)) if (TIMESTAMP_NOT_FINITE(timestamp))
{ EncodeSpecialTimestamp(timestamp, buf);
/* we have to format infinity ourselves */
appendStringInfoString(result, DT_INFINITY);
}
else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0) else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
{
EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf); EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
appendStringInfo(result, "\"%s\"", buf);
}
else else
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range"))); errmsg("timestamp out of range")));
appendStringInfo(result, "\"%s\"", buf);
} }
break; break;
case JSONTYPE_TIMESTAMPTZ: case JSONTYPE_TIMESTAMPTZ:
@ -1541,22 +1530,17 @@ datum_to_json(Datum val, bool is_null, StringInfo result,
const char *tzn = NULL; const char *tzn = NULL;
char buf[MAXDATELEN + 1]; char buf[MAXDATELEN + 1];
timestamp = DatumGetTimestamp(val); timestamp = DatumGetTimestampTz(val);
/* Same as timestamptz_out(), but forcing DateStyle */
if (TIMESTAMP_NOT_FINITE(timestamp)) if (TIMESTAMP_NOT_FINITE(timestamp))
{ EncodeSpecialTimestamp(timestamp, buf);
/* we have to format infinity ourselves */
appendStringInfoString(result, DT_INFINITY);
}
else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0) else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
{
EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf); EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
appendStringInfo(result, "\"%s\"", buf);
}
else else
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range"))); errmsg("timestamp out of range")));
appendStringInfo(result, "\"%s\"", buf);
} }
break; break;
case JSONTYPE_JSON: case JSONTYPE_JSON:
@ -1875,7 +1859,7 @@ json_agg_transfn(PG_FUNCTION_ARGS)
{ {
MemoryContext aggcontext, MemoryContext aggcontext,
oldcontext; oldcontext;
JsonAggState *state; JsonAggState *state;
Datum val; Datum val;
if (!AggCheckCallContext(fcinfo, &aggcontext)) if (!AggCheckCallContext(fcinfo, &aggcontext))
@ -1886,7 +1870,7 @@ json_agg_transfn(PG_FUNCTION_ARGS)
if (PG_ARGISNULL(0)) if (PG_ARGISNULL(0))
{ {
Oid arg_type = get_fn_expr_argtype(fcinfo->flinfo, 1); Oid arg_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
if (arg_type == InvalidOid) if (arg_type == InvalidOid)
ereport(ERROR, ereport(ERROR,
@ -1905,7 +1889,7 @@ json_agg_transfn(PG_FUNCTION_ARGS)
MemoryContextSwitchTo(oldcontext); MemoryContextSwitchTo(oldcontext);
appendStringInfoChar(state->str, '['); appendStringInfoChar(state->str, '[');
json_categorize_type(arg_type,&state->val_category, json_categorize_type(arg_type, &state->val_category,
&state->val_output_func); &state->val_output_func);
} }
else else
@ -1949,7 +1933,7 @@ json_agg_transfn(PG_FUNCTION_ARGS)
Datum Datum
json_agg_finalfn(PG_FUNCTION_ARGS) json_agg_finalfn(PG_FUNCTION_ARGS)
{ {
JsonAggState *state; JsonAggState *state;
/* cannot be called directly because of internal-type argument */ /* cannot be called directly because of internal-type argument */
Assert(AggCheckCallContext(fcinfo, NULL)); Assert(AggCheckCallContext(fcinfo, NULL));
@ -1976,7 +1960,7 @@ json_object_agg_transfn(PG_FUNCTION_ARGS)
{ {
MemoryContext aggcontext, MemoryContext aggcontext,
oldcontext; oldcontext;
JsonAggState *state; JsonAggState *state;
Datum arg; Datum arg;
if (!AggCheckCallContext(fcinfo, &aggcontext)) if (!AggCheckCallContext(fcinfo, &aggcontext))
@ -2007,7 +1991,7 @@ json_object_agg_transfn(PG_FUNCTION_ARGS)
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("could not determine data type for argument 1"))); errmsg("could not determine data type for argument 1")));
json_categorize_type(arg_type,&state->key_category, json_categorize_type(arg_type, &state->key_category,
&state->key_output_func); &state->key_output_func);
arg_type = get_fn_expr_argtype(fcinfo->flinfo, 2); arg_type = get_fn_expr_argtype(fcinfo->flinfo, 2);
@ -2017,7 +2001,7 @@ json_object_agg_transfn(PG_FUNCTION_ARGS)
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("could not determine data type for argument 2"))); errmsg("could not determine data type for argument 2")));
json_categorize_type(arg_type,&state->val_category, json_categorize_type(arg_type, &state->val_category,
&state->val_output_func); &state->val_output_func);
appendStringInfoString(state->str, "{ "); appendStringInfoString(state->str, "{ ");
@ -2065,7 +2049,7 @@ json_object_agg_transfn(PG_FUNCTION_ARGS)
Datum Datum
json_object_agg_finalfn(PG_FUNCTION_ARGS) json_object_agg_finalfn(PG_FUNCTION_ARGS)
{ {
JsonAggState *state; JsonAggState *state;
/* cannot be called directly because of internal-type argument */ /* cannot be called directly because of internal-type argument */
Assert(AggCheckCallContext(fcinfo, NULL)); Assert(AggCheckCallContext(fcinfo, NULL));

View File

@ -28,14 +28,6 @@
#include "utils/syscache.h" #include "utils/syscache.h"
#include "utils/typcache.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 typedef struct JsonbInState
{ {
JsonbParseState *parseState; JsonbParseState *parseState;
@ -798,21 +790,18 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
char buf[MAXDATELEN + 1]; char buf[MAXDATELEN + 1];
date = DatumGetDateADT(val); date = DatumGetDateADT(val);
jb.type = jbvString; /* Same as date_out(), but forcing DateStyle */
if (DATE_NOT_FINITE(date)) if (DATE_NOT_FINITE(date))
{ EncodeSpecialDate(date, buf);
jb.val.string.len = strlen(DT_INFINITY);
jb.val.string.val = pstrdup(DT_INFINITY);
}
else else
{ {
j2date(date + POSTGRES_EPOCH_JDATE, j2date(date + POSTGRES_EPOCH_JDATE,
&(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday)); &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
EncodeDateOnly(&tm, USE_XSD_DATES, buf); 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; break;
case JSONBTYPE_TIMESTAMP: case JSONBTYPE_TIMESTAMP:
@ -823,24 +812,18 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
char buf[MAXDATELEN + 1]; char buf[MAXDATELEN + 1];
timestamp = DatumGetTimestamp(val); timestamp = DatumGetTimestamp(val);
jb.type = jbvString; /* Same as timestamp_out(), but forcing DateStyle */
if (TIMESTAMP_NOT_FINITE(timestamp)) if (TIMESTAMP_NOT_FINITE(timestamp))
{ EncodeSpecialTimestamp(timestamp, buf);
jb.val.string.len = strlen(DT_INFINITY);
jb.val.string.val = pstrdup(DT_INFINITY);
}
else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0) else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
{
EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf); EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
jb.val.string.len = strlen(buf);
jb.val.string.val = pstrdup(buf);
}
else else
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range"))); errmsg("timestamp out of range")));
jb.type = jbvString;
jb.val.string.len = strlen(buf);
jb.val.string.val = pstrdup(buf);
} }
break; break;
case JSONBTYPE_TIMESTAMPTZ: case JSONBTYPE_TIMESTAMPTZ:
@ -852,24 +835,19 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
const char *tzn = NULL; const char *tzn = NULL;
char buf[MAXDATELEN + 1]; char buf[MAXDATELEN + 1];
timestamp = DatumGetTimestamp(val); timestamp = DatumGetTimestampTz(val);
jb.type = jbvString; /* Same as timestamptz_out(), but forcing DateStyle */
if (TIMESTAMP_NOT_FINITE(timestamp)) if (TIMESTAMP_NOT_FINITE(timestamp))
{ EncodeSpecialTimestamp(timestamp, buf);
jb.val.string.len = strlen(DT_INFINITY);
jb.val.string.val = pstrdup(DT_INFINITY);
}
else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0) else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
{
EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf); EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
jb.val.string.len = strlen(buf);
jb.val.string.val = pstrdup(buf);
}
else else
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range"))); errmsg("timestamp out of range")));
jb.type = jbvString;
jb.val.string.len = strlen(buf);
jb.val.string.val = pstrdup(buf);
} }
break; break;
case JSONBTYPE_JSONCAST: case JSONBTYPE_JSONCAST:

View File

@ -68,7 +68,6 @@ typedef struct
static TimeOffset time2t(const int hour, const int min, const int sec, const fsec_t fsec); static TimeOffset time2t(const int hour, const int min, const int sec, const fsec_t fsec);
static void EncodeSpecialTimestamp(Timestamp dt, char *str);
static Timestamp dt2local(Timestamp dt, int timezone); static Timestamp dt2local(Timestamp dt, int timezone);
static void AdjustTimestampForTypmod(Timestamp *time, int32 typmod); static void AdjustTimestampForTypmod(Timestamp *time, int32 typmod);
static void AdjustIntervalForTypmod(Interval *interval, int32 typmod); static void AdjustIntervalForTypmod(Interval *interval, int32 typmod);
@ -1500,7 +1499,7 @@ make_interval(PG_FUNCTION_ARGS)
/* EncodeSpecialTimestamp() /* EncodeSpecialTimestamp()
* Convert reserved timestamp data type to string. * Convert reserved timestamp data type to string.
*/ */
static void void
EncodeSpecialTimestamp(Timestamp dt, char *str) EncodeSpecialTimestamp(Timestamp dt, char *str)
{ {
if (TIMESTAMP_IS_NOBEGIN(dt)) if (TIMESTAMP_IS_NOBEGIN(dt))

View File

@ -90,6 +90,7 @@ typedef struct
/* date.c */ /* date.c */
extern double date2timestamp_no_overflow(DateADT dateVal); extern double date2timestamp_no_overflow(DateADT dateVal);
extern void EncodeSpecialDate(DateADT dt, char *str);
extern Datum date_in(PG_FUNCTION_ARGS); extern Datum date_in(PG_FUNCTION_ARGS);
extern Datum date_out(PG_FUNCTION_ARGS); extern Datum date_out(PG_FUNCTION_ARGS);

View File

@ -326,6 +326,7 @@ extern void EncodeDateOnly(struct pg_tm * tm, int style, char *str);
extern void EncodeTimeOnly(struct pg_tm * tm, fsec_t fsec, bool print_tz, int tz, int style, char *str); extern void EncodeTimeOnly(struct pg_tm * tm, fsec_t fsec, bool print_tz, int tz, int style, char *str);
extern void EncodeDateTime(struct pg_tm * tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str); extern void EncodeDateTime(struct pg_tm * tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str);
extern void EncodeInterval(struct pg_tm * tm, fsec_t fsec, int style, char *str); extern void EncodeInterval(struct pg_tm * tm, fsec_t fsec, int style, char *str);
extern void EncodeSpecialTimestamp(Timestamp dt, char *str);
extern int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc, extern int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc,
struct pg_tm * tm); struct pg_tm * tm);

View File

@ -418,18 +418,36 @@ select to_json(date 'Infinity');
"infinity" "infinity"
(1 row) (1 row)
select to_json(date '-Infinity');
to_json
-------------
"-infinity"
(1 row)
select to_json(timestamp 'Infinity'); select to_json(timestamp 'Infinity');
to_json to_json
------------ ------------
"infinity" "infinity"
(1 row) (1 row)
select to_json(timestamp '-Infinity');
to_json
-------------
"-infinity"
(1 row)
select to_json(timestamptz 'Infinity'); select to_json(timestamptz 'Infinity');
to_json to_json
------------ ------------
"infinity" "infinity"
(1 row) (1 row)
select to_json(timestamptz '-Infinity');
to_json
-------------
"-infinity"
(1 row)
--json_agg --json_agg
SELECT json_agg(q) SELECT json_agg(q)
FROM ( SELECT $$a$$ || x AS b, y AS c, FROM ( SELECT $$a$$ || x AS b, y AS c,

View File

@ -314,18 +314,36 @@ select to_jsonb(date 'Infinity');
"infinity" "infinity"
(1 row) (1 row)
select to_jsonb(date '-Infinity');
to_jsonb
-------------
"-infinity"
(1 row)
select to_jsonb(timestamp 'Infinity'); select to_jsonb(timestamp 'Infinity');
to_jsonb to_jsonb
------------ ------------
"infinity" "infinity"
(1 row) (1 row)
select to_jsonb(timestamp '-Infinity');
to_jsonb
-------------
"-infinity"
(1 row)
select to_jsonb(timestamptz 'Infinity'); select to_jsonb(timestamptz 'Infinity');
to_jsonb to_jsonb
------------ ------------
"infinity" "infinity"
(1 row) (1 row)
select to_jsonb(timestamptz '-Infinity');
to_jsonb
-------------
"-infinity"
(1 row)
--jsonb_agg --jsonb_agg
CREATE TEMP TABLE rows AS CREATE TEMP TABLE rows AS
SELECT x, 'txt' || x as y SELECT x, 'txt' || x as y

View File

@ -116,8 +116,11 @@ COMMIT;
select to_json(date '2014-05-28'); select to_json(date '2014-05-28');
select to_json(date 'Infinity'); select to_json(date 'Infinity');
select to_json(date '-Infinity');
select to_json(timestamp 'Infinity'); select to_json(timestamp 'Infinity');
select to_json(timestamp '-Infinity');
select to_json(timestamptz 'Infinity'); select to_json(timestamptz 'Infinity');
select to_json(timestamptz '-Infinity');
--json_agg --json_agg

View File

@ -76,8 +76,11 @@ COMMIT;
select to_jsonb(date '2014-05-28'); select to_jsonb(date '2014-05-28');
select to_jsonb(date 'Infinity'); select to_jsonb(date 'Infinity');
select to_jsonb(date '-Infinity');
select to_jsonb(timestamp 'Infinity'); select to_jsonb(timestamp 'Infinity');
select to_jsonb(timestamp '-Infinity');
select to_jsonb(timestamptz 'Infinity'); select to_jsonb(timestamptz 'Infinity');
select to_jsonb(timestamptz '-Infinity');
--jsonb_agg --jsonb_agg