1 Recursive SQL, Deductive Databases, Query Evaluation Book Chapter of Ramankrishnan and Gehrke DBMS Systems, 3 rd ed.

Slides:



Advertisements
Similar presentations
Chapter 5: Other Relational Languages
Advertisements

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.
Relational Calculus and Datalog
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.
Ver 1,12/09/2012Kode :CCs 111,Sistem basis DataFASILKOM Chapter 5: Other Relational Languages Database System Concepts, 5th Ed. ©Silberschatz, Korth and.
1 541: Relational Calculus. 2 Relational Calculus  Comes in two flavours: Tuple relational calculus (TRC) and Domain relational calculus (DRC).  Calculus.
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.
Web Science & Technologies University of Koblenz ▪ Landau, Germany Implementation Techniques Acknowledgements to Angele, Gehrke, STI.
CS 286, UC Berkeley, Spring 2007, R. Ramakrishnan 1 Evaluation of Recursive Queries Part 2: Pushing Selections.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Berendt: Advanced databases, winter term 2007/08, 1 Advanced databases – Inferring implicit/new.
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.
SECTIONS 21.4 – 21.5 Sanuja Dabade & Eilbroun Benjamin CS 257 – Dr. TY Lin INFORMATION INTEGRATION.
CSE 636 Data Integration Datalog Rules / Programs / Negation Slides by Jeffrey D. Ullman.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Rutgers University Relational Algebra 198:541 Rutgers University.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
Cs5611 Recursive SQL, Deductive Databases, Query Evaluation Slides based on book chapter, By Ramankrishnan and Gehrke DBMS Systems, 3 rd ed.
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,
Rutgers University Relational Calculus 198:541 Rutgers University.
CSCD343- Introduction to databases- A. Vaisman1 Relational Algebra.
Relational Algebra.
Relational Algebra, R. Ramakrishnan and J. Gehrke (with additions by Ch. Eick) 1 Relational Algebra.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
Solving fixpoint equations
The Relational Model: Relational Calculus
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Section 4.3.
Query Optimization (CB Chapter ) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems: An Application Oriented.
Lecture 05 Structured Query Language. 2 Father of Relational Model Edgar F. Codd ( ) PhD from U. of Michigan, Ann Arbor Received Turing Award.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4.
1 Relational Algebra. 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of data from a database. v Relational model supports.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
Datalog Inspired by the impedance mismatch in relational databases. Main expressive advantage: recursive queries. More convenient for analysis: papers.
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1 Relational Algebra and Calculas Chapter 4, Part A.
Relational Algebra.
ICS 321 Fall 2011 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 8/29/20111Lipyeow.
1 Relational Algebra Chapter 4, Sections 4.1 – 4.2.
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,
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
CSCD34-Data Management Systems - A. Vaisman1 Relational Algebra.
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
Computing & Information Sciences Kansas State University Wednesday, 17 Sep 2008CIS 560: Database System Concepts Lecture 9 of 42 Wednesday, 18 September.
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.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B.
Relational Algebra p BIT DBMS II.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4, Part B.
SQL and Query Execution for Aggregation. Example Instances Reserves Sailors Boats.
CS589 Principles of DB Systems Fall 2008 Lecture 4d: Recursive Datalog with Negation – What is the query answer defined to be? Lois Delcambre
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
Extensions of Datalog Wednesday, February 13, 2001.
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.
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
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
SQL: Structured Query Language
Logic Based Query Languages
Datalog Inspired by the impedance mismatch in relational databases.
Presentation transcript:

1 Recursive SQL, Deductive Databases, Query Evaluation Book Chapter of Ramankrishnan and Gehrke DBMS Systems, 3 rd ed.

2 Motivation Can SQL-92 express queries: –Are we running low on any parts needed to build a ZX600 sports car? –What is total component and assembly cost to build ZX600 at today's part prices? Can we extend the query language to cover such queries? –Yes, by adding recursion.

3 Towards Semantics : Datalog SQL queries can be read as follows: “If some tuples exist in From tables that satisfy Where conditions, then Select tuple is in answer.” Datalog is query language with same if-then flavor: –New: Answer table can appear in From clause, i.e., be defined recursively. –Prolog style syntax is commonly used.

4 Example Assembly instance part subpart number trike wheel frame spoke tire seat pedal rim tube

5 Example Find components of “trike”. Can you write relational algebra query to compute answer on the given instance of Assembly? Assembly instance part subpart number trike wheel frame spoke tire seat pedal rim tube

6 Example Find components of ‘trike’ There is no R.A. (or SQL- 92) query that computes answer on all Assembly instances. Assembly instance part subpart number trike wheel frame spoke tire seat pedal rim tube

7 Example Find the components of a trike? We can write a relational algebra query to compute the answer on the given instance of Assembly. But there is no R.A. (or SQL-92) query that computes the answer on all Assembly instances. Assembly instance part subpart number trike wheel frame spoke tire seat pedal rim tube

8 Problem with R.A. and SQL-92 Intuitively, we must join Assembly with itself to deduce that trike contains spoke and tire. –Takes us one level down Assembly hierarchy. –To find components that are one level deeper (e.g., rim), need another join. –To find all components, need as many joins as there are levels in the given instance! For any RA expression, we can create an Assembly instance for which some answers are not computed by including more levels than number of joins in expression!

9 Datalog Query that Does the Job Comp(Part, Subpt) :- Assembly(Part, Subpt, Qty). Comp(Part, Subpt) :- Assembly(Part, Part2, Qty), Comp(Part2, Subpt). Can read second rule as follows: “ For all values of Part, Subpt and Qty, if there is a tuple (Part, Part2, Qty) in Assembly and a tuple (Part2, Subpt) in Comp, then there must be a tuple (Part, Subpt) in Comp.” head of rulebody of ruleimplication

10 Datalog Datalog : Relational QL inspired by prolog Program : a collection of rules Rule : if RHS exists, must be in LHS result.

11 Using Rule to Deduce New Tuples Each rule is a template for making inferences: by assigning constants to variables so that each body “literal” is tuple in corresponding relation, we identify tuple that must be in head relation. Ex: Comp(Part, Subpt) :- Assembly(Part, Subpt, Qty). –By setting (Part=trike, Subpt=wheel, Qty=3) in rule, we deduce that tuple is in relation Comp. –This is called an inference using the rule. –Given a set of tuples, we apply rule by making all possible inferences with tuples in body.

12 Example For any instance of Assembly, we compute all Comp tuples by repeatedly applying two rules. Actually: we can apply Rule 1 just once, then apply Rule 2 repeatedly. Rule 1 ~ projection Rule2 ~ cross-product with equality join Comp(Part, Subpt) :- Assembly(Part, Subpt, Qty). Comp(Part, Subpt) :- Assembly(Part, Part2, Qty), Comp(Part2, Subpt).

13 Comp tuples by applying Rule 2 twice Comp tuples by applying Rule 2 once Assembly instance Comp(Part, Subpt) :- Assembly(Part, Part2, Qty), Comp(Part2, Subpt).

14 Example For any instance of Assembly, we can compute all Comp tuples by repeatedly applying the two rules. (Actually, we can apply Rule 1 just once, then apply Rule 2 repeatedly.) Comp tuples got by applying Rule 2 twice Comp tuples got by applying Rule 2 once

15 Datalog vs. SQL Notation A collection of Datalog rules can be rewritten in SQL syntax with recursion WITH RECURSIVE Comp(Part, Subpt) AS ( SELECT A1.Part, A1.Subpt FROM Assembly A1) UNION ( SELECT A2.Part, C1.Subpt FROM Assembly A2, Comp C1 WHERE A2.Subpt=C1.Part) SELECT * FROM Comp C2

16 Datalog vs. SQL Notation Or, modify query to have selection: WITH RECURSIVE Comp(Part, Subpt) AS ( SELECT A1.Part, A1.Subpt FROM Assembly A1) UNION ( SELECT A2.Part, C1.Subpt FROM Assembly A2, Comp C1 WHERE A2.Subpt=C1.Part) SELECT * FROM Comp C2 Where C2.part = trike.

17 Theoretical Foundations (least fixpoint semantics – conceptual evaluation strategy a la relational algebra )

18 Fixpoint Let f be a function that takes values from domain D and returns values from D. A value v in D is a fixpoint of f if f(v)=v.

19 Fixpoints Consider function double+ on integers E.g., double+({1,2,5}) = {2,4,10} Union {1,2,5} What are example fixpoints for double+?

20 Fixpoints Function double+ : double+({1,2,5})={2,4,10} Union {1,2,5} Example Fixpoints (input sets): –The set of all integers is a fixpoint of double+. –The set of all even integers is another fixpoint –The set of integer zero is another fixpoint.

21 Least Fixpoint Semantics Least fixpoint of a function f is a fixpoint v of f such that every other fixpoint of f is larger than or equal to v. There may be no (one) least fixpoint We could have two minimal fixpoints, neither of which is smaller than other. Least fixpoint of double+?

22 Least Fixpoint Semantics for Datalog Datalog ~~ function defined by relational algebra (without set-difference). Datalog applied to set of tuples and returns another set of tuples: It always has least fixpoint !!

23 Least Fixpoint Semantics for Datalog Comp = PROJECT [1,5] (PROJECT[1,2] (Assembly) UNION (Assembly JOIN[2=1] Comp) ) with Comp = function (Comp, Assembly) defined by RA expression. Least Fixpoint ~ Is instance of Comp that satisfies this query (query answer).

24 Least Fixpoint Semantics for Datalog The least fixpoint of a function f is a fixpoint v of f such that every other fixpoint of f is smaller than or equal to v. Eg., Big depends on Small table. In general, there may be no least fixpoint (we could have two minimal fixpoints, neither of which is smaller than the other). If we think of a Datalog program as a function that is applied to a set of tuples and returns another set of tuples, this function (fortunately!) always has a least fixpoint.

25 Unsafe/Safe Datalog Program If one unbound variable, then program is unsafe: Price-Parts (Part,Price) :- Assembly(Part, Subpart, Qty), Qty>2. Infinite number of different values for price would all make the rule correct. If least model of program is not finite, then program is unsafe. So all variables in head of rule must also appear in body (range-restricted).

26 Negation/Set-Difference Big(Part) :- Assembly(Part, Subpt, Qty), Qty >2, not Small(Part). Small(Part) :- Assembly(Part, Subpt, Qty), not Big(Part). What’s the problem below? If rules contain not, then there may not be a least fixpoint.

27 Negation One least fixpoint? Consider Assembly instance –What is intuitive answer? – trike is the only part that has 3 or more copies of some subpart. Intuitively, it should be in Big()! –If we apply Rule 1 first, we have Big(trike). –If we apply Rule 2 first, we have Small (trike). –Which one is right answer? Big(Part) :- Assembly(Part, Subpt, Qty), Qty >2, not Small(Part). Small(Part) :- Assembly(Part, Subpt, Qty), not Big(Part).

28 Negation If rules contain not, then there may not be a least fixpoint. Order of applying rules determines answer because : –Addition of tuples into one output relation may disallow inference of other tuples Need method to choose intended fixpoint. Big(Part) :- Assembly(Part, Subpt, Qty), Qty >2, not Small(Part). Small(Part) :- Assembly(Part, Subpt, Qty), not Big(Part).

29 Negation If rules contain not there may not be a least fixpoint. Consider the Assembly instance; trike is the only part that has 3 or more copies of some subpart. Intuitively, it should be in Big, and it will be if we apply Rule 1 first. –But we have Small(trike) if Rule 2 is applied first! –There are two minimal fixpoints for this program: Big is empty in one, and contains trike in the other (and all other parts are in Small in both fixpoints). Need a way to choose the intended fixpoint. Big(Part) :- Assembly(Part, Subpt, Qty), Qty >2, not Small(Part). Small(Part) :- Assembly(Part, Subpt, Qty), not Big(Part).

30 “NOT” in Body? Range-restricted program : every variable in head of rule appears in some positive (non-negated) relation occurrence in body.

31 Stratification T depends on S if some rule with T in the head contains S or (recursively) some predicate that depends on S, in the body. Example: Big depends on Small. Stratified program: If T depends on not S, then S cannot depend on T (or not T).

32 Stratification If program is stratified, tables in program can be partitioned into strata (fully order dependencies): –Stratum 0: All database tables. –Stratum I: Tables defined in terms of tables in Stratum I and lower strata. (1) If T depends on not S, S is in lower stratum than T. (2) Or, table in stratum I depends negatively only on tables in stratum I-1.

33 Stratification Question : Is Big/Small program stratified? Big/Small: Mutually recursive tables

34 Fixpoint Semantics for Stratified Programs Semantics of stratified program given by one of its minimal fixpoints. This fixpoint identified by operational definition: –Stratum 0 tables are fixed –First compute least fixpoint of all tables in Stratum 1. –Then, compute least fixpoint of tables in Stratum 2. –Then, compute least fixpoint of tables in Stratum 3, and so on, stratum-by-stratum.

35 Fixpoint Semantics for Stratified Programs This evaluation strategy is guaranteed to find one minimal fixpoint (but several may exist). RA : corresponds to range-restricted stratified datalog. SQL3 requires stratified programs.

36 Aggregate Operators The notation in head indicates grouping; remaining arguments (Part, in this example) are GROUP BY fields. In order to apply such rule, must have all of Assembly relation available. (not on partial computed relation). Stratification with respect to use of is restriction to deal with this problem; similar to negation. NumParts(Part, SUM ( )) :- Assembly(Part, Subpt, Qty). SELECT A.Part, SUM (A.Qty) FROM Assembly A GROUP BY A.Part

37 Query Optimization

38 Evaluation of Datalog Programs Avoid Repeated inferences: Avoid Unnecessary inferences:

39 Query Optimization #1.

40 Evaluation of Datalog Programs Avoid Repeated inferences: When recursive rules are repeatedly applied in naïve way, we make same inferences in several iterations.

41 Comp tuples by applying Rule 2 twice Comp tuples by applying Rule 2 once Assembly instance Comp(Part, Subpt) :- Assembly(Part, Part2, Qty), Comp(Part2, Subpt).

42 Avoiding Repeated Inferences Semi-naive Fixpoint Evaluation: Ensure that when rule is applied, at least one of body facts used was generated in most recent iteration. Such new inference could not have been carried out in earlier iterations.

43 Avoiding Repeated Inferences Idea: For each recursive table P, use table delta_P to store P tuples generated in previous iteration. –1. Rewrite program to use delta tables –2. Update delta tables between iterations. Comp(Part, Subpt) :- Assembly(Part, Part2, Qty), delta_Comp(Part2, Subpt). Comp(Part, Subpt) :- Assembly(Part, Part2, Qty), Comp(Part2, Subpt).

44 Query Optimization #2.

45 Avoiding Unnecessary Inferences WITH RECURSIVE Comp(Part, Subpt) AS ( SELECT A1.Part, A1.Subpt FROM Assembly A1) UNION ( SELECT A2.Part, C1.Subpt FROM Assembly A2, Comp C1 WHERE A2.Subpt=C1.Part) SELECT * FROM Comp C2 Where C2.part = trike.

46 Evaluation of Datalog Programs Unnecessary inferences: If we just want to find components of a particular part, say wheel, then first computing general fixpoint of Comp program and then at end selecting tuples with wheel in the first column is wasteful. This would compute many irrelevant facts.

47 Avoiding Unnecessary Inferences Semantics? SameLev(S1,S2) :- Assembly(P1,S1,Q1), Assembly(P1,S2,Q2). SameLev(S1,S2) :- Assembly(P1,S1,Q1), SameLev(P1,P2), Assembly(P2,S2,Q2). trike wheel frame spoke tire seat pedal rim tube

48 Avoiding Unnecessary Inferences Tuple (S1,S2) in SameLev if there is path up from S1 to some node and down to S2 with same number of up and down edges. SameLev(S1,S2) :- Assembly(P1,S1,Q1), Assembly(P1,S2,Q2). SameLev(S1,S2) :- Assembly(P1,S1,Q1), SameLev(P1,P2), Assembly(P2,S2,Q2). trike wheel frame spoke tire seat pedal rim tube

49 Avoiding Unnecessary Inferences Want all SameLev tuples with spoke in first column. Intuition: “Push” this selection into fixpoint computation. How do that? SameLev(S1,S2) :- Assembly(P1,S1,Q1), SameLev(P1,P2), Assembly(P2,S2,Q2). SameLev(spoke,S2) :- Assembly(P1,spoke,Q1), SameLev(P1?=spoke?,P2), Assembly(P2,S2,Q2).

50 Avoiding Unnecessary Inferences Intuition: “Push” this selection with spoke into fixpoint computation. SameLev(S1,S2) :- Assembly(P1,S1,Q1), SameLev(P1,P2), Assembly(P2,S2,Q2). SameLev(spoke,S2) :- Assembly(P1,spoke,Q1), SameLev(P1,P2), Assembly(P2,S2,Q2). SameLev(spoke,seat) :- Assembly(wheel,spoke,2), SameLev(wheel,frame), Assembly(frame,seat,1). Other SameLev tuples are needed to compute all such tuples with spoke, e.g. wheel

51 “Magic Sets” Idea 1. Define “filter” table that computes all relevant values 2. Restrict computation of SameLev to infer only tuples with relevant value in first column.

52 Intuition Relevant values: contains all tuples m for which we have to compute all same-level tuples with m in first column to answer query. Put differently, relevant values are all Same- Level tuples whose first field contains value on path from spoke up to root. We call it Magic-SameLevel (Magic-SL)

53 “Magic Sets” in Example Idea: Define “filter” table that computes all relevant values : Collect all parents of spoke. Magic_SL(P1) :- Magic_SL(S1), Assembly(P1,S1,Q1). Magic_SL(spoke) :-. Make Magic table as Magic-SameLevel.

54 “Magic Sets” Idea Idea: Use “filter” table to restrict the computation of SameLev. Magic_SL(P1) :- Magic_SL(S1), Assembly(P1,S1,Q1). Magic(spoke). SameLev(S1,S2) :- Magic_SL(S1), Assembly(P1,S1,Q1), Assembly(P1,S2,Q2). SameLev(S1,S2) :- Magic_SL(S1), Assembly(P1,S1,Q1), SameLev(P1,P2), Assembly(P2,S2,Q2).

55 “Magic Sets” Idea Idea: Define “filter” table that computes all relevant values, and restrict the computation of SameLev correspondingly. Magic_SL(P1) :- Magic_SL(S1), Assembly(P1,S1,Q1). Magic(spoke). SameLev(S1,S2) :- Magic_SL(S1), Assembly(P1,S1,Q1), Assembly(P1,S2,Q2). SameLev(S1,S2) :- Magic_SL(S1), Assembly(P1,S1,Q1), SameLev(P1,P2), Assembly(P2,S2,Q2).

56 The Magic Sets Algorithm 1. Generate an “adorned” program –Program is rewritten to make pattern of bound and free arguments in query explicit 2. Add magic filters of form “Magic_P” –for each rule in adorned program add a Magic condition to body that acts as filter on set of tuples generated (predicate P to restrict these rules) 3. Define new rules to define filter tables –Define new rules to define filter tables of form Magic_P

