Interface WindowFunction.Frame

Enclosing interface:
WindowFunction

public static interface WindowFunction.Frame
Window frame building methods. The frame is the set of rows of the partition that the aggregation functions and WindowFunction.firstValue(Expression), WindowFunction.lastValue(Expression) and WindowFunction.nthValue(Expression, Expression) read for the current row.

Get one with Exprs.window(). Use a shortcut (allRows(), rolling(int), expanding()), or build a custom frame in three steps: the frame type (rows(), range() or group()), the start and end of the frame (WindowFunction.Frame.From, WindowFunction.Frame.To), and the rows to exclude (WindowFunction.Frame.Exclusion).

Since:
15.0 - Southside
  • Method Details

    • rows

      Provides row based frame construction methods

      A rows frame is expressed relatively to the current row of the window. For example, a frame containing only the previous and next row without the current row will be defined as follows:

      
       def qapi = api.queryApi()
       qapi.exprs().window().rows().fromPreceding(1).toFollowing(1).excludeCurrent()
       
      Returns:
      the rows frame building methods
      Since:
      15.0 - Southside
    • range

      Provides range based frame construction methods

      A range frame is expressed relatively to the value of the sort by clause column (in this mode sortBy should contain only one column). The offset specifies the maximum difference between the current row value of this column value and the preceding or following rows.

      For example, here is a frame containing all the rows having a maximal value distance of 1:

      
       def qapi = api.queryApi()
       def table = qapi.tables().dataSource("myTable")
       qapi.source(
           table,
           [
               table.a,
               qapi.exprs().window().range().fromPreceding(1).toFollowing(1).noExclusion()
                   .sortBy([qapi.orders().ascNullsLast(table.a)])
                   .sum(table.a)
                   .as("sumOfCloseValues")
           ]
       ).stream { it.toList() }
       
      Returns:
      the range frame building methods
      Since:
      15.0 - Southside
    • group

      Provides a peer group based frame construction methods

      A peer group frame is expressed relatively to the order index of row group with respect to the sortBy clause (in this mode sortBy should be set). The offset specifies the number of peer groups to select before or after the current row peer group.

      For example, here is a frame containing the preceding and following peer group:

      
       def qapi = api.queryApi()
       def table = qapi.tables().dataSource("myTable")
       qapi.source(
           table,
           [
               table.category,
               qapi.exprs().window().group().fromPreceding(1).toFollowing(1).noExclusion()
                   .sortBy([qapi.orders().ascNullsLast(table.category)])
                   .avg(table.cost)
                   .as("avgCostIncludingNeighborCategories")
           ]
       ).stream { it.toList() }
       
      Returns:
      the group frame building methods
      Since:
      15.0 - Southside
    • rolling

      WindowFunction rolling(int nbRows)
      Defines a row frame of the nbRows last rows, including the current one.

      It is equivalent to

      
       api.queryApi().exprs().window().rows().fromPreceding(nbRows - 1).toCurrent().noExclusion()
       
      Parameters:
      nbRows - the number of rows of the rolling frame, at least 1; a smaller value fails with "nbRows should be >= 1"
      Returns:
      the window function building methods
      Since:
      15.0 - Southside
    • expanding

      WindowFunction expanding()
      Defines an extending row frame from the first row of the partition to the current row.

      It is equivalent to

      
       api.queryApi().exprs().window().rows().fromStart().toCurrent().noExclusion()
       
      Returns:
      the window function building methods
      Since:
      15.0 - Southside
    • allRows

      WindowFunction allRows()
      Defines a row frame containing all the rows of the current partition.

      It is equivalent to

      
       api.queryApi().exprs().window().rows().fromStart().toEnd().noExclusion()
       
      Returns:
      the window function building methods
      Since:
      15.0 - Southside