ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
random_variable_type.c
Go to the documentation of this file.
1/**
2 * @file random_variable_type.c
3 * @brief PostgreSQL I/O and constructor functions for the @c random_variable type.
4 *
5 * @c random_variable is a thin wrapper around a provenance gate UUID,
6 * used in user-facing SQL for continuous probabilistic c-tables.
7 *
8 * On-disk and on-wire text format: the canonical
9 * @c xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx UUID form (identical to
10 * PostgreSQL's @c uuid). A binary-coercible @c random_variable
11 * -> @c uuid cast (declared @c WITHOUT @c FUNCTION since the two
12 * types share their byte layout) keeps SQL ergonomics: an
13 * @c rv-typed column flows directly into any function that accepts
14 * a @c uuid at zero runtime cost.
15 */
16#include "postgres.h"
17#include "fmgr.h"
18#include "utils/uuid.h"
19#include "utils/fmgrprotos.h"
20
21/**
22 * @brief Binary internal layout of @c random_variable.
23 *
24 * 16 bytes UUID, no padding, @c TYPALIGN_CHAR (matching @c pg_uuid_t
25 * exactly) so a binary-coercible @c CREATE @c CAST is sound -- a
26 * cast site reinterprets the same bytes without a runtime
27 * conversion function.
28 */
29typedef struct random_variable {
30 pg_uuid_t tok; ///< Provenance gate UUID
32
33PG_FUNCTION_INFO_V1(random_variable_in);
34/**
35 * @brief Parse a @c random_variable from its text representation.
36 *
37 * Expected format: the standard hyphenated UUID
38 * @c xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, delegated to @c uuid_in.
39 * Raises @c ERROR on malformed input.
40 */
41Datum
42random_variable_in(PG_FUNCTION_ARGS)
43{
44 char *str = PG_GETARG_CSTRING(0);
45 Datum uuid_d = DirectFunctionCall1(uuid_in, CStringGetDatum(str));
46 random_variable *result = (random_variable *) palloc(sizeof(random_variable));
47 memcpy(&result->tok, DatumGetUUIDP(uuid_d), sizeof(pg_uuid_t));
48 PG_RETURN_POINTER(result);
49}
50
51PG_FUNCTION_INFO_V1(random_variable_out);
52/**
53 * @brief Render a @c random_variable as a hyphenated UUID string.
54 */
55Datum
56random_variable_out(PG_FUNCTION_ARGS)
57{
58 random_variable *rv = (random_variable *) PG_GETARG_POINTER(0);
59 return DirectFunctionCall1(uuid_out, UUIDPGetDatum(&rv->tok));
60}
61
62PG_FUNCTION_INFO_V1(random_variable_make);
63/**
64 * @brief Build a @c random_variable from a UUID.
65 *
66 * Internal constructor used by the @c provsql.normal /
67 * @c provsql.uniform / @c provsql.exponential / @c provsql.as_random
68 * SQL constructors after they have created the backing gate.
69 * Equivalent in effect to the binary-coercible @c uuid -> @c random_variable
70 * cast (both reinterpret the same 16 bytes), but kept as a named
71 * function so the constructors read uniformly with the rest of the
72 * SQL surface.
73 */
74Datum
75random_variable_make(PG_FUNCTION_ARGS)
76{
77 pg_uuid_t *tok = PG_GETARG_UUID_P(0);
78 random_variable *result = (random_variable *) palloc(sizeof(random_variable));
79 memcpy(&result->tok, tok, sizeof(pg_uuid_t));
80 PG_RETURN_POINTER(result);
81}
Datum random_variable_in(PG_FUNCTION_ARGS)
Parse a random_variable from its text representation.
Datum random_variable_out(PG_FUNCTION_ARGS)
Render a random_variable as a hyphenated UUID string.
Datum random_variable_make(PG_FUNCTION_ARGS)
Build a random_variable from a UUID.
UUID structure.
Binary internal layout of random_variable.
pg_uuid_t tok
Provenance gate UUID.