Cse 344 January 29th – Datalog.

Slides:



Advertisements
Similar presentations
1 Datalog: Logic Instead of Algebra. 2 Datalog: Logic instead of Algebra Each relational-algebra operator can be mimicked by one or several Database Logic.
Advertisements

Relational Calculus and Datalog
CSE 636 Data Integration Conjunctive Queries Containment Mappings / Canonical Databases Slides by Jeffrey D. Ullman.
Lecture 11: Datalog Tuesday, February 6, Outline Datalog syntax Examples Semantics: –Minimal model –Least fixpoint –They are equivalent Naive evaluation.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Part B.
CPSC 504: Data Management Discussion on Chandra&Merlin 1977 Laks V.S. Lakshmanan Dept. of CS UBC.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
1 541: Relational Calculus. 2 Relational Calculus  Comes in two flavours: Tuple relational calculus (TRC) and Domain relational calculus (DRC).  Calculus.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Winter 2002Arthur Keller – CS 18014–1 Schedule Today: Feb. 26 (T) u Datalog. u Read Sections Assignment 6 due. Feb. 28 (TH) u Datalog and SQL.
CSE 636 Data Integration Datalog Rules / Programs / Negation Slides by Jeffrey D. Ullman.
Local-as-View Mediators Priya Gangaraju(Class Id:203)
Embedded SQL Direct SQL is rarely used: usually, SQL is embedded in some application code. We need some method to reference SQL statements. But: there.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Credit: Slides are an adaptation of slides from Jeffrey D. Ullman 1.
Deductive Databases Chapter 25
Rutgers University Relational Calculus 198:541 Rutgers University.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
The Relational Model: Relational Calculus
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Section 4.3.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Logical Query Languages Motivation: 1.Logical rules extend more naturally to recursive queries than does relational algebra. u Used in SQL recursion. 2.Logical.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4.
CSE 544 Relational Calculus Lecture #2 January 11 th, Dan Suciu , Winter 2011.
Datalog Inspired by the impedance mismatch in relational databases. Main expressive advantage: recursive queries. More convenient for analysis: papers.
CSE 636 Data Integration Conjunctive Queries Containment Mappings / Canonical Databases Slides by Jeffrey D. Ullman Fall 2006.
1 Relational Algebra and Calculas Chapter 4, Part A.
Datalog –Another query language –cleaner – closer to a “logic” notation, prolog – more convenient for analysis – can express queries that are not expressible.
Chapter 5 Notes. P. 189: Sets, Bags, and Lists To understand the distinction between sets, bags, and lists, remember that a set has unordered elements,
Row Types in SQL-3 Row types define types for tuples, and they can be nested. CREATE ROW TYPE AddressType{ street CHAR(50), city CHAR(25), zipcode CHAR(10)
Lu Chaojun, SJTU 1 Extended Relational Algebra. Bag Semantics A relation (in SQL, at least) is really a bag (or multiset). –It may contain the same tuple.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4, Part B.
1 Database Design: DBS CB, 2 nd Edition Relational Algebra: Basic Operations & Algebra of Bags Ch. 5.
Extensions of Datalog Wednesday, February 13, 2001.
1 Finite Model Theory Lecture 9 Logics and Complexity Classes (cont’d)
CS589 Principles of DB Systems Fall 2008 Lecture 4c: Query Language Equivalence Lois Delcambre
Relational Calculus Chapter 4, Section 4.3.
Relational Algebra & Calculus
Basic Operations Algebra of Bags
Datalog Rules / Programs / Negation Slides by Jeffrey D. Ullman
Relational Algebra Chapter 4 1.
Relational Calculus Chapter 4, Part B
Goal for this lecture Demonstrate how we can prove that one query language is more expressive than (i.e., “contained in” as described in the book) another.
Containment Mappings Canonical Databases Sariaya’s Algorithm
Semantics of Datalog With Negation
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
Cse 344 May 7th – Exam Review.
Cse 344 April 11th – Datalog.
CSE 344: Section 5 Datalog February 1st, 2018.
February 7th – Exam Review
Database Applications (15-415) Relational Calculus Lecture 6, September 6, 2016 Mohammad Hammoud.
Relational Algebra 1.
Lecture 10: Relational Algebra (continued), Datalog
April 6th – relational algebra
January 19th – Subqueries 2 and relational algebra
Relational Algebra Chapter 4 1.
Instructor: Mohamed Eltabakh
Basic Operations Algebra of Bags
Lecture 9: Relational Algebra (continued), Datalog
Chapter 2: Intro to Relational Model
Local-as-View Mediators
Logic Based Query Languages
Axiomatic Semantics Will consider axiomatic semantics (A.S.) of IMP:
Example of a Relation attributes (or columns) tuples (or rows)
Brief Introduction to Computational Logic
Chapter 2: Intro to Relational Model
Datalog Inspired by the impedance mismatch in relational databases.
Relational Calculus Chapter 4, Part B 7/1/2019.
Rules Programs Negation
Relational Calculus Chapter 4, Part B
Presentation transcript:

Cse 344 January 29th – Datalog

Administrative minutiae HW3 due Friday OQ due Wednesday HW4 out Wednesday Exam next Friday 3:30 - 5:00

What is Datalog? Another query language for relational model Designed in the 80’s Simple, concise, elegant Extends relational queries with recursion Relies on a logical framework for ”record” selection

Datalog: Facts and Rules Facts = tuples in the database Rules = queries Actor(id, fname, lname) Casts(pid, mid) Movie(id, name, year) Schema Datalog consists of facts and rules.

Datalog: Facts and Rules Facts = tuples in the database Rules = queries Actor(344759,‘Douglas’, ‘Fowley’). Casts(344759, 29851). Casts(355713, 29000). Movie(7909, ‘A Night in Armour’, 1910). Movie(29000, ‘Arizona’, 1940). Movie(29445, ‘Ave Maria’, 1940). Datalog consists of facts and rules.

Datalog: Facts and Rules Facts = tuples in the database Rules = queries Actor(344759,‘Douglas’, ‘Fowley’). Casts(344759, 29851). Casts(355713, 29000). Movie(7909, ‘A Night in Armour’, 1910). Movie(29000, ‘Arizona’, 1940). Movie(29445, ‘Ave Maria’, 1940). Q1(y) :- Movie(x,y,z), z=‘1940’. Q2(f, l) :- Actor(z,f,l), Casts(z,x), Movie(x,y,’1940’). Q3(f,l) :- Actor(z,f,l), Casts(z,x1), Movie(x1,y1,1910), Casts(z,x2), Movie(x2,y2,1940)

Datalog: Facts and Rules Facts = tuples in the database Rules = queries Actor(344759,‘Douglas’, ‘Fowley’). Casts(344759, 29851). Casts(355713, 29000). Movie(7909, ‘A Night in Armour’, 1910). Movie(29000, ‘Arizona’, 1940). Movie(29445, ‘Ave Maria’, 1940). Q1(y) :- Movie(x,y,z), z=‘1940’. Q2(f, l) :- Actor(z,f,l), Casts(z,x), Movie(x,y,’1940’). EDB (defined by facts), IDB (defined by rules) Q3(f,l) :- Actor(z,f,l), Casts(z,x1), Movie(x1,y1,1910), Casts(z,x2), Movie(x2,y2,1940) Extensional Database Predicates = EDB = Actor, Casts, Movie Intensional Database Predicates = IDB = Q1, Q2, Q3

Datalog: Terminology head body atom atom atom (aka subgoal) Q2(f, l) :- Actor(z,f,l), Casts(z,x), Movie(x,y,’1940’). Atoms are also called “subgoals”. Atoms are predicates: relational predicate (IDB or EDB) or arithmetic predicate. f, l = head variables x,y,z = existential variables

More Datalog Terminology Q(args) :- R1(args), R2(args), .... Ri(argsi) called an atom, or a relational predicate Ri(argsi) evaluates to true when relation Ri contains the tuple described by argsi. Example: Actor(344759, ‘Douglas’, ‘Fowley’) is true In addition we can also have arithmetic predicates Example: z > ‘1940’. Book uses AND instead of , Q(args) :- R1(args) AND R2(args) ....

Semantics of a Single Rule Meaning of a datalog rule = a logical statement ! Q1(y) :- Movie(x,y,z), z=‘1940’. For all x, y, z: if (x,y,z) ∈ Movies and z = ‘1940’ then y is in Q1 (i.e. is part of the answer) ∀x∀y∀z [(Movie(x,y,z) and z=‘1940’) ⇒ Q1(y)] Logically equivalent: ∀y [(∃x∃z Movie(x,y,z) and z=‘1940’) ⇒ Q1(y)] Thus, non-head variables are called "existential variables” We want the smallest set Q1 with this property (why?) Rule is of the form head :- body and is read “<head> if it is known that <body>”

Datalog program A datalog program consists of several rules Importantly, rules may be recursive! Usually there is one distinguished predicate that’s the output We will show an example first, then give the general semantics.

Example R encodes a graph 5 1 4 2 3 R= 1 2 3 4 5

Example T(x,y) :- R(x,y) T(x,y) :- R(x,z), T(z,y) R encodes a graph 5 What does it compute? T(x,y) :- R(x,y) T(x,y) :- R(x,z), T(z,y) 1 4 2 3 R= 1 2 3 4 5

Example T(x,y) :- R(x,y) T(x,y) :- R(x,z), T(z,y) R encodes a graph 5 What does it compute? T(x,y) :- R(x,y) T(x,y) :- R(x,z), T(z,y) 1 4 2 3 R= Initially: T is empty. 1 2 3 4 5

Example T(x,y) :- R(x,y) T(x,y) :- R(x,z), T(z,y) R encodes a graph 5 What does it compute? T(x,y) :- R(x,y) T(x,y) :- R(x,z), T(z,y) 1 4 2 3 First iteration: T = R= Initially: T is empty. 1 2 3 4 5 1 2 3 4 5 First rule generates this Second rule generates nothing (because T is empty)

Example T(x,y) :- R(x,y) T(x,y) :- R(x,z), T(z,y) R encodes a graph 5 What does it compute? T(x,y) :- R(x,y) T(x,y) :- R(x,z), T(z,y) 1 4 2 3 Second iteration: T = First iteration: T = R= Initially: T is empty. 1 2 3 4 5 1 2 3 4 5 First rule generates this 1 2 3 4 5 Second rule generates this New facts

Example T(x,y) :- R(x,y) T(x,y) :- R(x,z), T(z,y) R encodes a graph 5 What does it compute? T(x,y) :- R(x,y) T(x,y) :- R(x,z), T(z,y) 1 4 2 Third iteration: T = 3 Second iteration: T = First iteration: T = R= 1 2 3 4 5 Both rules Initially: T is empty. 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 First rule Second rule New fact

Example T(x,y) :- R(x,y) T(x,y) :- R(x,z), T(z,y) R encodes a graph 5 What does it compute? T(x,y) :- R(x,y) T(x,y) :- R(x,z), T(z,y) 1 4 2 Third iteration: T = 3 Second iteration: T = First iteration: T = R= 1 2 3 4 5 Fourth iteration T = (same) Initially: T is empty. 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 No new facts. DONE

Datalog Semantics Fixpoint semantics Start: IDB0 = empty relations t = 0 Repeat: IDBt+1 = Compute Rules(EDB, IDBt) t = t+1 Until IDBt = IDBt-1 Remark: since rules are monotone: ∅ = IDB0 ⊆IDB1 ⊆ IDB2 ⊆ ... It follows that a datalog program w/o functions (+, *, ...) always terminates. (Why? In what time?)

Datalog Semantics Minimal model semantics: Return the IDB that For every rule, ∀vars [(Body(EDB,IDB) ⇒ Head(IDB)] Is the smallest IDB satisfying (1) Theorem: there exists a smallest IDB satisfying (1)

Datalog Semantics The fixpoint semantics tells us how to compute a datalog query The minimal model semantics is more declarative: only says what we get The two semantics are equivalent meaning: you get the same thing

Three Equivalent Programs R encodes a graph 5 T(x,y) :- R(x,y) T(x,y) :- R(x,z), T(z,y) Right linear 1 4 2 3 T(x,y) :- R(x,y) T(x,y) :- T(x,z), R(z,y) Left linear R= 1 2 3 4 5 T(x,y) :- R(x,y) T(x,y) :- T(x,z), T(z,y) Non-linear

Safe Datalog Rules U1(x,y) :- ParentChild(“Alice”,x), y != “Bob” ParentChild(p,c) Safe Datalog Rules Here are unsafe datalog rules. What’s “unsafe” about them ? U1(x,y) :- ParentChild(“Alice”,x), y != “Bob” U2(x) :- ParentChild(“Alice”,x), !ParentChild(x,y) These rules generate infinite numbers of answers! not Casts(u, x) is not safe because there is an infinite number of u’s that makes it true, so we don’t know when to stop evaluation! Stopped here Winter 2017 Compare this with SQL: every tuple variable is bound to a relation, so we will only iterate through each tuple in the bounded relation and nothing more But in RC / datalog, the variables are not explicitly bounded

Holds for every y other than “Bob” U1 = infinite! ParentChild(p,c) Safe Datalog Rules Here are unsafe datalog rules. What’s “unsafe” about them ? Holds for every y other than “Bob” U1 = infinite! U1(x,y) :- ParentChild(“Alice”,x), y != “Bob” U2(x) :- ParentChild(“Alice”,x), !ParentChild(x,y) These rules generate infinite numbers of answers! not Casts(u, x) is not safe because there is an infinite number of u’s that makes it true, so we don’t know when to stop evaluation! Stopped here Winter 2017 Compare this with SQL: every tuple variable is bound to a relation, so we will only iterate through each tuple in the bounded relation and nothing more But in RC / datalog, the variables are not explicitly bounded

Holds for every y other than “Bob” U1 = infinite! ParentChild(p,c) Safe Datalog Rules Here are unsafe datalog rules. What’s “unsafe” about them ? Holds for every y other than “Bob” U1 = infinite! U1(x,y) :- ParentChild(“Alice”,x), y != “Bob” U2(x) :- ParentChild(“Alice”,x), !ParentChild(x,y) Want Alice’s childless children, but we get all children x (because there exists some y that x is not parent of y) These rules generate infinite numbers of answers! not Casts(u, x) is not safe because there is an infinite number of u’s that makes it true, so we don’t know when to stop evaluation! Stopped here Winter 2017 Compare this with SQL: every tuple variable is bound to a relation, so we will only iterate through each tuple in the bounded relation and nothing more But in RC / datalog, the variables are not explicitly bounded

Holds for every y other than “Bob” U1 = infinite! ParentChild(p,c) Safe Datalog Rules Here are unsafe datalog rules. What’s “unsafe” about them ? Holds for every y other than “Bob” U1 = infinite! U1(x,y) :- ParentChild(“Alice”,x), y != “Bob” U2(x) :- ParentChild(“Alice”,x), !ParentChild(x,y) Want Alice’s childless children, but we get all children x (because there exists some y that x is not parent of y) These rules generate infinite numbers of answers! not Casts(u, x) is not safe because there is an infinite number of u’s that makes it true, so we don’t know when to stop evaluation! Stopped here Winter 2017 Compare this with SQL: every tuple variable is bound to a relation, so we will only iterate through each tuple in the bounded relation and nothing more But in RC / datalog, the variables are not explicitly bounded A datalog rule is safe if every variable appears in some positive relational atom

Datalog: relational database Datalog can express things RA cannot Recursive Queries Can Datalog express all queries in RA?

Datalog: relational database Datalog can express things RA cannot Recursive Queries Can Datalog express all queries in RA?

Relational Algebra Operators Union ∪, difference - Selection σ Projection π Cartesian product X, join ⨝

operators in Datalog Suppose we want Q1(…) to contain all the values from F1(…) and F2(…)

operators in Datalog Suppose we want Q1(…) to contain all the values from F1(…) and F2(…) Q1(…) :- F1(…) Q1(…) :- F2(…) What about for difference?

operators in Datalog Suppose we want Q1(…) to contain all the values from F1(…) and F2(…) Q1(…) :- F1(…) Q1(…) :- F2(…) What about for difference? Q1(…) :- F1(…), !F2(…)

operators in Datalog Suppose we want Q1(…) to contain all the values from F1(…) and F2(…) Q1(…) :- F1(…) Q1(…) :- F2(…) What about for difference? Q1(…) :- F1(…), !F2(…) The variables (…) in F1 and F2 must be the same, or else we have an unsafe rule

operators in Datalog Projection, from the variables R1,R2,...Rk select some subset of the variables

operators in Datalog Projection, from the variables R1,R2,...Rk select some subset of the variables Q1(subset) :- Original(all_attributes) Selection: only return certain records from our knowledge base

operators in Datalog Projection, from the variables R1,R2,...Rk select some subset of the variables Q1(subset) :- Original(all_attributes) Selection: only return certain records from our knowledge base Q1(…) :- Original(…), selection_criteria

operators in Datalog Cross product: find all the pairs between R(a1,a2…) and S(b1,b2…)

operators in Datalog Cross product: find all the pairs between R(a1,a2…) and S(b1,b2…) Q1(a1,b1,a2,b2…) :- R(a1,a2…), S(b1,b2…) Joins? Natural

operators in Datalog Cross product: find all the pairs between R(a1,a2…) and S(b1,b2…) Q1(a1,b1,a2,b2…) :- R(a1,a2…), S(b1,b2…) Joins? Natural: Q1(a,b,c) :- R(a,b), S(b,c) Theta

operators in Datalog Cross product: find all the pairs between R(a1,a2…) and S(b1,b2…) Q1(a1,b1,a2,b2…) :- R(a1,a2…), S(b1,b2…) Joins? Natural: Q1(a,b,c) :- R(a,b), S(b,c) Theta: Cross product with selection Equijoin: subset of Theta join

Example − (SELECT Q.sno FROM Supplier Q WHERE Q.sstate = ‘WA’) Supplier(sno,sname,scity,sstate) Part(pno,pname,psize,pcolor) Supply(sno,pno,price) (SELECT Q.sno FROM Supplier Q WHERE Q.sstate = ‘WA’) EXCEPT (SELECT P.sno FROM Supply P WHERE P.price > 100) − πsno πsno σsstate=‘WA’ σPrice > 100 Supplier Supply

Example − Datalog: πsno πsno σsstate=‘WA’ σPrice > 100 Supplier Supplier(sno,sname,scity,sstate) Part(pno,pname,psize,pcolor) Supply(sno,pno,price) Datalog: − πsno πsno σsstate=‘WA’ σPrice > 100 Supplier Supply

Example − Datalog: Q1(no,name,city,state) :- Supplier(sno,sname, Supplier(sno,sname,scity,sstate) Part(pno,pname,psize,pcolor) Supply(sno,pno,price) Datalog: Q1(no,name,city,state) :- Supplier(sno,sname, scity,sstate), sstate=’WA’ Q2(no,pno,price) :- Supply(s,pn,pr), pr > 100 Q3(sno) :- Q1(sno,n,c,s) Q4(sno) :- Q2(sno,pn,pr) Result(sno) :- Q1(sno), !Q2(sno) − πsno πsno σsstate=‘WA’ σPrice > 100 Supplier Supply

More Examples w/o Recursion Friend(name1, name2) Enemy(name1, name2) Find Joe's friends, and Joe's friends of friends. A(x) :- Friend('Joe', x) A(x) :- Friend('Joe', z), Friend(z, x)

More Examples w/o Recursion Find all of Joe's friends who do not have any friends except for Joe: JoeFriends(x) :- Friend('Joe',x) NonAns(x) :- JoeFriends(x), Friend(x,y), y != ‘Joe’ A(x) :- JoeFriends(x), NOT NonAns(x)

More Examples w/o Recursion Find all people such that all their enemies' enemies are their friends Q: if someone doesn't have any enemies nor friends, do we want them in the answer? A: Yes! Everyone(x) :- Friend(x,y) Everyone(x) :- Friend(y,x) Everyone(x) :- Enemy(x,y) Everyone(x) :- Enemy(y,x) NonAns(x) :- Enemy(x,y),Enemy(y,z), NOT Friend(x,z) A(x) :- Everyone(x), NOT NonAns(x)

More Examples w/o Recursion Find all persons x that have a friend all of whose enemies are x's enemies. Everyone(x) :- Friend(x,y) NonAns(x) :- Friend(x,y) Enemy(y,z), NOT Enemy(x,z) A(x) :- Everyone(x), NOT NonAns(x)

More Examples w/ Recursion ParentChild(p,c) More Examples w/ Recursion Two people are in the same generation if they are siblings, or if they have parents in the same generation Find all persons in the same generation with Alice

More Examples w/ Recursion ParentChild(p,c) More Examples w/ Recursion Find all persons in the same generation with Alice Let’s compute SG(x,y) = “x,y are in the same generation” SG(x,y) :- ParentChild(p,x), ParentChild(p,y) SG(x,y) :- ParentChild(p,x), ParentChild(q,y), SG(p,q) Answer(x) :- SG(“Alice”, x)

Datalog Summary EDB (base relations) and IDB (derived relations) Datalog program = set of rules Datalog is recursive Some reminders about semantics: Multiple atoms in a rule mean join (or intersection) Variables with the same name are join variables Multiple rules with same head mean union