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

"references invalid table" when querying CTE views #33965

Closed
tangenta opened this issue Apr 14, 2022 · 1 comment · Fixed by #33991
Closed

"references invalid table" when querying CTE views #33965

tangenta opened this issue Apr 14, 2022 · 1 comment · Fixed by #33991
Assignees
Labels
affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects 5.4.x versions. affects-6.0 severity/major sig/sql-infra SIG: SQL Infra type/bug The issue is confirmed as a bug.

Comments

@tangenta
Copy link
Contributor

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

-- set up 2 same tables in different db.
create database if not exists test;
create database if not exists test1;
drop table if exists test.t, test1.t;
drop view if exists test.v;
create table test.t (a int);
insert into test.t values (1);
create table test1.t (a int);
insert into test1.t values (2);

use test;
create view test.v as with tt as (select * from t) select * from tt;
use test;
select * from test.v;
use test1;
select * from test.v;

2. What did you expect to see? (Required)

mysql> use test;
Database changed
mysql> select * from test.v;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.01 sec)

mysql> use test1;
Database changed
mysql> select * from test.v;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

3. What did you see instead (Required)

mysql> use test;
Database changed
mysql> select * from test.v;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.01 sec)

mysql> use test1;
Database changed
mysql> select * from test.v;
ERROR 1356 (HY000): View 'test.v' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them

4. What is your TiDB version? (Required)

45e48b6

@tangenta tangenta added the type/bug The issue is confirmed as a bug. label Apr 14, 2022
@tangenta tangenta self-assigned this Apr 14, 2022
@tangenta tangenta added affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects 5.4.x versions. affects-6.0 labels Apr 14, 2022
@tangenta tangenta changed the title schema name in CTE should inherit the view's one "references invalid table" when querying CTE views Apr 14, 2022
@ti-chi-bot ti-chi-bot added may-affects-4.0 This bug maybe affects 4.0.x versions. may-affects-5.0 This bug maybe affects 5.0.x versions. labels Apr 14, 2022
@tangenta
Copy link
Contributor Author

This error is reported by BuildDataSourceFromView. This function converts several errors to ErrInvalidView:

selectLogicalPlan, err := b.Build(ctx, selectNode)
if err != nil {
if terror.ErrorNotEqual(err, ErrViewRecursive) &&
terror.ErrorNotEqual(err, ErrNoSuchTable) &&
terror.ErrorNotEqual(err, ErrInternal) &&
terror.ErrorNotEqual(err, ErrFieldNotInGroupBy) &&
terror.ErrorNotEqual(err, ErrMixOfGroupFuncAndFields) {
err = ErrViewInvalid.GenWithStackByArgs(dbName.O, tableInfo.Name.O)
}
return nil, err
}

The actual error is ErrUnknownColumn, which is further reported by the expression rewriter. It complains that column is not found in the output names:

func (er *expressionRewriter) toColumn(v *ast.ColumnName) {
idx, err := expression.FindFieldName(er.names, v)
if err != nil {
er.err = ErrAmbiguous.GenWithStackByArgs(v.Name, clauseMsg[fieldList])
return
}

er.err = ErrUnknownColumn.GenWithStackByArgs(v.String(), clauseMsg[er.b.curClause])

We can compare these names:

er.names = []{{
    DBName = {model.CIStr} test1
    TblName = {model.CIStr} tt
    ColName = {model.CIStr} a
}}
v = {
    Schema = {model.CIStr} test
    Table = {model.CIStr} tt
    Name = {model.CIStr} a
}

Obviously, the DBName and Schema here are incorrect because the CTE table name does not belong to any database.

The select SQL stored by view is:

WITH `tt` AS (SELECT `test`.`t`.`a` AS `a` FROM `test`.`t`) SELECT `test`.`tt`.`a` AS `a` FROM `tt`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects 5.4.x versions. affects-6.0 severity/major sig/sql-infra SIG: SQL Infra type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants