Visitar URL original
FirebaseFirestore Framework Reference

FirebaseFirestore Framework Reference

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 sort is not specified, the default frame is documents from .unbounded preceding to .unbounded following (the entire partition). - If sort is specified without an explicit documents or range frame, the default frame is range from .unbounded preceding 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, sort is optional; if no sort expressions are specified, documents are processed in incoming stream (fetch) order.
    • For range-based (range) window frames, one or more sort expressions are required.
    • Setting sort without an explicit documents or range frame changes the default window frame to range(preceding: .unbounded, following: .current).

    Multiple sort orderings and string or boolean sort fields are supported for documents frames and for range frames whose bounds are only .current or .unbounded (without a unit). range frames with numeric or time offsets require exactly one sort ordering on a numeric or timestamp field.

    Declaration

    Swift

    public func sort(_ sort: 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, one or more sort expressions are required.
    • Setting sort without an explicit documents or range frame changes the default window frame to range(preceding: .unbounded, following: .current).

    Multiple sort orderings and string or boolean sort fields are supported for documents frames and for range frames whose bounds are only .current or .unbounded (without a unit). range frames with numeric or time offsets require exactly one sort ordering on a numeric or timestamp field.

    Declaration

    Swift

    public func sort(_ sort: [Ordering]) -> WindowSpec
  • 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 documents frame, .current refers strictly to the current document's position (documents that tie on the sort value(s) at other positions are not included).

    Note

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

    Declaration

    Swift

    public func documents(preceding: WindowBound, following: WindowBound) -> WindowSpec

    Parameters

    preceding

    The starting boundary of the window frame. Can be .unbounded, .current, an integer offset, or an Expression. A positive integer (for example 2) 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.

    following

    The ending boundary of the window frame. Can be .unbounded, .current, an integer offset, or an Expression. A positive integer (for example 2) 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 WindowSpec with 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 range frame, .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 .current equivalent to an offset of 0, 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 each 10 document includes both 10s, and the frame for 20 includes all three documents.

    • When both bounds are .current or .unbounded (and unit is nil), the window may specify multiple sort orderings and may sort on string or boolean fields.
    • When numeric or time offsets are used, range requires exactly one sort ordering on a numeric or timestamp field. Specifying a unit on a string or boolean sort field is an error.

    Note

    One or more sort expressions 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 .weekMonday through .weekSunday) and .isoweek/.isoyear are 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) -> WindowSpec

    Parameters

    preceding

    The starting boundary of the window frame. Can be .unbounded, .current, an integer or floating-point offset, or an 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). For a descending sort, the direction is reversed.

    following

    The ending boundary of the window frame. Can be .unbounded, .current, an integer or floating-point offset, or an 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). For a descending sort, the direction is reversed.

    unit

    An 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 WindowSpec with the range-value based window frame configured.

  • 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, sort is optional; if no sort expressions are specified, documents are processed in incoming stream (fetch) order.
    • For range-based (range) window frames, one or more sort expressions are required.
    • Setting sort without an explicit documents or range frame changes the default window frame to range(preceding: .unbounded, following: .current).

    Multiple sort orderings and string or boolean sort fields are supported for documents frames and for range frames whose bounds are only .current or .unbounded (without a unit). range frames 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
  • Creates a window specification with the given sort order for 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, one or more sort expressions are required.
    • Setting sort without an explicit documents or range frame changes the default window frame to range(preceding: .unbounded, following: .current).

    Multiple sort orderings and string or boolean sort fields are supported for documents frames and for range frames whose bounds are only .current or .unbounded (without a unit). range frames 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
  • 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 documents frame, .current refers strictly to the current document's position (documents that tie on the sort value(s) at other positions are not included).

    Note

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

    Declaration

    Swift

    public static func documents(preceding: WindowBound, following: WindowBound) -> WindowSpec

    Parameters

    preceding

    The starting boundary of the window frame. Can be .unbounded, .current, an integer offset, or an Expression. A positive integer (for example 2) 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.

    following

    The ending boundary of the window frame. Can be .unbounded, .current, an integer offset, or an Expression. A positive integer (for example 2) 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 WindowSpec with 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 range frame, .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 .current equivalent to an offset of 0, 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 each 10 document includes both 10s, and the frame for 20 includes all three documents.

    • When both bounds are .current or .unbounded (and unit is nil), the window may specify multiple sort orderings and may sort on string or boolean fields.
    • When numeric or time offsets are used, range requires exactly one sort ordering on a numeric or timestamp field. Specifying a unit on a string or boolean sort field is an error.

    Note

    One or more sort expressions 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 .weekMonday through .weekSunday) and .isoweek/.isoyear are 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) -> WindowSpec

    Parameters

    preceding

    The starting boundary of the window frame. Can be .unbounded, .current, an integer or floating-point offset, or an 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). For a descending sort, the direction is reversed.

    following

    The ending boundary of the window frame. Can be .unbounded, .current, an integer or floating-point offset, or an 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). For a descending sort, the direction is reversed.

    unit

    An 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 WindowSpec with the range-value based window frame.

  • 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