ProvSQL SQL API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
Aggregate provenance

Functions for building and evaluating aggregate (GROUP BY) provenance, including the δ-semiring operator and semimodule multiplication. More...

Functions

UUID provsql.provenance_delta (UUID token)
 Create a δ-semiring gate wrapping a provenance token.
AGG_TOKEN provsql.provenance_aggregate (INTEGER aggfnoid, INTEGER aggtype, ANYELEMENT val, UUID[] tokens, BOOLEAN is_scalar=false)
 Build an aggregate provenance gate from grouped tokens.
UUID provsql.provenance_semimod (ANYELEMENT val, UUID token)
 Create a semimodule scalar multiplication gate.
UUID provsql.provenance_semimod_nested (AGG_TOKEN val, UUID token)
 The contribution of an aggregate result to an aggregate of another kind over it (internal).
UUID[] provsql.provenance_semimod_flat (AGG_TOKEN val, UUID token)
 The contributions of an aggregate result to an aggregate of the same kind over it (internal).
VOID provsql.provenance_contributions_cat (UUID[])
 Concatenation of the arrays of contributions of provenance_semimod_flat (internal).
UUID provsql.provenance_semimod_nullable (ANYELEMENT val, UUID token)
 Semimodule gate for an aggregate that sees its NULL inputs.
AGG_TOKEN provsql.row_number_as_rank (AGG_TOKEN rank, bigint row_number, BOOLEAN tied=false)
 The rank of a row standing for its row number (internal).
internal provsql.order_determined_transfn (internal, INTEGER, "any")
 Transition function of order_determined (internal).
BOOLEAN provsql.order_determined_finalfn (internal)
 Final function of order_determined (internal).
VOID provsql.order_determined (nvals INTEGER, VARIADIC "any")
 Whether an order-dependent aggregate reads its rows in an order the query determines (internal).
ANYELEMENT provsql.order_checked (ANYELEMENT val, BOOLEAN determined, BOOLEAN is_window=false)
 The value of an order-dependent aggregate, with a warning where its order is not determined (internal).
AGG_TOKEN provsql.ntile_as_rank (AGG_TOKEN bucket, bigint sql_bucket)
 The bucket of a row read over its rank, for ntile() (internal).
UUID provsql.agg_token_null_event (AGG_TOKEN aggtok, BOOLEAN want_null)
 The event that an aggregate result is NULL, or is not (internal).
BOOLEAN provsql.agg_count_can_be_zero (AGG_TOKEN cnt)
 Whether a count may be 0 where its group is there (internal).
UUID provsql.agg_gate_null_event (UUID g, BOOLEAN want_null)
 The event that the value of gate g is NULL, or is not (internal).
UUID[] provsql.window_distinct_tokens (anyarray vals, UUID[] tokens)
 The contributions of the distinct values of a window frame (internal).
ANYELEMENT provsql.plain (ANYELEMENT value)
 Mark a part of a query to be evaluated as plain SQL, not tracked.

Detailed Description

Functions for building and evaluating aggregate (GROUP BY) provenance, including the δ-semiring operator and semimodule multiplication.

Function Documentation

◆ agg_count_can_be_zero()

BOOLEAN provsql.agg_count_can_be_zero ( AGG_TOKEN cnt)

Whether a count may be 0 where its group is there (internal).

The companion count(arg) the explosion of an aggregate value gives a grouped aggregate has one contribution per row of the group, 1 where arg has a value and 0 where it is NULL: the aggregate can be NULL only if one row contributes 0. Its rows are all there, those the query keeps only for their provenance included, which a count of the data would miss.

Parameters
cntthe count
Source code
provsql.sql line 8188

◆ agg_gate_null_event()

UUID provsql.agg_gate_null_event ( UUID g,
BOOLEAN want_null )

The event that the value of gate g is NULL, or is not (internal).

An aggregate is NULL where none of the rows its gate holds is there (a row whose value is NULL contributes nothing); arithmetic is NULL where one of its operands is; a value, where it is NULL. See agg_token_null_event.

Parameters
gthe gate
want_nulltrue for IS NULL, false for IS NOT NULL
Source code
provsql.sql line 8210

◆ agg_token_null_event()

UUID provsql.agg_token_null_event ( AGG_TOKEN aggtok,
BOOLEAN want_null )

