1 module 10 xquery update, xqueryp disclaimer: work in progress!!!

54
1 Module 10 Module 10 XQuery Update, XQueryP XQuery Update, XQueryP Disclaimer: Work in Disclaimer: Work in progress!!! progress!!!

Upload: silvia-wilcox

Post on 04-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

1

Module 10Module 10

XQuery Update, XQueryPXQuery Update, XQueryP

Disclaimer: Work in Disclaimer: Work in progress!!!progress!!!

Page 2: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

2

Summary of M1-M9Summary of M1-M9 XML and XML SchemaXML and XML Schema

serialization of data (documents + structured serialization of data (documents + structured data)data)

mixing data from different sources (namespaces)mixing data from different sources (namespaces) validity data (constraints on structure)validity data (constraints on structure)

XQueryXQuery extracting, aggregating, processing (parts of) extracting, aggregating, processing (parts of) datadata

constructing new data; transformation of dataconstructing new data; transformation of data full-text search full-text search

Web Services and MashupsWeb Services and Mashups remote procedure calls on the Webremote procedure calls on the Web

(message format, service interfaces, broker)(message format, service interfaces, broker) Next: Updates and ScriptingNext: Updates and Scripting

bringing it all togheter!bringing it all togheter!

Page 3: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

XQuery Update XQuery Update FacilityFacility

Page 4: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

4

XQuery Updates OverviewXQuery Updates Overview Activity in W3C; work in progress (Activity in W3C; work in progress (~two years)~two years)

requirements, use cases, specification documentsrequirements, use cases, specification documents Use as transformation + DB operation (side-effect)Use as transformation + DB operation (side-effect)

Preserve Ids of affected nodes! (No Node Construction!)Preserve Ids of affected nodes! (No Node Construction!) Updates are expressions!Updates are expressions!

return “()” as resultreturn “()” as result in addition, return a in addition, return a Pending Update ListPending Update List

Updates are fully composable with other expr.Updates are fully composable with other expr. however, there are semantic restrictions!however, there are semantic restrictions! e.g., no update in condition of an if-then-else allowede.g., no update in condition of an if-then-else allowed

Primitive Updates: insert, delete, replace, renamePrimitive Updates: insert, delete, replace, rename Extensions to other expr: FLWOR, TypeSwitch, ...Extensions to other expr: FLWOR, TypeSwitch, ...

Page 5: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

5

ExamplesExamples do delete //book[@year lt 1968]do delete //book[@year lt 1968]

do insert <author/> into //book[@ISBN eq do insert <author/> into //book[@ISBN eq “34556”]“34556”]

for $x in //bookfor $x in //bookwhere $x/year lt 2000 and $x/price gt 100where $x/year lt 2000 and $x/price gt 100return do replace value of $x/price return do replace value of $x/price

with $x/price-0.3*$x/pricewith $x/price-0.3*$x/price

if ($book/price gt 200) then if ($book/price gt 200) then do rename $book as “expensive-book”do rename $book as “expensive-book”

The „do“ needed in syntax! (Don‘t ask, just The „do“ needed in syntax! (Don‘t ask, just do it!)do it!)

Page 6: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

6

OverviewOverview Insert: Insert new XML Insert: Insert new XML instancesinstances

Delete: Delete nodesDelete: Delete nodes Replace, Renam: Replace/Rename Replace, Renam: Replace/Rename nodesnodes

FLWOR Update: bulk update FLWOR Update: bulk update Conditional Updates:Conditional Updates:

if - then - elseif - then - else typeswitchtypeswitch

Comma ExpressionComma Expression Updating FunctionsUpdating Functions

Page 7: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

7

INSERT - Variant 1INSERT - Variant 1 Insert a new element into a documentInsert a new element into a documentdo insertdo insert InsertionSeqInsertionSeq intointo TargetNodeTargetNode

InsertionSeq: InsertionSeq: transform docs into their transform docs into their childrenchildren

TargetNode:TargetNode: Exactly one document or elementExactly one document or element otherwise ERRORotherwise ERROR

Specify whether to insert at the beginning or Specify whether to insert at the beginning or endend as last:as last: InsertionSeqInsertionSeq becomes first child of becomes first child of TargetTarget (default) (default)

as first:as first: InsertionSeqInsertionSeq becomes last child of becomes last child of TargetTarget

Nodes in Nodes in InsertionSeq InsertionSeq assume a new Id.assume a new Id. Whitespace, Textconventions as in Whitespace, Textconventions as in ElementConstruction of XQueryElementConstruction of XQuery

Page 8: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

8

INSERT Variant 1INSERT Variant 1 Insert new book at the end of the libraryInsert new book at the end of the library

do insert do insert <book> <title>Snowcrash</title> <book> <title>Snowcrash</title> </book></book>

intointo document(„www.uni-bib.ch“)//bib document(„www.uni-bib.ch“)//bib

Insert new book at the beginning of the Insert new book at the beginning of the librarylibrarydo insert do insert <book> <title>Snowcrash</title> <book> <title>Snowcrash</title> </book></book>

as first intoas first into document(„www.uni-bib.ch“)//bib document(„www.uni-bib.ch“)//bib

Insert new attribte into an elementInsert new attribte into an elementdo insertdo insert (attribute age { 13 }, <parents (attribute age { 13 }, <parents xsi:nil = „true“/>) xsi:nil = „true“/>)

intointo document(„ewm.de“)//person[@name = „KD“] document(„ewm.de“)//person[@name = „KD“]

Page 9: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

9

INSERT - Variant 2INSERT - Variant 2 Insert at a particular point in the documentInsert at a particular point in the documentdo insertdo insert InsertionSeq InsertionSeq (after | before)(after | before) TargetNodeTargetNode

Subtleties in InsertionSeqSubtleties in InsertionSeq No attributes allowed after an element!No attributes allowed after an element! Document nodes are transformed into their Document nodes are transformed into their childrenchildren

TargetNode:TargetNode: One Element, Comment or PI.One Element, Comment or PI. Otherwise ERROROtherwise ERROR

Specify whether before or behind target Specify whether before or behind target Before vs. AfterBefore vs. After

Nodes in Nodes in InsertionSeqInsertionSeq assume new Identity assume new Identity Whitespace, Text conventions as Whitespace, Text conventions as ElementConstructors of XQueryElementConstructors of XQuery

Page 10: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

10

Insert - Variant 2Insert - Variant 2

Add an author to a bookAdd an author to a book

do insertdo insert <author>Florescu</author> <author>Florescu</author>

beforebefore //article[title = „XL“]/author[. eq „Grünhagen“] //article[title = „XL“]/author[. eq „Grünhagen“]

Page 11: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

11

INSERT - Open INSERT - Open QuestionsQuestions

Insert into schema-validated Insert into schema-validated instances?instances? When and how to validate types?When and how to validate types? What is the type of the updated What is the type of the updated instance?instance?

Insert (V2): TargetNode has no Insert (V2): TargetNode has no Parent?Parent? Is that an error?Is that an error?

TargetNode is empty?TargetNode is empty? Is that an error or a no-operation?Is that an error or a no-operation?

Page 12: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

12

DELETEDELETE Delete nodes from an instanceDelete nodes from an instance

do deletedo delete TargetNodesTargetNodes TargetNodesTargetNodes:: sequence of nodes (no sequence of nodes (no values!)values!)

Delete XML papers.Delete XML papers.delete delete //article[header/keyword = „XML“]//article[header/keyword = „XML“]

Deletes 2‘s from (1, 1, 2, 1, 2, 3) Deletes 2‘s from (1, 1, 2, 1, 2, 3) not possiblenot possible need to construct new seqeunce with need to construct new seqeunce with FLWORFLWOR

Page 13: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

13

REPLACEREPLACE Variant 1: Replace a nodeVariant 1: Replace a node

do replacedo replace TargetNodeTargetNode with with UpdateContentUpdateContent Variant 2: Replace the content of a Variant 2: Replace the content of a nodenodedo replace value ofdo replace value of TargetNodeTargetNode withwith UpdateContentUpdateContent

TargetNode:TargetNode: One node (with Id)One node (with Id) UpdateContent:UpdateContent: Any sequence of items Any sequence of items Whitespace and Text as with inserts.Whitespace and Text as with inserts. Many subteltiesMany subtelties

in UpdateContent, replace document with its in UpdateContent, replace document with its childrenchildren

can only replace one node by another node (of can only replace one node by another node (of similar kind)similar kind)

Page 14: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

14

RENAMERENAME

Give a node a new nameGive a node a new namedo renamedo rename TargetTarget asas NewNameNewName

TargetTarget must be attribute, element, or must be attribute, element, or PIPI

NewNameNewName must be an expression that must be an expression that evaluates to a qname (or castable)evaluates to a qname (or castable)

First author of a book is principle First author of a book is principle author:author:do rename //book[1]/author[1]do rename //book[1]/author[1]

as „principle-author“as „principle-author“

Page 15: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

15

ComposabilityComposability

Insert, delete, renameInsert, delete, rename, , replace, and replace, and calls to updating functionscalls to updating functions are are expressionsexpressions

They are They are notnot fully composable with the fully composable with the rest rest Semantic, not syntactic restrictionsSemantic, not syntactic restrictions

Side-effectingSide-effecting expressions only allowed expressions only allowed inin ““return” clause of a FLWORreturn” clause of a FLWOR ““then” and “else” branches of a conditionalthen” and “else” branches of a conditional the body of a function the body of a function within a typeswitch or stand-alonewithin a typeswitch or stand-alone only in “control-flow” style expressionsonly in “control-flow” style expressions

