Skip to content

Commit

Permalink
Merge #34412
Browse files Browse the repository at this point in the history
34412: sql: JSON-ify timestamps using RFC3339 format r=bobvawter a=bobvawter

Currently, when converting a timestamp value to a JSON string, we use
CockroachDB's customary string format, which is RFC3339 without the T separator.
This creates some compatibility problems with JSON consumers which expect to
receive exactly RFC3339-formatted timestamp value.

The output in the logictest has been cross-checked against a PostgreSQL 10
database.

Fixes #34372

Release note (sql change): Formatting of timestamps as JSON strings has been
changed to always use the RFC3339 format instead of Cockroach's customary
format. Users can now expect to see a `T` separator instead of a space between
the date and time components.

Co-authored-by: Bob Vawter <[email protected]>
  • Loading branch information
craig[bot] and bobvawter committed Jan 30, 2019
2 parents 856ae2f + 215bdbb commit b2a359f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
18 changes: 14 additions & 4 deletions pkg/sql/logictest/testdata/logic_test/json_builtins
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,17 @@ SELECT to_json(ARRAY[[1, 2], [3, 4]])
query T
SELECT to_json('2014-05-28 12:22:35.614298'::TIMESTAMP)
----
"2014-05-28 12:22:35.614298+00:00"
"2014-05-28T12:22:35.614298"

query T
SELECT to_json('2014-05-28 12:22:35.614298-04'::TIMESTAMPTZ)
----
"2014-05-28 12:22:35.614298-04:00"
"2014-05-28T12:22:35.614298-04:00"

query T
SELECT to_json('2014-05-28 12:22:35.614298-04'::TIMESTAMP)
----
"2014-05-28T12:22:35.614298"

query T
SELECT to_json('2014-05-28'::DATE)
Expand Down Expand Up @@ -279,12 +284,17 @@ SELECT to_jsonb(ARRAY[[1, 2], [3, 4]])
query T
SELECT to_jsonb('2014-05-28 12:22:35.614298'::TIMESTAMP)
----
"2014-05-28 12:22:35.614298+00:00"
"2014-05-28T12:22:35.614298"

query T
SELECT to_jsonb('2014-05-28 12:22:35.614298-04'::TIMESTAMPTZ)
----
"2014-05-28 12:22:35.614298-04:00"
"2014-05-28T12:22:35.614298-04:00"

query T
SELECT to_jsonb('2014-05-28 12:22:35.614298-04'::TIMESTAMP)
----
"2014-05-28T12:22:35.614298"

query T
SELECT to_jsonb('2014-05-28'::DATE)
Expand Down
11 changes: 10 additions & 1 deletion pkg/sql/sem/tree/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -2455,7 +2455,16 @@ func AsJSON(d Datum) (json.JSON, error) {
builder.Add(fmt.Sprintf("f%d", i+1), j)
}
return builder.Build(), nil
case *DTimestamp, *DTimestampTZ, *DDate, *DUuid, *DOid, *DInterval, *DBytes, *DIPAddr, *DTime, *DBitArray:
case *DTimestampTZ:
// Our normal timestamp-formatting code uses a variation on RFC 3339,
// without the T separator. This causes some compatibility problems
// with certain JSON consumers, so we'll use an alternate formatting
// path here to maintain consistency with PostgreSQL.
return json.FromString(t.Time.Format(time.RFC3339Nano)), nil
case *DTimestamp:
// This is RFC3339Nano, but without the TZ fields.
return json.FromString(t.UTC().Format("2006-01-02T15:04:05.999999999")), nil
case *DDate, *DUuid, *DOid, *DInterval, *DBytes, *DIPAddr, *DTime, *DBitArray:
return json.FromString(AsStringWithFlags(t, FmtBareStrings)), nil
default:
if d == DNull {
Expand Down

0 comments on commit b2a359f

Please sign in to comment.