1 datalog logical rules recursion sql-99 recursion

88
1 Datalog Logical Rules Recursion SQL-99 Recursion

Post on 19-Dec-2015

257 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Datalog Logical Rules Recursion SQL-99 Recursion

1

Datalog

Logical RulesRecursion

SQL-99 Recursion

Page 2: 1 Datalog Logical Rules Recursion SQL-99 Recursion

2

Logic As a Query Language

If-then logical rules have been used in many systems. Most important today: EII (Enterprise

Information Integration). Nonrecursive rules are equivalent

to the core relational algebra. Recursive rules extend relational

algebra --- have been used to add recursion to SQL-99.

Page 3: 1 Datalog Logical Rules Recursion SQL-99 Recursion

3

A Logical Rule

Our first example of a rule uses the relations: Frequents(customer,rest), Likes(customer,soda), and Sells(rest,soda,price).

The rule is a query asking for “happy” customers --- those that frequent a rest that serves a soda that they like.

Page 4: 1 Datalog Logical Rules Recursion SQL-99 Recursion

4

Anatomy of a Rule

Happy(c) <- Frequents(c,rest) ANDLikes(c,soda) AND

Sells(rest,soda,p)

Page 5: 1 Datalog Logical Rules Recursion SQL-99 Recursion

5

Anatomy of a Rule

Happy(c) <- Frequents(c,rest) ANDLikes(c,soda) AND

Sells(rest,soda,p)Body = “antecedent” =AND of sub-goals.

Head = “consequent,”a single sub-goal

Read thissymbol “if”

Page 6: 1 Datalog Logical Rules Recursion SQL-99 Recursion

6

sub-goals Are Atoms

An atom is a predicate, or relation name with variables or constants as arguments.

The head is an atom; the body is the AND of one or more atoms.

Convention: Predicates begin with a capital, variables begin with lower-case.

Page 7: 1 Datalog Logical Rules Recursion SQL-99 Recursion

7

Example: Atom

Sells(rest, soda, p)

Page 8: 1 Datalog Logical Rules Recursion SQL-99 Recursion

8

Example: Atom

Sells(rest, soda, p)

The predicate= name of arelation

Arguments arevariables

Page 9: 1 Datalog Logical Rules Recursion SQL-99 Recursion

9

Interpreting Rules

A variable appearing in the head is called distinguished ; otherwise it is nondistinguished.

Page 10: 1 Datalog Logical Rules Recursion SQL-99 Recursion

10

Interpreting Rules

Rule meaning: The head is true of the distinguished

variables if there exist values of the

nondistinguished variables that make all sub-goals of the body

true.

Page 11: 1 Datalog Logical Rules Recursion SQL-99 Recursion

11

Example: Interpretation

Happy(c) <- Frequents(c,rest) ANDLikes(c,soda) AND

Sells(rest,soda,p)

Interpretation: customer d is happy if there exista 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.

Page 12: 1 Datalog Logical Rules Recursion SQL-99 Recursion

12

Example: Interpretation

Happy(c) <- Frequents(c,rest) ANDLikes(c,soda) AND

Sells(rest,soda,p)Distinguishedvariable

Nondistinguishedvariables

Interpretation: customer d is happy if there exista 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.

Page 13: 1 Datalog Logical Rules Recursion SQL-99 Recursion

13

Arithmetic sub-goals

In addition to relations as predicates, a predicate for a sub-goal of the body can be an arithmetic comparison. We write such sub-goals in the usual

way, e.g.: x < y.

Page 14: 1 Datalog Logical Rules Recursion SQL-99 Recursion

14

Example: Arithmetic

A soda is “cheap” if there are at least two rests that sell it for under $1.

Figure out a rule that would determine whether a soda is cheap or not.

Page 15: 1 Datalog Logical Rules Recursion SQL-99 Recursion

15

Example: Arithmetic

Cheap(soda) <- Sells(rest1,soda,p1) ANDSells(rest2,soda,p2) ANDp1 < 1.00 ANDp2 < 1.00 AND rest1 <> rest2

Page 16: 1 Datalog Logical Rules Recursion SQL-99 Recursion

16

Negated sub-goals

We may put “NOT” in front of a sub-goal, to negate its meaning.

Page 17: 1 Datalog Logical Rules Recursion SQL-99 Recursion

17

Negated sub-goals

Example: Think of Arc(a,b) as arcs in a graph. 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)

Page 18: 1 Datalog Logical Rules Recursion SQL-99 Recursion

18

Algorithms for Applying Rules

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.

Page 19: 1 Datalog Logical Rules Recursion SQL-99 Recursion

19

Example: Variable-Based --- 1

S(x,y) <- Arc(x,z) AND Arc(z,y)AND NOT Arc(x,y)

Arc(1,2) and Arc(2,3) are the only tuples in the Arc relation.

Only assignments to make the first sub-goal Arc(x,z) true are:

1. x = 1; z = 22. x = 2; z = 3

Page 20: 1 Datalog Logical Rules Recursion SQL-99 Recursion

20

Example: Variable-Based; x=1, z=2

S(x,y) <- Arc(x,z) AND Arc(z,y) AND NOT Arc(x,y)

1 1 2 2 1

Page 21: 1 Datalog Logical Rules Recursion SQL-99 Recursion

21

Example: Variable-Based; x=1, z=2

S(x,y) <- Arc(x,z) AND Arc(z,y) AND NOT Arc(x,y)

1 1 2 2 13 3 3

3 is the only value of y that makes allthree sub-goals true.

Makes S(1,3) a tupleof the answer

Page 22: 1 Datalog Logical Rules Recursion SQL-99 Recursion

22

Example: Variable-Based; x=2, z=3

S(x,y) <- Arc(x,z) AND Arc(z,y) AND NOT Arc(x,y)

2 2 3 3 2

Page 23: 1 Datalog Logical Rules Recursion SQL-99 Recursion

23

Example: Variable-Based; x=2, z=3

S(x,y) <- Arc(x,z) AND Arc(z,y) AND NOT Arc(x,y)

2 2 3 3 2No value of ymakes Arc(3,y)true.

Thus, no contributionto the head tuples;S = {(1,3)}

Page 24: 1 Datalog Logical Rules Recursion SQL-99 Recursion

24

Tuple-Based Assignment

Start with the non-negated, relational sub-goals only.

Consider all assignments of tuples to these sub-goals. Choose tuples only from the corresponding

relations.

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.

Page 25: 1 Datalog Logical Rules Recursion SQL-99 Recursion

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)

Four possible assignments to first two sub-goals:

Arc(x,z) Arc(z,y) (1,2) (1,2) (1,2) (2,3) (2,3) (1,2) (2,3) (2,3)

Page 26: 1 Datalog Logical Rules Recursion SQL-99 Recursion

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)

Four possible assignments to first two sub-goals:

Arc(x,z) Arc(z,y) (1,2) (1,2) (1,2) (2,3) (2,3) (1,2) (2,3) (2,3)

Only assignmentwith consistentz-value. Since italso makesNOT Arc(x,y) true,add S(1,3) toresult.

These two rows are invalid since z can’t be (3 and 1) or (3 and 2)simultaneously.

Page 27: 1 Datalog Logical Rules Recursion SQL-99 Recursion

27

Datalog Programs

A Datalog program is a collection of rules.

In a program, predicates can be either

1. EDB = Extensional Database = stored table.

2. IDB = Intensional Database = relation defined by rules.

Never both! No EDB in heads.

Page 28: 1 Datalog Logical Rules Recursion SQL-99 Recursion

28

Evaluating Datalog Programs

As long as there is no recursion, we can pick an order to evaluate the

IDB predicates, so that all the predicates in the body of

its rules have already been evaluated. If an IDB predicate has more than

one rule, each rule contributes tuples to its

relation.

Page 29: 1 Datalog Logical Rules Recursion SQL-99 Recursion

29

Example: Datalog Program

Using following EDB find all the manufacturers of sodas Joe doesn’t sell: Sells(rest, soda, price) and sodas(name, manf).

JoeSells(s) <- Sells(’Joe’’s rest’, s, p)Answer(m) <- Sodas(s,m)

AND NOT JoeSells(s)

Page 30: 1 Datalog Logical Rules Recursion SQL-99 Recursion

30

Expressive Power of Datalog

Without recursion, Datalog can express all and only the

queries of core relational algebra. The same as SQL select-from-where,

without aggregation and grouping.

Page 31: 1 Datalog Logical Rules Recursion SQL-99 Recursion

31

Expressive Power of Datalog

But with recursion, Datalog can express more than these

languages. Yet still not Turing-complete.

Page 32: 1 Datalog Logical Rules Recursion SQL-99 Recursion

32

Recursive Example:Generalized Cousins

EDB: Parent(c,p) = p is a parent of c.

Generalized cousins: people with common ancestors one or more generations back.

Note: We are all cousins according to this definition.

Page 33: 1 Datalog Logical Rules Recursion SQL-99 Recursion

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)

Page 34: 1 Datalog Logical Rules Recursion SQL-99 Recursion

34

Definition of Recursion

Form a dependency graph whose nodes = IDB predicates.

Arc X ->Y if and only if there is a rule with X in the head and

Y in the body. Cycle = recursion; No cycle = no recursion.

Page 35: 1 Datalog Logical Rules Recursion SQL-99 Recursion

35

Example: Dependency Graphs

Cousin

Sibling

Answer

JoeSells

Recursive Non-recursive

Page 36: 1 Datalog Logical Rules Recursion SQL-99 Recursion

36

Evaluating Recursive Rules

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.

Page 37: 1 Datalog Logical Rules Recursion SQL-99 Recursion

37

The “Naïve” Evaluation Algorithm

Start:IDB = 0

Apply rulesto IDB, EDB

Changeto IDB?

noyesdone

Page 38: 1 Datalog Logical Rules Recursion SQL-99 Recursion

38

Example: Evaluation of Cousin

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,xParent) AND Parent(y,yParent)AND

Cousin(xParent,yParent)

Page 39: 1 Datalog Logical Rules Recursion SQL-99 Recursion

39

Semi-naive Evaluation

Since the EDB never changes, on each round we only get new IDB

tuples if we use at least one IDB tuple that was obtained on the previous round.

Saves work; lets us avoid rediscovering most known facts. A fact could still be derived in a second

way.

Page 40: 1 Datalog Logical Rules Recursion SQL-99 Recursion

40

Example: Evaluation of Cousin

We’ll proceed in rounds to infer Sibling facts (red) and Cousin facts (green).

Page 41: 1 Datalog Logical Rules Recursion SQL-99 Recursion

41

Parent Data: Parent Above Child

a d

b c e

f g h

j k i

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?

Page 42: 1 Datalog Logical Rules Recursion SQL-99 Recursion

42

Parent Data: Parent Above Child

a d

b c e

f g h

j k i

Exercise:

1. What do you expect after first round?

Page 43: 1 Datalog Logical Rules Recursion SQL-99 Recursion

43

Round 1

a d

b c e

f g h

j k i

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?

Page 44: 1 Datalog Logical Rules Recursion SQL-99 Recursion

44

Round 2

a d

b c e

f g h

j k i

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?

Page 45: 1 Datalog Logical Rules Recursion SQL-99 Recursion

45

Round 3

a d

b c e

f g h

j k i

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?

Page 46: 1 Datalog Logical Rules Recursion SQL-99 Recursion

46

Round 4

a d

b c e

f g h

j k i

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?

Page 47: 1 Datalog Logical Rules Recursion SQL-99 Recursion

47

Done!

a d

b c e

f g h

j k i

Now we are done!

Page 48: 1 Datalog Logical Rules Recursion SQL-99 Recursion

48

Recursion Plus Negation

“Naïve” and “Semi-Naïve” evaluation doesn’t work when there are negated sub-goals. Discovering IDB tuples on one route

can decrease the IDB tuples on the next route.

Losing IDB tuples on one route can yield more tuples on the next route.

Page 49: 1 Datalog Logical Rules Recursion SQL-99 Recursion

49

Recursion Plus Negation

In fact, negation wrapped in a recursion makes no sense in general.

Even when recursion and negation are separate, we can have ambiguity about the correct IDB relations.

Page 50: 1 Datalog Logical Rules Recursion SQL-99 Recursion

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

Page 51: 1 Datalog Logical Rules Recursion SQL-99 Recursion

51

Stratified Negation

Stratification is a constraint usually placed on Datalog with recursion and negation. It rules out negation wrapped inside

recursion. Gives the sensible IDB relations when

negation and recursion are separate.

Page 52: 1 Datalog Logical Rules Recursion SQL-99 Recursion

52

Why Stratified Negation?

Usually require that Negation be stratified to prevent the problem just described. Stratification does two things: Lets us evaluate the IDB predicates in

a way that it converges. Lets us discover the “correct” solution

in face of “many solutions.”

Page 53: 1 Datalog Logical Rules Recursion SQL-99 Recursion

53

Safe Rules

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.

We allow only safe rules.

Page 54: 1 Datalog Logical Rules Recursion SQL-99 Recursion

54

Example: Unsafe Rules

Each of the following is unsafe and not allowed:

1. S(x) <- R(y) 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) 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 Because x appears in an arithmetic sub-goal (R(x))

but does not appear in a non-negated sub-goal.

Page 55: 1 Datalog Logical Rules Recursion SQL-99 Recursion

55

Example: Unsafe Rules

In each case, an infinite number of values for x can satisfy the rule, even if R is a finite relation.

Page 56: 1 Datalog Logical Rules Recursion SQL-99 Recursion

56

Strata

Stratum: Let us separate good negative

recursive negation from bad. Intuitively, the stratum of an IDB

predicate P is: the maximum number of negations

that can be applied to an IDB predicate used in evaluating P.

Page 57: 1 Datalog Logical Rules Recursion SQL-99 Recursion

57

Strata

Stratified negation = “finite strata.”

Notice in P(x) <- Q(x) AND NOT P(x), we can negate P an infinite number

of times deriving P(x).

Page 58: 1 Datalog Logical Rules Recursion SQL-99 Recursion

58

Stratum Graph

To formalize strata use the stratum graph : Nodes = IDB predicates. Arc A ->B if predicate A depends on

B. Label this arc “–” if the B sub-goal is

negated.

Page 59: 1 Datalog Logical Rules Recursion SQL-99 Recursion

59

Stratified Negation Definition

The stratum of a node (predicate) is: the maximum number of “–” arcs on a

path leading from that node. A Datalog program is stratified

if all its IDB predicates have finite strata.

Page 60: 1 Datalog Logical Rules Recursion SQL-99 Recursion

60

Example

P(x) <- Q(x) AND NOT P(x)

Infinite path due to loop: not stratified!

P

Q

_

Page 61: 1 Datalog Logical Rules Recursion SQL-99 Recursion

61

Another Example

Setting is graph: Nodes designated as source and target.

EDB consists of: Source in Source(x) Target in Target(x) Arcs between nodes in Arc(x,y)

Our problem is to find target nodes that are not reached from any source.

Page 62: 1 Datalog Logical Rules Recursion SQL-99 Recursion

62

Another Example

Rules: “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) First 2 rules recursively define Reach:

A node can be reached if it is a source or can be reached from a node connected to source.

NoReach if it is a target that cannot be reached.

Page 63: 1 Datalog Logical Rules Recursion SQL-99 Recursion

63

The Stratum Graph

NoReach

Reach

_

Stratum 0:No “–” arcs onany path out.

Stratum 1:<= 1 “–” arc onany 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.

Page 64: 1 Datalog Logical Rules Recursion SQL-99 Recursion

64

Models

To discuss possible results Concept imported from Logic to Datalog Discussion is limited to Datalog application.

A model is a choice of IDB relations that, with the given EDB relations makes all rules true regardless of what values are

substituted for the variables.

Page 65: 1 Datalog Logical Rules Recursion SQL-99 Recursion

65

Models

Remember: a rule is true whenever its body is false. If moon were made of blue cheese,

you will all flunk. However, if the body is true, then

the head must be true as well. If professor is human, you will get fair

grades.

Page 66: 1 Datalog Logical Rules Recursion SQL-99 Recursion

66

Minimal Models

A model should be minimal that if should not properly contain any other model

Intuitively, we don’t want to assert facts that do not have to be asserted

Page 67: 1 Datalog Logical Rules Recursion SQL-99 Recursion

67

Minimal Models

When there is no negation, a Datalog program has a unique minimal model One given by naïve and semi-naïve

evaluation

With negation and recursion, there can be several minimal models even if the program is stratified.

Fortunately, we can compute the minimal model that makes sense And that is the stratified model

Page 68: 1 Datalog Logical Rules Recursion SQL-99 Recursion

68

The Stratified Model

When the Datalog program is stratified: 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.

Once evaluated, treat it as EDB for next strata.

Proceed iteratively until all IDB predicates are evaluated

Page 69: 1 Datalog Logical Rules Recursion SQL-99 Recursion

69

Example: Multiple Models --- 1

Reach(x) <- Source(x)Reach(x) <- Reach(y) AND Arc(y,x)NoReach(x) <- Target(x) AND NOT Reach(x)

1 2 3 4 Source Target Target

ArcArc

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.

Page 70: 1 Datalog Logical Rules Recursion SQL-99 Recursion

70

Example: Multiple Models --- 1

Reach(x) <- Source(x)Reach(x) <- Reach(y) AND Arc(y,x)NoReach(x) <- Target(x) AND NOT Reach(x)

1 2 3 4 Source Target Target

ArcArc

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.

Page 71: 1 Datalog Logical Rules Recursion SQL-99 Recursion

71

Example: Multiple Models --- 2

Reach(x) <- Source(x)Reach(x) <- Reach(y) AND Arc(y,x)NoReach(x) <- Target(x) AND NOT Reach(x)

1 2 3 4 Source Target Target

ArcArc

Arc

Another model! Reach(1), Reach(2),Reach(3), Reach(4); NoReach is empty.

