Skip to content

sql: overload sequence operators to accept a regclass#59396

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
the-ericwang35:ericwang/overload_nextval
Feb 5, 2021
Merged

sql: overload sequence operators to accept a regclass#59396
craig[bot] merged 1 commit intocockroachdb:masterfrom
the-ericwang35:ericwang/overload_nextval

Conversation

@the-ericwang35
Copy link
Copy Markdown

@the-ericwang35 the-ericwang35 commented Jan 25, 2021

sql: overload sequence operators to accept a regclass

Previously, sequence operators (nextval, setval, currval)
only accepted a string to refer to the referenced sequence.
This patch allows these operators to accept a regclass as well.
Tests were also added to verify this.
This is the first step to fix this issue:
#51090.

Release note (sql change): overload sequence operators to accept a regclass

@the-ericwang35 the-ericwang35 requested review from a team and adityamaru and removed request for a team January 25, 2021 19:28
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@the-ericwang35 the-ericwang35 removed the request for review from adityamaru January 25, 2021 19:29
@the-ericwang35 the-ericwang35 marked this pull request as draft January 25, 2021 19:29
@the-ericwang35 the-ericwang35 changed the title sql: overload nextval to accept a regclass sql: overload sequence operators to accept a regclass Jan 25, 2021
@the-ericwang35 the-ericwang35 marked this pull request as ready for review January 26, 2021 00:20
@the-ericwang35 the-ericwang35 requested a review from a team January 26, 2021 00:20
Copy link
Copy Markdown
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.

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


pkg/sql/logictest/testdata/logic_test/sequences, line 62 at r1 (raw file):


query I
SELECT nextval(53::regclass)

almost definitely a bad idea to rely on this number being stable. Instead consider 'lastval_test_2'::regclass. Same goes for all of these.

Copy link
Copy Markdown
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/sql/logictest/testdata/logic_test/sequences, line 62 at r1 (raw file):

Previously, ajwerner wrote…

almost definitely a bad idea to rely on this number being stable. Instead consider 'lastval_test_2'::regclass. Same goes for all of these.

I've changed it to use let to query and store the IDs in a variable instead

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

needs a rebase but generally :lgtm:

Reviewed 1 of 7 files at r1, 1 of 2 files at r2, 2 of 2 files at r3.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @the-ericwang35)


pkg/sql/sequence.go, line 85 at r3 (raw file):

		return 0, err
	}
	fmt.Printf("ID: %v", descriptor.ID)

remove


pkg/sql/sequence.go, line 213 at r3 (raw file):

		return err
	}
	fmt.Printf("ID: %v", descriptor.ID)

remove


pkg/sql/logictest/testdata/logic_test/sequences, line 62 at r3 (raw file):


let $lastval_test_id
SELECT id FROM system.namespace WHERE name='lastval_test'

This is fine but maybe SELECT 'lastval_test'::regclass::int is a bit better because this would be ambiguous if this sequence were in another database.

Copy link
Copy Markdown
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 (and 1 stale) (waiting on @ajwerner)


pkg/sql/sequence.go, line 85 at r3 (raw file):

Previously, ajwerner wrote…

remove

Whoops sorry, done.


pkg/sql/sequence.go, line 213 at r3 (raw file):

Previously, ajwerner wrote…

remove

Done.


pkg/sql/logictest/testdata/logic_test/sequences, line 62 at r3 (raw file):

Previously, ajwerner wrote…

This is fine but maybe SELECT 'lastval_test'::regclass::int is a bit better because this would be ambiguous if this sequence were in another database.

Good point, done.

Copy link
Copy Markdown
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:

Reviewed 2 of 7 files at r1, 3 of 3 files at r4.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained

Previously, sequence operators (nextval, setval, currval)
only accepted a string to refer to the referenced sequence.
This patch allows these operators to accept a regclass as well.
Tests were also added to verify this.
This is the first step to fix this issue:
#51090.

Release note (sql change): overload sequence operators to accept a regclass
@the-ericwang35
Copy link
Copy Markdown
Author

TFTR!

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Feb 5, 2021

Build succeeded:

@craig craig bot merged commit 4be3a5f into cockroachdb:master Feb 5, 2021
@the-ericwang35 the-ericwang35 deleted the ericwang/overload_nextval branch February 5, 2021 18:17
craig bot pushed a commit that referenced this pull request Feb 18, 2021
59864: sql: encode and decode sequence regclasses, and allow sequence renaming r=the-ericwang35 a=the-ericwang35

This patch builds upon #59396,
where we overloaded sequence operators to accept a regclass.
Now that sequence operators can accept a regclass,
this patch starts using this change by storing sequences
via their `ID::regclass`, updating back references to distinguish
between references via names and IDs, and then transforming
these IDs back to their fully qualified names for printing.
This patch also starts allowing sequences that are referenced only
via ID to be renamed.
Lastly, tests were added to demonstrate all of this new functionality.

Release note (sql change): encode and decode sequence regclasses,
and allow renaming sequences

Co-authored-by: Eric Wang <ericw@cockroachlabs.com>
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.

3 participants