SlideShare a Scribd company logo

Polymorphic Table Functions in 18c

SQL offers many powerful techniques for analyzing your data out of the box, but being also extendable if you are still missing something. Now much more easier in 18c with polymorphic table functions (PTF). As an evolution of table functions, PTF is invoked in the FROM clause and is capable to encapsulate the custom processing of the input data, whereas the input row type does not have to be known at design time and the output row type may first be determined by the actual PTF invocation parameters. This session will give you an introduction based on simple examples. Discover how you can develop your own flexible and self-describing extensions while focusing on business logic and leaving complex things like parallel execution to the database.

1 of 35
Download to read offline
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));

Recommended

SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?Andrej Pashchenko
 
Polymorphic Table Functions in SQL
Polymorphic Table Functions in SQLPolymorphic Table Functions in SQL
Polymorphic Table Functions in SQLChris Saxon
 
Introducing the eDB360 Tool
Introducing the eDB360 ToolIntroducing the eDB360 Tool
Introducing the eDB360 ToolCarlos Sierra
 
Introduction to Oracle Data Guard Broker
Introduction to Oracle Data Guard BrokerIntroduction to Oracle Data Guard Broker
Introduction to Oracle Data Guard BrokerZohar Elkayam
 
MERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known FacetsMERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known FacetsAndrej Pashchenko
 
Inside PostgreSQL Shared Memory
Inside PostgreSQL Shared MemoryInside PostgreSQL Shared Memory
Inside PostgreSQL Shared MemoryEDB
 
UKOUG, Oracle Transaction Locks
UKOUG, Oracle Transaction LocksUKOUG, Oracle Transaction Locks
UKOUG, Oracle Transaction LocksKyle Hailey
 

More Related Content

What's hot

Advanced PLSQL Optimizing for Better Performance
Advanced PLSQL Optimizing for Better PerformanceAdvanced PLSQL Optimizing for Better Performance
Advanced PLSQL Optimizing for Better PerformanceZohar Elkayam
 
Row Pattern Matching in SQL:2016
Row Pattern Matching in SQL:2016Row Pattern Matching in SQL:2016
Row Pattern Matching in SQL:2016Markus Winand
 
Oracle statistics by example
Oracle statistics by exampleOracle statistics by example
Oracle statistics by exampleMauro Pagano
 
Same plan different performance
Same plan different performanceSame plan different performance
Same plan different performanceMauro Pagano
 
PostGreSQL Performance Tuning
PostGreSQL Performance TuningPostGreSQL Performance Tuning
PostGreSQL Performance TuningMaven Logix
 
ABAP 7.x New Features and Commands
ABAP 7.x New Features and CommandsABAP 7.x New Features and Commands
ABAP 7.x New Features and CommandsDr. Kerem Koseoglu
 
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
 
ABAP for Beginners - www.sapdocs.info
ABAP for Beginners - www.sapdocs.infoABAP for Beginners - www.sapdocs.info
ABAP for Beginners - www.sapdocs.infoY. Z. MERCAN
 
How to use histograms to get better performance
How to use histograms to get better performanceHow to use histograms to get better performance
How to use histograms to get better performanceMariaDB plc
 
Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle Kyle Hailey
 
The MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer TraceThe MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer Traceoysteing
 
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ OracleUnderstanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ OracleGuatemala User Group
 
IBM DB2 LUW UDB DBA Online Training by Etraining.guru
IBM DB2 LUW UDB DBA Online Training by Etraining.guruIBM DB2 LUW UDB DBA Online Training by Etraining.guru
IBM DB2 LUW UDB DBA Online Training by Etraining.guruRavikumar Nandigam
 
PostgreSQL Performance Tuning
PostgreSQL Performance TuningPostgreSQL Performance Tuning
PostgreSQL Performance Tuningelliando dias
 
Performance Tuning With Oracle ASH and AWR. Part 1 How And What
Performance Tuning With Oracle ASH and AWR. Part 1 How And WhatPerformance Tuning With Oracle ASH and AWR. Part 1 How And What
Performance Tuning With Oracle ASH and AWR. Part 1 How And Whatudaymoogala
 

What's hot (20)

Sap abap material
Sap abap materialSap abap material
Sap abap material
 
Advanced PLSQL Optimizing for Better Performance
Advanced PLSQL Optimizing for Better PerformanceAdvanced PLSQL Optimizing for Better Performance
Advanced PLSQL Optimizing for Better Performance
 
Row Pattern Matching in SQL:2016
Row Pattern Matching in SQL:2016Row Pattern Matching in SQL:2016
Row Pattern Matching in SQL:2016
 
Oracle statistics by example
Oracle statistics by exampleOracle statistics by example
Oracle statistics by example
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
Same plan different performance
Same plan different performanceSame plan different performance
Same plan different performance
 
PostGreSQL Performance Tuning
PostGreSQL Performance TuningPostGreSQL Performance Tuning
PostGreSQL Performance Tuning
 
ABAP 7.x New Features and Commands
ABAP 7.x New Features and CommandsABAP 7.x New Features and Commands
ABAP 7.x New Features and Commands
 
ORACLE_23-03-31_en.pdf
ORACLE_23-03-31_en.pdfORACLE_23-03-31_en.pdf
ORACLE_23-03-31_en.pdf
 
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
 
ABAP for Beginners - www.sapdocs.info
ABAP for Beginners - www.sapdocs.infoABAP for Beginners - www.sapdocs.info
ABAP for Beginners - www.sapdocs.info
 
How to use histograms to get better performance
How to use histograms to get better performanceHow to use histograms to get better performance
How to use histograms to get better performance
 
Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle Ash masters : advanced ash analytics on Oracle
Ash masters : advanced ash analytics on Oracle
 
The MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer TraceThe MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer Trace
 
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ OracleUnderstanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
 
IBM DB2 LUW UDB DBA Online Training by Etraining.guru
IBM DB2 LUW UDB DBA Online Training by Etraining.guruIBM DB2 LUW UDB DBA Online Training by Etraining.guru
IBM DB2 LUW UDB DBA Online Training by Etraining.guru
 
PostgreSQL Performance Tuning
PostgreSQL Performance TuningPostgreSQL Performance Tuning
PostgreSQL Performance Tuning
 
Performance Tuning With Oracle ASH and AWR. Part 1 How And What
Performance Tuning With Oracle ASH and AWR. Part 1 How And WhatPerformance Tuning With Oracle ASH and AWR. Part 1 How And What
Performance Tuning With Oracle ASH and AWR. Part 1 How And What
 
Indexes in postgres
Indexes in postgresIndexes in postgres
Indexes in postgres
 
Sql server basics
Sql server basicsSql server basics
Sql server basics
 

Similar to 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
 

Recently uploaded

Cybersecurity Measures For Remote Workers.pdf
Cybersecurity Measures For Remote Workers.pdfCybersecurity Measures For Remote Workers.pdf
Cybersecurity Measures For Remote Workers.pdfCIOWomenMagazine
 
OpenChain AI Study Group - North America and Europe - 2024-02-20
OpenChain AI Study Group - North America and Europe - 2024-02-20OpenChain AI Study Group - North America and Europe - 2024-02-20
OpenChain AI Study Group - North America and Europe - 2024-02-20Shane Coughlan
 
The Game-Changer_ How Software Development Outsource Can Catapult Your Growth...
The Game-Changer_ How Software Development Outsource Can Catapult Your Growth...The Game-Changer_ How Software Development Outsource Can Catapult Your Growth...
The Game-Changer_ How Software Development Outsource Can Catapult Your Growth...emili denli
 
"Taking an idea to a Product in Health diagnostics" by Dr. Geetha Manjunath, ...
"Taking an idea to a Product in Health diagnostics" by Dr. Geetha Manjunath, ..."Taking an idea to a Product in Health diagnostics" by Dr. Geetha Manjunath, ...
"Taking an idea to a Product in Health diagnostics" by Dr. Geetha Manjunath, ...ISPMAIndia
 
P1 Inspection Types in Municity 5 Smartsheet
P1 Inspection Types in Municity 5 SmartsheetP1 Inspection Types in Municity 5 Smartsheet
P1 Inspection Types in Municity 5 SmartsheetMatthewTHawley
 
AI Product Management by Abhijit Bendigiri
AI Product Management by Abhijit BendigiriAI Product Management by Abhijit Bendigiri
AI Product Management by Abhijit BendigiriISPMAIndia
 
Welcome to AltTask - the nexus where innovation converges with empowerment!
Welcome to AltTask - the nexus where innovation converges with empowerment!Welcome to AltTask - the nexus where innovation converges with empowerment!
Welcome to AltTask - the nexus where innovation converges with empowerment!alttaskcom
 
Product Manager vs Product Owner – Why Do Companies Still Struggle 23 Years A...
Product Manager vs Product Owner – Why Do Companies Still Struggle 23 Years A...Product Manager vs Product Owner – Why Do Companies Still Struggle 23 Years A...
Product Manager vs Product Owner – Why Do Companies Still Struggle 23 Years A...ISPMAIndia
 
The Age of AI: Elevating Experiences & Delivering Customer Value!
The Age of AI: Elevating Experiences & Delivering Customer Value!The Age of AI: Elevating Experiences & Delivering Customer Value!
The Age of AI: Elevating Experiences & Delivering Customer Value!ISPMAIndia
 
killingcamp 광고삽입문제 풀이, killingcamp 광고삽입문제 풀이
killingcamp 광고삽입문제 풀이, killingcamp 광고삽입문제 풀이killingcamp 광고삽입문제 풀이, killingcamp 광고삽입문제 풀이
killingcamp 광고삽입문제 풀이, killingcamp 광고삽입문제 풀이ssuser82c38d
 
Machine Learning Basics for Dummies (no math!)
Machine Learning Basics for Dummies (no math!)Machine Learning Basics for Dummies (no math!)
Machine Learning Basics for Dummies (no math!)Dmitry Zinoviev
 
Implementing Docker Containers with Windows Server 2019
Implementing Docker Containers with Windows Server 2019Implementing Docker Containers with Windows Server 2019
Implementing Docker Containers with Windows Server 2019VICTOR MAESTRE RAMIREZ
 
The Top Outages of 2023: Analyses and Takeaways
The Top Outages of 2023: Analyses and TakeawaysThe Top Outages of 2023: Analyses and Takeaways
The Top Outages of 2023: Analyses and TakeawaysThousandEyes
 
SPM 2024 – Overview of and benefits of AI in Product Management
SPM 2024 – Overview of and benefits of AI in Product ManagementSPM 2024 – Overview of and benefits of AI in Product Management
SPM 2024 – Overview of and benefits of AI in Product ManagementISPMAIndia
 
LLMOps with Azure Machine Learning prompt flow
LLMOps with Azure Machine Learning prompt flowLLMOps with Azure Machine Learning prompt flow
LLMOps with Azure Machine Learning prompt flowNaoki (Neo) SATO
 
DBA Fundamentals Group: Continuous SQL with Kafka and Flink
DBA Fundamentals Group: Continuous SQL with Kafka and FlinkDBA Fundamentals Group: Continuous SQL with Kafka and Flink
DBA Fundamentals Group: Continuous SQL with Kafka and FlinkTimothy Spann
 
No more Dockerfiles? Buildpacks to help you ship your image!
No more Dockerfiles? Buildpacks to help you ship your image!No more Dockerfiles? Buildpacks to help you ship your image!
No more Dockerfiles? Buildpacks to help you ship your image!Anthony Dahanne
 
killingcamp longest common subsequence.pdf
killingcamp longest common subsequence.pdfkillingcamp longest common subsequence.pdf
killingcamp longest common subsequence.pdfssuser82c38d
 
killing camp week 6 problem - maximal matrix.pdf
killing camp week 6 problem - maximal matrix.pdfkilling camp week 6 problem - maximal matrix.pdf
killing camp week 6 problem - maximal matrix.pdfssuser82c38d
 
Alluxio Monthly Webinar | Why a Multi-Cloud Strategy Matters for Your AI Plat...
Alluxio Monthly Webinar | Why a Multi-Cloud Strategy Matters for Your AI Plat...Alluxio Monthly Webinar | Why a Multi-Cloud Strategy Matters for Your AI Plat...
Alluxio Monthly Webinar | Why a Multi-Cloud Strategy Matters for Your AI Plat...Alluxio, Inc.
 

Recently uploaded (20)

Cybersecurity Measures For Remote Workers.pdf
Cybersecurity Measures For Remote Workers.pdfCybersecurity Measures For Remote Workers.pdf
Cybersecurity Measures For Remote Workers.pdf
 
OpenChain AI Study Group - North America and Europe - 2024-02-20
OpenChain AI Study Group - North America and Europe - 2024-02-20OpenChain AI Study Group - North America and Europe - 2024-02-20
OpenChain AI Study Group - North America and Europe - 2024-02-20
 
The Game-Changer_ How Software Development Outsource Can Catapult Your Growth...
The Game-Changer_ How Software Development Outsource Can Catapult Your Growth...The Game-Changer_ How Software Development Outsource Can Catapult Your Growth...
The Game-Changer_ How Software Development Outsource Can Catapult Your Growth...
 
