ensure_sorting

Function ensure_sorting 

Source
pub fn ensure_sorting(
    requirements: PlanWithCorrespondingSort,
) -> Result<Transformed<PlanWithCorrespondingSort>>
Expand description

This function enforces sorting requirements and makes optimizations without violating these requirements whenever possible. Requires a bottom-up traversal.

Steps

  1. Analyze if there are any immediate removals of SortExecs. If so, removes them (see analyze_immediate_sort_removal).
  2. For each child of the plan, if the plan requires an input ordering:
    • Checks if ordering is satisfied with the child. If not:
      • If the child has an output ordering, removes the unnecessary SortExec.
      • Adds sort above the child plan.
    • (Plan not requires input ordering)
      • Checks if the SortExec is neutralized in the plan. If so, removes it.
  3. Check and modify window operator:
    • Checks if the plan is a window operator, and connected with a sort. If so, either tries to update the window definition or removes unnecessary SortExecs (see adjust_window_sort_removal).
  4. Check and remove possibly unnecessary SPM:
    • Checks if the plan is SPM and child 1 output partitions, if so decides this SPM is unnecessary and removes it from the plan.