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

Typed aggregation value, operator, and aggregator abstractions. More...

#include "postgres.h"
#include "c_cpp_compatibility.h"
#include <variant>
#include <cmath>
#include <string>
#include <vector>
#include <cassert>
#include <memory>
Include dependency graph for Aggregation.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  AggValue
 A dynamically-typed aggregate value. More...
struct  Aggregator
 Abstract interface for an incremental aggregate accumulator. More...

Enumerations

enum class  ComparisonOperator {
  EQ , NE , LE , LT ,
  GE , GT
}
 SQL comparison operators used in gate_cmp circuit gates. More...
enum class  AggregationOperator {
  COUNT , SUM , MIN , MAX ,
  AVG , AND , OR , CHOOSE ,
  ARRAY_AGG , NONE
}
 SQL aggregation functions tracked by ProvSQL. More...
enum class  ArithmeticOperator {
  PLUS , TIMES , MINUS , DIV ,
  INTDIV , NEG , MAX , MIN ,
  POW , LN , EXP , PERCENTILE ,
  ROUND , FLOOR , CEIL , ABS ,
  AS_FLOAT8 , AS_FLOAT4
}
 Arithmetic operations carried by gate_arith circuit gates. More...
enum class  ValueType {
  INT , FLOAT , BOOLEAN , STRING ,
  ARRAY_INT , ARRAY_FLOAT , ARRAY_BOOLEAN , ARRAY_STRING ,
  NONE
}
 Runtime type tag for aggregate values. More...

Functions

template<class T>
bool sql_less (T a, T b)
 The order PostgreSQL gives numbers: for floats, NaN equals NaN and is greater than every other value (where IEEE comparisons are all false).
template<>
bool sql_less< double > (double a, double b)
 sql_less for double, with PostgreSQL's place for NaN.
AggregationOperator getAggregationOperator (Oid oid)
 Map a PostgreSQL aggregate function OID to an AggregationOperator.
ComparisonOperator cmpOpFromOid (Oid op_oid, bool &ok)
 Map a PostgreSQL comparison-operator OID to a ComparisonOperator.
ArithmeticOperator arithOpFromTag (unsigned tag, bool &ok)
 Map a gate_arith operator tag to an ArithmeticOperator.
std::unique_ptr< Aggregator > makeAggregator (AggregationOperator op, ValueType t)
 Create a concrete Aggregator for the given operator and value type.

Detailed Description

Typed aggregation value, operator, and aggregator abstractions.

This header provides the type system used by ProvSQL's aggregate provenance evaluation:

  • ComparisonOperator: the six standard SQL comparison operators, used by gate_cmp gates in the circuit.
  • AggregationOperator: the SQL aggregation functions that ProvSQL tracks provenance for (COUNT, SUM, MIN, MAX, AVG, AND, OR…).
  • ValueType: the runtime type tag for aggregate values.
  • AggValue: a tagged union holding one aggregate value of any supported type, built on std::variant.
  • Aggregator: an abstract interface for stateful incremental accumulators, one per aggregation function/type combination.

The free functions getAggregationOperator() and makeAggregator() map PostgreSQL OIDs and operator/type pairs to the corresponding C++ objects.

Definition in file Aggregation.h.

Enumeration Type Documentation

◆ AggregationOperator

enum class AggregationOperator
strong

SQL aggregation functions tracked by ProvSQL.

Enumerator
COUNT 

COUNT(*) or COUNT(expr) → integer.

SUM 

SUM → integer or float.

MIN 

MIN → input type.

MAX 

MAX → input type.

AVG 

AVG → float.

AND 

Boolean AND aggregate.

OR 

Boolean OR aggregate.

CHOOSE 

Arbitrary selection (pick one element).

ARRAY_AGG 

Array aggregation.

NONE 

No aggregation (returns NULL).

Definition at line 52 of file Aggregation.h.

◆ ArithmeticOperator

enum class ArithmeticOperator
strong

Arithmetic operations carried by gate_arith circuit gates.

The C++ mirror of the provsql_arith_op on-disk tag stored in the gate's info1 (see provsql_utils.h), decoupled from it so that the semiring interface – which is where these reach a circuit evaluator – stays free of the PostgreSQL headers, exactly as ComparisonOperator is decoupled from the comparison-operator OID. arithOpFromTag() performs the translation.

Enumerator
PLUS 

n-ary sum

TIMES 

n-ary product

MINUS 

binary difference

DIV 

binary quotient

INTDIV 

binary quotient truncated toward zero (integer division)

NEG 

unary negation

MAX 

n-ary maximum (order statistic)

MIN 

n-ary minimum (order statistic)

POW 

binary power

LN 

unary natural logarithm

EXP 

unary exponential

PERCENTILE 

continuous percentile over interleaved [indicator, value] wires

ROUND 

rounding, to a number of digits given by a second wire

FLOOR 

unary floor

CEIL 

unary ceiling

ABS 

unary absolute value

AS_FLOAT8 

unary: the value as double precision reads it

AS_FLOAT4 

unary: the value as real reads it

Definition at line 75 of file Aggregation.h.

◆ ComparisonOperator

enum class ComparisonOperator
strong

SQL comparison operators used in gate_cmp circuit gates.

Enumerator
EQ 

Equal (=).

NE 

Not equal (<>).

LE 

Less than or equal (<=).

LT 

Less than (<).

GE 

Greater than or equal (>=).

GT 

Greater than (>).

Definition at line 40 of file Aggregation.h.

◆ ValueType

enum class ValueType
strong

Runtime type tag for aggregate values.

Enumerator
INT 

Signed 64-bit integer.

FLOAT 

Double-precision float.

BOOLEAN 

Boolean.

STRING 

Text string.

ARRAY_INT 

Array of integers.

ARRAY_FLOAT 

Array of floats.

ARRAY_BOOLEAN 

Array of booleans.

ARRAY_STRING 

Array of strings.

NONE 

No value (NULL).

Definition at line 99 of file Aggregation.h.

Function Documentation

◆ arithOpFromTag()

ArithmeticOperator arithOpFromTag ( unsigned tag,
bool & ok )

Map a gate_arith operator tag to an ArithmeticOperator.

The tag is the provsql_arith_op value stored in gate_arith's info1 field.

Parameters
[in]tagThe persisted operator tag.
[out]okSet to true on a recognised tag, false when tag is outside the enumeration (a circuit written by a newer version of the extension).
Returns
The matching ArithmeticOperator on success; an unspecified value (currently PLUS) when ok is false.

Definition at line 94 of file Aggregation.cpp.

Here is the caller graph for this function:

◆ cmpOpFromOid()

ComparisonOperator cmpOpFromOid ( Oid op_oid,
bool & ok )

Map a PostgreSQL comparison-operator OID to a ComparisonOperator.

The OID is the one stored in gate_cmp's info1 field (the OID of one of the six standard comparators =, <>, <, <=, >, >=). The translation goes via get_opname() so it is operand-type agnostic.

Parameters
[in]op_oidComparison-operator OID.
[out]okSet to true on a recognised comparator, false when op_oid does not resolve in pg_operator or its name is not one of the six standard ones.
Returns
The matching ComparisonOperator on success; an unspecified value (currently EQ) when ok is false.

Definition at line 72 of file Aggregation.cpp.

Here is the caller graph for this function:

◆ getAggregationOperator()

AggregationOperator getAggregationOperator ( Oid oid)

Map a PostgreSQL aggregate function OID to an AggregationOperator.

Parameters
oidOID of the aggregate function (e.g. F_COUNT_ANY, F_SUM_INT4).
Returns
The corresponding AggregationOperator.

Definition at line 35 of file Aggregation.cpp.

Here is the caller graph for this function:

◆ makeAggregator()

std::unique_ptr< Aggregator > makeAggregator ( AggregationOperator op,
ValueType t )

Create a concrete Aggregator for the given operator and value type.

Parameters
opThe aggregation function to implement.
tThe type of input values that will be accumulated.
Returns
A heap-allocated Aggregator, or nullptr if the combination is not supported.

Definition at line 259 of file Aggregation.cpp.

Here is the caller graph for this function:

◆ sql_less()

template<class T>
bool sql_less ( T a,
T b )
inline

The order PostgreSQL gives numbers: for floats, NaN equals NaN and is greater than every other value (where IEEE comparisons are all false).

Definition at line 116 of file Aggregation.h.

Here is the caller graph for this function:

◆ sql_less< double >()

template<>
bool sql_less< double > ( double a,
double b )
inline

sql_less for double, with PostgreSQL's place for NaN.

Definition at line 118 of file Aggregation.h.