Additional SPARQL query

I have a turtle format RDF in the following

    @prefix ab: <http://learningsparql.com/ns/addressbook#> .
    @prefix d: <http://learningsparql.com/ns/data#> .
    d:i0432 ab:firstName "Richard" .
    d:i0432 ab:lastName "Mutt" .
    d:i0432 ab:homeTel "(229) 276-5135" .
    d:i0432 ab:nick "Dick" .
    d:i0432 ab:email "richard49@hotmail.com" .
    d:i9771 ab:firstName "Cindy" .
    d:i9771 ab:lastName "Marshall" .
    d:i9771 ab:homeTel "(245) 646-5488" .
    d:i9771 ab:email "cindym@gmail.com" .
    d:i8301 ab:firstName "Craig" .
    d:i8301 ab:lastName "Ellis" .
    d:i8301 ab:workTel "(245) 315-5486" .
    d:i8301 ab:email "craigellis@yahoo.com" .
    d:i8301 ab:email "c.ellis@usairwaysgroup.com" .

and request

    PREFIX ab: <http://learningsparql.com/ns/addressbook#>
    SELECT ?first ?last
    WHERE
    {
    ?s ab:lastName ?last .
    OPTIONAL {?s ab:nick ?first. }.
    OPTIONAL {?s ab:firstName ?first .}.
    }

result

    ------------------------
    | first   | last       |
    ========================
    | "Craig" | "Ellis"    |
    | "Cindy" | "Marshall" |
    | "Dick"  | "Mutt"     |
    ------------------------

but if I changed the request to

    PREFIX ab: <http://learningsparql.com/ns/addressbook#>
    SELECT ?first ?last
    WHERE
    {
    OPTIONAL {?s ab:nick ?first. }.
    OPTIONAL {?s ab:firstName ?first .}.
    ?s ab:lastName ?last .
    }

result

    -------------------
    | first  | last   |
    ===================
    | "Dick" | "Mutt" |
    -------------------

Can anyone explain what is the reason for this difference? I thought that the period in the SPARQL query matches the "and" operator.

+4
source share
1 answer

It's important to have order here

The semantics of SPARQL queries are expressed in terms of the SPARQL algebra, and the two queries here produce very different algebras. I am using the SPARQL Query Validator provided by Apache Jena (disclaimer - I participate in this project) to generate algebra.

In the first query, the following algebra is created:

(base <http://example/base/>
  (prefix ((ab: <http://learningsparql.com/ns/addressbook#>))
    (project (?first ?last)
      (leftjoin
        (leftjoin
          (bgp (triple ?s ab:lastName ?last))
          (bgp (triple ?s ab:nick ?first)))
        (bgp (triple ?s ab:firstName ?first))))))

And your second query creates the following algebra:

(base <http://example/base/>
  (prefix ((ab: <http://learningsparql.com/ns/addressbook#>))
    (project (?first ?last)
      (join
        (leftjoin
          (leftjoin
            (table unit)
            (bgp (triple ?s ab:nick ?first)))
          (bgp (triple ?s ab:firstName ?first)))
        (bgp (triple ?s ab:lastName ?last))))))

, , . , join, , leftjoin, LHS , .

, ab:lastName, ab:nick ab:firstName, , .

ab:nick, ab:firstName, , ab:lastName. .

, SPARQL "".

, ( ), "".

, (, leftjoin minus) OPTIONAL minus

- table unit?

table unit - , SPARQL.

, SELECT * WHERE { } (table unit)

, SPARQL , , . SPARQL table unit, .

join table unit join, table unit ( ) .

OPTIONAL SPARQL , . OPTIONAL ( ), leftjoin table unit . join table unit, leftjoin , LHS , RHS.

:

SELECT *
WHERE
{
  OPTIONAL { ?s a ?type }
}

:

(base <http://example/base/>
  (leftjoin
    (table unit)
    (bgp (triple ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?type))))
+6

All Articles