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: add notice for Alter TYPE ADD VALUE IF NOT EXIST command #52534

Merged
merged 1 commit into from Aug 10, 2020

Conversation

himanshuchawla009
Copy link
Contributor

@himanshuchawla009 himanshuchawla009 commented Aug 8, 2020

Fixes: #52327

Added a notice message similar to postgres if user tries to add a value in enum which already exists using IF NOT EXIST command.

root@:26257/defaultdb> alter type color ADD VALUE IF NOT EXISTS 'black';
ALTER TYPE

root@:26257/defaultdb> alter type color ADD VALUE IF NOT EXISTS 'black';
NOTICE: enum label "black" already exists, skipping

However, I noticed similar behaviour in other command like ALTER TABLE command. Altering existing table with if not exist
command doesn't shows any notice.

Release note (sql change): Show notice to user when user tries to add value which already exists in enum .

@cockroach-teamcity
Copy link
Member

cockroach-teamcity commented Aug 8, 2020

CLA assistant check
All committers have signed the CLA.

@blathers-crl
Copy link

blathers-crl bot commented Aug 8, 2020

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

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

  • Please ensure your git commit message contains a release note.
  • When CI has completed, please ensure no errors have appeared.

I have added a few people who may be able to assist in reviewing:

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan.

@blathers-crl blathers-crl bot added O-community Originated from the community X-blathers-triaged blathers was able to find an owner labels Aug 8, 2020
@blathers-crl blathers-crl bot requested a review from awoods187 August 8, 2020 07:49
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rohany rohany requested review from rohany and removed request for awoods187 August 8, 2020 19:21
@@ -268,7 +269,7 @@ func (desc *MutableTypeDescriptor) AddEnumValue(node *tree.AlterTypeAddValue) er
}
if found {
if node.IfNotExists {
return nil
return pgnotice.Newf("enum label %q already exists, skipping", node.NewVal)
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't want to return an error here. Instead, we want to use p.SendClientNotice and return nil. Also add a test in pkg/sql/logictest/testdata/logic_test/notice

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ohh... thanks for your review @rohany . i have noticed that sqlbase package is getting removed here #52519. Also planner struct is not accessible in sqlbase package. So using p.SendClientNotice directly doesn't seems feasible to me.
I have just started with this project and i am reading architecture docs in details. So your guidelines will be really helpful for me as a beginner to opensource world as i really want to learn and contribute to this project.

Copy link
Contributor

Choose a reason for hiding this comment

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

i have noticed that sqlbase package is getting removed here #52519.

This isn't a worry -- all of the code will still be here, the package is just getting split up into smaller packages.

Also planner struct is not accessible in sqlbase package. So using p.SendClientNotice directly doesn't seems feasible to me.

There are a couple options here:

  1. return an error here, and then catch it and convert it into a notice in the statement execution path
  2. pass a closure into this function that does the work of sending a notice
  3. In the statement (alter_type.go) just check if the enum value exists or not. Then this function can just error if it finds the value already present in the enum.

Overall, I think that the third is the cleanest

@blathers-crl
Copy link

blathers-crl bot commented Aug 8, 2020

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

@@ -154,7 +154,7 @@ ALTER TYPE build ADD VALUE 'a' BEFORE 'b'
statement error pq: \"b\" is not an existing enum label
ALTER TYPE build ADD VALUE 'a' AFTER 'b'

statement ok
statement error pq: enum label \"c\" already exists, skipping
Copy link
Contributor

Choose a reason for hiding this comment

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

Please look at the test I mentioned. It isn't correct to return an error here! The idea of a notice is that the statement executes successfully, but just prints out a notice to the user.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is pushed before i saw your reviews, i have submitted the changes as requested by you. I have added logic test in notice file and sent a client notice in alter_type.go file.

@@ -268,7 +269,7 @@ func (desc *MutableTypeDescriptor) AddEnumValue(node *tree.AlterTypeAddValue) er
}
if found {
if node.IfNotExists {
return nil
return pgnotice.Newf("enum label %q already exists, skipping", node.NewVal)
Copy link
Contributor

Choose a reason for hiding this comment

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

i have noticed that sqlbase package is getting removed here #52519.

This isn't a worry -- all of the code will still be here, the package is just getting split up into smaller packages.

Also planner struct is not accessible in sqlbase package. So using p.SendClientNotice directly doesn't seems feasible to me.

There are a couple options here:

  1. return an error here, and then catch it and convert it into a notice in the statement execution path
  2. pass a closure into this function that does the work of sending a notice
  3. In the statement (alter_type.go) just check if the enum value exists or not. Then this function can just error if it finds the value already present in the enum.

Overall, I think that the third is the cleanest

@blathers-crl
Copy link

blathers-crl bot commented Aug 9, 2020

Thank you for updating your pull request.

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

  • 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 otan.

@blathers-crl
Copy link

blathers-crl bot commented Aug 9, 2020

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

Copy link
Contributor

@rohany rohany left a comment

Choose a reason for hiding this comment

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

Looking good! Small comments now.

found := false
for _, member := range n.desc.EnumMembers {
if member.LogicalRepresentation == node.NewVal {
found = true
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure why it wasn't written this way in the first place, but we don't need a new variable and a break -- just do the notice/error return in here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

my bad, i have updated this now.

@@ -255,24 +255,6 @@ func (desc *MutableTypeDescriptor) IsNew() bool {

// AddEnumValue adds an enum member to the type.
func (desc *MutableTypeDescriptor) AddEnumValue(node *tree.AlterTypeAddValue) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Update the comment here now to say that "AddEnumValue assumes that the type is an enum, and that the new value doesn't exist already in the enum"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated comment, i will make sure to do such descriptive code comments in future contributions.

Copy link
Contributor

@rohany rohany left a comment

Choose a reason for hiding this comment

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

Some more nits.

Please update your commit title and message with the following:

  • The commit title should be prefaced with the packages touched (i.e sql: add notice for ALTER TYPE ADD VALUE ...)
  • The release note should also be a properly formatted sentence, and should use the correct release note tag -- in this case, it would be sql change, not just sql

return nil
}
return pgerror.Newf(pgcode.DuplicateObject, "enum label %q already exists", node.NewVal)

Copy link
Contributor

Choose a reason for hiding this comment

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

remove this newline

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@@ -253,26 +253,8 @@ func (desc *MutableTypeDescriptor) IsNew() bool {
return desc.ClusterVersion == nil
}

// AddEnumValue adds an enum member to the type.
//AddEnumValue assumes that the type is an enum, and that the new value doesn't exist already in the enum
Copy link
Contributor

Choose a reason for hiding this comment

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

make sure that your comment lines don't exceed 80 characters, and are full sentences (with a period at the end). You also must add a space after the //

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@himanshuchawla009 himanshuchawla009 changed the title notice/hint added for Alter TYPE ADD VALUE IF NOT EXIST command sql: add notice for Alter TYPE ADD VALUE IF NOT EXIST command Aug 10, 2020
@@ -253,26 +253,9 @@ func (desc *MutableTypeDescriptor) IsNew() bool {
return desc.ClusterVersion == nil
}

// AddEnumValue adds an enum member to the type.
// AddEnumValue assumes that the type is an enum, and that the new value
// doesn't exist already in the enum.
Copy link
Contributor

Choose a reason for hiding this comment

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

Last nit -- don't also remove the comment that actually tells what the function does!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

Release note (sql change): Show notice to user when user
tries to add value which already exists in enum.
@rohany
Copy link
Contributor

rohany commented Aug 10, 2020

Thanks for the contribution! Merging.

bors r=rohany

@craig
Copy link
Contributor

craig bot commented Aug 10, 2020

Build succeeded:

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 X-blathers-triaged blathers was able to find an owner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql: improve HINT when using IF NOT EXISTS and ENUMs
3 participants