1 Datalog Logical Rules Recursion SQL-99 Recursion.

Slides:



Advertisements
Similar presentations
1 Datalog: Logic Instead of Algebra. 2 Datalog: Logic instead of Algebra Each relational-algebra operator can be mimicked by one or several Database Logic.
Advertisements

Relational Calculus and Datalog
CSE 636 Data Integration Conjunctive Queries Containment Mappings / Canonical Databases Slides by Jeffrey D. Ullman.
1 Extended Conjunctive Queries Unions Arithmetic Negation.
Lecture 11: Datalog Tuesday, February 6, Outline Datalog syntax Examples Semantics: –Minimal model –Least fixpoint –They are equivalent Naive evaluation.
1 541: Relational Calculus. 2 Relational Calculus  Comes in two flavours: Tuple relational calculus (TRC) and Domain relational calculus (DRC).  Calculus.
Answer Set Programming Overview Dr. Rogelio Dávila Pérez Profesor-Investigador División de Posgrado Universidad Autónoma de Guadalajara
1 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke Deductive Databases Chapter 25.
1 Database Systems Relations as Bags Grouping and Aggregation Database Modification.
1 Introduction to SQL Multirelation Queries Subqueries Slides are reused by the approval of Jeffrey Ullman’s.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Winter 2002Arthur Keller – CS 18015–1 Schedule Today: Feb. 28 (TH) u Datalog and SQL Recursion, ODL. u Read Sections , Project Part 6.
Winter 2002Arthur Keller – CS 18014–1 Schedule Today: Feb. 26 (T) u Datalog. u Read Sections Assignment 6 due. Feb. 28 (TH) u Datalog and SQL.
CSE 636 Data Integration Datalog Rules / Programs / Negation Slides by Jeffrey D. Ullman.
Winter 2002Arthur Keller – CS 1809–1 Schedule Today: Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers,
1 Datalog Logical Rules Recursion SQL-99 Recursion.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
1 Decidability Turing Machines Coded as Binary Strings Diagonalizing over Turing Machines Problems as Languages Undecidable Problems.
1 Datalog Rules Programs Negation. 2 Review of Logical If-Then Rules h(X,…) :- a(Y,…) & b(Z,…) & … head body subgoals “The head is true if all the subgoals.
1 Semantics of Datalog With Negation Local Stratification Stable Models Well-Founded Models.
Normal forms for Context-Free Grammars
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
Embedded SQL Direct SQL is rarely used: usually, SQL is embedded in some application code. We need some method to reference SQL statements. But: there.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Credit: Slides are an adaptation of slides from Jeffrey D. Ullman 1.
Logical Rules Recursion
1 More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
1 Datalog Logical Rules Recursion. 2 Logic As a Query Language uIf-then logical rules have been used in many systems. wMost important today: EII (Enterprise.
Credit: Slides are an adaptation of slides from Jeffrey D. Ullman 1.
Deductive Databases Chapter 25
Rutgers University Relational Calculus 198:541 Rutgers University.
Database Systems Logical Query Languages assoc. prof., dr. Vladimir Dimitrov web: is.fmi.uni-sofia.bg.
Logical Query Languages Motivation: 1.Logical rules extend more naturally to recursive queries than does relational algebra. u Used in SQL recursion. 2.Logical.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
Databases 1 8th lecture. Topics of the lecture Multivalued Dependencies Fourth Normal Form Datalog 2.
Recursive query plans for Data Integration Oliver Michael By Rajesh Kanisetti.
The Relational Model: Relational Calculus
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
Logical Query Languages Motivation: 1.Logical rules extend more naturally to recursive queries than does relational algebra. u Used in SQL recursion. 2.Logical.
Advanced Topics in Propositional Logic Chapter 17 Language, Proof and Logic.
Datalog Inspired by the impedance mismatch in relational databases. Main expressive advantage: recursive queries. More convenient for analysis: papers.
CSE 636 Data Integration Conjunctive Queries Containment Mappings / Canonical Databases Slides by Jeffrey D. Ullman Fall 2006.
1 Relational Algebra and Calculas Chapter 4, Part A.
Datalog –Another query language –cleaner – closer to a “logic” notation, prolog – more convenient for analysis – can express queries that are not expressible.
Chapter 5 Notes. P. 189: Sets, Bags, and Lists To understand the distinction between sets, bags, and lists, remember that a set has unordered elements,
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
NP-completeness Section 7.4 Giorgi Japaridze Theory of Computability.
Introduction to Finite Automata
Lu Chaojun, SJTU 1 Extended Relational Algebra. Bag Semantics A relation (in SQL, at least) is really a bag (or multiset). –It may contain the same tuple.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
1 Reasoning with Infinite stable models Piero A. Bonatti presented by Axel Polleres (IJCAI 2001,
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
Security and User Authorization in SQL. Lu Chaojun, SJTU 2 Security Two aspects: –Users only see the data they’re supposed to; –Guard against malicious.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4, Part B.
CS589 Principles of DB Systems Fall 2008 Lecture 4d: Recursive Datalog with Negation – What is the query answer defined to be? Lois Delcambre
Extensions of Datalog Wednesday, February 13, 2001.
1 Datalog with negation Adapted from slides by Jeff Ullman.
CS589 Principles of DB Systems Spring 2014 Unit 2: Recursive Query Processing Lecture 2-1 – Naïve algorithm for recursive queries Lois Delcambre (slides.
Slides are reused by the approval of Jeffrey Ullman’s
Datalog Rules / Programs / Negation Slides by Jeffrey D. Ullman
CPSC-310 Database Systems
CPSC-608 Database Systems
Semantics of Datalog With Negation
Cse 344 January 29th – Datalog.
Logic Based Query Languages
Datalog Inspired by the impedance mismatch in relational databases.
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
Rules Programs Negation
Presentation transcript:

1 Datalog Logical Rules Recursion SQL-99 Recursion

2 Logic As a Query Language uIf-then logical rules have been used in many systems. wMost important today: EII (Enterprise Information Integration). uNonrecursive rules are equivalent to the core relational algebra. uRecursive rules extend relational algebra --- have been used to add recursion to SQL-99.

3 A Logical Rule uOur first example of a rule uses the relations: wFrequents(customer,rest), wLikes(customer,soda), and wSells(rest,soda,price). uThe rule is a query asking for “happy” customers --- those that frequent a rest that serves a soda that they like.

4 Anatomy of a Rule Happy(c) <- Frequents(c,rest) AND Likes(c,soda) AND Sells(rest,soda,p)

5 Anatomy of a Rule Happy(c) <- Frequents(c,rest) AND Likes(c,soda) AND Sells(rest,soda,p) Body = “antecedent” = AND of sub-goals. Head = “consequent,” a single sub-goal Read this symbol “if”

6 sub-goals Are Atoms uAn atom is a predicate, or relation name with variables or constants as arguments. uThe head is an atom; the body is the AND of one or more atoms. uConvention: Predicates begin with a capital, variables begin with lower-case.

7 Example: Atom Sells(rest, soda, p)

8 Example: Atom Sells(rest, soda, p) The predicate = name of a relation Arguments are variables

9 Interpreting Rules uA variable appearing in the head is called distinguished ; wotherwise it is nondistinguished.

10 Interpreting Rules uRule meaning: wThe head is true of the distinguished variables wif there exist values of the nondistinguished variables wthat make all sub-goals of the body true.

11 Example: Interpretation Happy(c) <- Frequents(c,rest) AND Likes(c,soda) AND Sells(rest,soda,p) Interpretation: customer d is happy if there exist a rest, a soda, and a price p such that c frequents the rest, likes the soda, and the rest sells the soda at price p.

12 Example: Interpretation Happy(c) <- Frequents(c,rest) AND Likes(c,soda) AND Sells(rest,soda,p) Distinguished variable Nondistinguished variables Interpretation: customer d is happy if there exist a rest, a soda, and a price p such that c frequents the rest, likes the soda, and the rest sells the soda at price p.

13 Arithmetic sub-goals uIn addition to relations as predicates, a predicate for a sub-goal of the body can be an arithmetic comparison. wWe write such sub-goals in the usual way, e.g.: x < y.

14 Example: Arithmetic uA soda is “cheap” if there are at least two rests that sell it for under $1. uFigure out a rule that would determine whether a soda is cheap or not.

15 Example: Arithmetic Cheap(soda) <- Sells(rest1,soda,p1) AND Sells(rest2,soda,p2) AND p1 < 1.00 AND p2 < 1.00 AND rest1 <> rest2

16 Negated sub-goals uWe may put “NOT” in front of a sub- goal, to negate its meaning.

17 Negated sub-goals uExample: wThink of Arc(a,b) as arcs in a graph. wS(x,y) says the graph is not transitive from x to y ; i.e., there is a path of length 2 from x to y, but no arc from x to y. S(x,y) <- Arc(x,z) AND Arc(z,y) AND NOT Arc(x,y)

18 Algorithms for Applying Rules uTwo approaches: 1.Variable-based : Consider all possible assignments to the variables of the body. If the assignment makes the body true, add that tuple for the head to the result. 2.Tuple-based : Consider all assignments of tuples from the non-negated, relational sub-goals. If the body becomes true, add the head’s tuple to the result.

19 Example: Variable-Based S(x,y) <- Arc(x,z) AND Arc(z,y) AND NOT Arc(x,y) uArc(1,2) and Arc(2,3) are the only tuples in the Arc relation. uOnly assignments to make the first sub-goal Arc(x,z) true are: 1.x = 1; z = 2 2.x = 2; z = 3

20 Example: Variable-Based; x=1, z=2 S(x,y) <- Arc(x,z) AND Arc(z,y) AND NOT Arc(x,y)

21 Example: Variable-Based; x=1, z=2 S(x,y) <- Arc(x,z) AND Arc(z,y) AND NOT Arc(x,y) is the only value of y that makes all three sub-goals true. Makes S(1,3) a tuple of the answer

22 Example: Variable-Based; x=2, z=3 S(x,y) <- Arc(x,z) AND Arc(z,y) AND NOT Arc(x,y)

23 Example: Variable-Based; x=2, z=3 S(x,y) <- Arc(x,z) AND Arc(z,y) AND NOT Arc(x,y) No value of y makes Arc(3,y) true. Thus, no contribution to the head tuples; S = {(1,3)}

24 Tuple-Based Assignment uStart with the non-negated, relational sub- goals only. uConsider all assignments of tuples to these sub-goals. wChoose tuples only from the corresponding relations. uIf the assigned tuples give a consistent value to all variables and make the other sub-goals true, add the head tuple to the result.

25 Example: Tuple-Based S(x,y) <- Arc(x,z) AND Arc(z,y) AND NOT Arc(x,y) Only possible values Arc(1,2), Arc(2,3) uFour possible assignments to first two sub- goals: Arc(x,z)Arc(z,y) (1,2) (1,2) (2,3) (2,3) (1,2) (2,3)

26 Example: Tuple-Based S(x,y) <- Arc(x,z) AND Arc(z,y) AND NOT Arc(x,y) Only possible values Arc(1,2), Arc(2,3) uFour possible assignments to first two sub- goals: Arc(x,z)Arc(z,y) (1,2) (1,2) (2,3) (2,3) (1,2) (2,3) Only assignment with consistent z-value. Since it also makes NOT Arc(x,y) true, add S(1,3) to result. These two rows are invalid since z can’t be (3 and 1) or (3 and 2) simultaneously.

27 Datalog Programs uA Datalog program is a collection of rules. uIn a program, predicates can be either 1.EDB = Extensional Database w= stored table. 2.IDB = Intensional Database w= relation defined by rules. wNever both! No EDB in heads.

28 Evaluating Datalog Programs uAs long as there is no recursion, wwe can pick an order to evaluate the IDB predicates, wso that all the predicates in the body of its rules have already been evaluated. uIf an IDB predicate has more than one rule, weach rule contributes tuples to its relation.

29 Example: Datalog Program uUsing following EDB find all the manufacturers of sodas Joe doesn’t sell: wSells(rest, soda, price) and wsodas(name, manf). JoeSells(s) <- Sells(’Joe’’s rest’, s, p) Answer(m) <- Sodas(s,m) AND NOT JoeSells(s)

30 Expressive Power of Datalog uWithout recursion, wDatalog can express all and only the queries of core relational algebra. wThe same as SQL select-from-where, without aggregation and grouping.

31 Expressive Power of Datalog uBut with recursion, wDatalog can express more than these languages. wYet still not Turing-complete.

32 Recursive Example: Generalized Cousins uEDB: Parent(c,p) = p is a parent of c. uGeneralized cousins: people with common ancestors one or more generations back. uNote: We are all cousins according to this definition.

33 Recursive Example Sibling(x,y) <- Parent(x,p) AND Parent(y,p) AND x<>y Cousin(x,y) <- Sibling(x,y) Cousin(x,y) <- Parent(x,xParent) AND Parent(y,yParent) AND Cousin(xParent,yParent)

34 Definition of Recursion uForm a dependency graph whose nodes = IDB predicates. uArc X ->Y if and only if wthere is a rule with X in the head and Y in the body. uCycle = recursion; uNo cycle = no recursion.

35 Example: Dependency Graphs Cousin Sibling Answer JoeSells Recursive Non-recursive

36 Evaluating Recursive Rules uThe following works when there is no negation: 1.Start by assuming all IDB relations are empty. 2.Repeatedly evaluate the rules using the EDB and the previous IDB, to get a new IDB. 3.End when no change to IDB.

37 The “Naïve” Evaluation Algorithm Start: IDB = 0 Apply rules to IDB, EDB Change to IDB? no yes done

38 Example: Evaluation of Cousin uRemember the rules: Sibling(x,y) <- Parent(x,p) AND Parent(y,p) AND x<>y Cousin(x,y) <- Sibling(x,y) Cousin(x,y) <- Parent(x,xParent) AND Parent(y,yParent) AND Cousin(xParent,yParent)

39 Semi-naive Evaluation uSince the EDB never changes, won each round we only get new IDB tuples if we use at least one IDB tuple that was obtained on the previous round. uSaves work; lets us avoid rediscovering most known facts. wA fact could still be derived in a second way.

40 Example: Evaluation of Cousin uWe’ll proceed in rounds to infer wSibling facts (red) wand Cousin facts (green).

41 Parent Data: Parent Above Child adbcefghjkiadbcefghjki The parent data, and edge goes downward from a parent to child. Exercises: 1. List some of the parent-child relationships. 2. What is contained in the Sibling and Cousin data?

42 Parent Data: Parent Above Child adbcefghjkiadbcefghjki Exercise: 1. What do you expect after first round?

43 Round 1 adbcefghjkiadbcefghjki Sibling and Cousin are presumed empty. Cousin remains empty since it depends on Sibling and Sibling is empty. Exercise: What do you expect in the next round?

44 Round 2 adbcefghjkiadbcefghjki Sibling facts remain unchanged because Sibling is not recursive. First execution of the Cousin rule “duplicates” the Sibling facts as Cousin facts (shown in green). Exercise: What do you expect in the next round?

45 Round 3 adbcefghjkiadbcefghjki The execution of the non-recursive Cousin rule gives us nothing However, the recursive call gives us several pairs (shown in bolder green). Exercise: What do you expect in the next round?

46 Round 4 adbcefghjkiadbcefghjki The execution of the non-recursive Cousin rule still gives us nothing However, the recursive call gives us several pairs (shown in even bolder green). Exercise: What do you expect in the next round?

47 Done! adbcefghjkiadbcefghjki Now we are done!

48 Recursion Plus Negation u“Naïve” and “Semi-Naïve” evaluation doesn’t work when there are negated sub-goals. wDiscovering IDB tuples on one route can decrease the IDB tuples on the next route. wLosing IDB tuples on one route can yield more tuples on the next route.

49 Recursion Plus Negation uIn fact, negation wrapped in a recursion makes no sense in general. uEven when recursion and negation are separate, we can have ambiguity about the correct IDB relations.

50 Problematic Recursive Negation P(x) <- Q(x) AND NOT P(x) EDB: Q(1), Q(2) Initial:P = { } Round 1:P = {(1), (2)} // From Q(1) & Q(2) Round 2:P = { } // From NOT(P(1)) & NOT(P(2)) Round 3:P = {(1), (2)} // From Q(1) & Q(2) Round n: etc., etc. …

51 Stratified Negation uStratification is a constraint usually placed on Datalog with recursion and negation. wIt rules out negation wrapped inside recursion. wGives the sensible IDB relations when negation and recursion are separate.

52 Why Stratified Negation? uUsually require that Negation be stratified to prevent the problem just described. Stratification does two things: wLets us evaluate the IDB predicates in a way that it converges. wLets us discover the “correct” solution in face of “many solutions.”

53 Safe Rules uA rule is safe if: 1.Each distinguished variable, 2.Each variable in a negated sub-goal, 3.Each variable in an arithmetic sub-goal, also appears in * a non-negated, relational sub-goal. uWe allow only safe rules.

54 Example: Unsafe Rules uEach of the following is unsafe and not allowed: 1.S(x) <- R(y) wBecause x appears as distinguished variable (S(x)) but does not appear in a non-negated sub-goal. 2.S(x) <- R(y) AND NOT R(x) wBecause x appears in negated sub-goal (R(x)) but does not appear in a sub-goal. 3.S(x) <- R(y) AND x < y wBecause x appears in an arithmetic sub-goal (R(x)) but does not appear in a non-negated sub-goal.

55 Example: Unsafe Rules uIn each case, an infinite number of values for x can satisfy the rule, even if R is a finite relation.

56 Strata uStratum: wLet us separate good negative recursive negation from bad. uIntuitively, the stratum of an IDB predicate P is: wthe maximum number of negations that can be applied to an IDB predicate used in evaluating P.

57 Strata uStratified negation = “finite strata.” uNotice in P(x) <- Q(x) AND NOT P(x), wwe can negate P an infinite number of times deriving P(x).

58 Stratum Graph uTo formalize strata use the stratum graph : wNodes = IDB predicates. wArc A ->B if predicate A depends on B. wLabel this arc “–” if the B sub-goal is negated.

59 Stratified Negation Definition uThe stratum of a node (predicate) is: wthe maximum number of “–” arcs on a path leading from that node. uA Datalog program is stratified wif all its IDB predicates have finite strata.

60 Example P(x) <- Q(x) AND NOT P(x) Infinite path due to loop: not stratified! P Q _

61 Another Example uSetting is graph: Nodes designated as source and target. uEDB consists of: wSource in Source(x) wTarget in Target(x) wArcs between nodes in Arc(x,y) uOur problem is to find target nodes that are not reached from any source.

62 Another Example uRules: “targets not reached from any source”: Reach(x) <- Source(x) Reach(x) <- Reach(y) AND Arc(y,x) NoReach(x) <- Target(x) AND NOT Reach(x) uFirst 2 rules recursively define Reach: wA node can be reached if it is a source or can be reached from a node connected to source. wNoReach if it is a target that cannot be reached.

63 The Stratum Graph NoReach Reach _ Stratum 0: No “–” arcs on any path out. Stratum 1: <= 1 “ – ” arc on any path out. NoReach Depends on Reach (negatively). Reach depends on itself (but not negatively). Since all strata are finite, this is an example of stratified negation.

64 Models uTo discuss possible results wConcept imported from Logic to Datalog wDiscussion is limited to Datalog application. uA model is a choice of IDB relations that, with the given EDB relations makes wall rules true regardless of what values are substituted for the variables.

65 Models uRemember: a rule is true whenever its body is false. wIf moon were made of blue cheese, you will all flunk. uHowever, if the body is true, then the head must be true as well. wIf professor is human, you will get fair grades.

66 Minimal Models uA model should be minimal that if should not properly contain any other model uIntuitively, we don’t want to assert facts that do not have to be asserted

67 Minimal Models uWhen there is no negation, a Datalog program has a unique minimal model wOne given by naïve and semi-naïve evaluation uWith negation and recursion, there can be several minimal models weven if the program is stratified. uFortunately, we can compute the minimal model that makes sense wAnd that is the stratified model

68 The Stratified Model uWhen the Datalog program is stratified: wWe evaluate IDB predicates in stratum 0 There can be several predicates in stratum but they can’t depend negatively on themselves on any other IDB predicate strata. wOnce evaluated, treat it as EDB for next strata. wProceed iteratively until all IDB predicates are evaluated

69 Example: Multiple Models Reach(x) <- Source(x) Reach(x) <- Reach(y) AND Arc(y,x) NoReach(x) <- Target(x) AND NOT Reach(x) 1234 Source Target Target Arc 1 is the only source; 2 and 3 are targets; 4 is an additional node. Reach is the only predicate at Stratum 0. Computation yeilds Reach(1) and Reach(2) Reach is fixed at 1 and 2. Since 1 and 2 can be reached, NoReach has one element in the set: 3.

70 Example: Multiple Models Reach(x) <- Source(x) Reach(x) <- Reach(y) AND Arc(y,x) NoReach(x) <- Target(x) AND NOT Reach(x) 1234 Source Target Target Arc Stratum 0: Reach(1), Reach(2) Stratum 1: NoReach(3) 1 is the only source; 2 and 3 are targets; 4 is an additional node. Reach is the only predicate at Stratum 0. Computation yeilds Reach(1) and Reach(2) Reach is fixed at 1 and 2. Since 1 and 2 can be reached, NoReach has one element in the set: 3.

71 Example: Multiple Models Reach(x) <- Source(x) Reach(x) <- Reach(y) AND Arc(y,x) NoReach(x) <- Target(x) AND NOT Reach(x) 1234 Source Target Target Arc Another model! Reach(1), Reach(2), Reach(3), Reach(4); NoReach is empty. 3 rd rule is always true because head is false.

72 SQL-99 Recursion uExcellent example of Theory -> Practice uDatalog recursion inspired the addition of recursion to the SQL-99 standard. uTrickier, because SQL allows wgrouping-and-aggregation, which behaves like negation and requires a more complex notion of stratification.

73 Example: SQL Recursion uFind Sally’s cousins, using SQL like the recursive Datalog example. uParent(child,parent) is the EDB. WITH Sibling(x,y) AS SELECT p1.child, p2.child FROM Parent p1, Parent p2 WHERE p1.parent = p2.parent AND p1.child <> p2.child; Like Sibling(x,y) <- Parent(x,p) AND Parent(y,p) AND x <> y Important is WITH clause define non-recusive temporary relation Sibling

74 Example: SQL Recursion WITH … RECURSIVE Cousin(x,y) AS (SELECT * FROM Sibling) UNION (SELECT p1.child, p2.child FROM Parent p1, Parent p2, Cousin WHERE p1.parent = Cousin.x AND p2.parent = Cousin.y); Basis Rule: Reflects Cousin(x,y) <- Sibling(x,y) Reflects recursive rule Cousin(x,y) <- Parent(x,xParent) AND Parent(y,yParent) AND Cousin(xParent,yParent )

75 Example: SQL Recursion uWith those definitions, we can add the query, which is about the “temporary view” Cousin(x,y): SELECT y FROM Cousin WHERE x = ‘Sally’;

76 Form of SQL Recursive Queries WITH Rule = [RECURSIVE] ( ) AS

77 Plan to Explain Legal SQL Recursion 1.Define “monotone,” a generalization of “stratified.” 2.Generalize stratum graph to apply to SQL. 3.Define proper SQL recursions in terms of the stratum graph.

78 Monotonicity uIf relation P is a function of relation Q (and perhaps other relations), we say P is monotone in Q if inserting tuples into Q cannot cause any tuple to be deleted from P. uExamples: wP = Q UNION R. wP = SELECT a =10 (Q ).

79 Example: Nonmonotonicity uIf Sells(rest,soda,price) is our usual relation, then the result of the query: SELECT AVG(price) FROM Sells WHERE rest = ’Joe’’s Rest’; is not monotone in Sells. uInserting a Joe’s-Rest tuple into Sells usually changes the average price and thus deletes the old average price.

80 SQL Stratum Graph uNodes = 1.IDB relations declared in WITH clause. 2.Subqueries in the body of the “rules.” wIncludes subqueries at any level of nesting.

81 SQL Stratum Graph uArcs P ->Q : 1.P is a rule head and Q is a relation in the FROM list (not of a subquery). 2.P is a rule head and Q is an immediate subquery of that rule. 3.P is a subquery, and Q is a relation in its FROM or an immediate subquery (like 1 and 2). wPut “–” on an arc if P is not monotone in Q. wStratified SQL = finite #’s of –’s on paths.

82 Example: Stratum Graph uIn our Cousin example, the structure of the rules was: Sib = … Cousin = ( … FROM Sib ) UNION ( … FROM Cousin … ) Subquery S1 Subquery S2

83 The Graph Sib S2S1 Cousin No “–” at all, so surely stratified.

84 Nonmonotone Example uChange the UNION in the Cousin example to EXCEPT: Sib = … Cousin = ( … FROM Sib ) EXCEPT ( … FROM Cousin … ) Subquery S1 Subquery S2 Inserting a tuple into S2 Can delete a tuple from Cousin

85 The Graph Sib S2S1 Cousin -- An infinite number of –’s exist on cycles involving Cousin and S2.

86 NOT Doesn’t Mean Nonmonotone uNot every NOT means the query is nonmonotone. wWe need to consider each case separately. uExample: Negating a condition in a WHERE clause just changes the selection condition. wBut all selections are monotone.

87 Example: Revised Cousin RECURSIVE Cousin AS (SELECT * FROM Sib) UNION (SELECT p1.child, p2.child FROM Par p1, Par p2, Cousin WHERE p1.parent = Cousin.x AND NOT (p2.parent = Cousin.y) ); Revised subquery S2 The only difference

88 S2 Still Monotone in Cousin uIntuitively, adding a tuple to Cousin cannot delete from S2. uAll former tuples in Cousin can still work with Par tuples to form S2 tuples. uIn addition, the new Cousin tuple might even join with Par tuples to add to S2.