Commit d4355425 authored by Tom Lane's avatar Tom Lane

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

Commit bda76c1c 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 ...)
parent 984ae04a
...@@ -40,7 +40,6 @@ ...@@ -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) ...@@ -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))
......
...@@ -32,9 +32,6 @@ ...@@ -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
...@@ -365,8 +362,8 @@ json_count_array_elements(JsonLexContext *lex) ...@@ -365,8 +362,8 @@ json_count_array_elements(JsonLexContext *lex)
/* /*
* 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 */
...@@ -1492,19 +1489,16 @@ datum_to_json(Datum val, bool is_null, StringInfo result, ...@@ -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, ...@@ -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, ...@@ -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:
...@@ -1905,7 +1889,7 @@ json_agg_transfn(PG_FUNCTION_ARGS) ...@@ -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
...@@ -2007,7 +1991,7 @@ json_object_agg_transfn(PG_FUNCTION_ARGS) ...@@ -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) ...@@ -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, "{ ");
......
...@@ -28,14 +28,6 @@ ...@@ -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,22 +790,19 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result, ...@@ -798,22 +790,19 @@ 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.type = jbvString;
jb.val.string.len = strlen(buf); jb.val.string.len = strlen(buf);
jb.val.string.val = pstrdup(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, ...@@ -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, ...@@ -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:
......
...@@ -68,7 +68,6 @@ typedef struct ...@@ -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) ...@@ -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))
......
...@@ -90,6 +90,7 @@ typedef struct ...@@ -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);
......
...@@ -326,6 +326,7 @@ extern void EncodeDateOnly(struct pg_tm * tm, int style, char *str); ...@@ -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);
......
...@@ -418,18 +418,36 @@ select to_json(date 'Infinity'); ...@@ -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,
......
...@@ -314,18 +314,36 @@ select to_jsonb(date 'Infinity'); ...@@ -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
......
...@@ -116,8 +116,11 @@ COMMIT; ...@@ -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
......
...@@ -76,8 +76,11 @@ COMMIT; ...@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment