Credit: Slides are an adaptation of slides from Jeffrey D. Ullman 1.

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

Interprocedural Analysis. Currently, we only perform data-flow analysis on procedures one at a time. Such analyses are called intraprocedural analyses.
CSE 636 Data Integration Conjunctive Queries Containment Mappings / Canonical Databases Slides by Jeffrey D. Ullman.
1 Extended Conjunctive Queries Unions Arithmetic Negation.
Lecture 11: Datalog Tuesday, February 6, Outline Datalog syntax Examples Semantics: –Minimal model –Least fixpoint –They are equivalent Naive evaluation.
CPSC 504: Data Management Discussion on Chandra&Merlin 1977 Laks V.S. Lakshmanan Dept. of CS UBC.
1 Conjunctions of Queries. 2 Conjunctive Queries A conjunctive query is a single Datalog rule with only non-negated atoms in the body. (Note: No negated.
1 Introduction to SQL Select-From-Where Statements Multirelation Queries Subqueries.
SECTION 21.5 Eilbroun Benjamin CS 257 – Dr. TY Lin INFORMATION INTEGRATION.
1 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke Deductive Databases Chapter 25.
1 Datalog Logical Rules Recursion SQL-99 Recursion.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
1 9. Evaluation of Queries Query evaluation – Quantifier Elimination and Satisfiability Example: Logical Level: r   y 1,…y n  r’ Constraint.
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.
1 Datalog Logical Rules Recursion SQL-99 Recursion.
1 Datalog Applied to Software Security Vulnerabilities Datalog Data-Flow Analysis.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using Definite Knowledge Notes for Ch.3 of Poole et al. CSCE 580 Marco Valtorta.
1 Datalog Rules Programs Negation. 2 Review of Logical If-Then Rules h(X,…) :- a(Y,…) & b(Z,…) & … head body subgoals “The head is true if all the subgoals.
1 Semantics of Datalog With Negation Local Stratification Stable Models Well-Founded Models.
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.
Credit: Slides are an adaptation of slides from Jeffrey D. Ullman 1.
Logical Rules Recursion
1 XQuery Values FLWR Expressions Other Expressions.
1 Datalog Logical Rules Recursion. 2 Logic As a Query Language uIf-then logical rules have been used in many systems. wMost important today: EII (Enterprise.
Deductive Databases Chapter 25
CS 286, UC Berkeley, Spring 2007, R. Ramakrishnan 1 Introduction to Logic and Databases R. Ramakrishnan Database Management Systems, Gehrke- Ramakrishnan,
Defining Polynomials p 1 (n) is the bound on the length of an input pair p 2 (n) is the bound on the running time of f p 3 (n) is a bound on the number.
Database Systems Logical Query Languages assoc. prof., dr. Vladimir Dimitrov web: is.fmi.uni-sofia.bg.
Logical Query Languages Motivation: 1.Logical rules extend more naturally to recursive queries than does relational algebra. u Used in SQL recursion. 2.Logical.
Predicates and Quantifiers
Intro to Maths for CS: 2013/14 Sets (2) – OPTIONAL MATERIAL John Barnden Professor of Artificial Intelligence School of Computer Science University of.
DEDUCTIVE DATABASE.
Databases 1 8th lecture. Topics of the lecture Multivalued Dependencies Fourth Normal Form Datalog 2.
Recursive query plans for Data Integration Oliver Michael By Rajesh Kanisetti.
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.
Formal Semantics of Programming Languages 虞慧群 Topic 3: Principles of Induction.
1 Cluster Computing and Datalog Recursion Via Map-Reduce Seminaïve Evaluation Re-engineering Map-Reduce for Recursion.
Computing & Information Sciences Kansas State University Thursday, 08 Feb 2007CIS 560: Database System Concepts Lecture 11 of 42 Thursday, 08 February.
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.
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,
Computing & Information Sciences Kansas State University Wednesday, 17 Sep 2008CIS 560: Database System Concepts Lecture 9 of 42 Wednesday, 18 September.
Introduction to Finite Automata
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.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 27 A First Course in Database Systems.
Datalog Another formalism for expressing queries: - cleaner - closer to a “logic” notation - more convenient for analysis - equivalent in power to relational.
ICS 321 Fall 2011 Algebraic and Logical Query Languages (ii) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4, Part B.
Extensions of Datalog Wednesday, February 13, 2001.
1 Datalog with negation Adapted from slides by Jeff Ullman.
CS589 Principles of DB Systems Fall 2008 Lecture 4c: Query Language Equivalence Lois Delcambre
Relational Calculus Chapter 4, Section 4.3.
CS589 Principles of DB Systems Spring 2014 Unit 2: Recursive Query Processing Lecture 2-1 – Naïve algorithm for recursive queries Lois Delcambre (slides.
Datalog Rules / Programs / Negation Slides by Jeffrey D. Ullman
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.
Semantics of Datalog With Negation
Cse 344 January 29th – Datalog.
Motivation for Datalog
Lecture 9: Relational Algebra (continued), Datalog
Logic Based Query Languages
Datalog Inspired by the impedance mismatch in relational databases.
Rules Programs Negation
Relational Calculus Chapter 4, Part B
Presentation transcript:

Credit: Slides are an adaptation of slides from Jeffrey D. Ullman 1

2 Datalog Rules Programs Negation

3 Review of Logical If-Then Rules h(X,…) :- a(Y,…) & b(Z,…) & … head body subgoals “The head is true if all the subgoals are true.”

4 Terminology uHead and subgoals are atoms. uAn atom consists of a predicate (lower case) applied to zero or more arguments (upper case letters or constants).

5 Semantics uPredicates represent relations. uAn atom is true for given values of its variables iff the arguments form a tuple of the relation. uWhenever an assignment of values to all variables makes all subgoals true, the rule asserts that the resulting head is also true.

6 Example uWe shall develop rules that describe what is necessary to “make” a file. uThe predicates/relations:  source(F) = F is a “source” file.  includes(F,G) = F #includes G.  create(F,P,G) = F is created by applying process P to file G.

7 Example --- Continued uRules to define “view” req(X,Y) = file Y is required to create file X : req(F,F) :- source(F) req(F,G) :- includes(F,G) req(F,G) :- create(F,P,G) req(F,G) :- req(F,H) & req(H,G) G is required for F if there is some process P that creates F from G. G is required for F if there is some H such that H is required for F and G is required for H.

8 Why Not Just Use SQL? 1.Recursion is much easier to express in Datalog.  Viz. last rule for req. 2.Rules express things that go on in both FROM and WHERE clauses, and let us state some general principles (e.g., containment of rules) that are almost impossible to state correctly in SQL.

9 IDB/EDB uA predicate representing a stored relation is called EDB (extensional database). uA predicate representing a “view,” i.e., a defined relation that does not exist in the database is called IDB (intesional database). uHead is always IDB; subgoals may be IDB or EDB.

10 Datalog Programs uA collection of rules is a (Datalog) program. uEach program has a distinguished IDB predicate that represents the result of the program.  E.g., req in our example.

11 Extensions 1.Negated subgoals. 2.Constants as arguments. 3.Arithmetic subgoals.

12 Negated Subgoals uNOT in front of a subgoal means that an assignment of values to variables must make it false in order for the body to be true. uExample: cycle(F) :- req(F,F) & NOT source(F)

13 Constants as Arguments uWe use numbers, lower-case letters, or quoted strings to indicate a constant.  Example: req(“foo.c”, “stdio.h”) :- wNote that empty body is OK. wMixed constants and variables also OK.

14 Arithmetic Subgoals uComparisons like < may be thought of as infinite, binary relations. wHere, the set of all tuples (x,y) such that x<y. uUse infix notation for these predicates.  Example: composite(A) :- divides(B,A) & B > 1 & B != A

15 Evaluating Datalog Programs 1.Nonrecursive programs. 2.Naïve evaluation of recursive programs without IDB negation.

16 Safety uWhen we apply a rule to finite relations, we need to get a finite result. uSimple guarantee: safety = all variables appear in some nonnegated, relational (not arithmetic) subgoal of the body. wStart with the join of the nonnegated, relational subgoals and select/delete from there.

17 Examples: Nonsafety p(X) :- q(Y) bachelor(X) :- NOT married(X,Y) bachelor(X) :- person(X) & NOT married(X,Y) X is the problem Both X and Y are problems Y is still a problem

18 Nonrecursive Evaluation uIf (and only if!) a Datalog program is not recursive, then we can order the IDB predicates so that in any rule for p (i.e., p is the head predicate), the only IDB predicates in the body precede p.

19 Why? uConsider the dependency graph with: wNodes = IDB predicates. wArc p -> q iff there is a rule for p with q in the body. uCycle involving node p means p is recursive. uNo cycles: use topological order to evaluate predicates.

20 Applying Rules uTo evaluate an IDB predicate p : 1.Apply each rule for p to the current relations corresponding to its subgoals. u“Apply” = If an assignment of values to variables makes the body true, insert the tuple that the head becomes into the relation for p (no duplicates). 2.Take the union of the result for each p- rule.

21 Example p(X,Y) :- q(X,Z) & r(Z,Y) & Y<10 Q = {(1,2), (3,4)}; R = {(2,5), (4,9), (4,10), (6,7)} uWhich assignments making the body true? uSo, what does P have?

22 Algorithm for Nonrecursive FOR (each predicate p in topological order) DO apply the rules for p to previously computed relations to compute relation P for p;

23 Naïve Evaluation for Recursive make all IDB relations empty; WHILE (changes to IDB) DO FOR (each IDB predicate p) DO evaluate p using current values of all relations;

24 Important Points uAs long as there is no negation of IDB subgoals, then each IDB relation “grows,” i.e., on each round it contains at least what it used to contain. uSince relations are finite, the loop must eventually terminate. uResult is the least fixedpoint (minimal model ) of rules.

Test Yourself uGiven predicates: wearn(P,S), worksIn(P,D), loc(D,C) u Formulate queries that find: w People who earn 1000 dollars w People who work in NY + name of their department w People who work in Toy and Textile dept. w People who work in Toy or Textile dept. 25

Test Yourself uHow does Datalog compare to SQL? uHow does Datalog compare to Relational Algebra? 26