ClickHouse/tests/testflows/window_functions/requirements/requirements.md
2021-04-27 08:55:01 -04:00

114 KiB

SRS019 ClickHouse Window Functions

Software Requirements Specification

Table of Contents

Revision History

This document is stored in an electronic form using Git source control management software hosted in a GitHub Repository. All the updates are tracked using the Revision History.

Introduction

This software requirements specification covers requirements for Map(key, value) data type in ClickHouse.

Requirements

General

RQ.SRS-019.ClickHouse.WindowFunctions

version: 1.0

ClickHouse SHALL support window functions that produce a result for each row inside the window.

RQ.SRS-019.ClickHouse.WindowFunctions.NonDistributedTables

version: 1.0

ClickHouse SHALL support correct operation of window functions on non-distributed table engines such as MergeTree.

RQ.SRS-019.ClickHouse.WindowFunctions.DistributedTables

version: 1.0

ClickHouse SHALL support correct operation of window functions on Distributed table engine.

Window Specification

RQ.SRS-019.ClickHouse.WindowFunctions.WindowSpec

version: 1.0

ClickHouse SHALL support defining a window using window specification clause. The window_spec SHALL be defined as

window_spec:
   [partition_clause] [order_clause] [frame_clause]

that SHALL specify how to partition query rows into groups for processing by the window function.

PARTITION Clause

RQ.SRS-019.ClickHouse.WindowFunctions.PartitionClause

version: 1.0

ClickHouse SHALL support partition_clause that indicates how to divide the query rows into groups. The partition_clause SHALL be defined as

partition_clause:
    PARTITION BY expr [, expr] ...

RQ.SRS-019.ClickHouse.WindowFunctions.PartitionClause.MultipleExpr

version: 1.0

ClickHouse SHALL support partitioning by more than one expr in the partition_clause definition.

For example,

SELECT x,s, sum(x) OVER (PARTITION BY x,s) FROM values('x Int8, s String', (1,'a'),(1,'b'),(2,'b'))
┌─x─┬─s─┬─sum(x) OVER (PARTITION BY x, s)─┐
│ 1 │ a │                               1 │
│ 1 │ b │                               1 │
│ 2 │ b │                               2 │
└───┴───┴─────────────────────────────────┘

RQ.SRS-019.ClickHouse.WindowFunctions.PartitionClause.MissingExpr.Error

version: 1.0

ClickHouse SHALL return an error if expr is missing in the partition_clause definition.

SELECT sum(number) OVER (PARTITION BY) FROM numbers(1,3)

RQ.SRS-019.ClickHouse.WindowFunctions.PartitionClause.InvalidExpr.Error

version: 1.0

ClickHouse SHALL return an error if expr is invalid in the partition_clause definition.

ORDER Clause

RQ.SRS-019.ClickHouse.WindowFunctions.OrderClause

version: 1.0

ClickHouse SHALL support order_clause that indicates how to sort rows in each window.

order_clause

The order_clause SHALL be defined as

order_clause:
    ORDER BY expr [ASC|DESC] [, expr [ASC|DESC]] ...

RQ.SRS-019.ClickHouse.WindowFunctions.OrderClause.MultipleExprs

version: 1.0

ClickHouse SHALL return support using more than one expr in the order_clause definition.

For example,

SELECT x,s, sum(x) OVER (ORDER BY x DESC, s DESC) FROM values('x Int8, s String', (1,'a'),(1,'b'),(2,'b'))
┌─x─┬─s─┬─sum(x) OVER (ORDER BY x DESC, s DESC)─┐
│ 2 │ b │                                     2 │
│ 1 │ b │                                     3 │
│ 1 │ a │                                     4 │
└───┴───┴───────────────────────────────────────┘

RQ.SRS-019.ClickHouse.WindowFunctions.OrderClause.MissingExpr.Error

version: 1.0

ClickHouse SHALL return an error if expr is missing in the order_clause definition.

RQ.SRS-019.ClickHouse.WindowFunctions.OrderClause.InvalidExpr.Error

version: 1.0

ClickHouse SHALL return an error if expr is invalid in the order_clause definition.

FRAME Clause

RQ.SRS-019.ClickHouse.WindowFunctions.FrameClause

version: 1.0

ClickHouse SHALL support frame_clause that SHALL specify a subset of the current window.

The frame_clause SHALL be defined as

frame_clause:
    {ROWS | RANGE } frame_extent

ROWS

RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame

version: 1.0

ClickHouse SHALL support ROWS frame to define beginning and ending row positions. Offsets SHALL be differences in row numbers from the current row number.

ROWS frame_extent

See frame_extent definition.

RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.MissingFrameExtent.Error

version: 1.0

ClickHouse SHALL return an error if the ROWS frame clause is defined without frame_extent.

For example,

SELECT number,sum(number) OVER (ORDER BY number ROWS) FROM numbers(1,3)
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.InvalidFrameExtent.Error

version: 1.0

ClickHouse SHALL return an error if the ROWS frame clause has invalid frame_extent.

For example,

SELECT number,sum(number) OVER (ORDER BY number ROWS '1') FROM numbers(1,3)
ROWS CURRENT ROW
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Start.CurrentRow

version: 1.0

ClickHouse SHALL include only the current row in the window partition when ROWS CURRENT ROW frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS CURRENT ROW) FROM numbers(1,2)
┌─number─┬─sum(number) OVER (ROWS BETWEEN CURRENT ROW AND CURRENT ROW)─┐
│      11 │
│      22 │
└────────┴─────────────────────────────────────────────────────────────┘
ROWS UNBOUNDED PRECEDING
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Start.UnboundedPreceding

version: 1.0

ClickHouse SHALL include all rows before and including the current row in the window partition when ROWS UNBOUNDED PRECEDING frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS UNBOUNDED PRECEDING) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)─┐
│      11 │
│      23 │
│      36 │
└────────┴─────────────────────────────────────────────────────────────────────┘
ROWS expr PRECEDING
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Start.ExprPreceding

version: 1.0

ClickHouse SHALL include expr rows before and including the current row in the window partition when ROWS expr PRECEDING frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS 1 PRECEDING) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)─┐
│      11 │
│      23 │
│      35 │
└────────┴─────────────────────────────────────────────────────────────┘
ROWS UNBOUNDED FOLLOWING
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Start.UnboundedFollowing.Error

version: 1.0

ClickHouse SHALL return an error when ROWS UNBOUNDED FOLLOWING frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS UNBOUNDED FOLLOWING) FROM numbers(1,3)
ROWS expr FOLLOWING
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Start.ExprFollowing.Error

version: 1.0

ClickHouse SHALL return an error when ROWS expr FOLLOWING frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS 1 FOLLOWING) FROM numbers(1,3)
ROWS BETWEEN CURRENT ROW
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.CurrentRow.CurrentRow

version: 1.0

ClickHouse SHALL include only the current row in the window partition when ROWS BETWEEN CURRENT ROW AND CURRENT ROW frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS BETWEEN CURRENT ROW AND CURRENT ROW) FROM numbers(1,2)
┌─number─┬─sum(number) OVER (ROWS BETWEEN CURRENT ROW AND CURRENT ROW)─┐
│      11 │
│      22 │
└────────┴─────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.CurrentRow.UnboundedPreceding.Error

version: 1.0

ClickHouse SHALL return an error when ROWS BETWEEN CURRENT ROW AND UNBOUNDED PRECEDING frame is specified.

RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.CurrentRow.ExprPreceding.Error

version: 1.0

ClickHouse SHALL return an error when ROWS BETWEEN CURRENT ROW AND expr PRECEDING frame is specified.

RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.CurrentRow.UnboundedFollowing

version: 1.0

ClickHouse SHALL include the current row and all the following rows in the window partition when ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)─┐
│      16 │
│      25 │
│      33 │
└────────┴─────────────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.CurrentRow.ExprFollowing

version: 1.0

ClickHouse SHALL include the current row and the expr rows that are following the current row in the window partition when ROWS BETWEEN CURRENT ROW AND expr FOLLOWING frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING)─┐
│      13 │
│      25 │
│      33 │
└────────┴─────────────────────────────────────────────────────────────┘
ROWS BETWEEN UNBOUNDED PRECEDING
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.UnboundedPreceding.CurrentRow

version: 1.0

ClickHouse SHALL include all the rows before and including the current row in the window partition when ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)─┐
│      11 │
│      23 │
│      36 │
└────────┴─────────────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.UnboundedPreceding.UnboundedPreceding.Error

version: 1.0

ClickHouse SHALL return an error when ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED PRECEDING frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED PRECEDING) FROM numbers(1,3)
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.UnboundedPreceding.ExprPreceding

version: 1.0

ClickHouse SHALL include all the rows until and including the current row minus expr rows preceding it when ROWS BETWEEN UNBOUNDED PRECEDING AND expr PRECEDING frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)─┐
│      10 │
│      21 │
│      33 │
└────────┴─────────────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.UnboundedPreceding.UnboundedFollowing

version: 1.0

ClickHouse SHALL include all rows in the window partition when ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)─┐
│      16 │
│      26 │
│      36 │
└────────┴─────────────────────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.UnboundedPreceding.ExprFollowing

version: 1.0

ClickHouse SHALL include all the rows until and including the current row plus expr rows following it when ROWS BETWEEN UNBOUNDED PRECEDING AND expr FOLLOWING frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING)─┐
│      13 │
│      26 │
│      36 │
└────────┴─────────────────────────────────────────────────────────────────────┘
ROWS BETWEEN UNBOUNDED FOLLOWING
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.UnboundedFollowing.Error

version: 1.0

ClickHouse SHALL return an error when UNBOUNDED FOLLOWING is specified as the start of the frame, including

  • ROWS BETWEEN UNBOUNDED FOLLOWING AND CURRENT ROW
  • ROWS BETWEEN UNBOUNDED FOLLOWING AND UNBOUNDED PRECEDING
  • ROWS BETWEEN UNBOUNDED FOLLOWING AND UNBOUNDED FOLLOWING
  • ROWS BETWEEN UNBOUNDED FOLLOWING AND expr PRECEDING
  • ROWS BETWEEN UNBOUNDED FOLLOWING AND expr FOLLOWING

For example,

SELECT number,sum(number) OVER (ROWS BETWEEN UNBOUNDED FOLLOWING AND CURRENT ROW) FROM numbers(1,3)
ROWS BETWEEN expr FOLLOWING
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.ExprFollowing.Error

version: 1.0

ClickHouse SHALL return an error when expr FOLLOWING is specified as the start of the frame and it points to a row that is after the start of the frame inside the window partition such as the following cases

  • ROWS BETWEEN expr FOLLOWING AND CURRENT ROW
  • ROWS BETWEEN expr FOLLOWING AND UNBOUNDED PRECEDING
  • ROWS BETWEEN expr FOLLOWING AND expr PRECEDING

For example,

SELECT number,sum(number) OVER (ROWS BETWEEN 1 FOLLOWING AND CURRENT ROW) FROM numbers(1,3)
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.ExprFollowing.ExprFollowing.Error

version: 1.0

ClickHouse SHALL return an error when ROWS BETWEEN expr FOLLOWING AND expr FOLLOWING is specified and the end of the frame specified by the expr FOLLOWING is a row that is before the row specified by the frame start.

SELECT number,sum(number) OVER (ROWS BETWEEN 1 FOLLOWING AND 0 FOLLOWING) FROM numbers(1,3)
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.ExprFollowing.UnboundedFollowing

version: 1.0

ClickHouse SHALL include all the rows from and including current row plus expr rows following it until and including the last row in the window partition when ROWS BETWEEN expr FOLLOWING AND UNBOUNDED FOLLOWING frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING)─┐
│      15 │
│      23 │
│      30 │
└────────┴─────────────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.ExprFollowing.ExprFollowing

version: 1.0

ClickHouse SHALL include the rows from and including current row plus expr following it until and including the row specified by the frame end when the frame end is the current row plus expr following it is right at or after the start of the frame when ROWS BETWEEN expr FOLLOWING AND expr FOLLOWING frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING)─┐
│      15 │
│      23 │
│      30 │
└────────┴─────────────────────────────────────────────────────────────┘
ROWS BETWEEN expr PRECEDING
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.ExprPreceding.CurrentRow

version: 1.0

ClickHouse SHALL include the rows from and including current row minus expr rows preceding it until and including the current row in the window frame when ROWS BETWEEN expr PRECEDING AND CURRENT ROW frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)─┐
│      11 │
│      23 │
│      35 │
└────────┴─────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.ExprPreceding.UnboundedPreceding.Error

version: 1.0

ClickHouse SHALL return an error when ROWS BETWEEN expr PRECEDING AND UNBOUNDED PRECEDING frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS BETWEEN 1 PRECEDING AND UNBOUNDED PRECEDING) FROM numbers(1,3)
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.ExprPreceding.UnboundedFollowing

version: 1.0

ClickHouse SHALL include the rows from and including current row minus expr rows preceding it until and including the last row in the window partition when ROWS BETWEEN expr PRECEDING AND UNBOUNDED FOLLOWING frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (ROWS BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING)─┐
│      16 │
│      26 │
│      35 │
└────────┴─────────────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.ExprPreceding.ExprPreceding.Error

version: 1.0

ClickHouse SHALL return an error when the frame end specified by the expr PRECEDING evaluates to a row that is before the row specified by the frame start in the window partition when ROWS BETWEEN expr PRECEDING AND expr PRECEDING frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS BETWEEN 1 PRECEDING AND 2 PRECEDING) FROM numbers(1,3)
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.ExprPreceding.ExprPreceding

version: 1.0

ClickHouse SHALL include the rows from and including current row minus expr rows preceding it until and including the current row minus expr rows preceding it if the end of the frame is after the frame start in the window partition when ROWS BETWEEN expr PRECEDING AND expr PRECEDING frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS BETWEEN 1 PRECEDING AND 0 PRECEDING) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (ROWS BETWEEN 1 PRECEDING AND 0 PRECEDING)─┐
│      11 │
│      23 │
│      35 │
└────────┴─────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RowsFrame.Between.ExprPreceding.ExprFollowing

version: 1.0

ClickHouse SHALL include the rows from and including current row minus expr rows preceding it until and including the current row plus expr rows following it in the window partition when ROWS BETWEEN expr PRECEDING AND expr FOLLOWING frame is specified.

For example,

SELECT number,sum(number) OVER (ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)─┐
│      13 │
│      26 │
│      35 │
└────────┴─────────────────────────────────────────────────────────────┘

RANGE

RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame

version: 1.0

ClickHouse SHALL support RANGE frame to define rows within a value range. Offsets SHALL be differences in row values from the current row value.

RANGE frame_extent

See frame_extent definition.

RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.DataTypes.DateAndDateTime

version: 1.0

ClickHouse SHALL support RANGE frame over columns with Date and DateTime data types.

RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.DataTypes.IntAndUInt

version: 1.0

ClickHouse SHALL support RANGE frame over columns with numerical data types such IntX and UIntX.

RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.MultipleColumnsInOrderBy.Error

version: 1.0

ClickHouse SHALL return an error if the RANGE frame definition is used with ORDER BY that uses multiple columns.

RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.MissingFrameExtent.Error

version: 1.0

ClickHouse SHALL return an error if the RANGE frame definition is missing frame_extent.

RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.InvalidFrameExtent.Error

version: 1.0

ClickHouse SHALL return an error if the RANGE frame definition has invalid frame_extent.

CURRENT ROW Peers
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.CurrentRow.Peers

version: 1.0

ClickHouse for the RANGE frame SHALL define the peers of the CURRENT ROW to be all the rows that are inside the same order bucket.

RANGE CURRENT ROW
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Start.CurrentRow.WithoutOrderBy

version: 1.0

ClickHouse SHALL include all rows in the window partition when RANGE CURRENT ROW frame is specified without the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (RANGE CURRENT ROW) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (RANGE BETWEEN CURRENT ROW AND CURRENT ROW)─┐
│      16 │
│      26 │
│      36 │
└────────┴──────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Start.CurrentRow.WithOrderBy

version: 1.0

ClickHouse SHALL include all rows that are current row peers in the window partition when RANGE CURRENT ROW frame is specified with the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (ORDER BY number RANGE CURRENT ROW) FROM values('number Int8', (1),(1),(2),(3))
┌─number─┬─sum(number) OVER (ORDER BY number ASC RANGE BETWEEN CURRENT ROW AND CURRENT ROW)─┐
│      12 │
│      12 │
│      22 │
│      33 │
└────────┴──────────────────────────────────────────────────────────────────────────────────┘
RANGE UNBOUNDED FOLLOWING
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Start.UnboundedFollowing.Error

version: 1.0

ClickHouse SHALL return an error when RANGE UNBOUNDED FOLLOWING frame is specified with or without order by as UNBOUNDED FOLLOWING SHALL not be supported as frame_start.

For example,

SELECT number,sum(number) OVER (RANGE UNBOUNDED FOLLOWING) FROM numbers(1,3)
RANGE UNBOUNDED PRECEDING
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Start.UnboundedPreceding.WithoutOrderBy

version: 1.0

ClickHouse SHALL include all rows in the window partition when RANGE UNBOUNDED PRECEDING frame is specified without the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (RANGE UNBOUNDED PRECEDING) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)─┐
│      16 │
│      26 │
│      36 │
└────────┴──────────────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Start.UnboundedPreceding.WithOrderBy

version: 1.0

ClickHouse SHALL include rows with values from and including the first row until and including all current row peers in the window partition when RANGE UNBOUNDED PRECEDING frame is specified with the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (ORDER BY number RANGE UNBOUNDED PRECEDING) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (ORDER BY number ASC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)─┐
│      11 │
│      23 │
│      36 │
└────────┴──────────────────────────────────────────────────────────────────────────────────────────┘
RANGE expr PRECEDING
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Start.ExprPreceding.WithoutOrderBy.Error

