![]() |
ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
|
SQL function provsql.rv_histogram(token, bins). More...
#include "postgres.h"#include "fmgr.h"#include "utils/jsonb.h"#include "utils/fmgrprotos.h"#include "utils/uuid.h"#include "provsql_utils.h"#include "provsql_error.h"#include "CircuitFromMMap.h"#include "Expectation.h"#include "GenericCircuit.h"#include "MonteCarloSampler.h"#include "RandomVariable.h"#include "RangeCheck.h"#include "provsql_utils_cpp.h"#include <algorithm>#include <cmath>#include <iomanip>#include <optional>#include <sstream>#include <string>#include <tuple>#include <utility>#include <vector>
Go to the source code of this file.
Functions | |
| Datum | rv_histogram (PG_FUNCTION_ARGS) |
SQL function provsql.rv_histogram(token, bins).
Returns an empirical histogram of the scalar values produced by the sub-circuit rooted at token, binned into bins equal-width intervals between the observed minimum and maximum. The sample count is taken from provsql.rv_mc_samples; pinning provsql.monte_carlo_seed makes the result reproducible.
Output: a jsonb array of objects {bin_lo, bin_hi, count}, one per non-empty bin (zero-count bins are still emitted so the client can draw a flat baseline). Returning jsonb rather than SETOF record keeps the C++ side free of SRF / FuncCallContext mechanics and matches the pattern used by simplified_circuit_subgraph.
Accepted root gate types:
gate_value: degenerate Dirac at the constant. Emits a single bin at (v, v) with count equal to the would-be sample count, so the client can normalise to a probability mass without a special case.gate_rv: sampled from the leaf's distribution.gate_arith: sampled by recursing through the arithmetic DAG, reusing gate_rv draws within an iteration so shared leaves are correctly correlated.gate_mixture: sampled by recursing through the mixture's Bernoulli (gate_input) wire and the selected scalar branch, reusing per-iteration caches so a shared p_token across the circuit produces coupled draws.gate_agg: sampled per Monte Carlo iteration by walking the gate_semimod children, applying their Boolean filter, and finalising the chosen aggregator (COUNT / SUM / AVG / MIN / MAX). Same machinery probability evaluation uses for HAVING under MC; see MonteCarloSampler::evalScalar.gate_semimod: a bare per-row contribution interpreted as value · 1_{k fires}, i.e. zero in worlds where the row's Boolean filter does not fire and the scalar value otherwise.Any other gate type still raises: Boolean-valued gates fall under probability_evaluate, and unsupported scalar shapes (delta…) have no defined sampling semantics.
Definition in file RvHistogram.cpp.
| Datum rv_histogram | ( | PG_FUNCTION_ARGS | ) |