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: reference sequences used in views by their IDs #61439

Merged
merged 1 commit into from
Mar 22, 2021
Merged

sql: reference sequences used in views by their IDs #61439

merged 1 commit into from
Mar 22, 2021

Conversation

the-ericwang35
Copy link
Contributor

@the-ericwang35 the-ericwang35 commented Mar 4, 2021

Followup to #59864, fixes #61205.
Previously, a change was introduced to store sequences
used in DEFAULT expressions to be referenced by their
IDs instead of their names, to allow sequence renaming.
In this patch, we want to do something similar by
rewriting sequence references in views to be
referenced by their IDs. This allows sequences
used in views to be renamed.

Another thing this PR does is change the output of sequences
to look like 's'::REGCLASS rather than 's':::STRING::REGCLASS,
since we're no longer type checking them (since it's unnecessary).

Release note (sql change): reference sequences used in views
by their IDs to allow these sequences to be renamed.

@the-ericwang35 the-ericwang35 marked this pull request as draft March 4, 2021 00:07
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@the-ericwang35 the-ericwang35 changed the title [WIP] [Do Not Merge] sql: reference sequences used in views by their IDs [WIP] sql: reference sequences used in views by their IDs Mar 4, 2021
@the-ericwang35 the-ericwang35 requested a review from a team March 10, 2021 20:07
@the-ericwang35 the-ericwang35 marked this pull request as ready for review March 10, 2021 23:54
@the-ericwang35 the-ericwang35 requested review from a team and adityamaru and removed request for a team and adityamaru March 10, 2021 23:54
@the-ericwang35 the-ericwang35 changed the title [WIP] sql: reference sequences used in views by their IDs sql: reference sequences used in views by their IDs Mar 11, 2021
@the-ericwang35 the-ericwang35 requested a review from ajwerner March 17, 2021 01:46
Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

nice

Reviewed 6 of 20 files at r1.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @the-ericwang35)


pkg/ccl/backupccl/restore_planning.go, line 128 at r1 (raw file):

// rewriteSequencesInExpr rewrites all sequence IDs in the input expression
// string according to rewrites.
func rewriteSequencesInExpr(expr string, rewrites DescRewriteMap) (string, error) {

did you test this for views? Can you write a test that backs up a view and then restores it? I'm worried we're going to have problems here.


pkg/sql/create_view.go, line 357 at r1 (raw file):

}

// startViewWalk prepares to walk the given viewQuery by defining the

this name is weird to me. I'd call it what it does, something about re-writing sequences.


pkg/sql/create_view.go, line 385 at r1 (raw file):

	stmt, err := parser.ParseOne(viewQuery)
	if err != nil {
		return "", err

nit: wrap this error for easier debugging


pkg/sql/show_create_clauses.go, line 112 at r1 (raw file):

	decodedViewQuery, err := formatViewQueryForDisplay(ctx, semaCtx, desc)
	if err != nil {
		f.WriteString(desc.GetViewQuery())

can you log a warning here?


pkg/sql/catalog/schemaexpr/column.go, line 327 at r1 (raw file):

// GetTypeExprAndSeqID takes an expr and looks for a sequence ID in
// this expr. If it finds one, it will return that ID as well as the
// Expr that contains it.

This API is now weird to me. If it returns a non-nil expr it always returns what it was given. How about just strip the expr return value? Also, it doesn't seem like anybody does anything with it anyway.

Copy link
Contributor Author

@the-ericwang35 the-ericwang35 left a comment

Choose a reason for hiding this comment

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

Dismissed @ajwerner from a discussion.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ajwerner)


pkg/ccl/backupccl/restore_planning.go, line 128 at r1 (raw file):

Previously, ajwerner wrote…

did you test this for views? Can you write a test that backs up a view and then restores it? I'm worried we're going to have problems here.

That's a good point, I've added some additional logic to look for descriptor rewrites in the view query, since it can now contain sequence IDs. I've also added a test to backup and restore a view.


pkg/sql/create_view.go, line 357 at r1 (raw file):

Previously, ajwerner wrote…

this name is weird to me. I'd call it what it does, something about re-writing sequences.

Done.


pkg/sql/create_view.go, line 385 at r1 (raw file):

Previously, ajwerner wrote…

nit: wrap this error for easier debugging

Done.


pkg/sql/show_create_clauses.go, line 112 at r1 (raw file):

Previously, ajwerner wrote…

can you log a warning here?

Done.


pkg/sql/catalog/schemaexpr/column.go, line 327 at r1 (raw file):

Previously, ajwerner wrote…

This API is now weird to me. If it returns a non-nil expr it always returns what it was given. How about just strip the expr return value? Also, it doesn't seem like anybody does anything with it anyway.

Good point, removed the expr return value.

Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

Reviewed 3 of 20 files at r1, 3 of 14 files at r2.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ajwerner and @the-ericwang35)


pkg/ccl/backupccl/restore_planning.go, line 128 at r1 (raw file):

