Class GroupByOperator

java.lang.Object
dev.frostlake.executor.operators.GroupByOperator
All Implemented Interfaces:
Operator

public class GroupByOperator extends Object implements Operator
GROUP BY operator - groups rows and computes aggregates. This operator supports two modes: - EXPLICIT: Groups rows by specified columns and computes aggregates per group - IMPLICIT: Treats all rows as one group (for queries with aggregates but no GROUP BY)
  • Constructor Details

    • GroupByOperator

      public GroupByOperator(List<String> groupByExpressions, List<String> selectExpressions, RowExpressionEvaluator columnEvaluator, AggregateEvaluator aggregateEvaluator)
      Create an explicit GROUP BY operator.
      Parameters:
      groupByExpressions - Expressions to group by (e.g., "dept", "city")
      selectExpressions - Expressions in the SELECT list (may include aggregates)
      columnEvaluator - Evaluator to evaluate column expressions on a single row
      aggregateEvaluator - Evaluator to evaluate aggregate expressions on a group of rows
  • Method Details

    • implicit

      public static GroupByOperator implicit(List<String> selectExpressions, AggregateEvaluator aggregateEvaluator)
      Create an implicit GROUP BY operator (all rows as one group).
      Parameters:
      selectExpressions - Expressions in the SELECT list (must be aggregates)
      aggregateEvaluator - Evaluator to evaluate aggregate expressions on all rows
    • collateKeys

      public void collateKeys(List<CollationSpec> collations)
      The collation each group key compares under. Keys that are equal under their collation fall into ONE group, and the group reports the smallest of its values by raw text — two rules a binary grouping cannot express.
      Parameters:
      collations - one entry per group-by expression, null where the key carries no collation
    • foldSmallGroups

      public void foldSmallGroups(int minGroupSize, List<String> entityKey, List<String> columnNames)
      Fold groups smaller than minGroupSize into ONE remainder group, the way an AGGREGATION POLICY does: the remainder's group keys read NULL, its aggregates are computed over the folded rows when the remainder itself reaches the floor, and read NULL when it does not (live-verified).
      Parameters:
      minGroupSize - the policy's floor, or 0 to fold nothing
      entityKey - the columns whose DISTINCT values count as one entity, or empty to count rows
      columnNames - the source table's column names, index-aligned with each row's values
    • captureGroupRows

      public void captureGroupRows(List<List<Row>> sink)
      Capture each output row's source group rows into sink (parallel to the returned list), so a caller can apply HAVING aggregates that are not SELECT items over the original group.
    • captureLateralAliases

      public void captureLateralAliases(List<String> aliasNames, Map<String,Object> sink)
      Expose Snowflake lateral column aliases to later SELECT items of the same list, the grouped counterpart of ProjectOperator's alias sink: as each item of a group is evaluated left to right, its aliasNames entry (when non-null) is written to sink with the value just computed, so a later item can reference it (e.g. SELECT x AS n, LOWER(n) AS lo, COUNT(1) FROM t GROUP BY x). sink must be the same map the aggregate evaluator reads as its lateral context. It is cleared at the start of every group, so aliases never leak between groups, and only earlier items are ever visible — a forward reference stays unresolved, exactly as in the ungrouped projection path.
      Parameters:
      aliasNames - one entry per select expression, the item's alias or null when it has none
      sink - the shared alias-value map, written as each group's items are evaluated
    • presetNullAliases

      public void presetNullAliases(List<String> aliasNames)
    • execute

      public List<Row> execute(List<Row> input, OperatorContext context)
      Description copied from interface: Operator
      Execute this operator on the input rows.
      Specified by:
      execute in interface Operator
      Parameters:
      input - The input rows from the previous operator
      context - The execution context containing table metadata and other information
      Returns:
      The output rows after applying this operator
    • getDescription

      public String getDescription()
      Description copied from interface: Operator
      Get a description of this operator for debugging/logging.
      Specified by:
      getDescription in interface Operator
      Returns:
      A human-readable description of the operator
    • createImplicit

      public static GroupByOperator createImplicit(List<String> selectExpressions, AggregateEvaluator aggregateEvaluator)
      Create an implicit GROUP BY operator (all rows as one group).