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/schemachanger: support ADD COLUMN SERIAL for DSC unique_rowid only #129377

Merged
merged 1 commit into from
Sep 6, 2024

Conversation

Dedej-Bergin
Copy link
Contributor

@Dedej-Bergin Dedej-Bergin commented Aug 20, 2024

Previously we could only add SERIAL columns with the legacy schema changer
with these code changes the ALTER TABLE ADD COLUMN statement
can now add SERIAL type columns via the declarative schema changer for
the default rowid serial_normalization mode.

Informs: #126900
Release note: none

Copy link

blathers-crl bot commented Aug 20, 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
Member

This change is Reviewable

@Dedej-Bergin Dedej-Bergin force-pushed the atac-serial branch 3 times, most recently from 3d88648 to d3ac03f Compare August 27, 2024 21:26
@Dedej-Bergin Dedej-Bergin force-pushed the atac-serial branch 7 times, most recently from bb23a49 to b27574c Compare August 29, 2024 22:12
@Dedej-Bergin Dedej-Bergin changed the title sql/schemachanger: ALTER TABLE ADD COLUMN add DSC support for SERIAL sql/schemachanger: support ADD COLUMN SERIAL for DSC unique_rowid only Aug 29, 2024
@Dedej-Bergin Dedej-Bergin marked this pull request as ready for review August 30, 2024 01:39
@Dedej-Bergin Dedej-Bergin requested a review from a team as a code owner August 30, 2024 01:39
@Dedej-Bergin Dedej-Bergin force-pushed the atac-serial branch 3 times, most recently from abf82a9 to 6b1946d Compare September 3, 2024 17:52
@Dedej-Bergin Dedej-Bergin requested a review from fqazi September 3, 2024 20:48
Copy link
Collaborator

@fqazi fqazi left a comment

Choose a reason for hiding this comment

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

Just some very minor comments. Great work!

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


pkg/sql/serial.go line 170 at r1 (raw file):

						"upgrading the column %s to %s to utilize the session serial_normalization setting",
						d.Name.String(),
						types.Int.SQLString(),

Not sure why this change was made?


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

		}
		d = alterTableAddColumnSerial(b, d, tn)
		d.IsSerial = false

Why do we even need to flip this?


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

// UseFullWidthIntType For unique_rowid(), unordered_unique_rowid(), or virtual sequences,
// full-width integer type is used when the values will not fit otherwise.
func useFullWidthIntType(ctx BuildCtx, d *tree.ColumnTableDef, sender eval.ClientNoticeSender) {

Nit: Does this need to be a function? There is no-reuse here or clarity improvement.


pkg/sql/logictest/testdata/logic_test/alter_table line 3995 at r1 (raw file):


statement ok
SET serial_normalization = sql_sequence

Should we check other modes too?

Copy link
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 @fqazi)


pkg/sql/serial.go line 170 at r1 (raw file):

Previously, fqazi (Faizan Qazi) wrote…

Not sure why this change was made?

It's leftovers from a failed refactoring attempt, thanks for catching it! Just updated it


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

Previously, fqazi (Faizan Qazi) wrote…

Why do we even need to flip this?

The d.IsSerial was already being flipped before, it was just happening a few function/method calls deep which made it a bit unclear.

But we need d.IsSerial to be false because it gets checked again lower in the code:

On line 115: cdd, err := tabledesc.MakeColumnDefDescs(b, d, b.SemaCtx(), b.EvalCtx(), tree.ColumnDefaultExprInAddColumn)

This MakeColumnDefDescs has this logic:

Code snippet:

