strati ed negation negation wrapp ed inside a recursion
play

Stratied Negation Negation wrapp ed inside a recursion mak - PDF document

Stratied 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


  1. 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. � Strati�cation recen tly adopted in the SQL3 standard for recursiv e SQL. 1

  2. 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 2

  3. 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 3

  4. Example Reach(x) <- Source(x) Reach(x) <- Reach(y) AND Arc(y,x) NoReach(x) <- Target(x) AND NOT Reach(x) NoReach � Reach 4

  5. 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 with recursion and negation is if ev ery IDB predicate has a �nite str ati�e d 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. 5

  6. 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 6

  7. 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. 7

  8. SQL3 Recursion WITH stu� that lo oks lik e Datalog rules an SQL query ab out EDB, IDB � Rule = ( < argumen ts > ) [RECURSIVE] R AS SQL query 8

  9. Example Find Sally's cousins, using EDB Par(child, parent) . WITH Sib(x,y) AS SELECT p1.child, p2,child FROM Par p1, Par p2 WHERE p1.parent = p2.parent AND p1.child <> p2.child, RECURSIVE Cousin(x,y) AS Sib UNION (SELECT p1.child, p2.child FROM Par p1, Par p2, Cousin WHERE p1.parent = Cousin.x AND p2.parent = Cousin.y ) SELECT y FROM Cousin WHERE x = 'Sally'; 9

  10. Plan for Describing Legal SQL3 recursion 1. De�ne \monotonicit y ," a prop ert y that generalizes \strati�cati on." 2. Generalize stratum graph to apply to SQL queries instead of Datalog rules. ✦ (Non)monotonicit y replaces in NOT subgoals. 3. De�ne seman tically correct SQL3 recursions in terms of stratum graph. Monotonicit y If relation is a function of relation (and P Q p erhaps other things), w e sa y is in P monotone if adding tuples to cannot cause an y tuple of Q Q to b e deleted. P 10

  11. Monotonicit y Example In addition to certain negations, an aggregation can cause nonmonotonicit y . Sells(bar , beer , price) SELECT AVG(price) FROM Sells WHERE bar = 'Joe''s Bar'; � Adding to a tuple that giv es a new b eer Sells Jo e sells will usually c hange the a v erage price of b eer at Jo e's. � Th us, the former result, whic h migh t b e a single tuple lik e (2 : 78) b ecomes another single tuple lik e (2 : 81), and the old tuple is lost. 11

  12. Generalizing Stratum Graph to SQL � No de for eac h relation de�ned b y a \rule." � No de for eac h sub query in the \b o dy" of a rule. � Arc ! if P Q a) is \head" of a rule, and is a relation P Q app earing in the list of the rule (not FROM in the list of a sub query). FROM b) is head of a rule, and is a sub query P Q directly used in that rule (not nested within some larger sub query). c) is a sub query , and is a relation or P Q sub query used directly within . P � Lab el the arc � if is monotone in Q . P not � Requiremen t for legal SQL3 recursion: �nite strata only . 12

  13. Example F or the Sib/Cousin example, there are three no des: Sib , Cousin , and (the second term of the S Q union in the rule for Cousin . Sib Cousin S Q � No nonmonotonicit y , hence legal. 13

  14. A Nonmonotonic Example Change the to in the rule for UNION EXCEPT Cousin . RECURSIVE Cousin(x,y) AS Sib EXCEPT (SELECT p1.child, p2.child FROM Par p1, Par p2, Cousin WHERE p1.parent = Cousin.x AND p2.parent = Cousin.y ) � No w, Adding to the result of the sub query can delete facts; i.e., is Cousin Cousin nonmonotone in G . S Sib Cousin � S Q � In�nite n um b er of � 's in cycle, so illegal in SQL3. 14

  15. Another Example: NOT Do esn't Mean Nonmonotone Lea v e as it w as, but negate one of the Cousin conditions in the where-clause. RECURSIVE Cousin(x,y) AS Sib UNION (SELECT p1.child, p2.child FROM Par p1, Par p2, Cousin WHERE p1.parent = Cousin.x AND NOT (p2.parent = Cousin.y) ) � Y ou migh t think that dep ends negativ ely S G on Cousin , but it do esn't. ✦ If I add a new tuple to Cousin , all the old tuples still exist and yield whatev er tuples in they used to yield. S G ✦ In addition, the new tuple migh t Cousin com bine with old p 1 and p 2 tuples to yield something new. 15

Recommend


More recommend