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

planner: fix CTE hang or wrong result when multiple Apply is used (#55553) #56200

Merged
Merged
Changes from 1 commit
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
Next Next commit
This is an automated cherry-pick of #55553
Signed-off-by: ti-chi-bot <[email protected]>
guo-shaoge authored and ti-chi-bot committed Sep 20, 2024

Verified

This commit was signed with the committer’s verified signature.
watson Thomas Watson
commit 5cc5b4e3aca5faab193d3daed54606a92e09aca1
64 changes: 42 additions & 22 deletions pkg/planner/core/rule_decorrelate.go
Original file line number Diff line number Diff line change
@@ -128,29 +128,33 @@ func ExtractCorrelatedCols4PhysicalPlan(p PhysicalPlan) []*expression.Correlated
// |_ Apply_3
// |_ outerSide
// |_ innerSide(cor_col_3)
<<<<<<< HEAD
func ExtractOuterApplyCorrelatedCols(p PhysicalPlan) []*expression.CorrelatedColumn {
return extractOuterApplyCorrelatedColsHelper(p, []*expression.Schema{})
}

func extractOuterApplyCorrelatedColsHelper(p PhysicalPlan, outerSchemas []*expression.Schema) []*expression.CorrelatedColumn {
=======
func ExtractOuterApplyCorrelatedCols(p base.PhysicalPlan) []*expression.CorrelatedColumn {
corCols, _ := extractOuterApplyCorrelatedColsHelper(p)
return corCols
}

func extractOuterApplyCorrelatedColsHelper(p base.PhysicalPlan) ([]*expression.CorrelatedColumn, []*expression.Schema) {
>>>>>>> d04ec9d914a (planner: fix CTE hang or wrong result when multiple Apply is used (#55553))
if p == nil {
return nil
return nil, nil
}
curCorCols := p.ExtractCorrelatedCols()
newCorCols := make([]*expression.CorrelatedColumn, 0, len(curCorCols))

// If a corresponding Apply is found inside this PhysicalPlan, ignore it.
for _, corCol := range curCorCols {
var found bool
for _, outerSchema := range outerSchemas {
if outerSchema.ColumnIndex(&corCol.Column) != -1 {
found = true
break
}
}
if !found {
newCorCols = append(newCorCols, corCol)
}
// allCorCols store all sub plan's correlated columns.
// allOuterSchemas store all child Apply's outer side schemas.
allCorCols := p.ExtractCorrelatedCols()
allOuterSchemas := []*expression.Schema{}

handler := func(child base.PhysicalPlan) {
childCorCols, childOuterSchemas := extractOuterApplyCorrelatedColsHelper(child)
allCorCols = append(allCorCols, childCorCols...)
allOuterSchemas = append(allOuterSchemas, childOuterSchemas...)
}

switch v := p.(type) {
@@ -161,19 +165,35 @@ func extractOuterApplyCorrelatedColsHelper(p PhysicalPlan, outerSchemas []*expre
} else {
outerPlan = v.Children()[0]
}
outerSchemas = append(outerSchemas, outerPlan.Schema())
newCorCols = append(newCorCols, extractOuterApplyCorrelatedColsHelper(v.Children()[0], outerSchemas)...)
newCorCols = append(newCorCols, extractOuterApplyCorrelatedColsHelper(v.Children()[1], outerSchemas)...)
allOuterSchemas = append(allOuterSchemas, outerPlan.Schema())
handler(v.Children()[0])
handler(v.Children()[1])
case *PhysicalCTE:
newCorCols = append(newCorCols, extractOuterApplyCorrelatedColsHelper(v.SeedPlan, outerSchemas)...)
newCorCols = append(newCorCols, extractOuterApplyCorrelatedColsHelper(v.RecurPlan, outerSchemas)...)
handler(v.SeedPlan)
handler(v.RecurPlan)
default:
for _, child := range p.Children() {
newCorCols = append(newCorCols, extractOuterApplyCorrelatedColsHelper(child, outerSchemas)...)
handler(child)
}
}

return newCorCols
resCorCols := make([]*expression.CorrelatedColumn, 0, len(allCorCols))

// If one correlated column is found in allOuterSchemas, it means this correlated column is corresponding to an Apply inside `p`.
// However, we only need the correlated columns that correspond to the Apply of the parent node of `p`.
for _, corCol := range allCorCols {
var found bool
for _, outerSchema := range allOuterSchemas {
if outerSchema.ColumnIndex(&corCol.Column) != -1 {
found = true
break
}
}
if !found {
resCorCols = append(resCorCols, corCol)
}
}
return resCorCols, allOuterSchemas
}

// decorrelateSolver tries to convert apply plan to join plan.
Loading