ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
provenance_evaluate.c
Go to the documentation of this file.
1/**
2 * @file provenance_evaluate.c
3 * @brief SQL function @c provsql.provenance_evaluate() – semiring evaluation.
4 *
5 * Implements the SQL-callable function that evaluates a provenance circuit
6 * over a user-supplied (m-)semiring. The semiring is described by six
7 * PostgreSQL function OIDs passed as arguments (plus, times, monus, delta,
8 * element-one, token2value) which are called via SPI to compute the result.
9 *
10 * This is the simpler, non-compiled variant of provenance evaluation.
11 * For the compiled (C++ generic) variant, see @c provenance_evaluate_compiled.cpp.
12 */
13#include "postgres.h"
14#include "fmgr.h"
15#include "catalog/pg_type.h"
16#include "utils/uuid.h"
17#include "executor/spi.h"
18#include "access/htup_details.h"
19
20#include "provsql_shmem.h"
21#include "provsql_utils.h"
22
23PG_FUNCTION_INFO_V1(provenance_evaluate);
24
25/** @brief PostgreSQL-callable wrapper for provenance_evaluate(). */
26Datum provenance_evaluate(PG_FUNCTION_ARGS)
27{
28 Datum token = PG_GETARG_DATUM(0);
29 Datum token2value = PG_GETARG_DATUM(1);
30 Oid element_type = get_fn_expr_argtype(fcinfo->flinfo, 2);
31 Datum element_one = PG_ARGISNULL(2)?((Datum) 0):PG_GETARG_DATUM(2);
32 Datum plus_function = PG_GETARG_DATUM(3);
33 Datum times_function = PG_GETARG_DATUM(4);
34 Datum monus_function = PG_GETARG_DATUM(5);
35 Datum delta_function = PG_GETARG_DATUM(6);
36 constants_t constants = get_constants(true);
37
38 bool isnull;
39 Datum result;
40 char nulls[8]={' ',' ',' ',' ',' ',' ',' ',' '};
41
42 HeapTuple tuple;
43
44 Datum arguments[8]={token,token2value,element_one,element_type,plus_function,times_function,monus_function,delta_function};
45 Oid argtypes[8];
46
47 if(PG_ARGISNULL(1) || PG_ARGISNULL(2) || PG_ARGISNULL(3) || PG_ARGISNULL(4))
48 PG_RETURN_NULL();
49
50 if(PG_ARGISNULL(5)) // No monus function provided
51 nulls[6]='n';
52
53 if(PG_ARGISNULL(6)) // No delta function provided
54 nulls[7]='n';
55
56 argtypes[0]=constants.OID_TYPE_UUID;
57 argtypes[1]=REGCLASSOID;
58 argtypes[2]=element_type;
59 argtypes[3]=REGTYPEOID;
60 argtypes[4]=REGPROCOID;
61 argtypes[5]=REGPROCOID;
62 argtypes[6]=REGPROCOID;
63 argtypes[7]=REGPROCOID;
64
65 SPI_connect();
66
67 if(SPI_execute_with_args(
68 "SELECT provsql.provenance_evaluate($1,$2,$3,$4,$5,$6,$7,$8)",
69 8,
70 argtypes,
71 arguments,
72 nulls,
73 true,
74 1) != SPI_OK_SELECT) {
75 provsql_error("Cannot execute real provenance_evaluate function");
76 }
77
78 tuple = SPI_copytuple(SPI_tuptable->vals[0]);
79 result = heap_getattr(tuple, 1, SPI_tuptable->tupdesc, &isnull);
80
81 SPI_finish();
82
83 if(isnull)
84 PG_RETURN_NULL();
85 else
86 PG_RETURN_DATUM(result);
87}
Datum provenance_evaluate(PG_FUNCTION_ARGS)
PostgreSQL-callable wrapper for provenance_evaluate().
#define provsql_error(fmt,...)
Report a fatal ProvSQL error and abort the current transaction.
Shared-memory segment and inter-process pipe management.
constants_t get_constants(bool failure_if_not_possible)
Retrieve the cached OID constants for the current database.
Core types, constants, and utilities shared across ProvSQL.
Structure to store the value of various constants.
Oid OID_TYPE_UUID
OID of the uuid TYPE.