23#include "utils/lsyscache.h"
24#include "utils/elog.h"
31 char *fname = get_func_name(oid);
34 provsql_error(
"Invalid OID for aggregation function: %d", oid);
36 std::string func_name {fname};
41 if(func_name ==
"count") {
43 }
else if(func_name ==
"sum") {
45 }
else if(func_name ==
"min") {
47 }
else if(func_name ==
"max") {
49 }
else if(func_name ==
"choose") {
51 }
else if(func_name ==
"avg") {
53 }
else if(func_name ==
"array_agg") {
55 }
else if(func_name ==
"and" || func_name==
"every") {
57 }
else if(func_name ==
"or") {
60 provsql_error(
"Aggregation operator %s not supported", func_name.c_str());
69 char *opname = get_opname(op_oid);
73 std::string s {opname};
104struct False : std::bool_constant<false> { };
124 if constexpr (std::is_same_v<T,long>)
126 else if constexpr (std::is_same_v<T,double>)
128 else if constexpr (std::is_same_v<T,bool>)
130 else if constexpr (std::is_same_v<T,std::string>)
145 const T& v = std::get<T>(x.
v);
162 const T& v = std::get<T>(x.
v);
183 const T& v = std::get<T>(x.
v);
204 auto b = std::get<bool>(x.
v);
221 auto b = std::get<bool>(x.
v);
258 const T& v = std::get<T>(x.
v);
270 if constexpr (std::is_same_v<T,long>)
272 else if constexpr (std::is_same_v<T,double>)
292 const T& v = std::get<T>(x.
v);
303 if constexpr (std::is_same_v<T,long>)
305 else if constexpr (std::is_same_v<T,double>)
307 else if constexpr (std::is_same_v<T,bool>)
309 else if constexpr (std::is_same_v<T,std::string>)
321 if (t ==
ValueType::INT)
return std::make_unique<SumAgg<long> >();
322 throw std::runtime_error(
"COUNT is normalized to SUM(INT)");
327 default:
throw std::runtime_error(
"SUM not supported for this type");
333 default:
throw std::runtime_error(
"MIN not supported for this type");
339 default:
throw std::runtime_error(
"MAX not supported for this type");
345 default:
throw std::runtime_error(
"AVG not supported for this type");
349 throw std::runtime_error(
"AND requires BOOLEAN");
352 throw std::runtime_error(
"OR requires BOOLEAN");
359 default:
throw std::runtime_error(
"CHOOSE not supported for this type");
367 default:
throw std::runtime_error(
"ARRAY_AGG not supported for this type");
370 return std::make_unique<NoneAgg>();
373 throw std::logic_error(
"Unhandled AggregationOperator");
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.
Typed aggregation value, operator, and aggregator abstractions.
AggregationOperator
SQL aggregation functions tracked by ProvSQL.
@ OR
Boolean OR aggregate.
@ COUNT
COUNT(*) or COUNT(expr) → integer.
@ AND
Boolean AND aggregate.
@ SUM
SUM → integer or float.
@ ARRAY_AGG
Array aggregation.
@ NONE
No aggregation (returns NULL).
@ CHOOSE
Arbitrary selection (pick one element).
ComparisonOperator
SQL comparison operators used in gate_cmp circuit gates.
@ LE
Less than or equal (<=).
@ GE
Greater than or equal (>=).
ValueType
Runtime type tag for aggregate values.
@ ARRAY_INT
Array of integers.
@ ARRAY_BOOLEAN
Array of booleans.
@ INT
Signed 64-bit integer.
@ ARRAY_FLOAT
Array of floats.
@ ARRAY_STRING
Array of strings.
@ FLOAT
Double-precision float.
Uniform error-reporting macros for ProvSQL.
#define provsql_error(fmt,...)
Report a fatal ProvSQL error and abort the current transaction.
A dynamically-typed aggregate value.
ValueType getType() const
Return the runtime type tag of this 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.
Abstract interface for an incremental aggregate accumulator.
void add(const AggValue &x) override
Incorporate one input value into the running aggregate.
AggregationOperator op() const override
Return the aggregation operator this accumulator implements.
Aggregator implementing ARRAY_AGG; collects all non-NULL inputs into an array.
void add(const AggValue &x) override
Incorporate one input value into the running aggregate.
ValueType resultType() const override
Return the type of the value returned by finalize().
AggregationOperator op() const override
Return the aggregation operator this accumulator implements.
AggValue finalize() const override
Return the final aggregate result.
std::vector< T > values
Accumulated elements.
Aggregator implementing AVG; always returns a float result.
bool has
true once the first non-NULL input has been seen
ValueType resultType() const override
Return the type of the value returned by finalize().
ValueType inputType() const override
Return the type of the input values accepted by add().
AggregationOperator op() const override
Return the aggregation operator this accumulator implements.
unsigned count
Number of non-NULL inputs seen so far.
void add(const AggValue &x) override
Incorporate one input value into the running aggregate.
AggValue finalize() const override
Return the final aggregate result.
double sum
Running sum of all non-NULL input values.
Aggregator implementing CHOOSE (returns the first non-NULL input).
AggregationOperator op() const override
Return the aggregation operator this accumulator implements.
void add(const AggValue &x) override
Incorporate one input value into the running aggregate.
Aggregator implementing MAX for integer or float types.
AggregationOperator op() const override
Return the aggregation operator this accumulator implements.
void add(const AggValue &x) override
Incorporate one input value into the running aggregate.
Aggregator implementing MIN for integer or float types.
AggregationOperator op() const override
Return the aggregation operator this accumulator implements.
void add(const AggValue &x) override
Incorporate one input value into the running aggregate.
Aggregator that ignores all inputs and always returns NULL.
AggValue finalize() const override
Return the final aggregate result.
ValueType inputType() const override
Return the type of the input values accepted by add().
AggregationOperator op() const override
Return the aggregation operator this accumulator implements.
void add(const AggValue &x) override
Incorporate one input value into the running aggregate.
void add(const AggValue &x) override
Incorporate one input value into the running aggregate.
AggregationOperator op() const override
Return the aggregation operator this accumulator implements.
Base aggregator template for scalar types (int, float, bool, string).
AggValue finalize() const override
Return the accumulated value, or NULL if no inputs were seen.
ValueType inputType() const override
Return the value type corresponding to T.
bool has
true once the first non-NULL input has been seen
T value
Current accumulated value.
Aggregator implementing SUM for integer or float types.
AggregationOperator op() const override
Return the aggregation operator this accumulator implements.
void add(const AggValue &x) override
Incorporate one input value into the running aggregate.