Page 16: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

16

Bulk Updates: FLWOR Bulk Updates: FLWOR UpdateUpdate

INSERT and REPLACE operate on ONE INSERT and REPLACE operate on ONE node!node!

Idea: Adopt FLWOR Syntax from XQueryIdea: Adopt FLWOR Syntax from XQuery(ForClause | LetClause)+ WhereClause? (ForClause | LetClause)+ WhereClause? OrderBy? OrderBy? return return SimpleUpdateSimpleUpdate

SimpleUpdate:SimpleUpdate: insert, delete, replace, or insert, delete, replace, or renamerename

Semantics: Carry out Semantics: Carry out SimpleUpdateSimpleUpdate for for every node bound by every node bound by FLWFLW.. Quiz: Does an OrderBy make sense here?Quiz: Does an OrderBy make sense here?

Page 17: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

17

FLWOR Update - FLWOR Update - ExamplesExamples

„„Müller“ marries „Lüdenscheid“.Müller“ marries „Lüdenscheid“.

for $n in //article/author/lastnamefor $n in //article/author/lastname

where $n/text() eq „Müller“ return dowhere $n/text() eq „Müller“ return do

replace value ofreplace value of $n $n withwith „Müller- „Müller-Lüdenscheid“Lüdenscheid“

Value-added tax of 19 percent.Value-added tax of 19 percent.

for $n in //book return dofor $n in //book return do

insert insert attribute vat { $n/@price * 0.19 } attribute vat { $n/@price * 0.19 } into into $n$n

Page 18: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

18

Snapshot SemanticsSnapshot Semantics Updates are applied at the very endUpdates are applied at the very end

inserts are not visible during executioninserts are not visible during execution avoids Halloween problemavoids Halloween problem allows optimizations (change order of updates)allows optimizations (change order of updates)

Three stepsThree steps evaluate expr; compose pending update list evaluate expr; compose pending update list (PUL)(PUL)

append „primitive“ to PUL in every iteration of FORappend „primitive“ to PUL in every iteration of FOR conformance test of PULconformance test of PUL

avoid duplicate updates to same node (complicated avoid duplicate updates to same node (complicated rule)rule)

avoids indeterminism due to optimizationsavoids indeterminism due to optimizations apply PUL (update primitives one at a time)apply PUL (update primitives one at a time)

Page 19: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

19

Halloween ProblemHalloween Problem

for $x in $db/*for $x in $db/*

return do insert $x into $dbreturn do insert $x into $db

Obviously, not a problem with Obviously, not a problem with snapshot semantics.snapshot semantics.

(SQL does the same!)(SQL does the same!)

Page 20: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

20

Conditional UpdateConditional Update

Adopted from XQuery‘s Adopted from XQuery‘s if then if then else else expr.expr. if (condition) thenif (condition) then

SimpleUpdateSimpleUpdate

elseelse

SimpleUpdateSimpleUpdate

Page 21: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

21

TransformationsTransformations Update streaming data - create new Update streaming data - create new instancesinstancestransform copytransform copy Var := SExprVar := SExpr modifymodify UExprUExpr returnreturn RExprRExpr

Delete salary of Java programmersDelete salary of Java programmersfor $e in //employee[skill = „Java“] returnfor $e in //employee[skill = „Java“] returntransform copy $je := $e transform copy $je := $e modifymodify do delete $je/salarydo delete $je/salaryreturn $jereturn $je

SExpr: SExpr: Source expression - what to Source expression - what to transformtransform

UExpr: UExpr: Update expression - updateUpdate expression - update RExpr:RExpr: Return expression - result Return expression - result returnedreturned

Page 22: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

22

Further Update Further Update ExpressionsExpressions

Comma ExpressionComma Expression Compose several updates (sequence of Compose several updates (sequence of updates)updates)

for $x in //booksfor $x in //books

return do delete $x/price, do delete return do delete $x/price, do delete $x/currency$x/currency

Typeswitch ExpressionTypeswitch Expression Carry out updates depending on the typeCarry out updates depending on the type

Function Declaration + Function CallFunction Declaration + Function Call Declare functions with side-effectsDeclare functions with side-effects Impacts optimization and exactly-once Impacts optimization and exactly-once semanticssemantics

Page 23: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

23

ImplementationsImplementations MXQuery (www.mxquery.org)MXQuery (www.mxquery.org)

implements full XQuery Update Facilityimplements full XQuery Update Facility but, limitations in how to bind data to but, limitations in how to bind data to update to variablesupdate to variables

but, MXQuery only implements subset of but, MXQuery only implements subset of XQueryXQuery

MXQuery is an MXQuery is an release; bleeding edge release; bleeding edge Most database vendors have a proprietary Most database vendors have a proprietary update language update language developed before the working drafts were developed before the working drafts were releasedreleased

need time to adjust to W3C recommendationneed time to adjust to W3C recommendation need to guarantee compatibility for need to guarantee compatibility for customers customers

Page 24: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

XQueryPXQueryP

Page 25: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

25

ObservationObservation Despite of XQuery and XQuery Updates, Despite of XQuery and XQuery Updates, we still need Javawe still need Java implement user interfacesimplement user interfaces call Web services; interact with other call Web services; interact with other programsprograms

expose functions as Web serviceexpose functions as Web service write complex applicationswrite complex applications

Once you start using Java, you are Once you start using Java, you are tempted to do everything in Java (-> tempted to do everything in Java (-> your projects :-) )your projects :-) )

Goal: Get rid of Java!!! All XQuery!Goal: Get rid of Java!!! All XQuery! XQueryP: Extension of XQuery for scriptingXQueryP: Extension of XQuery for scripting

Page 26: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

26

XQueryP OverviewXQueryP Overview Sequential Mode: Visibility of UpdatesSequential Mode: Visibility of Updates

define order in which expressions are define order in which expressions are evaluatedevaluated

fine-graned snapshot (update primitive)fine-graned snapshot (update primitive) New expressionsNew expressions

Assignment, Block, While, Break, Continue, Assignment, Block, While, Break, Continue, ReturnReturn

Error handling (try-catch)Error handling (try-catch) Graphs: references and de-referencingGraphs: references and de-referencing Web Service Import, Call, and ExportWeb Service Import, Call, and Export

Page 27: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

27

Sequential evaluation Sequential evaluation orderorder

Slight modification to existing rules:Slight modification to existing rules: FLWOR: FLWO clauses are evaluated first; FLWOR: FLWO clauses are evaluated first; result in a tuple stream; then Return clause result in a tuple stream; then Return clause is evaluated in order for each tuple. Side-is evaluated in order for each tuple. Side-effects made by one row are visible to the effects made by one row are visible to the subsequent rows.subsequent rows.

COMMA: subexpressions are evaluated in orderCOMMA: subexpressions are evaluated in order (UPDATING) FUNCTION CALL: arguments are (UPDATING) FUNCTION CALL: arguments are evaluated first before body gets evaluatedevaluated first before body gets evaluated

Required (only) if we add side-effects Required (only) if we add side-effects immediately visible to the program: e.g. variable immediately visible to the program: e.g. variable assignmentsassignments or or single snapshot atomic updates; single snapshot atomic updates; otherwise semantics not deterministic.otherwise semantics not deterministic.

Page 28: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

28

Reduce snapshot Reduce snapshot granularitygranularity

Today update snapshot: entire queryToday update snapshot: entire query Change:Change:

Every single atomic update expression Every single atomic update expression (insert, delete, rename, replace) is (insert, delete, rename, replace) is executed and made effective immediatelyexecuted and made effective immediately

The effects of side-effecting external The effects of side-effecting external functions are visible immediatelyfunctions are visible immediately

Semantics is deterministic because Semantics is deterministic because of the sequential evaluation order of the sequential evaluation order (point1)(point1)

Page 29: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

29

Sequential evaluation Sequential evaluation mode and the FLWORmode and the FLWOR

for $x in <for $x in <expressionexpression/>/>let $y := <let $y := <expressionexpression/>/>where <where <expressionexpression/>/>order by <order by <expressionexpression/>/>returnreturn<<side-effecting expressionside-effecting expression/>/>

No side-effects are visible until here.No side-effects are visible until here.

$ x$ x $ y$ y

Page 30: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

30

Adding new expressionsAdding new expressions Assignment expressionsAssignment expressions Block expressionsBlock expressions While expressionsWhile expressions Break,Continue, ReturnBreak,Continue, Return

Only under sequential evaluation Only under sequential evaluation modemode

Page 31: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

31

Assignment ExpressionAssignment Expression Syntax:Syntax:

““set” $VarName “:=“ ExprSingleset” $VarName “:=“ ExprSingle Semantics:Semantics:

Change the value of the variable Change the value of the variable Variable has to be external or Variable has to be external or declared in a block (no let, for, or declared in a block (no let, for, or typeswitch)typeswitch)

Updating expressionUpdating expression Semantics is deterministic because Semantics is deterministic because of the sequential evaluation orderof the sequential evaluation order restricted side-effects in ExprSingle: restricted side-effects in ExprSingle: only one side-effecting expression only one side-effecting expression (primitive) allowed!(primitive) allowed!

Page 32: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

32

Block expressionBlock expression Syntax:Syntax:

“ “{“ ( BlockDecl “;”)* Expr (“;” Expr)* “}”{“ ( BlockDecl “;”)* Expr (“;” Expr)* “}”BlockDecl := BlockDecl := (“declare” $VarName TypeDecl? (“:=“ (“declare” $VarName TypeDecl? (“:=“

ExprSingle) ?)?ExprSingle) ?)? (“,” $VarName TypeDecl? (“:=“ (“,” $VarName TypeDecl? (“:=“

ExprSingle) ? )*ExprSingle) ? )* Semantics:Semantics:

Declare a set of updatable variables, whose scope Declare a set of updatable variables, whose scope is only the block expression (in order)is only the block expression (in order)

Evaluate each expression (in order) and make the Evaluate each expression (in order) and make the effects visible immediatelyeffects visible immediately

Return the value of the last expressionReturn the value of the last expression Updating if body contains an updating Updating if body contains an updating

expressionexpression Optional “atomic” makes updates in block all Optional “atomic” makes updates in block all

or nothing (nothing, if an error occurs)or nothing (nothing, if an error occurs)

Page 33: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

33

Atomic BlocksAtomic Blocks Syntax:Syntax:

““atomic” “{“ . . . “}”atomic” “{“ . . . “}” Semantics:Semantics:

If the evaluation of Expr does not raise If the evaluation of Expr does not raise errors, then result is returnederrors, then result is returned

If the evaluation of Expr raises a If the evaluation of Expr raises a dynamic error then no partial side-dynamic error then no partial side-effects are performed (all are rolled effects are performed (all are rolled back) and the result is the errorback) and the result is the error

Only the largest atomic scope is Only the largest atomic scope is effectiveeffective

Note: XQuery! had a similar constructNote: XQuery! had a similar construct Snap {…} vs. atomic {…}Snap {…} vs. atomic {…}

Page 34: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

34

Functions and blocksFunctions and blocks Blocks are the body of functionsBlocks are the body of functions We relax the fact the a function cannot We relax the fact the a function cannot

update some nodes update some nodes andand return a value return a valuedeclare updating function local:prune($d as declare updating function local:prune($d as

xs:integer) as xs:integerxs:integer) as xs:integer

{{

declare $count as xs:integer := 0;declare $count as xs:integer := 0;

for $m in /mail/message[date lt $d]for $m in /mail/message[date lt $d]

return { do delete $m; return { do delete $m;

set $count := $count + 1 set $count := $count + 1

};};

$count$count

}}

Page 35: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

35

While expressionWhile expression Syntax:Syntax:

““while” “(“ while” “(“ exprSingleexprSingle “)” “return” “)” “return” exprexpr Semantics:Semantics:

Evaluate the test conditionEvaluate the test condition If “If “truetrue” then evaluate the return ” then evaluate the return clause; repeatclause; repeat

If “If “falsefalse” return the concatenation of ” return the concatenation of the values returned by all previous the values returned by all previous evaluations of returnevaluations of return

Syntactic sugar, mostly for Syntactic sugar, mostly for convenienceconvenience Could be written using recursive Could be written using recursive functionsfunctions

Page 36: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

36

Break, Continue, ReturnBreak, Continue, Return Traditional semantics, nothing Traditional semantics, nothing surprisingsurprising

BreakBreak (or (or continuecontinue) the closest FLWOR ) the closest FLWOR or WHILE iterationor WHILE iteration

ReturnReturn: early exit from a function : early exit from a function bodybody

Hard(er) to implement in a “database” Hard(er) to implement in a “database” style evaluation enginestyle evaluation engine Because of the lazy evaluationBecause of the lazy evaluation

Page 37: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

37

ExampleExampledeclare updating function declare updating function

myNs:cumCost($projects) as element( )*myNs:cumCost($projects) as element( )*{{

declare $total-cost as xs:decimal :=0;declare $total-cost as xs:decimal :=0;for $p in $projects[year eq 2005]for $p in $projects[year eq 2005]return return

{set $total-cost := {set $total-cost := $total-cost+$p/cost;$total-cost+$p/cost;

<project><project><name>{$p/name}</name><name>{$p/name}</name><cost>{$p/cost}</cost><cost>{$p/cost}</cost><cumCost>{$total-cost}</cumCost><cumCost>{$total-cost}</cumCost>

<project><project>}}

}} XQuery: self join or recursive functionXQuery: self join or recursive function

Page 38: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

38

Putting everything Putting everything together: the sequential together: the sequential

modemode New setter in the prologNew setter in the prolog Syntax:Syntax:

“ “declare” “execution” “sequential”declare” “execution” “sequential” Granularity: query or moduleGranularity: query or module What does it mean:What does it mean:

Sequential evaluation mode for expressionsSequential evaluation mode for expressions Single atomic update snapshotSingle atomic update snapshot Several new updating expressions (blocks, Several new updating expressions (blocks,

set, while, break, continue)set, while, break, continue) If the query has no side-effects, If the query has no side-effects,

sequential mode is irrelevant, and sequential mode is irrelevant, and traditional optimizations are still traditional optimizations are still applicableapplicable

Page 39: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

39

Try-catchTry-catch Errors in XQuery 1.0, Xpath 2.0, XSLT Errors in XQuery 1.0, Xpath 2.0, XSLT 2.02.0 fn:error(err:USER0005, "Value out of range", $value)fn:error(err:USER0005, "Value out of range", $value)

Traditional design for try-catchTraditional design for try-catchtry ( try ( target-exprtarget-expr ) )catch ( $name as catch ( $name as QName1QName1, $desc, $obj ) , $desc, $obj )

return return handler-expr1handler-expr1catch ( $name as catch ( $name as QName2QName2, $desc, $obj ) , $desc, $obj )

return return handler-expr2handler-expr2. . .. . .default ( $name, $desc, $obj ) default ( $name, $desc, $obj )

return return general-handler-exprgeneral-handler-expr

ExampleExamplelet $x := let $x := expr expr return return

try ( <a>{ $x }</a> ) try ( <a>{ $x }</a> ) catch (err:XQTY0024) catch (err:XQTY0024) return <a> {$x[self::attribute()],$x[fn:not(self::attribute())]} return <a> {$x[self::attribute()],$x[fn:not(self::attribute())]} </a></a>

Page 40: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

40

Web ServicesWeb Services WS are the standard way of WS are the standard way of sendingsending and and receivingreceiving XML XML datadata

XQuery are the standard way to XQuery are the standard way to programprogram the XML the XML processingprocessing

We should design them We should design them consistently, consistently, natural fitnatural fitXQuery Web ServicesXQuery Web Servicesmodule servicemodule servicefunctions/operations operationsfunctions/operations operationsarguments portsarguments portsvalues for arguments and value for input and outputvalues for arguments and value for input and outputResult: Result: XMLXML messages: messages: XMLXML

XQueryP proposes:XQueryP proposes: A standard way of importing a Web Service into an XQuery A standard way of importing a Web Service into an XQuery programprogram

A standard way of invoking a WS operation as a normal functionA standard way of invoking a WS operation as a normal function A standard way of exporting an XQuery module as a Web ServiceA standard way of exporting an XQuery module as a Web Service

Many XQuery implementations already support this. We Many XQuery implementations already support this. We have to agree on ahave to agree on a standard. standard.

Page 41: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

41

Calling Google... Calling Google... import service namespace ws=„urn:GoogleSearch“ from

"http://api.google.com/GoogleSearch.wsdl";

declare execution sequential;

declare variable $result;declare variable $query;

set $query := mxq:readLine();set $result := ws:doGoogleSearch("oIqddkdQFHIlwHMXPerc1KlNm+FDcPUf", $query, 0,\10, fn:true(), "", fn:false(), "", "UTF-8", "UTF-8");

<results query="{$query}">{ for $url in $result/resultElements/item/URL return data($url)}</results>

Page 42: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

42

Defining a Web ServiceDefining a Web Serviceservice namespace eth="service namespace eth="www.ethz.chwww.ethz.ch" port:2001;" port:2001;

declare execution sequential;declare execution sequential;

declare function eth:mul($a,$b) {$a * $b};declare function eth:mul($a,$b) {$a * $b};

declare function eth:add($a,$b) {$a + $b};declare function eth:add($a,$b) {$a + $b};

declare function eth:sub($a,$b) {$a - $b};declare function eth:sub($a,$b) {$a - $b};

declare function eth:div($a,$b) {$a div $b};declare function eth:div($a,$b) {$a div $b}; Calling that Web Service...Calling that Web Service...import service namespace ab="import service namespace ab="www.ethz.chwww.ethz.ch" from " from ""http://localhost:2001/wsdlhttp://localhost:2001/wsdl";";

ab:div(ab:sub(ab:mul(ab:add(1,2),ab:add(3,4)),ab:div(ab:sub(ab:mul(ab:add(1,2),ab:add(3,4)),1),5)1),5)

Page 43: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

43

Bubblesort in XQueryPBubblesort in XQueryPdeclare execution sequential;declare execution sequential;declare variable $data := declare variable $data := (5,1,9,5,7,1,7,23,7,22,432,4,2,765,3);(5,1,9,5,7,1,7,23,7,22,432,4,2,765,3);

declare variable $len := 15;declare variable $len := 15;declare variable $changed := fn:true();declare variable $changed := fn:true();while($changed) return {while($changed) return {declare $i := 1;declare $i := 1;set $changed := fn:false();set $changed := fn:false();while ($i < $len) return {while ($i < $len) return {

if ($data[$i] > $data[$i + 1]) then {if ($data[$i] > $data[$i + 1]) then {declare $cur := $data[$i];declare $cur := $data[$i];set $changed := fn:true();set $changed := fn:true();do replace $data[$i] with $data[$i+1];do replace $data[$i] with $data[$i+1];do replace $data[$i+1] with $cur } do replace $data[$i+1] with $cur }

else();else();set $i := $i + 1 } };set $i := $i + 1 } };

Page 44: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

44

Adding references to Adding references to XMLXML

XML tree, not graphXML tree, not graph E/R model graph, not treeE/R model graph, not tree Inherent tension, XML Data Model is the Inherent tension, XML Data Model is the

source of the problem, not XQuerysource of the problem, not XQuery ExampleExample

let $x := <a><b/><a/> return let $x := <a><b/><a/> return <c>{$x/b)</c><c>{$x/b)</c> /* copy of <b/>*//* copy of <b/>*/