The event that an aggregate result is NULL, or is not (internal).

Called by the query rewriter for an IS [NOT] NULL on an aggregate column of a subquery, where the aggregate's own call is not at hand. A row whose value is NULL contributes nothing to the aggregate's gate, so, the row of the aggregate being there, its value is NULL exactly when none of the rows its gate holds is: 1 ⊖ ⊕K over their tokens, and ⊕K for IS NOT NULL. A NULL aggtok (a padded row) is NULL.

Parameters
aggtokthe aggregate result
want_nulltrue for IS NULL, false for IS NOT NULL
Source code
provsql.sql line 8163

◆ ntile_as_rank()

AGG_TOKEN provsql.ntile_as_rank ( AGG_TOKEN bucket,
bigint sql_bucket )

The bucket of a row read over its rank, for ntile() (internal).

Called by the query rewriter for ntile(n) OVER (…) over provenance-tracked relations, whose bucket is computed from the rank of the row: rows that tie on the ORDER BY share a bucket, where SQL may split them. Returns bucket, with a warning, once per statement, when sql_bucket differs from it.

Parameters
bucketthe AGG_TOKEN of the tracked bucket
sql_bucketthe bucket PostgreSQL gave the row
Source code
provsql.sql line 8146

◆ order_checked()

ANYELEMENT provsql.order_checked ( ANYELEMENT val,
BOOLEAN determined,
BOOLEAN is_window = false )

The value of an order-dependent aggregate, with a warning where its order is not determined (internal).

Returns val, with a warning, once per statement, when determined (from order_determined) is false: the order of the rows in the database as it is is the one read in every world. is_window for a window function (lag, first_value, ...), which warns as such.

Source code
provsql.sql line 8129

◆ order_determined()

VOID provsql.order_determined ( nvals INTEGER,
VARIADIC "any"  )

Whether an order-dependent aggregate reads its rows in an order the query determines (internal).

Called by the query rewriter beside array_agg, string_agg, the json_agg family and xmlagg over provenance-tracked relations, with the aggregate's own arguments (the first nvals ones) and ORDER BY, over every row of the group, those present only in other worlds included: false when two rows tie on the ORDER BY (or there is none) with different values.

Source code
provsql.sql line 8114

◆ order_determined_finalfn()

BOOLEAN provsql.order_determined_finalfn ( internal )

Final function of order_determined (internal).

Source code
provsql.sql line 8099

◆ order_determined_transfn()

internal provsql.order_determined_transfn ( internal ,
INTEGER ,
"any"  )

Transition function of order_determined (internal).

Source code
provsql.sql line 8094

◆ plain()

ANYELEMENT provsql.plain ( ANYELEMENT value)

Mark a part of a query to be evaluated as plain SQL, not tracked.

Over provenance-tracked relations, ORDER BY … LIMIT k keeps, in each possible world, the rows that fewer than k present rows precede: every row that may be among them is output, annotated with that condition. LIMIT plain(k) (FETCH FIRST plain(k) ROWS, OFFSET plain(m)) instead truncates the result as computed on the actual data, and each row kept carries its provenance in the full result. The function returns its argument.

Parameters
valuethe marked value
Source code
provsql.sql line 8318

◆ provenance_aggregate()

AGG_TOKEN provsql.provenance_aggregate ( INTEGER aggfnoid,
INTEGER aggtype,
ANYELEMENT val,
UUID[] tokens,
BOOLEAN is_scalar = false )

Build an aggregate provenance gate from grouped tokens.

Called internally by the query rewriter for GROUP BY queries. Creates an agg gate linking all contributing tokens and records the aggregate function OID and the computed scalar value. The gate's address hashes everything it records, result type and value included: what is recorded is written once, and two aggregations that differ only there (a polymorphic aggregate over two types with the same texts, a floating-point sum rounded differently by two plans) must be two gates.

Parameters
aggfnoidOID of the SQL aggregate function
aggtypeOID of the aggregate result type
valcomputed aggregate value
tokensarray of provenance tokens being aggregated
is_scalartrue for a scalar (no GROUP BY) aggregation, whose output row exists even when no tuple is present; stored in the high bit of info2

Implemented in C (gate_builders.c), with the declared cost of the PL/pgSQL function it was, so that plans stay the same.

