Web Data Management. Tree Pattern Evaluation. Philippe Rigaux CNAM Paris & INRIA Saclay

Similar documents
Web Data Management. XML query evaluation. Philippe Rigaux CNAM Paris & INRIA Saclay

XML Storage and Indexing

XML APIs. Web Data Management and Distribution. Serge Abiteboul Philippe Rigaux Marie-Christine Rousset Pierre Senellart

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe. Slide 27-1

Outline. Depth-first Binary Tree Traversal. Gerênciade Dados daweb -DCC922 - XML Query Processing. Motivation 24/03/2014

CHAPTER 3 LITERATURE REVIEW

Evaluating XPath Queries

SFilter: A Simple and Scalable Filter for XML Streams

Web Data Management. Introduction. Philippe Rigaux CNAM Paris & INRIA Saclay

An Algorithm for Streaming XPath Processing with Forward and Backward Axes

XML: Extensible Markup Language

XPath. Web Data Management and Distribution. Serge Abiteboul Philippe Rigaux Marie-Christine Rousset Pierre Senellart

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

XML databases. Jan Chomicki. University at Buffalo. Jan Chomicki (University at Buffalo) XML databases 1 / 9

Updating XML with XQuery

Part V. SAX Simple API for XML. Torsten Grust (WSI) Database-Supported XML Processors Winter 2008/09 84

Part V. SAX Simple API for XML

THESE. Présentée en vue d obtention du grade de. Discipline : Informatique. par Nicoleta PREDA. Titre :

4 SAX. XML Application. SAX Parser. callback table. startelement! startelement() characters! 23

1 <?xml encoding="utf-8"?> 1 2 <bubbles> 2 3 <!-- Dilbert looks stunned --> 3

SAX Simple API for XML

Binary Trees, Binary Search Trees

XML Filtering Technologies

Ioana Manolescu is a researcher at INRIA Saclay, and the scientific leader of the LEO team, joint between INRIA and University Paris XI.

Databases and Information Systems 1

Data Exchange. Hyper-Text Markup Language. Contents: HTML Sample. HTML Motivation. Cascading Style Sheets (CSS) Problems w/html

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Binary Trees

CSI33 Data Structures

Part VII. Querying XML The XQuery Data Model. Marc H. Scholl (DBIS, Uni KN) XML and Databases Winter 2005/06 153

Trees. (Trees) Data Structures and Programming Spring / 28

Web Data Management. Text indexing with LUCENE (Nicolas Travers) Philippe Rigaux CNAM Paris & INRIA Saclay

Introduction to XQuery. Overview. Basic Principles. .. Fall 2007 CSC 560: Management of XML Data Alexander Dekhtyar..

CSI 3140 WWW Structures, Techniques and Standards. Representing Web Data: XML

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Databases and Information Systems XML storage in RDBMS and core XPath implementation. Prof. Dr. Stefan Böttcher

MANAGING INFORMATION (CSCU9T4) LECTURE 4: XML AND JAVA 1 - SAX

YFilter: an XML Stream Filtering Engine. Weiwei SUN University of Konstanz

A FRAMEWORK FOR EFFICIENT DATA SEARCH THROUGH XML TREE PATTERNS

Indexing Keys in Hierarchical Data

Navigation- vs. Index-Based XML Multi-Query Processing

Accelerating XML Structural Matching Using Suffix Bitmaps

XML Technologies. Doc. RNDr. Irena Holubova, Ph.D. Web pages:

Ecient XPath Axis Evaluation for DOM Data Structures

CMSC351 - Fall 2014, Homework #2

Database System Concepts

XQuery Semantics CSE 232B. March 31, 2016

Introduction to XML. Large Scale Programming, 1DL410, autumn 2009 Cons T Åhs

Part XII. Mapping XML to Databases. Torsten Grust (WSI) Database-Supported XML Processors Winter 2008/09 321

12 Abstract Data Types

XML Path Matching: Implementing the X-scan operator and investigating ways to optimize X-scan

Data Structure. IBPS SO (IT- Officer) Exam 2017

One of the main selling points of a database engine is the ability to make declarative queries---like SQL---that specify what should be done while

Chapter 20: Binary Trees

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Document Parser Interfaces. Tasks of a Parser. 3. XML Processor APIs. Document Parser Interfaces. ESIS Example: Input document

Course: The XPath Language

Semi-structured Data. 8 - XPath

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Simple API for XML (SAX)

