-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Conversation
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. |
c0eca19
to
b0e358c
Compare
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. |
7e5e1e2
to
6cf9e82
Compare
There was a problem hiding this 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: complete! 0 of 0 LGTMs obtained (waiting on @andrew-delph)
-- commits
line 4 at r1:
Change this to Informs: #110010
There was a problem hiding this 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: 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?
Thank you for updating your pull request. Before a member of our team reviews your PR, I have some potential action items for you:
🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf. |
dbc1116
to
319a1b0
Compare
319a1b0
to
c8eb478
Compare
There was a problem hiding this 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: 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.
c8eb478
to
3058ec2
Compare
3058ec2
to
0e36b60
Compare
0e36b60
to
4cc2cab
Compare
Hi @rafiss , |
94f8e50
to
c38fedc
Compare
c38fedc
to
e41426b
Compare
ff3bb6a
to
7f92192
Compare
7f92192
to
c032e99
Compare
|
57e03ac
to
8a87443
Compare
8a87443
to
ece7e93
Compare
if err != nil { | ||
return err | ||
} | ||
newDef, prefix, seqName, seqOpts, err := params.p.processSerialLikeInColumnDef(params.ctx, colDef, tn) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
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`
There was a problem hiding this 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+
Build succeeded: |
Thank you! |
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'.