Datalog Logical Rules Recursion SQL99 Recursion 1 Logic

  • Slides: 88
Download presentation
Datalog Logical Rules Recursion SQL-99 Recursion 1

Datalog Logical Rules Recursion SQL-99 Recursion 1

Logic As a Query Language u. If-then logical rules have been used in many

Logic As a Query Language u. If-then logical rules have been used in many systems. w Most important today: EII (Enterprise Information Integration). u. Nonrecursive rules are equivalent to the core relational algebra. u. Recursive rules extend relational algebra --- have been used to add recursion to SQL-99. 2

A Logical Rule u. Our first example of a rule uses the relations: w

A Logical Rule u. Our first example of a rule uses the relations: w Frequents(customer, rest), w Likes(customer, soda), and w Sells(rest, soda, price). u. The rule is a query asking for “happy” customers --- those that frequent a rest that serves a soda that they like. 3

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

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

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

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

sub-goals Are Atoms u. An atom is a predicate, or relation name with variables

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

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

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

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

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

Interpreting Rules u. A variable appearing in the head is called distinguished ; w

Interpreting Rules u. A variable appearing in the head is called distinguished ; w otherwise it is nondistinguished. 9

Interpreting Rules u. Rule meaning: w The head is true of the distinguished variables

Interpreting Rules u. Rule meaning: w The head is true of the distinguished variables w if there exist values of the nondistinguished variables w that make all sub-goals of the body true. 10

Example: Interpretation Happy(c) <- Frequents(c, rest) AND Likes(c, soda) AND Sells(rest, soda, p) Interpretation:

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

Example: Interpretation Happy(c) <- Frequents(c, rest) AND Likes(c, soda) AND Sells(rest, soda, p) Distinguished

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

Arithmetic sub-goals u. In addition to relations as predicates, a predicate for a sub-goal

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

Example: Arithmetic u. A soda is “cheap” if there at least two rests that

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

Example: Arithmetic Cheap(soda) <Sells(rest 1, soda, p 1) AND Sells(rest 2, soda, p 2)

Example: Arithmetic Cheap(soda) <Sells(rest 1, soda, p 1) AND Sells(rest 2, soda, p 2) AND p 1 < 1. 00 AND p 2 < 1. 00 AND rest 1 <> rest 2 15

Negated sub-goals u. We may put “NOT” in front of a subgoal, to negate

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

Negated sub-goals u. Example: w Think of Arc(a, b) as arcs in a graph.

Negated sub-goals u. Example: w Think of Arc(a, b) as arcs in a graph. w S(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) 17

Algorithms for Applying Rules u Two approaches: 1. Variable-based : Consider all possible assignments

Algorithms for Applying Rules u Two 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. 18

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

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

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

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

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

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

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

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

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

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

Tuple-Based Assignment u. Start with the non-negated, relational subgoals only. u. Consider all assignments

Tuple-Based Assignment u. Start with the non-negated, relational subgoals only. u. Consider all assignments of tuples to these sub-goals. w Choose tuples only from the corresponding relations. u. If the assigned tuples give a consistent value to all variables and make the other sub-goals true, add the head tuple to the result. 24

Example: Tuple-Based S(x, y) <- Arc(x, z) AND Arc(z, y) AND NOT Arc(x, y)

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) u. Four possible assignments to first two subgoals: Arc(x, z) (1, 2) (2, 3) Arc(z, y) (1, 2) (2, 3) 25

