Negation-Conjuctive Queries-Lecture Notes, Study notes of Conjunctive Queries

This lecture handout was provided by Prof. Megha Kiran at Ambedkar University, Delhi for Conjuctive Queries course. Its main points are: CQ, Negation, Levy, Sagiv, Test, Canonical, Arithmetic, Database, Klug, Comparisons

Typology: Study notes

2011/2012

Uploaded on 07/15/2012

sadhwani
sadhwani 🇮🇳

4.5

(4)

27 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CQ's With Negation
General form of conjunctive query with negation
(CQN):
H
:-
G
1
& ... &
G
n
&
NOT
F
1
& ... & NOT
F
m
G
's are
positive
subgoals;
F
's are
negative
subgoals.
Apply CQN
Q
to DB
D
by considering all
possible substitutions of constants for the
variables of
Q
. If for some substitution:
1.
All
the positive subgoals b ecome facts in
D
and
2.
None
of the negative subgoals do,
then infer the substituted head.
Set of inferred facts is
Q
(
D
).
Containment of CQ's doesn't change:
Q
1
Q
2
i for every database
D
,
Q
1
(
D
)
Q
2
(
D
).
Example
C
1
: p(X,Z) :- a(X,Y) & a(Y,Z) &
NOT a(X,Z)
C
2
: p(A,C) :- a(A,B) & a(B,C) &
NOT a(A,D)
Intuitively,
C
1
looks for paths of length 2 that
are not \short-circuited" by a single arc from
beginning to end.
C
2
looks for paths of length 2 that start from
anode
A
that is not a \universal source"; i.e.,
there is at least one node
D
not reachable
from
A
by an arc.
Wethus expect
C
1
C
2
, but not vice-versa.
Levy-Sagiv Test
There is a straightforward, time-consuming test for
Q
1
Q
2
:
Create a large-but-nite family of canonical
DB's that consist of all DB's using only the
constants 1
;
2
;:::;n
, where
n
is the number of
variables in
Q
1
.
1
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Negation-Conjuctive Queries-Lecture Notes and more Study notes Conjunctive Queries in PDF only on Docsity!

CQ's With Negation

General form of conjunctive query with negation (CQN):

H :- G 1 & ... & Gn & NOT F 1 & ... & NOT Fm

 G's are positive subgoals; F 's are negative subgoals.

 Apply CQN Q to DB D by considering all p ossible substitutions of constants for the variables of Q. If for some substitution:

  1. Al l the p ositive subgoals b ecome facts in D and
  2. None of the negative subgoals do,

then infer the substituted head.

 Set of inferred facts is Q(D ).

 Containment of CQ's do esn't change: Q 1  Q 2 i for every database D , Q 1 (D )  Q 2 (D ).

Example

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

 Intuitively, C 1 lo oks for paths of length 2 that are not \short-circuited" by a single arc from b eginning to end.

 C 2 lo oks for paths of length 2 that start from a no de A that is not a \universal source"; i.e., there is at least one no de D not reachable from A by an arc.

 We thus exp ect C 1  C 2 , but not vice-versa.

Levy-Sagiv Test

There is a straightforward, time-consuming test for Q 1  Q 2 :

 Create a large-but- nite family of canonical DB's that consist of all DB's using only the constants 1 ; 2 ; : : : ; n, where n is the numb er of variables in Q 1.

 Test each canonical DB. If Q 1 (D ) is not contained in Q 2 (D ) for even one canonical DB D , then containment of CQ's surely do esn't hold. Otherwise, we claim that Q 1  Q 2.

Pro of of L/S Test

 Supp ose Q 1 (D )  Q 2 (D ) for each canonical DB D , but there is some other DB E , for which containment do esn't hold. That is, Q 1 (E ) contains a tuple t that Q 2 (E ) do es not contain.

 Consider the at most n symb ols that variables of Q 1 map to when showing that Q 1 (E ) contains t. We may rename these symb ols 1 ; 2 ; : : : ; n; the counterexample still holds.

 Let D b e the canonical DB consisting of E restricted to the tuples having only the symb ols 1 ; 2 ; : : : ; n.

 Since the L/S test passed, we know that Q 2 (D ) contains t.

 Since the assignment of Q 2 's variables that shows t is in Q 2 (D ) maps variables only to 1 ; 2 ; : : : ; n (rememb er all CQ's are assumed safe), the same assignment maps the p ositive subgoals of Q 2 to tuples of E and negative subgoals of Q 2 to tuples not in E.

