Expand description
EnforceSorting optimizer rule inspects the physical plan with respect to local sorting requirements and does the following:
- Adds a
SortExecwhen a requirement is not met, - Removes an already-existing
SortExecif it is possible to prove that this sort is unnecessary
The rule can work on valid and invalid physical plans with respect to sorting requirements, but always produces a valid physical plan in this sense.
A non-realistic but easy to follow example for sort removals: Assume that we somehow get the fragment
SortExec: expr=[nullable_col@0 ASC]
SortExec: expr=[non_nullable_col@1 ASC]in the physical plan. The first sort is unnecessary since its result is overwritten
by another SortExec. Therefore, this rule removes it from the physical plan.
ModulesΒ§
- replace_
with_ order_ preserving_ variants - Optimizer rule that replaces executors that lose ordering with their order-preserving variants when it is helpful; either in terms of performance or to accommodate unbounded streams by fixing the pipeline.
- sort_
pushdown
StructsΒ§
- Enforce
Sorting - This rule inspects
SortExecβs in the given physical plan in order to remove unnecessary sorts, and optimize sort performance across the plan.
FunctionsΒ§
- adjust_
window_ πsort_ removal - Adjusts a
WindowAggExecor aBoundedWindowAggExecto determine whether it may allow removing a sort. - analyze_
immediate_ πsort_ removal - Analyzes if there are any immediate sort removals by checking the
SortExecs and their ordering requirement satisfactions with children If the sort is unnecessary, either replaces it withSortPreservingMergeExec/LimitExecor removes theSortExec. Otherwise, returns the original plan - ensure_
sorting - This function enforces sorting requirements and makes optimizations without violating these requirements whenever possible. Requires a bottom-up traversal.
- get_
sort_ πexprs - Converts an ExecutionPlan trait object to a LexOrdering reference when possible.
- parallelize_
sorts - Transform
CoalescePartitionsExec+SortExeccascades intoSortExec - remove_
bottleneck_ πin_ subplan - Removes parallelization-reducing, avoidable
CoalescePartitionsExecs from the plan innode. After the removal of suchCoalescePartitionsExecs from the plan, some of the remainingRepartitionExecs might become unnecessary. Removes suchRepartitionExecs from the plan as well. - remove_
corresponding_ πsort_ from_ sub_ plan - Removes the sort from the plan in
node. - replace_
with_ πpartial_ sort - Only interested with
SortExecs and their unbounded children. If the plan is not aSortExecor its child is not unbounded, returns the original plan. Otherwise, by checking the requirement satisfaction searches for a replacement chance. If thereβs one replaces theSortExecplan with aPartialSortExec - update_
child_ πto_ remove_ unnecessary_ sort - Updates child to remove the unnecessary sort below it.
- update_
coalesce_ πctx_ children - Discovers the linked Coalesce->Sort cascades.
- update_
sort_ πctx_ children_ data - For a given node, update the
PlanContext.dataattribute.
Type AliasesΒ§
- Plan
With Corresponding Coalesce Partitions - This object is used within the
EnforceSortingrule to track the closestCoalescePartitionsExecdescendant(s) for every child of a plan. The data attribute stores whether the plan is aCoalescePartitionsExecor is connected to aCoalescePartitionsExecvia its children. - Plan
With Corresponding Sort - This context object is used within the
EnforceSortingrule to track the closestSortExecdescendant(s) for every child of a plan. The data attribute stores whether the plan is aSortExecor is connected to aSortExecvia its children.