Example: Tuple-Based S(x, y) <- Arc(x, z) AND Arc(z, y) AND NOT Arc(x, y)

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) u. Four possible assignments to first two subgoals: These two rows are invalid since z can’t be (3 and 1) or (3 and 2) simultaneously. Arc(x, z) (1, 2) (2, 3) Arc(z, y) (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. 26

Datalog Programs u A Datalog program is a collection of rules. u In a

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

Evaluating Datalog Programs u. As long as there is no recursion, w we can

Evaluating Datalog Programs u. As long as there is no recursion, w we can pick an order to evaluate the IDB predicates, w so that all the predicates in the body of its rules have already been evaluated. u. If an IDB predicate has more than one rule, w each rule contributes tuples to its relation. 28

Example: Datalog Program u. Using following EDB find all the manufacturers of sodas Joe

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

Expressive Power of Datalog u. Without recursion, w Datalog can express all and only

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

Expressive Power of Datalog u. But with recursion, w Datalog can express more than

Expressive Power of Datalog u. But with recursion, w Datalog can express more than these languages. w Yet still not Turing-complete. 31

Recursive Example: Generalized Cousins u. EDB: Parent(c, p) = p is a parent of

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

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

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, x. Parent) AND Parent(y, y. Parent) AND Cousin(x. Parent, y. Parent) 33

Definition of Recursion u. Form a dependency graph whose nodes = IDB predicates. u.

Definition of Recursion u. Form a dependency graph whose nodes = IDB predicates. u. Arc X ->Y if and only if w there is a rule with X in the head and Y in the body. u. Cycle = recursion; u. No cycle = no recursion. 34

Example: Dependency Graphs Cousin Answer Sibling Joe. Sells Recursive Non-recursive 35

Example: Dependency Graphs Cousin Answer Sibling Joe. Sells Recursive Non-recursive 35

Evaluating Recursive Rules u The following works when there is no negation: 1. Start

Evaluating Recursive Rules u The 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. 36

The “Naïve” Evaluation Algorithm Start: IDB = 0 Apply rules to IDB, EDB yes

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

Example: Evaluation of Cousin u. Remember the rules: Sibling(x, y) <Parent(x, p) AND Parent(y,

Example: Evaluation of Cousin u. Remember 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, x. Parent) AND Parent(y, y. Parent) AND Cousin(x. Parent, y. Parent) 38

Semi-naive Evaluation u. Since the EDB never changes, w on each round we only

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

Example: Evaluation of Cousin u. We’ll proceed in rounds to infer w Sibling facts

Example: Evaluation of Cousin u. We’ll proceed in rounds to infer w Sibling facts (red) w and Cousin facts (green). 40

Parent Data: Parent Above Child The parent data, and edge goes downward from a

Parent Data: Parent Above Child 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? j a d b c f g k e h i 41

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

Parent Data: Parent Above Child Exercise: 1. What do you expect after first round? j a d b c f g k e h i 42

Sibling and Cousin are presumed empty. Round 1 Cousin remains empty since it depends

Sibling and Cousin are presumed empty. Round 1 Cousin remains empty since it depends on Sibling and Sibling is empty. Exercise: What do you expect in the next round? j a d b c f g k e h i 43

Sibling facts remain unchanged because Sibling is not recursive. First execution of the Cousin

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? j Round 2 a d b c f g k e h i 44

The execution of the non-recursive Cousin rule gives us nothing Round 3 However, the

The execution of the non-recursive Cousin rule gives us nothing Round 3 However, the recursive call gives us several pairs (shown in bolder green). Exercise: What do you expect in the next round? j a d b c f g k e h i 45

The execution of the non-recursive Cousin rule still gives us nothing However, the recursive

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? j Round 4 a d b c f g k e h i 46

Done! a Now we are done! j d b c f g k e

Done! a Now we are done! j d b c f g k e h i 47

Recursion Plus Negation u“Naïve” and “Semi-Naïve” evaluation doesn’t work when there are negated sub-goals.

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

Recursion Plus Negation u. In fact, negation wrapped in a recursion makes no sense

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

Problematic Recursive Negation P(x) <- Q(x) AND NOT P(x) EDB: Q(1), Q(2) Initial: P={}

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

Stratified Negation u. Stratification is a constraint usually placed on Datalog with recursion and

Stratified Negation u. Stratification is a constraint usually placed on Datalog with recursion and negation. w It rules out negation wrapped inside recursion. w Gives the sensible IDB relations when negation and recursion are separate. 51

