Top Banner
SPARQL - Querying the Web of Data Seminar WS 2008/2009 The Semantics of SPARQL Olaf Hartig [email protected]
43

The Semantics of SPARQL

May 11, 2015

Download

Technology

Olaf Hartig

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.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: The Semantics of SPARQL

SPARQL - Querying the Web of Data

Seminar WS 2008/2009

The Semantics of SPARQL

Olaf [email protected]

Page 2: The Semantics of SPARQL

The Semantics of SPARQL 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.

Page 3: The Semantics of SPARQL

The Semantics of SPARQL 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

Page 4: The Semantics of SPARQL

The Semantics of SPARQL 4

Converting to an Abstract Query

● 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

● 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>,G

1) , ... , (<u

n>,G

n) }

● R is a query form

Page 5: The Semantics of SPARQL

The Semantics of SPARQL 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 .

Page 6: The Semantics of SPARQL

The Semantics of SPARQL 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 THENA := Transform( G );

ELSEA := Union( A, Transform(G) );

RETURN A;IF the AST Symbol is GraphGraphPattern THEN

...

Page 7: The Semantics of SPARQL

The Semantics of SPARQL 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> "xy".

OPTIONAL { ?x <http://example.org/p3> ?y. }

}

UNION

{

?x <http://example.org/p1> <http://example.org/B>.

}

Page 8: The Semantics of SPARQL

The Semantics of SPARQL 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> "xy".

OPTIONAL { ?x <http://example.org/p3> ?y. }

}

UNION

{

?x <http://example.org/p1> <http://example.org/B>.

}

Page 9: The Semantics of SPARQL

The Semantics of SPARQL 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> "xy". )

OPTIONAL { ?x <http://example.org/p3> ?y. }

}

UNION

{

?x <http://example.org/p1> <http://example.org/B>.

}

Page 10: The Semantics of SPARQL

The Semantics of SPARQL 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> "xy".)

OPTIONAL { BGP(?x <http://example.org/p3> ?y.) }

}

UNION

{

BGP(?x <http://example.org/p1> <http://example.org/B>.)

}

Page 11: The Semantics of SPARQL

The Semantics of SPARQL 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> "xy".)

OPTIONAL { BGP(?x <http://example.org/p3> ?y.) }

}

UNION

{

BGP(?x <http://example.org/p1> <http://example.org/B>.)

}

Page 12: The Semantics of SPARQL

The Semantics of SPARQL 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> "xy".) ,

BGP(?x <http://example.org/p3> ?y.)

)

}

UNION

{

BGP(?x <http://example.org/p1> <http://example.org/B>.)

}

Page 13: The Semantics of SPARQL

The Semantics of SPARQL 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> "xy".) ,

BGP(?x <http://example.org/p3> ?y.)

)

}

UNION

{

BGP(?x <http://example.org/p1> <http://example.org/B>.)

}

Page 14: The Semantics of SPARQL

The Semantics of SPARQL 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> "xy".) ,

BGP(?x <http://example.org/p3> ?y.)

) ,

BGP(?x <http://example.org/p1> <http://example.org/B>.)

)

Page 15: The Semantics of SPARQL

The Semantics of SPARQL 15

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> "xy".)

BGP(?x <http://example.org/p3> ?y.)

LeftJoin

BGP(?x <http://example.org/p1> <http://example.org/B>.)

Union

Page 16: The Semantics of SPARQL

The Semantics of SPARQL 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 THENM := OrderBy( M, order comparators )

3. M := Project( M, vars )where vars is the set of variables in SELECT

4. IF query has DISTINCT THENM := Distinct( M )

...

● Finally, M becomes the algebra expression E of the query

Page 17: The Semantics of SPARQL

The Semantics of SPARQL 17

Evaluation

eval( DG, BGP(bgp) ) = multiset of solutions of bgp

eval( DG, Union(a

1 , a

2) ) = Union( eval(D

G , a

1) , eval(D

G , a

2) )

eval( DG, Filter(e,a) ) = Filter( e , eval(D

G , a) )

eval( DG, Graph(uri,a) ) = eval( D

D(uri) , a )

...

eval( D, ToList(a) ) = eval( DD(dflt)

, a )

...

● eval( DG

, a ) defines the evaluation of

● an algebra expression a● on an RDF dataset D● with the active graph G

Page 18: The Semantics of SPARQL

The Semantics of SPARQL 18

Evaluation of BGPs

SELECT ?name WHERE {

_:x rdf:type umbel-sc:Volcano ;

rdfs:label ?name .

}

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 .

)

Abstract Query

umbel-sc:Volcanordf:type

?name rdfs:label

Page 19: The Semantics of SPARQL

The Semantics of SPARQL 19

Evaluation of BGPs

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ;

rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano .

dbpedia:Beerenberg rdf:type umbel-sc:Volcano ;

rdfs:label "Beerenberg"@en .

Data

umbel-sc:Volcanordf:type

?name rdfs:label

Page 20: The Semantics of SPARQL

The Semantics of SPARQL 20

Evaluation of BGPs

umbel-sc:Volcanordf:type

?name rdfs:label

dbpedia:Mount_Baker

umbel-sc:Volcano

rdf:type"Etna"

rdfs:label

dbpedia:Mount_Etna rdf:type

"Beerenberg"@en

dbpedia:Beerenberg

rdfs:label

rdf:type

?name

Page 21: The Semantics of SPARQL

The Semantics of SPARQL 21

Evaluation of BGPs

umbel-sc:Volcanordf:type

?name rdfs:label

dbpedia:Mount_Baker

umbel-sc:Volcano

rdf:type"Etna"

rdfs:label

dbpedia:Mount_Etna rdf:type

"Beerenberg"@en

dbpedia:Beerenberg

rdfs:label

rdf:type

?name

"Etna"

Page 22: The Semantics of SPARQL

The Semantics of SPARQL 22

Evaluation of BGPs

umbel-sc:Volcanordf:type

?name rdfs:label

dbpedia:Mount_Baker

umbel-sc:Volcano

rdf:type"Etna"

rdfs:label

dbpedia:Mount_Etna rdf:type

"Beerenberg"@en

dbpedia:Beerenberg

rdfs:label

rdf:type

?name

"Etna"

?No matchingsubgraph !

Page 23: The Semantics of SPARQL

The Semantics of SPARQL 23

Evaluation of BGPs

umbel-sc:Volcanordf:type

?name rdfs:label

dbpedia:Mount_Baker

umbel-sc:Volcano

rdf:type"Etna"

rdfs:label

dbpedia:Mount_Etna rdf:type

"Beerenberg"@en

dbpedia:Beerenberg

rdfs:label

rdf:type

?name

"Etna""Beerenberg"@en

Page 24: The Semantics of SPARQL

The Semantics of SPARQL 24

Basic Graph Pattern Matching

Definition: A solution mapping, µ, is a partialfunction

µ: 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.

Page 25: The Semantics of SPARQL

The Semantics of SPARQL 25

Evaluation of BGPs

umbel-sc:Volcanordf:type

?name rdfs:label

dbpedia:Mount_Baker

umbel-sc:Volcano

rdf:type"Etna"

rdfs:label

dbpedia:Mount_Etna rdf:type

"Beerenberg"@en

dbpedia:Beerenberg

rdfs:label

rdf:type

μ = { ?name → "Etna" }σ = { _:x → dbpedia:Mount_Etna }

Page 26: The Semantics of SPARQL

The Semantics of SPARQL 26

Evaluation of BGPs

● SPARQL algebra operators operate on multisets Ω (or sequences) of solution mappings

● 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.

Page 27: The Semantics of SPARQL

The Semantics of SPARQL 27

Algebra Operators

Union( Ω1 , Ω

2 ) = { μ | μ is in Ω

1 or μ is in Ω

2 }

● card[ Union( Ω1 , Ω

2 ) ](μ) = card[Ω

1](μ) + card[Ω

2](μ)

Union

dbpedia:Mount_Etna "Etna"dbpedia:Beerenberg "Beerenberg"@endbpedia:Beerenberg "Бееренберг"@rudbpedia:Beerenberg "Beerenberg"@en

?v ?namedbpedia:Mount_Baker 1880

?v ?le

dbpedia:Mount_Etna "Etna"dbpedia:Beerenberg "Beerenberg"@endbpedia:Beerenberg "Бееренберг"@rudbpedia:Beerenberg "Beerenberg"@endbpedia:Mount_Baker 1880

?v ?name ?le

Page 28: The Semantics of SPARQL

The Semantics of SPARQL 28

Algebra Operators

Filter( e , Ω ) = { μ | μ is in Ω and e(μ) evaluates to true }● card[ Filter( e , Ω ) ](μ) = card[Ω](μ)

FilterSTR(?name) = "Beerenberg"

dbpedia:Mount_Etna "Etna"dbpedia:Beerenberg "Beerenberg"@endbpedia:Beerenberg "Бееренберг"@rudbpedia:Beerenberg "Beerenberg"@en

?v ?name

dbpedia:Beerenberg "Beerenberg"@endbpedia:Beerenberg "Beerenberg"@en

?v ?name

Page 29: The Semantics of SPARQL

The Semantics of SPARQL 29

Algebra Operators

Definition: Let μ1 and μ

2 compatible solution mappings.

merge(μ1 , μ

2) = μ

1 U μ

2

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).

Example: μ1 = { ?x → ex:A , ?y → "abc" }

μ2 = { ?x → ex:A , ?z → ex:B }

merge(μ1 , μ

2) = { ?x → ex:A , ?y → "abc" , ?z → ex:B }

Page 30: The Semantics of SPARQL

The Semantics of SPARQL 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

Page 31: The Semantics of SPARQL

The Semantics of SPARQL 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)

Join

dbpedia:Beerenberg "Beerenberg"@en 1985dbpedia:Mount_Baker 1880

?v ?name ?le

dbpedia:Beerenberg "Beerenberg"@en 1985

?v ?name ?le

dbpedia:Mount_Etna "Etna"dbpedia:Beerenberg "Beerenberg"@endbpedia:Beerenberg "Бееренберг"@ru

?v ?name

Σ

Page 32: The Semantics of SPARQL

The Semantics of SPARQL 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( Ω1 , Ω

2 , e ) ](μ) = card[Ω

1](μ)

DiffSTR(?name) = "Beerenberg"

dbpedia:Mount_Etna "Etna"dbpedia:Beerenberg "Beerenberg"@endbpedia:Beerenberg "Бееренберг"@ru

?v ?namedbpedia:Mount_Baker 1880dbpedia:Beerenberg 1985

?v ?le

dbpedia:Mount_Etna "Etna"dbpedia:Beerenberg "Бееренберг"@ru

?v ?name

Page 33: The Semantics of SPARQL

The Semantics of SPARQL 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)](μ)

LeftJoin

dbpedia:Mount_Etna "Etna"dbpedia:Beerenberg "Beerenberg"@endbpedia:Beerenberg "Бееренберг"@ru

?v ?name

dbpedia:Mount_Etna "Etna"dbpedia:Beerenberg "Бееренберг"@ru 1985dbpedia:Beerenberg "Beerenberg"@en 1985

?v ?name ?le

dbpedia:Mount_Baker 1880dbpedia:Beerenberg 1985

?v ?le

Page 34: The Semantics of SPARQL

The Semantics of SPARQL 34

Algebra Operators

ToList( Ω ) = Ψ where Ψ is a sequence of solution mappings μ in Ω

● card[ ToList( Ω ) ](μ) = card[Ω](μ)

ToList

dbpedia:Mount_Etna "Etna"dbpedia:Beerenberg "Бееренберг"@ru 1985dbpedia:Beerenberg "Beerenberg"@en 1985dbpedia:Mount_Baker 1880

?v ?name ?le

dbpedia:Mount_Etna "Etna"dbpedia:Beerenberg "Бееренберг"@ru 1985dbpedia:Beerenberg "Beerenberg"@en 1985dbpedia:Mount_Baker 1880

?v ?name ?le

Page 35: The Semantics of SPARQL

The Semantics of SPARQL 35

Algebra Operators

OrderBy( Ψ , c ) = [ μ | μ in Ψ and the sequence satisfies c ]where c is an ordering condiction

● card[ OrderBy(Ψ,c) ](μ) = card[Ψ](μ)

OrderByDESC(?le)

dbpedia:Beerenberg "Бееренберг"@ru 1985dbpedia:Beerenberg "Beerenberg"@en 1985dbpedia:Mount_Baker 1880dbpedia:Mount_Etna "Etna"

?v ?name ?le

dbpedia:Mount_Etna "Etna"dbpedia:Beerenberg "Бееренберг"@ru 1985dbpedia:Beerenberg "Beerenberg"@en 1985dbpedia:Mount_Baker 1880

?v ?name ?le

