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: Allow Identity Column to Change Generation Type #115889

Merged
merged 1 commit into from
Mar 11, 2024

Conversation

andrew-delph
Copy link
Contributor

@andrew-delph andrew-delph commented Dec 8, 2023

sql: Allow Alter Column to ADD and SET Identity
Informs: #110010

This change adds a new command, 'ALTER TABLE
ALTER [COLUMN] ADD GENERATED
{ ALWAYS | BY DEFAULT } [( <opt_sequence_option_list> )]'
and 'ALTER TABLE ALTER [COLUMN] SET
GENERATED { ALWAYS | BY DEFAULT }',
which allows a column to become an Identity
or change the generation type of the the
identity.

Release note (sql change): Columns can have
become an IdentityType by running 'ALTER
TABLE t ALTER COLUMN c ADD GENERATED ALWAYS
[( <opt_sequence_option_list> )]' or 'ALTER TABLE t ALTER
COLUMN c ADD GENERATED BY DEFAULT [( <opt_sequence_option_list> )]'.
Identity columns can have their IdentityType altered
by running 'ALTER TABLE t ALTER COLUMN c SET GENERATED ALWAYS'
or 'ALTER TABLE t ALTER COLUMN c SET GENERATED BY DEFAULT'.

@andrew-delph andrew-delph requested review from a team as code owners December 8, 2023 19:59
Copy link

blathers-crl bot commented Dec 8, 2023

It looks like your PR touches SQL parser code but doesn't add or edit parser tests. Please make sure you add or edit parser tests if you edit the parser.

Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

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

@blathers-crl blathers-crl bot added the O-community Originated from the community label Dec 8, 2023
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link

blathers-crl bot commented Dec 8, 2023

Thank you for updating your pull request.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

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

@andrew-delph andrew-delph force-pushed the alter_col_gen branch 5 times, most recently from 7e5e1e2 to 6cf9e82 Compare December 12, 2023 03:20
pkg/sql/parser/sql.y Outdated Show resolved Hide resolved
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.

This all looks good, just one minor comment for the commit message.

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


-- commits line 4 at r1:
Change this to Informs: #110010

Copy link
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.