Why Stratified Negation? u. Usually require that Negation be stratified to prevent the problem

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

Safe Rules u A rule is safe if: 1. Each distinguished variable, 2. Each

Safe Rules u A 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. u We allow only safe rules. 53

Example: Unsafe Rules u Each of the following is unsafe and not allowed: 1.

Example: Unsafe Rules u Each of the following is unsafe and not allowed: 1. S(x) <- R(y) w Because 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) w Because x appears in negated sub-goal (R(x)) but does not appear in a sub-goal. 3. S(x) <- R(y) AND x < y w Because x appears in an arithmetic sub-goal (R(x)) but does not appear in a non-negated sub-goal. 54

Example: Unsafe Rules u In each case, an infinite number of values for x

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

Strata u. Stratum: w Let us separate good negative recursive negation from bad. u.

Strata u. Stratum: w Let us separate good negative recursive negation from bad. u. Intuitively, the stratum of an IDB predicate P is: w the maximum number of negations that can be applied to an IDB predicate used in evaluating P. 56

Strata u. Stratified negation = “finite strata. ” u. Notice in P(x) <- Q(x)

Strata u. Stratified negation = “finite strata. ” u. Notice in P(x) <- Q(x) AND NOT P(x), w we can negate P an infinite number of times deriving P(x). 57

Stratum Graph u. To formalize strata use the stratum graph : w Nodes =

Stratum Graph u. To formalize strata use the stratum graph : w Nodes = IDB predicates. w Arc A ->B if predicate A depends on B. w Label this arc “–” if the B sub-goal is negated. 58

Stratified Negation Definition u. The stratum of a node (predicate) is: w the maximum

Stratified Negation Definition u. The stratum of a node (predicate) is: w the maximum number of “–” arcs on a path leading from that node. u. A Datalog program is stratified w if all its IDB predicates have finite strata. 59

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

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

Another Example u. Setting is graph: Nodes designated as source and target. u. EDB

Another Example u. Setting is graph: Nodes designated as source and target. u. EDB consists of: w Source in Source(x) w Target in Target(x) w Arcs between nodes in Arc(x, y) u. Our problem is to find target nodes that are not reached from any source. 61

Another Example u. Rules: “targets not reached from any source”: Reach(x) <- Source(x) Reach(x)

Another Example u. Rules: “targets not reached from any source”: Reach(x) <- Source(x) Reach(x) <- Reach(y) AND Arc(y, x) No. Reach(x) <- Target(x) AND NOT Reach(x) u. First 2 rules recursively define Reach: w A node can be reached if it is a source or can be reached from a node connected to source. w No. Reach if it is a target that cannot be reached. 62

The Stratum Graph Stratum 1: <= 1 “–” arc on any path out. Stratum

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

Models u. To discuss possible results w Concept imported from Logic to Datalog w

Models u. To discuss possible results w Concept imported from Logic to Datalog w Discussion is limited to Datalog application. u. A model is a choice of IDB relations that, with the given EDB relations makes w all rules true regardless of what values are substituted for the variables. 64

Models u. Remember: a rule is true whenever its body is false. w If

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

Minimal Models u. A model should be minimal that if should not properly contain

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

Minimal Models u. When there is no negation, a Datalog program has a unique

Minimal Models u. When there is no negation, a Datalog program has a unique minimal model w One given by naïve and semi-naïve evaluation u. With negation and recursion, there can be several minimal models w even if the program is stratified. u. Fortunately, we can compute the minimal model that makes sense w And that is the stratified model 67

The Stratified Model u. When the Datalog program is stratified: w We evaluate IDB

The Stratified Model u. When the Datalog program is stratified: w We 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. w Once evaluated, treat it as EDB for next strata. w Proceed iteratively until all IDB predicates are evaluated 68

Example: Multiple Models --- 1 1 is the only source; 2 and 3 are

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

Example: Multiple Models --- 1 1 is the only source; 2 and 3 are

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

