test(planning): pin count() to series-count, not distinct cardinality - #724
Merged
Merged
Conversation
zzylol
force-pushed
the
feat/shared-series-quantiles
branch
from
September 14, 2026 01:58
98d19a3 to
e10c3fc
Compare
zzylol
force-pushed
the
fix/count-is-not-distinct-cardinality
branch
from
September 14, 2026 01:58
33ca73e to
14ef7dd
Compare
PromQL `count(v)` counts series. It is not a distinct-item count, and it
must not be answered by merging HLL registers: for two series holding
{a,b,c} and {d,e,f} that returns 6 -- the union's distinct cardinality --
where PromQL's answer is 2.
That is not hypothetical. ASAPPlanner `ca7546de`, the revision main pins
today, lowers a bare `count by (d) (...)` to `AggIntent::Cardinality`,
which `capability_for` binds to `Capability::CardinalityApprox` (HLL).
Upstream narrowed the mapping in `b8b5d705` -- bare `count` now lowers to
`AggIntent::Count`, and `Cardinality` is reserved for the distinct-count
idiom (`distinct_over_time`, `COUNT(DISTINCT ...)`, or the inner node of
`count(distinct_over_time(...))`). This branch already carries that bump.
Nothing here changes behavior. These are the regression tests for an
invariant that has flip-flopped across Planner revisions, asserted at both
ends so a future bump cannot quietly reintroduce the conflation:
- `query_parser`: bare `count(v)` / `count by (l) (v)` carry a Count intent
and no Cardinality intent, while the distinct-count idiom keeps its
Cardinality intent.
- `runtime_capability`: approximate Count binds `FrequencyEstimate` and
Cardinality binds `CardinalityApprox` -- never the same capability. This
restates in an executable assertion what the module's own comment on
`capability_for_count_approximate_returns_frequency_estimate` already
argues in prose.
Verified against both revisions: the parser test fails on `ca7546de` with
its intended diagnostic and passes on `b8b5d705`.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
zzylol
force-pushed
the
fix/count-is-not-distinct-cardinality
branch
from
September 14, 2026 02:28
14ef7dd to
6de42cd
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.
Stacked on #700 — base is
feat/shared-series-quantiles, notmain. Merge #700 first.The invariant
PromQL
count(v)counts series. It is not a distinct-item count, and it mustnot be answered by merging HLL registers: for two series holding
{a,b,c}and{d,e,f}that returns 6 — the union's distinct cardinality — where PromQL'sanswer is 2.
Why this needs a test
This is not hypothetical. ASAPPlanner
ca7546de, the revisionmainpinstoday, lowers a bare
count by (d) (...)toAggIntent::Cardinality, whichcapability_forbinds toCapability::CardinalityApprox(HLL):Upstream narrowed the mapping in
b8b5d705—Cardinalityis now reserved forthe distinct-count idiom (
distinct_over_time,COUNT(DISTINCT ...), or theinner node of
count(distinct_over_time(...))). #700 already carries that bumpas a side effect of needing typed
ExactKind::Min.The mapping has flip-flopped across Planner revisions — both spellings appear in
the vendored checkout history. Nothing currently asserts which one is correct, so
a future bump can reintroduce the conflation silently.
What this adds
No behavior change. Two regression tests, one at each end of the invariant:
query_parser::tests::bare_count_is_series_count_not_distinct_cardinality—bare
count(v)/count by (l) (v)carry aCountintent and noCardinalityintent; the distinct-count idiom keeps itsCardinalityintent.runtime_capability::tests::count_and_cardinality_bind_different_sketch_capabilities—approximate
CountbindsFrequencyEstimate,CardinalitybindsCardinalityApprox, and never the same capability. This restates as anexecutable assertion what the module's own comment on
capability_for_count_approximate_returns_frequency_estimatealready argues inprose: "
COUNT(DISTINCT)never lowers toCount."Verification
Checked against both revisions, so the test is known to discriminate:
ca7546de(main's pin) the parser test fails with its intendeddiagnostic:
count(unique_users) must not carry a Cardinality intent …b8b5d705(this branch) it passescargo fmt --check,clippy --workspace --all-targets -D warnings, andcargo test -p control_plane --lib(755 passed, 0 failed) all green.Note on provenance
The original #700 branch changed this behavior and rewrote the surrounding tests
without mentioning it in any commit message, deleting the ASAPController#163/#165
rationale comments in the process. That is what made it look like an arbitrary
reversal during the rebase. It was not — it was adopting an upstream Planner fix.
This PR separates the assertion from the feature work and writes down the argument.
🤖 Generated with Claude Code