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 std::vector<gate_t> via; ///< the @c gate_arith gates of constant arithmetic between the cmp and @c agg, folded into @c op and @c C
50};
51
52/**
53 * @brief Whether the aggregate of @p match is consumed by its comparison
54 * alone: @c agg and every gate of @c via has reference count 1.
55 */
56bool aggPrivateToCmp(const AggCmpMatch &match, const std::vector<unsigned> &ref);
57
58/**
59 * @brief Try to match @p cmp against
60 * @c gate_cmp(gate_agg(α, semimod_i(K_i, m_i)*), gate_value(C)).
61 *
62 * Accepts both operand orders (agg left or right), flipping @c op in the
63 * latter case. Mirrors @c pw_from_cmp_gate's @c build_from and the
64 * SUM-of-1s → COUNT remap. The aggregate may be under constant
65 * arithmetic (@c agg + c, @c c + agg, @c agg - c, @c c - agg, @c -agg),
66 * which is folded into the comparator and the threshold: an aggregate
67 * computed in a subquery (@c rank(), @c count(*) + 1) and compared in an
68 * enclosing query reaches the comparison that way. Returns @c false
69 * (leaving @p out untouched) on any shape mismatch; cheap to call.
70 *
71 * @param[in] gc Circuit to inspect.
72 * @param[in] cmp Candidate @c gate_cmp.
73 * @param[out] out Filled on success.
74 * @return @c true iff the shape matched.
75 */
76bool matchAggCmp(GenericCircuit &gc, gate_t cmp, AggCmpMatch &out);
77
78/**
79 * @brief Reference count of every gate as a wire-target across the whole
80 * circuit. One pass over all wire lists; @c O(total wires).
81 */
82std::vector<unsigned> computeRefCounts(const GenericCircuit &gc);
83
84/**
85 * @brief Read-once marginal probability of a count/aggregate contributor
86 * (the K side of a semimod).
87 *
88 * Exact precisely when the contributor's sub-circuit is a private
89 * read-once tree: every randomness-bearing gate it visits must have
90 * reference count 1. That single condition gives pairwise-disjoint leaf
91 * sets across contributors, no reuse outside the cmp, and read-once-ness
92 * within a contributor. Supports @c input / @c times / @c plus /
93 * @c monus and the @c one / @c zero constants; clears @p ok on any other
94 * gate type or on a reference-count violation. See
95 * @c CountCmpEvaluator.h for the full argument.
96 */
97double contributorProb(const GenericCircuit &gc, gate_t g,
98 const std::vector<unsigned> &ref, bool &ok);
99
100} // namespace provsql
101
102#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.
bool aggPrivateToCmp(const AggCmpMatch &match, const std::vector< unsigned > &ref)
Whether the aggregate of match is consumed by its comparison alone: agg and every gate of via has ref...
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< gate_t > via
the gate_arith gates of constant arithmetic between the cmp and agg, folded into op and C
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