A Simple Syntax-Directed Translator

.. Cal Poly CPE/CSC 366: Database Modeling, Design and Implementation Alexander Dekhtyar..

Lazy Query Evaluation for Active XML

2.2 Syntax Definition

The Lorax Programming Language

Experience with XML Signature and Recommendations for future Development

CS301 - Data Structures Glossary By

Making XT XML capable

An Effective and Efficient Approach for Keyword-Based XML Retrieval. Xiaoguang Li, Jian Gong, Daling Wang, and Ge Yu retold by Daryna Bronnykova

Efficient pebbling for list traversal synopses

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

The XQuery Data Model

singly and doubly linked lists, one- and two-ended arrays, and circular arrays.

Syntax-Directed Translation. Lecture 14

XML TUPLE ALGEBRA. Ioana Manolescu INRIA Futurs, Gemo group, France

CS61B Lecture #20: Trees. Last modified: Wed Oct 12 12:49: CS61B: Lecture #20 1

EMERGING TECHNOLOGIES

Querying XML. COSC 304 Introduction to Database Systems. XML Querying. Example DTD. Example XML Document. Path Descriptions in XPath

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Intermediate Code Generation

Data Structures and Algorithms (DSA) Course 9 Lists / Graphs / Trees. Iulian Năstac

Navigating Input Documents Using Paths4

H2 Spring B. We can abstract out the interactions and policy points from DoDAF operational views

Intro to XML. Borrowed, with author s permission, from:

Index-Driven XQuery Processing in the exist XML Database

Retrieving Meaningful Relaxed Tightest Fragments for XML Keyword Search

Query Languages for XML

CS61B Lecture #20. Today: Trees. Readings for Today: Data Structures, Chapter 5. Readings for Next Topic: Data Structures, Chapter 6

Course: The XPath Language

Draw a diagram of an empty circular queue and describe it to the reader.

Supporting Positional Predicates in Efficient XPath Axis Evaluation for DOM Data Structures

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Semantic actions for expressions

XPath. Asst. Prof. Dr. Kanda Runapongsa Saikaew Dept. of Computer Engineering Khon Kaen University

Chapter 13 XML: Extensible Markup Language

TwigList: Make Twig Pattern Matching Fast

Saving Space and Time Using Index Merging

Query Processing & Optimization

UNIT 3 XML DATABASES

Transcription:

http://webdam.inria.fr/ Web Data Management Tree Pattern Evaluation Serge Abiteboul INRIA Saclay & ENS Cachan Ioana Manolescu INRIA Saclay & Paris-Sud University Philippe Rigaux CNAM Paris & INRIA Saclay Marie-Christine Rousset Grenoble University Pierre Senellart Télécom ParisTech Copyright @2011 by Serge Abiteboul, Ioana Manolescu, Philippe Rigaux, Marie-Christine Rousset, Pierre Senellart; to be published by Cambridge University Press 2011. For personal use only, not for distribution. http://webdam.inria.fr/jorge/

For personal use only, not for distribution. 2 Contents 1 Tree-pattern dialects 2 2 CTP evaluation 5 3 Extensions 9 In this chapter, we learn how to build an evaluation engine for tree-pattern queries, using the SAX (Simple API for XML) programming model. We thereby follow a dual goal: (i) improve our understanding of XML query languages and (ii) become familiar with SAX, a stream parser for XML, with an event-driven API. Recall that the main features of SAX were presented in Section??. 1 Tree-pattern dialects We will consider tree-pattern languages of increasing complexity. We introduce them in this section. C-TP This is the dialect of conjunctive tree-patterns. A C-TP is a tree, in which each node is labeled either with an XML element name, or with an XML attribute name. C-TP nodes corresponding to attributes are distinguished by prefixing them with @, e.g., @color. Each node has zero or more children, connected by edges that are labeled either / (with the semantics of child) or // (with the semantics of descendant). Finally, the nodes that one wants to be returned are marked. As an example, Figure 1 shows a simple XML document d where each node is annotated with its preorder number. (Recall the definition of this numbering from Section??.) Figure 2 shows a C-TP pattern denoted t 1 and the three tuples resulting from matchings of t 1 into d. A matching v is a mapping from the nodes in the tree-pattern to the nodes in the XML tree that verifies the following conditions: For each nodes n,m, If n is labelled l for some l, v(n) is an element node labelled l; If n is labelled @l, v(n) is an attribute node labelled @l; If there is a / edge from n to m, v(n) is a parent of v(m); If there is a // edge from n to m, v(n) is an ancestor of v(m). In the figure, the nodes that we want to be returned are marked by boxes surrounding their labels. Observe that a result is a tuple of nodes denoted using their preorder numbers. For now, assume that C-TPs return tuples of preorder numbers. In real-world scenarios, of course, we may also want to retrieve the corresponding XML subtrees, and at the end of this chapter, the reader will be well-equipped to write the code that actually does it. Before moving on and extending our language of tree-pattern queries, we next observe an important aspect of the language. For that consider the following three queries:

For personal use only, not for distribution. 3 XML document d 1 people 2 person 7 person 11 person 3 4 name 8 name 12 13 14 name m@home 5 first 6 Mary Jones 9 first 10 15 first 16 a@home Bob Lang Al Hart a@work Figure 1: A sample document. Query q 1 : for $p in //person[] [name/] return ($p//, $p/name/) Query q 1 : for $p in //person return ($p//, $p/name/) Query q 2 : for $p in //person[name/] return ($p//, $p/name/) Which one do you think corresponds to the tree-pattern query t 1? Well, it is the first one. In q 1, the for clause requires the elements matching $p to have both an e-mail descendant, and a descendant on the path name/. Similarly, to obtain a matching from t 1 and d, we need matches on both paths. In contrast, consider the more relaxed query q 1 that would output the name of a person element without an, or the of a person element without name. Lastly, query q 2 requires a name but no. This motivates an extension of the language we consider next. TP The dialect TP is a superset of C-TP, extending it to allow optional edges. Syntactically, TP distinguishes between two kinds of edges, compulsory and optional edges. In a nutshell, the semantics of TP is defined as follows. Matchings are defined as for C-TP. The only difference is that for an optional child edge from n to m, two cases can be considered: If v(n) has some child v(m) such that there is a matching from the subtree of the query rooted at m and the subtree of d rooted at v(m); then v extends such a matching from

For personal use only, not for distribution. 4 Tree pattern t 1 : person Embeddings of t 1 in d person name 2 3 4 6 11 12 14 16 11 13 14 16 name Expected result t 1 (d) 3 6 12 16 13 16 Figure 2: A C-TP and its result for a sample document. Tree-pattern t 2 : person name Embeddings of t 2 in d Expected result t 2 (d) person name 2 3 4 6 7 null 8 10 11 12 14 16 11 13 14 16 3 6 null 10 12 16 13 16 Figure 3: A TP and its result for the sample document in Figure 1. the m-subtree to the v(m)-subtree. Or v(n) has no such child, then v has a null value for m and all its descendants. And similarly for descendant. As an example, Figure 3 shows the TP pattern t 2 corresponding to the previously see query q 2. It resembles t 1 but the edge between the person and nodes is optional (denoted by the dashed lines in the figure). As the figure shows, the second person element from the document in Figure 1 lacks an, which leads to a matching tuple with a null. As a consequence, one of the tuples in t 2 (d) contains a null. To conclude this section, we consider three somehow orthogonal extensions of both TP and C-TP. Figure 4 shows the extended tree-pattern t 3,t 4 and t 5, together with their equivalent queries, respectively q 3,q 4 and q 5, and their results for the sample document in Figure 1. The three extensions are: Star (*) patterns We allow labels to be not only element or attribute names, but also * that are interpreted as wild cards matched by any element or attribute. Value predicate patterns We can impose a constraint on nodes using some predicates. For instance, we can impose the value of an, e.g, = m@home.

