ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
agg_token.c File Reference

PostgreSQL I/O functions and cast for the agg_token composite type. More...

#include "postgres.h"
#include "fmgr.h"
#include "catalog/pg_type.h"
#include "catalog/pg_collation.h"
#include "utils/uuid.h"
#include "utils/numeric.h"
#include "utils/fmgrprotos.h"
#include "utils/builtins.h"
#include "access/xact.h"
#include "executor/spi.h"
#include "access/htup_details.h"
#include "utils/datum.h"
#include "utils/typcache.h"
#include "utils/lsyscache.h"
#include "provsql_utils.h"
#include "agg_token.h"
Include dependency graph for agg_token.c:

Go to the source code of this file.

Classes

struct  order_determined_state
 Transition state of order_determined. More...

Functions

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.
const char * agg_token_value_cstring (const agg_token *aggtok)
 The value of aggtok, as a C string.
Datum agg_token_in (PG_FUNCTION_ARGS)
 Parse an agg_token value from its text representation.
Datum agg_token_out (PG_FUNCTION_ARGS)
 Produce a display string for an agg_token.
Datum agg_token_cast (PG_FUNCTION_ARGS)
 Cast an agg_token to text, returning only the UUID part.
static bool agg_token_val_is_null (const agg_token *aggtok)
 True when an agg_token value string cannot carry a number.
static void warn_conversion_once (const char *target)
 The "provenance information is lost" warning of a conversion, once for a statement and for each target type.
static void warn_ordering_once (void)
 The "sorted on the plain value" warning, once for a statement.
static bool text_is_number (const char *v)
 Whether v is the text of a number, as numeric_in would read it: the check a comparison makes before parsing, since parsing what is not one would raise where a comparison must not.
Datum agg_token_btree_cmp (PG_FUNCTION_ARGS)
 Order two agg_token values by the value each carries.
Datum agg_token_to_numeric (PG_FUNCTION_ARGS)
 Cast an agg_token to numeric, extracting only the value.
Datum agg_token_value (PG_FUNCTION_ARGS)
 Extract the running value of an agg_token as numeric.
Datum agg_token_to_float8 (PG_FUNCTION_ARGS)
 Cast an agg_token to double precision, extracting only the value.
Datum agg_token_to_int4 (PG_FUNCTION_ARGS)
 Cast an agg_token to integer, extracting only the value.
Datum agg_token_to_int8 (PG_FUNCTION_ARGS)
 Cast an agg_token to bigint, extracting only the value.
Datum agg_token_to_bool (PG_FUNCTION_ARGS)
 Cast an agg_token to boolean, extracting only the value.
Datum agg_token_to_text (PG_FUNCTION_ARGS)
 Cast an agg_token to text, extracting only the value.
Datum agg_token_plain_text (PG_FUNCTION_ARGS)
 The value of an agg_token as text, NULL for a NULL value, without the "provenance information is lost" warning.
Datum ntile_as_rank (PG_FUNCTION_ARGS)
 The agg_token of the bucket ntile() gives a row, read over its rank.
Datum row_number_as_rank (PG_FUNCTION_ARGS)
 The agg_token of rank() standing for row_number().
static bool same_value (Datum a, Datum b, int16 typlen, bool typbyval)
 Whether two values of the same type are the same value, written the same way: equal bytes, after decompression for a varlena.
Datum order_determined_transfn (PG_FUNCTION_ARGS)
 Transition of order_determined(nvals, v_1, ..., v_nvals, k_1, ..., k_m): reads the rows of a group in the order of its sort keys k, and records whether two consecutive rows tie on the keys (are peers) with different values v.
Datum order_determined_finalfn (PG_FUNCTION_ARGS)
 Final function of order_determined: whether no two peers have different values.
Datum order_checked (PG_FUNCTION_ARGS)
 The value of an order-dependent aggregate, with a warning, once per statement, where determined says its order is not determined.

Detailed Description

PostgreSQL I/O functions and cast for the agg_token composite type.

Implements the three SQL-callable C functions that back the agg_token type:

The on-wire text format is ( UUID , value ) where UUID is the 36-character hyphenated UUID of the provenance gate and value is the aggregate running value.

Definition in file agg_token.c.

Function Documentation

◆ agg_token_btree_cmp()

Datum agg_token_btree_cmp ( PG_FUNCTION_ARGS )

Order two agg_token values by the value each carries.

An agg_token is a value and a provenance, and the value is the one the data as it is gives: ordering by it – ORDER BY, DISTINCT, a GROUP BY that sorts – is ordering by that one value, which another possible world need not agree with. The warning says so, once for the statement rather than once per comparison, since a sort makes n log n of them.

A token with no value (an aggregate over no row) sorts first, as a NULL does under NULLS FIRST; two numbers are compared as numbers and anything else by its text, so that the order is total whatever the aggregate's own type.

Definition at line 318 of file agg_token.c.

Here is the call graph for this function:

◆ agg_token_cast()

Datum agg_token_cast ( PG_FUNCTION_ARGS )

Cast an agg_token to text, returning only the UUID part.

This is used when the caller needs the provenance circuit UUID stored in the token rather than the aggregate value.

Returns
Text datum containing the UUID string of the token.

Definition at line 186 of file agg_token.c.

◆ agg_token_in()

Datum agg_token_in ( PG_FUNCTION_ARGS )

Parse an agg_token value from its text representation.

Expected format: "( UUID , value )" with a single space around the comma and at the outer parentheses. Raises ERROR on malformed input.

Returns
Pointer to the newly allocated agg_token.

Definition at line 100 of file agg_token.c.

Here is the call graph for this function:

◆ agg_token_out()

Datum agg_token_out ( PG_FUNCTION_ARGS )

Produce a display string for an agg_token.

Default: returns "value (*)" (the running value followed by " (*)"), matching EXPLAIN and direct CAST to text. Where the value is SQL NULL it is "(*)" alone: SQL writes nothing for a NULL, so writing "NULL" would put four characters where a reader expects none, and a text client would read them as data.

When the provsql.aggtoken_text_as_uuid GUC is on, returns the underlying provenance UUID instead. This is the form ProvSQL Studio enables per session so agg_token cells in a result table expose the circuit root UUID for click-through; the user-facing "value (*)" string is recovered via the provsql.agg_token_value_text(uuid) helper.

Returns
C-string representation of the agg_token.

Definition at line 151 of file agg_token.c.

Here is the call graph for this function:

◆ agg_token_plain_text()

Datum agg_token_plain_text ( PG_FUNCTION_ARGS )

The value of an agg_token as text, NULL for a NULL value, without the "provenance information is lost" warning.

The internal accessor of the rewriter's sort keys: an ORDER BY on an aggregate result sorts on this value, read in the aggregate's type, while the column itself keeps its agg_token.

Definition at line 526 of file agg_token.c.

Here is the call graph for this function:

◆ agg_token_set_value()

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.

A value too long for agg_token::val keeps its first AGG_TOKEN_PREFIX_LEN bytes, marked by AGG_TOKEN_TRUNCATED in the last byte; aggtok must be zeroed.

Definition at line 40 of file agg_token.c.

Here is the caller graph for this function:

◆ agg_token_to_bool()

Datum agg_token_to_bool ( PG_FUNCTION_ARGS )

Cast an agg_token to boolean, extracting only the value.

The value of a boolean aggregate (bool_or, bool_and, every). Emits a WARNING that provenance information is lost during the conversion.

Returns
Boolean datum parsed from the aggregate value string.

Definition at line 476 of file agg_token.c.

Here is the call graph for this function:

◆ agg_token_to_float8()

Datum agg_token_to_float8 ( PG_FUNCTION_ARGS )

Cast an agg_token to double precision, extracting only the value.

Emits a WARNING that provenance information is lost during the conversion.

Returns
Float8 datum parsed from the aggregate value string.

Definition at line 406 of file agg_token.c.

Here is the call graph for this function:

◆ agg_token_to_int4()

Datum agg_token_to_int4 ( PG_FUNCTION_ARGS )

Cast an agg_token to integer, extracting only the value.

Emits a WARNING that provenance information is lost during the conversion.

Returns
Int32 datum parsed from the aggregate value string.

Definition at line 429 of file agg_token.c.

Here is the call graph for this function:

◆ agg_token_to_int8()

Datum agg_token_to_int8 ( PG_FUNCTION_ARGS )

Cast an agg_token to bigint, extracting only the value.

Emits a WARNING that provenance information is lost during the conversion.

Returns
Int64 datum parsed from the aggregate value string.

Definition at line 452 of file agg_token.c.

Here is the call graph for this function:

◆ agg_token_to_numeric()

Datum agg_token_to_numeric ( PG_FUNCTION_ARGS )

Cast an agg_token to numeric, extracting only the value.

Emits a WARNING that provenance information is lost during the conversion.

Returns
Numeric datum parsed from the aggregate value string.

Definition at line 354 of file agg_token.c.

Here is the call graph for this function:

◆ agg_token_to_text()

Datum agg_token_to_text ( PG_FUNCTION_ARGS )

Cast an agg_token to text, extracting only the value.

Unlike agg_token_cast which returns the UUID part, this returns the aggregate value as text. Emits a WARNING that provenance information is lost during the conversion.

Returns
Text datum containing the aggregate value string.

Definition at line 498 of file agg_token.c.

Here is the call graph for this function:

◆ agg_token_val_is_null()

bool agg_token_val_is_null ( const agg_token * aggtok)
static

