ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
provenance_evaluate_compiled.hpp
Go to the documentation of this file.
1/**
2 * @file provenance_evaluate_compiled.hpp
3 * @brief Template helper for populating provenance mappings from SPI results.
4 *
5 * When evaluating a provenance circuit over a user-defined semiring, the
6 * first step is to build a @c provenance_mapping that maps each input
7 * gate to its semiring value. The values come from a database table or
8 * temporary view queried via SPI; this template handles the SPI-result
9 * iteration that is common to all semiring types.
10 *
11 * After reading the SPI result set, the function optionally drops the
12 * temporary helper table (if @p drop_table is @c true) and closes the
13 * SPI connection.
14 */
15#ifndef PROVENANCE_EVALUATE_COMPILED_HPP
16#define PROVENANCE_EVALUATE_COMPILED_HPP
17
18extern "C" {
19#include "executor/spi.h"
20}
21
22#include "provsql_utils_cpp.h"
23#include "CircuitFromMMap.h"
24#include "Circuit.hpp"
25
26/** @brief DROP TABLE statement for the per-query temporary provenance mapping table. */
27extern const char *drop_temp_table;
28
29/**
30 * @brief Populate a provenance mapping from the current SPI result set.
31 *
32 * Iterates over the rows returned by the most recent SPI query. Each
33 * row is expected to have two columns:
34 * 1. A value column (column 1) containing the semiring value as text.
35 * 2. A UUID column (column 2) identifying the circuit input gate.
36 *
37 * The @p charp_to_value converter is called on the text of column 1 to
38 * produce the corresponding @c T value.
39 *
40 * @tparam T Semiring value type.
41 * @param constants Cached OID constants (used to verify column types).
42 * @param c The @c GenericCircuit used to resolve gate IDs.
43 * @param provenance_mapping Output map from gate IDs to semiring values;
44 * entries are added by this function.
45 * @param charp_to_value Converter from the column-1 text to a @c T value.
46 * @param drop_table If @c true, execute the @c drop_temp_table SQL
47 * statement after processing all rows.
48 */
49template<typename T>
51 const constants_t &constants,
53 std::unordered_map<gate_t, T> &provenance_mapping,
54 const std::function<T(const char *)> &charp_to_value,
55 bool drop_table
56 )
57{
58 for (uint64 i = 0; i < SPI_processed; i++) {
59 HeapTuple tuple = SPI_tuptable->vals[i];
60 TupleDesc tupdesc = SPI_tuptable->tupdesc;
61
62 if (SPI_gettypeid(tupdesc, 2) != constants.OID_TYPE_UUID) {
63 SPI_finish();
64 throw CircuitException("Invalid type for provenance mapping attribute");
65 continue;
66 }
67
68 char *value = SPI_getvalue(tuple, tupdesc, 1);
69 char *uuid = SPI_getvalue(tuple, tupdesc, 2);
70
71 provenance_mapping[c.getGate(uuid)]=charp_to_value(value);
72
73 pfree(value);
74 pfree(uuid);
75 }
76 if(drop_table)
77 SPI_exec(drop_temp_table, 0);
78 SPI_finish();
79}
80
81#endif /* PROVENANCE_EVALUATE_COMPILED_HPP */
Build in-memory circuits from the mmap-backed persistent store.
Out-of-line template method implementations for Circuit<gateType>.
Exception type thrown by circuit operations on invalid input.
Definition Circuit.h:205
gate_t getGate(const uuid &u)
Return (or create) the gate associated with UUID u.
Definition Circuit.hpp:33
In-memory provenance circuit with semiring-generic evaluation.
void initialize_provenance_mapping(const constants_t &constants, GenericCircuit &c, std::unordered_map< gate_t, T > &provenance_mapping, const std::function< T(const char *)> &charp_to_value, bool drop_table)
Populate a provenance mapping from the current SPI result set.
const char * drop_temp_table
DROP TABLE statement for the per-query temporary provenance mapping table.
C++ utility functions for UUID manipulation.
Structure to store the value of various constants.
Oid OID_TYPE_UUID
OID of the uuid TYPE.