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: update the plan_clone_gen file #57141

Merged
merged 3 commits into from
Nov 5, 2024
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
140 changes: 136 additions & 4 deletions pkg/planner/core/generator/plan_cache/plan_clone_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,18 @@ func genPlanCloneForPlanCache(x any) ([]byte, error) {
cloned.%v = *basePlan`, fieldName, fieldName)
case "baseimpl.Plan", "core.baseSchemaProducer":
c.write("cloned.%v = *op.%v.CloneWithNewCtx(newCtx)", f.Name, f.Name)
case "[]expression.Expression", "[]*ranger.Range", "[]*util.ByItems", "[]*expression.Column", "[]model.CIStr",
"[]*expression.Constant", "[]*expression.ScalarFunction", "[]property.SortItem", "[]types.Datum",
"[]kv.Handle", "[]*expression.Assignment":
case "[]expression.Expression", "[]*expression.Column",
"[]*expression.Constant", "[]*expression.ScalarFunction":
structureName := strings.Split(f.Type.String(), ".")[1] + "s"
c.write("cloned.%v = clone%vForPlanCache(op.%v)", f.Name, structureName, f.Name)
case "[][]*expression.Constant", "[][]expression.Expression":
structureName := strings.Split(f.Type.String(), ".")[1]
c.write("cloned.%v = clone%v2DForPlanCache(op.%v)", f.Name, structureName, f.Name)
case "[]*ranger.Range", "[]*util.ByItems", "[]model.CIStr", "[]property.SortItem",
"[]types.Datum", "[]kv.Handle", "[]*expression.Assignment":
structureName := strings.Split(f.Type.String(), ".")[1] + "s"
c.write("cloned.%v = util.Clone%v(op.%v)", f.Name, structureName, f.Name)
case "[][]*expression.Constant", "[][]types.Datum", "[][]expression.Expression":
case "[][]types.Datum":
structureName := strings.Split(f.Type.String(), ".")[1]
c.write("cloned.%v = util.Clone%v2D(op.%v)", f.Name, structureName, f.Name)
case "planctx.PlanContext":
Expand Down Expand Up @@ -239,6 +245,132 @@ func clonePhysicalPlansForPlanCache(newCtx base.PlanContext, plans []base.Physic
}
return clonedPlans, true
}

func cloneExpressionsForPlanCache(exprs []expression.Expression) []expression.Expression {
if exprs == nil {
return nil
}
allSafe := true
for _, e := range exprs {
if !e.SafeToShareAcrossSession() {
allSafe = false
break
}
}
if allSafe {
return exprs
}
cloned := make([]expression.Expression, 0, len(exprs))
for _, e := range exprs {
if e.SafeToShareAcrossSession() {
cloned = append(cloned, e)
} else {
cloned = append(cloned, e.Clone())
}
}
return cloned
}

func cloneExpression2DForPlanCache(exprs [][]expression.Expression) [][]expression.Expression {
if exprs == nil {
return nil
}
cloned := make([][]expression.Expression, 0, len(exprs))
for _, e := range exprs {
cloned = append(cloned, cloneExpressionsForPlanCache(e))
}
return cloned
}

func cloneScalarFunctionsForPlanCache(scalarFuncs []*expression.ScalarFunction) []*expression.ScalarFunction {
if scalarFuncs == nil {
return nil
}
allSafe := true
for _, f := range scalarFuncs {
if !f.SafeToShareAcrossSession() {
allSafe = false
break
}
}
if allSafe {
return scalarFuncs
}
cloned := make([]*expression.ScalarFunction, 0, len(scalarFuncs))
for _, f := range scalarFuncs {
if f.SafeToShareAcrossSession() {
cloned = append(cloned, f)
} else {
cloned = append(cloned, f.Clone().(*expression.ScalarFunction))
}
}
return cloned
}

func cloneColumnsForPlanCache(cols []*expression.Column) []*expression.Column {
if cols == nil {
return nil
}
allSafe := true
for _, c := range cols {
if !c.SafeToShareAcrossSession() {
allSafe = false
break
}
}
if allSafe {
return cols
}
cloned := make([]*expression.Column, 0, len(cols))
for _, c := range cols {
if c == nil {
cloned = append(cloned, nil)
continue
}
if c.SafeToShareAcrossSession() {
cloned = append(cloned, c)
} else {
cloned = append(cloned, c.Clone().(*expression.Column))
}
}
return cloned
}

func cloneConstantsForPlanCache(constants []*expression.Constant) []*expression.Constant {
if constants == nil {
return nil
}
allSafe := true
for _, c := range constants {
if !c.SafeToShareAcrossSession() {
allSafe = false
break
}
}
if allSafe {
return constants
}
cloned := make([]*expression.Constant, 0, len(constants))
for _, c := range constants {
if c.SafeToShareAcrossSession() {
cloned = append(cloned, c)
} else {
cloned = append(cloned, c.Clone().(*expression.Constant))
}
}
return cloned
}

func cloneConstant2DForPlanCache(constants [][]*expression.Constant) [][]*expression.Constant {
if constants == nil {
return nil
}
cloned := make([][]*expression.Constant, 0, len(constants))
for _, c := range constants {
cloned = append(cloned, cloneConstantsForPlanCache(c))
}
return cloned
}
`

func main() {
Expand Down
5 changes: 3 additions & 2 deletions pkg/planner/core/plan_cache_rebuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,9 @@ func testCachedPlanClone(t *testing.T, tk1, tk2 *testkit.TestKit, prep, set, exe
ctx = context.WithValue(ctx, core.PlanCacheKeyTestClone{}, func(plan, cloned base.Plan) {
checked = true
require.NoError(t, checkUnclearPlanCacheClone(plan, cloned,
".ctx",
"*collate"))
".ctx", ".AccessCondition", ".filterCondition", ".Conditions", ".Exprs", ".IndexConstants",
"*collate", ".IdxCols", ".OutputColumns", ".EqualConditions", ".OuterHashKeys", ".InnerHashKeys",
".HandleParams", ".IndexValueParams", ".Insert.Lists", ".accessCols"))
})
if isDML {
tk2.MustExecWithContext(ctx, exec2)
Expand Down
Loading