This document discusses types in Oracle databases and how they power PL/SQL interoperability. It covers the history of types, standard types, user-defined types, collections of types, and how types can be used for structured programming, SQL and PL/SQL interaction, and interacting with external systems. It provides examples of defining types and collections, and using types and collections for object-oriented programming techniques in Oracle databases.
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Absolutely Typical - The whole story on Types and how they power PL/SQL Interoperability (UKOUG, 2011)
1. Lucas Jellema, AMIS
ABSOLUTELY TYPICAL -
THE WHOLE STORY ON TYPES AND HOW
THEY POWER PL/SQL INTEROPERABILITY
On Types and Objects, Collections and OO
UKOUG, December 2011, Birmingham, UK
2. OVERVIEW
• History of Types
• Areas of use
• Type
• True OO
• Collections [of Types]
• Table and Multiset
• Interaction with the outside world
– XML, JSON
– Web Services
3. STANDARD TYPES IN ORACLE DATABASE
• Standard as in „installed out of the box‟
– Number, Char, Date
– Varchar2
– RAW, LONG
– SDO_GEOMETRY, …
– BLOB, CLOB, TIMESTAMP, INTERVAL
– XMLType
– DICOM (Medical Records)
4. THE DATABASE SUFFIX
• 12c
• 11g
• 10g
• 9i
• 8i
• 8.0
– That is 8.o (as in oh, not as in zero!)
o
• And: 8 really is o
o
• OO in Oracle turned out to mean
– Define user defined types to use as SQL and PL/SQL
data structure programming constructs
• that have some OO characteristics as well
• that can be used to define table columns for
5. AREAS OF USE FOR TYPES
• Structured programming in PL/SQL
• Interaction SQL and PL/SQL
– Elegant, performing, organized
• Interaction with the outside world
– Java, SOA Suite, other consumers
• OO-like storage of nested type structures
• Do OO design and application development
6. EXAMPLE OF TYPE DEFINITION
• Types are manipulated through Type as Object
DDL statements:
– CREATE OR REPLACE TYPE PERSON_T
– Object Types have properties (attributes)
– Object Types are similar to PL/SQL records
– Object Types live in Data Dictionary as primary
database objects
7. INSTANTIATING AN OBJECT
• A variable based on a type (as object) needs to be
instantiated before it can be assigned or read
• An instance of a type == an object
9. CALLING FUNCTION WITH TYPE
PARAMETER FROM SQL
• Types can be instantiated from within SQL statements
and passed to PL/SQL functions
– SQL*Plus and other tools typically know how to render
types in query results
11. SCALAR SUBQUERY
• Inline query that returns a single result
– One row, one column
select ename Employees with their job and sal and the
, job number of staff they manage
, sal
, (select count(empno) from emp s where s.mgr= e1.empno)
from emp e1
select ename Employees who earn between the average
, sal salary for salesmen and analysts
from emp
where sal between
(select avg(sal) from emp where job = 'SALESMAN')
and
(select avg(sal) from emp where job = 'ANALYST')
12. WORKAROUND TO RETRIEVE MULTIPLE
VALUES: CONCATENATION OF VALUES
select ename
, job
, sal
, substr(sal_aggs, 1, instr(sal_aggs,'x') -1)
avg_sal_in_job
, substr(sal_aggs, instr(sal_aggs,'x') +1)
max_sal_in_job
from ( select ename
, job
, sal
, ( select avg(sal)||'x'||max(sal)
from emp
where job = e1.job
) sal_aggs
from emp e1
)
order by job, sal desc
13. MUCH BETTER WORK AROUND: ADT
select ename
create type
, job
multivalue_return_type
, sal
as object
, emps.sal_aggs.value1 avg_sal_in_job
( value1 number(20)
, emps.sal_aggs.value2 max_sal_in_job
, value2 number(20)
from ( select ename
, value3 number(20)
, job
)
, sal
, ( select multivalue_return_type
( avg(sal), max(sal), null)
from emp
where job = e1.job
) sal_aggs
from emp e1
) emps
order by job, sal desc
14. TYPES AND THE DBA
• Before Oracle Database 11g – altering type definitions
was not easy
– Drop, Alter or Replace of a Type could only be done if no
references existed to the type from other types or tables
– That usually meant all types associated with an
application were dropped and recreated – to alter one!
• Starting in 11g the DROP and REPLACE/ALTER TYPE
operations can specify FORCE to ignore dependencies
– Helps to overcome frowning, type-averse DBAs!
• Note: Data Dictionary Views are available to tell on types
• Types are editionable objects in terms of 11gR2 Edition
Based Redefinition and can evolve gracefully
15. COMPLEX TYPES
• Attribute of a Type (as Object) can be based on an ADT
aka UDT: Object or Collection
– Limitation: Self-reference is not allowed
Type as Object
Type as Object
PERSON_T
SOCIAL_PROFILE_T
16. SOCIAL PROFILES STORED IN TABLES
• Two options for storing social profiles:
– The normal, explicit
relational way:
– The nested table
approach:
17. INSTANTIATING NESTED OBJECT IN SQL
• Instantiate „look up object‟ using Scalar Subquery
• Instantiate „look up object‟ from (outer) joined columns
18. COLLECTIONS IN ORACLE
• Oracle supports in-memory collections of type
instances
– Can be of standard type (number, varchar2, date,…) or
complex User Defined Type (aka ADT)
• Three kinds of collections
– Associative Array (PL/SQL only) – {key,value} pairs,
very similar to Map in Java and Hashtable in C#
– VARRAY (the variable array of fixed size)
– Nested Table
• These collections are similar, yet quite distinct
– Definition and instantiation
– Similar operations (count, first, next, delete, …)
– Sparse vs. Dense
– SQL Interaction support
21. COLLECTION OPERATIONS
• Exists – to check if element with/at index exists
• Extend (only for Nested Table)
• Count
• First
• Last
• Next
• Prior
• Delete
• Trim
22. COMPLEX TYPES
Type as Object
Type as Object
PERSON_T
SOCIAL_PROFILE_T
Type as Table
of email_address_t
EMAIL_ADDRESS_TBL
Type as Object
EMAIL_ADDRESS_T
25. TRUE OO: SUBTYPES AND INHERITANCE
• Types can be created as subtypes of another Type
• A subtype has multiple identities:
– EMPLOYEE_T is both PERSON_T and EMPLOYEE_T
– Anything that can handle a PERSON_T can also
handle an EMPLOYEE_T and a CUSTOMER_T
Type as Object
PERSON_T
Type as Object Type as Object
EMPLOYEE_T CUSTOMER_T
28. OO OPERATORS IN TYPE HIERARCHY
• TREAT (AS type) – downcast an instance to a specific
subtype – to access specialized subtype aspects
• IS OF ( [ONLY] type) – to identify type of an instance
29. MEMBER FUNCTIONS
• A type can have operations
– Specification and Implementation (in the Body)
– Acting on the members – public and private – of the
object
– Possibly overriding methods of the super-type
• Two types of operations
– Constructor Type PERSON_T as
– Normal member functions Object
<public methods>
Type Body
PERSON_T
<implementation
of private and
public methods>
30. CONSTRUCTOR FUNCTIONS
• A type is instantiated through a call to one of its
constructor methods
• A type can have multiple, overloaded user defined
constructor functions
– In addition to the
default system
defined
constructor that
simply expects
input for every
public member
32. MEMBER FUNCTIONS
• Types can have member functions – similar to
functions in a package
– Just like package can act on package state (globals),
member functions act on object (type instance)
34. OVERRIDING MEMBER FUNCTIONS
• Sub types can override member functions from super
– When the function is invoked on a PERSON_T and
PERSON_T happens to be a CUSTOMER_T, then the
function as defined on CUSTOMER_T is executed
– Overriding subtype member function can invoke the
overridden super typer function
Type PERSON_T as Object
Type EMPLOYEE_T as Object Type CUSTOMER_T as Object
35. EXAMPLE OF OVERRIDING MEMBER
FUNCTION
Type PERSON_T as Object
Type CUSTOMER_T as Object
36. MAP AND ORDER MEMBER FUNCTION
SORTING COLLECTIONS
• Map Member Function translates an object to a scalar
data type that Oracle knows how to compare and sort
– Varchar2, Number, Date or even an UDT with scalar
attributes (or a Map function of its own)
• Note: Map member function is also used for equality
comparison
– In this case John Doe and Bill Doe are seen as equal!
37. MAP MEMBER FUNCTION IN ACTION -
SORTING IN SQL
• Map function dictates:
– Women come first, then sort by birth date (young
before old)
38. ORDER MEMBER FUNCTION USED TO
ORDER BY IN SQL
• Order member function to compare objects
– returns -1 when self comes before other (when sorted),
1 when self comes after and 0 when they draw
40. USER DEFINED AGGREGATION FUNCTIONS
IN SQL
select avg(hiredate) from emp
ORA-00932: inconsistent datatypes:
expected NUMBER got DATE
• The Oracle Data Cartridge offers User Extensibility
framework for the database
• Data Cartridge provides:
– user defined aggregate functions
– domain indexes and functions
• used for Oracle Text and Oracle Spatial
– Pipelined Table Functions – the interface approach
• Dynamic handling of AnyData objects
41. IMPLEMENTING USER DEFINED
AGGREGATE
• Implement an object with the following operations:
– ODCIAggregateInitialize
– ODCIAggregateIterate
– ODCIAggregateTerminate
– ODCIAggregateMerge
• Optional, to enable parallelism
create type DateAvgImpl
as object
( count number
, sumdates NUMBER
, function ODCIAggregateInitialize
, function ODCIAggregateIterate
, function ODCIAggregateTerminate
, function ODCIAggregateMerge
)
42. IMPLEMENTING USER DEFINED
AGGREGATE
• Register new aggregate function implemented by
the object
function dateavg (input date)
return date
PARALLEL_ENABLE
AGGREGATE USING DATEAVGIMPL
• Use the new function in queries
select DateAvg(hiredate) from emp
• “Analytic Function Friendly”
43. FUNCTION CHAINING
• Dot notation (navigation) is used to call a function on
the result of a function (on the result…)
– if the result of the nested functions are types
44. BULK COLLECT TO PREVENT MULTIPLE
ROW-BY-ROW PL/SQL TO SQL ACCESS
• Traditional cursor based approaches for fetching data
from the database using SQL resulted in
– Unnecessary SQL/PLSQL context switches
• By fetching multiple rows at once – the number of
(expensive) switches can be reduced
• Several approaches exist – all rely on Collections
– Bulk Collect
– Select Collection
• Note: as of Oracle 10g the „normal‟ cursor-for-loop
also implicitly does fetching in bulk
46. CREATE A COLLECTION IN SQL – COLLECT
OPERATOR
• COLLECT is an aggregation function (like MAX, SUM,
COUNT) that aggregates into nested tables
– Group by can be used to create ‘clusters’
48. MULTISET TURNING A (SUB) QUERY RESULT
SET ON THE FLY INTO A NESTED TABLE
• MULTISET can be used inside SQL Queries to convert
a query result set into a collection (nested table)
– On the fly (in the middle of a query)
50. MULTISET OPERATORS ACTING ON NESTED
TABLES IN SQL AND/OR PL/SQL
• C1 = C2
– Comparison based on named type, cardinality and
comparison of individual elements
• C1 <> C2 or C1 != C2
– Inverse of equality test
• C1 IN C2 returns true if C1 is one of the collections in
C2 which is a collection of collections
• POWERMULTISET C1 generates all non-empty sub-
multisets from collection C1
– All possible combinations created from the elements in
C1, produced as collections of the same type as C1
• C1 IS EMPTY returns true if C1 is empty
• CARDINALITY C1 returns the number of elements in
C1
51. MULTISET OPERATORS ACTING ON NESTED
TABLES IN SQL AND PL/SQL
• C1 SUBMULTISET C2 evaluates to true when the
members of C1 are all found in C2
• X MEMBER OF C1 evaluates to true when X is found
as a member of collection C1
• C1 MULTISET UNION [DISTINCT] C2 – combining two
collections
– Use DISTINCT to eliminate duplicate elements
• C1 MULTISET INTERSECT [DISTINCT] C2 – produces
collection of elements that occur in both C1 and C2
• C1 MULTISET EXCEPT [DISTINCT] C2 – produces the
collection of elements that appear in C1 and not in C2
– Use ALL to allow (reduced number of )duplicates
• C1 IS A SET – tests if the collection C1 only has
unique members (no duplicates)
52. COULD BE ANYTHING
• The Any… Types are like generic placeholders for data
that is only known at run time
– The contents of Any… instances is accessible through
introspection
• AnyData – some thing, could be any thing (Object)
• AnyType – holds the definition of some thing
• Like AnyData without the data – only meta data
• AnySet – some collection of things (all of the same
type)
– can be used for interface parameters to communicate
self-descriptive sets of data
– Used in Pipelined Table Functions created through
Data Cartridge to return dynamically determined data
structures
59. EXTREME ENCAPSULATION
• View on top of table(collection)
– With instead of trigger handling DML
– Data queried from NUMBERS is produced in PL/SQL –
no underlying table involved
– Data manipulated in NUMBERS is processed in
memory
61. FOR GENERATING ROWS FOR SPECIAL
DOMAINS OF ALLOWABLE VALUES
• Have TABLE operate on an in-line created collection
with countries as
( select column_value country
from table(
string_table('Belgium','France'
,'Egypt','Italy'
)
• To save)on repeated
) – select value from dual UNION ALL select value …
select country
from countries
with gender_values as
( select column_value gender
from table( string_table('MALE', 'FEMALE',‘UNKNOWN' ))
)
select gender
from gender_values
62. PIPING ROWS USING COLLECTIONS AND
THE „TABLE FUNCTION‟
• A Table Function normally returns the entire collection
at once
• However: it can also return collection elements one at
a time
– That is called PIPELINED
• As soon as the function pipes a single element,
processing that element can continue
• It‟s bit like the
/*+ FIRST_ROWS */ Hint
63. THE SLOW ALPHABET…
create or replace
function alphabet
return letter_table
is
l_alphabet letter_table:= letter_table();
begin
l_alphabet.extend(26);
for i in 1..26 loop
l_alphabet(i):= chr(64+i);
dbms_lock.sleep(0.1);
end loop;
return l_alphabet;
end alphabet;
Note: it is not just 2.79 seconds for all rows to appear:
it takes that long for any row to appear
64. THE PIPELINED SLOW ALPHABET…
create or replace
function alphabet return letter_table
pipelined
is
l_alphabet letter_table:= letter_table();
begin
l_alphabet.extend(26);
for i in 1..26 loop
l_alphabet(i):= chr(64+i);
dbms_lock.sleep(0.1);
pipe row (l_alphabet(i));
end loop;
return;
end alphabet;
Note: total processing time is just as long as before;
however, the first row appears after less than 0.5 sec!
67. DO NOT DO IT…
ON YOUR OWN
• Parallel means: multiple resources contributing to a
task at the same time
• Introduce parallellism into your database application
– parallel query and DML
– coordinated jobs (dbms_job, dbms_scheduler)
– 11g: dbms_parallel_execute
– Pipelined & Parallel table functions
71. PIPELINED PANCAKE PARTY
• Parallel Pipelined: multiple different resources working
on different components of the task in [potentially]
different tiers
– From batch processing to unit processing => pass the
unit on for further processing as soon as part of the
task is done – to leverage resources (across tiers) in
parallel
• Instead of baking the entire stack first and only then
eating it…
– … start eating as soon as the first one is done
– Even the last guest ends earlier than without pipelining
provided he eats more than one pancake
• (he eats his first pancake when it is done, not when the
stack is done)
– The first eater is done much sooner
• first rows/all rows ≈ first pancake/all pancakes
76. INTRODUCING XML
• Structured ASCII
• Esperanto
• Cross technology – interoperability
• Tools & frameworks for common operations
– Parsing, validation, construction, transformation and
querying
• Standards:
– XSD – design and contract
– XPath - querying
– XSLT - transformation
– XQuery – querying and manipulation
77. WHAT IS XML?
eXtensible Markup Language – for exchange of data
Plain text / ASCII files
Structured messages
Human readable
More important: machine interpreted
Meaning defined with tags
Elements and attributes
<?xml version="1.0" encoding="UTF-8"?>
<root element>
<element1 attributeName="value">
content</element1>
<element2 attributeName="value" />
<element3>
<element4>content</element4>
</element3>
</root element>
79. CONSTRUCTING XML IN DATABASE
APPLICATIONS
• Many ways to produce XML content in Oracle
– XMLType – from text, types, cursor, bfile contents, …
– SQL/XML (XMLAgg, XMLElement, XMLForest, …)
– DBMS_XMLGEN
– SYS_XMLGEN and SYS_XMLAGG
– DBURIType and XDBURIType
– XML SQL Utility (XSU) – command line, Java, PL/SQL
– fn:doc and fn:collection (replacing ora:view)
XMLType
80. CONVERTING TYPE STRUCTURE TO
XMLTYPE
• XMLType can be instantiated based on a single
complex User Defined Type instance
– Not (directly) on a nested table
82. CONVERT XMLTYPE TO NESTED USER
DEFINED TYPE
• XMLType supports conversion directly in a –
potentially nested – user defined type
– Provided element names match names of type,
subtypes and attributes
83. XML CAN BE SOMEWHAT HEAVY
• Size of XML documents compared to the actual data
content of those documents is „XXL‟
• Complex nested structure that requires parsing
• Turning XML into native data structures and vice versa
is expensive (Marshalling/Unmarshalling )
• XML Programming facilities not available on all
platforms and in all languages
– Objective C, JavaScript, …
• The search for alternatives
is on: structured, contract,
standardized, facilities,
lighter weight!
95. PUBLISH PACKAGE AS HTTP-BASED API
USING DBMS_EPG
http
• Hide database protocol
– Not its physical location nor
the schema, and user authentication
• HTTP communication is truly cross technology
– Browser, Java, .Net, JavaScript & RIA clients, …
• Is also possibly remote, across networks, firewalls etc.
– easier than normal JDBC connections
• Can publish in various formats
– Text, HTML, CSV, JSON, XML, RSS
• Use case:cross-technology, internal no WS*/ESB
97. RESTFUL WEBSERVICES
• Simple, stateless, light weight, http-based message
exchange protocol
– Use standard get, put, post, delete operations
– Use URL to pass resource reference
• Small, easy-to-understand messages
• Typically XML or JSON based
• Not using SOAP, or the WS*-stack
• Often used directly from User Interface – (AJAX
enabled) JavaScript
104. CONSUMING REST-FUL SERVICES
• Invoking a REST-ful service is simply making an HTTP
request to some URL
• Can be done through:
– utl_http
– httpuritype
– Stored Java
• Result can be processed
as just a string or
as XML, JSON
or other format
– Libraries (PL/JSON) can help with special formats
105. SOAP WEB SERVICES
• All messages (input and output) are XML
• The message consists of two parts inside an envelope
(a SOAP XML wrapper)
– The header with meta-data
– The body with the contents to be handled by or
returned by the service
• The WebService Definition Language (WSDL)
document describes the service
• An XML Schema Document (XSD) describes the
structure of the XML messages
– XSD is like an ERD or Table Design
106. ORACLE RDBMS 11G –
NATIVE DATABASE WEB SERVICES
• Database Schema can be published WS/SOAP
through native database web services
– Each package corresponds with a WSDL
• With an operation for each Procedure or Function
• Auto-mapping from PL/SQL input and output parameters
to WSDL (XSD) Types
– WSDL and XSD are dynamically generated
– A generic Query Service (across schema) is exposed
• To execute user defined queries
• With support for pagination and some formatting
– http and https is supported
– Limited control over WSDL & XSD
– Use case: internal, cross technology, WS enabled
client, no ESB or Application Server available
107. STUFF INC
• Challenge: publish SOAP Web Services to expose data
services based on an Oracle Database
– Read data and Manipulate data
• Use as decoupled an approach
as possible
– Java developers do not
touch SQL or PL/SQL
– Database developers are not
aware of web service context
• See white paper on OTN
http://www.oracle.com/technetwork/articles/soa/jellema-esb-pattern-
1385306.html for details
108. XML
Relational/Oracle Type
JEE Server Database
Native DB
WebService
HTTP
http EPG
ADF BC JSON/ CSV
SDO /SDO WS XML PL/SQL
XML & XSD
SOA WS
JAX-WS
package
Ref Cursor
Suite JPublisher
DB Types & Coll
WS JDBC XML
AQ Types
Adapters
AQ
JMS Queue JMS
utl_file, View
JMS
BFILE,
Oracle EJB
EJB/JPA URITYPE
Service Pojo Other Table
(Email, FTP/File, XML
Bus File
XMPP/Chat) DB
FTP UMS
Chat/IM XMPP
Server
File/FTP Server
Email Server
109. PL/SQL MUSIC API EXPRESSED IN
TYPES AND COLLECTIONS
Type
Package
110. FOR JAVA DEVELOPERS BENEFIT:
SOAP WebService WRAP TYPE IN XMLTYPE
Java Class Java Class
JAX-WS ESB Architecture pattern:
The
XMLType
Oracle JDBC
Type
Package
Mediator
Decouple XMLType
Java Class
exchange
Validate, Enrich, Transform, Route, …
111. TO ACCOMMODATE FRAMEWORKS
SOAP WebService THAT DO NOT UNDERSTAND XMLTYPE
Java Class Java Class
JAX-WS
Oracle JDBC
XMLType
String
Type
CLOB
Mediator XMLType Package
Java Class JDBC
exchange
112. USING THE OSB OR SOA SUITE DATABASE
ADAPTER – USER DEFINED TYPES ARE FINE
• Database adapter interacting with Type based PL/SQL
API
– Derives XSD from input and output parameters
– Allows for perfect decoupling between PL/SQL on one
side of the fence and Web Service/XML on the other
Validate Enrich Transform Route & Operate
SOAP WebService
User
XML
Proxy Service Defined
Business
Type
Type Package
HTTP Service
SOAP JDBC
Database
Adapter
113. JUKEBOX SERVICE - COMPLETE
Validate Enrich Transform Route & Operate
SOAP WebService
User
XML
Proxy Service Defined
Business
Type
Type Package
HTTP Service
SOAP JDBC
Database
Adapter
114. SUMMARY
• Long live types!
• Structured programming in PL/SQL
– Multiple input and output parameters, step by step
result constructions
• Integration between SQL and PL/SQL
– Scalar Subquery, Bulk Collect and Collect Aggregator
– Table and Multiset operators
– Pipelined Table Functions
• Integrating with the external world
– Type turning XML and/or JSON
– Native Database Web Services
– Database Adapter in SOA Suite and Service Bus
Compare ECT:Unload all containers, than start moving out (train?)Unload one, put on truck and start driving; then unload next one
http://download.oracle.com/docs/cd/E11882_01/appdev.112/e16659/xdb13gen.htm#i1027254XSU – PL/SQL API http://download.oracle.com/docs/cd/E11882_01/appdev.112/e10708/adx_j_xsu.htm#BABGGGAH