True when an agg_token value string cannot carry a number.

An empty or literal "NULL" value string (an aggregate whose value is SQL NULL) must convert to SQL NULL rather than feed the type-input function a string it rejects.

Definition at line 212 of file agg_token.c.

Here is the caller graph for this function:

◆ agg_token_value()

Datum agg_token_value ( PG_FUNCTION_ARGS )

Extract the running value of an agg_token as numeric.

Unlike agg_token_to_numeric, this does not emit the "provenance information is lost" warning: it is the internal accessor used by the provenance-preserving agg_token arithmetic operators (which keep the provenance in a gate_arith circuit), where no provenance is dropped.

Returns
Numeric datum parsed from the aggregate value string.

Definition at line 383 of file agg_token.c.

Here is the call graph for this function:

◆ agg_token_value_cstring()

const char * agg_token_value_cstring ( const agg_token * aggtok)

The value of aggtok, as a C string.

The value stored in the token, or, when it is only a prefix, the value of its gate, read with provsql.agg_token_value_text; the prefix is returned if the gate gives none that starts with it.

Definition at line 59 of file agg_token.c.

Here is the caller graph for this function:

◆ ntile_as_rank()

Datum ntile_as_rank ( PG_FUNCTION_ARGS )

The agg_token of the bucket ntile() gives a row, read over its rank.

Rows that tie on the ORDER BY of the window share the bucket of their rank, which is the reading of the semantics; SQL numbers them in some order and may split them between two buckets. Its SQL arguments are bucket, the agg_token of the tracked bucket, and sql_bucket, the bucket PostgreSQL gave the row; when the two differ, a warning says so, once per statement.

Returns
bucket.

Definition at line 550 of file agg_token.c.

Here is the call graph for this function:

◆ order_checked()

Datum order_checked ( PG_FUNCTION_ARGS )

The value of an order-dependent aggregate, with a warning, once per statement, where determined says its order is not determined.

The rows tie on its ORDER BY (or it has none) with different values: SQL leaves their order open, and the order of the database as it is is the one read in every world.

Returns
its first argument.

Definition at line 765 of file agg_token.c.

◆ order_determined_finalfn()

Datum order_determined_finalfn ( PG_FUNCTION_ARGS )

Final function of order_determined: whether no two peers have different values.

Definition at line 746 of file agg_token.c.

◆ order_determined_transfn()

Datum order_determined_transfn ( PG_FUNCTION_ARGS )

Transition of order_determined(nvals, v_1, ..., v_nvals, k_1, ..., k_m): reads the rows of a group in the order of its sort keys k, and records whether two consecutive rows tie on the keys (are peers) with different values v.

The aggregate it accompanies reads its rows in an order the query then does not determine. Without sort keys, every row is a peer of every other. Two NULLs are peers, as they sort together.

Definition at line 660 of file agg_token.c.

Here is the call graph for this function:

◆ row_number_as_rank()

Datum row_number_as_rank ( PG_FUNCTION_ARGS )

The agg_token of rank() standing for row_number().

The two are equal when the ORDER BY of the window leaves no ties; with ties, SQL itself does not determine which row gets which number. The rank is what is tracked. Its SQL arguments are rank, the agg_token of the rank of the row, and row_number, the row number of the row among those of the database as it is, or NULL for a row absent from it; when the two differ, or when tied says the row ties with a row of any world, a warning says so, once per statement.

Returns
rank.

Definition at line 583 of file agg_token.c.

Here is the call graph for this function:

◆ same_value()

bool same_value ( Datum a,
Datum b,
int16 typlen,
bool typbyval )
static

Whether two values of the same type are the same value, written the same way: equal bytes, after decompression for a varlena.

Definition at line 636 of file agg_token.c.

Here is the caller graph for this function:

◆ text_is_number()

bool text_is_number ( const char * v)
static

Whether v is the text of a number, as numeric_in would read it: the check a comparison makes before parsing, since parsing what is not one would raise where a comparison must not.

Definition at line 277 of file agg_token.c.

Here is the caller graph for this function:

◆ warn_conversion_once()

void warn_conversion_once ( const char * target)
static

The "provenance information is lost" warning of a conversion, once for a statement and for each target type.

A conversion runs for every row, and so did its warning: a query over a large relation wrote one line per row, which says nothing the first does not and which has filled a server log before. The statement is identified by provsql_stmt_serial, which the executor hook bumps for the user's outermost statement; a conversion outside any statement of the user (a direct call in a function the rewriting runs) warns once as well.

Definition at line 228 of file agg_token.c.

Here is the caller graph for this function:

◆ warn_ordering_once()

void warn_ordering_once ( void )
static

The "sorted on the plain value" warning, once for a statement.

Definition at line 262 of file agg_token.c.

Here is the caller graph for this function: