Skip to content

Pass 2 summary-capability rule: share one summary sized for the strictest consumer - #595

Closed
zzylol wants to merge 2 commits into
stack/509-w2-executor-panesfrom
stack/509-w7a-pass2-sizing
Closed

zzylol wants to merge 2 commits into
stack/509-w2-executor-panesfrom
stack/509-w7a-pass2-sizing

Conversation

@zzylol

@zzylol zzylol commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Rebased on main d4869a7 (DF 54).

Wave 1 chain: #594 → #593 → #592 → #591 → #595 → #596 → #597 → #598 → #599 (on #589)

Part of #509 (Pass 2), #580 item D, decision W5.

Why

Stage 1 only planned Pass 2's identical-expression rule. Two queries that read the same input with different accuracy requirements, or different quantiles at the same requirement over an unkeyed SQL scan, never shared a summary. #509's summary-capability rule allows one summary sized for the strictest consumer.

What

  • pass2::summary_capability: targets with the same summary input data and window (same input sub-DAG including TimeRange/WHERE, same grouping, same quantile column, approximate requirement) form a key. Every target of a key is re-sized for the strictest requirement (smallest ε and δ), all or nothing per key (W5). Sizing reuses the legacy reconciliation's argument (accuracy_budget; dominates is checked): requirements resolve to (ε, δ) and the sizing formulas are monotonic. First family: quantiles (KLL, DDSketch), so one KLL serves p50 and p99.
  • Stage 1 now has up to three variants, Sharing::{Independent, IdenticalExpressions, SummaryCapability}. SharingVariant.shared: bool is replaced by sharing: Sharing. The capability variant is built on the identical-expression inventory and merges identical producers after composition. It is skipped when it would repeat that variant. Stage 3 checks each query against its own target and chooses.
  • Fix in the tree DP (plan_selection::select_variant): pairs of targets reading a common input were collected but never checked. The loop required one to read the other. Merged producers were therefore missed. The DP now checks those pairs, and compares inputs by value too (SQL scans have no unique key, so CSE does not alias them).
  • Fix in composition: a summary evaluation keeps the measure's explicit SQL output name.

Before / After

quantile(0.5, lat) and quantile(0.99, lat), ε = 0.01, through the facade (stage_pipeline numbers, 15k samples):

selected cost (cpu_ms/eval)
Before P10: two exact quantiles over one shared scan (the DP missed the merge) 0.108
After P14: one KLL, two estimates 0.093

p50 at ε = 0.01 and p99 at ε = 0.001 over lat[5m]: Before, the two KLLs differ (k for 0.01 and for 0.001) and never merge. After, the summary-capability variant sizes both for ε = 0.001, and the plan deploys one KLL (stage_pipeline_shares_one_kll_sized_for_the_strictest_consumer).

Tests

  • Un-ignored in crates/planner/tests/summary_sharing.rs: cross_series_p50_and_p99_share_one_producer, quantiles_with_equal_params_share_one_producer, identical_sql_percentiles_share_one_producer.
  • Still ignored, with new precise reasons:
    • quantiles_share_one_producer_sized_for_the_strictest_consumer: the sharing assertions pass. The pipeline attaches no guarantee to plan roots, and Stage 3 ignores PlanningModels.cost, so alone the looser query selects raw.
    • sql_p50_and_p99_share_one_producer: the shared half now passes, names included. The unshared negative cases select raw, since a query-time summary never costs less until Stage 2 materializes.
  • New: stage_pipeline_shares_one_kll_sized_for_the_strictest_consumer and stage_pipeline_shares_sql_p50_and_p99 cover the passing halves, plus 3 unit tests for the rule and 3 DP-equals-exhaustive tests with sharing (PromQL, PromQL plus an unrelated query, SQL). Two of the DP tests fail without the coupling fix.

Gate

fmt and clippy (-D warnings) are clean. cargo test --workspace: 1,528 passed and 7 ignored, against #589's 1,517 and 10. That is 3 un-ignored plus 8 new tests. The Example 1 fixture regenerates byte-identically through stage_pipeline, because no capability key applies there.

Gaps

  • One variant per workload. Mixed decisions across keys (key A shared, key B independently sized) are not listed.
  • Window-composition rule not planned.
  • Root guarantees are not attached by the pipeline.

@zzylol
zzylol force-pushed the stack/509-w7a-pass2-sizing branch from 63d6468 to 3c07c2d Compare October 4, 2026 18:25
@zzylol
zzylol changed the base branch from stack/509-e2-keyed-additive-topk to stack/509-w2-executor-panes October 4, 2026 18:25
@zzylol
zzylol force-pushed the stack/509-w7a-pass2-sizing branch from 3c07c2d to cf0df8a Compare October 5, 2026 06:21
@zzylol
zzylol force-pushed the stack/509-w2-executor-panes branch from 7fa00bd to 4dce4aa Compare October 8, 2026 16:47
@zzylol
zzylol force-pushed the stack/509-w7a-pass2-sizing branch 2 times, most recently from 4f7e557 to 5a99a8b Compare October 10, 2026 14:32
@zzylol
zzylol force-pushed the stack/509-w2-executor-panes branch from 4dce4aa to 4f7a521 Compare October 10, 2026 14:32
zzylol and others added 2 commits October 11, 2026 16:22
…riant

