Used in SQL recursion. 2. Logical rules form the basis - - PDF document

used in sql recursion 2 logical rules form the basis for
SMART_READER_LITE
LIVE PREVIEW

Used in SQL recursion. 2. Logical rules form the basis - - PDF document

Logical Query Languages Motiv atio n: 1. Logical rules extend more naturally to recursiv e queries than do es relational algebra. Used in SQL recursion. 2. Logical rules form the basis for man y information-in


slide-1
SLIDE 1 Logical Query Languages Motiv atio n: 1. Logical rules extend more naturally to recursiv e queries than do es relational algebra.

Used in SQL recursion. 2. Logical rules form the basis for man y information-in tegration systems and applications. 1
slide-2
SLIDE 2 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
  • v
e is a rule.
  • Left
side = he ad.
  • Righ
t side = b
  • dy
= AND
  • f
sub go als.
  • Head
and subgoals are atoms.

A tom = pr e dic ate and argumen ts.

Predicate = relation name
  • r
arithmetic predicate, e.g. <.

Argumen ts are v ariables
  • r
constan ts.
  • Subgoals
(not head) ma y
  • ptionally
b e negated b y NOT. 2
slide-3
SLIDE 3 Meaning
  • f
Rules Head is true
  • f
its argumen ts if there exist v alues for lo c al v ariables (those in b
  • dy
, not in head) that mak e all
  • f
the subgoals true.
  • If
no negation
  • r
arithmetic comparisons, just natural join the subgoals and pro ject
  • n
to the head v ariables. Example Ab
  • v
e rule equiv alen t to Happy(d) =
  • dr
ink er (Frequents . / Likes . / Sells) 3
slide-4
SLIDE 4 Ev aluation
  • f
Rules Tw
  • ,
dual, approac hes: 1. V ariable-b ase d : Consider all p
  • ssible
assignmen ts
  • f
v alues to v ariables. If all subgoals are true, add the head to the result relation. 2. T uple-b ase d : Consider all assignmen ts
  • f
tuples to subgoals that mak e eac h subgoal true. If the v ariables are assigned consisten t v alues, add the head to the result. Example: V ariable-Based Assignmen t S(x,y) <- R(x,z) AND R(z,y) AND NOT R(x,y) R = A B 1 2 2 3 4
slide-5
SLIDE 5
  • Only
assignmen ts that mak e rst subgoal true: 1. x ! 1, z ! 2. 2. x ! 2, z ! 3.
  • In
case (1), y ! 3 mak es second subgoal true. Since (1; 3) is not in R , the third subgoal is also true.

Th us, add (x; y ) = (1; 3) to relation S .
  • In
case (2), no v alue
  • f
y mak es the second subgoal true. Th us, S = A B 1 3 5
slide-6
SLIDE 6 Example: T uple-Based Assignmen t T ric k: start with the p
  • sitiv
e (not negated), relational (not arithmetic) subgoals
  • nly
. S(x,y) <- R(x,z) AND R(z,y) AND NOT R(x,y) R = A B 1 2 2 3
  • F
  • ur
assignmen ts
  • f
tuples to subgoals: R (x; z ) R (z ; y ) (1; 2) (1; 2) (1; 2) (2; 3) (2; 3) (1; 2) (2; 3) (2; 3)
  • Only
the second giv es a consisten t v alue to z .
  • That
assignmen t also mak es NOT R(x,y) true.
  • Th
us, (1; 3) is the
  • nly
tuple for the head. 6
slide-7
SLIDE 7 Safet y A rule can mak e no sense if v ariables app ear in funn y w a ys. Examples
  • S(x)
<- R(y)
  • S(x)
<- NOT R(x)
  • S(x)
<- R(y) AND x < y In eac h
  • f
these cases, the result is innite, ev en if the relation R is nite.
  • T
  • mak
e sense as a database
  • p
eration, w e need to require three things
  • f
a v ariable x (= denition
  • f
safety). If x app ears in either 1. The head, 2. A negated subgoal,
  • r
3. An arithmetic comparison, then x m ust also app ear in a nonnegated, \ordinary" (relational) subgoal
  • f
the b
  • dy
.
  • W
e insist that rules b e safe, henceforth. 7
slide-8
SLIDE 8 Datalog Programs
  • A
collect i
  • n
  • f
rules is a Datalo g pr
  • gr
am.
  • Predicates/relati
  • ns
divide in to t w
  • classes:

EDB = extensional datab ase = relation stored in DB.

IDB = intensional datab ase = relation dened b y
  • ne
  • r
more rules.
  • A
predicate m ust b e IDB
  • r
EDB, not b
  • th.

Th us, an IDB predicate can app ear in the b
  • dy
  • r
head
  • f
a rule; EDB
  • nly
in the b
  • dy
. 8
slide-9
SLIDE 9 Example Con v ert the follo wing SQL (Find the man ufacturers
  • f
the b eers Jo e sells): Beers(name , manf) Sells(bar , beer , price) SELECT manf FROM Beers WHERE name IN( SELECT beer FROM Sells WHERE bar = 'Joe''s Bar' ); to a Datalog program. JoeSells(b) <- Sells('Joe''s Bar', b, p) Answer(m) <- JoeSells(b) AND Beers(b,m)
  • Note:
