WindowSpec

@Beta
class WindowSpec


Specification defining how documents are partitioned, ordered, and bounded for window functions in com.google.firebase.firestore.Pipeline.addWindowFields.

Window Functions SDK features are in preview until the backend feature launches. Look for launch announcement at: https://docs.cloud.google.com/firestore/docs/release-notes

Sorting and Window Frames:

  • For document-based (documents) window frames, sort is optional; if no sort expressions are specified, documents are processed in incoming stream (fetch) order. WindowBound.CURRENT refers strictly to the current document's position (documents with tied sort values are not included).

  • For range-based (range) window frames, at least one sort expression is required. WindowBound.CURRENT is peer-inclusive (like SQL CURRENT ROW in RANGE mode): it includes all documents whose sort value(s) tie with the current document, making it semantically equivalent to an offset of 0. Range frames bounded only by WindowBound.CURRENT and/or WindowBound.UNBOUNDED (with no numeric or time offsets) may use multiple sort orderings and non-numeric sort values (such as strings or booleans, provided no unit is set). Range frames with numeric or time offsets require exactly one sort ordering.

  • When no explicit documents or range frame is specified, omitting sort defaults the window frame to documents(WindowBound.UNBOUNDED, WindowBound.UNBOUNDED), whereas specifying sort changes the default window frame to range(WindowBound.UNBOUNDED, WindowBound.CURRENT).

Summary

Public constructors

Creates an empty window spec: a single global partition covering the entire result set, with no sort and no explicit frame (defaulting to documents(WindowBound.UNBOUNDED, WindowBound.UNBOUNDED)).

Public functions

WindowSpec
documents(preceding: Any, following: Any)

Specify a document-count frame with bounds of mixed or heterogeneous types, e.g. (Int, WindowBound) or (Int, Expression).

WindowSpec
documents(preceding: Expression, following: Expression)

Specifies a document-count based window frame.

WindowSpec
documents(preceding: Int, following: Int)

Specifies a document-count based window frame.

WindowSpec
documents(preceding: WindowBound, following: WindowBound)

Specifies a document-count frame using symbolic bounds, e.g. (WindowBound.UNBOUNDED, WindowBound.CURRENT).

open operator Boolean
equals(other: Any?)
open Int
WindowSpec
partition(expression: Expression, vararg additionalExpressions: Any)

Specify partition group columns.

WindowSpec
partition(fieldName: String, vararg additionalExpressions: Any)
WindowSpec
range(preceding: Any, following: Any)

Specify a range frame with bounds of mixed or heterogeneous types, e.g. (Int, WindowBound) or (Int, Expression).

WindowSpec
range(preceding: Double, following: Double)

Specify a numeric range frame with fractional bounds.

WindowSpec
range(preceding: Expression, following: Expression)

Specifies a range-value based window frame.

WindowSpec
range(preceding: Int, following: Int)

Specifies a range-value based window frame with numeric offsets.

WindowSpec
range(preceding: WindowBound, following: WindowBound)

Specifies a range frame using symbolic bounds, e.g. (WindowBound.UNBOUNDED, WindowBound.CURRENT).

WindowSpec
range(preceding: Any, following: Any, unit: Expression)

Specify a range frame with bounds of mixed or heterogeneous types and a time unit.

WindowSpec
range(preceding: Any, following: Any, unit: String)

Specify a range frame with bounds of mixed or heterogeneous types and a time unit.

WindowSpec
range(preceding: Double, following: Double, unit: Expression)

Specify a numeric range frame with fractional bounds and a time unit.

WindowSpec
range(preceding: Double, following: Double, unit: String)

Specify a numeric range frame with fractional bounds and a time unit.

WindowSpec
range(preceding: Expression, following: Expression, unit: Expression)

Specifies a range-value based window frame with a time unit.

WindowSpec
range(preceding: Expression, following: Expression, unit: String)

Specifies a range-value based window frame with a time unit.

WindowSpec
range(preceding: Int, following: Int, unit: Expression)

Specifies a range-value based window frame with a time unit.

WindowSpec
range(preceding: Int, following: Int, unit: String)

Specifies a range-value based window frame with a time unit.

