Package dev.frostlake.executor.operators
Class GroupByOperator
java.lang.Object
dev.frostlake.executor.operators.GroupByOperator
- All Implemented Interfaces:
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 Summary
ConstructorsConstructorDescriptionGroupByOperator(List<String> groupByExpressions, List<String> selectExpressions, RowExpressionEvaluator columnEvaluator, AggregateEvaluator aggregateEvaluator) Create an explicit GROUP BY operator. -
Method Summary
Modifier and TypeMethodDescriptionvoidcaptureGroupRows(List<List<Row>> sink) Capture each output row's source group rows intosink(parallel to the returned list), so a caller can apply HAVING aggregates that are not SELECT items over the original group.voidExpose Snowflake lateral column aliases to later SELECT items of the same list, the grouped counterpart ofProjectOperator's alias sink: as each item of a group is evaluated left to right, itsaliasNamesentry (when non-null) is written tosinkwith the value just computed, so a later item can reference it (e.g.voidcollateKeys(List<CollationSpec> collations) The collation each group key compares under.static GroupByOperatorcreateImplicit(List<String> selectExpressions, AggregateEvaluator aggregateEvaluator) Create an implicit GROUP BY operator (all rows as one group).execute(List<Row> input, OperatorContext context) Execute this operator on the input rows.voidfoldSmallGroups(int minGroupSize, List<String> entityKey, List<String> columnNames) Fold groups smaller thanminGroupSizeinto 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).Get a description of this operator for debugging/logging.static GroupByOperatorimplicit(List<String> selectExpressions, AggregateEvaluator aggregateEvaluator) Create an implicit GROUP BY operator (all rows as one group).voidpresetNullAliases(List<String> aliasNames)
-
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 rowaggregateEvaluator- 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
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
Fold groups smaller thanminGroupSizeinto 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 nothingentityKey- the columns whose DISTINCT values count as one entity, or empty to count rowscolumnNames- the source table's column names, index-aligned with each row's values
-
captureGroupRows
Capture each output row's source group rows intosink(parallel to the returned list), so a caller can apply HAVING aggregates that are not SELECT items over the original group. -
captureLateralAliases
Expose Snowflake lateral column aliases to later SELECT items of the same list, the grouped counterpart ofProjectOperator's alias sink: as each item of a group is evaluated left to right, itsaliasNamesentry (when non-null) is written tosinkwith 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).sinkmust 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 nonesink- the shared alias-value map, written as each group's items are evaluated
-
presetNullAliases
-
execute
Description copied from interface:OperatorExecute this operator on the input rows. -
getDescription
Description copied from interface:OperatorGet a description of this operator for debugging/logging.- Specified by:
getDescriptionin interfaceOperator- 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).
-