Beers, Sells = EDB; JoeSells, Answer = IDB. 9
slide-10
SLIDE 10 Expressiv e P
  • w
er
  • f
Datalog
  • Nonrecursiv
e Datalog = (classical ) relational algebra.

See discussion in text.
  • Datalog
sim ulates SQL select-from-where without aggregation and grouping.
  • Recursiv
e Datalog expresses queries that cannot b e expressed in SQL.
  • But
none
  • f
these languages ha v e full expressiv e p
  • w
er (T uring c
  • mpleteness).
10
slide-11
SLIDE 11 Recursion
  • IDB
predicate P dep ends
  • n
predicate Q if there is a rule with P in the head and Q in a subgoal.
  • Dra
w a graph: no des = IDB predicates, arc P ! Q means P dep ends
  • n
Q.
  • Cycles
i recursiv e. Recursiv e 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) 11
slide-12
SLIDE 12 Iterativ e Fixed-P
  • in
t Ev aluates Recursiv e Rules Change to IDB? Start IDB = ; Apply rules to IDB, EDB y es no done 12
slide-13
SLIDE 13 Example EDB Par = a d b c e f g j k i h
  • Note,
b ecause
  • f
symmetry , Sib and Cousin facts app ear in pairs, so w e shall men tion
  • nly
(x; y ) when b
  • th
(x; y ) and (y ; x) are mean t. 13
slide-14
SLIDE 14 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 ) 14
slide-15
SLIDE 15 Stratied Negation
  • Negation
wrapp ed inside a recursion mak es no sense.
  • Ev
en when negation and recursion are separated, there can b e am biguit y ab
  • ut
what the rules mean, and some
  • ne
meaning m ust b e selected.
  • Str
atie d ne gation is an additional restrain t
  • n
recursiv e rules (lik e safet y) that solv es b
  • th
problems: 1. It rules
  • ut
negation wrapp ed in recursion. 2. When negation is separate from recursion, it yields the in tuitiv el y correct meaning
  • f
rules (the str atie d mo del). 15
slide-16
SLIDE 16 Problem with Recursiv e Negation Consider: P(x) <- Q(x) AND NOT P(x)
  • Q
= EDB = f1; 2g.
  • Compute
IDB P iterativ el y?

Initial ly , P = ;.

Round 1: P = f1; 2g.

Round 2: P = ;, etc., etc. 16
slide-17
SLIDE 17 Strata In tuitiv ely : stratum
  • f
an IDB predicate = maxim um n um b er
  • f
negations y
  • u
can pass through
  • n
the w a y to an EDB predicate.
  • Must
not b e 1 in \stratied" rules.
  • Dene
str atum gr aph:

No des = IDB predicates.

Arc P ! Q if Q app ears in the b
  • dy
  • f
a rule with head P .

Lab el that arc
  • if
Q is in a negated subgoal. Example P(x) <- Q(x) AND NOT P(x) P
  • 17
slide-18
SLIDE 18 Example Whic h target no des cannot b e reac hed from an y source no de? Reach(x) <- Source(x) Reach(x) <- Reach(y) AND Arc(y,x) NoReach(x) <- Target(x) AND NOT Reach(x) Reach NoReach
  • 18
slide-19
SLIDE 19 Computing Strata Str atum
  • f
an IDB predicate A = maxim um n um b er
  • f
  • arcs
  • n
an y path from A in the stratum graph. Examples
  • F
  • r
rst example, stratum
  • f
P is 1.
  • F
  • r
second example, stratum
  • f
Reach is 0; stratum
  • f
NoReach is 1. Stratied Negation A Datalog program is str atie d if ev ery IDB predicate has a nite stratum. Stratied Mo del If a Datalog program is stratied, w e can compute the relations for the IDB predicates lo w est- stratum-rst. 19
slide-20
SLIDE 20 Example Reach(x) <- Source(x) Reach(x) <- Reach(y) AND Arc(y,x) NoReach(x) <- Target(x) AND NOT Reach(x)
  • EDB:

Source = f1g.

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

Target = f2; 3g. 1 2 3 4 source target target
  • First
compute Reach = f1; 2g (stratum 0).
  • Next
compute NoReach = f3g. 20
slide-21
SLIDE 21 Is the Stratied Solution \Ob vious"? Not really .
  • There
is another mo del that mak es the rules true no matter what v alues w e substitute for the v ariables.

Reach = f1; 2; 3; 4g.

NoReach = ;.
  • Remem
b er: the
  • nly
w a y to mak e a Datalog rule false is to nd v alues for the v ariables that mak e the b
  • dy
true and the head false.

F
  • r
this mo del, the heads
  • f
the rules for Reach are true for all v alues, and in the rule for NoReach the subgoal NOT Reach(x) assures that the b
  • dy
cannot b e true. 21