thanks for your contribution!

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @andrew-delph)


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

	case *tree.AlterTableSetGeneratedAlways:
		if !col.IsGeneratedAsIdentity() {
			return sqlerrors.NewSyntaxErrorf("column %q is not an identity column", col.GetName())

nit: let's match the postgres error code, and use pgcode.ObjectNotInPrerequisiteState here:

return pgerror.Newf(pgcode.ObjectNotInPrerequisiteState,  
  "column %s of relation %s is not an identity column",  
  col.GetName(), tableDesc.GetName())

(and same for the one below)


pkg/sql/parser/sql.y line 2767 at r1 (raw file):

Previously, andrew-delph (Andrew Delph) wrote…

Possibly, It would be better to return a struct here such as
AlterTableIdentity{
AlterTableIdentityOption option
}

It would give more similarity to Postgres and easier list options
https://github.com/postgres/postgres/blob/0e917508b89dd21c5bcd9183e77585f01055a20d/src/backend/parser/gram.y#L3037C7-L3037C7

i agree with this - let's use a struct like the one you described


pkg/sql/sem/tree/stmt.go line 2252 at r1 (raw file):

func (n *AlterTableDropNotNull) String() string               { return AsString(n) }
func (n *AlterTableDropStored) String() string                { return AsString(n) }
func (n *AlterTableSetGeneratedAlways) String() string        { return AsString(n) }

nit: should AlterTableSetGeneratedDefault be here too?

Copy link

blathers-crl bot commented Dec 23, 2023

Thank you for updating your pull request.

Before a member of our team reviews your PR, I have some potential action items for you:

  • We notice you have more than one commit in your PR. We try break logical changes into separate commits, but commits such as "fix typo" or "address review commits" should be squashed into one commit and pushed with --force
  • Please ensure your git commit message contains a release note.
  • When CI has completed, please ensure no errors have appeared.

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

@andrew-delph andrew-delph force-pushed the alter_col_gen branch 5 times, most recently from dbc1116 to 319a1b0 Compare December 23, 2023 22:34
@andrew-delph
Copy link
Contributor Author

Thanks @fqazi and @rafiss for reviews.
I have updated the pr with the requested changes.

Copy link
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.

thanks for the update! this is getting close

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


pkg/sql/parser/sql.y line 10184 at r3 (raw file):

  SET GENERATED_ALWAYS ALWAYS {
    x:= tree.GeneratedAlways
    $$.val = tree.IdentityOption{GeneratedAsIdentityType: &x}

as per the below comment, this should not be a pointer, so this should just be

$$.val = tree.IdentityOption{GeneratedAsIdentityType: tree.GeneratedAlways}

(and similar for below)


pkg/sql/sem/tree/alter_table.go line 746 at r3 (raw file):

type IdentityOption struct {
	SeqOption               *SequenceOption
	GeneratedAsIdentityType *GeneratedIdentityType

nit: this should not be a pointer. GeneratedIdentityType is an enum, so it should just be referenced by value.

@andrew-delph
Copy link
Contributor Author

andrew-delph commented Feb 15, 2024

Hi @rafiss ,
I have modified the pr to include the 'ADD GENERATED...' command and also changed the parser layout.

Here is the current grammar:
image

This is what the change to the grammar looks like:
image

@andrew-delph andrew-delph force-pushed the alter_col_gen branch 4 times, most recently from 94f8e50 to c38fedc Compare February 16, 2024 17:29
@andrew-delph andrew-delph requested a review from a team as a code owner February 16, 2024 17:29
@andrew-delph andrew-delph requested review from mgartner and removed request for a team February 16, 2024 17:29
@andrew-delph andrew-delph marked this pull request as draft February 19, 2024 15:47
@andrew-delph andrew-delph force-pushed the alter_col_gen branch 4 times, most recently from ff3bb6a to 7f92192 Compare February 20, 2024 20:56
@andrew-delph andrew-delph marked this pull request as ready for review February 21, 2024 02:41
@andrew-delph
Copy link
Contributor Author

@andrew-delph andrew-delph force-pushed the alter_col_gen branch 3 times, most recently from 57e03ac to 8a87443 Compare February 21, 2024 22:23
@andrew-delph
Copy link
Contributor Author

I have updated the set identity grammer to the following:
image
image

if err != nil {
return err
}
newDef, prefix, seqName, seqOpts, err := params.p.processSerialLikeInColumnDef(params.ctx, colDef, tn)
Copy link
Contributor Author

@andrew-delph andrew-delph Feb 25, 2024

Choose a reason for hiding this comment

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

Creating the column definition here in this way feels incorrect.
I believe that using the ColumnTableDef provides a way to use existing functions for generation of the identity sequence. The goal for doing this is to follow existing steps for the sequence creation.

After creation of the sequence, the existing column is then given ownership.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Possibly, the tree.NewColumnTableDef can instead be called in the sql.y

@rafiss rafiss self-requested a review March 11, 2024 17:08
fixes: cockroachdb#110010

This change adds a new command, 'ALTER TABLE
ALTER [COLUMN] <colname> ADD GENERATED { ALWAYS | BY DEFAULT }
AS IDENTITY [( <opt_sequence_option_list> )]'
and 'ALTER TABLE ALTER [COLUMN] <colname> SET
GENERATED { ALWAYS | BY DEFAULT }',
which allows a column to become an Identity
or change the generation type of the the
identity.

Release note (sql change): Columns can be changed to an identity column
by running one of:
- `ALTER TABLE t ALTER COLUMN c ADD GENERATED ALWAYS AS IDENTITY [( <opt_sequence_option_list> )]`
- `ALTER TABLE t ALTER COLUMN c ADD GENERATED BY DEFAULT AS IDENTITY[( <opt_sequence_option_list> )]`

Identity columns can have their identity type altered by running one of:
- `ALTER TABLE t ALTER COLUMN c SET GENERATED ALWAYS`
- `ALTER TABLE t ALTER COLUMN c SET GENERATED BY DEFAULT`
Copy link
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.

thanks for your contribution! i have updated the release note slightly, and merging this now

bors r+

@craig
Copy link
Contributor

craig bot commented Mar 11, 2024

Build succeeded:

@craig craig bot merged commit df1220b into cockroachdb:master Mar 11, 2024
13 of 19 checks passed
@andrew-delph
Copy link
Contributor Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-community Originated from the community
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants