Class WindowFunctionHelper

java.lang.Object
dev.frostlake.functions.window.WindowFunctionHelper

public final class WindowFunctionHelper extends Object
Shared logic for Snowflake window functions. All methods work on pre-sorted partition row lists.
  • Method Details

    • ntile

      public static long ntile(List<Row> sortedRows, Row currentRow, int buckets)
      NTILE(n) — divides the sorted partition into n equal buckets, returns bucket number (1-based).
    • percentRank

      public static double percentRank(List<Row> sortedRows, Row currentRow, List<Object> orderValues)
      PERCENT_RANK — (rank - 1) / (N - 1); ranges [0, 1].
    • cumeDist

      public static double cumeDist(List<Row> sortedRows, Row currentRow, List<Object> orderValues)
      CUME_DIST — fraction of rows with order value <= current; ranges (0, 1].
    • ratioToReport

      public static Object ratioToReport(Object currentValue, List<Object> allValues)
      RATIO_TO_REPORT(expr) — value / SUM(value over partition).
    • firstValue

      public static Object firstValue(List<Object> frameValues, boolean ignoreNulls)
      FIRST_VALUE(expr) — value of expr in the first row of the window frame. With RESPECT NULLS (Snowflake's default) the first row's value is returned even if it is NULL; with IGNORE NULLS the first non-null value is returned (null if the frame has none).
    • lastValue

      public static Object lastValue(List<Object> frameValues, boolean ignoreNulls)
      LAST_VALUE(expr) — value of expr in the last row of the window frame. RESPECT NULLS (default) returns the last row's value even if NULL; IGNORE NULLS returns the last non-null value.
    • nthValue

      public static Object nthValue(List<Object> frameValues, int n)
      NTH_VALUE(expr, n) — value of expr in the nth row of the partition (1-based).
    • indexOf

      public static int indexOf(List<Row> rows, Row target)
    • compareValues

      public static int compareValues(Object a, Object b)
    • toDouble

      public static double toDouble(Object v)
    • sum

      public static Object sum(List<Object> values)
      SUM over the frame, ignoring nulls; null when the frame has no non-null values (SQL SUM semantics).
    • sum

      public static Object sum(List<Object> values, boolean variantArgument)
      The declared-VARIANT-argument flavour — live, SUM(v:b) OVER () is DOUBLE (SYSTEM$TYPEOF FLOAT) exactly like the grouped form, even over whole-number JSON values.
    • avg

      public static Object avg(List<Object> values)
      AVG over the frame, ignoring nulls; null when there are no non-null values. Shares the grouped AVG's Snowflake typing: fixed-point inputs average to a scale-(max input scale + 6) BigDecimal, any double/VARIANT input keeps the double average.
    • avg

      public static Object avg(List<Object> values, boolean variantArgument)
      See sum(List, boolean) — the same declared-VARIANT rule applied to a window AVG.
    • nonCumulativeAvg

      public static Object nonCumulativeAvg(List<Object> values, boolean variantArgument)
      AVG over a NON-CUMULATIVE window, which declares three decimals wider than its input rather than six and TRUNCATES to that scale — see AggregateNumerics.nonCumulativeWindowAvg(Iterable, boolean).
    • sum

      public static Object sum(List<List<Object>> partials, boolean variantArgument, boolean rowWise)
      SUM over a window frame given as the partials its FLOAT sum is read in — see AggregateNumerics.windowSum(List, boolean, boolean).
    • avg

      public static Object avg(List<List<Object>> partials, boolean variantArgument, boolean nonCumulative, boolean rowWise)
      AVG over a window frame given as the partials its FLOAT sum is read in, at a cumulative window's scale or a non-cumulative one's — see AggregateNumerics.windowAvg(List, boolean, boolean, boolean).
    • min

      public static Object min(List<Object> values)
      MIN over the frame (by compareValues(java.lang.Object, java.lang.Object)), ignoring nulls; preserves the element's type.
    • max

      public static Object max(List<Object> values)
      MAX over the frame (by compareValues(java.lang.Object, java.lang.Object)), ignoring nulls; preserves the element's type.