OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer...

28
OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University

Transcript of OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer...

Page 1: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 1

Chapter 4

Object Query Languages

Prof. Hyoung-Joo KimOOPSLA Lab.

Dept. of Computer Engineering

Seoul National University

Page 2: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 2

Contents

• 4.1 Introduction

• 4.2 Principles

• 4.3 Query Input and Result

• 4.4 Object Identity

• 4.5 Path Expressions

• 4.6 Null Values

• 4.7 Method Invoking

• 4.8 Polymorphism

• 4.9 Operator Composition

• 4.10 Language Definition

• 4.11 Syntactical Abbreviations

• 4.12 OQL BNF

Page 3: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 3

4.2 Principles

• Principles and assumptions– rely on ODMG object model

– very close to SQL92

– high-level primitives to deal with sets of objects

– functional language

– not computationally complete

– can be invoked within programming language

– no explicit update operators

– declarative access to objects

– formal semantics of OQL can easily be defined

Page 4: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 4

4.3 Query Input and Output(1)

• OQL as a stand-alone language– query denotable objects starting from their names

• OQL as an embedded language– query denotable objects which are supported by the native language

through expressions yielding atoms, structures, collections, and literals

• Example

Select distinct x.agefrom Person xwhere x.name = “Pat”

Returns a literal of type set<integer>

Page 5: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 5

4.3 Query Input and Output(2)

• select distinct struct(a: x.age, s: x.sex) from Person x where x.name = “Pat”

returns a literal of type set<struct>

• select distinct struct(name: x.name, hps: (select y from x.subordinates as y where y.salary > 100000))

from Employees x

returns a literal of type set<struct(name: string, hps:bag<employee>)>

• Chairman

returns the Chairman object

• Chairman.subordinates

returns the set of subordinates of the Chairman

Page 6: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 6

4.4 Object Identity(1)

• Denotable object has an OID• Literal

– identity = their value• Create objects

– a type name constructor is usedPerson(name: “Pat”, birthdate: “3/28/56”, salary: 100,000)struct(a:10, b: “Pat”)

typedef set<integer> vectintinterface stat {attributes attribute Short a; attribute Char c;};typedef bag<stat> stats;

Page 7: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 7

4.4 Object Identity(2)• Vectint(select distinct age from Persons where name = “Pat”) returns an object of type vectint

• stats(select stat (a: age, s: sex) from Persons where name = “Pat”) returns an object of type stats

• Selecting existing objects– the extraction expressions may return :

• a collection of objects with identity• an object with identity• a collection of literals, and literal

Page 8: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 8

4.5 Path Expressions(1)

• 1-1 relationship– find the name of the city where the person p’s spouse

lives

p.spouse.address.city.name

• n-p relationships– find the names of the children of the person p

select c.name

from p.children c

Page 9: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 9

4.5 Path Expressions(2)

• Predicate select c.address

from Person P, p.children c

where p.address.street = “Main Street” and count(p.children) >= 2 and c.address.city != p.address.city

• Joinselect p

from Person p, Flowers f

where p.name = f.name

Page 10: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 10

4.6 Null Values

• The rules for managing UNDEFINED are:

– accessing operations applied to an UNDEFINED operand produce

UNDEFINED

– comparison operations with either or both operands being

UNDEFINED produce False

– is_undefined(UNDEFINED) returns True: is_defined(UNDEFINED)

returns False

– Any other operation with any UNDEFINED operands results in a run-

time error

Page 11: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 11

4.7 Method Invoking

• A method with parametersselect max(select c.age from p.children c)

from Persons p

where p.name = “Paul”

• A method without parametersselect p.oldest_child.address.street

from Persons p

where p.lives_in(“Paris”)

Page 12: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 12

4.8 Polymorphism

• For instance, the set Persons contains objects of classes Person, Employee, and Student

• Late binding

– example• select p.activities

• from Persons p

• Class indicator

– example• select ((Student)p).grade

• from Person p

• where “course of study” in p.activities

Page 13: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 13

4.9 Operator Composition(1)

• All operators can be composed freely as long as the type system is

respected.

• Example

– find the name of the street where employees live and have the

smallest salary on average, compared to employees living in other

streets

1. define Employees() as

select (Employee) p from Persons p

where “has a job” in p.activities

Page 14: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 14

4.9 Operator Composition(2)

2. define salary_map() as

select street, average_salary:avg(select x.e.salary from partition x)

from Employees() e

group by street: e.address.street

3. define sorted_salary_map() as

select s from salary_map() s order by s.average_salary

4. first(sorted_salary_map()).street

Page 15: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 15

4.10 Language Definition(1)

• OQL is an expression language

• OQL is a typed language

• Notation

– q : query name ,

– e : expression,

– p : property name

– x : variable,

– t : type name,

– f : operation name

Page 16: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 16

4.10 Language Definition(2)

• Named query definition– example

define smiths() as select p from Persons p where p.name=“Smith”

• Elementary Expression– atomic literal

• nil, false, true, 27, ‘z’

– name object• Student

– iterator variable• e as x, e x, x in e

– named query• smith()

Page 17: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 17

4.10 Language Definition(3)

• Construction expressions– object : t(p1:e1, …….. , pn:en)

• Employee(name: “Peter”, boss: Chairman)

– structure : struct(p1:e1, …….. , pn:en)

• struct(name: “Peter”, age:25)

– set : set(e1, …. , en)

• set(1,2,3)

– bag : bag(e1, …. , en)

• bag(1,1,2,3,3)

