Docsity

Containment Mappings in Datalog Queries, Study notes of Conjunctive Queries

Toggle sidebar
bg1
Conjunctive Queries
= safe, Datalog rules:
H
:-
G
1
&

&
G
n
Most common form of query; equivalentto
select-project-join queries.
Useful for optimization of active elements,
e.g., checking distributed constraints,
maintaining materialized views.)
Useful for information integration.
Applying a CQ to a Database
If
Q
is a CQ, and
D
is a database of EDB facts,
then
Q
(
D
) is the set of heads of
Q
that we get
when we:
Substitute constants for variables in the bo dy
of
Q
in all possible ways.
Require all subgoals to become true.
Example
p
(
X; Y
):
,
q
(
X; Z
)&
q
(
Z; Y
)
EDB =
f
q
(1
;
2)
;q
(2
;
3)
;q
(3
;
4)
g
.
Only substitutions that make subgoals both
true:
1.
X
!
1;
Y
!
3;
Z
!
2.
2.
X
!
2;
Y
!
4;
Z
!
3.
Yield heads
p
(1
;
3) and
p
(2
;
4).
Containment
Q
1
Q
2
i for every database
D
,
Q
1
(
D
)
Q
2
(
D
).
Containment problem is NP-complete, but
not a \hard" problem in practical situations
(short queries, few pairs of subgoals with same
predicate).
Function symbols do not make problems more
dicult.
Adding negated subgoals and/or arithmetic
subgoals, e.g.,
X<Y
, makes things more
complex, but important special cases.
Example
1
docsity.com
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Containment Mappings in Datalog Queries and more Study notes Conjunctive Queries in PDF only on Docsity!

Conjunctive Queries

= safe, Datalog rules:

H :- G 1 &    & Gn

 Most common form of query; equivalent to select-pro ject-join queries.

 Useful for optimization of active elements, e.g., checking distributed constraints, maintaining materialized views.)

 Useful for information integration.

Applying a CQ to a Database

If Q is a CQ, and D is a database of EDB facts, then Q(D ) is the set of heads of Q that we get when we:

 Substitute constants for variables in the b o dy of Q in all p ossible ways.

 Require all subgoals to b ecome true.

Example

p(X ; Y ) : q (X ; Z ) & q (Z ; Y )

 EDB = fq (1; 2); q (2; 3); q (3; 4)g.

 Only substitutions that make subgoals b oth true:

  1. X! 1; Y! 3; Z! 2.
  2. X! 2; Y! 4; Z! 3.

 Yield heads p(1; 3) and p(2; 4).

Containment

Q 1  Q 2 i for every database D , Q 1 (D )  Q 2 (D ).

 Containment problem is NP-complete, but not a \hard" problem in practical situations (short queries, few pairs of subgoals with same predicate).

 Function symb ols do not make problems more dicult.

 Adding negated subgoals and/or arithmetic subgoals, e.g., X < Y , makes things more complex, but imp ortant sp ecial cases.

Example

A: p(X,Y) :- r(X,W) & b(W,Z) & r(Z,Y) B : p(X,Y) :- r(X,W) & b(W,W) & r(W,Y)

 Claim: B  A.

 In pro of, supp ose p(x; y ) is in B (D ). Then there is some w such that r (x; w ), b(w ; w ), and r (w ; y ) are in D.

 In A, make the substitution X! x, Y! y , W! w , Z! w.

 Thus, the head of A b ecomes p(x; y ), and all subgoals of A are in D.

 Thus, p(x; y ) is also in A(D ), proving B  A.

Testing Containment of CQ's

  1. Containment mappings.
  2. Canonical databases.

 Similar for basic CQ case, but (2) is useful for more general cases like negated subgoals.

Containment Mappings

Mapping from variables of CQ Q 2 to variables of CQ Q 1 such that

  1. Head of Q 2 b ecomes head of Q 1.
  2. Each subgoal of Q 2 b ecomes some subgoal of Q 1. F It is not necessary that every subgoal of Q 1 is the target of some subgoal of Q 2.

Example

A, B as ab ove:

A: p(X,Y) :- r(X,W) & b(W,Z) & r(Z,Y) B : p(X,Y) :- r(X,W) & b(W,W) & r(W,Y)

 Containment mapping from A to B : X! X , Y! Y , W! W , Z! W.

 No containment mapping from B to A. Subgoal b(W; W ) in B can only go to b(W; Z ) in A. That would require b oth W! W and W! Z.

Example

C 1 : p(X) :- a(X,Y) & a(Y,Z) & a(Z,W) C 2 : p(X) :- a(X,Y) & a(Y,X)

Pro of (Only If ) Continued

Let Q 1  Q 2. Let database D b e the frozen Q 1.

 Q 1 (D ) contains t, the \frozen" head of Q 1

F Sounds gruesome, but the reason is that we can use the substitution in which each variable of Q 1 is replaced by its corresp onding constant.

 Since Q 1  Q 2 , Q 2 (D ) must also contain t.

 Let  b e the substitution of constants from D for the variables of Q 2 that makes each subgoal of Q 2 a tuple of D and yields t as the head.

 Let  b e the substitution that maps constants of D to their unique, corresp onding variable of Q 1.

E :- F 1 &   

t

H :- G 1 &    &

Q 2 :

Q 1 :



Gi (A; B ) &   



Fm (X ; Y )

a b

    is a containment mapping from Q 2 to Q 1 b ecause: a) The head of Q 2 is mapp ed by  to t, and t is the frozen head of Q 1 , so    maps the head of Q 2 to the \unfrozen" t, that is, the head of Q 1.