Targets with the same summary input data and window (same input, grouping
and quantile column) are re-sized for their strictest consumer, all or
nothing per key (#580 W5), reusing the legacy reconciliation's
accuracy_budget/dominates argument. Stage 1 adds this inventory as a third
variant (Sharing::SummaryCapability) next to the independent and
identical-expression ones; composition then builds identical summary
producers, which are merged, and Stage 3 chooses.

Also:
- the tree DP now checks coupling between targets that read a common (or
  equal) input; it skipped those pairs, so it missed merged producers;
- a composed summary evaluation keeps the measure's explicit output name
  (SQL), instead of the derived one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Pass 1 takes the declared metric types since #593, and #594's year-long scan
test selects its variant by the Sharing enum instead of a shared flag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zzylol
zzylol force-pushed the stack/509-w2-executor-panes branch from 4f7a521 to b30a3cc Compare October 11, 2026 17:11
@zzylol
zzylol force-pushed the stack/509-w7a-pass2-sizing branch from 5a99a8b to 13aee91 Compare October 11, 2026 17:11
zzylol added a commit that referenced this pull request Oct 13, 2026
The specs and tests from #590 run the current stages through the shared
planner_layering_common pipeline (Pass 1 and composition,
optimize_physical_asap, select_plan); tests are named after the stages.
Materialization is read from the physical DAG (#667); window forms and
retention stay pending adapters. Behavior that works passes (29 tests);
every expectation that needs a feature not in this stack is ignored with
the PR that delivers it (29 tests): Pass 2 sharing (#588, #595, #596),
window composition (#601, #628, #634), materialization enumeration and
MatAtQuery (#604, #625), UnivMon sizing and accuracy (#621), SQL
recognition and COUNT(*) composition (#598, #603) and plan selection row
estimates (#632). Each spec's "Today" column states the current output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W7qG9aFyPij5uWsyAJCxDW
zzylol added a commit that referenced this pull request Oct 13, 2026
The specs and tests from #590 run the current stages through the shared
planner_layering_common pipeline (Pass 1 and composition,
optimize_physical_asap, select_plan); tests are named after the stages.
Materialization is read from the physical DAG (#667); window forms and
retention stay pending adapters. Behavior that works passes (29 tests);
every expectation that needs a feature not in this stack is ignored with
the PR that delivers it (29 tests): Pass 2 sharing (#588, #595, #596),
window composition (#601, #628, #634), materialization enumeration and
MatAtQuery (#604, #625), UnivMon sizing and accuracy (#621), SQL
recognition and COUNT(*) composition (#598, #603) and plan selection row
estimates (#632). Each spec's "Today" column states the current output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W7qG9aFyPij5uWsyAJCxDW
zzylol added a commit that referenced this pull request Oct 13, 2026
…stages (#577)

* test: acceptance spec and pending tests for planner-layering Example 1

Define MVP acceptance for #509 Example 1 before the Phase C stage APIs
exist: 1 -> 6 -> 6 -> 1 candidates (Pass 1 x identical-expression
sharing; physical operator implementation only, no materialization).

- Spec with per-stage candidate tables, invariants and doc ambiguities.
- Integration tests against todo!() stage stubs, ignored until the
  stages land; workload and Stage 0 frontend-shape tests run now.
- Expected-only asap-stage-pipeline/v1 fixture for the DAG viewer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: run the Example 1 acceptance tests against the real stages

Replace the todo!() stubs with adapters over Stage 0-3, un-ignore the 11
tests that pass, and give each still-ignored test the precise difference
between the implementation and the spec. Add runtime capability checks:
the selected plan compiles in the physical planner; compiling every
candidate stays ignored with the two runtime gaps it finds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: read Example 1 logical stages as flat DAGs after the export removal

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W7qG9aFyPij5uWsyAJCxDW

* test: Example 1 acceptance on the composed stages (1 -> 3 -> 3 -> 1)

The tests run Pass 1 and composition, optimize_physical_asap and
select_plan through a shared tests/planner_layering_common pipeline, and
follow the decided behavior: three Pass 1 candidates (Q2 exact, CMS+heap,
CountSketch+heap), every sub-DAG NotMaterialized, P1 selected, CMS+heap
invalid (weights not proven non-negative) and CountSketch+heap invalid (a
Top-K output has no guarantee and no deployment evidence). Hydra (#605),
Pass 2 sharing (#588) and the two runtime gaps (#593, #579) stay ignored
with the PR that delivers them. Tests are named after the stages; the
compile check no longer uses the PromQL fallback. The spec lists where the
doc and the planner differ. The expected-only viewer fixture is removed:
the generated four-stage fixture replaces it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W7qG9aFyPij5uWsyAJCxDW

* test: port #590's Example 2-4 acceptance specs and tests

The specs and tests from #590 run the current stages through the shared
planner_layering_common pipeline (Pass 1 and composition,
optimize_physical_asap, select_plan); tests are named after the stages.
Materialization is read from the physical DAG (#667); window forms and
retention stay pending adapters. Behavior that works passes (29 tests);
every expectation that needs a feature not in this stack is ignored with
the PR that delivers it (29 tests): Pass 2 sharing (#588, #595, #596),
window composition (#601, #628, #634), materialization enumeration and
MatAtQuery (#604, #625), UnivMon sizing and accuracy (#621), SQL
recognition and COUNT(*) composition (#598, #603) and plan selection row
estimates (#632). Each spec's "Today" column states the current output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W7qG9aFyPij5uWsyAJCxDW

* feat(devtools): stage_pipeline examples for #509 Example 3

Ported from #590: --example planner-layering-3a (Pattern A, ad hoc batch of
five p99 reports) and planner-layering-3b (Pattern B, repeating 5-min p99),
the Example 3 acceptance tests' workloads. Example 1's output is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W7qG9aFyPij5uWsyAJCxDW

* test: follow #576's per-consumer charging and exact top-k columns

Plan selection charges a NotMaterialized node once per consuming query,
so the charging helper checks NodeCost.multiplier and the Example 4 A3
ambiguity is recorded as decided. Example 1 checks that exact Q2 sorts by
the ranked value and returns [job, topk_10]. The stage viewer guide's
tab 1 row now describes the actual all-exact result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W7qG9aFyPij5uWsyAJCxDW

* refactor(devtools): one planning pipeline for stage_pipeline and the acceptance tests

run_planning_stages and the #509 example workloads move into the
asap-devtools library; stage_pipeline serializes the run and the
planner-layering tests adapt it. The shared data workload drops
ingestion_rate for data at rest, which validate() requires.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W7qG9aFyPij5uWsyAJCxDW

* fix(devtools): select over every candidate; --max-displayed limits only the document

stage_pipeline capped enumeration at 64, so Example 3a (243 candidates)
selected P41 instead of the cheapest P122 (KLL for all five queries).
The pipeline now enumerates, optimizes and prices every candidate; the
document shows the first --max-displayed (default 64) plus the selected
one, with `combinations` and `capped`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W7qG9aFyPij5uWsyAJCxDW

* docs: Example 2's SQL candidates compose; the SQL test waits only on #598

Running the ignored SQL test composes every candidate and fails only on
Q2's options (per-source counts until #598), so #603 is not a blocker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W7qG9aFyPij5uWsyAJCxDW

* test: name stages by number and name; say query-language frontends

The frontends' Run field, id and locals become stage0_query_language_frontends,
and test names carry stage0..stage3 with the stage name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W7qG9aFyPij5uWsyAJCxDW

* test: charging helper follows plan selection's per-consumer rule

With a materialized node the helper accepted any multiplier >= 1. It now
computes each node's computations as plan selection does (walks from
query roots and materialized nodes' inputs, stopping at materialized
nodes), checked on a hand-built candidate with a materialized node.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W7qG9aFyPij5uWsyAJCxDW

* test: Example 1 picks Q2 candidates by (option, no shared input)

physical_with and the invalid-candidate check no longer assume one
candidate per Q2 option, so #588's shared-input variants need no change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W7qG9aFyPij5uWsyAJCxDW

* docs: Example 3 Pattern B charges each node once per consuming query

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W7qG9aFyPij5uWsyAJCxDW

* docs(stage-viewer): rows 2, 3a and 3b describe the planner's current output

Read from uncapped runs: Example 2's sketch plans are rejected for having
no (ε, δ) guarantee, 3a selects P122 (first of 108 tied cheapest plans,
nothing shared yet) and 3b selects query-time KLL; panes and the latency
check come with the window-summary PRs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W7qG9aFyPij5uWsyAJCxDW

* docs(stage-viewer): row 2 from the SQL run; 3a ties 32 plans, not 108

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W7qG9aFyPij5uWsyAJCxDW

* test: Example 1 counts only unshared candidates; stage3_ charging-helper names

The 3-logical and 3-physical counts now hold when #588 adds shared-input
variants.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W7qG9aFyPij5uWsyAJCxDW

* refactor(devtools): PlanningStagesRun fields carry stage number and name

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W7qG9aFyPij5uWsyAJCxDW

* test(devtools): unique temp file per stage_pipeline run

cargo test runs the tests as threads of one process, so two runs of the same
example shared a file name and one deleted the other's output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01W7qG9aFyPij5uWsyAJCxDW

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@zzylol

zzylol commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #707 (merged): G5, Example 2 — Pass 2 summary-capability rule, UnivMon, SQL L2/entropy idioms.

@zzylol zzylol closed this Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant