CS240A: Databases and Knowledge Bases Recursive Queries in SQL 2003 Carlo Zaniolo Department of Computer Science University of California, Los Angeles.

Slides:



Advertisements
Similar presentations
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
Advertisements

Lecture 11: Datalog Tuesday, February 6, Outline Datalog syntax Examples Semantics: –Minimal model –Least fixpoint –They are equivalent Naive evaluation.
Copyright © 2004 Pearson Education, Inc.. Chapter 15 Algorithms for Query Processing and Optimization.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1.
CS240A: Databases and Knowledge Bases From Deductive Rules to Active Rules Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
CS240A: Databases and Knowledge Bases From Deductive Rules to Active Rules Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
The Logic of Intelligence Pei Wang Department of Computer and Information Sciences Temple University.
The Volcano/Cascades Query Optimization Framework
Implementing Reflective Access Control in SQL Lars E. Olson 1, Carl A. Gunter 1, William R. Cook 2, and Marianne Winslett 1 1 University of Illinois at.
1 Recursive SQL, Deductive Databases, Query Evaluation Book Chapter of Ramankrishnan and Gehrke DBMS Systems, 3 rd ed.
1 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke Deductive Databases Chapter 25.
Database Management Systems 3ed, R. Ramakrishnan and Johannes Gehrke1 Evaluation of Relational Operations: Other Techniques Chapter 14, Part B.
Database Management Systems, R. Ramakrishnan and Johannes Gehrke1 Evaluation of Relational Operations: Other Techniques Chapter 12, Part B.
ISBN Chapter 3 Describing Syntax and Semantics.
Induction and Decision Trees. Artificial Intelligence The design and development of computer systems that exhibit intelligent behavior. What is intelligence?
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
1 Basic abstract interpretation theory. 2 The general idea §a semantics l any definition style, from a denotational definition to a detailed interpreter.
CS240A: Databases and Knowledge Bases Introduction Carlo Zaniolo Department of Computer Science University of California, Los Angeles WINTER 2002.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
CS240A: Databases and Knowledge Bases Time Ontology and Representations Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
CS240A: Databases and Knowledge Bases Fixpoint Semantics of Datalog Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
CS 240A: Databases and Knowledge Bases Analysis of Active Databases Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
Research on Intelligent Information Systems Himanshu Gupta Michael Kifer Annie Liu C.R. Ramakrishnan I.V. Ramakrishnan Amanda Stent David Warren Anita.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
CS240A: Databases and Knowledge Bases From Differential Fixpoints to Magic Sets Carlo Zaniolo Department of Computer Science University of California,
CS240A: Databases and Knowledge Bases Applications of Active Database Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
©Silberschatz, Korth and Sudarshan1.1Database System Concepts Chapter 1: Introduction n Why Database Systems? n Data Models n Data Definition Language.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
CS240A: Databases and Knowledge Bases From Deductive Rules to Active Rules Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
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.
Describing Syntax and Semantics
CS240A: Databases and Knowledge Bases Time Ontology and Representations Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
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
Semantic Web Technologies Lecture # 2 Faculty of Computer Science, IBA.
CS240A: Databases and Knowledge Bases Introduction Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
1 Data Mining Books: 1.Data Mining, 1996 Pieter Adriaans and Dolf Zantinge Addison-Wesley 2.Discovering Data Mining, 1997 From Concept to Implementation.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
Introduction to Database Systems
1 Advanced Databases – CM036 Dr. Akhtar Ali School of Informatics.
Theory of Languages and Automata
The Relational Model: Relational Calculus
Christopher Re and Dan Suciu University of Washington Efficient Evaluation of HAVING Queries on a Probabilistic Database.
Steffen Staab Advanced Data Modeling 1 of 32 WeST Häufungspunkte Bifurkation: x n+1 = r x n (1-x n ) Startwert x 0 = 0,25.
1 Knowledge Representation. 2 Definitions Knowledge Base Knowledge Base A set of representations of facts about the world. A set of representations of.
Datalog Inspired by the impedance mismatch in relational databases. Main expressive advantage: recursive queries. More convenient for analysis: papers.
CS240A: Databases and Knowledge Bases Recursive Queries in SQL 1999 Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
CS Introduction to AI Tutorial 8 Resolution Tutorial 8 Resolution.
Formal Semantics of Programming Languages 虞慧群 Topic 1: Introduction.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
Semantics In Text: Chapter 3.
SQL—Query Power From the DB2 textbook. Expressive Power Topics 5.1 Case Expressions 5.2 Subqueries—Table expressions 5.3 Table Functions (later) 5.4 Explicit.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
DEDUCTION PRINCIPLES AND STRATEGIES FOR SEMANTIC WEB Chain resolution and its fuzzyfication Dr. Hashim Habiballa University of Ostrava.
CS240A: Databases and Knowledge Bases Temporal Databases Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
CS 240A: Databases and Knowledge Bases Analysis of Active Databases Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
Chapter 9: Web Services and Databases Title: NiagaraCQ: A Scalable Continuous Query System for Internet Databases Authors: Jianjun Chen, David J. DeWitt,
CS240A: Databases and Knowledge Bases TSQL2 Carlo Zaniolo Department of Computer Science University of California, Los Angeles Notes From Chapter 6 of.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
Presented by Kyumars Sheykh Esmaili Description Logics for Data Bases (DLHB,Chapter 16) Semantic Web Seminar.
Extensions of Datalog Wednesday, February 13, 2001.
CS240A: Databases and Knowledge Bases Introduction Carlo Zaniolo Department of Computer Science University of California, Los Angeles.
Logic as a Query Language: from Frege to XML
CS240A: Databases and Knowledge Bases Introduction
7/4/2018.
7/5/2018.
Introduction CI612 Compiler Design CI612 Compiler Design.
Evaluation of Relational Operations: Other Techniques
CS240A: Databases and Knowledge Bases TSQL2
Presentation transcript:

CS240A: Databases and Knowledge Bases Recursive Queries in SQL 2003 Carlo Zaniolo Department of Computer Science University of California, Los Angeles Notes From Chapter 9 of Advanced Database Systems by Zaniolo, Ceri, Faloutsos, Snodgrass, Subrahmanian and Zicari Morgan Kaufmann, 1997

Datalog Patterns assemby(PART, SUBPART).. %left-linear all(Major, Minor) <- all (Major, Mid), assembly(Mid, Minor). %right-linear all(Major, Minor) <- assembly(Major, Mid), all(Mid, Minor).

The Part/Subparts Example assembly(PART, SUBPART, QTY) 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

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 ALL 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‘  this corresponds to a right-linear Datalog rule

Implementation CREATE RECURSIVE VIEW all_subparts(Major, Minor) AS SELECT PART SUBPART FROM assembly UNION ALL 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 eliminate duplicates as they are generated)

Find the parts using top tube (on Right­Linear Recursion) WITH RECURSIVE all_super(Major, Minor) AS (SELECT PART, SUBPART FROM assembly UNIONALL SELECT assb.PART, all.Minor FROM assembly AS assb, all_super AS all WHERE assb.SUBPART = all.Major) SELECT * FROM all_super 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 ALL SELECT assb.PART, all.Minor FROM assembly AS assb, all_super AS all WHERE assb.SUBPART = all.Major AND all.Minor = 'top tube‘) SELECT * FROM all_super

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 right­linear equivalent, then adds Minor ='top tube' to the WHERE clause.

Example from Chamberlin’s book WITH trips(destination, route, nsegs, totalcost) AS ((SELECT destination, CAST(destination AS Varchar(20)), 1, cost FROM flights WHERE origin = 'SFO') UNION ALL (SELECT f.destination, CAST(t.route || ', ' || f.destination AS Varchar(20)), t.nsegs + 1, t.totalcost + f.cost FROM trips t, flights f WHERE t.destination = f.origin AND f.destination <> 'SFO' AND f.origin <> 'JFK' AND t.nsegs < 3 )) SELECT route, totalcost FROM trips WHERE destination = 'JFK' AND totalcost = (SELECT min(totalcost) FROM trips WHERE destination = 'JFK');

Limitations of Stratification  Aggregates are nonmonotonic  They cannot be used inside recursion— stratification condition for aggregates  Without aggregates we cannot express optimal graph-traversal algorithm  Much research work in nonmonotonic reasoning.  Naïve extensions do not work: e.g. Russell’s paradox—who shaves the barber?

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 Mutual recursion is supported and this where WITH becomes indispensable * Because aggregates are nonmonotonic!

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 SQL:1999

Discussion  Datalog contributions  Power (beyond SQL-2)  Rule-based programming paradigm very effective in many applications  Simple Formal semantics  Novel Technology  Significant impact on SQL:1999 (SQL:2003 a clean up version of SQL:1999)  Recursive queries now supported in commercial DBMSs  Limited used in applications, because hard to write and also restrictions imposed by stratification.

Discussion (cont.) Recursion add expressive power to SQL but  Complex objects not supported: (Deductive Object- Oriented DBs—DOODs)... As opposed to OO-DB research that instead wants to unify DBs and object-oriented PL.  The Non-Monotonic conundrum:  Not a new problem: naïve set theory (Russell’s) paradox, AI Circumscription (J. Carthy)  Stratification is not sufficient, in many cases to:  ensure termination, and  Express classical graph algorithms (using extrema aggregates).  Significant progress achieved in this area not part of SQL standards.

Discussion (cont.)  Datalog remains a great framework for formal database research: E.g.  Integration Information,  Data Mining (Inductive Databases)  Network routing algorithms  Static program analysis.