views: alternate data representations zachary g. ives university of pennsylvania cis 550 –...
Post on 21-Dec-2015
218 views
TRANSCRIPT
Views: Alternate Data Representations
Zachary G. IvesUniversity of Pennsylvania
CIS 550 – Database & Information Systems
November 2, 2004
Some slide content courtesy of Susan Davidson, Dan Suciu, & Raghu Ramakrishnan
2
Administrivia
One- to two-page project plan document due Thursday
Today: your first paper for reviewing See the handout on writing a paper review Please post the (short) review to the
newsgroup, before class next Tuesday
3
Where We Left Off…
Wrapping up a discussion of XQuery Very flexible and powerful language for XML Clean and orthogonal: can always replace a
collection with an expression that creates collections
DB and document-oriented (we hope) The core is relatively clean and easy to
understand
One feature worth noting, which had no correspondence in the SQL world…
4
Querying & Defining Metadata – Can’t Do This in SQL
Can get a node’s name by querying node-name():for $x in document(“dblp.xml”)/dblp/*return node-name($x)
Can construct elements and attributes using computed names:
for $x in document(“dblp.xml”)/dblp/*,$year in $x/year,$title in $x/title/text(),
element node-name($x) {attribute {“year-” + $year} { $title }
}
5
A Problem
We frequently want to reference data in a way that differs from the way it’s stored XML data HTML, text, etc. Relational data XML data Relational data Different relational representation XML data Different XML representation
Generally, these can all be thought of as different views over the data A view is a named query Let’s start with a special presentation language for XML
HTML
6
XSL(T): XML “Other Stuff”
XSL (XML Stylesheet Language) is actually divided into two parts: XSL:FO: formatting for XML XSLT: a special transformation language
We’ll leave XSL:FO for you to read off www.w3.org, if you’re interested
XSLT is actually able to convert from XML HTML, which is how many people do their formatting today Products like Apache Cocoon generally translate XML
HTML on the server side Your browser will do XML HTML on the client side
7
A Different Style of Language
XSLT is based on a series of templates that match different parts of an XML document There’s a policy for what rule or template is applied if
more than one matches (it’s not what you’d think!) XSLT templates can invoke other templates XSLT templates can be nonterminating (beware!)
XSLT templates are based on XPath “match”es, and we can also apply other templates (potentially to “select”ed XPaths) Within each template, we describe what should be
output (Matches to text default to outputting it)
8
An XSLT Stylesheet
<xsl:stylesheet version=“1.1”> <xsl:template match=“/dblp”> <html><head>This is DBLP</head> <body> <xsl:apply-templates /> </body> </html> </xsl:template> <xsl:template match=“inproceedings”>
<h2><xsl:apply-templates select=“title” /></h2> <p><xsl:apply-templates select=“author”/></p> </xsl:template> …</xsl:stylesheet>
9
Results of XSLT Stylesheet
<dblp> <inproceedings> <title>Paper1</title> <author>Smith</author> </inproceedings> <inproceedings>
<author>Chakrabarti</author>
<author>Gray</author> <title>Paper2</title> </inproceedings></dblp>
<html><head>This Is DBLP</head>
<body> <h2>Paper1</h2> <p>Smith</p> <h2>Paper2</h2> <p>Chakrabarti</p> <p>Gray</p></body></html>
10
What XSLT Can and Can’t Do
XSLT is great at converting XML to other formats XML diagrams in SVG; HTML; LaTeX …
XSLT doesn’t do joins (well), it only works on one XML file at a time, and it’s limited in certain respects It’s not a query language, really … But it’s a very good formatting language
Most web browsers (post Netscape 4.7x) support XSLT and XSL formatting objects
But most real implementations use XSLT with something like Apache Cocoon – compatible with more browsers
You should use XSL/XSLT to format the forms and pages for your projects – see www.w3.org/TR/xslt or the chapter we handed out for more details
11
Other Forms of Views
XSLT is a language primarily designed from going from XML non-XML
Obviously, we can do XML XML in XQuery… Or relations relations
… What about relations XML and XML relations?
Let’s start with XML XML, relations relations
12
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)
SQL:CREATE VIEW V(A,B,C) AS
SELECT A,B,C FROM R WHERE R.A = “123”
XQuery:declare function V() as element(content)* {
for $r in doc(“R”)/root/tree, $a in $r/a, $b in $r/b, $c in $r/cwhere $a = “123”return <content>{$a, $b, $c}</content>
}
SELECT * FROM V, RWHERE V.B = 5 AND V.C = R.C
for $v in V()/content, $r in doc(“r”)/root/treewhere $v/b = $r/breturn $v
Using the views:
13
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
14
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”
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
SELECT * FROM V, RWHERE V.B = 5 AND V.C = R.C
15
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)
16
View Maintenance and the View Update Problem
There exist algorithms to incrementally recompute a materialized view when the base relations change
We can try to propagate view changes to the base relations However, there are lots of views that aren’t easily
updatable:
We can ensure views are updatable by enforcing certain constraints (e.g., no aggregation),but this limits the kinds of views we can have
A B
1 2
2 2
B C
2 4
2 3
R S A B C
1 2 4
1 2 3
2 2 4
2 2 3
R⋈S
delete?
17
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 relationsStore a relational view of XML
(or create an XML view of relations)
18
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!
19
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
20
The Simplest Way to Encode a Tree
You saw this one in your midterm:
<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?
21
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
22
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…
23
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
24
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
25
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…
26
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
27
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?
28
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!
29
“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
30
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
?
31
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
32
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?
33
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
34
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)
35
Wrapping up…
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
Next time: we’ll start reasoning about views, which involves the introduction of another query language!