Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros committed Nov 1, 2023
1 parent 5b3a514 commit f06773f
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions pkg/planner/core/recheck_cte.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 PingCAP, Inc.
// Copyright 2023 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -14,24 +14,20 @@

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)
findCTEs(p, 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,
Expand All @@ -41,18 +37,17 @@ func findCTEs(
if !isRootTree {
inDegree[cte.IDForStorage]++
}
if checked.Has(cte.IDForStorage) {
if _, ok := ctes[cte.IDForStorage]; ok {
return
}
ctes[cte.IDForStorage] = cte
checked.Insert(cte.IDForStorage)
findCTEs(cte.seedPartLogicalPlan, checked, ctes, false, inDegree)
findCTEs(cte.seedPartLogicalPlan, ctes, false, inDegree)
if cte.recursivePartLogicalPlan != nil {
findCTEs(cte.recursivePartLogicalPlan, checked, ctes, false, inDegree)
findCTEs(cte.recursivePartLogicalPlan, ctes, false, inDegree)
}
return
}
for _, child := range p.Children() {
findCTEs(child, checked, ctes, isRootTree, inDegree)
findCTEs(child, ctes, isRootTree, inDegree)
}
}

0 comments on commit f06773f

Please sign in to comment.