Skip to content

feat(types): name a materialized view by its view state key and plan id (#1429) - #1536

Merged
zzylol merged 8 commits into
mainfrom
feat/view-state-key
Nov 27, 2026
Merged

zzylol merged 8 commits into
mainfrom
feat/view-state-key

Conversation

@zzylol

@zzylol zzylol commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Problem: a deployment filed stored view states under the view's coverage (definition_id, selection). Coverage leaves out θ and the view's tumbling-window width. In the planned chains-10m-5m-3m-10s workload, three views share one coverage: a 5-minute chain, a 10-second chain, and 60-second windows merged from the 10-second chain. Across the 22 snapshots with views, 43 of 54 views share their coverage with another view of the same plan.
  • Change: two new ids.
    • MaterializedView::view_state_key() says what the stored state is, in any plan: ViewStateKey { definition_id, window, logical_properties }, taken from the view node alone.
    • PhysicalASAPDAG::plan_id() (also MaterializedView::plan_id()) is a content hash of the DAG. With node_id, it names a view inside one installed plan.
  • Effect: the store key is (ViewStateKey, concrete selection). All 54 snapshot views get distinct keys. Plans, costs and the plan-file format (INSTALLABLE_PLAN_WIRE_VERSION) are unchanged.

Review guide

  • Base: main. Not stacked.
  • Read first:
    • crates/types/src/plan/materialized_view/view_state_key.rs: the key, and the rule for which window a view has.
    • crates/types/src/plan/materialized_view/views.rs (ViewIndex::views): where both ids are set.
    • crates/types/src/plan/content_id.rs: the hash, moved here from view_coverage.rs.
    • docs/design_docs/physical-planning-and-deployment.md §4, "Identifying a materialized view".
  • Needs careful review:
    • ViewWindow::of_node: which window each view gets.
    • The new Eq and Hash on SketchParams (summary_type/parameters.rs).
    • float_roundtrip in crates/types/Cargo.toml.
  • Mechanical:
    • The regenerated snapshots: they add plan_id and view_state_key to each view, plus the two inherited plan changes listed under Verification.
    • Design doc: "deployment systems such as …" becomes "deployment runtimes".
    • fnv1a_128_hex moved unchanged.
    • The renamed keyed-store test.

Behavior: before → after

Input: the committed sketch workload crates/planner-cli/tests/sketch_plan_snapshots/workloads/chains-10m-5m-3m-10s.json: quantile_over_time(0.5, data[10m]) and quantile_over_time(0.9, data[5m]) every 5 min, and quantile_over_time(0.99, data[3m]) every 10 s, each with max_error 0.01 and failure_probability 0.001. Raw data is not retained (mvp.capabilities.json).

Invocation:

asap_planner --workload …/chains-10m-5m-3m-10s.json --run-until stage3 --format json \
  --deployment-capabilities …/deployments/mvp.capabilities.json \
  --cost-inputs tools/dag-viewer/cost-inputs/aws-fargate-us-east-1.json

The plan's materialized_views:

node operator before: what a store could key by after: view_state_key
1 SummaryTumblingWindowChain, 300 s definition_id bfbffb8f…, selection [{"columns": []}] bfbffb8f…, window: {"tumbling": {"width_ms": 300000}}, parameters: {"Kll": {"k": 2863}}
10 SummaryTumblingWindowChain, 10 s the same as node 1 bfbffb8f…, window: {"tumbling": {"width_ms": 10000}}, Kll {k: 1404}
11 SummaryMergeWindows, 10 s → 60 s the same as node 1 bfbffb8f…, window: {"tumbling": {"width_ms": 60000}}, Kll {k: 1404}

All three share "plan_id": "f6f31722b12fc85f69ff48e068761870".

  • Before: a store keyed by coverage plus the request's time range could answer node 1's read of (T-5m, T] with node 10's thirty 10-second rows. The executor never re-filters a view read.
  • After: the three keys differ, so that read finds only node 1's rows.

Who needs it: any runtime that keeps view states, and especially one that keeps them across restarts or replanning. The plan-independent key lets it reuse a stored state under a new plan. (plan_id, node_id) addresses a view within the installed plan.

Interfaces

Permalinks are at the PR head, 220e0e5.

crates/types/src/plan/materialized_view/view_state_key.rs (new)

+/// What a view's stored state is, in any plan and process, from the view node
+/// alone. Equal keys with one concrete selection hold interchangeable states.
+#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
+#[serde(deny_unknown_fields)]
+pub struct ViewStateKey {
+    /// What is computed: the memo group's coverage definition.
+    pub definition_id: String,
+    /// The windows the view's rows are, from its own operator; `None` for one
+    /// state per group, whose window is in its selection.
+    pub window: Option<ViewWindow>,
+    /// The view node's candidate's logical properties (θ).
+    pub logical_properties: LogicalProperties,
+}
+/// The windows a view's rows are, from its operator.
+#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+#[non_exhaustive]
+pub enum ViewWindow {
+    /// A chain's or `SummaryMergeWindows`' rows, also through a `TimeRange` or
+    /// `TimeShift` over one.
+    Tumbling { width_ms: u64 },
+    /// A `TimeRangeCover`'s rows: one width per level, widest first.
+    TumblingLevels { widths_ms: Vec<u64> },
+}

crates/types/src/plan/materialized_view/mod.rs

+pub use view_state_key::{ViewStateKey, ViewWindow};
 /// One materialized view of a DAG (stays).
 pub struct MaterializedView {
     pub node_id: PhysicalASAPNodeId,
+    pub(crate) plan_id: String,
+    pub(crate) view_state_key: ViewStateKey,
     pub(crate) refresh: MaterializedViewRefresh,
     pub(crate) coverage: ViewCoverage,
     pub inputs: MaterializedViewInputs,
     pub(crate) retention_ms: u64,
 }
 impl MaterializedView {
     pub fn refresh(&self) -> MaterializedViewRefresh;
     pub fn retention_ms(&self) -> u64;
+    /// The plan it belongs to: with `node_id`, its address in one installed plan.
+    pub fn plan_id(&self) -> &str;
+    /// What its stored state is, in any plan: a store keys states by it and the
+    /// concrete selection.
+    pub fn view_state_key(&self) -> &ViewStateKey;
+    /// What it computes and which rows went in (was test-support only).
+    pub fn coverage(&self) -> &ViewCoverage;
 }

crates/types/src/plan/physical_asap_dag.rs

 impl PhysicalASAPDAG {
+    /// The plan's content id: the 128-bit FNV-1a hash, in hex, of its JSON. Equal
+    /// for equal serializations across processes and plan-file round trips.
+    pub fn plan_id(&self) -> String;
 }
 impl ViewCoverage {
+    /// Logical: equal for views of different θ or widths, so not a store key alone
+    /// (was test-support only).
+    pub fn definition_id(&self) -> &str;
+    /// The rows that went in (was test-support only).
+    pub fn selection(&self) -> &[SelectionBox];
 }

crates/types/src/stages/logical/candidate/mod.rs and crates/types/src/summary_type/parameters.rs

 /// A candidate's logical properties (stays); now part of a view's state key.
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
+#[serde(deny_unknown_fields)]
 pub struct LogicalProperties {
     pub(super) parameters: Option<SketchParams>,
 }
+/// θ compares as a value (DDSketch `alpha` lies in (0, 1) in every planner-built
+/// θ) and hashes by its JSON text, so a key can key a hash map.
+impl Eq for SketchParams {}
+impl Hash for SketchParams { /* hash of its JSON text */ }

crates/types/Cargo.toml: serde_json gains float_roundtrip, so a plan file read back has the plan id it was written with.

Plan-file format: unchanged; INSTALLABLE_PLAN_WIRE_VERSION stays 15. CLI JSON: each materialized_views[] entry gains plan_id and view_state_key.

Usage

Adapted from view_state_key::planned_views_of_one_coverage_get_distinct_view_state_keys and the pinned-key test, which run at the PR head; the store lines are illustrative.

// The workload above, read as the snapshot test reads it: three quantile queries over `data`.
let workload: PlanningWorkload = serde_json::from_str(&read(".../workloads/chains-10m-5m-3m-10s.json"))?;
// Raw data not retained, so Stage 3 materializes chains at ingestion time.
let capabilities: DeploymentCapabilities = serde_json::from_str(&read(".../deployments/mvp.capabilities.json"))?;
// The snapshot's cost inputs, so this plan is the snapshot's plan.
let cost_inputs: CostInputs = serde_json::from_str(&read("tools/dag-viewer/cost-inputs/aws-fargate-us-east-1.json"))?;
let models = PlanningModels::builtin()
    .with_deployment_capabilities(&capabilities)
    .with_cost_inputs(&cost_inputs);
// Frontend + Stages 1-3, then installation checks: the plan a runtime installs.
let (plan, _report) = plan_installable(UserInput::new(&workload, FrontendInput::Promql, models)).await?;
// Each view with its two ids, derived from the DAG (no plan-file field).
let views = plan.materialized_views()?;
for view in &views {
    // Where the runtime finds the view in this plan, e.g. ("f6f31722…", 1).
    let address = (view.plan_id(), view.node_id);
    // What its stored state is, in any plan, e.g. {bfbffb8f…, Tumbling{300000}, Kll{k: 2863}}.
    let key = view.view_state_key();
    // A store files each window's state under (key, absolute interval) and reads it back by the
    // same, e.g. a HashMap<(ViewStateKey, (i64, i64)), Batch> as the keyed-store test's store.
    states.insert((key.clone(), (window_start_ms, window_end_ms)), state);
}
// The three views share a coverage (definition bfbffb8f…, empty selection) and have three keys.

Algorithm changes

None. No search, sizing or cost changes in this PR; the only plan and cost differences in the snapshots are the two inherited from main (#1537), listed under Verification.

Decisions and open questions

Needs reviewer decision: none.

Decided (2026-11-27):

  • Key fields. The key is taken from the view node alone: the definition, the node's own window and its candidate's LogicalProperties.
    • Rejected: an opaque hash, a set of properties over the sub-DAG, and a per-node property list. The nodes below a view say how its state is computed, not what it is. So SummaryMerge(TimeRange(5m, Chain[1m])) and SummaryMerge(TimeRange(5m, Chain[5m])) share a key.
    • This narrows the last comment on [Feature] Physical view id: identify a materialized view by what its stored states are, and drop the logical-only id #1429 (property fields on every node of the physical DAG): only the view node's θ and window decide what its state is, and θ keeps its planner name, LogicalProperties. The per-node fields that comment asks for already exist (parameters and the operator payload), and (plan_id, node_id) gives the per-plan address it asks for.
  • θ source. θ is read from the node's existing parameters field via LogicalProperties::of_parameters.
    • Rejected: changing the field to LogicalProperties, which would need a wire-version bump and would rewrite every plan snapshot.
  • plan_id is the content id of the DAG only, and it is set on every MaterializedView.
    • Rejected: a random id stored in the plan file, and a hash of the file text or of the planning input.
  • SketchParams hashing. Hash is the hash of its JSON text. float_roundtrip is on.
  • TimeRangeCover levels. A level that is not a chain is a planner bug (debug_assert), so the key never silently drops a window.
  • Window rule.
    • A TimeRange or TimeShift view over a chain takes the chain's width.
    • A TimeRangeCover view takes its levels' widths (TumblingLevels). This came from review.
    • Any other view gets window: None.

Verification

All runs used cargo 1.100 on an eval host, on this branch rebased onto main at e727be3 (after #1535 and #1537).

Scope Result Newly failing vs main Notes
cargo test -p asap-types 215 + 95 + 8 passed, 1 ignored none ignored: the #1494 known-limitation test
cargo test -p asap-types --test types -- known_limitation --include-ignored 1 failed (expected) — #1494: definition_id 5a060b84… ≠ a7a6e6a7…
cargo test -p asap-integration-tests 349 passed, 8 ignored none
cargo test -p asap-stage3-plan-selection --lib 57 passed none
cargo test -p asap-executor --lib 158 passed, 1 ignored none
cargo test -p asap-planner-cli --test planner_cli 68 passed, 2 ignored none
cargo test --release -p asap-planner-cli --test corpus_snapshots -- --ignored (corpus + sketch snapshots) 3 passed none after UPDATE_SNAPSHOTS=1; diffs below
cargo test --release -p asap-planner-cli --test goldens 14 passed none unchanged
cargo fmt --all --check, cargo clippy -p asap-types -p asap-executor -p asap-integration-tests -p asap-planner-cli --all-targets -D warnings clean

New tests. On main none of them compiles (the #1535 test extension aside). Before the implementation, each failed with not yet implemented, except where noted.

  • kll_tumbling_window_execution::views_differing_only_in_sketch_parameters_have_different_view_state_keys: KLL k=200 vs k=400 have one coverage and different keys.
  • view_state_key::chain_views_differing_only_in_window_width_have_different_view_state_keys: a 1-minute vs a 5-minute chain view.
  • view_state_key::time_range_views_over_chains_of_different_widths_have_different_view_state_keys: TimeRange(5m) over a 1-minute vs a 5-minute chain (from review).
  • view_state_key::merges_of_one_range_over_chains_of_different_widths_share_a_view_state_key: equivalent merges share a key.
  • view_state_key::a_time_range_cover_view_keys_its_levels_widths: a TimeRangeCover view's window is {"tumbling_levels": {"widths_ms": [300000, 60000]}} (from review).
  • coverage_finalize::a_finalized_rate_covers_what_the_rate_covers (from fix(types): a finalized exact state covers what its aggregate covers #1535) now also asserts equal view_coverages, so a finalized rate and Aggregate(Rate) get one key.
  • view_state_key::views_differing_only_in_refresh_retention_or_medium_have_equal_view_state_keys: one key, two plan ids.
  • view_state_key::planned_views_of_one_coverage_get_distinct_view_state_keys: the three planned views above.
  • view_state_key::the_view_state_key_of_a_planned_chain_is_pinned_and_round_trips_through_json: node 1's key, written out; its JSON reads back equal.
  • view_state_key::a_view_planned_in_two_workloads_keeps_its_view_state_key: quantile_over_time(0.99, data[5m]) alone and beside sum_over_time(other[1m]). Each view keeps its key, and the plan ids differ.
  • physical_asap_dag::the_plan_id_survives_a_plan_file_round_trip: a literal 1.0715660391465826e-75 reads back bit-exact with the same plan id. Before float_roundtrip it read back as …825e-75.
  • physical_asap_dag::the_plan_id_changes_with_the_plan_and_only_with_it: retention, θ, lifetime and a literal change the id; selected_physical_candidate does not.
  • materialized_views::the_plan_id_of_a_committed_plan_file_is_pinned: 90231c1c36f2b1660ed23d24725d635f.
  • kll_tumbling_window_execution::a_store_keyed_by_view_state_key_and_time_range_answers_the_tumbling_windows: the renamed keyed-store test; the five shifted views share one key.
  • kll_tumbling_window_execution::a_store_keyed_by_view_state_key_misses_states_of_another_sketch_parameter: a k=400 query over k=200 states fails with "no state for". A key of definition_id alone would have answered it.

Snapshot changes: 22 files. Two of them also carry plan changes that come from main (#1537), whose committed snapshots passed at equal cost without being rewritten:

  • chains-10m-5m-3m-10s-mvp: the 5-minute chain (node 1) and the nodes reading it are sized with k = 2863 (was 1404); the 10-second chain and its merged windows stay at 1404. The cost is the committed one.
  • sql-rollup-quantile-same-window-raw-retained: node 9's lifetime changes; its detail line goes from "holds 24 bytes" to "holds 240 bytes", its cost per second from 1.8740000046e-15 to 1.87400004601e-15, average_memory_bytes from …351778 to …351779 and peak_memory_bytes from 3255584 to 3255800.

With plan_id and view_state_key removed, all 57 regenerated snapshot files equal the ones main (e727be3) writes itself.

  • sketch_plan_snapshots/: 12 files.
  • corpus_snapshots/o11y_bench/: 10 files: q02, q05, q06, q18, q19, q21, q22, q23, q25, q26.

Each view gains plan_id (one per file) and view_state_key. Apart from those two inherited changes, no other byte differs: every other DAG, cost, coverage and accepted line is unchanged. In 21 views of the corpus snapshots and 22 of the sketch snapshots, the view shares its coverage with another view of its plan. All 54 keys are distinct within their plans.

Follow-ups

Closes #1429

@zzylol zzylol added enhancement New feature or request ir Pre-ASAP or post-ASAP intermediate representation labels Nov 27, 2026
@zzylol
zzylol force-pushed the feat/view-state-key branch from 8d5a968 to 2042383 Compare November 27, 2026 17:45
Phase 2 of #1429, tests first. A materialized view's stored state is
named by ViewStateKey { definition_id, window, logical_properties },
taken from the view node alone, and an installed plan by its content id,
PhysicalASAPDAG::plan_id. The tests check that:

- views differing only in θ or in the view node's window width get
  different keys;
- views differing only in refresh mode, retention or storage medium get
  one key and different plan ids;
- merges of one range over 1-minute and 5-minute chains share a key;
- the three planned views of one coverage in the chains-10m-5m-3m-10s
  workload get distinct keys;
- a view planned in two workloads keeps its key;
- the key is pinned and round-trips through JSON;
- the plan id round-trips through a plan file, changes only with the
  plan, and is pinned for a committed plan file;
- a store keyed by (view state key, time range) answers the five
  tumbling windows, and a k = 400 query misses k = 200 states.

Two known limitations are ignored tests: #1493 (a finalized exact
aggregate has no coverage) and #1494 (coverage depends on where a
filter sits).

plan_id, MaterializedView::plan_id and MaterializedView::view_state_key
are todo!() stubs. LogicalProperties gains PartialEq, Eq, Hash and serde,
and SketchParams gains Eq and Hash, so the key can key a hash map.
…id (#1429)

A deployment filed stored view states by the view's coverage
(definition_id and selection), which leaves out θ and the view's
tumbling-window width: the three views of the planned
chains-10m-5m-3m-10s workload (a 5-minute chain, a 10-second chain and
60-second windows merged from it) share one coverage.

- PhysicalASAPDAG::plan_id: the content id of the plan's DAG (128-bit
  FNV-1a of its JSON, the hash definition_id already uses, moved to
  plan::content_id). With node_id it names a view in one installed plan.
- MaterializedView::view_state_key: ViewStateKey { definition_id,
  window, logical_properties }, from the view node alone. A store keys
  states by it and the concrete selection, across plans.
- MaterializedView::plan_id; MaterializedView::coverage and
  ViewCoverage::definition_id/selection are public.
- asap-types turns on serde_json's float_roundtrip, so a plan file read
  back has the plan id it was written with.
- Docs: physical-planning-and-deployment.md section 4 explains the two
  scopes and the layering; the DataStore doc, the primitive-schema
  proposal and the DAG viewer README follow.
…hain's windows in its key (#1429)

Review fixes:
- ViewWindow::of_node walks a TimeRange or TimeShift down to the chain
  it selects windows from, and gives a TimeRangeCover its levels' widths
  (ViewWindow::TumblingLevels): TimeRange(5m, Chain[1m]) and
  TimeRange(5m, Chain[5m]) hold 5 rows and 1 row and no longer share a
  key.
- ViewStateKey::of_view builds the key next to its type.
- The two-workload test matches views by operator before comparing keys.
- Docs: plan-id equality is equality of serializations; the five-window
  example names the views it means; the store contract names the key.
…view_state_key (#1429)

22 snapshots with materialized views gain plan_id and view_state_key
per view. Nothing else changes: every plan, cost and coverage is as
before. 43 of the 54 views share their coverage with another view of
their plan; all 54 have distinct keys.
…te (#1429)

#1535 gave a finalized exact state its aggregate's coverage, so the
#1493 known-limitation test is no longer ignored and now asserts full
equality of the two view coverages. The design doc keeps #1494 as the
one known limitation.
- ViewStateKey::of_view takes the view's coverage and the DAG's node map
  instead of a lookup closure; Candidate::new builds its logical
  properties with LogicalProperties::of_parameters.
- A TimeRangeCover level that is no chain is a planner bug
  (debug_assert) rather than a silent loss of the window; a test keys a
  cover view's levels' widths.
- The #1493 test duplicated #1535's a_finalized_rate_covers_what_the_rate_covers:
  removed, and that test now also asserts equal view coverages.
- The integration tests share one view_over_chain helper.
- Docs: concrete selection throughout; the key relies on chains serving
  only readers on their grid; the test table names the θ test.
…two stale snapshots (#1429)

#1537 changed two sketch plans at the committed cost, so their
snapshots on main passed without being rewritten: chains-10m-5m-3m-10s
now sizes its KLL chains with k = 2863 (was 1404), and
sql-rollup-quantile-same-window keeps one more node's result. With
plan_id and view_state_key removed, every regenerated snapshot equals
the one main writes itself. The pinned 5-minute chain key follows
(k = 2863).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request ir Pre-ASAP or post-ASAP intermediate representation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Physical view id: identify a materialized view by what its stored states are, and drop the logical-only id

1 participant