Multiset semantics in SPARQL, Relational Algebra and Datalog∗
arXiv:2605.00417v1 [cs.DB] 1 May 2026
RENZO ANGLES, Department of Computer Science, Faculty of Engineering, Universidad de Talca, Chile and Millenium Institute for Foundational Research on Data (IMFD), Chile CLAUDIO GUTIERREZ, Department of Computer Science, Universidad de Chile, Chile and Millenium Institute for Foundational Research on Data (IMFD), Chile DANIEL HERNÁNDEZ† , Institute for Artificial Intelligence, University of Stuttgart, Germany The paper analyzes and characterizes the algebraic and logical structure of the multiset semantics for SPARQL patterns involving AND, UNION, FILTER, EXCEPT, and SELECT. To do this, we align SPARQL with two well-established query languages: Datalog and Relational Algebra. Specifically, we study (i) a version of non-recursive Datalog with safe negation extended to support multisets, and (ii) a multiset relational algebra comprising projection, selection, natural join, arithmetic union, and except. We prove that these three formalisms are expressively equivalent under multiset semantics. Additional Key Words and Phrases: Query Languages, Multisets, Bags, SPARQL, Datalog, Relational Algebra
1
Introduction
Informally speaking, multisets are sets in which each element could occur multiple times, that is, the number of “copies” of each element matters. In the field of databases, the notion of multisets (also called “duplicates” or “bags”)1 has been studied in several contexts, including programming languages [10, 34], bag languages [3, 13, 20, 21, 32, 33], relational algebra [14, 17, 29], Datalog [2, 9, 12, 36, 37], SQL [22, 42], SPARQL [6, 18, 27, 40] and data integration [24]. The incorporation of multisets in query languages is essentially due to practical concerns: duplicate elimination is expensive, and duplicates might be required for some applications, e.g., for aggregation. Although this design decision may be debatable (e.g., see [16]), today multisets are an established reality in database systems [19, 31]. The classical theory behind declarative query languages includes formalisms (relational algebra or relational calculus) that for sets have a clear and intuitive semantics for users, developers and theoreticians [1]. The same cannot be said for their extensions to multisets, whose theory is complex (particularly the containment of queries), and their practical use not always clear [19]. Worst, there exist several possible ways of extending set relational operators to multisets, which makes the study and design of multiset semantics for query languages challenging. To illustrate the variety of possible semantics, we will show the different extensions to multisets of set operators found in the literature. Consider the following multiset relations: 𝑅(𝑊 , 𝑋 ) = ⦃(𝑎, 𝑏), (𝑎, 𝑏), (𝑎, 𝑑)⦄, 𝑆 (𝑊 , 𝑋 ) = ⦃(𝑎, 𝑏)⦄ and 𝑇 (𝑌 , 𝑍 ) = ⦃(𝑏, 𝑐), (𝑏, 𝑐)⦄. For the first relation, 𝑅 is the name of the relation, 𝑊 and 𝑋 are the attributes that conform the schema of 𝑅, 𝑅 contains three tuples, and the tuple (𝑎, 𝑏) is duplicated (i.e., its cardinality is 2). A similar description can be ∗ This is the authors’ preprint of: R. Angles, C. Gutierrez, and D. Hernández. Multiset semantics in SPARQL, Relational Algebra and Datalog. Semantic Web, 2026. DOI: 10.1177/22104968261439426. † Corresponding author. 1 There seems to be no agreement on the best terminology [35, p. 27]. In this paper, we will use the word “multiset”.
Authors’ Contact Information: Renzo Angles, Department of Computer Science, Faculty of Engineering, Universidad de Talca, Chile and Millenium Institute for Foundational Research on Data (IMFD), Chile, [email protected]; Claudio Gutierrez, Department of Computer Science, Universidad de Chile, Chile and Millenium Institute for Foundational Research on Data (IMFD), Chile, [email protected]; Daniel Hernández, Institute for Artificial Intelligence, University of Stuttgart, Germany, [email protected].
2
Angles et al.
Table 1. Possible ways of extending set operators with multiset semantics in SQL and SPARQL. The table shows several extended relational algebra operations for multisets currently present (or possible to implement) in SQL and SPARQL. Let 𝑅, 𝑆 and 𝑇 be multiset relations satisfying that 𝑅 and 𝑆 have the same attributes, and 𝑇 does not have attributes in common with 𝑅. The cardinality of an element 𝑥 in a relation 𝑅 is represented as 𝑅(𝑥). Note that SPARQL works with multisets of bindings, whose corresponding schema is a set of variables. Operation
Operator
SQL
SPARQL
𝜎𝜑 (𝑅)
Cardinality for 𝑥 𝑅(𝑥) if 𝑥 satisfies 𝜑, 0 otherwise.
Selection
SELECT * FROM 𝑅 WHERE 𝜑
𝑅 FILTER (𝜑)
Cartesian product
𝑅 ×𝑇
𝑅(𝑥) × 𝑇 (𝑥)
𝑅 CROSS JOIN 𝑇
𝑅 AND 𝑇
Join
𝑅 Z𝜑 𝑇
𝑅(𝑥) × 𝑇 (𝑥)
(𝑅 CROSS JOIN 𝑇 ) WHERE 𝜑
(𝑅 AND 𝑇 ) FILTER (𝜑)
Max-union
𝑅 ⊔𝑆
max(𝑅(𝑥), 𝑆 (𝑥))
(𝑅 UNION ALL 𝑆) EXCEPT ALL (𝑅 INTERSECT ALL 𝑆)
–
Arithmetic union
𝑅 ∪𝑆
𝑅(𝑥) + 𝑆 (𝑥)
𝑅 UNION ALL 𝑆
𝑅 UNION 𝑆
Min-intersection
𝑅 ∩𝑆
min(𝑅(𝑥), 𝑆 (𝑥))
𝑅 INTERSECT ALL 𝑆
–
Max-intersection
𝑅 ⊓𝑆
𝑆 (𝑥) × 𝑆 (𝑥)
𝑅 NATURAL JOIN 𝑆
𝑅 AND 𝑆
max(0, 𝑅(𝑥) − 𝑆 (𝑥)) 𝑅(𝑥) if 𝑆 (𝑥) = 0, 0 otherwise. Í 𝑡 ∈𝑅, 𝑡 [𝐴𝑡𝑡𝑠 ]=𝑥 𝑅(𝑥)
𝑅 EXCEPT ALL 𝑆
–
SELECT * FROM 𝑅 WHERE 𝑥 NOT IN (𝑆)
𝑅 MINUS 𝑆
SELECT 𝐴𝑡𝑡𝑠 FROM 𝑅
SELECT 𝐴𝑡𝑡𝑠
Arithmetic difference 𝑅 − 𝑆 Existential negation
𝑅 \𝑆
Projection
𝜋𝐴𝑡𝑡𝑠 (𝑅)
given for the relations 𝑆 and 𝑇 . Note that 𝑅 and 𝑆 have the same attributes, while 𝑇 does not have attributes in common with 𝑅 and 𝑆. • The selection returns the tuples satisfying a given condition but keeping cardinalities. For example, 𝜎𝑋 =′𝑏 ′ (𝑅) returns the multiset ⦃(𝑎, 𝑏), (𝑎, 𝑏)⦄ with schema (𝑊 , 𝑋 ). • The cartesian product results in the multiplication of the cardinalities. For example, 𝑅 × 𝑇 returns the multiset ⦃ (𝑎, 𝑏, 𝑏, 𝑐), (𝑎, 𝑏, 𝑏, 𝑐), (𝑎, 𝑏, 𝑏, 𝑐), (𝑎, 𝑏, 𝑏, 𝑐), (𝑎, 𝑑, 𝑏, 𝑐), (𝑎, 𝑑, 𝑏, 𝑐) ⦄ with schema (𝑊 , 𝑋, 𝑌 , 𝑍 ). • The join results in the multiplication of the cardinalities, as it is expressed as a cartesian product followed by a selection. For example, 𝑅 Z𝑋 =𝑌 𝑇 returns the multiset ⦃ (𝑎, 𝑏, 𝑏, 𝑐), (𝑎, 𝑏, 𝑏, 𝑐), (𝑎, 𝑏, 𝑏, 𝑐), (𝑎, 𝑏, 𝑏, 𝑐) ⦄ with schema (𝑊 , 𝑋, 𝑌 , 𝑍 ). • The max-union takes the maximum number of occurrences of an element. For example, 𝑅 ⊔ 𝑆 returns the multiset ⦃ (𝑎, 𝑏), (𝑎, 𝑏), (𝑎, 𝑑) ⦄ with schema (𝑊 , 𝑋 ). • The arithmetic union adds up cardinalities. For example, 𝑅 ∪ 𝑆 returns the multiset ⦃ (𝑎, 𝑏), (𝑎, 𝑏), (𝑎, 𝑑), (𝑎, 𝑏) ⦄ with schema (𝑊 , 𝑋 ). • The min-intersection takes the minimum number of occurrences of each element in the intersection. For example, 𝑅 ∩ 𝑆 returns the multiset ⦃ (𝑎, 𝑏) ⦄ with schema (𝑊 , 𝑋 ). • The max-intersection returns the product of the cardinalities of each element in the intersection. For example, 𝑅 ⊓ 𝑆 returns the multiset ⦃ (𝑎, 𝑏), (𝑎, 𝑏) ⦄ with schema (𝑊 , 𝑋 ). • The arithmetic difference subtracts the cardinalities of the elements up to zero. For example 𝑅 − 𝑆 returns the multiset ⦃ (𝑎, 𝑏), (𝑎, 𝑑) ⦄ with schema (𝑊 , 𝑋 ). • The existential negation returns the elements in the first multiset that do not occur in the second one, and keeping the cardinalities of such elements in the first multiset. For example, the expression 𝑅 \ 𝑆 returns the multiset ⦃ (𝑎, 𝑑) ⦄ with schema (𝑊 , 𝑋 ).
The multiset semantics of SPARQL patterns
3
• The projection reduces the number of attributes in each tuple, and gives rise to new cardinalities for the resulting tuples. For example 𝜋𝑊 (𝑅) returns the multiset ⦃ (𝑎), (𝑎), (𝑎) ⦄ with schema (𝑊 ). Table 1 shows a summary of the above operators, and their corresponding implementation in SQL and SPARQL. Note that SQL can express all the operators, whereas SPARQL does not support max-union, min-intersection, and arithmetic difference. Also note that SPARQL uses the AND operator to implement cartesian product and max-intersection. The first case occurs because 𝑅 and 𝑇 do not have variables in common, and the second case occurs because 𝑅 and 𝑆 have the same set of variables. The landscape of operators over multisets poses important challenges for integrating multisets in query languages. First, as shown in Table 1, some operators exhibit different semantics when applied to multisets. Second, while relational algebra and SQL support all the semantics listed, SPARQL and Datalog only support a subset. Third – and this is the main motivation for our research – it remains unclear whether there exists an optimal set of multiset operators for SPARQL, and if so, which one it is. To tackle these questions, it is essential to understand how formalisms that are “closed” with respect to SPARQL behave, and how their design and behavior can inform or be translated into SPARQL. In technical terms, this means analyzing the expressive power of SPARQL regarding multisets. To this end, we focus on two natural and well-studied reference points: relational algebra and Datalog. That is the aim of this article. Next, we review the existing literature about multisets. Related Work. First, we consider the research works that define general algebras for manipulating bags. Albert [3] extended typical set operations (union, intersection, difference, and boolean selection) to bags, and demonstrated that some of the algebraic properties for sets fail for multisets. Grumbach et al. [20] introduced a bag algebra, called BALG, that extends relational operations to handle duplicates. This paper shows that BALG is more expressive than standard relational algebra because it can count duplicates, but it still has low data complexity (LOGSPACE). Grumbach and Milo [21] focused on designing bag algebras that are both expressive and computationally tractable. They introduce restricted forms of projection and join to maintain tractable data complexity. Libkin and Wong [32, 33] introduced BQL, a query language for handling bags and aggregate functions (sum, count, avg.). They show that BQL is more expressive than traditional set-based languages, and shows that after incorporating structural recursion to BQL, it is able to express all primitive recursive functions, significantly increasing its computational power. Ricciotti and Cheney [42] explored how to mix set and bag semantics in query languages, addressing practical needs found in SQL (e.g., SELECT versus SELECT DISTINCT). They propose a formal model that supports both semantics and allows translation between them. The first attempt to extend the relational algebra to include multisets was made by Dayal et al. [17]. In this work, the authors introduced a multiset relational algebra (formed by the operators of projection, selection, join, max-union, arithmetic union, min-intersection and arithmetic difference) and studied their algebraic properties. This work laid the groundwork for formalizing bag semantics in relational query languages. Klauser and Goodman [29] provided a semantic framework for understanding the role of multirelations (relations with duplicates) at the conceptual level. The authors explain how any query language can be extended consistently to have full multirelational expressiveness. Afrati et al. [2] studied query containment in relational databases under bag semantics and bag-set semantics (duplicates allowed in intermediate steps but not in final output). The authors identify conditions under which containment is decidable and provide complexity results. Console et al. [14] investigated fragments of bag relational algebra, focusing on their expressive power. The authors also study query answering over bags with nulls (i.e. under incomplete data).
4
Angles et al.
Multisets have also been the subject of study in the context of Datalog, with various extensions proposed to support bag semantics. Mumick et al. [36] defined the Magic Sets transformation for optimizing recursive queries, and described how to adapt this technique to support duplicates. They also showed how to efficiently evaluate recursive queries under multiset semantics. In a subsequent work [37], Mumick et al. extended the Magic Sets technique to support duplicates and aggregate functions in recursive queries. The authors also studied the challenges of preserving correct bag semantics when applying recursion and aggregation. Cohen [12] studied the problem of query equivalence under bag semantics. This work includes complexity results and demonstrates that equivalence checking is significantly harder under bag semantics. Bertossi et al. [36] developed a translation of Datalog under bag semantics into warded Datalog± , a well-behaved extension under set semantics. The authors investigated the properties of the resulting Datalog± programs, the problem of deciding multiplicities, and expressibility of some bag operations. For SPARQL – the standard query language for RDF databases – Pérez et al. [38] provided the first formal treatment of its multiset semantics. This work influenced the definition of SPARQL 1.0 [41] and SPARQL 1.1 [23], whose semantics are based on operations over multisets of mappings (although a database is a set of RDF triples). Schmidt et al. [44] presented a formal framework for SPARQL query optimization, addressing both set and bag semantics. The authors analyzed the algebraic properties of SPARQL operations like OPTIONAL, UNION, and FILTER under multisets, and introduced equivalence rules and normal forms for optimizing queries. Kaminski et al. [28] presented a formal investigation of subqueries and aggregate functions in SPARQL 1.1, focusing on their semantics under multisets. The authors analyzed the expressive power of these constructs, showing that SPARQL 1.1 is strictly more expressive than SPARQL 1.0 due to these features. Finally, we review research articles that present comparisons and translations among SPARQL, relational algebra, and Datalog. Cyganiak [15] was among the first to translate a core fragment of SPARQL into relational algebra. Polleres [39] proved the inclusion of the fragment of SPARQL patterns with safe filters into Datalog by providing a precise and correct set of rules. Schenk [43] proposed a formal semantics for SPARQL based on Datalog, but concentrated on complexity more than expressiveness issues. Both Polleres and Schenk did not consider the multiset semantics of SPARQL in their translations. Angles and Gutierrez [5] studied the expressive power of SPARQL by providing a translation to non-recursive safe Datalog with negation. Chebotko et al. [11] addressed the problem of translating SPARQL queries into SQL while preserving bag semantics. The authors proposed a formal translation framework that captures the subtleties of OPTIONAL, UNION, and FILTER, and ensures that duplicates in the result sets are handled correctly when mapped to relational databases. Angles and Gutierrez [6] studied the multiset semantics of SPARQL patterns by translating its patterns into two languages: a version of multiset relational algebra and multiset non-recursive Datalog with safe negation. Angles et al. [4] implemented the translation from SPARQL to Datalog within the Vadalog system [8]. Objectives and Contributions. The main objective of this article is to examine the theoretical foundations of SPARQL’s multiset semantics. To do so, we compare it with classical algebraic and logical frameworks – specifically, Relational Algebra and Datalog. We focus on the SPARQL fragment built from AND, UNION, FILTER, EXCEPT, and SELECT, characterizing its structure and proving its expressive equivalence with corresponding fragments of Relational Algebra and Datalog. The specific contributions of our research are as follows: (1). Based on the work of Mumick et al [37], who defined the multiset semantics for Datalog without negation, we defined a version called Non-Recursive Multiset Datalog with Safe Negation (NRMD¬ ). The definition of NRMD¬ includes negation and follows a proof-theoretic semantics.
The multiset semantics of SPARQL patterns
5
Table 2. Schema of correspondences among: SPARQL graph patterns, Multiset Relational Algebra (MRA) expressions, Non-Recursive Datalog with safe Negation (NRMD¬ ) rules, and SQL expressions. The operator EXCEPT is not part of SPARQL, but it replaces the standard operators MINUS and OPT without changing the expressiveness of the fragment. In MRA, ⊎ is the arithmetic union and \ is the multiset filter difference. SPARQL patterns are assumed normalized, that is, variables in the filter condition are in the schema of the filtered pattern, and operators EXCEPT AND UNION assume operands with the same schema. 𝑃 1 and 𝑃2 are SPARQL patterns that are associated to atoms 𝐿1 and 𝐿2 in the NRMD¬ translation, and relations 𝑟 1 and 𝑟 2 in the MRA translations, respectively. SPARQL
NRMD¬
MRA
SQL
SELECT X 𝑃 1
𝐿 ← 𝐿1, null(X \ X1 )
𝜋 X (𝑟 1 ) Z null(X \ X1 )
SELECT X FROM 𝑟 1 NATURAL JOIN null(X \ X1 )
𝑃 1 FILTER 𝑋 = 𝑎
𝐿 ← 𝐿1 , 𝑋 = 𝑎
𝜎𝑋 =𝑎 (𝑟 1 )
FROM 𝑟 1 WHERE 𝑋 = 𝑎
𝑃 1 AND 𝑃2
𝐿 ←𝑣 1 (𝐿1 ), 𝑣 2 (𝐿2 ), comp(𝑣 1, 𝑣 2, X)
𝜋 X̄ (𝜌 𝑣1 (𝑟 1 ) Z 𝜌 𝑣2 (𝑟 2 ) Z comp(𝑣 2, 𝑣 2, X))
SELECT X FROM 𝑟 1 NATURAL JOIN 𝑟 2 NATURAL JOIN comp(𝑣 2, 𝑣 2, X)
𝑃 1 UNION 𝑃2
𝐿 ← 𝐿1 ; 𝐿 ← 𝐿2
𝑟1 ⊎ 𝑟2
𝑟 1 UNION ALL 𝑟 2
𝑃 1 EXCEPT 𝑃2
𝐿 ← 𝐿1, ¬𝐿2
𝑟1 \ 𝑟2
𝑟 1 EXCEPT 𝑟 2
(2). Based on the work of Dayal et al. [17], who extended the relational algebra to include multiset relations, we defined a Multiset Relational Algebra (MRA). The definition of MRA includes the operators of projection (𝜋), selection (𝜎), natural join (Z), arithmetic union (∪) and filter difference (\), all of them working under multiset semantics. (3). We show the equivalence among the aforementioned SPARQL fragment, MRA and NRMD¬ by providing translations for databases, queries, and answers. Table 2 shows a glimpse of these translations, whose details are developed in this paper. This paper extends a previously published conference paper [6]. Herein, we provide extended discussion throughout, we extend the study to some operators that were introduced in the version 1.1 of SPARQL after the publication of our previous work, and we extend the analysis to also consider bag semantics. Some of the additional contributions of this paper come from Hernandez’s Ph.D. thesis [25]. The rest of the article is organized as follows. Section 2 presents basic concepts and notations. The SPARQL query language is defined in Section 3. Non-recursive Multiset Datalog with Safe Negation (NRMD¬ ) is defined in Section 4. The Multiset Relational Algebra (MRA) is defined in Section 5. The equivalence between SPARQL and NRMD¬ is presented in Section 6. The equivalence between MRA and NRMD¬ is presented in Section 7. The equivalence between MRA and SPARQL is presented in Section 8. Conclusions are presented in Section 9. 2
Preliminaries
This section provides the concepts and formal notation we will follow regarding multisets and the expressive power of query languages. 2.1
Multisets
Informally, a multiset is an unordered collection of elements where each element may occur more than once. Formally, a multiset is a tuple 𝑀 = (𝑆, card) where 𝑆 is the underlying set of 𝑀 (containing the distinct elements), and card : 𝑆 → N+ is a function that defines the cardinality in 𝑀 of each element 𝑎 ∈ 𝑆. We write set(𝑀) = 𝑆 to denote that the underlying set of 𝑀 is 𝑆. Given a
6
Angles et al.
positive natural number 𝑛, card(𝑎, 𝑀) = 𝑛 denotes 𝑎 ∈ set(𝑀) and the cardinality of 𝑎 in 𝑀 is 𝑛, and is usually written as (𝑎, 𝑛) ∈ 𝑀. Abusing notation, we write card(𝑎, 𝑀) = 0 if 𝑎 ∉ set(𝑀) and 𝑎 ∈ 𝑀 when card(𝑎, 𝑀) ≥ 1. In what follows, we will prefer these formal notions over the informal and intuitive ⦃𝑎, 𝑎, 𝑎, 𝑏⦄. When dealing with multisets, formally describing each “copy” of an element is challenging. The notion of colored set [37] is a formalism to do this. Indeed, assuming that the set of colors is N+ , the colored set of a multiset 𝑀, denoted coloring(𝑀), is the set {⟨𝑎, 𝑖⟩ | 𝑎 ∈ set(𝑀) and 1 ≤ 𝑖 ≤ card(𝑎, 𝑀)}. In the contrary direction, we write coloring−1 (𝐶) for the multiset 𝑀 with set(𝑀) = set(𝐶) and defined by a colored set 𝐶 when forgetting the colors, that is, when for every element 𝑎 ∈ set(𝑀), card(𝑎, 𝑀) is the number of colored copies of 𝑎 in 𝐶. Abusing notation, we write coloring−1 (⟨𝑎, 𝑖⟩) = 𝑎. Example 2.1. Let 𝐴 be the set {𝑎, 𝑏, 𝑐}, 𝑀 be a multiset with set(𝑀) = 𝐴, card(𝑎, 𝑀) = 1, card(𝑏, 𝑀) = 2, and card(𝑐, 𝑀) = 3. Then, coloring(𝑀) is the colored set 𝐶 = {⟨𝑎, 1⟩, ⟨𝑏, 1⟩, ⟨𝑏, 2⟩, ⟨𝑐, 1⟩, ⟨𝑐, 2⟩, ⟨𝑐, 3⟩}. 2.2
Comparing the expressive power of query languages
Next, we present the notion of query language and two notions of expressive power used in this paper. Definition 2.2 (Query language). A query language L is a quadruple (Q, D, S, Eval), where Q is the set of queries in L, D is the set of databases in L, S is the set of query answers in L, and Eval : Q × D → S is the query evaluation function of L. Let L = (Q, D, S, Eval) be a query language. Two queries 𝑄 1, 𝑄 2 ∈ Q are said to be equivalent, denoted 𝑄 1 ≡ 𝑄 2 , if for every database 𝐷 ∈ D, it holds that Eval(𝑄 1, 𝐷) = Eval(𝑄 2, 𝐷), i.e., they return the same query answer for all input databases. Given a query language (Q, D, S, Eval), a query 𝑄 ∈ Q determines a function 𝑞 : D → S defined as 𝑞(𝐷) = Eval(𝑄, 𝐷), called the query function of 𝑄. Two queries 𝑄 1 and 𝑄 2 are thus equivalent, denoted 𝑄 1 ≡ 𝑄 2 , if they determine the same query function. In this context, the expressive power of a query language L is understood as the set of all query functions that are expressible by L. Abiteboul et al. [1] summarizes how this notion is used to compare the expressive power of relational algebra, Datalog, and relational calculus. In the context of SPARQL, Zhang and Van den Bussche [45], Kontchakov et al. [30], and Angles and Gutierrez [7] use this notion to compare different fragments of SPARQL. The query languages studied in this paper do not satisfy the aforementioned property of having a common set of databases and query answers. Thus, we need an extended version of the notion of expressive power as in Definition 2.3 below. Definition 2.3 (Generalized expressive power). Given two query languages L1 = (Q1, D1, S1, Eval1 ) and L2 = (Q2, D2, S2, Eval2 ), we say that L1 is contained in L2 if and only if there exist functions 𝑔 : D1 → D2 (called the database translation), 𝑓 : Q1 → Q2 (called the query translation), and ℎ : S2 → S1 (called the query answer translation), such that for every 𝑄 ∈ Q1 and database 𝐷 ∈ D1 it holds that Eval1 (𝑄, 𝐷) = ℎ(Eval2 (𝑓 (𝑄), 𝑔(𝐷))). If that is the case, we say that the triple (𝑓 , 𝑔, ℎ) is a simulation of L1 in L2 . We say that the languages L1 and L2 have the same expressive power, denoted L1 L2 , if and only if L1 is contained in L2 and L2 is contained in L1 .
The multiset semantics of SPARQL patterns
7 𝑔12
D1
𝑔23
D2 𝑓12 (𝑄 1 )
𝑄1 ℎ 12
S1
D3
𝑓12 ◦𝑓23 (𝑄 1 ) ℎ 23
S2
S3
Fig. 1. Transitivity of language containment. The figure represents three languages L𝑖 = (Q𝑖 , D𝑖 , S𝑖 , Eval𝑖 ) where 𝑖 ∈ {1, 2, 3}. The containment of a language L𝑖 in L𝑖+1 is given by the simulation (𝑓𝑖,𝑖+1, 𝑔𝑖,𝑖+1, ℎ𝑖,𝑖+1 ). The transitive containment of L1 in L3 is given by the simulation (𝑓12 ◦ 𝑓23, 𝑔12 ◦ 𝑔23, ℎ 23 ◦ ℎ 12 ) where ◦ denotes the composition of functions (e.g., 𝑔12 ◦ 𝑔23 denotes the function from 𝐷 1 to 𝐷 3 that results from composing 𝑔12 and 𝑔23 ).
The above definition of generalized expressive power is implicit in the translations by Polleres [39], Angles and Gutierrez [5, 6], and Polleres and Wallner [40]. Observe that the extended notion defined above defines a partial order: the containment relation on the equivalence classes over the relation . In fact, reflexivity and antisymmetry follow directly from the definition, while transitivity is shown in Figure 1. 2.3
Comparing SPARQL, NRMD¬ and MRA
In the remainder of this paper, we define three families of query languages: Non-recursive Multiset Datalog with Safe Negation (NRMD¬ ), Multiset Relational Algebra (MRA) and a core fragment of SPARQL. After defining these languages, we present simulations that show the equivalence among these three families of query languages. These simulations are depicted in Figure 2. (1) SPARQL
𝑇31
𝑇12 𝑇13
𝑇21
(2) NRMD¬
𝑇32
(3) MRA
𝑇23 Fig. 2. The triangle of simulations among SPARQL, Non-Recursive Multiset Datalog with Safe Negation (NRMD¬ ), and Multiset Relational Algebra (MRA) described in this paper. The query languages are identified by numbers, and 𝑇𝑖 𝑗 denotes the simulation of language 𝑖 using language 𝑗.
3
Multiset SPARQL
SPARQL [23, 41] is the standard query language for RDF. In this paper we study a fragment of SPARQL, the “relational core”, described by Angles and Gutierrez [6], which considers the operators FILTER, SELECT, AND, UNION, and EXCEPT. This fragment captures essentially the graph pattern queries in SPARQL. In fact, it has been proved [6, 30] that it is mutually expressible with the standard-core consisting of the operators FILTER, SELECT, AND, UNION, OPTIONAL, and MINUS. (In what follows when speaking of “SPARQL” we will mean this fragment).
8
3.1
Angles et al.
RDF Graphs
Assume two disjoint infinite sets I and L, called IRIs and literals, respectively. An RDF term is an element in the set T = I ∪ L. An RDF triple is a triple (𝑠, 𝑝, 𝑜) ∈ I × I × T where 𝑠 is called the subject, 𝑝 is called the predicate and 𝑜 is called the object. An RDF graph (just graph from now on) is a set of RDF triples. Given a graph 𝐺, the function terms(𝐺) returns the RDF terms that occur in 𝐺, that is, terms(𝐺) = {𝑠 | (𝑠, 𝑝, 𝑜) ∈ 𝐺 } ∪ {𝑝 | (𝑠, 𝑝, 𝑜) ∈ 𝐺 } ∪ {𝑜 | (𝑠, 𝑝, 𝑜) ∈ 𝐺 } The union of graphs, 𝐺 1 ∪ 𝐺 2 , is the theoretical union of their sets of triples. A SPARQL database will be a set of RDF triples. Note: In addition to I and L, SPARQL admits as terms anonymous resources called blank nodes. In this paper, we do not include them to help focus on the issues arising from multisets. Avoiding blank nodes does not affect the results presented in this paper. Indeed, in SPARQL, blank nodes in the data can be consistently replaced by IRIs and produce equivalent query results, and blank nodes in queries can be replaced by fresh variables without changing the semantics of the query [26]. 3.2
SPARQL Syntax
Assume the existence of an infinite set V of variables disjoint from T (RDF terms). A filter condition is defined recursively as follows: (i) If ?𝑋, ?𝑌 ∈ V and 𝑐 ∈ T then (?𝑋 = 𝑐), (?𝑋 = ?𝑌 ) and bound(?𝑋 ) are atomic filter conditions; (ii) If 𝜑 1 ,𝜑 2 are filter conditions then (𝜑 1 ∧ 𝜑 2 ),(𝜑 1 ∨ 𝜑 2 ) and ¬𝜑 1 are complex filter conditions. We denote by var(𝜑) the set of variables occurring in 𝜑. A SPARQL pattern is defined recursively as follows: • A triple from (I ∪ V) × (I ∪ V) × (I ∪ L ∪ V) is a pattern called a triple pattern. We will assume that a triple pattern has at least one variable. • If 𝑃1 and 𝑃2 are patterns then (𝑃 1 AND 𝑃2 ), (𝑃1 UNION 𝑃2 ), and (𝑃 1 EXCEPT 𝑃2 ) are patterns. • If 𝑃 is a pattern and 𝜑 is a filter condition then (𝑃 FILTER 𝜑) is a pattern. • If 𝑊 is a set of variables and 𝑃 1 is a pattern then (SELECT𝑊 𝑃 1 ) is a pattern. 3.3
SPARQL Semantics
A solution mapping (or just mapping from now on) is a partial function 𝜇 : V → T where the domain of 𝜇, denoted dom(𝜇), is the subset of V where 𝜇 is defined. We write 𝜇 ∅ to denote the mapping with empty domain (i.e., dom(𝜇 ∅ ) = ∅). Given ?𝑋 ∈ V and 𝑐 ∈ T, we write 𝜇 (?𝑋 ) = 𝑐 to denote that 𝜇 maps the variable ?𝑋 to the term 𝑐. Given a finite set of variables 𝑊 , the restriction of a mapping 𝜇 to 𝑊 , denoted 𝜇 |𝑊 , is a mapping 𝜇 ′ that satisfies dom(𝜇 ′ ) = 𝑊 ∩ dom(𝜇) and 𝜇 ′ (?𝑋 ) = 𝜇 (?𝑋 ) when ?𝑋 ∈ dom(𝜇 ′ ). Two solution mappings 𝜇1, 𝜇2 are compatible, denoted 𝜇1 ∼ 𝜇2 , when for all ?𝑋 ∈ dom(𝜇 1 ) ∩ dom(𝜇 2 ) they satisfy 𝜇1 (?𝑋 ) = 𝜇 2 (?𝑋 ), that is, when 𝜇1 ∪ 𝜇2 is also a mapping. Note that two mappings with disjoint domains are always compatible. Let Ω be a multiset of solution mappings. The domain of variables in Ω, denoted dom(Ω), is defined as the set union of the domains of the variables that occur in the solution mappings of Ω. Given a mapping 𝜇, the cardinality of 𝜇 in Ω will be denoted as card(𝜇, Ω). If 𝜇 ∉ Ω then card(𝜇, Ω) = 0. The evaluation of a filter condition 𝜑 under a mapping 𝜇, denoted 𝜇 (𝜑), is defined in a threevalued logic with values true, false and error. We say that 𝜇 satisfies 𝜑 when 𝜇 (𝜑) = true. The semantics of 𝜇 (𝜑) is defined recursively as follows: • If 𝜑 is ?𝑋 = 𝑐 and 𝑐 ∈ T, then: (a) If ?𝑋 ∈ dom(𝜇) then 𝜇 (𝜑) = true when 𝜇 (?𝑋 ) = 𝑐 and 𝜇 (𝜑) = false otherwise; (b) If ?𝑋 ∉ dom(𝜇) then 𝜇 (𝜑) = error. • If 𝜑 is ?𝑋 = ?𝑌 and ?𝑋, ?𝑌 ∈ dom(𝜇), then 𝜇 (𝜑) = true when 𝜇 (?𝑋 ) = 𝜇 (?𝑌 ), and 𝜇 (𝜑) = false otherwise. If ?𝑋 ∉ dom(𝜇) or ?𝑌 ∉ dom(𝜇) then 𝜇 (𝜑) = error.
The multiset semantics of SPARQL patterns
9
Table 3. Evaluation of complex filter conditions [41, §17.2], where 𝜇 is a solution mapping, and 𝜑 1 ,𝜑 2 are filter conditions.
𝜇 (𝜑 1 )
𝜇 (𝜑 2 )
𝜇 (𝜑 1 ) ∧ 𝜇 (𝜑 2 )
𝜇 (𝜑 1 ) ∨ 𝜇 (𝜑 2 )
true true true false false false error error error
true false error true false error true false error
true false error false false false error false error
true true true true false error true error error
𝜇 (𝜑 1 )
¬(𝜇 (𝜑 1 ))
true false error
false true error
• If 𝜑 is bound(?𝑋 ) and ?𝑋 ∈ dom(𝜇) then 𝜇 (𝜑) = true; otherwise 𝜇 (𝜑) = false. • If 𝜑 is a complex filter condition, then it is evaluated following the three valued logic shown in Table 3. The evaluation of a pattern 𝑃 on a graph 𝐺 is defined as a function J𝑃K𝐺 , which returns a multiset of mappings. Let 𝑃1, 𝑃2 be SPARQL patterns, 𝜑 be a filter condition and 𝑊 be a set of variables. For simplicity of reading, denote 𝑀 = J𝑃K𝐺 , 𝑀1 = J𝑃1 K𝐺 , and 𝑀2 = J𝑃2 K𝐺 . The evaluation J𝑃K𝐺 is defined recursively as follows: • If 𝑃 is a triple pattern 𝑡 then set(𝑀) = {𝜇 | dom(𝜇) = var(𝑡), 𝜇 (𝑡) ∈ 𝐺 }, where 𝜇 (𝑡) is the triple obtained by replacing the variables in 𝑡 according to 𝜇, and card(𝜇, 𝑀) = 1. • If 𝑃 is (𝑃 1 AND Í 𝑃2 ) then set(𝑀) = {𝜇1 ∪ 𝜇 2 | 𝜇1 ∈ 𝑀1, 𝜇 2 ∈ 𝑀2, and 𝜇1 ∼ 𝜇2 } and card(𝜇, 𝑀) = 𝜇=𝜇1 ∪𝜇2 card(𝜇 1, 𝑀1 ) × card(𝜇 2, 𝑀2 ). • If 𝑃 is (𝑃1 UNION 𝑃2 ) then set(𝑀) = {𝜇 | 𝜇 ∈ 𝑀1 ∨ 𝜇 ∈ 𝑀2 } and card(𝜇, 𝑀) = card(𝜇, 𝑀1 ) + card(𝜇, 𝑀2 ). • If 𝑃 is (𝑃1 EXCEPT 𝑃2 ) then set(𝑀) = {𝜇 | 𝜇 ∈ 𝑀1, 𝜇 ∉ 𝑀2 } and card(𝜇, 𝑀) = card(𝜇, 𝑀1 ). • If 𝑃 is (𝑃 1 FILTER 𝜑) then set(𝑀) = {𝜇 | 𝜇 ∈ 𝑀1, 𝜇 (𝜑) = true} and card(𝜇, 𝑀) = card(𝜇, 𝑀1 ). • If 𝑃 is (SELECT𝑊 𝑃1 ) then set(𝑀) = {𝜇 ′ |Í𝜇 ′ = 𝜇 |𝑊 ∧ 𝜇 ∈ 𝑀1 } and card(𝜇 ′, 𝑀) = 𝜇 ′ =𝜇 |𝑊 card(𝜇, 𝑀1 ). To facilitate the translation from SPARQL to relational algebra and Datalog, we use the difference operator EXCEPT in SPARQL, called SetMinus by Kontchakov et al. [30]. Kontchakov et al. [30] proved that, over this fragment, the operator EXCEPT and the pair of standard operators {MINUS, OPTIONAL} are mutually expressible. 3.4
Normalization of SPARQL patterns
The solution mappings of a SPARQL pattern 𝑃 may have different domains. To translate SPARQL to languages built upon relations, we require representing multisets of mappings as relations whose tuples have the same set of attributes. This set of attributes has to contain all variables that can appear in the solution mappings of 𝑃. The SPARQL specification [23] defines a finite set of variables, called in-scope, that include all variables of a SPARQL pattern 𝑃 that can occur in the solution mappings of 𝑃. To complete the relation, unbound values need to be denoted with a distinguished constant of the target languages.
10
Angles et al.
Example 3.1. Assume a pattern 𝑃 with in-scope variables ?𝑋 , ?𝑌 , and ?𝑍 that returns the multiset of mappings Ω = ⦃{?𝑋 ↦→ 𝑎}, {?𝑋 ↦→ 𝑏, ?𝑌 ↦→ 𝑐}, {?𝑌 ↦→ 𝑑 }⦄. Since all variables in the solution mappings are ensured to be in-scope variables of 𝑃, we can represent this multiset of mappings as the following relation (⊥ denotes the distinguished constant to denote unbound values): ?𝑋 ?𝑌 ?𝑍 𝑎 ⊥ ⊥ . 𝑏 𝑐 ⊥ ⊥ 𝑑 ⊥ In-scope variables are defined as follows. Let 𝑃1 , 𝑃2 and 𝑃3 be patterns, 𝜑 be a filter condition, and 𝑊 be a set of variables. The set of in-scope variables of a pattern 𝑃, denoted inScope(𝑃), is defined recursively as follows: (1) If 𝑃 is a triple pattern then inScope(𝑃) is the set of variables occuring in 𝑃. (2) If 𝑃 is (𝑃1 AND 𝑃2 ) or (𝑃1 UNION 𝑃2 ) then inScope(𝑃) = inScope(𝑃 1 ) ∪ inScope(𝑃 2 ); (3) If 𝑃 is (𝑃1 FILTER 𝜑) or (𝑃 1 EXCEPT 𝑃2 ) then inScope(𝑃) = inScope(𝑃 1 ); (4) If 𝑃 is (SELECT 𝑊 𝑃 1 ) then inScope(𝑃) = 𝑊 . So far, we have described how to translate the results of SPARQL queries to relations. However, languages built upon relations have some restrictions that difficult a straightforward translation of the SPARQL operations. The relational selection operation requires all attributes in the selection formula being attributes of the relation; the relational union is done over relations of the same schema; and the relational difference requires all variables in the subtrahend be instanced in the minuend. Conversely, SPARQL does not have these restrictions. We next present a normal form to simplify the translation from SPARQL to relational languages by satisfying the constraints of the target languages. Definition 3.2 (SPARQL normal form). A pattern 𝑃 is said to be in normalized or in normal form if the following conditions hold: (1) For every sub-pattern (𝑃 1 FILTER 𝜑) in 𝑃 it holds that var(𝜑) ⊆ inScope(𝑃 1 ); (2) For every sub-pattern (𝑃 1 UNION 𝑃2 ) in 𝑃 it holds that inScope(𝑃 1 ) = inScope(𝑃2 ); (3) For every sub-pattern (𝑃 1 EXCEPT 𝑃2 ) in 𝑃 it holds that inScope(𝑃 1 ) = inScope(𝑃2 ). Lemma 3.3. Every SPARQL query (in the fragment described in Section 3.2) can be rewritten as an equivalent normalized SPARQL query. Proof. The conditions that make a pattern normalized refer to restrictions to the in-scope variables of patterns. Patterns that are not normalized include at least one sub-pattern that has either the form (𝑃 1 FILTER 𝜑), (𝑃2 UNION 𝑃3 ), or (𝑃 2 EXCEPT 𝑃3 ), where 𝜑 contains a variable ?𝑋 ∉ inScope(𝑃1 ), and inScope(𝑃2 ) ≠ inScope(𝑃3 ). We next present a method to normalize these patterns. Given a pattern 𝑃, and a finite set of variables 𝑋 , 𝑃 ≡ (SELECT (inScope(𝑃) ∪ 𝑋 ) 𝑃). Indeed, a mapping 𝜇 is a solution of the pattern (SELECT (inScope(𝑃) ∪ 𝑋 ) 𝑃) if and only there exists a solution mapping 𝜇 ′ of pattern 𝑃 such that 𝜇 = 𝜇 ′ | inScope(𝑃 )∪𝑋 . By the definition of the inscope variables, dom(𝜇 ′ ) ⊆ inScope(𝑃). Then, dom(𝜇 ′ ) ⊆ inScope(𝑃) ∪ 𝑋 . Then, 𝜇 = 𝜇 ′ . Hence, 𝑃 ≡ (SELECT (inScope(𝑃) ∪ 𝑋 ) 𝑃). Let 𝑃1′ , 𝑃2′ , and 𝑃 3′ be the patterns defined as follows: 𝑃1′ = (SELECT (inScope(𝑃1 ) ∪ var(𝜑)) 𝑃1 ), 𝑃2′ = (SELECT (inScope(𝑃2 ) ∪ inScope(𝑃 3 )) 𝑃 2 ), 𝑃3′ = (SELECT (inScope(𝑃2 ) ∪ inScope(𝑃 3 )) 𝑃 3 ).
The multiset semantics of SPARQL patterns
11
Since 𝑃1′ ≡ 𝑃1 , 𝑃2′ ≡ 𝑃2 , and 𝑃3′ ≡ 𝑃3 , the following equivalences hold: (𝑃 1 FILTER 𝜑) ≡ (𝑃1′ FILTER 𝜑), (𝑃 2 UNION 𝑃3 ) ≡ (𝑃2′ UNION 𝑃3′ ), (𝑃 2 EXCEPT 𝑃3 ) ≡ (𝑃2′ EXCEPT 𝑃3′ ). Unlike the patterns on the left side of these equivalences, the patterns on the right side are normalized. Indeed, by the definition of the inScope function, var(𝜑) ⊆ inScope(𝑃1′ ) and inScope(𝑃2′ ) = inScope(𝑃3′ ). Hence, these equivalences can be used to normalize SPARQL patterns. □ Example 3.4. Let 𝑃 be the pattern (𝑃1 UNION 𝑃2 ) where 𝑃1 is the triple pattern (?𝑋, is, person) and 𝑃2 is the triple pattern (?𝑋, email, ?𝑌 ), and 𝐺 be the RDF graph that includes the triples (𝑎, is, person) and (𝑎, email, [email protected]). The pattern 𝑃 is not in normal form because variable ?𝑌 is in inScope(𝑃2 ), but not in inScope(𝑃1 ). The normal form of the pattern 𝑃 is a pattern 𝑃 ′ that results from replacing 𝑃1 by the pattern 𝑃1′ = (SELECT ?𝑋 ?𝑌 (?𝑋, is, person)). The patterns 𝑃 and 𝑃 ′ are equivalent because the patterns 𝑃1 and 𝑃 1′ return the same multiset of solution mappings Ω1 = ⦃{?𝑋 ↦→ 𝑎}⦄. Note that variable ?𝑌 is not in the solutions of 𝑃 1 nor 𝑃 1′ . However, variable ?𝑌 is in inScope(𝑃1′ ) but not in inScope(𝑃1 ). Using the in-scope variables of the patterns to translate the results of patterns 𝑃 1 and 𝑃1′ as relations we get the respective relations # " # " ?𝑋 ?𝑋 ?𝑌 and . 𝑎 𝑎 ⊥ Although both relations represent the same multiset of mappings, just the second relation has the same attributes as the result of pattern 𝑃 2 , and thus can be operated with the relational union. 4
Non-Recursive Multiset Datalog with Safe Negation (NRMD¬ )
This section presents an extension of Datalog to support multiset semantics. Based on the work of Mumick et al. [37], a database is defined to allow duplicate facts, and the evaluation of a fact is given by the number of different proofs for that fact. We extended Mumick’s formalism in [6] to provide a more complete formalism including negation, which we call MD¬ . Furthermore, we follow the work of Bertossi et al. [9] for the semantics of MD¬ . We call Non-Recursive Multiset Datalog with Safe Negation (NRMD¬ ) to the fragment of MD¬ restricted to non-recursive queries. 4.1
NRMD¬ Syntax
Assume three disjoint sets: variables, constants and predicate names. A term is either a variable or a constant. An atom is an expression 𝑝 (𝑡 1, . . . , 𝑡𝑛 ) where 𝑝 is a predicate name and each 𝑡𝑖 is a term. An equality expression will be represented by an atom of the form 𝑒𝑞(𝑡 1, 𝑡 2 ). A literal is either an atom (i.e. a positive literal 𝐴) or the negation of an atom (i.e. a negative literal ¬𝐴). Given a literal 𝐿, we use var(𝐿) to denote the variables in 𝐿. A Horn Clause, or simply clause, is an expression containing at most one positive literal. There are three types of clauses, named facts, rules and goals. A fact is a positive literal that does not contain any variables. A MD¬ Database is a finite multiset of facts. The vocabulary of a MD¬ database 𝐷 is a pair (𝑃, 𝛼) where 𝑃 is the set of predicate names occurring in the facts of 𝐷, and 𝛼 is a function defining the arity of each predicate name in 𝑃, i.e. if 𝑝 (𝑐 1, . . . , 𝑐𝑛 ) ∈ 𝐷 then 𝛼 (𝑝) = 𝑛. The predicate names occurring in 𝐷 are called extensional. A program Π is a finite set of rules. A rule is an expression 𝐿𝑛+1 ← 𝐿1, . . . , 𝐿𝑛 where 𝐿𝑛+1 is a positive literal with no constants called the head, and 𝐿1, . . . , 𝐿𝑛 (𝑛 ≥ 1) is a set of literals called the body. The predicate names occurring in the head of the rules of Π are called intensional.
12
Angles et al.
𝑟 (𝑎)
𝑟 (𝑎)
𝑝 (𝑎)
𝑝 (𝑎)
𝐹
𝐹
𝑆
𝑆
𝑅
𝑟 (𝑎)
𝑟 (𝑎)
𝑟 (𝑎)
𝑝 (𝑎)
𝑟 (𝑎)
𝑝 (𝑎)
𝑟 (𝑎)
𝑝 (𝑎)
𝑟 (𝑎)
𝑝 (𝑎)
𝐹
𝐹
𝐹
𝑆
𝐹
𝑆
𝐹
𝑆
𝐹
𝑆
⟨𝑟 (𝑎), 1⟩ ⟨𝑟 (𝑎), 2⟩
𝑞(𝑎)
⟨𝑟 (𝑎), 1⟩ ⟨𝑟 (𝑎), 2⟩ ⟨𝑟 (𝑎), 1⟩
𝑞(𝑎) 𝑅
𝑅
𝑟 (𝑎) 𝐹
⟨𝑟 (𝑎), 1⟩
⟨𝑟 (𝑎), 1⟩
𝑞(𝑎) 𝑅
𝑅
𝑟 (𝑎) 𝐹
⟨𝑟 (𝑎), 2⟩
⟨𝑟 (𝑎), 2⟩
𝑞(𝑎) 𝑅
𝑅
𝑟 (𝑎) 𝐹
⟨𝑟 (𝑎), 1⟩
⟨𝑟 (𝑎), 2⟩
𝑅
𝑟 (𝑎) 𝐹
⟨𝑟 (𝑎), 2⟩
Fig. 3. Example of derivation trees. Let 𝐷 be a NRMD¬ database, 𝐹 = 𝑟 (𝑎) be a fact in 𝐷 with card(𝐹, 𝐷) = 2, Π = {𝑅, 𝑆 } be a NRMD¬ program where 𝑅 is the rule 𝑞(𝑋 ) ← 𝑟 (𝑋 ), 𝑝 (𝑋 ) and 𝑆 is the rule 𝑝 (𝑋 ) ← 𝑟 (𝑋 ). This figure shows the derivation trees of Π with respect to 𝐷.
A variable 𝑋 occurs positively in a rule 𝑅 if and only if 𝑋 occurs in a positive literal in the body of 𝑅. A rule 𝑅 is said to be safe if all its variables occur positively. Additionally, we will assume that every literal in the body of a rule has a variable at least. A program is safe if all its rules are safe. A MD¬ program is a safe program. The dependency graph of a program Π is a digraph (𝑁 , 𝐸) where the set of nodes 𝑁 is the set of predicates names that occur in the literals of Π, and there is an edge (𝑝 1, 𝑝 2 ) in 𝐸 if there is a rule in Π whose body contains the predicate name 𝑝 1 , and whose head contains the predicate name 𝑝 2 . A program is said to be non-recursive if its dependency graph is acyclic. A NRMD¬ program is a MD¬ that is non-recursive. A goal clause is an atom without constants. A MD¬ query is a pair (𝐿, Π) where 𝐿 is a goal clause, and Π is a MD¬ program. A NRMD¬ query is a MD¬ query (𝐿, Π) such that Π is non-recursive. A NRMD¬ database is a MD¬ database. We assume that extensional and intensional predicate symbols are disjoint; in particular, a predicate cannot appear both as a fact in the database and as the head of a rule. 4.2
NRMD¬ Semantics
We follow the formalisms by Mumick et al. [37] and Bertossi et al. [9] that use a proof-theoretic semantics for NRMD¬ programs. The semantics is based on the notions of “substitution” and “derivation tree”. A substitution is a partial function 𝜃 from variables to constants. Given a literal 𝐿 (positive or negative), and a substitution 𝜃 , we write 𝜃 (𝐿) to denote the literal 𝐿 ′ obtained by replacing all variables 𝑥 occurring in 𝐿 with 𝜃 (𝑥). Informally, the answer for a query (𝐿, Π) where Π is a NRMD¬ program, over a database 𝐷, will be a multiset of substitutions with the same domain, each obtained from one proof showing that this substitution works. We will use the notion of “colored set” [37] to identify the different copies of an element, as introduce in Section 2.1. The notion of “derivation tree” [37] will be used to count the number of proofs for an atom. Formally, a Derivation Tree is a connected, undirected graph, with no cycles, represented as a tuple T = (N, E, L, 𝜖, 𝜆) where N is a set of nodes, E is a set of edges, L is a set of labels (for nodes and edges), 𝜖 : E → N × N is a total function that assigns a pair of nodes to each edge, and 𝜆 : (N ∪ E) → L is a total function that assigns a label to each node and edge. The function 𝑟𝑜𝑜𝑡 (T ) will be used to obtain the root node of T . Let 𝑅 be a rule of the form 𝐿𝑛+1 ← 𝐿1, . . . , 𝐿𝑚 , 𝐿𝑚+1, . . . , 𝐿𝑛 where 𝐿1, . . . , 𝐿𝑚 are positive literals, and 𝐿𝑚+1, . . . , 𝐿𝑛 are negative literals, 𝐷𝑇 be a set of derivation trees, and 𝑆𝑇 = (T1, . . . , T𝑚 ) be a sequence of derivation trees that satisfy that every derivation tree in 𝑆𝑇 is also in 𝐷𝑇 . We say that 𝐷𝑇 matches 𝑅 with 𝑆𝑇 , denoted 𝐷𝑇 |=𝑆𝑇 𝑅, if there is a substitution 𝜃 satisfying: (i) for every
The multiset semantics of SPARQL patterns
13
positive literal 𝐿𝑖 ∈ 𝑅 it applies that 𝜃 (𝐿𝑖 ) = 𝑟𝑜𝑜𝑡 (T𝑖 ) where T𝑖 ∈ 𝑆𝑇 ; and (ii) for every negative literal 𝐿 𝑗 ∈ it applies that 𝐷𝑇 does not contain a derivation tree whose root node has the label 𝜃 (𝐿 𝑗 ). Assume that 𝐷𝑇 |=𝑆𝑇 𝑅 where 𝑆𝑇 = (T1, . . . , T𝑚 ), T1 = (N1, E1, L1, 𝜖1, 𝜆1 ), . . . , and T𝑚 = (N𝑚 , E𝑚 , L𝑚 , 𝜖𝑚 , 𝜆𝑚 ). The derivation tree T𝑅 = (N𝑅 , E𝑅 , L𝑅 , 𝜖𝑅 , 𝜆𝑅 ) for the rule 𝑅 is defined as follows: N𝑅 = {𝑛𝑟 }∪N1 ∪· · ·∪N𝑚 , E𝑅 = {𝑒 1, . . . , 𝑒𝑚 }∪E1 ∪· · ·∪E𝑚 , L𝑅 = {𝑅, 𝜃 (𝐿𝑛+1 )}∪L1 ∪· · ·∪L𝑚 , every assignment in 𝜖𝑖 is also in 𝜖𝑅 , 𝜖𝑅 (𝑒 1 ) = (𝑛𝑟 , 𝑟𝑜𝑜𝑡 (T1 )), . . . , 𝜖𝑅 (𝑒𝑚 ) = (𝑛𝑟 , 𝑟𝑜𝑜𝑡 (T𝑚 )), every assignment in 𝜆𝑖 is also in 𝜆𝑅 , 𝜆𝑅 (𝑛𝑟 ) = 𝜃 (𝐿𝑛+1 ), and 𝜆𝑅 (𝑒 1 ) = 𝑅. Let 𝐷 be a NRMD¬ database and Π a NRMD¬ program. The set of derivation trees of Π with respect to 𝐷, denoted dt(Π, 𝐷), is defined as follows: (1) For every fact 𝐹 ∈ 𝐷 of the form 𝑝 (𝑡𝑖 , . . . , 𝑡𝑛 ), and for every colored copy ⟨𝑝 (𝑡𝑖 , . . . , 𝑡𝑛 ), 𝑖⟩, it applies that dt(Π, 𝐷) contains a derivation tree T𝐹𝑖 = (N𝐹𝑖 , E𝐹𝑖 , L𝐹𝑖 , 𝜖𝐹𝑖 , 𝜆𝑖𝐹 ) where N𝐹𝑖 = {𝑛 1, 𝑛 2 }, E𝐹𝑖 = {𝑒 1 }, L𝐹𝑖 = {𝑝 (𝑡𝑖 , . . . , 𝑡𝑛 ), ⟨𝑝 (𝑡𝑖 , . . . , 𝑡𝑛 ), 𝑖⟩, 𝐹 }, 𝜖𝐹𝑖 (𝑒 1 ) = (𝑛 1, 𝑛 2 ), 𝜆𝑖𝐹 (𝑛 1 ) = 𝑝 (𝑡𝑖 , . . . , 𝑡𝑛 ), 𝜆𝑖𝐹 (𝑛 2 ) = ⟨𝑝 (𝑡𝑖 , . . . , 𝑡𝑛 ), 𝑖⟩, and 𝜆𝑖𝐹 (𝑒 1 ) = 𝐹 . (2) Assume that 𝐷𝑇 is the set of derivation trees obtained for the facts in 𝐷 as defined above. Given a rule 𝑅 in Π and a sequence of derivation trees 𝑆𝑇 satisfying 𝐷𝑇 |=𝑆𝑇 𝑅, the derivation tree for 𝑅 is added to 𝐷𝑇 . This process is repeated for every rule 𝑅 in Π, until no more derivation trees are generated. Finally, dt(Π, 𝐷) = 𝐷𝑇 . Let Π be a NRMD¬ program, 𝐷 be a NRMD¬ database and 𝐹 be a fact. A derivation tree T ∈ dt(Π, 𝐷) is said to be a proof for the fact 𝐹 if the label of the root node is 𝐹 . The multiset of atoms of Π in 𝐷, denoted atoms(Π, 𝐷), is the multiset of facts 𝐹 such that there is a proof for 𝐹 in dt(Π, 𝐷), and the cardinality of 𝐹 in atoms(Π, 𝐷) is the number of proofs of 𝐹 . Figure 3 shows the derivation trees that are proofs of the facts derived from an example NRMD¬ program. The facts 𝑟 (𝑎), 𝑝 (𝑎) and 𝑞(𝑎) belong to atoms(Π, 𝐷) with cardinalities 2, 2, and 4. The NRMD¬ query language over a vocabulary 𝜏 is the query language (Q, D, S, J·K· ) where: (1) Q is the set of NRMD¬ queries over 𝜏; (2) D is the set of NRMD¬ databases over 𝜏; (3) S is the set of NRMD¬ query answers (i.e., pairs (𝑉 , 𝑀) where 𝑉 is a set of variables and 𝑀 is a multiset of substitutions 𝜃 with dom(𝜃 ) = 𝑉 ); and (4) J·K is the function that receives a NRMD¬ query (𝐿, Π) and a NRMD¬ database 𝐷, and returns a NRMD¬ query answer (𝑉 , 𝑀) where 𝑉 = var(𝐿), set(𝑀) = {𝜃 | 𝜃 (𝐿) ∈ atoms(Π, 𝐷) and dom(𝜃 ) = 𝑉 }, and card(𝜃, 𝑀) = card(𝜃 (𝐿), atoms(Π, 𝐷)). Observe that the domain of the query answer for a query (𝐿, Π) is var(𝐿). Abusing notation, we will say that it is also the domain of the query (𝐿, Π), denoted dom((𝐿, Π)) = var(𝐿). The Multiset Datalog query language presented here, NRMD¬ , differs from the version proposed by Bertossi et al. [9] in that we do not allow recursive programs nor constants in the head of rules. These restrictions permit to match the expressive power of the SPARQL fragment studied here. 4.3
Normalization of NRMD¬ programs
To simplify the translations from NRMD¬ to SPARQL and 𝑀𝑅𝐴, we assume that every NRMD¬ query is normalized into a query that contains only rules of the three following types: 𝐿0 ← 𝐿1 , where var(𝐿0 ) ⊆ var(𝐿1 ); (projection rule) 𝐿0 ← 𝐿1 , 𝐿2 , where var(𝐿0 ) = var(𝐿1 ) ∪ var(𝐿2 ); (join rule) 𝐿0 ← 𝐿1, ¬𝐿2 , where var(𝐿2 ) = var(𝐿1 ) and var(𝐿0 ) = var(𝐿1 ). (negation rule) Next, we show the feasibility of this normalization. Lemma 4.1. Every NRMD¬ query is equivalent to a normalized NRMD¬ query.
14
Angles et al.
Proof. We provide a normalization algorithm that replaces every rule in the query by a set of rules that do not change the semantics of the query. Given a NRMD¬ query (𝐿, Π), every rule 𝑅 ∈ Π has the form 𝑝 (𝑋¯ ) ← 𝐴1, . . . , 𝐴𝑚 , ¬𝐵 1, . . . , ¬𝐵𝑛 , where 𝐴1, . . . , 𝐴𝑚 are positive literals, and ¬𝐵 1, . . . , ¬𝐵𝑛 are negative literals. For 1 ≤ 𝑖 ≤ 𝑚, let 𝑌¯𝑖 be the set of variables that consists of the variables occurring in the atoms 𝐴1, . . . , 𝐴𝑖 . Then, we replace rule 𝑅 by the minimal set of rules Π𝑅 that includes the following rules: (1) Rules 𝑅𝑖𝐴 , for 2 ≤ 𝑖 ≤ 𝑚, defined recursively as follows: (a) 𝑅2𝐴 = 𝑞𝐴2 (𝑌¯2 ) ← 𝐴1, 𝐴2 . (b) 𝑅𝑖𝐴 = 𝑞𝑖𝐴 (𝑌¯𝑖 ) ← 𝑞𝑖𝐴−1 (𝑌¯𝑖 −1 ), 𝐴𝑖 . ′ (2) Rules 𝑅 𝐵𝑗 and 𝑅 𝐵𝑗 for 1 ≤ 𝑗 ≤ 𝑛, defined recursively as follows: 𝐴 (𝑌¯ ), (a) 𝑅0𝐵 = 𝑟 0𝐵 (𝑌¯𝑚 ) ← 𝑞𝑚 𝑚 𝐵 𝐵 𝐵 ¯ (b) 𝑅 𝑗 = 𝑟 𝑗 (𝑌𝑚 ) ← 𝑟 𝑗 −1 (𝑌¯𝑚 ), ¬𝐵 ′𝑗 (𝑌¯𝑚 ), ′ (c) 𝑅 𝐵𝑗 = 𝐵 ′𝑗 (𝑌¯𝑚 ) ← 𝑟 𝐵𝑗−1 (𝑌¯𝑚 ), 𝐵 𝑗 . (3) A rule 𝑅 ′ = 𝑝 (𝑋¯ ) ← 𝑟𝑛𝐵 (𝑌¯𝑚 ). Let (𝐿, Π ′ ) be the query resulting from replacing rule 𝑅 with the rules in Π𝑅 . It is clear that the program is normal (recall that the original program is safe). Need to show that both programs are equivalent, that is, that the solutions of query (𝑝 (𝑋¯ ), Π) after the replacement are the same and have the same cardinalities. These two conditions follow from Claim 3 in the Appendix. □ 5
Multiset Relational Algebra (MRA)
The multiset relational algebra used in this paper is based on the semantics defined by Dayal et al. [17]. This algebra considers the operations of selection, projection, natural join and arithmetic union. Additionally, we include operators for renaming and filter difference (or “except”). 5.1
Multiset relations
Assume that N, A, C are disjoint infinite sets, where N is the domain of relation names, A is the domain of attributes, and C is the domain of constants or values. A relation schema is given by a relation name 𝑅 ∈ N and a set of attributes {𝐴1, . . . , 𝐴𝑛 } where 𝐴𝑖 ∈ A for 1 ≤ 𝑖 ≤ 𝑛. To simplify the notation, we will use the relation name 𝑅 to denote the relation schema, and 𝑅b to denote the attributes of 𝑅. A relational database schema is a finite set of relation schemas. A tuple over a relation schema 𝑅 with attributes 𝑅b = {𝐴1, . . . , 𝐴𝑛 } is a total mapping 𝑡 from 𝑅b to C. The value of tuple 𝑡 on an attribute 𝐴𝑖 ∈ 𝑅b will be denoted as 𝑡 (𝐴𝑖 ). Given a set of attributes 𝑈 ⊆ 𝑅b and a tuple 𝑡, we write 𝑡 [𝑈 ] to denote the tuple 𝑡 ′ with attributes 𝑈 such that 𝑡 ′ (𝐴) = 𝑡 (𝐴) for every attribute 𝐴 ∈ 𝑈 . b We write 𝑟ˆ to denote A multiset relation 𝑟 over a relation schema 𝑅 is a multiset of tuples over 𝑅. the relation schema 𝑅 where the multiset relation 𝑟 is defined. Given a tuple 𝑡 ∈ 𝑟 , we will use card(𝑡, 𝑟 ) to denote the cardinality of tuple 𝑡 in 𝑟 . A relational database schema is a set of relation schemas. Given a relational database schema 𝑇 = {𝑅1, . . . , 𝑅𝑛 }, a multiset relational database over 𝑇 is a set of multiset relations {𝑟 1, . . . , 𝑟𝑛 } where each relation 𝑟𝑖 is defined over the schema 𝑅𝑖 . Sometimes we will write MRA database, emphasizing that the multiset relational database is in the context of MRA. Let 𝑟 1, 𝑟 2 be two multiset relations, and 𝑡 1 ∈ 𝑟 1 and 𝑡 2 ∈ 𝑟 2 be tuples. We say that 𝑡 1 and 𝑡 2 are compatible, denoted 𝑡 1 ∼ 𝑡 2 , if (i) for every attribute 𝐴 ∈ 𝑟ˆ1 ∩ 𝑟ˆ2 it holds that 𝑡 1 (𝐴) = 𝑡 2 (𝐴), or (ii) 𝑟ˆ1 ∩ 𝑟ˆ2 = ∅. If 𝑡 1 and 𝑡 2 are compatible, then the merge of them, denoted 𝑡 1 ∪ 𝑡 2 , is the tuple 𝑡 with
The multiset semantics of SPARQL patterns
15
attributes 𝑟ˆ1 ∪ 𝑟ˆ2 where 𝑡 (𝐴) = 𝑡 1 (𝐴) for each attribute 𝐴 ∈ 𝑟ˆ1 , and 𝑡 (𝐵) = 𝑡 2 (𝐵) for each attribute 𝐵 ∈ 𝑟ˆ2 \ 𝑟ˆ1 . 5.2
Syntax of MRA
The multiset relational algebra defined in this paper includes the operators of selection (𝜎), projection (𝜋), renaming (𝜌), join (Z), union (∪), and except (\). Next we describe the syntax of MRA expressions containing the above operators. A selection formula 𝜓 is a Boolean combination of equality expressions of the form 𝑥 = 𝑦 where 𝑥, 𝑦 ∈ A ∪ C. We define a MRA expression 𝐸 over a relational database schema 𝑇 , and the attributes b by mutual recursion as follows: of 𝐸, denoted 𝐸, b • A relation name 𝑅 ∈ 𝑇 is a MRA expression 𝐸, and 𝐸b = 𝑅. • If 𝐸 1 is a MRA expression and 𝜓 is a selection formula where the attributes occurring in 𝜓 are included in 𝐸b1 , then 𝜎𝜓 (𝐸 1 ) is a MRA expression 𝐸, and 𝐸b = 𝐸b1 . • If 𝐸 1 is a MRA expression and 𝑆 ⊆ 𝐸b1 is a set of attributes, them 𝜋𝑆 (𝐸 1 ) is a MRA expression 𝐸, and 𝐸b = 𝑆. • If 𝐸 1 is a MRA expression, 𝐴 ∈ 𝐸b1 and 𝐵 ∈ A are attributes, then 𝜌𝐴/𝐵 (𝐸 1 ) is a MRA expression 𝐸, and 𝐸b = (𝐸b1 \ 𝐴) ∪ 𝐵. • If 𝐸 1 and 𝐸 2 are MRA expressions, then (𝐸 1 Z 𝐸 2 ) is an MRA expression 𝐸, and 𝐸b = 𝐸b1 ∪ 𝐸b2 . • If 𝐸 1 and 𝐸 2 are MRA expressions and 𝐸b1 = 𝐸b2 , then (𝐸 1 \ 𝐸 2 ) is a MRA expression 𝐸, and 𝐸b = 𝐸b1 . • If 𝐸 1 and 𝐸 2 are MRA expressions and 𝐸b1 = 𝐸b2 , then (𝐸 1 ∪ 𝐸 2 ) is a MRA expression 𝐸, and 𝐸b = 𝐸b1 . Note that a selection operation 𝜎𝜓 (𝐸 1 ) requires that attributes in the selection formula 𝜓 be attributes of the MRA expression 𝐸 1 ; the projection operation 𝜋𝑆 (𝐸 1 ) requires that 𝑆 be a subset of the attributes of the MRA expression 𝐸 1 ; and that the union 𝐸 1 ∪ 𝐸 2 and difference 𝐸 1 \ 𝐸 2 expressions require that expressions 𝐸 1 and 𝐸 2 have the same set of attributes. 5.3
Semantics of MRA
Given a selection formula 𝜓 and a tuple 𝑡 over a relation schema 𝑅, we will use 𝑡 |= 𝜓 to denote that 𝑡 satisfies 𝜓 , and its evaluation is given as follows: (1) if 𝜓 is 𝐴 = 𝐵 where 𝐴, 𝐵 ∈ 𝑅b are attributes, then 𝑡 |= 𝜓 iff 𝑡 (𝐴) = 𝑡 (𝐵); (2) if 𝜓 is 𝐴 = 𝑐 where 𝐴 ∈ 𝑅b is an attribute and 𝑐 ∈ C is a constant, then 𝑡 |= 𝜓 iff 𝑡 (𝐴) = 𝑐; (3) if 𝜓 is 𝑐 1 = 𝑐 2 where 𝑐 1, 𝑐 2 ∈ C are constants, then 𝑡 |= 𝜓 iff 𝑐 1 is the same constant as 𝑐 2 ; (4) if 𝜓 is 𝜓 1 ∧ 𝜓 2 , then 𝑡 |= 𝜓 iff 𝑡 |= 𝜓 1 and 𝑡 |= 𝜓 2 ; (5) if 𝜓 is 𝜓 1 ∨ 𝜓 2 , then 𝑡 |= 𝜓 iff 𝑡 |= 𝜓 1 or 𝑡 |= 𝜓 2 ; (6) if 𝜓 is ¬𝜓 1 , then 𝑡 |= 𝜓 iff 𝑡 |= 𝜓 1 does not hold. Now, the evaluation of a MRA expression 𝐸 over a multiset relational database 𝐷 (of the same schema as 𝐸) is defined as a function Eval(𝐸, 𝐷) that returns a multiset relation 𝑟 with the same schema as 𝐸. Let 𝐷 be a MRA database over a schema𝑇 and 𝐸, 𝐸 1, 𝐸 2 be MRA expressions over𝑇 . The evaluation of Eval(𝐸, 𝐷) is the multiset relation 𝑟 defined recursively as follows (assume that Eval(𝐸 1, 𝐷) = 𝑟 1 , and Eval(𝐸 2, 𝐷) = 𝑟 2 ): • If 𝐸 is a relation name 𝑅1 ∈ 𝑇 , then 𝑟 is the relation for the relation name 𝑅1 in the database 𝐷. • If 𝐸 is 𝜎𝜓 (𝐸 1 ) then set(𝑟 ) = {𝑡 | 𝑡 ∈ 𝑟 1 and 𝑡 |= 𝜓 } and card(𝑡, 𝑟 ) = card(𝑡, 𝑟 1 ).
16
Angles et al.
• If 𝐸 is 𝜋𝑆 (𝐸 1 ) then set(𝑟 ) = {𝑡 ′ | 𝑡 ′ = 𝑡 [𝑆] and 𝑡 ∈ 𝑟 1 } and ∑︁ card(𝑡 ′, 𝑟 ) = card(𝑡, 𝑟 1 ). 𝑡 with 𝑡 [𝑆 ]=𝑡 ′
• If 𝐸 is 𝜌𝐴/𝐵 (𝐸 1 ) then 𝑟 is the result from renaming in 𝑟 1 the attribute 𝐴 as 𝐵. • If 𝐸 is (𝐸 1 Z 𝐸 2 ) then set(𝑟 ) = {𝑡 1 ∪ 𝑡 2 | 𝑡 1 ∈ 𝑟 1, 𝑡 2 ∈ 𝑟 2, and 𝑡 1 ∼ 𝑡 2 } and card(𝑡 1 ∪ 𝑡 2, 𝑟 ) = card(𝑡 1, 𝑟 1 ) × card(𝑡 2, 𝑟 2 ). • If 𝐸 is (𝐸 1 ∪ 𝐸 2 ) then set(𝑟 ) = {𝑡 | 𝑡 ∈ 𝑟 1 or 𝑡 ∈ 𝑟 2 } and card(𝑡, 𝑟 ) = card(𝑡, 𝑟 1 ) + card(𝑡, 𝑟 2 ). • If 𝐸 is (𝐸 1 \ 𝐸 2 ) then set(𝑟 ) = {𝑡 | 𝑡 ∈ 𝑟 1 and 𝑡 ∉ 𝑟 2 } and card(𝑡, 𝑟 ) = card(𝑡, 𝑟 1 ). Hence, in MRA, the set of queries is the set of MRA expressions, the set of databases is the set of multiset relational databases, the set of results is the set of multiset relations, and the evaluation procedure is the aforementioned function Eval. 6
Equivalence between SPARQL and NRMD¬
This section presents the simulations that prove that SPARQL and Non-Recursive Multiset Datalog with Safe Negation (NRMD¬ ) have the same expressive power. Specifically, we show that SPARQL can be simulated by NRMD¬ (Section 6.1), and NRMD¬ can be simulated by SPARQL (Section 6.2). 6.1
From SPARQL to NRMD¬
This section shows that SPARQL can be simulated by Non-Recursive Multiset Datalog with Safe Negation (NRMD¬ ). To support this, we describe the following translation functions: • function 𝑓12 , that translates SPARQL queries into NRMD¬ queries; • function 𝑔12 , that translates SPARQL databases into NRMD¬ databases; and • function ℎ 12 , that translates NRMD¬ query answers into SPARQL query answers. 6.1.1 Translating databases from SPARQL to NRMD¬ . Recall that a SPARQL database is a set of RDF triples and a NRMD¬ database is a multiset of facts. Given this, the basic idea is to translate each RDF triple into a Datalog atom. Additionally, we create an atom to encode all RDF terms, and an atom to encode the unbound value. Definition 6.1 (Function 𝑔12 ). Let ⊥ be an RDF term which will exclusively used to encode the unbounded value. Let (𝑃, 𝛼) be a Datalog vocabulary where 𝑃 = {term, eq, comp, triple, null} and 𝛼 (term) = 1, 𝛼 (eq) = 2, 𝛼 (comp) = 3, 𝛼 (triple) = 3, and 𝛼 (null) = 1. Given an RDF graph 𝐺, the function 𝑔12 (𝐺) returns a NRMD¬ database 𝐷 with vocabulary (𝑃, 𝛼), where the multiset of facts in 𝐷 is defined as follows: • for each RDF term 𝑡 in terms(𝐺) (i.e. the set of terms in 𝐺) , 𝐷 contains the facts term(𝑡), eq(𝑡, 𝑡), comp(𝑡, 𝑡, 𝑡), comp(𝑡, ⊥, 𝑡), comp(⊥, 𝑡, 𝑡); • for each RDF triple (𝑠, 𝑝, 𝑜) ∈ 𝐺, 𝐷 contains the fact triple(𝑠, 𝑝, 𝑜); • 𝐷 contains the fact comp(⊥, ⊥, ⊥); • 𝐷 contains the fact null(⊥) ∈ 𝐷. Intuitively: a fact of the form term(𝑡) is used to represent that 𝑡 is an RDF term; a fact of the form eq(𝑡, 𝑡) is used to represent the equality of the RDF term 𝑡; for each term 𝑡 in 𝐺, the notion of compatible is represented by the facts comp(𝑡, 𝑡, 𝑡), comp(𝑡, ⊥, 𝑡) and comp(⊥, 𝑡, 𝑡); a fact of the form triple(𝑠, 𝑝, 𝑜) is used to represent an RDF triple (𝑠, 𝑝, 𝑜). Example 6.2. Let 𝐺 be the RDF graph defined as follows 𝐺 = {(Alice, livesIn, Santiago), (Alice, knows, Bob), (Bob, livesIn, Santiago), (Bob, knows, Carol), (Carol, livesIn, Lima)}.
The multiset semantics of SPARQL patterns
17
The translation of the above RDF triples into Datalog facts is given as follows: 𝑔12 (𝐺) = ⦃
term(Alice), eq(Alice, Alice), comp(Alice, Alice, Alice), comp(Alice, ⊥, Alice), comp(⊥, Alice, Alice), .. . term(Lima), eq(Lima, Lima), comp(Lima, Lima, Lima), comp(Lima, ⊥, Lima), comp(⊥, Lima, Lima), triple(Alice, livesIn, Santiago), triple(Alice, knows, Bob), triple(Bob, livesIn, Santiago), triple(Bob, knows, Carol), triple(Carol, livesIn, Lima), comp(⊥, ⊥, ⊥), null(⊥) ⦄.
6.1.2 Translating queries from SPARQL to NRMD¬ . In general terms, any SPARQL graph pattern can be translated into a set of NRMD¬ rules. However, there are some subtleties that need to be discussed before presenting the general translation rules. An initial issue is the translation of a filter graph pattern 𝑃 = (𝑃 1 FILTER 𝜑) where 𝜑 is a complex filter condition. In order to simplify the translation to Datalog, we need to transform 𝑃 into a collection of filter graph patterns where every filter condition is an atomic filter condition. Consider the following equivalences: (𝑃1 FILTER 𝜑 1 ∧ 𝜑 2 ) ≡ ((𝑃1 FILTER 𝜑 1 ) FILTER 𝜑 2 ).
(1)
(𝑃1 FILTER 𝜑 1 ∨ 𝜑 2 ) ≡ (𝑃1 FILTER 𝜑 1 ) UNION(𝑃1 FILTER 𝜑 2 ).
(2)
(𝑃1 FILTER ¬(𝜑 1 )) ≡ (𝑃1 EXCEPT(𝑃 1 FILTER 𝜑 1 )).
(3)
Intuitively, these equivalences seem to be true, since similar equivalences are valid in set relational algebra, namely 𝜎𝜑1 ∧𝜑2 (𝑅) = 𝜎𝜑1 (𝜎𝜑2 (𝑅)), 𝜎𝜑1 ∨𝜑2 (𝑅) = 𝜎𝜑1 (𝑅) ∪ 𝜎𝜑2 (𝑅), and 𝜎¬𝜑1 (𝑅) = 𝑅 \ 𝜎𝜑1 (𝑅). Under set semantics, these three equivalences are valid. However, under bag semantics, just equivalence (1) is valid, and equivalences (2) and (3) present problems. Let us analyze them and provide valid equivalences. • To see why equivalence (2) is not valid, consider the case where for a solution 𝜇 of the pattern 𝑃1 the evaluation of formulas 𝜑 1 and 𝜑 2 are true. Then, 𝜇 is a solution of the queries in both sides of equivalence (2). However, the cardinality differs. Indeed, the cardinality of 𝜇 for the query on the right side is twice the cardinality for the query on the left side. Hence, equivalence (2) is valid for set semantics but not for bag semantics. • To see why equivalence (3) is not valid, consider the case where for a solution mapping 𝜇 of the pattern 𝑃1 , formula 𝜑 1 produces an error. Then, formula ¬𝜑 1 also produces an error, and hence 𝜇 is not a solution to the query on the left side. On the other hand, since 𝜇 is a solution mapping for 𝑃1 but not a solution to the pattern (𝑃 1 FILTER 𝜑 1 ), 𝜇 is a solution mapping to the query on the right side. Hence, this equivalency is not valid because the queries do not have the same solution mappings. Intuitively, equivalence (2) is no longer valid when we change from set semantics to bag semantics, whereas equivalence (3) is no longer valid when we change from 2-valued logic to 3-valued logic. In the following, we show how to solve these problems. Lemma 6.3 (Rewriting of disjoint filter conditions). We say that two filter conditions 𝜑 1 and 𝜑 2 are disjoint, if for every mapping 𝜇 it does not hold that 𝜇 (𝜑 1 ) and 𝜇 (𝜑 2 ) are simultaneously true. Equivalence (2) is true when 𝜑 1 and 𝜑 2 are disjoint.
18
Angles et al.
Proof. Given that 𝜑 1 and 𝜑 2 are disjoint, it applies that 𝜇 (𝜑 1 ) is true when 𝜇 (𝜑 2 ) is not true (and vice versa). So, it holds that 𝜇 (𝜑 1 ∨ 𝜑 2 ) = true if and only if 𝜇 (𝜑 1 ) = true or 𝜇 (𝜑 2 ) = true, and the cardinality of 𝜇 on the left hand side is the sum of the cardinalities of 𝜇 in each of the terms of the right hand side. □ Now, consider the following equivalence: (𝑃 1 FILTER 𝜑 1 ∨ 𝜑 2 ) ≡(𝑃1 FILTER 𝜑 1 ∧ ¬𝜑 2 ) UNION (𝑃 1 FILTER ¬𝜑 1 ∧ 𝜑 2 ) UNION (𝑃 1 FILTER 𝜑 1 ∧ 𝜑 2 ).
(4)
Equivalence (4) solves one of the problems of equivalence (2), but it still has problems in evaluating formulas with errors. In order to solve them, we introduce the notion of “error filter condition.” Definition 6.4 (Error filter condition). Let 𝜑, 𝜑 1 and 𝜑 2 be filter conditions. The function Error(𝜑) returns a filter condition defined recursively as follows: • if 𝜑 is bound(?𝑋 ) then Error(𝜑) = false; • if 𝜑 is ?𝑋 = 𝑎 then Error(𝜑) = ¬ bound(?𝑋 ); • if 𝜑 is ?𝑋 = ?𝑌 then Error(𝜑) = (¬ bound(?𝑋 ) ∧ bound(?𝑌 )) ∨ (bound(?𝑋 ) ∧ ¬ bound(?𝑌 )) ∨ (¬ bound(?𝑋 ) ∧ ¬ bound(?𝑌 )); • if 𝜑 is 𝜑 1 ∧ 𝜑 2 then Error(𝜑) = (𝜑 1 ∧ Error(𝜑 2 )) ∨ (Error(𝜑 1 ) ∧ 𝜑 2 ) ∨ (Error(𝜑 1 ) ∧ Error(𝜑 2 )); • if 𝜑 is 𝜑 1 ∨ 𝜑 2 then Error(𝜑) = (¬𝜑 1 ∧ Error(𝜑 2 )) ∨ (Error(𝜑 1 ) ∧ ¬𝜑 2 ) ∨ (Error(𝜑 1 ) ∧ Error(𝜑 2 )); • if 𝜑 is ¬𝜑 1 then Error(𝜑) = Error(𝜑 1 ). Lemma 6.5. For every filter condition 𝜑 and mapping 𝜇 it holds that 𝜇 (𝜑) = error if and only if 𝜇 (Error(𝜑)) = true. Proof. This lemma is proved by induction on the structure of the filter condition (see Claim 1 in the appendix). □ Example 6.6. Let 𝜑 be the filter condition 𝐿 ∨ ¬𝐿 where 𝐿 is the equality ?𝑋 = 𝑎. According to Definition 6.4, Error(𝜑) will be the filter condition (¬𝐿 ∧ Error(¬𝐿)) ∨ (Error(𝐿) ∧ ¬¬𝐿) ∨ (Error(𝐿) ∧ Error(¬𝐿)). Since ¬¬𝐿 is equivalent to 𝐿 and Error(¬𝐿) is equivalent to Error(𝐿), then Error(𝜑) is equivalent to (¬𝐿 ∧ Error(𝐿)) ∨ (Error(𝐿) ∧ 𝐿) ∨ (Error(𝐿)), which is equivalent to (𝜑 ∧ Error(𝐿)) ∨ (Error(𝐿)), and then, equivalent to Error(𝐿). According to Definition 6.4, we conclude that Error(𝜑) is equivalent to ¬ bound(?𝑋 ). There are three possible values for variable ?𝑋 in a mapping 𝜇, namely 𝜇 (?𝑋 ) = 𝑎, 𝜇 (?𝑋 ) = 𝑏 (for a term 𝑏 ≠ 𝑎), and variable ?𝑋 is unbound in 𝜇 (denoted 𝜇 (?𝑋 ) = ⊥). The following table shows the values for 𝜇 (𝜑) and 𝜇 (Error(𝜑)) for these three cases. 𝜇 (?𝑋 )
𝜇 (𝜑)
𝜇 (Error(𝜑))
𝑎 𝑏 ⊥
true true error
false false true
As defined by Lemma 6.5, the filter condition 𝜑 produces error for mappings 𝜇 where 𝜇 (Error(𝜑)) = true, and 𝜇 (Error(𝜑)) is either true or false. Now we present an equivalence for the disjunction that works in all cases.
The multiset semantics of SPARQL patterns
19
Lemma 6.7 (Disjunction rewriting). Given two filter conditions 𝜑 1 and 𝜑 2 , and a pattern 𝑃, the following equivalence holds for bag semantics: (𝑃 FILTER 𝜑 1 ∨ 𝜑 2 ) ≡ (𝑃 FILTER 𝜑 1 ∧ 𝜑 2 ) UNION (𝑃 FILTER 𝜑 1 ∧ ¬𝜑 2 ) UNION (𝑃 FILTER ¬𝜑 1 ∧ 𝜑 2 ) UNION (𝑃 FILTER 𝜑 1 ∧ Error(𝜑 2 )) UNION (𝑃 FILTER Error(𝜑 1 ) ∧ 𝜑 2 ).
(5)
Proof. Since 𝜑 ∨ ¬𝜑 ∨ Error(𝜑) is a tautology for every filter condition 𝜑, the following equivalences hold: 𝜑 1 ≡ 𝜑 1 ∧ (𝜑 2 ∨ ¬𝜑 2 ∨ Error(𝜑 2 )) ≡ (𝜑 1 ∧ 𝜑 2 ) ∨ (𝜑 1 ∧ ¬𝜑 2 ) ∨ (𝜑 1 ∧ Error(𝜑 2 )), 𝜑 2 ≡ 𝜑 2 ∧ (𝜑 1 ∨ ¬𝜑 1 ∨ Error(𝜑 1 )) ≡ (𝜑 2 ∧ 𝜑 1 ) ∨ (𝜑 2 ∧ ¬𝜑 1 ) ∨ (𝜑 2 ∧ Error(𝜑 1 )). Hence, the following equivalence holds: 𝜑 1 ∨ 𝜑 2 ≡ (𝜑 1 ∧ 𝜑 2 ) ∨ (𝜑 1 ∧ ¬𝜑 2 ) ∨ (¬𝜑 1 ∧ 𝜑 2 ) ∨ (𝜑 1 ∧ Error(𝜑 2 )) ∨ (Error(𝜑 1 ) ∧ 𝜑 2 ). Since all filter conditions in the disjunction of the right side of this equivalence are disjoint, by Lemma 6.3, we got equivalence (5). □ Finally, we provide a translation for filter graph patterns which have a negation. Under twovalued logic, the evaluation of a pattern 𝑃 of the form (𝑃 1 FILTER ¬𝜑) may be understood as “all solutions 𝜇 of 𝑃1 except those where 𝜇 (𝜑) is true.” Under 3-valued logic, the evaluation of 𝑃 means “all solutions 𝜇 of 𝑃 except those where 𝜇 (𝜑) is true or 𝜇 (𝜑) is error.” Thus according to the latter meaning we have: Lemma 6.8 (Negation rewriting). Given a filter condition 𝜑, and a pattern 𝑃1 , the following equivalence holds: (𝑃1 FILTER ¬𝜑) ≡ ((𝑃1 EXCEPT (𝑃1 FILTER 𝜑)) EXCEPT (𝑃1 FILTER Error(𝜑))).
(6)
Proof. The equivalence follows from the fact that the filter discards from the solutions of 𝑃 those solutions 𝜇 such that 𝜇 (𝜑) is false or error. □ Now we are ready to present the effectiveness of rewriting that allows for the reduction of complex filter conditions. Definition 6.9 (Reduction of complex filter conditions). Given a pattern (𝑃 1 FILTER 𝜑), the filterreduced pattern of it is the pattern that results of applying recursively the equivalences (1), (5), and (6) until in the resulting patterns only occur atomic formulas (i.e. no logical connectives). Lemma 6.10. Given a pattern (𝑃 1 FILTER 𝜑), the procedure to reduce complex filter conditions described in Definition 6.9 produces a pattern equivalent to the original and with no logical connectives in filter conditions. Proof. This lemma is proved by induction on the structure of the filter condition in the pattern. The base case consists in a filter condition 𝜑 without logical connectives. The case where 𝜑 is 𝜑 1 ∧𝜑 2 is straightforward. The pattern (𝑃 FILTER 𝜑) can be reduced to the pattern ((𝑃 FILTER 𝜑 1 ) FILTER 𝜑 2 ), and the inductive hypothesis can be applied on 𝜑 1 and 𝜑 2 . The cases where 𝜑 is 𝜑 1 ∨ 𝜑 2 or ¬𝜑 1 are more involved because the application of the respective equivalences eliminates a logical connective from 𝜑 but adds new logical connectives to the resulting filter conditions. The proof for the cases involving disjunction or negation follows from Claim 2 in the appendix. □
20
Angles et al.
Table 4. Definition of function 𝛿 (𝑃) which allows to translate a SPARQL graph pattern 𝑃 into a set of NRMD¬ rules. Given a graph pattern 𝑃, the function 𝑃¯ returns the variables of 𝑃 in lexicographical order. Note that 𝑝𝑖 is a fresh predicate name used to codify the graph pattern 𝑃𝑖 . Graph pattern 𝑃
𝛿 (𝑃)
where ...
(𝑥 1, 𝑥 2, 𝑥 3 )
¯ ← 𝑡𝑟𝑖𝑝𝑙𝑒 (𝑥 1, 𝑥 2, 𝑥 3 ) 𝑝 (𝑃)
𝑃¯ contains the variables in the set {𝑥 1, 𝑥 2, 𝑥 3 }.
¯ ← 𝜈 1 (𝑝 1 (𝑃¯1 )), 𝜈 2 (𝑝 2 (𝑃¯2 )), 𝑝 (𝑃) {comp(𝜈 1 (𝑋 ), 𝜈 2 (𝑋 ), 𝑋 ) | 𝑋 ∈ 𝑃¯1 ∩ 𝑃¯2 } (𝑃1 AND 𝑃2 ) Assume that 𝜈 1 and 𝜈 2 are functions with 𝛿 (𝑃1 ) the same domain 𝑃¯1 ∩ 𝑃¯2 and with different 𝛿 (𝑃2 ) range. Given a literal 𝐿, the function 𝜈𝑖 (𝐿) returns a copy of 𝐿 where the variables have been renamed according to 𝑣𝑖 . ¯ ← 𝑝 1 (𝑃¯1 ) 𝑝 (𝑃) ¯ ← 𝑝 2 (𝑃¯2 ) 𝑝 (𝑃) ¯ 𝑃¯1 and 𝑃¯2 contain the same variables. 𝑃, (𝑃1 UNION 𝑃2 ) 𝛿 (𝑃1 ) 𝛿 (𝑃2 ) ¯ ← 𝑝 1 (𝑃¯1 ), ¬𝑝 2 (𝑃¯2 ) 𝑝 (𝑃) 𝛿 (𝑃1 ) (𝑃1 EXCEPT 𝑃2 ) 𝑃¯ and 𝑃¯1 contain the same variables. 𝛿 (𝑃2 ) ¯ ← 𝑝 1 (𝑃¯1 ), eq(𝑥 1, 𝑥 2 ) 𝑝 (𝑃) (𝑃1 FILTER 𝑥 1 = 𝑥 2 ) 𝑃¯ and 𝑃¯1 contain the same variables. 𝛿 (𝑃1 ) ¯ ← 𝑝 1 (𝑃¯1 ), term(?𝑋 ); 𝑝 (𝑃) (𝑃1 FILTER bound(?𝑋 )) 𝑃¯ and 𝑃¯1 contain the same variables. 𝛿 (𝑃1 ); ¯ ← 𝑝 1 (𝑃¯1 ), null(𝑥 1 ), . . . , null(𝑥𝑛 ) 𝑝 (𝑃) (SELECT 𝑊 𝑃1 ) 𝑃¯ = 𝑊 , and 𝑥 1, . . . , 𝑥𝑛 are the variables that 𝛿 (𝑃1 ) are in 𝑊 but not in inScope(𝑃 1 ).
Translation of SPARQL graph patterns into Datalog rules. The translation essentially follows the idea presented by Polleres [39], adapted to multisets by Angles and Gutierrez [6], and improved by Hernández [25]. Specifically, we cover the following issues: (1) It considers the cases where a filter condition is evaluated as error. Some solutions are lost when these cases are not considered. (2) It considers that the equality 𝑋 = 𝑌 must be evaluated as true only if 𝑋 and 𝑌 are bound. The translation is fixed by using the literal eq(𝑋, 𝑌 ) instead of a built-in equality 𝑋 = 𝑌 . Since, atom eq(𝑋, 𝑌 ) is true only if 𝑋 and 𝑌 are terms in the database, the translation of the filter-condition 𝑋 = 𝑌 is not evaluated as true when 𝑋 and 𝑌 are unbound. Given a SPARQL graph pattern 𝑃, the function 𝛿 (𝑃) is introduced to transform 𝑃 into a set of NRMD¬ rules. The definition of function 𝛿 is given by the translation rules presented in Table 4. Note that this function implies a recursive translation of graph patterns into Datalog rules, as well as the generation of fresh predicates. To do this, the application of function 𝛿 over a graph 𝑃 will be based on exploring the parse tree of 𝑃. A parse tree for a graph pattern 𝑃 is an ordered rooted tree whose internal nodes represent the graph pattern operators in 𝑃 (i.e., AND, UNION, EXCEPT, FILTER, SELECT), and whose leaves represent the triple patterns of 𝑃. Additionally, we will assume that the nodes will be enumerated using a post-order traversal of the tree, that is, for any node 𝑛, first visit the left subtree, then visit the right subtree, and finally visit the node 𝑛 itself.
The multiset semantics of SPARQL patterns
21
𝑃11 (UNION) 𝑃3 (AND)
𝑃10 (SELECT)
𝑃1
𝑃9 (EXCEPT)
𝑃2
𝑃4
𝑃 8 (SELECT) 𝑃7 (AND) 𝑃5
𝑃6
Fig. 4. Parse tree of the normalized SPARQL graph pattern presented in Example 6.11. Each node represents a graph pattern, and each edge represents a composition relationship between graph patterns. The nodes were enumerated using a post-order traversal of the tree. In this figure: 𝑃 1 is the triple pattern (?𝑝𝑒𝑟𝑠𝑜𝑛, livesIn, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒), 𝑃2 is (?𝑝𝑒𝑟𝑠𝑜𝑛, knows, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦), 𝑃3 is (?𝑝𝑒𝑟𝑠𝑜𝑛, livesIn, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒), 𝑃4 is (?𝑝𝑒𝑟𝑠𝑜𝑛, livesIn, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒), and 𝑃5 is (?𝑝𝑒𝑟𝑠𝑜𝑛, knows, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦).
Given a graph pattern 𝑃 and its parse tree 𝑇 , the execution of the function 𝛿 consists of applying the rules shown in Table 4 to each node in 𝑇 , following their enumeration. Next we present an example of this procedure. Example 6.11. Let 𝑄 be the following SPARQL query asking for all people, the place where they live, and optionally the people they know: (((?𝑝𝑒𝑟𝑠𝑜𝑛, livesIn, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒) AND (?𝑝𝑒𝑟𝑠𝑜𝑛, knows, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦)) UNION ((?𝑝𝑒𝑟𝑠𝑜𝑛, livesIn, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒) EXCEPT ( SELECT ?𝑝𝑒𝑟𝑠𝑜𝑛 ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒 WHERE ((?𝑝𝑒𝑟𝑠𝑜𝑛, livesIn, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒) AND (?𝑝𝑒𝑟𝑠𝑜𝑛, knows, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦))))). This SPARQL query is not normalized because both sides of the UNION operator have different variables. To normalize this query, we replace the right-hand operand of the UNION pattern by a SELECT clause, and include the projection variables ?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒 and ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦. The resulting normalized query is the following: (((?𝑝𝑒𝑟𝑠𝑜𝑛, livesIn, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒) AND (?𝑝𝑒𝑟𝑠𝑜𝑛, knows, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦)) UNION ( SELECT ?𝑝𝑒𝑟𝑠𝑜𝑛 ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒 ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦 WHERE ((?𝑝𝑒𝑟𝑠𝑜𝑛, livesIn, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒) EXCEPT ( SELECT ?𝑝𝑒𝑟𝑠𝑜𝑛 ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒 WHERE ((?𝑝𝑒𝑟𝑠𝑜𝑛, livesIn, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒) AND (?𝑝𝑒𝑟𝑠𝑜𝑛, knows, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦)))))). Assume that 𝑃 is the normalized SPARQL graph pattern presented above, and 𝑇 is the parse tree of 𝑃 shown in Figure 4. Hence, the application of the function 𝛿 (𝑃) results in the following Datalog rules: 𝑝 1 (?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒) ← triple(?𝑝𝑒𝑟𝑠𝑜𝑛, livesIn, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒) 𝑝 2 (?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦) ← triple(?𝑝𝑒𝑟𝑠𝑜𝑛, knows, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦) 𝑝 3 (?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦) ← 𝑝 1 (?𝑝𝑒𝑟𝑠𝑜𝑛 1, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒),
22
Angles et al.
𝑝 2 (?𝑝𝑒𝑟𝑠𝑜𝑛 2, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦), comp(?𝑝𝑒𝑟𝑠𝑜𝑛 1, ?𝑝𝑒𝑟𝑠𝑜𝑛 2, ?𝑝𝑒𝑟𝑠𝑜𝑛) 𝑝 4 (?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒) ← triple(?𝑝𝑒𝑟𝑠𝑜𝑛, livesIn, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒) 𝑝 5 (?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒) ← triple(?𝑝𝑒𝑟𝑠𝑜𝑛, livesIn, 𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒) 𝑝 6 (?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦) ← triple(?𝑝𝑒𝑟𝑠𝑜𝑛, knows, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦) 𝑝 7 (?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦) ← 𝑝 5 (?𝑝𝑒𝑟𝑠𝑜𝑛 1, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒), 𝑝 6 (?𝑝𝑒𝑟𝑠𝑜𝑛 2, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦), comp(?𝑝𝑒𝑟𝑠𝑜𝑛 1, ?𝑝𝑒𝑟𝑠𝑜𝑛 2, ?𝑝𝑒𝑟𝑠𝑜𝑛) 𝑝 8 (?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒) ← 𝑝 7 (?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦) 𝑝 9 (?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒) ← 𝑝 4 (?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒), ¬𝑝 8 (?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒) 𝑝 10 (?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦) ← 𝑝 9 (?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒), null(?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦) 𝑝 11 (?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦) ← 𝑝 3 (?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦) 𝑝 11 (?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦) ← 𝑝 10 (?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦) Note that the predicate names are enumerated according to the parse tree shown in Figure 4. In this sense: the rule 𝑝 1 (. . . ) ← . . . encodes the triple pattern 𝑃1 ; the same applies for the predicated names 𝑝 2 , 𝑝 4 , 𝑝 5 and 𝑝 6 ; the rule 𝑝 3 (. . . ) ← . . . encodes the pattern (𝑃 1 AND 𝑃2 ); the rule 𝑝 7 (. . . ) ← . . . encodes the pattern (𝑃 5 AND 𝑃6 ); the rule 𝑝 8 (. . . ) ← . . . encodes the pattern (SELECT ?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒 WHERE 𝑃7 ); the rule 𝑝 9 (. . . ) ← . . . encodes the pattern (𝑃 4 EXCEPT 𝑃8 ); the rule 𝑝 10 (. . . ) ← . . . encodes the pattern (SELECT ?𝑝𝑒𝑟𝑠𝑜𝑛, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦 WHERE 𝑃9 ); finally, the two rules with predicate name 𝑝 11 encode the graph pattern (𝑃 3 UNION 𝑃10 ). Based on the function 𝛿, we define a general method to transform SPARQL queries into NRMD¬ queries. Definition 6.12 (Function 𝑓12 ). Given a SPARQL query 𝑄, the function 𝑓12 (𝑃) returns a NRMD¬ ¯ and Π is a Datalog program containing the rules query (𝐿, Π) where 𝐿 is the goal atom 𝑝 (𝑃) produced by 𝛿 (𝑄) (note that 𝑄 is a graph pattern). 6.1.3 Translating query answers from NRMD¬ to SPARQL. Recall that a NRMD¬ query answer is a pair (𝑉 , 𝑀) where 𝑉 is a set of variables and 𝑀 is a multiset of substitutions. Additionally, a SPARQL query answer is a multiset of solution mappings Ω. The main difference between a multiset of substitutions 𝑀 and a multiset of solution mappings Ω is the representation of null values. Given a substitution 𝜃 and a variable ?𝑋 ∈ dom(𝜃 ), a null value is represented with the assignment 𝜃 (?𝑋 ) = ⊥. On the other hand, for a solution mapping 𝜇, a null value for ?𝑋 is represented by not including ?𝑋 in the domain of 𝜇. Given a Datalog substitution 𝜃 , the function NotNull(𝜃 ) returns a SPARQL solution mapping 𝜇 defined as follows: (i) dom(𝜇) = {𝑥 ∈ dom(𝜃 ) | 𝜃 (𝑥) ≠ ⊥}, that is, the variables of 𝜇 are the variables in 𝜃 whose assignment is not ⊥; and, (ii) for every variable ?𝑋 ∈ dom(𝜃 ), if 𝜃 (?𝑋 ) ≠ ⊥ then 𝜇 (?𝑋 ) = 𝜃 (?𝑋 ). Hence, every value different from ⊥ is translated into an unbound variable in the corresponding solution mapping. Definition 6.13 (Function ℎ 12 ). Given a multiset of Datalog substitutions Θ, the function ℎ 12 (Θ) returns a multiset of SPARQL solution mappings Ω defined as follows: (i) set(Ω) = {NotNull(𝜃 ) | 𝜃 ∈ set(Θ)}; and, for each substitution 𝜃 ∈ set(Θ), it applies that card(NotNull(𝜃 ), Ω) = card(𝜃, Θ). In other words, the cardinality of each solution mapping NotNull(𝜃 ) in Ω is defined as the number of colored copies ⟨𝜃, 𝑖⟩ occurring in coloring(Θ). Lemma 6.14. SPARQL can be simulated by NRMD¬ .
The multiset semantics of SPARQL patterns
23
Proof. We need to show that, using the functions defined above, (𝑓12, 𝑔12, ℎ 12 ) is a simulation of SPARQL in NRMD¬ . The proof is in the Claim 4 of the Appendix. □ 6.2
From NRMD¬ to SPARQL
This section shows that Non-Recursive Multiset Datalog with Safe Negation (NRMD¬ ) can be simulated by SPARQL. To support this, we describe the following translation functions: • 𝑓21 is the function that translates NRMD¬ queries into SPARQL queries; • 𝑔21 is the function that translates NRMD¬ databases into SPARQL databases; and • ℎ 21 is the function that translates SPARQL query answers into NRMD¬ query answers. 6.2.1 Translating databases from NRMD¬ to SPARQL. In general terms, a fact 𝑝 (𝑐 1, . . . , 𝑐𝑛 ) can be translated into a set of triples of the form (𝑢, 𝛼𝑖 , 𝑐𝑖 ) where 𝑢 is a fresh IRI that identifies the fact, and 𝛼𝑖 is a reserved IRI which allows to describe that constant 𝑐𝑖 is in the position 𝑖 of the fact2 . Also recall that the semantics of NRMD¬ relies on the notion of colored set of a multiset (see Section 4.2), which is the set containing the colored copies of the element of the multiset. This idea is formalized next. In what follows, we will assume that 𝐴 = {𝛼 0, 𝛼 1 . . . } is an enumerable set of special IRIs used to codify positions in Datalog atoms, NULL is a special IRI, and any Datalog constant 𝑐 and name of predicate 𝑝 has an equivalent RDF IRI term (excluding the aforementioned special IRIs) that we will denote with the same symbol 𝑐 and 𝑝. Definition 6.15 (Function 𝑔21 ). Assume that the NRMD¬ database 𝐷 contains 𝑛 copies of a fact 𝐹 (namely 𝑝 (𝑐 1, . . . , 𝑐𝑛 )), and the coloring(𝐷) contains the colored copies ⟨𝐹, 1⟩, . . . , ⟨𝐹, 𝑛⟩ of fact 𝐹 . For each colored copy ⟨𝐹, 𝑖⟩ of 𝐹 , we assume the existence of a fresh IRI 𝑢 ⟨𝐹,𝑖 ⟩ , which we use to identify the colored copy. Similarly the subscript ⟨𝐹,𝑖 ⟩ is used to identify the colored copy of a constant or a predicate. Then the function 𝑔21 applied to the multiset of NRMD¬ facts 𝐷, returns a set of RDF triples (i.e. an RDF graph) defined as Ø 𝑔21 (𝐷) = {(NULL, NULL, NULL)} ⟨𝐹,𝑖 ⟩ ∈coloring(𝐷 )
{(𝑢 ⟨𝐹,𝑖 ⟩ , 𝛼 0, 𝑝 ⟨𝐹,𝑖 ⟩ ), (𝑢 ⟨𝐹,𝑖 ⟩ , 𝛼 1, (𝑐 1 ) ⟨𝐹,𝑖 ⟩ ), . . . , (𝑢 ⟨𝐹,𝑖 ⟩ , 𝛼𝑛 , (𝑐𝑛 ) ⟨𝐹,𝑖 ⟩ )}. Example 6.16. Let 𝐷 be the following NRMD¬ database: 𝐷 = ⦃𝑝 (𝑎, 𝑏), 𝑝 (𝑎, 𝑏), 𝑝 (𝑎, 𝑐), 𝑞(𝑏, 𝑑, 𝑎), 𝑞(𝑏, 𝑒, 𝑎)⦄. Note that the fact 𝑝 (𝑎, 𝑏) occurs twice in the multiset 𝐷, so we need to generate the colored copies for this fact, namely ⟨𝑝 (𝑎, 𝑏), 1⟩ and ⟨𝑝 (𝑎, 𝑏), 2⟩. In contrast, the fact 𝑝 (𝑎, 𝑏) occurs once, so we just generate a simple colored copy, that is, ⟨𝑝 (𝑎, 𝑐), 1⟩. Then the data is translated for SPARQL as follows: 𝑔21 (𝐷) = {(NULL, NULL, NULL), (𝑢 ⟨𝑝 (𝑎,𝑏 ),1⟩ , 𝛼 0, 𝑝), (𝑢 ⟨𝑝 (𝑎,𝑏 ),1⟩ , 𝛼 1, 𝑎), (𝑢 ⟨𝑝 (𝑎,𝑏 ),1⟩ , 𝛼 2, 𝑏), (𝑢 ⟨𝑝 (𝑎,𝑏 ),2⟩ , 𝛼 0, 𝑝), (𝑢 ⟨𝑝 (𝑎,𝑏 ),2⟩ , 𝛼 1, 𝑎), (𝑢 ⟨𝑝 (𝑎,𝑏 ),2⟩ , 𝛼 2, 𝑏), (𝑢 ⟨𝑝 (𝑎,𝑐 ),1⟩ , 𝛼 0, 𝑝), (𝑢 ⟨𝑝 (𝑎,𝑐 ),1⟩ , 𝛼 1, 𝑎), (𝑢 ⟨𝑝 (𝑎,𝑐 ),1⟩ , 𝛼 2, 𝑐), (𝑢 ⟨𝑝 (𝑏,𝑑,𝑎),1⟩ , 𝛼 0, 𝑞), (𝑢 ⟨𝑝 (𝑏,𝑑,𝑎),1⟩ , 𝛼 1, 𝑏), (𝑢 ⟨𝑝 (𝑏,𝑑,𝑎),1⟩ , 𝛼 2, 𝑑), (𝑢 ⟨𝑝 (𝑏,𝑑,𝑎),1⟩ , 𝛼 3, 𝑎), (𝑢 ⟨𝑝 (𝑏,𝑒,𝑎),1⟩ , 𝛼 0, 𝑞), (𝑢 ⟨𝑝 (𝑏,𝑒,𝑎),1⟩ , 𝛼 1, 𝑏), (𝑢 ⟨𝑝 (𝑏,𝑒,𝑎),1⟩ , 𝛼 2, 𝑑), (𝑢 ⟨𝑝 (𝑏,𝑒,𝑎),1⟩ , 𝛼 3, 𝑎)}. 2 An option can be the use of properties rdf:_1, rdf:_2, rdf:_3, . . . , defined in the RDF Schema 1.1 vocabulary.
24
Angles et al.
Intuitively, the SPARQL database corresponding to the NRMD¬ database 𝐷 consists of a set of triples that describe each of the facts, and the inclusion of triple (NULL, NULL, NULL) allows to ensure that the SPARQL database is not empty. The need of this additional triple is explained next when describing the translation from NRMD¬ queries to SPARQL. 6.2.2 Translating queries from NRMD¬ to SPARQL. A notable difference between NRMD¬ and SPARQL is the way both languages define the scope of variables. In NRMD¬ , all variables in a rule are universally quantified, and they are not in the scope of the query. On the other hand, variables in a SPARQL query are divided into in-scope and non-in-scope (see Subsection 3.4). To see this difference, consider the NRMD¬ query (𝑞(𝑋, 𝑌 ), Π) where the program Π consists of the single rule 𝑅 = 𝑞(𝑌 , 𝑍 ) ← 𝑝 (𝑋, 𝑍, 𝑌 ). Notice that the variables in the goal of the query do not correspond to the variables in the head of the rule 𝑅. To simplify the translation, we rename variables in rules according to the goal of the query. In this case, we rewrite 𝑅 as the rule 𝑅 ′ = 𝑞(𝑋, 𝑌 ) ← 𝑝 (𝑍, 𝑌 , 𝑋 ). Formally, given a literal 𝐿 = 𝑞(𝑋 1, . . . , 𝑋𝑛 ) and a rule 𝑅 whose head is 𝑞(𝑌1, . . . , 𝑌𝑛 ), the renamed rule of 𝑅 with respect to 𝐿, denoted vr(𝑅, 𝐿), is the rule 𝑅 ′ that results from 𝑅 by consistently renaming each variable 𝑌𝑖 as 𝑋𝑖 , for 1 ≤ 𝑖 ≤ 𝑛. Let 𝐿 be a positive literal 𝑝 (𝑋 1, . . . , 𝑋𝑛 ) and Π be a normalized NRMD¬ program. We define the function gp(𝐿, Π) which translates 𝐿 into a SPARQL graph pattern. The function gp is defined recursively as follows: (1) If predicate name 𝑝 does not occur in the head of any rule of Π (i.e., 𝑝 is extensional), then gp(𝐿, Π) returns SELECT 𝑋 ((?𝑌 , 𝛼 0, 𝑝) AND (?𝑌 , 𝛼 1, ?𝑋 1 ) AND · · · AND (?𝑌 , 𝛼𝑛 , ?𝑋𝑛 )), where 𝑋 = var(𝐿) and ?𝑌 is a fresh variable. (2) Otherwise, if 𝑝 occurs in the head of the rules {𝑅1, . . . , 𝑅𝑛 } in Π (i.e., 𝑝 is intensional), then gp(𝐿, Π) returns: (𝑇 (vr(𝑅1, 𝐿)) UNION · · · UNION𝑇 (vr(𝑅𝑛 , 𝐿))), where the operator 𝑇 (𝑅) is defined as follows: • If 𝑅 is a projection rule 𝐿 ← 𝐿1 then 𝑇 (𝑅) is (SELECT 𝑋 𝑃 1 ) where 𝑋 = var(𝐿) and 𝑃1 = gp(𝐿1, Π); • If 𝑅 is a join rule 𝐿 ← 𝐿1, 𝐿2 then 𝑇 (𝑅) is (𝑃 1 AND 𝑃2 ) where 𝑃1 = gp(𝐿1, Π) and 𝑃2 = gp(𝐿2, Π); • If 𝑅 is a negation rule 𝐿 ← 𝐿1, ¬𝐿2 then 𝑇 (𝑅) is (𝑃 1 EXCEPT 𝑃2 ) where 𝑃 1 = gp(𝐿1, Π) and 𝑃2 = gp(𝐿2, Π). Note that, if there is just one rule 𝑅1 then gp(𝐿, Π) can be reduced to 𝑇 (𝑅1 ) (no need to rename variables). Example 6.17. Consider the NRMD¬ query (𝑞(𝑋 ), Π) where program Π consists of the rule 𝑞(𝑋 ) ← 𝑝 (𝑋, 𝑌 ). Then, gp(𝑞(𝑋 ), Π) is the SPARQL query SELECT ?𝑋 ((?𝑈 , 𝛼 0, 𝑝) AND (?𝑈 , 𝛼 1, ?𝑋 ) AND (?𝑈 , 𝛼 2, ?𝑌 )). The function gp is not enough to translate NRMD¬ queries to SPARQL queries. Recall that a NRMD¬ query answer is a pair (𝑉 , 𝑀) where 𝑉 is a set of NRMD¬ variables and 𝑀 is a set of NRMD¬ substitutions, and a SPARQL query answer is a multiset Ω of SPARQL mappings. To conclude the translation, we need to define a function that, given a SPARQL query answer Ω, returns a NRMD¬ query answer (𝑉 , 𝑀). The issue is that we cannot compute the set 𝑉 when the multiset Ω is empty. For example, an empty NRMD¬ database 𝐷 is translated as the SPARQL
The multiset semantics of SPARQL patterns
25
database consisting of the set of triples {(NULL, NULL, NULL)} (see Subsection 6.2.1). The evaluation of the query gp(𝑞(𝑋 ), Π) in Example 6.17 returns an empty multiset of mappings, Ω, where the query answer to the NRMD¬ query (𝑞(𝑋 ), Π) is a pair ({𝑋 }, 𝑀) such that 𝑀 is an empty multiset of solutions. Hence, the SPARQL query answer Ω does not contain the information needed to generate the set of variables {𝑋 } in the answer of the NRMD¬ query. To solve the aforementioned issue of having an empty SPARQL query answer, we can extend the function gp with a query that introduces the variables of the query. This is done using the additional triple (NULL, NULL, NULL) we introduced in the translation. Given a set of NRMD¬ variables 𝑉 = {𝑋 1, . . . , 𝑋𝑛 } we write VarQuery(V) to denote the SPARQL pattern (NULL, NULL, ?𝑋 1 ) AND · · · AND (NULL, NULL, ?𝑋𝑛 ). The translation of a NRMD¬ query is then the union of the graph patterns computed by the functions gp and VarQuery. Definition 6.18 (Function 𝑓21 ). Given a NRMD¬ query 𝑄 = (𝐿, Π), the function 𝑓21 (𝑄) returns a SPARQL graph pattern (gp(𝐿, Π) UNION VarQuery(var(𝐿))). Example 6.19. Consider the NRMD¬ query (𝑞(𝑋 ), Π) in Example 6.17. Then, 𝑓21 ((𝑞(𝑋 ), Π)) is the following SPARQL graph pattern: (SELECT ?𝑋 ((?𝑈 , 𝛼 0, 𝑝) AND (?𝑈 , 𝛼 1, ?𝑋 ) AND (?𝑈 , 𝛼 2, ?𝑋 ))) UNION (NULL, NULL, ?𝑋 ). The result of evaluating the NRMD¬ query on an empty set of facts 𝐷 is the pair ({𝑋 }, 𝑀) where 𝑀 is an empty multiset of NRMD¬ substitutions, whereas the result of evaluating the graph pattern 𝑓21 ((𝑞(𝑋 ), Π)) on the SPARQL database 𝑔21 (𝐷) = {(NULL, NULL, NULL)} is the SPARQL query answer Ω = {{?𝑋 ↦→ NULL}}. Intuitively, the mapping {?𝑋 ↦→ NULL} does not codify a NRMD¬ substitution, but the variables in the domain of NRMD¬ substitutions. 6.2.3 Translating query answers from SPARQL to NRMD¬ . Recall that a SPARQL query answer is a multiset of solution mappings, and a NRMD¬ query answer is a pair (𝑉 , 𝑀) (where 𝑉 is a set of variables and 𝑀 is a multiset of substitutions). Since a SPARQL solution mapping can be seen as a NRMD¬ substitution, the translation from SPARQL mappings to NRMD¬ substitutions does not require modifications, except for the mapping {?𝑋 1 ↦→ NULL, . . . , ?𝑋𝑛 ↦→ NULL} which is used to codify the solution variables. Definition 6.20 (Function ℎ 21 ). Let Ω be a multiset of SPARQL solution mappings that includes a mapping 𝜇𝑉 ↦→NULL with cardinality 1 where dom(𝜇𝑉 ↦→NULL ) = 𝑉 and 𝜇 (?𝑋 ) = NULL for every variable ?𝑋 ∈ dom(𝜇𝑉 ↦→NULL ), and for every mapping 𝜇 ′ ∈ Ω it holds that dom(𝜇 ′ ) = 𝑉 . The NRMD¬ solution for Ω, denoted ℎ 21 (Ω), is the pair (𝑉 , 𝑀) where 𝑀 is the multiset of substitutions 𝜃 defined as follows: (1) Given an SPARQL mapping 𝜇 = {?𝑋 1 ↦→ 𝑐 1, . . . , ?𝑋𝑛 ↦→ 𝑐𝑛 } the corresponding NRMD¬ substitution for mapping 𝜇 is the substitution 𝜃 𝜇 = {𝑋 1 ↦→ 𝑐 1, . . . , 𝑋𝑛 ↦→ 𝑐𝑛 } where the NRMD¬ variable 𝑋𝑖 corresponds to the SPARQL variable ?𝑋𝑖 . (2) set(𝑀) = {𝜃 𝜇 | 𝜇 ∈ Ω \ {𝜇𝑉 ↦→NULL }}. (3) card(𝜃 𝜇 , 𝑀) = card(𝜇, Ω). Lemma 6.21. NRMD¬ can be simulated by SPARQL. Proof. This is a long but straightforward induction on Datalog queries using as hypothesis that (𝑓21, 𝑔21, ℎ 21 ) is a simulation of NRMD¬ in SPARQL. The details of this proof are in the appendix (Claim 5). □
26
Angles et al.
6.3
SPARQL and NRMD¬ have the same expressive power
Putting together the simulations among SPARQL and NRMD¬ stated in this section, we get the following theorem: Theorem 6.22. SPARQL and NRMD¬ have the same expressive power. Proof. The claim is based on the simulation of SPARQL with NRMD¬ (Lemma 6.14) and the simulation of NRMD¬ with MRA (Lemma 6.21). □ 7
Equivalence between MRA and NRMD¬
This section presents the simulations that prove that Multiset Relational Algebra (MRA) and Non-Recursive Multiset Datalog with Safe Negation (NRMD¬ ) have the same expressive power. Specifically, we show that MRA can be simulated by NRMD¬ (Section 7.1), and NRMD¬ can be simulated by MRA (Section 7.2). 7.1
From MRA to NRMD¬
This section shows that Multiset Relational Algebra (MRA) can be simulated by Non-Recursive Multiset Datalog with Safe Negation (NRMD¬ ). To support this, we describe the following translation functions: • 𝑓32 is the function that translates MRA queries into NRMD¬ queries; • 𝑔32 is the function that translates MRA databases into NRMD¬ databases; and • ℎ 32 is the function that translates NRMD¬ query answers into MRA query answers. 7.1.1 Translating databases from MRA to NRMD¬ . Recall that a MRA database is a set of relations (where each relation is a multiset of tuples), and a NRMD¬ database is a multiset of facts. First, we define a method to translate a MRA relation into a multiset of facts. Then, we define a method to translate a set of MRA relations into a multiset of NRMD¬ facts. Assume the existence of functions that map: MRA relation names to NRMD¬ predicate names, MRA attributes to NRMD¬ variables, and MRA constants to NRMD¬ constants. Given a relation schema 𝑅, we write 𝑅® to denote a tuple containing the attributes of 𝑅 in lexicographical order. Given a multiset relation 𝑟 , defined over a relation schema 𝑅, with 𝑅® = (𝐴1, . . . , 𝐴𝑛 ), the function Σ(𝑟 ) returns a multiset of Datalog facts defined as follows: For each tuple 𝑡 in 𝑟 , Σ(𝑟 ) contains a fact 𝑓 of the form 𝑝 (𝑐 1, . . . , 𝑐𝑛 ) where 𝑝 is the image of 𝑅, every 𝑐𝑖 is 𝑡 (𝐴𝑖 ), and the cardinality of 𝑓 in Σ(𝑟 ) is given by the cardinality of 𝑡 in 𝑟 . Definition 7.1 (Function 𝑔32 ). Given a MRA database 𝐷, the function 𝑔32 returns a multiset of NRMD¬ facts 𝐷 ′ defined as follows: (1) For each MRA relation 𝑟 in 𝐷, 𝐷 ′ contains the facts returned by Σ(𝑟 ); (2) For each constant 𝑐 in 𝐷, 𝐷 ′ contains a fact eq(𝑐, 𝑐). Example 7.2. Let 𝐷 be an MRA dataset consisting in two relations 𝑟 and 𝑠 with respective relation schemas 𝑅 and 𝑆 with 𝑅® = (𝐴1, 𝐴2 ) and 𝑆® = (𝐴1, 𝐴3 ), and defined as follows: 𝑟 = ⦃{𝐴1 ↦→ 𝑎 1, 𝐴2 ↦→ 𝑎 2 }, {𝐴1 ↦→ 𝑎 1, 𝐴2 ↦→ 𝑎 2 }, {𝐴1 ↦→ 𝑎 1, 𝐴2 ↦→ 𝑎 3 }⦄, 𝑠 = ⦃{𝐴1 ↦→ 𝑎 1, 𝐴3 ↦→ 𝑎 4 }⦄. Then, the corresponding NRMD¬ dataset is the following: 𝑔32 (𝐷) = ⦃𝑝 𝑅 (𝑎 1, 𝑎 2 ), 𝑝𝑅 (𝑎 1, 𝑎 2 ), 𝑝 𝑅 (𝑎 1, 𝑎 3 ), 𝑝𝑆 (𝑎 1, 𝑎 4 ), eq(𝑎 1, 𝑎 1 ), eq(𝑎 2, 𝑎 2 ), eq(𝑎 3, 𝑎 3 ), eq(𝑎 4, 𝑎 4 )⦄, where predicates 𝑝 𝑅 and 𝑝𝑆 are the corresponding images for the relation schemas 𝑅 and 𝑆.
The multiset semantics of SPARQL patterns
27
Note that the multiset of Datalog facts 𝐷 ′ is defined over the vocabulary that includes as predicate names all the relation names in 𝐷, and as arity of the predicate name 𝑅 the number of attributes of the relation name 𝑅. 7.1.2 Translating queries from MRA to NRMD¬ . Recall that a MRA query is a relational algebra expression, and a NRMD¬ query is a set of rules. First, we need to provide a recursive method to reduce MRA selection formulas into atomic formulas. Such method is based on the following equivalences where 𝐸 is an MRA expression, and 𝜓 , 𝜓 1 , and 𝜓 2 are selection formulas: 𝜎𝜓1 ∧𝜓2 (𝐸) ≡ 𝜎𝜓2 (𝜎𝜓1 (𝐸)),
(7)
𝜎𝜓1 ∨𝜓2 (𝐸) ≡ 𝜎𝜓1 ∧¬𝜓2 (𝐸) ∪ 𝜎¬𝜓1 ∧𝜓2 (𝐸) ∪ 𝜎𝜓1 ∧𝜓2 (𝐸),
(8)
𝜎¬𝜓 (𝐸) ≡ 𝐸 \ 𝜎𝜓 (𝐸).
(9)
The proof of the validity of the above equivalences follows directly from the semantics of the selection operator. In particular, Equivalence 8 is rather involved because separates the disjunction in a union of three disjoint multiset relations in order to preserve the cardinality of each solution. Using the above equivalence, we get the following lemma. Lemma 7.3. For every MRA expression 𝐸, there exists an equivalent MRA expression 𝐸 ′ satisfying that all selection formulas in 𝐸 ′ are atomic. Proof. The proof follows from induction in the number 𝑘 of Boolean connectives in selection formulas occurring in an MRA expression 𝐸. The base case is 𝑘 = 0 and thus all selection formulas are atomic. If 𝑘 > 0, then the expression includes a selection expression whose formula has either the form 𝜓 1 ∧ 𝜓 2 , ¬𝜓 , or 𝜓 1 ∨ 𝜓 2 . In the first two cases, equivalences 7 and 9 reduce by one of the Boolean connectives of the expression. In the third case, the consecutive application of equivalences 8, 7, and 9 (in that order) reduces by one the number of Boolean connectives. Hence, we produce an equivalent query with 𝑘 − 1 Boolean connectives. □ Definition 7.4 (Function 𝑓32 ). Let 𝑄 be a MRA query (i.e. an MRA expression), where selection formulas are atomic (i.e., have no Boolean connectives). The function 𝑓32 (𝑄) returns a NRMD¬ ® where 𝑞 is a predicate name corresponding query (𝐿, Π) where 𝐿 is a goal clause of the form 𝑞(𝑄) b (sorted in lexicographical to 𝑄, 𝑄® are the variables corresponding to the attributes in the schema 𝑄 order), and Π is a set of NRMD¬ rules (i.e. a NRMD¬ program) created by applying recursively the rules shown in Table 5. Note that function 𝑓32 assigns a fresh predicate name to each operation in query 𝑄 by following a non-deterministic approach. Although it is not difficult to define deterministic ways (like in the translation from SPARQL to NRMD¬ ), we omit in how intensional predicate names are assigned. 7.1.3 Translating query answers from NRMD¬ to MRA. Recall that a NRMD¬ query answer is a pair (𝑉 , 𝑀) where 𝑉 is a set of variables, and 𝑀 is a multiset of NRMD¬ substitutions. On the other hand, an MRA query answer is a multiset relation. Next, we define a function ℎ 32 which translates a NRMD¬ query answer into a MRA query answer. Definition 7.5 (Function ℎ 32 ). Given a NRMD¬ query answer 𝐴 = (𝑉 , 𝑀), the function ℎ 32 (𝐴) returns a multiset relation 𝑟 where: the schema of 𝑟 is given by the set of attributes 𝑉 (assume a simple transformation of variables to attribute names); for each substitution 𝜃 in 𝑀, there is a tuple b and card(𝑡, 𝑟 ) = card(𝜃, 𝑀). 𝑡 in 𝑟 satisfying that 𝑡 (𝑋 ) = 𝜃 (𝑋 ) for every attribute 𝑋 ∈ 𝑅, Lemma 7.6. MRA can be simulated by NRMD¬ .
28
Angles et al.
Table 5. Definition of function Γ which translates an MRA expression into a set of Datalog rules. Given a ¯ is a positive literal MRA expression 𝐸, the recursive function Γ(𝐸) returns a set of NRMD¬ rules where: 𝑞𝑖 (𝐴) related to the MRA expression 𝐸𝑖 , 𝑞𝑖 is a fresh predicate name, 𝐴¯𝑖 denotes a set of variables, 𝑅® denotes the b sorted in lexicographical order. attributes in schema 𝑅, MRA expression 𝐸 0
Γ(𝐸 0 )
where ...
𝑅
® 𝑞 0 (𝐴¯0 ) ← 𝑅(𝑅)
𝐴¯0 = 𝑅®
(𝐸 1 Z 𝐸 2 )
𝑞 0 (𝐴¯0 ) ← 𝑞 1 (𝐴¯1 ), 𝑞 2 (𝐴¯2 ); Γ(𝐸 1 ); Γ(𝐸 2 )
𝐴¯0 = 𝐴¯1 ∪ 𝐴¯2
(𝐸 1 ∪ 𝐸 2 )
𝑞 0 (𝐴¯0 ) ← 𝑞 1 (𝐴¯1 ); 𝑞 0 (𝐴¯0 ) ← 𝑞 2 (𝐴¯2 ); Γ(𝐸 1 ); Γ(𝐸 2 )
𝐴¯0 = 𝐴¯1 = 𝐴¯2
(𝐸 1 \ 𝐸 2 )
𝑞 0 (𝐴¯0 ) ← 𝑞 1 (𝐴¯1 ), ¬𝑞 2 (𝐴¯2 ); Γ(𝐸 1 ); Γ(𝐸 2 )
𝐴¯0 = 𝐴¯1
𝜋𝑆 (𝐸 1 )
𝑞 0 (𝐴¯0 ) ← 𝑞 1 (𝐴¯1 ); Γ(𝐸 1 )
𝐴¯0 = 𝑆
𝜌𝐴/𝐵 (𝐸 1 )
𝑞 0 (𝐴¯0 ) ← 𝑞 1 (𝐴¯1 ), eq(𝐴, 𝐵); Γ(𝐸 1 )
𝐴¯0 = (𝐴¯1 \ {𝐴}) ∪ {𝐵}
𝜎𝐴=𝐵 (𝐸 1 )
𝑞 0 (𝐴¯0 ) ← 𝑞 1 (𝐴¯1 ), eq(𝐴, 𝐵); Γ(𝐸 1 )
𝐴¯0 = 𝐴¯1
Proof. Let 𝑓32 , 𝑔32 , ℎ 32 be the functions described in Definition 7.1, Definition 7.5 and Definition 7.4 respectively. The proof of this theorem follows from the claim that (𝑓32 , 𝑔32 , ℎ 32 ) simulates MRA in NRMD¬ by using induction in the structure of queries. The proof of this claim is in the appendix (Claim 6). □ 7.2 From NRMD¬ to MRA This section shows that Non-Recursive Multiset Datalog with Safe Negation (NRMD¬ ) can be simulated by Multiset Relational Algebra (MRA). To support this, we describe the following translation functions: • 𝑓23 is the function that translates NRMD¬ queries into MRA queries; • 𝑔23 is the function that translates NRMD¬ databases into MRA databases; and • ℎ 23 is the function that translates MRA query answers into NRMD¬ query answers. 7.2.1 Translating databases from NRMD¬ to MRA. Recall that a database in NRMD¬ is a multiset of facts, and a database in MRA is a set of relations (where each relation is a multiset of tuples). First, we define a method to translate a multiset of facts with the same predicate name into a relation 𝑟 . Let 𝑀 be a multiset of NRMD¬ facts having the same predicate name, i.e. every fact in 𝑀 has the form 𝑝 (𝑡 1, . . . , 𝑡𝑛 ). The function 𝜓 (𝑀) returns a MRA relation 𝑟 where: the relation schema b 𝑟 of 𝑟 is given by the relation name 𝑝 and the set of attributes {𝐴1, . . . , 𝐴𝑛 }, where each attribute name has the form 𝑎𝑡𝑡_𝑖 with 1 ≤ 𝑖 ≤ 𝑛; for each fact 𝑝 (𝑡 1, . . . , 𝑡𝑛 ) in 𝑀 there is a tuples 𝑡 in 𝑟 satisfying that 𝑡 (𝐴𝑖 ) = 𝑡𝑖 . Next, we define function 𝑔23 which allows translating a multiset of facts into a set of relations. Definition 7.7 (Function 𝑔23 ). Let 𝑀 be a multiset of NRMD¬ facts 𝑀 (i.e. an NRMD¬ database), and {𝑝 1, . . . , 𝑝𝑛 } are the predicate names in 𝑀. The function 𝑔23 (𝑀) returns a set of relations (i.e. a MRA database) {𝑟 1, . . . , 𝑟𝑛 } where 𝑟𝑖 = 𝜓 (𝑀𝑖 ) such that 𝑀𝑖 is the subset of NRMD¬ facts of 𝑀 having the predicate name 𝑝𝑖 . Example 7.8. Let 𝑀 be the multiset of NRMD¬ facts defined as follows: 𝑀 = ⦃𝑝 1 (𝑐 1, 𝑐 2 ), 𝑝 1 (𝑐 1, 𝑐 2 ), 𝑝 1 (𝑐 1, 𝑐 3 ), 𝑝 2 (𝑐 1, 𝑐 4 )⦄,
The multiset semantics of SPARQL patterns
29
Then, the corresponding MRA dataset 𝑔23 (𝑀) consists of the following relations 𝑟 1 and 𝑟 2 , over relation schemas 𝑅1 and 𝑅2 , and with attributes 𝑅b1 = {𝐴1, 𝐴2 } and 𝑅b2 = {𝐵 1, 𝐵 2 }: 𝑟 1 = ⦃{𝐴1 ↦→ 𝑐 1, 𝐴2 ↦→ 𝑐 2 }, {𝐴1 ↦→ 𝑐 1, 𝐴2 ↦→ 𝑐 2 }, {𝐴1 ↦→ 𝑐 1, 𝐴2 ↦→ 𝑐 3 }⦄, 𝑟 2 = ⦃{𝐵 1 ↦→ 𝑐 1, 𝐵 2 ↦→ 𝑐 4 }⦄. 7.2.2 Translating queries from NRMD¬ to MRA. Recall that a NRMD¬ query is a set of rules, and a MRA query is a relational algebra expression. Let Π be a normalized NRMD¬ program. We define, by mutual recursion, functions 𝛿 1 (𝐿, Π) and 𝛿 2 (𝑟, Π) to translate (respectively) literals and rules into MRA expressions. Given a literal 𝐿 in Π of the form 𝑝 (𝑋 1, . . . , 𝑋𝑛 ), the function 𝛿 1 (𝐿, Π) is defined as follows: (1) If predicate name 𝑝 does not occur in the head of any rule of Π, then 𝛿 1 (𝐿, Π) returns the MRA expression 𝜌𝐴1 /𝑋1 (· · · 𝜌𝐴𝑛 /𝑋𝑛 (𝑅) · · · ) where 𝑅 is the relation name associated to 𝑝; (2) Otherwise, if 𝑝 occurs in the head of the rules {𝑟 1, . . . , 𝑟𝑚 } in Π, then 𝛿 1 (𝐿, Π) returns the MRA expression (𝐸 1 ∪ (𝐸 2 ∪ (· · · 𝐸𝑚 ) · · · ) where each 𝐸𝑖 is a MRA expression returned by 𝛿 2 (𝑟𝑖 , Π). Given a rule 𝑟 in Π, the function 𝛿 2 (𝑟, Π) is defined as follows: • If 𝑟 is a projection rule 𝐿0 ← 𝐿1 then 𝛿 2 (𝑟, Π) returns the MRA expression 𝜋𝑆 (𝐸) where 𝑆 is the set of variables var(𝐿0 ) and 𝐸 is the MRA expression returned by 𝛿 1 (𝐿1, Π); • If 𝑟 is a join rule 𝐿0 ← 𝐿1, 𝐿2 then 𝛿 2 (𝑟, Π) returns the MRA expression (𝐸 1 Z 𝐸 2 ) where 𝐸 1 and 𝐸 2 are the MRA expressions returned by 𝛿 1 (𝐿1, Π) and 𝛿 1 (𝐿2, Π) respectively; • If 𝑟 is a negation rule 𝐿0 ← 𝐿1, ¬𝐿2 then 𝛿 2 (𝑟, Π) returns the MRA expression (𝐸 1 \ 𝐸 2 ) where 𝐸 1 and 𝐸 2 are the MRA expressions returned by 𝛿 1 (𝐿1, Π) and 𝛿 1 (𝐿2, Π) respectively. Definition 7.9 (Function 𝑓23 ). Given a normalized NRMD¬ query 𝑄 = (𝐿, Π) where 𝐿 is the goal clause, and Π a NRMD¬ program, the function 𝑓23 (𝑄) returns a MRA query defined by 𝛿 1 (𝐿, Π). 7.2.3 Translating query answers from MRA to NRMD¬ . Recall that a MRA query answer is a multiset relation, and a NRMD¬ query answer is a pair (𝑉 , 𝑀) where 𝑉 is a set of variables, and 𝑀 is a multiset of substitutions. Since a MRA tuple can be seen (interpreted) also as a Datalog substitution, the translation from MRA tuples to Datalog substitutions requires essentially no modifications. Next, we define a function ℎ 23 which transforms a MRA query answer into a NRMD¬ query answer. Definition 7.10 (Function ℎ 23 ). Given a MRA relation 𝑅 with schema 𝑅b = {𝐴1, . . . , 𝐴𝑛 }, the function ℎ 23 (𝑅) returns a NRMD¬ query answer 𝐴 = (𝑉 , 𝑀) where: 𝑉 is a set of variables {𝑋 1, . . . , 𝑋𝑛 } where variable 𝑋𝑖 corresponds to attribute 𝐴𝑖 (assume a simple transformation of attribute names to variable names); and, for each tuple 𝑡 in 𝑅, there is a substitution 𝜃 in 𝑀 satisfying that 𝜃 (𝑋𝑖 ) = 𝑡 (𝐴𝑖 ) b and card(𝜃, 𝑀) = card(𝑡, 𝑅). for every attribute 𝐴𝑖 ∈ 𝑅, Lemma 7.11. NRMD¬ can be simulated by MRA. Proof. This is a long but straightforward induction on Datalog queries using as hypothesis that (𝑓23, 𝑔23, ℎ 23 ) is a simulation of NRMD¬ in MRA. The details of this proof are in the appendix (Claim 7). □ 7.3
MRA and NRMD¬ have the same expressive power
Putting together the simulations among MRA and NRMD¬ stated in this section, we get the following theorem: Theorem 7.12. MRA and NRMD¬ have the same expressive power. Proof. The claim is based on the simulation of MRA with NRMD¬ (Lemma 7.6) and the simulation of NRMD¬ with MRA (Lemma 7.11). □
30
8
Angles et al.
Equivalence between MRA and SPARQL
This section presents the simulations that prove that Multiset Relational Algebra (MRA) and SPARQL have the same expressive power. Specifically, we show that MRA can be simulated by SPARQL (Section 8.1), and SPARQL can be simulated by MRA (Section 8.2). 8.1
From MRA to SPARQL
This section shows that Multiset Relational Algebra (MRA) can be simulated by SPARQL. To support this, we describe the following translation functions: • 𝑓31 is the function that translates MRA queries into SPARQL queries; • 𝑔31 is the function that translates MRA databases into SPARQL databases; and • ℎ 31 is the function that translates SPARQL query answers into MRA query answers. 8.1.1 Translating databases from MRA to SPARQL. Recall that a MRA database is a set of relations (where each relation is a multiset of tuples), and a SPARQL database is a set of triples. Assume the existence of functions that map relation names to IRIs, relation attributes to IRIs, and tuples to IRIs. Let 𝑟 be a multiset relation, 𝑡 be a tuple in 𝑟 and {𝑡 1, . . . , 𝑡 𝑛 } be the set of colored copies of 𝑡 where 𝑛 = card(𝑡, 𝑟 ). The function 𝛽 (𝑡, 𝑟 ) returns a set of RDF triples defined as follows: for each copy 𝑡 𝑖 of 𝑡, 𝛽 (𝑡, 𝑟 ) contains a triple (iri_𝑡 𝑖 , iri_𝑏, 𝑖𝑟𝑖_𝑟 ) where iri_𝑡 𝑖 is an IRI which identifies the tuple 𝑡 𝑖 , 𝑖𝑟𝑖_𝑟 is an IRI which identifies the relation 𝑟 , and iri_𝑏 is an IRI which describes that iri_𝑡 𝑖 is a tuple of 𝑖𝑟𝑖_𝑟 ; and, for each attribute 𝐴 in b 𝑟 , 𝛽 (𝑡, 𝑟 ) contains a triple of the form (iri_𝑡 𝑖 , iri_𝐴, lit_𝐴) where iri_𝐴 is an IRI which identifies the attribute 𝐴, and lit_𝐴 is a literal equivalent to the value 𝑡 (𝐴). Hence, for each copy of a tuple 𝑡 we create a set of RDF triples. Definition 8.1 (Function 𝑔31 ). Given a MRA database 𝐷, the function 𝑔31 (𝐷) returns a set of RDF triples 𝐷 ′ defined as follows: • For each multiset relation 𝑟 in 𝐷, and for each tuple 𝑡 in 𝑟 , 𝐷 ′ contains the RDF triples returned by 𝛽 (𝑡, 𝑟 ); • 𝐷 ′ contains a triple (NULL, NULL, NULL) where NULL is a special IRI. Like in the simulation of NRMD¬ with SPARQL, the simulation of MRA with SPARQL uses this special triple to retrieve the variables that are attributes of the MRA query answer. Example 8.2. Let 𝐷 be an MRA dataset consisting in two relations 𝑟 and 𝑠 with respective relation schemas 𝑅 and 𝑆 with 𝑅® = (𝐴1, 𝐴2 ) and 𝑆® = (𝐴1, 𝐴3 ), and defined as follows: 𝑟 = ⦃{𝐴1 ↦→ 𝑎 1, 𝐴2 ↦→ 𝑎 2 }, {𝐴1 ↦→ 𝑎 1, 𝐴2 ↦→ 𝑎 2 }, {𝐴1 ↦→ 𝑎 1, 𝐴2 ↦→ 𝑎 3 }⦄, 𝑠 = ⦃{𝐴1 ↦→ 𝑎 1, 𝐴3 ↦→ 𝑎 4 }⦄. Then, the corresponding SPARQL dataset is the following: 𝑔31 = {(𝑢 11, iri_𝑏, iri_𝑟 ), (𝑢 11, iri_𝐴1, lit_𝐴𝑎1 ), (𝑢 11, iri_𝐴2, lit_𝐴𝑎2 ), (𝑢 12, iri_𝑏, iri_𝑟 ), (𝑢 12, iri_𝐴1, lit_𝐴𝑎1 ), (𝑢 12, iri_𝐴2, lit_𝐴𝑎2 ), (𝑢 21, iri_𝑏, iri_𝑟 ), (𝑢 21, iri_𝐴1, lit_𝐴𝑎1 ), (𝑢 21, iri_𝐴2, lit_𝐴𝑎3 ), (𝑣 11, iri_𝑏, iri_𝑠), (𝑣 11, iri_𝐴1, lit_𝐴𝑎1 ), (𝑣 11, iri_𝐴3, lit_𝐴𝑎4 ), (NULL, NULL, NULL)}, where 𝑢 11 , 𝑢 12 , and 𝑢 21 correspond to the IRIs for the tuples in the multiset relation 𝑟 and 𝑣 11 correspond to the IRI of the tuple in the multiset relation 𝑠.
The multiset semantics of SPARQL patterns
31
8.1.2 Translating queries from MRA to SPARQL. Recall that a MRA query is a relational algebra expression and a SPARQL query is a graph pattern. First, consider the following issue. A query answer in MRA is a multiset relation 𝑟 over a set of attributes b 𝑟 , whereas a query answer in SPARQL does not specify a set of variables for which solutions are defined. For example, the evaluation of the triple pattern (?𝑋, ?𝑌 , ?𝑍 ) over an empty RDF graph results in an empty multiset Ω. The reference to the variables is not carried in the SPARQL answer. Like with the simulation of NRMD¬ with SPARQL, we need to define a SPARQL pattern to retrieve the answer variables. Given an MRA expression 𝐸, with attributes 𝐸b = {𝑋 1, . . . , 𝑋𝑛 }, we write AttrQuery(𝐸) to denote the SPARQL pattern (NULL, NULL, ?𝑋 1 ) AND · · · AND (NULL, NULL, ?𝑋𝑛 ), where, for 1 ≤ 𝑖 ≤ 𝑛, variable ?𝑋𝑖 is the corresponding SPARQL variable for the MRA attribute 𝑋𝑖 . Example 8.3. Consider the MRA expression 𝑟 Z 𝑠 where b 𝑟 = {𝑋, 𝑌 } and b 𝑠 = {𝑌 , 𝑍 }. Then, AttrQuery(𝐸) = (NULL, NULL, ?𝑋 ) AND (NULL, NULL, ?𝑌 ) AND (NULL, NULL, ?𝑍 ), where ?𝑋 , ?𝑌 , and ?𝑍 are the corresponding SPARQL variables for attributes 𝑋 , 𝑌 , and 𝑍 . Recall that a MRA query is an MRA expression, and a SPARQL query is a SPARQL graph pattern. We will show that every type of MRA expression can be translated to a specific type of SPARQL graph pattern. Table 6 shows the translation rules which are the basis for the following definition. Definition 8.4 (Function 𝑓31 ). Given an MRA expression 𝐸, the function 𝑓31 returns a SPARQL graph pattern defined by (Υ(𝐸) UNION AttrQuery(E)).
Table 6. Definition of function Υ which translates an MRA expression into a SPARQL pattern. MRA expression SPARQL pattern Υ(𝐸) 𝐸 (SELECT ?𝑋 1 · · ·?𝑋𝑛
where ...
((?𝑌 , iri_b, iri_r) AND ((?𝑌 , iri _A1, ?𝑋 1 ) AND(· · · AND(?𝑌 , iri _A2, ?𝑋𝑛 ) · · · )
𝑖𝑟𝑖𝑟 is the IRI that identifies 𝑅, ?𝑌 is a variable used to match every tuple of 𝑅, and ?𝑋𝑖 is a variable that corresponds to the attribute 𝐴𝑖 in b schema 𝑅.
(𝐸 1 Z 𝐸 2 )
(𝑃1 AND 𝑃2 )
𝑃 1 = Υ(𝐸 1 ) and 𝑃 2 = Υ(𝐸 2 ).
(𝐸 1 ∪ 𝐸 2 )
(𝑃1 UNION 𝑃2 )
𝑃 1 = Υ(𝐸 1 ) and 𝑃 2 = Υ(𝐸 2 ).
(𝐸 1 \ 𝐸 2 )
(𝑃1 EXCEPT 𝑃2 )
𝑃 1 = Υ(𝐸 1 ) and 𝑃 2 = Υ(𝐸 2 ).
𝜋𝑆 (𝐸 1 )
(SELECT𝑊 𝑃1 )
𝑃 1 = Υ(𝐸 1 ) and 𝑊 is the set of variables corresponding to the attributes in 𝑆.
𝜌𝐴/𝐵 (𝐸 1 )
subs?𝑋 /?𝑌 (𝑃 1 )
𝑃 1 = Υ(𝐸 1 ), ?𝑋 is the variable that corresponds to attribute 𝐴, ?𝑌 is the variable that corresponds to attribute 𝐵, and subs?𝑋 /?𝑌 (𝑃 1 ) denotes the renaming of variable ?𝑋 with variable ?𝑌 in the SPARQL query 𝑃1 (see Appendix A).
𝜎𝜓 (𝐸 1 )
(𝑃 1 FILTER 𝜑)
𝑃 1 = Υ(𝐸 1 ), and 𝜑 is a filter condition equivalent to the selection condition 𝜓 .
𝑅
32
Angles et al.
Example 8.5. Consider the MRA expression 𝐸 = 𝑅 Z 𝑆 where 𝑅b = {𝐴, 𝐵} and 𝑆b = {𝐵, 𝐶}. Then the corresponding SPARQL query 𝑓31 (𝐸) is the following: 𝑓31 (𝐸) = (((SELECT {?𝑋 𝐴 , ?𝑋 𝐵 } WHERE (?𝑌 1, iri _A, ?𝑋 𝐴 ) AND(?𝑌 1, iri _B, ?𝑋 𝐵 )) AND (SELECT {?𝑋 𝐵 , ?𝑋 𝐶 } WHERE (?𝑌 1, iri _B, ?𝑋 𝐵 ) AND(?𝑌 1, iri _C, ?𝑋 𝐶 ))) UNION ((NULL, NULL, ?𝑋 𝐴 ) AND (NULL, NULL, ?𝑋 𝐴 ) AND (NULL, NULL, ?𝑋 𝐴 ))). If 𝑅 = ⦃{𝐴 ↦→ 𝑎, 𝐵 ↦→ 𝑏}, {𝐴 ↦→ 𝑎, 𝐵 ↦→ 𝑏}⦄ and 𝑆 = ⦃{𝐵 ↦→ 𝑏, 𝐶 ↦→ 𝑐}⦄, the answer to the SPARQL query over the corresponding translation of the MRA database 𝐷 to an RDF graph is the following multiset: J𝑓31 𝐸K𝑔31 (𝐷 ) = ⦃{?𝑋 𝐴 ↦→ 𝑎, ?𝑋 𝐵 ↦→ 𝑏, ?𝑋 𝐶 ↦→ 𝑐}, {?𝑋 𝐴 ↦→ 𝑎, ?𝑋 𝐵 ↦→ 𝑏, ?𝑋 𝐶 ↦→ 𝑐}, {?𝑋 𝐴 ↦→ NULL, ?𝑋 𝐵 ↦→ NULL, ?𝑋 𝐶 ↦→ NULL}⦄. Otherwise, if 𝑅 is empty then: J𝑓31 𝐸K𝑔31 (𝐷 ) = ⦃{?𝑋 𝐴 ↦→ NULL, ?𝑋 𝐵 ↦→ NULL, ?𝑋 𝐶 ↦→ NULL}⦄.
Whereas the first two SPARQL mappings {?𝑋 𝐴 ↦→ 𝑎, ?𝑋 𝐵 ↦→ 𝑏, ?𝑋 𝐶 ↦→ 𝑐} correspond are duplicates of the same MRA answer, {𝐴 ↦→ 𝑎, 𝐵 ↦→ 𝑏, 𝐶 ↦→ 𝑐}, the last mapping does not correspond to an answer, but encodes the attributes of the MRA query. By encoding the attributes of the MRA query, we can reconstruct the result MRA relation even in the case it is empty. 8.1.3 Translating query answers from SPARQL to MRA. Recall that a query answer in SPARQL is a multiset of mappings, and a query answer in MRA is a multiset relation (i.e. a multiset of tuples). Intuitively, a multiset of mappings Ω can be transformed into a MRA relation 𝑟 where the attributes in b 𝑟 are the variables in the domain of Ω. This notion is defined next. Definition 8.6 (Function ℎ 31 ). Let Ω be a multiset of mappings with dom(𝜇) = 𝑉 for every mapping 𝜇 ∈ Ω, and that includes the mapping 𝜇𝑉 ↦→NULL with dom(𝜇𝑉 ↦→NULL ) = 𝑉 , 𝜇𝑉 ↦→NULL (?𝑋 ) = NULL for every variable ?𝑋 ∈ 𝑉 , . The function ℎ 31 (Ω) returns a multiset relation 𝑟 where: 𝑟 includes the MRA attribute 𝐴 corresponding to • For each variable ?𝑋 ∈ 𝑉 , the schema b variable ?𝑋 . • The tuple 𝑡 𝜇 corresponding to a mapping 𝜇 with dom(𝜇) = 𝑉 is the tuple with attributes b 𝑟 such that 𝑡 (𝐴) = 𝜇 (?𝑋 ), for each MRA attribute 𝐴 ∈ b 𝑟 corresponding to a variable ?𝑋 ∈ 𝑉 . • set(𝑟 ) = {𝑡 𝜇 | 𝜇 ∈ set(Ω) \ {𝜇𝑉 ↦→NULL }}. • card(𝑡 𝜇 , 𝑟 ) = card(𝜇, Ω) Lemma 8.7. MRA can be simulated in SPARQL. Proof. Let 𝑓31, 𝑔31, ℎ 31 denote respectively the functions stated in definitions and 8.4, 8.1, and 8.6. The proof of this theorem follows from the claim that (𝑓31, 𝑔31, ℎ 31 ) simulates MRA in SPARQL by using induction in the structure of queries. The proof of this claim is in the appendix (Claim 8). □ 8.2
From SPARQL to MRA
This section shows that SPARQL can be simulated by Multiset Relational Algebra (MRA). To support this, we describe the following translation functions: • 𝑓13 is the function that translates SPARQL queries into MRA queries; • 𝑔13 is the function that translates SPARQL databases into MRA databases; and • ℎ 13 is the function that translates MRA query answers into SPARQL query answers. The translation presented here is inspired by the one presented by Cyganiak [15]. However, unlike Cyganiak, we do not use null values with the SQL semantics. Instead, we use a special constant, denoted ⊥, used to codify unbound values.
The multiset semantics of SPARQL patterns
33
8.2.1 Translating databases from SPARQL to MRA. Recall that a SPARQL database is a set of RDF triples, and a MRA database is a set of multiset relations. The translation of a set of RDF triples 𝐺 will produce three multiset relations (without duplicates): Trip, which codifies the RDF triples in 𝐺; Null, introduced to manage the unbound values of SPARQL; and Comp, introduced to simulate the notion of compatibility between mappings. Definition 8.8 (Function 𝑔13 ). Let ⊥ be a special constant. Given a set of RDF triples 𝐺, the function 𝑔13 (𝐺) returns a multiset relational database 𝐷 ′ containing the multiset relations Trip, Null, and Comp defined as follows: d = {𝑆, 𝑃, 𝑂 }, set(Trip) = {{𝑆 ↦→ 𝑠, 𝑃 ↦→ 𝑝, 𝑂 ↦→ 𝑜 } | (𝑠, 𝑝, 𝑜) ∈ 𝐺 }, and card(𝑡, Trip) = 1 (1) Trip for every tuple 𝑡 ∈ set(Trip). d = {𝑁 }, set(Null) = {{𝑁 ↦→ ⊥}}, and card({𝑁 ↦→ ⊥}, Null) = 1. (2) Null (3) Comp = {𝐴1, 𝐴2, 𝐴}, set(Comp) includes the tuple {𝐴1 ↦→ ⊥, 𝐴2 ↦→ ⊥, 𝐴 ↦→ ⊥} and all tuples of the form {𝐴1 ↦→ 𝑎, 𝐴2 ↦→ 𝑎, 𝐴 ↦→ 𝑎}, {𝐴1 ↦→ ⊥, 𝐴2 ↦→ 𝑎, 𝐴 ↦→ 𝑎}, and {𝐴1 ↦→ 𝑎, 𝐴2 ↦→ ⊥, 𝐴 ↦→ 𝑎} where 𝑎 is an RDF term in 𝐺, and card(𝑡, Comp) = 1 for every tuple 𝑡 ∈ set(Comp). Example 8.9. Let 𝐺 be the RDF graph defined as follows 𝐺 = {(Alice, livesIn, Santiago), (Alice, knows, Bob), (Bob, livesIn, Santiago), (Bob, knows, Carol), (Carol, livesIn, Lima)}. Then the data is translated for MRA as the database 𝑔13 (𝐺) with the multiset relations Trip, Null, and Comp defined as follows: Trip = ⦃{𝑆 ↦→ Alice, 𝑃 ↦→ livesIn, 𝑂 ↦→ Santiago}, {𝑆 ↦→ Alice, 𝑃 ↦→ knows, 𝑂 ↦→ Bob} {𝑆 ↦→ Bob, 𝑃 ↦→ livesIn, 𝑂 ↦→ Santiago}, {𝑆 ↦→ Bob, 𝑃 ↦→ knows, 𝑂 ↦→ Carol}, {𝑆 ↦→ Carol, 𝑃 ↦→ livesIn, 𝑂 ↦→ Lima}⦄ Null = ⦃{𝑁 ↦→ ⊥}⦄ Comp = ⦃{𝐴1 ↦→ ⊥, 𝐴2 ↦→ ⊥, 𝐴3 ↦→ ⊥}, {𝐴1 ↦→ Alice, 𝐴2 ↦→ Alice, 𝐴3 ↦→ Alice}, {𝐴1 ↦→ Alice, 𝐴2 ↦→ ⊥, 𝐴3 ↦→ Alice}, {𝐴1 ↦→ ⊥, 𝐴2 ↦→ Alice, 𝐴3 ↦→ Alice}, {𝐴1 ↦→ livesIn, 𝐴2 ↦→ livesIn, 𝐴3 ↦→ livesIn}, {𝐴1 ↦→ livesIn, 𝐴2 ↦→ ⊥, 𝐴3 ↦→ livesIn}, {𝐴1 ↦→ ⊥, 𝐴2 ↦→ livesIn, 𝐴3 ↦→ livesIn}, .. . {𝐴1 ↦→ Lima, 𝐴2 ↦→ Lima, 𝐴3 ↦→ Lima}, {𝐴1 ↦→ Lima, 𝐴2 ↦→ ⊥, 𝐴3 ↦→ Lima}, {𝐴1 ↦→ ⊥, 𝐴2 ↦→ Lima, 𝐴3 ↦→ Lima}⦄. 8.2.2 Translating queries from SPARQL to MRA. Recall that a SPARQL query is a graph pattern, and a MRA query is a relational algebra expression. First, we define the function Λ which allows translating an RDF triple pattern into a MRA expression. Assume that 𝑎, 𝑏, 𝑐 are RDF terms, and ?𝑋 , ?𝑌 , ?𝑍 are variables. Recall that Trip is a multiset d = {𝑆, 𝑃, 𝑂 } is the schema of Trip. relation that is obtained from a set of RDF triples, where Trip Given a triple pattern 𝑇 , the function Λ(𝑇 ) returns a MRA expression defined as follows3 : • if 𝑇 is (?𝑋, 𝑏, 𝑐) then Λ(𝑇 ) returns 𝜋 ?𝑋 (𝜌𝑆/?𝑋 (𝜎𝑃=𝑏∧𝑂=𝑐 (Trip))); 3 These rules are based on Cyganiak’s translation [15].
34
Angles et al.
• if 𝑇 is (𝑎, ?𝑌 , 𝑐) then Λ(𝑇 ) returns 𝜋?𝑌 (𝜌 𝑃 /?𝑌 (𝜎𝑆=𝑎∧𝑂=𝑐 (Trip))); • if 𝑇 is (𝑎, 𝑏, ?𝑍 ) then Λ(𝑇 ) returns 𝜋?𝑍 (𝜌𝑂/?𝑍 (𝜎𝑆=𝑎∧𝑃=𝑏 (Trip))); • if 𝑇 is (?𝑋, ?𝑌 , 𝑐) then Λ(𝑇 ) returns 𝜋?𝑋 ,?𝑌 (𝜌 𝑃 /?𝑌 (𝜌𝑆/?𝑋 (𝜎𝑂=𝑐 (Trip)))); • if 𝑇 is (?𝑋, 𝑏, ?𝑍 ) then Λ(𝑇 ) returns 𝜋?𝑋 ,?𝑍 (𝜌𝑂/?𝑍 (𝜌𝑆/?𝑋 (𝜎𝑃=𝑏 (Trip)))); • if 𝑇 is (𝑎, ?𝑌 , ?𝑍 ) then Λ(𝑇 ) returns 𝜋 ?𝑌 ,?𝑍 (𝜌𝑂/?𝑍 (𝜌 𝑃/?𝑌 (𝜎𝑆=𝑎 (Trip)))); • if 𝑇 is (?𝑋, ?𝑌 , ?𝑍 ) then Λ(𝑇 ) returns 𝜋 ?𝑋 ,?𝑌 ,?𝑍 (𝜌𝑂/?𝑍 (𝜌 𝑃/?𝑌 (𝜌𝑆/?𝑋 (Trip)))); • if 𝑇 is (?𝑋, ?𝑋, 𝑐) then Λ(𝑇 ) returns 𝜋?𝑋 (𝜌𝑆/?𝑋 (𝜎𝑆=𝑃 ∧𝑂=𝑐 (Trip))); • if 𝑇 is (?𝑋, 𝑏, ?𝑋 ) then Λ(𝑇 ) returns 𝜋?𝑋 (𝜌𝑆/?𝑋 (𝜎𝑃 =𝑏∧𝑆=𝑂 (Trip))). • if 𝑇 is (𝑎, ?𝑋, ?𝑋 ) then Λ(𝑇 ) returns 𝜋 ?𝑋 (𝜌 𝑃 /?𝑋 (𝜎𝑆=𝑎∧𝑃=𝑂 (Trip))); • if 𝑇 is (?𝑋, ?𝑋, ?𝑋 ) then Λ(𝑇 ) returns 𝜋 ?𝑋 (𝜌𝑆/?𝑋 (𝜎𝑆=𝑃∧𝑃 =𝑂 (Trip))); Second, we define a function 𝛾 that allows translating a SPARQL filter condition into a MRA selection condition. Like in the translation from SPARQL to NRMD¬ , it is not necessary to translate complex filter conditions (SPARQL) to complex selection formulas (MRA) because SPARQL queries can be normalized to avoid logical connectives. Given an atomic filter condition 𝜑, the function 𝛾 (𝜑) is defined recursively as follows: • If 𝜑 is ?𝑋 = 𝑐 then 𝛾 (𝜑) is (¬(𝑋 = ⊥) ∧ 𝑋 = 𝑐) where 𝑋 is the attribute name corresponding to variable ?𝑋 ; • If 𝜑 is ?𝑋 = ?𝑌 then 𝛾 (𝜑) is ((¬(𝑋 = ⊥) ∧ ¬(𝑌 = ⊥)) ∧ 𝑋 = 𝑌 ) where 𝑋 and 𝑌 are the attribute names corresponding to variables ?𝑋 and ?𝑌 , respectively; • If 𝜑 is bound(𝑋 ) then 𝛾 (𝜑) is ¬(𝑋 = ⊥) where 𝑋 is the attribute name corresponding to variable ?𝑋 . In Definition 8.8, we introduced the relation named Comp to simulate the compatibility between mappings. For example, to simulate the SPARQL query 𝑄 = (𝑃1 AND 𝑃2 ) we need to ensure that check if two pairs of mappings 𝜇1 ∈ J𝑃1 K𝐺 and 𝜇2 ∈ J𝑃2 K𝐺 are compatible, and if they are compatible, return the mapping 𝜇 = 𝜇 1 ∪ 𝜇 2 resulting from joining them. To explain how this operation is simulated with MRA, let inScope(𝑃 1 ) ∩ inScope(𝑃2 ) = {?𝑋 } and tuples 𝑡 1 and 𝑡 2 correspond to mappings 𝜇1 and 𝜇2 . To be compatible, either both mappings map variable ?𝑋 to the same value, or at least for one of the mappings, variable ?𝑋 is unbound. For tuples, an unbound variable ?𝑋 is represented with an attribute value ⊥ (e.g., 𝑡 (𝑋 ) = ⊥). Then, to check if tuples 𝑡 1 and 𝑡 2 are compatible, we need to rename the attribute name 𝑋 corresponding to variable ?𝑋 as two attributes, namely 𝑋 1 and 𝑋 2 and check if there exists a tuple 𝑡 3 in the result of query 𝜌𝐴1 /𝑋1 (𝜌𝐴2 /𝑋2 (Comp)) that agrees with tuples 𝑡 1 and 𝑡 2 (i.e., 𝑡 3 (𝑋 1 ) = 𝑡 1 (𝑋 ) and 𝑡 3 (𝑋 2 ) = 𝑡 2 (𝑋 )) or agrees with either 𝑡 1 or 𝑡 2 whereas for the other tuple the value is ⊥ (e.g., 𝑡 3 (𝑋 1 ) = 𝑡 1 (𝑋 ) and 𝑡 2 (𝑋 2 ) = ⊥). We recover the renamed attribute 𝑋 for the attribute 𝐴 in the relation named Comp. That is, for the compatibility we use the MRA expression 𝜌𝐴/𝑋 (𝜌𝐴1 /𝑋1 (𝜌𝐴2 /𝑋2 (Comp))) which is generalized as follows for multiple common variables in the scope of patterns 𝑃 1 and 𝑃3 . Let X be a finite set of attribute names, and 𝜈 1 and 𝜈 2 be two bijective functions that map each attribute 𝑋 ∈ X to two different sets of attributes (i.e., the ranges of 𝜈 1 and 𝜈 2 are disjoint). Then, we write Comp(𝜈 1, 𝜈 2, X) to denote the join of MRA expressions of the form 𝜌𝐴/𝑋 (𝜌𝐴1 /𝜈1 (𝑋 ) (𝜌𝐴2 /𝜈2 (𝑋 ) (Comp))), for every attribute name 𝑋 ∈ X. b and 𝜈 Let 𝐸 be a MRA expression, and X = {𝑋 1, . . . , 𝑋𝑛 } be a subset of the attribute names in 𝐸, b a bijective function that maps each attribute name in X to a fresh attribute name (i.e., 𝜈 (𝑋 ) ∉ 𝐸). We call 𝜈 (𝐸) to the MRA expression that renames each attribute name 𝑋 ∈ X with 𝜈 (𝑋 ). That is, 𝜈 (𝐸) = 𝜌𝑋1 /𝜈 (𝑋1 ) (· · · 𝜌𝑋𝑛 /𝜈 (𝑋𝑛 ) (𝐸) · · · ).
The multiset semantics of SPARQL patterns
35
Given two MRA expressions 𝐸 1 and 𝐸 2 , assume two bijective functions 𝜈 1 and 𝜈 2 that map each attribute 𝑋 ∈ 𝐸b1 ∩ 𝐸b2 to two fresh attributes (i.e., 𝜈 1 (𝑋 ), 𝜈 2 (𝑋 ) ∉ 𝐸b1 ∪ 𝐸b2 ), and satisfy range(𝜈 1 ) ∩ range(𝜈 2 ) = ∅. Then, we define the MRA operation 𝐸 1 ∗ 𝐸 2 in terms of existing MRA operators as follows: 𝐸 1 ∗ 𝐸 2 = 𝜋𝐸b1 ∪𝐸b2 (Comp(𝜈 1, 𝜈 2, 𝐸b1 ∩ 𝐸b2 ) Z 𝜈 1 (𝐸 1 ) Z 𝜈 2 (𝐸 2 )). Notice that the attribute names in the ranges of functions 𝜇1 and 𝜇2 in the definition of expression 𝐸 1 ∗ 𝐸 2 do not matter because are not in the schema of the multiset that results from expression 𝐸1 ∗ 𝐸2 . To translate SPARQL queries 𝑄 of the form (SELECT X 𝑃) where the set of variables X include a variable that is not in the scope of 𝑃, we need to generate values ⊥ to fill the tuples returned by the translated query. For example, if inScope(𝑃) = {?𝑋 } and X = {?𝑋, ?𝑌 }, then the MRA expression 𝐸 that corresponds to the SPARQL pattern 𝑃 can be extended with an attribute name 𝑌 by joining 𝐸 with the MRA relation 𝜌 𝑁 /𝑌 (Null). Given a set Y = {𝑌1, . . . , 𝑌𝑛 } of attribute names, we define the MRA expression Δ(Y) as 𝜌 𝑁 /𝑌1 (Null) Z · · · Z 𝜌 𝑁 /𝑌𝑛 (Null). Next, we present the translation of SPARQL queries to MRA queries. Definition 8.10 (Function 𝑓13 ). The translation rules in Table 7 define the function 𝑓13 from normalized graph patterns whose filter conditions have no Boolean connectives to MRA queries.
Table 7. Definition of the function 𝑓13 , which takes a normalized SPARQL pattern 𝑃 as input (without logical connectives in filter conditions) and returns an MRA query. SPARQL pattern 𝑃
MRA query 𝑓13 (𝑃)
(𝑠, 𝑝, 𝑜)
Λ(𝑠, 𝑝, 𝑜)
(𝑃1 AND 𝑃2 )
(𝑓13 (𝑃 1 ) ∗ 𝑓13 (𝑃 2 ))
(𝑃1 UNION 𝑃2 )
(𝑓13 (𝑃 1 ) ∪ 𝑓13 (𝑃 2 ))
(𝑃1 EXCEPT 𝑃2 )
(𝑓13 (𝑃 1 ) \ 𝑓13 (𝑃 2 ))
(SELECT inScope(𝑃) 𝑃1 )
𝜋 A (𝑓13 (𝑃 1 ) Z Δ B )
(𝑃1 FILTER 𝜑)
𝜎𝛾 (𝜑 ) (𝑓13 (𝑃 1 ))
where...
A is the set of attribute names corresponding to the variables in set inScope(𝑃) and B is the set of attribute names that correspond to variables that are in set inScope(𝑃) \ inScope(𝑃1 ).
Example 8.11. Let 𝑄 be the following SPARQL query asking for all people, the place where they live, and optionally the people their know (notice that this query is already normalized as we discussed in Example 6.11). (((?𝑝𝑒𝑟𝑠𝑜𝑛, livesIn, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒) AND (?𝑝𝑒𝑟𝑠𝑜𝑛, knows, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦)) UNION ( SELECT ?𝑝𝑒𝑟𝑠𝑜𝑛 ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒 ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦 WHERE ((?𝑝𝑒𝑟𝑠𝑜𝑛, livesIn, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒) EXCEPT ( SELECT ?𝑝𝑒𝑟𝑠𝑜𝑛 ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒 WHERE ((?𝑝𝑒𝑟𝑠𝑜𝑛, livesIn, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒) AND (?𝑝𝑒𝑟𝑠𝑜𝑛, knows, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦)))))).
36
Angles et al.
Then, the corresponding query 𝑓13 (𝑄) is the query (𝑞(𝑋 ), Π) where Π is defined as follows: (Λ(?𝑝𝑒𝑟𝑠𝑜𝑛, livesIn, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒) ∗ Λ(?𝑝𝑒𝑟𝑠𝑜𝑛, knows, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦)) ∪ ((𝜋 Person (Λ(?𝑝𝑒𝑟𝑠𝑜𝑛, livesIn, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒)) \ 𝜋Person (Λ(?𝑝𝑒𝑟𝑠𝑜𝑛, livesIn, ?𝑠𝑜𝑚𝑒𝑤ℎ𝑒𝑟𝑒) ∗ Λ(?𝑝𝑒𝑟𝑠𝑜𝑛, knows, ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦))) Z 𝜌 𝑁 /Somebody (Null)) , where the MRA attributes Person and Somebody correspond to the SPARQL variables ?𝑝𝑒𝑟𝑠𝑜𝑛 and ?𝑠𝑜𝑚𝑒𝑏𝑜𝑑𝑦. 8.2.3 Translating query answers from MRA to SPARQL. Recall that a MRA query answer is a multiset of tuples, and a SPARQL query answer is a multiset of solution mappings. Next, we define the function ℎ 31 that transforms MRA query answers into NRMD¬ query answers. Intuitively, the translation of a MRA tuple 𝑡 as a SPARQL solution mapping 𝜇 consists of removing from tuple 𝑡 every attribute whose value is ⊥, and viewing the result tuple as a SPARQL mapping 𝜇. For example, the result of translating a tuple 𝑡 with 𝑡ˆ = {𝑋, 𝑌 }, 𝑡 (𝑋 ) = 𝑎, and 𝑡 (𝑌 ) = ⊥, is the SPARQL mapping 𝜇 = {?𝑋 ↦→ 𝑎}. Recall that we write ?𝑋 to denote the corresponding SPARQL variable for a MRA attribute 𝑋 . Definition 8.12 (Function ℎ 31 ). Given a MRA tuple 𝑡, we write 𝑓31 (𝑡) to denote the SPARQL mapping 𝜇 such that: (1) 𝜇 (?𝑋 ) = 𝑡 (𝑋 ) if 𝑋 ∈ 𝑡ˆ and 𝑡 (𝑋 ) ≠ ⊥, and (2) variable ?𝑌 is not in dom(𝜇) if 𝑌 ∉ 𝑡ˆ or 𝑡 (𝑌 ) = ⊥. Abusing notation, 𝑓31 (𝑟 ) is also the function that receives a MRA relation 𝑟 and returns the multiset Ω of SPARQL mappings where set(Ω) = {𝜇 | there exist 𝑡 ∈ 𝑟 such that 𝑓31 (𝑡) = 𝜇} and the cardinality of mapping 𝑓31 (𝑡) in Ω is the cardinality of tuple 𝑡 in 𝑟 . Lemma 8.13. SPARQL can be simulated by MRA. Proof. This is a long but straightforward induction on the structure of SPARQL queries using as hypothesis that (𝑓13, 𝑔13, ℎ 13 ) is a simulation of SPARQL by MRA. The details of this proof are in the appendix (Claim 9). □ 8.3
MRA and SPARQL have the same expressive power
Putting together the simulations among MRA and SPARQL stated in this section, we get the following theorem: Theorem 8.14. MRA and SPARQL have the same expressive power. Proof. The claim is based on the simulation of MRA with SPARQL (Lemma 8.7) and the simulation of SPARQL with MRA (Lemma 8.13). □ 9
Conclusions
We studied the algebraic and logic structure of the multiset semantics of the core SPARQL patterns, and compared it to the classical and well-studied formalisms of multiset relational algebra and multiset Datalog. Our motivation was to shed light on the underlying theoretical structure of the multiset features of SPARQL that could help improve future designs and implementations. In this regard, the main discoveries of this research are: (1) the core fragment of SPARQL patterns matches precisely the multiset semantics of Datalog as defined by Mumick et al. [37]; and (2) this logical structure corresponds to a simple multiset algebra, namely the Multiset Relational Algebra (MRA). These correspondences, besides showing a nice parallel to the one exhibited by classical set relational algebra and relational calculus, and thus transferring theoretical guarantees from these well-studied formalisms, could help to give new insights on possible optimizations and future extensions of SPARQL.
The multiset semantics of SPARQL patterns
37
We think there are a couple of lessons learned in the investigation of the multiset features of SPARQL. First, contrary to the rather chaotic variety of multiset operators in SQL, it is interesting to observe that the SPARQL design comprises a more coherent body of multiset operators. We suggest that this asset should be considered and curated by designers in order to try to keep this clean design in future extensions of SPARQL. Second, there is a challenging goal for query language designers that work with multisets: existing a diversity of multiset extensions for each of the classical set operators, it is not evident at all from a theoretical perspective how to develop a logically coherent formalism that could integrate all or most of them. Our study shows that there are fragments that behave coherently, but that operators that do not fit in this schema, when available (not always), have to be accessed in a very ad-hoc manner. Last but not least, this study shows (and adds evidence of) the complexities and challenges that the introduction of multisets brings to query languages, exemplified here in the case of SPARQL. Much more use cases are needed in order to match the theoretical restrictions and recommendations (e.g. as studied in this paper), and real-life use cases that to the best of our knowledge do not have yet a good systematization. Acknowledgments R. Angles was supported by ANID FONDECYT Chile through grant 1221727. D. Hernández was partially supported by the German Research Foundation, Deutsche Forschungsgemeinschaft (DFG), grant SFB-1574-471687386. This work was partly funded by ANID - Millennium Science Initiative Program - Code ICN17_002. References [1] Serge Abiteboul, Richard Hull, and Victor Vianu. 1995. Foundations of Databases. Addison-Wesley. [2] Foto N. Afrati, Matthew Damigos, and Manolis Gergatsoulis. 2010. Query Containment Under Bag and Bag-set Semantics. Inform. Process. Lett. 110, 10 (2010), 360–369. https://doi.org/10.1016/j.ipl.2010.02.017 [3] Joseph Albert. 1991. Algebraic Properties of Bag Data Types. In Proc. of the Int. Conference on Very Large Data Bases (VLDB). Morgan Kaufmann Publishers Inc., San Francisco, CA, USA, 211–219. https://dl.acm.org/doi/10.5555/645917. 672310 [4] Renzo Angles, Georg Gottlob, Aleksandar Pavlović, Reinhard Pichler, and Emanuel Sallinger. 2023. SparqLog: A System for Efficient Evaluation of SPARQL 1.1 Queries via Datalog. Proc. VLDB Endow. 16, 13 (2023), 4240–4253. https://doi.org/10.14778/3625054.3625061 [5] Renzo Angles and Claudio Gutiérrez. 2008. The Expressive Power of SPARQL. In Proc. of the International Semantic Web Conference (ISWC) (LNCS, Vol. 5318). Springer, 114–129. https://doi.org/10.1007/978-3-540-88564-1_8 [6] Renzo Angles and Claudio Gutiérrez. 2016. The Multiset Semantics of SPARQL Patterns. In 15th International Semantic Web Conference (ISWC) (LNCS, Vol. 9981). Springer, 20–36. https://doi.org/10.1007/978-3-319-46523-4_2 [7] Renzo Angles and Claudio Gutierrez. 2016. Negation in SPARQL. In Alberto Mendelzon Int. Workshop on Foundations of Data Management (AMW). https://ceur-ws.org/Vol-1644/paper11.pdf [8] Luigi Bellomarini, Emanuel Sallinger, and Georg Gottlob. 2018. The Vadalog system: datalog-based reasoning for knowledge graphs. Proc. VLDB Endow. 11, 9 (2018), 975–987. https://doi.org/10.14778/3213880.3213888 [9] Leopoldo Bertossi, Georg Gottlob, and Reinhard Pichler. 2019. Datalog: Bag Semantics via Set Semantics. In International Conference on Database Theory (ICDT), Vol. 127. Schloss Dagstuhl–Leibniz-Zentrum fuer Informatik, Dagstuhl, Germany, 16:1–16:19. https://doi.org/10.4230/LIPIcs.ICDT.2019.16 [10] Val Breazu-Tannen and Ramesh Subrahmanyam. 1991. Logical and computational aspects of programming with sets/bags/lists. In Automata, Languages and Programming. Springer Berlin Heidelberg, Berlin, Heidelberg, 60–75. [11] Artem Chebotko, Shiyong Lu, and Farshad Fotouhi. 2009. Semantics preserving SPARQL-to-SQL translation. Data & Knowledge Engineering 68, 10 (2009), 973–1000. https://doi.org/10.1016/j.datak.2009.04.001 [12] Sara Cohen. 2009. Equivalence of Queries That Are Sensitive to Multiplicities. The VLDB Journal 18, 3 (June 2009), 765–785. https://doi.org/10.1007/s00778-008-0122-1 [13] Latha S. Colby and Leonid Libkin. 1997. Tractable iteration mechanisms for bag languages. In International Conferencia on Database Theory (ICDT) (LNCS, Vol. 1186). Springer Berlin Heidelberg, Berlin, Heidelberg, 461–475. https://doi.org/ 10.1007/3-540-62222-5_64
38
Angles et al.
[14] Marco Console, Paolo Guagliardo, and Leonid Libkin. 2022. Fragments of bag relational algebra: Expressiveness and certain answers. Information Systems (2022). https://doi.org/10.1016/j.is.2020.101604 [15] Richard Cyganiak. 2005. A relational algebra for SPARQL. Technical Report HPL-2005-170. HP Labs. [16] C. J. Date. 2006. Date on Database: Writings 2000-2006. APress. [17] Umeshwar Dayal, Nathan Goodman, and Randy H. Katz. 1982. An Extended Relational Algebra with Control over Duplicate Elimination. In Proc. of the Symposium on Principles of Database Systems (PODS). ACM, 117–123. https://doi.org/10.1145/588111.588132 [18] Floris Geerts, Thomas Unger, Grigoris Karvounarakis, Irini Fundulaki, and Vassilis Christophides. 2016. Algebraic Structures for Capturing the Provenance of SPARQL Queries. J. ACM 63, 1 (2016), 63 pages. https://doi.org/10.1145/ 2810037 [19] Todd J. Green. 2009. Bag Semantics. In Encyclopedia of Database Systems. 201–206. [20] Stéphane Grumbach, Leonid Libkin, Tova Milo, and Limsoon Wong. 1996. Query languages for bags: expressive power and complexity. SIGACT News 27, 2 (1996), 30–44. https://doi.org/10.1145/235767.235770 [21] Stéphane Grumbach and Tova Milo. 1996. Towards Tractable Algebras for Bags. J. Comput. System Sci. 52, 3 (1996), 570–588. https://doi.org/10.1006/jcss.1996.0042 [22] Paolo Guagliardo and Leonid Libkin. 2017. A Formal Semantics of SQL Queries, Its Validation, and Applications. Proc. VLDB Endow. 11, 1 (2017), 27–39. https://doi.org/10.14778/3151113.3151116 [23] Steve Harris and Andy Seaborne. 2013. SPARQL 1.1 Query Language - W3C Recommendation. http://www.w3.org/TR/2013/REC-sparql11-query-20130321/. [24] A. Hernich and P. G. Kolaitis. 2017. Foundations of information integration under bag semantics. In 32nd Annual ACM/IEEE Symposium on Logic in Computer Science (LICS). 1–12. https://doi.org/10.1109/LICS.2017.8005104 [25] Daniel Hernández. 2020. The Problem of Incomplete Data in SPARQL. Ph.D. dissertation. Universidad de Chile - Faculty of Physical and Mathematical Sciences, Santiago, Chile. https://repositorio.uchile.cl/handle/2250/178033 [26] Aidan Hogan, Marcelo Arenas, Alejandro Mallea, and Axel Polleres. 2014. Everything You Always Wanted to Know About Blank Nodes. Journal of Web Semantics 27, 1 (2014). https://doi.org/10.1016/j.websem.2014.06.004 [27] Mark Kaminski, Egor V. Kostylev, and Bernardo Cuenca Grau. 2016. Semantics and Expressive Power of Subqueries and Aggregates in SPARQL 1.1. In Proc. of the International Conference on World Wide Web. 227–238. [28] Mark Kaminski, Egor V. Kostylev, and Bernardo Cuenca Grau. 2016. Semantics and Expressive Power of Subqueries and Aggregates in SPARQL 1.1.. In Proceedings of the Int. Conference on World Wide Web (WWW). ACM, 227–238. https://doi.org/10.1145/2872427.2883022 [29] Aviel Klausner and Nathan Goodman. 1985. Multirelations - Semantics and languages. In Proc. of Int. Conference on Very Large Data Bases (VLDB). VLDB Endowment, 251–258. https://dl.acm.org/doi/10.5555/1286760.1286783 [30] Roman Kontchakov and Egor V. Kostylev. 2016. On Expressibility of Non-Monotone Operators in SPARQL. In Int. Conference on the Principles of Knowledge Representation and Reasoning. AAAI Press, 369–378. https://dl.acm.org/doi/ 10.5555/3032027.3032071 [31] G. Lamperti, M. Melchiori, and M. Zanella. 2001. On Multisets in Database Systems. In Proceedings of the Workshop on Multiset Processing. 147–216. https://dl.acm.org/doi/10.5555/647269.721839 [32] Leonid Libkin and Limsoon Wong. 1994. Some Properties of Query Languages for Bags. In Proc. of the Int. Workshop on Database Programming Languages (DBPL) - Object Models and Languages. 97–114. https://doi.org/10.1007/978-14471-3564-7_7 [33] Leonid Libkin and Limsoon Wong. 1997. Query languages for bags and aggregate functions. J. Comput. System Sci. 55, 2 (1997), 241–272. https://doi.org/10.1006/jcss.1997.1523 [34] J. W. Lloyd. 1998. Programming with multisets. Technical Report. University of Bristol. [35] J. Melton and A. R. Simon. 2002. SQL:1999. Understanding Relational Language Components. Morgan Kaufmann Publ. [36] I. S. Mumick, S. J. Finkelstein, Hamid Pirahesh, and Raghu Ramakrishnan. 1990. Magic is Relevant. SIGMOD Rec. 19, 2 (1990), 247–258. https://doi.org/10.1145/93605.98734 [37] Inderpal Singh Mumick, Hamid Pirahesh, and Raghu Ramakrishnan. 1990. The Magic of Duplicates and Aggregates. In Proc. of the International Conference on Very Large Data Bases. Morgan Kaufmann Publishers Inc., San Francisco, CA, USA, 264–277. https://dl.acm.org/doi/10.5555/94362.94426 [38] Jorge Pérez, Marcelo Arenas, and Claudio Gutierrez. 2006. Semantics of SPARQL. Technical Report TR/DCC-2006-17. Department of Computer Science, University of Chile. [39] A. Polleres. 2007. From SPARQL to Rules (and back). In Proceedings of the 16th Int. World Wide Web Conference (WWW). ACM, 787–796. https://doi.org/10.1145/1242572.1242679 [40] Axel Polleres and Johannes Peter Wallner. 2013. On the relation between SPARQL1.1 and Answer Set Programming. Journal of Applied Non-Classical Logics 23, 1-2 (2013), 159–212. https://doi.org/10.1080/11663081.2013.798992 [41] Eric Prud’hommeaux and Andy Seaborne. 2008. SPARQL Query Language for RDF. W3C Recommendation. http://www.w3.org/TR/2008/REC-115-sparql-query-20080115/.
The multiset semantics of SPARQL patterns
39
[42] Wilmer Ricciotti and James Cheney. 2019. Mixing Set and Bag Semantics. In Proc. 17th ACM SIGPLAN International Symposium on Database Programming Languages (DBPL) (Phoenix, AZ, USA). ACM, New York, NY, USA, 70–73. https://doi.org/10.1145/3315507.3330202 [43] Simon Schenk. 2007. A SPARQL Semantics Based on Datalog. In Annual German Conference on Advances in Artificial Intelligence, Vol. 4667. 160–174. https://doi.org/10.1007/978-3-540-74565-5_14 [44] Michael Schmidt, Michael Meier, and Georg Lausen. 2010. Foundations of SPARQL query optimization. In Proc. of the Int. Conference on Database Theory. ACM, 4–33. https://doi.org/10.1145/1804669.1804675 [45] Xiaowang Zhang and Jan Van den Bussche. 2014. On the primitivity of operators in SPARQL. Inf. Process. Lett. 114, 9 (2014), 480–485. https://doi.org/10.1016/j.ipl.2014.03.014
A
Variable renaming in SPARQL
This appendix section defines function subs(·, ·), which renames SPARQL variables. This function is used to simulate the MRA operator renaming 𝜌𝐴/𝐵 (see Table 6). Note that function subs(·, ·) is not an additional algebraic operation but an operation over expressions (i.e., a query rewriting). Intuitively, given a MRA query 𝑄, a SPARQL pattern 𝑃 that simulates 𝑄, a renaming of MRA attributes 𝐴/𝐵 and a renaming of variables ?𝑋 /?𝑌 where ?𝑋 and ?𝑌 are the corresponding variables for attributes 𝐴 and 𝐵, the query rewriting subs?𝑋 /?𝑌 (𝑃) simulates the MRA query 𝜌𝐴,𝐵 (𝑄). To this end, SPARQL variables are renamed in the pattern, instead of renaming query result attributes as MRA does. Definition A.1 (SPARQL Variable Renaming). Given two SPARQL variables ?𝑋 and ?𝑌 , we define the function 𝜈 ?𝑋 /?𝑌 : I ∪ L ∪ V → I ∪ L ∪ V as the function such that 𝜈 ?𝑋 /?𝑌 (?𝑋 ) = ?𝑌 and 𝜈 ?𝑋 /?𝑌 (𝑠) = 𝑠, for every 𝑠 ∈ (I ∪ L ∪ V) \ {?𝑋 }. Given a SPARQL pattern 𝑃 and two SPARQL variables ?𝑋 ∈ inScope(𝑃) and ?𝑌 ∉ inScope(𝑃), we write subs?𝑋 /?𝑌 (𝑃) to denote the pattern defined recursively as follows: (1) If 𝑃 is a triple pattern (𝑠, 𝑝, 𝑜) then subs?𝑋 /?𝑌 (𝑃) = (𝜈 ?𝑋 /?𝑌 (𝑠), 𝜈 ?𝑋 /?𝑌 (𝑝), 𝜈 ?𝑋 /?𝑌 (𝑜)). (2) If 𝑃 has the form (𝑃1 AND 𝑃2 ) then subs?𝑋 /?𝑌 (𝑃) = subs?𝑋 /?𝑌 (𝑃 1 ) AND subs?𝑋 /?𝑌 (𝑃 2 ). (3) If 𝑃 has the form (𝑃1 UNION 𝑃2 ) then subs?𝑋 /?𝑌 (𝑃) = subs?𝑋 /?𝑌 (𝑃 1 ) UNION subs?𝑋 /?𝑌 (𝑃 2 ). (4) If 𝑃 has the form (𝑃 1 EXCEPT 𝑃2 ) then subs?𝑋 /?𝑌 (𝑃) = subs?𝑋 /?𝑌 (𝑃 1 ) EXCEPT subs?𝑋 /?𝑌 (𝑃 2 ). (5) If 𝑃 has the form (𝑃1 FILTER 𝜑) then subs?𝑋 /?𝑌 (𝑃) = (subs?𝑋 /?𝑌 (𝑃 1 ) FILTER 𝜈 ?𝑋 /?𝑌 (𝜑)) where, abusing of notation, 𝜈 ?𝑋 /?𝑌 (𝜑) is the selection formula defined recursively as follows: (a) If 𝜑 has the form 𝑎 = 𝑏, where 𝑎, 𝑏 ∈ V ∪ I ∪ I, then 𝜈 ?𝑋 /?𝑌 (𝜑) = 𝜈 ?𝑋 /?𝑌 (𝑎) = 𝜈 ?𝑋 /?𝑌 (𝑏). (b) If 𝜑 has the form bound(?𝑥) then 𝜈 ?𝑋 /?𝑌 (𝜑) = bound(𝜈 ?𝑋 /?𝑌 (?𝑥)). (c) If 𝜑 has the form 𝜓 1 ∧ 𝜓 2 then 𝜈 ?𝑋 /?𝑌 (𝜑) = 𝜈 ?𝑋 /?𝑌 (𝜓 1 ) ∧ 𝜈 ?𝑋 /?𝑌 (𝜓 2 ). (d) If 𝜑 has the form 𝜓 1 ∨ 𝜓 2 then 𝜈 ?𝑋 /?𝑌 (𝜑) = 𝜈 ?𝑋 /?𝑌 (𝜓 1 ) ∨ 𝜈 ?𝑋 /?𝑌 (𝜓 2 ). (e) If 𝜑 has the form ¬𝜓 then 𝜈 ?𝑋 /?𝑌 (𝜑) = ¬𝜈 ?𝑋 /?𝑌 (𝜓 ). (6) If 𝑃 has the form (SELECT𝑊 WHERE 𝑃1 ) then: (a) If ?𝑌 ∉ inScope(𝑃1 ), then subs?𝑋 /?𝑌 (𝑃) = (SELECT (𝑊 \ {?𝑋 } ∪ {?𝑌 }) WHERE 𝑃 1 ). (b) Otherwise, subs?𝑋 /?𝑌 (𝑃) = (SELECT (𝑊 \ {?𝑋 } ∪ {?𝑌 }) WHERE subs?𝑌 /𝑍 (𝑃 1 )), where ?𝑍 is a fresh variable. We rename variable ?𝑌 as ?𝑍 when is not in-scope of 𝑃 to avoid a variable name clash. B B.1
Proof of claims Error filter condition
Claim Ô 1. For every SPARQL formula 𝜑, the formula Error(𝜑) can be expressed as a formula of the form 𝜓 ∈𝐶 𝜓 where 𝐶 is a non-empty set of conjunctions of formulas belonging to one of the following types:
40
Angles et al.
Table 8. Truth values for the error formula of a conjunction. According to Definition 6.4, given a formula 𝜑 of the form 𝜑 1 ∧ 𝜑 2 , the formula Error(𝜑) is the formula 𝜓 1 ∨ 𝜓 2 ∨ 𝜓 3 where 𝜓 1 is the formula (𝜑 1 ∧ Error(𝜑 2 )), 𝜓 2 is the formula (Error(𝜑 1 ) ∧ 𝜑 2 ), and 𝜓 3 is the formula (Error(𝜑 1 ) ∧ Error(𝜑 2 )). Given an arbitrary mapping 𝜇, this table shows the possible truth values for formulas 𝜑, Error(𝜑), and its components. 𝜇 (𝜑 1 )
𝜇 (𝜑 2 )
𝜇 (𝜑)
𝜇 (Error(𝜑 1 ))
𝜇 (Error(𝜑 2 ))
true true true false false false error error error
true false error true false error true false error
true false error false false false error false error
false or error false or error false or error false or error false or error false or error true true true
false or error false or error true false or error false or error true false or error false or error true
𝜇 (𝜓 1 )
𝜇 (𝜓 2 )
𝜇 (𝜓 3 )
false or error false or error false or error false or error false false or error true false or error false or error false false or error false or error false false false or error false false or error false or error false or error true false or error false or error false false or error error error true
𝜇 (Error(𝜑)) false or error false or error true false or error false or error false or error true false or error true
Table 9. Truth values for the error formula of a disjunction. According to Definition 6.4, given a formula 𝜑 of the form 𝜑 1 ∨𝜑 2 , the formula Error(𝜑) is the formula 𝜓 1 ∨𝜓 2 ∨𝜓 3 where 𝜓 1 is the formula (¬𝜑 1 ∧Error(𝜑 2 )), 𝜓 2 is the formula (Error(𝜑 1 ) ∧ ¬𝜑 2 ), and 𝜓 3 is the formula (Error(𝜑 1 ) ∧ Error(𝜑 2 )). Given an arbitrary mapping 𝜇, this table shows the possible truth values for formulas 𝜑, Error(𝜑), and its components. 𝜇 (𝜑 1 )
𝜇 (𝜑 2 )
𝜇 (𝜑)
𝜇 (Error(𝜑 1 ))
𝜇 (Error(𝜑 2 ))
true true true false false false error error error
true false error true false error true false error
true true true true false error true error error
false or error false or error false or error false or error false or error false or error true true true
false or error false or error true false or error false or error true false or error false or error true
𝜇 (𝜓 1 )
𝜇 (𝜓 2 )
𝜇 (𝜓 3 )
false false false or error false false or error false or error false false or error false or error false or error false false or error false or error false or error false or error true false or error false or error false or error false false or error false or error true false or error error error true
𝜇 (Error(𝜑)) false or error false or error false or error false or error false or error true false or error true true
(1) positive or negative literals (i.e., formulas of the form false, ?𝑋 = 𝑎, ¬(?𝑋 = 𝑎), ¬(?𝑋 = ?𝑌 ), bound(?𝑋 ), or ¬ bound(?𝑋 )), (2) formulas 𝜑 ′ , ¬𝜑 ′ , or Error(𝜑 ′ ) such that 𝜑 ′ occurs in 𝜑 and 𝜑 ′ is strictly smaller than 𝜑; and for every mapping 𝜇, 𝜇 (𝜑) = error if and only if there exists a unique formula 𝜓 ∈ 𝐶 for which 𝜇 (𝜓 ) = true. Proof. We next show this result by induction on the structure of the query. (1) If 𝜑 has the form bound(?𝑋 ) then Error(𝜑) is the formula false. Formula 𝜑 satisfies the claim. Indeed, 𝐶 = {false} and 𝜇 (Error(𝜑)) = false for every mapping 𝜇 because formula 𝜑 does not produce error. (2) If 𝜑 has the form ?𝑋 = 𝑎 then Error(𝜑) is the formula ¬ bound(?𝑋 ). Formula 𝜑 satisfies the claim. Indeed, 𝐶 = {¬ bound(?𝑋 )} and 𝜇 (Error(𝜑)) = true if and only if variable ?𝑋 is unbound in 𝜇, that is the unique case when formula 𝜑 produces error. (3) If 𝜑 has the form ?𝑋 = ?𝑌 then Error(𝜑) is the formula 𝜓 1 ∨ 𝜓 2 ∨ 𝜓 3 where 𝜓 1 is the formula (¬ bound(?𝑋 ) ∧ bound(?𝑌 )), 𝜓 2 is the formula (bound(?𝑋 ) ∧ ¬ bound(?𝑌 )), and 𝜓 3 is the formula (¬ bound(?𝑋 ) ∧ ¬ bound(?𝑌 )). Formula 𝜑 satisfies the claim. Indeed, 𝐶 = {𝜓 1,𝜓 2,𝜓 3 }, and by construction, only one formula in 𝐶 can be true, and 𝜇 (Error(𝜑)) = true if and only if 𝜇 (𝜑) = error. (4) If 𝜑 has the form ¬𝜑 1 then Error(𝜑) is the formula Error(𝜑 1 ). In this case 𝐶 = {Error(𝜑 1 )}. By the induction hypothesis, 𝜇 (Error(𝜑 1 )) = true if and only if 𝜇 (𝜑 1 ) = error. Because
The multiset semantics of SPARQL patterns
41
¬ error is error, we conclude that 𝜇 (Error(𝜑)) = true if and only if 𝜇 (𝜑) = error. Hence, formula 𝜑 satisfies the claim. (5) If 𝜑 has the form 𝜑 1 ∧ 𝜑 2 then Error(𝜑) is the formula 𝜓 1 ∨ 𝜓 2 ∨ 𝜓 3 where 𝜓 1 is the formula (𝜑 1 ∧ Error(𝜑 2 )), 𝜓 2 is the formula (Error(𝜑 1 ) ∧ 𝜑 2 ), and 𝜓 3 is the formula (Error(𝜑 1 ) ∧ Error(𝜑 2 )). The validity of the claim for formula 𝜑 is shown in Table 8. There are three cases where 𝜇 (𝜑) = error: Case ET: 𝜇 (𝜑 1 ) = error and 𝜇 (𝜑 2 ) = true, Case TE: 𝜇 (𝜑 1 ) = true and 𝜇 (𝜑 2 ) = error, Case EE: 𝜇 (𝜑 1 ) = error and 𝜇 (𝜑 2 ) = error. Note that if 𝜇 (𝜑 1 ) = error and 𝜇 (𝜑 2 ) = false then 𝜇 (𝜑) = false (because error ∧ false is false). The values in the remaining columns can be computed using the inductive hypothesis. We next present case ET as an example. The other cases follow the same reasoning. In case ET, 𝜇 (𝜑 1 ) = error and 𝜇 (𝜑 2 ) = true. By the induction hypothesis, 𝜇 (Error(𝜑 1 )) = true and 𝜇 (Error(𝜑 2 )) is either false or error. (a) If 𝜇 (Error(𝜑 2 )) = false then: 𝜇 (𝜓 1 ) = 𝜇 (𝜑 1 ) ∧ 𝜇 (𝐸𝑟𝑟𝑜𝑟 (𝜑 2 )) = error ∧ false = false . 𝜇 (𝜓 2 ) = 𝜇 (𝐸𝑟𝑟𝑜𝑟 (𝜑 1 )) ∧ 𝜇 (𝜑 2 ) = true ∧ true = true . 𝜇 (𝜓 3 ) = 𝜇 (𝐸𝑟𝑟𝑜𝑟 (𝜑 1 )) ∧ 𝜇 (Error(𝜑 2 )) = true ∧ false = false . Hence, 𝜇 (Error(𝜑)) = 𝜇 (𝜓 1 ∨ 𝜓 2 ∨ 𝜓 3 ) = false ∨ true ∨ false = true. (b) If 𝜇 (Error(𝜑 2 )) = error then: 𝜇 (𝜓 1 ) = 𝜇 (𝜑 1 ) ∧ 𝜇 (𝐸𝑟𝑟𝑜𝑟 (𝜑 2 )) = error ∧ error = error . 𝜇 (𝜓 2 ) = 𝜇 (𝐸𝑟𝑟𝑜𝑟 (𝜑 1 )) ∧ 𝜇 (𝜑 2 ) = true ∧ true = true . 𝜇 (𝜓 3 ) = 𝜇 (𝐸𝑟𝑟𝑜𝑟 (𝜑 1 )) ∧ 𝜇 (Error(𝜑 2 )) = true ∧ error = error . Hence, 𝜇 (Error(𝜑)) = 𝜇 (𝜓 1 ∨ 𝜓 2 ∨ 𝜓 3 ) = error ∨ true ∨ error = true. (6) If 𝜑 has the form 𝜑 1 ∨ 𝜑 2 then the validity of the claim for formula 𝜑 is shown in Table 9, following the same reasoning as for the previous case where 𝜑 is a conjunction 𝜑 1 ∧ 𝜑 2 . □ B.2
Reduction of complex filter conditions
To prove the following claims, we introduce the notion of reduction and reducible filter condition. Section 6.1.2 presents three equivalences to transform a pattern with complex filter conditions into a pattern where all filter conditions are atomic. In this appendix, we show that these equivalences can be used to this end. For each equivalence (𝑃 FILTER 𝜑) ≡ 𝑃 ′ , we define a function that maps the filter condition 𝜑 to the set Σ𝜑 of filter conditions in pattern 𝑃 ′ .
42
Angles et al.
Consider the following equivalences: (𝑃 FILTER𝜓 1 ∧ 𝜓 2 ) ≡ ((𝑃 FILTER𝜓 1 ) FILTER𝜓 2 ), (𝑃 FILTER𝜓 1 ∨ 𝜓 2 ) ≡ (𝑃 FILTER𝜓 1 ∧ 𝜓 2 ) UNION (𝑃 FILTER𝜓 1 ∧ ¬𝜓 2 ) UNION (𝑃 FILTER ¬𝜓 1 ∧ 𝜓 2 ) UNION (𝑃 FILTER𝜓 1 ∧ Error(𝜓 2 )) UNION (𝑃 FILTER Error(𝜓 1 ) ∧ 𝜓 2 ), (𝑃 FILTER ¬𝜓 ) ≡ ((𝑃 EXCEPT (𝑃 FILTER𝜓 )) EXCEPT (𝑃 FILTER Error(𝜓 ))). These three equivalences define the following functions, called reduction rules: {𝜓 1, 𝜓 2 } if 𝜑 has the form 𝜓 1 ∧ 𝜓 2, 𝑓∧ (𝜑) = {𝜑 } otherwise; if 𝜑 has the form 𝜓 1 ∨ 𝜓 2, {𝜓 1 ∧ 𝜓 2, 𝜓 1 ∧ ¬𝜓 2, 𝜓 1 ∧ Error(𝜓 2 ), ¬𝜓 1 ∧ 𝜓 2, Error(𝜓 1 ) ∧ 𝜓 2 } 𝑓∨ (𝜑) = {𝜑 } otherwise; {𝜓, Error(𝜓 )} if 𝜑 has the form ¬𝜓, 𝑓¬ (𝜑) = {𝜑 } otherwise; Note that if the filter condition 𝜑 does not have the form of filter condition on the left side of the identity, we return the set {𝜑 }. This captures the fact that the equivalence cannot be applied to reduce filter condition 𝜑. For convenience, we also define the reduction function that eliminates atomic formulas 𝑓◦ and a reduction that composes 𝑓∨ with 𝑓∧ , called 𝑓∨∧ . {𝜑 } if 𝜑 is a complex filter condition, 𝑓◦ (𝜑) = ∅ if 𝜑 is an atomic filter condition; {𝜓 1,𝜓 2, ¬𝜓 1, ¬𝜓 2, Error(𝜓 1 ), Error(𝜓 2 )} if 𝜑 has the form 𝜓 1 ∨ 𝜓 2, 𝑓∨∧ (𝜑) = {𝜑 } otherwise. For 𝑟 ∈ {∧, ∨, ¬, ◦, ∨∧}, let 𝐹𝑟 be the function that receives a set of filter conditions Σ and returns Ð the set of filter conditions 𝐹𝑟 (Σ) = 𝜑 ∈Σ 𝑓𝑟 (𝜑). Given two sets of filter conditions Σ1 and Σ2 we 𝑟
𝑟
write Σ1 → − Σ2 if 𝐹𝑟 (Σ1 ) = Σ2 . In this case, we say that Σ1 → − Σ2 is a reduction. We said that a 𝑟𝑛 𝑟1 𝑟2 filter condition 𝜑 is reducible if there is a finite sequence of reductions {𝜑 } −→ Σ1 −→ · · · −→ ∅. Intuitively, reductions are applied until all complex filter conditions are eliminated. It is not difficult to see that we can apply the aforementioned equivalences to transform every pattern 𝑃 1 to a pattern 𝑃2 with no complex formulas if and only if every filter condition 𝜑 is reducible. We next prove that every filter condition is reducible by induction on the structure of the filter condition. For this induction, we define the components of a filter condition 𝜑, denoted comp(𝜑), to be the set of filter conditions defined as follows: If 𝜑 is atomic, then comp(𝜑) = ∅; if 𝜑 = 𝜓 1 ∨ 𝜓 2 or 𝜑 = 𝜓 1 ∧ 𝜓 2 , then comp(𝜑) = {𝜓 1,𝜓 2 } ∪ comp(𝜓 1 ) ∪ comp(𝜓 2 ); and if 𝜑 = ¬𝜓 then comp(𝜑) = {𝜓 } ∪ comp(𝜓 ). Claim 2. Every filter condition 𝜑 is reducible. Proof. We prove this by induction using the following hypothesis: if 𝜑 is a filter condition where for each filter condition 𝜓 ∈ comp(𝜑), 𝜓 and Error(𝜓 ) are reducible, then the filter conditions 𝜑 and Error(𝜑) are reducible.
The multiset semantics of SPARQL patterns
43
(1) If 𝜑 is bound(?𝑋 ) then ◦
{𝜑 } → − ∅, ◦
{Error(𝜑)} = {false} → − ∅. (2) If 𝜑 is ?𝑋 = 𝑎 then ◦
{𝜑 } → − ∅, ¬
◦
{Error(𝜑)} = {¬ bound(?𝑋 )} − → {bound(?𝑋 ), Error(¬ bound(?𝑋 ))} = {bound(?𝑋 ), false} → − ∅. (3) If 𝜑 is ?𝑋 = ?𝑌 then ◦
{𝜑 } → − ∅, {Error(𝜑)} = {¬ bound(?𝑋 ) ∨ ¬ bound(?𝑌 )} ∨∧
−−→ { bound(?𝑋 ), bound(?𝑌 ), ¬ bound(?𝑋 ), ¬ bound(?𝑌 ), Error(¬ bound(?𝑋 )), Error(¬ bound(?𝑌 ))} = {bound(?𝑋 ), bound(?𝑌 ), ¬ bound(?𝑋 ), ¬ bound(?𝑌 ), 𝑓 𝑎𝑙𝑠𝑒} ◦
→ − {¬ bound(?𝑋 ), ¬ bound(?𝑌 )} ¬
− → {bound(?𝑋 ), Error(bound(?𝑋 )), bound(?𝑌 ), Error(bound(?𝑌 ))} = {bound(?𝑋 ), false, bound(?𝑌 ), false} ◦
→ − ∅. (4) If 𝜑 is ¬𝜓 1 then ¬
{𝜑 } − → {𝜓, Error(𝜓 )}, {Error(𝜑)} = {Error(𝜓 )}. Since 𝜓 ∈ comp(𝜑) and by inductive hypothesis, the filter conditions 𝜓 and Error(𝜓 ) are reducible. Hence, the filter conditions 𝜑 and Error(𝜑) are reducible. (5) If 𝜑 is 𝜓 1 ∧ 𝜓 2 then ∧
{𝜑 } − → {𝜓 1, 𝜓 2 }, {Error(𝜑)} = {Error(𝜓 ) ∨ Error(𝜓 2 )} ∨∧
−−→ { Error(𝜓 1 ), Error(𝜓 2 ), ¬ Error(𝜓 1 ), ¬ Error(𝜓 2 ), Error(Error(𝜓 1 )), Error(Error(𝜓 2 ))} = {Error(𝜓 1 ), Error(𝜓 2 ), ¬ Error(𝜓 1 ), ¬ Error(𝜓 2 ), false}. Since 𝜓 1,𝜓 2 ∈ comp(𝜑) and by the induction hypothesis, the filter conditions Error(𝑝𝑠𝑖 1 ) and Error(𝜓 2 ) are reducible. To show that 𝜑 is reducible, we have to show that ¬ Error(𝜓 1 ) and ¬ Error(𝜓 2 ) are reducible. ¬
{¬ Error(𝜓 1 )} − → {Error(𝜓 ), Error(Error(𝜓 2 ))} = {Error(𝜓 ), 𝑓 𝑎𝑙𝑠𝑒}. By the induction hypothesis, Error(𝜓 ) is reducible. Hence, ¬ Error(𝜓 1 ) is reducible. Similarly, ¬ Error(𝜓 1 ) is reducible. Then, Error(𝜑) is reducible.
44
Angles et al.
(6) Let 𝜑 be 𝜓 1 ∨ 𝜓 2 . First, we show that 𝜑 is reducible. ∨∧
{𝜑 } −−→ {𝜓 1,𝜓 2, ¬𝜓 1, ¬𝜓 2, Error(𝜓 1 ), Error(𝜓 2 )} By the induction hypothesis on 𝜓 1 and 𝜓 2 , 𝜓 1 , 𝜓 2 , Error(𝜓 1 ), and Error(𝜓 2 ) are reducible. To prove that 𝜑 is reducible, suffices to prove that ¬𝜓 1 and ¬𝜓 2 are reducible. ¬
{¬𝜓 1 } − → {𝜓 1, Error(𝜓 1 )}. By the induction hypothesis in 𝜓 1 , 𝜓 1 and Error(𝜓 1 ) are reducible. Hence, ¬𝜓 1 is reducible. Similarly, ¬𝜓 2 is reducible. Hence, 𝜑 is reducible. Second, we show that Error(𝜑) is reducible. ∧
{Error(𝜑)} = {Error(𝜓 1 ) ∧ Error(𝜓 2 )} − → {Error(𝜓 1 ), Error(𝜓 2 )}. By the induction hypothesis in 𝜓 1 and 𝜓 2 , Error(𝜓 1 ) and Error(𝜓 2 ) are reducible. Hence, Error(𝜑) is reducible. Hence, for every filter condition 𝜑, the filter conditions 𝜑 and Error(𝜑) are reducible.
□
B.3
Normalization of NRMD¬ queries Claim 3 (Normalized NRMD¬ ). Let (𝑝 (𝑋¯ ), Π) be a NRMD¬ query, and 𝑅 be a rule in Π with form 𝑝 (𝑋¯ ) ← 𝐴1, . . . , 𝐴𝑚 , ¬𝐵 1, . . . , ¬𝐵𝑛 ,
where 𝐴1, . . . , 𝐴𝑚 are positive literals, and ¬𝐵 1, . . . , ¬𝐵𝑛 are negative literals. For 1 ≤ 𝑖 ≤ 𝑚, let 𝑌¯𝑖 be the set of variables that consists of the variables atoms 𝐴1, . . . , 𝐴𝑖 . Consider the minimal set of rules Π𝑅 that includes the following rules: (1) Rules 𝑅𝑖𝐴 , for 2 ≤ 𝑖 ≤ 𝑚, defined recursively as follows: (a) 𝑅2𝐴 = 𝑞𝐴2 (𝑌¯2 ) ← 𝐴1, 𝐴2 . (b) 𝑅𝑖𝐴 = 𝑞𝑖𝐴 (𝑌¯𝑖 ) ← 𝑞𝑖𝐴−1 (𝑌¯𝑖 −1 ), 𝐴𝑖 . (2) Rules 𝑅 𝐵𝑗 for 1 ≤ 𝑗 ≤ 𝑛, defined recursively as follows: 𝐴 (𝑌¯ ), (a) 𝑅0𝐵 = 𝑟 0𝐵 (𝑌¯𝑚 ) ← 𝑞𝑚 𝑚 (b) 𝑅 𝐵𝑗 = 𝑟 𝐵𝑗 (𝑌¯𝑚 ) ← 𝑟 𝐵𝑗−1 (𝑌¯𝑚 ), ¬𝐵 ′𝑗 (𝑌¯𝑚 ), ′ (c) 𝑅 𝐵𝑗 = 𝐵 ′𝑗 (𝑌¯𝑚 ) ← 𝑟 𝐵𝑗−1 (𝑌¯𝑚 ), 𝐵 𝑗 . (3) A rule 𝑅 ′ = 𝑝 (𝑋¯ ) ← 𝑟𝑛𝐵 (𝑌¯𝑚 ). The NRMD¬ query (𝑝 (𝑋¯ ), Π ′ ) that results from replacing rule 𝑅 in query (𝑝 (𝑋¯ ), Π) with the rules in Π𝑅 is equivalent to query (𝑝 (𝑋¯ ), Π). Proof. We next prove this claim by induction on the numbers 𝑚, of positive literals, and 𝑛, of negative literals, in a rule 𝑅. The hypothesis of induction states that the query (𝑞(𝑋¯ ), {𝑅}) and its normalized query (𝑞(𝑋¯ ), Π𝑅 ) are equivalent. Since we assumed that every literal in the body of a rule must have at least one variable (see Section 4), to guarantee safeness, the body of the rule cannot include a negative literal without having at least a positive literal. (1) If 𝑚 = 1 and 𝑛 = 0, rule 𝑅 is already normalized because it is the projection rule 𝑝 (𝑋¯ ) ← 𝐴1 .
The multiset semantics of SPARQL patterns
45
𝜃 (𝑝 (𝑋¯ )) 𝑅 ′′
𝜃 (𝑞𝐴2 (𝑌¯2 )) 𝜃 (𝐴3 )
𝜃 (𝑝 (𝑋¯ )) 𝑅
𝜃 (𝐴1 )
𝑅
𝜃 (𝐴2 )
𝑅
𝜃 (𝐴3 )
𝑅 ′′
𝑅2𝐴
𝑅
...
𝜃 (𝐴𝑚 )
𝜃 (𝐴1 )
𝑅 ′′
...
𝜃 (𝐴𝑚 )
𝑅2𝐴
𝜃 (𝐴2 )
Fig. 5. Derivation trees for the ground literal 𝜃 (𝑝 (𝑋¯ )) regarding query (𝑝 (𝑋¯ ), {𝑅}) (on the left), and query (𝑝 (𝑋¯ ), {𝑅2𝐴 , 𝑅 ′′ }) (on the right). The children of the nodes labeled with the positive ground literals 𝜃 (𝐴𝑖 ) are omitted.
(2) If 𝑚 > 1 and 𝑛 = 0 then the normalization of rule 𝑅 consists of a set Π𝑅 of rules 𝑅𝑖𝐴 , for 2 ≤ 𝑖 ≤ 𝑚, defined recursively as follows:
𝑅2𝐴 = 𝑞𝐴2 (𝑌¯2 ) ← 𝐴1, 𝐴2, 𝑅𝑖𝐴 = 𝑞𝑖𝐴 (𝑌¯𝑖 ) ← 𝑞𝑖𝐴−1 (𝑌¯𝑖 −1 ), 𝐴𝑖
for 2 ≤ 𝑖 ≤ 𝑚,
𝑅0𝐵 = 𝑟 0 (𝑌¯𝑚 ) ← 𝑞𝑛𝐴 (𝑌¯𝑚 ), 𝑅 ′ = 𝑝 (𝑋¯ ) ← 𝑟 0𝐵 (𝑌¯𝑚 ).
𝐴 , 𝑅 𝐵 , 𝑅 ′ }) is equivalent to the By the induction hypothesis, the query (𝑝 (𝑋¯ ), {𝑅3𝐴 , . . . , 𝑅𝑚 0 ′′ ′′ 𝐴 ¯ ¯ ¯ query (𝑝 (𝑋 ), {𝑅 }) where 𝑅 is the rule 𝑝 (𝑋 ) ← 𝑞 2 (𝑌2 ), 𝐴3, . . . , 𝐴𝑚 . Hence, the query (𝑝 (𝑋¯ ), Π𝑅 ) is equivalent to the query (𝑝 (𝑋¯ ), {𝑅2𝐴 , 𝑅 ′′ }). To show that these queries are equivalent to query (𝑝 (𝑋¯ ), {𝑅}), we need to show that they have the same answers, and each answer has the same cardinality. Assume that a substitution 𝜃 is an answer to query (𝑝 (𝑋¯ ), {𝑅}). Then, program {𝑅} has a derivation tree whose root is labeled with the ground literal 𝜃 (𝑝 (𝑋¯ )), has 𝑚 children labeled with the ground literals 𝜃 (𝐴𝑖 ), for 1 ≤ 𝑖 ≤ 𝑚, and the edges from the root to the children are labeled with rule 𝑅, as is shown in Figure 5 (on the left). Then, for 1 ≤ 𝑖 ≤ 𝑚, there is a derivation three whose root is labeled with the ground literal 𝜃 (𝐴𝑖 ). The existence of the ground literals 𝜃 (𝐴𝑖 ) as labels of derivation tree roots proves that the ground literal 𝜃 (𝑝 (𝑋¯ )) is inferred using the rules 𝑅2𝐴 and 𝑅 ′′ , as is shown in the Figure 5 (on the right). Then, if 𝜃 is an answer to query (𝑝 (𝑋¯ ), {𝑅}) then 𝜃 is an answer to query (𝑝 (𝑋¯ ), {𝑅2𝐴 , 𝑅 ′′ }). The same argument can be used in the contrary direction to prove that if 𝜃 is an answer to query (𝑝 (𝑋¯ ), {𝑅2𝐴 , 𝑅 ′′ }) then 𝜃 is an answer to query (𝑝 (𝑋¯ ), {𝑅}). Finally, the cardinality of 𝜃 (𝑝 (𝑋¯ )) is, for both queries, the product of the cardinalities of 𝜃 (𝐴𝑖 ), for 1 ≤ 𝑖 ≤ 𝑚. Hence, both queries are equivalent.
46
Angles et al.
(3) If 𝑚 > 1 and 𝑛 > 0 then the normalization of rule 𝑅 consists of a set Π𝑅 of rules 𝑅𝑖𝐴 , for 2 ≤ 𝑖 ≤ 𝑚, defined recursively as follows: 𝑅2𝐴 = 𝑞𝐴2 (𝑌¯2 ) ← 𝐴1, 𝐴2, 𝑅𝑖𝐴 = 𝑞𝑖𝐴 (𝑌¯𝑖 ) ← 𝑞𝑖𝐴−1 (𝑌¯𝑖 −1 ), 𝐴𝑖 𝐴 ¯ 𝑅0𝐵 = 𝑟 0𝐵 (𝑌¯𝑚 ) ← 𝑞𝑚 (𝑌𝑚 ), 𝐵 𝐵 ¯ 𝐵 𝑅 𝑗 = 𝑟 𝑗 (𝑌𝑚 ) ← 𝑟 𝑗 −1 (𝑌¯𝑚 ), ¬𝐵 ′ 𝑗 (𝑌¯𝑚 ) ′ 𝑅 𝐵𝑗 = 𝐵 ′𝑗 (𝑌¯𝑚 ) ← 𝑟 𝐵𝑗−1 (𝑌¯𝑚 ), 𝐵 𝑗 ,
for 2 ≤ 𝑖 ≤ 𝑚,
for 1 ≤ 𝑗 ≤ 𝑛,
𝑅 ′ = 𝑝 (𝑋¯ ) ← 𝑟𝑛𝐵 (𝑌¯𝑚 ). The rules above are equivalent to the following rules: 𝑅2𝐴 = 𝑞𝐴2 (𝑌¯2 ) ← 𝐴1, 𝐴2, 𝑅𝑖𝐴 = 𝑞𝑖𝐴 (𝑌¯𝑖 ) ← 𝑞𝑖𝐴−1 (𝑌¯𝑖 −1 ), 𝐴𝑖 𝐴 ¯ (𝑌𝑚 ), 𝑅0𝐵 = 𝑟 0𝐵 (𝑌¯𝑚 ) ← 𝑞𝑚 𝐵 ¯ 𝐵 𝐵 𝑅 𝑗 = 𝑟 𝑗 (𝑌𝑚 ) ← 𝑟 𝑗 −1 (𝑌¯𝑚 ), ¬𝐵 ′𝑗 (𝑌¯𝑚 ) ′ 𝑅 𝐵𝑗 = 𝐵 ′𝑗 (𝑌¯𝑚 ) ← 𝑟 𝐵𝑗−1 (𝑌¯𝑚 ), 𝐵 𝑗 ,
for 2 ≤ 𝑖 ≤ 𝑚,
for 1 ≤ 𝑗 ≤ 𝑛 − 1,
𝐵 𝑅𝛼 = 𝑡 (𝑌¯𝑚 ) ← 𝑟𝑛−1 (𝑌¯𝑚 ), 𝑅𝛽 = 𝑟𝑛𝐵 (𝑌¯𝑚 ) ← 𝑡 (𝑌¯𝑚 ), ¬𝐵𝑛 ,
𝑅 ′′ = 𝑝 (𝑋¯ ) ← 𝑟𝑛𝐵 (𝑌¯𝑚 ). By the induction hypothesis, the query (𝑡 (𝑌¯𝑚 ), (Π𝑅 ∪ {𝑅𝛼 }) \ {𝑅𝑛𝐵 , 𝑅 ′ }) is equivalent to the query (𝑡 (𝑌¯𝑚 ), {𝑅𝛾 }) where 𝑅𝛾 is the rule 𝑡 (𝑌¯𝑚 ) ← 𝐴1, . . . , 𝐴𝑚 , 𝐵 1, . . . , 𝐵𝑛−1 . Hence, the query (𝑝 (𝑋¯ ), Π𝑅 ) is equivalent to the query (𝑝 (𝑋¯ ), {𝑅𝛾 , 𝑅𝛽 , 𝑅 ′′ }) To show that these queries are equivalent to query (𝑝 (𝑋¯ ), {𝑅}), we need to show that they have the same answers, and each answer has the same cardinalities. Assume that a substitution 𝜃 is an answer to query (𝑝 (𝑋¯ ), {𝑅}). Then, program {𝑅} has a derivation tree whose root is labeled with the ground literal 𝜃 (𝑝 (𝑋¯ )), has 𝑚 children labeled with the ground literals 𝜃 (𝐴𝑖 ), and 𝑛 children labeled with literals ¬𝜃 (𝐵 𝑗 ), for 1 ≤ 𝑖 ≤ 𝑚 and 1 ≤ 𝑗 ≤ 𝑛, and the edges from the root to the children are labeled with rule 𝑅, as is shown in Figure 6 (on the left). Then, for 1 ≤ 𝑖 ≤ 𝑚, there is a derivation three whose root is labeled with the ground literal 𝜃 (𝐴𝑖 ), and for 1 ≤ 𝑗 ≤ 𝑛, there is no derivation three whose root is labeled with the ground literal 𝜃 (𝐵 𝑗 ). The existence of the ground literals 𝜃 (𝐴𝑖 ) and the non-existence of the ground literals 𝜃 (𝐵 𝑗 ) as labels of derivation tree roots prove that the ground literal 𝜃 (𝑝 (𝑋¯ )) is inferred using the rules 𝑅𝛾 , 𝑅𝛽 , and 𝑅 ′′ , as is shown in the Figure 6 (on the right). Then, if 𝜃 is an answer to query (𝑝 (𝑋¯ ), {𝑅}) then 𝜃 is an answer to query (𝑝 (𝑋¯ ), {𝑅𝛾 , 𝑅𝛽 , 𝑅 ′′ }). The same argument can be used in the contrary direction to prove that if 𝜃 is an answer to query (𝑝 (𝑋¯ ), {𝑅𝛾 , 𝑅𝛽 , 𝑅 ′′ }) then 𝜃 is an answer to query (𝑝 (𝑋¯ ), {𝑅}). Finally, the cardinality of 𝜃 (𝑝 (𝑋¯ )) is, for both queries, the product of the cardinalities of 𝜃 (𝐴𝑖 ), for 1 ≤ 𝑖 ≤ 𝑚. Hence, both queries are equivalent. Hence, we have proved that the normalization method produces an equivalent NRMD¬ query. □ B.4
Simulations between query languages
Claim 4 (SPARQL to NRMD¬ ). The triple (𝑓12, 𝑔12, ℎ 21 ) is a simulation of SPARQL in NRMD¬ .
The multiset semantics of SPARQL patterns
47
𝜃 (𝑝 (𝑋¯ )) 𝑅
𝜃 (𝐴1 )
𝑅
...
𝑅
𝑅
𝜃 (𝐴𝑚 ) ¬𝜃 (𝐵 1 )
...
¬𝜃 (𝐵𝑛 )
𝜃 (𝑝 (𝑋¯ )) 𝑅 ′′
𝜃 (𝑟𝑛𝐵 (𝑌¯𝑚 )) 𝑅𝛽
𝑅𝛽
𝜃 (𝑡 (𝑌¯𝑚 )) 𝑅𝛾
𝜃 (𝐴1 )
...
𝑅𝛾
𝑅𝛾
𝜃 (𝐴𝑚 ) ¬𝜃 (𝐵 1 )
¬𝜃 (𝐵𝑛′ (𝑌¯𝑚 )) 𝑅𝛾
...
¬𝜃 (𝐵𝑛−1 )
Fig. 6. Derivation trees for the ground atom 𝜃 (𝑝 (𝑋¯ )) regarding query (𝑝 (𝑋¯ ), {𝑅}) (on the left), and query (𝑝 (𝑋¯ ), {𝑅2𝐴 , 𝑅 ′′ }) (on the right). The children of the nodes labeled with the positive ground literals 𝜃 (𝐴𝑖 ) are omitted. Nodes label with the negative ground literals ¬𝜃 (𝐵 𝑗 ) have no children and do no derivation tree include the positive literal 𝜃 (𝐵 𝑗 ) as the root label.
Proof. To prove this claim, we show that, for every SPARQL query 𝑄 and RDF graph 𝐺, it holds that J𝑄K𝐺 = ℎ 21 (J𝑓12 (𝑄)K𝑔12 ) by induction on the structure of a normalized SPARQL query 𝑄. In this proof, we assume that 𝜃 is a NRMD¬ substitution for the variables of the NRMD¬ query 𝑓12 (𝑄), and 𝜇 is the SPARQL mapping ℎ 21 (𝜃 ). To show then that J𝑄K𝐺 = ℎ 21 (J𝑓12 (𝑄)K𝑔12 ), we have to prove that 𝜇 ∈ J𝑄K𝐺 if and only if 𝜃 ∈ ℎ 21 (J𝑓12 (𝑄)K𝑔12 ), and card(𝜇, J𝑄K𝐺 ) = card(𝜃, ℎ 21 (J𝑓12 (𝑄)K𝑔12 )).
(1) Let 𝑄 be a triple pattern (?𝑋, 𝑝, ?𝑌 ). In this case, there is a corresponding version of the triple pattern as a NRMD¬ literal triple(𝑋, 𝑝, 𝑌 ), where 𝑋 and 𝑌 are the corresponding variables for ?𝑋 and ?𝑌 . The NRMD¬ query 𝑓12 (𝑄) is then (𝑞(𝑋, 𝑌 ), Π) where Π is the program with a rule 𝑞(𝑋, 𝑌 ) ← triple(𝑋, 𝑝, 𝑌 ). Let 𝜃 be the NRMD¬ substitution 𝜃 = (𝑋 /𝑠, 𝑌 /𝑜) and 𝜇 be the SPARQL mapping ℎ 21 (𝜃 ) = {?𝑋 ↦→ 𝑠, ?𝑌 ↦→ 𝑜 }. (a) According to the NRMD¬ semantics, 𝜃 ∈ J𝑓12 (𝑄)K𝑔12 (𝐺 ) if and only if triple(𝑠, 𝑝, 𝑜) ∈ 𝑔12 (𝐺). By the definition function 𝑔12 , triple(𝑠, 𝑝, 𝑜) ∈ 𝑔12 (𝐺) if and only if (𝑠, 𝑝, 𝑜) ∈ 𝐺. By the SPARQL semantics, the SPARQL mapping 𝜇 is in J𝑄K𝐺 if and only if (𝑠, 𝑝, 𝑜) ∈ 𝐺. Hence, 𝜃 ∈ J𝑓12 (𝑄)K𝑔12 (𝐺 ) if and only if 𝜇 ∈ J𝑄K𝐺 . (b) By construction, card(𝜃, J𝑓12 (𝑄)K𝑔12 (𝐺 ) ) = 1 and card(𝜇, J𝑄K𝐺 ) = 1. Hence, card(𝜃, J𝑓12 (𝑄)K𝑔12 (𝐺 ) ) = card(𝜇, J𝑄K𝐺 ). We have shown that we can simulate triple patterns of the form (𝑋, 𝑝, 𝑌 ) with NRMD¬ queries. However, it is not difficult to apply the same argument for the other forms of triple patterns (e.g., (𝑋, 𝑝, 𝑜) or (𝑠, 𝑋, 𝑌 )). Hence, SPARQL triple patterns are simulable with NRMD¬ . (2) Let 𝑄 be a query (𝑃 1 AND 𝑃2 ). Assume that inScope(𝑃1 ) = {?𝑋, ?𝑌 } and inScope(𝑃 2 ) = {?𝑋, ?𝑍 }. The NRMD¬ query 𝑓12 (𝑄) is then (𝑞(𝑋, 𝑌 , 𝑍 ), Π) where Π is the program that consists of the rules in the programs of queries (𝑝 1 (𝑋, 𝑌 ), Π 1 ) = 𝑓12 (𝑃 1 ) and (𝑝 2 (𝑋, 𝑍 ), Π 2 ) =
48
Angles et al.
𝑓12 (𝑃 2 ), the rule 𝑞(𝑋, 𝑌 , 𝑍 ) ← 𝑝 1 (𝑋 1, 𝑌 ), 𝑝 2 (𝑋 2, 𝑍 ), comp(𝑋 1, 𝑋 2, 𝑋 ) and the rules that define the compatibility between values (which may also included in Π 1 and Π 2 ) comp(𝑋, 𝑋, 𝑋 ) ← term(𝑋 ) comp(𝑋, 𝑌 , 𝑋 ) ← term(𝑋 ), null(𝑌 ) comp(𝑌 , 𝑋, 𝑋 ) ← term(𝑋 ), null(𝑌 ) comp(𝑌 , 𝑌 , 𝑌 ) ← null(𝑌 ). (a) If 𝜃 ∈ J𝑓12 (𝑄)K𝑔12 (𝐺 ) then, by the semantics of NRMD¬ , there exists the NRMD¬ solutions 𝜃 1 = {𝑋 1 /𝑎 1, 𝑌 /𝑏}, 𝜃 2 = {𝑋 2 /𝑎 2, 𝑍 /𝑐}, and 𝜃 3 = {𝑋 1 /𝑎 1, 𝑋 2 /𝑎 2, 𝑋 /𝑎} such that {𝑋 1 /𝑎 1, 𝑌 /𝑏} ∈ J(𝑝 1 (𝑋 1, 𝑌 ), Π 1 )K𝑔12 (𝐺 ) , {𝑋 2 /𝑎 2, 𝑍 /𝑐} ∈ J(𝑝 2 (𝑋 2, 𝑍 ), Π 2 )K𝑔12 (𝐺 ) ,
{𝑋 1 /𝑎 1, 𝑋 2 /𝑎 2, 𝑋 /𝑎} ∈ J(comp(𝑋 1, 𝑋 2, 𝑋 ), Π)K𝑔12 (𝐺 ) .
By the induction hypothesis in 𝑃 1 and 𝑃 2 , 𝜃 1 ∈ {𝑋 1 /𝑎 1, 𝑌 /𝑏} ∈ J(𝑝 1 (𝑋 1, 𝑌 ), Π 1 )K𝑔12 (𝐺 ) and 𝜃 2 ∈ {𝑋 1 /𝑎 1, 𝑌 /𝑏} ∈ J(𝑝 2 (𝑋 1, 𝑌 ), Π 2 )K𝑔12 (𝐺 ) if and only if mappings 𝜇1 = ℎ 21 (𝜃 1 ) and 𝜇2 = ℎ 21 (𝜃 2 ) hold 𝜇1 ∈ J𝑃1 K𝐺 and 𝜇2 ∈ J𝑃2 K𝐺 . By the rules defining comp, it holds that 𝜇1 ∼ 𝜇2 and 𝜇1 ∪ 𝜇2 = 𝜇. By the semantics of the SPARQL operator AND, this it holds that 𝜇 ∈ J𝑄K𝐺 . Hence, 𝜃 ∈ J𝑓12 (𝑄)K𝑔12 (𝐺 ) if and only if 𝜇 ∈ J𝑄K𝐺 . (b) By definition, ∑︁ card(𝜇, J𝑄K𝐺 ) = card(𝜇 1, J𝑃1 K𝐺 ) × card(𝜇 2, J𝑃2 K𝐺 ). 𝜇 1 ∈ J𝑃1 K𝐺 𝜇 2 ∈ J𝑃2 K𝐺 𝜇 1 ∼𝜇 2 𝜇=𝜇1 ∪𝜇 2
By the induction hypothesis, ∑︁ card(𝜇, J𝑄K𝐺 ) = card({𝑋 1 /𝑎 1, 𝑌 /𝑏}, J𝑓12 (𝑃 1 )K𝑔12 (𝐺 ) ) ×card({𝑋 2 /𝑎 2, 𝑍 /𝑐}, J𝑓12 (𝑃1 )K𝑔12 (𝐺 ) ). {𝑋 1 /𝑎 1 ,𝑌 /𝑏 } ∈ J (𝑝 1 (𝑋 1 ,𝑌 ),Π1 ) K𝑔12 (𝐺 ) {𝑋 2 /𝑎 2 ,𝑍 /𝑐 } ∈ J (𝑝 2 (𝑋 2 ,𝑌 ),Π2 ) K𝑔12 (𝐺 ) {𝑋 1 /𝑎 1 ,𝑋 2 /𝑎 2 ,𝑋 /𝑎} ∈ J (comp(𝑋 1 ,𝑋 2 ,𝑋 ),Π) K𝑔12 (𝐺 )
By the semantics of NRMD¬ , we conclude that card(𝜇, J𝑄K𝐺 ) = card(𝜃, J𝑓12 (𝑄)K𝑔12 (𝐺 ) ). We have shown that we can simulate queries of the form (𝑃 1 AND 𝑃2 ), where inScope(𝑃1 ) = {?𝑋, ?𝑌 } and inScope(𝑃2 ) = {?𝑋, ?𝑍 }, with NRMD¬ queries. However, it is not difficult to apply the same argument for queries where 𝑃1 and 𝑃2 have different sets of in-scope variables. Hence, SPARQL queries of the form (𝑃 1 AND 𝑃2 ) are simulable with NRMD¬ . (3) Let 𝑄 be a query (𝑃 1 EXCEPT 𝑃2 ), and ?𝑋¯ be the list of SPARQL variables in set inScope(𝑄). The NRMD¬ query 𝑓12 (𝑄) is then (𝑞(𝑋¯ ), Π) where Π is the program that consists of the rules in programs of queries (𝑝 1 (𝑋¯ ), Π1 ) = 𝑓12 (𝑃1 ) and (𝑝 2 (𝑋¯ ), Π2 ) = 𝑓12 (𝑃 2 ), and the rule 𝑞(𝑋¯ ) ← 𝑝 1 (𝑋¯ ), ¬𝑝 2 (𝑋¯ ). (a) By the semantics of NRMD¬ , 𝜃 ∈ J𝑓12 (𝑄)K𝑔12 (𝐺 ) if and only if 𝜃 ∈ J𝑓12 (𝑃 1 )K𝑔12 (𝐺 ) and 𝜃 ∉ J𝑓12 (𝑃 2 )K𝑔12 (𝐺 ) . By the induction hypothesis, the last condition is equivalent to 𝜇 ∈ J𝑃 1 K𝐺 and 𝜇 ∉ J𝑃 2 K𝐺 . By the SPARQL semantics, this is equivalent to 𝜇 ∈ J𝑄K𝐺 .
The multiset semantics of SPARQL patterns
49
(b) By definition, card(𝜇, J𝑄K𝐺 ) = card(𝜇, J𝑃1 K𝐺 ) and card(𝜃, J𝑓12 (𝑄)K𝑔12 (𝐺 ) ) = card(𝜃, J𝑓12 (𝑃 1 )K𝑔12 (𝐺 ) ). By the induction hypothesis, card(𝜇, J𝑃 1 K𝐺 ) = card(𝜃, J𝑓12 (𝑃 1 )K𝑔12 (𝐺 ) ). Hence, card(𝜇, J𝑄K𝐺 ) = card(𝜃, J𝑓12 (𝑄)K𝑔12 (𝐺 ) ).
Hence, SPARQL queries of the form (𝑃 1 EXCEPT 𝑃2 ) are simulable with NRMD¬ . (4) Let 𝑄 be a SPARQL query (𝑃 1 UNION 𝑃2 ). The NRMD¬ query 𝑓12 (𝑄) is then (𝑞(𝑋¯ ), Π) where 𝑋¯ is the list with the variables in set inScope(𝑄), and Π is the program that consists of the rules in program of queries (𝑝 1 (𝑋¯ ), Π1 ) = 𝑓12 (𝑃 1 ) and (𝑝 2 (𝑋¯ ), Π2 ) = 𝑓12 (𝑃 2 ), and the rules that correspond the operation UNION, namely 𝑞(𝑋¯ ) ← 𝑝 1 (𝑋¯ ) and 𝑞(𝑋¯ ) ← 𝑝 2 (𝑋¯ ). (a) By the NRMD¬ semantics, 𝜃 is a solution of (𝑞(𝑋¯ ), Π) if and only if 𝜃 ∈ J(𝑝 1 (𝑋¯ ), Π1 )K𝑔12 (𝐺 ) or 𝜃 ∈ J(𝑝 2 (𝑋¯ ), Π2 )K𝑔12 (𝐺 ) . By the induction hypothesis, this is equivalent to that 𝜇 ∈ J𝑃 1 K𝐺 or 𝜇 ∈ J𝑃 1 K𝐺 . By the SPARQL semantics, this is equivalent to 𝜇 ∈ J𝑄K𝐺 . (b) By the NRMD¬ semantics, card(𝜃, J𝑓12 (𝑄)K𝑔12 (𝐺 ) ) = card(𝜃, J𝑓12 (𝑃 1 )K𝑔12 (𝐺 ) ) + card(𝜃, J𝑓12 (𝑃 2 )K𝑔12 (𝐺 ) ) and card(𝜃, J𝑓12 (𝑄)K𝑔12 (𝐺 ) ) = card(𝜇, J𝑃1 K𝐺 ) + card(𝜇, J𝑃2 K𝐺 ). By the induction hypothesis, card(𝜃, J𝑓12 (𝑃 1 )K𝑔12 (𝐺 ) ) = card(𝜇, J𝑃 1 K𝐺 ) and card(𝜃, J𝑓12 (𝑃 2 )K𝑔12 (𝐺 ) ) = card(𝜇, J𝑃2 K𝐺 ). Hence card(𝜃, J𝑓12 (𝑄)K𝑔12 (𝐺 ) ) = card(𝜇, J𝑄K𝐺 ). Hence, SPARQL queries of the form (𝑃 1 UNION 𝑃2 ) are simulable with NRMD¬ . (5) Let 𝑄 be the SPARQL query (𝑃 FILTER 𝜑) where is an atomic filter condition (i.e., a filter condition of the form ?𝑋 = 𝑐, ?𝑋 = ?𝑌 , or bound(?𝑋 )), and 𝐿𝜑 be a set of NRMD¬ literals defined as follows: if 𝜑 is ?𝑋 = 𝑐, 𝑋 = 𝑐, bound(𝑋 ) 𝑋 = 𝑌 , bound(𝑋 ), bound(𝑌 ) if 𝜑 is ?𝑋 = ?𝑌 , bound(𝑋 ) if 𝜑 is bound(?𝑋 ). The NRMD¬ query 𝑓12 (𝑄) is then (𝑞(𝑋¯ ), Π) where 𝑋¯ is the list with the variables in set inScope(𝑄), and Π is the program that consists of the rules in program of query (𝑝 (𝑋¯ ), Π ′ ) = 𝑓12 (𝑃), and the rule that corresponds the operation FILTER, namely rule 𝑞(𝑋¯ ) ← 𝑝 (𝑋¯ ), 𝐿𝜑 . (a) By the NRMD¬ semantics, 𝜃 is a solution of (𝑞(𝑋¯ ), Π) if and only if 𝜃 ∈ J(𝑝 (𝑋¯ ), Π ′ )K𝑔12 (𝐺 ) , and 𝜃 (𝐿𝜑 ) ⊆ 𝑔12 (𝐺). By the induction hypothesis, 𝜃 ∈ J(𝑝 (𝑋¯ ), Π ′ )K𝑔12 (𝐺 ) is equivalent to 𝜇 ∈ J𝑃K𝐺 . By construction, 𝜃 (𝐿𝜑 ) ⊆ atoms(Π ′, 𝑔12 (𝐺)) if and only if 𝜇 (𝜑) = true. By the SPARQL semantics, this is equivalent to 𝜇 ∈ J𝑄K𝐺 . (b) By construction, every fact in 𝜃 (𝐿𝜑 ) occurs once in 𝑔12 (𝐺). For each fact in 𝐹 ∈ 𝜃 (𝐿𝜑 ) there is then only one proof that 𝐹 ∈ atoms(Π, 𝑔12 (𝐺)). Hence, card(𝜃, J𝑓12 (𝑄)K𝑔12 (𝐺 ) ) = card(𝜃, J𝑓12 (𝑃)K𝑔12 (𝐺 ) ). By the induction hypothesis, card(𝜃, J𝑓12 (𝑃)K𝑔12 (𝐺 ) ) = card(𝜇, J𝑃K𝐺 ). According to the SPARQL semantics, card(𝜇, J𝑃K𝐺 ) = card(𝜇, J𝑄K𝐺 ). Hence, card(𝜃, J𝑓12 (𝑄)K𝑔12 (𝐺 ) ) = card(𝜇, J𝑄K𝐺 ). Hence, SPARQL queries of the form (𝑃 FILTER 𝜑) are simulable with NRMD¬ . (6) Let 𝑄 be the SPARQL query (SELECT 𝑋¯ 𝑃). The NRMD¬ query 𝑓12 (𝑄) is then (𝑞(𝑋¯ ), Π), where Π is the program that consists of the rules in the program of query (𝑝 (𝑌¯ , Π ′ ) = 𝑓12 (𝑃), and the rule that corresponds to the operation projects, namely rule 𝑞(𝑋¯ ) ← 𝑝 (𝑌¯ ), null(𝑥 1 ), . . . , null(𝑥𝑛 ), where 𝑥 1, . . . , 𝑥𝑛 are the variables that are in 𝑊 but not in inScope(𝑃1 ). 𝐿𝜑 =
50
Angles et al.
(a) By the NRMD¬ semantics, 𝜃 is a solution of (𝑞(𝑋¯ ), Π) if and only if there exists a solution 𝜃 ′ ∈ J(𝑝 (𝑌¯ , Π ′ )K𝑔12 (𝐺 ) such that 𝜃 (𝑥) = 𝜃 ′ (𝑥) if 𝑥 ∈ inScope(𝑄) ∩ inScope(𝑃). Let 𝜇 = ℎ 21 (𝜃 ) and 𝜇 ′ = ℎ 21 (𝜃 ′ ). By construction 𝜇 = 𝜇 ′ | inScope(𝑄 ) . By the induction hypothesis, 𝜇 ′ ∈ J𝑃K𝐺 . Hence, 𝜇 ∈ J𝑄K𝐺 . (b) By construction, ∑︁ card(𝜃, J𝑓12 (𝑄)K𝑔12 (𝐺 ) ) = card(𝜃 ′, J𝑓12 (𝑃)K𝑔12 (𝐺 ) ). 𝜃 ′ | inScope(𝑄 ) =𝜃 𝜃 ′ ∈ J 𝑓12 (𝑃 ) K𝑔12 (𝐺 )
By the induction hypothesis, card(𝜃, J𝑓𝑒1,2 (𝑄)K𝑔12 (𝐺 ) ) =
∑︁ 𝜃′|
inScope(𝑄 ) =𝜃 𝜇 ′ =ℎ 21 (𝜃 ′ ) 𝜇 ′ ∈ J𝑃 K𝐺
card(𝜇 ′, J𝑃K𝐺 ).
By construction, card(𝜃, J𝑓𝑒1,2 (𝑄)K𝑔12 (𝐺 ) ) =
∑︁ 𝜇 ′ | inScope(𝑄 ) =ℎ 21 (𝜃 ) 𝜇 ′ ∈ J𝑃 K𝐺
card(𝜇 ′, J𝑃K𝐺 ).
Hence, card(𝜃, J𝑓12 (𝑄)K𝑔12 (𝐺 ) ) = card(ℎ 21 (𝜃 ), J𝑄K𝐺 ). Hence, SPARQL queries of the form (SELECT 𝑋¯ 𝑃) are simulable with NRMD¬ . Hence, the triple (𝑓12, 𝑔12, ℎ 21 ) is a simulation of SPARQL in NRMD¬ .
□
Claim 5 (NRMD¬ to SPARQL). The triple (𝑓21, 𝑔21, ℎ 12 ) is a simulation of NRMD¬ in SPARQL. Proof. To prove this claim, we consider only normalized NRMD¬ queries 𝑄, that is, queries where rules consist of projection rules, join rules and negation rules (see Section 6.2). This proof follows from induction on the structure of query 𝑄 = (𝑞(𝑋¯ ), Π) with inductive hypothesis 𝜃 ∈ J𝑄K𝐷 if and only if ℎ 12 (𝜃 ) ∈ J𝑓21 (𝑄)K𝑔21 (𝐷 ) and card(𝜃, J𝑄K𝐷 ) = card(ℎ 12 (𝜃 ), J𝑓21 (𝑄)K𝑔21 (𝐷 ) ). (1) If 𝑞 is an extensional predicate, then 𝑓21 (𝑄) is the SPARQL query (SELECT ?𝑋 1 . . . ?𝑋 𝑛 ((?𝑌 , 𝛼 0, 𝑝) AND (?𝑌 , 𝛼 1, ?𝑋 1 ) AND · · · AND (?𝑌 , 𝛼𝑛 , ?𝑋 𝑛 )), where the SPARQL variables ?𝑋 1 . . . ?𝑋 𝑛 correspond to the 𝑛 NRMD¬ variables in 𝑋¯ . (a) By construction, 𝜃 ∈ J𝑄K𝐷 if and only if ℎ 12 (𝜃 ) ∈ J𝑓21 (𝑄)K𝑔21 (𝐷 ) . Indeed, each colored fact ⟨𝑞(𝑎 1, . . . , 𝑎𝑛 ), 𝑖⟩ in coloring(𝐷) corresponds to a subgraph {(𝑢𝑖 , 𝛼 0, 𝑝), (𝑢𝑖 , 𝛼 1, 𝑎 1 ), · · · , (𝑢𝑖 , 𝛼𝑛 , 𝑎𝑛 )} where 𝑢𝑖 is a fresh IRI to identify the colored fact, and 𝑎𝑖 = 𝜃 (𝑥𝑖 ), for the 𝑖-th variable 𝑥𝑖 ∈ 𝑋¯ . (b) card(𝜃, J𝑄K𝐷 ) is the cardinality of 𝑞(𝑎 1, . . . , 𝑎𝑛 ) in multiset 𝐷. By construction, this is the number of subsgraphs of the form {(𝑢𝑖 , 𝛼 0, 𝑝), (𝑢𝑖 , 𝛼 1, 𝑎 1 ), · · · , (𝑢𝑖 , 𝛼𝑛 , 𝑎𝑛 )} of 𝑔21 (𝐷). Hence, card(𝜃, J𝑄K𝐷 ) = card(ℎ 12 (𝜃 ), J𝑓21 (𝑄)K𝑔21 (𝐷 ) ). (2) If 𝑞 is an intensional predicate, then there are several rules in Π with head 𝑞(𝑋¯ ), each one matching one of the following forms: • 𝑞(𝑋¯ ) ← 𝑝 (𝑌¯ ), • 𝑞(𝑋¯ ) ← 𝑝 1 (𝑌¯1 ), 𝑝 2 (𝑌¯2 ), • 𝑞(𝑋¯ ) ← 𝑝 3 (𝑌¯3 ), ¬𝑝 4 (𝑌¯4 ). The function 𝑓21 (𝑄) maps each of these rules to one of the following SPARQL queries: • (SELECT 𝑋¯ 𝑓21 ((𝑝 (𝑌¯ ), Π))), • (𝑓21 ((𝑝 1 (𝑋¯ ), Π)) AND 𝑓21 ((𝑝 2 (𝑋¯ ), Π))), • (𝑓21 ((𝑝 3 (𝑋¯ ), Π)) EXCEPT 𝑓21 ((𝑝 4 (𝑋¯ ), Π)).
The multiset semantics of SPARQL patterns
51
If {𝑅1, . . . , 𝑅𝑛 } is the set rules in Π with predicate 𝑞 in the head, then the SPARQL query 𝑓21 (𝑄) has the form (𝑃1 UNION · · · UNION 𝑃𝑛 ), where 𝑃𝑖 is the corresponding SPARQL query for the rule 𝑅𝑖 , for 1 ≤ 𝑖 ≤ 𝑛. (a) First we will prove that the SPARQL query and the NRMD¬ query have the same answers. A substitution 𝜃 is an answer of query 𝑄 if and only if at least one of the following conditions holds: • For a rule 𝑅𝑖 of the form 𝑞(𝑋¯ ) ← 𝑝 (𝑌¯ ), there exists a solution 𝜃 ′ of query (𝑝 (𝑌¯ ), Π) such that 𝜃 (𝑥) = 𝜃 ′ (𝑥) for every variable 𝑥 ∈ 𝑋¯ . Then, by the inductive hypothesis, there exists a solution 𝜇 ′ ∈ J𝑓21 ((𝑝 (𝑌¯ ), Π))K𝑔21 (𝐷 ) such that ℎ 12 (𝜇 ′ ) = 𝜃 ′ . Let 𝜇 be the solution mapping 𝜇 ′ |𝑋¯ . By construction, 𝜇 ∈ J𝑓21 (𝑄)K𝑔21 (𝐷 ) and ℎ 12 (𝜇) = 𝜃 . • For a rule 𝑅𝑖 of the form 𝑞(𝑋¯ ) ← 𝑝 1 (𝑌¯1 ), 𝑝 2 (𝑌¯2 ), substitutions 𝜃 1 = 𝜃 |𝑌¯1 and 𝜃 2 = 𝜃 |𝑌¯2 are solutions of queries (𝑝 1 (𝑌¯1 ), Π) and (𝑝 2 (𝑌¯2 ), Π). By the inductive hypothesis, there exist two solutions 𝜇1 ∈ J𝑓21 ((𝑝 1 (𝑌¯1 ), Π))K𝑔21 (𝐷 ) and 𝜇2 ∈ J𝑓21 ((𝑝 2 (𝑌¯2 ), Π))K𝑔21 (𝐷 ) such that ℎ 12 (𝜇1 ) = 𝜃 1 and ℎ 12 (𝜇2 ) = 𝜃 2 . Let 𝜇 be the solution mapping 𝜇 1 ∪ 𝜇 2 . By construction, 𝜇 ∈ J𝑓21 (𝑄)K𝑔21 (𝐷 ) and ℎ 12 (𝜇) = 𝜃 . • For a rule 𝑅𝑖 of the form 𝑞(𝑋¯ ) ← 𝑝 3 (𝑌¯3 ), ¬𝑝 4 (𝑌¯4 ), substitution 𝜃 is a solution of query (𝑝 3 (𝑌¯3 ), Π) and 𝜃 is not a solution of query (𝑝 4 (𝑌¯3 ), Π). By the inductive hypothesis, there exists a solution 𝜇 ∈ J𝑓21 ((𝑝 3 (𝑌¯3 ), Π))K𝑔21 (𝐷 ) such that 𝜇 ∉ J𝑓21 ((𝑝 4 (𝑌¯4 ), Π))K𝑔21 (𝐷 ) , and ℎ 12 (𝜇) = 𝜃 . By construction, 𝜇 ∈ J𝑓21 (𝑄)K𝑔21 (𝐷 ) . Hence, 𝜃 ∈ J𝑄K𝐷 if and only if there exists 𝜇 such that 𝑓12 (𝜇) = 𝜃 and 𝜇 ∈ J𝑓21 (𝑄)K𝑔21 (𝐷 ) . (b) We next prove that the answers have the same cardinality in SPARQL and NRMD¬ . By definition, ∑︁ card(𝜃, J𝑄K𝐷 ) = card(𝜃 ′, J(𝑝 (𝑌¯ ), Π)K𝐷 ) + 𝜃 ′ |𝑋¯ =𝜃
card(𝜃 1, J(𝑝 1 (𝑌¯1 ), Π)K𝐷 ) × card(𝜃 2, J(𝑝 2 (𝑌¯2 ), Π)K𝐷 ) + card(𝜃, J(𝑝 3 (𝑌¯3 ), Π)K𝐷 ). By the inductive hypothesis, ∑︁ card(𝜃, J𝑄K𝐷 ) = card(𝜇 ′, J𝑓21 ((𝑝 (𝑌¯ ), Π))K𝑔21 (𝐷 ) ) + 𝜃 ′ |𝑋¯ =𝜃 ℎ 12 (𝜇 ′ )=𝜃 ′
card(𝜇 1, J𝑓21 ((𝑝 1 (𝑌¯1 ), Π))K𝑔21 (𝐷 ) ) × card(𝜇 2, J𝑓21 ((𝑝 2 (𝑌¯2 ), Π))K𝑔21 (𝐷 ) ) + card(𝜇, J𝑓21 ((𝑝 3 (𝑌¯3 ), Π))K𝑔 (𝐷 ) ) 21
= card(𝜇, J𝑓21 (𝑄)K𝑔21 (𝐷 ) ).
Hence, the triple (𝑓21, 𝑔21, ℎ 12 ) is a simulation of NRMD¬ in SPARQL.
□
Claim 6 (MRA to NRMD¬ ). The triple (𝑓32, 𝑔32, ℎ 2,3 ) is a simulation of MRA in NRMD¬ . Proof. We prove this claim for normalized MRA expressions where the condition of a selection formula is always an equality atom (e.g., 𝜎𝐴=𝐵 (𝑅)). This proof follows by induction on the structure of a MRA expression 𝐸, assuming that given a MRA database 𝐷, for every subquery 𝐸 ′ of 𝐸 it holds that 𝑡 ′ ∈ J𝐸 ′ K𝐷 if and only if there exists a NRMD¬ solution 𝜃 ′ ∈ J𝑓32 (𝐸)K𝑔32 (𝐷 ) such that ℎ 2,3 (𝜃 ′ ) = 𝑡 ′ . b ∅) where 𝑟 is an extensional (1) If 𝐸 is a relation name 𝑅 then 𝑓32 (𝐸) is the NRMD¬ query (𝑟 (𝐸), predicate.
52
Angles et al.
(a) By definition, 𝑡 ∈ J𝐸K𝐷 if and only if 𝑡 belongs to the multiset relation corresponding to the relation name 𝑅 in the database 𝐷. By construction, 𝑡 ∈ J𝐸K𝐷 is thus equivalent to 𝜃 ∈ J𝑓32 (𝐸)K𝑔32 (𝐷 ) , where ℎ 2,3 (𝜃 ) = 𝑡. Indeed, 𝑡 ∈ 𝑅 𝐼 if and only if 𝑟 (𝑎 1, . . . , 𝑎𝑛 ) ∈ 𝑔32 (𝐷) and 𝑡 = (𝑎 1, . . . , 𝑎𝑛 ). (b) The fact that card(𝑡, J𝐸K𝐷 ) = card(𝜃, J𝑓32 (𝐸)K𝑔32 (𝐷 ) ) follows by construction; the cardinality of 𝑡 in the multiset relation corresponding to the relation name 𝑅 is the same as the cardinality of fact 𝑟 (𝑎 1, . . . , 𝑎𝑛 ) in multiset 𝑔32 (𝐷). b and 𝑓32 (𝐸) is a NRMD¬ query (𝑞( 𝐸), b Π) (2) If 𝐸 is a query 𝐸 1 ∪ 𝐸 2 , then 𝐸b1 = 𝐸b and 𝐸b2 = 𝐸, b b such that 𝑓32 (𝐸 1 ) = (𝑞 1 (𝐸), Π) and 𝑓32 (𝐸 2 ) = (𝑞 2 (𝐸), Π), and program Π includes the rules b ← 𝑞 1 (𝐸) b and 𝑞( 𝐸) b ← 𝑞 2 ( 𝐸). b 𝑞(𝐸) (a) By definition, 𝑡 ∈ J𝐸K𝐷 if and only if 𝑡 ∈ J𝐸 1 K𝐷 or 𝑡 ∈ J𝐸 2 K𝐷 . By the induction hypothesis, 𝑡 ∈ J𝐸 2 K𝐷 is equivalent to say that there exists 𝜃 such that ℎ 2,3 (𝜃 ) = 𝑡 and 𝜃 ∈ J𝑓32 (𝐸 1 )K𝑔32 (𝐷 ) or 𝜃 ∈ J𝑓32 (𝐸 2 )K𝑔32 (𝐷 ) . That is, 𝜃 ∈ J𝑓32 (𝐸)K𝑔32 (𝐷 ) . (b) Assume the respective answers 𝑡 and 𝜃 described in (a). By definition, card(𝑡, J𝐸K𝐷 ) = card(𝑡, J𝐸 1 K𝐷 ) + card(𝑡, J𝐸 2 K𝐷 ),
card(𝜃, J𝑓32 (𝐸)K𝑔32 (𝐷 ) ) = card(𝜃, J𝑓32 (𝐸 1 )K𝑔32 (𝐷 ) ) + card(𝜃, J𝑓32 (𝐸 2 )K𝑔32 (𝐷 ) ). By the inductive hypothesis, these two cardinalities are equal. b Π), 𝑓32 (𝐸 1 ) = (𝑞 1 (𝐸b1 ), Π), b 𝑓32 (𝐸) = (𝑞(𝐸), (3) If 𝐸 is a query 𝐸 1 Z 𝐸 2 then 𝐸b1 ∪ 𝐸b2 = 𝐸, b b 𝑓32 (𝐸 2 ) = (𝑞 2 ( 𝐸 2 ), Π), and program Π includes the rule 𝑞(𝐸) ← 𝑞 1 (𝐸b1 ), 𝑞 2 (𝐸b2 ). (a) By definition, 𝑡 ∈ J𝐸K𝐷 if and only if there exists two tuples 𝑡 1 and 𝑡 2 such that 𝑡 1 ∼ 𝑡 2 , 𝑡 = 𝑡 1 ∪ 𝑡 2 , 𝑡 1 ∈ J𝐸 1 K𝐷 and 𝑡 2 ∈ J𝐸 2 K𝐷 . By the induction hypothesis, 𝑡 ∈ J𝐸K𝐷 if and only if there exists two NRMD¬ solutions 𝜃 1 ∈ J𝑓32 (𝐸 1 )K𝑔32 (𝐷 ) and 𝜃 2 ∈ J𝑓32 (𝐸 2 )K𝑔32 (𝐷 ) where ℎ 2,3 (𝜃 1 ) = 𝑡 1 and ℎ 2,3 (𝜃 2 ) = 𝑡 2 . Let 𝜃 be 𝜃 1 ∪𝜃 2 . By construction, 𝜃 ∈ J𝑓32 (𝐸)K𝑔32 (𝐷 ) and ℎ 2,3 (𝜃 ) = 𝑡. (b) Assume the respective answers 𝑡, 𝑡 1 , 𝑡 2 , 𝜃 , 𝜃 1 , and 𝜃 2 described in (a). By definition, card(𝑡, J𝐸K𝐷 ) = card(𝑡 1, J𝐸 1 K𝐷 ) × card(𝑡 2, J𝐸 1 K𝐷 ),
card(𝜃, J𝑓32 (𝐸)K𝑔32 (𝐷 ) ) = card(𝜃 1, J𝑓32 (𝐸 1 )K𝑔32 (𝐷 ) ) × card(𝜃 2, J𝑓32 (𝐸 1 )K𝑔32 (𝐷 ) ). By the inductive hypothesis, these two cardinalities are equal. b 𝐸b1 = 𝐸, b 𝑓32 (𝐸) = (𝑞( 𝐸), b Π), 𝑓32 (𝐸 1 ) = (𝑞 1 (𝐸), b Π), (4) If 𝐸 is a query 𝐸 1 \ 𝐸 2 then 𝐸b1 = 𝐸, b b b b 𝑓32 (𝐸 2 ) = (𝑞 2 ( 𝐸), Π), and program Π includes the rule 𝑞(𝐸) ← 𝑞 1 ( 𝐸), ¬𝑞 2 (𝐸). (a) By definition, 𝑡 ∈ J𝐸K𝐷 if and only if 𝑡 ∈ J𝐸 1 K𝐷 and 𝑡 ∉ J𝐸 2 K𝐷 . By the induction hypothesis, 𝑡 ∈ J𝐸K𝐷 if and only if there exists a NRMD¬ solution 𝜃 such that 𝜃 ∈ J𝑓32 (𝐸 1 )K𝑔32 (𝐷 ) , 𝜃 ∉ J𝑓32 (𝐸 2 )K𝑔32 (𝐷 ) , and ℎ 2,3 (𝜃 ) = 𝑡. By construction, 𝜃 ∈ J𝑓32 (𝐸)K𝑔32 (𝐷 ) . (b) Assume the respective answers 𝑡 and 𝜃 described in (a). By definition, card(𝑡, J𝐸K𝐷 ) = card(𝑡, J𝐸 1 K𝐷 ) and card(𝜃, J𝑓32 (𝐸)K𝑔32 (𝐷 ) ) = card(𝜃, J𝑓32 (𝐸 1 )K𝑔32 (𝐷 ) ). By the induction hypothesis, these two cardinalities are equal. (5) If 𝐸 is a query 𝜋𝑆 (𝐸 1 ) then 𝐸b = 𝑆 and 𝑆 ⊆ 𝐸b1 , and 𝑓32 (𝐸) is a NRMD¬ query (𝑞(𝑋¯ ), Π) such b Π), and program Π includes the rule 𝑞(𝐸) b ← 𝑞 1 (𝐸b1 ). that 𝑓32 (𝐸 1 ) = (𝑞 1 ( 𝐸), (a) By definition, 𝑡 ∈ J𝐸K𝐷 if and only if there exists a tuple 𝑡 1 ∈ J𝐸 1 K𝐷 such that 𝑡 1 | 𝐸b = 𝑡. By the induction hypothesis, 𝑡 1 ∈ J𝐸K𝐷 if and only if there exists 𝜃 1 ∈ J𝑓32 (𝐸 1 )K𝑔32 (𝐷 ) such that ℎ 2,3 (𝜃 1 ) = 𝑡 1 . Let 𝜃 be 𝜃 1 | 𝐸b. By construction, 𝑡 ∈ J𝐸K𝐷 if and only if 𝜃 ∈ J𝑓32 (𝐸)K𝑔32 (𝐷 ) and ℎ 2,3 (𝜃 ) = 𝑡.
The multiset semantics of SPARQL patterns
53
(b) Assume the respective answers 𝑡 and 𝜃 described in (a). By definition, ∑︁ card(𝑡 1, J𝐸 1 K𝐷 ), card(𝑡, J𝐸K𝐷 ) = 𝑡 1 ∈ J𝐸 1 K𝐷 𝑡 1 | 𝐸b=𝑡
card(𝜃, J𝑓32 (𝐸)K𝑔32 (𝐷 ) ) =
∑︁ 𝜃 1 ∈ J 𝑓32 (𝐸 1 ) K𝑔32 (𝐷 ) 𝜃 1 | 𝐸b=𝜃
card(𝜃 1, J𝑓32 (𝐸 1 )K𝑔32 (𝐷 ) ).
By the induction hypothesis, these two cardinalities are equal. b Π), and 𝑓32 (𝐸 1 ) = (6) If 𝐸 is a query 𝜌𝐴/𝐵 (𝐸 1 ) then 𝐸b = (𝐸b1 \ {𝐴}) ∪ {𝐵}, 𝑓32 (𝐸) = (𝑞(𝐸), (𝑞( 𝐸b1 ), Π). (a) By definition, 𝑡 ∈ J𝐸K𝐷 if and only if there exists a tuple 𝑡 1 ∈ J𝐸 1 K𝐷 where 𝑡 (𝐶) = 𝑡 1 (𝐶) for every attribute 𝐶 ∈ 𝐸b \ {𝐴}, and 𝑡 (𝐴) = 𝑡 1 (𝐵). By the induction hypothesis, 𝑡 1 ∈ J𝐸 1 K𝐷 if and only if there exists a solution 𝜃 1 ∈ J𝑓32 (𝐸 1 )K𝑔32 (𝐷 ) such that ℎ 2,3 (𝜃 1 ) = 𝑡 1 . Let 𝜃 be the tuple with domain 𝐸b such that 𝜃 (𝐶) = 𝜃 1 (𝐶) for every attribute 𝐶 ∈ 𝐸b\ {𝐴}, and 𝜃 (𝐴) = 𝜃 1 (𝐵). By construction, 𝜃 ∈ J𝑓32 (𝐸)K𝑔32 (𝐷 ) if and only if 𝜃 1 ∈ J𝑓32 (𝐸 1 )K𝑔32 (𝐷 ) and ℎ 2,3 (𝜃 ) = 𝑡. (b) Assume the respective query answers 𝑡, 𝑡 1 , 𝜃 , and 𝜃 1 described in (a). By definition, card(𝑡, J𝐸K𝐷 ) = card(𝑡 1, J𝐸 1 K𝐷 ),
card(𝜃, J𝑓32 (𝐸)K𝑔32 (𝐷 ) ) = card(𝜃 1, J𝑓32 (𝐸 1 )K𝑔32 (𝐷 ) ). By the induction hypothesis, these two cardinalities are equal. b Π), and 𝑓32 (𝐸 1 ) = (𝑞 1 (𝐸b1 ), Π) and (7) If 𝐸 is a query 𝜎𝐴=𝐵 (𝐸 1 ) then 𝐸b = 𝐸b1 , 𝑓32 (𝐸) = (𝑞(𝐸), b ← 𝑞 1 ( 𝐸b1 ), 𝐴 = 𝐵. program Π includes the rule 𝑞( 𝐸) (a) By definition, 𝑡 ∈ J𝐸K𝐷 if and only if 𝑡 ∈ J𝐸 1 K𝐷 and 𝑡 (𝐴) = 𝑡 (𝐵). By the induction hypothesis, there is an answer 𝜃 ∈ J𝑓32 (𝐸 1 )K𝑔32 (𝐷 ) such that ℎ 2,3 (𝜃 ) = 𝑡. By construction, 𝜃 (𝐴) = 𝜃 (𝐵). Then, 𝜃 ∈ J𝑓32 (𝐸)K𝑔32 (𝐷 ) . (b) Assume the respective query answers 𝑡 and 𝜃 described in (a). By definition, card(𝑡, J𝐸K𝐷 ) = card(𝑡 1, J𝐸 1 K𝐷 ),
card(𝜃, J𝑓32 (𝐸)K𝑔32 (𝐷 ) ) = card(𝜃 1, J𝑓32 (𝐸 1 )K𝑔32 (𝐷 ) ).
By the induction hypothesis, these two cardinalities are equal. Hence, the triple (𝑓32, 𝑔32, ℎ 2,3 ) is a simulation of MRA in NRMD¬ .
□
Claim 7 (NRMD¬ to MRA). The triple (𝑓2,3, 𝑔2,3, ℎ 32 ) is a simulation of NRMD¬ in SPARQL. Proof. To prove this claim we consider only normalized NRMD¬ queries 𝑄, that is, queries where rules consist of projection rules, join rules and negation rules (see Section 6.2). This proof follows from induction on the structure of query 𝑄 = (𝑞(𝑋¯ ), Π) with inductive hypothesis 𝜃 ∈ J𝑄K𝐷 if and only if ℎ 32 (𝜃 ) ∈ J𝑓2,3 (𝑄)K𝑔2,3 (𝐷 ) and card(𝜃, J𝑄K𝐷 ) = card(ℎ 32 (𝜃 ), J𝑓2,3 (𝑄)K𝑔2,3 (𝐷 ) ). (1) If 𝑞 is a extensional predicate, then 𝑓21 (𝑄) is the MRA query 𝜌𝐴1 /𝑋1 (· · · 𝜌𝐴𝑛 /𝑋𝑛 (𝑅)), where the MRA attributes 𝑋 1, . . . , 𝑋𝑛 correspond to the 𝑛 NRMD¬ variables in 𝑋¯ , and 𝑅 is the relation name corresponding to predicate 𝑞. (a) Let 𝑟 be the MRA relation associated to relation name 𝑅 in the MRA database 𝑔2,3 (𝐷). Let 𝜃 be a NRMD¬ answer with domain {𝑋 1, . . . , 𝑋 1 }, and 𝑡 be the MRA tuple where 𝑡 (𝐴𝑖 ) = 𝜃 (𝑋𝑖 ) for 1 ≤ 𝑖 ≤ 𝑛. By definition, 𝜃 ∈ J𝑄K𝐷 if and only if 𝑝 (𝜃 (𝑋 1 ), . . . , 𝜃 (𝑋𝑛 )) ∈ 𝐷. Because, by definition, each fact 𝑞(𝑎 1, . . . , 𝑎𝑛 ) in 𝐷 corresponds to a tuple 𝑡 ∈ 𝑟 where 𝑡 (𝐴𝑖 ) = 𝑎𝑖 for 1 ≤ 𝑖 ≤ 𝑛, then 𝜃 ∈ J𝑄K𝐷 if and only if 𝑡 ∈ 𝑟 . Let 𝑠 be a MRA tuple
54
Angles et al.
with 𝑠ˆ = {𝑋 1, . . . , 𝑋𝑛 } where 𝑠 (𝑋𝑖 ) = 𝑡 (𝐴𝑖 ), for 1 ≤ 𝑖 ≤ 𝑛. By definition, 𝑡 ∈ 𝑟 if and only if 𝑠 ∈ J𝜌𝐴1 /𝑋1 (· · · 𝜌𝐴𝑛 /𝑋𝑛 (𝑅))K𝑔2,3 (𝐷 ) . By construction, 𝑠 = ℎ 32 (𝜃 ). Hence, 𝜃 ∈ J𝑄K𝐷 if and only if ℎ 32 (𝜃 ) ∈ J𝑓2,3 (𝑄)K𝑔2,3 (𝐷 ) . (b) The identity card(𝜃, J𝑄K𝐷 ) = card(ℎ 32 (𝜃 ), J𝑓2,3𝑄K 𝑓2,3 (𝐷 ) ) follows from the next identities: card(𝜃, J𝑄K𝐷 ) = card(𝑞(𝜃 (𝑋 1 ), . . . , 𝜃 (𝑋𝑛 )), 𝐷) = card(𝑡, 𝑟 )
= card(𝑠, J𝑓2,3 (𝑄)K𝑔2,3 (𝐷 ) )
= card(ℎ 32 (𝜃 ), J𝑓2,3𝑄K𝑔2,3 (𝐷 ) ).
(2) If 𝑞 is an intensional predicate then there are several rules in Π with head 𝑞(𝑋¯ ), each one has matches of the following forms: • 𝑞(𝑋¯ ) ← 𝑝 (𝑌¯ ), • 𝑞(𝑋¯ ) ← 𝑝 1 (𝑌¯1 ), 𝑝 2 (𝑌¯2 ), • 𝑞(𝑋¯ ) ← 𝑝 3 (𝑌¯3 ), ¬𝑝 4 (𝑌¯4 ). where 𝑋¯ ⊆ 𝑌¯ , 𝑌¯1 ∪ 𝑌¯2 = 𝑋¯ , 𝑌¯3 = 𝑋¯ , and 𝑌¯4 = 𝑋¯ . The function 𝑓2,3 (𝑄) maps each of these rules to one of the following MRA queries: • 𝜋𝑋¯ (𝑓2,3 ((𝑝 (𝑌¯ ), Π))), • (𝑓2,3 ((𝑝 1 (𝑋¯ ), Π)) Z 𝑓2,3 ((𝑝 2 (𝑋¯ ), Π))), • (𝑓2,3 ((𝑝 3 (𝑋¯ ), Π)) \ 𝑓2,3 ((𝑝 4 (𝑋¯ ), Π))). If {𝑅1, . . . , 𝑅𝑛 } is the set rules in Π with predicate 𝑞 in the head, then the MRA query 𝑓2,3 (𝑄) has the form (𝐸 1 ∪ · · · ∪ 𝐸𝑛 ), where 𝐸𝑖 is the corresponding MRA expression for the rule 𝑅𝑖 , for 1 ≤ 𝑖 ≤ 𝑛. (a) First, we will prove that the MRA expression and the NRMD¬ query have the same answers. A substitution 𝜃 is an answer of query 𝑄 if and only if one of the following conditions holds: • There exists a solution 𝜃 ′ of query (𝑝 (𝑌¯ ), Π) such that 𝜃 (𝑥) = 𝜃 ′ (𝑥) for every variable 𝑥 ∈ 𝑋¯ . By the induction hypothesis, there exists a solution 𝑡 ′ ∈ J𝑓2,3 ((𝑝 (𝑌¯ ), Π))K𝑔2,3 (𝐷 ) such that ℎ 32 (𝑡 ′ ) = 𝜃 ′ . Let 𝑡 be the solution mapping 𝑡 ′ |𝑋¯ . By construction, 𝑡 ∈ J𝑓2,3 (𝑄)K𝑔2,3 (𝐷 ) and ℎ 32 (𝑡) = 𝜃 . • Substitutions 𝜃 1 = 𝜃 |𝑌¯1 and 𝜃 2 = 𝜃 |𝑌¯2 are solutions of queries (𝑝 1 (𝑌¯1 ), Π) and (𝑝 2 (𝑌¯2 ), Π). By the induction hypothesis, there exists two solutions 𝑡 1 ∈ J𝑓2,3 ((𝑝 1 (𝑌¯1 ), Π))K𝑔2,3 (𝐷 ) and 𝑡 2 ∈ J𝑓2,3 ((𝑝 2 (𝑌¯2 ), Π))K𝑔2,3 (𝐷 ) such that ℎ 32 (𝑡 1 ) = 𝜃 1 and ℎ 32 (𝑡 2 ) = 𝜃 2 . Let 𝑡 be the MRA solution 𝑡 1 ∪ 𝑡 2 . By construction, 𝑡 ∈ J𝑓2,3 (𝑄)K𝑔2,3 (𝐷 ) and ℎ 32 (𝑡) = 𝜃 . • 𝜃 is a solution of query (𝑝 3 (𝑌¯3 ), Π) and 𝜃 is not a solution of query (𝑝 4 (𝑌¯3 ), Π). By the induction hypothesis, there exists a solution 𝑡 ∈ J𝑓2,3 ((𝑝 3 (𝑌¯3 ), Π))K𝑔2,3 (𝐷 ) such that 𝑡 ∉ J𝑓2,3 ((𝑝 4 (𝑌¯4 ), Π))K𝑔2,3 (𝐷 ) , and ℎ 32 (𝑡) = 𝜃 . By construction, 𝑡 ∈ J𝑓2,3 (𝑄)K𝑔2,3 (𝐷 ) . Hence, 𝜃 ∈ J𝑄K𝐷 if and only if there exists 𝜇 such that 𝑓12 (𝜇) = 𝜃 and 𝜇 ∈ J𝑓21 (𝑄)K𝑔21 (𝐷 ) . (b) We next prove that the answers have the same cardinality in MRA and NRMD¬ . By definition, ∑︁ card(𝜃, J𝑄K𝐷 ) = card(𝜃 ′, J(𝑝 (𝑌¯ ), Π)K𝐷 ) + 𝜃 ′ |𝑋¯ =𝜃
card(𝜃 1, J(𝑝 1 (𝑌¯1 ), Π)K𝐷 ) × card(𝜃 2, J(𝑝 2 (𝑌¯2 ), Π)K𝐷 ) + card(𝜃, J(𝑝 3 (𝑌¯3 ), Π)K𝐷 ).
The multiset semantics of SPARQL patterns
55
By the induction hypothesis, ∑︁ card(𝜃, J𝑄K𝐷 ) = card(𝑡 ′, J𝑓2,3 ((𝑝 (𝑌¯ ), Π))K𝑔2,3 (𝐷 ) ) + 𝜃 ′ |𝑋¯ =𝜃 ℎ 12 (𝑡 ′ )=𝜃 ′
card(𝑡 1, J𝑓2,3 ((𝑝 1 (𝑌¯1 ), Π))K𝑔2,3 (𝐷 ) ) × card(𝑡 2, J𝑓2,3 ((𝑝 2 (𝑌¯2 ), Π))K𝑔2,3 (𝐷 ) ) + card(𝑡, J𝑓2,3 ((𝑝 3 (𝑌¯3 ), Π))K𝑔 (𝐷 ) ) 2,3
= card(𝑡, J𝑓2,3 (𝑄)K𝑔2,3 (𝐷 ) ).
Hence, the triple (𝑓2,3, 𝑔2,3, ℎ 32 ) is a simulation of NRMD¬ in MRA.
□
Claim 8 (MRA to SPARQL). The triple (𝑓31, 𝑔31, ℎ 13 ) is a simulation of NRMD¬ in SPARQL. Proof. We proof this claim for normalized MRA expressions where the condition of a selection formula is always an equality atom (e.g., 𝜎𝐴=𝐵 (𝑅)). We proof this claim by induction on the structure of a MRA expression 𝐸, assuming that given an MRA database 𝐷, for every subquery 𝐸 ′ of 𝐸 it holds that 𝑡 ′ ∈ J𝐸 ′ K𝐷 if and only if there exists a SPARQL solution 𝜇 ′ ∈ J𝑓31 (𝐸)K𝑔31 (𝐷 ) such that ℎ 13 (𝜇 ′ ) = 𝑡 ′ . (1) If 𝐸 is a relation name 𝑅 then 𝑓31 (𝐸) is the SPARQL query (SELECT ?𝐴1 · · · ?𝐴𝑛 𝑃) where 𝑃 is the basic graph pattern ((?𝑋, 𝑢𝑏 , 𝑢𝑟 ) AND(?𝑋, 𝑢 1, ?𝐴1 ) AND · · · AND(?𝑋, 𝑢𝑛 , ?𝐴𝑛 ))), and ?𝐴1, . . . , ?𝐴𝑛 are the variables corresponding to the attributes associated to relation name 𝑅. b and 𝜇 be an SPARQL mapping with ℎ 13 (𝜇) = 𝑡. (a) Let 𝑡 be an MRA tuple with 𝑡ˆ = 𝑅, By definition, 𝑡 ∈ J𝐸K𝐷 if and only if tuple 𝑡 belongs to multiset relation 𝑅 𝐷 . By construction, 𝑡 ∈ J𝐸K𝐷 is thus equivalent to the existence of an an IRI 𝑢 such that the triples (𝑢, 𝑢𝑏 , 𝑢𝑟 ), (𝑢, 𝑢 1, 𝑡 (𝐴1 )), . . . , (𝑢, 𝑢𝑛 , 𝑡 (𝐴𝑛 )) belong to the RDF graph 𝑔31 (𝐷). By definition, there exists such an IRI 𝑢 if and only if there exists a SPARQL mapping 𝜇 ′ ∈ J𝑃K𝑔31 (𝐷 ) where 𝜇 ′ (?𝑋 ) = 𝑢 and 𝜇 (?𝐴𝑖 ) = 𝑡 (𝑎𝑖 ), for 1 ≤ 𝑖 ≤ 𝑛. By construction, 𝜇 ′ | ?𝐴1,...,?𝐴𝑛 = 𝜇. Then, 𝜇 ′ ∈ J𝑃K𝑔31 (𝐷 ) if and only if 𝜇 ∈ J𝑓31 (𝑄)K𝑔31 (𝐷 ) . (b) The fact that card(𝑡, J𝐸K𝐷 ) = card(𝜇, J𝑓31 (𝐸)K𝑔31 (𝐷 ) ) follows by construction; the cardinality of 𝑡 in the multiset relation 𝑅 𝐷 is the same as the number of IRIs 𝑢 such that such that the triples (𝑢, 𝑢𝑏 , 𝑢𝑟 ), (𝑢, 𝑢 1, 𝑡 (𝐴1 )), . . . , (𝑢, 𝑢𝑛 , 𝑡 (𝐴𝑛 )) belong to the RDF graph 𝑔31 (𝐷). b 𝐸b2 = 𝐸, b and 𝑓31 (𝐸) is the SPARQL query (2) If 𝐸 is a query 𝐸 1 ∪ 𝐸 2 , then 𝐸b1 = 𝐸, (𝑓31 (𝐸 1 ) UNION 𝑓31 (𝐸 2 )). b and 𝜇 be an SPARQL mapping such that ℎ 13 (𝜇) = 𝑡. By (a) Let 𝑡 be an MRA tuple with 𝑡ˆ = 𝐸, definition, 𝑡 ∈ J𝐸K𝐷 if and only if 𝑡 ∈ J𝐸 1 K𝐷 or 𝑡 ∈ J𝐸 2 K𝐷 . By the induction hypothesis, 𝑡 ∈ J𝐸K𝐷 if and only if 𝜇 ∈ J𝑓31 (𝐸 1 )K𝑔31 (𝐷 ) or 𝜇 ∈ J𝑓31 (𝐸 2 )K𝑔31 (𝐷 ) . By definition, 𝑡 ∈ J𝐸K𝐷 if and only if 𝜇 ∈ J𝑓31 (𝐸)K𝑔31 (𝐷 ) . (b) Assume the respective answers 𝑡 and 𝜇 described in (a). By definition, card(𝑡, J𝐸K𝐷 ) = card(𝑡, J𝐸 1 K𝐷 ) + card(𝑡, J𝐸 2 K𝐷 ),
card(𝜇, J𝑓31 (𝐸)K𝑔31 (𝐷 ) ) = card(𝜇, J𝑓31 (𝐸 1 )K𝑔31 (𝐷 ) ) + card(𝜇, J𝑓31 (𝐸 2 )K𝑔31 (𝐷 ) ). By the induction hypothesis, these two cardinalities are equal. b and 𝑓31 (𝐸) is the SPARQL query (3) If 𝐸 is a query 𝐸 1 Z 𝐸 2 then 𝐸b1 ∪ 𝐸b2 = 𝐸, (𝑓31 (𝐸 1 ) AND 𝑓31 (𝐸 2 )). b and 𝜇 be an SPARQL mapping such that ℎ 13 (𝜇) = 𝑡. (a) Let 𝑡 be an MRA tuple with 𝑡ˆ = 𝐸, By definition, 𝑡 ∈ J𝐸K𝐷 if and only if there exists two tuples 𝑡 1 and 𝑡 2 such that 𝑡 1 ∼ 𝑡 2 , 𝑡 = 𝑡 1 ∪ 𝑡 2 , 𝑡 1 ∈ J𝐸 1 K𝐷 and 𝑡 2 ∈ J𝐸 2 K𝐷 . By the induction hypothesis, 𝑡 1 ∈ J𝐸 1 K𝐷 and 𝑡 2 ∈ J𝐸 2 K𝐷 if and only there exist two SPARQL mappings 𝜇1 and 𝜇2 such that ℎ 13 (𝜇 1 ) = 𝑡 1 ,
56
Angles et al.
ℎ 13 (𝜇 2 ) = 𝑡 2 , 𝜇1 ∈ J𝑓31 (𝐸 1 )K𝑔31 (𝐷 ) , and 𝜇1 ∈ J𝑓31 (𝐸 1 )K𝑔31 (𝐷 ) . By construction, 𝜇1 ∼ 𝜇2 , 𝜇1 ∪ 𝜇 2 = 𝜇, and 𝜇 ∈ J𝑓31 (𝐸)K𝑔31 (𝐷 ) . Hence, 𝑡 ∈ J𝐸K𝐷 if and only if 𝜇 ∈ J𝑓31 (𝐸)K𝑔31 (𝐷 ) . (b) Assume the respective answers 𝑡, 𝑡 1 , 𝑡 2 , 𝜇, 𝜇 1 , and 𝜇2 described in (a). By definition, card(𝑡, J𝐸K𝐷 ) = card(𝑡 1, J𝐸 1 K𝐷 ) × card(𝑡 2, J𝐸 1 K𝐷 ),
card(𝜇, J𝑓31 (𝐸)K𝑔31 (𝐷 ) ) = card(𝜇1, J𝑓31 (𝐸 1 )K𝑔31 (𝐷 ) ) × card(𝜇 2, J𝑓31 (𝐸 1 )K𝑔31 (𝐷 ) ). By the induction hypothesis, these two cardinalities are equal. b 𝐸b1 = 𝐸, b and 𝑓31 (𝐸) is the SPARQL query (4) If 𝐸 is a query 𝐸 1 \ 𝐸 2 then 𝐸b1 = 𝐸, (𝑓31 (𝐸 1 ) EXCEPT 𝑓31 (𝐸 2 )). b and 𝜇 be an SPARQL mapping such that ℎ 13 (𝜇) = 𝑡. By (a) Let 𝑡 be an MRA tuple with 𝑡ˆ = 𝐸, definition, 𝑡 ∈ J𝐸K𝐷 if and only if 𝑡 ∈ J𝐸 1 K𝐷 and 𝑡 ∉ J𝐸 2 K𝐷 . By the induction hypothesis, 𝑡 ∈ J𝐸K𝐷 if and only if 𝜇 ∈ J𝑓31 (𝐸 1 )K𝑔31 (𝐷 ) and 𝜇 ∉ J𝑓31 (𝐸 2 )K𝑔31 (𝐷 ) . Hence, 𝑡 ∈ J𝐸K𝐷 if and only if 𝜇 ∈ J𝑓31 (𝐸)K𝑔31 (𝐷 ) . (b) Assume the respective answers 𝑡 and 𝜇 described in (a). By definition, card(𝑡, J𝐸K𝐷 ) = card(𝑡, J𝐸 1 K𝐷 ) and card(𝜇, J𝑓31 (𝐸)K𝑔31 (𝐷 ) ) = card(𝜇, J𝑓31 (𝐸 1 )K𝑔31 (𝐷 ) ). By the induction hypothesis, these two cardinalities are equal. (5) If 𝐸 is a query 𝜋𝑆 (𝐸 1 ) then 𝐸b = 𝑆 and 𝑆 ⊆ 𝐸b1 , and 𝑓31 (𝐸) is a SPARQL query (SELECT 𝑊 𝑓31 (𝐸 1 )) such that 𝑊 is the corresponding set of SPARQL variables for the set of attributes 𝑆. b By definition, 𝑡 ∈ J𝐸K𝐷 if and only if there exists a (a) Let 𝑡 be an MRA tuple with 𝑡ˆ = 𝐸. tuple 𝑡 1 ∈ J𝐸 1 K𝐷 such that 𝑡 1 | 𝐸b = 𝑡. By the induction hypothesis, 𝑡 1 ∈ J𝐸K𝐷 if and only if there exists 𝜇1 ∈ J𝑓31 (𝐸 1 )K𝑔31 (𝐷 ) such that ℎ 13 (𝜇 1 ) = 𝑡 1 . Let 𝜇 be 𝜇1 |𝑊 . By construction, 𝑡 ∈ J𝐸K𝐷 if and only if 𝜇 ∈ J𝑓31 (𝐸)K𝑔31 (𝐷 ) and ℎ 13 (𝜇) = 𝑡. (b) Assume the respective answers 𝑡 and 𝜇 described in (a). By definition, ∑︁ card(𝑡, J𝐸K𝐷 ) = card(𝑡 1, J𝐸 1 K𝐷 ), 𝑡 1 ∈ J𝐸 1 K𝐷 𝑡 1 | 𝐸b=𝑡
card(𝜇, J𝑓31 (𝐸)K𝑔31 (𝐷 ) ) =
∑︁ 𝜇 1 ∈ J 𝑓31 (𝐸 1 ) K𝑔31 (𝐷 ) 𝜇 1 |𝑊 =𝜇
card(𝜇 1, J𝑓31 (𝐸 1 )K𝑔31 (𝐷 ) ).
By the induction hypothesis, these two cardinalities are equal. (6) If 𝐸 is a query 𝜌𝐴/𝐵 (𝐸 1 ) then 𝐸b = (𝐸b1 \ {𝐴}) ∪ {𝐵}, 𝑓31 (𝐸) is the SPARQL query that results from consistently renaming variable ?𝐴 as variable ?𝐵 in 𝑓31 (𝐸 1 ) (i.e., subs?𝐴/?𝐵 (𝐴)), and ?𝐴 and ?𝐵 are the corresponding SPARQL variables for atributes 𝐴 and 𝐵. (a) By definition, 𝑡 ∈ J𝐸K𝐷 if and only if there exists a tuple 𝑡 1 ∈ J𝐸 1 K𝐷 where 𝑡 (𝐶) = 𝑡 1 (𝐶) for every attribute 𝐶 ∈ 𝐸b \ {𝐴}, and 𝑡 (𝐴) = 𝑡 1 (𝐵). By the induction hypothesis, 𝑡 1 ∈ J𝐸 1 K𝐷 if and only if there exists a solution 𝜇1 ∈ J𝑓31 (𝐸 1 )K𝑔31 (𝐷 ) such that ℎ 13 (𝜇 1 ) = 𝑡 1 . Let 𝜇 be the SPARQL mapping with domain (dom(𝜇 ′ ) \ {?𝐴}) ∪ {?𝐵} such that 𝜇 (?𝐶) = 𝜇1 (?𝐶) for every variable ?𝐶 ∈ dom(𝜇 ′ ) \ {?𝐴}, and 𝜇 (?𝐴) = 𝜇 1 (?𝐵). By construction, ℎ 13 (𝜇) = 𝑡. Hence, 𝑡 ∈ J𝐸K𝐷 if and only if 𝜇 ∈ J𝑓31 (𝐸)K𝑔31 (𝐷 ) . (b) Assume the respective query answers 𝑡, 𝑡 1 , 𝜇, and 𝜇 1 described in (a). By definition, card(𝑡, J𝐸K𝐷 ) = card(𝑡 1, J𝐸 1 K𝐷 ),
card(𝜇, J𝑓31 (𝐸)K𝑔31 (𝐷 ) ) = card(𝜇1, J𝑓31 (𝐸 1 )K𝑔31 (𝐷 ) ). By the induction hypothesis, these two cardinalities are equal.
The multiset semantics of SPARQL patterns
57
b Π), and 𝑓31 (𝐸 1 ) is the SPARQ query (7) If 𝐸 is a query 𝜎𝐴=𝐵 (𝐸 1 ) then 𝐸b = 𝐸b1 , 𝑓31 (𝐸) = (𝑞(𝐸), (𝑃 1 FILTER ?𝐴 = ?𝐵) where ?𝐴 and ?𝐵 are the corresponding SPARQL variables for the MRA attributes 𝐴 and 𝐵. (a) By definition, 𝑡 ∈ J𝐸K𝐷 if and only if 𝑡 ∈ J𝐸 1 K𝐷 and 𝑡 (𝐴) = 𝑡 (𝐵). By the induction hypothesis, there is an answer 𝜇 ∈ J𝑓31 (𝐸 1 )K𝑔31 (𝐷 ) such that ℎ 13 (𝜇) = 𝑡. By construction, 𝜇 (𝐴) = 𝜇 (𝐵). Then, 𝑡 ∈ J𝐸K𝐷 if and only if 𝜇 ∈ J𝑓31 (𝐸)K𝑔31 (𝐷 ) . (b) Assume the respective query answers 𝑡 and 𝜃 described in (a). By definition, card(𝑡, J𝐸K𝐷 ) = card(𝑡 1, J𝐸 1 K𝐷 ),
card(𝜇, J𝑓31 (𝐸)K𝑔31 (𝐷 ) ) = card(𝜇1, J𝑓31 (𝐸 1 )K𝑔31 (𝐷 ) ). By the induction hypothesis, these two cardinalities are equal. Hence, the triple (𝑓31, 𝑔31, ℎ 13 ) is a simulation of MRA in NRMD¬ .
□
Claim 9 (SPARQL to MRA). The triple (𝑓13, 𝑔13, ℎ 31 ) is a simulation of NRMD¬ in SPARQL. Proof. To prove this claim we show that, for every SPARQL query 𝑄 and RDF graph 𝐺, it holds that J𝑄K𝐺 = ℎ 31 (J𝑓13 (𝑄)K𝑔13 ). For simplicity, we write 𝐷 instead of 𝐷. We next show this identity by induction on the structure of a normalized SPARQL query 𝑄. In this proof we assume that 𝑡 is a MRA tuple with the attributes of the MRA expression 𝑓13 (𝑄), and 𝜇 is the SPARQL mapping ℎ 31 (𝑡). To show that J𝑄K𝐺 = ℎ 31 (J𝑓13 (𝑄)K𝑔13 ), we prove that 𝜇 ∈ J𝑄K𝐺 if and only if 𝑡 ∈ J𝑓13 (𝑄)K𝐷 and card(𝜇, J𝑄K𝐺 ) = card(𝑡, J𝑓13 (𝑄)K𝐷 ). (1) Case 𝑄 is a triple pattern. (a) By definition, every triple pattern is translated to a MRA expression consisting of operations 𝜎, 𝜌, and 𝜋 over the relation name Trip. For example, if 𝑄 is the triple pattern (?𝑋, 𝑝, ?𝑋 ), then 𝑓12 (𝑄) is the expression Π𝑋 (𝜌𝑆/𝑋 (𝜎𝑃=𝑝∧𝑆=𝑂 (Trip))). It can be shown that the triple pattern 𝑄 = (?𝑋, 𝑝, ?𝑋 ) is equivalent to the SPARQL query 𝑄 ′ = (SELECT ?𝑋 ((?𝑋, ?𝑃, ?𝑂) FILTER(?𝑃 = 𝑝 ∧ ?𝑋 =?𝑂)). Then, 𝜇 ∈ J𝑄K𝐺 if and only if there exists a solution 𝜇 ′ ∈ J(?𝑋, ?𝑃, ?𝑂)K𝐺 such that 𝜇 = 𝜇 ′ | {𝑋 } , 𝜇 ′ (?𝑃) = 𝑝 and 𝜇 ′ (?𝑋 ) = 𝜇 ′ (?𝑂). Without loss of generality, let 𝜇 ′ (?𝑋 ) = 𝑎. Such mapping 𝜇 ′ is a solution of the triple pattern (?𝑋, ?𝑃, ?𝑂) if and only if (𝑎, 𝑝, 𝑎) ∈ 𝐺. By construction, (𝑎, 𝑝, 𝑎) ∈ 𝐺 if and only if (𝑎, 𝑝, 𝑎) ∈ Trip𝐷 , where 𝐷 is the MRA database 𝐷. If (𝑎, 𝑝, 𝑎) ∈ Trip𝐷 then 𝑡 ∈ J𝑓13 (𝑄)K𝐷 . Hence, 𝜇 ∈ J𝑄K𝐺 if and only if 𝑡 ∈ J𝑓13 (𝑄)K𝐷 . So far, we showed that the claim follows for a particular triple pattern. This result can be extended for all the triple patterns following the same procedure. (b) By construction, card(𝜇, J𝑓1,1 (𝑄)K𝑔1,1 (𝐺 ) ) = 1 and card(𝜇, J𝑄K𝐺 ) = 1. Hence, card(𝑡, J𝑓1,1 (𝑄)K𝑔1,1 (𝐺 ) ) = card(𝜇, J𝑄K𝐺 ). (2) Case 𝑄 is a query (𝑃 1 AND 𝑃2 ). Without loss of generality assume that inScope(𝑃1 ) = {?𝑋, ?𝑌 } and inScope(𝑃2 ) = {?𝑋, ?𝑍 }. By definition, the MRA expression for query 𝑄 is: 𝑓13 (𝑄) = 𝑓13 (𝑃 1 ) ∗ 𝑓13 (𝑃 2 ) = 𝜋𝑋 ,𝑌 ,𝑍 (𝜌𝐴1 /𝑋1 (𝜌𝐴2 /𝑋2 (𝜌𝐴/𝑋 (Comp))) Z 𝜌𝑋 /𝑋1 (𝑓13 (𝑃 1 )) Z 𝜌𝑋 /𝑋2 (𝑓13 (𝑃 2 ))). (a) If 𝑡 ∈ J𝑓13 (𝑄)K𝐷 then, there are MRA tuples 𝑡 1 ∈ J𝑓13 (𝑃 1 )K𝐷 , 𝑡 2 ∈ J𝑓13 (𝑃 2 )K𝐷 , and 𝑡 3 ∈ JCompK𝐷 such that 𝑡 (𝑋 ) = 𝑡 3 (𝐴), 𝑡 (𝑌 ) = 𝑡 2 (𝑌 ), 𝑡 (𝑍 ) = 𝑡 3 (𝑍 ), and 𝑡 1 (𝑋 ) = 𝑡 3 (𝐴1 ), 𝑡 2 (𝑋 ) = 𝑡 3 (𝐴2 ). Let 𝜇1 = ℎ 31 (𝑡 1 ), 𝜇2 = ℎ 31 (𝑡 2 ), and 𝜇3 = ℎ 31 (𝑡 3 ). By the induction hypothesis in 𝑃1 and 𝑃 2 , 𝑡 1 ∈ J𝑓13 (𝑃 1 )K𝐷 and 𝑡 2 ∈ J𝑓13 (𝑃 2 )K𝐷 if and only if 𝜇1 ∈ J𝑃1 K𝐺 and 𝜇2 ∈ J𝑃 2 K𝐺 . By the definition of Comp𝐷 , it holds that 𝜇1 ∼ 𝜇 2 and 𝜇1 ∪ 𝜇 2 = 𝜇. By the semantics of the SPARQL operator AND, it holds then that 𝜇 ∈ J𝑄K𝐺 . Hence, 𝑡 ∈ J𝑓13 (𝑄)K𝐷 if and only if 𝜇 ∈ J𝑄K𝐺 .
58
Angles et al.
(b) By definition, card(𝜇, J𝑄K𝐺 ) =
card(𝑡, J𝑓13 (𝑄)K𝐷 ) =
∑︁ 𝜇 1 ∈ J𝑃1 K𝐺 𝜇 2 ∈ J𝑃2 K𝐺 𝜇 1 ∼𝜇 2 𝜇=𝜇1 ∪𝜇2
card(𝜇 1, J𝑃1 K𝐺 ) × card(𝜇 2, J𝑃2 K𝐺 ),
∑︁ 𝑡 1 ∈ J 𝑓13 (𝑃 1 ) K𝐷 𝑡 2 ∈ J 𝑓13 (𝑃 2 ) K𝐷 𝑡 3 ∈ JCompK𝐷 𝜑 (𝑡 1 ,𝑡 2 ,𝑡 3 )
card(𝑡 3, JCompK𝐷 ) × card(𝑡 1, J𝑃1 K𝐷 ) × card(𝑡 2, J𝑃2 K𝐷 ),
where 𝜑 (𝑡 1, 𝑡 2, 𝑡 3 ) is a condition coresponding to the compatibility, that is true if and only if the following statements hold: (i) 𝑡 1 (𝑌 ) = 𝑡 (𝑌 ), (ii) 𝑡 2 (𝑍 ) = 𝑡 (𝑍 ), and (iii) either (A) (𝑡 1 (𝑋 ) = 𝑡 (𝑋 ) and 𝑡 2 (𝑋 ) = 𝑡 (𝑋 ), (B) (𝑡 1 (𝑋 ) = 𝑡 (𝑋 ) and 𝑡 2 (𝑋 ) = 𝑡 (𝑋 ), or (C) (𝑡 1 (𝑋 ) = 𝑡 (𝑋 ) and 𝑡 2 (𝑋 ) = 𝑡 (𝑋 ). By the induction hypothesis, card(𝜇 1, J𝑃1 K𝐺 ) = card(𝑡 1, J𝑓13 (𝑃 1 )K𝐷 ) and card(𝜇 2, J𝑃2 K𝐺 ) = card(𝑡 2, J𝑓13 (𝑃 2 )K𝐷 ). By construction, card(𝑡 3, JCompK𝐷 ) = 1. Hence, card(𝜇, J𝑄K𝐺 ) = card(𝑡, J𝑓13 (𝑄)K𝐷 ). (3) Case 𝑄 is a query (𝑃1 EXCEPT 𝑃2 ). Let ?𝑋¯ be the list of SPARQL variables in set inScope(𝑄). The MRA query 𝑓13 (𝑄) is then 𝑓13 (𝑃 1 ) \ 𝑓13 (𝑃 2 ). (a) By definition, 𝑡 ∈ J𝑓13 (𝑄)K𝐷 if and only if 𝑡 ∈ J𝑓13 (𝑃 1 )K𝐷 and 𝑡 ∉ J𝑓13 (𝑃2 )K𝐷 . By the induction hypothesis, the last condition is equivalent to 𝜇 ∈ J𝑃 1 K𝐺 and 𝜇 ∉ J𝑃 2 K𝐺 . By the SPARQL semantics, 𝑡 ∈ J𝑓13 (𝑄)K𝐺 if and only if 𝜇 ∈ J𝑄K𝐺 . (b) By definition, card(𝜇, J𝑄K𝐺 ) = card(𝜇, J𝑃1 K𝐺 ) and card(𝑡, J𝑓13 (𝑄)K𝐷 ) = card(𝑡, J𝑓13 (𝑃 1 )K𝐷 ). By the induction hypothesis, card(𝜇, J𝑃 1 K𝐺 ) = card(𝑡, J𝑓13 (𝑃1 )K𝐷 ). Hence, card(𝜇, J𝑄K𝐺 ) = card(𝑡, J𝑓13 (𝑄)K𝐷 ). (4) Case 𝑄 is a SPARQL query (𝑃 1 UNION 𝑃2 ). The MRA expression 𝑓13 (𝑄) is then 𝑓13 (𝑃 1 ) ∪ 𝑓13 (𝑃 2 ). (a) By definition, 𝑡 ∈ J𝑓13 (𝑄)K𝐺 if and only if 𝑡 ∈ J𝑓13 (𝑃 1 )K𝐷 or 𝑡 ∈ J𝑓13 (𝑃 2 )K𝐷 . By the induction hypothesis, 𝑡 ∈ J𝑓13 (𝑄)K𝐺 if and only if 𝜇 ∈ J𝑃 1 K𝐺 or 𝜇 ∈ J𝑃 1 K𝐺 . By the SPARQL semantics, 𝑡 ∈ J𝑓13 (𝑄)K𝐺 if and only if 𝜇 ∈ J𝑄K𝐺 . (b) By definition, card(𝑡, J𝑓13 (𝑄)K𝐷 ) = card(𝑡, J𝑓13 (𝑃 1 )K𝐷 ) + card(𝑡, J𝑓13 (𝑃 2 )K𝐷 ), card(𝜇, J𝑄K𝐺 ) = card(𝜇, J𝑃1 K𝐺 ) + card(𝜇, J𝑃2 K𝐺 ).
By the induction hypothesis, card(𝑡, J𝑓13 (𝑃 1 )K𝐷 ) = card(𝜇, J𝑃1 K𝐺 )
and
card(𝑡, J𝑓13 (𝑃 2 )K𝐷 ) = card(𝜇, J𝑃2 K𝐺 ).
Hence card(𝑡, J𝑓13 (𝑄)K𝐷 ) = card(𝜇, J𝑄K𝐺 ). (5) Case 𝑄 is a SPARQL query (𝑃 FILTER 𝜑) where 𝜑 is an atomic filter condition (i.e., a filter condition of the form ?𝑋 = 𝑐, ?𝑋 = ?𝑌 , or bound(?𝑋 )). The MRA expression 𝑓13 (𝑄) is then
The multiset semantics of SPARQL patterns
59
𝜎𝜓 (𝑓13 (𝑃)) where 𝜓 is the MRA selection condition defined as follows: if 𝜑 is ?𝑋 = 𝑐, 𝑋 = 𝑐 ∧ ¬(𝑋 = ⊥) if 𝜑 is ?𝑋 = ?𝑌 , 𝜓 = 𝑋 = 𝑌 ∧ ¬(𝑋 = ⊥) ∧ ¬(𝑌 = ⊥) ¬(𝑋 = ⊥) if 𝜑 is bound(?𝑋 ).
(a) By definition, 𝑡 ∈ J𝑓13 (𝑄)K𝐷 if and only if 𝑡 ∈ J𝑓13 (𝑃)K𝐷 and 𝑡 satisfies condition 𝜓 . It is not difficult to see that 𝑡 satisfies condition 𝜓 if and only if 𝜇 satisfies condition 𝜑. By the induction hypothesis, 𝑡 ∈ J𝑓13 (𝑃)K𝐷 if and only if 𝜇 ∈ J𝑃K𝐺 . Hence, 𝜇 ∈ J𝑓13 (𝑄)K𝐷 if and only if 𝜇 ∈ J𝑄K𝐺 . (b) By definition, if 𝑡 and 𝜇 satisfy the respective conditions, then: card(𝑡, J𝑓13 (𝑄)K𝐷 ) = card(𝑡, J𝑓13 (𝑃)K𝐷 ), card(𝜇, J𝑄K𝐺 ) = card(𝜇, J𝑃K𝐺 ).
By the induction hypothesis, card(𝑡, J𝑓13 (𝑃)K𝐷 ) = card(𝜇, J𝑃K𝐺 ). Hence,
card(𝑡, J𝑓13 (𝑄)K𝐷 ) = card(𝜇, J𝑄K𝐺 ). (6) Case 𝑄 is a SPARQL query (SELECT ?𝑋¯ 𝑃). The MRA expression 𝑓13 (𝑄) is then 𝜋𝑋¯ (𝑓13 (𝑃) Z Δ𝑌¯ ), where 𝑋¯ is the corresponding set of attributes for the variables ?𝑋¯ and 𝑌¯ is the correponding set of attributes for the variables in set inScope(𝑃) \ inScope(𝑄). (a) By definition, 𝑡 ∈ J𝑓13 (𝑄)K𝐷 if and only if 𝑡 (𝑌 ) = ⊥ for every attribute name 𝑌 ∈ 𝑌¯ and there exists a solution 𝑡 ′ ∈ J𝑓13 (𝑃)K𝐷 such that 𝑡 ′ (𝐴) = 𝑡 (𝐴) for every attribute 𝐴 ∈ 𝑋¯ \ 𝑌¯ . Let 𝜇 ′ = ℎ 31 (𝑡 ′ ). By the induction hypothesis, 𝑡 ′ ∈ J𝑓13 (𝑃)K𝐷 if and only if 𝜇 ′ ∈ J𝑃K𝐺 . By construction 𝜇 = 𝜇 ′ | ?𝑋¯ . Hence, 𝑡 ∈ J𝑓13 (𝑄)K𝐷 if and only if 𝑡 ∈ J𝑃K𝐺 . (b) By construction, ∑︁ card(𝑡, J𝑓13 (𝑄)K𝐷 ) = card(𝑡 ′, J𝑓13 (𝑃)K𝐷 ), 𝑡 ′ | inScope(𝑄 ) =𝑡 𝑡 ′ ∈ J 𝑓13 (𝑃 ) K𝐷
card(𝜇, J𝑄K𝐺 ) =
∑︁
𝜇 ′ | inScope(𝑄 ) =𝜇 𝜇 ′ ∈ J𝑃 K𝐺
card(𝜇 ′, J𝑃K𝐺 ),
By the induction hypothesis, card(𝑡 ′, J𝑓13 (𝑃)K𝐷 ) = card(𝜇 ′, J𝑃K𝐺 ). Hence, card(𝑡, J𝑓13 (𝑄)K𝐷 ) = card(𝜇 ′, J𝑄K𝐺 ).
Hence, the triple (𝑓13, 𝑔13, ℎ 31 ) is a simulation of SPARQL in MRA.
□