how group graph pattern work in SPARQL

2020-04-20 08:16发布

问题:

First of all, i don't know if this really called group graph pattern or not. Anyway,

Look at this query please

select ?x  ?y where {
  {?x rdf:type rs:Recommendable}
 union
  {?xd rs:doesntexist ?y}
} 

there is no rs:doesntexist but with union i got the results only from the first sub graph which is {?x rdf:type rs:Recommendable}

but if i remove the union, so the query will be:

select ?x  ?y where {
  {?x rdf:type rs:Recommendable}

  {?xd rs:doesntexist ?y}
}

I get empty results, may I ask you please who this work?

and the weird thing to me that this query

select ?x  ?y where {
  {?x rdf:type rs:Recommendable}.{}

}

works perfectly so the one before it doesnt?

Update

I think the union is like optional, I'm not sure. is it right please? (and by optional i didn't mean the optional from sparql, but i meant like when extracting the data from a union two graphs, its optional that two of them have data but if one of them is empty, we'll have the data from the other)

回答1:

select ?x  ?y where {
  {?x rdf:type rs:Recommendable}

  {?xd rs:doesntexist ?y}
}

is like (not identical) to:

select ?x  ?y where {
  ?x rdf:type rs:Recommendable
  ?x rs:doesntexist ?y
}

Both patterns must match. If there is no rs:doesntexist the whole thing fails.

{}

matches all (zero) its patterns so it always works.