Skip to content

sql/schemachanger: support generated columns in ADD COLUMN#139135

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
fqazi:finishGeneratedSyntax
Jan 15, 2025
Merged

sql/schemachanger: support generated columns in ADD COLUMN#139135
craig[bot] merged 1 commit intocockroachdb:masterfrom
fqazi:finishGeneratedSyntax

Conversation

@fqazi
Copy link
Copy Markdown
Collaborator

@fqazi fqazi commented Jan 15, 2025

Previously, the add column statement in the declarative schema changer only supported serial columns. This patch adds support for adding generated columns inside the declarative schema changer.

Fixes: #128087

Release note: None

@fqazi fqazi requested a review from a team as a code owner January 15, 2025 14:31
@blathers-crl
Copy link
Copy Markdown

blathers-crl bot commented Jan 15, 2025

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.

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

very nice that this was pretty simple to add, nice work. i just had one question.

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


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

	// Generate identity is always a SQL sequence based column.
	if isGeneratedColumn {
		serialNormalizationMode = sessiondatapb.SerialUsesSQLSequences

so i think this would lead to a divergence from current behavior. i wrote up a quick logic test to demonstrate the current behavior, where adding a column like this is GENERATED, but still backed by unique_rowid. can you add a test case like the one below? (and if we want to intentionally change the behavior, let's codify that in tests.)

statement ok
SET serial_normalization = rowid

statement ok
CREATE TABLE t(a INT PRIMARY KEY)

statement ok
ALTER TABLE t ADD COLUMN b SERIAL GENERATED ALWAYS AS IDENTITY

query T
SELECT crdb_internal.pb_to_json('desc', descriptor)->'table'->'columns'->1->>'defaultExpr'
FROM system.descriptor
WHERE id = 't_serial_identity_no_sequence'::regclass::oid
----
unique_rowid()

query T
SELECT crdb_internal.pb_to_json('desc', descriptor)->'table'->'columns'->1->>'generatedAsIdentityType'
FROM system.descriptor
WHERE id = 't_serial_identity_no_sequence'::regclass::oid
----
GENERATED_ALWAYS

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.

Do you think we need any tests added under sql/schemachanger/scbuild/testdata and sql/schemachanger/scplan/testdata? Or do you think we have sufficient coverage with the e2e tests you added.

Reviewed all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @fqazi and @rafiss)


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

	// Generate identity is always a SQL sequence based column.
	if isGeneratedColumn {
		serialNormalizationMode = sessiondatapb.SerialUsesSQLSequences

Do we need to set this before we call telemetry.Inc(sqltelemetry.SerialColumnNormalizationCounter( above? It seems like we might increment the wrong counter instead.

Previously, the add column statement in the declarative schema changer
only supported serial columns. This patch adds support for adding
generated columns inside the declarative schema changer.

Fixes: cockroachdb#128087

Release note: None
@fqazi fqazi force-pushed the finishGeneratedSyntax branch from 3f1db20 to ac9a722 Compare January 15, 2025 18:51
Copy link
Copy Markdown
Collaborator Author

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

Added extra tests for generated columns under both

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


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

Previously, rafiss (Rafi Shamim) wrote…

so i think this would lead to a divergence from current behavior. i wrote up a quick logic test to demonstrate the current behavior, where adding a column like this is GENERATED, but still backed by unique_rowid. can you add a test case like the one below? (and if we want to intentionally change the behavior, let's codify that in tests.)

statement ok
SET serial_normalization = rowid

statement ok
CREATE TABLE t(a INT PRIMARY KEY)

statement ok
ALTER TABLE t ADD COLUMN b SERIAL GENERATED ALWAYS AS IDENTITY

query T
SELECT crdb_internal.pb_to_json('desc', descriptor)->'table'->'columns'->1->>'defaultExpr'
FROM system.descriptor
WHERE id = 't_serial_identity_no_sequence'::regclass::oid
----
unique_rowid()

query T
SELECT crdb_internal.pb_to_json('desc', descriptor)->'table'->'columns'->1->>'generatedAsIdentityType'
FROM system.descriptor
WHERE id = 't_serial_identity_no_sequence'::regclass::oid
----
GENERATED_ALWAYS

Done.

It gets the same behaviour as before because IsGeneratedColumn is only set if the column is not serial. I added a test to confirm this


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

Previously, spilchen wrote…

Do we need to set this before we call telemetry.Inc(sqltelemetry.SerialColumnNormalizationCounter( above? It seems like we might increment the wrong counter instead.

Done.

Good catch!

@fqazi fqazi requested review from rafiss and spilchen January 15, 2025 18:51
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 20 of 26 files at r1, 4 of 4 files at r2, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @rafiss)

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 work! i added a backport label since the branch cut just happened today, but we want this in 25.1

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

@fqazi
Copy link
Copy Markdown
Collaborator Author

fqazi commented Jan 15, 2025

@rafiss @spilchen TFTR!

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Jan 15, 2025

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.

ALTER TABLE ADD COLUMN add DSC support for GENERATED

4 participants