Logical Query Languages Datalog Datalog Example Datalog Rules Interpreting Datalog Rules Safe Datalog Rules a Rule Is Safe If Ea

Logical Query Languages Datalog Datalog Example Datalog Rules Interpreting Datalog Rules Safe Datalog Rules a Rule Is Safe If Ea

Datalog ● Logical query language for the relational model ● Consists of “if-then” rules made up of atoms: Logical Query Languages - relational : predicates corresponding to relations >EDB extensional database (stored relations) >IDB intensional database (relations defined by rules) - arithmetic 1 2 Datalog example Datalog Rules Rule: head ← body Example: database schema: Head: a relational atom (no EDB predicates!) Movie(title, year, length, inColor, studionName, producerC#) Contracts(starName, studioName, title, year, salary) Body: one or more atoms called subgoals Example: relational atom: Movie (t, y, l, c, s, p) datalog rule: LongMovie(t, y) ← Movie(t,y,l,c,s,p) AND l >=10 Relational Algebra.... arithmetic atom: l > 100 Relational Calculus... 3 4 Interpreting Datalog Rules Safe Datalog Rules Variables: - distinguished – appear in the head A rule is safe if each distinguished and - nondistinguished –appear in the body nondistinguished variable appears in at least one nonnegated relational atom. Interpreting rules the head is true of the distinguished variables if there exist values of the non-distinguished variables that Note: only safe rules are allowed make all subgoals of the body true. 5 6 1 Unsafe Datalog Rules Algorithms for Evaluating Datalog Rules Example: Variable-based: Consider all possible assignments to the variable of the body. If the assignment makes E(w) ← NOT Movies(t, y, l, c, s, p) the body true, add the tuple for the head to the result. Years(w) ← Movies(t, y, l, c, s, p) AND w < y Tuple-based: Consider all assignments of tuples from the nonnegated relational subgoals. If the Note: in each case an infinity of w’s can satisfy the assignment makes the body true, add the tuple rule, even though Movies is a finite relation. for the head to the result. 7 8 Variable-based Evaluation Tuple-based Evaluation from to from to Example: 1 2 Example: 1 2 Database: Edge(from, to) 2 3 Database: Edge(from, to) 2 3 Datalog rule: NotTranzitive(x,z) ← Edge(x, y) AND Edge(y, z) AND NOT Edge(x, z) Datalog rule: NotTranzitive(x,z) ← Edge(x, y) AND Edge(y, z) AND NOT Edge(x, z) Assignment x = 1, y = 2 , z = 3 Assignment (x, y) = (1, 2) , (y, z) = (2, 3), consistent assignment Edge(1, 2) AND Edge(2, 3) AND NOT Edge(1, 3) is true, make (1, 3) a tuple of Edge(1, 2) AND Edge(2, 3) AND NOT Edge(1, 3) is true, make (1, 3) a tuple of the answer the answer Assignment (x, y) = (1, 2) , (y, z) = (1, 2), inconsistent assignment Assignment x = 1, y = 2 , z = ? Edge(1, 2) AND Edge(2, ?) AND NOT Edge(1, ?) no z makes the body true Assignment (x, y) = (2, 3), (y, z)=(1, 2), inconsistent assignment Assignment x = 2, y = 3 , z = ? Edge(2, 3) AND Edge(3, ?) AND NOT Edge(2, ?) no z makes the body true Assignment (x, y) = (2, 3), (y, z)=(2, 3), inconsistent assignment Note: No other assignment for x and y makes Edge(x, y) true. Stop searching. Note: No other assignment for( x, y) makes Edge(x, y) true. Stop searching. 9 10 Datalog Programs Datalog Programs Evaluation A Datalog Program is a collection of rules Non-recursive programs: - pick an order to evaluate the rules (the IDB Example: predicates) so that all the predicates in the body “Find actors who starred in the color movies made in the 1950” have already been evaluated. MoviesColor50 (t,y)← Movie(t,y,l,c,s,p) AND y = “1950” AND c = “y” Answer(star) ← Movies90(t,y) AND Contracts(star, studio, t, y, salary) - if an IDB predicate has more than one rule, each contributes tuples to its relation (union). 11 12 2 From Relational Algebra to Datalog -1 From Relational Algebra to Datalog -2 Intersection: R(x, y) ∩ T(x, y) Projection: π (R) I(x, y) ÅR(x, y) AND T(x, y) x P(x) ÅR(x,y) Union: R(x, y) U T(x, y) Selection: σ (R) U(x, y) ÅR(x, y) x>10 S(x, y) ÅR(x, y) AND x>10 U(x, y) ÅT(x, y) Product: R X T Differece: R(x, y) –T(x, y) P(x, y, z, w) ÅR(x,y ) AND T(z, w) D(x, y) ÅR(x, y) AND NOT T(x, y) 13 14 From Relational Algebra to Datalog -3 Datalog Queries Natural Join R T Datalog Query: a datalog program. J(x, y, z) Å R(x, y) AND T(y, z) Expressive Power: Theta Join R .R.x >T.yT - without recursion, Datalog has the same power J(x, y, z, w) Å R(x, y) AND T(z, w) AND x > y as Core Relational Algebra and Relational Calculus - with recursion: much more, but not Turing-complete 15 16 Recursivity Recursive Rules Example: FollowOn(x, y) ← SequelOf(x, y) Example: FollowOn(x, y) ← SequelOf(x, z) AND FollowOn(z, y) Database: SequelOf(movie, sequel) Dependency Graph (of a program) Query: “What are the sequels of sequels of movies in the database?” - nodes: the IDB predicates π first,second (ρ first,second (SequelOf ) >< ρsecond ,third (SequelOf )) - edges: from node1(predicate1) to node(predicate2) if and only if there is a rule with predicate1 in the head “What are the sequels of the sequels of the sequels?” and predicate2 in the body. Infinite unions? A datalog program is recursive iff its dependency graph has a cycle. 17 18 3 Evaluating Recursive Rules Dependency graph without Negation Example: FirstSequelOf(x, y) ← SequelOf(x, y) Naive algorithm FollowOn(x, y) ← FirstSequelOf(x, y) FollowOn(x, y) ← FirstSequelOf(x, z) AND FollowOn(z, y) 1. Begin by assuming all IDB relations are empty FollowOn 2. Repeatedly evaluate the rules using the EDB Cyclic graph Æ recursive datalog program and the previous IDB to get a new IDB FirstSequelOf 3. End when there is no change to IDB 19 20 Example: SequelOf Negation in recursive rules Database: SequelOf := {(t1,t2), (t2,t3), (t3,t4),(t5,t6),(t6,t7), (t7,t8)} movie sequel t1 t2 FirstSequelOf(x, y) ← SequelOf(x, y) t2 t3 - Naive evaluation does not work when there are t3 t4 FollowOn(x, y) ← FirstSequelOf(x, y) t5 t6 negated subgoals. FollowOn(x, y) ← FirstSequelOf(x, z) AND FollowOn(z, y) t6 t7 t7 t8 We will proceed in rounds to infer FirstSequel facts and then FollowOn facts. - Arguably negation wrapped in a recursion makes Initial FirstSequelof:={}, FollowOn := {} little or no sense in general Round1 FirstSequelOf := {(t1,t2), (t2,t3), (t3,t4),(t5,t6),(t6,t7), (t7,t8)}, FollowOn ={} Round2 FollowOn := {(t1,t2), (t2,t3), (t3,t4),(t5,t6),(t6,t7), (t7,t8)} Round3 FollowOn := {(t1,t2), (t2,t3), (t3,t4),(t5,t6),(t6,t7), (t7,t8)} U {(t1,t3), (t2,t4), (t5,t7),(t6,t8)} - Even when negation and recursion are separate Round4 FollowOn := {(t1,t2), (t2,t3), (t3,t4),(t5,t6),(t6,t7), (t7,t8), (t1,t3), (t2,t4), (t5,t7),(t6,t8)} U there is ambiguity sbout the “correct” IDB relations {(t1,t4), (t5,t8)} Round 5 no change in FollowOn. STOP 21 22 Example: Example: EDB← predicate R= {(0)} EDB predicate S = {(1)} P(x) Å R(x) AND NOT Q(x) R(x) Å S(x) AND NOT R(x) Q(x) Å R(x) AND NOT P(x) Initial R := {} 2 solutions Round 1 R = {(1)} P={(0)}, Q = Ф Round 2 R = {} P = Ф, Q = {(0)} Round 3 R = {(1)}, etc. Which one to choose? 23 24 4 Stratified Negation Stratum Graph - Constraint imposed on recursive Datalog programs Labeled dependency graph - nodes: the IDB predicates - Rules out negation wrapped in recursion - edges: from node1(predicate1) to node(predicate2) if - The maximum number of negations that can be and only if there is a rule with predicate1 in the head and predicate2 in the body. If predicate2 appears negated, applied to an IDB predicate used in evaluating an label the edge with “-”. IDB predicate must be finite. 25 26 Strata Example: • The stratum of a node (predicate) is the R(x) Å S(x) AND NOT R(x) maximum number of “-” labeled edges on P a path leading from that node . R -- - • A Datalog program is stratified if al its IDB Q predicates have finite strata. P(x) Å R(x) AND NOT Q(x) Q(x) Å R(x) AND NOT P(x) 27 28 Stratified Datalog Evaluation SQL Recursion Algorithm: • Datalog recursion has inspired the introduction of recursion in the SQL-99 1. Evaluate IDB predicates lowest-stratum-first standard. 2. Once evaluated, treat them as “EDB” for the • More difficult: SQL allows grouping and IDB predicates with higher strata. aggregation Æ requires a more complex notion of stratification 29 30 5 SQL Recursive Queries • The keyword WITH Syntax • One or more definitions, separated by comas, of WITH the form: - the optional keyword RECURSIVE <Datalog-like rules> - the name of the relation being defined - the keyword AS <a core SQL query using the predicates in - the query that defines the relation the rules > • A query which may refer to any of the prior definitions, and forms the result of the WITH 31 statement. 32 Example: “Find all Rocky’s sequels” Monotonicity WITH • If a relation P is a function of a relation Q, we FirstSequelOf(x,y) AS SELECT * FROM SequelOf; say P is monotone in Q if inserting tuples into Q RECURSIVE FollowOn(x, y) AS cannot cause any tuples to be deleted from P. (SELECT * FROM FirstSequelOf) UNION Example: (SELECT FirstSequelOf.x, FollowOn.y FROM FirstSequelOf, FollowOn P = Q UNION R WHERE FirstSequelOf.y = FollowOn.x ) P = SELECT * FROM Q SELECT y FROM FollowOn WHERE x=“Rocky” 33 34 Nonmonotonicity SQL Stratum Graph Nodes - IDB relations declared in WITH clause Example: - Subqueries in the body of the rules (at any level of nesting) Let P be the result relation of the query SELECT AVG(x) FROM Q P is not monotone in Q: inserting a new tuple in Q may change the Edges PÆQ if: average and thus delete the old average.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    7 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us