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 Runtime type tag for aggregate values.
66 */
67enum class ValueType {
68 INT, ///< Signed 64-bit integer
69 FLOAT, ///< Double-precision float
70 BOOLEAN, ///< Boolean
71 STRING, ///< Text string
72 ARRAY_INT, ///< Array of integers
73 ARRAY_FLOAT, ///< Array of floats
74 ARRAY_BOOLEAN,///< Array of booleans
75 ARRAY_STRING, ///< Array of strings
76 NONE ///< No value (NULL)
77};
78
79/**
80 * @brief A dynamically-typed aggregate value.
81 *
82 * Wraps a @c std::variant of all supported scalar and array types.
83 * The active alternative is identified by the @c ValueType tag returned
84 * by @c getType().
85 */
86struct AggValue {
87private:
88 ValueType t; ///< Active type tag
89
90public:
91 /** @brief The variant holding the actual value. */
92 std::variant<long, double, bool, std::string,
93 std::vector<long>, std::vector<double>, std::vector<bool>, std::vector<std::string> > v;
94
95 /** @brief Construct a NULL (NONE) value. */
97 }
98 /** @brief Construct an integer value. @param l Integer value. */
99 AggValue(long l) : t(ValueType::INT), v(l) {
100 }
101 /** @brief Construct a float value. @param d Float value. */
102 AggValue(double d) : t(ValueType::FLOAT), v(d) {
103 }
104 /** @brief Construct a boolean value. @param b Boolean value. */
105 AggValue(bool b) : t(ValueType::BOOLEAN), v(b) {
106 }
107 /** @brief Construct a string value. @param s String value. */
108 AggValue(std::string s) : t(ValueType::STRING), v(s) {
109 }
110 /** @brief Construct an integer-array value. @param vec Integer array. */
111 AggValue(std::vector<long> vec) : t(ValueType::ARRAY_INT), v(vec) {
112 }
113 /** @brief Construct a float-array value. @param vec Float array. */
114 AggValue(std::vector<double> vec) : t(ValueType::ARRAY_FLOAT), v(vec) {
115 }
116 /** @brief Construct a boolean-array value. @param vec Boolean array. */
117 AggValue(std::vector<bool> vec) : t(ValueType::ARRAY_BOOLEAN), v(vec) {
118 }
119 /** @brief Construct a string-array value. @param vec String array. */
120 AggValue(std::vector<std::string> vec) : t(ValueType::ARRAY_STRING), v(vec) {
121 }
122
123 /**
124 * @brief Return the runtime type tag of this value.
125 * @return The @c ValueType identifying the active alternative.
126 */
128 return t;
129 }
130};
131
132/**
133 * @brief Abstract interface for an incremental aggregate accumulator.
134 *
135 * Each concrete subclass implements one aggregation function for one
136 * input type (e.g., SUM over integers, MAX over floats). Instances are
137 * created by @c makeAggregator().
138 */
140 virtual ~Aggregator() = default;
141
142 /**
143 * @brief Incorporate one input value into the running aggregate.
144 * @param x Input value to add.
145 */
146 virtual void add(const AggValue& x) = 0;
147
148 /**
149 * @brief Return the final aggregate result.
150 * @return The accumulated aggregate as an @c AggValue.
151 */
152 virtual AggValue finalize() const = 0;
153
154 /**
155 * @brief Return the type of the input values accepted by @c add().
156 * @return The @c ValueType of values passed to @c add().
157 *
158 * Used (via the @c resultType() default) to report the aggregate's
159 * result type; see @c makeAggregator() for the operators actually built.
160 */
161 virtual ValueType inputType() const = 0;
162
163 /**
164 * @brief Return the type of the value returned by @c finalize().
165 *
166 * Defaults to @c inputType(); override when the result type differs
167 * (e.g., AVG returns FLOAT regardless of the input type).
168 * @return The @c ValueType of the value returned by @c finalize().
169 */
170 virtual ValueType resultType() const {
171 return inputType();
172 }
173};
174
175/**
176 * @brief Map a PostgreSQL aggregate function OID to an @c AggregationOperator.
177 *
178 * @param oid OID of the aggregate function (e.g. @c F_COUNT_ANY, @c F_SUM_INT4).
179 * @return The corresponding @c AggregationOperator.
180 */
182
183/**
184 * @brief Map a PostgreSQL comparison-operator OID to a @c ComparisonOperator.
185 *
186 * The OID is the one stored in @c gate_cmp's @c info1 field (the OID of
187 * one of the six standard comparators @c =, @c <>, @c <, @c <=, @c >,
188 * @c >=). The translation goes via @c get_opname() so it is operand-type
189 * agnostic.
190 *
191 * @param[in] op_oid Comparison-operator OID.
192 * @param[out] ok Set to @c true on a recognised comparator, @c false
193 * when @p op_oid does not resolve in @c pg_operator
194 * or its name is not one of the six standard ones.
195 * @return The matching @c ComparisonOperator on success; an
196 * unspecified value (currently @c EQ) when @p ok is @c false.
197 */
198ComparisonOperator cmpOpFromOid(Oid op_oid, bool &ok);
199
200/**
201 * @brief Create a concrete @c Aggregator for the given operator and value type.
202 *
203 * @param op The aggregation function to implement.
204 * @param t The type of input values that will be accumulated.
205 * @return A heap-allocated @c Aggregator, or @c nullptr if the combination
206 * is not supported.
207 */
208std::unique_ptr<Aggregator> makeAggregator(AggregationOperator op, ValueType t);
209
210#endif /* AGGREGATION_H */
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:67
@ ARRAY_INT
Array of integers.
Definition Aggregation.h:72
@ ARRAY_BOOLEAN
Array of booleans.
Definition Aggregation.h:74
@ INT
Signed 64-bit integer.
Definition Aggregation.h:68
@ STRING
Text string.
Definition Aggregation.h:71
@ ARRAY_FLOAT
Array of floats.
Definition Aggregation.h:73
@ ARRAY_STRING
Array of strings.
Definition Aggregation.h:75
@ BOOLEAN
Boolean.
Definition Aggregation.h:70
@ FLOAT
Double-precision float.
Definition Aggregation.h:69
Fix macro conflicts between PostgreSQL headers and the C++ STL/Boost.
A dynamically-typed aggregate value.
Definition Aggregation.h:86
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.
Definition Aggregation.h:96
AggValue(long l)
Construct an integer value.
Definition Aggregation.h:99
ValueType t
Active type tag.
Definition Aggregation.h:88
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.
Definition Aggregation.h:93
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