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

sql: fix parsing of 0000-01-01 as Time/TimeTZ #42762

Merged
merged 1 commit into from
Dec 3, 2019

Conversation

otan
Copy link
Contributor

@otan otan commented Nov 25, 2019

Resolves #42749

Since lib/pq outputs time.Time for time datums as 0000-01-01, we
should be able to parse this in. However, pgdate library cannot do
that - so we hack around it for now by replacing the year for these
kinds of dates.

Release note (bug fix): Previously, attempting to parse 0000-01-01 00:00 when involving time did not work as pgdate does not
understand 0000 as a year. This PR will fix that behaviour.

@otan otan requested a review from a team November 25, 2019 22:59
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@otan otan force-pushed the otan-parse_time branch 3 times, most recently from b6d8728 to 16ab75a Compare November 26, 2019 23:38
Copy link
Contributor

@solongordon solongordon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm: All comments minor so feel free to take them or leave them.

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @otan)


pkg/sql/logictest/testdata/logic_test/time, line 350 at r1 (raw file):


query T
SELECT '0000-01-01 24:00:00'::time

Maybe worth casting it to string to check that it's actually 24:00 under the hood?


pkg/sql/sem/tree/datum.go, line 1876 at r1 (raw file):

	// Replace "0000-01-01" as a date (which lib/pq outputs) to
	// "1970-01-01", as pgdate cannot parse 0000-01-01.
	s = strings.ReplaceAll(s, "0000-01-01", "1970-01-01")

Minor: Perhaps cheaper to only check for 0000-01-01 at the beginning of the string.


pkg/util/timetz/timetz.go, line 42 at r1 (raw file):

	// timeTZHasTimeComponent determines whether there is a time component at all
	// in a given string.
	timeTZHasTimeComponent = regexp.MustCompile(`\d+:`)

Nit: This is equivalent to \d:, no?


pkg/util/timetz/timetz.go, line 104 at r1 (raw file):

		// Replace "0000-01-01" as a date (which lib/pq outputs) to
		// "1970-01-01", as pgdate cannot parse 0000-01-01.
		s = strings.ReplaceAll(s, "0000-01-01", "1970-01-01")

Again seems unnecessary to replace at other points besides the beginning of the string.

Copy link
Contributor Author

@otan otan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @solongordon)


pkg/sql/logictest/testdata/logic_test/time, line 350 at r1 (raw file):

Previously, solongordon (Solon) wrote…

Maybe worth casting it to string to check that it's actually 24:00 under the hood?

Done.


pkg/sql/sem/tree/datum.go, line 1876 at r1 (raw file):

Previously, solongordon (Solon) wrote…

Minor: Perhaps cheaper to only check for 0000-01-01 at the beginning of the string.

Done!


pkg/util/timetz/timetz.go, line 104 at r1 (raw file):

Previously, solongordon (Solon) wrote…

Again seems unnecessary to replace at other points besides the beginning of the string.

Done.

@otan otan force-pushed the otan-parse_time branch 6 times, most recently from 61e2a34 to 1cb2069 Compare December 3, 2019 00:12
@otan
Copy link
Contributor Author

otan commented Dec 3, 2019

bors r+

@craig
Copy link
Contributor

craig bot commented Dec 3, 2019

Merge conflict

Since `lib/pq` outputs `time.Time` for time datums as 0000-01-01, we
should be able to parse this in. However, `pgdate` library cannot do
that - so we hack around it for now by replacing the year for these
kinds of dates.

Release note (bug fix): Previously, attempting to parse `0000-01-01
00:00` when involving `time` did not work as `pgdate` does not
understand `0000` as a year. This PR will fix that behaviour.
@otan
Copy link
Contributor Author

otan commented Dec 3, 2019

bors r+

craig bot pushed a commit that referenced this pull request Dec 3, 2019
42762: sql: fix parsing of 0000-01-01 as Time/TimeTZ r=otan a=otan

Resolves #42749

Since `lib/pq` outputs `time.Time` for time datums as 0000-01-01, we
should be able to parse this in. However, `pgdate` library cannot do
that - so we hack around it for now by replacing the year for these
kinds of dates.

Release note (bug fix): Previously, attempting to parse `0000-01-01
00:00` when involving `time` did not work as `pgdate` does not
understand `0000` as a year. This PR will fix that behaviour.

Co-authored-by: Oliver Tan <[email protected]>
@craig
Copy link
Contributor

craig bot commented Dec 3, 2019

Build succeeded

@craig craig bot merged commit 06f3076 into cockroachdb:master Dec 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants