ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
aggregation_evaluate.c
Go to the documentation of this file.
1/**
2 * @file aggregation_evaluate.c
3 * @brief SQL function @c provsql.aggregation_evaluate() – aggregate provenance evaluation.
4 *
5 * Implements the SQL-callable function that evaluates an aggregate
6 * provenance circuit over a user-supplied semiring. The function is
7 * invoked when the query engine encounters a @c gate_agg / @c gate_semimod
8 * sub-circuit and dispatches to the appropriate SPI-based evaluation helper
9 * in @c Aggregation.cpp.
10 *
11 * All semiring operations (plus, times, monus, delta, agg, semimod) are
12 * passed as PostgreSQL function OIDs so that the semiring is fully
13 * user-defined and extensible from SQL.
14 */
15#include "postgres.h"
16#include "fmgr.h"
17#include "catalog/pg_type.h"
18#include "utils/uuid.h"
19#include "executor/spi.h"
20#include "access/htup_details.h"
21
22#include "provsql_utils.h"
23#include "provsql_shmem.h"
24
25PG_FUNCTION_INFO_V1(aggregation_evaluate);
26
27/** @brief PostgreSQL-callable wrapper for aggregation_evaluate(). */
28Datum aggregation_evaluate(PG_FUNCTION_ARGS)
29{
30 Datum token = PG_GETARG_DATUM(0);
31 Datum token2value = PG_GETARG_DATUM(1);
32 Datum agg_function_final = PG_GETARG_DATUM(2);
33 Datum agg_function = PG_GETARG_DATUM(3);
34 Datum semimod_function = PG_GETARG_DATUM(4);
35 Oid element_type = get_fn_expr_argtype(fcinfo->flinfo, 5);
36 Datum element_one = PG_ARGISNULL(5)?((Datum) 0):PG_GETARG_DATUM(5);
37 Datum plus_function = PG_GETARG_DATUM(6);
38 Datum times_function = PG_GETARG_DATUM(7);
39 Datum monus_function = PG_GETARG_DATUM(8);
40 Datum delta_function = PG_GETARG_DATUM(9);
41 const constants_t constants = get_constants(true);
42
43 bool isnull;
44 Datum result;
45 char nulls[11]={' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
46
47 HeapTuple tuple;
48
49 Datum arguments[11]={token,token2value,agg_function_final,agg_function,semimod_function,element_one,element_type,plus_function,
50 times_function,monus_function,delta_function};
51 Oid argtypes[11];
52
53 if(PG_ARGISNULL(1) || PG_ARGISNULL(2) || PG_ARGISNULL(3) || PG_ARGISNULL(4)
54 || PG_ARGISNULL(5) || PG_ARGISNULL(6) || PG_ARGISNULL(7))
55 PG_RETURN_NULL();
56
57 if(PG_ARGISNULL(8)) // No monus function provided
58 nulls[9]='n';
59
60 if(PG_ARGISNULL(9)) // No delta function provided
61 nulls[10]='n';
62
63 argtypes[0]=constants.OID_TYPE_UUID;
64 argtypes[1]=REGCLASSOID;
65 argtypes[2]=REGPROCOID;
66 argtypes[3]=REGPROCOID;
67 argtypes[4]=REGPROCOID;
68 argtypes[5]=element_type;
69 argtypes[6]=REGTYPEOID;
70 argtypes[7]=REGPROCOID;
71 argtypes[8]=REGPROCOID;
72 argtypes[9]=REGPROCOID;
73 argtypes[10]=REGPROCOID;
74
75 SPI_connect();
76
77 if(SPI_execute_with_args(
78 "SELECT provsql.aggregation_evaluate($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)",
79 11,
80 argtypes,
81 arguments,
82 nulls,
83 true,
84 1) != SPI_OK_SELECT) {
85 provsql_error("Cannot execute real aggregation_evaluate function");
86 }
87
88 tuple = SPI_copytuple(SPI_tuptable->vals[0]);
89 result = heap_getattr(tuple, 1, SPI_tuptable->tupdesc, &isnull);
90
91 SPI_finish();
92
93 if(isnull)
94 PG_RETURN_NULL();
95 else
96 PG_RETURN_DATUM(result);
97}
Datum aggregation_evaluate(PG_FUNCTION_ARGS)
PostgreSQL-callable wrapper for aggregation_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.