Skip to content

feat(runtime): execute exact frequency L2 and entropy - #559

Closed
zzylol wants to merge 1 commit into
stack/528-15-frequency-inputsfrom
stack/509-16-design-gaps
Closed

zzylol wants to merge 1 commit into
stack/528-15-frequency-inputsfrom
stack/509-16-design-gaps

Conversation

@zzylol

@zzylol zzylol commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Problem: the exact L2 and entropy candidates of #509 Example 2 cannot execute

#509 §Pass 1 (local candidate generation) lists an exact candidate next to the summary candidates for both frequency statistics:

Original computation Local candidates (#509 §Pass 1 table)
Entropy(x) Exact entropy, a specialized entropy summary, UnivMon
L2(x) Exact L2 norm, a specialized norm summary, UnivMon

#509 §Example 2 uses these for Q2 and Q3: "each gets its local candidates from the Pass 1 table: exact, a specialized summary, or UnivMon". The independent candidates "are kept as well" after Pass 2, so selection can always fall back to the exact plan. #509 §Physical operator implementation must then turn every node, including the exact one, into a physical operator.

Before this PR, the IR already has AggIntent::FrequencyL2 and AggIntent::FrequencyEntropy, but no exact physical implementation exists:

Aggregate { reduction: Reduce(none), measures: [FrequencyL2 { col: Some(0), accuracy: Exact }] }
  native binding (physical_planner::bind_operation)
    → Err("aggregate intent has no native implementation")
  raw analytical costing (query_physical_lowering, supports_hash_aggregate)
    → Err(AnalyticalCostError::UnsupportedQueryOperator)

The only way to answer these queries is a manually built UnivMon state (#557). That is an approximation, not the exact fallback #509 assumes.

Scope. This PR supplies the exact physical operator for the L2(x) and Entropy(x) rows of the #509 §Pass 1 table, and makes the raw cost path accept them. It does not add SQL recognition of the Q2/Q3 idioms (#509 §Example 2 TODO), Pass 2 sharing, or UnivMon accuracy. It also adds a developer status note that audits #557 against #509 and lists the remaining scopes in order.

Proposed method

Physical runtime and raw costing only. Logical planning is unchanged.

  1. Native binding. bind_operation maps AggIntent::FrequencyL2 { col, .. } to Reduction::FrequencyL2(column) and AggIntent::FrequencyEntropy { col, .. } to Reduction::FrequencyEntropy(column). As for other intents, col: None resolves to the sample-value column.
  2. Construction check. Operator::aggregate accepts these reductions only over a plain Bool, Int64, Float64 or Utf8 column. The output column is non-nullable Float64.
  3. Exact reducer. For each group, reduce_one:
    • skips NULL;
    • rejects non-finite Float64 identities;
    • encodes the value with the type-tagged Value::key() (so -0.0 and 0.0 are one key, and Int64 keys above 2^53 stay distinct);
    • counts each key in a BTreeMap<Vec<u8>, u64> and tracks the total;
    • computes L2 as the running hypot of all counts, or entropy as -Σ p·log2(p) with p = count / total.
      An empty population (no rows or only NULL) returns 0.0.
  4. Memory and cancellation. Each new key reserves 64 + size_of::<Vec<u8>>() + key.len() + size_of::<u64>() bytes in a Workspace tied to the RunContext. The reservation fails with Error::MemoryLimit when over budget and is released afterward. Each row and each count passes work.checkpoint(), so long reductions yield and can be cancelled. reduce_one now receives the RunContext for this.
  5. Raw costing. supports_hash_aggregate lists FrequencyL2 and FrequencyEntropy, so a raw plan with these intents lowers to PhysicalOperator::HashAggregate instead of failing.

Entropy is in bits, matching the AggIntent::FrequencyEntropy doc ("Shannon entropy in bits").

Key code interfaces

crates/asap-physical-operators/src/operators/aggregate/mod.rs

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
pub enum Reduction {
    Count,
    Sum(usize),
    Avg(usize),
    Min(usize),
    Max(usize),
    /// L2 norm of unit-update frequencies; NULL identities are skipped.
    FrequencyL2(usize),
    /// Shannon entropy in bits; NULL identities are skipped.
    FrequencyEntropy(usize),
    Quantile { column: usize, q: f64 },
}

impl Operator {
    pub fn aggregate(
        input: SchemaRef,
        groups: Vec<usize>,
        measures: Vec<(String, Reduction)>,
    ) -> Result<Self, Error>;
}

Existing IR intents now bound natively (crates/types/src/pre_asap/agg_intent.rs, unchanged):

FrequencyL2 { col: Option<C>, accuracy: AccuracyTarget },
FrequencyEntropy { col: Option<C>, accuracy: AccuracyTarget },

Binding (crates/asap-physical-operators/src/physical_planner/mod.rs) and raw costing (crates/asap-aware-mapping/src/query_physical_lowering.rs):

AggIntent::FrequencyL2 { col, .. } => Reduction::FrequencyL2(column(*col)?),
AggIntent::FrequencyEntropy { col, .. } => Reduction::FrequencyEntropy(column(*col)?),

fn supports_hash_aggregate(reduction: &Reduction, measures: &[AggIntent]) -> bool; // + FrequencyL2, FrequencyEntropy

Usage (tests/physical_semantics.rs::exact_frequency_grouping_and_entropy_bits):

let operator = Operator::aggregate(
    input,                 // (group: Int64, key: Float64 nullable)
    vec![0],
    vec![
        ("l2".into(), Reduction::FrequencyL2(1)),
        ("entropy".into(), Reduction::FrequencyEntropy(1)),
    ],
)?;

Fields

Reduction (new variants)

Variant Field Meaning / invariants
FrequencyL2(usize) column index Identity column. Must be plain Bool, Int64, Float64 or Utf8. Output: sqrt(Σ count_k²) over distinct non-NULL keys k in the group, as non-nullable Float64. 0.0 for an empty population.
FrequencyEntropy(usize) column index Same column rule. Output: -Σ p_k·log2(p_k) in bits, p_k = count_k / total. 0.0 for an empty or single-key population.

Both: NULL identities are skipped; non-finite Float64 identities are an error at execution; each row counts once (unit update).

Operator::aggregate

Parameter Type Meaning
input SchemaRef Input row schema.
groups Vec<usize> Grouping columns. Each group gets its own frequency population. Empty = one global row.
measures Vec<(String, Reduction)> Output name and reduction per measure.

AggIntent::FrequencyL2 / FrequencyEntropy (as used by binding)

Field Type Use in this PR
col Option<usize> Identity column. None resolves to ColumnRef::SampleValue like other intents.
accuracy AccuracyTarget Ignored by binding. The exact reducer always returns the exact value.

supports_hash_aggregate

Parameter Meaning
reduction Must be Reduction::Reduce(_).
measures Non-empty; every intent must be in the allowed list, which now includes the two frequency intents.

Examples

End to end through binding (tests/physical_semantics.rs::exact_frequency_intents_execute_typed_keys_and_empty_input). A PostAsapDAGNode with Aggregate { measures: [FrequencyL2 | FrequencyEntropy { col: Some(0), accuracy: Exact }] } is compiled with compile_node and run. Input is two keys, each twice, then one NULL (a,a,b,b,NULL):

Key type Keys L2 Entropy (bits)
Utf8 "a", "b" √8 1
Int64 9_007_199_254_740_992, 9_007_199_254_740_993 √8 1
Bool false, true √8 1
Float64 -0.0, 1.0 √8 1

Empty input and NULL-only input both return Float64(0.0) for both intents. Results are checked to 1e-12.

Grouping (exact_frequency_grouping_and_entropy_bits).

group key values L2 entropy
1 -0.0, 0.0, 0.0, 1.0 (zero ×3, one ×1) √10 -0.75·log2(0.75) - 0.25·log2(0.25)
2 2.0, NULL 1 0
3 NULL 0 0

Grouped aggregation over no input rows returns no rows.

Memory (tests/blocking_resources.rs::frequency_dictionary_enforces_memory_budget). 64 distinct Int64 rows under a 12,000-byte budget:

Reduction Result
Count succeeds
FrequencyL2(0) Err(Error::MemoryLimit)
FrequencyEntropy(0) Err(Error::MemoryLimit)

In every case retained_bytes() is 0 afterward.

Accepted vs rejected

Case Result
identity Bool / Int64 / Float64 / Utf8 accepted
other identity type rejected by Operator::aggregate
non-finite Float64 value error at execution
NULL value skipped

Status note. docs/develop_docs/planner-layering-status.md (new) audits #557 against #509 per contract (frontends, local alternatives, summary-capability sharing, window composition, materialization, whole-workload selection, deployment inputs, TODOs) and lists seven ordered follow-up scopes. Step 1, exact frequency execution, is this PR.

Out of scope

Stack and validation

Stacked on #557 · Next: #562 · Reference/tracker: #528

Validation: native binding regression failed before the implementation and passes afterward; native runtime suite; focused grouping and dictionary-memory tests; formatting; native/runtime and mapping all-target Clippy with warnings denied.

@zzylol

zzylol commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Parked as draft: PR priorities changed (see #528). Order is now (A) finish #511 operator sharing, (B) the #572 crate/module reorganization, (C) #509 end-to-end stages. This PR sits on the old stack/528-legacy-physical-base chain, and Phase B moves the files it touches. Its content will be re-scoped onto the new layout in Phase C.

@zzylol

zzylol commented Oct 4, 2026

Copy link
Copy Markdown
Contributor Author

Ported onto the current stack in #597

zzylol added a commit that referenced this pull request Oct 4, 2026
…ntities

Port of the parked runtime PRs for #509 Example 2 onto asap-executor:

- native UnivMon build and Cardinality / FrequencyL2 / FrequencyEntropy
  readouts (the old stack's 14c8ac8, a prerequisite missing here);
- #557: a UnivMon build accepts Utf8, Int64 and Bool identities as
  type-tagged keys; heap key bytes count toward state memory;
- #559: exact FrequencyL2 / FrequencyEntropy reducers (bits, NULL skipped,
  0 for an empty population, memory-accounted, cooperative);
- #563: exact Cardinality reducer over typed tuples.

The legacy raw-cost adapter lists the three intents as hash aggregates.

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

zzylol commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Closing: this parked chain (on stack/528-09-accuracy) has been superseded. Its content was ported or reimplemented further up the current stack, in #597, on the unified IR. Closed with the author's approval during the 2026-10-13 stack cleanup.

@zzylol zzylol closed this Oct 13, 2026
zzylol added a commit that referenced this pull request Oct 15, 2026
Exact Cardinality, FrequencyL2 and FrequencyEntropy aggregates had no
native implementation, so the exact option of #509 Example 2's three
statistics could not execute.

- Reduction::Cardinality(cols) counts distinct tuples; a tuple with any
  NULL is skipped, and grouped key encoding keeps tuple boundaries and
  folds signed zero and NaN payloads.
- Reduction::FrequencyL2(col) and FrequencyEntropy(col) count the unit
  frequencies of one Bool/Int64/Float64/Utf8 identity column: NULL is
  skipped, a non-finite float is an error, an empty population gives 0.0,
  and entropy is in bits, as AggIntent::FrequencyEntropy and UnivMon's
  readout define it.
- The dictionaries are memory-accounted and cooperative.
- Logical aggregates bind them: grouped ones (SQL COUNT(DISTINCT)) and
  per-series range reductions (distinct_over_time, l2_over_time,
  entropy_over_time), which reduce each series' window samples, restore
  the run's timestamp and drop the metric name as range functions do.
- The analytical cost lowering prices them as hash aggregates.

Ports the exact-reducer part of #597 (from #559 and #563).
zzylol added a commit that referenced this pull request Oct 15, 2026
Exact Cardinality, FrequencyL2 and FrequencyEntropy aggregates had no
native implementation, so the exact option of #509 Example 2's three
statistics could not execute.

- Reduction::Cardinality(cols) counts distinct tuples; a tuple with any
  NULL is skipped, and grouped key encoding keeps tuple boundaries and
  folds signed zero and NaN payloads.
- Reduction::FrequencyL2(col) and FrequencyEntropy(col) count the unit
  frequencies of one Bool/Int64/Float64/Utf8 identity column: NULL is
  skipped, a non-finite float is an error, an empty population gives 0.0,
  and entropy is in bits, as AggIntent::FrequencyEntropy and UnivMon's
  readout define it.
- The dictionaries are memory-accounted and cooperative.
- Logical aggregates bind them: grouped ones (SQL COUNT(DISTINCT)) and
  per-series range reductions (distinct_over_time, l2_over_time,
  entropy_over_time), which reduce each series' window samples, restore
  the run's timestamp and drop the metric name as range functions do.
- The analytical cost lowering prices them as hash aggregates.

Ports the exact-reducer part of #597 (from #559 and #563).
zzylol added a commit that referenced this pull request Oct 15, 2026
…and UnivMon frequency statistics (#707)

feat(types): summary-capability facts in the family-capability table

Each family lists the readouts one of its states answers (Readout:
total, item frequency, any quantile rank, distinct count, L2, entropy,
top-k), so a planner rule can check that one state serves every
consumer's statistic: UnivMon answers its total and every frequency
moment, KLL every rank, CountSketch item frequencies and L2, the heap
sketches top-k and item frequencies. Exact accumulators are read as
finalized values and answer no readout.
feat(logical-asap-optimization): Pass 2 summary-capability rule

Composition sizes each consumer's summary build alone, for its own
target. The summary-capability rule groups the builds of one candidate
that chose the same family over the same input sub-DAG (window, WHERE
and time range included), item and weight, grouping and filter, whose
every reader is an estimate the family answers (the family-capability
table's readouts), and that differ only in size. For every choice, per
group, of no sharing or one subset of two or more members whose sizes
differ (partial sharing included), the subset becomes one build sized
with each parameter's largest value among the members (a KLL's failure
probability and DDSketch's alpha: the smallest), and every guarantee
above it is composed again, so plan selection checks each consumer
against the shared state. Builds of equal size stay the
identical-expression rule's; unshared builds are untouched and no
consumer changes family.

Stage 1 lists each composed candidate, its identical-expression
candidates, then each summary-capability candidate followed by its own
identical-expression candidates, every distinct DAG once. The Pass 2
record (Pass2Candidate) names the shared summaries ("UnivMon for Q1,
Q3") and the shared identical groups.

Example 2 (PromQL stand-in): with #702's sizing the three UnivMons
differ (Q1 a distinct-count heap on 19 layers, Q2 the baseline shape for
entropy, Q3 a one-layer Count Sketch for L2), so the rule offers one
UnivMon for each pair and one for all three, each sized with the
members' largest heap, rows, columns and layers and the smallest
recorded distinct-count failure probability; the third query keeps its
own options. A shared UnivMon whose rows and columns together exceed
asap_sketchlib's hash budget (rows * (ceil(log2 cols) + 1) <= 128) is
not offered, since the library would not build it. The shared UnivMon
stays one build in Stage 2.
feat(frontend-sql): recognize the SQL frequency L2 and entropy idioms

#509 Example 2 writes L2(src_ip) and Entropy(src_ip) as SQL idioms over
grouped unit counts. The SQL lowering (single query and batch) now names
them at the query root as FrequencyL2 / FrequencyEntropy aggregates
carrying the query's accuracy target, so Pass 1 offers exact and summary
candidates for each:

- L2: SQRT(SUM(c * c)) over COUNT(*) per key, only when the product is
  already Float64 (CAST(c AS DOUBLE) factors or CAST(COUNT(*) AS DOUBLE)).
  The Int64 product stays relational: SQL integer overflow is observable.
- Entropy: -SUM(p * LN(p)) with p = COUNT(*)[*1.0] / SUM(COUNT(*)) OVER ()
  over the complete unordered frame; the IR's bits are converted to nats
  and SQL's -0.0 is kept.

Both require a single non-nullable Bool/Int64/Utf8 key, no HAVING and no
measure filter, follow projections only, keep the original output schema,
and decide SQL's empty-input NULL by an exact population count joined
beside the statistic, never by an estimate. Scaled probabilities, LOG2,
partitioned or ordered windows and nullable keys stay relational.

Ports the frontend part of #598 (originally #562 and #564).
feat(physical-operators): exact distinct, L2 and entropy reducers

Exact Cardinality, FrequencyL2 and FrequencyEntropy aggregates had no
native implementation, so the exact option of #509 Example 2's three
statistics could not execute.

- Reduction::Cardinality(cols) counts distinct tuples; a tuple with any
  NULL is skipped, and grouped key encoding keeps tuple boundaries and
  folds signed zero and NaN payloads.
- Reduction::FrequencyL2(col) and FrequencyEntropy(col) count the unit
  frequencies of one Bool/Int64/Float64/Utf8 identity column: NULL is
  skipped, a non-finite float is an error, an empty population gives 0.0,
  and entropy is in bits, as AggIntent::FrequencyEntropy and UnivMon's
  readout define it.
- The dictionaries are memory-accounted and cooperative.
- Logical aggregates bind them: grouped ones (SQL COUNT(DISTINCT)) and
  per-series range reductions (distinct_over_time, l2_over_time,
  entropy_over_time), which reduce each series' window samples, restore
  the run's timestamp and drop the metric name as range functions do.
- The analytical cost lowering prices them as hash aggregates.

Ports the exact-reducer part of #597 (from #559 and #563).
feat(physical-operators): native UnivMon state over typed identities

UnivMon had a kernel but no native DAG state: a build was rejected
("summary family has no native DAG state implementation"), a summary
build accepted only Float64 samples, and Pass 1's UnivMon update (each
value of the item column one unit occurrence) did not bind, so no
UnivMon candidate of #509 Example 2 could execute.

- UnivMon is a native family; its states validate against their
  parameters, and one state answers its total, distinct count, L2 norm
  and entropy.
- A UnivMon build takes Bool, Int64 and Utf8 identities as type-tagged
  key bytes (Int64 above 2^53 stays distinct) besides Float64 samples;
  NULL is skipped. Retained heap key bytes count toward state memory.
- A unit-frequency update over one item column binds, for UnivMon and
  HLL, as a value-frequency build over that column: grouped (SQL
  src_ip) and per series (PromQL sample values).

Ports the UnivMon part of #597 (from #557).
test(integration-tests): Example 2 in SQL; the recognized idioms execute

Example 2's SQL Q3 casts its counts to DOUBLE (the design's Int64
SUM(c * c) can overflow and stays relational), and the SQL Q2 and Q3
now lower to the entropy and L2 intents, so the SQL workload has the
PromQL stand-in's Pass 1 options. The recognized entropy executes in
nats with SQL's -0.0, L2 executes exactly, and both give NULL on empty
input, decided by the exact population count.
feat(devtools): stage_pipeline example planner-layering-2

stage_pipeline plans #509 Example 2: three SQL dashboard panels over
the last minute of flows (distinct sources at eps 0.02, entropy at
0.05, L2 at 0.01, delta 0.01), lowered over its flows(ts, src_ip)
table. SQL workloads lower every entry, batch and repeating, each with
its own accuracy target (lower_sql_batch read only batch entries).

The stage-viewer tab 2 row is regenerated from it: 82 plans, 16 from
Pass 1, the rest from the identical-expression and summary-capability
rules (one UnivMon for each pair of queries and for all three); 6
valid, P1 (all exact) selected at 6.08 per second. The row previously
said 8 plans were valid.
feat(logical-asap-optimization): guarantees through a join beside exact inputs and an exactly decided CASE

SQL Example 2's L2 decides the empty-input NULL from an exact population
count cross-joined beside the estimate and a CASE on that count. No
guarantee rule covered the join or the CASE, so every SQL plan with
UnivMon for Q3 was rejected ("the output has no guarantee"), although
the L2 estimate is certified.

- A join (inner, outer or cross) whose inputs are exact but one keeps
  that input's guarantee when its predicate reads only exact columns:
  which rows match, and how often, then does not depend on an estimate.
  A key on the approximate input's columns, a semi- or anti-join, or two
  approximate inputs have none.
- A CASE whose operand and conditions are exact keeps its weakest
  branch's guarantee; a literal or NULL branch, or a missing ELSE, is
  exact. A condition over an approximate value has none.
- A projection over such a join composes each output column this way.

Guarantees are per node, so every column of the approximate input counts
as approximate; column-level tracking (#689) stays the general solution.

A root's default metric (#690) looks through the same shape: a
projection whose CASE value branches read columns (or literals, cast or
not) takes those columns' metric, following them through joins and
row-preserving operators to an aggregate's measure, so SQL's L2 root
resolves to a relative target as the PromQL one does. Any other
expression stays absolute.
docs(stage-viewer): regenerate the Example 2 row with SQL L2 through UnivMon

With the join and CASE guarantee rules, the SQL Q3 UnivMon plans are
valid: 12 of the 82 plans are valid (Q3 exact or UnivMon), 70 invalid
(Q1 HLL or UnivMon without a distinct bound, Q2 UnivMon without an
entropy bound). P1 (all exact) is still selected at 6.08 per second;
UnivMon on Q3 costs 8.08.
fix(physical-operators): charge UnivMon key memory without walking its heaps

A build charges a state's memory after every input row, and UnivMon's
charge summed the bytes of every key on every layer's heap, so building
was quadratic in the heap: a 20,000-key heap on 19 layers over 40,000
keys took 3.3 s in release. The charge now bounds the retained keys by
every heap slot holding the longest key seen (tracked on insert, merge
and adoption of a decoded sketch), in constant time. A test checks that
the charge does not grow with heap occupancy; a timing test (ignored,
release) checks the large build.
fix(logical-asap-optimization): summary-capability review fixes

- A capability group of more than 63 builds is an error: one 64-bit mask
  enumerates the subsets of at most 63, and the shift was clamped, so a
  larger group was enumerated wrongly (and took 2^63 steps) instead of
  refused.
- A summary-capability variant whose guarantee composition fails carries
  its own error, so Stage 1 lists it as a candidate that cannot be
  composed and keeps the remaining variants; one failure used to drop
  them all.
- shared_size_params no longer claims every sizing bound is monotone (a
  Count Sketch's collision bound is tighter at a power-of-two width than
  one column wider); the shared state is checked by composing every
  guarantee above it again.
fix(logical-asap-optimization): read each column's guarantee in a projection over a join

A projection's guarantee assumed the error lies only in its child's
sample-value column (the column named "value", or the only numeric
one). Over a join that column can be an exact input's, so the
approximate input's columns were read as exact: projecting
sum(m) * 1000 beside the exact side's value composed to the
approximate side's absolute error 1, while the truth is 1000.

A projection over a join (directly, or through projections, filters,
sorts, limits or deduplication) now reads each column's own guarantee
(column_guarantees), and a projection over a computed projection over a
join, whose columns' guarantees are unknown, has none. Plan selection's
operand-domain lookup reads columns the same way. column_guarantees and
join_guarantee document their assumption that every approximate node of
Stage 1 has an exact row set. Per-column tracking in general is #689.
fix(logical-asap-optimization): refuse summary-capability groups of more than 20 builds

Every subset of two or more builds of a group is a candidate, 2^n of
them; past 20 builds (about a million) the rule returns an error naming
the limit instead of enumerating.
fix(physical-operators): bound UnivMon key memory by the key bytes inserted

Charging every heap slot at the longest key seen overcounts badly: one
1 KB key in a sketch of 19 layers of 20,000 slots was charged 380 MB.
Each layer's heap keeps at most one copy of a key, so the retained keys
are also bounded by the layers times the bytes of every key inserted
(or merged in); the charge takes the smaller bound, still in constant
time. asap_sketchlib exposes no eviction hook for an exact running
total.
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