SlideShare ist ein Scribd-Unternehmen logo
1 von 35
Downloaden Sie, um offline zu lesen
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)

Weitere ähnliche Inhalte

Was ist angesagt?

Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Achmad Solichin
 
Les03 (Using Single Row Functions To Customize Output)
Les03 (Using Single Row Functions To Customize Output)Les03 (Using Single Row Functions To Customize Output)
Les03 (Using Single Row Functions To Customize Output)Achmad Solichin
 
Troubleshooting Complex Performance issues - Oracle SEG$ contention
Troubleshooting Complex Performance issues - Oracle SEG$ contentionTroubleshooting Complex Performance issues - Oracle SEG$ contention
Troubleshooting Complex Performance issues - Oracle SEG$ contentionTanel Poder
 
Tanel Poder - Performance stories from Exadata Migrations
Tanel Poder - Performance stories from Exadata MigrationsTanel Poder - Performance stories from Exadata Migrations
Tanel Poder - Performance stories from Exadata MigrationsTanel Poder
 
Oracle Database SQL Tuning Concept
Oracle Database SQL Tuning ConceptOracle Database SQL Tuning Concept
Oracle Database SQL Tuning ConceptChien Chung Shen
 
How the Postgres Query Optimizer Works
How the Postgres Query Optimizer WorksHow the Postgres Query Optimizer Works
How the Postgres Query Optimizer WorksEDB
 
Advanced MySQL Query Tuning
Advanced MySQL Query TuningAdvanced MySQL Query Tuning
Advanced MySQL Query TuningAlexander Rubin
 
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptxFive_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptxMaria Colgan
 
MySQLとPostgreSQLの基本的な実行プラン比較
MySQLとPostgreSQLの基本的な実行プラン比較MySQLとPostgreSQLの基本的な実行プラン比較
MySQLとPostgreSQLの基本的な実行プラン比較Shinya Sugiyama
 
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Top 10 Mistakes When Migrating From Oracle to PostgreSQLTop 10 Mistakes When Migrating From Oracle to PostgreSQL
Top 10 Mistakes When Migrating From Oracle to PostgreSQLJim Mlodgenski
 
[APJ] Common Table Expressions (CTEs) in SQL
[APJ] Common Table Expressions (CTEs) in SQL[APJ] Common Table Expressions (CTEs) in SQL
[APJ] Common Table Expressions (CTEs) in SQLEDB
 
PostgreSQL Performance Tuning
PostgreSQL Performance TuningPostgreSQL Performance Tuning
PostgreSQL Performance Tuningelliando dias
 
09 Managing Dependencies
09 Managing Dependencies09 Managing Dependencies
09 Managing Dependenciesrehaniltifat
 
06 Using More Package Concepts
06 Using More Package Concepts06 Using More Package Concepts
06 Using More Package Conceptsrehaniltifat
 
Histograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQLHistograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQLSergey Petrunya
 
07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Developmentrehaniltifat
 
Making Nested Columns as First Citizen in Apache Spark SQL
Making Nested Columns as First Citizen in Apache Spark SQLMaking Nested Columns as First Citizen in Apache Spark SQL
Making Nested Columns as First Citizen in Apache Spark SQLDatabricks
 
Restricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data BaseRestricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data BaseSalman Memon
 

Was ist angesagt? (20)

Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)
 
Les03 (Using Single Row Functions To Customize Output)
Les03 (Using Single Row Functions To Customize Output)Les03 (Using Single Row Functions To Customize Output)
Les03 (Using Single Row Functions To Customize Output)
 
Troubleshooting Complex Performance issues - Oracle SEG$ contention
Troubleshooting Complex Performance issues - Oracle SEG$ contentionTroubleshooting Complex Performance issues - Oracle SEG$ contention
Troubleshooting Complex Performance issues - Oracle SEG$ contention
 
Tanel Poder - Performance stories from Exadata Migrations
Tanel Poder - Performance stories from Exadata MigrationsTanel Poder - Performance stories from Exadata Migrations
Tanel Poder - Performance stories from Exadata Migrations
 
Oracle Database SQL Tuning Concept
Oracle Database SQL Tuning ConceptOracle Database SQL Tuning Concept
Oracle Database SQL Tuning Concept
 
How the Postgres Query Optimizer Works
How the Postgres Query Optimizer WorksHow the Postgres Query Optimizer Works
How the Postgres Query Optimizer Works
 
Advanced MySQL Query Tuning
Advanced MySQL Query TuningAdvanced MySQL Query Tuning
Advanced MySQL Query Tuning
 
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptxFive_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
Five_Things_You_Might_Not_Know_About_Oracle_Database_v2.pptx
 
MySQLとPostgreSQLの基本的な実行プラン比較
MySQLとPostgreSQLの基本的な実行プラン比較MySQLとPostgreSQLの基本的な実行プラン比較
MySQLとPostgreSQLの基本的な実行プラン比較
 
Sql query patterns, optimized
Sql query patterns, optimizedSql query patterns, optimized
Sql query patterns, optimized
 
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
Top 10 Mistakes When Migrating From Oracle to PostgreSQLTop 10 Mistakes When Migrating From Oracle to PostgreSQL
Top 10 Mistakes When Migrating From Oracle to PostgreSQL
 
[APJ] Common Table Expressions (CTEs) in SQL
[APJ] Common Table Expressions (CTEs) in SQL[APJ] Common Table Expressions (CTEs) in SQL
[APJ] Common Table Expressions (CTEs) in SQL
 
PostgreSQL Performance Tuning
PostgreSQL Performance TuningPostgreSQL Performance Tuning
PostgreSQL Performance Tuning
 
09 Managing Dependencies
09 Managing Dependencies09 Managing Dependencies
09 Managing Dependencies
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
 
06 Using More Package Concepts
06 Using More Package Concepts06 Using More Package Concepts
06 Using More Package Concepts
 
Histograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQLHistograms in MariaDB, MySQL and PostgreSQL
Histograms in MariaDB, MySQL and PostgreSQL
 
07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development
 
Making Nested Columns as First Citizen in Apache Spark SQL
Making Nested Columns as First Citizen in Apache Spark SQLMaking Nested Columns as First Citizen in Apache Spark SQL
Making Nested Columns as First Citizen in Apache Spark SQL
 
Restricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data BaseRestricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data Base
 

Ähnlich wie 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
 

Kürzlich hochgeladen

CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
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
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
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
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
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.
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 

Kürzlich hochgeladen (20)

CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
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...
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
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
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
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...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 

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)