Example: Multiple Models --- 2 3 rd rule is always true because head is

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

SQL-99 Recursion u. Excellent example of Theory -> Practice u. Datalog recursion inspired the

SQL-99 Recursion u. Excellent example of Theory -> Practice u. Datalog recursion inspired the addition of recursion to the SQL-99 standard. u. Trickier, because SQL allows w grouping-and-aggregation, which behaves like negation and requires a more complex notion of stratification. 72

Example: SQL Recursion --- 1 u. Find Sally’s cousins, using SQL like the recursive

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

Example: SQL Recursion --- 2 Basis Rule: Reflects Cousin(x, y) <- Sibling(x, y) WITH

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

Example: SQL Recursion --- 3 u. With those definitions, we can add the query,

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

Form of SQL Recursive Queries WITH <stuff that looks like Datalog rules> <an SQL

Form of SQL Recursive Queries WITH <stuff that looks like Datalog rules> <an SQL query about EDB, IDB> Rule = [RECURSIVE] <name>(<arguments>) AS <query> 76

Plan to Explain Legal SQL Recursion 1. Define “monotone, ” a generalization of “stratified.

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

Monotonicity u. If relation P is a function of relation Q (and perhaps other

Monotonicity u. If 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. u. Examples: w P = Q UNION R. w P = SELECTa =10(Q ). 78

Example: Nonmonotonicity u. If Sells(rest, soda, price) is our usual relation, then the result

Example: Nonmonotonicity u. If 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. u. Inserting a Joe’s-Rest tuple into Sells usually changes the average price and thus deletes the old average price. 79

SQL Stratum Graph --- 2 u Nodes = 1. IDB relations declared in WITH

SQL Stratum Graph --- 2 u Nodes = 1. IDB relations declared in WITH clause. 2. Subqueries in the body of the “rules. ” w Includes subqueries at any level of nesting. 80

SQL Stratum Graph --- 2 u Arcs P ->Q : 1. P is a

SQL Stratum Graph --- 2 u Arcs 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). w w Put “–” on an arc if P is not monotone in Q. Stratified SQL = finite #’s of –’s on paths. 81

Example: Stratum Graph u. In our Cousin example, the structure of the rules was:

Example: Stratum Graph u. In our Cousin example, the structure of the rules was: Subquery S 1 Sib = … Cousin = ( … FROM Sib ) Subquery S 2 UNION ( … FROM Cousin … ) 82

The Graph No “–” at all, so surely stratified. Sib Cousin S 1 S

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

Nonmonotone Example u. Change the UNION in the Cousin example to EXCEPT: Subquery S

Nonmonotone Example u. Change the UNION in the Cousin example to EXCEPT: Subquery S 1 Sib = … Cousin = ( … FROM Sib ) Subquery S 2 EXCEPT ( … FROM Cousin … ) Can delete a tuple from Cousin Inserting a tuple into S 2 84

The Graph Sib An infinite number of –’s exist on cycles involving Cousin and

The Graph Sib An infinite number of –’s exist on cycles involving Cousin and S 2. Cousin -S 1 S 2 85

NOT Doesn’t Mean Nonmonotone u. Not every NOT means the query is nonmonotone. w

NOT Doesn’t Mean Nonmonotone u. Not every NOT means the query is nonmonotone. w We need to consider each case separately. u. Example: Negating a condition in a WHERE clause just changes the selection condition. w But all selections are monotone. 86

Example: Revised Cousin Revised RECURSIVE Cousin AS subquery S 2 (SELECT * FROM Sib)

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

S 2 Still Monotone in Cousin u. Intuitively, adding a tuple to Cousin cannot

S 2 Still Monotone in Cousin u. Intuitively, adding a tuple to Cousin cannot delete from S 2. u. All former tuples in Cousin can still work with Par tuples to form S 2 tuples. u. In addition, the new Cousin tuple might even join with Par tuples to add to S 2. 88