Page 36: The Semantics of SPARQL

The Semantics of SPARQL 36

Algebra Operators

● Further operators:● Project( Ψ , PVars )● Distinct( Ψ )● Reduced( Ψ )● Slice( Ψ , start, length )

Page 37: The Semantics of SPARQL

The Semantics of SPARQL 37

Summary

● Parsing a SPARQL query into an abstract syntax tree (AST)

● Converting the AST to a SPARQL Abstract Query (with 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.

● Evaluation semantics defined by the definition of solu-tions for BGPs, the algebra operators, and eval( D

G , a )

e.g.: GroupOrUnionGraphPattern ::= GroupGraphPattern ( 'UNION' GroupGraphPattern )*

e.g.: GroupOrUnionGraphPattern → Union(a1 , a

2)

e.g.: Union( a1 , a

2 ) → Union( Ω

1 , Ω

2 )

Page 38: The Semantics of SPARQL

The Semantics of SPARQL 38

Example

SELECT ?v WHERE {

?v rdf:type umbel-sc:Volcano .

OPTIONAL { ?v rdfs:label ?name .

FILTER (STR(?name) = "Beerenberg") }

FILTER ( ! BOUND(?name) )

}

Query?v

dbpedia:Mount_Etnadbpedia:Mount_Baker

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ;

rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano .

dbpedia:Beerenberg rdf:type umbel-sc:Volcano ;

rdfs:label "Beerenberg"@en ;

rdfs:label " "@ru .Бееренберг

Data

● Question: What volcanos are not called “Beerenberg”?

Negation as Failure

Page 39: The Semantics of SPARQL

The Semantics of SPARQL 39

Example

SELECT ?v WHERE {

?v rdf:type umbel-sc:Volcano .

OPTIONAL { ?v rdfs:label ?name .

FILTER (STR(?name) = "Beerenberg") }

FILTER ( ! BOUND(?name) )

}

Query

Page 40: The Semantics of SPARQL

The Semantics of SPARQL 40

SELECT ?v WHERE {

?v rdf:type umbel-sc:Volcano .

OPTIONAL { ?v rdfs:label ?name .

FILTER (STR(?name) = "Beerenberg") }

FILTER ( ! BOUND(?name) )

}

Query

Example

BGP( ?v rdf:type umbel-sc:Volcano )

BGP( ?v rdfs:label ?name )

LeftJoin

Filter! BOUND(?name)

FilterSTR(?name) = "Beerenberg"

Page 41: The Semantics of SPARQL

The Semantics of SPARQL 41

Example

BGP( ?v rdf:type umbel-sc:Volcano )

BGP( ?v rdfs:label ?name )

LeftJoin

Filter! BOUND(?name)

dbpedia:Mount_Etna "Etna"dbpedia:Beerenberg "Beerenberg"@endbpedia:Beerenberg "Бееренберг"@ru

?v ?name

FilterSTR(?name) = "Beerenberg"

dbpedia:Mount_Etna rdf:type umbel-sc:Volcano ;

rdfs:label "Etna" .

dbpedia:Mount_Baker rdf:type umbel-sc:Volcano .

dbpedia:Beerenberg rdf:type umbel-sc:Volcano ;

rdfs:label "Beerenberg"@en ;

rdfs:label " "@ru .Бееренберг

Data

dbpedia:Mount_Etnadbpedia:Beerenbergdbpedia:Mount_Baker

?v

Page 42: The Semantics of SPARQL

The Semantics of SPARQL 42

Example

BGP( ?v rdf:type umbel-sc:Volcano )

BGP( ?v rdfs:label ?name )

LeftJoin

Filter! BOUND(?name)

dbpedia:Mount_Etnadbpedia:Beerenbergdbpedia:Mount_Baker

?v

dbpedia:Mount_Etna "Etna"dbpedia:Beerenberg "Beerenberg"@endbpedia:Beerenberg "Бееренберг"@ru

?v ?name

dbpedia:Mount_Etnadbpedia:Beerenberg "Beerenberg"@endbpedia:Mount_Baker

?v ?name

FilterSTR(?name) = "Beerenberg"

dbpedia:Beerenberg "Beerenberg"@en

?v ?name

dbpedia:Mount_Etnadbpedia:Mount_Baker

?v ?name

Page 43: The Semantics of SPARQL

The Semantics of SPARQL 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/