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

ttl: validate ttl_expiration_expression is a TIMESTAMPTZ #86269

Merged
merged 2 commits into from
Aug 20, 2022

Conversation

rafiss
Copy link
Collaborator

@rafiss rafiss commented Aug 16, 2022

fixes #86410
fixes #85531
fixes #84725

Previously the only validation is that the expression is valid SQL
syntax. Now the expression must also be a TIMESTAMPTZ which will
prevent errors in the TTL job when comparing to the cutoff.

Validation is also added so that a column cannot be dropped or altered
in type if it is used in the TTL expression. If the column is renamed, the
expression is automatically updated.

Release note: None

Release justification: low risk changes to validation

@rafiss rafiss requested review from ajwerner, ecwall and a team August 16, 2022 22:18
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

Nice, thanks for doing this!

Reviewed 1 of 11 files at r1.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ecwall and @rafiss)


pkg/sql/alter_table.go line 497 at r1 (raw file):

							t.Column,
						),
						"use ALTER TABLE %s RESET (ttl) or ALTER TABLE %s SET (ttl_expiration_expression = ...) instead",

need to repeat the argument or use a positional format specifier


pkg/sql/catalog/schemaexpr/expr.go line 461 at r1 (raw file):

// ValidateTTLExpirationExpressionHasNoDependents verifies that the
// ttl_expiration_expression, if any, does not reference the given column.
func ValidateTTLExpirationExpressionHasNoDependents(

this is a weird name given you're validating that the TTLExpression does not depend on the column. Maybe this should be ValidateTTLExpressionDoesNotDependOnColumn?


pkg/sql/catalog/tabledesc/validate.go line 786 at r1 (raw file):

	})

	vea.Report(ValidateRowLevelTTL(desc.GetRowLevelTTL()))

Any reason not to add desc as the argument to ValidateRowLevelTTL and call this or add the logic there? It looked to me like all the callers have a descriptor handy.

Code quote:

	vea.Report(ValidateRowLevelTTL(desc.GetRowLevelTTL()))
	if desc.HasRowLevelTTL() {

pkg/sql/schemachanger/scbuild/builder_state.go line 473 at r1 (raw file):

		panic(errors.AssertionFailedf("failed to get descriptor for table %d", tableID))
	}
	tableDesc, ok := cachedDesc.desc.(catalog.TableDescriptor)

can you revise this error to say that it's not a table and print the type with %T or DescriptorType()

Code quote:

	tableDesc, ok := cachedDesc.desc.(catalog.TableDescriptor)
	if !ok {
		panic(errors.AssertionFailedf("failed to get descriptor for table %d", tableID))

pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_drop_column.go line 78 at r1 (raw file):

	})

	if rowLevelTTL != nil {

nit: invert the condition and early return?


pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_drop_column.go line 97 at r1 (raw file):

			}
			wrappedExpr := b.WrapExpression(tbl.TableID, expr)
			for _, referencedCol := range wrappedExpr.ReferencedColumnIDs {

if descpb.ColumnIDs(wrappedExpr.ReferencedColumnIDs).Contains(colToDrop.ColumnID) {?

Code quote:

			for _, referencedCol := range wrappedExpr.ReferencedColumnIDs {
				if referencedCol == colToDrop.ColumnID {

Copy link
Collaborator Author

@rafiss rafiss 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 @ajwerner and @ecwall)


pkg/sql/alter_table.go line 497 at r1 (raw file):

Previously, ajwerner wrote…

need to repeat the argument or use a positional format specifier

fixing


pkg/sql/catalog/schemaexpr/expr.go line 461 at r1 (raw file):

Previously, ajwerner wrote…

this is a weird name given you're validating that the TTLExpression does not depend on the column. Maybe this should be ValidateTTLExpressionDoesNotDependOnColumn?

good call


pkg/sql/catalog/tabledesc/validate.go line 786 at r1 (raw file):

Previously, ajwerner wrote…

Any reason not to add desc as the argument to ValidateRowLevelTTL and call this or add the logic there? It looked to me like all the callers have a descriptor handy.

i attempted to explain in the comment below. the reason is that the usage at

if err := tabledesc.ValidateRowLevelTTL(ttl); err != nil {
can be called with a relatively empty table descriptor when the ttl_expiration_expression param is configured as part of CREATE TABLE


pkg/sql/schemachanger/scbuild/builder_state.go line 473 at r1 (raw file):

Previously, ajwerner wrote…

can you revise this error to say that it's not a table and print the type with %T or DescriptorType()

fixing


pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_drop_column.go line 78 at r1 (raw file):

Previously, ajwerner wrote…

nit: invert the condition and early return?

fixing


pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_drop_column.go line 97 at r1 (raw file):

Previously, ajwerner wrote…

if descpb.ColumnIDs(wrappedExpr.ReferencedColumnIDs).Contains(colToDrop.ColumnID) {?

fixing

@rafiss rafiss force-pushed the validate-ttl-expression branch from d663614 to 08e2247 Compare August 18, 2022 16:21
@rafiss rafiss requested a review from ajwerner August 18, 2022 16:22
@rafiss rafiss force-pushed the validate-ttl-expression branch from 08e2247 to 2747b6f Compare August 18, 2022 17:42
Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

:lgtm:

just needs a rewrite of pkg/sql/schemachanger/scbuild

Reviewed 5 of 11 files at r1, 5 of 8 files at r2, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @ajwerner, @ecwall, and @rafiss)


pkg/sql/alter_table.go line 1997 at r2 (raw file):

	}

	// validate ttl_expiration_expression

nit: fix up this comment.

@postamar
Copy link
Contributor

I appreciate that this PR includes significant coverage in the form of logic tests, still, this seems like a good place to add all of the missing unit test cases in tabledesc for the whole TTL configuration in the table descriptors, as tracked in #86410

@rafiss rafiss force-pushed the validate-ttl-expression branch from 2747b6f to 7737e82 Compare August 19, 2022 17:13
@rafiss
Copy link
Collaborator Author

rafiss commented Aug 19, 2022

@postamar i've added a separate commit that focuses on TTL validation testing.

Copy link
Contributor

@postamar postamar left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM

@rafiss
Copy link
Collaborator Author

rafiss commented Aug 19, 2022

TFTRs!

bors r=ajwerner,postamar

@craig
Copy link
Contributor

craig bot commented Aug 19, 2022

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Aug 20, 2022

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Aug 20, 2022

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Aug 20, 2022

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Aug 20, 2022

Build failed:

Previously the only validation is that the expression is valid SQL
syntax. Now the expression must also be a TIMESTAMPTZ which will
prevent errors in the TTL job when comparing to the cutoff.

Validation is also added so that a column cannot be dropped or altered
in type if it is used in the TTL expression. If the column is renamed, the
expression is automatically updated.

Release note: None

Release justification: high priority fix to new functionality
This moves more of the TTL validation into ttl.go, and outside of the
CrossTableReferences validation.

Tests are added for all the TTL validation steps.

Release justification: test only change and small refactor
Release note: None
@rafiss rafiss force-pushed the validate-ttl-expression branch from 7737e82 to 5b5543d Compare August 20, 2022 19:39
@rafiss
Copy link
Collaborator Author

rafiss commented Aug 20, 2022

bors r=ajwerner,postamar

@craig
Copy link
Contributor

craig bot commented Aug 20, 2022

Build succeeded:

@craig craig bot merged commit 0c9ac43 into cockroachdb:master Aug 20, 2022
@rafiss rafiss deleted the validate-ttl-expression branch August 23, 2022 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants