Static Typing in XQuery Mary Fernández, Jérôme Siméon, Philip Wadler Επιμέλεια...

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

    19-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    1

Transcript of Static Typing in XQuery Mary Fernández, Jérôme Siméon, Philip Wadler Επιμέλεια...

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

Static Typing in XQuery

Mary Fernández, Jérôme Siméon, Philip Wadler

Επιμέλεια Παρουσίασης:

Μαγδαληνός Παναγής

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

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

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

Introduction

DefinitionsExample

Static Analysis PhaseDynamic Evaluation Phase

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

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

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

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.

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

Example – Relation Data in XML<auction xmlns=”http://www.example.com/auction”>

<users><user id=“U01”>

<name><first>Tom</first><last>Jones</last></name><rating>B</rating>

</user></users><articles>

<article id=“1001”><name>Red Bicycle</name><seller idref=“U01”/><start_date>1999-01-05</start_date><end_date>1999-01-20</end_date><reserve_price>40</reserve_prize>

</article></articles><bids>

<bid><userid idref=“U01”/><articleno idref=“1001”/><amount>45</amount><date>1999-01-13</date>

</bid></bids>

</auction>

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

Example – XML Schema<xs:schema targetNamespace=“http://example.com/auction”

xmlns:xs=“http://www.w3.org/2001/XMLSchema”xmlns=“http://www.example.com/auction” >

…………………………………………………………………………..<xs:element name=”users”>

<xs:complexType> <xs:sequence>

<xs:element name=“users”/><xs:element name=“articles”/><xs:element name=“bids”/>

</xs:sequence></sx:complexType>

</xs:element>

<xs:element name=“users”><xs:complexType>

<xs:element ref=“user” maxOccurs=“unbounded”/></xs:complexType>

</xs:element>…………………………………………………………………………<xs:element name=“user” type=“User”/><xs:complexType name=“User”>

…………………………….</xs:complexType>………………………………………………………………………….

</xs:schema>

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

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.

XQuery Op - Tree

Static Context

Parse Query

Modules

Normalize

Static Type Check

Res

olve

Nam

es

Sta

tic Q

uery

Pro

log

Environmental Initialization

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

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

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

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.

ExecutionEngine

DynamicContext

Access Op - Tree

Acc

ess

and

Cha

nge

Environmental Initialization

Provide Access

Access and Create

Dat

a M

odel

Ins

tanc

es

Data

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

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

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

Values and Types in XQuery

XML Schema and XQuery TypesValuesRelating Values and Types

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

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

<xs: element name=“name” type=“xs:string”/> 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

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

Values

XML input document:<user id=“U02”>

<name><first>Mary</first><last>Doe</last>

</name><rating>A</rating>

</user> 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” }

}

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

Relating Values and Types

Usage of XQuery TypesAnalysis timeEvaluation time

Both proceed bottom – up Labeled sub-expressions, label an overall

expressionLabeled values of sub-expressions, label

the value of an overall expression

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

Examples

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

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

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

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?

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

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 typesAre promoted to the signature declared types

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

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

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

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) ) +

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

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) ?

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

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 *

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

Validation Context

Hierarchically defined context validationTree-like definition of the validation context

Global, …

Explicitly defined context validation $first := validate context user/name {<first>Mary</first>}

The latter is used if the context definition cannot be done lexically

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

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”

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

Conclusions

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

To Conclude…

Today we’ve learned about:The XQuery processing systemThe values and types in XQuery and their

associations with XML SchemaThe way types and values are assigned to

variables, functions,…The way an XQuery is validated and the

different validation forms