Docsity

SQL Recursion: Understanding Monotonicity and Stratum Graphs, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download SQL Recursion: Understanding Monotonicity and Stratum Graphs and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

SQL Recursion

WITH stu that lo oks like Datalog rules an SQL query ab out EDB, IDB

 Rule =

[RECURSIVE] R () AS SQL query

Example

Find Sally's cousins, using EDB Par(child, parent).

WITH Sib(x,y) AS SELECT p1.child, p2,child FROM Par p1, Par p WHERE p1.parent = p2.parent AND p1.child <> p2.child, RECURSIVE Cousin(x,y) AS Sib UNION (SELECT p1.child, p2.child FROM Par p1, Par p2, Cousin WHERE p1.parent = Cousin.x AND p2.parent = Cousin.y ) SELECT y FROM Cousin WHERE x = 'Sally';

Monotonicity Example

In addition to certain negations, an aggregation can cause nonmonotonicity.

Sells(bar , beer , price)

SELECT AVG(price) FROM Sells WHERE bar = 'Joe''s Bar';

 Adding to Sells a tuple that gives a new b eer Jo e sells will usually change the average price of b eer at Jo e's.

 Thus, the former result, which might b e a single tuple like (2:78) b ecomes another single tuple like (2:81), and the old tuple is lost.

Generalizing Stratum Graph to SQL

 No de for each relation de ned by a \rule."

 No de for each sub query in the \b o dy" of a rule.

 Arc P! Q if

a) P is \head" of a rule, and Q is a relation app earing in the FROM list of the rule (not in the FROM list of a sub query), as argument of a UNION, etc. b) P is head of a rule, and Q is a sub query directly used in that rule (not nested within some larger sub query). c) P is a sub query, and Q is a relation or sub query used directly within P [analogous to (a) and (b) for rule heads].

 Lab el the arc if P is not monotone in Q.

 Requirement for legal SQL recursion: nite strata only.

A Nonmonotonic Example

Change the UNION to EXCEPT in the rule for Cousin.

RECURSIVE Cousin(x,y) AS Sib EXCEPT (SELECT p1.child, p2.child FROM Par p1, Par p2, Cousin WHERE p1.parent = Cousin.x AND p2.parent = Cousin.y )

 Now, adding to the result of the sub query can delete Cousin facts; i.e., Cousin is nonmonotone in S Q.

Sib Cousin

S Q

 In nite numb er of 's in cycle, so illegal in SQL.

Another Example: NOT Do esn't Mean Nonmonotone

Leave Cousin as it was, but negate one of the conditions in the where-clause.

RECURSIVE Cousin(x,y) AS Sib UNION (SELECT p1.child, p2.child FROM Par p1, Par p2, Cousin WHERE p1.parent = Cousin.x AND NOT (p2.parent = Cousin.y) )

 You might think that S Q dep ends negatively on Cousin, but it do esn't. F If I add a new tuple to Cousin, all the old tuples still exist and yield whatever tuples in S Q they used to yield. F In addition, the new Cousin tuple might combine with old p 1 and p 2 tuples to yield something new.

ODL Overview

Class declarations include:

  1. Name for the class.
  2. Key declaration(s), which are optional.
  3. Extent declaration = name for the set of currently existing ob jects of a class.
  4. Element declarations. An element is an attribute, a relationship, or a metho d.

ODL Class Declarations

class { elements = attributes, relationships, metho ds }

Element Declarations

attribute ; relationship ;

 Relationshi ps involve ob jects; attributes (usually) involve non-ob ject values, e.g., integers.

Metho d Example

float gpa(in string) raises(noGrades)

 float = return typ e.

 in: indicates the argument (a student name, presumably) is read-only.

F Other options: out, inout.

 noGrades is an exception that can b e raised by metho d gpa.

Beers-Bars-Drinkers Example

class Beers { attribute string name; attribute string manf; relationship Set servedAt inverse Bars::serves; relationship Set fans inverse Drinkers::likes; }

 An element from another class is indicated by ::

 Form a set typ e with Set.

class Bars { attribute string name; attribute Struct Addr {string street, string city, int zip} address; attribute Enum Lic {full, beer, none} licenseType; relationship Set customers inverse Drinkers::frequents; relationship Set serves inverse Beers::servedAt; }

 Structured typ es have names and bracketed lists of eld-typ e pairs.

 Enumerated typ es have names and bracketed lists of values.

ODL Typ e System

 Basic typ es: int, real/ oat, string, enumerated typ es, and classes.

 Typ e constructors: Struct for structures and ve col lection types : Set, Bag, List, Array, and Dictionary.

 Relationshi p typ es many only b e classes or a collecti on of a class.

Many-One Relationships

Don't use a collecti on typ e for relationship in the \many" class.

Example: Drinkers Have Favorite Beers

class Drinkers { attribute string name; attribute Struct Bars::Addr address; relationship Set likes inverse Beers::fans; relationship Beers favoriteBeer inverse Beers::realFans; relationship Set frequents inverse Bars::customers; }

 Also add to Beers:

relationship Set realFans inverse Drinkers::favoriteBe er;

Roles in ODL

Names of relationships handle \roles."

Example: Sp ouses and Drinking Buddies

class Drinkers { attribute string name; attribute Struct Bars::Addr address; relationship Set likes inverse Beers::fans; relationship Set frequents inverse Bars::customers; relationship Drinkers husband inverse wife; relationship Drinkers wife inverse husband; relationship Set buddies inverse buddies; }

 Notice that Drinkers:: is optional when the inverse is a relationship of the same class.