28#include "utils/lsyscache.h"
29#include "utils/elog.h"
37 char *fname = get_func_name(oid);
40 provsql_error(
"Invalid OID for aggregation function: %d", oid);
42 std::string func_name {fname};
47 if(func_name ==
"count") {
49 }
else if(func_name ==
"sum") {
51 }
else if(func_name ==
"min") {
53 }
else if(func_name ==
"max") {
55 }
else if(func_name ==
"choose") {
57 }
else if(func_name ==
"avg") {
59 }
else if(func_name ==
"array_agg") {
61 }
else if(func_name ==
"bool_and" || func_name ==
"every") {
63 }
else if(func_name ==
"bool_or") {
66 provsql_error(
"Aggregation operator %s not supported", func_name.c_str());
75 char *opname = get_opname(op_oid);
79 std::string s {opname};
116struct False : std::bool_constant<false> { };
136 if constexpr (std::is_same_v<T,long>)
138 else if constexpr (std::is_same_v<T,double>)
140 else if constexpr (std::is_same_v<T,bool>)
142 else if constexpr (std::is_same_v<T,std::string>)
157 const T& v = std::get<T>(x.
v);
171 const T& v = std::get<T>(x.
v);
189 const T& v = std::get<T>(x.
v);
224 const T& v = std::get<T>(x.
v);
233 if constexpr (std::is_same_v<T,long>)
235 else if constexpr (std::is_same_v<T,double>)
258 if (t ==
ValueType::INT)
return std::make_unique<SumAgg<long> >();
259 throw std::runtime_error(
"COUNT expects an integer-valued contribution");
264 default:
throw std::runtime_error(
"SUM not supported for this type");
270 default:
throw std::runtime_error(
"MIN not supported for this type");
276 default:
throw std::runtime_error(
"MAX not supported for this type");
282 default:
throw std::runtime_error(
"AVG not supported for this type");
290 default:
throw std::runtime_error(
"CHOOSE not supported for this type");
297 throw std::runtime_error(
298 "makeAggregator: boolean/array_agg aggregates are handled by the "
299 "m-semiring HAVING rewrite, not the deterministic sampler");
302 throw std::logic_error(
"Unhandled AggregationOperator");
ArithmeticOperator arithOpFromTag(unsigned tag, bool &ok)
Map a gate_arith operator tag to an ArithmeticOperator.
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.
ArithmeticOperator
Arithmetic operations carried by gate_arith circuit gates.
@ MAX
n-ary maximum (order statistic)
@ PERCENTILE
continuous percentile over interleaved [indicator, value] wires
@ MIN
n-ary minimum (order statistic)
@ LN
unary natural logarithm
Uniform error-reporting macros for ProvSQL.
#define provsql_error(fmt,...)
Report a fatal ProvSQL error and abort the current transaction.
Core types, constants, and utilities shared across ProvSQL.
provsql_arith_op
Arithmetic operator tags used by gate_arith.
@ PROVSQL_ARITH_PERCENTILE
continuous percentile (order-statistic aggregate): wires are interleaved [ind_1, x_1,...
@ PROVSQL_ARITH_DIV
binary, child0 / child1
@ PROVSQL_ARITH_LN
unary, natural logarithm of child0 (a negative draw raises at evaluation)
@ PROVSQL_ARITH_PLUS
n-ary, sum of children
@ PROVSQL_ARITH_POW
binary, child0 ^ child1 (real branch only: a negative base drawn with a non-integer exponent raises a...
@ PROVSQL_ARITH_NEG
unary, -child0
@ PROVSQL_ARITH_MINUS
binary, child0 - child1
@ PROVSQL_ARITH_EXP
unary, e^child0
@ PROVSQL_ARITH_TIMES
n-ary, product of children
@ PROVSQL_ARITH_MIN
n-ary, min of children (order statistic; least / min aggregate)
@ PROVSQL_ARITH_MAX
n-ary, max of children (order statistic; greatest / max aggregate)
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.