@@ -103,12 +103,18 @@ func genPlanCloneForPlanCache(x any) ([]byte, error) {
103
103
cloned.%v = *basePlan` , fieldName , fieldName )
104
104
case "baseimpl.Plan" , "core.baseSchemaProducer" :
105
105
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" :
109
115
structureName := strings .Split (f .Type .String (), "." )[1 ] + "s"
110
116
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" :
112
118
structureName := strings .Split (f .Type .String (), "." )[1 ]
113
119
c .write ("cloned.%v = util.Clone%v2D(op.%v)" , f .Name , structureName , f .Name )
114
120
case "planctx.PlanContext" :
@@ -239,6 +245,132 @@ func clonePhysicalPlansForPlanCache(newCtx base.PlanContext, plans []base.Physic
239
245
}
240
246
return clonedPlans, true
241
247
}
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
+ }
242
374
`
243
375
244
376
func main () {
0 commit comments