xml views & reasoning about views zachary g. ives university of pennsylvania cis 550 –...
Post on 20-Dec-2015
221 views
TRANSCRIPT
![Page 1: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/1.jpg)
XML Views & Reasoning about Views
Zachary G. IvesUniversity of Pennsylvania
CIS 550 – Database & Information Systems
November 4, 2004
Some slide content courtesy of Susan Davidson, Dan Suciu, & Raghu Ramakrishnan
![Page 2: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/2.jpg)
2
A View as a Translation betweenXML and Relations
You’ll be reviewing the most-cited paper in this area (Shanmugasundaram et al), and there are many more (Fernandez et al., …)
Techniques already making it into commercial systems XPERANTO at IBM Research will appear in DB2
v8 or 9 SQL Server has some XML support; Oracle is
also doing some XML … Now you’ll know how it works!
![Page 3: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/3.jpg)
3
Issues in Mapping Relational XML
We know the following: XML is a tree XML is SEMI-structured
There’s some structured “stuff” There is some unstructured “stuff”
Issues relate to describing XML structure, particularly parent/child in a relational encoding Relations are flat Tuples can be “connected” via
foreign-key/primary-key links
![Page 4: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/4.jpg)
4
The Simplest Way to Encode a Tree
Suppose we had:<tree id=“0”> <content id=“1”> <sub-content>XYZ </sub-content> <i-content>14 </i-content> </content></tree>
If we have no IDs, we CREATE values…
BinaryLikeEdge(key, label, type, value, parent)
key
label type
value
parent
0 tree ref - -
1 content ref - 0
2 sub-content
ref - 1
3 i-content ref - 1
4 - str XYZ 2
5 - int 14 3What are shortcomings here?
![Page 5: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/5.jpg)
5
Florescu/Kossmann Improved Edge Approach
Consider order, typing; separate the values Edge(parent, ordinal,
label, flag, target)
Vint(vid, value)
Vstring(vid, value)
parent
ord
label flag
target
- 1 tree ref 0
0 1 content ref 1
1 1 sub-content
str v2
1 1 i-content int v3
vid
value
v3 14
vid
value
v2 XYZ
![Page 6: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/6.jpg)
6
How Do You Compute the XML?
Assume we know the structure of the XML tree (we’ll see how to avoid this later)
We can compute an “XML-like” SQL relation using “outer unions” – we first this technique in XPERANTO Idea: if we take two non-union-compatible
expressions, pad each with NULLs, we can UNION them together
Let’s see how this works…
![Page 7: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/7.jpg)
7
A Relation that Mirrors theXML Hierarchy
Output relation would look like:
rLabel
rid
rOrd
clabel
cid
cOrd
sLabel sid
sOrd
str int
tree 0 1 - - - - - - - -
- 0 1 content
1 1 - - - - -
- 0 1 - 1 1 sub-content
2 1 - -
- 0 1 - 1 1 - 2 1 XYZ
-
- 0 1 - 1 2 i-content 3 1 - -
- 0 1 - 1 2 - 3 1 - 14
![Page 8: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/8.jpg)
8
A Relation that Mirrors theXML Hierarchy
Output relation would look like:
rLabel
rid
rOrd
clabel
cid
cOrd
sLabel sid
sOrd
str int
tree 0 1 - - - - - - - -
- 0 1 content
1 1 - - - - -
- 0 1 - 1 1 sub-content
2 1 - -
- 0 1 - 1 1 - 2 1 XYZ
-
- 0 1 - 1 2 i-content 3 1 - -
- 0 1 - 1 2 - 3 1 - 14
![Page 9: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/9.jpg)
9
A Relation that Mirrors theXML Hierarchy
Output relation would look like:
rLabel
rid
rOrd
clabel
cid
cOrd
sLabel sid
sOrd
str int
tree 0 1 - - - - - - - -
- 0 1 content
1 1 - - - - -
- 0 1 - 1 1 sub-content
2 1 - -
- 0 1 - 1 1 - 2 1 XYZ
-
- 0 1 - 1 2 i-content 3 1 - -
- 0 1 - 1 2 - 3 1 - 14
Colors are representative of separate SQL queries…
![Page 10: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/10.jpg)
10
SQL for Outputting XML
For each sub-portion we preserve the keys (target, ord) of the ancestors
Root:select E.label AS rLabel, E.target AS rid, E.ord AS rOrd, null AS cLabel, null AS cid, null AS cOrd, null AS subOrd, null AS sid, null AS str, null AS intfrom Edge Ewhere parent IS NULL
First-level children:select null AS rLabel, E.target AS rid, E.ord AS rOrd, E1.label AS cLabel, E1.target AS cid, E1.ord AS cOrd, null AS …from Edge E, Edge E1where E.parent IS NULL AND E.target = E1.parent
![Page 11: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/11.jpg)
11
The Rest of the Queries
Grandchild:select null as rLabel, E.target AS rid, E.ord AS rOrd, null AS cLabel, E1.target AS cid, E1.ord AS cOrd, E2.label as sLabel, E2.target as sid, E2.ord AS sOrd, null as …from Edge E, Edge E1, Edge E2where E.parent IS NULL AND E.target = E1.parent AND E1.target = E2.parent
Strings:select null as rLabel, E.target AS rid, E.ord AS rOrd, null AS cLabel, E1.target AS cid, E1.ord AS cOrd, null as sLabel, E2.target as sid, E2.ord AS sOrd, Vi.val AS str, null as intfrom Edge E, Edge E1, Edge E2, Vint Vi where E.parent IS NULL AND E.target = E1.parent AND E1.target = E2.parent AND Vi.vid = E2.target
How would we do integers?
![Page 12: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/12.jpg)
12
Finally…
Union them all together:( select E.label as rLabel, E.target AS rid, E.ord AS rOrd, … from Edge E where parent IS NULL)UNION ( select null as rLabel, E.target AS rid, E.ord AS rOrd, E1.label AS cLabel, E1.target AS cid, E1.ord AS cOrd, null as … from Edge E, Edge E1 where E.parent IS NULL AND E.target = E1.parent) UNION ( . :) UNION ( . :)
Then another module will add the XML tags, and we’re done!
![Page 13: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/13.jpg)
13
“Inlining” Techniques
Folks at Wisconsin noted we can exploit the “structured” aspects of semi-structured XML If we’re given a DTD, often the DTD has a lot of
required (and often singleton) child elements Book(title, author*, publisher)
Recall how normalization worked: Decompose until we have everything in a relation
determined by the keys … But don’t decompose any further than that
Shanmugasundaram et al. try not to decompose XML beyond the point of singleton children
![Page 14: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/14.jpg)
14
Inlining Techniques
Start with DTD, build a graph representing structure
tree
content
sub-content i-content
*
* *
• The edges are annotated with ?, * indicating repetition,optionality of children
• They simplify the DTD to figure this out
@id
@id
?
![Page 15: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/15.jpg)
15
Building Schemas
Now, they tried several alternatives that differ in how they handle elements w/multiple ancestors Can create a separate relation for each path Can create a single relation for each element Can try to inline these
For tree examples, these are basically the same Combine non-set-valued things with parent Add separate relation for set-valued child elements Create new keys as needed
name
book author
![Page 16: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/16.jpg)
16
Schemas for Our Example
TheRoot(rootID) Content(parentID, id, @id) Sub-content(parentID, varchar) I-content(parentID, int)
If we suddenly changed DTD to <!ELEMENT content(sub-content*, i-content?) what would happen?
![Page 17: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/17.jpg)
17
XQuery to SQL
Inlining method needs external knowledge about the schema Needs to supply the tags and info not stored in
the tables
We can actually directly translate simple XQuery into SQL over the relations – not simply reconstruct the XML
![Page 18: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/18.jpg)
18
An Example
for $X in document(“mydoc”)/tree/contentwhere $X/sub-content = “XYZ”return $X
The steps of the path expression are generally joins … Except that some steps are eliminated by the fact
we’ve inlined subelements Let’s try it over the schema:
TheRoot(rootID)Content(parentID, id, @id)Sub-content(parentID, varchar)I-content(parentID, int)
![Page 19: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/19.jpg)
19
XML Views of Relations
We’ve seen that views are useful things Allow us to store and refer to the results of
a query We’ve seen an example of a view that
changes from XML to relations – and we’ve even seen how such a view can be posed in XQuery and “unfolded” into SQL
![Page 20: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/20.jpg)
20
An Important Set of Questions
Views are incredibly powerful formalisms for describing how data relates: fn: rel … rel rel
Can I define a view recursively? Why might this be useful? When should the recursion
stop?
Suppose we have two views, v1 and v2
How do I know whether they represent the same data? If v1 is materialized, can we use it to compute v2?
This is fundamental to query optimization and data integration, as we’ll see later
![Page 21: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/21.jpg)
21
Reasoning about Queries and Views
SQL or XQuery are a bit too complex to reason about directly Some aspects of it make reasoning about SQL
queries undecidable
We need an elegant way of describing views (let’s assume a relational model for now) Should be declarative Should be less complex than SQL Doesn’t need to support all of SQL –
aggregation, for instance, may be more than we need
![Page 22: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/22.jpg)
22
Let’s Go Back a Few Weeks…Domain Relational Calculus
Queries have form:
{<x1,x2, …, xn>| p }
Predicate: boolean expression over x1,x2, …, xn We have the following operations:
<xi,xj,…> R xi op xj xi op const const op xi
xi. p xj. p pq, pq p, pqwhere op is , , , , , and
xi,xj,… are domain variables; p,q are predicates Recall that this captures the same
expressiveness as the relational algebra
domain variables predicate
![Page 23: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/23.jpg)
23
A Similar Logic-Based Language:Datalog
Borrows the flavor of the relational calculus but is a “real” query language Based on the Prolog logic-programming language A “datalog program” will be a series of if-then rules
(Horn rules) that define relations from predicates
Rules are generally of the form:Rout(T1) R1(T2), R2(T3), …, c(T2 [ … Tn)
where Rout is the relation representing the query result, Ri are predicates representing relations, c is an expression using arithmetic/boolean predicates
over vars, and Ti are tuples of variables
![Page 24: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/24.jpg)
24
Datalog Terminology
An example datalog rule:idb(x,y) r1(x,z), r2(z,y), z < 10
Irrelevant variables can be replaced by _ (anonymous var)
Extensional relations or database schemas (edbs) are relations only occurring in rules’ bodies – these are base relations with “ground facts”
Intensional relations (idbs) appear in the heads – these are basically views
Distinguished variables are the ones output in the head
Ground facts only have constants, e.g., r1(“abc”, 123)
head subgoals
body
![Page 25: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/25.jpg)
25
Datalog in Action
As in DRC, the output (head) consists of a tuple for each possible assignment of variables that satisfies the predicate We typically avoid “8” in Datalog queries:
variables in the body are existential, ranging over all possible values
Multiple rules with the same relation in the head represent a union
We often try to avoid disjunction (“Ç”) within rules Let’s see some examples of datalog queries
(which consist of 1 or more rules): Given Professor(fid, name), Teaches(fid, serno, sem),
Courses(serno, cid, desc), Student(sid, name) Return course names other than CIS 550 Return the names of the teachers of CIS 550 Return the names of all people (professors or students)
![Page 26: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/26.jpg)
26
Datalog is Relationally Complete
We can map RA Datalog: Selection p: p becomes a datalog subgoal
Projection A: we drop projected-out variables from head Cross-product r s: q(A,B,C,D) r(A,B),s(C,D) Join r ⋈ s: q(A,B,C,D) r(A,B),s(C,D), condition Union r U s: q(A,B) r(A,B) ; q(C, D) :- s(C,D) Difference r – s: q(A,B) r(A,B), : s(A,B)
(If you think about it, DRC Datalog is even easier)
Great… But then why do we care about Datalog?
![Page 27: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/27.jpg)
27
A Query We Can’tAnswer in RA/TRC/DRC…
Recall our example of a binary relation for graphs or trees (similar to an XML Edge relation):
edge(from, to)
If we want to know what nodes are reachable:
reachable(F, T) :- edge(F, T) distance 1reachable(F, T) :- edge(F, X), edge(X, T) dist. 2reachable(F, T) :- edge(F, X), dist2(X, T) dist. 3
But how about all reachable paths? (Note this was easy in XPath over an XML representation -- //edge)
(another way of writing )
![Page 28: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/28.jpg)
28
Recursive Datalog Queries
Define a recursive query in datalog:reachable(F, T) :- edge(F, T) distance 1
reachable(F, T) :- edge(F, X), reachable(X, T)distance >1
What does this mean, exactly, in terms of logic? There are actually three different (equivalent)
definitions of semantics All make a “closed-world” assumption: facts should
exist only if they can be proven true from the input – i.e., assume the DB contains all of the truths out there!
![Page 29: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/29.jpg)
29
Fixpoint Semantics
One of the three Datalog models is based on a notion of fixpoint: We start with an instance of data, then derive
all immediate consequences We repeat as long as we derive new facts
In the RA, this requires a while loop! However, that is too powerful and needs to be
restricted Special case: “inflationary semantics”
(which terminates in time polynomial in the size of the database!)
![Page 30: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/30.jpg)
30
Our Query in RA + while(inflationary semantics, no negation)
Datalog:reachable(F, T) :- edge(F, T)reachable(F, T) :- edge(F, X), reachable(X, T)
RA procedure with while:reachable += edgewhile change {
reachable += F, T(T ! X(edge) ⋈ F ! X(reachable))
}
![Page 31: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/31.jpg)
31
Negation in Datalog
Datalog allows for negation in rules It’s essential for capturing RA set difference-
style ops:Professor(, name), : Student(, name)
But negation can be tricky… … You may recall that in the DRC, we had a
notion of “unsafe” queries, and they return here…
Single(X) Person(X), : Married(X,Y)
![Page 32: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/32.jpg)
32
Safe Rules/Queries
Range restriction, which requires that every variable: Occurs at least once in a positive relational
predicate in the body, Or it’s constrained to equal a finite set of
values by arithmetic predicatesUnsafe:q(X) r(Y)q(X) : r(X,X)q(X) r(X) Ç t(Y)
Safe:q(X) r(X,Y)q(X) X = 5 q(X) : r(X,X), s(X)q(X) r(X) Ç (t(Y),u(X,Y))
![Page 33: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/33.jpg)
33
Negation and Recursion
Unfortunately, the fixpoint semantics we mentioned previously for recursion “breaks” with negation… q(x) : q(x) No fixpoint!
p(x) : q(x) Multiple minimal fixpoints!q(x) : p(x)
Or the fixpoint may not “converge” (or converge to a minimal fixpoint)
This is all bad news…
![Page 34: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/34.jpg)
34
One Way to Fix Things: Stratified Semantics
Start with semipositive datalog: negation only over edb predicates From here, we can compute a set of values for
the idb predicates that depend on the edb’s
Now we can “materialize” the results of the first set of idbs; we’ll remove their rules and treat them as edbs to compute a next “stratum”
r (1,1)r (1,2)s (1,1)v1(x,y) :- r(x,y), : s(x,y)q(x,y) :- v1(x,y), : s(x,y)
r (1,1)r (1,2)s (1,1)v1 (1,2)q(x,y) :- v1(x,y), : s(x,y)
r (1,1)r (1,2)s (1,1)v1 (1,2)q (1,2)
![Page 35: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/35.jpg)
35
Precedence Graphs
Take a set of datalog rules, create a node for each relation (edb or idb) If r r’ then add an edge labeled “+” from r
to r’ If r : r’ then add an edge labeled “-” from r
to r’ We can stratify if there are no cycles with “-”
edgesv1(x,y) :- r(x,y), : s(x,y)q(x,y) :- v1(x,y), : s(x,y)
sr
v1 q-
+
+
![Page 36: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/36.jpg)
36
Stratifying Datalog Execution
foreach predicate p, set stratum(p) = 1do until no change, or some stratum > # of predicates
foreach rule h b {foreach negated subgoal of b with predicate q {stratum(p) = max(stratum(p), 1+stratum(q))}foreach positive subgoal of b with predicate q {stratum(p) = max(stratum(p), stratum(q)}
}
![Page 37: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/37.jpg)
37
Conjunctive Queries
A single Datalog rule with no “Ç,” “:,” “8” can express select, project, and join – a conjunctive query
Conjunctive queries are possible to reason about statically (Note that we can write CQ’s in other languages, e.g., SQL!)
We know how to “minimize” conjunctive queriesAn important simplification that can’t be done for general SQL
We can test whether one conjunctive query’s answers always contain another conjunctive query’s answers (for ANY instance)
Why might this be useful?
![Page 38: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/38.jpg)
38
Example of Containment
Suppose we have two queries:
q1(S,C) :- Student(S, N), Takes(S, C), Course(C, X), inCSE(C),
Course(C, “DB & Info Systems”)
q2(S,C) :- Student(S, N), Takes(S, C), Course(C, X)
Intuitively, q1 must contain the same or fewer answers vs. q2: It has all of the same conditions, except one extra conjunction
(i.e., it’s more restricted) There’s no union or any other way it can add more data
We can say that q2 contains q1 because this holds for any instance of our DB {Student, Takes, Course}
![Page 39: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/39.jpg)
39
Checking Containment via Canonical DBs
To test for q1 µ q2: Create a “canonical DB” that contains a tuple for
each subgoal in q1 Execute q2 over it If q2 returns a tuple that matches the head of q1,
then q1 µ q2
(This is an NP-complete algorithm in the size of the query. Testing for full first-order logic queries is undecidable!!!)
Let’s see this for our example…
![Page 40: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/40.jpg)
40
Example Canonical DB
q1(S,C) :- Student(S, N), Takes(S, C), Course(C, X), inCSE(C), Course(C, “DB & Info Systems”)
q2(S,C) :- Student(S, N), Takes(S, C), Course(C, X)
Student Takes Course inCSE
S N S C C X
C DB & Info
Systems
S
Need to get tuple <S,C> in executing q2 over this database
![Page 41: XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 4, 2004 Some slide content](https://reader036.vdocument.in/reader036/viewer/2022062320/56649d435503460f94a1f147/html5/thumbnails/41.jpg)
41
Wrapping up…
We’ve seen a new language, Datalog It’s basically a glorified DRC with a special feature,
recursion It’s much cleaner than SQL for reasoning about … But negation (as in the DRC) poses some
challenges
We’ve seen that a particular kind of query, the conjunctive query, is written naturally in Datalog Conjunctive queries are possible to reason about We saw an example of testing for containment Next time we’ll see some further examples