b) Each subgoal Fi of Q 2 is mapp ed by  to some tuple of D , which is a frozen version of some subgoal Gj of Q 1. Then    maps Fi to the unfrozen tuple, that is, to Gj itself.

Dual View of Containment Mappings

A containment mapping, de ned as a mapping on variables, induces a mapping on subgoals.

 Therefore, we can alternatively de ne a containment mapping as a function on subgoals, thus inducing a mapping on variables.

 The containment mapping condition b ecomes: the subgoal mapping do es not cause a variable to b e mapp ed to two di erent variables or

4

constants, nor cause a constant to b e mapp ed to a variable or a constant other than itself.

Example

Again consider

A: p(X,Y) :- r(X,W) & b(W,Z) & r(Z,Y) B : p(X,Y) :- r(X,W) & b(W,W) & r(W,Y)

 Previously, we found the containment mapping X! X , Y! Y , W! W , Z! W from A to B.

 We could as well describ e this mapping as r (X ; W )! r (X ; W ), b(W; Z )! b(W; W ), and r (Z ; Y )! r (W; Y ).

Metho d of Canonical Databases

Instead of lo oking for a containment mapping from Q 2 to Q 1 in order to test Q 1  Q 2 , we can apply the following test:

  1. Create a canonical database D that is the frozen b o dy of Q 1.
  2. Compute Q 2 (D ).
  3. If Q 2 (D ) contains the frozen head of Q 1 , then Q 1  Q 2 ; else not.

 The pro of that this metho d works is essentially the same as the argument for containment mappings:

F The only way the frozen head of Q 1 can b e in Q 2 (D ) is for there to b e a containment mapping Q 2! Q 1.

Example

C 1 : p(X) :- a(X,Y) & a(Y,Z) & a(Z,W) C 2 : p(X) :- a(X,Y) & a(Y,X)

Here is the test for C 2  C 1 :

 Cho ose constants X! 0, Y! 1.

 Canonical DB from C 1 is

D = fa(0; 1); a(1; 0)g

 C 1 (D ) = fp(0); p(1)g.

  1. We close the set of inferences we must make.

F Then we can forever forget ab out the question of how to map the determined subgoals and variables.

F We have found one mapping that works and that can't interfere with the mapping of any other subgoals or variables, so we make another arbitrary choice if there are any unmapp ed subgoals.

Example

Let us test C 1  C 2 , where:

C 1 : p(B) :- a(A,B) & a(B,A) & b(A,C) & b(C,B) C 2 : p(X) :- a(X,Y) & b(Y,Z) & b(Z,W) & a(W,X)

 Note this simple example omits some options: C 1 could have a predicate app earing only once in the b o dy, and C 2 could have 3 or more o ccurrences of some predicates.

 Here is a description of inferences that might b e made: (1) Supp ose a(X ; Y )! a(A; B ) (2) Then X! A, Y! B (3) Now, b(Y ; Z )! b(B ; ?) (4) Since there is no b(B ; ?), fail (5) Thus, we must map a(X ; Y )! a(B ; A) (6) Then X! B and Y! A, (7) b(Y ; Z )! b(A; C ), Z! C , (8) b(Z ; W )! b(C ; B ), W! B (9) Now, a(W; X ) must map to a(B ; B ) (10) Since a(B ; B ) do es not exist, fail

 Note, however, that if the last subgoal of C 1 were b(C ; A), we would have W! A at line (8) and a(W; X )! a(A; B ) at line (9).

F That completes the containment mapping successfully, with X! B , Y! A, Z! C , and W! A.

Generalization to Unions of CQ's

P 1 [ P 2 [    [ Pk  Q 1 [ Q 2 [    [ Qn i for all Pi there exists some Qj such that Pi  Qj.

Pro of (If )

Obvious.

7

Pro of (Only If )

Assume the containment holds.

 Let D b e the canonical (frozen) database from CQ Pi.

 Since the containment holds, and Pi (D ) surely includes the frozen head of Pi , there must b e some Qj such that Qj (D ) includes the frozen head of Pi.

 Thus, Pi  Qj.

Union Theorem Just Misses Being False

Consider generalized CQ's allowing arithmetic- comparison subgoals.

P 1 : p(X) :- e(X) & 10 <= X & X <= 20 Q 1 : p(X) :- e(X) & 10 <= X & X <= 15 Q 2 : p(X) :- e(X) & 15 <= X & X <= 20

 P 1  Q 1 [ Q 2 , but P 1  Q 1 and P 1  Q 2 are b oth false.

CQ Contained in Recursive Datalog

Test relies on metho d of canonical DB's; containment mapping approach do esn't work (it's meaningless).

 Make DB D from frozen b o dy of CQ.

 Apply program to D. If frozen head of CQ app ears in result, then yes (contained), else no.

Example

 CQ Q 1 is:

Q 1 : path(X,Y) :- arc(X,Z) & arc(Z,W) & arc(W,Y)

 Q 2 is the value of path in the following recursive Datalog program: r 1 : path(X,Y) :- arc(X,Y) r 2 : path(X,Y) :- path(X,Z) & path(Z,Y)

 Intuitively, Q 1 = paths of length 3; Q 2 = paths of length 1 or more.

 Freeze Q 1 , say with 0, 1, 2, 3 as constants for X , Z , W , Y , resp ectively. D = far c(0; 1); ar c(1; 2); ar c(2; 3)g