Class AdaptivePlanner

java.lang.Object
io.trino.sql.planner.AdaptivePlanner

public class AdaptivePlanner extends Object
This class is responsible for re-optimizing the plan using exchange statistics in FTE. For example, reordering of join or mitigation of skewness. It will significantly impact cost and performance if the plan chosen by the static optimiser isn’t the best due to the underestimation of statistics or lack of statistics.

Re-planning Steps: 1. It first merges all SubPlans into a single PlanNode where RemoteSourceNode for stages that haven’t finished will get replaced with Remote Exchanges. On the other hand, RemoteSourceNode for finished stages will remain as it is in the plan.

2. Once we have a merged plan which contains all the unfinished parts, we will reoptimize it using a set of PlanOptimizers.

3. During re-optimization, it is possible that some new exchanges need to be added due to the change in partitioning strategy. For instance, if a rule changes the distribution type of the join from BROADCAST to PARTITIONED. It is also possible that some remote exchanges are removed. For example, while changing the order of the join.

4. Ultimately, the planner will fragment the optimized PlanNode again and generate the SubPlans with new PlanFragmentIds. The re-fragmentation will only happen if the old plan and the new plan have some differences. To check these differences, we rely on PlanOptimizer#optimizeAndMarkPlanChanges api which also returns changed plan ids.

Note: We do not change the fragment ids which have no changes and are not downstream of the changed plan nodes. This optimization is done to avoid unnecessary stage restart due to speculative execution.