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 a view dep on sequence string arguments #50103

Merged
merged 1 commit into from
Jun 16, 2020

Conversation

RichardJCai
Copy link
Contributor

@RichardJCai RichardJCai commented Jun 11, 2020

Add a dependency on sequences that are used via string argument when creating a view.

Also fixed minor bug where column dependencies were not being added when a sequence is referred to in a currval call.

Fixes (part of) #24556 and #50033

Release note (sql change): Sequences passed as a string argument into views
are now tracked as a dependency. Example: CREATE VIEW v AS SELECT nextval('s')
will now add a dependency on sequence s.

@RichardJCai RichardJCai requested review from a team June 11, 2020 16:17
@cockroach-teamcity
Copy link
Member

This change is Reviewable

pkg/sql/opt/optbuilder/scalar.go Show resolved Hide resolved
pkg/sql/sqlbase/sequence.go Outdated Show resolved Hide resolved
pkg/sql/sqlbase/sequence.go Outdated Show resolved Hide resolved
pkg/sql/sqlbase/sequence.go Outdated Show resolved Hide resolved
pkg/sql/sqlbase/sequence.go Outdated Show resolved Hide resolved
pkg/sql/sqlbase/sequence.go Outdated Show resolved Hide resolved
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.

So.. if nextval('s') is inside a CASE and it wouldn't necessarily get executed, do we want to error out if the source doesn't exist? E.g. this works in postgres:

radu=> CREATE VIEW v AS SELECT CASE WHEN EXISTS (SELECT * FROM t WHERE x=1) THEN nextval('s') ELSE 0 END;
CREATE VIEW
radu=> SELECT * FROM v;
ERROR:  "s" is not a sequence
radu=> DELETE FROM t WHERE true;
DELETE 1
radu=> SELECT * FROM v;
 case 
------
    0
(1 row)

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

@RichardJCai
Copy link
Contributor Author

RichardJCai commented Jun 11, 2020

So.. if nextval('s') is inside a CASE and it wouldn't necessarily get executed, do we want to error out if the source doesn't exist? E.g. this works in postgres:

radu=> CREATE VIEW v AS SELECT CASE WHEN EXISTS (SELECT * FROM t WHERE x=1) THEN nextval('s') ELSE 0 END;
CREATE VIEW
radu=> SELECT * FROM v;
ERROR:  "s" is not a sequence
radu=> DELETE FROM t WHERE true;
DELETE 1
radu=> SELECT * FROM v;
 case 
------
    0
(1 row)

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

So would it be correct if we didn't error out if the source didn't exist?

This case is really strange to me, is this the only one you can think of that results in this behaviour? Where it's valid to have a call referring to a sequence where the sequence may not exist.

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 (waiting on @RichardJCai and @rohany)


pkg/sql/opt/optbuilder/scalar.go, line 512 at r1 (raw file):

Previously, rohany (Rohan Yadav) wrote…

What if resolve datasource returns an error? I think you only want to ignore it if it is a "UnknownDescriptor" error (or something like that)

The second arg is a name, not an error. Errors are returned via panics that are caught at the optbuilder entrpoint.

That said, I agree that we probably want to tolerate "UnknownDescriptor" here.. Postgres definitely allows you to create a view with a nextval call for a bogus sequence.

@RichardJCai
Copy link
Contributor Author

So.. if nextval('s') is inside a CASE and it wouldn't necessarily get executed, do we want to error out if the source doesn't exist? E.g. this works in postgres:

radu=> CREATE VIEW v AS SELECT CASE WHEN EXISTS (SELECT * FROM t WHERE x=1) THEN nextval('s') ELSE 0 END;
CREATE VIEW
radu=> SELECT * FROM v;
ERROR:  "s" is not a sequence
radu=> DELETE FROM t WHERE true;
DELETE 1
radu=> SELECT * FROM v;
 case 
------
    0
(1 row)

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

Wait what version of PG are you using @RaduBerinde , I'm using PG 12 and this does raise an error.

postgres=# create table t(x int);
CREATE TABLE
postgres=# CREATE VIEW v AS SELECT CASE WHEN EXISTS (SELECT * FROM t WHERE x=1) THEN nextval('s') ELSE 0 END;
ERROR:  relation "s" does not exist
LINE 1: ...N EXISTS (SELECT * FROM t WHERE x=1) THEN nextval('s') ELSE ...
                                                             ^
postgres=# 

Or maybe your error comes from s existing but being a table instead of a sequence?
ERROR: "s" is not a sequence

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.

Oh, wow, good catch, yeah it looks like I had a table 's' that I forgot about. I retract my comments then, erroring out seems like the correct thing to do.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @RichardJCai and @rohany)

@RichardJCai RichardJCai force-pushed the fix_sequence_deps_in_views branch 2 times, most recently from 744c380 to 884ba01 Compare June 11, 2020 22:40
@RichardJCai RichardJCai force-pushed the fix_sequence_deps_in_views branch from 884ba01 to 57666ad Compare June 11, 2020 23:34
pkg/sql/sem/tree/function_definition.go Outdated Show resolved Hide resolved
pkg/util/sequence/sequence.go Show resolved Hide resolved
pkg/util/sequence/sequence.go Show resolved Hide resolved
@RichardJCai RichardJCai force-pushed the fix_sequence_deps_in_views branch from 57666ad to 4d9c795 Compare June 12, 2020 16:34
@RichardJCai RichardJCai requested a review from rohany June 12, 2020 17:31
@RichardJCai
Copy link
Contributor Author

@RaduBerinde Do you want to take another look before I merge this?

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:

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @RaduBerinde, @RichardJCai, and @rohany)

@RichardJCai
Copy link
Contributor Author

Thanks for the reviews!

@RichardJCai
Copy link
Contributor Author

bors r+

@craig
Copy link
Contributor

craig bot commented Jun 15, 2020

Build failed (retrying...)

@craig
Copy link
Contributor

craig bot commented Jun 15, 2020

Build failed (retrying...)

@craig
Copy link
Contributor

craig bot commented Jun 15, 2020

Build failed

@RichardJCai
Copy link
Contributor Author

I think TestDistinct is flakey, although I noticed one of my comments is out of date so I'm gonna update that and retry.

…when creating a view.

Also fixed minor bug where column dependencies were not being added when a sequence is
referred to in a currval call.

Fixes cockroachdb#24556, 50033

Release note (sql change): Sequences passed as a string argument into views
are now tracked as a dependency. Example: CREATE VIEW v AS SELECT nextval('s')
will now add a dependency on sequence s.
@RichardJCai RichardJCai force-pushed the fix_sequence_deps_in_views branch from 4d9c795 to abee50f Compare June 15, 2020 23:54
@RichardJCai
Copy link
Contributor Author

bors r+

@craig
Copy link
Contributor

craig bot commented Jun 16, 2020

Build succeeded

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.

4 participants