Skip to content

Commit

Permalink
Merge #28112
Browse files Browse the repository at this point in the history
28112: sql: strip timezone info from DTimestamp parsing r=jordanlewis a=jordanlewis

Previously, parsing a DTimestamp would silently retain timezone
information, even though it wouldn't get printed out in many cases. This
led to problems with timestamp serialization in DistSQL mode in
particular.

Fixes #28110.

Release note (bug fix): ensure that the timestamp data type never
retains a timezone and renders consistently across a distributed
computation flow.

Co-authored-by: Jordan Lewis <[email protected]>
  • Loading branch information
craig[bot] and jordanlewis committed Jul 31, 2018
2 parents 4065120 + 0b6baa3 commit cbe3d10
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 32 deletions.
13 changes: 6 additions & 7 deletions pkg/sql/logictest/testdata/logic_test/datetime
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ INSERT INTO t VALUES
('2015-08-29 23:10:09.98763', '2015-08-29', '234h45m2s234ms')

query TTT
SELECT * FROM t WHERE a = '2015-08-25 05:45:45.53453+01:00'::timestamp
SELECT * FROM t WHERE a = '2015-08-25 04:45:45.53453+01:00'::timestamp
----
2015-08-25 04:45:45.53453 +0000 +0000 2015-08-25 00:00:00 +0000 +0000 2h45m2s234ms

# insert duplicate value with different time zone offset
statement error duplicate key value \(a\)=\('2015-08-30 03:34:45\.34567\+00:00'\) violates unique constraint "primary"
INSERT INTO t VALUES
('2015-08-29 20:34:45.34567-07:00', '2015-08-31', '35h2s')
('2015-08-30 03:34:45.34567-07:00', '2015-08-31', '35h2s')

# Check that time/date/interval representations outside of the index are okay.
statement ok
Expand Down Expand Up @@ -60,7 +60,7 @@ query ITTTT
SELECT * FROM u ORDER BY a
----
123 2015-08-30 03:34:45.34567 +0000 +0000 2015-08-29 22:34:45.34567 -0500 -0500 2015-08-30 00:00:00 +0000 +0000 34h2s
234 2015-08-25 06:45:45.53453 +0000 +0000 2015-08-25 01:45:45.53453 -0500 -0500 2015-08-25 00:00:00 +0000 +0000 2h45m2s234ms
234 2015-08-25 04:45:45.53453 +0000 +0000 2015-08-25 01:45:45.53453 -0500 -0500 2015-08-25 00:00:00 +0000 +0000 2h45m2s234ms
345 2015-08-29 23:10:09.98763 +0000 +0000 2015-08-29 23:10:09.98763 -0500 -0500 2015-08-29 00:00:00 +0000 +0000 234h45m2s234ms
456 2015-08-29 23:10:09.98763 +0000 +0000 2015-08-29 18:10:09.98763 -0500 -0500 2015-08-29 00:00:00 +0000 +0000 234h45m2s234ms

Expand All @@ -71,7 +71,7 @@ query ITTTT
SELECT * FROM u ORDER BY a
----
123 2015-08-30 03:34:45.34567 +0000 +0000 2015-08-30 03:34:45.34567 +0000 +0000 2015-08-30 00:00:00 +0000 +0000 34h2s
234 2015-08-25 06:45:45.53453 +0000 +0000 2015-08-25 06:45:45.53453 +0000 +0000 2015-08-25 00:00:00 +0000 +0000 2h45m2s234ms
234 2015-08-25 04:45:45.53453 +0000 +0000 2015-08-25 06:45:45.53453 +0000 +0000 2015-08-25 00:00:00 +0000 +0000 2h45m2s234ms
345 2015-08-29 23:10:09.98763 +0000 +0000 2015-08-30 04:10:09.98763 +0000 +0000 2015-08-29 00:00:00 +0000 +0000 234h45m2s234ms
456 2015-08-29 23:10:09.98763 +0000 +0000 2015-08-29 23:10:09.98763 +0000 +0000 2015-08-29 00:00:00 +0000 +0000 234h45m2s234ms

