ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
probability_evaluate.cpp File Reference

SQL function provsql.probability_evaluate() – probabilistic circuit evaluation. More...

#include "postgres.h"
#include "fmgr.h"
#include "catalog/pg_type.h"
#include "miscadmin.h"
#include "storage/latch.h"
#include "utils/uuid.h"
#include "executor/spi.h"
#include "provsql_shmem.h"
#include "provsql_utils.h"
#include "c_cpp_compatibility.h"
#include <set>
#include <stack>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <csignal>
#include "BooleanCircuit.h"
#include "CircuitFromMMap.h"
#include "GenericCircuit.h"
#include "AnalyticEvaluator.h"
#include "CountCmpEvaluator.h"
#include "HybridEvaluator.h"
#include "RangeCheck.h"
#include "MonteCarloSampler.h"
#include "dDNNFTreeDecompositionBuilder.h"
#include "StructuredDNNF.h"
#include "having_semantics.hpp"
#include "provsql_mmap.h"
#include "safe_query_cert.h"
#include "provsql_utils_cpp.h"
#include "tool_registry_sync.h"
#include "semiring/BoolExpr.h"
Include dependency graph for probability_evaluate.cpp:

Go to the source code of this file.

Functions

static void provsql_sigint_handler (int)
 SIGINT handler that sets the global interrupted flag.
static bool collect_inversion_free_keys (const GenericCircuit &gc, gate_t gc_root, const std::unordered_map< gate_t, gate_t > &gc_to_bc, const BooleanCircuit &c, gate_t bc_root, std::map< gate_t, StructuredDNNFBuilder::InputKey > &out)
 Collect the inversion-free per-input order keys for the structured builder.
static std::map< gate_t, int > inversion_free_rank (const std::map< gate_t, StructuredDNNFBuilder::InputKey > &keys)
 Flatten the per-input order keys into a total rank for the structured builder's order-only constructor.
dDNNF buildInversionFreeDDNNF (pg_uuid_t token)
 Compile a query certified inversion-free to its structured d-DNNF.
static Datum probability_evaluate_internal (pg_uuid_t token, const string &method, const string &args)
 Core implementation of probability evaluation for a circuit token.
Datum probability_evaluate (PG_FUNCTION_ARGS)
 PostgreSQL-callable wrapper for probability_evaluate().

Detailed Description

SQL function provsql.probability_evaluate() – probabilistic circuit evaluation.

Implements provsql.probability_evaluate(), which computes the probability that a provenance circuit evaluates to true under the tuple-independent probabilistic-database model.

The method argument selects the computation algorithm:

  • "possible-worlds": exact enumeration of all 2^n worlds.
  • "monte-carlo": approximate via random sampling (fast, inexact).
  • "weightmc": approximate using the weightmc model counter.
  • "tree-decomposition": exact via tree-decomposition-based d-DNNF.
  • "independent": exact evaluation for disconnected circuits.
  • "inversion-free": exact via the structured-d-DNNF builder over the query-derived order; errors unless the root carries an inversion-free certificate. The default method also tries it (after "independent") when a certificate is present.
  • Any external compiler name ("d4", "c2d", "minic2d", "dsharp").

A SIGINT signal sets a process-local flag that causes the evaluation to abort and return NULL (used when the user cancels a long-running probability computation).

Definition in file probability_evaluate.cpp.

Function Documentation

◆ buildInversionFreeDDNNF()

dDNNF buildInversionFreeDDNNF ( pg_uuid_t token)

Compile a query certified inversion-free to its structured d-DNNF.

Builds the same structured d-DNNF the 'inversion-free' probability method uses (over the query-derived Prop. 4.5 order carried on the circuit's annotation markers), for the knowledge-compilation surface (DOT / NNF / stats). Throws CircuitException if token's root carries no inversion-free certificate. Defined in probability_evaluate.cpp.

Definition at line 190 of file probability_evaluate.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ collect_inversion_free_keys()

bool collect_inversion_free_keys ( const GenericCircuit & gc,
gate_t gc_root,
const std::unordered_map< gate_t, gate_t > & gc_to_bc,
const BooleanCircuit & c,
gate_t bc_root,
std::map< gate_t, StructuredDNNFBuilder::InputKey > & out )
static

Collect the inversion-free per-input order keys for the structured builder.

Walks the GenericCircuit gc for K-prefixed annotation gates whose child is a gate_input (the per-input order markers attached by the planner on the certified path), parses each key, and maps the wrapped input to its BooleanCircuit variable via gc_to_bc. Returns true only if every BooleanCircuit input reachable from bc_root carries a key (the structured builder needs a total order over all variables); a missing marker means the certified markers are absent / incomplete and the caller must not use the structured path.

Definition at line 109 of file probability_evaluate.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ inversion_free_rank()

std::map< gate_t, int > inversion_free_rank ( const std::map< gate_t, StructuredDNNFBuilder::InputKey > & keys)
static

Flatten the per-input order keys into a total rank for the structured builder's order-only constructor.

Sorts the certified inputs into a Prop. 4.5-consistent order – root-class value first (one independent block per value), then secondary-class value (one tile per value within a block), then the shared self-join guard before the payloads of its tile, then by factor – and assigns consecutive ranks. Ties (two inputs with identical keys) keep a deterministic order via the input gate id, so distinct variables always get distinct ranks. Unlike the keyed (factored-sweep) constructor this makes no single-secondary-axis / one-payload-per-tile assumption, so it certifies every hierarchical inversion-free lineage, including the self-join-free case.

Definition at line 169 of file probability_evaluate.cpp.

Here is the caller graph for this function:

◆ probability_evaluate()

Datum probability_evaluate ( PG_FUNCTION_ARGS )

PostgreSQL-callable wrapper for probability_evaluate().

Definition at line 549 of file probability_evaluate.cpp.

Here is the call graph for this function:

◆ probability_evaluate_internal()

Datum probability_evaluate_internal ( pg_uuid_t token,
const string & method,
const string & args )
static

Core implementation of probability evaluation for a circuit token.

Parameters
tokenUUID of the root provenance gate.
methodEvaluation method name (e.g. "independent", "monte-carlo").
argsAdditional arguments for the chosen method.
Returns
Float8 Datum containing the computed probability.

Definition at line 221 of file probability_evaluate.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ provsql_sigint_handler()

void provsql_sigint_handler ( int )
static

SIGINT handler that sets the global interrupted flag.

The signal number argument is required by the signal() API but is not used.

In addition to the provsql_interrupted flag polled by the long Monte-Carlo / possible-worlds evaluation loops, we drive PG's standard cancel pipeline (InterruptPending / QueryCancelPending

  • SetLatch) the same way PG's own StatementCancelHandler does. That makes a SIGINT delivered to the backend (e.g. via pg_cancel_backend) outside of a system() wait turn into a proper 57014 cancel at the next CHECK_FOR_INTERRUPTS instead of being silently absorbed. (The matching case where an external compiler is running is handled in run_external_tool, which runs the tool in its own process group and SIGKILLs that group on a pending cancel, then lets CHECK_FOR_INTERRUPTS raise it.)

Definition at line 85 of file probability_evaluate.cpp.

Here is the caller graph for this function: