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

Embedded CTE in view will be prefixed with DB name #54582

Closed
MimeLyc opened this issue Jul 11, 2024 · 4 comments · Fixed by #57253
Closed

Embedded CTE in view will be prefixed with DB name #54582

MimeLyc opened this issue Jul 11, 2024 · 4 comments · Fixed by #57253
Assignees
Labels
affects-6.5 This bug affects the 6.5.x(LTS) versions. affects-7.1 This bug affects the 7.1.x(LTS) versions. affects-7.5 This bug affects the 7.5.x(LTS) versions. affects-8.1 This bug affects the 8.1.x(LTS) versions. affects-8.5 This bug affects the 8.5.x(LTS) versions. severity/major sig/planner SIG: Planner type/bug The issue is confirmed as a bug.

Comments

@MimeLyc
Copy link

MimeLyc commented Jul 11, 2024

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

Create users and grant some permissions.(root user)

CREATE USER 'db_a'@'%';
CREATE USER 'db_b'@'%';

GRANT ALL PRIVILEGES ON `db_a`.* TO 'db_a'@'%'
GRANT ALL PRIVILEGES ON `db_b`.* TO 'db_a'@'%'
GRANT ALL PRIVILEGES ON `db_b`.* TO 'db_b'@'%'
GRANT ALL PRIVILEGES ON `db_b`.* TO 'db_b'@'%'

 SHOW GRANTS FOR 'db_a'@'%';
+------------------------------------------------+
| Grants for db_a@%                              |
+------------------------------------------------+
| GRANT USAGE ON *.* TO 'db_a'@'%'               |
| GRANT ALL PRIVILEGES ON `db_a`.* TO 'db_a'@'%' |
| GRANT ALL PRIVILEGES ON `db_b`.* TO 'db_a'@'%' |
+------------------------------------------------+

 SHOW GRANTS FOR 'db_b'@'%';
+------------------------------------------------+
| Grants for db_b@%                              |
+------------------------------------------------+
| GRANT USAGE ON *.* TO 'db_b'@'%'               |
| GRANT ALL PRIVILEGES ON `db_a`.* TO 'db_b'@'%' |
| GRANT ALL PRIVILEGES ON `db_b`.* TO 'db_b'@'%' |
+------------------------------------------------+

Create views.(root user)

create database db_a;
create database db_b;

use db_a;
 CREATE TABLE `tmp_table1` (                                 
   `id` decimal(18,0) NOT NULL,                              
   `row_1` varchar(255) DEFAULT NULL,                        
   PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */     
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin

 create  ALGORITHM=UNDEFINED DEFINER=`db_a`@`%` SQL SECURITY DEFINER VIEW view_test_v1 as (
                         with rs1 as(
                            select otn.*
                             from tmp_table1 otn
                          )
                        select ojt.* from rs1 ojt
                        )

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

use db_b;
select * from db_a.view_test_v1;

-- Get result

3. What did you see instead (Required)

show create table db_a.view_test_v1
-- CREATE ALGORITHM=UNDEFINED DEFINER=`db_a`@`%` SQL SECURITY DEFINER VIEW `view_test_v1` (`row_1`) AS (WITH `rs1` AS (SELECT `db_a`.`otn`.`id` AS `id`,`db_a`.`otn`.`row_1` AS `row_1` FROM `db_a`.`tmp_table1` AS `otn`), `rs2` AS (SELECT `oon`.`row_1` AS `row_1` FROM `db_a`.`tmp_table1` AS `oon` LEFT JOIN `rs1` AS `tfn` ON `oon`.`row_1`=`tfn`.`row_1`) SELECT `db_a`.`ojt`.`row_1` AS `row_1` FROM `rs2` AS `ojt`) 

...And if I query this view from db_a, the following error occurs:

use db_b;
select * from db_a.view_test_v1;

(1356, "View 'db_a.view_test_v1' 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)

+-----------------------------------------------------------+
| TIDB_VERSION() |
+-----------------------------------------------------------+
| Release Version: v8.1.0 |
| Edition: Community |
| Git Commit Hash: 945d07c |
| Git Branch: HEAD |
| UTC Build Time: 2024-05-21 03:51:57 |
| GoVersion: go1.21.10 |
| Race Enabled: false |
| Check Table Before Drop: false |
| Store: tikv |
+-----------------------------------------------------------+

@MimeLyc MimeLyc added the type/bug The issue is confirmed as a bug. label Jul 11, 2024
@hawkingrei hawkingrei self-assigned this Jul 11, 2024
@hawkingrei hawkingrei added the sig/planner SIG: Planner label Jul 11, 2024
@mayjiang0203
Copy link

/severity major

@ti-chi-bot ti-chi-bot added affects-8.1 This bug affects the 8.1.x(LTS) versions. affects-7.5 This bug affects the 7.5.x(LTS) versions. affects-7.1 This bug affects the 7.1.x(LTS) versions. affects-6.5 This bug affects the 6.5.x(LTS) versions. and removed may-affects-8.1 may-affects-7.5 may-affects-7.1 may-affects-6.5 labels Jul 11, 2024
@MimeLyc
Copy link
Author

MimeLyc commented Jul 12, 2024

Seems like this bug will only happen when there is any table aliase.

@winoros winoros removed may-affects-5.4 This bug maybe affects 5.4.x versions. may-affects-6.1 affects-6.5 This bug affects the 6.5.x(LTS) versions. affects-7.1 This bug affects the 7.1.x(LTS) versions. affects-7.5 This bug affects the 7.5.x(LTS) versions. labels Jul 15, 2024
@ti-chi-bot ti-chi-bot bot added the affects-8.5 This bug affects the 8.5.x(LTS) versions. label Nov 1, 2024
@harry1129
Copy link

This can be reproduced in TiDB v6.5.11.

@harry1129
Copy link

The problem will cause the query to information_schema.columns to return no results and produce a warning message.

mysql> use test;
Database changed
mysql> select * from test.view_test_v1;
Empty set (0.00 sec)

mysql> use test1;
Database changed
mysql> select * from test.view_test_v1;
mysql> select * from information_schema.columns where table_name='view_test_v1';
Empty set, 1 warning (0.01 sec)

mysql> show warnings;
+---------+------+-------------------------------------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                                                             |
+---------+------+-------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 1356 | View 'test.view_test_v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them |
+---------+------+-------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select tidb_version()\G
*************************** 1. row ***************************
tidb_version(): Release Version: v6.5.11
Edition: Community
Git Commit Hash: 3f2073261bf9f454c3c677d76d120c6ab40ca05c
Git Branch: HEAD
UTC Build Time: 2024-09-18 04:07:03
GoVersion: go1.19.13
Race Enabled: false
TiKV Min Version: 6.2.0-alpha
Check Table Before Drop: false
Store: tikv
1 row in set (0.00 sec)

@hawkingrei hawkingrei added the affects-7.5 This bug affects the 7.5.x(LTS) versions. label Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-6.5 This bug affects the 6.5.x(LTS) versions. affects-7.1 This bug affects the 7.1.x(LTS) versions. affects-7.5 This bug affects the 7.5.x(LTS) versions. affects-8.1 This bug affects the 8.1.x(LTS) versions. affects-8.5 This bug affects the 8.5.x(LTS) versions. severity/major sig/planner SIG: Planner type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants