SlideShare une entreprise Scribd logo
1  sur  35
Télécharger pour lire hors ligne
BASEL BERN BRUGG BUCHAREST DÜSSELDORF FRANKFURT A.M. FREIBURG I.BR. GENEVA
HAMBURG COPENHAGEN LAUSANNE MANNHEIM MUNICH STUTTGART VIENNA ZURICH
Blog
blog.sqlora.com
Polymorphic Table Functions in 18c
Andrej Pashchenko
Andrej_SQL
About me
Polymorphic Table Functions in 18c2 08.05.2019
Senior Consultant at Trivadis GmbH, Düsseldorf
Focus Oracle
– Data Warehousing
– Application Development
– Application Performance
Course instructor „Oracle New Features for Developer“
Blog: https://blog.sqlora.com
Table functions in Oracle before 18c
Polymorphic Table Functions in 18c3 08.05.2019
Table functions are known since 8i
The return collection type must be declared beforehand
Forwarding the data stream into the function in a SQL query is not trivial (CURSOR
parameter).
User defined aggregate functions using Oracle Data Cartridge Interface (ODCI)
CREATE OR REPLACE TYPE names_t IS TABLE OF VARCHAR2 (100);
SELECT t.*
FROM TABLE(get_emp_names()) t;
SMITH
ALLEN
WARD
JONES
Polymorphic Table Functions (PTF)
Polymorphic Table Functions in 18c4 08.05.2019
Evolution of table functions: part of the ANSI SQL2016 standard
Abstract complicated business logic and make it available generically at SQL level
SELECT from the function in the FROM clause
Like a view, but more procedural and dynamic (without dynamic SQL)
Polymorphic: a PTF supports different input and output data structures
– can accept generic table parameters whose structure does not have to be known on definition.
Oracle: exactly one table parameter is mandatory in 18c
– the return table type does not have to be declared on definition, it depends on the input
structure and additional function parameters.
Oracle provides the infrastructure in the DBMS_TF package and a new SQL pseudo-
operator COLUMNS().
Polymorphic Table Functions (PTF)
Polymorphic Table Functions in 18c6 08.05.2019
A B C
- - -
- - -
- - -
- - -
C1 C2 C3
- - -
- - -
- - -
- - -
A B D
- - -
- - -
- - -
- - -
- - -
Polymorphic Table Function
add/remove rows,
add/remove columns
Not
allowed!
Table T
MY_PTF( )
SELECT a,b,d
FROM MY_PTF(t);
Example Task for the First PTF
Polymorphic Table Functions in 18c7 08.05.2019
Keep only a defined list of columns of the source table in the output.
Concatenate the remaining (hidden) columns with a separator and display them as a
new column.
MY_PTF
SELECT a,b,d FROM MY_PTF(t, COLUMNS(a,b));
Fits to our task
Types of PTF
Polymorphic Table Functions in 18c8 08.05.2019
Each result row can be derived
exclusively from the current source
row.
Use cases:
– Adding new calculated columns
– Reformat the data set
– Output in a special format (JSON, CSV,
etc.)
– Replication
– Pivot / Transpose
The output row results by looking at the
current source row and already
processed rows
Works on the whole table or a (logical)
partition of it
Input table can optionally be partitioned
and sorted
Use cases:
– User-defined aggregate or window
functions
PTF
Row-Semantic Table-Semantic
Interface Methods
Polymorphic Table Functions in 18c9 08.05.2019
Each PTF needs an implementation package that provides the implementation of the
interface methods:
DESCRIBE OPEN FETCH_ROWS CLOSE
Parse/Compile Runtime
decsribes the
structure of the
result row
Setup Process the source
rows and produce the
result
Cleanup
mandatory! optional optional optional
Definition: Package and PTF
Polymorphic Table Functions in 18c10 08.05.2019
CREATE OR REPLACE PACKAGE my_ptf_package AS
FUNCTION describe (tab IN OUT dbms_tf.table_t, cols2stay IN dbms_tf.columns_t )
RETURN dbms_tf.describe_t;
PROCEDURE fetch_rows;
-- Not required for now
--PROCEDURE open;
--PROCEDURE close;
END my_ptf_package;
/
CREATE OR REPLACE FUNCTION my_ptf (tab TABLE, cols2stay COLUMNS )
RETURN TABLE PIPELINED ROW POLYMORPHIC USING my_ptf_package;
column list: these
columns should remain
DESCRIBE
Polymorphic Table Functions in 18c11 08.05.2019
The method is called by the database when parsing a SQL and it cannot be invoked
explicitly.
Defines the structure of the result set based on:
– the structure of the input table
– other passed parameters
The database converts the table metadata to DBMS_TF.TABLE_T and any existing
columns parameters to DBMS_TF.COLUMNS_T.
Returns the metadata about the new columns - DBMS_TF.DESCRIBE_T
The columns of the input table can be marked as "pass-through" or "for read"
(DBMS_TF.TABLE_T is an IN OUT parameter)
PASS-THROUGH and FOR READ Columns
Polymorphic Table Functions in 18c12 08.05.2019
A B C D E
- - - - -
- - - - -
- - - - -
- - - - -
D E F G
- - -
- - -
- - -
- - -
FOR_READ
Table T
MY_PTF( )
SELECT *
FROM MY_PTF(t);PASS_THROUGH
new columns F and G
PASS-THROUGH and FOR READ Columns
Polymorphic Table Functions in 18c13 08.05.2019
PASS THROUGH
passed unchanged to the result set
defaults:
– row semantic – all columns
– table semantic – no columns, except
partitioning clause*
FOR READ
only these columns can be
retrieved in FETCH_ROWS.
default - no columns
Both properties are NOT mutually exclusive!
The columns in the parameter
COLS2STAY are PASS_THROUGH,
all others are FOR_READ
* - not working in 18c. Bug?
Implementing DESCRIBE
Polymorphic Table Functions in 18c14 08.05.2019
…
FUNCTION describe (tab IN OUT dbms_tf.table_t, cols2stay IN dbms_tf.columns_t )
RETURN dbms_tf.describe_t IS
new_col_name CONSTANT VARCHAR2(30) := 'AGG_COL';
BEGIN
FOR I IN 1 .. tab.COLUMN.COUNT LOOP
IF NOT tab.COLUMN(i).description.name MEMBER OF cols2stay THEN
tab.column(i).pass_through := false;
tab.column(i).for_read := true;
END IF;
END LOOP;
RETURN dbms_tf.describe_t( new_columns =>
dbms_tf.columns_new_t( 1 => dbms_tf.column_metadata_t(
name => new_col_name,
TYPE => dbms_tf.type_varchar2)));
END;
…
Hide and aggregate these
columns
Default=true
for all others
Metadata about
the new columns
Implementing FETCH_ROWS
Polymorphic Table Functions in 18c15 08.05.2019
Context DESCRIBE
FOR READ Columns
New Columns
PASS_THROUGH columns
Context FETCH_ROWS
GET-Columns
PUT-Columns
Not visible in FETCH_ROWS
The Structure of ROWSET
Polymorphic Table Functions in 18c16 08.05.2019
Implementation of FETCH_ROWS
Polymorphic Table Functions in 18c17 08.05.2019
…
PROCEDURE fetch_rows IS
rowset dbms_tf.row_set_t;
colcnt PLS_INTEGER;
rowcnt PLS_INTEGER;
agg_col dbms_tf.tab_varchar2_t;
BEGIN
dbms_tf.get_row_set(rowset, rowcnt, colcnt);
FOR r IN 1..rowcnt LOOP
agg_col(r) := '';
FOR c IN 1..colcnt LOOP
agg_col(r) := agg_col(r)||';'||DBMS_TF.COL_TO_CHAR(rowset(c), r);
END LOOP;
agg_col(r) := ltrim (agg_col(r) ,';');
END LOOP;
dbms_tf.put_col(1, agg_col);
END;
…
fetch the read columns
„aggregate“
return the Data for the
new column
Polymorphism in Action
Polymorphic Table Functions in 18c18 08.05.2019
SQL> SELECT * FROM my_ptf(t, COLUMNS(A));
A AGG_COL
---------- --------------------------------------------------
1 2;3;"ONE"
4 5;6;"TWO"
7 8;9;"THREE"
SQL> SELECT * FROM my_ptf(t, COLUMNS(A,B));
A B AGG_COL
---------- ---------- --------------------------------------------------
1 2 3;"ONE"
4 5 6;"TWO"
7 8 9;"THREE"
SQL> SELECT * FROM my_ptf(scott.emp, COLUMNS(empno));
EMPNO AGG_COL
---------- --------------------------------------------------
7369 "SMITH";"CLERK";7902;"17-DEC-80";800;;20
7499 "ALLEN";"SALESMAN";7698;"20-FEB-81";1600;300;30
7521 "WARD";"SALESMAN";7698;"22-FEB-81";1250;500;30
Task 2 = Task 1 + JSON-Format
Polymorphic Table Functions in 18c19 08.05.2019
Keep only a defined list of columns of the source table in the output.
Return the remaining columns as JSON document
SPLIT_JSON
SELECT a,b, json_col FROM split_json(t, COLUMNS(a,b));
Implementation for Task 2
Polymorphic Table Functions in 18c20 08.05.2019
…
FUNCTION describe …
PROCEDURE fetch_rows IS
rowset dbms_tf.row_set_t;
rowcnt PLS_INTEGER;
json_col dbms_tf.tab_varchar2_t;
BEGIN
dbms_tf.get_row_set(rowset, rowcnt);
FOR r IN 1..rowcnt LOOP
json_col(r) := DBMS_TF.ROW_TO_CHAR(rowset, r, DBMS_TF.FORMAT_JSON);
END LOOP;
dbms_tf.put_col(1, json_col);
END;…
DESCRIBE is still the same…
Return the row
set as JSON
SPLIT_JSON
Polymorphic Table Functions in 18c21 08.05.2019
SQL> SELECT * FROM split_json(t, COLUMNS(A));
A JSON_COL
---------- -------------------------------------
1 {"B":2, "C":3, "V":"ONE"}
4 {"B":5, "C":6, "V":"TWO"}
7 {"B":8, "C":9, "V":"THREE"}
SQL> SELECT * FROM split_json(t, COLUMNS(A,B));
A B JSON_COL
---------- ---------- --------------------------
1 2 {"C":3, "V":"ONE"}
4 5 {"C":6, "V":"TWO"}
7 8 {"C":9, "V":"THREE"}
SQL> SELECT * FROM split_json(scott.emp, COLUMNS(empno));
EMPNO JSON_COL
---------- ----------------------------------------------------------------------
7369 {"ENAME":"SMITH", "JOB":"CLERK", "MGR":7902, "HIREDATE":"17-DEC-80", ...
7499 {"ENAME":"ALLEN", "JOB":"SALESMAN", "MGR":7698, "HIREDATE":"20-FEB-81", ...
7521 {"ENAME":"WARD", "JOB":"SALESMAN", "MGR":7698, "HIREDATE":"22-FEB-81", ...
Task 3: Transpose the Columns to Rows
Polymorphic Table Functions in 18c22 08.05.2019
Keep only a defined list of columns of the source table
in the output to identify the rows.
Return the remaining columns as key-value pairs
MY_PTF
SELECT * FROM tab2keyval(t, COLUMNS(a,b));
More rows in the
result than in the
source?
Row Replication
Polymorphic Table Functions in 18c23 08.05.2019
Row replication allows to multiply or hide
records
DBMS_TF.ROW_REPLICATION
As a fixed factor for all rows
Or a value per row
A flag have to be set in DESCRIBE,
ORA-62574 otherweise
At the moment it is the only way to add
new records. But pay attention to
PASS_THROUGH columns!
Source PTF Result 1:1
Source PTF Result
Result
Result
1:N
Source PTF 1:0
Implementation for Task 3
Polymorphic Table Functions in 18c24 08.05.2019
…
FUNCTION describe (tab IN OUT dbms_tf.table_t, cols2stay IN dbms_tf.columns_t )
RETURN dbms_tf.describe_t IS
BEGIN
FOR I IN 1 .. tab.COLUMN.COUNT LOOP
IF NOT tab.COLUMN(i).description.name MEMBER OF cols2stay THEN
tab.column(i).pass_through := false;
tab.column(i).for_read := true;
END IF;
END LOOP;
RETURN dbms_tf.describe_t(new_columns =>
dbms_tf.columns_new_t( 1 => dbms_tf.column_metadata_t(
name => 'KEY_NAME', TYPE => dbms_tf.type_varchar2),
2 => dbms_tf.column_metadata_t(
name => 'KEY_VALUE', TYPE => dbms_tf.type_varchar2)),
row_replication => true);
END;
…
Set the row-replication flag,
otherwise ORA-62574
Implementation for Task 3 - FETCH_ROWS
Polymorphic Table Functions in 18c25 08.05.2019
…
PROCEDURE fetch_rows IS
rowset dbms_tf.row_set_t;
repfac dbms_tf.tab_naturaln_t;
rowcnt PLS_INTEGER;
colcnt PLS_INTEGER;
name_col dbms_tf.tab_varchar2_t;
val_col dbms_tf.tab_varchar2_t;
env dbms_tf.env_t := dbms_tf.get_env();
BEGIN
dbms_tf.get_row_set(rowset, rowcnt, colcnt);
FOR i IN 1 .. rowcnt LOOP
repfac(i) := 0;
END LOOP;
...
Collections for new columns
Environment
information
Implementation for Task 3 - FETCH_ROWS
Polymorphic Table Functions in 18c26 08.05.2019
...
FOR r IN 1..rowcnt LOOP
FOR c IN 1..colcnt LOOP
repfac(r) := repfac(r) + 1;
name_col(nvl(name_col.last+1,1)) :=
INITCAP( regexp_replace(env.get_columns(c).name, '^"|"$'));
val_col(nvl(val_col.last+1,1)) := DBMS_TF.COL_TO_CHAR(rowset(c), r);
END LOOP;
END LOOP;
dbms_tf.row_replication(replication_factor => repfac);
dbms_tf.put_col(1, name_col);
dbms_tf.put_col(2, val_col);
END;
…
Duplicate the row for each column to be
transposed.
Set the replication
factor
TAB2KEYVAL
Polymorphic Table Functions in 18c27 08.05.2019
SQL> SELECT * FROM tab2keyval(t, COLUMNS(A,B));
A B KEY_NAME KEY_VALUE
---------- ---------- ---------- --------------------
1 2 C 3
1 2 V "ONE"
4 5 C 6
4 5 V "TWO"
7 8 C 9
7 8 V "THREE"
SQL> SELECT * FROM tab2keyval(scott.emp, COLUMNS(empno));
EMPNO KEY_NAME KEY_VALUE
---------- ---------- --------------------
7369 Ename "SMITH"
7369 Job "CLERK"
7369 Mgr 7902
7369 Hiredate "17-DEC-80"
7369 Sal 800
...
Task 4: The Length of the CSV representation
Polymorphic Table Functions in 18c28 08.05.2019
Based on Task 1, calculate the lengths of the CSV representation for the whole table
or logical partition
Table Semantic PTF!
Execution of FETCH_ROWS
Polymorphic Table Functions in 18c29 08.05.2019
FETCH_ROWS is executed 0 to N times
Data is processed in rowsets
The size of the rowset is calculated at runtime (<= 1024)
Can also be executed in parallel
– Row Semantics can be parallelized by the DB without restrictions
– Table Semantics: parallelization based on the PARTITION BY clause
The developer always works with the "active" rowset only
The required intermediate results can be stored in execution store (XSTORE) or
package structures protected by execution ID (XID).
Implementation for Task 4
Polymorphic Table Functions in 18c30 08.05.2019
…
FUNCTION describe (tab IN OUT dbms_tf.table_t, cols2sum IN dbms_tf.columns_t )
RETURN dbms_tf.describe_t IS
BEGIN
FOR I IN 1 .. tab.COLUMN.COUNT LOOP
IF tab.COLUMN(i).description.name MEMBER OF cols2sum THEN
tab.column(i).for_read := true;
END IF;
END LOOP;
RETURN dbms_tf.describe_t(
new_columns => dbms_tf.columns_new_t(
1 => dbms_tf.column_metadata_t(
name => 'LEN', TYPE => dbms_tf.type_number),
2 => dbms_tf.column_metadata_t(
name => 'SUM_LEN', TYPE => dbms_tf.type_number)));
END;
…
Implementation for Task 4
Polymorphic Table Functions in 18c31 08.05.2019
PROCEDURE fetch_rows IS
...
len_col dbms_tf.tab_number_t;
len_curr_col dbms_tf.tab_number_t;
v_len pls_integer;
v_currlen pls_integer := 0;
BEGIN
dbms_tf.get_row_set(rowset, rowcnt, colcnt);
dbms_tf.xstore_get('len', v_len);
v_currlen := nvl(v_len, 0);
FOR r IN 1..rowcnt LOOP
len_col(r) := length (rowset(1).tab_varchar2(r));
v_currlen := v_currlen + len_col(r);
len_curr_col(r) := v_currlen ;
END LOOP;
dbms_tf.xstore_set('len', v_len+v_currlen);
dbms_tf.put_col(1, len_col);
dbms_tf.put_col(2, len_curr_col);
END;
…
Reading the intermediate result
from the Execution Store
Saving the intermediate result in
the Execution Store
SUMLEN_PTF
Polymorphic Table Functions in 18c32 08.05.2019
SQL> select * from sumlen_ptf(my_ptf(scott.emp, COLUMNS(deptno)), columns(agg_col));
ORA-62569: nested polymorphic table function is disallowed
SQL> with agg as (SELECT * FROM my_ptf(scott.emp, COLUMNS(deptno)))
2 select * from sumlen_ptf(agg partition by deptno, columns(agg_col));
DEPTNO AGG_COL LEN SUM_LEN
---------- -------------------------------------------------- ---------- ----------
10 7782;"CLARK";"MANAGER";7839;"09-JUN-81";2450; 45 45
10 7839;"KING";"PRESIDENT";;"17-NOV-81";5000; 42 87
10 7934;"MILLER";"CLERK";7782;"23-JAN-82";1300; 44 131
20 7566;"JONES";"MANAGER";7839;"02-APR-81";2975; 45 45
20 7902;"FORD";"ANALYST";7566;"03-DEC-81";3000; 44 89
20 7876;"ADAMS";"CLERK";7788;"23-MAY-87";1100; 43 132
20 7369;"SMITH";"CLERK";7902;"17-DEC-80";800; 42 174
20 7788;"SCOTT";"ANALYST";7566;"19-APR-87";3000; 45 219
30 7521;"WARD";"SALESMAN";7698;"22-FEB-81";1250;500 48 48
30 7844;"TURNER";"SALESMAN";7698;"08-SEP-81";1500;0 48 96
30 7499;"ALLEN";"SALESMAN";7698;"20-FEB-81";1600;300 49 145
30 7900;"JAMES";"CLERK";7698;"03-DEC-81";950; 42 187
30 7698;"BLAKE";"MANAGER";7839;"01-MAY-81";2850; 45 232
30 7654;"MARTIN";"SALESMAN";7698;"28-SEP-81";1250;140 51 283
...
the input is partitioned
Predicate Pushdown
Polymorphic Table Functions in 18c33 08.05.2019
If semantically possible, predicates, projections, partitions are passed to the
underlying table.
Only possible with PASS_THROUGH and PARTITION BY columns
SELECT * FROM my_ptf(scott.emp, COLUMNS(deptno)) WHERE deptno = 10
---------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
| 1 | POLYMORPHIC TABLE FUNCTION | MY_PTF | 3 | 261 | | |
| 2 | TABLE ACCESS BY INDEX ROWID BATCHED| EMP | 3 | 114 | 2 (0)| 00:00:01 |
|* 3 | INDEX RANGE SCAN | SCOTT_DEPTNO_IDX | 3 | | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("EMP"."DEPTNO"=10)
TOPNPLUS
Polymorphic Table Functions in 18c34 08.05.2019
Return the Top-N records and an aggregation of the remaining rows in a single
query.
Not yet 100% properly implementable in 18.3
SQL> SELECT deptno, empno, ename, job, sal
2 FROM topnplus(scott.emp partition by deptno order by sal desc, COLUMNS(sal), columns(deptno), 2)
DEPTNO EMPNO ENAME JOB SAL
---------- ---------- ---------- ---------- ----------
10 7839 KING PRESIDENT 5000
10 7782 CLARK MANAGER 2450
10 1300
20 7788 SCOTT ANALYST 3000
20 7902 FORD ANALYST 3000
20 4875
30 7698 BLAKE MANAGER 2850
30 7499 ALLEN SALESMAN 1600
30 4950
Conclusion
Polymorphic Table Functions in 18c35 08.05.2019
Powerful and flexible extension for SQL
Clearly defined interface to the DB lets the developer do his job: more business logic,
less technical details
Performance optimizations right from the beginning
ANSI SQL but not all PTF features from ANSI SQL 2016 implemented yet
No real support for adding new rows yet
Real aggregate functions not yet 100% possible
Partly contradictory documentation
Test it!
Polymorphic Table Functions in 18c36 08.05.2019
http://blog.sqlora.com/en/tag/ptf/
http://standards.iso.org/ittf/PubliclyAvailableStandards/c069776_ISO_IEC_TR_19075-
7_2017.zip - document on PTF (ISO/IEC TR 19075-7:2017)

Contenu connexe

Tendances

SQL window functions for MySQL
SQL window functions for MySQLSQL window functions for MySQL
SQL window functions for MySQLDag H. Wanvik
 
MERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known FacetsMERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known FacetsAndrej Pashchenko
 
Performance Stability, Tips and Tricks and Underscores
Performance Stability, Tips and Tricks and UnderscoresPerformance Stability, Tips and Tricks and Underscores
Performance Stability, Tips and Tricks and UnderscoresJitendra Singh
 
Part1 of SQL Tuning Workshop - Understanding the Optimizer
Part1 of SQL Tuning Workshop - Understanding the OptimizerPart1 of SQL Tuning Workshop - Understanding the Optimizer
Part1 of SQL Tuning Workshop - Understanding the OptimizerMaria Colgan
 
HOT Understanding this important update optimization
HOT Understanding this important update optimizationHOT Understanding this important update optimization
HOT Understanding this important update optimizationGrant McAlister
 
Inside PostgreSQL Shared Memory
Inside PostgreSQL Shared MemoryInside PostgreSQL Shared Memory
Inside PostgreSQL Shared MemoryEDB
 
UKOUG - 25 years of hints and tips
UKOUG - 25 years of hints and tipsUKOUG - 25 years of hints and tips
UKOUG - 25 years of hints and tipsConnor McDonald
 
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스PgDay.Seoul
 
Introduction to PostgreSQL
Introduction to PostgreSQLIntroduction to PostgreSQL
Introduction to PostgreSQLMark Wong
 
Oracle LOB Internals and Performance Tuning
Oracle LOB Internals and Performance TuningOracle LOB Internals and Performance Tuning
Oracle LOB Internals and Performance TuningTanel Poder
 
Creating Views - oracle database
Creating Views - oracle databaseCreating Views - oracle database
Creating Views - oracle databaseSalman Memon
 
JSON in Oracle 18c and 19c
JSON in Oracle 18c and 19cJSON in Oracle 18c and 19c
JSON in Oracle 18c and 19cstewashton
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsNirav Shah
 
Oracle Database SQL Tuning Concept
Oracle Database SQL Tuning ConceptOracle Database SQL Tuning Concept
Oracle Database SQL Tuning ConceptChien Chung Shen
 
PostgreSQL Performance Tuning
PostgreSQL Performance TuningPostgreSQL Performance Tuning
PostgreSQL Performance Tuningelliando dias
 
Oracle Database Management Basic 1
Oracle Database Management Basic 1Oracle Database Management Basic 1
Oracle Database Management Basic 1Chien Chung Shen
 
Postgres MVCC - A Developer Centric View of Multi Version Concurrency Control
Postgres MVCC - A Developer Centric View of Multi Version Concurrency ControlPostgres MVCC - A Developer Centric View of Multi Version Concurrency Control
Postgres MVCC - A Developer Centric View of Multi Version Concurrency ControlReactive.IO
 

Tendances (20)

SQL window functions for MySQL
SQL window functions for MySQLSQL window functions for MySQL
SQL window functions for MySQL
 
MERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known FacetsMERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known Facets
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 
Performance Stability, Tips and Tricks and Underscores
Performance Stability, Tips and Tricks and UnderscoresPerformance Stability, Tips and Tricks and Underscores
Performance Stability, Tips and Tricks and Underscores
 
Part1 of SQL Tuning Workshop - Understanding the Optimizer
Part1 of SQL Tuning Workshop - Understanding the OptimizerPart1 of SQL Tuning Workshop - Understanding the Optimizer
Part1 of SQL Tuning Workshop - Understanding the Optimizer
 
HOT Understanding this important update optimization
HOT Understanding this important update optimizationHOT Understanding this important update optimization
HOT Understanding this important update optimization
 
Inside PostgreSQL Shared Memory
Inside PostgreSQL Shared MemoryInside PostgreSQL Shared Memory
Inside PostgreSQL Shared Memory
 
UKOUG - 25 years of hints and tips
UKOUG - 25 years of hints and tipsUKOUG - 25 years of hints and tips
UKOUG - 25 years of hints and tips
 
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
 
Introduction to PostgreSQL
Introduction to PostgreSQLIntroduction to PostgreSQL
Introduction to PostgreSQL
 
Oracle LOB Internals and Performance Tuning
Oracle LOB Internals and Performance TuningOracle LOB Internals and Performance Tuning
Oracle LOB Internals and Performance Tuning
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Creating Views - oracle database
Creating Views - oracle databaseCreating Views - oracle database
Creating Views - oracle database
 
JSON in Oracle 18c and 19c
JSON in Oracle 18c and 19cJSON in Oracle 18c and 19c
JSON in Oracle 18c and 19c
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tips
 
Oracle Database SQL Tuning Concept
Oracle Database SQL Tuning ConceptOracle Database SQL Tuning Concept
Oracle Database SQL Tuning Concept
 
PostgreSQL Performance Tuning
PostgreSQL Performance TuningPostgreSQL Performance Tuning
PostgreSQL Performance Tuning
 
Oracle Database Management Basic 1
Oracle Database Management Basic 1Oracle Database Management Basic 1
Oracle Database Management Basic 1
 
 
Postgres MVCC - A Developer Centric View of Multi Version Concurrency Control
Postgres MVCC - A Developer Centric View of Multi Version Concurrency ControlPostgres MVCC - A Developer Centric View of Multi Version Concurrency Control
Postgres MVCC - A Developer Centric View of Multi Version Concurrency Control
 

Similaire à Polymorphic Table Functions in 18c (20)

Sql statement,functions and joins
Sql statement,functions and joinsSql statement,functions and joins
Sql statement,functions and joins
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricks
 
Histograms: Pre-12c and now
Histograms: Pre-12c and nowHistograms: Pre-12c and now
Histograms: Pre-12c and now
 
Sql 
statements , functions & joins
Sql 
statements , functions  &  joinsSql 
statements , functions  &  joins
Sql 
statements , functions & joins
 
Trig
TrigTrig
Trig
 
Les09 Manipulating Data
Les09 Manipulating DataLes09 Manipulating Data
Les09 Manipulating Data
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Mysql1
Mysql1Mysql1
Mysql1
 
Les10 Creating And Managing Tables
Les10 Creating And Managing TablesLes10 Creating And Managing Tables
Les10 Creating And Managing Tables
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
 
Online Statistics Gathering for ETL
Online Statistics Gathering for ETLOnline Statistics Gathering for ETL
Online Statistics Gathering for ETL
 
Function and types
Function  and typesFunction  and types
Function and types
 
Les09
Les09Les09
Les09
 
Advanced plsql mock_assessment
Advanced plsql mock_assessmentAdvanced plsql mock_assessment
Advanced plsql mock_assessment
 
Mysql
MysqlMysql
Mysql
 
Mysql
MysqlMysql
Mysql
 
Mysql
MysqlMysql
Mysql
 
Select To Order By
Select  To  Order BySelect  To  Order By
Select To Order By
 
Introduction to MySQL - Part 2
Introduction to MySQL - Part 2Introduction to MySQL - Part 2
Introduction to MySQL - Part 2
 
Columnrename9i
Columnrename9iColumnrename9i
Columnrename9i
 

Dernier

Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 

Dernier (20)

Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 

Polymorphic Table Functions in 18c

  • 1. BASEL BERN BRUGG BUCHAREST DÜSSELDORF FRANKFURT A.M. FREIBURG I.BR. GENEVA HAMBURG COPENHAGEN LAUSANNE MANNHEIM MUNICH STUTTGART VIENNA ZURICH Blog blog.sqlora.com Polymorphic Table Functions in 18c Andrej Pashchenko Andrej_SQL
  • 2. About me Polymorphic Table Functions in 18c2 08.05.2019 Senior Consultant at Trivadis GmbH, Düsseldorf Focus Oracle – Data Warehousing – Application Development – Application Performance Course instructor „Oracle New Features for Developer“ Blog: https://blog.sqlora.com
  • 3. Table functions in Oracle before 18c Polymorphic Table Functions in 18c3 08.05.2019 Table functions are known since 8i The return collection type must be declared beforehand Forwarding the data stream into the function in a SQL query is not trivial (CURSOR parameter). User defined aggregate functions using Oracle Data Cartridge Interface (ODCI) CREATE OR REPLACE TYPE names_t IS TABLE OF VARCHAR2 (100); SELECT t.* FROM TABLE(get_emp_names()) t; SMITH ALLEN WARD JONES
  • 4. Polymorphic Table Functions (PTF) Polymorphic Table Functions in 18c4 08.05.2019 Evolution of table functions: part of the ANSI SQL2016 standard Abstract complicated business logic and make it available generically at SQL level SELECT from the function in the FROM clause Like a view, but more procedural and dynamic (without dynamic SQL) Polymorphic: a PTF supports different input and output data structures – can accept generic table parameters whose structure does not have to be known on definition. Oracle: exactly one table parameter is mandatory in 18c – the return table type does not have to be declared on definition, it depends on the input structure and additional function parameters. Oracle provides the infrastructure in the DBMS_TF package and a new SQL pseudo- operator COLUMNS().
  • 5. Polymorphic Table Functions (PTF) Polymorphic Table Functions in 18c6 08.05.2019 A B C - - - - - - - - - - - - C1 C2 C3 - - - - - - - - - - - - A B D - - - - - - - - - - - - - - - Polymorphic Table Function add/remove rows, add/remove columns Not allowed! Table T MY_PTF( ) SELECT a,b,d FROM MY_PTF(t);
  • 6. Example Task for the First PTF Polymorphic Table Functions in 18c7 08.05.2019 Keep only a defined list of columns of the source table in the output. Concatenate the remaining (hidden) columns with a separator and display them as a new column. MY_PTF SELECT a,b,d FROM MY_PTF(t, COLUMNS(a,b));
  • 7. Fits to our task Types of PTF Polymorphic Table Functions in 18c8 08.05.2019 Each result row can be derived exclusively from the current source row. Use cases: – Adding new calculated columns – Reformat the data set – Output in a special format (JSON, CSV, etc.) – Replication – Pivot / Transpose The output row results by looking at the current source row and already processed rows Works on the whole table or a (logical) partition of it Input table can optionally be partitioned and sorted Use cases: – User-defined aggregate or window functions PTF Row-Semantic Table-Semantic
  • 8. Interface Methods Polymorphic Table Functions in 18c9 08.05.2019 Each PTF needs an implementation package that provides the implementation of the interface methods: DESCRIBE OPEN FETCH_ROWS CLOSE Parse/Compile Runtime decsribes the structure of the result row Setup Process the source rows and produce the result Cleanup mandatory! optional optional optional
  • 9. Definition: Package and PTF Polymorphic Table Functions in 18c10 08.05.2019 CREATE OR REPLACE PACKAGE my_ptf_package AS FUNCTION describe (tab IN OUT dbms_tf.table_t, cols2stay IN dbms_tf.columns_t ) RETURN dbms_tf.describe_t; PROCEDURE fetch_rows; -- Not required for now --PROCEDURE open; --PROCEDURE close; END my_ptf_package; / CREATE OR REPLACE FUNCTION my_ptf (tab TABLE, cols2stay COLUMNS ) RETURN TABLE PIPELINED ROW POLYMORPHIC USING my_ptf_package; column list: these columns should remain
  • 10. DESCRIBE Polymorphic Table Functions in 18c11 08.05.2019 The method is called by the database when parsing a SQL and it cannot be invoked explicitly. Defines the structure of the result set based on: – the structure of the input table – other passed parameters The database converts the table metadata to DBMS_TF.TABLE_T and any existing columns parameters to DBMS_TF.COLUMNS_T. Returns the metadata about the new columns - DBMS_TF.DESCRIBE_T The columns of the input table can be marked as "pass-through" or "for read" (DBMS_TF.TABLE_T is an IN OUT parameter)
  • 11. PASS-THROUGH and FOR READ Columns Polymorphic Table Functions in 18c12 08.05.2019 A B C D E - - - - - - - - - - - - - - - - - - - - D E F G - - - - - - - - - - - - FOR_READ Table T MY_PTF( ) SELECT * FROM MY_PTF(t);PASS_THROUGH new columns F and G
  • 12. PASS-THROUGH and FOR READ Columns Polymorphic Table Functions in 18c13 08.05.2019 PASS THROUGH passed unchanged to the result set defaults: – row semantic – all columns – table semantic – no columns, except partitioning clause* FOR READ only these columns can be retrieved in FETCH_ROWS. default - no columns Both properties are NOT mutually exclusive! The columns in the parameter COLS2STAY are PASS_THROUGH, all others are FOR_READ * - not working in 18c. Bug?
  • 13. Implementing DESCRIBE Polymorphic Table Functions in 18c14 08.05.2019 … FUNCTION describe (tab IN OUT dbms_tf.table_t, cols2stay IN dbms_tf.columns_t ) RETURN dbms_tf.describe_t IS new_col_name CONSTANT VARCHAR2(30) := 'AGG_COL'; BEGIN FOR I IN 1 .. tab.COLUMN.COUNT LOOP IF NOT tab.COLUMN(i).description.name MEMBER OF cols2stay THEN tab.column(i).pass_through := false; tab.column(i).for_read := true; END IF; END LOOP; RETURN dbms_tf.describe_t( new_columns => dbms_tf.columns_new_t( 1 => dbms_tf.column_metadata_t( name => new_col_name, TYPE => dbms_tf.type_varchar2))); END; … Hide and aggregate these columns Default=true for all others Metadata about the new columns
  • 14. Implementing FETCH_ROWS Polymorphic Table Functions in 18c15 08.05.2019 Context DESCRIBE FOR READ Columns New Columns PASS_THROUGH columns Context FETCH_ROWS GET-Columns PUT-Columns Not visible in FETCH_ROWS
  • 15. The Structure of ROWSET Polymorphic Table Functions in 18c16 08.05.2019
  • 16. Implementation of FETCH_ROWS Polymorphic Table Functions in 18c17 08.05.2019 … PROCEDURE fetch_rows IS rowset dbms_tf.row_set_t; colcnt PLS_INTEGER; rowcnt PLS_INTEGER; agg_col dbms_tf.tab_varchar2_t; BEGIN dbms_tf.get_row_set(rowset, rowcnt, colcnt); FOR r IN 1..rowcnt LOOP agg_col(r) := ''; FOR c IN 1..colcnt LOOP agg_col(r) := agg_col(r)||';'||DBMS_TF.COL_TO_CHAR(rowset(c), r); END LOOP; agg_col(r) := ltrim (agg_col(r) ,';'); END LOOP; dbms_tf.put_col(1, agg_col); END; … fetch the read columns „aggregate“ return the Data for the new column
  • 17. Polymorphism in Action Polymorphic Table Functions in 18c18 08.05.2019 SQL> SELECT * FROM my_ptf(t, COLUMNS(A)); A AGG_COL ---------- -------------------------------------------------- 1 2;3;"ONE" 4 5;6;"TWO" 7 8;9;"THREE" SQL> SELECT * FROM my_ptf(t, COLUMNS(A,B)); A B AGG_COL ---------- ---------- -------------------------------------------------- 1 2 3;"ONE" 4 5 6;"TWO" 7 8 9;"THREE" SQL> SELECT * FROM my_ptf(scott.emp, COLUMNS(empno)); EMPNO AGG_COL ---------- -------------------------------------------------- 7369 "SMITH";"CLERK";7902;"17-DEC-80";800;;20 7499 "ALLEN";"SALESMAN";7698;"20-FEB-81";1600;300;30 7521 "WARD";"SALESMAN";7698;"22-FEB-81";1250;500;30
  • 18. Task 2 = Task 1 + JSON-Format Polymorphic Table Functions in 18c19 08.05.2019 Keep only a defined list of columns of the source table in the output. Return the remaining columns as JSON document SPLIT_JSON SELECT a,b, json_col FROM split_json(t, COLUMNS(a,b));
  • 19. Implementation for Task 2 Polymorphic Table Functions in 18c20 08.05.2019 … FUNCTION describe … PROCEDURE fetch_rows IS rowset dbms_tf.row_set_t; rowcnt PLS_INTEGER; json_col dbms_tf.tab_varchar2_t; BEGIN dbms_tf.get_row_set(rowset, rowcnt); FOR r IN 1..rowcnt LOOP json_col(r) := DBMS_TF.ROW_TO_CHAR(rowset, r, DBMS_TF.FORMAT_JSON); END LOOP; dbms_tf.put_col(1, json_col); END;… DESCRIBE is still the same… Return the row set as JSON
  • 20. SPLIT_JSON Polymorphic Table Functions in 18c21 08.05.2019 SQL> SELECT * FROM split_json(t, COLUMNS(A)); A JSON_COL ---------- ------------------------------------- 1 {"B":2, "C":3, "V":"ONE"} 4 {"B":5, "C":6, "V":"TWO"} 7 {"B":8, "C":9, "V":"THREE"} SQL> SELECT * FROM split_json(t, COLUMNS(A,B)); A B JSON_COL ---------- ---------- -------------------------- 1 2 {"C":3, "V":"ONE"} 4 5 {"C":6, "V":"TWO"} 7 8 {"C":9, "V":"THREE"} SQL> SELECT * FROM split_json(scott.emp, COLUMNS(empno)); EMPNO JSON_COL ---------- ---------------------------------------------------------------------- 7369 {"ENAME":"SMITH", "JOB":"CLERK", "MGR":7902, "HIREDATE":"17-DEC-80", ... 7499 {"ENAME":"ALLEN", "JOB":"SALESMAN", "MGR":7698, "HIREDATE":"20-FEB-81", ... 7521 {"ENAME":"WARD", "JOB":"SALESMAN", "MGR":7698, "HIREDATE":"22-FEB-81", ...
  • 21. Task 3: Transpose the Columns to Rows Polymorphic Table Functions in 18c22 08.05.2019 Keep only a defined list of columns of the source table in the output to identify the rows. Return the remaining columns as key-value pairs MY_PTF SELECT * FROM tab2keyval(t, COLUMNS(a,b)); More rows in the result than in the source?
  • 22. Row Replication Polymorphic Table Functions in 18c23 08.05.2019 Row replication allows to multiply or hide records DBMS_TF.ROW_REPLICATION As a fixed factor for all rows Or a value per row A flag have to be set in DESCRIBE, ORA-62574 otherweise At the moment it is the only way to add new records. But pay attention to PASS_THROUGH columns! Source PTF Result 1:1 Source PTF Result Result Result 1:N Source PTF 1:0
  • 23. Implementation for Task 3 Polymorphic Table Functions in 18c24 08.05.2019 … FUNCTION describe (tab IN OUT dbms_tf.table_t, cols2stay IN dbms_tf.columns_t ) RETURN dbms_tf.describe_t IS BEGIN FOR I IN 1 .. tab.COLUMN.COUNT LOOP IF NOT tab.COLUMN(i).description.name MEMBER OF cols2stay THEN tab.column(i).pass_through := false; tab.column(i).for_read := true; END IF; END LOOP; RETURN dbms_tf.describe_t(new_columns => dbms_tf.columns_new_t( 1 => dbms_tf.column_metadata_t( name => 'KEY_NAME', TYPE => dbms_tf.type_varchar2), 2 => dbms_tf.column_metadata_t( name => 'KEY_VALUE', TYPE => dbms_tf.type_varchar2)), row_replication => true); END; … Set the row-replication flag, otherwise ORA-62574
  • 24. Implementation for Task 3 - FETCH_ROWS Polymorphic Table Functions in 18c25 08.05.2019 … PROCEDURE fetch_rows IS rowset dbms_tf.row_set_t; repfac dbms_tf.tab_naturaln_t; rowcnt PLS_INTEGER; colcnt PLS_INTEGER; name_col dbms_tf.tab_varchar2_t; val_col dbms_tf.tab_varchar2_t; env dbms_tf.env_t := dbms_tf.get_env(); BEGIN dbms_tf.get_row_set(rowset, rowcnt, colcnt); FOR i IN 1 .. rowcnt LOOP repfac(i) := 0; END LOOP; ... Collections for new columns Environment information
  • 25. Implementation for Task 3 - FETCH_ROWS Polymorphic Table Functions in 18c26 08.05.2019 ... FOR r IN 1..rowcnt LOOP FOR c IN 1..colcnt LOOP repfac(r) := repfac(r) + 1; name_col(nvl(name_col.last+1,1)) := INITCAP( regexp_replace(env.get_columns(c).name, '^"|"$')); val_col(nvl(val_col.last+1,1)) := DBMS_TF.COL_TO_CHAR(rowset(c), r); END LOOP; END LOOP; dbms_tf.row_replication(replication_factor => repfac); dbms_tf.put_col(1, name_col); dbms_tf.put_col(2, val_col); END; … Duplicate the row for each column to be transposed. Set the replication factor
  • 26. TAB2KEYVAL Polymorphic Table Functions in 18c27 08.05.2019 SQL> SELECT * FROM tab2keyval(t, COLUMNS(A,B)); A B KEY_NAME KEY_VALUE ---------- ---------- ---------- -------------------- 1 2 C 3 1 2 V "ONE" 4 5 C 6 4 5 V "TWO" 7 8 C 9 7 8 V "THREE" SQL> SELECT * FROM tab2keyval(scott.emp, COLUMNS(empno)); EMPNO KEY_NAME KEY_VALUE ---------- ---------- -------------------- 7369 Ename "SMITH" 7369 Job "CLERK" 7369 Mgr 7902 7369 Hiredate "17-DEC-80" 7369 Sal 800 ...
  • 27. Task 4: The Length of the CSV representation Polymorphic Table Functions in 18c28 08.05.2019 Based on Task 1, calculate the lengths of the CSV representation for the whole table or logical partition Table Semantic PTF!
  • 28. Execution of FETCH_ROWS Polymorphic Table Functions in 18c29 08.05.2019 FETCH_ROWS is executed 0 to N times Data is processed in rowsets The size of the rowset is calculated at runtime (<= 1024) Can also be executed in parallel – Row Semantics can be parallelized by the DB without restrictions – Table Semantics: parallelization based on the PARTITION BY clause The developer always works with the "active" rowset only The required intermediate results can be stored in execution store (XSTORE) or package structures protected by execution ID (XID).
  • 29. Implementation for Task 4 Polymorphic Table Functions in 18c30 08.05.2019 … FUNCTION describe (tab IN OUT dbms_tf.table_t, cols2sum IN dbms_tf.columns_t ) RETURN dbms_tf.describe_t IS BEGIN FOR I IN 1 .. tab.COLUMN.COUNT LOOP IF tab.COLUMN(i).description.name MEMBER OF cols2sum THEN tab.column(i).for_read := true; END IF; END LOOP; RETURN dbms_tf.describe_t( new_columns => dbms_tf.columns_new_t( 1 => dbms_tf.column_metadata_t( name => 'LEN', TYPE => dbms_tf.type_number), 2 => dbms_tf.column_metadata_t( name => 'SUM_LEN', TYPE => dbms_tf.type_number))); END; …
  • 30. Implementation for Task 4 Polymorphic Table Functions in 18c31 08.05.2019 PROCEDURE fetch_rows IS ... len_col dbms_tf.tab_number_t; len_curr_col dbms_tf.tab_number_t; v_len pls_integer; v_currlen pls_integer := 0; BEGIN dbms_tf.get_row_set(rowset, rowcnt, colcnt); dbms_tf.xstore_get('len', v_len); v_currlen := nvl(v_len, 0); FOR r IN 1..rowcnt LOOP len_col(r) := length (rowset(1).tab_varchar2(r)); v_currlen := v_currlen + len_col(r); len_curr_col(r) := v_currlen ; END LOOP; dbms_tf.xstore_set('len', v_len+v_currlen); dbms_tf.put_col(1, len_col); dbms_tf.put_col(2, len_curr_col); END; … Reading the intermediate result from the Execution Store Saving the intermediate result in the Execution Store
  • 31. SUMLEN_PTF Polymorphic Table Functions in 18c32 08.05.2019 SQL> select * from sumlen_ptf(my_ptf(scott.emp, COLUMNS(deptno)), columns(agg_col)); ORA-62569: nested polymorphic table function is disallowed SQL> with agg as (SELECT * FROM my_ptf(scott.emp, COLUMNS(deptno))) 2 select * from sumlen_ptf(agg partition by deptno, columns(agg_col)); DEPTNO AGG_COL LEN SUM_LEN ---------- -------------------------------------------------- ---------- ---------- 10 7782;"CLARK";"MANAGER";7839;"09-JUN-81";2450; 45 45 10 7839;"KING";"PRESIDENT";;"17-NOV-81";5000; 42 87 10 7934;"MILLER";"CLERK";7782;"23-JAN-82";1300; 44 131 20 7566;"JONES";"MANAGER";7839;"02-APR-81";2975; 45 45 20 7902;"FORD";"ANALYST";7566;"03-DEC-81";3000; 44 89 20 7876;"ADAMS";"CLERK";7788;"23-MAY-87";1100; 43 132 20 7369;"SMITH";"CLERK";7902;"17-DEC-80";800; 42 174 20 7788;"SCOTT";"ANALYST";7566;"19-APR-87";3000; 45 219 30 7521;"WARD";"SALESMAN";7698;"22-FEB-81";1250;500 48 48 30 7844;"TURNER";"SALESMAN";7698;"08-SEP-81";1500;0 48 96 30 7499;"ALLEN";"SALESMAN";7698;"20-FEB-81";1600;300 49 145 30 7900;"JAMES";"CLERK";7698;"03-DEC-81";950; 42 187 30 7698;"BLAKE";"MANAGER";7839;"01-MAY-81";2850; 45 232 30 7654;"MARTIN";"SALESMAN";7698;"28-SEP-81";1250;140 51 283 ... the input is partitioned
  • 32. Predicate Pushdown Polymorphic Table Functions in 18c33 08.05.2019 If semantically possible, predicates, projections, partitions are passed to the underlying table. Only possible with PASS_THROUGH and PARTITION BY columns SELECT * FROM my_ptf(scott.emp, COLUMNS(deptno)) WHERE deptno = 10 --------------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | | | 2 (100)| | | 1 | POLYMORPHIC TABLE FUNCTION | MY_PTF | 3 | 261 | | | | 2 | TABLE ACCESS BY INDEX ROWID BATCHED| EMP | 3 | 114 | 2 (0)| 00:00:01 | |* 3 | INDEX RANGE SCAN | SCOTT_DEPTNO_IDX | 3 | | 1 (0)| 00:00:01 | --------------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 3 - access("EMP"."DEPTNO"=10)
  • 33. TOPNPLUS Polymorphic Table Functions in 18c34 08.05.2019 Return the Top-N records and an aggregation of the remaining rows in a single query. Not yet 100% properly implementable in 18.3 SQL> SELECT deptno, empno, ename, job, sal 2 FROM topnplus(scott.emp partition by deptno order by sal desc, COLUMNS(sal), columns(deptno), 2) DEPTNO EMPNO ENAME JOB SAL ---------- ---------- ---------- ---------- ---------- 10 7839 KING PRESIDENT 5000 10 7782 CLARK MANAGER 2450 10 1300 20 7788 SCOTT ANALYST 3000 20 7902 FORD ANALYST 3000 20 4875 30 7698 BLAKE MANAGER 2850 30 7499 ALLEN SALESMAN 1600 30 4950
  • 34. Conclusion Polymorphic Table Functions in 18c35 08.05.2019 Powerful and flexible extension for SQL Clearly defined interface to the DB lets the developer do his job: more business logic, less technical details Performance optimizations right from the beginning ANSI SQL but not all PTF features from ANSI SQL 2016 implemented yet No real support for adding new rows yet Real aggregate functions not yet 100% possible Partly contradictory documentation Test it!
  • 35. Polymorphic Table Functions in 18c36 08.05.2019 http://blog.sqlora.com/en/tag/ptf/ http://standards.iso.org/ittf/PubliclyAvailableStandards/c069776_ISO_IEC_TR_19075- 7_2017.zip - document on PTF (ISO/IEC TR 19075-7:2017)