Algorithm Search⌘K

Modeladvanced

CQL stream-relation model

Also called Continuous Query Language, stream-to-relation operators.

Define stream queries by window operators that turn streams into relations, relational operators, and operators that turn relations back into streams.

The CQL stream model, of Arasu, Babu and Widom (2006), gives a precise meaning to a continuous query over streams and relations that change with time: window operators turn a stream into a relation, ordinary relational operators act on the relation at each instant, and the operators Istream, Dstream and Rstream turn a relation back into a stream. A continuous query is the general notion of a query whose answer follows a growing input; CQL is one semantics for it, with snapshot answers at each instant of a single ordered time domain. The Dataflow model differs: it separates event time from processing time, lets input arrive out of order, and makes the time and the form of each output a choice of the user. CQL also differs from the sliding window model, which is a model of computation for small-memory algorithms over the last WW items and not a query language.

The machine

Time domain. Time is a discrete, ordered domain T={0,1,2,…}\mathcal{T} = \{0, 1, 2, \ldots\}. A time instant is an element τ\tau of T\mathcal{T}. The domain models the time of the application, not the wall clock of the system, and the semantics needs only a discrete, ordered domain (Section 4).

Streams. A stream SS is a possibly infinite bag of elements ⟨s,τ⟩\langle s, \tau \rangle, where ss is a tuple of the schema of SS and τ∈T\tau \in \mathcal{T} is its timestamp. For each τ\tau, only finitely many elements have timestamp τ\tau (Definition 1). A base stream is an input; a derived stream is the output of an operator.

Relations. A relation RR is a map from T\mathcal{T} to finite bags of tuples of its schema. R(τ)R(\tau) is the instantaneous relation of RR at τ\tau (Definition 2). A stored table that is updated now and then is a relation in this sense: its value at τ\tau is its content after the updates up to τ\tau.

Operators. There are three classes, and no direct operator from streams to streams.

  1. Stream-to-relation operators are windows. Each one maps a stream SS to a relation RR.
    • S [Range T]S\,[\text{Range } T], a time-based window: R(τ)={s∣⟨s,τ′⟩∈S, max⁡(τ−T,0)≤τ′≤τ}R(\tau) = \{ s \mid \langle s, \tau' \rangle \in S,\ \max(\tau - T, 0) \le \tau' \le \tau \}. The interval is closed at both ends, so it covers T+1T + 1 instants.
    • S [Now]S\,[\text{Now}] is [Range 0][\text{Range } 0]: the elements with timestamp exactly τ\tau. S [Range Unbounded]S\,[\text{Range Unbounded}] has T=∞T = \infty: all elements up to τ\tau.
    • S [Rows N]S\,[\text{Rows } N], a count-based window: the NN elements with the largest timestamps at most τ\tau, or all of them if there are fewer than NN. If several elements share the timestamp at the boundary, the choice among them is not fixed, so this window can be nondeterministic.
    • S [Partition By A1,…,Ak Rows N]S\,[\text{Partition By } A_1, \ldots, A_k \text{ Rows } N]: split SS by the values of A1,…,AkA_1, \ldots, A_k, take [Rows N][\text{Rows } N] of each part, and take the union.
    • S [Range T Slide L]S\,[\text{Range } T \text{ Slide } L]: the window of [Range T][\text{Range } T] taken at the instant ⌊τ/L⌋⋅L\lfloor \tau / L \rfloor \cdot L, so the relation changes only once every LL instants. With L=TL = T this is a tumbling window.
  2. Relation-to-relation operators are the operators of SQL or the relational algebra, applied at each instant: O(R1,…,Rn)(τ)=O(R1(τ),…,Rn(τ))O(R_1, \ldots, R_n)(\tau) = O(R_1(\tau), \ldots, R_n(\tau)).
  3. Relation-to-stream operators. With bag union, bag difference and R(−1)=∅R(-1) = \emptyset:
    • Istream(R)=⋃τ≥0((R(τ)−R(τ−1))×{τ})\text{Istream}(R) = \bigcup_{\tau \ge 0} \big( (R(\tau) - R(\tau - 1)) \times \{\tau\} \big): the tuples inserted at τ\tau.
    • Dstream(R)=⋃τ>0((R(τ−1)−R(τ))×{τ})\text{Dstream}(R) = \bigcup_{\tau > 0} \big( (R(\tau - 1) - R(\tau)) \times \{\tau\} \big): the tuples deleted at τ\tau.
    • Rstream(R)=⋃τ≥0(R(τ)×{τ})\text{Rstream}(R) = \bigcup_{\tau \ge 0} \big( R(\tau) \times \{\tau\} \big): the whole relation at every instant.

Queries and their semantics. A CQL query is a composition of these operators, written in SQL with window clauses after each stream in the From clause and an Istream, Dstream or Rstream in the Select clause. When the query result is a relation and no relation-to-stream operator is given, but the relation can only grow, CQL adds Istream by default (Section 6.4). The continuous semantics (Definition 3) is that the result at τ\tau, a relation R(τ)R(\tau) or the stream elements with timestamp τ\tau, depends only on the input elements with timestamp at most τ\tau and on the relations up to τ\tau.

When time advances. Time advances from τ−1\tau - 1 to τ\tau when all inputs with timestamps up to τ−1\tau - 1 have been processed. The semantics thus assumes that the system knows when the input of an instant is complete. Section 8 of the paper meets this with heartbeats: a heartbeat with timestamp τ\tau promises that no later stream element has a timestamp of τ\tau or less. A heartbeat can come from the system clock (the system stamps elements on arrival, so they are in order by construction), from each source (the query-level heartbeat is the minimum of the source heartbeats), or from a known bound DD on the delay, as t−Dt - D at global time tt.

Late data. An element that arrives after a heartbeat that covers its timestamp breaks the promise. The model does not give it a meaning: the answers for the instants up to the heartbeat are already final. A heartbeat of the third kind is a heuristic if the bound DD is only an estimate, and then the system must drop such elements or treat them by a policy outside the model. This is the main difference from the Dataflow model, where late data is a normal case and earlier answers can be refined. Heartbeats are the same idea as an event-time watermark and as a punctuation on the timestamp attribute.

What it counts

The model assigns to each query and each instant τ\tau one result: the instantaneous relation R(τ)R(\tau) if the query returns a relation, or the bag of elements with timestamp τ\tau if it returns a stream. Equality of two queries means equality of these results at every instant, for every input. The model is about meaning, not cost, but the paper and the STREAM system give the memory that each operator needs, its synopsis. With the cost conventions of this area, where nn is the number of elements in the window:

  • [Rows N][\text{Rows } N] keeps the last NN elements in a queue. Each arrival does one insert and at most one expiry, so the work is O(1)O(1) worst case per element and the memory is NN elements.
  • [Range T][\text{Range } T] keeps the elements of the last T+1T + 1 instants. Each element is inserted once and expired once, so the work is O(1)O(1) amortized per element. The memory nn has no bound in terms of TT alone, because the model allows any finite number of elements per instant.
  • Istream\text{Istream} and Dstream\text{Dstream} must compare R(τ)R(\tau) with R(τ−1)R(\tau - 1), so they keep the previous instantaneous relation or receive their input as a stream of inserts and deletes. The STREAM system does the second: every operator exchanges a stream of insertions and deletions, which it calls a plus-minus stream (Section 12).
  • An aggregate over a window, such as Count(∗)\text{Count}(*) over [Range T][\text{Range } T], is maintained in O(1)O(1) per insert and delete when the aggregate has an inverse. Without an inverse, it needs the methods of sliding window aggregation.

Relation to other models

  • Tapestry. A Tapestry query returns at τ\tau the union of the answers of a one-time query at all instants up to τ\tau (see continuous query). Section 11.2 shows that this is the CQL query Select Istream(Distinct *) From (Istream(QQ)) [Range Unbounded]. Tapestry has no sliding windows and no relation-to-stream operators.
  • Materialized views and chronicles. A view defined by a SQL query QQ over relations is the same QQ in CQL. The chronicle data model of Jagadish et al. has streams (chronicles), relations and persistent views, and both of its operator classes are expressible in CQL (Section 11.1).
  • Stream-only languages. For each relational operator OO, define a stream operator Os(S1,…,Sn)=Rstream(O(S1[Now],…,Sn[Now]))O_s(S_1, \ldots, S_n) = \text{Rstream}(O(S_1[\text{Now}], \ldots, S_n[\text{Now}])), and for each window WW, define S[Ws]=Rstream(S[W])S[W_s] = \text{Rstream}(S[W]). Section 11.6 shows that the stream-only language LsL_s obtained this way has essentially the same power as CQL, and that it is close to the language of TelegraphCQ. The authors keep both types because SQL then applies unchanged.
  • The Dataflow model. CQL has one time domain and assumes complete input up to each instant. The Dataflow model has event time and processing time, uses a watermark that may be wrong, lets a window emit early, on-time and late results, and defines how later results relate to earlier ones. A CQL window at instant τ\tau is close to a Dataflow window that fires once when the watermark passes its end, in discarding mode.
  • Windows of other pages. [Range T Slide L][\text{Range } T \text{ Slide } L] with L<TL < T is a hopping window, and with L=TL = T a tumbling window. CQL has no session window; a session window is defined by gaps in the data and not by a fixed interval.

Known results in the model

All results here are theorems in the model, from Sections 6, 10 and 11 of Arasu, Babu and Widom (2006).

Rstream is enough. Istream and Dstream can be written with Rstream, windows and relational operators (Section 6.3). So every query has an equivalent form whose only relation-to-stream operator is Rstream.

Window reduction (Section 10.1). For any list LL of attributes and any condition CC, Select Istream(LL) From SS [Range Unbounded] Where CC is equivalent to Select Rstream(LL) From SS [Now] Where CC. Both return each element of SS that satisfies CC, at its own timestamp. More generally, Istream and Rstream give the same stream on any relation RR with R(τ)∩R(τ−1)=∅R(\tau) \cap R(\tau - 1) = \emptyset for all τ\tau. The second form needs no state, so a system should use it.

Filter-window commutativity (Section 10.2). A filter on a stream commutes with a time-based window: σC(S [Range T])=(σCS) [Range T]\sigma_C(S\,[\text{Range } T]) = (\sigma_C S)\,[\text{Range } T]. The same does not hold for a tuple-based window in general. For example, with [Rows 1][\text{Rows } 1], the filter applied after the window can return nothing when the last element fails CC, while the window over the filtered stream returns the last element that passes CC.

Expressiveness (Section 11). Basic CQL, without user-defined functions, aggregates or windows beyond those above, is strictly more expressive than Tapestry, Tribeca, GSQL and materialized views over relations with or without chronicles.

A worked example. The stream SS has the elements ⟨a,1⟩\langle a, 1 \rangle, ⟨b,1⟩\langle b, 1 \rangle, ⟨c,2⟩\langle c, 2 \rangle, ⟨d,4⟩\langle d, 4 \rangle and ⟨e,5⟩\langle e, 5 \rangle. The window is S [Range 2]S\,[\text{Range } 2], so at τ\tau it holds the elements with timestamps from max⁡(τ−2,0)\max(\tau - 2, 0) to τ\tau.

τ\tau S [Range 2](τ)S\,[\text{Range } 2](\tau) Istream at τ\tau Dstream at τ\tau S [Now](τ)S\,[\text{Now}](\tau)
0 empty none none empty
1 a, b a, b none a, b
2 a, b, c c none c
3 a, b, c none none empty
4 c, d d a, b d
5 d, e e c e
6 d, e none none empty
7 e none d empty

The Rstream of the window repeats the whole relation at each instant, for example ⟨c,4⟩\langle c, 4 \rangle and ⟨d,4⟩\langle d, 4 \rangle at τ=4\tau = 4. Select Rstream(Count(*)) From SS [Range 2] gives the counts 2, 3, 3, 2, 2, 2 and 1 at τ=1\tau = 1 to 7. For S [Rows 2]S\,[\text{Rows } 2] at τ=2\tau = 2, the window holds cc and one of aa and bb, because both have timestamp 1; CQL leaves this choice open. The Istream of S [Range Unbounded]S\,[\text{Range Unbounded}] and the Rstream of S [Now]S\,[\text{Now}] both give aa and bb at 1, cc at 2, dd at 4 and ee at 5, as window reduction says.

References

  1. Arvind Arasu, Shivnath Babu and Jennifer Widom, The CQL continuous query language, semantic foundations and query execution, The VLDB Journal 15(2), 2006, pages 121 to 142, Section 4 (streams and relations, Definitions 1 and 2), Section 5 (the abstract semantics, Definition 3), Section 6 (the window operators of 6.1 and Istream, Dstream and Rstream of 6.3), Section 8 (heartbeats), Section 10 (window reduction and filter-window commutativity), Section 11 (comparison with Tapestry, Tribeca, GSQL and chronicles, and the stream-only language of 11.6) and Section 12 (the STREAM implementation)
  2. Models and issues in data stream systems, the discussion of windows, blocking operators and the memory needs of continuous queries
  3. Douglas Terry, David Goldberg, David Nichols and Brian Oki, Continuous queries over append-only databases, Proceedings of ACM SIGMOD 1992, pages 321 to 330, the Tapestry semantics that CQL generalizes

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/MST56H/text.yaml