Skip to content

sql: ALTER COLUMN TYPE does not check partial index definitions#131590

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
Dedej-Bergin:act-check-partial
Oct 3, 2024
Merged

sql: ALTER COLUMN TYPE does not check partial index definitions#131590
craig[bot] merged 1 commit intocockroachdb:masterfrom
Dedej-Bergin:act-check-partial

Conversation

@Dedej-Bergin
Copy link
Copy Markdown
Contributor

@Dedej-Bergin Dedej-Bergin commented Sep 30, 2024

Previously we would allow ALTER COLUMN TYPE to succeed on a column even when we have a partial index on it. This would cause issues down the line for example at insert and is inconsistent with postgres. This fix now checks for partial index and errors out like postgres when your run the ALTER COLUMN TYPE command on a column with a partial index.

Fixes: #72456
Epic: CRDB-25314

Release note (bug fix): ALTER COLUMN TYPE now errors out when we have a partial index dependent on the column being altered.

@blathers-crl
Copy link
Copy Markdown

blathers-crl bot commented Sep 30, 2024

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@Dedej-Bergin Dedej-Bergin force-pushed the act-check-partial branch 3 times, most recently from 12e2476 to 1ac07e7 Compare September 30, 2024 15:44
@Dedej-Bergin Dedej-Bergin marked this pull request as ready for review September 30, 2024 15:44
@Dedej-Bergin Dedej-Bergin requested a review from a team as a code owner September 30, 2024 15:44
Copy link
Copy Markdown
Contributor

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

You were on the right track. I just had a couple of suggestions for modifications.

Reviewed 4 of 4 files at r1, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @Dedej-Bergin)


-- commits line 11 at r1:
Let's include the epic in the commit message: CRDB-25314


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

) error {
	for _, index := range tableDesc.AllIndexes() {
		if index.IsPartial() {

Should we verify that the column is included in the partial index?


pkg/sql/logictest/testdata/logic_test/alter_column_type line 1218 at r1 (raw file):

DROP TABLE t_int;

subtest end

I usually aim to include a single subtest end at the bottom of the test. Since you've already added one, you can probably remove this line.


pkg/sql/logictest/testdata/logic_test/alter_column_type line 1223 at r1 (raw file):


statement ok
set enable_experimental_alter_column_type_general='true';

I don't think we need this statement. We already call this higher up in the testcase.


pkg/sql/logictest/testdata/logic_test/alter_column_type line 1224 at r1 (raw file):

statement ok
set enable_experimental_alter_column_type_general='true';
set use_declarative_schema_changer = off;

I think we call this test for the local-legacy-schema-changer config. So, I don't think we need this.


pkg/sql/logictest/testdata/logic_test/alter_column_type line 1233 at r1 (raw file):


statement error pq: cannot alter type of column "b" because it is referenced by partial index "idx"
alter table a alter column b set data type timestamp  using a::timestamp;

We should expand this test and verify that we can alter the type of other columns not included in the partial index.

Copy link
Copy Markdown
Contributor Author

@Dedej-Bergin Dedej-Bergin 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)


-- commits line 11 at r1:

Previously, spilchen wrote…

Let's include the epic in the commit message: CRDB-25314

Thanks, just updated it.


pkg/sql/logictest/testdata/logic_test/alter_column_type line 1218 at r1 (raw file):

Previously, spilchen wrote…

I usually aim to include a single subtest end at the bottom of the test. Since you've already added one, you can probably remove this line.

Thanks, I just removed it.


pkg/sql/logictest/testdata/logic_test/alter_column_type line 1223 at r1 (raw file):

Previously, spilchen wrote…

I don't think we need this statement. We already call this higher up in the testcase.

Okay great, I just removed it, thanks!


pkg/sql/logictest/testdata/logic_test/alter_column_type line 1224 at r1 (raw file):

Previously, spilchen wrote…

I think we call this test for the local-legacy-schema-changer config. So, I don't think we need this.

Right, I just added onlyif config local-legacy-schema-changer just now and removed this line, thanks!

Copy link
Copy Markdown
Contributor

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

Reviewed all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @Dedej-Bergin)


-- commits line 11 at r1:

Previously, Dedej-Bergin (Bergin Dedej) wrote…

Thanks, just updated it.

Apologies, I meant to say that you should include this in the commit message. I thought there was a git hook in place to ensure the commit message follows the correct format.

Epic: CRDB-25314

pkg/sql/logictest/testdata/logic_test/alter_column_type line 1224 at r1 (raw file):

Previously, Dedej-Bergin (Bergin Dedej) wrote…

Right, I just added onlyif config local-legacy-schema-changer just now and removed this line, thanks!

We should be seeing the same error when the commands are run through the DSC. Are you observing a different outcome?

@Dedej-Bergin
Copy link
Copy Markdown
Contributor Author

pkg/sql/logictest/testdata/logic_test/alter_column_type line 1224 at r1 (raw file):

Previously, spilchen wrote…

We should be seeing the same error when the commands are run through the DSC. Are you observing a different outcome?

Yes a bit different, for the new code changes I just added, with these repro steps:

create table roach\_village ( roach\_id int primary key, first\_name text, last\_name text, legs int );
create index idx on roach\_village(last\_name) where first\_name = 'papa roach';
alter table roach\_village alter column first\_name set data type timestamp  using first\_name::timestamp;
alter table roach\_village alter column last\_name set data type timestamp  using last\_name::timestamp;

I was thinking to make it work like this:

Screenshot 2024-10-01 at 3.56.16 PM.png

For DSC on the second one it seems to error out with unimplemented... not sure if that's the error message we want:
Screenshot 2024-10-01 at 3.55.56 PM.png

Copy link
Copy Markdown
Contributor Author

@Dedej-Bergin Dedej-Bergin 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)


-- commits line 11 at r1:

Previously, spilchen wrote…

Apologies, I meant to say that you should include this in the commit message. I thought there was a git hook in place to ensure the commit message follows the correct format.

Epic: CRDB-25314

Just added this, for some reason I thought this happens automatically.


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

Previously, spilchen wrote…

Should we verify that the column is included in the partial index?

Yes just added this code, thanks @spilchen!


pkg/sql/logictest/testdata/logic_test/alter_column_type line 1233 at r1 (raw file):

Previously, spilchen wrote…

We should expand this test and verify that we can alter the type of other columns not included in the partial index.

Just added the expanded test, thanks!

@Dedej-Bergin Dedej-Bergin requested a review from spilchen October 1, 2024 22:31
Copy link
Copy Markdown
Contributor

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

Reviewed 2 of 2 files at r3, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @Dedej-Bergin)


pkg/sql/logictest/testdata/logic_test/alter_column_type line 1224 at r1 (raw file):

Previously, Dedej-Bergin (Bergin Dedej) wrote…

Yes a bit different, for the new code changes I just added, with these repro steps:

create table roach\_village ( roach\_id int primary key, first\_name text, last\_name text, legs int );
create index idx on roach\_village(last\_name) where first\_name = 'papa roach';
alter table roach\_village alter column first\_name set data type timestamp  using first\_name::timestamp;
alter table roach\_village alter column last\_name set data type timestamp  using last\_name::timestamp;

I was thinking to make it work like this:

Screenshot 2024-10-01 at 3.56.16 PM.png

For DSC on the second one it seems to error out with unimplemented... not sure if that's the error message we want:
Screenshot 2024-10-01 at 3.55.56 PM.png

The unimplemented error is expected for now—that’s what #127014 will address. In the meantime, can you try using a simpler type conversion instead of a complex one? A simple conversion is one that doesn’t require a backfill. This is supported in the DSC now and should let us run the test in both modes. For example, converting from TEXT to CHAR(20) would qualify as a simple conversion.

Copy link
Copy Markdown
Contributor Author

@Dedej-Bergin Dedej-Bergin 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/logictest/testdata/logic_test/alter_column_type line 1224 at r1 (raw file):

Previously, spilchen wrote…

The unimplemented error is expected for now—that’s what #127014 will address. In the meantime, can you try using a simpler type conversion instead of a complex one? A simple conversion is one that doesn’t require a backfill. This is supported in the DSC now and should let us run the test in both modes. For example, converting from TEXT to CHAR(20) would qualify as a simple conversion.

So it seems that DSC doesn't block when the column is part of the predicate.
Screenshot 2024-10-02 at 3.32.33 PM.png

@Dedej-Bergin
Copy link
Copy Markdown
Contributor Author

pkg/sql/logictest/testdata/logic_test/alter_column_type line 1224 at r1 (raw file):

Previously, Dedej-Bergin (Bergin Dedej) wrote…

So it seems that DSC doesn't block when the column is part of the predicate.
Screenshot 2024-10-02 at 3.32.33 PM.png

Actually we seem to error out on the predicate, but on the column that has the index we seem to not error out on DSC.
Is this something we should change in the DSC?

Screenshot 2024-10-02 at 4.36.44 PM.png

@Dedej-Bergin Dedej-Bergin requested a review from spilchen October 2, 2024 20:38
Copy link
Copy Markdown
Contributor

@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 @Dedej-Bergin)


pkg/sql/logictest/testdata/logic_test/alter_column_type line 1224 at r1 (raw file):

Previously, Dedej-Bergin (Bergin Dedej) wrote…

Actually we seem to error out on the predicate, but on the column that has the index we seem to not error out on DSC.
Is this something we should change in the DSC?

Screenshot 2024-10-02 at 4.36.44 PM.png

Thanks for running these tests. The DSC behavior is correct: it should fail only when modifying the partial expression and succeed when modifying a key column. Key columns can be changed because we’ll rewrite the index if a backfill is needed, or, if no backfill is required, the on-disk encoding of the column in the index remains unchanged (e.g., BIGINT to SMALLINT).

Could you remove the key column check in the legacy code? Sorry for not mentioning this earlier!

Previously we would allow ALTER COLUMN TYPE to succeed on a
column even when we have a partial index on it.  This would
cause issues down the line for example at insert and is
inconsistent with postgres.  This fix now checks for
partial index and errors out like postgres when your run
the ALTER COLUMN TYPE command on a column with a partial index.

Fixes: cockroachdb#72456
Epic: CRDB-25314

Release note (bug fix): ALTER COLUMN TYPE now errors out when we have a
partial index dependent on the column being altered.
@Dedej-Bergin
Copy link
Copy Markdown
Contributor Author

pkg/sql/logictest/testdata/logic_test/alter_column_type line 1224 at r1 (raw file):

Previously, spilchen wrote…

Thanks for running these tests. The DSC behavior is correct: it should fail only when modifying the partial expression and succeed when modifying a key column. Key columns can be changed because we’ll rewrite the index if a backfill is needed, or, if no backfill is required, the on-disk encoding of the column in the index remains unchanged (e.g., BIGINT to SMALLINT).

Could you remove the key column check in the legacy code? Sorry for not mentioning this earlier!

No worries, just made the changes. The test-case results are now coming back the same for both DSC and legacy, thanks!

@Dedej-Bergin Dedej-Bergin requested a review from spilchen October 3, 2024 14:23
Copy link
Copy Markdown
Contributor

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

:lgtm:

Reviewed 2 of 2 files at r4, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @Dedej-Bergin)

@Dedej-Bergin
Copy link
Copy Markdown
Contributor Author

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Oct 3, 2024

@craig craig bot merged commit 1922797 into cockroachdb:master Oct 3, 2024
@Dedej-Bergin Dedej-Bergin deleted the act-check-partial branch October 3, 2024 19:01
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: ALTER COLUMN TYPE does not check partial index definitions

3 participants