ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
agg_token.c
Go to the documentation of this file.
1/**
2 * @file agg_token.c
3 * @brief PostgreSQL I/O functions and cast for the @c agg_token composite type.
4 *
5 * Implements the three SQL-callable C functions that back the
6 * @c agg_token type:
7 * - @c agg_token_in() – text → agg_token (input function)
8 * - @c agg_token_out() – agg_token → text (output function)
9 * - @c agg_token_cast() – agg_token → text (cast, extracts the UUID part)
10 *
11 * The on-wire text format is @c ( UUID , value ) where @c UUID is the
12 * 36-character hyphenated UUID of the provenance gate and @c value is
13 * the aggregate running value.
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 "agg_token.h"
24
25PG_FUNCTION_INFO_V1(agg_token_in);
26/**
27 * @brief Parse an @c agg_token value from its text representation.
28 *
29 * Expected format: @c "( UUID , value )" with a single space around the
30 * comma and at the outer parentheses. Raises @c ERROR on malformed input.
31 * @return Pointer to the newly allocated @c agg_token.
32 */
33Datum
34agg_token_in(PG_FUNCTION_ARGS)
35{
36 char *str = PG_GETARG_CSTRING(0);
37 agg_token* result;
38 const unsigned toklen=sizeof(result->tok)-1;
39 unsigned vallen;
40
41 result = (agg_token *)palloc(sizeof(agg_token));
42
43 // str is ( UUID , string ) with UUID starting at 2 and with length
44 // 20 (2*UUID-LEN=16) plus 4 hashes; then three characters we can
45 // ignore (two spaces and comma) then the string then two ignored
46 // spaces at the end
47 if(strlen(str)<toklen+7 ||
48 str[0]!='(' || str[1]!=' ' || str[2+toklen] != ' ' || str[2+toklen+1] != ','
49 || str[2+toklen+2] != ' ' || str[strlen(str)-2] != ' '
50 || str[strlen(str)-1] != ')')
51 ereport(ERROR,
52 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
53 errmsg("invalid input syntax for agg_token: \"%s\"",
54 str)));
55
56 strncpy(result->tok, str+2, toklen);
57 result->tok[toklen]='\0';
58
59 vallen=strlen(str)-toklen-2-3-2;
60 if(vallen>=sizeof(result->val))
61 vallen=sizeof(result->val)-1;
62 strncpy(result->val, str+2+toklen+3, vallen);
63 result->val[vallen]='\0';
64
65 PG_RETURN_POINTER(result);
66}
67
68PG_FUNCTION_INFO_V1(agg_token_out);
69/**
70 * @brief Produce a human-readable display string for an @c agg_token.
71 *
72 * Returns @c "value (*)", i.e. the running value followed by @c " (*)".
73 * This is the output used by @c EXPLAIN and direct @c CAST to text.
74 * @return C-string representation of the agg_token value.
75 */
76Datum
77agg_token_out(PG_FUNCTION_ARGS)
78{
79 agg_token *aggtok = (agg_token *) PG_GETARG_POINTER(0);
80 char *result;
81
82 result = psprintf("%s (*)", aggtok->val);
83
84 PG_RETURN_CSTRING(result);
85}
86
87PG_FUNCTION_INFO_V1(agg_token_cast);
88/**
89 * @brief Cast an @c agg_token to @c text, returning only the UUID part.
90 *
91 * This is used when the caller needs the provenance circuit UUID
92 * stored in the token rather than the aggregate value.
93 * @return Text datum containing the UUID string of the token.
94 */
95Datum
96agg_token_cast(PG_FUNCTION_ARGS)
97{
98 agg_token *aggtok = (agg_token *) PG_GETARG_POINTER(0);
99 char *result;
100 text *txt_result;
101 int len;
102
103 result = psprintf("%s", aggtok->tok);
104 len = strlen(result);
105
106 txt_result = (text *) palloc(len + ((int32) sizeof(int32)));
107
108 SET_VARSIZE(txt_result, len + ((int32) sizeof(int32)));
109 memcpy(VARDATA(txt_result), result, len);
110
111 PG_RETURN_TEXT_P(txt_result);
112}
Datum agg_token_out(PG_FUNCTION_ARGS)
Produce a human-readable display string for an agg_token.
Definition agg_token.c:77
Datum agg_token_cast(PG_FUNCTION_ARGS)
Cast an agg_token to text, returning only the UUID part.
Definition agg_token.c:96
Datum agg_token_in(PG_FUNCTION_ARGS)
Parse an agg_token value from its text representation.
Definition agg_token.c:34
Aggregate-provenance token type used in SQL aggregate functions.
Core types, constants, and utilities shared across ProvSQL.
Aggregate token bundling a provenance UUID with a running value.
Definition agg_token.h:29
char val[80]
Aggregate running value as a text string.
Definition agg_token.h:31
char tok[2 *UUID_LEN+5]
Provenance UUID as a text string.
Definition agg_token.h:30