STREAM Online Help > Running the System > CQL Continuous Query Language
This section gives a quick introduction to the CQL declarative continuous query language for streams and relations supported by the STREAM data stream management system. We also point to literature where more information on CQL can be obtained. We recommend that users of the demo read this page before attempting to run queries on STREAM.
CQL, which stands for Continuous Query Language, is based on modifications to SQL to support streams plus relations and continuous result semantics. CQL retains SQL's familiar Select-From-Where syntax. Some new reserved words have been added to support sliding windows, sampling, etc. We will introduce the syntax and semantics of CQL through a few examples.
One Stream Query
This query is probably the simplest query that one can think of over stream R.
Simple Aggregation Query
This query is a simple windowed aggregation over stream S, the window consisting of the tuples in the last 15 seconds in S.
SELECT name, AVG(value)
FROM S RANGE 15 SECONDS
GROUP BY name
Simple Join Query
This query is a simple windowed equijoin query over streams R and S on attribute name, the windows each consisting of the last 10 tuples in the respective streams.
FROM R ROWS 10, S ROWS 10
WHERE R.name = S.name
These example queries show how CQL retains the look and feel of SQL by retaining SQL's Select-From-Where-GroupBy syntax. CQL supports streams in addition to relations. A stream in CQL is a possibly infinite multiset of timestamped elements. Streams can appear only in the From clause of a CQL query and they must be accompanied by a sliding window specification, with the default window consisting of all tuples that have arrived in the stream so far. (Since the example One Stream Query does not specify a window over R, the default window will be used.) Apart from the input streams that arrive at the system, streams can also be derived from other streams and relations using CQL (sub)queries.
A relation in CQL is like a materialized view in a traditional DBMS: A relation is a time-varying multiset of tuples. That is, a relation contains a (possibly empty) multiset of tuples at any point in time and this multiset changes over time though insertions and deletions of tuples.A sliding window is applied to a stream and it converts the stream into a relation. The three common types of sliding windows used in CQL include:
ISTREAM, DSTREAM, and RSTREAM are the three operators supported by CQL to convert relations to streams in queries. The semantics of these conversions are defined below:
Intuitively, applying ISTREAM over a relation Rel (in CQL, ISTREAM (SELECT * FROM Rel)) outputs a stream consisting of the insertions to Rel. (Recall again that a relation in CQL is just like a materialized view in a DBMS.) Similarly, applying DSTREAM over a relation Rel (in CQL, DSTREAM (SELECT * FROM Rel)) outputs a stream consisting of the deletions from Rel. Applying RSTREAM over a relation Rel (in CQL, RSTREAM (SELECT * FROM Rel)) will output a stream consisting of the contents of Rel at each instant of time. We give one example query here to illustrate these operators. More examples follow in the Running the Benchmark section of this help.
Assume we have an relation CurPrice(stock integer, price float), where
price represents the current price of the stock
stock. We are interested in the average price of each
stock over the last day. This query can be expressed in
STREAM in the following manner:
REGISTER STREAM PriceStream (stock integer, price float);
ISTREAM (SELECT * FROM CurPrice) INTO PriceStream;
SELECT stock, AVG(price)
FROM PriceStream RANGE 1 DAY
GROUP BY stock
The above specification creates a derived stream called PriceStream and specifies that this stream contains the insertions to the CurPrice relation. The CQL query then applies a 1-day window over PriceStream and does the required aggregation.
More example CQL queries are given in the Running the Benchmark section of the help. Before we give the citations to literature where more information on the syntax, semantics, and execution of CQL queries can be obtained, here is something that must be borne in mind:
WARNING: The CQL syntax supported currently by STREAM differs from the example CQL queries that have appeared in previous papers and talks on CQL and also from the queries in the Stream Query Repository. The authoritative source on CQL query syntax supported currently by STREAM is available here.Noteworthy differences in CQL syntax between the demo and earlier papers, talks, and the stream query repository include: