planner: the recursive cte will make UPDATE's result wrong (#49400)

close pingcap/tidb#48969
This commit is contained in:
Yiding Cui
2023-12-13 21:16:49 +08:00
committed by GitHub
parent 09091124dd
commit 421aee147d
3 changed files with 40 additions and 0 deletions

View File

@ -7731,6 +7731,10 @@ func (b *PlanBuilder) buildRecursiveCTE(ctx context.Context, cte ast.ResultSetNo
// Build seed part plan.
saveSelect := x.SelectList.Selects
x.SelectList.Selects = x.SelectList.Selects[:i]
// We're rebuilding the seed part, so we pop the result we built previously.
for _i := 0; _i < i; _i++ {
b.handleHelper.popMap()
}
p, err = b.buildSetOpr(ctx, x)
if err != nil {
return err

View File

@ -313,3 +313,22 @@ a length(a)
select *,length(a) from t2 where a like '测试';
a length(a)
测试 4
drop view if exists v1;
create view v1(id) as
with recursive cte(a) as (select 1 union select a+1 from cte where a<3)
select * from cte;
create table test2(id int,value int);
insert into test2 values(1,1),(2,2),(3,3),(4,4),(5,5);
update test2
set value=0
where test2.id in
(
select * from v1
);
select * from test2;
id value
1 0
2 0
3 0
4 4
5 5

View File

@ -169,3 +169,20 @@ explain format = brief select *,length(a) from t2 where a like '测试 %';
explain format = brief select *,length(a) from t2 where a like '测试';
select *,length(a) from t2 where a like '测试 %';
select *,length(a) from t2 where a like '测试';
# https://github.com/pingcap/tidb/issues/48969
drop view if exists v1;
create view v1(id) as
with recursive cte(a) as (select 1 union select a+1 from cte where a<3)
select * from cte;
create table test2(id int,value int);
insert into test2 values(1,1),(2,2),(3,3),(4,4),(5,5);
update test2
set value=0
where test2.id in
(
select * from v1
);
select * from test2;