Previously, the-ericwang35 (Eric Wang) wrote…

That's a good point, I've added some additional logic to look for descriptor rewrites in the view query, since it can now contain sequence IDs. I've also added a test to backup and restore a view.

I saw you added a test for the old views and making sure that that does not get upgraded. Can you also test that for these new views that when we back them up and then restore them that everything is happy -- that the references are updated correctly etc.

I think there are a few versions of that. One is backing up the entire database and restoring it. In that case we'd expect both the sequence and view to get restored and to refer to eachother. Another is restoring the view after dropping it. Another still is restoring both the sequence and the view into an existing database.


pkg/sql/show_create_clauses.go, line 112 at r2 (raw file):

	decodedViewQuery, err := formatViewQueryForDisplay(ctx, semaCtx, desc)
	if err != nil {
		log.Warningf(ctx, "error converting sequence IDs to names: %+v", err)

one more, can you put the ID and the name here? If we see this we'll want to be able to figure out which view.

Copy link
Contributor Author

@the-ericwang35 the-ericwang35 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 (waiting on @ajwerner)


pkg/ccl/backupccl/restore_planning.go, line 128 at r1 (raw file):

Previously, ajwerner wrote…

I saw you added a test for the old views and making sure that that does not get upgraded. Can you also test that for these new views that when we back them up and then restore them that everything is happy -- that the references are updated correctly etc.

I think there are a few versions of that. One is backing up the entire database and restoring it. In that case we'd expect both the sequence and view to get restored and to refer to eachother. Another is restoring the view after dropping it. Another still is restoring both the sequence and the view into an existing database.

Yep that's a good idea, added TestBackupRestoreSequencesInViews in backup_test.go. There's already a test called TestBackupRestoreSequence that tests this for sequences used in default expressions, so I didn't add any new tests for those cases.


pkg/sql/show_create_clauses.go, line 112 at r2 (raw file):

Previously, ajwerner wrote…

one more, can you put the ID and the name here? If we see this we'll want to be able to figure out which view.

Yep, done.

@the-ericwang35 the-ericwang35 requested a review from a team as a code owner March 18, 2021 18:08
@the-ericwang35 the-ericwang35 removed the request for review from a team March 18, 2021 20:30
Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

:lgtm_strong:

Reviewed 1 of 20 files at r1, 1 of 14 files at r2, 3 of 10 files at r3.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @ajwerner)

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.

Nice work!

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @ajwerner and @the-ericwang35)


pkg/sql/sem/tree/walk.go, line 1212 at r3 (raw file):

		stmtCopy.Limit = &lCopy
	}
	return &stmtCopy

Need to make a copy of stmt.With - both the slice, and each element (or switch the elements to not be pointers). In general, please double-check that all slices that we're now modifying in walkStmt are getting copied.


pkg/sql/sem/tree/walk.go, line 1599 at r3 (raw file):

// called in VisitPre for every node. The visitor stops as soon as an error is
// returned.
type SimpleVisitor struct {

[nit] Why do you need to export this? It's meant to be used via SimpleVisit().

Copy link
Contributor Author

@the-ericwang35 the-ericwang35 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! 1 of 0 LGTMs obtained (waiting on @ajwerner and @RaduBerinde)


pkg/sql/sem/tree/walk.go, line 1212 at r3 (raw file):

Previously, RaduBerinde wrote…

Need to make a copy of stmt.With - both the slice, and each element (or switch the elements to not be pointers). In general, please double-check that all slices that we're now modifying in walkStmt are getting copied.

Ah sorry good catch, added code to make a copy.


pkg/sql/sem/tree/walk.go, line 1599 at r3 (raw file):

Previously, RaduBerinde wrote…

[nit] Why do you need to export this? It's meant to be used via SimpleVisit().

SimpleVisit() takes in a tree.Expr, whereas I wanted to pass in a tree.Statement to walk the statement. You're right though, there was no need to export SimpleVisitor, it makes more sense to create a function similar to SimpleVisit() but for statements, so I created a new function SimpleStmtVisit() inside of this file so that I no longer need to export SimpleVisitor.

Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 9 files at r4.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @ajwerner and @RaduBerinde)

Followup to #59864.
Previously, a change was introduced to store sequences
used in DEFAULT expressions to be referenced by their
IDs instead of their names, to allow sequence renaming.
In this patch, we want to do something similar by
rewriting sequence references in views to be
referenced by their IDs. This allows sequences
used in views to be renamed.

Release note (sql change): reference sequences used in views
by their IDs to allow these sequences to be renamed.
@the-ericwang35
Copy link
Contributor Author

TFTRs!

bors r=ajwerner

@craig
Copy link
Contributor

craig bot commented Mar 22, 2021

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Mar 22, 2021

Build succeeded:

@craig craig bot merged commit 7899744 into cockroachdb:master Mar 22, 2021
@the-ericwang35 the-ericwang35 deleted the ericwang/view_sequences branch March 22, 2021 21:30
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.

Rewrite sequence expressions in views
4 participants