For personal use only, not for distribution. 5 Query q 3 : for $p in //person[] return $p//name/* Equivalent tree pattern t 3 : person name Evaluation result of t 3 (d): * 5 6 9 10 15 16 * Query q 4 : for $p in //person[//first] [//] where $p/= m@home return ($p//first, $p//) Equivalent tree pattern t 4 : person = m@home first Evaluation result of t 4 (d): first 5 6 Figure 4: Extended patterns. Query q 5 : for $p in //person where $p/= m@home return <res>{$p/*/</res> Equivalent tree pattern t 5 : person = m@home * Evaluation result of t 5 (d): <>Jones</> Patterns returning full elements Finally, we can request the tree-pattern to not only return the preorder values of nodes but the entire corresponding subtrees. For simplicity, we do not introduce any explicit graphical notation for requesting that in answers. In Figure 4, the result of t 5 (d) assumes that full answers are requested. From a practical perspective of XML querying, all these extensions are interesting. In particular, C-TP with all the extensions corresponds to a large and useful subset of XPath, whereas its counterpart building on TP is at the core of an important expressive fragment of XQuery. We first consider the evaluation of C-TP. Then look at extensions. 2 CTP evaluation We now describe an algorithm, named StackEval, for evaluating a C-TP pattern t on a document d in a single SAX-based traversal of the document. To understand the algorithm, it is useful to have in mind that a node matches both because it satisfies some ancestor condition (the path from the root has a certain pattern) and also because its descendants satisfy some descendant conditions. We know whether an XML node satisfies some ancestor conditions, by the time we encounter the opening tag of the node, because by that time, all its ancestors have been encountered already. However, we can only decide if the node satisfies descendant conditions after the complete traversal of all its descendants, that is, when encountering the closing tag of the node. We start by describing some data structures we will use: When a node n d in document d is found to satisfy the ancestor conditions related to a node n t in t, a Match object is created to record this information. A Match holds the following:

For personal use only, not for distribution. 6 class Match { int start; int state; Match parent; Map <PatternNode, Array<Match> > children; TPEStack st; int getstatus() {... class TPEStack { PatternNode p; Stack <Match> matches; TPEStack spar; Array <TPEStack> getdescendantstacks(); {... // gets the stacks for all descendants of p push(match m){ matches.push(m); Match top(){ return matches.top(); Match pop(){ return matches.pop(); Table 1: Outline of the Match and TPEStack classes. the start number of node n d (an integer). an internal state flag, whose value can be either open or closed. (ancestor conditions) the Match parent that corresponds to a match between the parent (or an ancestor) of n d, and the parent of n t in the tree pattern query. If the edge above n t is a parent edge, then parent can only correspond to the parent node of n d. Otherwise, parent may be built from the parent or another ancestor of n d. (descendant conditions) the (possibly empty) array of matches that were created out of n d s children or descendants, for each child of n t in the query. Such matches for n t children are mapped in the children structure on the PatternNode children of n t. a pointer st to a TPEStack (standing for tree pattern query evaluation stack). As we will see, we associate a TPEStack to each node in the pattern. Then st points to the stack associated to the pattern node n t. For each node p in the tree-pattern, a TPEStack is created, on which the matches corresponding to this pattern node are pushed as they are created. Observe that a TPEStack contains a regular Stack in which Match objects are pushed and from which they are popped. The extra structure of a TPEStack serves to connect them to each other according to the structure or the query. More specifically, each TPEStack corresponding to a pattern node p points to:

For personal use only, not for distribution. 7 the TPEStack spar corresponding to the parent of p, if p is not the pattern root. a set childstacks of the TPEStacks corresponding to the children of p, if any. The Match and TPEStack structures are interconnected, i.e., each Match points to the (unique) TPEStack on which the Match has been pushed upon creation. The main features of the Match and TPEStack classes are summarized in Table 1. In our discussion of the algorithms, unless otherwise specified, we use the term stack to refer to a TPEStack. The StackEval algorithm The algorithm evaluates C-TP queries based on the SAX XML document processing model. More specifically, the query evaluation algorithm runs suitable handlers of the methods: startelement endelement (String namespaceuri, String localname, String rawname, Attribute attributes) (String namespaceuri, String localname, String rawname) described in Section??. For simplicity, within startelement we only use the localname and Attributes, whereas from the parameters of endelement we only use localname. As a consequence, the evaluation algorithm we describe does not take into account namespaces. Extending it to include the support of namespaces does not raise interesting algorithmic issues. The StackEval class (Table 2) contains the stack corresponding to the query root. It also stores an element counter called currentpre, from which pre number will be assigned to new Match objects. Finally, it stores a stack of all pre numbers of elements currently open but whose end has not been encountered yet. The startelement handler is notified that an element with a given localname and a set of attributes has just started. The handler seeks to identify the stack (or stacks) associated to query nodes which the newly started element may match. To this purpose, it enumerates all the stacks created for the query (by getting all descendants of the root stack), and for each stack it checks two conditions. The first condition is that the label of the starting node matches the label of the query nodes for which the stacks were created. A second condition applies in the case of a stack s created for a query node p having a parent in the query: we push a new match on s if and only if there is an open match on the parent stack of s, namely p.spar. Such an open match signifies that the newly started element appears in the right context, i.e. all the required ancestors have been matched above this element. In this case, a Match is created with the current pre number (which is incremented). The Match is open by default when created. Finally, the currentpre and preofopennodes are updated to reflect the new element. Since tree-patterns may also require matches in XML attributes, the next lines in the startelement handler repeat the previously described procedure for each of the attributes whose presence is signaled in the same call to startelement. The endelement handler (Table 3) plays a dual role with respect to the startelement one. Ancestor conditions for a potential query node match are enforced by startelement when the element starts; descendant conditions are checked by endelement when the element s

For personal use only, not for distribution. 8 class StackEval extends DocumentHandler { TreePattern q; TPEStack rootstack; // stack for the root of q // pre number of the element which has started: int currentpre = 0; // pre numbers for all elements having started but not ended yet: Stack <Integer> preofopennodes; startelement(string localname, Attribute attributes){ for(s in rootstack.getdescendantstacks()){ if(localname == s.p.name && s.spar.top().status == open){ Match m = new Match(currentPre, s.spar.top(), s); // create a match satisfying the ancestor conditions // of query node s.p s.push(m); preofopennodes.push(currentpre); currentpre ++; for (a in attributes){ // similarly look for query nodes possibly matched // by the attributes of the currently started element for (s in rootstack.getdescendantstacks()){ if (a.name == s.p.name && s.par.top().status == open){ Match ma = new Match(currentPre, s.spar.top(), s); s.push(ma); currentpre ++; Table 2: StartElement handler for the StackEval tree-pattern evaluation algorithm. traversal has finished, because at this time, all the descendants of the XML node for which the match was created have been traversed by the algorithm. Thus, we know for sure what parts of the queries could be matched in the descendants of the current node. The endelement handler plays two roles: prune out of the stacks those matches which satisfied the ancestor constraints but not the descendant constraints; close all Match objects corresponding to the XML element which has just finished (there may be several such matches, if several query nodes carry the same label). Closing these Matches is important as it is required in order for future tests made by the startelement handler to work.

For personal use only, not for distribution. 9 class StackEval{... endelement(string localname){ // we need to find out if the element ending now corresponded // to matches in some stacks // first, get the pre number of the element that ends now: int preofopen = preofopennodes.pop(); // now look for Match objects having this pre number: for(s in rootstack.getdescendantstacks()){ if (s.p.name == localname && s.top().status == open &&) s.top().pre == preoflastopen){ // all descendants of this Match have been traversed by now. Match m = s.pop(); // check if m has child matches for all children // of its pattern node for (pchild in s.p.getchildren()){ // pchild is a child of the query node for which m was created if (m.children.get(pchild) == null){ // m lacks a child Match for the pattern node pchild // we remove m from its Stack, detach it from its parent etc. remove(m, s); m.close(); Table 3: EndElement handler for the StackEval tree-pattern evaluation algorithm. Instructions Based on the previous explanation: 1. Implement an evaluation algorithm for C-TP tree-patterns. At the end of the execution, the stacks should contain only those Match objects that participate to complete query answers. 2. Implement an algorithm that computes the result tuples of C-TP tree patterns, out of the stacks content. 3 Extensions to richer tree-patterns Once this is implemented, the reader might want to consider implementing the extensions previously outlined. For all these extensions, a single traversal of the document suffices. More precisely, one can consider: 1. Extend the evaluation algorithm developed in (1.) at the end of the previous section to

For personal use only, not for distribution. 10 * wildcards. For this, Stack objects are allowed to be created for *-labeled query tree nodes. Also the startelement and endelement handlers are adapted. 2. Extend the evaluation algorithm to optional nodes, by modifying the tests performed in endelement (looking for children which the Match should have) to avoid pruning a Match if only optional children are missing. 3. Extend the algorithm developed in (2.) to handle optional nodes, by filling in partial result tuples with nulls as necessary. 4. Extend the evaluation algorithm to support value predicates, by (i) implementing a handler for the characters(...) SAX method, in order to record the character data contained within an XML element and (ii) using it to compare the text values of XML elements for which Match objects are created, to the value predicates imposed in the query. 5. Extend the algorithm in (2.) to return subtrees and not only preorder numbers. The subtrees are represented using the standard XML syntax with opening/closing tags.