Let's say I make the following insertions into my GraphDB 8.3 triplestore:
PREFIX : <http://example.com/>
insert data { :hello a :word }
and
PREFIX : <http://example.com/>
insert data { graph :farewells { :goodbye a :word }}
now, if I ask
select * where {
graph ?g {
?s ?p ?o .
}
}
I only get
+--------------------------------+------------------------------+---------------------------------------------------+---------------------------+
| ?g | ?s | ?p | ?o |
+--------------------------------+------------------------------+---------------------------------------------------+---------------------------+
| <http://example.com/farewells> | <http://example.com/goodbye> | <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> | <http://example.com/word> |
+--------------------------------+------------------------------+---------------------------------------------------+---------------------------+
I can obviously get both "triples about words" with the following, but then the named-graph membership is not shown
select * { ?s ?p ?o }
How can I write a query that retrieves both triples about words and indicates that { :goodbye a :word }
comes from graph :farewells
?
In case you use have to use implicit triples in other part of the query, and in case a FILTER NOT EXISTS
causes performance issues.
Then you can query what you inserted without graph clause with GRAPH <http://rdf4j.org/schema/rdf4j#nil>
In your example, you could use the query:
SELECT *
WHERE {
{ GRAPH ?g { ?s ?p ?o } }
UNION { GRAPH <http://rdf4j.org/schema/rdf4j#nil> { ?s ?p ?o } }
}
returns:
+------------------------------------+----------+-----------+-------+
| ?g | ?s | ?p | ?o |
+------------------------------------+----------+-----------+-------+
| | :hello | rdf:type | :word |
| :farewells | :goodbye | rdf:type | :word |
+------------------------------------+----------+-----------+-------+
At least it works in my free graphDB GraphDB 10.2.0 • RDF4J 4.2.2
Edit: I found one "better":
SELECT *
WHERE {
VALUES ?g {UNDEF <http://rdf4j.org/schema/rdf4j#nil>}
GRAPH ?g { ?s ?p ?o }
}
It returns:
+------------------------------------+----------+-----------+-------+
| ?g | ?s | ?p | ?o |
+------------------------------------+----------+-----------+-------+
| rdf4j:nil | :hello | rdf:type | :word |
| :farewells | :goodbye | rdf:type | :word |
+------------------------------------+----------+-----------+-------+