Skip to content

sql: add precision support for TIMESTAMP/TIMESTAMPTZ#42552

Closed
otan wants to merge 1 commit intocockroachdb:masterfrom
otan-cockroach:otan-precision
Closed

sql: add precision support for TIMESTAMP/TIMESTAMPTZ#42552
otan wants to merge 1 commit intocockroachdb:masterfrom
otan-cockroach:otan-precision

Conversation

@otan
Copy link
Copy Markdown
Contributor

@otan otan commented Nov 18, 2019

Resolves #32098.

Add precision support for TIMESTAMP/TIMESTAMPTZ, supporting precisions
from 0 to 6 inclusive.

When storing types, we use the value 0 to mean DEFAULT (which will
be a precision of 6). A precision of 0 will be stored as -1. This
allows backwards compatibility with older versions of
TIMESTAMP/TIMESTAMPTZ, which had a precision of 0 when persisted.
This detail is abstracted away in the Make* and Precision() functions.

Note this change is not backwards compatible for those who have set
TIMESTAMP(0)/TIMESTAMPTZ(0), as this is stored as -1. If someone
does this in v20.1, it does not cover them well in case of a downgrade back
to v19.2.

Release note (sql change): Introduces precision support for TIMESTAMP
and TIMESTAMPTZ, supporting precisions from 0 to 6 inclusive. Previous
versions of TIMESTAMP and TIMESTAMPTZ will default to 6 units of
precision.

@otan otan requested review from a team and rohany November 18, 2019 21:34
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

Copy link
Copy Markdown
Contributor

@rohany rohany left a comment

Choose a reason for hiding this comment

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

A few comments, but I'd also like @solongordon to take a look at this PR just in case.

Reviewed 11 of 13 files at r1.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @otan and @rohany)


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

// duration to use to pass into time.Truncate to truncate to that duration.
// Panics if the precision is not supported.
func precisionToTruncDuration(precision int32) time.Duration {

i feel like this should be a method on the DTimeStamp and DTimeStampTZ so that its clear this is only meant to be used on timestamps. or maybe just change the name so its clear for use on the time precisions only.


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

	switch precision {
	case types.DefaultTimePrecision:
		return time.Microsecond

maybe make this a constant too, time.DefaultPrecision or something


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

		return time.Second
	case 1:
		return time.Millisecond * 100

tfw time.Decisecond is missing


pkg/sql/types/types.go, line 677 at r1 (raw file):

		return Time
	}
	if precision != 6 {

whats the reason we aren't also adding precision to the time type?


pkg/sql/types/types.go, line 837 at r1 (raw file):

// PrecisionExpandDefault returns the Precision, but for default
// values it will return the precision with default set.
func (t *T) PrecisionExpandDefault() int32 {

it looks like this is unused


pkg/sql/types/types_test.go, line 227 at r1 (raw file):

		// TIMESTAMP
		{MakeTimestamp(-1), Timestamp},
		{MakeTimestamp(-1), MakeScalar(TimestampFamily, oid.T_timestamp, -1, 0, emptyLocale)},

is there some way you can explicitly test that the 19.2 disk table descriptor is read correctly?

Copy link
Copy Markdown
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! 0 of 0 LGTMs obtained (waiting on @rohany and @solongordon)


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

Previously, rohany (Rohan Yadav) wrote…

i feel like this should be a method on the DTimeStamp and DTimeStampTZ so that its clear this is only meant to be used on timestamps. or maybe just change the name so its clear for use on the time precisions only.

I will change the naming.


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

Previously, rohany (Rohan Yadav) wrote…

maybe make this a constant too, time.DefaultPrecision or something

Done.


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

Previously, rohany (Rohan Yadav) wrote…

tfw time.Decisecond is missing

:P


pkg/sql/types/types.go, line 677 at r1 (raw file):

Previously, rohany (Rohan Yadav) wrote…

whats the reason we aren't also adding precision to the time type?

Needed more plumbing, and it's a separate issue. Was planning to do as a follow up.


pkg/sql/types/types.go, line 837 at r1 (raw file):

PrecisionExpandDefault

should be

pkg/sql/schemachange/alter_column_type.go
148: oldPrecision := oldType.PrecisionExpandDefault()
149: newPrecision := newType.PrecisionExpandDefault()


pkg/sql/types/types_test.go, line 227 at r1 (raw file):

Previously, rohany (Rohan Yadav) wrote…

is there some way you can explicitly test that the 19.2 disk table descriptor is read correctly?

I'm not sure, suggestions welcome.
Worst case, I can manually try upgrading... (if there's a wiki to do that lmk)

Copy link
Copy Markdown
Contributor

@rohany rohany 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! 0 of 0 LGTMs obtained (waiting on @otan, @rohany, and @solongordon)


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

Previously, otan (Oliver Tan) wrote…

Done.

This doesn't look changed?


pkg/sql/types/types.go, line 837 at r1 (raw file):

Previously, otan (Oliver Tan) wrote…
PrecisionExpandDefault

should be

pkg/sql/schemachange/alter_column_type.go
148: oldPrecision := oldType.PrecisionExpandDefault()
149: newPrecision := newType.PrecisionExpandDefault()

ah cmd f is lying to me


pkg/sql/types/types_test.go, line 227 at r1 (raw file):

Previously, otan (Oliver Tan) wrote…

I'm not sure, suggestions welcome.
Worst case, I can manually try upgrading... (if there's a wiki to do that lmk)

i guess what would convince me is explicitly make the type as it was before this commit (without the precision), and make sure that it reads the default precision.

Copy link
Copy Markdown
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! 0 of 0 LGTMs obtained (waiting on @rohany and @solongordon)


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

Previously, rohany (Rohan Yadav) wrote…

This doesn't look changed?

sigh, didn't press save.


pkg/sql/types/types.go, line 837 at r1 (raw file):

Previously, rohany (Rohan Yadav) wrote…

ah cmd f is lying to me

Done.


pkg/sql/types/types_test.go, line 227 at r1 (raw file):

Previously, rohany (Rohan Yadav) wrote…

i guess what would convince me is explicitly make the type as it was before this commit (without the precision), and make sure that it reads the default precision.

I can try the manual upgrade technique in lieu of making a test suite of upgrading versions of cockroach :)

Copy link
Copy Markdown
Contributor

@rohany rohany 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! 0 of 0 LGTMs obtained (waiting on @otan, @rohany, and @solongordon)


pkg/sql/types/types_test.go, line 227 at r1 (raw file):

Previously, otan (Oliver Tan) wrote…

I can try the manual upgrade technique in lieu of making a test suite of upgrading versions of cockroach :)

sorry i wasn't clear -- i meant in this test, make an instance of the timestamp struct as it was defined before this commit -- (without the precision)

Copy link
Copy Markdown
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! 0 of 0 LGTMs obtained (waiting on @rohany and @solongordon)


pkg/sql/types/types_test.go, line 227 at r1 (raw file):

Previously, rohany (Rohan Yadav) wrote…

sorry i wasn't clear -- i meant in this test, make an instance of the timestamp struct as it was defined before this commit -- (without the precision)

Done.

Add precision support for TIMESTAMP/TIMESTAMPTZ, supporting precisions
from 0 to 6 inclusive.

When storing types, we use the value `0` to mean `DEFAULT` (which will
be a precision of 6). A precision of `0` will be stored as `-1`. This
allows backwards compatibility with older versions of
TIMESTAMP/TIMESTAMPTZ, which had a precision of 0 when persisted.
This detail is abstracted away in the Make* and Precision() functions.

Release note (sql change): Introduces precision support for TIMESTAMP
and TIMESTAMPTZ, supporting precisions from 0 to 6 inclusive. Previous
versions of TIMESTAMP and TIMESTAMPTZ will default to 6 units of
precision.
@otan
Copy link
Copy Markdown
Contributor Author

otan commented Nov 19, 2019

just realised this ain't backwards compatible, so going to close this off for review for a second.

@otan otan closed this Nov 19, 2019
@otan
Copy link
Copy Markdown
Contributor Author

otan commented Nov 19, 2019

will note it in the PR message

@otan otan reopened this Nov 19, 2019
@otan
Copy link
Copy Markdown
Contributor Author

otan commented Nov 19, 2019

Oops, had another idea. Gimme a sec.

@otan otan closed this Nov 19, 2019
craig bot pushed a commit that referenced this pull request Nov 21, 2019
41788: storage/rangefeed: push rangefeed.Filter into Raft processing goroutine r=danhhz a=nvanbenschoten

This commit creates a `rangefeed.Filter` object, which informs the producer of logical operations of the information that a `rangefeed.Processor` is interested in, given its current set of registrations. It can be used to avoid performing extra work to provide the Processor with information which will be ignored.

This is an important optimization for single-key or small key span rangefeeds, as it avoids some extraneous work for the key spans not being watched.

For now, this extra work is just fetching the post-operation value for `MVCCWriteValueOp` and `MVCCCommitIntentOp` operations, but this can be extended in the future to include the pre-operation value as well. This will be important to avoid any perf regression when addressing #28666.

42580: sql: add precision support for TIMESTAMP/TIMESTAMPTZ (v2) r=otan a=otan

Refs #32098
Alternative to #42552

----

Add precision support for TIMESTAMP/TIMESTAMPTZ, supporting precisions
from 0 to 6 inclusive.

When storing this in the type proto, we introduce a `TimePrecisionIsSet`
variable, which is used to differentiate 0 (unset) or 0 (explicitly set)
for time related protos. This is abstracted away in the `Precision()`
function which returns the correct precision (unset => 6) for time. This
allows forwards and backwards compatibility.

Note Time will come later, as it involves a bit more plumbing to the
base library.

Release note (sql change): Introduces precision support for TIMESTAMP
and TIMESTAMPTZ, supporting precisions from 0 to 6 inclusive. Previous
versions of TIMESTAMP and TIMESTAMPTZ will default to 6 units of
precision. Note that if anyone downgrades whilst having precision set,
they will have full precision (6) again, but if they re-upgrade they
will find their precisions truncated again.

Co-authored-by: Nathan VanBenschoten <nvanbenschoten@gmail.com>
Co-authored-by: Oliver Tan <otan@cockroachlabs.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: support optional TIMESTAMP precision

3 participants