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

The CTE inside of view can't determine whether it can be inlined #56582

Closed
elsa0520 opened this issue Oct 11, 2024 · 1 comment · Fixed by #56609
Closed

The CTE inside of view can't determine whether it can be inlined #56582

elsa0520 opened this issue Oct 11, 2024 · 1 comment · Fixed by #56609
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. severity/major sig/planner SIG: Planner type/bug The issue is confirmed as a bug.

Comments

@elsa0520
Copy link
Contributor

elsa0520 commented Oct 11, 2024

Bug Report

If the CTE is inside of view, optimizer can't determine whether it can be inlined by rule "The CTE is consumed only once. It can be inlined by default."

1. Minimal reproduce step (Required)

  1. create view with cte1, cte2, cte3. The consumer count of CTE1 is 2, So it could not be inlined.
create view test_multi_consumer_cte as 
with cte1 as (select a from test), 
cte2 as (select a from cte1 where a=1), 
cte3 as (select cte1.a from cte1 join cte2 where cte1.a=2) 
select * from cte3;
  1. query table
mysql> explain select * from test_multi_consumer_cte;
+------------------------------+----------+-----------+---------------+--------------------------------+
| id                           | estRows  | task      | access object | operator info                  |
+------------------------------+----------+-----------+---------------+--------------------------------+
| HashJoin_38                  | 100.00   | root      |               | CARTESIAN inner join           |
| ├─TableReader_45(Build)      | 10.00    | root      |               | data:Selection_44              |
| │ └─Selection_44             | 10.00    | cop[tikv] |               | eq(test.test.a, 1)             |
| │   └─TableFullScan_43       | 10000.00 | cop[tikv] | table:test    | keep order:false, stats:pseudo |
| └─TableReader_42(Probe)      | 10.00    | root      |               | data:Selection_41              |
|   └─Selection_41             | 10.00    | cop[tikv] |               | eq(test.test.a, 2)             |
|     └─TableFullScan_40       | 10000.00 | cop[tikv] | table:test    | keep order:false, stats:pseudo |
+------------------------------+----------+-----------+---------------+--------------------------------+
7 rows in set (0.00 sec)

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

The behavior should be seem as the query view directly

+---------------------------------+----------+-----------+---------------+--------------------------------------------+
| id                              | estRows  | task      | access object | operator info                              |
+---------------------------------+----------+-----------+---------------+--------------------------------------------+
| HashJoin_32                     | 256.00   | root      |               | CARTESIAN inner join                       |
| ├─Selection_36(Build)           | 16.00    | root      |               | eq(test.test.a, 1)                         |
| │ └─CTEFullScan_37              | 20.00    | root      | CTE:cte1      | data:CTE_0                                 |
| └─Selection_34(Probe)           | 16.00    | root      |               | eq(test.test.a, 2)                         |
|   └─CTEFullScan_35              | 20.00    | root      | CTE:cte1      | data:CTE_0                                 |
| CTE_0                           | 20.00    | root      |               | Non-Recursive CTE                          |
| └─TableReader_29(Seed Part)     | 20.00    | root      |               | data:Selection_28                          |
|   └─Selection_28                | 20.00    | cop[tikv] |               | or(eq(test.test.a, 2), eq(test.test.a, 1)) |
|     └─TableFullScan_27          | 10000.00 | cop[tikv] | table:test    | keep order:false, stats:pseudo             |
+---------------------------------+----------+-----------+---------------+--------------------------------------------+
9 rows in set (0.01 sec)

3. What did you see instead (Required)

The CTE1 is inlined which is incorrect , because it is consumed by CTE2 and CTE3.
The consumer count of CTE1 should be 2 more than 1. So it could not be inlined be default.

4. What is your TiDB version? (Required)

v8.1

@elsa0520 elsa0520 added the type/bug The issue is confirmed as a bug. label Oct 11, 2024
@elsa0520
Copy link
Contributor Author

The main reason

The preprocessor is used to compute the cte.consumercount

image

But, the query of view will skip this step which means the cte.consumercount inside of view will be always 0

image

This cause optimizer could not judge the "CTE inline by default"

@elsa0520 elsa0520 self-assigned this Oct 12, 2024
@ti-chi-bot ti-chi-bot bot closed this as completed in fa723c3 Oct 15, 2024
ti-chi-bot bot pushed a commit that referenced this issue Oct 16, 2024
…it/distinct and inside of view | tidb-test=e1d0c1e615f749e7139f5be95bc4a2b8cedb7380 (#56609) (#56666)

close #56582, close #56603
ti-chi-bot bot pushed a commit that referenced this issue Nov 7, 2024
…it/distinct and inside of view | tidb-test=pr/2422 (#56609) (#56696)

close #56582, close #56603
ti-chi-bot bot pushed a commit that referenced this issue Dec 4, 2024
…it/distinct and inside of view | tidb-test=pr/2438 (#56609) (#56695)

close #56582, close #56603
@ti-chi-bot ti-chi-bot bot added the affects-6.5 This bug affects the 6.5.x(LTS) versions. label Dec 16, 2024
@jebter jebter added 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. labels Dec 17, 2024
ti-chi-bot bot pushed a commit that referenced this issue Dec 17, 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. severity/major sig/planner SIG: Planner type/bug The issue is confirmed as a bug.
Projects
None yet
2 participants