This preview shows page 1. Sign up to view the full content.
Unformatted text preview: CS345 Notes for Lecture 10/14/96
Conjunctive Queries
= safe, datalog rules: H : G1 & & G n Most common form of query; equivalent to
selectprojectjoin queries.
Useful for optimization of active elements
(\triggers," constraints, instantiated views).
Useful for information integration. Containment Q1 Q2 i for every database D, Q1 (D)
Q2 (D).
Remember, Q(D) is what we get by making
all possible substitutions for variables of Q. If
a substitution turns all subgoals of Q's body
to facts in D, then the head of Q, with this
substitution, is in Q(D).
Containment problem for CQ's is central.
Problem is NPcomplete, 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: 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)
B A. In proof, suppose 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
! ! ! 1 ! Thus, the head of A becomes 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 Q2 to variables of
CQ Q1 such that
1. Head of Q2 becomes head of Q1 .
2. Each subgoal of Q2 becomes a subgoal of Q1 .
It is not necessary that every subgoal of Q1 is
the target of some subgoal of Q2.
Example: A, B as above. 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 both W W and
W Z.
! ! ! ! ! ! Example:
C1 :
C2 : p(X) : a(X,Y) & a(Y,Z) & a(Z,W)
p(X) : a(X,Y) & a(Y,X) Containment mapping C1 C2:
X X, Y Y , Z X, W Y
No containment mapping C2 C1. Proof:
a) X X required for head.
b) Thus, rst subgoal of C2 must map to
rst subgoal of C1; Y must map to Y .
c) Similarly, 2nd subgoal of C2 must map
to 2nd subgoal of C1, so X must map to
Z.
d) But we already found X maps to X .
! ! ! ! ! ! ! 2 Containment Mapping Theorem Q1 Q2 i there exists a containment mapping
from Q2 to Q1 . Proof (If) Let : Q2 Q1 be a containment mapping. Let
D be any DB.
Every tuple t in Q1 (D) is produced by some
substitution on the variables of Q1 that
makes Q1 's subgoals all become facts in D.
Claim:
is a substitution for variables of
Q2 that produces t.
1.
(Fi ) = (some Gj ). Therefore, it is
in D.
2.
(H2 ) = (H1 ) = t.
Thus, every t in Q1(D) is also in Q2 (D); i.e.,
Q1 Q 2 .
! Proof (Only If) Key idea: frozen CQ.
1. Create a unique constant for each variable of
the CQ Q.
2. Frozen Q is a database consisting of all the
subgoals of Q, with the chosen constants substituted for variables. Example: p(X) : a(X,Y) & a(Y,Z) & a(Z,W) Let x be the constant for X , etc. The relation
for predicate a consists of the three tuples (x; y),
(y; z), and (z; w).
The proof: Let Q1 Q2 . Let database D be the
frozen Q1 .
Q1(D) contains t, the \frozen" head of Q1
(sounds gruesome, but the reason is that we
can use the substitution in which each variable of Q1 is replaced by its corresponding
constant).
3 Since Q1 Q2 , Q2 (D) must also contain t.
Let be the substitution of constants from
D for the variables of Q2 that makes each
subgoal of Q2 a tuple of D and yields t as the
head.
Let be the substitution that maps each
variable X of Q2 to the variable of Q1 that
corresponds to the constant (X ).
is a containment mapping from Q2 to Q1
because:
a) The head of Q2 is mapped by to t, and
t is the frozen head of Q1 , so maps the
head of Q2 to the \unfrozen" t, that is,
the head of Q1.
b) Each subgoal Fi of Q2 is mapped by to
some tuple of D, which is a frozen version
of some subgoal Gj of Q1 . Then maps
Fi to the unfrozen tuple, that is, to Gj
itself.
0 0
0 0 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 becomes: the subgoal mapping does not cause
a variable to be mapped to two di erent variables or constants, nor cause a constant to be
mapped to a variable or a constant other than
itself.
Example: Again consider A:
B: p(X,Y) : r(X,W) & b(W,Z) & r(Z,Y)
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.
! ! ! 4 ! We could as well describe this mapping as
r(X; W ) r(X; W ), b(W; Z ) b(W; W ),
and r(Z; Y ) r(W; Y ).
! ! ! Method of Canonical Databases Instead of looking for a containment mapping from
Q2 to Q1 in order to test Q1 Q2 , we can apply
the following test:
1. Create a canonical database D that is the
frozen body of Q1 .
2. Compute Q2 (D).
3. If Q2 (D) contains the frozen head of Q1 , then
Q1 Q2; else not.
The proof that this method works is essentially the same as the argument for containment mappings.
The only way the frozen head of Q1 can
be in Q2 (D) is for there to be a containment mapping Q2 Q1 .
! Example:
C1 :
C2 : p(X) : a(X,Y) & a(Y,Z) & a(Z,W)
p(X) : a(X,Y) & a(Y,X) Test C2 C1.
Choose constants X 0, Y 1.
Canonical DB from C1 is
D = a(0; 1); a(1; 0)
C1(D) = p(0); p(1) .
Since the frozen head of C2 is p(0), which is
in C1(D), we conclude C2 C1.
Note that the instantiation of C1 that shows
p(0) is in C1(D) is X 0, Y 1, Z 0,
and W 1.
If we replace 0 and 1 by the variables X
and Y they stand for, we have the containment mapping from C1 to C2.
! f f ! g g ! ! 5 ! ! ...
View
Full
Document
This note was uploaded on 02/14/2012 for the course CSE 6590 taught by Professor Kotakoski during the Winter '12 term at York University.
 Winter '12
 Kotakoski

Click to edit the document details