<<

Logical Query Languages Datalog Example

Motivatio n

beer Likesdrinker

beer price Sellsbar

Logical rules extend more naturally to

bar Frequentsdrinker

recursive queries than do es

✦ Used in SQL

Happyd

Frequentsdbar AND

Logical rules form the basis for many

Likesdbeer AND

informationintegration systems and

Sellsbarbeerp

applications

Ab oveisarule

Left side head

Right side body AND of subgoals

Head and subgoals are atoms

✦ Atom predicate and arguments

✦ Predicate relation name or arithmetic

predicate eg

✦ Arguments are variables or constants

Subgoals not head may optionally be

negated by NOT

Meaning of Rules Evaluation of Rules

Head is true of its arguments if there exist values Two dual approaches

for local variables those in b o dy not in head that

Variablebased Consider all p ossible

make all of the subgoals true

assignments of values to variables If all

If no or arithmetic comparisons just subgoals are true add the head to the result

natural join the subgoals and pro ject onto the relation

head variables

Tuplebased Consider all assignments of

tuples to subgoals that makeeach subgoal

Example

true If the variables are assigned consistent

Ab ove rule equivalent to Happyd

values add the head to the result

 Frequents  Likes  Sells

dr ink er

Example VariableBased Assignment

Sxy Rxz AND Rzy

AND NOT Rxy

R

B A

Only assignments that make rst subgoal true Example TupleBased Assignment

Trick start with the p ositive not negated

x z

relational not arithmetic subgoals only

x z

Sxy Rxz AND Rzy

 In case y makes second subgoal true

AND NOT Rxy

Since is not in R the third subgoal is

R

also true

B A

✦ Thus add x y to relation S

 In case no value of y makes the second

subgoal true Thus S

B A

Four assignments of tuples to subgoals

R x z R z y

Only the second gives a consistentvalue to z

That assignmentalsomakes NOT Rxy true

Thus is the only tuple for the head

Safety Datalog Programs

A rule can make no sense if variables app ear in

A collect i on of rules is a Datalogprogram

funnyways

Predicatesrelati ons divide into two classes

Examples

✦ EDB extensional relation

stored in DB

Sx Ry

✦ IDB intensional database relation

Sx NOT Rx

dened by one or more rules

Sx Ry AND x y

A predicate must b e IDB or EDB not b oth

In each of these cases the result is innite even if

✦ Thus an IDB predicate can app ear in the

the relation R is nite

b o dy or head of a rule EDB only in the

To make sense as a database op eration we

body

need to require three things of a variable x

denition of safety If x app ears in either

The head

A negated subgoal or

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

Example ExpressivePower of Datalog

Convert the following SQL Find the

Nonrecursive Datalog classical relational

manufacturers of the b eers Jo e sells

algebra

manf Beersname

✦ See discussion in text

beer price Sellsbar

Datalog simulates SQL selectfromwhere

SELECT manf

without aggregation and grouping

FROM Beers

Recursive Datalog expresses queries that

WHERE name IN

cannot b e expressed in SQL

SELECT beer

FROM Sells

But none of these languages havefull

WHERE bar Joes Bar

expressivepower

to a Datalog program

JoeSellsb

SellsJoes Bar b p

Answerm

JoeSellsb AND Beersbm

Note Beers Sells EDBJoeSells

Answer IDB

Recursion Iterative FixedPoint Evaluates Recursive

Rules

IDB predicate P depends on predicate Q if

there is a rule with P in the head and Q in a

subgoal

Start

Draw a graph no des IDB predicates arc

IDB

P Q means P dep ends on Q

 Cycles i recursive

Recursive Example

Apply rules

to IDB EDB

Sibxy Parxp AND Paryp

AND x y

Cousinxy Sibxy

Cousinxy Parxxp

Change

AND Paryyp

done

to IDB

yes no

AND Cousinxpyp

Sib Cousin

Example

Initial

EDB Par

Round b c e

add g h j k

a d

Round b c c e

add g h j k

b c e

Round f g f h

add g i h i

i k

f g h

Round k k

add i j

j k i

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

Stratied Negation Problem with Recursive Negation

Consider

Negation wrapp ed inside a recursion makes no

sense Px Qx AND NOT Px

Even when negation and recursion are

Q EDBf g

separated there can b e ambiguity ab out what

Compute IDB P iterativel y the rules mean and some one meaning must

b e selected

✦ Initial ly P

Stratiednegation is an additional restrainton

✦ Round P f g

recursive rules like safety that solves b oth

✦ Round P etc etc problems

It rules out negation wrapp ed in

recursion

When negation is separate from recursion

it yields the intuitivel y correct meaning of

rules the stratiedmodel

Strata Example

Intuitively stratum of an IDB predicate Which target no des cannot b e reached from any

maximum numb er of you can pass source no de

through on the way to an EDB predicate

Reachx Sourcex

Reachx Reachy AND Arcyx

Must not b e in stratied rules

NoReachx Targetx

 Dene stratum graph

AND NOT Reachx

✦ No des IDB predicates

✦ Arc P  Q if Q app ears in the b o dy of a

NoReach

rule with head P

✦ Lab el that arc  if Q is in a negated

Reach

subgoal

Example

Px Qx AND NOT Px



P

Computing Strata Example

Stratum of an IDB predicate A maximum

Reachx Sourcex

number of arcs on anypathfromA in the

Reachx Reachy AND Arcyx

stratum graph

NoReachx Targetx

Examples AND NOT Reachx

For rst example stratum of P is 

EDB

For second example stratum of Reach is

✦ Source fg

stratum of NoReach is

✦ Arc f g

Stratied Negation

✦ Target f g

A Datalog program is stratied if every IDB

predicate has a nite stratum

Stratied Mo del

source target target

If a Datalog program is stratied we can compute

First compute Reach f g stratum

the relations for the IDB predicates lowest

stratumrst

Next compute NoReach fg

Is the Stratied Solution Obvious

Not really

There is another mo del that makes the rules

true no matter what values we substitute for

the variables

✦ Reach f g

✦ NoReach

Rememb er the only way to make a Datalog

rule false is to nd values for the variables

that make the b o dy true and the head false

✦ For this mo del the heads of the rules

for Reach are true for all values and

in the rule for NoReach the subgoal

NOT Reachx assures that the b o dy

cannot b e true