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

planbuilder: Keep with clause when build recursive cte #43362

Merged
merged 1 commit into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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