Class AggregateFunctions

java.lang.Object
dev.frostlake.executor.AggregateFunctions

public final class AggregateFunctions extends Object
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 Details

    • withinGroupOrderBy

      The ORDER BY of a trailing WITHIN GROUP (...) on this expression, or null if absent.
      Parameters:
      expr - the aggregate call's expression parse tree (null tolerated), scanned child by child for a WITHIN GROUP clause
      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)
      Evaluate PERCENTILE_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 BY
      orderedValues - 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 column
      approximateKey - whether the key's declared type is FLOAT / DOUBLE / REAL
      coercedKey - 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

      public static Object applyAggregateFunction(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). 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 null
      values - 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