3rd rule is always true because head is false.

Page 72: 1 Datalog Logical Rules Recursion SQL-99 Recursion

72

SQL-99 Recursion

Excellent example of Theory -> Practice

Datalog recursion inspired the addition of recursion to the SQL-99 standard.

Trickier, because SQL allows grouping-and-aggregation, which

behaves like negation and requires a more complex notion of stratification.

Page 73: 1 Datalog Logical Rules Recursion SQL-99 Recursion

73

Example: SQL Recursion --- 1

Find Sally’s cousins, using SQL like the recursive Datalog example.

Parent(child,parent) is the EDB.WITH Sibling(x,y) AS

SELECT p1.child, p2.childFROM Parent p1, Parent p2WHERE 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

Page 74: 1 Datalog Logical Rules Recursion SQL-99 Recursion

74

Example: SQL Recursion --- 2

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 ruleCousin(x,y) <-Parent(x,xParent) ANDParent(y,yParent) ANDCousin(xParent,yParent)

Page 75: 1 Datalog Logical Rules Recursion SQL-99 Recursion

75

Example: SQL Recursion --- 3

With those definitions, we can add the query, which is about the “temporary view” Cousin(x,y):

SELECT yFROM CousinWHERE x = ‘Sally’;

Page 76: 1 Datalog Logical Rules Recursion SQL-99 Recursion

76

Form of SQL Recursive Queries

WITH<stuff that looks like Datalog rules><an SQL query about EDB, IDB>

Rule =[RECURSIVE] <name>(<arguments>)AS <query>

Page 77: 1 Datalog Logical Rules Recursion SQL-99 Recursion

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.

Page 78: 1 Datalog Logical Rules Recursion SQL-99 Recursion

78

Monotonicity

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.

Examples: P = Q UNION R. P = SELECTa =10(Q ).

Page 79: 1 Datalog Logical Rules Recursion SQL-99 Recursion

79

Example: Nonmonotonicity

If Sells(rest,soda,price) is our usual relation, then the result of the query:

SELECT AVG(price)FROM SellsWHERE rest = ’Joe’’s Rest’;is not monotone in Sells. Inserting a Joe’s-Rest tuple into Sells

usually changes the average price and thus deletes the old average price.

Page 80: 1 Datalog Logical Rules Recursion SQL-99 Recursion

80

SQL Stratum Graph --- 2

Nodes =1. IDB relations declared in WITH

clause.2. Subqueries in the body of the

“rules.” Includes subqueries at any level of

nesting.

Page 81: 1 Datalog Logical Rules Recursion SQL-99 Recursion

81

SQL Stratum Graph --- 2

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

Put “–” on an arc if P is not monotone in Q. Stratified SQL = finite #’s of –’s on paths.

Page 82: 1 Datalog Logical Rules Recursion SQL-99 Recursion

82

Example: Stratum Graph

In our Cousin example, the structure of the rules was:

Sib = … Cousin = ( … FROM Sib )

UNION ( … FROM Cousin … )

Subquery S1

Subquery S2

Page 83: 1 Datalog Logical Rules Recursion SQL-99 Recursion

83

The Graph

Sib

S2S1

Cousin

No “–” at all,so surelystratified.

Page 84: 1 Datalog Logical Rules Recursion SQL-99 Recursion

84

Nonmonotone Example

Change the UNION in the Cousin example to EXCEPT:

Sib = … Cousin = ( … FROM Sib )

EXCEPT ( … FROM Cousin … )

Subquery S1

Subquery S2

Inserting a tuple into S2Can delete a tuple from Cousin

Page 85: 1 Datalog Logical Rules Recursion SQL-99 Recursion

85

The Graph

Sib

S2S1

Cousin

--

An infinite numberof –’s exist oncycles involvingCousin and S2.

Page 86: 1 Datalog Logical Rules Recursion SQL-99 Recursion

86

NOT Doesn’t Mean Nonmonotone

Not every NOT means the query is nonmonotone. We need to consider each case

separately. Example: Negating a condition in a

WHERE clause just changes the selection condition. But all selections are monotone.

Page 87: 1 Datalog Logical Rules Recursion SQL-99 Recursion

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));

Revisedsubquery S2

The onlydifference

Page 88: 1 Datalog Logical Rules Recursion SQL-99 Recursion

88

S2 Still Monotone in Cousin

Intuitively, adding a tuple to Cousin cannot delete from S2.

All former tuples in Cousin can still work with Par tuples to form S2 tuples.

In addition, the new Cousin tuple might even join with Par tuples to add to S2.