feat(asap-aware-mapping): select CSE using effective cross-group costs - #272
Merged
Merged
Conversation
zzylol
force-pushed
the
feat/whole-plan-cost-selection-271
branch
from
August 25, 2026 22:50
3c10e84 to
a42d3d0
Compare
zzylol
force-pushed
the
feat/whole-plan-cost-selection-271
branch
from
August 25, 2026 22:51
a42d3d0 to
b9b0e90
Compare
zzylol
force-pushed
the
feat/whole-plan-cost-selection-271
branch
from
August 25, 2026 23:09
b9b0e90 to
de880da
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #271. Part of #33.
Why this PR is needed
PlanSpace::cost_sortedranks each memo group independently. For CSE choices, it passes the group's structuralconsumer_counttoCostModel::cse_share_decision.That count is correct only when ancestor choices do not change how often the group executes. If a shared ancestor is instead selected as
RecomputeIndependently, every execution of that ancestor also executes its descendants. A descendant can therefore run more times than its number of direct incoming edges suggests.Concrete example:
Assume three dashboard queries run over the same request table:
After CSE, the repeated work is represented as:
The two identical enterprise queries share one structural node
a. Therefore the graph contains only onea -> cedge. Structurally, the planner sees:Now use concrete cost estimates:
The cost model first selects
a = RecomputeIndependently, because its two executions cost 80 instead of 100. Dashboard 1 and Dashboard 2 therefore execute separate copies ofa, and each copy also executesc. Dashboard 3 executesconce more:A local decision uses
c.consumer_count = 2and chooses recomputation at cost 80. The whole-plan decision usesc.effective_consumer_count = 3and chooses sharing at cost 100 instead of recomputation at cost 120.Independent group ranking still costs
cas two executions and can choose the wrong CSE plan. The planner needs to carry an ancestor's selected multiplicity into its descendants before deciding them.How this PR solves it
This PR adds
PlanSpace::global_selection, a parent-before-child dynamic-programming pass over the complete candidate space.1. Identify candidate roles explicitly
ReplacementSubDAGnow carriesReplacementProvenance:The planner can locate the exact CSE share/recompute pair even when summary, Hydra, roll-up, or semantic-rewrite candidates coexist in the same memo group. It no longer infers CSE semantics from rationale text, replacement shape, or pointer identity alone.
2. Build the possible reference graph
The planner builds a
ReferenceGraphfrom:Kahn's algorithm produces a real parent-before-child topological order. Discovery order is insufficient for shared diamonds because a later-discovered parent can reference an earlier-discovered child.
3. Propagate effective execution counts
For each group in topological order, selection computes:
A selected parent contributes:
Only the selected rewrite's child edges propagate counts. Edges belonging to unselected alternatives affect topological ordering but do not inflate execution counts.
4. Re-cost CSE with the corrected count
When a group has a CSE pair and at least two effective uses,
global_selectioncalls the existingCostModel::cse_share_decisionwith the corrected count. It does not introduce a second cost interface.If a shared ancestor collapses a descendant to one effective execution, the descendant still receives a valid selected plan: an unrelated non-CSE candidate when available, otherwise the original/share candidate as the single-execution fallback.
Before and after
For the example above:
PlanSpace::cost_sortedremains available for inspecting all locally ranked alternatives.PlanSpace::global_selectionreturns oneSelectedGroupper site with both its structural and effective consumer counts.Interaction with current strategies
Scope
This is not an exhaustive search across every possible plan combination or a global resource-budget optimizer. It corrects the ancestor-dependent execution count used by the existing pairwise CSE cost decision.
Tests
Coverage includes:
Share;Validation
cargo test -p asap-types -p asap-aware-mapping -p asap-integration-testscargo clippy -p asap-aware-mapping --all-targets -- -D warningscargo fmt --all -- --checkAll local validation passes, including 142
asap-aware-mappingtests.