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

Code before:
				hintTask = curTask
			}
		} else {
			if normalTask.Invalid() {
				normalTask = curTask
			} else if curIsBetter, err := compareTaskCost(curTask, normalTask); err != nil {
				return nil, false, err
			} else if curIsBetter {
				normalTask = curTask
			}
		}
	}
	if !hintTask.Invalid() {
		return hintTask, true, nil
	}
	return normalTask, false, nil
}

func enumeratePhysicalPlans4TaskHelper(
	super base.LogicalPlan,
	physicalPlans []base.PhysicalPlan,
	prop *property.PhysicalProperty,
	addEnforcer bool,
) (base.Task, bool, error) {
	var normalIterTask, normalPreferTask, hintTask = base.InvalidTask, base.InvalidTask, base.InvalidTask
	initState := &enumerateState{}
	_, baseLP, childLen, iteration, iterObj := prepareIterationDownElems(super)
	childTasks := make([]base.Task, 0, childLen)
	var fd *funcdep.FDSet
	if addEnforcer && len(physicalPlans) != 0 {
		switch logicalPlan := baseLP.Self().(type) {
		case *logicalop.LogicalJoin, *logicalop.LogicalAggregation:
			// TODO(hawkingrei): FD should be maintained as logical prop instead of constructing it in physical phase
			fd = logicalPlan.ExtractFD()
		}
	}

	var err error
	for _, pp := range physicalPlans {
		childTasks, err = iteration(iterObj, pp, childTasks, prop)
		if err != nil {
			return nil, false, err
		}

		// This check makes sure that there is no invalid child task.
		if len(childTasks) != childLen {
			continue
		}

		// Combine the best child tasks with parent physical plan.
		curTask := pp.Attach2Task(childTasks...)

Review comment (suggestion):
The variable `returnedTask` is redundantly initialized here and reassigned later. It should be removed from this declaration since it's unconditionally assigned in lines 231-236. Only declare it with the final return value assignment.
```suggestion
	var normalIterTask, normalPreferTask, hintTask base.Task
```

Code after:
				hintTask = curTask
			}
		} else {
			if normalTask.Invalid() {
				normalTask = curTask
			} else if curIsBetter, err := compareTaskCost(curTask, normalTask); err != nil {
				return nil, false, err
			} else if curIsBetter {
				normalTask = curTask
			}
		}
	}
	if !hintTask.Invalid() {
		return hintTask, true, nil
	}
	return normalTask, false, nil
}

func enumeratePhysicalPlans4TaskHelper(
	super base.LogicalPlan,
	physicalPlans []base.PhysicalPlan,
	prop *property.PhysicalProperty,
	addEnforcer bool,
) (base.Task, bool, error) {
	var err error
	var normalIterTask, normalPreferTask, hintTask = base.InvalidTask, base.InvalidTask, base.InvalidTask
	initState := &enumerateState{}
	_, baseLP, childLen, iteration, iterObj := prepareIterationDownElems(super)
	childTasks := make([]base.Task, 0, childLen)

	var fd *funcdep.FDSet
	if addEnforcer && len(physicalPlans) != 0 {
		switch logicalPlan := baseLP.Self().(type) {
		case *logicalop.LogicalJoin, *logicalop.LogicalAggregation:
			// TODO(hawkingrei): FD should be maintained as logical prop instead of constructing it in physical phase
			fd = logicalPlan.ExtractFD()
		}
	}

	for _, pp := range physicalPlans {
		childTasks, err = iteration(iterObj, pp, childTasks, prop)
		if err != nil {
			return nil, false, err
		}

		// This check makes sure that there is no invalid child task.
		if len(childTasks) != childLen {
			continue
		}

		// Combine the best child tasks with parent physical plan.