Skip to content

sql/schemachanger: add TTL dependency checking for ALTER TYPE in DSC#127436

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
spilchen:issue-126143-ttl-expression-block
Jul 19, 2024
Merged

sql/schemachanger: add TTL dependency checking for ALTER TYPE in DSC#127436
craig[bot] merged 1 commit intocockroachdb:masterfrom
spilchen:issue-126143-ttl-expression-block

Conversation

@spilchen
Copy link
Copy Markdown
Contributor

This extracts the TTL expression, whether user-defined or system-defined, into a declarative schema changer (DSC) Expression element. This allows us to correctly establish column dependencies for the column used in the TTL expression. By doing so, we can traverse these column dependencies to return appropriate errors when attempting to alter a column that the TTL expression depends on.

Previously, checking the TTL expression was an outstanding TODO in the new alter type code. The new dependency checking I added allows us to address that TODO.

Using column dependencies in DSC elements is more manageable than the previous method, which was in checkRowLevelTTLColumn. Therefore, this update also modifies the drop column functionality to use column dependencies, allowing us to get rid of that function.

Additionally, this update standardizes the error messages for attempts to alter a column dependent on the TTL expression. I opted for more verbose error messages with hints, leading to updates in some of the errors checked in logic tests.

Epic: CRDB-25314
Closes: #126143
Release note: None

@spilchen spilchen self-assigned this Jul 18, 2024
@spilchen spilchen requested a review from a team as a code owner July 18, 2024 14:47
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

Copy link
Copy Markdown
Collaborator

@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.

nice, this is a solid improvement! just had some nits about changing the string type, then feel free to merge after addressing those

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


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

	// Check for elements depending on the column we are altering.
	walkColumnDependencies(b, col, "alter type of", "column", func(e scpb.Element, op, objType string) {

nit: let's make these parameters be of type redact.SafeString, so that way they don't get redacted in logs and errors.


pkg/sql/sqlerrors/errors.go line 297 at r1 (raw file):

// the internal column created for the duration expression. These are tables
// that define the ttl_expire_after setting.
func NewAlterDependsOnDurationExprError(op, objType, colName, tabName string) error {

nit: let's make these parameters be of type redact.SafeString, so that way they don't get redacted in logs and errors.


pkg/sql/sqlerrors/errors.go line 312 at r1 (raw file):

// the column that is referenced in the expiration expression.
func NewAlterDependsOnExpirationExprError(
	op, objType, colName, tabName, expirationExpr string,

nit: let's make these parameters be of type redact.SafeString, so that way they don't get redacted in logs and errors.

@spilchen spilchen force-pushed the issue-126143-ttl-expression-block branch from 24ed396 to d96e0a8 Compare July 18, 2024 17:36
Copy link
Copy Markdown
Contributor Author

@spilchen spilchen left a comment

Choose a reason for hiding this comment

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

tftr @rafiss. You approved the changed, but I wasn't able to apply the change exactly as you had requested. I would appreciate another look when you have a chance.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rafiss and @spilchen)


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

Previously, rafiss (Rafi Shamim) wrote…

nit: let's make these parameters be of type redact.SafeString, so that way they don't get redacted in logs and errors.

Making this change had a snowball effect. I modified about 14 files and around 100 lines, but more changes were still needed. Is it sufficient to cast the string to redact.SafeString right when the error is logged? That's what I ended up doing instead, but I'm not completely sure if this will ensure redaction in the logs.


pkg/sql/sqlerrors/errors.go line 297 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nit: let's make these parameters be of type redact.SafeString, so that way they don't get redacted in logs and errors.

See my comment about the snowball affect of such a change.


pkg/sql/sqlerrors/errors.go line 312 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nit: let's make these parameters be of type redact.SafeString, so that way they don't get redacted in logs and errors.

See my comment about the snowball affect of such a change.

@spilchen spilchen requested a review from rafiss July 18, 2024 17:52
Copy link
Copy Markdown
Collaborator

@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 @spilchen)


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

Previously, spilchen wrote…

Making this change had a snowball effect. I modified about 14 files and around 100 lines, but more changes were still needed. Is it sufficient to cast the string to redact.SafeString right when the error is logged? That's what I ended up doing instead, but I'm not completely sure if this will ensure redaction in the logs.

yes, casting to redact.SafeString when constructing the error will also achieve the desired result.

to clarify, the goal is to not perform redaction on those strings, since we know they don't have sensitive data. all strings are redacted by default unless we opt-in to non-redaction in some way. more details are here: https://cockroachlabs.atlassian.net/wiki/spaces/CRDB/pages/1824817806/Log+and+error+redactability


pkg/sql/sqlerrors/errors.go line 302 at r2 (raw file):

			pgcode.InvalidTableDefinition,
			`cannot %s %s %s while ttl_expire_after is set`,
			redact.SafeString(op), redact.SafeString(objType), redact.SafeString(colName),

ah this was my bad - the "operation" and "object type" are both safe strings, since these are strings that we construct within our own code. but the column name (and table name) are not safe strings, since these come from customer input


pkg/sql/sqlerrors/errors.go line 318 at r2 (raw file):

			pgcode.InvalidTableDefinition,
			`cannot %s %s %q referenced by row-level TTL expiration expression %q`,
			redact.SafeString(op), redact.SafeString(objType), redact.SafeString(colName),

same here, colName should not be considered safe

This extracts the TTL expression, whether user-defined or
system-defined, into a declarative schema changer (DSC) Expression
element. This allows us to correctly establish column dependencies for
the column used in the TTL expression. By doing so, we can traverse
these column dependencies to return appropriate errors when attempting
to alter a column that the TTL expression depends on.

Previously, checking the TTL expression was an outstanding TODO in the
alter type code. The new dependency checking I added allows us to
address that TODO.

Using column dependencies in DSC elements is more manageable than the
previous method, which was in `checkRowLevelTTLColumn`. Therefore, this
update also modifies the drop column functionality to use column
dependencies, allowing us to get rid of that function.

Additionally, this update standardizes the error messages for attempts
to alter a column dependent on the TTL expression. I opted for more
verbose error messages with hints, leading to updates in some of the
errors checked in logic tests.

Epic: CRDB-25314
Closes: cockroachdb#126143
Release note: None
@spilchen spilchen force-pushed the issue-126143-ttl-expression-block branch from d96e0a8 to 3225b9a Compare July 18, 2024 18:35
Copy link
Copy Markdown
Contributor Author

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


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

Previously, rafiss (Rafi Shamim) wrote…

yes, casting to redact.SafeString when constructing the error will also achieve the desired result.

to clarify, the goal is to not perform redaction on those strings, since we know they don't have sensitive data. all strings are redacted by default unless we opt-in to non-redaction in some way. more details are here: https://cockroachlabs.atlassian.net/wiki/spaces/CRDB/pages/1824817806/Log+and+error+redactability

Thanks for the context.


pkg/sql/sqlerrors/errors.go line 302 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

ah this was my bad - the "operation" and "object type" are both safe strings, since these are strings that we construct within our own code. but the column name (and table name) are not safe strings, since these come from customer input

Done.


pkg/sql/sqlerrors/errors.go line 318 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

same here, colName should not be considered safe

Done.

@spilchen
Copy link
Copy Markdown
Contributor Author

tftr

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Jul 18, 2024

Build failed:

@spilchen
Copy link
Copy Markdown
Contributor Author

bors retry

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Jul 19, 2024

@craig craig bot merged commit ac0b25c into cockroachdb:master Jul 19, 2024
@spilchen spilchen deleted the issue-126143-ttl-expression-block branch July 29, 2024 10:56
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/schemachanger: support trivial ALTER COLUMN ... SET TYPE in declarative schema changer

3 participants