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 drop type to the prepared statement generator #61394

Merged
merged 1 commit into from
Mar 4, 2021

Conversation

tharun208
Copy link
Contributor

@tharun208 tharun208 commented Mar 3, 2021

Release justification: fixes for high-priority or high-severity bugs in existing functionality

Previously, prepare statement doesn't have support for drop type
in the optimizer, so it panics during execution as no flags are generated.

This patch fixes this by adding dropType to the optimizer.

Resolves #61226

Release note: none

Signed-off-by: Tharun rajendrantharun@live.com

Screen Shot 2021-03-03 at 2 39 29 PM

@blathers-crl
Copy link

blathers-crl bot commented Mar 3, 2021

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.

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 Mar 3, 2021
@blathers-crl blathers-crl bot requested a review from rafiss March 3, 2021 08:15
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@tharun208
Copy link
Contributor Author

@rafiss, can you help by re-triggering the ci ?. thanks in advance.

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 this fix!

this needs a test. can you add one in pkg/sql/logictest/testdata/logic_test/drop_type

the change looks fine, but since it's optimizer-related and we're in stability period, @RaduBerinde can you please sign off?

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


pkg/sql/plan_opt.go, line 64 at r1 (raw file):

		*tree.CreateStats,
		*tree.Deallocate, *tree.Discard, *tree.DropDatabase, *tree.DropIndex,
		*tree.DropTable, *tree.DropView, *tree.DropSequence,

could you add DropType here, closer to the other drop statements?

@rafiss rafiss requested a review from RaduBerinde March 3, 2021 16:10
@tharun208
Copy link
Contributor Author

pkg/sql/logictest/testdata/logic_test/drop_type

@rafiss, I think the test should need to be a part of prepare statement

@blathers-crl
Copy link

blathers-crl bot commented Mar 3, 2021

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.

@tharun208 tharun208 force-pushed the sql/fix_prepare_drop_type branch 2 times, most recently from fb9f20f to bd61249 Compare March 3, 2021 18:18
@tharun208 tharun208 requested a review from rafiss March 3, 2021 18:19
@tharun208 tharun208 force-pushed the sql/fix_prepare_drop_type branch 2 times, most recently from e87836a to 726ca2b Compare March 3, 2021 19:12
Copy link
Member

@RaduBerinde RaduBerinde left a comment

Choose a reason for hiding this comment

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

:lgtm:

Can you also add DROP TYPE to TestStatementReuses ? This tests that all those statements work with PREPARE and EXPLAIN.

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

@tharun208
Copy link
Contributor Author

tharun208 commented Mar 3, 2021

:lgtm:

Can you also add DROP TYPE to TestStatementReuses ? This tests that all those statements work with PREPARE and EXPLAIN.

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

@RaduBerinde done with the changes. To pass the TestStatementReuses , I need to add Explain to the plan_opt. But , now existing test for select explain is failing

Copy link
Member

@RaduBerinde RaduBerinde 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 (and 1 stale) (waiting on @rafiss and @tharun208)


pkg/sql/plan_opt.go, line 65 at r2 (raw file):

		*tree.Deallocate, *tree.Discard, *tree.DropDatabase, *tree.DropIndex,
		*tree.DropTable, *tree.DropView, *tree.DropSequence, *tree.DropType,
		*tree.Execute, *tree.Explain,

Adding Explain here is not correct, Explain has result columns and is supported by the optimizer.

I assume you added it because some PREPARE EXPLAIN case was failing?

@tharun208
Copy link
Contributor Author

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

pkg/sql/plan_opt.go, line 65 at r2 (raw file):

		*tree.Deallocate, *tree.Discard, *tree.DropDatabase, *tree.DropIndex,
		*tree.DropTable, *tree.DropView, *tree.DropSequence, *tree.DropType,
		*tree.Execute, *tree.Explain,

Adding Explain here is not correct, Explain has result columns and is supported by the optimizer.

I assume you added it because some PREPARE EXPLAIN case was failing?

yes, just now checked EXPLAIN will return the result columns and yes, I added *tree.Explain because, when I tried adding DROP TYPE to TestStatementReuses , PREPARE p AS EXPLAIN of my test DROP TYPE type_name failed

@RaduBerinde
Copy link
Member


pkg/sql/plan_opt.go, line 65 at r2 (raw file):

Previously, tharun208 (Tharun Rajendran) wrote…

yes, just now checked EXPLAIN will return the result columns and yes, I added *tree.Explain because, when I tried adding DROP TYPE to TestStatementReuses , PREPARE p AS EXPLAIN of my test DROP TYPE type_name failed

What was the failure?

@tharun208
Copy link
Contributor Author

pkg/sql/plan_opt.go, line 65 at r2 (raw file):

Previously, tharun208 (Tharun Rajendran) wrote…
What was the failure?

The same stack trace error we got for DROP TYPE on prepared statement , no Annotations found.

@RaduBerinde
Copy link
Member

I think the right fix here is to move the part that uses .Ann() in startExec instead of DropType, similar to how the drop_table code does it.

@tharun208
Copy link
Contributor Author

I think the right fix here is to move the part that uses .Ann() in startExec instead of DropType, similar to how the drop_table code does it.

yes, I am able to achieve it, without adding dropType to the optimizer.

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.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @RaduBerinde and @tharun208)


pkg/sql/drop_type.go, line 33 at r3 (raw file):

type typeToDrop struct {
	desc   *typedesc.Mutable
	fqName string

i believe fqName should be removed now


pkg/sql/drop_type.go, line 111 at r3 (raw file):

		// Record these descriptors for deletion.
		node.td[typeDesc.ID] = typeToDrop{
			desc: typeDesc,

instead of removing fqName here, does it work with:

		fqName, err := getTypeNameFromTypeDescriptor(
			oneAtATimeSchemaResolver{ctx, p},
			typeDesc,
		)
		if err != nil {
			return nil, err
		}
		node.td[typeDesc.ID] = typeToDrop{
			desc:   typeDesc,
			fqName: fqName.FQString(),
		}

pkg/sql/drop_type.go, line 151 at r3 (raw file):

func (n *dropTypeNode) startExec(params runParams) error {
	for _, toDrop := range n.td {
		typ, fqName := toDrop.desc, toDrop.fqName

and now this can be changed to

fqName := tree.AsStringWithFQNames(n.n, params.Ann())

pkg/sql/drop_type.go, line 152 at r3 (raw file):

	for _, toDrop := range n.td {
		typ, fqName := toDrop.desc, toDrop.fqName
		if err := params.p.dropTypeImpl(params.ctx, typ, tree.AsStringWithFQNames(n.n, params.Ann()), true /* queueJob */); err != nil {

use fqName as the 3rd argument here

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.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @RaduBerinde and @tharun208)


pkg/sql/drop_type.go, line 33 at r3 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

i believe fqName should be removed now

sorry, i didn't mean to publish this comment.


pkg/sql/drop_type.go, line 151 at r3 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

and now this can be changed to

fqName := tree.AsStringWithFQNames(n.n, params.Ann())

i didn't mean to publish this comment

Copy link
Member

@RaduBerinde RaduBerinde 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 (and 1 stale) (waiting on @tharun208)


pkg/sql/drop_type.go, line 112 at r3 (raw file):

		node.td[typeDesc.ID] = typeToDrop{
			desc:   typeDesc,
			fqName: tree.AsStringWithFQNames(name, p.Ann()),

I don't get it, now fqName is not set in half the cases, but we are using it in startExec.

Why don't we remove the field altogether and just do getTypeNameFromTypeDescriptor() in all cases instartExec and use FQString()

@RaduBerinde
Copy link
Member


pkg/sql/drop_type.go, line 152 at r3 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

use fqName as the 3rd argument here

Noete that the third argument is the entire statement, not just the type name. I would move it out of the loop and assign to an aptly named variable, it's silly to reformat for each type.

@tharun208 tharun208 marked this pull request as draft March 4, 2021 06:35
@tharun208 tharun208 marked this pull request as ready for review March 4, 2021 07:46
@tharun208
Copy link
Contributor Author

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

pkg/sql/drop_type.go, line 112 at r3 (raw file):

		node.td[typeDesc.ID] = typeToDrop{
			desc:   typeDesc,
			fqName: tree.AsStringWithFQNames(name, p.Ann()),

I don't get it, now fqName is not set in half the cases, but we are using it in startExec.

Why don't we remove the field altogether and just do getTypeNameFromTypeDescriptor() in all cases instartExec and use FQString()

this will change the TypeName in the event log. Current changes will not alter the event log` and all necessary cases got passed.

Copy link
Member

@RaduBerinde RaduBerinde 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 (and 1 stale) (waiting on @rafiss and @tharun208)


pkg/sql/drop_type.go, line 112 at r3 (raw file):

Previously, tharun208 (Tharun Rajendran) wrote…

this will change the TypeName in the event log. Current changes will not alter the event log` and all necessary cases got passed.

I don't understand.. what I'm suggesting should be equivalent to the latest revision, we would just call getTypeNameFromTypeDescriptor from startExec instead. Here's a diff (it passes the sql package tests and the drop_type logictest):

https://gist.github.com/RaduBerinde/f78a2b2c17f22cc429734b366f53edc5

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.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @RaduBerinde and @tharun208)


pkg/sql/drop_type.go, line 152 at r3 (raw file):

Previously, RaduBerinde wrote…

Noete that the third argument is the entire statement, not just the type name. I would move it out of the loop and assign to an aptly named variable, it's silly to reformat for each type.

oh good call. hmm well also this parameter is the job description, which is used in the db console as a display name. so perhaps it would be better for them to have different names anyway.

maybe change to

params.p.dropTypeImpl(params.ctx, typ, "dropping type " + fqName, true /* queueJob */)

@RaduBerinde
Copy link
Member


pkg/sql/drop_type.go, line 152 at r3 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

oh good call. hmm well also this parameter is the job description, which is used in the db console as a display name. so perhaps it would be better for them to have different names anyway.

maybe change to

params.p.dropTypeImpl(params.ctx, typ, "dropping type " + fqName, true /* queueJob */)

Yeah that's probably better

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.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @RaduBerinde, @rafiss, and @tharun208)


pkg/sql/drop_type.go, line 135 at r4 (raw file):

	statementStr := tree.AsStringWithFQNames(n.n, params.Ann())
	for _, typeDesc := range n.toDrop {
		err := params.p.dropTypeImpl(params.ctx, typeDesc, "dropping type "+statementStr, true /* queueJob */)

please change "dropping type "+statementStr to dropping type "+typeFQName

and remove the statementStr variable entirely

@tharun208
Copy link
Contributor Author

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @RaduBerinde, @rafiss, and @tharun208)

pkg/sql/drop_type.go, line 135 at r4 (raw file):

	statementStr := tree.AsStringWithFQNames(n.n, params.Ann())
	for _, typeDesc := range n.toDrop {
		err := params.p.dropTypeImpl(params.ctx, typeDesc, "dropping type "+statementStr, true /* queueJob */)

please change "dropping type "+statementStr to dropping type "+typeFQName

and remove the statementStr variable entirely

so, this dropping type "+typeFQName will be changed to dropping type "+typeFQName.FQString(). since both are of different types.

Release justification: fixes for high-priority or high-severity bugs in existing functionality

Previously, prepare statement doesnt have support for drop type
in optimizer, so it panic during execution as no flags are generated.

This patch fixes this by adding dropType to the optimizer.

Resolves cockroachdb#61226

Release note: none

Signed-off-by: Tharun <rajendrantharun@live.com>
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.

thank you! this was a nice cleanup

@rafiss
Copy link
Collaborator

rafiss commented Mar 4, 2021

bors r+

Copy link
Member

@RaduBerinde RaduBerinde left a comment

Choose a reason for hiding this comment

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

:lgtm: Thanks!

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

@craig
Copy link
Contributor

craig bot commented Mar 4, 2021

Build succeeded:

@craig craig bot merged commit 69ed26b into cockroachdb:master Mar 4, 2021
@tharun208 tharun208 deleted the sql/fix_prepare_drop_type branch March 4, 2021 22:23
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: PREPARE q AS DROP TYPE typ causes an internal error
4 participants