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());
82struct False : std::bool_constant<false> { };
102 if constexpr (std::is_same_v<T,long>)
104 else if constexpr (std::is_same_v<T,double>)
106 else if constexpr (std::is_same_v<T,bool>)
108 else if constexpr (std::is_same_v<T,std::string>)
123 const T& v = std::get<T>(x.
v);
140 const T& v = std::get<T>(x.
v);
161 const T& v = std::get<T>(x.
v);
182 auto b = std::get<bool>(x.
v);
199 auto b = std::get<bool>(x.
v);
236 const T& v = std::get<T>(x.
v);
248 if constexpr (std::is_same_v<T,long>)
250 else if constexpr (std::is_same_v<T,double>)
270 const T& v = std::get<T>(x.
v);
281 if constexpr (std::is_same_v<T,long>)
283 else if constexpr (std::is_same_v<T,double>)
285 else if constexpr (std::is_same_v<T,bool>)
287 else if constexpr (std::is_same_v<T,std::string>)
299 if (t ==
ValueType::INT)
return std::make_unique<SumAgg<long> >();
300 throw std::runtime_error(
"COUNT is normalized to SUM(INT)");
305 default:
throw std::runtime_error(
"SUM not supported for this type");
311 default:
throw std::runtime_error(
"MIN not supported for this type");
317 default:
throw std::runtime_error(
"MAX not supported for this type");
323 default:
throw std::runtime_error(
"AVG not supported for this type");
327 throw std::runtime_error(
"AND requires BOOLEAN");
330 throw std::runtime_error(
"OR requires BOOLEAN");
337 default:
throw std::runtime_error(
"CHOOSE not supported for this type");
345 default:
throw std::runtime_error(
"ARRAY_AGG not supported for this type");
348 return std::make_unique<NoneAgg>();
351 throw std::logic_error(
"Unhandled AggregationOperator");
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)
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.
Aggregator implementing boolean AND (returns false if any input is false).
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.
Aggregator implementing boolean OR (returns true if any input is true).
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.