– list : list(e1, …. , en)

• list(1,1,2,3,3)

– array : array(e1, …. , en)

• array(1,1,2,3,3)

Page 18: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 18

4.10 Language Description(4)

• Atomic type expressions– unary expressions : <op> e

• not(true)

– binary expressions : e1<op>e2

• count(Students) - count(TA)

– string expressions • ‘a nice string’ like ‘%nice%str_ng’ is true

• Object Expressions– comparison of objects

• Doe = element(select s from Students s where s.name = “Doe”)

– comparison of literals• e1=e2, e1 != e2, not(e1=e2)

Page 19: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 19

4.10 Language Description(5)

– Applying an operation to an object

• jones->number_of_students

• Doe->apply_course(“Math”, Turing)->number

• Collection expressions– universal quantification : for all x in e1:e2

• for all x in Students:x.student_id > 0

– existential quantification : exists x in e1:e2

• exists x in Doe.takes:x.taught_by.name=“Turing”

– membership testing : e1 in e2

• Doe in TA

– aggregate operators• max(select salary from Professors)

Page 20: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 20

4.10 Language Definition(6)

• Select from where

– select [distinct] e from x1 in e1, … , xn in en where e’

– select [distinct] e from e1 as x1, … , en as xn where e’

• select couple(student:x.name, professor:z.name)

from Student as x, x.takes as y, y.taught_by as z

where z.rank = “full professor”

• Group-by operator

– select_query group by partition_attributes [having predicate]• select * from Employees e group by low: x.salary < 1000, medium: x.salary >= 1000 and x.salary < 10000, high: x.salary >= 10000

Page 21: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 21

4.10 Language Definition(7)

• returns set<struct(low: boolean, medium: boolean, high: boolean, partitions: bag<struct(e: Employees)>)>

• Order-by operator– select_query order by e1, …, en

• select p from Persons p order by p.age, p.name

• Indexed collection expressions– get i-th element : e1[e2]

• list(a, b, c, d)[1]

• element(select x

from Course x

where x.name = “Math’ and x.number=“101”).requires[2]

Page 22: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 22

4.10 Language Definition(8)

– extracting a subcollection : e1[e2:e3]

• list(a, b, c, d)[1:3]

• element(select x

from Course x

where x.name = “Math’ and x.number=“101”).requires[2]

– last and first element : first(e), last(e)

• element(select x

from Course x

where x.name = “Math’ and x.number=“101”).requires[2]

– concatenating : e1 + e2

• list(1,2) + list(2, 3)

Page 23: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 23

4.10 Language Definition(9)

• Binary set expression

– union, intersection, difference

• bag(2,2,3,3,3) union bag(2,3,3,3)

• bag(2,2,3,3,3) intersection bag(2,3,3,3)

• bag(2,2,3,3,3) except bag(2,3,3,3)

– inclusion

• e1 < e2 is true if e1 is include in e2 bit not equal to e2

• e1 <= e2 is true if e1 is included in e2

• set(1,2,3) < set(3,4,2,1) is true

Page 24: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 24

4.10 Language Definition(10)

• Conversion expressions

– extracting the element of a singleton : element(e)

• element(select x from Professors x in x.name = “Turning”)

– list to set : listtoset(e)

• listtoset(list(1,2,3,2))

– flattening : flatten(e)

• flatten(list(set(1,2,3), set(3,4,5,6),set(7))) == set(1,2,3,4,5,6,7)

• flatten(list(list(1,2), list(1,2,3))) == list(1,2,1,2,3)

• flatten(set(list(1,2), list(1,2,3))) == set(1,2,3)

– typing an expression : (t)e select ( (Employee) s).salary

from Students s

where s in (select sec.assistant from sec in sections)

Page 25: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 25

4.10 Language Definition(11)

• Scope rules

– explicit variable : select ….. from Persons p…

• p.age

– implicit variable : select …… from Persons

• Persons.age

– a name appearing in a (nested) query

• a variable in the current scope

• a named query

• a named object

• an attribute name or an operation name of a variable in the current scope

– Example

Page 26: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 26

4.10 Language Definition(12)

• scope1 : Persons, c, Cities, all property name of class Person and class City

• scope2 : child, Persons, c, Cities, the property names of the class City

• scope3 & scope 4 : age, partition, p, v, the same names from scope1, except “age”,

“partition”, “p”, and “v” if they exist

• scope5 : p and the same name from scope1, except “p” if it exists

• scope6 : p, v, Persons, c, Cities, the property names of the class City

Select scope1from Persons, Cities cwhere exists (select scope2 from children as child) or count (select scope3, (select scope4 from partition) from children p, scope5 v group by age:scope6 )

Page 27: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 27

4.11 Syntactical Abbreviations(1)

• OQL– orthogonal expression language

– functional language

• Structure constructions– an alternate syntax is allowed in two contexts

• select projection {, projection} ..

• select … group by projection {, projection}

– example

select p.name, salary, student_id

from Professors p, p.teaches

• returns bag<struct(name: string, salary: float, student_id: integer)>

Page 28: OOPSLA LAB. 1 Chapter 4 Object Query Languages Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.

OOPSLA LAB. 28

4.11 Syntactical Abbreviations(2)

• Aggregate operators– example

select count(*) from …… is equivalent to

count(select * from ….)

select aggregate(query) from …. Is equivalent to

aggregate(select query from …. )

select aggregate(distinct query) from …. Is equivalent to

aggregate(distinct( select query from …. ))

• Composite predicate– example

10 < some(8, 15, 7, 22) is true