Record Class AggregateFunction
- Record Components:
operator- the aggregate operatorargument- the expression the operator reads (the ranking expression for ARGMAX/ARGMIN); never nullyieldExpr- for ARGMAX/ARGMIN, the expression whose value is returned at the extremal row; empty for every other operatorAn aggregate whose result depends on the order it reads its rows in —
STRING_AGG,COLLECT— may carry an ordering: the group's rows are read in that order, as SQL'sSTRING_AGG(x, ', ' ORDER BY y)reads them.MODEreads one too, to break a tie: the first of the equally frequent values in that order wins. Every other aggregate has none.alias- the optional output column namedistinct- whether the aggregate reads each distinct argument value onceorderBy- the order the group's rows are read in; empty for the order they arrive in
Operand argument, with an optional alias
for the result.
The argument is a full scalar expression, not merely a column reference, so
an aggregate may be computed over a derived value — e.g. SUM(price * qty)
or MIN(Abs(delta)). The common case SUM(amount) is simply an
aggregate over an AttributeOperand.
The ARGMAX/ARGMIN operators additionally carry a
yield expression: ARGMAX(rank, yield) ranks rows by
argument and returns the yieldExpr value of the extremal row.
For every other operator the yield expression is empty.
Examples:
- Simple aggregate:
SUM(salary) - Derived aggregate:
SUM(price * qty),MIN(Abs(delta)) - Aliased aggregate:
SUM(salary) → total_salary - Argmax:
ARGMAX(amount, order_id) → biggest_order - Distinct:
COUNT(DISTINCT session) → sessions
A distinct aggregate reads each distinct value of its
argument once per group, as SQL's COUNT(DISTINCT x) does. It is not
decomposable: a distinct count is not the sum of partial distinct counts.
-
Constructor Summary
ConstructorsConstructorDescriptionAggregateFunction(AggregateOperator operator, Operand argument, Optional<Operand> yieldExpr, Optional<String> alias) An aggregate over every value of its argument, as written withoutDISTINCT.AggregateFunction(AggregateOperator operator, Operand argument, Optional<Operand> yieldExpr, Optional<String> alias, boolean distinct) An aggregate read in the order its rows arrive in.AggregateFunction(AggregateOperator operator, Operand argument, Optional<Operand> yieldExpr, Optional<String> alias, boolean distinct, List<SortSpecification> orderBy) Creates an instance of aAggregateFunctionrecord class. -
Method Summary
Modifier and TypeMethodDescriptionalias()Returns the value of thealiasrecord component.static AggregateFunctionaliased(AggregateOperator operator, Operand argument, String alias) Creates an aggregate over an arbitrary expression, with an alias.static AggregateFunctionaliased(AggregateOperator operator, String attribute, String alias) Creates an aggregate over a bare column, with an alias.static AggregateFunctionarg(AggregateOperator operator, String attribute, String yieldColumn) Creates an argmax/argmin-style aggregate that ranks by columnattributeand returns columnyieldColumn, without an alias.static AggregateFunctionargAliased(AggregateOperator operator, String attribute, String yieldColumn, String alias) Creates an argmax/argmin-style aggregate over bare columns, with an alias.argument()Returns the value of theargumentrecord component.booleandistinct()Returns the value of thedistinctrecord component.final booleanIndicates whether some other object is "equal to" this one.static AggregateFunctionSQL'sGROUPING(k, …): which ofkeysa row has rolled up, one bit each, the last the lowest.The keys aGROUPINGreports on, in the order written; empty for every other operator.final inthashCode()Returns a hash code value for this object.static AggregateFunctionof(AggregateOperator operator, Operand argument) Creates an aggregate over an arbitrary expression, without an alias.operator()Returns the value of theoperatorrecord component.orderBy()Returns the value of theorderByrecord component.The name of the output column this aggregate produces: thealias()when present, otherwise a syntheticoperator_argumentname (e.g.static AggregateFunctionsimple(AggregateOperator operator, String attribute) Creates an aggregate over a bare column, without an alias.final StringtoString()Returns a string representation of this record class.Returns the value of theyieldExprrecord component.
-
Constructor Details
-
AggregateFunction
public AggregateFunction(AggregateOperator operator, Operand argument, Optional<Operand> yieldExpr, Optional<String> alias, boolean distinct, List<SortSpecification> orderBy) Creates an instance of aAggregateFunctionrecord class.- Parameters:
operator- the value for theoperatorrecord componentargument- the value for theargumentrecord componentyieldExpr- the value for theyieldExprrecord componentalias- the value for thealiasrecord componentdistinct- the value for thedistinctrecord componentorderBy- the value for theorderByrecord component
-
AggregateFunction
public AggregateFunction(AggregateOperator operator, Operand argument, Optional<Operand> yieldExpr, Optional<String> alias, boolean distinct) An aggregate read in the order its rows arrive in. -
AggregateFunction
public AggregateFunction(AggregateOperator operator, Operand argument, Optional<Operand> yieldExpr, Optional<String> alias) An aggregate over every value of its argument, as written withoutDISTINCT.
-
-
Method Details
-
simple
Creates an aggregate over a bare column, without an alias. -
aliased
Creates an aggregate over a bare column, with an alias. -
arg
public static AggregateFunction arg(AggregateOperator operator, String attribute, String yieldColumn) Creates an argmax/argmin-style aggregate that ranks by columnattributeand returns columnyieldColumn, without an alias.- Parameters:
operator-AggregateOperator.ARGMAXorAggregateOperator.ARGMINattribute- the ranking columnyieldColumn- the column to return at the extremal row- Returns:
- the aggregate function
-
argAliased
public static AggregateFunction argAliased(AggregateOperator operator, String attribute, String yieldColumn, String alias) Creates an argmax/argmin-style aggregate over bare columns, with an alias.- Parameters:
operator-AggregateOperator.ARGMAXorAggregateOperator.ARGMINattribute- the ranking columnyieldColumn- the column to return at the extremal rowalias- the output column name- Returns:
- the aggregate function
-
of
Creates an aggregate over an arbitrary expression, without an alias. -
aliased
Creates an aggregate over an arbitrary expression, with an alias. -
grouping
SQL'sGROUPING(k, …): which ofkeysa row has rolled up, one bit each, the last the lowest.- Parameters:
keys- the grouping keys it reports on, as written in the groupingalias- the output column name- Returns:
- the aggregate function
-
groupingArguments
The keys aGROUPINGreports on, in the order written; empty for every other operator.- Returns:
- the keys
-
outputName
The name of the output column this aggregate produces: thealias()when present, otherwise a syntheticoperator_argumentname (e.g.sum_amount,min_Abs, orsum_exprfor a complex expression). Schema inference and the optimizer use this so the produced column name is consistent everywhere.- Returns:
- the output column name; never blank
-
toString
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components. -
hashCode
public final int hashCode()Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with '=='. -
operator
Returns the value of theoperatorrecord component.- Returns:
- the value of the
operatorrecord component
-
argument
Returns the value of theargumentrecord component.- Returns:
- the value of the
argumentrecord component
-
yieldExpr
Returns the value of theyieldExprrecord component.- Returns:
- the value of the
yieldExprrecord component
-
alias
Returns the value of thealiasrecord component.- Returns:
- the value of the
aliasrecord component
-
distinct
public boolean distinct()Returns the value of thedistinctrecord component.- Returns:
- the value of the
distinctrecord component
-
orderBy
Returns the value of theorderByrecord component.- Returns:
- the value of the
orderByrecord component
-