version: 1.0

ClickHouse SHALL return an error when RANGE expr PRECEDING frame is specified without the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (RANGE 1 PRECEDING) FROM numbers(1,3)
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Start.ExprPreceding.OrderByNonNumericalColumn.Error

version: 1.0

ClickHouse SHALL return an error when RANGE expr PRECEDING is used with ORDER BY clause over a non-numerical column.

RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Start.ExprPreceding.WithOrderBy

version: 1.0

ClickHouse SHALL include rows with values from and including current row value minus expr until and including the value for the current row when RANGE expr PRECEDING frame is specified with the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (ORDER BY number RANGE 1 PRECEDING) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (ORDER BY number ASC RANGE BETWEEN 1 PRECEDING AND CURRENT ROW)─┐
│      11 │
│      23 │
│      35 │
└────────┴──────────────────────────────────────────────────────────────────────────────────┘
RANGE expr FOLLOWING
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Start.ExprFollowing.WithoutOrderBy.Error

version: 1.0

ClickHouse SHALL return an error when RANGE expr FOLLOWING frame is specified without the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (RANGE 1 FOLLOWING) FROM numbers(1,3)
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Start.ExprFollowing.WithOrderBy.Error

version: 1.0

ClickHouse SHALL return an error when RANGE expr FOLLOWING frame is specified wit the ORDER BY clause as the value for the frame start cannot be larger than the value for the frame end.

For example,

SELECT number,sum(number) OVER (ORDER BY number RANGE 1 FOLLOWING) FROM numbers(1,3)
RANGE BETWEEN CURRENT ROW
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.CurrentRow.CurrentRow

version: 1.0

ClickHouse SHALL include all current row peers in the window partition when RANGE BETWEEN CURRENT ROW AND CURRENT ROW frame is specified with or without the ORDER BY clause.

For example,

Without ORDER BY

SELECT number,sum(number) OVER (RANGE BETWEEN CURRENT ROW AND CURRENT ROW) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (RANGE BETWEEN CURRENT ROW AND CURRENT ROW)─┐
│      16 │
│      26 │
│      36 │
└────────┴──────────────────────────────────────────────────────────────┘

With ORDER BY

SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN CURRENT ROW AND CURRENT ROW) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (ORDER BY number ASC RANGE BETWEEN CURRENT ROW AND CURRENT ROW)─┐
│      11 │
│      22 │
│      33 │
└────────┴──────────────────────────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.CurrentRow.UnboundedPreceding.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN CURRENT ROW AND UNBOUNDED PRECEDING frame is specified with or without the ORDER BY clause.

For example,

Without ORDER BY

SELECT number,sum(number) OVER (RANGE BETWEEN CURRENT ROW AND UNBOUNDED PRECEDING) FROM numbers(1,3)

With ORDER BY

SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN CURRENT ROW AND UNBOUNDED PRECEDING) FROM numbers(1,3)
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.CurrentRow.UnboundedFollowing

version: 1.0

ClickHouse SHALL include all rows with values from and including current row peers until and including the last row in the window partition when RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING frame is specified with or without the ORDER BY clause.

For example,

Without ORDER BY

SELECT number,sum(number) OVER (RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)─┐
│      16 │
│      26 │
│      36 │
└────────┴──────────────────────────────────────────────────────────────────────┘

With ORDER BY

SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM numbers(1,3)
┌─number─┬─sum(number) OVER (ORDER BY number ASC RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)─┐
│      16 │
│      25 │
│      33 │
└────────┴──────────────────────────────────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.CurrentRow.ExprFollowing.WithoutOrderBy.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN CURRENT ROW AND expr FOLLOWING frame is specified without the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (RANGE BETWEEN CURRENT ROW AND 1 FOLLOWING) FROM numbers(1,3)
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.CurrentRow.ExprFollowing.WithOrderBy

version: 1.0

ClickHouse SHALL include all rows with values from and including current row peers until and including current row value plus expr when RANGE BETWEEN CURRENT ROW AND expr FOLLOWING frame is specified with the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN CURRENT ROW AND 1 FOLLOWING) FROM values('number Int8', (1),(1),(2),(3))
┌─number─┬─sum(number) OVER (ORDER BY number ASC RANGE BETWEEN CURRENT ROW AND 1 FOLLOWING)─┐
│      14 │
│      14 │
│      25 │
│      33 │
└────────┴──────────────────────────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.CurrentRow.ExprPreceding.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN CURRENT ROW AND expr PRECEDING frame is specified with or without the ORDER BY clause.

For example,

Without ORDER BY

SELECT number,sum(number) OVER (RANGE BETWEEN CURRENT ROW AND 1 PRECEDING) FROM numbers(1,3)

With ORDER BY

SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN CURRENT ROW AND 1 PRECEDING) FROM numbers(1,3)
RANGE BETWEEN UNBOUNDED PRECEDING
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.UnboundedPreceding.CurrentRow

version: 1.0

ClickHouse SHALL include all rows with values from and including the first row until and including current row peers in the window partition when RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW frame is specified with and without the ORDER BY clause.

For example,

Without ORDER BY

SELECT number,sum(number) OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM values('number Int8', (1),(1),(2),(3))
┌─number─┬─sum(number) OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)─┐
│      17 │
│      17 │
│      27 │
│      37 │
└────────┴──────────────────────────────────────────────────────────────────────┘

With ORDER BY

SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM values('number Int8', (1),(1),(2),(3))
┌─number─┬─sum(number) OVER (ORDER BY number ASC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)─┐
│      12 │
│      12 │
│      24 │
│      37 │
└────────┴──────────────────────────────────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.UnboundedPreceding.UnboundedPreceding.Error

version: 1.0

ClickHouse SHALL return and error when RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED PRECEDING frame is specified with and without the ORDER BY clause.

For example,

Without ORDER BY

