Skip to content

Commit

Permalink
fix the value of isOuterMostCTE
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros committed Oct 31, 2023
1 parent 59a6777 commit b584d52
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 4 deletions.
1 change: 1 addition & 0 deletions pkg/planner/core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ go_library(
"point_get_plan.go",
"preprocess.go",
"property_cols_prune.go",
"recheck_cte.go",
"resolve_indices.go",
"rule_aggregation_elimination.go",
"rule_aggregation_push_down.go",
Expand Down
4 changes: 0 additions & 4 deletions pkg/planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4869,14 +4869,10 @@ func (b *PlanBuilder) tryBuildCTE(ctx context.Context, tn *ast.TableName, asName
LimitEnd: limitEnd,
pushDownPredicates: make([]expression.Expression, 0),
ColumnMap: make(map[string]*expression.Column),
isOuterMostCTE: true,
}
}
var p LogicalPlan
lp := LogicalCTE{cteAsName: tn.Name, cteName: tn.Name, cte: cte.cteClass, seedStat: cte.seedStat}.Init(b.ctx, b.getSelectOffset())
if b.buildingCTE {
lp.cte.isOuterMostCTE = false
}
prevSchema := cte.seedLP.Schema().Clone()
lp.SetSchema(getResultCTESchema(cte.seedLP.Schema(), b.ctx.GetSessionVars()))

Expand Down
3 changes: 3 additions & 0 deletions pkg/planner/core/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ func BuildLogicalPlanForTest(ctx context.Context, sctx sessionctx.Context, node
if err != nil {
return nil, nil, err
}
if logic, ok := p.(LogicalPlan); ok {
RecheckCTE(logic)
}
return p, p.OutputNames(), err
}

Expand Down
58 changes: 58 additions & 0 deletions pkg/planner/core/recheck_cte.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2016 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package core

import "github.com/pingcap/tidb/pkg/util/intset"

// RecheckCTE fills the IsOuterMostCTE field for CTEs.
// It's a temp solution to before we fully use the Sequence to optimize the CTEs.
// This func will find the dependent relation of each CTEs.
func RecheckCTE(p LogicalPlan) {
checked := intset.NewFastIntSet()
ctes := make(map[int]*CTEClass)
inDegreeMap := make(map[int]int)
findCTEs(p, &checked, ctes, true, inDegreeMap)
for id, cte := range ctes {
cte.isOuterMostCTE = inDegreeMap[id] == 0
}
}

func findCTEs(
p LogicalPlan,
checked *intset.FastIntSet,
ctes map[int]*CTEClass,
isRootTree bool,
inDegree map[int]int,
) {
if cteReader, ok := p.(*LogicalCTE); ok {
cte := cteReader.cte
if !isRootTree {
inDegree[cte.IDForStorage]++
}
if checked.Has(cte.IDForStorage) {
return
}
ctes[cte.IDForStorage] = cte
checked.Insert(cte.IDForStorage)
findCTEs(cte.seedPartLogicalPlan, checked, ctes, false, inDegree)
if cte.recursivePartLogicalPlan != nil {
findCTEs(cte.recursivePartLogicalPlan, checked, ctes, false, inDegree)
}
return
}
for _, child := range p.Children() {
findCTEs(child, checked, ctes, isRootTree, inDegree)
}
}
2 changes: 2 additions & 0 deletions pkg/planner/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,8 @@ func optimize(ctx context.Context, sctx sessionctx.Context, node ast.Node, is in
return p, names, 0, nil
}

core.RecheckCTE(logic)

// Handle the logical plan statement, use cascades planner if enabled.
if sessVars.GetEnableCascadesPlanner() {
finalPlan, cost, err := cascades.DefaultOptimizer.FindBestPlan(sctx, logic)
Expand Down

0 comments on commit b584d52

Please sign in to comment.