SPARQL query GRAPH unions (maybe through virtual graph)

Hi,
As far as we know, in SPARQL query processing, the FROM clause is used to identify the default graph for a query and if multiple FROM clauses are specified in a query then the contents of those graphs are merged to provide a union graph. Having said that, our questions are:

  • If multiple FROM NAMED clauses are specified in a query then the contents of those graphs are not merged to provide a union graph, right?
  • If yes, is there any trick or workaround to specify multiple FROM NAMED clauses in a query to execute the query over the union of all named graphs while returning the graph name as a variable too?
  • Is it possible to create a virtual graph which is a union of two or more named graphs?

Thank you!

Suppose you have this data:

INSERT DATA {
  GRAPH <graph:a> {
    <urn:a> a <urn:Gecko> .
  }
  GRAPH <graph:b> {
    <urn:a> rdfs:label "Carl" .
  }
}

and want something like:

SELECT ?g ?type ?label FROM NAMED <graph:a> FROM NAMED <graph:b> {
  GRAPH ?g {
    <urn:a> a ?type ;
      rdfs:label ?label .
  }
}

... but the type triple is in one graph and the label is in another and you don't know which is which.

You can use:

SELECT ?gt ?type ?gl ?label FROM NAMED <graph:a> FROM NAMED <graph:b> {
  GRAPH ?gt {
    <urn:a> a ?type ;
  }
  GRAPH ?gl {
    <urn:a> rdfs:label ?label .
  }
}

Note this is a join rather than a union. The significance is that it will not return any solutions unless it finds a match for both type and label.

-Paul

1 Like