Skip to content

Commit 6004c3e

Browse files
authored
planner: update the plan_clone_gen file (#57141)
ref #54057
1 parent 865213c commit 6004c3e

File tree

4 files changed

+289
-71
lines changed

4 files changed

+289
-71
lines changed

pkg/planner/core/generator/plan_cache/plan_clone_generator.go

Lines changed: 136 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,18 @@ func genPlanCloneForPlanCache(x any) ([]byte, error) {
103103
cloned.%v = *basePlan`, fieldName, fieldName)
104104
case "baseimpl.Plan", "core.baseSchemaProducer":
105105
c.write("cloned.%v = *op.%v.CloneWithNewCtx(newCtx)", f.Name, f.Name)
106-
case "[]expression.Expression", "[]*ranger.Range", "[]*util.ByItems", "[]*expression.Column", "[]model.CIStr",
107-
"[]*expression.Constant", "[]*expression.ScalarFunction", "[]property.SortItem", "[]types.Datum",
108-
"[]kv.Handle", "[]*expression.Assignment":
106+
case "[]expression.Expression", "[]*expression.Column",
107+
"[]*expression.Constant", "[]*expression.ScalarFunction":
108+
structureName := strings.Split(f.Type.String(), ".")[1] + "s"
109+
c.write("cloned.%v = clone%vForPlanCache(op.%v)", f.Name, structureName, f.Name)
110+
case "[][]*expression.Constant", "[][]expression.Expression":
111+
structureName := strings.Split(f.Type.String(), ".")[1]
112+
c.write("cloned.%v = clone%v2DForPlanCache(op.%v)", f.Name, structureName, f.Name)
113+
case "[]*ranger.Range", "[]*util.ByItems", "[]model.CIStr", "[]property.SortItem",
114+
"[]types.Datum", "[]kv.Handle", "[]*expression.Assignment":
109115
structureName := strings.Split(f.Type.String(), ".")[1] + "s"
110116
c.write("cloned.%v = util.Clone%v(op.%v)", f.Name, structureName, f.Name)
111-
case "[][]*expression.Constant", "[][]types.Datum", "[][]expression.Expression":
117+
case "[][]types.Datum":
112118
structureName := strings.Split(f.Type.String(), ".")[1]
113119
c.write("cloned.%v = util.Clone%v2D(op.%v)", f.Name, structureName, f.Name)
114120
case "planctx.PlanContext":
@@ -239,6 +245,132 @@ func clonePhysicalPlansForPlanCache(newCtx base.PlanContext, plans []base.Physic
239245
}
240246
return clonedPlans, true
241247
}
248+
249+
func cloneExpressionsForPlanCache(exprs []expression.Expression) []expression.Expression {
250+
if exprs == nil {
251+
return nil
252+
}
253+
allSafe := true
254+
for _, e := range exprs {
255+
if !e.SafeToShareAcrossSession() {
256+
allSafe = false
257+
break
258+
}
259+
}
260+
if allSafe {
261+
return exprs
262+
}
263+
cloned := make([]expression.Expression, 0, len(exprs))
264+
for _, e := range exprs {
265+
if e.SafeToShareAcrossSession() {
266+
cloned = append(cloned, e)
267+
} else {
268+
cloned = append(cloned, e.Clone())
269+
}
270+
}
271+
return cloned
272+
}
273+
274+
func cloneExpression2DForPlanCache(exprs [][]expression.Expression) [][]expression.Expression {
275+
if exprs == nil {
276+
return nil
277+
}
278+
cloned := make([][]expression.Expression, 0, len(exprs))
279+
for _, e := range exprs {
280+
cloned = append(cloned, cloneExpressionsForPlanCache(e))
281+
}
282+
return cloned
283+
}
284+
285+
func cloneScalarFunctionsForPlanCache(scalarFuncs []*expression.ScalarFunction) []*expression.ScalarFunction {
286+
if scalarFuncs == nil {
287+
return nil
288+
}
289+
allSafe := true
290+
for _, f := range scalarFuncs {
291+
if !f.SafeToShareAcrossSession() {
292+
allSafe = false
293+
break
294+
}
295+
}
296+
if allSafe {
297+
return scalarFuncs
298+
}
299+
cloned := make([]*expression.ScalarFunction, 0, len(scalarFuncs))
300+
for _, f := range scalarFuncs {
301+
if f.SafeToShareAcrossSession() {
302+
cloned = append(cloned, f)
303+
} else {
304+
cloned = append(cloned, f.Clone().(*expression.ScalarFunction))
305+
}
306+
}
307+
return cloned
308+
}
309+
310+
func cloneColumnsForPlanCache(cols []*expression.Column) []*expression.Column {
311+
if cols == nil {
312+
return nil
313+
}
314+
allSafe := true
315+
for _, c := range cols {
316+
if !c.SafeToShareAcrossSession() {
317+
allSafe = false
318+
break
319+
}
320+
}
321+
if allSafe {
322+
return cols
323+
}
324+
cloned := make([]*expression.Column, 0, len(cols))
325+
for _, c := range cols {
326+
if c == nil {
327+
cloned = append(cloned, nil)
328+
continue
329+
}
330+
if c.SafeToShareAcrossSession() {
331+
cloned = append(cloned, c)
332+
} else {
333+
cloned = append(cloned, c.Clone().(*expression.Column))
334+
}
335+
}
336+
return cloned
337+
}
338+
339+
func cloneConstantsForPlanCache(constants []*expression.Constant) []*expression.Constant {
340+
if constants == nil {
341+
return nil
342+
}
343+
allSafe := true
344+
for _, c := range constants {
345+
if !c.SafeToShareAcrossSession() {
346+
allSafe = false
347+
break
348+
}
349+
}
350+
if allSafe {
351+
return constants
352+
}
353+
cloned := make([]*expression.Constant, 0, len(constants))
354+
for _, c := range constants {
355+
if c.SafeToShareAcrossSession() {
356+
cloned = append(cloned, c)
357+
} else {
358+
cloned = append(cloned, c.Clone().(*expression.Constant))
359+
}
360+
}
361+
return cloned
362+
}
363+
364+
func cloneConstant2DForPlanCache(constants [][]*expression.Constant) [][]*expression.Constant {
365+
if constants == nil {
366+
return nil
367+
}
368+
cloned := make([][]*expression.Constant, 0, len(constants))
369+
for _, c := range constants {
370+
cloned = append(cloned, cloneConstantsForPlanCache(c))
371+
}
372+
return cloned
373+
}
242374
`
243375

244376
func main() {

pkg/planner/core/plan_cache_rebuild_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,9 @@ func testCachedPlanClone(t *testing.T, tk1, tk2 *testkit.TestKit, prep, set, exe
234234
ctx = context.WithValue(ctx, core.PlanCacheKeyTestClone{}, func(plan, cloned base.Plan) {
235235
checked = true
236236
require.NoError(t, checkUnclearPlanCacheClone(plan, cloned,
237-
".ctx",
238-
"*collate"))
237+
".ctx", ".AccessCondition", ".filterCondition", ".Conditions", ".Exprs", ".IndexConstants",
238+
"*collate", ".IdxCols", ".OutputColumns", ".EqualConditions", ".OuterHashKeys", ".InnerHashKeys",
239+
".HandleParams", ".IndexValueParams", ".Insert.Lists", ".accessCols"))
239240
})
240241
if isDML {
241242
tk2.MustExecWithContext(ctx, exec2)

0 commit comments

Comments
 (0)