The Difference between FROM and FROM NAMED in SPARQL, and the SeRQL Alternative

The SYNAT project involves intense use of semantic web technologies. We store data in an RDF repository (OWLIM), and use the SPARQL and SeRQL languages to query the data. The former is considered a standard, the latter, proposed by Aduna (producer of the Sesame RDF repository) is easier to use at least for some of us.

Last week we realized that no matter how often we use SPARQL, it was about time to fully understand the difference between FROM and FROM NAMED. It turned out that finding a reliable and complete source providing this information was not that easy, so we decided to create this post (based on this post on a team member’s private blog) to clear the matters.

It seems that one of the bigger problems is the name itself. Both FROM and FROM NAMED concern named graphs, which we hold reponsible for a lot of misunderstandings around those clauses. Below is a consise Q&A section that descibes the situation.

If you do not declare FROM or FROM NAMED, what exactly do you query?
You query the active graph. The active graph does not need to be the default graph! In OWLIM, for instance, the active graph is the whole of the repository.

Example (from the SPARQL specification):

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?nameX ?nameY ?nickY
WHERE
  { ?x foaf:knows ?y ;
       foaf:name ?nameX .
    ?y foaf:name ?nameY .
    OPTIONAL { ?y foaf:nick ?nickY }
  }

What is the active graph?
It is the graph(s) that is queried when FROM and FROM NAMED are not used. It might be the default graph, the whole repository contents… or possibly something else, depending on the implementation.

What is the default graph?
The default graph is the graph without a name, or without a context. This is the graph whose triples are in fact triples and not quads.

What does the FROM clause change?
If you use the FROM clause, you restrict the set of graphs that are queried. Only the named graph(s) given in the FROM clause(s) will be considered while matching the template.

Example. Only triples from the <http://example.org/foaf/aliceFoaf> graph will be used.

PREFIX foaf: <http://xmlns.com/foaf/0.1/glt;
SELECT  ?name
FROM    <http://example.org/foaf/aliceFoaf>
WHERE   { ?x foaf:name ?name }

What does the FROM NAMED clause change?
If you use the FROM NAMED, every graph name you use in the query will be matched only to the graph provided in the clause.

Example (which combines FROM and FROM NAMED). ?g will be matched either to <http://example.org/alice> or to <http://example.org/bob>, but to no other named graph.

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>

SELECT ?who ?g ?mbox
FROM <http://example.org/dft.ttl>
FROM NAMED <http://example.org/alice>
FROM NAMED <http://example.org/bob>
WHERE
{
   ?g dc:publisher ?who .
   GRAPH ?g { ?x foaf:mbox ?mbox }
}

Can you combine FROM and FROM NAMED?
Yes, see the question above. In the example the named triple has to be found in one of the graphs given in the FROM NAMED clause, and the loose triple will be matched against the graph given in the FROM clause.

What if there is only one FROM NAMED clause?
Then the following two queries yield the same results:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>

SELECT ?who ?mbox
FROM <http://example.org/dft.ttl>
FROM NAMED <http://example.org/alice>
WHERE
{
   ?g dc:publisher ?who .
   GRAPH ?g { ?x foaf:mbox ?mbox }
}

is equal to

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>

SELECT ?who ?mbox
FROM <http://example.org/dft.ttl>
WHERE
{
   ?g dc:publisher ?who .
   GRAPH <http://example.org/alice> { ?x foaf:mbox ?mbox }
}

Is it easier in SeRQL?
Yes. Both FROM and FROM NAMED are done using the FROM CONTEXT clause (example from the SeRQL specification):

SELECT name, mbox
FROM CONTEXT <http://example.org/context/graph2>
     {x} foaf:name {name};
         foaf:mbox {mbox}
USING NAMESPACE
foaf = <http://xmlns.com/foaf/0.1/>

Leave a Reply

Your email address will not be published. Required fields are marked *

*