Skip to content

sql: un-break view descriptors by being more conservative about dependencies#17280

Merged
knz merged 1 commit intocockroachdb:masterfrom
knz:20170728-fix-view-dep
Jul 28, 2017
Merged

sql: un-break view descriptors by being more conservative about dependencies#17280
knz merged 1 commit intocockroachdb:masterfrom
knz:20170728-fix-view-dep

Conversation

@knz
Copy link
Copy Markdown
Contributor

@knz knz commented Jul 28, 2017

Prior to this patch, a view defined with

CREATE VIEW vx AS SELECT k FROM (SELECT k, v FROM kv)

would only establish a dependency on column k of kv. Subsequently,
ALTER TABLE kv DROP COLUMN v would be allowed without error and the
view would become broken.

There is a spectrum of solutions, see the discussion on #17269.

This patch implements the simplest solution that still allows schema
changes on depended-on tables: when a view is created, it now
establishes a dependency on all the columns that existed at the time
the view is created
. New columns can be added without error; a
subsequent patch can also address #10083 (renaming columns depended on
by views) which is an orthogonal issue.

@knz knz requested review from RaduBerinde and jordanlewis July 28, 2017 15:51
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@knz knz changed the title sql: un-break view descriptors by being more conservative about depen… sql: un-break view descriptors by being more conservative about dependencies Jul 28, 2017
@knz
Copy link
Copy Markdown
Contributor Author

knz commented Jul 28, 2017

  1. Do you think I should also implement a migration to fix all the existing view descriptors in a previously created cluster?

  2. is this a candidate for a 1.0 patch too?

@jordanlewis
Copy link
Copy Markdown
Member

:lgtm:

I don't know about the migration. It does seem like that's the right thing to do but I'm unsure of the costs of adding such migrations. I wonder if @bdarnell has thoughts there.


Review status: 0 of 4 files reviewed at latest revision, 1 unresolved discussion, all commit checks successful.


pkg/sql/logictest/testdata/logic_test/alter_table, line 330 at r1 (raw file):

# ALTER TABLE t DROP COLUMN d
statement ok
ALTER TABLE t DROP COLUMN d CASCADE

Please add a test that demonstrates our blunt handling of this issue to make it crystal clear what's going on. Like, statement ok CREATE TABLE t (a int, b int); statement ok CREATE VIEW v as SELECT a FROM t; statement error ALTER TABLE t DROP COLUMN b


Comments from Reviewable

@knz knz force-pushed the 20170728-fix-view-dep branch from 495e093 to 3b7c810 Compare July 28, 2017 16:23
@knz
Copy link
Copy Markdown
Contributor Author

knz commented Jul 28, 2017

Done, also added a migration.

…dencies

Prior to this patch, a view defined with

```sql
CREATE VIEW vx AS SELECT k FROM (SELECT k, v FROM kv)
```

would only establish a dependency on column `k` of `kv`. Subsequently,
`ALTER TABLE kv DROP COLUMN v` would be allowed without error and the
view would become broken.

There is a spectrum of solutions, see the discussion on cockroachdb#17269.

This patch implements the simplest solution that still allows schema
changes on depended-on tables: when a view is created, it now
establishes a dependency on *all the columns that existed at the time
the view is created*. New columns can be added without error; a
subsequent patch can also address cockroachdb#10083 (renaming columns depended on
by views) which is an orthogonal issue.
@knz
Copy link
Copy Markdown
Contributor Author

knz commented Jul 28, 2017

I am merging this to enable progress on #15388. I feel the migration is benign since it does not change descriptor semantics -- whether it applies or not views will work the same, it's only a protection against further alter table drop column.

@knz knz merged commit 143e793 into cockroachdb:master Jul 28, 2017
@knz knz deleted the 20170728-fix-view-dep branch July 28, 2017 18:20
@a-robinson
Copy link
Copy Markdown
Contributor

Many thanks @knz! I'm sorry for not considering cases like this.


// repopulateViewDeps ensures that each view V that depends on a table
// T depends on all columns in T. (#17269)
func repopulateViewDeps(ctx context.Context, r runner) error {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is complicated enough that it really merits a test if possible, though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can't figure a way to create an invalid view descriptor in package migration without exporting a bunch of stuff from sql and sqlbase. What would you suggest?

@RaduBerinde
Copy link
Copy Markdown
Member

LGTM

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.

5 participants