WindowSpec
range(preceding: WindowBound, following: WindowBound, unit: Expression)

Specifies a range frame using symbolic bounds and a time unit.

WindowSpec
range(preceding: WindowBound, following: WindowBound, unit: String)

Specifies a range frame using symbolic bounds and a time unit.

WindowSpec
sort(orders: List<Ordering>)

Specifies the sort order of documents within each partition.

WindowSpec
sort(order: Ordering, vararg additionalOrders: Ordering)

Specifies the sort order of documents within each partition.

Public constructors

WindowSpec

WindowSpec()

Creates an empty window spec: a single global partition covering the entire result set, with no sort and no explicit frame (defaulting to documents(WindowBound.UNBOUNDED, WindowBound.UNBOUNDED)).

Public functions

documents

fun documents(preceding: Any, following: Any): WindowSpec

Specify a document-count frame with bounds of mixed or heterogeneous types, e.g. (Int, WindowBound) or (Int, Expression). Invalid combinations are encoded and rejected by the backend.

sort is optional for document-based window frames; if no sort expressions are specified, documents are processed in incoming stream (fetch) order.

Parameters
preceding: Any

The start of the window frame relative to the current document (e.g. an integer offset, WindowBound, or Expression). A positive integer (e.g. 2) includes up to that many documents before the current document. A negative integer (e.g. -1) indicates a boundary following the current document, enabling frames that start after the current document.

following: Any

The end of the window frame relative to the current document (e.g. an integer offset, WindowBound, or Expression). A positive integer (e.g. 2) includes up to that many documents after the current document. A negative integer (e.g. -1) indicates a boundary preceding the current document, enabling frames that end before the current document (e.g. excluding the current document).

Returns
WindowSpec

A new WindowSpec with the specified document-count frame.

documents

fun documents(preceding: Expression, following: Expression): WindowSpec

Specifies a document-count based window frame.

sort is optional for document-based window frames; if no sort expressions are specified, documents are processed in incoming stream (fetch) order.

Parameters
preceding: Expression

The start of the window frame relative to the current document. A positive integer (e.g. 2) includes up to that many documents before the current document. A negative integer (e.g. -1) indicates a boundary following the current document, enabling frames that start after the current document.

following: Expression

The end of the window frame relative to the current document. A positive integer (e.g. 2) includes up to that many documents after the current document. A negative integer (e.g. -1) indicates a boundary preceding the current document, enabling frames that end before the current document (e.g. excluding the current document).

Returns
WindowSpec

A new WindowSpec with the specified document-count frame.

documents

fun documents(preceding: Int, following: Int): WindowSpec

Specifies a document-count based window frame.

sort is optional for document-based window frames; if no sort expressions are specified, documents are processed in incoming stream (fetch) order.

Parameters
preceding: Int

The start of the window frame relative to the current document. A positive integer (e.g. 2) includes up to that many documents before the current document. A negative integer (e.g. -1) indicates a boundary following the current document, enabling frames that start after the current document.

following: Int

The end of the window frame relative to the current document. A positive integer (e.g. 2) includes up to that many documents after the current document. A negative integer (e.g. -1) indicates a boundary preceding the current document, enabling frames that end before the current document (e.g. excluding the current document).

Returns
WindowSpec

A new WindowSpec with the specified document-count frame.

documents

fun documents(preceding: WindowBound, following: WindowBound): WindowSpec

Specifies a document-count frame using symbolic bounds, e.g. (WindowBound.UNBOUNDED, WindowBound.CURRENT).

In a documents frame, WindowBound.CURRENT refers strictly to the current document's position (documents with tied sort values are not included). sort is optional for document-based window frames; if no sort expressions are specified, documents are processed in incoming stream (fetch) order.

Parameters
preceding: WindowBound

The symbolic lower boundary (WindowBound.UNBOUNDED or WindowBound.CURRENT ). For numeric offsets (where a positive integer such as 2 includes up to that many documents before the current document, and a negative integer such as -1 indicates a boundary following the current document, enabling frames that start after the current document), use the (Int, Int) or (Any, Any) overload.

following: WindowBound

The symbolic upper boundary (WindowBound.UNBOUNDED or WindowBound.CURRENT ). For numeric offsets (where a positive integer such as 2 includes up to that many documents after the current document, and a negative integer such as -1 indicates a boundary preceding the current document, enabling frames that end before the current document), use the (Int, Int) or (Any, Any) overload.

Returns
WindowSpec

A new WindowSpec with the specified document-count frame.

equals

open operator fun equals(other: Any?): Boolean

hashCode

open fun hashCode(): Int

partition

fun partition(expression: Expression, vararg additionalExpressions: Any): WindowSpec

Specify partition group columns.

partition

fun partition(fieldName: String, vararg additionalExpressions: Any): WindowSpec

range

fun range(preceding: Any, following: Any): WindowSpec

Specify a range frame with bounds of mixed or heterogeneous types, e.g. (Int, WindowBound) or (Int, Expression). Invalid combinations are encoded and rejected by the backend.

Range frames with numeric offsets require a single sort expression; range frames bounded only by WindowBound.CURRENT and/or WindowBound.UNBOUNDED allow one or more sort expressions and non-numeric sort values.

Parameters
preceding: Any

The lower bound offset relative to the current document's sort value (e.g. a numeric offset, WindowBound, or Expression). A positive offset subtracts from the current document's sort value (looking into the past). A negative offset adds to the current document's sort value (shifting the lower boundary past the current document).

following: Any

The upper bound offset relative to the current document's sort value (e.g. a numeric offset, WindowBound, or Expression). A positive offset adds to the current document's sort value (looking into the future). A negative offset subtracts from the current document's sort value (shifting the upper boundary before the current document).

Returns
WindowSpec

A new WindowSpec with the specified range frame.

range

fun range(preceding: Double, following: Double): WindowSpec

Specify a numeric range frame with fractional bounds.

Only meaningful for value-based (non-time) range frames, e.g. when sorting by a price or score. The backend rejects fractional offsets for time-based range frames. Range frames with numeric offsets require a single numeric sort expression.

Parameters
preceding: Double

The lower bound offset relative to the current document's sort value. A positive offset subtracts from the current document's sort value (looking into the past). A negative offset adds to the current document's sort value (shifting the lower boundary past the current document).

following: Double

The upper bound offset relative to the current document's sort value. A positive offset adds to the current document's sort value (looking into the future). A negative offset subtracts from the current document's sort value (shifting the upper boundary before the current document).

Returns
WindowSpec

A new WindowSpec with the specified range frame.

range

fun range(preceding: Expression, following: Expression): WindowSpec

Specifies a range-value based window frame.

Range frames with numeric offsets require a single sort expression; range frames bounded only by WindowBound.CURRENT and/or WindowBound.UNBOUNDED allow one or more sort expressions and non-numeric sort values.

Parameters
preceding: Expression

The lower bound offset relative to the current document's sort value. A positive offset subtracts from the current document's sort value (looking into the past). A negative offset adds to the current document's sort value (shifting the lower boundary past the current document).

following: Expression

The upper bound offset relative to the current document's sort value. A positive offset adds to the current document's sort value (looking into the future). A negative offset subtracts from the current document's sort value (shifting the upper boundary before the current document).

Returns
WindowSpec

A new WindowSpec with the specified range frame.

range

fun range(preceding: Int, following: Int): WindowSpec

Specifies a range-value based window frame with numeric offsets.

Range frames with numeric offsets require a single numeric sort expression. An offset of 0 is peer-inclusive (equivalent to WindowBound.CURRENT in a range frame).

Parameters
preceding: Int

The lower bound offset relative to the current document's sort value. A positive offset subtracts from the current document's sort value (looking into the past). A negative offset adds to the current document's sort value (shifting the lower boundary past the current document).

following: Int

The upper bound offset relative to the current document's sort value. A positive offset adds to the current document's sort value (looking into the future). A negative offset subtracts from the current document's sort value (shifting the upper boundary before the current document).

Returns
WindowSpec

A new WindowSpec with the specified range frame.

range

fun range(preceding: WindowBound, following: WindowBound): WindowSpec

Specifies a range frame using symbolic bounds, e.g. (WindowBound.UNBOUNDED, WindowBound.CURRENT).

In a range frame, WindowBound.CURRENT is peer-inclusive (like SQL CURRENT ROW in RANGE mode): it includes all documents whose sort value(s) tie with the current document, making it semantically equivalent to an offset of 0. Because both bounds are symbolic ( WindowBound.CURRENT / WindowBound.UNBOUNDED) with no time unit, one or more sort orderings and non-numeric sort values (such as strings or booleans) are supported.

Parameters
preceding: WindowBound

The symbolic lower boundary (WindowBound.UNBOUNDED or WindowBound.CURRENT ). For numeric offsets (where a positive offset subtracts from the current document's sort value, looking into the past, and a negative offset adds to the current document's sort value, shifting the lower boundary past the current document), use the numeric or (Any, Any) overload.

following: WindowBound

The symbolic upper boundary (WindowBound.UNBOUNDED or WindowBound.CURRENT ). For numeric offsets (where a positive offset adds to the current document's sort value, looking into the future, and a negative offset subtracts from the current document's sort value, shifting the upper boundary before the current document), use the numeric or (Any, Any) overload.

Returns
WindowSpec

A new WindowSpec with the specified range frame.

range

fun range(preceding: Any, following: Any, unit: Expression): WindowSpec

Specify a range frame with bounds of mixed or heterogeneous types and a time unit.

Supported unit values are "microsecond", "millisecond", "second", "minute", "hour", "day", "week", "month", "quarter", and "year".

Range frames with numeric or time offsets require a single timestamp sort expression (a unit on a string or boolean sort field is rejected by the backend).

Parameters
preceding: Any

The lower bound offset relative to the current document's sort value (e.g. a numeric offset, WindowBound, or Expression). A positive offset subtracts from the current document's sort value (looking into the past). A negative offset adds to the current document's sort value (shifting the lower boundary past the current document).

following: Any

The upper bound offset relative to the current document's sort value (e.g. a numeric offset, WindowBound, or Expression). A positive offset adds to the current document's sort value (looking into the future). A negative offset subtracts from the current document's sort value (shifting the upper boundary before the current document).

unit: Expression

The time unit expression for the range offsets.

Returns
WindowSpec

A new WindowSpec with the specified range frame.

range

fun range(preceding: Any, following: Any, unit: String): WindowSpec

Specify a range frame with bounds of mixed or heterogeneous types and a time unit.

Supported unit values are "microsecond", "millisecond", "second", "minute", "hour", "day", "week", "month", "quarter", and "year".

Range frames with numeric or time offsets require a single timestamp sort expression (a unit on a string or boolean sort field is rejected by the backend).

Parameters
preceding: Any

The lower bound offset relative to the current document's sort value (e.g. a numeric offset, WindowBound, or Expression). A positive offset subtracts from the current document's sort value (looking into the past). A negative offset adds to the current document's sort value (shifting the lower boundary past the current document).

following: Any

The upper bound offset relative to the current document's sort value (e.g. a numeric offset, WindowBound, or Expression). A positive offset adds to the current document's sort value (looking into the future). A negative offset subtracts from the current document's sort value (shifting the upper boundary before the current document).

unit: String

The time unit for the range offsets.

Returns
WindowSpec

A new WindowSpec with the specified range frame.

range

fun range(preceding: Double, following: Double, unit: Expression): WindowSpec

Specify a numeric range frame with fractional bounds and a time unit.

Supported unit values are "microsecond", "millisecond", "second", "minute", "hour", "day", "week", "month", "quarter", and "year".

Range frames with numeric or time offsets require a single sort expression.

Parameters
preceding: Double

The lower bound offset relative to the current document's sort value. A positive offset subtracts from the current document's sort value (looking into the past). A negative offset adds to the current document's sort value (shifting the lower boundary past the current document).

following: Double

The upper bound offset relative to the current document's sort value. A positive offset adds to the current document's sort value (looking into the future). A negative offset subtracts from the current document's sort value (shifting the upper boundary before the current document).

unit: Expression

The time unit expression for the range offsets.

Returns
WindowSpec

A new WindowSpec with the specified range frame.

range

fun range(preceding: Double, following: Double, unit: String): WindowSpec

Specify a numeric range frame with fractional bounds and a time unit.

Supported unit values are "microsecond", "millisecond", "second", "minute", "hour", "day", "week", "month", "quarter", and "year".

Range frames with numeric or time offsets require a single sort expression.

Parameters
preceding: Double

The lower bound offset relative to the current document's sort value. A positive offset subtracts from the current document's sort value (looking into the past). A negative offset adds to the current document's sort value (shifting the lower boundary past the current document).

following: Double

The upper bound offset relative to the current document's sort value. A positive offset adds to the current document's sort value (looking into the future). A negative offset subtracts from the current document's sort value (shifting the upper boundary before the current document).

unit: String

The time unit for the range offsets.

Returns
WindowSpec

A new WindowSpec with the specified range frame.

range

fun range(preceding: Expression, following: Expression, unit: Expression): WindowSpec

Specifies a range-value based window frame with a time unit.

Supported unit values are "microsecond", "millisecond", "second", "minute", "hour", "day", "week", "month", "quarter", and "year".

Range frames with numeric or time offsets require a single timestamp sort expression (a unit on a string or boolean sort field is rejected by the backend).

Parameters
preceding: Expression

The lower bound offset relative to the current document's sort value. A positive offset subtracts from the current document's sort value (looking into the past). A negative offset adds to the current document's sort value (shifting the lower boundary past the current document).

following: Expression

The upper bound offset relative to the current document's sort value. A positive offset adds to the current document's sort value (looking into the future). A negative offset subtracts from the current document's sort value (shifting the upper boundary before the current document).

unit: Expression

The time unit expression for the range offsets.

Returns
WindowSpec

A new WindowSpec with the specified range frame.

range

fun range(preceding: Expression, following: Expression, unit: String): WindowSpec

Specifies a range-value based window frame with a time unit.

Supported unit values are "microsecond", "millisecond", "second", "minute", "hour", "day", "week", "month", "quarter", and "year".

Range frames with numeric or time offsets require a single timestamp sort expression (a unit on a string or boolean sort field is rejected by the backend).

Parameters
preceding: Expression

The lower bound offset relative to the current document's sort value. A positive offset subtracts from the current document's sort value (looking into the past). A negative offset adds to the current document's sort value (shifting the lower boundary past the current document).

following: Expression

The upper bound offset relative to the current document's sort value. A positive offset adds to the current document's sort value (looking into the future). A negative offset subtracts from the current document's sort value (shifting the upper boundary before the current document).

unit: String

The time unit for the range offsets.

Returns
WindowSpec

A new WindowSpec with the specified range frame.

range

fun range(preceding: Int, following: Int, unit: Expression): WindowSpec

Specifies a range-value based window frame with a time unit.

Supported unit values are "microsecond", "millisecond", "second", "minute", "hour", "day", "week", "month", "quarter", and "year".

Range frames with time offsets require a single timestamp sort expression (a unit on a string or boolean sort field is rejected by the backend).

Parameters
preceding: Int

The lower bound offset relative to the current document's sort value. A positive offset subtracts from the current document's sort value (looking into the past). A negative offset adds to the current document's sort value (shifting the lower boundary past the current document).

following: Int

The upper bound offset relative to the current document's sort value. A positive offset adds to the current document's sort value (looking into the future). A negative offset subtracts from the current document's sort value (shifting the upper boundary before the current document).

unit: Expression

The time unit expression for the range offsets.

Returns
WindowSpec

A new WindowSpec with the specified range frame.

range

fun range(preceding: Int, following: Int, unit: String): WindowSpec

Specifies a range-value based window frame with a time unit.

Supported unit values are "microsecond", "millisecond", "second", "minute", "hour", "day", "week", "month", "quarter", and "year".

Range frames with time offsets require a single timestamp sort expression (a unit on a string or boolean sort field is rejected by the backend).

Parameters
preceding: Int

The lower bound offset relative to the current document's sort value. A positive offset subtracts from the current document's sort value (looking into the past). A negative offset adds to the current document's sort value (shifting the lower boundary past the current document).

following: Int

The upper bound offset relative to the current document's sort value. A positive offset adds to the current document's sort value (looking into the future). A negative offset subtracts from the current document's sort value (shifting the upper boundary before the current document).

unit: String

The time unit for the range offsets.

Returns
WindowSpec

A new WindowSpec with the specified range frame.

range

fun range(preceding: WindowBound, following: WindowBound, unit: Expression): WindowSpec

Specifies a range frame using symbolic bounds and a time unit.

Supported unit values are "microsecond", "millisecond", "second", "minute", "hour", "day", "week", "month", "quarter", and "year".

In a range frame, WindowBound.CURRENT is peer-inclusive. Specifying a time unit requires a timestamp sort expression (a unit on a string or boolean sort field is rejected by the backend).

Parameters
preceding: WindowBound

The symbolic lower boundary (WindowBound.UNBOUNDED or WindowBound.CURRENT ). For numeric offsets (where a positive offset subtracts from the current document's sort value, looking into the past, and a negative offset adds to the current document's sort value, shifting the lower boundary past the current document), use the (Any, Any, Expression) overload.

following: WindowBound

The symbolic upper boundary (WindowBound.UNBOUNDED or WindowBound.CURRENT ). For numeric offsets (where a positive offset adds to the current document's sort value, looking into the future, and a negative offset subtracts from the current document's sort value, shifting the upper boundary before the current document), use the (Any, Any, Expression) overload.

unit: Expression

The time unit expression for the range frame.

Returns
WindowSpec

A new WindowSpec with the specified range frame.

range

fun range(preceding: WindowBound, following: WindowBound, unit: String): WindowSpec

Specifies a range frame using symbolic bounds and a time unit.

Supported unit values are "microsecond", "millisecond", "second", "minute", "hour", "day", "week", "month", "quarter", and "year".

In a range frame, WindowBound.CURRENT is peer-inclusive. Specifying a time unit requires a timestamp sort expression (a unit on a string or boolean sort field is rejected by the backend).

Parameters
preceding: WindowBound

The symbolic lower boundary (WindowBound.UNBOUNDED or WindowBound.CURRENT ). For numeric offsets (where a positive offset subtracts from the current document's sort value, looking into the past, and a negative offset adds to the current document's sort value, shifting the lower boundary past the current document), use the (Any, Any, String) overload.

following: WindowBound

The symbolic upper boundary (WindowBound.UNBOUNDED or WindowBound.CURRENT ). For numeric offsets (where a positive offset adds to the current document's sort value, looking into the future, and a negative offset subtracts from the current document's sort value, shifting the upper boundary before the current document), use the (Any, Any, String) overload.

unit: String

The time unit for the range frame.

Returns
WindowSpec

A new WindowSpec with the specified range frame.

sort

fun sort(orders: List<Ordering>): WindowSpec

Specifies the sort order of documents within each partition.

  • For document-based (documents) window frames, sort is optional; if no sort expressions are specified, documents are processed in incoming stream (fetch) order.

  • For range-based (range) window frames, at least one sort expression is required. Range frames whose bounds are only WindowBound.CURRENT and/or WindowBound.UNBOUNDED may use multiple sort orderings and non-numeric sort values (strings, booleans, provided no time unit is set), whereas range frames with numeric or time offsets require a single sort ordering.

  • Setting sort without an explicit documents or range frame changes the default window frame from documents(WindowBound.UNBOUNDED, WindowBound.UNBOUNDED) (when sort is omitted) to range(WindowBound.UNBOUNDED, WindowBound.CURRENT) (when sort is specified).

sort

fun sort(order: Ordering, vararg additionalOrders: Ordering): WindowSpec

Specifies the sort order of documents within each partition.

  • For document-based (documents) window frames, sort is optional; if no sort expressions are specified, documents are processed in incoming stream (fetch) order.

  • For range-based (range) window frames, at least one sort expression is required. Range frames whose bounds are only WindowBound.CURRENT and/or WindowBound.UNBOUNDED may use multiple sort orderings and non-numeric sort values (strings, booleans, provided no time unit is set), whereas range frames with numeric or time offsets require a single sort ordering.

  • Setting sort without an explicit documents or range frame changes the default window frame from documents(WindowBound.UNBOUNDED, WindowBound.UNBOUNDED) (when sort is omitted) to range(WindowBound.UNBOUNDED, WindowBound.CURRENT) (when sort is specified).