Nodes in XDM have node identifiersNodes in XDM have node identifiers Lifetime and scope of nodeids, implementation definedLifetime and scope of nodeids, implementation defined

XQueryP solution: XQueryP solution: fn:reffn:ref($x as node()) as xs:anyURI($x as node()) as xs:anyURI fn:dereffn:deref($x as xs:anyURI) as node()($x as xs:anyURI) as node()

Lifetime and scope of URIs, implementation Lifetime and scope of URIs, implementation defineddefined

Untyped references (URIs)Untyped references (URIs) No changes required to:No changes required to:

XML Schema, XDM Data Model, Xquery type systemXML Schema, XDM Data Model, Xquery type system NOT YET IMPLEMENTED IN MXQuery!!!NOT YET IMPLEMENTED IN MXQuery!!!

Page 45: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

45

XQueryP usage scenariosXQueryP usage scenarios XQueryP programs XQueryP programs in the browsersin the browsers

We all love Ajax (the results). A pain to program. We all love Ajax (the results). A pain to program. Really primitive as XML processing goes.Really primitive as XML processing goes.

Embedding XQueryP in browsersEmbedding XQueryP in browsers XQueryP code can take input data from WS, RSS XQueryP code can take input data from WS, RSS

streams, directly from databasesstreams, directly from databases Automatically change the XHTML of the pageAutomatically change the XHTML of the page

