Record Class AggregateFunction

java.lang.Object
java.lang.Record
com.darkcollective.relix.ast.AggregateFunction
Record Components:
operator - the aggregate operator
argument - the expression the operator reads (the ranking expression for ARGMAX/ARGMIN); never null
yieldExpr - for ARGMAX/ARGMIN, the expression whose value is returned at the extremal row; empty for every other operator

An 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's STRING_AGG(x, ', ' ORDER BY y) reads them. MODE reads 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 name
distinct - whether the aggregate reads each distinct argument value once
orderBy - the order the group's rows are read in; empty for the order they arrive in

public record AggregateFunction(AggregateOperator operator, Operand argument, Optional<Operand> yieldExpr, Optional<String> alias, boolean distinct, List<SortSpecification> orderBy) extends Record
Represents an aggregate function in an aggregation operation. An aggregate function applies an operator (SUM, AVG, COUNT, MIN, MAX, COLLECT, ARGMAX, ARGMIN) to an 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 Details

  • Method Details

    • simple

      public static AggregateFunction simple(AggregateOperator operator, String attribute)
      Creates an aggregate over a bare column, without an alias.
    • aliased

      public static AggregateFunction aliased(AggregateOperator operator, String attribute, String alias)
      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 column attribute and returns column yieldColumn, without an alias.
      Parameters:
      operator - AggregateOperator.ARGMAX or AggregateOperator.ARGMIN
      attribute - the ranking column
      yieldColumn - 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.ARGMAX or AggregateOperator.ARGMIN
      attribute - the ranking column
      yieldColumn - the column to return at the extremal row
      alias - the output column name
      Returns:
      the aggregate function
    • of

      public static AggregateFunction of(AggregateOperator operator, Operand argument)
      Creates an aggregate over an arbitrary expression, without an alias.
    • aliased

      public static AggregateFunction aliased(AggregateOperator operator, Operand argument, String alias)
      Creates an aggregate over an arbitrary expression, with an alias.
    • grouping

      public static AggregateFunction grouping(List<Operand> keys, String alias)
      SQL's GROUPING(k, …): which of keys a row has rolled up, one bit each, the last the lowest.
      Parameters:
      keys - the grouping keys it reports on, as written in the grouping
      alias - the output column name
      Returns:
      the aggregate function
    • groupingArguments

      public List<Operand> groupingArguments()
      The keys a GROUPING reports on, in the order written; empty for every other operator.
      Returns:
      the keys
    • outputName

      public String outputName()
      The name of the output column this aggregate produces: the alias() when present, otherwise a synthetic operator_argument name (e.g. sum_amount, min_Abs, or sum_expr for 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

      public final String 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.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • 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.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • operator

      public AggregateOperator operator()
      Returns the value of the operator record component.
      Returns:
      the value of the operator record component
    • argument

      public Operand argument()
      Returns the value of the argument record component.
      Returns:
      the value of the argument record component
    • yieldExpr

      public Optional<Operand> yieldExpr()
      Returns the value of the yieldExpr record component.
      Returns:
      the value of the yieldExpr record component
    • alias

      public Optional<String> alias()
      Returns the value of the alias record component.
      Returns:
      the value of the alias record component
    • distinct

      public boolean distinct()
      Returns the value of the distinct record component.
      Returns:
      the value of the distinct record component
    • orderBy

      public List<SortSpecification> orderBy()
      Returns the value of the orderBy record component.
      Returns:
      the value of the orderBy record component