Interface WindowFunction


public interface WindowFunction
Window function building methods.

A window function is a very powerful means to compute aggregate-like functions over some portion of the rows selected by a query. This API provides equivalents of the standard SQL window functions.

Get a window function builder with Exprs.window(), that is api.queryApi().exprs().window(). Build it in this order:

  1. Choose the window frame: WindowFunction.Frame.allRows(), WindowFunction.Frame.rolling(int), WindowFunction.Frame.expanding(), or a custom frame built with WindowFunction.Frame.rows(), WindowFunction.Frame.range() or WindowFunction.Frame.group().
  2. Optionally, set partitionBy(List), sortBy(List) and filter(Expression). Each of them can be set only once in a chain.
  3. End with the function to compute, for example rank(), lag(Expression) or sum(Expression). It returns an Expression that you select in the query.

For example, this query computes for each row the average quantity of the current and the two previous transactions of the same product:


 def qapi = api.queryApi()
 def t = qapi.tables().datamart("Sales")
 qapi.source(
     t,
     [
         t.sku,
         t.InvoiceDate,
         t.Quantity,
         qapi.exprs().window()
             .rolling(3)
             .partitionBy([t.sku])
             .sortBy([qapi.orders().ascNullsLast(t.InvoiceDate)])
             .avg(t.Quantity)
             .as("avgQuantityLast3")
     ]
 ).stream { it.toList() }
 
Since:
15.0 - Southside
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    Window frame building methods.
  • Method Summary

    Modifier and Type
    Method
    Description
    avg(Expression expression)
    Builds a window function computing the average of the given expression over the frame.
    Builds a window function computing the number of rows into the frame.
    Builds a window function computing the number of non-null expression value over the frame.
    Builds a window function computing the cumulative distribution, that is (number of partition rows preceding or peers with current row) / (total partition rows).
    Builds a window function computing the rank of the current row, without gaps; this function effectively counts peer groups.
    Defines a filter to be applied only on the window function.
    firstValue(Expression expression)
    Builds a window function computing the value evaluated at the row that is the first row of the window frame.
    lag(Expression expression)
    Builds a window function computing value evaluated at the row before the current row within the partition; if there is no such row, instead returns NULL.
    lag(Expression expression, Expression offset)
    Builds a window function computing value evaluated at the row that is offset rows before the current row within the partition; if there is no such row, instead returns NULL.
    lag(Expression expression, Expression offset, Expression defaultValue)
    Builds a window function computing value evaluated at the row that is offset rows before the current row within the partition; if there is no such row, instead returns default (which must be of a type compatible with value).
    lastValue(Expression expression)
    Builds a window function computing the value evaluated at the row that is the last row of the window frame.
    lead(Expression expression)
    Builds a window function computing value evaluated at the row after the current row within the partition; if there is no such row, instead returns NULL.
    lead(Expression expression, Expression offset)
    Builds a window function computing value evaluated at the row that is offset rows after the current row within the partition; if there is no such row, instead returns NULL.
    lead(Expression expression, Expression offset, Expression defaultValue)
    Builds a window function computing value evaluated at the row that is offset rows after the current row within the partition; if there is no such row, instead returns default (which must be of a type compatible with value).
    max(Expression expression)
    Builds a window function computing the maximum of the given expression over the frame.
    min(Expression expression)
    Builds a window function computing the minimum of the given expression over the frame.
    nthValue(Expression expression, Expression offset)
    Builds a window function computing the value evaluated at the row that is the n'th row of the window frame (counting from 1); returns NULL if there is no such row.
    ntile(Expression expression)
    Builds a window function computing an integer ranging from 1 to the argument value, dividing the partition as equally as possible.
    Defines the group of rows to be processed separately.
    Builds a window function computing the relative rank of the current row, that is (rank - 1) / (total partition rows - 1).
    Builds a window function computing the rank of the current row, with gaps; that is, the row_number of the first row in its peer group.
    Builds a window function computing the number of the current row within its partition, counting from 1.
    Defines the ordering of the window.
    sum(Expression expression)
    Builds a window function computing the sum of the given expression over the frame.
  • Method Details

    • partitionBy

      WindowFunction partitionBy(List<Expression> partitions)
      Defines the group of rows to be processed separately.

      Like PipelineStage.aggregateBy(Function, Function), each group is defined by an existing unique combination of values of the given list of expression.

      Here is a window function that computes the average cost of each row's category:

      
       def qapi = api.queryApi()
       def table = qapi.tables().dataSource("costs")
       qapi.source(
           table,
           [
               table.sku,
               table.category,
               table.cost,
               qapi.exprs().window()
                   .allRows()
                   .partitionBy([table.category])
                   .avg(table.cost)
                   .as("avgCategoryCost")
           ]
       ).stream { it.toList() }
       

      The partition can be set only once: calling this method on a builder that already has a non-empty partition fails with "partition is already set".

      Parameters:
      partitions - the list of expressions defining the partition
      Returns:
      the window function building methods
      Since:
      15.0 - Southside
    • filter

      WindowFunction filter(Expression filter)
      Defines a filter to be applied only on the window function.

      Filter can only be used on aggregation functions, i.e. sum(Expression), avg(Expression), countAll(), countNonNull(Expression), min(Expression) and max(Expression).

      For example, here is a window function that computes the number of rows that have a cost lower than 5 in the given category:

      
       def qapi = api.queryApi()
       def table = qapi.tables().dataSource("costs")
       qapi.source(
           table,
           [
               table.sku,
               table.category,
               table.cost,
               qapi.exprs().window()
                   .allRows()
                   .partitionBy([table.category])
                   .filter(table.cost.lessThan(5))
                   .countAll()
                   .as("nbLowCost")
           ]
       ).stream { it.toList() }
       

      The filter can be set only once: calling this method on a builder that already has a filter fails with "filter is already set".

      Parameters:
      filter - the filter to be applied
      Returns:
      the window function building methods
      Since:
      15.0 - Southside
    • sortBy

      WindowFunction sortBy(List<Orders.Order> orders)
      Defines the ordering of the window.

      Like PipelineStage.sortBy(Function), the ordering is defined as a list of Orders.Order that can be created thanks to QueryApi.orders().

      For example here is a query returning the skus having the 3 lowest cost by category:

      
       def qapi = api.queryApi()
       def table = qapi.tables().dataSource("costs")
       qapi.source(
           table,
           [
               table.sku,
               table.category,
               table.cost,
               qapi.exprs().window()
                   .allRows()
                   .partitionBy([table.category])
                   .sortBy([qapi.orders().ascNullsLast(table.cost)])
                   .rank()
                   .as("costRank")
           ]
       ).filter { prev -> prev.costRank.lessOrEqual(3) }
       .stream { it.toList() }
       

      The ordering can be set only once: calling this method on a builder that already has a non-empty ordering fails with "sortBy is already set".

      Parameters:
      orders - list of orders
      Returns:
      the window function building methods
      Since:
      15.0 - Southside
    • rank

      Expression rank()
      Builds a window function computing the rank of the current row, with gaps; that is, the row_number of the first row in its peer group.
      Returns:
      the expression representing this window function
      Since:
      15.0 - Southside
    • rowNumber

      Expression rowNumber()
      Builds a window function computing the number of the current row within its partition, counting from 1.
      Returns:
      the expression representing this window function
      Since:
      15.0 - Southside
    • denseRank

      Expression denseRank()
      Builds a window function computing the rank of the current row, without gaps; this function effectively counts peer groups.
      Returns:
      the expression representing this window function
      Since:
      15.0 - Southside
    • percentRank

      Expression percentRank()
      Builds a window function computing the relative rank of the current row, that is (rank - 1) / (total partition rows - 1).

      The value thus ranges from 0 to 1 inclusive.

      Returns:
      the expression representing this window function
      Since:
      15.0 - Southside
      See Also:
    • cumeDist

      Expression cumeDist()
      Builds a window function computing the cumulative distribution, that is (number of partition rows preceding or peers with current row) / (total partition rows).

      The value thus ranges from 1/N to 1.

      Returns:
      the expression representing this window function
      Since:
      15.0 - Southside
      See Also:
    • ntile

      Expression ntile(Expression expression)
      Builds a window function computing an integer ranging from 1 to the argument value, dividing the partition as equally as possible.
      Parameters:
      expression - the number of buckets, as an integer expression, for example qapi.exprs().integer(10)
      Returns:
      the expression representing this window function
      Since:
      15.0 - Southside
    • lag

      Expression lag(Expression expression, Expression offset, Expression defaultValue)
      Builds a window function computing value evaluated at the row that is offset rows before the current row within the partition; if there is no such row, instead returns default (which must be of a type compatible with value).

      Both offset and default are evaluated with respect to the current row.

      Parameters:
      expression - the value to read from the earlier row
      offset - the number of rows before the current row, as an integer expression
      defaultValue - the value to return when there is no such row
      Returns:
      the expression representing this window function
      Since:
      15.0 - Southside
      See Also:
    • lag

      Expression lag(Expression expression, Expression offset)
      Builds a window function computing value evaluated at the row that is offset rows before the current row within the partition; if there is no such row, instead returns NULL.

      Offset is evaluated with respect to the current row.

      Parameters:
      expression - the value to read from the earlier row
      offset - the number of rows before the current row, as an integer expression
      Returns:
      the expression representing this window function
      Since:
      15.1 - Southside
      See Also:
    • lag

      Expression lag(Expression expression)
      Builds a window function computing value evaluated at the row before the current row within the partition; if there is no such row, instead returns NULL.

      For example, this query reads for each price the previous price of the same product:

      
       def qapi = api.queryApi()
       def t = qapi.tables().dataSource("PriceHistory")
       def wnd = qapi.exprs().window()
           .allRows()
           .partitionBy([t.sku])
           .sortBy([qapi.orders().ascNullsLast(t.ValidFrom)])
       qapi.source(t, [t.sku, t.ValidFrom, t.Price, wnd.lag(t.Price).as("PreviousPrice")])
           .stream { it.toList() }
       
      Parameters:
      expression - the value to read from the previous row
      Returns:
      the expression representing this window function
      Since:
      15.1 - Southside
      See Also:
    • lead

      Expression lead(Expression expression, Expression offset, Expression defaultValue)
      Builds a window function computing value evaluated at the row that is offset rows after the current row within the partition; if there is no such row, instead returns default (which must be of a type compatible with value).

      Both offset and default are evaluated with respect to the current row.

      Parameters:
      expression - the value to read from the later row
      offset - the number of rows after the current row, as an integer expression
      defaultValue - the value to return when there is no such row
      Returns:
      the expression representing this window function
      Since:
      15.0 - Southside
      See Also:
    • lead

      Expression lead(Expression expression, Expression offset)
      Builds a window function computing value evaluated at the row that is offset rows after the current row within the partition; if there is no such row, instead returns NULL.

      Offset is evaluated with respect to the current row.

      Parameters:
      expression - the value to read from the later row
      offset - the number of rows after the current row, as an integer expression
      Returns:
      the expression representing this window function
      Since:
      15.1 - Southside
      See Also:
    • lead

      Expression lead(Expression expression)
      Builds a window function computing value evaluated at the row after the current row within the partition; if there is no such row, instead returns NULL.
      Parameters:
      expression - the value to read from the next row
      Returns:
      the expression representing this window function
      Since:
      15.1 - Southside
      See Also:
    • firstValue

      Expression firstValue(Expression expression)
      Builds a window function computing the value evaluated at the row that is the first row of the window frame.
      Parameters:
      expression - the value to read from the first row of the frame
      Returns:
      the expression representing this window function
      Since:
      15.0 - Southside
    • lastValue

      Expression lastValue(Expression expression)
      Builds a window function computing the value evaluated at the row that is the last row of the window frame.
      Parameters:
      expression - the value to read from the last row of the frame
      Returns:
      the expression representing this window function
      Since:
      15.0 - Southside
    • nthValue

      Expression nthValue(Expression expression, Expression offset)
      Builds a window function computing the value evaluated at the row that is the n'th row of the window frame (counting from 1); returns NULL if there is no such row.
      Parameters:
      expression - the value to read from the n'th row of the frame
      offset - the position n of the row in the frame, counting from 1, as an integer expression
      Returns:
      the expression representing this window function
      Since:
      15.0 - Southside
    • sum

      Expression sum(Expression expression)
      Builds a window function computing the sum of the given expression over the frame.
      Parameters:
      expression - the value to sum
      Returns:
      the expression representing this window function
      Since:
      15.0 - Southside
    • avg

      Expression avg(Expression expression)
      Builds a window function computing the average of the given expression over the frame.
      Parameters:
      expression - the value to average
      Returns:
      the expression representing this window function
      Since:
      15.0 - Southside
    • min

      Expression min(Expression expression)
      Builds a window function computing the minimum of the given expression over the frame.
      Parameters:
      expression - the value to get the minimum of
      Returns:
      the expression representing this window function
      Since:
      15.0 - Southside
    • max

      Expression max(Expression expression)
      Builds a window function computing the maximum of the given expression over the frame.
      Parameters:
      expression - the value to get the maximum of
      Returns:
      the expression representing this window function
      Since:
      15.0 - Southside
    • countNonNull

      Expression countNonNull(Expression expression)
      Builds a window function computing the number of non-null expression value over the frame.
      Parameters:
      expression - the value to count when it is not null
      Returns:
      the expression representing this window function
      Since:
      15.0 - Southside
    • countAll

      Expression countAll()
      Builds a window function computing the number of rows into the frame.
      Returns:
      the expression representing this window function
      Since:
      15.0 - Southside