Repository: pingcap/tidb
File: pkg/planner/core/pb_to_plan.go (Go)

Code before:

func (b *PBPlanBuilder) predicatePushDown(physicalPlan base.PhysicalPlan, predicates []expression.Expression) ([]expression.Expression, base.PhysicalPlan) {
	if physicalPlan == nil {
		return predicates, physicalPlan
	}
	switch plan := physicalPlan.(type) {
	case *physicalop.PhysicalMemTable:
		memTable := plan
		if memTable.Extractor == nil {
			return predicates, plan
		}
		names := make([]*types.FieldName, 0, len(memTable.Columns))
		for _, col := range memTable.Columns {
			names = append(names, &types.FieldName{
				TblName:     memTable.Table.Name,
				ColName:     col.Name,
				OrigTblName: memTable.Table.Name,
				OrigColName: col.Name,
			})
		}
		// Set the expression column unique ID.
		// Since the expression is build from PB, It has not set the expression column ID yet.
		schemaCols := memTable.Schema().Columns
		cols := expression.ExtractColumnsMapFromExpressions(nil, predicates...)
		for _, col := range cols {
			col.UniqueID = schemaCols[col.Index].UniqueID
		}
		predicates = memTable.Extractor.Extract(b.sctx, memTable.Schema(), names, predicates)
		return predicates, memTable
	case *physicalop.PhysicalSelection:
		selection := plan
		conditions, child := b.predicatePushDown(plan.Children()[0], selection.Conditions)
		if len(conditions) > 0 {
			selection.Conditions = conditions
			selection.SetChildren(child)
			return predicates, selection
		}
		return predicates, child
	default:
		if children := plan.Children(); len(children) > 0 {
			_, child := b.predicatePushDown(children[0], nil)
			plan.SetChildren(child)
		}
		return predicates, plan
	}
}


Review comment (refactor):
It makes no sense to me `col.UniqueID` to be updated, if the original `col.UniqueID` is not reliable, then we should not use  `expression.ExtractColumnsMapFromExpressions` since it use `col.UniqueID` to deduplicate the columns

Code after:

func (b *PBPlanBuilder) predicatePushDown(physicalPlan base.PhysicalPlan, predicates []expression.Expression) ([]expression.Expression, base.PhysicalPlan) {
	if physicalPlan == nil {
		return predicates, physicalPlan
	}
	switch plan := physicalPlan.(type) {
	case *physicalop.PhysicalMemTable:
		memTable := plan
		if memTable.Extractor == nil {
			return predicates, plan
		}
		names := make([]*types.FieldName, 0, len(memTable.Columns))
		for _, col := range memTable.Columns {
			names = append(names, &types.FieldName{
				TblName:     memTable.Table.Name,
				ColName:     col.Name,
				OrigTblName: memTable.Table.Name,
				OrigColName: col.Name,
			})
		}
		// Set the expression column unique ID.
		// Since the expression is build from PB, It has not set the expression column ID yet.
		schemaCols := memTable.Schema().Columns
		cols := expression.ExtractAllColumnsFromExpressions(predicates, nil)
		for _, col := range cols {
			col.UniqueID = schemaCols[col.Index].UniqueID
		}
		predicates = memTable.Extractor.Extract(b.sctx, memTable.Schema(), names, predicates)
		return predicates, memTable
	case *physicalop.PhysicalSelection:
		selection := plan
		conditions, child := b.predicatePushDown(plan.Children()[0], selection.Conditions)
		if len(conditions) > 0 {
			selection.Conditions = conditions
			selection.SetChildren(child)
			return predicates, selection
		}
		return predicates, child
	default:
		if children := plan.Children(); len(children) > 0 {
			_, child := b.predicatePushDown(children[0], nil)
			plan.SetChildren(child)
		}
		return predicates, plan
	}
}