WindowSpec
public struct WindowSpec : Sendable[Public Preview] Window specification for window functions.
Defines how documents are partitioned, sorted, and framed when evaluating window functions in
an addWindowFields stage.
- Default frame behavior:
- If
sortis not specified, the default frame isdocumentsfrom.unboundedpreceding to.unboundedfollowing (the entire partition). - Ifsortis specified without an explicitdocumentsorrangeframe, the default frame israngefrom.unboundedpreceding to.current.
Warning
Window Functions SDK features are in preview until the backend feature launches. Look for the launch announcement at: https://docs.cloud.google.com/firestore/docs/release-notes
-
Creates an empty window specification representing a single global partition covering the entire result set, with no sort and no explicit frame.
Declaration
Swift
public init() -
Specify group/partition configuration on top of this spec.
Declaration
Swift
public func partition(_ groups: [Expression]) -> WindowSpec -
Specify group/partition configuration using field names.
Declaration
Swift
public func partition(_ groups: [String]) -> WindowSpec -
Specifies the sort order of documents within each partition.
- For document-based (
documents) window frames,sortis optional; if nosortexpressions are specified, documents are processed in incoming stream (fetch) order. - For range-based (
range) window frames, one or moresortexpressions are required. - Setting
sortwithout an explicitdocumentsorrangeframe changes the default window frame torange(preceding: .unbounded, following: .current).
Multiple sort orderings and string or boolean sort fields are supported for
documentsframes and forrangeframes whose bounds are only.currentor.unbounded(without aunit).rangeframes with numeric or time offsets require exactly one sort ordering on a numeric or timestamp field.Declaration
Swift
public func sort(_ sort: Ordering) -> WindowSpec - For document-based (
-
Specifies the sort order of documents within each partition.
- For document-based (
documents) window frames,sortis optional; if nosortexpressions are specified, documents are processed in incoming stream (fetch) order. - For range-based (
range) window frames, one or moresortexpressions are required. - Setting
sortwithout an explicitdocumentsorrangeframe changes the default window frame torange(preceding: .unbounded, following: .current).
Multiple sort orderings and string or boolean sort fields are supported for
documentsframes and forrangeframes whose bounds are only.currentor.unbounded(without aunit).rangeframes with numeric or time offsets require exactly one sort ordering on a numeric or timestamp field.Declaration
Swift
public func sort(_ sort: [Ordering]) -> WindowSpec - For document-based (
-
Specifies a document-count based window frame (row-based frame).
Defines frame boundaries relative to the current document's position within the partition using document counts. In a
documentsframe,.currentrefers strictly to the current document's position (documents that tie on the sort value(s) at other positions are not included).Note
sortis optional for document-count frames; if nosortis specified on the window, documents are processed in incoming stream (fetch) order.Declaration
Swift
public func documents(preceding: WindowBound, following: WindowBound) -> WindowSpecParameters
precedingThe starting boundary of the window frame. Can be
.unbounded,.current, an integer offset, or anExpression. A positive integer (for example2) includes up to that many documents before the current document. A negative integer (for example-1) indicates a boundary following the current document, enabling frames that start after the current document.followingThe ending boundary of the window frame. Can be
.unbounded,.current, an integer offset, or anExpression. A positive integer (for example2) includes up to that many documents after the current document. A negative integer (for example-1) indicates a boundary preceding the current document, enabling frames that end before the current document (for example excluding the current document).Return Value
A new
WindowSpecwith the document-count based window frame configured. -
Undocumented
Declaration
Swift
public func documents(preceding: Expression, following: Expression) -> WindowSpec -
Undocumented
Declaration
Swift
public func documents(preceding: Expression, following: WindowBound) -> WindowSpec -
Undocumented
Declaration
Swift
public func documents(preceding: WindowBound, following: Expression) -> WindowSpec -
Specifies a range-value based window frame (value-based frame).
Defines frame boundaries relative to the sort key value(s) of the current document within the partition.
In a
rangeframe,.currentis peer-inclusive (like SQLCURRENT ROWinRANGEmode): it includes all documents whose sort value(s) tie with the current document, making.currentequivalent to an offset of0, but also allowed with multiple or non-numeric sort keys. For example, given sort values[10, 10, 20]and.range(preceding: .unbounded, following: .current), the frame for each10document includes both10s, and the frame for20includes all three documents.- When both bounds are
.currentor.unbounded(andunitisnil), the window may specify multiple sort orderings and may sort on string or boolean fields. - When numeric or time offsets are used,
rangerequires exactly one sort ordering on a numeric or timestamp field. Specifying auniton a string or boolean sort field is an error.
Note
One or moresortexpressions are required when using a range-value based window frame. For time-based range frames, the backend only accepts duration units:.microsecond,.millisecond,.second,.minute,.hour,.day,.week,.month,.quarter,.year(or matching strings). Day-of-week week variants (such as.weekMondaythrough.weekSunday) and.isoweek/.isoyearare only for timestamp truncation, not window range duration frames, and will be rejected by the backend.Declaration
Swift
public func range(preceding: WindowBound, following: WindowBound, unit: Sendable? = nil) -> WindowSpecParameters
precedingThe starting boundary of the window frame. Can be
.unbounded,.current, an integer or floating-point offset, or anExpression. 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). For a descending sort, the direction is reversed.followingThe ending boundary of the window frame. Can be
.unbounded,.current, an integer or floating-point offset, or anExpression. 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). For a descending sort, the direction is reversed.unitAn optional date/time granularity unit (such as
TimeGranularity.day) when computing range offsets over timestamp fields. When a unit is specified, offsets must be whole numbers.Return Value
A new
WindowSpecwith the range-value based window frame configured. - When both bounds are
-
Undocumented
Declaration
Swift
public func range(preceding: Expression, following: Expression, unit: Sendable? = nil) -> WindowSpec -
Undocumented
Declaration
Swift
public func range(preceding: Expression, following: WindowBound, unit: Sendable? = nil) -> WindowSpec -
Undocumented
Declaration
Swift
public func range(preceding: WindowBound, following: Expression, unit: Sendable? = nil) -> WindowSpec -
Creates a partition/group specification.
Declaration
Swift
public static func partition(_ groups: [Expression]) -> WindowSpec -
Undocumented
Declaration
Swift
public static func partition(_ groups: [String]) -> WindowSpec -
Creates a window specification with the given sort order for documents within each partition.
- For document-based (
documents) window frames,sortis optional; if nosortexpressions are specified, documents are processed in incoming stream (fetch) order. - For range-based (
range) window frames, one or moresortexpressions are required. - Setting
sortwithout an explicitdocumentsorrangeframe changes the default window frame torange(preceding: .unbounded, following: .current).
Multiple sort orderings and string or boolean sort fields are supported for
documentsframes and forrangeframes whose bounds are only.currentor.unbounded(without aunit).rangeframes with numeric or time offsets require exactly one sort ordering on a numeric or timestamp field.Declaration
Swift
public static func sort(_ sort: Ordering) -> WindowSpec - For document-based (
-
Creates a window specification with the given sort order for documents within each partition.
- For document-based (
documents) window frames,sortis optional; if nosortexpressions are specified, documents are processed in incoming stream (fetch) order. - For range-based (
range) window frames, one or moresortexpressions are required. - Setting
sortwithout an explicitdocumentsorrangeframe changes the default window frame torange(preceding: .unbounded, following: .current).
Multiple sort orderings and string or boolean sort fields are supported for
documentsframes and forrangeframes whose bounds are only.currentor.unbounded(without aunit).rangeframes with numeric or time offsets require exactly one sort ordering on a numeric or timestamp field.Declaration
Swift
public static func sort(_ sort: [Ordering]) -> WindowSpec - For document-based (
-
Creates a document-count based window specification (row-based frame).
Defines frame boundaries relative to the current document's position within the partition using document counts. In a
documentsframe,.currentrefers strictly to the current document's position (documents that tie on the sort value(s) at other positions are not included).Note
sortis optional for document-count frames; if nosortis specified on the window, documents are processed in incoming stream (fetch) order.Declaration
Swift
public static func documents(preceding: WindowBound, following: WindowBound) -> WindowSpecParameters
precedingThe starting boundary of the window frame. Can be
.unbounded,.current, an integer offset, or anExpression. A positive integer (for example2) includes up to that many documents before the current document. A negative integer (for example-1) indicates a boundary following the current document, enabling frames that start after the current document.followingThe ending boundary of the window frame. Can be
.unbounded,.current, an integer offset, or anExpression. A positive integer (for example2) includes up to that many documents after the current document. A negative integer (for example-1) indicates a boundary preceding the current document, enabling frames that end before the current document (for example excluding the current document).Return Value
A new
WindowSpecwith the document-count based window frame. -
Undocumented
Declaration
Swift
public static func documents(preceding: Expression, following: Expression) -> WindowSpec -
Undocumented
Declaration
Swift
public static func documents(preceding: Expression, following: WindowBound) -> WindowSpec -
Undocumented
Declaration
Swift
public static func documents(preceding: WindowBound, following: Expression) -> WindowSpec -
Creates a range-value based window specification (value-based frame).
Defines frame boundaries relative to the sort key value(s) of the current document within the partition.
In a
rangeframe,.currentis peer-inclusive (like SQLCURRENT ROWinRANGEmode): it includes all documents whose sort value(s) tie with the current document, making.currentequivalent to an offset of0, but also allowed with multiple or non-numeric sort keys. For example, given sort values[10, 10, 20]and.range(preceding: .unbounded, following: .current), the frame for each10document includes both10s, and the frame for20includes all three documents.- When both bounds are
.currentor.unbounded(andunitisnil), the window may specify multiple sort orderings and may sort on string or boolean fields. - When numeric or time offsets are used,
rangerequires exactly one sort ordering on a numeric or timestamp field. Specifying auniton a string or boolean sort field is an error.
Note
One or moresortexpressions are required when using a range-value based window frame. For time-based range frames, the backend only accepts duration units:.microsecond,.millisecond,.second,.minute,.hour,.day,.week,.month,.quarter,.year(or matching strings). Day-of-week week variants (such as.weekMondaythrough.weekSunday) and.isoweek/.isoyearare only for timestamp truncation, not window range duration frames, and will be rejected by the backend.Declaration
Swift
public static func range(preceding: WindowBound, following: WindowBound, unit: Sendable? = nil) -> WindowSpecParameters
precedingThe starting boundary of the window frame. Can be
.unbounded,.current, an integer or floating-point offset, or anExpression. 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). For a descending sort, the direction is reversed.followingThe ending boundary of the window frame. Can be
.unbounded,.current, an integer or floating-point offset, or anExpression. 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). For a descending sort, the direction is reversed.unitAn optional date/time granularity unit (such as
TimeGranularity.day) when computing range offsets over timestamp fields. When a unit is specified, offsets must be whole numbers.Return Value
A new
WindowSpecwith the range-value based window frame. - When both bounds are
-
Undocumented
Declaration
Swift
public static func range(preceding: Expression, following: Expression, unit: Sendable? = nil) -> WindowSpec -
Undocumented
Declaration
Swift
public static func range(preceding: Expression, following: WindowBound, unit: Sendable? = nil) -> WindowSpec -
Undocumented
Declaration
Swift
public static func range(preceding: WindowBound, following: Expression, unit: Sendable? = nil) -> WindowSpec