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 NEG, ///< unary negation
80 MAX, ///< n-ary maximum (order statistic)
81 MIN, ///< n-ary minimum (order statistic)
82 POW, ///< binary power
83 LN, ///< unary natural logarithm
84 EXP, ///< unary exponential
85 PERCENTILE ///< continuous percentile over interleaved [indicator, value] wires
86};
87
88/**
89 * @brief Runtime type tag for aggregate values.
90 */
91enum class ValueType {
92 INT, ///< Signed 64-bit integer
93 FLOAT, ///< Double-precision float
94 BOOLEAN, ///< Boolean
95 STRING, ///< Text string
96 ARRAY_INT, ///< Array of integers
97 ARRAY_FLOAT, ///< Array of floats
98 ARRAY_BOOLEAN,///< Array of booleans
99 ARRAY_STRING, ///< Array of strings
100 NONE ///< No value (NULL)
101};
102
103/**
104 * @brief A dynamically-typed aggregate value.
105 *
106 * Wraps a @c std::variant of all supported scalar and array types.
107 * The active alternative is identified by the @c ValueType tag returned
108 * by @c getType().
109 */
110struct AggValue {
111private:
112 ValueType t; ///< Active type tag
113
114public:
115 /** @brief The variant holding the actual value. */
116 std::variant<long, double, bool, std::string,
117 std::vector<long>, std::vector<double>, std::vector<bool>, std::vector<std::string> > v;
118
119 /** @brief Construct a NULL (NONE) value. */
121 }
122 /** @brief Construct an integer value. @param l Integer value. */
123 AggValue(long l) : t(ValueType::INT), v(l) {
124 }
125 /** @brief Construct a float value. @param d Float value. */
126 AggValue(double d) : t(ValueType::FLOAT), v(d) {
127 }
128 /** @brief Construct a boolean value. @param b Boolean value. */
129 AggValue(bool b) : t(ValueType::BOOLEAN), v(b) {
130 }
131 /** @brief Construct a string value. @param s String value. */
132 AggValue(std::string s) : t(ValueType::STRING), v(s) {
133 }
134 /** @brief Construct an integer-array value. @param vec Integer array. */
135 AggValue(std::vector<long> vec) : t(ValueType::ARRAY_INT), v(vec) {
136 }
137 /** @brief Construct a float-array value. @param vec Float array. */
138 AggValue(std::vector<double> vec) : t(ValueType::ARRAY_FLOAT), v(vec) {
139 }
140 /** @brief Construct a boolean-array value. @param vec Boolean array. */
141 AggValue(std::vector<bool> vec) : t(ValueType::ARRAY_BOOLEAN), v(vec) {
142 }
143 /** @brief Construct a string-array value. @param vec String array. */
144 AggValue(std::vector<std::string> vec) : t(ValueType::ARRAY_STRING), v(vec) {
145 }
146
147 /**
148 * @brief Return the runtime type tag of this value.
149 * @return The @c ValueType identifying the active alternative.
150 */
152 return t;
153 }
154};
155
156/**
157 * @brief Abstract interface for an incremental aggregate accumulator.
158 *
159 * Each concrete subclass implements one aggregation function for one
160 * input type (e.g., SUM over integers, MAX over floats). Instances are
161 * created by @c makeAggregator().
162 */
164 virtual ~Aggregator() = default;
165
166 /**
167 * @brief Incorporate one input value into the running aggregate.
168 * @param x Input value to add.
169 */
170 virtual void add(const AggValue& x) = 0;
171
172 /**
173 * @brief Return the final aggregate result.
174 * @return The accumulated aggregate as an @c AggValue.
175 */
176 virtual AggValue finalize() const = 0;
177
178 /**
179 * @brief Return the type of the input values accepted by @c add().
180 * @return The @c ValueType of values passed to @c add().
181 *
182 * Used (via the @c resultType() default) to report the aggregate's
183 * result type; see @c makeAggregator() for the operators actually built.
184 */
185 virtual ValueType inputType() const = 0;
186
187 /**
188 * @brief Return the type of the value returned by @c finalize().
189 *
190 * Defaults to @c inputType(); override when the result type differs
191 * (e.g., AVG returns FLOAT regardless of the input type).
192 * @return The @c ValueType of the value returned by @c finalize().
193 */
194 virtual ValueType resultType() const {
195 return inputType();
196 }
197};
198
199/**
200 * @brief Map a PostgreSQL aggregate function OID to an @c AggregationOperator.
201 *
202 * @param oid OID of the aggregate function (e.g. @c F_COUNT_ANY, @c F_SUM_INT4).
203 * @return The corresponding @c AggregationOperator.
204 */
206
207/**
208 * @brief Map a PostgreSQL comparison-operator OID to a @c ComparisonOperator.
209 *
210 * The OID is the one stored in @c gate_cmp's @c info1 field (the OID of
211 * one of the six standard comparators @c =, @c <>, @c <, @c <=, @c >,
212 * @c >=). The translation goes via @c get_opname() so it is operand-type
213 * agnostic.
214 *
215 * @param[in] op_oid Comparison-operator OID.
216 * @param[out] ok Set to @c true on a recognised comparator, @c false
217 * when @p op_oid does not resolve in @c pg_operator
218 * or its name is not one of the six standard ones.
219 * @return The matching @c ComparisonOperator on success; an
220 * unspecified value (currently @c EQ) when @p ok is @c false.
221 */
222ComparisonOperator cmpOpFromOid(Oid op_oid, bool &ok);
223
224/**
225 * @brief Map a @c gate_arith operator tag to an @c ArithmeticOperator.
226 *
227 * The tag is the @c provsql_arith_op value stored in @c gate_arith's
228 * @c info1 field.
229 *
230 * @param[in] tag The persisted operator tag.
231 * @param[out] ok Set to @c true on a recognised tag, @c false when
232 * @p tag is outside the enumeration (a circuit written
233 * by a newer version of the extension).
234 * @return The matching @c ArithmeticOperator on success; an
235 * unspecified value (currently @c PLUS) when @p ok is @c false.
236 */
237ArithmeticOperator arithOpFromTag(unsigned tag, bool &ok);
238
239/**
240 * @brief Create a concrete @c Aggregator for the given operator and value type.
241 *
242 * @param op The aggregation function to implement.
243 * @param t The type of input values that will be accumulated.
244 * @return A heap-allocated @c Aggregator, or @c nullptr if the combination
245 * is not supported.
246 */
247std::unique_ptr<Aggregator> makeAggregator(AggregationOperator op, ValueType t);
248
249#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:91
@ ARRAY_INT
Array of integers.
Definition Aggregation.h:96
@ ARRAY_BOOLEAN
Array of booleans.
Definition Aggregation.h:98
@ INT
Signed 64-bit integer.
Definition Aggregation.h:92
@ STRING
Text string.
Definition Aggregation.h:95
@ ARRAY_FLOAT
Array of floats.
Definition Aggregation.h:97
@ ARRAY_STRING
Array of strings.
Definition Aggregation.h:99
@ BOOLEAN
Boolean.
Definition Aggregation.h:94
@ FLOAT
Double-precision float.
Definition Aggregation.h:93
ArithmeticOperator
Arithmetic operations carried by gate_arith circuit gates.
Definition Aggregation.h:74
@ POW
binary power
Definition Aggregation.h:82
@ DIV
binary quotient
Definition Aggregation.h:78
@ PERCENTILE
continuous percentile over interleaved [indicator, value] wires
Definition Aggregation.h:85
@ NEG
unary negation
Definition Aggregation.h:79
@ EXP
unary exponential
Definition Aggregation.h:84
@ TIMES
n-ary product
Definition Aggregation.h:76
@ LN
unary natural logarithm
Definition Aggregation.h:83
@ 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