sql: make view descriptors depend on table/columns by IDs#17501
Draft
knz wants to merge 5 commits intocockroachdb:masterfrom
Draft
sql: make view descriptors depend on table/columns by IDs#17501knz wants to merge 5 commits intocockroachdb:masterfrom
knz wants to merge 5 commits intocockroachdb:masterfrom
Conversation
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.
Member
This was referenced Aug 8, 2017
Contributor
Author
Contributor
Author
|
Reminder: checks that this fixes #12611 |
Contributor
|
Is this abandoned / should we close it? |
Contributor
Author
|
It's not abandoned and the idea in here is still sound. |
Member
|
<3 we should still do this |
Contributor
Author
Contributor
|
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. |
Contributor
Author
|
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. |
Contributor
Author
|
@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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Supersedes #15388.
Contains prefix commits from #17475; will rebase when that merges.
This patch makes one more step towards addressing #10028/#10083.
For example:
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.