Skip to content

feat(plan): let CostModel offer sketches for exact TopK - #293

Closed
zzylol wants to merge 3 commits into
mainfrom
feat/topk-exact-costmodel-candidates-151
Closed

feat(plan): let CostModel offer sketches for exact TopK#293
zzylol wants to merge 3 commits into
mainfrom
feat/topk-exact-costmodel-candidates-151

Conversation

@zzylol

@zzylol zzylol commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Closes #151.

Problem

TopK { accuracy: Exact } currently short-circuits to PassThrough. Unlike exact Count, top-k has no exact mergeable accumulator, but some deployments still want to offer the closest available sketch approximation.

The planner previously had no cost-model extension point for expressing that policy.

Changes

  • Add CostModel::topk_exact_accuracy_target, defaulting to None.
  • Keep existing behavior unchanged when the hook returns None: only PassThrough is offered.
  • When the hook returns an approximate target, offer the ranked and correctly sized CmsWithHeap and CountSketchWithHeap candidates alongside PassThrough.
  • Require an explicit approximate sizing budget instead of sizing against Exact, which would saturate CMS width at 1 << 26 and could create multi-gigabyte sketch instances.
  • Reject Some(AccuracyTarget::Exact) as a cost-model contract violation before sizing.
  • Leave all other exact intents unchanged, including the exact Count accumulator.
  • Document the new hook in the developer and user guides.

Compatibility

The hook has a provided default implementation returning None, so existing CostModel implementations require no changes and preserve the current TopK { accuracy: Exact } behavior.

Validation

  • cargo build --workspace
  • cargo test --workspace
  • cargo clippy --workspace --all-targets -- -D warnings
  • cargo fmt --all -- --check

Focused coverage verifies default pass-through behavior, opted-in candidate ranking and sizing, retention of PassThrough, rejection of an Exact sizing target, explanation output, and no behavior change for exact Count.

zzylol and others added 3 commits August 26, 2026 13:43
…etch candidates (#151)

`implementations_for_with`'s `AccuracyTarget::Exact` arm previously
short-circuited every approximate-capable intent straight to
`exact_realization`, which for `TopK` always meant `PassThrough` --
there's no exact mergeable top-k accumulator, so no `CostModel` hook
was ever consulted for that shape.

Add `CostModel::topk_exact_offers_sketch(&self, intent) -> bool`
(default `false`) as the opt-in gate. When a deployment's `CostModel`
returns `true` for a `TopK { accuracy: Exact, .. }` intent,
`implementations_for_with` routes it through the same
`sketch_implementations` pipeline an approximate `TopK` request uses --
`summary_candidates` for the CmsWithHeap/CountSketchWithHeap family,
ranked via `rank_candidates` and sized via `size_params` at the
tightest budget `AccuracyTarget::Exact` degrades to -- instead of
inventing a parallel candidate-generation path.

Default-preserves-behavior guarantee: the new method has a provided
default body (`false`), so every existing `CostModel` implementation
(including ones that only override the required `rank_candidates`)
keeps today's `PassThrough` behavior for `TopK { accuracy: Exact, .. }`
byte for byte, with no changes required on their part.
`AggIntent::Count { accuracy: Exact }` and the rest of the Exact arm
are untouched -- only `TopK` is gated by the new hook.

Tests (crates/asap-aware-mapping/src/replacement.rs):
- topk_exact_default_cost_model_still_passes_through: (a) no regression
- topk_exact_opted_in_cost_model_offers_sketch_candidates: (b) opt-in
  CostModel gets a real, ranked, sized sketch candidate
- count_exact_unaffected_by_topk_exact_opt_in: (c) Count{Exact} unaffected,
  even against a CostModel that opts every intent in

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…PassThrough (#151)

Code review of #293 found two bugs in the CostModel opt-in for
AggIntent::TopK { accuracy: Exact, .. }:

1. Sketches were sized via accuracy_budget(Exact), which resolves to
   (f64::MIN_POSITIVE, DEFAULT_DELTA) -- cms_width saturates that to
   its clamp maximum (1<<26). A CmsWithHeap/CountSketchWithHeap
   candidate under that budget is 67,108,864 x 5 = 335,544,320
   counters (~1.3-2.7 GB) per instance, multiplied per subpopulation
   for a grouped top-k. Both this crate's SQL and PromQL frontends
   default to Exact when no accuracy is specified, so every
   un-annotated topk query in an opted-in deployment would have gotten
   the largest possible sketch.

2. The opt-in replaced PassThrough instead of adding sketch candidates
   alongside it, pruning a candidate implementations_for_with's own
   docs (and rank_candidates' contract) call exhaustive/never-prune.

Fix:

- Replaced `CostModel::topk_exact_offers_sketch(&self, intent) -> bool`
  with `CostModel::topk_exact_accuracy_target(&self, intent) ->
  Option<AccuracyTarget>`. `None` (the default) preserves today's
  PassThrough-only behavior exactly. `Some(target)` supplies the real
  budget to size at -- the hook itself documents why `target` must
  never be `AccuracyTarget::Exact` again (same clamp-saturation
  problem, now an explicit deployment choice instead of a silent
  default).
- `topk_exact_realization` (new, replacing the inline match arm) now
  offers the target-sized, rank_candidates-ranked sketch candidate(s)
  *ahead of* PassThrough, not instead of it -- PassThrough stays in
  the returned Vec unconditionally.
- Updated cost_model.rs/replacement.rs doc comments accordingly.

Tests (crates/asap-aware-mapping/src/replacement.rs):
- topk_exact_default_cost_model_still_passes_through: now also checks
  an explicit `None` behaves identically to the default.
- topk_exact_opted_in_cost_model_offers_sketch_candidates: sizes at a
  sane eps=0.1 target, asserts PassThrough is still present (not
  pruned) and last, and pins width=28/depth=5 explicitly so a
  regression back to the clamp-saturated (1<<26, ..) is caught by a
  failing assertion instead of going unpinned.
- count_exact_unaffected_by_topk_exact_opt_in: unchanged coverage,
  updated to the new hook signature.
- Consolidated the two near-duplicate test CostModel impls into one
  parameterized `TopkExactCostModel { target: Option<AccuracyTarget> }`.

Also addressed:
- explanation.rs: added
  topk_exact_opted_in_cost_model_reports_a_sketch_applicability_finding,
  confirming an opted-in TopK{Exact} candidate is correctly reported as
  a SketchApproximation finding (no code change needed -- the module's
  existing "does this candidate list contain anything beyond the
  trivial realization" framing already covers this once the candidate
  is generated); scoped
  exact_quantile_is_not_a_sketch_applicability_finding's doc comment to
  note this one exception.
- docs/developer_docs/ASAP-aware-mapping-developer-guide.md: added
  `topk_exact_accuracy_target` to the CostModel hook table, a full
  "Which hook" subsection, the quick-reference table, and corrected the
  "single candidate" claim for exact/pass-through realizations.
- docs/user-guide/user-guide.md: added the new hook to the CostModel
  hooks mentioned there.

cargo build --workspace, cargo test --workspace,
cargo clippy --workspace --all-targets -- -D warnings, and
cargo fmt --all -- --check all pass clean.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@zzylol zzylol changed the title feat(plan): let CostModel opt TopK{accuracy: Exact} into sketch candidates (#151) feat(plan): let CostModel offer sketches for exact TopK Aug 28, 2026
@zzylol

zzylol commented Aug 28, 2026

Copy link
Copy Markdown
Contributor Author

Closing this approach because AccuracyTarget::Exact is a semantic requirement, not a cost-model preference. Exact top-k should remain PassThrough and be recomputed from raw data; sketch candidates are valid only when the user supplies an approximate target such as Epsilon or EpsilonDelta. Allowing a CostModel to weaken Exact to an approximation would violate the declared accuracy contract.

@zzylol zzylol closed this Aug 28, 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.

TopK { accuracy: Exact } routes to PassThrough — no sketch-tier approximation offered even when one would serve

1 participant