Logical Query Languages Motiv atio n: 1. Logical rules extend more naturally to recursiv e queries than do es relational algebra. ✦ Used in SQL recursion. 2. Logical rules form the basis for man y information-in tegration systems and applications. 1
Datalog Example Likes(drinker , beer ) Sells(bar , beer , price) Frequents(drinker , bar) Happy(d) <- Frequents(d,bar) AND Likes(d,beer) AND Sells(bar,beer,p ) � Ab o v e is a rule . � Left side = ad . he � Righ t side = = of als . b o dy AND sub go � Head and subgoals are atoms . ✦ A tom = and argumen ts. pr e dic ate ✦ Predicate = relation name or arithmetic predicate, e.g. < . ✦ Argumen ts are v ariables or constan ts. � Subgoals (not head) ma y optionally b e negated b y NOT . 2
Meaning of Rules Head is true of its argumen ts if there exist v alues for v ariables (those in b o dy , not in head) that lo c al mak e all of the subgoals true. � If no negation or arithmetic comparisons, just natural join the subgoals and pro ject on to the head v ariables. Example Ab o v e rule equiv alen t to = Happy(d) � (Frequents . / Likes . / Sells) dr ink er 3
Ev aluation of Rules Tw o, dual, approac hes: 1. : Consider all p ossible V ariable-b ase d assignmen ts of v alues to v ariables. If all subgoals are true, add the head to the result relation. 2. : Consider all assignmen ts of T uple-b ase d tuples to subgoals that mak e eac h subgoal true. If the v ariables are assigned consisten t v alues, add the head to the result. Example: V ariable-Based Assignmen t S(x,y) <- R(x,z) AND R(z,y) AND NOT R(x,y) = R A B 1 2 2 3 4
� Only assignmen ts that mak e �rst subgoal true: 1. ! 1, ! 2. x z 2. ! 2, ! 3. x z � In case (1), ! 3 mak es second subgoal true. y Since (1 ; 3) is in , the third subgoal is not R also true. ✦ Th us, add ( x; ) = (1 ; 3) to relation . y S � In case (2), no v alue of mak es the second y subgoal true. Th us, = S A B 1 3 5
Example: T uple-Based Assignmen t T ric k: start with the p ositiv e (not negated), relational (not arithmetic) subgoals only . S(x,y) <- R(x,z) AND R(z,y) AND NOT R(x,y) = R A B 1 2 2 3 � F our assignmen ts of tuples to subgoals: ( x; ) ( z ) R z R ; y (1 ; 2) (1 ; 2) (1 ; 2) (2 ; 3) (2 ; 3) (1 ; 2) (2 ; 3) (2 ; 3) � Only the second giv es a consisten t v alue to . z � That assignmen t also mak es true. NOT R(x,y) � Th us, (1 ; 3) is the only tuple for the head. 6
Safet y A rule can mak e no sense if v ariables app ear in funn y w a ys. Examples � S(x) <- R(y) � S(x) <- NOT R(x) � S(x) <- R(y) AND x < y In eac h of these cases, the result is in�nite, ev en if the relation is �nite. R � T o mak e sense as a database op eration, w e need to require three things of a v ariable (= x de�nition of safety ). If app ears in either x 1. The head, 2. A negated subgoal, or 3. An arithmetic comparison, then m ust also app ear in a nonnegated, x \ordinary" (relational) subgoal of the b o dy . � W e insist that rules b e safe, henceforth. 7
Datalog Programs � A collect i on of rules is a am . Datalo g pr o gr � Predicates/relati ons divide in to t w o classes: ✦ EDB = extensional datab ase = relation stored in DB. ✦ IDB = = relation intensional datab ase de�ned b y one or more rules. � A predicate m ust b e IDB or EDB, not b oth. ✦ Th us, an IDB predicate can app ear in the b o dy or head of a rule; EDB only in the b o dy . 8
Example Con v ert the follo wing SQL (Find the man ufacturers of the b eers Jo e sells): Beers(name , manf) Sells(bar , beer , price) SELECT manf FROM Beers WHERE name IN( SELECT beer FROM Sells WHERE bar = 'Joe''s Bar' ); to a Datalog program. JoeSells(b) <- Sells('Joe''s Bar', b, p) Answer(m) <- JoeSells(b) AND Beers(b,m) � Note: Beers , = EDB; JoeSells , Sells = IDB. Answer 9
Expressiv e P o w er of Datalog � Nonrecursiv e Datalog = (classical ) relational algebra. ✦ See discussion in text. � Datalog sim ulates SQL select-from-where without aggregation and grouping. � Recursiv e Datalog expresses queries that cannot b e expressed in SQL. � But none of these languages ha v e full expressiv e p o w er ( T ompleteness ). uring c 10
Recursion � IDB predicate on predicate if P dep ends Q there is a rule with in the head and in a P Q subgoal. � Dra w a graph: no des = IDB predicates, arc ! means dep ends on Q . P Q P � Cycles i� recursiv e. Recursiv e Example Sib(x,y) <- Par(x,p) AND Par(y,p) AND x <> y Cousin(x,y) <- Sib(x,y) Cousin(x,y) <- Par(x,xp) AND Par(y,yp) AND Cousin(xp,yp) 11
Iterativ e Fixed-P oin t Ev aluates Recursiv e Rules Start ; IDB = Apply rules to IDB, EDB Change done to IDB? y es no 12
Example EDB = Par a d b c e f g h j k i � Note, b ecause of symmetry , and Sib Cousin facts app ear in pairs, so w e shall men tion only ( x; ) when b oth ( x; ) and ( y x ) are mean t. y y ; 13
Sib Cousin Initial ; ; Round 1 ( b; c ) ; ( c; e ) ; add: ( g h ) ; ( j; ) ; k Round 2 ( b; c ) ; ( c; e ) add: ( g h ) ; ( j; ) ; k Round 3 ( f ) ; ( f h ) ; g ; add: ( g i ) ; ( h; i ) ; ( i; ) k Round 4 ( k ) ; k add: ( i; ) j 14
Strati�ed Negation � Negation wrapp ed inside a recursion mak es no sense. � Ev en when negation and recursion are separated, there can b e am biguit y ab out what the rules mean, and some one meaning m ust b e selected. � is an additional restrain t on Str ati�e d ne gation recursiv e rules (lik e safet y) that solv es b oth problems: 1. It rules out negation wrapp ed in recursion. 2. When negation is separate from recursion, it yields the in tuitiv el y correct meaning of rules (the del ). str ati�e d mo 15
Problem with Recursiv e Negation Consider: P(x) <- Q(x) AND NOT P(x) � f 1 ; 2 g . Q = EDB = � Compute IDB iterativ el y? P ✦ Initial ly , = ; . P ✦ Round 1: = f 1 ; 2 g . P ✦ Round 2: = ; , etc., etc. P 16
Strata In tuitiv ely : stratum of an IDB predicate = maxim um n um b er of negations y ou can pass through on the w a y to an EDB predicate. � 1 Must not b e in \strati�ed" rules. � De�ne aph : str atum gr ✦ No des = IDB predicates. ✦ Arc ! if app ears in the b o dy of a P Q Q rule with head . P ✦ Lab el that arc � if is in a negated Q subgoal. Example P(x) <- Q(x) AND NOT P(x) � P 17
Example Whic h target no des cannot b e reac hed from an y source no de? Reach(x) <- Source(x) Reach(x) <- Reach(y) AND Arc(y,x) NoReach(x) <- Target(x) AND NOT Reach(x) NoReach � Reach 18
Computing Strata of an IDB predicate = maxim um Str atum A � n um b er of arcs on an y path from in the A stratum graph. Examples � F or �rst example, stratum of is 1 . P � F or second example, stratum of is 0; Reach stratum of is 1. NoReach Strati�ed Negation A Datalog program is if ev ery IDB str ati�e d predicate has a �nite stratum. Strati�ed Mo del If a Datalog program is strati�ed, w e can compute the relations for the IDB predicates lo w est- stratum-�rst. 19
Example Reach(x) <- Source(x) Reach(x) <- Reach(y) AND Arc(y,x) NoReach(x) <- Target(x) AND NOT Reach(x) � EDB: ✦ = f 1 g . Source ✦ f (1 ; 3) g . = 2) ; (3 ; 4) ; (4 ; Arc ✦ f 2 ; 3 g . = Target 1 2 3 4 source target target � f 1 ; 2 g First compute = (stratum 0). Reach � Next compute = f 3 g . NoReach 20
Is the Strati�ed Solution \Ob vious"? Not really . � There is another mo del that mak es the rules true no matter what v alues w e substitute for the v ariables. ✦ f 1 ; 4 g . = 2 ; 3 ; Reach ✦ = ; . NoReach � Remem b er: the only w a y to mak e a Datalog rule false is to �nd v alues for the v ariables that mak e the b o dy true and the head false. ✦ F or this mo del, the heads of the rules for are true for all v alues, and Reach in the rule for the subgoal NoReach assures that the b o dy NOT Reach(x) cannot b e true. 21
Recommend
More recommend