XML Views Reasoning about Views Zachary G Ives
XML Views & Reasoning about Views Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems November 5, 2007 Some slide content courtesy of Susan Davidson, Dan Suciu, & Raghu Ramakrishnan
Views: Alternate Representations XSLT is a language primarily designed from going from XML non-XML Obviously, we can do XML in XQuery … Or relations … What about relations XML and XML relations? Let’s start with XML, relations 2
Views in SQL and XQuery § A view is a named query § We use the name of the view to invoke the query (treating it as if it were the relation it returns) Using the views: SQL: SELECT * CREATE VIEW V(A, B, C) AS FROM V, R SELECT A, B, C FROM R WHERE R. A = “ 123” WHERE V. B = 5 AND V. C = R. C XQuery: declare function V() as element(content)* { for $v in V()/content, for $r in doc(“R”)/root/tree, $r in doc(“r”)/root/tree $a in $r/a, $b in $r/b, $c in $r/c where $v/b = $r/b where $a = “ 123” return $v return <content>{$a, $b, $c}</content> } 3
What’s Useful about Views Providing security/access control § We can assign users permissions on different views § Can select or project so we only reveal what we want! Can be used as relations in other queries § Allows the user to query things that make more sense Describe transformations from one schema (the base relations) to another (the output of the view) § The basis of converting from XML to relations or vice versa § This will be incredibly useful in data integration, discussed soon… Allow us to define recursive queries 4
Materialized vs. Virtual Views § A virtual view is a named query that is actually re-computed every time – it is merged with the referencing query CREATE VIEW V(A, B, C) AS SELECT A, B, C FROM R WHERE R. A = “ 123” SELECT * FROM V, R WHERE V. B = 5 AND V. C = R. C § A materialized view is one that is computed once and its results are stored as a table § § Think of this as a cached answer These are incredibly useful! Techniques exist for using materialized views to answer other queries Materialized views are the basis of relating tables in different schemas 5
Views Should Stay Fresh § Views (sometimes called intensional relations ) behave, from the perspective of a query language, exactly like base relations (extensional relations) § But there’s an association that should be maintained: § If tuples change in the base relation, they should change in the view (whether it’s materialized or not) § If tuples change in the view, that should reflect in the base relation(s) 6
Views as a Bridge between Data Models A claim we’ve made several times: “XML can’t represent anything that can’t be expressed in in the relational model” If this is true, then we must be able to represent XML in relations Store a relational view of XML (or create an XML view of relations) 7
A View as a Translation between XML and Relations § You have 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, soon to be DB 2 v 9 § SQL Server 2005 will have XQuery support; Oracle will also shortly have XQuery support § … Now you’ll know how it works! 8
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 9
The Simplest Way to Encode a Tree § Suppose we had: <tree id=“ 0”> <content id=“ 1”> <sub-content>XYZ key 0 </sub-content> <i-content>14 1 </i-content> 2 </content> </tree> 3 § If we have no IDs, we 4 CREATE values… 5 § Binary. Like. Edge(key, label, type, value, parent) label tree type value parent ref - content subcontent ref - 0 1 i-content - ref str int XYZ 14 1 2 3 What are shortcomings here? 10
Florescu/Kossmann Improved Edge Approach § Consider order, typing; separate the values § Edge(parent, ordinal, label, flag, target) parent ord label 1 tree 0 1 content § Vint(vid, value) vid value v 3 14 flag target ref 0 § Vstring(vid, value) ref 1 1 1 sub-content str v 2 1 1 i-content v 3 int vid value v 2 XYZ 11
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… 12
A Relation that Mirrors the XML Hierarchy § Output relation would look like: r. Label rid r. Ord clabel cid c. Ord s. Label sid s. Ord 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 13
A Relation that Mirrors the XML Hierarchy § Output relation would look like: r. Label rid r. Ord clabel cid c. Ord s. Label sid s. Ord 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 14
A Relation that Mirrors the XML Hierarchy § Output relation would look like: r. Label rid r. Ord clabel cid c. Ord s. Label sid s. Ord 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… 15
SQL for Outputting XML § For each sub-portion we preserve the keys (target, ord) of the ancestors § Root: select E. label AS r. Label, E. target AS rid, E. ord AS r. Ord, null AS c. Label, null AS cid, null AS c. Ord, null AS sub. Ord, null AS sid, null AS str, null AS int from Edge E where parent IS NULL § First-level children: select null AS r. Label, E. target AS rid, E. ord AS r. Ord, E 1. label AS c. Label, E 1. target AS cid, E 1. ord AS c. Ord, null AS … from Edge E, Edge E 1 where E. parent IS NULL AND E. target = E 1. parent 16
The Rest of the Queries § Grandchild: select null as r. Label, E. target AS rid, E. ord AS r. Ord, null AS c. Label, E 1. target AS cid, E 1. ord AS c. Ord, E 2. label as s. Label, E 2. target as sid, E 2. ord AS s. Ord, null as … from Edge E, Edge E 1, Edge E 2 where E. parent IS NULL AND E. target = E 1. parent AND E 1. target = E 2. parent § Strings: select null as r. Label, E. target AS rid, E. ord AS r. Ord, null AS c. Label, E 1. target AS cid, E 1. ord AS c. Ord, null as s. Label, E 2. target as sid, E 2. ord AS s. Ord, Vi. val AS str, null as int from Edge E, Edge E 1, Edge E 2, Vint Vi where E. parent IS NULL AND E. target = E 1. parent AND E 1. target = E 2. parent AND Vi. vid = E 2. target § How would we do integers? 17
Finally… § Union them all together: ( select E. label as r. Label, E. target AS rid, E. ord AS r. Ord, … from Edge E where parent IS NULL) UNION ( select null as r. Label, E. target AS rid, E. ord AS r. Ord, E 1. label AS c. Label, E 1. target AS cid, E 1. ord AS c. Ord, null as … from Edge E, Edge E 1 where E. parent IS NULL AND E. target = E 1. parent ) UNION (. : ) § Then another module will add the XML tags, and we’re done! 18
“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 19
Inlining Techniques § Start with DTD, build a graph representing structure tree ? @id * content * sub-content * @id i-content • The edges are annotated with ? , * indicating repetition, optionality of children • They simplify the DTD to figure this out 20
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 book author § Can create a single relation for each element § Can try to inline these name § 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 21
Schemas for Our Example § § The. Root(root. ID) Content(parent. ID, id, @id) Sub-content(parent. ID, varchar) I-content(parent. ID, int) § If we suddenly changed DTD to <!ELEMENT content(sub-content*, i-content? ) what would happen? 22
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 23
An Example for $X in document(“mydoc”)/tree/content where $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: The. Root(root. ID) Content(parent. ID, id, @id) Sub-content(parent. ID, varchar) I-content(parent. ID, int) 24
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 25
An Important Set of Questions § Views are incredibly powerful formalisms for describing how data relates: fn: rel … rel § Can I define a view recursively? § Why might this be useful? When should the recursion stop? § Suppose we have two views, v 1 and v 2 § How do I know whether they represent the same data? § If v 1 is materialized, can we use it to compute v 2? This is fundamental to query optimization and data integration, as we’ll see later 26
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 27
Let’s Go Back a Few Weeks… Domain Relational Calculus Queries have form: domain variables {<x 1, x 2, …, xn>| p } predicate Predicate: boolean expression over x 1, x 2, …, xn § We have the following operations: <xi, xj, …> R xi op xj xi op const op xi xi. p xj. p p q, p q p, p q where op is , , , and xi, xj, … are domain variables; p, q are predicates § Recall that this captures the same expressiveness as the relational algebra 28
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(T 1) R 1(T 2), R 2(T 3), …, c(T 2 [ … 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 29
Datalog Terminology § An example datalog rule: body idb(x, y) r 1(x, z), r 2(z, y), z < 10 head subgoals § 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. , r 1(“abc”, 123) 30
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) 31
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? 32
- Slides: 32