SELECT number,sum(number) OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED PRECEDING) FROM values('number Int8', (1),(1),(2),(3))

With ORDER BY

SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED PRECEDING) FROM values('number Int8', (1),(1),(2),(3))
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.UnboundedPreceding.UnboundedFollowing

version: 1.0

ClickHouse SHALL include all rows in the window partition when RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING frame is specified with and without the ORDER BY clause.

For example,

Without ORDER BY

SELECT number,sum(number) OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM values('number Int8', (1),(1),(2),(3))
┌─number─┬─sum(number) OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)─┐
│      17 │
│      17 │
│      27 │
│      37 │
└────────┴──────────────────────────────────────────────────────────────────────────────┘

With ORDER BY

SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM values('number Int8', (1),(1),(2),(3))
┌─number─┬─sum(number) OVER (ORDER BY number ASC RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)─┐
│      17 │
│      17 │
│      27 │
│      37 │
└────────┴──────────────────────────────────────────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.UnboundedPreceding.ExprPreceding.WithoutOrderBy.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN UNBOUNDED PRECEDING AND expr PRECEDING frame is specified without the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) FROM values('number Int8', (1),(1),(2),(3))
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.UnboundedPreceding.ExprPreceding.WithOrderBy

version: 1.0

ClickHouse SHALL include all rows with values from and including the first row until and including the value of the current row minus expr in the window partition when RANGE BETWEEN UNBOUNDED PRECEDING AND expr PRECEDING frame is specified with the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) FROM values('number Int8', (1),(1),(2),(3))
┌─number─┬─sum(number) OVER (ORDER BY number ASC RANGE BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)─┐
│      10 │
│      10 │
│      22 │
│      34 │
└────────┴──────────────────────────────────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.UnboundedPreceding.ExprFollowing.WithoutOrderBy.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN UNBOUNDED PRECEDING AND expr FOLLOWING frame is specified without the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING) FROM values('number Int8', (1),(1),(2),(3))
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.UnboundedPreceding.ExprFollowing.WithOrderBy

version: 1.0

ClickHouse SHALL include all rows with values from and including the first row until and including the value of the current row plus expr in the window partition when RANGE BETWEEN UNBOUNDED PRECEDING AND expr FOLLOWING frame is specified with the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING) FROM values('number Int8', (1),(1),(2),(3))
┌─number─┬─sum(number) OVER (ORDER BY number ASC RANGE BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING)─┐
│      14 │
│      14 │
│      27 │
│      37 │
└────────┴──────────────────────────────────────────────────────────────────────────────────────────┘
RANGE BETWEEN UNBOUNDED FOLLOWING
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.UnboundedFollowing.CurrentRow.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN UNBOUNDED FOLLOWING AND CURRENT ROW frame is specified with or without the ORDER BY clause.

RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.UnboundedFollowing.UnboundedFollowing.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN UNBOUNDED FOLLOWING AND UNBOUNDED FOLLOWING frame is specified with or without the ORDER BY clause.

RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.UnboundedFollowing.UnboundedPreceding.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN UNBOUNDED FOLLOWING AND UNBOUNDED PRECEDING frame is specified with or without the ORDER BY clause.

RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.UnboundedFollowing.ExprPreceding.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN UNBOUNDED FOLLOWING AND expr PRECEDING frame is specified with or without the ORDER BY clause.

RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.UnboundedFollowing.ExprFollowing.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN UNBOUNDED FOLLOWING AND expr FOLLOWING frame is specified with or without the ORDER BY clause.

RANGE BETWEEN expr PRECEDING
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprPreceding.CurrentRow.WithOrderBy

version: 1.0

ClickHouse SHALL include all rows with values from and including current row minus expr until and including current row peers in the window partition when RANGE BETWEEN expr PRECEDING AND CURRENT ROW frame is specified with the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN 1 PRECEDING AND CURRENT ROW) FROM values('number Int8', (1),(1),(2),(3))
┌─number─┬─sum(number) OVER (ORDER BY number ASC RANGE BETWEEN 1 PRECEDING AND CURRENT ROW)─┐
│      12 │
│      12 │
│      24 │
│      35 │
└────────┴──────────────────────────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprPreceding.CurrentRow.WithoutOrderBy.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN expr PRECEDING AND CURRENT ROW frame is specified without the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (RANGE BETWEEN 1 PRECEDING AND CURRENT ROW) FROM values('number Int8', (1),(1),(2),(3))
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprPreceding.UnboundedPreceding.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN expr PRECEDING AND UNBOUNDED PRECEDING frame is specified with or without the ORDER BY clause.

For example,

Without ORDER BY

SELECT number,sum(number) OVER (RANGE BETWEEN 1 PRECEDING AND UNBOUNDED PRECEDING) FROM values('number Int8', (1),(1),(2),(3))

With ORDER BY

SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN 1 PRECEDING AND UNBOUNDED PRECEDING) FROM values('number Int8', (1),(1),(2),(3))
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprPreceding.UnboundedFollowing.WithoutOrderBy.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN expr PRECEDING AND UNBOUNDED FOLLOWING frame is specified without the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (RANGE BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING) FROM numbers(1,3)
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprPreceding.UnboundedFollowing.WithOrderBy

version: 1.0

ClickHouse SHALL include all rows with values from and including current row minus expr until and including the last row in the window partition when RANGE BETWEEN expr PRECEDING AND UNBOUNDED FOLLOWING frame is specified with the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING) FROM values('number Int8', (1),(1),(2),(3))
┌─number─┬─sum(number) OVER (ORDER BY number ASC RANGE BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING)─┐
│      17 │
│      17 │
│      27 │
│      35 │
└────────┴──────────────────────────────────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprPreceding.ExprFollowing.WithoutOrderBy.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN expr PRECEDING AND expr FOLLOWING frame is specified without the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) FROM numbers(1,3)
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprPreceding.ExprFollowing.WithOrderBy

version: 1.0

ClickHouse SHALL include all rows with values from and including current row minus preceding expr until and including current row plus following expr in the window partition when RANGE BETWEEN expr PRECEDING AND expr FOLLOWING frame is specified with the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) FROM values('number Int8', (1),(1),(2),(3))
┌─number─┬─sum(number) OVER (ORDER BY number ASC RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING)─┐
│      14 │
│      14 │
│      27 │
│      35 │
└────────┴──────────────────────────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprPreceding.ExprPreceding.WithoutOrderBy.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN expr PRECEDING AND expr PRECEDING frame is specified without the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (RANGE BETWEEN 1 PRECEDING AND 0 PRECEDING) FROM numbers(1,3)
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprPreceding.ExprPreceding.WithOrderBy.Error

version: 1.0

ClickHouse SHALL return an error when the value of the frame_end specified by the current row minus preceding expr is greater than the value of the frame_start in the window partition when RANGE BETWEEN expr PRECEDING AND expr PRECEDING frame is specified with the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (RANGE BETWEEN 1 PRECEDING AND 2 PRECEDING) FROM values('number Int8', (1),(1),(2),(3))
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprPreceding.ExprPreceding.WithOrderBy

version: 1.0

ClickHouse SHALL include all rows with values from and including current row minus preceding expr for the frame_start until and including current row minus following expr for the frame_end in the window partition when RANGE BETWEEN expr PRECEDING AND expr PRECEDING frame is specified with the ORDER BY clause if an only if the frame_end value is equal or greater than frame_start value.

For example,

Greater Than

SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN 1 PRECEDING AND 0 PRECEDING) FROM values('number Int8', (1),(1),(2),(3))
┌─number─┬─sum(number) OVER (ORDER BY number ASC RANGE BETWEEN 1 PRECEDING AND 0 PRECEDING)─┐
│      12 │
│      12 │
│      24 │
│      35 │
└────────┴──────────────────────────────────────────────────────────────────────────────────┘

or Equal

 SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN 1 PRECEDING AND 1 PRECEDING) FROM values('number Int8', (1),(1),(2),(3))
┌─number─┬─sum(number) OVER (ORDER BY number ASC RANGE BETWEEN 1 PRECEDING AND 1 PRECEDING)─┐
│      10 │
│      10 │
│      22 │
│      32 │
└────────┴──────────────────────────────────────────────────────────────────────────────────┘
RANGE BETWEEN expr FOLLOWING
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprFollowing.CurrentRow.WithoutOrderBy.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN expr FOLLOWING AND CURRENT ROW frame is specified without the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (RANGE BETWEEN 0 FOLLOWING AND CURRENT ROW) FROM values('number Int8', (1),(1),(2),(3))
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprFollowing.CurrentRow.WithOrderBy.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN expr FOLLOWING AND CURRENT ROW frame is specified with the ORDER BY clause and expr is greater than 0.

For example,

SELECT number,sum(number) OVER (RANGE BETWEEN 1 FOLLOWING AND CURRENT ROW) FROM values('number Int8', (1),(1),(2),(3))
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprFollowing.CurrentRow.ZeroSpecialCase

version: 1.0

ClickHouse SHALL include all current row peers in the window partition when RANGE BETWEEN expr FOLLOWING AND CURRENT ROW frame is specified with the ORDER BY clause if and only if the expr equals to 0.

For example,

Without ORDER BY

SELECT number,sum(number) OVER (RANGE BETWEEN 0 FOLLOWING AND CURRENT ROW) FROM values('number Int8', (1),(1),(2),(3))
┌─number─┬─sum(number) OVER (RANGE BETWEEN 0 FOLLOWING AND CURRENT ROW)─┐
│      17 │
│      17 │
│      27 │
│      37 │
└────────┴──────────────────────────────────────────────────────────────┘

With ORDER BY

SELECT number,sum(number) OVER (RANGE BETWEEN 0 FOLLOWING AND CURRENT ROW) FROM values('number Int8', (1),(1),(2),(3))
┌─number─┬─sum(number) OVER (ORDER BY number ASC RANGE BETWEEN 0 FOLLOWING AND CURRENT ROW)─┐
│      12 │
│      12 │
│      22 │
│      33 │
└────────┴──────────────────────────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprFollowing.UnboundedFollowing.WithoutOrderBy.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN expr FOLLOWING AND UNBOUNDED FOLLOWING frame is specified without the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (RANGE BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) FROM values('number Int8', (1),(1),(2),(3))
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprFollowing.UnboundedFollowing.WithOrderBy

version: 1.0

ClickHouse SHALL include all rows with values from and including current row plus expr until and including the last row in the window partition when RANGE BETWEEN expr FOLLOWING AND UNBOUNDED FOLLOWING frame is specified with the ORDER BY clause.

For example,

SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) FROM values('number Int8', (1),(1),(2),(3))
┌─number─┬─sum(number) OVER (ORDER BY number ASC RANGE BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING)─┐
│      15 │
│      15 │
│      23 │
│      30 │
└────────┴──────────────────────────────────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprFollowing.UnboundedPreceding.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN expr FOLLOWING AND UNBOUNDED PRECEDING frame is specified with or without the ORDER BY clause.

For example,

Without ORDER BY

SELECT number,sum(number) OVER (RANGE BETWEEN 1 FOLLOWING AND UNBOUNDED PRECEDING) FROM values('number Int8', (1),(1),(2),(3))

With ORDER BY

SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN 1 FOLLOWING AND UNBOUNDED PRECEDING) FROM values('number Int8', (1),(1),(2),(3))
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprFollowing.ExprPreceding.WithoutOrderBy.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN expr FOLLOWING AND expr PRECEDING frame is specified without the ORDER BY.

RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprFollowing.ExprPreceding.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN expr FOLLOWING AND expr PRECEDING frame is specified with the ORDER BY clause if the value of both expr is not 0.

RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprFollowing.ExprPreceding.WithOrderBy.ZeroSpecialCase

version: 1.0

ClickHouse SHALL include all rows with value equal to current row peers in the window partition when RANGE BETWEEN expr FOLLOWING AND expr PRECEDING frame is specified with the ORDER BY clause if and only if both expr's are 0.

For example,

SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN 0 FOLLOWING AND 0 PRECEDING) FROM values('number Int8', (1),(1),(2),(3))
┌─number─┬─sum(number) OVER (ORDER BY number ASC RANGE BETWEEN 0 FOLLOWING AND 0 PRECEDING ─┐
│      12 │
│      12 │
│      22 │
│      33 │
└────────┴──────────────────────────────────────────────────────────────────────────────────┘
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprFollowing.ExprFollowing.WithoutOrderBy.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN expr FOLLOWING AND expr FOLLOWING frame is specified without the ORDER BY clause.

RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprFollowing.ExprFollowing.WithOrderBy.Error

version: 1.0

ClickHouse SHALL return an error when RANGE BETWEEN expr FOLLOWING AND expr FOLLOWING frame is specified with the ORDER BY clause but the expr for the frame_end is less than the expr for the frame_start.

For example,

SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN 1 FOLLOWING AND 0 FOLLOWING) FROM values('number Int8', (1),(1),(2),(3))
RQ.SRS-019.ClickHouse.WindowFunctions.RangeFrame.Between.ExprFollowing.ExprFollowing.WithOrderBy

version: 1.0

ClickHouse SHALL include all rows with value from and including current row plus expr for the frame_start until and including current row plus expr for the frame_end in the window partition when RANGE BETWEEN expr FOLLOWING AND expr FOLLOWING frame is specified with the ORDER BY clause if and only if the expr for the frame_end is greater than or equal than the expr for the frame_start.

For example,

SELECT number,sum(number) OVER (ORDER BY number RANGE BETWEEN 1 FOLLOWING AND 2 FOLLOWING) FROM values('number Int8', (1),(1),(2),(3))
┌─number─┬─sum(number) OVER (ORDER BY number ASC RANGE BETWEEN 1 FOLLOWING AND 2 FOLLOWING)─┐
│      15 │
│      15 │
│      23 │
│      30 │
└────────┴──────────────────────────────────────────────────────────────────────────────────┘

Frame Extent

RQ.SRS-019.ClickHouse.WindowFunctions.Frame.Extent

version: 1.0

ClickHouse SHALL support frame_extent defined as

frame_extent:
    {frame_start | frame_between}

Frame Start

RQ.SRS-019.ClickHouse.WindowFunctions.Frame.Start

version: 1.0

ClickHouse SHALL support frame_start defined as

frame_start: {
    CURRENT ROW
  | UNBOUNDED PRECEDING
  | UNBOUNDED FOLLOWING
  | expr PRECEDING
  | expr FOLLOWING
}

Frame Between

RQ.SRS-019.ClickHouse.WindowFunctions.Frame.Between

version: 1.0

ClickHouse SHALL support frame_between defined as

frame_between:
    BETWEEN frame_start AND frame_end

Frame End

RQ.SRS-019.ClickHouse.WindowFunctions.Frame.End

version: 1.0

ClickHouse SHALL support frame_end defined as

frame_end: {
    CURRENT ROW
  | UNBOUNDED PRECEDING
  | UNBOUNDED FOLLOWING
  | expr PRECEDING
  | expr FOLLOWING
}

CURRENT ROW

RQ.SRS-019.ClickHouse.WindowFunctions.CurrentRow

version: 1.0

ClickHouse SHALL support CURRENT ROW as frame_start or frame_end value.

  • For ROWS SHALL define the bound to be the current row
  • For RANGE SHALL define the bound to be the peers of the current row

UNBOUNDED PRECEDING

RQ.SRS-019.ClickHouse.WindowFunctions.UnboundedPreceding

version: 1.0

ClickHouse SHALL support UNBOUNDED PRECEDING as frame_start or frame_end value and it SHALL define that the bound is the first partition row.

UNBOUNDED FOLLOWING

RQ.SRS-019.ClickHouse.WindowFunctions.UnboundedFollowing

version: 1.0

ClickHouse SHALL support UNBOUNDED FOLLOWING as frame_start or frame_end value and it SHALL define that the bound is the last partition row.

expr PRECEDING

RQ.SRS-019.ClickHouse.WindowFunctions.ExprPreceding

version: 1.0

ClickHouse SHALL support expr PRECEDING as frame_start or frame_end value

  • For ROWS it SHALL define the bound to be the expr rows before the current row
  • For RANGE it SHALL define the bound to be the rows with values equal to the current row value minus the expr.
RQ.SRS-019.ClickHouse.WindowFunctions.ExprPreceding.ExprValue

version: 1.0

ClickHouse SHALL support only non-negative numeric literal as the value for the expr in the expr PRECEDING frame boundary.

For example,

5 PRECEDING

expr FOLLOWING

RQ.SRS-019.ClickHouse.WindowFunctions.ExprFollowing

version: 1.0

ClickHouse SHALL support expr FOLLOWING as frame_start or frame_end value

  • For ROWS it SHALL define the bound to be the expr rows after the current row
  • For RANGE it SHALL define the bound to be the rows with values equal to the current row value plus expr
RQ.SRS-019.ClickHouse.WindowFunctions.ExprFollowing.ExprValue

version: 1.0

ClickHouse SHALL support only non-negative numeric literal as the value for the expr in the expr FOLLOWING frame boundary.

For example,

5 FOLLOWING

WINDOW Clause

RQ.SRS-019.ClickHouse.WindowFunctions.WindowClause

version: 1.0

ClickHouse SHALL support WINDOW clause to define one or more windows.

WINDOW window_name AS (window_spec)
    [, window_name AS (window_spec)] ..

The window_name SHALL be the name of a window defined by a WINDOW clause.

The window_spec SHALL specify the window.

For example,

SELECT ... FROM table WINDOW w AS (partiton by id))

RQ.SRS-019.ClickHouse.WindowFunctions.WindowClause.MultipleWindows

version: 1.0

ClickHouse SHALL support WINDOW clause that defines multiple windows.

For example,

SELECT ... FROM table WINDOW w1 AS (partition by id), w2 AS (partition by customer)

RQ.SRS-019.ClickHouse.WindowFunctions.WindowClause.MissingWindowSpec.Error

version: 1.0

ClickHouse SHALL return an error if the WINDOW clause definition is missing window_spec.

OVER Clause

RQ.SRS-019.ClickHouse.WindowFunctions.OverClause

version: 1.0

ClickHouse SHALL support OVER clause to either use named window defined using WINDOW clause or adhoc window defined inplace.

OVER ()|(window_spec)|named_window 

Empty Clause

RQ.SRS-019.ClickHouse.WindowFunctions.OverClause.EmptyOverClause

version: 1.0

ClickHouse SHALL treat the entire set of query rows as a single partition when OVER clause is empty. For example,

SELECT sum(x) OVER () FROM table

Ad-Hoc Window

RQ.SRS-019.ClickHouse.WindowFunctions.OverClause.AdHocWindow

version: 1.0

ClickHouse SHALL support ad hoc window specification in the OVER clause.

OVER [window_spec]

See window_spec definition.

For example,

(count(*) OVER (partition by id order by time desc))
RQ.SRS-019.ClickHouse.WindowFunctions.OverClause.AdHocWindow.MissingWindowSpec.Error

version: 1.0

ClickHouse SHALL return an error if the OVER clause has missing window_spec.

Named Window

RQ.SRS-019.ClickHouse.WindowFunctions.OverClause.NamedWindow

version: 1.0

ClickHouse SHALL support using a previously defined named window in the OVER clause.

OVER [window_name]

See [window_name] definition.

For example,

SELECT count(*) OVER w FROM table WINDOW w AS (partition by id)
RQ.SRS-019.ClickHouse.WindowFunctions.OverClause.NamedWindow.InvalidName.Error

version: 1.0

ClickHouse SHALL return an error if the OVER clause reference invalid window name.

RQ.SRS-019.ClickHouse.WindowFunctions.OverClause.NamedWindow.MultipleWindows.Error

version: 1.0

ClickHouse SHALL return an error if the OVER clause references more than one window name.

Window Functions

Nonaggregate Functions

The first_value(expr) Function
RQ.SRS-019.ClickHouse.WindowFunctions.FirstValue

version: 1.0

ClickHouse SHALL support first_value window function that SHALL be synonum for the any(value) function that SHALL return the value of expr from first row in the window frame.

first_value(expr) OVER ...
The last_value(expr) Function
RQ.SRS-019.ClickHouse.WindowFunctions.LastValue

version: 1.0

ClickHouse SHALL support last_value window function that SHALL be synonym for the anyLast(value) function that SHALL return the value of expr from the last row in the window frame.

last_value(expr) OVER ...
The lag(value, offset) Function Workaround
RQ.SRS-019.ClickHouse.WindowFunctions.Lag.Workaround

version: 1.0

ClickHouse SHALL support a workaround for the lag(value, offset) function as

any(value) OVER (.... ROWS BETWEEN <offset> PRECEDING AND <offset> PRECEDING)

The function SHALL returns the value from the row that lags (precedes) the current row by the N rows within its partition. Where N is the value passed to the any function.

If there is no such row, the return value SHALL be default.

For example, if N is 3, the return value is default for the first two rows. If N or default are missing, the defaults are 1 and NULL, respectively.

N SHALL be a literal non-negative integer. If N is 0, the value SHALL be returned for the current row.

The lead(value, offset) Function Workaround
RQ.SRS-019.ClickHouse.WindowFunctions.Lead.Workaround

version: 1.0

ClickHouse SHALL support a workaround for the lead(value, offset) function as

any(value) OVER (.... ROWS BETWEEN <offset> FOLLOWING AND <offset> FOLLOWING)

The function SHALL returns the value from the row that leads (follows) the current row by the N rows within its partition. Where N is the value passed to the any function.

If there is no such row, the return value SHALL be default.

For example, if N is 3, the return value is default for the last two rows. If N or default are missing, the defaults are 1 and NULL, respectively.

N SHALL be a literal non-negative integer. If N is 0, the value SHALL be returned for the current row.

The rank() Function
RQ.SRS-019.ClickHouse.WindowFunctions.Rank

version: 1.0

ClickHouse SHALL support rank window function that SHALL return the rank of the current row within its partition with gaps.

Peers SHALL be considered ties and receive the same rank. The function SHALL not assign consecutive ranks to peer groups if groups of size greater than one exist and the result is noncontiguous rank numbers.

If the function is used without ORDER BY to sort partition rows into the desired order then all rows SHALL be peers.

rank() OVER ...
The dense_rank() Function
RQ.SRS-019.ClickHouse.WindowFunctions.DenseRank

version: 1.0

ClickHouse SHALL support dense_rank function over a window that SHALL return the rank of the current row within its partition without gaps.

Peers SHALL be considered ties and receive the same rank. The function SHALL assign consecutive ranks to peer groups and the result is that groups of size greater than one do not produce noncontiguous rank numbers.

If the function is used without ORDER BY to sort partition rows into the desired order then all rows SHALL be peers.

dense_rank() OVER ...
The row_number() Function
RQ.SRS-019.ClickHouse.WindowFunctions.RowNumber

version: 1.0

ClickHouse SHALL support row_number function over a window that SHALL returns the number of the current row within its partition.

Rows numbers SHALL range from 1 to the number of partition rows.

The ORDER BY affects the order in which rows are numbered. Without ORDER BY, row numbering MAY be nondeterministic.

row_number() OVER ...

Aggregate Functions

RQ.SRS-019.ClickHouse.WindowFunctions.AggregateFunctions

version: 1.0

ClickHouse SHALL support using aggregate functions over windows.

Combinators
RQ.SRS-019.ClickHouse.WindowFunctions.AggregateFunctions.Combinators

version: 1.0

ClickHouse SHALL support aggregate functions with combinator prefixes over windows.

Parametric
RQ.SRS-019.ClickHouse.WindowFunctions.AggregateFunctions.Parametric

version: 1.0

ClickHouse SHALL support parametric aggregate functions over windows.

References