Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pgwire: lazily populate second timezone offset #57265

Merged
merged 1 commit into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 0 additions & 22 deletions pkg/sql/pgwire/testdata/pgtest/timestamptz

This file was deleted.

58 changes: 58 additions & 0 deletions pkg/sql/pgwire/testdata/pgtest/timezone
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
send
Query {"String": "SELECT '00:00:00+01:01'::\"timetz\""}
----

until
ReadyForQuery
----
{"Type":"RowDescription","Fields":[{"Name":"timetz","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":1266,"DataTypeSize":16,"TypeModifier":-1,"Format":0}]}
{"Type":"DataRow","Values":[{"text":"00:00:00+01:01"}]}
{"Type":"CommandComplete","CommandTag":"SELECT 1"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send
Query {"String": "SELECT '00:00:00+01:01:03'::\"timetz\""}
----

until
ReadyForQuery
----
{"Type":"RowDescription","Fields":[{"Name":"timetz","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":1266,"DataTypeSize":16,"TypeModifier":-1,"Format":0}]}
{"Type":"DataRow","Values":[{"text":"00:00:00+01:01:03"}]}
{"Type":"CommandComplete","CommandTag":"SELECT 1"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send
Query {"String": "SELECT '1882-05-23T00:00:00'::\"timestamptz\""}
----

until
ReadyForQuery
----
{"Type":"RowDescription","Fields":[{"Name":"timestamptz","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":1184,"DataTypeSize":24,"TypeModifier":-1,"Format":0}]}
{"Type":"DataRow","Values":[{"text":"1882-05-23 00:00:00+00:00"}]}
{"Type":"CommandComplete","CommandTag":"SELECT 1"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send
Query {"String": "SET TIME ZONE \"America/Chicago\""}
----

until
ReadyForQuery
----
{"Type":"ParameterStatus","Name":"TimeZone","Value":"America/Chicago"}
{"Type":"CommandComplete","CommandTag":"SET"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send
Query {"String": "SELECT '1882-05-23T00:00:00-05:51'::\"timestamptz\""}
----

until
ReadyForQuery
----
{"Type":"RowDescription","Fields":[{"Name":"timestamptz","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":1184,"DataTypeSize":24,"TypeModifier":-1,"Format":0}]}
{"Type":"DataRow","Values":[{"text":"1882-05-23 00:00:24-05:50:36"}]}
{"Type":"CommandComplete","CommandTag":"SELECT 1"}
{"Type":"ReadyForQuery","TxStatus":"I"}
11 changes: 9 additions & 2 deletions pkg/sql/pgwire/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ const (
pgTimeTZFormat = pgTimeFormat + "-07:00"
pgDateFormat = "2006-01-02"
pgTimeStampFormatNoOffset = pgDateFormat + " " + pgTimeFormat
pgTimeStampFormat = pgTimeStampFormatNoOffset + "-07:00:00"
pgTimeStampFormat = pgTimeStampFormatNoOffset + "-07:00"
pgTime2400Format = "24:00:00"
)

Expand All @@ -551,7 +551,11 @@ func formatTime(t timeofday.TimeOfDay, tmp []byte) []byte {
// Note it does not understand the "second" component of the offset as lib/pq
// cannot parse it.
func formatTimeTZ(t timetz.TimeTZ, tmp []byte) []byte {
ret := t.ToTime().AppendFormat(tmp, pgTimeTZFormat)
format := pgTimeTZFormat
if t.OffsetSecs%60 != 0 {
format += ":00"
}
ret := t.ToTime().AppendFormat(tmp, format)
// time.Time's AppendFormat does not recognize 2400, so special case it accordingly.
if t.TimeOfDay == timeofday.Time2400 {
// It instead reads 00:00:00. Replace that text.
Expand All @@ -567,6 +571,9 @@ func formatTs(t time.Time, offset *time.Location, tmp []byte) (b []byte) {
var format string
if offset != nil {
format = pgTimeStampFormat
if _, offset := t.In(offset).Zone(); offset%60 != 0 {
format += ":00"
}
} else {
format = pgTimeStampFormatNoOffset
}
Expand Down