Record Class AggregationNode

java.lang.Object
java.lang.Record
com.darkcollective.relix.ast.AggregationNode
Record Components:
groupingKeys - the keys to group by; the output columns, in this order
groupingSets - the grouping sets, as indexes into groupingKeys; empty for the ordinary grouping by every key
aggregates - the aggregate functions to apply
input - the source relation
location - the source location of this node; never null
All Implemented Interfaces:
RelNode

public record AggregationNode(List<GroupingKey> groupingKeys, List<List<Integer>> groupingSets, List<AggregateFunction> aggregates, RelNode input, SourceLocation location) extends Record implements RelNode
Represents an aggregation operation (γ) in relational algebra. Groups tuples by the grouping keys and applies aggregate functions.

A grouping key is a full Operand expression, not merely a column reference, so a group may be formed over a derived value — e.g. γ YEAR(occurred_at) → yr, COUNT(id) (Events) — with the common case γ customer_id, SUM(amount) (Orders) simply grouping over a bare column.

Grouping sets

SQL's ROLLUP, CUBE and GROUPING SETS group the same rows several ways in one aggregation. Each grouping set is a list of indexes into groupingKeys(): the rows are grouped by those keys, and every other key is NULL in the rows the set produces. ROLLUP (region, product) is the three sets (region, product), (region) and (). An empty list of sets is the ordinary grouping, by every key at once, and a single set naming every key is normalised to it.

The sets are held in a canonical shape, so that a node prints as text that parses back to it: each set lists its indexes ascending, and read in order the sets name the keys first in index order. Every key is in at least one set.

  • Constructor Details

    • AggregationNode

      public AggregationNode(List<GroupingKey> groupingKeys, List<List<Integer>> groupingSets, List<AggregateFunction> aggregates, RelNode input, SourceLocation location)
      Creates an instance of a AggregationNode record class.
      Parameters:
      groupingKeys - the value for the groupingKeys record component
      groupingSets - the value for the groupingSets record component
      aggregates - the value for the aggregates record component
      input - the value for the input record component
      location - the value for the location record component
    • AggregationNode

      public AggregationNode(List<GroupingKey> groupingKeys, List<AggregateFunction> aggregates, RelNode input, SourceLocation location)
      An ordinary grouping, by every key at once.
      Parameters:
      groupingKeys - the keys to group by
      aggregates - the aggregate functions to apply
      input - the source relation
      location - the source location of this node; never null
    • AggregationNode

      public AggregationNode(List<String> groupingColumns, List<AggregateFunction> aggregates, RelNode input)
      Convenience constructor grouping by bare column names — the classic form, used throughout call sites and tests that group by plain columns. Uses SourceLocation.UNKNOWN. A null column list is preserved as a null grouping (ungrouped aggregation).
  • Method Details

    • withInput

      public AggregationNode withInput(RelNode newInput)
      This aggregation over another input, with the same keys, grouping sets and aggregates.
      Parameters:
      newInput - the input to aggregate
      Returns:
      the node over newInput
    • withKeysAndAggregates

      public AggregationNode withKeysAndAggregates(List<GroupingKey> keys, List<AggregateFunction> aggregates)
      This aggregation with other keys and aggregates, keeping its grouping sets and input. The keys replace the current ones position for position.
      Parameters:
      keys - the replacement keys, as many as there are now
      aggregates - the replacement aggregates
      Returns:
      the node with them
    • hasGroupingSets

      public boolean hasGroupingSets()
      Whether this aggregation groups by more than one set of its keys — a ROLLUP, CUBE or GROUPING SETS.
      Returns:
      true for several grouping sets
    • effectiveGroupingSets

      public List<List<Integer>> effectiveGroupingSets()
      The grouping sets as they are run: the declared ones, or for an ordinary grouping the one set of every key.
      Returns:
      the sets, each a list of key indexes ascending
    • inEverySet

      public boolean inEverySet(int index)
      Whether key index is in every grouping set, so that no row this node produces has it rolled up to NULL.
      Parameters:
      index - a key index
      Returns:
      true when every set groups by it
    • producesRowOverEmptyInput

      public boolean producesRowOverEmptyInput()
      Whether this node produces a row over an empty input. It does when one of its grouping sets is empty, as an ungrouped aggregate gives its one row of totals.
      Returns:
      true when an empty input still produces a row
    • groupingIndex

      public OptionalInt groupingIndex(Operand argument)
      The key a GROUPING(k) argument names: the key whose expression is the same as argument wherever each was written, or else, for a bare name, the key whose output column it is.
      Parameters:
      argument - an argument of GROUPING
      Returns:
      the key's index, or empty when it names none
    • groupingIndex

      public static OptionalInt groupingIndex(List<GroupingKey> groupingKeys, Operand argument)
      The key of groupingKeys a GROUPING(k) argument names, as groupingIndex(Operand) finds it.
      Parameters:
      groupingKeys - the keys of a γ
      argument - an argument of GROUPING
      Returns:
      the key's index, or empty when it names none
    • accept

      public <R> R accept(RelNodeVisitor<R> visitor)
      Specified by:
      accept in interface RelNode
    • 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. All components in this record class are compared with Objects::equals(Object,Object).
      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.
    • groupingKeys

      public List<GroupingKey> groupingKeys()
      Returns the value of the groupingKeys record component.
      Returns:
      the value of the groupingKeys record component
    • groupingSets

      public List<List<Integer>> groupingSets()
      Returns the value of the groupingSets record component.
      Returns:
      the value of the groupingSets record component
    • aggregates

      public List<AggregateFunction> aggregates()
      Returns the value of the aggregates record component.
      Returns:
      the value of the aggregates record component
    • input

      public RelNode input()
      Returns the value of the input record component.
      Returns:
      the value of the input record component
    • location

      public SourceLocation location()
      Returns the value of the location record component.
      Specified by:
      location in interface RelNode
      Returns:
      the value of the location record component