Package dev.frostlake.executor
Class AggregateFunctions
java.lang.Object
dev.frostlake.executor.AggregateFunctions
Stateless aggregate-evaluation helpers extracted from
QueryExecutor: WITHIN GROUP parsing,
PERCENTILE_CONT/DISC evaluation, and the simple aggregate value reducer. All pure functions of
their arguments — they carry no engine instance state.-
Method Summary
Modifier and TypeMethodDescriptionstatic ObjectapplyAggregateFunction(String funcName, List<Object> values) The simple aggregate value reducer: folds an already-collected list of values with COUNT / SUM / AVG / MIN / MAX, preserving Snowflake's result typing (SUM keeps integer-ness, AVG widens fixed-point inputs, MIN/MAX keep the winning value's original numeric type).static ObjectevaluatePercentile(FrostlakeParser.FunctionCallExprContext funcCtx, List<Object> orderedValues, boolean approximateKey, boolean coercedKey) EvaluatePERCENTILE_CONT(p) / PERCENTILE_DISC(p) WITHIN GROUP (ORDER BY <expr>): the function argument is the fraction p in [0, 1]; the WITHIN GROUP ORDER BY expression supplies the data values.The ORDER BY of a trailingWITHIN GROUP (...)on this expression, or null if absent.
-
Method Details
-
withinGroupOrderBy
public static FrostlakeParser.OrderByClauseContext withinGroupOrderBy(FrostlakeParser.ExpressionContext expr) The ORDER BY of a trailingWITHIN GROUP (...)on this expression, or null if absent.- Parameters:
expr- the aggregate call's expression parse tree (null tolerated), scanned child by child for aWITHIN GROUPclause- Returns:
- the clause's ORDER BY parse tree, or null when the expression carries no WITHIN GROUP
-
evaluatePercentile
public static Object evaluatePercentile(FrostlakeParser.FunctionCallExprContext funcCtx, List<Object> orderedValues, boolean approximateKey, boolean coercedKey) EvaluatePERCENTILE_CONT(p) / PERCENTILE_DISC(p) WITHIN GROUP (ORDER BY <expr>): the function argument is the fraction p in [0, 1]; the WITHIN GROUP ORDER BY expression supplies the data values. Reuses the accumulators' interpolation / discrete logic (which sort internally, so the ORDER BY direction is immaterial to the result). Name, argument and ORDER BY column all come from the parse tree.- Parameters:
funcCtx- the PERCENTILE_CONT / PERCENTILE_DISC call's parse tree, carrying the function name, the fraction argument and the WITHIN GROUP ORDER BYorderedValues- the WITHIN GROUP key's value for each row of the group, already EVALUATED — the key is an expression like any other (n * 2, a CAST, a CASE, a qualified name), and resolving it by column NAME fed the accumulator nulls for everything but a bare unqualified columnapproximateKey- whether the key's declared type is FLOAT / DOUBLE / REALcoercedKey- whether that key is declared VARCHAR or VARIANT, converted value by value- Returns:
- the continuous (interpolated) or discrete percentile of the group's values, or null for an empty group
-
applyAggregateFunction
The simple aggregate value reducer: folds an already-collected list of values with COUNT / SUM / AVG / MIN / MAX, preserving Snowflake's result typing (SUM keeps integer-ness, AVG widens fixed-point inputs, MIN/MAX keep the winning value's original numeric type). COUNT counts every value it is handed — the caller collects exactly what its aggregate should see.- Parameters:
funcName- the aggregate's upper-cased name; anything but the five above yields nullvalues- the values collected for the group, in row order- Returns:
- the folded aggregate value, or null for an empty list or an unrecognized function name
-