Download presentation
Presentation is loading. Please wait.
Published byGeorgiana White Modified over 9 years ago
1
CS240A: Databases and Knowledge Bases Recursive Queries in SQL 1999 Carlo Zaniolo Department of Computer Science University of California, Los Angeles December, 2001 Notes From Chapter 9 of Advanced Database Systems by Zaniolo, Ceri, Faloutsos, Snodgrass, Subrahmanian and Zicari Morgan Kaufmann, 1997
2
The Part/Subparts Example CREATE RECURSIVE VIEW all_subparts(Major, Minor) AS SELECT PART SUBPART FROM assembly UNION SELECT all.Major assb.SUBPART FROM all_subparts AS all, assembly AS assb WHERE all.Minor= assb.PART This is called a recursive union. We have the union of an Exit Select and a Recursive Select This definition corresponds to left-linear recursive Datalog rules Materialization of the recursive view from the previous Example SELECT * FROM all_subparts
3
The WITH Construct The WITH construct provides another way, and a more direct one to express recursion in SQL:1999. Query: Find the parts using top tube WITH RECURSIVE all_super(Major, Minor) AS (SELECT PART, SUBPART FROM assembly UNION SELECT assb.PART, all.Minor FROM assembly AS assb, all_super AS all WHERE assb.SUBPART = all.Major) SELECT * WHERE Minor = 'top_tube‘ this corresponds to a right-linear Datalog rule
4
Implementation CREATE RECURSIVE VIEW all_subparts(Major, Minor) AS SELECT PART SUBPART FROM assembly UNION SELECT all.Major assb.SUBPART FROM all_subparts AS all, assembly AS assb WHERE all.Minor= assb.PART To implement the differential fixpoint improvement we replace the recursive relation all_subparts in the FROM clause by all_subparts, where all_subparts contains the new tuples generated in the previous iteration of differential fixpoint Algorithm. (But also elimitate duplicates as they are generated)
5
Find the parts using top tube (on RightLinear Recursion) WITH RECURSIVE all_super(Major, Minor) AS (SELECT PART, SUBPART FROM assembly UNION SELECT assb.PART, all.Minor FROM assembly AS assb, all_super AS all WHERE assb.SUBPART = all.Major) SELECT * FROM all_subparts WHERE Minor = 'top tube‘ Add Minor= 'top tube', to the WHERE clauses in exit select and the recursive select : WITH RECURSIVE all_super(Major, Minor) AS (SELECT PART, SUBPART FROM assembly WHERE SUBPART = 'top tube' UNION SELECT assb.PART, all.Minor FROM assembly AS assb, all_super AS all WHERE assb.SUBPART = all.Major AND WHERE all.Minor = 'top tube‘) SELECT * FROM all_subparts
6
Implementation left-recursion and right-recursion SELECT * FROM_all subparts WHERE Minor = 'top tube' Expressed against the virtual view of CREATE RECURSIVE view all_subparts(Major, Minor) AS SELECT PART SUBPART FROM assembly UNION SELECT all.Major AS assb.SUBPART FROM all_subparts AS all, assembly AS assb WHERE all.Minor= assb.PART The addition of the condition Minor = 'top tube' to the recursive select would not produce an equivalent query. Thus, the compiler transforms the recursive select into its rightlinear equivalent, then adds Minor ='top tube' to the WHERE clause.
7
Recursive Rule Processing in SQL In general the compilation techniques usable for such transformations are basically those previously described for Datalog. Including the magic set transformation. Also stratification w.r.t. negation and aggregates is required in SQL 1999
8
Recursive Rule Processing in SQL In general, the compilation techniques usable for such transformations are basically those previously described for Datalog---Including the magic set transformation Also stratification w.r.t. `negation’ and aggregates is required by SQL 1999. By negation, we mean NOT EXIST and EXCEPT because conditions such as NOT(A>B) become A<=B. That means that you cannot express several graph algorithms in SQL99
9
Discussion Datalog contributions Power (beyond SQL-2) Rule-based programming paradigm very effective in many applications Simple Formal semantics Technology Impact on SQL1999 Recursive queries now supported in commercial DBMSs Limited used in applications, because hard to write and also restrictions imposed by stratification.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.