Expand All @@ -86,7 +86,7 @@ SELECT max(b), max(c), max(d), max(e) FROM u
query TTTT
SELECT min(b), min(c), min(d), min(e) FROM u
----
2015-08-25 06:45:45.53453 +0000 +0000 2015-08-25 01:45:45.53453 -0500 -0500 2015-08-25 00:00:00 +0000 +0000 2h45m2s234ms
2015-08-25 04:45:45.53453 +0000 +0000 2015-08-25 01:45:45.53453 -0500 -0500 2015-08-25 00:00:00 +0000 +0000 2h45m2s234ms

query BB
SELECT now() < now() + '1m'::interval, now() <= now() + '1m'::interval
Expand Down Expand Up @@ -941,7 +941,7 @@ query ITT
SELECT a, b, c FROM tz ORDER BY a
----
1 2015-08-30 03:34:45 +0000 +0000 2015-08-30 01:34:45 -0200 -0200
2 2015-08-30 02:34:45 +0000 +0000 2015-08-30 00:34:45 -0200 -0200
2 2015-08-30 03:34:45 +0000 +0000 2015-08-30 00:34:45 -0200 -0200

query TTTT
SELECT b + interval '1m', interval '1m' + b, c + interval '1m', interval '1m' + c FROM tz WHERE a = 1
Expand All @@ -968,7 +968,6 @@ query I rowsort
SELECT a FROM tz WHERE b = c::timestamp
----
1
2

# reset for what follows.
statement ok
Expand Down
17 changes: 17 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/timestamp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# LogicTest: local local-opt local-parallel-stmts fakedist fakedist-opt fakedist-metadata

query T
SELECT '2000-05-05 10:00:00+03':::TIMESTAMP
----
2000-05-05 10:00:00 +0000 +0000

statement ok
CREATE TABLE a (a int); INSERT INTO a VALUES(1)

# Ensure that timestamp serialization doesn't break even if the computation is
# distributed: #28110.

query T
SELECT '2000-05-05 10:00:00+03':::TIMESTAMP FROM a
----
2000-05-05 10:00:00 +0000 +0000
2 changes: 1 addition & 1 deletion pkg/sql/logictest/testdata/planner_test/select
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ INSERT INTO dt VALUES
('2015-08-29 23:10:09.98763', '2015-08-29', '234h45m2s234ms')

statement ok
SET tracing = on,kv,results; SELECT * FROM dt WHERE a = '2015-08-25 06:45:45.53453+02:00'::timestamp; SET tracing = off
SET tracing = on,kv,results; SELECT * FROM dt WHERE a = '2015-08-25 04:45:45.53453+02:00'::timestamp; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/exec/execbuilder/testdata/select
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ INSERT INTO dt VALUES
('2015-08-29 23:10:09.98763', '2015-08-29', '234h45m2s234ms')

statement ok
SET tracing = on,kv,results; SELECT * FROM dt WHERE a = '2015-08-25 06:45:45.53453+02:00'::timestamp; SET tracing = off
SET tracing = on,kv,results; SELECT * FROM dt WHERE a = '2015-08-25 04:45:45.53453+02:00'::timestamp; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION]
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/pgwire/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestParseTs(t *testing.T) {
{"2006-07-08T00:00:00.000000123Z", time.Date(2006, 7, 8, 0, 0, 0, 123, time.FixedZone("UTC", 0))},

// The format accepted by pq.ParseTimestamp.
{"2001-02-03 04:05:06.123-07", time.Date(2001, time.February, 3, 4, 5, 6, 123000000, time.FixedZone("", -7*60*60))},
{"2001-02-03 04:05:06.123-07", time.Date(2001, time.February, 3, 4, 5, 6, 123000000, time.FixedZone("", 0))},
}

