18#include "catalog/namespace.h"
19#include "catalog/pg_type.h"
20#include "parser/parse_coerce.h"
21#include "lib/stringinfo.h"
22#include "utils/array.h"
23#include "utils/builtins.h"
24#include "utils/hsearch.h"
25#include "utils/lsyscache.h"
26#include "utils/memutils.h"
27#include "utils/uuid.h"
36 0x92, 0x0d, 0x4f, 0x02, 0x87, 0x18, 0x53, 0x19,
37 0x95, 0x32, 0xd4, 0xab, 0x83, 0xa6, 0x44, 0x89
48#define NAME_FLUSH 8192
62static void name_begin(StringInfo buf,
const char *prefix) {
65 appendBinaryStringInfo(buf, (
const char *)
provsql_ns, UUID_LEN);
66 appendStringInfoString(buf, prefix);
71 static const char hex[] =
"0123456789abcdef";
75 for (i = 0; i < UUID_LEN; ++i) {
76 if (i == 4 || i == 6 || i == 8 || i == 10)
78 out[p++] = hex[u->
data[i] >> 4];
79 out[p++] = hex[u->
data[i] & 0xF];
81 appendBinaryStringInfo(buf, out, 36);
88 appendStringInfoChar(buf,
'{');
89 for (i = 0; i < n; ++i) {
91 appendStringInfoChar(buf,
',');
96 appendStringInfoChar(buf,
'}');
101 unsigned char digest[20];
106 memcpy(u.
data, digest, UUID_LEN);
107 u.
data[6] = (
unsigned char)((u.
data[6] & 0x0F) | 0x50);
108 u.
data[8] = (
unsigned char)((u.
data[8] & 0x3F) | 0x80);
115 return *DatumGetUUIDP(DirectFunctionCall1(uuid_in, CStringGetDatum(text)));
120 static bool known =
false;
127 static bool known =
false;
133 return memcmp(a, b, UUID_LEN);
138 memcpy(r, u, UUID_LEN);
139 return UUIDPGetDatum(r);
152 deconstruct_array(arr, UUIDOID, UUID_LEN,
false,
'c', &elems, &nulls, &n);
154 for (i = 0; i < n; ++i) {
158 u = DatumGetUUIDP(elems[i]);
159 if (memcmp(u->
data, neutral->
data, UUID_LEN) == 0)
177 memcpy(sorted, children,
sizeof(
pg_uuid_t) * n);
233 Oid nsp = LookupExplicitNamespace(
"pg_temp",
true);
234 return OidIsValid(nsp) ? get_relname_relid(name, nsp) : InvalidOid;
244 while ((e = (
PlantedEntry *)hash_seq_search(&seq)) != NULL)
249 if (*link == scope) {
268 if (strcmp(s->
name, name) == 0) {
277 found = (
PlantedScope *)MemoryContextAllocZero(TopMemoryContext,
280 strlcpy(found->
name, name, NAMEDATALEN);
289 return hash_search(
planted, address, HASH_FIND, NULL) != NULL;
295 unsigned info1,
unsigned info2) {
306 provsql_error(
"planting a gate outside the lowering of a recursive CTE");
312 true, info1, info2, NULL);
316 memset(&ctl, 0,
sizeof(ctl));
319 ctl.hcxt = TopMemoryContext;
320 planted = hash_create(
"ProvSQL planted gates", 256, &ctl,
321 HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
332 char *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
340 char *name = text_to_cstring(PG_GETARG_TEXT_PP(0));
341 char *type_name = text_to_cstring(PG_GETARG_TEXT_PP(1));
346 memset(¬hing, 0xFF,
sizeof(nothing));
349 name, strcmp(type_name,
"plus") == 0 ?
gate_plus :
351 children, n, PG_GETARG_UUID_P(3),
352 (
unsigned)PG_GETARG_INT32(4), (
unsigned)PG_GETARG_INT32(5));
361 const char *canonical_prefix,
404 for (i = 0; i < n; ++i)
405 if (memcmp(tokens[i].data, neutral->
data, UUID_LEN) != 0)
406 out[kept++] = tokens[i];
478 Oid type = get_fn_expr_argtype(fcinfo->flinfo, argno);
479 Datum val = PG_GETARG_DATUM(argno);
481 if (!OidIsValid(type))
483 if (cast == NULL || cast->
type != type) {
484 Oid funcid = InvalidOid;
487 cast = (
VarcharCast *)MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt,
489 cast->
path = find_coercion_pathway(VARCHAROID, type, COERCION_EXPLICIT,
491 if (cast->
path == COERCION_PATH_FUNC) {
492 fmgr_info_cxt(funcid, &cast->
fn, fcinfo->flinfo->fn_mcxt);
493 cast->
nargs = get_func_nargs(funcid);
494 }
else if (cast->
path != COERCION_PATH_RELABELTYPE) {
496 cast->
path = COERCION_PATH_COERCEVIAIO;
497 getTypeOutputInfo(type, &funcid, &is_varlena);
498 fmgr_info_cxt(funcid, &cast->
fn, fcinfo->flinfo->fn_mcxt);
501 fcinfo->flinfo->fn_extra = cast;
504 switch (cast->
path) {
505 case COERCION_PATH_FUNC: {
507 if (cast->
nargs == 1)
508 r = FunctionCall1(&cast->
fn, val);
509 else if (cast->
nargs == 2)
510 r = FunctionCall2(&cast->
fn, val, Int32GetDatum(-1));
512 r = FunctionCall3(&cast->
fn, val, Int32GetDatum(-1), BoolGetDatum(
true));
513 return text_to_cstring(DatumGetTextPP(r));
515 case COERCION_PATH_RELABELTYPE:
516 return text_to_cstring(DatumGetTextPP(val));
518 return OutputFunctionCall(&cast->
fn, val);
527#define WRITTEN_VALUES_MAX 65536
547 appendStringInfoString(&buf, suffix);
557 memset(&ctl, 0,
sizeof(ctl));
560 ctl.hcxt = TopMemoryContext;
561 written_values = hash_create(
"ProvSQL value gates written", 256, &ctl,
562 HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
564 if (hash_search(
written_values, &token, HASH_FIND, NULL) == NULL) {
581 children[0] = *token;
582 children[1] = *value_token;
589 provsql_error(
"provenance_semimod: the provenance token must not be NULL");
590 return PG_GETARG_UUID_P(1);
656 aggtok = (
agg_token *)PG_GETARG_POINTER(0);
657 inner = *DatumGetUUIDP(DirectFunctionCall1(uuid_in,
658 CStringGetDatum(aggtok->
tok)));
680 PG_RETURN_ARRAYTYPE_P(construct_empty_array(UUIDOID));
681 aggtok = (
agg_token *)PG_GETARG_POINTER(0);
682 agg = *DatumGetUUIDP(DirectFunctionCall1(uuid_in,
683 CStringGetDatum(aggtok->
tok)));
686 provsql_error(
"provenance_semimod_flat: not the result of an aggregate");
689 for (i = 0; i < n; ++i) {
691 pg_uuid_t *wires = NULL, scaled, pair[2];
694 provsql_error(
"provenance_semimod_flat: unexpected contribution");
703 out[i] = *DatumGetUUIDP(
semimod_gate(&wires[1], &scaled));
708 Datum *elems = (Datum *)palloc(
sizeof(Datum) * (n > 0 ? n : 1));
709 for (i = 0; i < n; ++i)
710 elems[i] = UUIDPGetDatum(&out[i]);
711 PG_RETURN_ARRAYTYPE_P(construct_array(elems, (
int)n, UUIDOID, UUID_LEN,
733 int32 aggfnoid, aggtype;
734 bool is_scalar = !PG_ARGISNULL(4) && PG_GETARG_BOOL(4);
740 if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
741 provsql_error(
"provenance_aggregate: the aggregate function and its "
742 "result type must not be NULL");
743 aggfnoid = PG_GETARG_INT32(0);
744 aggtype = PG_GETARG_INT32(1);
745 if (!PG_ARGISNULL(2))
747 if (!PG_ARGISNULL(3)) {
748 memset(¬hing, 0xFF,
sizeof(nothing));
754 if (n == 0 && !is_scalar)
760 appendStringInfo(&buf,
"%d", aggfnoid);
763 appendStringInfoChar(&buf,
'S');
764 appendStringInfo(&buf,
":%d", aggtype);
766 appendStringInfo(&buf,
":%s", val);
769 true, (
unsigned)aggfnoid,
770 is_scalar ? ((
unsigned)aggtype | 0x80000000u)
779 if (val == NULL && n == 0)
786 memcpy(result->
tok, t.data, 36);
791 PG_RETURN_POINTER(result);
795 return memcmp(a->
data, b->
data, UUID_LEN) == 0;
813 (errmsg(
"provenance_monus is called with first argument NULL")));
814 token1 = PG_GETARG_UUID_P(0);
817 token2 = PG_GETARG_UUID_P(1);
835 children[0] = *token1;
836 children[1] = *token2;
852 token = PG_GETARG_UUID_P(0);
879 unsigned n = 0, m = 0, info1 = 0, info2 = 0;
892 double c = strtod(v, &end);
893 result = end != v && *end ==
'\0' && c != 0 && c == c;
912 char *name = get_opname(op);
914 if (name == NULL || strcmp(name,
"=") != 0)
933 if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2))
935 left = PG_GETARG_UUID_P(0);
936 op = PG_GETARG_OID(1);
937 right = PG_GETARG_UUID_P(2);
945 appendStringInfo(&buf,
"%u", op);
949 children[1] = *right;
951 true, (
unsigned)op, 0, NULL);
970 token = PG_GETARG_UUID_P(0);
971 if (!PG_ARGISNULL(1))
972 extra = text_to_cstring(PG_GETARG_TEXT_PP(1));
977 appendStringInfoString(&buf, extra);
990 text *root = PG_GETARG_TEXT_PP(0), *sec = PG_GETARG_TEXT_PP(1);
993 initStringInfo(&buf);
994 appendStringInfo(&buf,
"K%d %d:", PG_GETARG_INT32(2),
995 (
int)VARSIZE_ANY_EXHDR(root));
996 appendBinaryStringInfo(&buf, VARDATA_ANY(root), VARSIZE_ANY_EXHDR(root));
997 appendStringInfo(&buf,
"%d:", (
int)VARSIZE_ANY_EXHDR(sec));
998 appendBinaryStringInfo(&buf, VARDATA_ANY(sec), VARSIZE_ANY_EXHDR(sec));
999 PG_RETURN_TEXT_P(cstring_to_text_with_len(buf.data, buf.len));
1015 if (strcmp(assumption,
"boolean") != 0 && strcmp(assumption,
"absorptive") != 0)
1016 ereport(ERROR, (errmsg(
"provenance_assume: unknown assumption %s", assumption)));
1018 appendStringInfoString(&buf, assumption);
1022 info1 != 0, info1, 0, assumption);
1032 if (PG_ARGISNULL(0))
1034 if (PG_ARGISNULL(1))
1035 ereport(ERROR, (errmsg(
"provenance_assume: unknown assumption NULL")));
1037 text_to_cstring(PG_GETARG_TEXT_PP(1)), 0);
1047 if (PG_ARGISNULL(0))
1062 ArrayType *positions;
1066 StringInfoData buf, extra;
1069 if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
1071 token = PG_GETARG_UUID_P(0);
1072 positions = PG_GETARG_ARRAYTYPE_P(1);
1073 deconstruct_array(positions, INT4OID,
sizeof(int32),
true,
'i',
1074 &elems, &nulls, &n);
1079 appendStringInfoChar(&buf,
'{');
1080 initStringInfo(&extra);
1081 appendStringInfoChar(&extra,
'{');
1082 for (i = 0; i < n; ++i) {
1084 appendStringInfoChar(&buf,
',');
1085 appendStringInfoChar(&extra,
',');
1088 appendStringInfoString(&buf,
"NULL");
1089 appendStringInfo(&extra,
"{NULL,%d}", i + 1);
1091 int32 pos = DatumGetInt32(elems[i]);
1092 appendStringInfo(&buf,
"%d", pos);
1094 appendStringInfo(&extra,
"{NULL,%d}", i + 1);
1096 appendStringInfo(&extra,
"{%d,%d}", pos, i + 1);
1099 appendStringInfoChar(&buf,
'}');
1100 appendStringInfoChar(&extra,
'}');
1103 false, 0, 0, n > 0 ? extra.
data : NULL);
1114 int32 pos1 = PG_ARGISNULL(1) ? 0 : PG_GETARG_INT32(1);
1115 int32 pos2 = PG_ARGISNULL(2) ? 0 : PG_GETARG_INT32(2);
1117 if (PG_ARGISNULL(0))
1119 token = PG_GETARG_UUID_P(0);
1122 if (!PG_ARGISNULL(1))
1123 appendStringInfo(&buf,
"%d", pos1);
1124 appendStringInfoChar(&buf,
',');
1125 if (!PG_ARGISNULL(2))
1126 appendStringInfo(&buf,
"%d", pos2);
1129 true, (
unsigned)pos1, (
unsigned)pos2, NULL);
1138 pg_uuid_t arith, *children = NULL, nothing;
1140 int32 op = PG_ARGISNULL(0) ? 0 : PG_GETARG_INT32(0);
1143 if (!PG_ARGISNULL(0))
1144 appendStringInfo(&buf,
"%d", op);
1145 if (!PG_ARGISNULL(1)) {
1146 ArrayType *arr = PG_GETARG_ARRAYTYPE_P(1);
1147 if (array_contains_nulls(arr))
1148 provsql_error(
"provenance_arith: children array must not contain NULL elements");
1149 memset(¬hing, 0xFF,
sizeof(nothing));
1155 true, (
unsigned)op, 0, NULL);
void provsql_sha1_final(provsql_sha1_ctx *ctx, unsigned char out[20])
The SHA-1 of all the bytes given to ctx.
void provsql_sha1_init(provsql_sha1_ctx *ctx)
Start an incremental SHA-1.
void provsql_sha1_update(provsql_sha1_ctx *ctx, const unsigned char *data, size_t len)
Hash len more bytes of data.
void agg_token_set_value(agg_token *aggtok, const char *val, size_t len)
Set the value of aggtok to the len first bytes of val.
Aggregate-provenance token type used in SQL aggregate functions.
Datum provenance_monus(PG_FUNCTION_ARGS)
token1 ⊖ token2.
Datum provenance_semimod_flat(PG_FUNCTION_ARGS)
The contributions semimod(v_i, token ⊗ k_i) of the aggregate result val, whose gate aggregates the co...
Datum provenance_semimod_nested(PG_FUNCTION_ARGS)
The semimodule gate of a row whose value is itself an aggregate result: semimod(the inner aggregate's...
Datum provenance_times(PG_FUNCTION_ARGS)
⊗ of a list of tokens.
static void name_begin(StringInfo buf, const char *prefix)
Start the name of a gate address.
Datum provenance_arith(PG_FUNCTION_ARGS)
The arith gate applying operator op (a provsql_arith_op, its info1) to children, in order.
static provsql_sha1_ctx name_hash
The hash of the name being built, which takes the buffer's content whenever it grows past NAME_FLUSH ...
static HTAB * written_values
Datum provenance_eq(PG_FUNCTION_ARGS)
The where-provenance eq gate of token, equating positions pos1 and pos2 (its infos).
static pg_uuid_t value_gate(const char *prefix, const char *suffix, const char *extra)
The value gate of the text str, at the address named name ("value" followed by str,...
Datum inversion_free_key(PG_FUNCTION_ARGS)
The order key of an input on the inversion-free route, "K<factor> <bytes of root>:<root><bytes of sec...
pg_uuid_t provsql_plant_canonical(const char *work_name, gate_type type, const pg_uuid_t *children, int n, const pg_uuid_t *target, unsigned info1, unsigned info2)
Pre-create ("plant") a gate at the canonical address of a multiset of tokens, for the working table o...
static const pg_uuid_t * address_of_zero(void)
pg_uuid_t provsql_build_delta(const pg_uuid_t *token)
δ(token), with δ(𝟘) = 𝟘 and δ(𝟙) = 𝟙.
static pg_uuid_t nary_gate(gate_type type, const char *plain_prefix, const char *canonical_prefix, const pg_uuid_t *children, int n)
A gate of type type over children, unless this backend planted one at the canonical address of their ...
Datum annotate(PG_FUNCTION_ARGS)
A transparent annotation gate over token, carrying extra.
static const pg_uuid_t * address_of_one(void)
static pg_uuid_t name_end(StringInfo buf)
The version-5 UUID of the name accumulated in buf.
static pg_uuid_t constant_uuid(const char *text)
The UUID text (one of the constants of provsql_utils.h).
static pg_uuid_t assumed_gate(const pg_uuid_t *token, const char *assumption, unsigned info1)
The assumed wrapper of token under assumption, with info1 as the route tag (0 for none).
Datum provenance_aggregate(PG_FUNCTION_ARGS)
The agg gate of a group, paired with the aggregate's value.
static void name_flush(StringInfo buf)
Hash what buf holds, and empty it.
static Datum semimod_gate(const pg_uuid_t *value_token, const pg_uuid_t *token)
The semimod gate value_token ⊗ token.
static void name_add_uuid_array(StringInfo buf, const pg_uuid_t *u, int n)
Append the text form of a uuid[], "{u1,u2,...}".
Datum provenance_plus(PG_FUNCTION_ARGS)
⊕ of an array of tokens.
static PlantedScope * planted_current
Datum planted_scope(PG_FUNCTION_ARGS)
SQL entry point of planted_open_scope(), called by the drivers of recursive CTEs right after they (re...
void provsql_gate_builders_forget(void)
Forget what gate_builders.c remembers of the store (planted gates, value gates written).
Datum provenance_cmp(PG_FUNCTION_ARGS)
The comparison gate left op right of a HAVING condition.
static int token_cmp(const void *a, const void *b)
Datum provenance_assume(PG_FUNCTION_ARGS)
Wrap token in the assumption marker assumption ('boolean' or 'absorptive').
static int filtered_tokens(ArrayType *arr, const pg_uuid_t *neutral, pg_uuid_t **out)
The elements of a uuid[] that are neither NULL nor neutral, in order.
#define WRITTEN_VALUES_MAX
static pg_uuid_t canonical_address(const char *canonical_prefix, const pg_uuid_t *children, int n)
The canonical address, "<prefix>{sorted children}".
static bool is_planted(const pg_uuid_t *address)
Datum provenance_semimod(PG_FUNCTION_ARGS)
The semimodule gate val ⊗ token of an aggregated row.
static Oid temp_table_oid(const char *name)
OID of the temporary table name of this backend, if any.
static bool is_shift_of(const pg_uuid_t *sum, const pg_uuid_t *base)
Is sum the gate of base plus a nonzero constant?
pg_uuid_t provsql_build_monus(const pg_uuid_t *token1, const pg_uuid_t *token2)
token1 ⊖ token2, with X ⊖ X = 𝟘 ⊖ X = 𝟘 and X ⊖ 𝟘 = X.
static int planted_next_id
Datum assume_boolean(PG_FUNCTION_ARGS)
The Boolean-assumption wrapper the safe-query rewriter puts on every per-row root,...
pg_uuid_t provsql_build_plus(const pg_uuid_t *tokens, int n)
⊕ of n tokens: 𝟘 dropped, none gives 𝟘, one is returned.
static void planted_forget(PlantedScope *scope)
Datum provenance_project(PG_FUNCTION_ARGS)
The where-provenance project gate of token: its text lists, for each output position,...
Datum plant_canonical(PG_FUNCTION_ARGS)
SQL entry point of provsql_plant_canonical().
static Datum uuid_result(const pg_uuid_t *u)
static const unsigned char provsql_ns[UUID_LEN]
uuid_ns_provsql(), the namespace of every gate address.
pg_uuid_t provsql_build_times(const pg_uuid_t *tokens, int n)
⊗ of n tokens: 𝟙 dropped, none gives 𝟙, one is returned.
static int drop_neutral(const pg_uuid_t *tokens, int n, const pg_uuid_t *neutral, pg_uuid_t *out)
Datum provenance_delta(PG_FUNCTION_ARGS)
δ(token).
static bool cmp_impossible(const pg_uuid_t *left, Oid op, const pg_uuid_t *right)
Does left op right hold in no world, by its shape alone?
static const pg_uuid_t * semimod_token_argument(FunctionCallInfo fcinfo)
static PlantedScope * planted_open_scope(const char *name, bool reset)
Open the scope of the working table name, just (re)created: forget what was planted for the table it ...
static PlantedScope * planted_scopes
static char * varchar_of_argument(FunctionCallInfo fcinfo, int argno)
CAST(val AS varchar) of argument argno, as a C string.
static bool same_token(const pg_uuid_t *a, const pg_uuid_t *b)
static void name_add_uuid(StringInfo buf, const pg_uuid_t *u)
Append a UUID in the text form of uuid_out.
Datum provenance_semimod_nullable(PG_FUNCTION_ARGS)
provenance_semimod for the aggregates that see their NULL inputs (array_agg, json_agg,...
The gate builders of the query rewriter, for C/C++ callers.
#define provsql_error(fmt,...)
Report a fatal ProvSQL error and abort the current transaction.
void provsql_internal_create_gate(const pg_uuid_t *token, gate_type type, unsigned nb_children, const pg_uuid_t *children_data)
Internal entry point behind create_gate(): cache + worker IPC.
void provsql_internal_create_gate_with(const pg_uuid_t *token, gate_type type, unsigned nb_children, const pg_uuid_t *children, bool has_infos, unsigned info1, unsigned info2, const char *extra)
Create a gate together with its infos and its text, in one message that is not answered.
gate_type provsql_fetch_gate(const pg_uuid_t *token, unsigned *nb_children_out, pg_uuid_t **children_out)
PostgreSQL-callable wrapper for get_gate_type().
void provsql_internal_get_infos(const pg_uuid_t *token, unsigned *info1, unsigned *info2)
Read a gate's info1 and info2, through the worker.
char * provsql_internal_get_extra(const pg_uuid_t *token)
Read a gate's extra text, through the worker; palloc'd.
Background worker and IPC primitives for mmap-backed circuit storage.
Core types, constants, and utilities shared across ProvSQL.
@ PROVSQL_ARITH_PLUS
n-ary, sum of children
#define PROVSQL_GATE_ZERO_UUID
UUID of the semiring zero gate: the result of the SQL function gate_zero(), created with the extensio...
#define PROVSQL_GATE_ONE_UUID
UUID of the semiring one gate, the result of gate_one().
@ gate_annotation
Transparent single-child wrapper carrying a query-level annotation in extra (inversion-free certifica...
@ gate_arith
n-ary arithmetic gate over scalar-valued children (info1 holds operator tag)
@ gate_assumed
Structural marker over a single child whose sub-circuit was computed under a Boolean-provenance assum...
@ PROVSQL_ROUTE_SQ_REWRITE
Hierarchical-CQ read-once rewrite (src/safe_query.c).
pg_uuid_t address
Hash key.
A working table with planted gates.
struct PlantedScope * next
Oid relid
The table, as created by the lowering.
Aggregate token bundling a provenance UUID with a running value.
char tok[2 *UUID_LEN+5]
Provenance UUID as a text string.
The state of an incremental SHA-1: the digest of a text given in pieces, without holding it whole (th...