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" || func_name ==
"array_collect") {
61 }
else if(func_name ==
"bool_and" || func_name ==
"every") {
63 }
else if(func_name ==
"bool_or") {
75 char *opname = get_opname(op_oid);
79 std::string s {opname};
123struct False : std::bool_constant<false> { };
143 if constexpr (std::is_same_v<T,long>)
145 else if constexpr (std::is_same_v<T,double>)
147 else if constexpr (std::is_same_v<T,bool>)
149 else if constexpr (std::is_same_v<T,std::string>)
164 const T& v = std::get<T>(x.
v);
178 const T& v = std::get<T>(x.
v);
196 const T& v = std::get<T>(x.
v);
231 const T& v = std::get<T>(x.
v);
240 if constexpr (std::is_same_v<T,long>)
242 else if constexpr (std::is_same_v<T,double>)
265 if (t ==
ValueType::INT)
return std::make_unique<SumAgg<long> >();
266 throw std::runtime_error(
"COUNT expects an integer-valued contribution");
271 default:
throw std::runtime_error(
"SUM not supported for this type");
277 default:
throw std::runtime_error(
"MIN not supported for this type");
283 default:
throw std::runtime_error(
"MAX not supported for this type");
289 default:
throw std::runtime_error(
"AVG not supported for this type");
297 default:
throw std::runtime_error(
"CHOOSE not supported for this type");
304 throw std::runtime_error(
305 "makeAggregator: boolean/array_agg aggregates are handled by the "
306 "m-semiring HAVING rewrite, not the deterministic sampler");
309 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)
@ ROUND
rounding, to a number of digits given by a second wire
@ PERCENTILE
continuous percentile over interleaved [indicator, value] wires
@ INTDIV
binary quotient truncated toward zero (integer division)
@ ABS
unary absolute value
@ AS_FLOAT8
unary: the value as double precision reads it
@ AS_FLOAT4
unary: the value as real reads it
@ 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.
#define provsql_unsupported(scope, tag, fmt,...)
Refuse a query ProvSQL cannot track, and abort the 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_ASFLOAT8
unary, child0 as double precision
@ PROVSQL_ARITH_LN
unary, natural logarithm of child0 (a negative draw raises at evaluation)
@ PROVSQL_ARITH_ROUND
child0 rounded half away from zero, to child1 decimal digits where a second child is given (SQL round...
@ 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_ABS
unary, |child0|
@ PROVSQL_ARITH_FLOOR
unary, greatest integer <= child0
@ PROVSQL_ARITH_NEG
unary, -child0
@ PROVSQL_ARITH_INTDIV
binary, child0 / child1 truncated toward zero: SQL's division of two integers
@ PROVSQL_ARITH_MINUS
binary, child0 - child1
@ PROVSQL_ARITH_CEIL
unary, least integer >= child0
@ 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)
@ PROVSQL_ARITH_ASFLOAT4
unary, child0 as real reads it
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.