28#include "utils/lsyscache.h"
29#include "utils/elog.h"
36 char *fname = get_func_name(oid);
39 provsql_error(
"Invalid OID for aggregation function: %d", oid);
41 std::string func_name {fname};
46 if(func_name ==
"count") {
48 }
else if(func_name ==
"sum") {
50 }
else if(func_name ==
"min") {
52 }
else if(func_name ==
"max") {
54 }
else if(func_name ==
"choose") {
56 }
else if(func_name ==
"avg") {
58 }
else if(func_name ==
"array_agg") {
60 }
else if(func_name ==
"bool_and" || func_name ==
"every") {
62 }
else if(func_name ==
"bool_or") {
65 provsql_error(
"Aggregation operator %s not supported", func_name.c_str());
74 char *opname = get_opname(op_oid);
78 std::string s {opname};
94struct False : std::bool_constant<false> { };
114 if constexpr (std::is_same_v<T,long>)
116 else if constexpr (std::is_same_v<T,double>)
118 else if constexpr (std::is_same_v<T,bool>)
120 else if constexpr (std::is_same_v<T,std::string>)
135 const T& v = std::get<T>(x.
v);
149 const T& v = std::get<T>(x.
v);
167 const T& v = std::get<T>(x.
v);
202 const T& v = std::get<T>(x.
v);
211 if constexpr (std::is_same_v<T,long>)
213 else if constexpr (std::is_same_v<T,double>)
233 if (t ==
ValueType::INT)
return std::make_unique<SumAgg<long> >();
234 throw std::runtime_error(
"COUNT is normalized to SUM(INT)");
239 default:
throw std::runtime_error(
"SUM not supported for this type");
245 default:
throw std::runtime_error(
"MIN not supported for this type");
251 default:
throw std::runtime_error(
"MAX not supported for this type");
257 default:
throw std::runtime_error(
"AVG not supported for this type");
265 default:
throw std::runtime_error(
"CHOOSE not supported for this type");
272 throw std::runtime_error(
273 "makeAggregator: boolean/array_agg aggregates are handled by the "
274 "m-semiring HAVING rewrite, not the deterministic sampler");
277 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.
@ INT
Signed 64-bit integer.
@ 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 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().
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).
void add(const AggValue &x) override
Incorporate one input value into the running aggregate.
Aggregator implementing MAX for integer or float types.
void add(const AggValue &x) override
Incorporate one input value into the running aggregate.
Aggregator implementing MIN for integer or float types.
void add(const AggValue &x) override
Incorporate one input value into the running aggregate.
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.
void add(const AggValue &x) override
Incorporate one input value into the running aggregate.