From accff686216c62c8a1f7bdb597141e666e3f2bde Mon Sep 17 00:00:00 2001 From: YangKeao Date: Tue, 11 Oct 2022 00:09:50 -0400 Subject: [PATCH] planner, cte: fix cte in the embeded union (#38122) close pingcap/tidb#37928 --- cmd/explaintest/r/cte.result | 16 ++++++++++++++++ cmd/explaintest/t/cte.test | 3 +++ planner/core/logical_plan_builder.go | 4 ++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cmd/explaintest/r/cte.result b/cmd/explaintest/r/cte.result index 467f47dcea960..cf427c05a181c 100644 --- a/cmd/explaintest/r/cte.result +++ b/cmd/explaintest/r/cte.result @@ -785,3 +785,19 @@ INSERT INTO `t_dnmxh` VALUES (104,571000,NULL),(104,572000,44.37),(104,573000,59 WITH cte_0 AS (select distinct ref_0.wkey as c0, ref_0.pkey as c1, ref_0.c_xhsndb as c2 from t_dnmxh as ref_0 where (1 <= ( select ref_1.pkey not in ( select ref_5.wkey as c0 from t_dnmxh as ref_5 where (ref_5.wkey < ( select ref_6.pkey as c0 from t_cqmg3b as ref_6 where 88 between 96 and 76)) ) as c0 from (t_cqmg3b as ref_1 left outer join t_dnmxh as ref_2 on (ref_1.wkey = ref_2.wkey )) where ref_0.c_xhsndb is NULL union select 33 <= 91 as c0 from t_cqmg3b as ref_8 ))), cte_1 AS (select ref_9.wkey as c0, ref_9.pkey as c1, ref_9.c_anpf_c as c2, ref_9.c_b_fp_c as c3, ref_9.c_ndccfb as c4, ref_9.c_8rswc as c5 from t_cqmg3b as ref_9) select count(1) from cte_0 as ref_10 where case when 56 < 50 then case when 100 in ( select distinct ref_11.c4 as c0 from cte_1 as ref_11 where (ref_11.c4 > ( select ref_13.pkey as c0 from t_dnmxh as ref_13 where (ref_13.wkey > ( select distinct ref_11.c1 as c0 from cte_0 as ref_14)) )) or (1 = 1)) then null else null end else '7mxv6' end not like 'ki4%vc'; count(1) 24 +with cte1 as (select 1), cte2 as (select 2) select * from cte1 union (with cte2 as (select 3) select * from cte2 union all select * from cte2) order by 1; +1 +1 +3 +explain with cte1 as (select 1), cte2 as (select 2) select * from cte1 union (with cte2 as (select 3) select * from cte2 union all select * from cte2); +id estRows task access object operator info +HashAgg_24 3.00 root group by:Column#9, funcs:firstrow(Column#9)->Column#9 +└─Union_25 3.00 root + ├─Projection_26 1.00 root 1->Column#9 + │ └─TableDual_27 1.00 root rows:1 + └─Union_29 2.00 root + ├─CTEFullScan_31 1.00 root CTE:cte2 data:CTE_2 + └─CTEFullScan_33 1.00 root CTE:cte2 data:CTE_2 +CTE_2 1.00 root Non-Recursive CTE +└─Projection_22(Seed Part) 1.00 root 3->Column#5 + └─TableDual_23 1.00 root rows:1 diff --git a/cmd/explaintest/t/cte.test b/cmd/explaintest/t/cte.test index 13edac8f6002e..c2441f288fa2a 100644 --- a/cmd/explaintest/t/cte.test +++ b/cmd/explaintest/t/cte.test @@ -333,3 +333,6 @@ CREATE TABLE `t_dnmxh` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; INSERT INTO `t_dnmxh` VALUES (104,571000,NULL),(104,572000,44.37),(104,573000,59.91),(104,574000,91.5),(104,575000,9.53),(104,576000,92.4),(104,577000,47.96),(106,585000,NULL),(106,586000,NULL),(106,587000,NULL),(106,588000,NULL),(106,589000,NULL),(108,595000,13.35),(108,596000,13.51),(108,597000,47.51),(108,598000,NULL),(113,616000,24.73),(113,617000,NULL),(113,618000,92.6),(113,619000,NULL),(113,620000,91.65),(113,621000,100.46),(113,622000,31.3),(113,623000,63.81); WITH cte_0 AS (select distinct ref_0.wkey as c0, ref_0.pkey as c1, ref_0.c_xhsndb as c2 from t_dnmxh as ref_0 where (1 <= ( select ref_1.pkey not in ( select ref_5.wkey as c0 from t_dnmxh as ref_5 where (ref_5.wkey < ( select ref_6.pkey as c0 from t_cqmg3b as ref_6 where 88 between 96 and 76)) ) as c0 from (t_cqmg3b as ref_1 left outer join t_dnmxh as ref_2 on (ref_1.wkey = ref_2.wkey )) where ref_0.c_xhsndb is NULL union select 33 <= 91 as c0 from t_cqmg3b as ref_8 ))), cte_1 AS (select ref_9.wkey as c0, ref_9.pkey as c1, ref_9.c_anpf_c as c2, ref_9.c_b_fp_c as c3, ref_9.c_ndccfb as c4, ref_9.c_8rswc as c5 from t_cqmg3b as ref_9) select count(1) from cte_0 as ref_10 where case when 56 < 50 then case when 100 in ( select distinct ref_11.c4 as c0 from cte_1 as ref_11 where (ref_11.c4 > ( select ref_13.pkey as c0 from t_dnmxh as ref_13 where (ref_13.wkey > ( select distinct ref_11.c1 as c0 from cte_0 as ref_14)) )) or (1 = 1)) then null else null end else '7mxv6' end not like 'ki4%vc'; +#case +with cte1 as (select 1), cte2 as (select 2) select * from cte1 union (with cte2 as (select 3) select * from cte2 union all select * from cte2) order by 1; +explain with cte1 as (select 1), cte2 as (select 2) select * from cte1 union (with cte2 as (select 3) select * from cte2 union all select * from cte2); diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index b288809c82ea9..203f35ebd7aca 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -1732,7 +1732,7 @@ func (b *PlanBuilder) buildIntersect(ctx context.Context, selects []ast.Node) (L leftPlan, err = b.buildSelect(ctx, x) case *ast.SetOprSelectList: afterSetOperator = x.AfterSetOperator - leftPlan, err = b.buildSetOpr(ctx, &ast.SetOprStmt{SelectList: x}) + leftPlan, err = b.buildSetOpr(ctx, &ast.SetOprStmt{SelectList: x, With: x.With}) } if err != nil { return nil, nil, err @@ -1756,7 +1756,7 @@ func (b *PlanBuilder) buildIntersect(ctx context.Context, selects []ast.Node) (L // TODO: support intersect all return nil, nil, errors.Errorf("TiDB do not support intersect all") } - rightPlan, err = b.buildSetOpr(ctx, &ast.SetOprStmt{SelectList: x}) + rightPlan, err = b.buildSetOpr(ctx, &ast.SetOprStmt{SelectList: x, With: x.With}) } if err != nil { return nil, nil, err