The Semantics of SPARQL

3,314 views
3,179 views

Published on

I used these slides for an introductory lecture (90min) to a seminar on SPARQL. This slideset introduces the semantics of the RDF query language SPARQL.

Published in: Technology, Design
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,314
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
198
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

The Semantics of SPARQL

  1. 1. SPARQL - Querying the Web of Data Seminar WS 2008/2009 The Semantics of SPARQL Olaf Hartig hartig@informatik.hu-berlin.de
  2. 2. Overview Algebra-based approach to define the SPARQL semantics ● Definition of “correct behavior for evaluation of graph ● patterns and solution modifiers” [SPARQL Spec.] Series of steps: ● Parsing a SPARQL query into an abstract syntax tree ● Converting the abstract syntax tree to an abstract query ● (i.e. an algebra expression) Evaluating the abstract query on an RDF dataset ● Each symbol in the abstract query is associated with an ● algebra operator of the same name. The Semantics of SPARQL 2
  3. 3. Abstract Syntax SPARQL parser creates an abstract syntax tree (AST) ● Extract of the SPARQL grammar (AST symbols): ● WhereClause ::= 'WHERE'? GroupGraphPattern GroupGraphPattern ::= '{' TriplesBlock? ( ( GraphPatternNotTriples | Filter ) '.'? TriplesBlock? )* '}' TriplesBlock ::= TriplesSameSubject ( '.' TriplesBlock? )? GraphPatternNotTriples ::= OptionalGraphPattern | GroupOrUnionGraphPattern | GraphGraphPattern OptionalGraphPattern ::= 'OPTIONAL' GroupGraphPattern GraphGraphPattern ::= 'GRAPH' VarOrIRIref GroupGraphPattern GroupOrUnionGraphPattern ::= GroupGraphPattern ( 'UNION' GroupGraphPattern )* Filter ::= 'FILTER' Constraint The Semantics of SPARQL 3
  4. 4. Converting to an Abstract Query A SPARQL Abstract Query is a tuple (E, DS, R) where: ● E is a SPARQL algebra expression ● DS is an RDF dataset – DS = { G , (<u1>,G1) , ... , (<un>,Gn) } ● R is a query form ● Converting an AST to an algebra expression: ● Step 1: Expand abbreviations for URIs and triple patterns ● Step 2: Recursively translate the query pattern ● Step 3: Translate the solution modifiers ● The Semantics of SPARQL 4
  5. 5. Converting to an Abstract Query Step 1: Expand abbreviations for URIs and triple patterns ● ?v rdf:type umbel-sc:Volcano ; rdfs:label ?name . ?v <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://umbel.org/umbel/sc/Volcano> ; <http://www.w3.org/2000/01/rdf-schema#label> ?name . ?v <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://umbel.org/umbel/sc/Volcano> . ?v <http://www.w3.org/2000/01/rdf-schema#label> ?name . The Semantics of SPARQL 5
  6. 6. Converting to an Abstract Query Step 2: Recursively translate the query pattern ● PROCEDURE Transform ( AST Symbol ) IF the AST Symbol is TriplesBlock THEN RETURN BGP( list of triple patterns ); IF the AST Symbol is GroupOrUnionGraphPattern THEN LET A := undefined; FOR EACH G in the AST Symbol DO IF A is undefined THEN A := Transform( G ); ELSE A := Union( A, Transform(G) ); RETURN A; IF the AST Symbol is GraphGraphPattern THEN ... The Semantics of SPARQL 6
  7. 7. Converting to an Abstract Query Step 2: Recursively translate the query pattern ● { ?x <http://example.org/p1> <http://example.org/A>. ?x <http://example.org/p2> quot;xyquot;. OPTIONAL { ?x <http://example.org/p3> ?y. } } UNION { ?x <http://example.org/p1> <http://example.org/B>. } ... The Semantics of SPARQL 7
  8. 8. Converting to an Abstract Query Step 2: Recursively translate the query pattern ● { ?x <http://example.org/p1> <http://example.org/A>. ?x <http://example.org/p2> quot;xyquot;. OPTIONAL { ?x <http://example.org/p3> ?y. } } UNION { ?x <http://example.org/p1> <http://example.org/B>. } ... The Semantics of SPARQL 8
  9. 9. Converting to an Abstract Query Step 2: Recursively translate the query pattern ● { BGP(?x <http://example.org/p1> <http://example.org/A>. ?x <http://example.org/p2> quot;xyquot;. ) OPTIONAL { ?x <http://example.org/p3> ?y. } } UNION { ?x <http://example.org/p1> <http://example.org/B>. } ... The Semantics of SPARQL 9
  10. 10. Converting to an Abstract Query Step 2: Recursively translate the query pattern ● { BGP(?x <http://example.org/p1> <http://example.org/A>. ?x <http://example.org/p2> quot;xyquot;.) OPTIONAL { BGP(?x <http://example.org/p3> ?y.) } } UNION { BGP(?x <http://example.org/p1> <http://example.org/B>.) } ... The Semantics of SPARQL 10
  11. 11. Converting to an Abstract Query Step 2: Recursively translate the query pattern ● { BGP(?x <http://example.org/p1> <http://example.org/A>. ?x <http://example.org/p2> quot;xyquot;.) OPTIONAL { BGP(?x <http://example.org/p3> ?y.) } } UNION { BGP(?x <http://example.org/p1> <http://example.org/B>.) } ... The Semantics of SPARQL 11
  12. 12. Converting to an Abstract Query Step 2: Recursively translate the query pattern ● { LeftJoin( BGP(?x <http://example.org/p1> <http://example.org/A>. ?x <http://example.org/p2> quot;xyquot;.) , BGP(?x <http://example.org/p3> ?y.) ) } UNION { BGP(?x <http://example.org/p1> <http://example.org/B>.) } ... The Semantics of SPARQL 12
  13. 13. Converting to an Abstract Query Step 2: Recursively translate the query pattern ● { LeftJoin( BGP(?x <http://example.org/p1> <http://example.org/A>. ?x <http://example.org/p2> quot;xyquot;.) , BGP(?x <http://example.org/p3> ?y.) ) } UNION { BGP(?x <http://example.org/p1> <http://example.org/B>.) } ... The Semantics of SPARQL 13
  14. 14. Converting to an Abstract Query Step 2: Recursively translate the query pattern ● Union( LeftJoin( BGP(?x <http://example.org/p1> <http://example.org/A>. ?x <http://example.org/p2> quot;xyquot;.) , BGP(?x <http://example.org/p3> ?y.) ), BGP(?x <http://example.org/p1> <http://example.org/B>.) ) ... The Semantics of SPARQL 14
  15. 15. Converting to an Abstract Query Step 2: Recursively translate the query pattern ● Union LeftJoin BGP(?x <http://example.org/p3> ?y.) BGP(?x <http://example.org/p1> <http://example.org/A>. ?x <http://example.org/p2> quot;xyquot;.) BGP(?x <http://example.org/p1> <http://example.org/B>.) The Semantics of SPARQL 15
  16. 16. Converting to an Abstract Query Step 3: Translate the solution modifiers ● 1. M := ToList( algebra expression of the query pattern ) 2. IF query has ORDER BY THEN M := OrderBy( M, order comparators ) 3. M := Project( M, vars ) where vars is the set of variables in SELECT 4. IF query has DISTINCT THEN M := Distinct( M ) ... Finally, M becomes the algebra expression E of the query ● The Semantics of SPARQL 16
  17. 17. Evaluation eval( DG , a ) defines the evaluation of ● an algebra expression a ● on an RDF dataset D ● with the active graph G ● eval( DG, BGP(bgp) ) = multiset of solutions of bgp eval( DG, Union(a1 , a2) ) = Union( eval(DG , a1) , eval(DG , a2) ) eval( DG, Filter(e,a) ) = Filter( e , eval(DG , a) ) eval( DG, Graph(uri,a) ) = eval( DD(uri) , a ) ... eval( D, ToList(a) ) = eval( DD(dflt) , a ) ... The Semantics of SPARQL 17
  18. 18. Evaluation of BGPs Query SELECT ?name WHERE { _:x rdf:type umbel-sc:Volcano ; rdfs:label ?name . } Abstract Query BGP( _:x <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://umbel.org/umbel/sc/Volcano> . _:x <http://www.w3.org/2000/01/rdf-schema#label> ?name . ) ?name umbel-sc:Volcano rdfs:label rdf:type The Semantics of SPARQL 18
  19. 19. Evaluation of BGPs Data dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label quot;Etnaquot; . dbpedia:Mount_Baker rdf:type umbel-sc:Volcano . dbpedia:Beerenberg rdf:type umbel-sc:Volcano ; rdfs:label quot;Beerenbergquot;@en . ?name umbel-sc:Volcano rdfs:label rdf:type The Semantics of SPARQL 19
  20. 20. Evaluation of BGPs dbpedia:Mount_Etna dbpedia:Beerenberg rdf:type rdf:type rdfs:label umbel-sc:Volcano rdfs:label quot;Etnaquot; rdf:type quot;Beerenbergquot;@en dbpedia:Mount_Baker ?name ?name umbel-sc:Volcano rdfs:label rdf:type The Semantics of SPARQL 20
  21. 21. Evaluation of BGPs dbpedia:Mount_Etna dbpedia:Beerenberg rdf:type rdf:type rdfs:label umbel-sc:Volcano rdfs:label quot;Etnaquot; rdf:type quot;Beerenbergquot;@en dbpedia:Mount_Baker ?name quot;Etnaquot; ?name umbel-sc:Volcano rdfs:label rdf:type The Semantics of SPARQL 21
  22. 22. Evaluation of BGPs dbpedia:Mount_Etna dbpedia:Beerenberg rdf:type rdf:type rdfs:label umbel-sc:Volcano rdfs:label quot;Etnaquot; rdf:type quot;Beerenbergquot;@en dbpedia:Mount_Baker ? No matching ?name quot;Etnaquot; subgraph ! ?name umbel-sc:Volcano rdfs:label rdf:type The Semantics of SPARQL 22
  23. 23. Evaluation of BGPs dbpedia:Mount_Etna dbpedia:Beerenberg rdf:type rdf:type rdfs:label umbel-sc:Volcano rdfs:label quot;Etnaquot; rdf:type quot;Beerenbergquot;@en dbpedia:Mount_Baker ?name quot;Etnaquot; quot;Beerenbergquot;@en ?name umbel-sc:Volcano rdfs:label rdf:type The Semantics of SPARQL 23
  24. 24. Basic Graph Pattern Matching Definition: A solution mapping, µ, is a partial function µ: V  T. Definition: A pattern instance mapping, P, is the combination of an RDF instance mapping, σ, and solution mapping, μ. P(x) = μ(σ(x)) Definition: Let bgp be a basic graph pattern and let G be an RDF graph. μ is a solution for bgp from G when there is a pattern instance mapping P such that P(bgp) is a subgraph of G and μ is the restriction of P to the query variables in bgp. The Semantics of SPARQL 24
  25. 25. Evaluation of BGPs dbpedia:Mount_Etna dbpedia:Beerenberg rdf:type rdf:type rdfs:label umbel-sc:Volcano rdfs:label quot;Etnaquot; rdf:type quot;Beerenbergquot;@en dbpedia:Mount_Baker σ = { _:x → dbpedia:Mount_Etna } μ = { ?name → quot;Etnaquot; } ?name umbel-sc:Volcano rdfs:label rdf:type The Semantics of SPARQL 25
  26. 26. Evaluation of BGPs Evaluation of a BGP yields a multiset Ω of solution ● mappings μ where each μ is a solution for the BGP. card[Ω](μ) denotes the cardinality of each μ in Ω ● Definition: Let bgp be a basic graph pattern and let G be an RDF graph. card[Ω](μ) is the number of distinct RDF instance map- pings, σ, such that P(x) = μ(σ(x)) is a pattern instance mapping and ● P(bgp) is a subgraph of G. ● SPARQL algebra operators operate on multisets Ω (or ● sequences) of solution mappings The Semantics of SPARQL 26
  27. 27. Algebra Operators Union( Ω1 , Ω2 ) = { μ | μ is in Ω1 or μ is in Ω2 } card[ Union( Ω1 , Ω2 ) ](μ) = card[Ω1](μ) + card[Ω2](μ) ● ?v ?name ?le dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Beerenberg quot;Beerenbergquot;@en dbpedia:Beerenberg quot;Бееренбергquot;@ru dbpedia:Beerenberg quot;Beerenbergquot;@en dbpedia:Mount_Baker 1880 Union ?v ?name ?v ?le dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Mount_Baker 1880 dbpedia:Beerenberg quot;Beerenbergquot;@en dbpedia:Beerenberg quot;Бееренбергquot;@ru dbpedia:Beerenberg quot;Beerenbergquot;@en The Semantics of SPARQL 27
  28. 28. Algebra Operators Filter( e , Ω ) = { μ | μ is in Ω and e(μ) evaluates to true } ● card[ Filter( e , Ω ) ](μ) = card[Ω](μ) ?v ?name dbpedia:Beerenberg quot;Beerenbergquot;@en dbpedia:Beerenberg quot;Beerenbergquot;@en Filter STR(?name) = quot;Beerenbergquot; ?v ?name dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Beerenberg quot;Beerenbergquot;@en dbpedia:Beerenberg quot;Бееренбергquot;@ru dbpedia:Beerenberg quot;Beerenbergquot;@en The Semantics of SPARQL 28
  29. 29. Algebra Operators Join( Ω1 , Ω2 ) = { merge(μ1 , μ2) | μ1 is in Ω1 and μ2 is in Ω2 and μ1 and μ2 are compatible } Definition: Two solution mappings μ1 , μ2 are compatible if μ1(v) = μ2(v) holds for every variable v in dom(μ1) and v in dom(μ2). Definition: Let μ1 and μ2 compatible solution mappings. merge(μ1 , μ2) = μ1 U μ2 Example: μ1 = { ?x → ex:A , ?y → quot;abcquot; } μ2 = { ?x → ex:A , ?z → ex:B } merge(μ1 , μ2) = { ?x → ex:A , ?y → quot;abcquot; , ?z → ex:B } The Semantics of SPARQL 29
  30. 30. Algebra Operators Join( Ω1 , Ω2 ) = { merge(μ1 , μ2) | μ1 is in Ω1 and μ2 is in Ω2 and μ1 and μ2 are compatible } Σ card[ Join( Ω1 , Ω2 ) ](μ) = card[Ω1](μ1) · card[Ω2](μ2) ● (μ1 , μ2) where μ1 is in Ω1 and μ2 is in Ω2 and μ1 and μ2 are compatible The Semantics of SPARQL 30
  31. 31. Algebra Operators Join( Ω1 , Ω2 ) = { merge(μ1 , μ2) | μ1 is in Ω1 and μ2 is in Ω2 and μ1 and μ2 are compatible } Σ card[ Join( Ω1 , Ω2 ) ](μ) = card[Ω1](μ1) · card[Ω2](μ2) ● ?v ?name ?le dbpedia:Beerenberg quot;Beerenbergquot;@en 1985 Join ?v ?name dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Beerenberg quot;Beerenbergquot;@en dbpedia:Beerenberg quot;Бееренбергquot;@ru ?v ?name ?le dbpedia:Beerenberg quot;Beerenbergquot;@en 1985 dbpedia:Mount_Baker 1880 The Semantics of SPARQL 31
  32. 32. Algebra Operators Diff( Ω1 , Ω2 , e ) = { μ | μ is in Ω1 such that for all μ' in Ω2 either μ and μ' are not compatible or e(merge(μ,μ')) evaluates to false } ● card[ Diff( Ω , Ω , e ) ](μ) = card[Ω ](μ) 1 2 1 ?v ?name dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Beerenberg quot;Бееренбергquot;@ru Diff STR(?name) = quot;Beerenbergquot; ?v ?name ?v ?le dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Mount_Baker 1880 dbpedia:Beerenberg quot;Beerenbergquot;@en dbpedia:Beerenberg 1985 dbpedia:Beerenberg quot;Бееренбергquot;@ru The Semantics of SPARQL 32
  33. 33. Algebra Operators LeftJoin( Ω1 , Ω2 , e ) = Filter( e, Join(Ω1 , Ω2) ) U Diff(Ω1,Ω2,e) card[ LeftJoin(Ω1,Ω2,e) ](μ) = card[Filter( e, Join(Ω1,Ω2) )](μ) ● + card[Diff(Ω1,Ω2,e)](μ) ?v ?name ?le dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Beerenberg quot;Бееренбергquot;@ru 1985 dbpedia:Beerenberg quot;Beerenbergquot;@en 1985 LeftJoin ?v ?name ?v ?le dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Mount_Baker 1880 dbpedia:Beerenberg quot;Beerenbergquot;@en dbpedia:Beerenberg 1985 dbpedia:Beerenberg quot;Бееренбергquot;@ru The Semantics of SPARQL 33
  34. 34. Algebra Operators ToList( Ω ) = Ψ where Ψ is a sequence of solution mappings μ in Ω ● card[ ToList( Ω ) ](μ) = card[Ω](μ) ?v ?name ?le dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Beerenberg quot;Бееренбергquot;@ru 1985 dbpedia:Beerenberg quot;Beerenbergquot;@en 1985 dbpedia:Mount_Baker 1880 ToList ?v ?name ?le dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Beerenberg quot;Бееренбергquot;@ru 1985 dbpedia:Beerenberg quot;Beerenbergquot;@en 1985 dbpedia:Mount_Baker 1880 The Semantics of SPARQL 34
  35. 35. Algebra Operators OrderBy( Ψ , c ) = [ μ | μ in Ψ and the sequence satisfies c ] where c is an ordering condiction ● card[ OrderBy(Ψ,c) ](μ) = card[Ψ](μ) ?v ?name ?le dbpedia:Beerenberg quot;Бееренбергquot;@ru 1985 dbpedia:Beerenberg quot;Beerenbergquot;@en 1985 dbpedia:Mount_Baker 1880 dbpedia:Mount_Etna quot;Etnaquot; OrderBy DESC(?le) ?v ?name ?le dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Beerenberg quot;Бееренбергquot;@ru 1985 dbpedia:Beerenberg quot;Beerenbergquot;@en 1985 dbpedia:Mount_Baker 1880 The Semantics of SPARQL 35
  36. 36. Algebra Operators Further operators: ● Project( Ψ , PVars ) ● Distinct( Ψ ) ● Reduced( Ψ ) ● Slice( Ψ , start, length ) ● The Semantics of SPARQL 36
  37. 37. Summary Parsing a SPARQL query into an abstract syntax tree (AST) ● e.g.: GroupOrUnionGraphPattern ::= GroupGraphPattern ( 'UNION' GroupGraphPattern )* Converting the AST to a SPARQL Abstract Query (with an ● algebra expression) e.g.: GroupOrUnionGraphPattern → Union(a1 , a2) Evaluating the abstract query on an RDF dataset ● Each symbol in the abstract query is associated with an ● algebra operator of the same name. e.g.: Union( a1 , a2 ) → Union( Ω1 , Ω2 ) Evaluation semantics defined by the definition of solu- ● tions for BGPs, the algebra operators, and eval( DG , a ) The Semantics of SPARQL 37
  38. 38. Example Data dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label quot;Etnaquot; . dbpedia:Mount_Baker rdf:type umbel-sc:Volcano . dbpedia:Beerenberg rdf:type umbel-sc:Volcano ; rdfs:label quot;Beerenbergquot;@en ; rdfs:label quot;Бееренбергquot;@ru . Question: What volcanos are not called “Beerenberg”? ● ?v Query SELECT ?v WHERE { dbpedia:Mount_Etna ?v rdf:type umbel-sc:Volcano . dbpedia:Mount_Baker OPTIONAL { ?v rdfs:label ?name . FILTER (STR(?name) = quot;Beerenbergquot;) } FILTER ( ! BOUND(?name) ) Negation as Failure } The Semantics of SPARQL 38
  39. 39. Example Query SELECT ?v WHERE { ?v rdf:type umbel-sc:Volcano . OPTIONAL { ?v rdfs:label ?name . FILTER (STR(?name) = quot;Beerenbergquot;) } FILTER ( ! BOUND(?name) ) } The Semantics of SPARQL 39
  40. 40. Example Query SELECT ?v WHERE { Filter ?v rdf:type umbel-sc:Volcano . ! BOUND(?name) OPTIONAL { ?v rdfs:label ?name . FILTER (STR(?name) = quot;Beerenbergquot;) } LeftJoin FILTER ( ! BOUND(?name) ) } Filter STR(?name) = quot;Beerenbergquot; BGP( ?v rdf:type umbel-sc:Volcano ) BGP( ?v rdfs:label ?name ) The Semantics of SPARQL 40
  41. 41. Example Data dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ; rdfs:label quot;Etnaquot; . Filter ! BOUND(?name) dbpedia:Mount_Baker rdf:type umbel-sc:Volcano . dbpedia:Beerenberg rdf:type umbel-sc:Volcano ; LeftJoin rdfs:label quot;Beerenbergquot;@en ; rdfs:label quot;Бееренбергquot;@ru . ?v dbpedia:Mount_Etna Filter dbpedia:Beerenberg STR(?name) = quot;Beerenbergquot; dbpedia:Mount_Baker ?v ?name BGP( ?v rdf:type umbel-sc:Volcano ) dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Beerenberg quot;Beerenbergquot;@en dbpedia:Beerenberg quot;Бееренбергquot;@ru BGP( ?v rdfs:label ?name ) The Semantics of SPARQL 41
  42. 42. ?v ?name Example dbpedia:Mount_Etna dbpedia:Mount_Baker ?v ?name dbpedia:Mount_Etna Filter dbpedia:Beerenberg quot;Beerenbergquot;@en ! BOUND(?name) dbpedia:Mount_Baker LeftJoin ?v ?name dbpedia:Beerenberg quot;Beerenbergquot;@en ?v dbpedia:Mount_Etna Filter dbpedia:Beerenberg STR(?name) = quot;Beerenbergquot; dbpedia:Mount_Baker ?v ?name BGP( ?v rdf:type umbel-sc:Volcano ) dbpedia:Mount_Etna quot;Etnaquot; dbpedia:Beerenberg quot;Beerenbergquot;@en dbpedia:Beerenberg quot;Бееренбергquot;@ru BGP( ?v rdfs:label ?name ) The Semantics of SPARQL 42
  43. 43. Further Reading SPARQL Query Language for RDF, W3C Recommendation ● http://www.w3.org/TR/rdf-sparql-query/ SPARQL interface for dbpedia: http://dbpedia.org/snorql/ ● The Semantics of SPARQL 43

×