Docsity

Logical Query Languages-Database systems-Lecture 14 Slides-Computer Science, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download Logical Query Languages-Database systems-Lecture 14 Slides-Computer Science and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Logical Query Languages

Motivatio n:

  1. Logical rules extend more naturally to recursive queries than do es relational algebra. F Used in SQL recursion.
  2. Logical rules form the basis for many information-integration systems and applications.

Datalog Example

Likes(drinker , beer ) Sells(bar , beer , price) Frequents(drinker , bar)

Happy(d) <- Frequents(d,bar) AND Likes(d,beer) AND Sells(bar,beer,p )

 Ab ove is a rule.

 Left side = head.

 Right side = body = AND of subgoals.

 Head and subgoals are atoms.

F Atom = predicate and arguments. F Predicate = relation name or arithmetic predicate, e.g. <. F Arguments are variables or constants.

 Subgoals (not head) may optionally b e negated by NOT.

Evaluation of Rules

Two, dual, approaches:

  1. Variable-based : Consider all p ossible assignments of values to variables. If all subgoals are true, add the head to the result relation.
  2. Tuple-based : Consider all assignments of tuples to subgoals that make each subgoal true. If the variables are assigned consistent values, add the head to the result.

Example: Variable-Based Assignment

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

R = A B

1 2 2 3

 Only assignments that make rst subgoal true:

  1. x! 1, z! 2.
  2. x! 2, z! 3.

 In case (1), y! 3 makes second subgoal true. Since (1; 3) is not in R , the third subgoal is also true. F Thus, add (x; y ) = (1; 3) to relation S.

 In case (2), no value of y makes the second subgoal true. Thus, S =

A B 1 3

Safety

A rule can make no sense if variables app ear in funny ways.

Examples

 S(x) <- R(y)

 S(x) <- NOT R(x)

 S(x) <- R(y) AND x < y

In each of these cases, the result is in nite, even if the relation R is nite.

 To make sense as a database op eration, we need to require three things of a variable x (= de nition of safety). If x app ears in either

  1. The head,
  2. A negated subgoal, or
  3. An arithmetic comparison,

then x must also app ear in a nonnegated, \ordinary" (relational) subgoal of the b o dy.

 We insist that rules b e safe, henceforth.

Datalog Programs

 A collect i on of rules is a Datalog program.

 Predicates/relati ons divide into two classes:

F EDB = extensional database = relation stored in DB.

F IDB = intensional database = relation de ned by one or more rules.

 A predicate must b e IDB or EDB, not b oth.

F Thus, an IDB predicate can app ear in the b o dy or head of a rule; EDB only in the b o dy.

Expressive Power of Datalog

 Nonrecursive Datalog = (classical ) relational algebra. F See discussion in text.

 Datalog simulates SQL select-from-where without aggregation and grouping.

 Recursive Datalog expresses queries that cannot b e expressed in SQL.

 But none of these languages have full expressive p ower (Turing completeness).

Recursion

 IDB predicate P depends on predicate Q if there is a rule with P in the head and Q in a subgoal.

 Draw a graph: no des = IDB predicates, arc P! Q means P dep ends on Q.

 Cycles i recursive.

Recursive Example

Sib(x,y) <- Par(x,p) AND Par(y,p) AND x <> y Cousin(x,y) <- Sib(x,y) Cousin(x,y) <- Par(x,xp) AND Par(y,yp) AND Cousin(xp,yp)

Example

EDB Par =

a d

b c e

f g

j k i

h

 Note, b ecause of symmetry, Sib and Cousin facts app ear in pairs, so we shall mention only (x; y ) when b oth (x; y ) and (y ; x) are meant.

Sib Cousin

Initial ; ;

Round 1 (b; c); (c; e) ; add: (g ; h); (j; k )

Round 2 (b; c); (c; e) add: (g ; h); (j; k )

Round 3 (f ; g ); (f ; h) add: (g ; i); (h; i) (i; k )

Round 4 (k ; k ) add: (i; j )

Problem with Recursive Negation

Consider:

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

 Q = EDB = f 1 ; 2 g.

 Compute IDB P iterativel y?

F Initial ly, P = ;. F Round 1: P = f 1 ; 2 g. F Round 2: P = ;, etc., etc.

Strata

Intuitively : stratum of an IDB predicate = maximum numb er of negations you can pass through on the way to an EDB predicate.

 Must not b e 1 in \strati ed" rules.

 De ne stratum graph:

F No des = IDB predicates. F Arc P! Q if Q app ears in the b o dy of a rule with head P. F Lab el that arc if Q is in a negated subgoal.

Example

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

P

Computing Strata

Stratum of an IDB predicate A = maximum numb er of arcs on any path from A in the stratum graph.

Examples

 For rst example, stratum of P is 1.

 For second example, stratum of Reach is 0; stratum of NoReach is 1.

Strati ed Negation

A Datalog program is strati ed if every IDB predicate has a nite stratum.

Strati ed Mo del

If a Datalog program is strati ed, we can compute the relations for the IDB predicates lowest- stratum- rst.

Example

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

 EDB:

F Source = f 1 g.

F Arc = f(1; 2); (3; 4); (4; 3)g. F Target = f 2 ; 3 g.

source target target

 First compute Reach = f 1 ; 2 g (stratum 0).

 Next compute NoReach = f 3 g.