for i, test := range parseTsTests {
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/sem/tree/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -1873,6 +1873,9 @@ func ParseDTimestamp(s string, precision time.Duration) (*DTimestamp, error) {
if err != nil {
return nil, err
}
// Truncate the timezone. DTimestamp doesn't carry its timezone around.
_, offset := t.Zone()
t = duration.Add(t, duration.FromInt64(int64(offset))).UTC()
return MakeDTimestamp(t, precision), nil
}

Expand Down
14 changes: 7 additions & 7 deletions pkg/sql/sem/tree/datum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,25 +578,25 @@ func TestParseDTimestamp(t *testing.T) {
{"2001-02-03 04:05:06.12345", time.Date(2001, time.February, 3, 4, 5, 6, 123450000, time.FixedZone("", 0))},
{"2001-02-03 04:05:06.123456", time.Date(2001, time.February, 3, 4, 5, 6, 123456000, time.FixedZone("", 0))},
{"2001-02-03 04:05:06.123-07", time.Date(2001, time.February, 3, 4, 5, 6, 123000000,
time.FixedZone("", -7*60*60))},
time.FixedZone("", 0))},
{"2001-02-03 04:05:06-07", time.Date(2001, time.February, 3, 4, 5, 6, 0,
time.FixedZone("", -7*60*60))},
time.FixedZone("", 0))},
{"2001-02-03 04:05:06-07:42", time.Date(2001, time.February, 3, 4, 5, 6, 0,
time.FixedZone("", -(7*60*60+42*60)))},
time.FixedZone("", 0))},
{"2001-02-03 04:05:06-07:30:09", time.Date(2001, time.February, 3, 4, 5, 6, 0,
time.FixedZone("", -(7*60*60+30*60+9)))},
time.FixedZone("", 0))},
{"2001-02-03 04:05:06+07", time.Date(2001, time.February, 3, 4, 5, 6, 0,
time.FixedZone("", 7*60*60))},
time.FixedZone("", 0))},
{"2001-02-03 04:0:06", time.Date(2001, time.February, 3, 4, 0, 6, 0,
time.FixedZone("", 0))},
{"2001-02-03 0:0:06", time.Date(2001, time.February, 3, 0, 0, 6, 0,
time.FixedZone("", 0))},
{"2001-02-03 4:05:0", time.Date(2001, time.February, 3, 4, 5, 0, 0,
time.FixedZone("", 0))},
{"2001-02-03 4:05:0-07:0:00", time.Date(2001, time.February, 3, 4, 5, 0, 0,
time.FixedZone("", -7*60*60))},
time.FixedZone("", 0))},
{"2001-02-03 4:0:6 +3:0:0", time.Date(2001, time.February, 3, 4, 0, 6, 0,
time.FixedZone("", 3*60*60))},
time.FixedZone("", 0))},
}
for _, td := range testData {
actual, err := tree.ParseDTimestamp(td.str, time.Nanosecond)
Expand Down
30 changes: 15 additions & 15 deletions pkg/sql/sem/tree/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1079,15 +1079,15 @@ func TestEval(t *testing.T) {
{`'2010-09-28'::timestamptz`, `'2010-09-28 00:00:00+00:00'`},
{`('2010-09-28 12:00:00.1'::timestamp)::date`, `'2010-09-28'`},
{`'2010-09-28 12:00:00.1'::timestamp`, `'2010-09-28 12:00:00.1+00:00'`},
{`'2010-09-28 12:00:00.1+02:00'::timestamp`, `'2010-09-28 10:00:00.1+00:00'`},
{`'2010-09-28 12:00:00.524000 +02:00:00'::timestamp`, `'2010-09-28 10:00:00.524+00:00'`},
{`'2010-09-28 12:00:00.1-07:00'::timestamp`, `'2010-09-28 19:00:00.1+00:00'`},
{`'2010-09-28 12:00:00.1+02:00'::timestamp`, `'2010-09-28 12:00:00.1+00:00'`},
{`'2010-09-28 12:00:00.524000 +02:00:00'::timestamp`, `'2010-09-28 12:00:00.524+00:00'`},
{`'2010-09-28 12:00:00.1-07:00'::timestamp`, `'2010-09-28 12:00:00.1+00:00'`},
{`'2010-09-28T12:00:00'::timestamp`, `'2010-09-28 12:00:00+00:00'`},
{`'2010-09-28T12:00:00Z'::timestamp`, `'2010-09-28 12:00:00+00:00'`},
{`'2010-09-28T12:00:00.1'::timestamp`, `'2010-09-28 12:00:00.1+00:00'`},
{`('2010-09-28'::date)::timestamp`, `'2010-09-28 00:00:00+00:00'`},
{`'2010-09-28 12:00:00.1-04'::timestamp`, `'2010-09-28 16:00:00.1+00:00'`},
{`'2010-09-28 12:00:00.1-04'::timestamp::text`, `'2010-09-28 16:00:00.1+00:00'`},
{`'2010-09-28 12:00:00.1-04'::timestamp`, `'2010-09-28 12:00:00.1+00:00'`},
{`'2010-09-28 12:00:00.1-04'::timestamp::text`, `'2010-09-28 12:00:00.1+00:00'`},
{`'2010-09-28 12:00:00.1-04'::timestamptz::text`, `'2010-09-28 12:00:00.1-04:00'`},
{`'12h2m1s23ms'::interval`, `'12h2m1s23ms'`},
{`'12h2m1s23ms'::interval::text`, `'12h2m1s23ms'`},
Expand All @@ -1106,16 +1106,16 @@ func TestEval(t *testing.T) {
{`'12:00:00+01'::timetz + '1s'::interval`, `'12:00:01+01:00'`},
{`'1s'::interval + '12:00:00+01'::timetz`, `'12:00:01+01:00'`},
{`'12:00:01+01'::timetz - '12:00:00+01'::time`, `'00:00:01+01:00'`},
{`'2010-09-28 12:00:00.1-04:00'::timestamp + '12h2m'::interval`, `'2010-09-29 04:02:00.1+00:00'`},
{`'12h2m'::interval + '2010-09-28 12:00:00.1-04:00'::timestamp`, `'2010-09-29 04:02:00.1+00:00'`},
{`'12 hours 2 minutes'::interval + '2010-09-28 12:00:00.1-04:00'::timestamp`, `'2010-09-29 04:02:00.1+00:00'`},
{`'PT12H2M'::interval + '2010-09-28 12:00:00.1-04:00'::timestamp`, `'2010-09-29 04:02:00.1+00:00'`},
{`'12:2'::interval + '2010-09-28 12:00:00.1-04:00'::timestamp`, `'2010-09-29 04:02:00.1+00:00'`},
{`'2010-09-28 12:00:00.1-04:00'::timestamp - '12h2m'::interval`, `'2010-09-28 03:58:00.1+00:00'`},
{`'2010-09-28 12:00:00.1-04:00'::timestamp - '12 hours 2 minutes'::interval`, `'2010-09-28 03:58:00.1+00:00'`},
{`'2010-09-28 12:00:00.1-04:00'::timestamp - 'PT12H2M'::interval`, `'2010-09-28 03:58:00.1+00:00'`},
{`'2010-09-28 12:00:00.1-04:00'::timestamp - '12:2'::interval`, `'2010-09-28 03:58:00.1+00:00'`},
{`'2010-09-28 12:00:00.1-04:00'::timestamp - '2010-09-28 12:00:00.1+00:00'::timestamp`, `'4h'`},
{`'2010-09-28 12:00:00.1-04:00'::timestamp + '12h2m'::interval`, `'2010-09-29 00:02:00.1+00:00'`},
{`'12h2m'::interval + '2010-09-28 12:00:00.1-04:00'::timestamp`, `'2010-09-29 00:02:00.1+00:00'`},
{`'12 hours 2 minutes'::interval + '2010-09-28 12:00:00.1-04:00'::timestamp`, `'2010-09-29 00:02:00.1+00:00'`},
{`'PT12H2M'::interval + '2010-09-28 12:00:00.1-04:00'::timestamp`, `'2010-09-29 00:02:00.1+00:00'`},
{`'12:2'::interval + '2010-09-28 12:00:00.1-04:00'::timestamp`, `'2010-09-29 00:02:00.1+00:00'`},
{`'2010-09-28 12:00:00.1-04:00'::timestamp - '12h2m'::interval`, `'2010-09-27 23:58:00.1+00:00'`},
{`'2010-09-28 12:00:00.1-04:00'::timestamp - '12 hours 2 minutes'::interval`, `'2010-09-27 23:58:00.1+00:00'`},
{`'2010-09-28 12:00:00.1-04:00'::timestamp - 'PT12H2M'::interval`, `'2010-09-27 23:58:00.1+00:00'`},
{`'2010-09-28 12:00:00.1-04:00'::timestamp - '12:2'::interval`, `'2010-09-27 23:58:00.1+00:00'`},
{`'2010-09-28 12:00:00.1-04:00'::timestamp - '2010-09-28 12:00:00.1+00:00'::timestamp`, `'0s'`},
{`'1970-01-01 00:01:00.123456-00:00'::timestamp::int`, `60`},
{`'1970-01-01 00:01:00.123456-00:00'::timestamptz::int`, `60`},
{`'1970-01-10'::date::int`, `9`},
Expand Down

0 comments on commit cbe3d10

Please sign in to comment.