diff --git a/planner/core/physical_plans.go b/planner/core/physical_plans.go index 537b0826eb381..5473525f5f6d9 100644 --- a/planner/core/physical_plans.go +++ b/planner/core/physical_plans.go @@ -265,9 +265,8 @@ func (p *PhysicalTableReader) Clone() (PhysicalPlan, error) { if cloned.tablePlan, err = p.tablePlan.Clone(); err != nil { return nil, err } - if cloned.TablePlans, err = clonePhysicalPlan(p.TablePlans); err != nil { - return nil, err - } + // TablePlans are actually the flattened plans in tablePlan, so can't copy them, just need to extract from tablePlan + cloned.TablePlans = flattenPushDownPlan(cloned.tablePlan) return cloned, nil } diff --git a/planner/core/planbuilder_test.go b/planner/core/planbuilder_test.go index 13494433f0fbe..01041486365ef 100644 --- a/planner/core/planbuilder_test.go +++ b/planner/core/planbuilder_test.go @@ -215,6 +215,36 @@ func TestDeepClone(t *testing.T) { require.NoError(t, checkDeepClone(sort1, sort2)) } +func TestTablePlansAndTablePlanInPhysicalTableReaderClone(t *testing.T) { + ctx := mock.NewContext() + col, cst := &expression.Column{RetType: types.NewFieldType(mysql.TypeString)}, &expression.Constant{RetType: types.NewFieldType(mysql.TypeLonglong)} + schema := expression.NewSchema(col) + tblInfo := &model.TableInfo{} + hist := &statistics.Histogram{Bounds: chunk.New(nil, 0, 0)} + + // table scan + tableScan := &PhysicalTableScan{ + AccessCondition: []expression.Expression{col, cst}, + Table: tblInfo, + Hist: hist, + } + tableScan = tableScan.Init(ctx, 0) + tableScan.SetSchema(schema) + + // table reader + tableReader := &PhysicalTableReader{ + tablePlan: tableScan, + TablePlans: []PhysicalPlan{tableScan}, + StoreType: kv.TiFlash, + } + tableReader = tableReader.Init(ctx, 0) + clonedPlan, err := tableReader.Clone() + require.NoError(t, err) + newTableReader, ok := clonedPlan.(*PhysicalTableReader) + require.True(t, ok) + require.True(t, newTableReader.tablePlan == newTableReader.TablePlans[0]) +} + func TestPhysicalPlanClone(t *testing.T) { ctx := mock.NewContext() col, cst := &expression.Column{RetType: types.NewFieldType(mysql.TypeString)}, &expression.Constant{RetType: types.NewFieldType(mysql.TypeLonglong)}