XQueryP programs XQueryP programs in the databasesin the databases Complex data manipulation executed directly inside Complex data manipulation executed directly inside

the databasethe database Takes advantage of the DB goodies, performance, Takes advantage of the DB goodies, performance,

scalability, security, etcscalability, security, etc XQueryP programs XQueryP programs in application serversin application servers

Orchestration of WS calls, together with data Orchestration of WS calls, together with data extraction for a variety of data sources extraction for a variety of data sources (applications, databases, files), and XML data (applications, databases, files), and XML data transformationstransformations

XML data mashupsXML data mashups

Page 46: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

46

Related workRelated work Programming for XML:Programming for XML:

Extensions to other programming languagesExtensions to other programming languages Xlinq, ECMAScript, PhP, XJ, etcXlinq, ECMAScript, PhP, XJ, etc

Extensions to XQueryExtensions to XQuery XL, XQuery!, MarkLogic’s extensionXL, XQuery!, MarkLogic’s extension

Re-purposing other technologies: BPELRe-purposing other technologies: BPEL Long history of adding control flow Long history of adding control flow

logic to query languageslogic to query languages 15 years of success of PL /SQL and others15 years of success of PL /SQL and others SQL might have failed otherwise !SQL might have failed otherwise !

This is certainly not new research, but This is certainly not new research, but a natural evolutiona natural evolution

Florescu, Kossmann: SIGMOD 2006 Florescu, Kossmann: SIGMOD 2006 TutorialTutorial

Page 47: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

47

XQueryP ImplementationsXQueryP Implementations Prototype in Big OracleDBPrototype in Big OracleDB

Presented at Plan-X 2005Presented at Plan-X 2005 Prototype in BerkeleyDB-XMLPrototype in BerkeleyDB-XML

Might be open sourced (if interest)Might be open sourced (if interest) MXQueryMXQuery

http://www.mxquery.orghttp://www.mxquery.org (Java) (Java) Runs on mobile phones: Java CLDC1.1; some Runs on mobile phones: Java CLDC1.1; some cuts even run CLDC 1.0cuts even run CLDC 1.0

Eclipse Plugin available in March 2007Eclipse Plugin available in March 2007

Zorba C++ engine (FLWOR Foundation)Zorba C++ engine (FLWOR Foundation) Small footprint, performance, Small footprint, performance, extensibility, potentially embeddable extensibility, potentially embeddable in many contextsin many contexts

Page 48: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

48

XQueryP Pet Projects (at XQueryP Pet Projects (at ETH)ETH)

Airline AlliancesAirline Alliances every student programs his/her own airlineevery student programs his/her own airline form alliancesform alliances experiment: do this in Java/SQL first; then in XQueryPexperiment: do this in Java/SQL first; then in XQueryP

Public TransportationPublic Transportation mobile phone computes best route (S-Bahn)mobile phone computes best route (S-Bahn) integrate calendar, address book, ZVV, GPSintegrate calendar, address book, ZVV, GPS