F In pro of: note that D and E , after renaming of symb ols, agree on all tuples that involve only 1 ; 2 ; : : : ; n. That is, D and E \lo ok the same" whenever we assign variables to only 1 ; 2 ; : : : ; n.

CQ's With Arithmetic

Supp ose we allow subgoals with <, 6 =, and other comparison op erators.

 We must assume database constants can b e compared.

 Technique is a generalization of the L/S algorithm, but it is due to Tony Klug.

 We shall work the case where < is a total order; other assumptions lead to other algorithms, and we shall later give an all- purp ose technique using a di erent approach.

 However, no assignment of values to A, B , and C makes all three subgoals of C 2 true, when D is the database.

 Thus, p(0; 0) is not in C 2 (D ), and D is a counterexample to C 1  C 2.

Key Theorems No Longer Hold When Some Predicates are Interpreted (e.g., Arithmetic Comparisons)

 Union of CQ's theorem is false.

Example

Consider something we've seen b efore:

Q 1 : p(X) :- a(X) & 10 X & X 20 R 1 : p(X) :- a(X) & 5 X & X 15 R 2 : p(X) :- a(X) & 15 X & X 25

Q 1  R 1 [ R 2 , but neither Q 1  R 1 nor Q 1  R 2 is true.

 Containment mapping theorem is false.

Example

Q 1 : panic :- r(U,V) & r(V,U) Q 2 : panic :- r(U,V) & UV

 Note, \panic" is a 0-ary predicate, i.e., a prop ositional variable.

F 0-ary predicates in the head present no problems for CQ's but don't make anything easier either.

 Informally: Q 1 = \cycle of length 2"; Q 2 = \nondecreasing arc."

 Thus, Q 1  Q 2.

F That is, whenever there is a pair of arcs U! V and V! U , surely one is nondecreasing.

 However, if  is a containment mapping from Q 2 to Q 1 , there is no subgoal that (U  V ) can b e.

 Hence, no containment mapping from Q 2 to Q 1.

Generalizing the Containment-Mapping Theorem

 The Klug/Levy/Sagiv approach uses canonical databases to handle arithmetic.

 Another approach, due to Ashish Gupta and Zhang/Ozsoyoglu, uses containment mappings.

F It has the advantage of working for any kind of interpreted (\built-in") predicate, although we shall use arithmetic comparisons in our examples.

The G/Z/O Test

To test whether Q 1  Q 2 , where Q 1 , Q 2 are CQ's with interpreted predicates:

  1. Recti cation : replace variables and constants by new variables so that no variable app ears twice among the relational subgoals and the head. Also, no constant may app ear there at all.
  2. Add equality comparisons so the new variables are equated to the variable or constant they replace.

Examples

a) Q 1 ab ove:

panic :- r(U,V) & r(V,U)

b ecomes

panic :- r(U,V) & r(X,Y) & U=Y & V=X

b)

p(X) :- q(X,Y,X) & r(Y,a)

would b ecome:

p(Z) :- q(X,Y,W) & r(V,U) & X=W & X=Z & Y=V & U=a

G/Z/O Test (Continued)

  1. Having mo di ed the CQ's, let M b e the set of all containment mappings from the relational subgoals of Q 2 to the relational subgoals of Q 1.

F Note that with all variables app earing only once, every mapping from subgoals to subgoals that matches predicates gives us a containment mapping.

two p ossible orders are:

U < V < X < Y X < U = V < Y

 For this implication, the only orders that make the hyp othesis (U = Y ^ V = X ) true are: U = V = X = Y U = Y < V = X V = X < U = Y

 Conclusion U  V _ X  Y holds for each of the three orders.

 Test is exp onential but works.

Extensions

 Extends to test for a CQ contained in a union of CQ's. The logical implication includes the OR over all containment mappings from any of the CQ's in the union.

 Extends to containment of unions of CQ's: handle each CQ in the contained unions separately.