Source code
provsql.sql line 7990

◆ provenance_contributions_cat()

VOID provsql.provenance_contributions_cat ( UUID[] )

Concatenation of the arrays of contributions of provenance_semimod_flat (internal).

Source code
provsql.sql line 8049

◆ provenance_delta()

UUID provsql.provenance_delta ( UUID token)

Create a δ-semiring gate wrapping a provenance token.

Used internally for aggregate provenance. Returns the token unchanged if it is gate_zero() or gate_one(), and gate_one() if the token is NULL.

Implemented in C (gate_builders.c), with the declared cost of the PL/pgSQL function it was, so that plans stay the same.

◆ provenance_semimod()

UUID provsql.provenance_semimod ( ANYELEMENT val,
UUID token )

Create a semimodule scalar multiplication gate.

Pairs a scalar value with a provenance token, used internally by the query rewriter for aggregate provenance.

Parameters
valthe scalar value
tokenthe provenance token to multiply

Implemented in C (gate_builders.c), with the declared cost of the PL/pgSQL function it was, so that plans stay the same.

Source code
provsql.sql line 8011

◆ provenance_semimod_flat()

UUID[] provsql.provenance_semimod_flat ( AGG_TOKEN val,
UUID token )

The contributions of an aggregate result to an aggregate of the same kind over it (internal).

For a row of token token whose value val is the result of sum / count / max / min over a group, aggregated again by sum / max / min: the contributions semimod(v_i, token ⊗ k_i) of the group's own contributions semimod(v_i, k_i), so that sum(sum(x)) is sum(x) over the rows of the groups – semimodule scalar multiplication, in every semiring. Collected by provenance_contributions_cat.

Source code
provsql.sql line 8043

◆ provenance_semimod_nested()

UUID provsql.provenance_semimod_nested ( AGG_TOKEN val,
UUID token )

The contribution of an aggregate result to an aggregate of another kind over it (internal).

For a row of token token whose value val is the result of an aggregate of another kind than the one aggregating it (an avg of a count, a max of a sum, an aggregate of an arithmetic expression over aggregates): semimod(g, token), where g is the inner aggregate's own gate. Its value is one per possible world, not one value of the database, so the evaluators that read a value per world resolve it and the closed forms decline it.

Source code
provsql.sql line 8027

◆ provenance_semimod_nullable()

UUID provsql.provenance_semimod_nullable ( ANYELEMENT val,
UUID token )

Semimodule gate for an aggregate that sees its NULL inputs.

Variant of provenance_semimod() used by the query rewriter for array_agg, json_agg and the like, whose result lists every input, NULLs included: a NULL value still yields a semimod gate, over the constant value gate gate_null().

Parameters
valthe scalar value, possibly NULL
tokenthe provenance token to multiply

Implemented in C (gate_builders.c), with the declared cost of the PL/pgSQL function it was, so that plans stay the same.

Source code
provsql.sql line 8069

◆ row_number_as_rank()

AGG_TOKEN provsql.row_number_as_rank ( AGG_TOKEN rank,
bigint row_number,
BOOLEAN tied = false )

The rank of a row standing for its row number (internal).

Called by the query rewriter for row_number() OVER (…) over provenance-tracked relations, which is tracked as rank(): the two are equal when the ORDER BY of the window leaves no ties. Returns rank, with a warning, once per statement, when row_number differs from it.

Parameters
rankthe AGG_TOKEN of the rank of the row
row_numberthe row number of the row among those of the database as it is, or NULL for a row absent from it (no check then)
tiedwhether the row ties on the ORDER BY with another row of any world, which warns as well
Source code
provsql.sql line 8088

◆ window_distinct_tokens()

UUID[] provsql.window_distinct_tokens ( anyarray vals,
UUID[] tokens )

The contributions of the distinct values of a window frame (internal).

Called by the query rewriter for dense_rank() OVER (…) over provenance-tracked relations, which counts the distinct ordering values before the current row: one provenance_semimod(1, ⊕ tokens) per distinct value of vals, the ⊕ of the tokens of the rows that have it, which is present when one of them is. Values are compared with the equality of their type, as the peers of a window are, NULLs being equal.

Parameters
valsthe ordering values of the rows of the frame
tokensthe provenance tokens of these rows, in the same order
Source code
provsql.sql line 8297