func MakeColumnDefDescs(
	ctx context.Context,
	d *tree.ColumnTableDef,
	semaCtx *tree.SemaContext,
	evalCtx *eval.Context,
	defaultExprCtx tree.SchemaExprContext,
) (*ColumnDefDescs, error) {
	if d.IsSerial {
		// To the reader of this code: if control arrives here, this means
		// the caller has not suitably called processSerialInColumnDef()
		// prior to calling MakeColumnDefDescs. The dependent sequences
		// must be created, and the SERIAL type eliminated, prior to this
		// point.
		return nil, pgerror.New(pgcode.FeatureNotSupported,
			"SERIAL cannot be used in this context")
	}

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

Previously, fqazi (Faizan Qazi) wrote…

Nit: Does this need to be a function? There is no-reuse here or clarity improvement.

Thanks for catching this, it does not need to be a function, I just made the changes for this.


pkg/sql/logictest/testdata/logic_test/alter_table line 3995 at r1 (raw file):

Previously, fqazi (Faizan Qazi) wrote…

Should we check other modes too?

Yes, but maybe in future PRs? I'm planning on splitting this epic into 3 PRs, and I'll be re-writing/updating this testcase with the next PR as I will add support for sql_sequence. This is a temporary testcase, when we close this epic we won't need this specific testcase as sql_sequence and other modes will work and will not return an error.

@spilchen spilchen requested a review from fqazi September 4, 2024 15:22
Copy link
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.

It looks good to me too. I had a refactor suggestion and some other tests that we may want.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @Dedej-Bergin and @fqazi)


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

	if d.IsSerial {
		if b.SessionData().SerialNormalizationMode != sessiondatapb.SerialUsesRowID {
			panic(scerrors.NotImplementedErrorf(d, "contains serial data type in unsupported mode"))

nit: I suggest moving this panic into alterTableAddColumnSerial. Additionally, consider adding a switch statement to handle the different modes, which would be a logical place for this change.


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

		defType.Name(), b.SessionData().SerialNormalizationMode.String()))

	if defType.Width() < types.Int.Width() {

nit: I believe this logic is specific to using row ID as the default expression. And seeing as you need to expand this function for other serial normalization modes, could we structure this in a switch statement? That will make it easier to expand for other modes.


pkg/sql/logictest/testdata/logic_test/alter_table line 3971 at r1 (raw file):

subtest end

subtest alter_table_add_column_serial

Any tests to verify the dsc plan? i.e. in pkg/sql/schemachanger/scbuild/testdata, pkg/sql/schemachanger/scplan/testdata, and/or pkg/sql/schemachanger/testdata/end_to_end


pkg/sql/logictest/testdata/logic_test/alter_table line 3977 at r1 (raw file):


statement ok
create table roach (id int);

Can we add rows to the table? I think the alter is more interesting if it has to generate values for the new column.

@Dedej-Bergin Dedej-Bergin force-pushed the atac-serial branch 3 times, most recently from 5a790be to a9790ca Compare September 4, 2024 22:55
Copy link

blathers-crl bot commented Sep 4, 2024

Your pull request contains more than 1000 changes. It is strongly encouraged to split big PRs into smaller chunks.

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

Copy link
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 @fqazi and @spilchen)


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

Previously, spilchen wrote…

nit: I suggest moving this panic into alterTableAddColumnSerial. Additionally, consider adding a switch statement to handle the different modes, which would be a logical place for this change.

Thanks Matt! I was thinking of moving the panic and adding the switch on my next PR as the code grows with additional mode support.


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

Previously, spilchen wrote…

nit: I believe this logic is specific to using row ID as the default expression. And seeing as you need to expand this function for other serial normalization modes, could we structure this in a switch statement? That will make it easier to expand for other modes.

Thanks Matt! I'll add the switch on my next PR if that's okay? As the code grows with additional mode support.


pkg/sql/logictest/testdata/logic_test/alter_table line 3971 at r1 (raw file):

Previously, spilchen wrote…

Any tests to verify the dsc plan? i.e. in pkg/sql/schemachanger/scbuild/testdata, pkg/sql/schemachanger/scplan/testdata, and/or pkg/sql/schemachanger/testdata/end_to_end

Just added them! Thanks Matt!


pkg/sql/logictest/testdata/logic_test/alter_table line 3977 at r1 (raw file):

Previously, spilchen wrote…

Can we add rows to the table? I think the alter is more interesting if it has to generate values for the new column.

Sounds good, I added some rows, thanks Matt!

@Dedej-Bergin Dedej-Bergin force-pushed the atac-serial branch 2 times, most recently from ae355e1 to 0f745bf Compare September 5, 2024 00:10
Copy link
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 r2, 1 of 1 files at r3, 15 of 15 files at r4, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @fqazi)


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

