Skip to content

Query Definition in RSP4J

Riccardo Tommasini edited this page Jan 2, 2021 · 3 revisions

RSP-QL is a reference model for explaining the semantics of RSP dialects and the execution semantics RSP engines. The RSP W3C working group is still working on a standard syntax. Nevertheless, the community already reached an agreement on the language pillars.

Background on Continuous Queries

Continuous queries, also known as persistent or standing queries, are a special class of queries that allow interested users to receive new results as soon as data becomes available. Differently from traditional database queries, CQ run repeatedly over the input streams and provide updated answers.

To understand CF, we need to understand the notion of continuous semantics, i.e., the processing of an infinite input produces an infinite output [@DBLP:conf/sigmod/TerryGNO92].

Under continuous semantics, the result of a query is the set of results that would be returned if the query were executed at every instant in time.

Many continuous query languages have been designed for to this extent. RSP-QL, and its fragments, are based upon the Continuous Query Language (CQL) [@DBLP:journals/vldb/ArasuBW06]. CQL passed the test of time for what concerns relational data Stream Processing.

The figure above shows CQL's main abstractions Streams and Relations.

A data stream S is a possibly infinite multiset of elements <o,t>, where o is a data item, e.g., a tuple, and t is a timestamp.

A relation R is a mapping from each time instant t to a finite but unbounded bag of tuples belonging to a fixed schema consisting of a set of named attributes.

Upon these two abstractions, CQL defines three classes of operators (also in the figure above) that, together, allow to write continuous queries over data streams:

Continuous queries can be registered on the color stream, e.g., for counting the number of green boxes in the last 15 minutes as shown in the figure below.

RSP-QL Primer

SP-QL extends SPARQL 1.1 syntax and semantics to define streaming transformations over RDF Streams. The RSP-QL semantic is inspired by CQL and SECRET. The formal framework includes operator classes (like CQL), and primitives to describe the operational semantics (like SECRET).

The figure above depicts the anatomy of an RSP-QL query. An RSP-QL continuous query consists of a Dataset Clause, a Query Form, and a Where Clause. RSP-QL extends SPARQL' Dataset Clause to target and query streams.

Continuous queries over streams can be enriched with contextual domain knowledge. For instance, for counting the cool colors in the last minute window we must add an ontology of colors as shown in the figure below, where using an ontology we can be used for identifying warm colors.

My First RSP-QL Query

The previously registered color stream can be queried to retrieve the number of blue boxes in the last minute. This requires the RSP to ingest the data stream and to register a continuous query. Listing 1.8 shows the corresponding RSP-QL query.

1 PREFIX color: <http://linkeddata.stream/ontologies/colors#>
2 PREFIX : <http://linkeddata.stream/resource/>
3 REGISTER RSTREAM <sout> AS 
4 SELECT (COUNT(?y) as ?numYellows)
5 FROM NAMED WINDOW <bw> ON :colorstream [RANGE PT15S STEP PT5S]
6 WHERE {
7    WINDOW ?bw { ?y a color:Yellow .}
8 }

The query above is an example of RSP-QL query. Like a SPARQL query, an RSP-QL query consists of a Dataset Clause, a Query Form, and a Where Clause. RSP-QL extends the SPARQL's Dataset Clause to include Window Operators (Line 5); it introduces the WINDOW keywords for referring to stream in the Where Clause (Line 7), and it adds the REGISTER clause to name the R2S operators and the output streams (Line 3). Notably, the output of an RSP-QL query with the register clause (named RSP-QL query) is not necessarily an RDF Stream. It depends on the Query Form.

Using RSP-QL syntax in RSP4J

public class QueryTest {

    public static void main(String[] args) throws IOException {

        String q = ""+
                 "PREFIX color: <http://linkeddata.stream/ontologies/colors#>\n" +
                 "PREFIX color: <http://linkeddata.stream/ontologies/colors#>\n" +
                 "REGISTER RSTREAM <output> AS\n" +
                 "SELECT (COUNT(?y) as ?numYellows)\n" +
                 "FROM NAMED WINDOW <yw> ON :colorstream [RANGE PT15S STEP PT5S]\n" +
                 "WHERE {\n" +
                 "   WINDOW <yw> { ?y a color:Yellow .}\n" +
                 "}";

        ContinuousQuery parse = QueryFactory.parse(q);


    }