ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
Aggregation.h
Go to the documentation of this file.
1/**
2 * @file Aggregation.h
3 * @brief Typed aggregation value, operator, and aggregator abstractions.
4 *
5 * This header provides the type system used by ProvSQL's aggregate
6 * provenance evaluation:
7 *
8 * - @c ComparisonOperator: the six standard SQL comparison operators,
9 * used by @c gate_cmp gates in the circuit.
10 * - @c AggregationOperator: the SQL aggregation functions that ProvSQL
11 * tracks provenance for (COUNT, SUM, MIN, MAX, AVG, AND, OR…).
12 * - @c ValueType: the runtime type tag for aggregate values.
13 * - @c AggValue: a tagged union holding one aggregate value of any
14 * supported type, built on @c std::variant.
15 * - @c Aggregator: an abstract interface for stateful incremental
16 * accumulators, one per aggregation function/type combination.
17 *
18 * The free functions @c getAggregationOperator() and @c makeAggregator()
19 * map PostgreSQL OIDs and operator/type pairs to the corresponding C++
20 * objects.
21 */
22#ifndef AGGREGATION_H
23#define AGGREGATION_H
24
25extern "C" {
26#include "postgres.h"
27}
28#include "c_cpp_compatibility.h"
29
30#include <variant>
31#include <string>
32#include <vector>
33#include <cassert>
34#include <memory>
35
36/**
37 * @brief SQL comparison operators used in @c gate_cmp circuit gates.
38 */
40 EQ, ///< Equal (=)
41 NE, ///< Not equal (<>)
42 LE, ///< Less than or equal (<=)
43 LT, ///< Less than (<)
44 GE, ///< Greater than or equal (>=)
45 GT ///< Greater than (>)
46};
47
48/**
49 * @brief SQL aggregation functions tracked by ProvSQL.
50 */
52 COUNT, ///< COUNT(*) or COUNT(expr) → integer
53 SUM, ///< SUM → integer or float
54 MIN, ///< MIN → input type
55 MAX, ///< MAX → input type
56 AVG, ///< AVG → float
57 AND, ///< Boolean AND aggregate
58 OR, ///< Boolean OR aggregate
59 CHOOSE, ///< Arbitrary selection (pick one element)
60 ARRAY_AGG, ///< Array aggregation
61 NONE, ///< No aggregation (returns NULL)
62};
63
64/**
65 * @brief Arithmetic operations carried by @c gate_arith circuit gates.
66 *
67 * The C++ mirror of the @c provsql_arith_op on-disk tag stored in the
68 * gate's @c info1 (see @c provsql_utils.h), decoupled from it so that
69 * the semiring interface -- which is where these reach a circuit
70 * evaluator -- stays free of the PostgreSQL headers, exactly as
71 * @c ComparisonOperator is decoupled from the comparison-operator OID.
72 * @c arithOpFromTag() performs the translation.
73 */
75 PLUS, ///< n-ary sum
76 TIMES, ///< n-ary product
77 MINUS, ///< binary difference
78 DIV, ///< binary quotient
79 INTDIV, ///< binary quotient truncated toward zero (integer division)
80 NEG, ///< unary negation
81 MAX, ///< n-ary maximum (order statistic)
82 MIN, ///< n-ary minimum (order statistic)
83 POW, ///< binary power
84 LN, ///< unary natural logarithm
85 EXP, ///< unary exponential
86 PERCENTILE, ///< continuous percentile over interleaved [indicator, value] wires
87 ROUND, ///< rounding, to a number of digits given by a second wire
88 FLOOR, ///< unary floor
89 CEIL, ///< unary ceiling
90 ABS, ///< unary absolute value
91 AS_FLOAT8, ///< unary: the value as double precision reads it
92 AS_FLOAT4 ///< unary: the value as real reads it
93};
94
95/**
96 * @brief Runtime type tag for aggregate values.
97 */
98enum class ValueType {
99 INT, ///< Signed 64-bit integer
100 FLOAT, ///< Double-precision float
101 BOOLEAN, ///< Boolean
102 STRING, ///< Text string
103 ARRAY_INT, ///< Array of integers
104 ARRAY_FLOAT, ///< Array of floats
105 ARRAY_BOOLEAN,///< Array of booleans
106 ARRAY_STRING, ///< Array of strings
107 NONE ///< No value (NULL)
108};
109
110/**
111 * @brief A dynamically-typed aggregate value.
112 *
113 * Wraps a @c std::variant of all supported scalar and array types.
114 * The active alternative is identified by the @c ValueType tag returned
115 * by @c getType().
116 */
117struct AggValue {
118private:
119 ValueType t; ///< Active type tag
120
121public:
122 /** @brief The variant holding the actual value. */
123 std::variant<long, double, bool, std::string,
124 std::vector<long>, std::vector<double>, std::vector<bool>, std::vector<std::string> > v;
125
126 /** @brief Construct a NULL (NONE) value. */
128 }
129 /** @brief Construct an integer value. @param l Integer value. */
130 AggValue(long l) : t(ValueType::INT), v(l) {
131 }
132 /** @brief Construct a float value. @param d Float value. */
133 AggValue(double d) : t(ValueType::FLOAT), v(d) {
134 }
135 /** @brief Construct a boolean value. @param b Boolean value. */
136 AggValue(bool b) : t(ValueType::BOOLEAN), v(b) {
137 }
138 /** @brief Construct a string value. @param s String value. */
139 AggValue(std::string s) : t(ValueType::STRING), v(s) {
140 }
141 /** @brief Construct an integer-array value. @param vec Integer array. */
142 AggValue(std::vector<long> vec) : t(ValueType::ARRAY_INT), v(vec) {
143 }
144 /** @brief Construct a float-array value. @param vec Float array. */
145 AggValue(std::vector<double> vec) : t(ValueType::ARRAY_FLOAT), v(vec) {
146 }
147 /** @brief Construct a boolean-array value. @param vec Boolean array. */
148 AggValue(std::vector<bool> vec) : t(ValueType::ARRAY_BOOLEAN), v(vec) {
149 }
150 /** @brief Construct a string-array value. @param vec String array. */
151 AggValue(std::vector<std::string> vec) : t(ValueType::ARRAY_STRING), v(vec) {
152 }
153
154 /**
155 * @brief Return the runtime type tag of this value.
156 * @return The @c ValueType identifying the active alternative.
157 */
159 return t;
160 }
161};
162
163/**
164 * @brief Abstract interface for an incremental aggregate accumulator.
165 *
166 * Each concrete subclass implements one aggregation function for one
167 * input type (e.g., SUM over integers, MAX over floats). Instances are
168 * created by @c makeAggregator().
169 */
171 virtual ~Aggregator() = default;
172
173 /**
174 * @brief Incorporate one input value into the running aggregate.
175 * @param x Input value to add.
176 */
177 virtual void add(const AggValue& x) = 0;
178
179 /**
180 * @brief Return the final aggregate result.
181 * @return The accumulated aggregate as an @c AggValue.
182 */
183 virtual AggValue finalize() const = 0;
184
185 /**
186 * @brief Return the type of the input values accepted by @c add().
187 * @return The @c ValueType of values passed to @c add().
188 *
189 * Used (via the @c resultType() default) to report the aggregate's
190 * result type; see @c makeAggregator() for the operators actually built.
191 */
192 virtual ValueType inputType() const = 0;
193
194 /**
195 * @brief Return the type of the value returned by @c finalize().
196 *
197 * Defaults to @c inputType(); override when the result type differs
198 * (e.g., AVG returns FLOAT regardless of the input type).
199 * @return The @c ValueType of the value returned by @c finalize().
200 */
201 virtual ValueType resultType() const {
202 return inputType();
203 }
204};
205
206/**
207 * @brief Map a PostgreSQL aggregate function OID to an @c AggregationOperator.
208 *
209 * @param oid OID of the aggregate function (e.g. @c F_COUNT_ANY, @c F_SUM_INT4).
210 * @return The corresponding @c AggregationOperator.
211 */
213
214/**
215 * @brief Map a PostgreSQL comparison-operator OID to a @c ComparisonOperator.
216 *
217 * The OID is the one stored in @c gate_cmp's @c info1 field (the OID of
218 * one of the six standard comparators @c =, @c <>, @c <, @c <=, @c >,
219 * @c >=). The translation goes via @c get_opname() so it is operand-type
220 * agnostic.
221 *
222 * @param[in] op_oid Comparison-operator OID.
223 * @param[out] ok Set to @c true on a recognised comparator, @c false
224 * when @p op_oid does not resolve in @c pg_operator
225 * or its name is not one of the six standard ones.
226 * @return The matching @c ComparisonOperator on success; an
227 * unspecified value (currently @c EQ) when @p ok is @c false.
228 */
229ComparisonOperator cmpOpFromOid(Oid op_oid, bool &ok);
230
231/**
232 * @brief Map a @c gate_arith operator tag to an @c ArithmeticOperator.
233 *
234 * The tag is the @c provsql_arith_op value stored in @c gate_arith's
235 * @c info1 field.
236 *
237 * @param[in] tag The persisted operator tag.
238 * @param[out] ok Set to @c true on a recognised tag, @c false when
239 * @p tag is outside the enumeration (a circuit written
240 * by a newer version of the extension).
241 * @return The matching @c ArithmeticOperator on success; an
242 * unspecified value (currently @c PLUS) when @p ok is @c false.
243 */
244ArithmeticOperator arithOpFromTag(unsigned tag, bool &ok);
245
246/**
247 * @brief Create a concrete @c Aggregator for the given operator and value type.
248 *
249 * @param op The aggregation function to implement.
250 * @param t The type of input values that will be accumulated.
251 * @return A heap-allocated @c Aggregator, or @c nullptr if the combination
252 * is not supported.
253 */
254std::unique_ptr<Aggregator> makeAggregator(AggregationOperator op, ValueType t);
255
256#endif /* AGGREGATION_H */
ArithmeticOperator arithOpFromTag(unsigned tag, bool &ok)
Map a gate_arith operator tag to an ArithmeticOperator.
AggregationOperator
SQL aggregation functions tracked by ProvSQL.
Definition Aggregation.h:51
@ OR
Boolean OR aggregate.
Definition Aggregation.h:58
@ MAX
MAX → input type.
Definition Aggregation.h:55
@ COUNT
COUNT(*) or COUNT(expr) → integer.
Definition Aggregation.h:52
@ AND
Boolean AND aggregate.
Definition Aggregation.h:57
@ SUM
SUM → integer or float.
Definition Aggregation.h:53
@ ARRAY_AGG
Array aggregation.
Definition Aggregation.h:60
@ NONE
No aggregation (returns NULL).
Definition Aggregation.h:61
@ CHOOSE
Arbitrary selection (pick one element).
Definition Aggregation.h:59
@ AVG
AVG → float.
Definition Aggregation.h:56
ComparisonOperator
SQL comparison operators used in gate_cmp circuit gates.
Definition Aggregation.h:39
@ LT
Less than (<).
Definition Aggregation.h:43
@ GT
Greater than (>).
Definition Aggregation.h:45
@ LE
Less than or equal (<=).
Definition Aggregation.h:42
@ NE
Not equal (<>).
Definition Aggregation.h:41
@ GE
Greater than or equal (>=).
Definition Aggregation.h:44
ComparisonOperator cmpOpFromOid(Oid op_oid, bool &ok)
Map a PostgreSQL comparison-operator OID to a ComparisonOperator.
AggregationOperator getAggregationOperator(Oid oid)
Map a PostgreSQL aggregate function OID to an AggregationOperator.
std::unique_ptr< Aggregator > makeAggregator(AggregationOperator op, ValueType t)
Create a concrete Aggregator for the given operator and value type.
ValueType
Runtime type tag for aggregate values.
Definition Aggregation.h:98
@ ARRAY_INT
Array of integers.
@ ARRAY_BOOLEAN
Array of booleans.
@ INT
Signed 64-bit integer.
Definition Aggregation.h:99
@ STRING
Text string.
@ ARRAY_FLOAT
Array of floats.
@ ARRAY_STRING
Array of strings.
@ BOOLEAN
Boolean.
@ FLOAT
Double-precision float.
ArithmeticOperator
Arithmetic operations carried by gate_arith circuit gates.
Definition Aggregation.h:74
@ POW
binary power
Definition Aggregation.h:83
@ DIV
binary quotient
Definition Aggregation.h:78
@ ROUND
rounding, to a number of digits given by a second wire
Definition Aggregation.h:87
@ PERCENTILE
continuous percentile over interleaved [indicator, value] wires
Definition Aggregation.h:86
@ FLOOR
unary floor
Definition Aggregation.h:88
@ CEIL
unary ceiling
Definition Aggregation.h:89
@ NEG
unary negation
Definition Aggregation.h:80
@ INTDIV
binary quotient truncated toward zero (integer division)
Definition Aggregation.h:79
@ ABS
unary absolute value
Definition Aggregation.h:90
@ EXP
unary exponential
Definition Aggregation.h:85
@ TIMES
n-ary product
Definition Aggregation.h:76
@ AS_FLOAT8
unary: the value as double precision reads it
Definition Aggregation.h:91
@ AS_FLOAT4
unary: the value as real reads it
Definition Aggregation.h:92
@ LN
unary natural logarithm
Definition Aggregation.h:84
@ MINUS
binary difference
Definition Aggregation.h:77
Fix macro conflicts between PostgreSQL headers and the C++ STL/Boost.
A dynamically-typed aggregate value.
AggValue(std::vector< bool > vec)
Construct a boolean-array value.
ValueType getType() const
Return the runtime type tag of this value.
AggValue(std::vector< long > vec)
Construct an integer-array value.
AggValue()
Construct a NULL (NONE) value.
AggValue(long l)
Construct an integer value.
ValueType t
Active type tag.
AggValue(std::vector< std::string > vec)
Construct a string-array value.
AggValue(double d)
Construct a float value.
AggValue(std::string s)
Construct a string value.
AggValue(bool b)
Construct a boolean value.
std::variant< long, double, bool, std::string, std::vector< long >, std::vector< double >, std::vector< bool >, std::vector< std::string > > v
The variant holding the actual value.
AggValue(std::vector< double > vec)
Construct a float-array value.
Abstract interface for an incremental aggregate accumulator.
virtual void add(const AggValue &x)=0
Incorporate one input value into the running aggregate.
virtual AggValue finalize() const =0
Return the final aggregate result.
virtual ValueType inputType() const =0
Return the type of the input values accepted by add().
virtual ~Aggregator()=default
virtual ValueType resultType() const
Return the type of the value returned by finalize().
#define MIN(x, y)
Return the minimum of two values.
Definition subset.cpp:73