ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
CmpEvaluatorCommon.h
Go to the documentation of this file.
1/**
2 * @file CmpEvaluatorCommon.h
3 * @brief Shared machinery for the closed-form HAVING @c gate_cmp
4 * probability evaluators (Poisson-binomial COUNT, MIN / MAX…).
5 *
6 * Every closed-form HAVING evaluator that runs as a probability-side
7 * pre-pass (see @c probability_evaluate.cpp) shares three concerns:
8 *
9 * - matching the canonical shape
10 * @c gate_cmp(gate_agg(α, semimod_i(K_i, m_i)*), gate_value(C))
11 * in either operand order (@c matchAggCmp) ;
12 * - certifying that the @c gate_agg children are mutually independent
13 * Bernoulli contributors with leaves private to the cmp's subtree,
14 * via the reference-count walk (@c computeRefCounts) ;
15 * - computing each contributor's read-once marginal probability
16 * (@c contributorProb).
17 *
18 * These were first written for @c CountCmpEvaluator; they are factored
19 * here so the MIN / MAX (and future SUM) evaluators reuse the exact same
20 * soundness contract rather than re-deriving it. See
21 * @c CountCmpEvaluator.h for the soundness argument in full.
22 */
23#ifndef PROVSQL_CMP_EVALUATOR_COMMON_H
24#define PROVSQL_CMP_EVALUATOR_COMMON_H
25
26#include <vector>
27
28#include "Aggregation.h" // AggregationOperator + ComparisonOperator
29#include "GenericCircuit.h"
30
31namespace provsql {
32
33/**
34 * @brief Result of matching a @c gate_cmp against the canonical HAVING
35 * aggregate-comparison shape.
36 *
37 * Populated by @c matchAggCmp. The per-evaluator soundness checks (ref
38 * counts on @c agg / @c semimods, read-once marginals over @c ks) are
39 * left to the caller; this struct only carries the syntactic match.
40 */
42 gate_t agg{}; ///< the @c gate_agg operand of the cmp
43 std::vector<gate_t> semimods; ///< the per-child @c gate_semimod parents
44 std::vector<gate_t> ks; ///< the K side of each semimod (contributor root)
45 std::vector<long> ms; ///< the M side of each semimod (per-row value), scaled to a common integer grid (numeric / decimal-float domains; see @c matchAggCmp)
46 AggregationOperator agg_kind{}; ///< effective aggregate (SUM-of-1s remapped to COUNT)
47 ComparisonOperator op{}; ///< comparator, flipped if the agg sits on the right
48 long C{}; ///< the constant threshold, on the same integer grid as @c ms
49};
50
51/**
52 * @brief Try to match @p cmp against
53 * @c gate_cmp(gate_agg(α, semimod_i(K_i, m_i)*), gate_value(C)).
54 *
55 * Accepts both operand orders (agg left or right), flipping @c op in the
56 * latter case. Mirrors @c pw_from_cmp_gate's @c build_from and the
57 * SUM-of-1s → COUNT remap. Returns @c false (leaving @p out untouched)
58 * on any shape mismatch; cheap to call.
59 *
60 * @param[in] gc Circuit to inspect.
61 * @param[in] cmp Candidate @c gate_cmp.
62 * @param[out] out Filled on success.
63 * @return @c true iff the shape matched.
64 */
65bool matchAggCmp(GenericCircuit &gc, gate_t cmp, AggCmpMatch &out);
66
67/**
68 * @brief Reference count of every gate as a wire-target across the whole
69 * circuit. One pass over all wire lists; @c O(total wires).
70 */
71std::vector<unsigned> computeRefCounts(const GenericCircuit &gc);
72
73/**
74 * @brief Read-once marginal probability of a count/aggregate contributor
75 * (the K side of a semimod).
76 *
77 * Exact precisely when the contributor's sub-circuit is a private
78 * read-once tree: every randomness-bearing gate it visits must have
79 * reference count 1. That single condition gives pairwise-disjoint leaf
80 * sets across contributors, no reuse outside the cmp, and read-once-ness
81 * within a contributor. Supports @c input / @c times / @c plus /
82 * @c monus and the @c one / @c zero constants; clears @p ok on any other
83 * gate type or on a reference-count violation. See
84 * @c CountCmpEvaluator.h for the full argument.
85 */
86double contributorProb(const GenericCircuit &gc, gate_t g,
87 const std::vector<unsigned> &ref, bool &ok);
88
89} // namespace provsql
90
91#endif // PROVSQL_CMP_EVALUATOR_COMMON_H
Typed aggregation value, operator, and aggregator abstractions.
AggregationOperator
SQL aggregation functions tracked by ProvSQL.
Definition Aggregation.h:51
ComparisonOperator
SQL comparison operators used in gate_cmp circuit gates.
Definition Aggregation.h:39
gate_t
Strongly-typed gate identifier.
Definition Circuit.h:49
Semiring-agnostic in-memory provenance circuit.
In-memory provenance circuit with semiring-generic evaluation.
std::vector< unsigned > computeRefCounts(const GenericCircuit &gc)
Reference count of every gate as a wire-target across the whole circuit.
bool matchAggCmp(GenericCircuit &gc, gate_t cmp, AggCmpMatch &out)
Try to match cmp against gate_cmp(gate_agg(α, semimod_i(K_i, m_i)*), gate_value(C)).
double contributorProb(const GenericCircuit &gc, gate_t g, const std::vector< unsigned > &ref, bool &ok)
Read-once marginal probability of a count/aggregate contributor (the K side of a semimod).
Result of matching a gate_cmp against the canonical HAVING aggregate-comparison shape.
gate_t agg
the gate_agg operand of the cmp
long C
the constant threshold, on the same integer grid as ms
std::vector< gate_t > ks
the K side of each semimod (contributor root)
std::vector< gate_t > semimods
the per-child gate_semimod parents
std::vector< long > ms
the M side of each semimod (per-row value), scaled to a common integer grid (numeric / decimal-float ...
AggregationOperator agg_kind
effective aggregate (SUM-of-1s remapped to COUNT)
ComparisonOperator op
comparator, flipped if the agg sits on the right