Presentation is loading. Please wait.

Presentation is loading. Please wait.

Static Typing in XQuery Mary Fernández, Jérôme Siméon, Philip Wadler Επιμέλεια Παρουσίασης: Μαγδαληνός Παναγής.

Similar presentations


Presentation on theme: "Static Typing in XQuery Mary Fernández, Jérôme Siméon, Philip Wadler Επιμέλεια Παρουσίασης: Μαγδαληνός Παναγής."— Presentation transcript:

1 Static Typing in XQuery Mary Fernández, Jérôme Siméon, Philip Wadler Επιμέλεια Παρουσίασης: Μαγδαληνός Παναγής

2 Contents Introduction XQuery Processing Phases Definitions Example Static Analysis Phase Dynamic Evaluation Phase Values and Types in XQuery XML Schema and XQuery Types Values Relating Values and Types Examples

3 Introduction Definitions Example Static Analysis Phase Dynamic Evaluation Phase

4 Definitions Dynamic Typing: The process of checking during dynamic evaluation that the type of a value is compatible with the context in which it is used. Static Typing: The process of checking during static analysis that the type of an expression is compatible with the context in which it is used Type Correct Program: Never raise a type error in any evaluation

5 Definitions (Cont.) Dynamic Typing ? Sort of run-time evaluation. Static Typing ? Sort of compile-time and pre-compile time evaluation. Type Correct ? An equivalent to a Java program being able to compile.

6 Example – Relation Data in XML Tom Jones B Red Bicycle 1999-01-05 1999-01-20 40 45 1999-01-13

7 Example – XML Schema <xs:schema targetNamespace=“http://example.com/auction”http://example.com/auction xmlns:xs=“http://www.w3.org/2001/XMLSchema”http://www.w3.org/2001/XMLSchema xmlns=“http://www.example.com/auction” >http://www.example.com/auction ………………………………………………………………………….. ………………………………………………………………………… ……………………………. ………………………………………………………………………….

8 Static Analysis Phase Static type: A type such that, when the expression is evaluated, the resulting type will always conform to the static type Static Error: Parse Error Inconsistency between Op- Tree and Static Context Type Error: Incompatibility between the static type of an expression and the type required by the context. XQueryOp - Tree Static Context Parse Query Modules Normalize Static Type Check Resolve Names Static Query Prolog Environmental Initialization

9 Static Analysis Phase (Cont.) Static Analysis equiv. Writing & Compiling Static Type example: Java: Vector v; ……. v.elementAt(i);  Return type is always java.lang.Object Type Error example: Java: Vector v; v.add(new Integer(1)); int i =v.elementAt(0)  Type Error. Expected type java.lang.Object but primitive type int was found

10 Dynamic Evaluation Phase Dynamic Type: Associated with each value during computation. More specific than Static Type. Dynamic Error: Errors occurring at runtime Type casting Schema dis-conformance Type Error: The dynamic type of a value does not match the expected type of the context in which the value occurs. Execution Engine Dynamic Context Access Op - Tree Access and Change Environmental Initialization Provide Access Access and Create Data Model Instances Data

11 Dynamic Evaluation Phase (Cont.) Dynamic Evaluation equiv. Execution Dynamic Type Example: Java: Vector v; ……. v.elementAt(i);  Return type is always java.lang.Object Type Error & Dynamic Error Example Java: Vector v; v.add(new Integer(1)); double i =Double.valueOf((Double)v.elementAt(0)) Dynamic & Type Error

12 Values and Types in XQuery XML Schema and XQuery Types Values Relating Values and Types

13 XML Schema and XQuery Types XML Schema  XQuery type notation Global/Local element declaration A local element is distinguished by content Type can be either user-defined or predefined An element declaration associates an element name and a type. E.g define element name of type xs:string A complex type declaration associates name and content model define type User{ attribute id of type xs:ID, element name of AnonymousType 1, element rating ? } Types can be combined with operators (, | ? + *) define type IntegerList {xs:integer + } Derivation by Restriction define type NewUser restricts User

14 Values XML input document: Mary Doe A XQuery formal notation: element user of type User{ attribute id of type xs:ID { “U02” }, element name of type AnonymousType1{ element first of type xs:string { “Mary “}, element last of type xs:string { “Doe” }, }, element rating of type xs:string { “A” } }

15 Relating Values and Types Usage of XQuery Types Analysis time Evaluation time Both proceed bottom – up Labeled sub-expressions, label an overall expression Labeled values of sub-expressions, label the value of an overall expression

16 Examples

17 Literals and Operators The static type system checks that the type of an expression is compatible with the type required by the context in which the expression is used. Examples: “hello” has type xs:string 42 + 69 has type xs:integer 42 + 69.00 has type xs:decimal (type promotion) “42” + 69.00 is a static type error “one” < “two” has type xs:boolean “one” < 2 is a static type error

18 Variables Variable bounding: let $z:= 1 + 2 return $z + $z has type xs:integer Variable sub-typing: let $x as xs:decimal :=1 + 2 return $x + $x has type xs:decimal Variable sub-typing is different than type promotion let $x as xs:double :=1 + 2 return $x + $x is a static type error Correct (sort of type-casting): let $x as xs:double :=xs:double( 1 + 2) return $x +$x has type xs:double Sum up: Every expression is evaluated with respect to its sub-expressions let $x as xs:decimal :=1, $y as xs:integer :=$x + $x return $y + $y Why does it raise a static type error? let $x :=1, $y :=$x + $x return $y + $y What is the variable’s y and return value’s type?

19 Functions Every function has a signature fn:concat ($op1 as xs:string ?, $op2 as xs:string ?) as xs:string Every function must be well-typed: Required types: Derive from the signature declared types Are promoted to the signature declared types

20 Conditionals The condition must be a Boolean if ( $x<$y ) then 3 else 4 has type xs:integer If ( $x<$y ) then “three” else 4.00 has type (xs:string | xs:decimal) ( if ($x<$y) then 4.2e1 else 69) +0.5 has type (xs:double | xs:decimal) if( alwaysTrue() then 1 else “two”) +3 raises a static type error! Conclusion: Typing rules understand no infeasible paths

21 Path Expressions Examples: $auction/articles/article Meaning : all local elements article of local element articles of global element auction Has type :element (article) + $auction/(articles | users) Meaning: all articles and users elements of element auction Has type :((element(articles) | element(users)) + The previous : factored type Any re-ordering in a factored type sequence results in the same factored type What is the meaning of the following? $auction//(user|articles)/name Has type ( element(type(User)/name) | element(type(Article)/name) ) +

22 Predicates A predicate selects nodes in a path expression that satisfy a given expression: $auction/articles/article [start_date<=date()] has type element(article)* Why the following? $auction/articles/article [@id=$id] has type element(article)* !!! exactly-one(),zero-or-one() functions More than one predicates can be used in an expression: $auction/articles/article[start_date<=current_date()][1]) has type element(article) ?

23 FLWOR Expressions FLWOR Expressions relies on factored types Examples: for $article in $articles return $article/end_date - $article/start_date has type xdt:dayTimeDuration + for $article in $articles where $article/start_date <= current_date() return $article/end_date - $article/start_date has type xdt:dayTimeDuration *

24 Validation Context Hierarchically defined context validation Tree-like definition of the validation context Global, … Explicitly defined context validation $first := validate context user/name { Mary } The latter is used if the context definition cannot be done lexically

25 Validation Mode Valid XML Data: Conform to an XML Schema or a DTD Well Formed XML Data: All open tags have matching end tags Validation modes: Strict : A declaration is available for the element and the element must validate with respect to that declaration Skip : Only well formatting constraints Lax : Validate strictly if a declaration is available Validation is lexically defined Any conflicts can be surpassed with “overriding”

26 Conclusions

27 To Conclude… Today we’ve learned about: The XQuery processing system The values and types in XQuery and their associations with XML Schema The way types and values are assigned to variables, functions,… The way an XQuery is validated and the different validation forms


Download ppt "Static Typing in XQuery Mary Fernández, Jérôme Siméon, Philip Wadler Επιμέλεια Παρουσίασης: Μαγδαληνός Παναγής."

Similar presentations


Ads by Google