Skip to content

Commit

Permalink
planbuilder: Keep with clause when build recursive cte (#43362) (#43372)
Browse files Browse the repository at this point in the history
close #43318
  • Loading branch information
ti-chi-bot committed Apr 24, 2023
1 parent f179885 commit 7add1ad
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions planner/core/casetest/testdata/plan_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@
{
"name": "TestSingleConsumerCTE",
"cases": [
"with base1 as (WITH RECURSIVE cte(a) AS (with tmp as (select 1 as a) SELECT a from tmp UNION SELECT a+1 FROM cte) SELECT * FROM cte) select * from base1; -- issue #43318",
"with cte as (select 1) select * from cte; -- inline cte",
"with cte1 as (select 1), cte2 as (select 2) select * from cte1 union select * from cte2; -- inline cte1, cte2",
"with cte as (select 1) select * from cte union select * from cte; -- cannot be inlined",
Expand Down
12 changes: 12 additions & 0 deletions planner/core/casetest/testdata/plan_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -3750,6 +3750,18 @@
{
"Name": "TestSingleConsumerCTE",
"Cases": [
{
"SQL": "with base1 as (WITH RECURSIVE cte(a) AS (with tmp as (select 1 as a) SELECT a from tmp UNION SELECT a+1 FROM cte) SELECT * FROM cte) select * from base1; -- issue #43318",
"Plan": [
"CTEFullScan 2.00 root CTE:cte data:CTE_3",
"CTE_3 2.00 root Recursive CTE",
"├─Projection(Seed Part) 1.00 root 1->Column#15",
"│ └─TableDual 1.00 root rows:1",
"└─Projection(Recursive Part) 1.00 root cast(plus(Column#16, 1), bigint(1) BINARY)->Column#18",
" └─CTETable 1.00 root Scan on CTE_3"
],
"Warning": null
},
{
"SQL": "with cte as (select 1) select * from cte; -- inline cte",
"Plan": [
Expand Down
2 changes: 1 addition & 1 deletion planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7145,9 +7145,9 @@ func (b *PlanBuilder) buildRecursiveCTE(ctx context.Context, cte ast.ResultSetNo
// 1. Handle the WITH clause if exists.
if x.With != nil {
l := len(b.outerCTEs)
sw := x.With
defer func() {
b.outerCTEs = b.outerCTEs[:l]
sw := x.With
x.With = sw
}()
err := b.buildWith(ctx, x.With)
Expand Down

0 comments on commit 7add1ad

Please sign in to comment.