Previously, Dedej-Bergin (Bergin Dedej) wrote…

Thanks Matt! I was thinking of moving the panic and adding the switch on my next PR as the code grows with additional mode support.

Yes, this is totally fine by me.


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

Previously, Dedej-Bergin (Bergin Dedej) wrote…

The d.IsSerial was already being flipped before, it was just happening a few function/method calls deep which made it a bit unclear.

But we need d.IsSerial to be false because it gets checked again lower in the code:

On line 115: cdd, err := tabledesc.MakeColumnDefDescs(b, d, b.SemaCtx(), b.EvalCtx(), tree.ColumnDefaultExprInAddColumn)

This MakeColumnDefDescs has this logic:

I would suggest that we leave a comment so that we know why we flip it. Also, could we do the flip inside alterTableAddColumnSerial? I think that makes sense logically since we are gong to be returning a modified ColumnTableDef anyway.


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

Previously, Dedej-Bergin (Bergin Dedej) wrote…

Thanks Matt! I'll add the switch on my next PR if that's okay? As the code grows with additional mode support.

Yes, I'm okay with this. Thanks.

@Dedej-Bergin
Copy link
Contributor Author

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

Previously, spilchen wrote…

I would suggest that we leave a comment so that we know why we flip it. Also, could we do the flip inside alterTableAddColumnSerial? I think that makes sense logically since we are gong to be returning a modified ColumnTableDef anyway.

Sounds good, I added the comment and moved it to alterTableAddColumnSerial.

Copy link
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 r5, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @Dedej-Bergin and @fqazi)


pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_add_column.go line 301 at r5 (raw file):

	}

	// Flip to false, as we validate or panic with this in MakeColumnDefDescs.

minor nit: Would it be better to phrase this in terms of what we're doing rather than what we're avoiding? For example:

// Serial is an alias for a real column definition. Set to false to indicate the alias has been remapped.

I also noticed that in the function comment for MakeColumnDefDescs, we specify which functions should be called to clear the IsSerial flag. Should we also mention alterTableAddColumnSerial for DSC alters?

Previously we could only add SERIAL columns with the legacy schema changer
with these code changes the ALTER TABLE ADD COLUMN statement
can now add SERIAL type columns via the declarative schema changer for
the default rowid serial_normalization mode.

Informs: cockroachdb#126900
Release note: none
@Dedej-Bergin
Copy link
Contributor Author

pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_add_column.go line 301 at r5 (raw file):

Previously, spilchen wrote…

minor nit: Would it be better to phrase this in terms of what we're doing rather than what we're avoiding? For example:

// Serial is an alias for a real column definition. Set to false to indicate the alias has been remapped.

I also noticed that in the function comment for MakeColumnDefDescs, we specify which functions should be called to clear the IsSerial flag. Should we also mention alterTableAddColumnSerial for DSC alters?

Yes, I just changed the comment. For the function comment for MakeColumnDefDescs I was thinking of trying to refactor it in a later PR. So maybe for now I will leave it as is and I will either refactor it or update the comment for it in a future PR if that's okay?

Copy link
Collaborator

@fqazi fqazi 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 1 of 2 files at r2, 4 of 15 files at r4, 1 of 2 files at r5, 12 of 12 files at r6, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @spilchen)

@Dedej-Bergin
Copy link
Contributor Author

bors r+

Copy link
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 all commit messages.
Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @fqazi)


pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_add_column.go line 301 at r5 (raw file):

Previously, Dedej-Bergin (Bergin Dedej) wrote…

Yes, I just changed the comment. For the function comment for MakeColumnDefDescs I was thinking of trying to refactor it in a later PR. So maybe for now I will leave it as is and I will either refactor it or update the comment for it in a future PR if that's okay?

Yes, that's fine. Thanks for revising.

@celiala
Copy link
Collaborator

celiala commented Sep 6, 2024

bors crashed yesterday. re-trying:

bors r+

@craig craig bot merged commit 09f968d into cockroachdb:master Sep 6, 2024
23 checks passed
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.

5 participants