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: make view descriptors depend on table/columns by IDs #17501

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

knz
Copy link
Contributor

@knz knz commented Aug 8, 2017

Supersedes #15388.
Contains prefix commits from #17475; will rebase when that merges.

This patch makes one more step towards addressing #10028/#10083.

For example:

> CREATE TABLE t.kv(k INT PRIMARY KEY, v INT);
> CREATE VIEW vx AS SELECT v AS x FROM t.kv;
> SHOW CREATE VIEW t.vx;
+------+-----------------------------------------------+
| View |                 CreateView                    |
+------+-----------------------------------------------+
| vx   | -- t.kv(k,v) = [71(1,2)]␤                     |
|      | CREATE VIEW vx (x) AS SELECT v AS x FROM t.kv |
+------+-----------------------------------------------+
(1 row)

i.e. the view query is unchanged but the view descriptor tracks the
mapping of names into the query to the actual IDs of the
dependencies. This information is reported in SHOW CREATE VIEW as a
SQL comment and in more details in
crdb_internal.backward_dependencies.

When the view query is loaded, this information in the view descriptor
overrides the regular name resolution path. For this we introduce a new
"lookupEnvironment" which allows view descriptors to override the name
to ID mapping for relation names, column names and index names.

knz added 5 commits August 7, 2017 11:59
If a view is defined by a query over table `d.t`, and this view's
query is inspected (using either SHOW CREATE VIEW or pg_catalog) in
the context of database `d`, ensure that the prefix `d.` is hidden.

This is useful to ease reuse of the view definition when migrating
databases.
This hidden bug cropped up while investigating different ways to
encode view descriptors.
This patch makes it possible to use the numeric table reference syntax
with a selection of columns (e.g. `[123(1,2,3)]`) when the reference
is for a view. This selects a subset of the view's definition.
… the query

Prior to this patch, a view defined with `CREATE VIEW v(x) FROM SELECT
y FROM ...` would only store the name "x" in the descriptor. This
would cause queries to pg_catalog or information_schema to hide the
fact the view renders a column named "x" (only showing "SELECT y
...").

This patch improves upon this situation by forcing an explicit rename
in the view query if the view definition changes the names.

This was the most glaring limitation of the previous approach, but in
addition to this a bug was lurking: a view created with `CREATE VIEW
v(rowid) FROM TABLE t`, which *intends* to reveal the `rowid` such
that subsequently `SELECT * FROM v` actually selects `rowid`, didn't
work as intended: when the view plan was expanded, the `rowid` column
would be picked from the underlying plan, which would make it hidden,
which would subsequently cause `SELECT * FROM v` to not pick any
column at all. The explicit rename approach taken in this patch fixes
this bug. This bug probably never affected anyone given how unlikely
the use case is, however fixing this fits the general effort of view
descriptor improvements this patch is part of.
For example:
```
> CREATE TABLE t.kv(k INT PRIMARY KEY, v INT);
> CREATE VIEW vx AS SELECT v AS x FROM t.kv;
> SHOW CREATE VIEW t.vx;
+------+-----------------------------------------------+
| View |                 CreateView                    |
+------+-----------------------------------------------+
| vx   | -- t.kv(k,v) = [71(1,2)]␤                     |
|      | CREATE VIEW vx (x) AS SELECT v AS x FROM t.kv |
+------+-----------------------------------------------+
(1 row)
```

i.e. the view query is unchanged but the view descriptor tracks the
mapping of names into the query to the actual IDs of the
dependencies. This information is reported in SHOW CREATE VIEW as a
SQL comment and in more details in
`crdb_internal.backward_dependencies`.

When the view query is loaded, this information in the view descriptor
overrides the regular name resolution path.
@knz knz requested review from a team August 8, 2017 12:33
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@knz
Copy link
Contributor Author

knz commented Aug 8, 2017

Note that even if we push the work to fix #10023/#10083 to 1.2, I think this PR should still make it to 1.1.

@knz
Copy link
Contributor Author

knz commented Aug 9, 2017

Reminder: checks that this fixes #12611

@dt
Copy link
Member

dt commented Aug 17, 2018

Is this abandoned / should we close it?

@knz
Copy link
Contributor Author

knz commented Aug 17, 2018

It's not abandoned and the idea in here is still sound.

@jordanlewis
Copy link
Member

<3 we should still do this

@jordanlewis jordanlewis removed request for a team June 19, 2019 01:27
@tbg tbg added the X-noremind Bots won't notify about PRs with X-noremind label Jun 19, 2019
@knz
Copy link
Contributor Author

knz commented Aug 4, 2020

@rohany @ajwerner if I understand well, this PR is being superseded by recent work? Can you point me to the PR number(s) so I can close this with a reference? Thanks

@rohany
Copy link
Contributor

rohany commented Aug 4, 2020

It's not being entirely superseded. The truncate PR (#52235) would allow us to do this without having to worry about remapping IDs on truncate.

@knz
Copy link
Contributor Author

knz commented Aug 6, 2020

I'm assigning this PR to the relevant SQL projects so that folk who intend to work on this can pluck utility out of it when the work is restarted.

@knz knz marked this pull request as draft August 6, 2020 09:43
@knz
Copy link
Contributor Author

knz commented Dec 28, 2022

@mgartner @postamar @chengxiong-ruan do you want to take a last look at this to see if there's anything worth salvaging, even if just ideas? Then I can close this and let it sleep in history forever.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
X-noremind Bots won't notify about PRs with X-noremind
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants