37#include "catalog/pg_type.h"
38#include "utils/array.h"
39#include "utils/uuid.h"
45#include <unordered_set>
63 std::unordered_set<gate_t> &out,
64 std::unordered_set<gate_t> &seen)
66 if(!seen.insert(g).second)
88 collect_group_tokens(gc, ch, out, seen);
93 const std::unordered_set<gate_t> &group)
104 std::vector<gate_t> operands;
106 const auto &pw = gc.
getWires(dw[0]);
107 operands.assign(pw.begin(), pw.end());
109 operands.push_back(dw[0]);
112 if(operands.size() != group.size())
115 if(group.find(o) == group.end())
125 const std::unordered_set<gate_t> &group,
126 std::vector<gate_t> &out)
128 if(delta_subsumed_by(gc, g, group))
133 surviving_factors(gc, ch, group, out);
151 if(PG_ARGISNULL(0) || PG_ARGISNULL(1))
155 ArrayType *arr = PG_GETARG_ARRAYTYPE_P(0);
156 pg_uuid_t cmp = *DatumGetUUIDP(PG_GETARG_DATUM(1));
161 if(ARR_NDIM(arr) > 1)
162 provsql_error(
"cmp_surviving_factors: tokens must be a 1-D array");
164 deconstruct_array(arr, UUIDOID, 16,
false,
'c', &elems, &nulls, &nelems);
170 std::vector<pg_uuid_t> roots;
171 roots.push_back(cmp);
172 for(
int i = 0; i < nelems; ++i) {
175 roots.push_back(*DatumGetUUIDP(elems[i]));
178 std::vector<gate_t> gates;
181 std::unordered_set<gate_t> group, seen;
182 collect_group_tokens(gc, gates[0], group, seen);
184 std::vector<Datum> kept;
185 std::unordered_set<gate_t> emitted;
187 for(std::size_t r = 1; r < gates.size(); ++r) {
188 std::vector<gate_t> factors;
189 surviving_factors(gc, gates[r], group, factors);
191 if(!emitted.insert(f).second)
196 kept.push_back(UUIDPGetDatum(p));
201 ArrayType *res = construct_array(kept.data(), (
int) kept.size(),
202 UUIDOID, 16,
false,
'c');
203 PG_RETURN_ARRAYTYPE_P(res);
205 }
catch(
const std::exception &e) {
GenericCircuit getJointCircuit(const std::vector< pg_uuid_t > &tokens, std::vector< gate_t > &gates)
Multi-root variant of getJointCircuit.
Build in-memory circuits from the mmap-backed persistent store.
gate_t
Strongly-typed gate identifier.
Semiring-agnostic in-memory provenance circuit.
std::vector< gate_t > & getWires(gate_t g)
Return a mutable reference to the child-wire list of gate g.
gateType getGateType(gate_t g) const
Return the type of gate g.
uuid getUUID(gate_t g) const
Return the UUID string associated with gate g.
In-memory provenance circuit with semiring-generic evaluation.
Datum cmp_surviving_factors(PG_FUNCTION_ARGS)
cmp_surviving_factors(tokens uuid[], cmp uuid) -> uuid[]
#define provsql_error(fmt,...)
Report a fatal ProvSQL error and abort the current transaction.
Core types, constants, and utilities shared across ProvSQL.
@ gate_arith
n-ary arithmetic gate over scalar-valued children (info1 holds operator tag)
pg_uuid_t string2uuid(const string &source)
Parse a UUID string into a pg_uuid_t.
C++ utility functions for UUID manipulation.