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

proposal: add more detail view implement proposal #8416

Merged
merged 21 commits into from
Dec 3, 2018

Conversation

AndrewDi
Copy link
Contributor

@AndrewDi AndrewDi commented Nov 22, 2018

What problem does this PR solve?

Proposal view implement.

What is changed and how it works?

After discuss about the view implement detail, add more details to document.

Check List

Tests

  • No code

Side effects
Increased code complexity

Related changes
Need to update the documentation


This change is Reviewable

@sre-bot
Copy link
Contributor

sre-bot commented Nov 22, 2018

Hi contributor, thanks for your PR.

This patch needs to be approved by someone of admins. They should reply with "/ok-to-test" to accept this PR for running test automatically.

@AndrewDi
Copy link
Contributor Author

@XuHuaiyu PTAL

@shenli shenli added component/docs contribution This PR is from a community contributor. labels Nov 22, 2018
## SubTask Schedule
|Action |Priority|Deadline|Notes|
| ------ | ------ | ------ |-----|
|`CREATE [OR REPLACE] VIEW view_name [(column_list)] AS select_statement`|P1|2019/01/15|后续的所有工作都依赖这项|
Copy link
Contributor

Choose a reason for hiding this comment

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

Better use English here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@zhexuany
Copy link
Contributor

/run-common-test tidb-test=pr/658

@AndrewDi
Copy link
Contributor Author

/run-common-test tidb-test=pr/658

@zhexuany I didn't do any code change, why test fail...

@zhexuany
Copy link
Contributor

@AndrewDi Please ignored the error. I am adding the new test case in our internal test framework.

@zhexuany
Copy link
Contributor

/run-common-test tidb-test=pr/658
/run-integration-common-test tidb-test=pr/658

@XuHuaiyu XuHuaiyu self-assigned this Nov 23, 2018
@zhexuany
Copy link
Contributor

/run-common-test
/run-integration-common-test

@zhexuany
Copy link
Contributor

/run-common-test tidb-test=pr/668
/run-integration-common-test tidb-test=pr/668

docs/design/2018-10-24-view-support.md Outdated Show resolved Hide resolved
Security SecurityType `json:"view_security""`
SelectStmt string `json:"view_select"`
CheckOption CheckOption `json:"view_checkoption"`
Cols []string `json:"view_cols"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need this?
Seems duplicate with TableInfo.ColumnName.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use TableInfo.ColumnName to store origin column names, and use View.Cols to store column alias name

Copy link
Contributor

Choose a reason for hiding this comment

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

s/Cols []string/ Cols []model.CIStr

* IsUpdatable ref https://dev.mysql.com/doc/refman/5.7/en/view-updatability.html
This parameter mark if this view is updatable.
* SelectStmt
This string is the select sql statement after sql rewriter.
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. We only store the origin sql of create_view now.
  2. Add an example here to show the expected format.

docs/design/2018-10-24-view-support.md Outdated Show resolved Hide resolved
docs/design/2018-10-24-view-support.md Outdated Show resolved Hide resolved
docs/design/2018-10-24-view-support.md Outdated Show resolved Hide resolved
|Add some test cases for CreateView and Select … From View(port from MySQL test)|P1|2019/03/30|--|
|UPDATE VIEW|P2| |Difficult|
|INSERT VIEW|P2| |Difficult, dependent on UPDATE VIEW)|
|SHOW CREATE [VIEW | TABLE]|P2| | |
Copy link
Contributor

Choose a reason for hiding this comment

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

We should add a task like support CREATE_VIEW_PRIV check with priority P2.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Cols []string `json:"view_cols"`
}
```
* AlgorithmType ref https://dev.mysql.com/doc/refman/5.7/en/view-algorithms.html
Copy link
Contributor

Choose a reason for hiding this comment

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

```
* AlgorithmType ref https://dev.mysql.com/doc/refman/5.7/en/view-algorithms.html
The view SQL AlGORITHM characteristic. The value is one of UNDEFINED、MERGE OR TEMPTABLE, if no ALGORITHM clause is present, UNDEFINED is the default algorithm.
We will implement Algorithm=UNDEFINED only now.
Copy link
Contributor

Choose a reason for hiding this comment

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

We will implement MERGE not UNDEFINED, and regard UNDEFINED as MERGE now.
As the MySQL manual shows:

If no ALGORITHM clause is present, UNDEFINED is the default algorithm prior to MySQL 5.7.6. As of 5.7.6, the default algorithm is determined by the value of the derived_merge flag of the optimizer_switch system variable. For additional discussion, see Section 8.2.2.3, “Optimizing Derived Tables and View References”.

* SelectStmt
This string is the origin select sql statement.
* Cols
This string array is the view's column alias names.
Copy link
Contributor

Choose a reason for hiding this comment

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

Not string array.

```
1. Parse the create view statement and build a logical plan for select cause part. If any grammar error occurs, return errors to parser.
2. Examine view definer's privileges. Definer should own both `CREATE_VIEW` and base table's `SELECT` privileges.
3. Examine create view statement, If ViewFieldList cause part is empty, then we should generate view column names from SelectStmt cause. Otherwise check len(ViewFieldList) == len(Columns from SelectStmt). And then we save column names to `TableInfo.Columns` .
2. Drop a view
Copy link
Contributor

Choose a reason for hiding this comment

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

s/ Drop a view/ DROP VIEW

2. Drop a view
Implement DROP VIEW grammar, and delete the existing view tableinfo object.
Implement `DROP VIEW` grammar, and delete the existing view tableinfo object. This function should reuse `DROP TABLE` code logical
3. Select from a view
Copy link
Contributor

Choose a reason for hiding this comment

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

s/ Select from a view/ SELECT FROM VIEW

select * from t;
```
Once we query from view `v`, database will rewrite view's `SelectStmt` from `select * from t` into **`select a as a,b as b from t`**

Copy link
Contributor

Choose a reason for hiding this comment

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

Add a line here:
But, if we alter the schema of t like this:

drop table t;
create table t(c int,d int);
```
If we rebuild table `v` from sql above and query from view `v` again, database will rewrite view's `SelectStmt` from `select * from t` into **`select c as c,d as d from t`**
Copy link
Contributor

Choose a reason for hiding this comment

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

change line 92~93 to:
Executing select * from v now is equivalent to select c as c, d as d from t.The result of this query will be incompatible with the character of VIEW that a VIEW should be "frozen" after defined.

@AndrewDi
Copy link
Contributor Author

AndrewDi commented Dec 3, 2018

@AndrewDi Don't forget to update the LAST UPDATED DATE.

Thanks for your reminder...

In order to solve the problem described above, we must build a `Projection` at the top of original select's `LogicalPlan`, just like we rewrite view's `SelectStmt` from `select * from t` into **`select a as a,b as b from (select * from t)`**.
This is a temporary fix and we will implement TiDB to rewrite SQL with replacing all wildcard finally.
4. Show table status
Modify `SHOW TABLE STATUS` function to support show view status, and we use this command to check if `CREATE VIEW` and `DROP VIEW` operation is successful. To reuse `SHOW TABLE STATUS` code logical is preferred.
Copy link
Contributor

Choose a reason for hiding this comment

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

s/ code logical/ code logic?

@XuHuaiyu
Copy link
Contributor

XuHuaiyu commented Dec 3, 2018

PTAL @zz-jason @lamxTyler

@XuHuaiyu XuHuaiyu mentioned this pull request Dec 3, 2018
@XuHuaiyu
Copy link
Contributor

XuHuaiyu commented Dec 3, 2018

The effect of SQL_MODE should also be taken into consideration. We need to list it as a P2 or P3 priority task.
#8001 (comment)

@AndrewDi
Copy link
Contributor Author

AndrewDi commented Dec 3, 2018

The effect of SQL_MODE should also be taken into consideration. We need to list it as a P2 or P3 priority task.
#8001 (comment)

I think P3 would be nice

XuHuaiyu
XuHuaiyu previously approved these changes Dec 3, 2018
Copy link
Contributor

@XuHuaiyu XuHuaiyu 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: 0 of 1 files reviewed, 12 unresolved discussions (waiting on @lamxTyler, @XuHuaiyu, and @zz-jason)

Copy link
Contributor

@alivxxx alivxxx left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r16.
Reviewable status: all files reviewed, 11 unresolved discussions (waiting on @XuHuaiyu, @zz-jason, and @lamxTyler)

```
* [Algorithm](https://dev.mysql.com/doc/refman/5.7/en/view-algorithms.html)
The view SQL `AlGORITHM` characteristic. The value should be one of `UNDEFINED`, `MERGE` OR `TEMPTABLE`. If no `ALGORITHM` clause is present, `UNDEFINED` is the default algorithm.
Currently, we will only implement Algorithm=MERGE algorithm.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Currently, we will only implement Algorithm=MERGE algorithm.
Currently, we will only implement the `MERGE` algorithm.

* [Security](https://dev.mysql.com/doc/refman/5.7/en/create-view.html)
The view SQL `SECURITY` characteristic. The value is one of `DEFINER` or `INVOKER`.
* [CheckOption](https://dev.mysql.com/doc/refman/5.7/en/view-check-option.html)
The `WITH CHECK OPTION` clause can be given to an updatable view to prevent inserts to rows for which the WHERE clause in the select_statement is not true. It also prevents updates to rows for which the WHERE clause is true but the update would cause it to be not true (in other words, it prevents visible rows from being updated to nonvisible rows).
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
The `WITH CHECK OPTION` clause can be given to an updatable view to prevent inserts to rows for which the WHERE clause in the select_statement is not true. It also prevents updates to rows for which the WHERE clause is true but the update would cause it to be not true (in other words, it prevents visible rows from being updated to nonvisible rows).
The `WITH CHECK OPTION` clause can be given to an updatable view to prevent inserts to rows for which the `WHERE` clause in the select statement is not true. It also prevents updating rows for which the `WHERE` clause is true but the update operation would cause it to be false. In other words, it prevents visible rows from being updated to nonvisible rows.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"updates to rows" would be more precisely.

The view SQL `SECURITY` characteristic. The value is one of `DEFINER` or `INVOKER`.
* [CheckOption](https://dev.mysql.com/doc/refman/5.7/en/view-check-option.html)
The `WITH CHECK OPTION` clause can be given to an updatable view to prevent inserts to rows for which the WHERE clause in the select_statement is not true. It also prevents updates to rows for which the WHERE clause is true but the update would cause it to be not true (in other words, it prevents visible rows from being updated to nonvisible rows).
In a `WITH CHECK OPTION` clause for an updatable view, the `LOCAL` and `CASCADED` keywords determine the scope of check testing when the view is defined in terms of another view. When neither keyword is given, the default is CASCADED.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
In a `WITH CHECK OPTION` clause for an updatable view, the `LOCAL` and `CASCADED` keywords determine the scope of check testing when the view is defined in terms of another view. When neither keyword is given, the default is CASCADED.
In a `WITH CHECK OPTION` clause for an updatable view, the `LOCAL` and `CASCADED` keywords determine the scope of check testing when the view is defined in terms of another view. When neither keyword is given, the default is `CASCADED`.

* SelectStmt
This string is the origin `SELECT` SQL statement.
* Cols
This model.CIStr array stores view's column alias names.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
This model.CIStr array stores view's column alias names.
This `model.CIStr` array stores view's column alias names.

```
1. Parse the create view statement and build a logical plan for select clause part. If any grammar error occurs, return errors to parser.
2. Examine view definer's privileges. Definer should own both `CREATE_VIEW_PRIV` privilege and base table's `SELECT` privilege. We will reuse `CREATE_PRIV` privilege when implement `CREATE VIEW` feature and will support `CREATE_VIEW_PRIV` check later.
3. Examine create view statement. If the ViewFieldList clause part is empty, then we should generate view column names from SelectStmt clause. Otherwise check len(ViewFieldList) == len(Columns from SelectStmt). And then we save column names to `TableInfo.Columns`.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
3. Examine create view statement. If the ViewFieldList clause part is empty, then we should generate view column names from SelectStmt clause. Otherwise check len(ViewFieldList) == len(Columns from SelectStmt). And then we save column names to `TableInfo.Columns`.
3. Examine create view statement. If the `ViewFieldList` clause part is empty, then we should generate view column names from the `SelectStmt` clause. Otherwise check `len(ViewFieldList) == len(Columns from SelectStmt)`. And then we save column names to `TableInfo.Columns`.


## Compatibility
It makes TiDB more compatible with MySQL.
Add TiDB support for the basic VIEW feature without affecting other existing functions, and make TiDB more compatible with MySQL.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Add TiDB support for the basic VIEW feature without affecting other existing functions, and make TiDB more compatible with MySQL.
Support the basic VIEW functionality without affecting other existing functionalities in TiDB. And make TiDB be more compatible with MySQL.

We convert `expression.columns` into `table.Column` and reuse function `buildTableInfo` to build view
4. logical_plan_builder.go
Every time we make a `SELECT` statement within a view, modify the `buildDataSource` function to rewrite the view table to select `LogicalPlan`.
|Action |Priority|Deadline|Notes|
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
|Action |Priority|Deadline|Notes|
Here is the work items list:
|Action |Priority|Deadline|Notes|

Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r16.
Reviewable status: all files reviewed, 16 unresolved discussions (waiting on @zz-jason and @lamxTyler)

@zz-jason
Copy link
Member

zz-jason commented Dec 3, 2018

Another thing is:

s/```mysql/```sql/

@AndrewDi

@AndrewDi
Copy link
Contributor Author

AndrewDi commented Dec 3, 2018

@zz-jason PTAL again

Copy link
Member

@zz-jason zz-jason 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 1 of 1 files at r17.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @lamxTyler)

Copy link
Contributor

@alivxxx alivxxx left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r17.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @lamxTyler)

Copy link
Contributor

@alivxxx alivxxx 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! all files reviewed, all discussions resolved

@XuHuaiyu XuHuaiyu merged commit 04682ce into pingcap:master Dec 3, 2018
@AndrewDi AndrewDi deleted the view/proposal_discuss branch December 3, 2018 12:23
iamzhoug37 pushed a commit to iamzhoug37/tidb that referenced this pull request Dec 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/docs contribution This PR is from a community contributor.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants