Sequence service
This page presents a SPARQL service which can be used to generate numerical sequences and process them in SPARQL queries.
Page Contents
Description and examples
It is often useful to be able to generate sequences within a SPARQL query. One common use case is testing, particularly, issuing queries which consistently take a long time without overusing the server’s hardware resources (other than the CPU):
prefix seq: <tag:stardog:api:sequence:>
select (count(*) as ?count) (sum(?output) as ?sum ) {
service seq:service {
[] seq:output ?output ;
seq:start 1;
seq:increment 1;
seq:stop 100000000 .
}
}
The above query will generate numbers from 1 to 100M and return a count and sum of them all. Another possibility is to generate test data entirely within SPARQL, such as 100M instances of a certain class:
prefix seq: <tag:stardog:api:sequence:>
insert { ?s a <urn:Class> } WHERE {
service seq:service {
[] seq:output ?n ;
seq:start 1;
seq:increment 1;
seq:stop 100 .
}
bind(iri(concat("urn:", str(?n))) as ?s)
}
Of course, the generated sequence can be freely joined with other SPARQL pattern, for example, to check whether certain data exists:
select ?s {
service seq:service {
[] seq:output ?n ;
seq:start 1;
seq:stop 1000 .
}
bind(iri(concat("urn:", str(?n))) as ?s)
filter exists { ?s a <urn:Class> }
}
Note the use of SPARQL functions str
, concat
, and iri
to map the generated numbers to IRIs before applying the filter.
Parameters
tag:stardog:api:sequence:output
: defines the variable to hold each generated numbertag:stardog:api:sequence:start
: defines the first number (inclusive)tag:stardog:api:sequence:stop
: defines the last number (inclusive)tag:stardog:api:sequence:increment
: defines the delta between each two consecutive numbers (can be negative)