57 Step 1:Generating Adorned Rules Adorned program for query pattern SameLev bf, assuming right-to-left order of rule evaluation : SameLev bf (S1,S2) :- Assembly(P1,S1,Q1), Assembly(P1,S2,Q2). SameLev bf (S1,S2) :- Assembly(P1,S1,Q1), SameLev bf (P1,P2), Assembly(P2,S2,Q2).  Argument of (a given body occurrence of) SameLev is:  b if it appears to the left in body,  or if it is a b argument of head of rule,  Otherwise it is free.  Assembly not adorned because explicitly stored table.

58 Step 1:Generating Adorned Rules Adorned program for query pattern SameLev bf, assuming right-to-left order of rule evaluation : SameLev bf (S1,S2) :- Assembly(P1,S1,Q1), Assembly(P1,S2,Q2). SameLev bf (S1,S2) :- Assembly(P1,S1,Q1), SameLev bf (P1,P2), Assembly(P2,S2,Q2).  Argument of (a given body occurrence of) SameLev is:  b if it appears to the left in body,  or if it is a b argument of head of rule,  Otherwise it is free.  Assembly not adorned because explicitly stored table.

59 Step 2: Add Magic Filters For every rule in adorned program add a ‘magic filter’ predicate SameLev bf (S1,S2) :- Magic_SL (S1), Assembly(P1,S1,Q1), Assembly(P1,S2,Q2). SameLev bf (S1,S2) :- Magic_SL (S1), Assembly(P1,S1,Q1), SameLev bf (P1,P2), Assembly(P2,S2,Q2). Filter predicate: copy of head of rule, Magic prefix, and delete free variable

60 Step 3:Defining Magic Tables Rule for Magic_P is generated from each occurrence of recursive P in body of rule: –Delete everything to right of P –Add prefix “Magic” and delete free columns of P –Move P, with these changes, into head of rule

61 Step 3:Defining Magic Table Rule for Magic_P is generated from each occurrence O of recursive P in body of rule: –Delete everything to right of P SameLev bf (S1,S2) :- Magic_SL(S1), Assembly(P1,S1,Q1), SameLev bf (P1,P2), Assembly(P2,S2,Q2). –Add prefix “Magic” and delete free columns of P Magic- SameLev bf (S1,S2) :- Magic_SL(S1), Assembly(P1,S1,Q1), Magic-SameLev bf (P1 ). –Move P, with these changes, into head of rule Magic_SL(P1) :- Magic_SL(S1), Assembly(P1,S1,Q1).

62 Step 3:Defining Magic Tables Rule for Magic_P is generated from each occurrence of P in body of such rule: SameLev bf (S1,S2) :- Magic_SL(S1), Assembly(P1,S1,Q1), SameLev bf (P1,P2), Assembly(P2,S2,Q2). Magic_SL(P1) :- Magic_SL(S1), Assembly(P1,S1,Q1).

63 “Magic Sets” Idea Define “filter” table that computes all relevant values: Restrict computation of SameLev Magic_SL(P1) :- Magic_SL(S1), Assembly(P1,S1,Q1). Magic(spoke). SameLev(S1,S2) :- Magic_SL(S1), Assembly(P1,S1,Q1), Assembly(P1,S2,Q2). SameLev(S1,S2) :- Magic_SL(S1), Assembly(P1,S1,Q1), SameLev(P1,P2), Assembly(P2,S2,Q2).

64 Summary Adding recursion extends relational algebra and SQL-92 in a fundamental way Recursion included in SQL:1999 Semantics based on iterative fixpoint evaluation. Programs with negation are restricted to be stratified to ensure semantics is intuitive and unambiguous. Evaluation must avoid repeated and unnecessary inferences. –“Semi-naive” fixpoint evaluation –“Magic Sets” query transformation