Context-sensitive Remote ControlContext-sensitive Remote Control mote captures „clicks“ and movementsmote captures „clicks“ and movements mobile phone determines context and action (TV, garage, mobile phone determines context and action (TV, garage, ..)..)

Lego MindstormLego Mindstorm move to warmest place in a roommove to warmest place in a room

Less of a toy (OracleLess of a toy (Oracle):): XML Schema validator in XML Schema validator in XQueryPXQueryP

Your CS345b project goes here!Your CS345b project goes here!

Page 49: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

49

XQueryP GrammarXQueryP Grammar(MXQuery)(MXQuery)

Bold:Bold: modifications to XQuery modifications to XQuery grammar rulesgrammar rules

Italic:Italic: new XQueryP grammar rulesnew XQueryP grammar rules

Page 50: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

50

LibraryModule::= (ModuleDecl | ServiceDecl) Prolog;

Setter ::= BoundarySpaceDecl | DefaultCollationDecl | BaseURIDecl | ConstructionDecl | OrderingModeDecl | EmptyOrderDecl | RevalidationDecl | CopyNamespacesDecl | ExecutionDecl;

ExecutionDecl ::= "declare" "execution" " sequential";

Import::= SchemaImport | ModuleImport | ServiceImport;

QueryBody ::= SequentialExpr; (=> rewritten)

SequentialExpr ::= Expr(";" Expr)*;

PrimaryExpr ::= Literal | VarRef | ParenthesizedExpr | ContextItemExpr | FunctionCall | OrderedExpr | UnorderedExpr | Constructor | Block

Page 51: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

51

FunctionDecl ::= "declare" "updating"? "function" QName "(" ParamList? ")" ("as" SequenceType)? (Block | "external");

ExprSingle ::= FLWORExpr | QuantifiedExpr | TypeswitchExpr | IfExpr | InsertExpr | DeleteExpr | RenameExpr | ReplaceExpr | TransformExpr | AssignExpr | WhileExpr | TryExpr | OrExpr

Block ::= "atomic"? "{" (BlockDecl ";")* SequentialExpr ("return" ExprSingle| "continue"|"break")?"}" ;

BlockDecl ::= "declare" "$" VarName TypeDeclaration? ("=" ExprSingle)? ("," "$" VarName TypeDeclaration? (":=" ExprSingle)? )*;

AssignExpr ::= "set" "$" VarName ":=" ExprSingle;

WhileExpr::= "while" "(" ExprSingle ")" "return" ExprSingle;

Page 52: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

52

TryExpr ::= "try" "(" ExprSingle ")" CatchExpr* (CatchExpr | DefaultCatchExpr);

CatchExpr ::= "catch" "(" ( "$" VarName ("as" NameTest)? ("," "$" VarName

("," "$" VarName)?)?)? ")" "return" ExprSingle;

DefaultCatchExpr ::= "default" "(" "$" VarName ("," "$" VarName ("," "$" VarName)?)?)? ")" "return" ExprSingle;

IfExpr ::= "if" "(" Expr ")" "then" (ExprSingle|"return" ExprSingle|"break"|"continue") "else ExprSingle|"return" ExprSingle|"break"|"continue”;

TypeswitchExpr ::= "typeswitch" "(" Expr ")" CaseClause+ "default" ("$" VarName)? "return" (ExprSingle|"return"ExprSingle|"break"|"continue");

CaseClause ::= "case" ("$" VarName "as")? SequenceType "return" (ExprSingle|"return" ExprSingle|"break"|"continue");

Page 53: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

53

ServiceImport ::= "import" "service" "namespace" NCName "=" URILiteral "from" URILiteral ("name" = NCName)?;

ServiceDecl ::= "service" "namespace" NCName ="URILiteral" "port:" IntegerLiteral;

Page 54: 1 Module 10 XQuery Update, XQueryP Disclaimer: Work in progress!!!

54

SummarySummary Side-effectsSide-effects

change data without re-creating the datachange data without re-creating the data data keeps its identity (stays the „same“)data keeps its identity (stays the „same“) open questions concern „re-validation“ of dataopen questions concern „re-validation“ of data

Add scripting capabilitiesAdd scripting capabilities assignment, error handling, visibility of assignment, error handling, visibility of updatesupdates

Web Service calls; basic MashupsWeb Service calls; basic Mashups How does that impact your project?How does that impact your project?

Do you still need Java/PHP? Probably yes. :-Do you still need Java/PHP? Probably yes. :-((

Prediction: 1 year, can do projects without Prediction: 1 year, can do projects without JavaJava

Prediction: 10 years, XQuery(P) is the new Prediction: 10 years, XQuery(P) is the new JavaJava

Implementations: stay tuned :-)Implementations: stay tuned :-)