"Taking an idea to a Product in Health diagnostics" by Dr. Geetha Manjunath, ...
"Taking an idea to a Product in Health diagnostics" by Dr. Geetha Manjunath, ..."Taking an idea to a Product in Health diagnostics" by Dr. Geetha Manjunath, ...
"Taking an idea to a Product in Health diagnostics" by Dr. Geetha Manjunath, ...
 
P1 Inspection Types in Municity 5 Smartsheet
P1 Inspection Types in Municity 5 SmartsheetP1 Inspection Types in Municity 5 Smartsheet
P1 Inspection Types in Municity 5 Smartsheet
 
AI Product Management by Abhijit Bendigiri
AI Product Management by Abhijit BendigiriAI Product Management by Abhijit Bendigiri
AI Product Management by Abhijit Bendigiri
 
Welcome to AltTask - the nexus where innovation converges with empowerment!
Welcome to AltTask - the nexus where innovation converges with empowerment!Welcome to AltTask - the nexus where innovation converges with empowerment!
Welcome to AltTask - the nexus where innovation converges with empowerment!
 
Product Manager vs Product Owner – Why Do Companies Still Struggle 23 Years A...
Product Manager vs Product Owner – Why Do Companies Still Struggle 23 Years A...Product Manager vs Product Owner – Why Do Companies Still Struggle 23 Years A...
Product Manager vs Product Owner – Why Do Companies Still Struggle 23 Years A...
 
The Age of AI: Elevating Experiences & Delivering Customer Value!
The Age of AI: Elevating Experiences & Delivering Customer Value!The Age of AI: Elevating Experiences & Delivering Customer Value!
The Age of AI: Elevating Experiences & Delivering Customer Value!
 
killingcamp 광고삽입문제 풀이, killingcamp 광고삽입문제 풀이
killingcamp 광고삽입문제 풀이, killingcamp 광고삽입문제 풀이killingcamp 광고삽입문제 풀이, killingcamp 광고삽입문제 풀이
killingcamp 광고삽입문제 풀이, killingcamp 광고삽입문제 풀이
 
Machine Learning Basics for Dummies (no math!)
Machine Learning Basics for Dummies (no math!)Machine Learning Basics for Dummies (no math!)
Machine Learning Basics for Dummies (no math!)
 
Implementing Docker Containers with Windows Server 2019
Implementing Docker Containers with Windows Server 2019Implementing Docker Containers with Windows Server 2019
Implementing Docker Containers with Windows Server 2019
 
The Top Outages of 2023: Analyses and Takeaways
The Top Outages of 2023: Analyses and TakeawaysThe Top Outages of 2023: Analyses and Takeaways
The Top Outages of 2023: Analyses and Takeaways
 
SPM 2024 – Overview of and benefits of AI in Product Management
SPM 2024 – Overview of and benefits of AI in Product ManagementSPM 2024 – Overview of and benefits of AI in Product Management
SPM 2024 – Overview of and benefits of AI in Product Management
 
LLMOps with Azure Machine Learning prompt flow
LLMOps with Azure Machine Learning prompt flowLLMOps with Azure Machine Learning prompt flow
LLMOps with Azure Machine Learning prompt flow
 
DBA Fundamentals Group: Continuous SQL with Kafka and Flink
DBA Fundamentals Group: Continuous SQL with Kafka and FlinkDBA Fundamentals Group: Continuous SQL with Kafka and Flink
DBA Fundamentals Group: Continuous SQL with Kafka and Flink
 
No more Dockerfiles? Buildpacks to help you ship your image!
No more Dockerfiles? Buildpacks to help you ship your image!No more Dockerfiles? Buildpacks to help you ship your image!
No more Dockerfiles? Buildpacks to help you ship your image!
 
killingcamp longest common subsequence.pdf
killingcamp longest common subsequence.pdfkillingcamp longest common subsequence.pdf
killingcamp longest common subsequence.pdf
 
killing camp week 6 problem - maximal matrix.pdf
killing camp week 6 problem - maximal matrix.pdfkilling camp week 6 problem - maximal matrix.pdf
killing camp week 6 problem - maximal matrix.pdf
 
Alluxio Monthly Webinar | Why a Multi-Cloud Strategy Matters for Your AI Plat...
Alluxio Monthly Webinar | Why a Multi-Cloud Strategy Matters for Your AI Plat...Alluxio Monthly Webinar | Why a Multi-Cloud Strategy Matters for Your AI Plat...
Alluxio Monthly Webinar | Why a Multi-Cloud Strategy Matters for Your AI Plat...
 

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)