XQuery with User-defined Functions David Bednárek ulita.ms.mff.cuni.cz/~bednarek Department of Software Engineering Faculty of Mathematics and Physics Charles University Prague
XQuery program with user-defined functions declare function local:toc($P) { for $X in $P/section return { $X/title, local:toc($X) } }; { for $S in $I/book return local:toc($S) }
XQuery with user-defined functions Turing-complete language Identification of patterns of "strong" computation Relational algebra representation Identifier handling Bulk-evaluation R-programs Temporary structures Static analysis Generation of the output document Reversed evaluation
Turing-completeness of XQuery Identification of patterns of "strong" computation
The simplest XQuery application XML document tree A B C D XQuery evaluation tree
The simplest XQuery application Cartesian product A B C D
The simplest XQuery application Two-dimensional attribute grammar Dependence analysis
The simplest XQuery application Turing-strong patterns Mesh Tape
Relational algebra representation Identifier handling
Example – input document and its Dewey encoding / 1book 1.5section 1.5.7title 1.5.8section title p 1.9section 1.9.4title 1.9.8p
XQuery program and its effect on encoded documents / 1book 1.5section 1.5.7title 1.5.8section title p 1.9section 1.9.4title 1.9.8p / ()toc ()((1)(1.5))section ()((1)(1.5))(2(1.5.7))title ()((1)(1.5))(3(1.5.8))section ()((1)(1.5))(2(1.5.7))(2( ))title ()((1)(1.9))section ()((1)(1.9))(2(1.9.4))title declare function local:toc($P) { for $X in $P/section return { $X/title, local:toc($X) } }; { for $S in $I/book return local:toc($S) }
Example – decoded output document / ()toc ()((1)(1.5))section ()((1)(1.5)) (2(1.5.7))title ()((1)(1.5)) (3(1.5.8))section ()((1)(1.5)) ()((1)(1.5)) (2(1.5.7))(2( ))title ()((1)(1.9))section ()((1)(1.9)) (2(1.9.4))title
Life cycle of identifiers
Bulk-evaluation R-programs
Handling recursion by expansion Bulk-evaluation by relational algebra
R-programs DAG of relational algebra operators Gradual expansion driven by triggers
Temporary structures Static analysis Published in: "Reducing Temporary Trees in XQuery" ADBIS 2008, Pori, Finland, LNCS
Static analysis algorithm Input: a XQuery program Output: a set of modes assigned to each sub- expression and each variable Mode determines a particular way of representation required Reversed evaluation is one of the modes Time O(n 2 ), space O(n)
Generation of the output document Reversed evaluation Published in: "Output-driven evaluation of XQuery" IDC 2008, Catania, Italy, Springer Verlag
Standard data flow declare function local:toc($P) { for $X in $P/section return { $X/title, local:toc($X) } }; { for $S in $I/book return local:toc($S) }
Reversed data flow
Future work Cost-based optimization in R-programs Distributed/parallel XQuery engine