When organizations upgrade to the latest release of the Oracle Database, they will benefit from many improvements that are inherent to the product. However, to fully make use of the new release and thereby leverage the investment in Oracle technology to the max, it is important that DBAs and developers learn how to use new features. If they are not upgraded along with the database, they may use a modern platform no differently than its decade old predecessor.
This session will demonstrate a number of features in 12c that help developers achieve real benefits. These include Edition Based Redefinition, Data Redaction, JSON and RESTful Services, Pattern Matching, User Defined Types & XML and SQL Translation.
outline
Although most organizations using Oracle Database regularly upgrade to new releases, many of them spend little time on the upgrade of their database administrators and developers. Developers who have learned their essential skills in PL/SQL, SQL and database development with Oracle Database 7, 8 or even 9 often have never really absorbed the essence of later releases. They may have Oracle Database 12c at their fingertips, but fail to get their (bosses') money's worth from it. This obviously is a waste. If you use today's database platform as if it were its 20 year old predecessor, you might as well start using a much cheaper alternative. Instead, by spending some time on getting acquainted with modern capabilities of the Oracle Database, these database professionals will be able to make their databases fly again. Recent features and mechanisms will help them be more productive, create better performing and better scaling applications and write code that is elegant, concise and far more maintainable.
This will be a fast-paced session that challenges and sparks your creativity with SQL. And it provides you with a number of SQL power-tools that will help improve performance and maintainability of virtually any application. Any Oracle developer will benefit from this SQL injection ;-)
Slides from the Singapore Oracle Sessions presentation on July 13th 2015, sponsored by the Oracle ACE Program and organized by Doug Burns.
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Singpore Oracle Sessions III - What is truly useful in Oracle Database 12c for database developers
1. Lucas Jellema
Singapore Oracle Sessions III - 14th July 2015
What is truly relevant for database
developers in Oracle Database 12c
2. The Presenter:
Lucas Jellema
• Lives in The Netherlands
(close to Amsterdam)
• Started doing Oracle in 1994 with
Oracle Consulting (Oracle Designer, Forms, Database)
• Joined AMIS in 2002 – now working as CTO,
Consultant (Architect, Technical Lead, Programmer)
and Trainer
• Oracle ACE (2005) & ACE Director (2006)
• Author of ‘Oracle SOA Suite 11g Handbook’
(Oracle Press, 2010), ‘Oracle SOA Suite 12c Handbook’ (2015)
• Presenter at Oracle OpenWorld, JavaOne and
many Oracle and Java User Group Conferences
• Frequent blogger at http://technology.amis.nl
• Active with SQL & PL/SQL, Java EE & ADF,
SOA, BPM & more Fusion Middleware
12. 12
JSON
• Light Weight, Structured, fairly tied coupled (low bandwidth) interactions
between application[component]s
• Very popular in rich client web applications and mobile apps – usually as
the format used in REST-services
• Oracle Database 12c is JSON aware
– Store documents (in a column VARCHAR2, CLOB, NCLOB, BLOB, RAW, BFILE –
and have them checked for JSON validity
– Create Indexes on JSON contents
– Access JSON content directly from SQL queries
– Note: similar to but only a subset of XML support in Oracle Database
(as JSON in terms of functionality is a somewhat pale subset of XML)
• Oracle Driver for Node.js – similar to JBDC, all in JSON
alter table X
add CONSTRAINT ensure_json_chk CHECK (column_y IS JSON))
13. 13
JSON in the Oracle Database
• Why JSON in relational database?
– Consistency (integrity, transaction ACIDity) for storing JSON documents
– Quickly interpret data received (and stored?) in JSON format
– Leverage JSON as a convenient way to handle structured data as a string
– Easy data transfer over some protocols, including HTTP – especially when database
engages directly in HTTP interactions
15. 15
Check for a valid JSON
document and/or path result
• IS JSON can be used in the WHERE clause to filter on rows that contain a
valid JSON document
– It can be used in CHECK Constraints for that same reason
• JSON_EXISTS is typically used in the WHERE clause to filter on rows
whose JSON content satisfies some JSON path condition
select *
from customers
where json_exists(doc, '$.addresses.privateAddress')
select *
from customers
where doc IS JSON
16. 16
JSON_VALUE to retrieve scalar
value from JSON document
• JSON_VALUE is a SQL Function used to retrieve a scalar value from a
JSON document
– JSON_VALUE can be used like any other SQL Function in queries and DML – in
various clauses like SELECT, WHERE, GROUP BY, etc.
select json_value
( '{"matches":
[ {"matchLineUp": "NED-ESP"
, "score": "5-2","matchDate":"13-06-2014"}
, {"matchLineUp": "SWI-FRA"
, "score": "2-5","matchDate":"20-06-2014"}
, {"matchLineUp": "GER-BRA"
, "score": "1-7","matchDate":"08-07-2014"}
]
}'
, '$.matches[1].score') as "2nd_match_score"
from dual
17. 17
JSON_QUERY to retrieve JSON-
snippets from a JSON document
select json_query
( '{"matches":
[ {"matchLineUp": "NED-ESP"
, "score": "5-2","matchDate":"13-06-2014"}
, {"matchLineUp": "SWI-FRA"
, "score": "2-5","matchDate":"20-06-2014"}
, {"matchLineUp": "GER-BRA"
, "score": "1-7","matchDate":"08-07-2014"}
]
}'
, $.matches[*].score' WITH WRAPPER) as scores
from dual
• JSON_QUERY is a SQL Function used to retrieve a JSON snippet from a
JSON document - the result is a string that contains valid JSON
– Use WRAPPER to wrap an Array result in an object to return a valid document
18. 18
JSON_TABLE to expose records
from a JSON document relationally
with match_results as
( select '...' json_doc from dual)
select lineUp, score
, to_date(matchDate, 'DD-MM-YYYY') matchDate
from match_results
, json_table( json_doc, '$.matches[*]'
COLUMNS
( lineUp varchar2(20) PATH '$.matchLineUp'
, score varchar2(20) PATH '$.score'
, matchDate varchar2(20) PATH '$.matchDate'
)
)
• JSON_TABLE is used in the FROM clause of SQL statements to project
data from a JSON document as a virtual relation view
– JSON_TABLE is very similar to XML_TABLE
19. 19
12.1.0.2 – JSON
What it does not do?
• Support for variable strings as JSON_PATH in JSON-functions
• Deal with JSON in PL/SQL
– JSON_VALUE and JSON_QUERY cannot be used directly from PL/SQL
• Construct JSON documents
– Not supported: conversion from ADT (to XMLType) to JSON type and vice versa
– Not supported: a SQL/JSON syntax similar to SQL/XML for querying together a
JSON document
– Not supported: facilities that inject JSON into RESTful Services implemented through
the PL/SQL Embedded Gateway
Types
OBJECTS,
NESTED TABLE XMLType
20. The PL/JSON library
• This library has been around for several years – and is still pretty much
relevant
• Especially useful
– For composing JSON documents
– And for converting back and forth from and to JSON to XMLType and ADT/UDT
22. In-line PL/SQL Functions and
procedures
• Procedures are also allowed in-line
• In-Line Functions and Procedures can invoke each other
WITH
procedure increment( operand in out number
, incsize in number)
is
begin
operand:= operand + incsize;
end;
FUNCTION inc(value number) RETURN number IS
l_value number(10):= value;
BEGIN
increment(l_value, 100);
RETURN l_value;
end;
SELECT inc(sal)
from emp
23. In-line PL/SQL Functions and
procedures
• In-Line Functions and Procedures can invoke each other
– And themselves (recursively)
WITH
FUNCTION inc(value number)
RETURN number IS
BEGIN
if value < 6000
then
return inc(value+100);
else
return value + 100;
END if;
end;
SELECT inc(sal)
from emp
24. Dynamic (PL/)SQL is allowed
inside inline functions
• EXECUTE IMMEDIATE can be used inside an inline PL/SQL function to
dynamically construct and execute SQL and PL/SQL
WITH
FUNCTION EMP_ENRICHER(operand varchar2) RETURN varchar2 IS
sql_stmt varchar2(500);
job varchar2(500);
BEGIN
sql_stmt := 'SELECT job FROM emp WHERE ename = :param';
EXECUTE IMMEDIATE sql_stmt INTO job USING operand;
RETURN ' has job '||job;
END;
SELECT ename || EMP_ENRICHER(ename)
from emp
Note: do not try this at home!
It is a horrible query!
(looks too much like POST_QUERY for comfort)
In-Line PL/SQL is not an excuse for lousy SQL
25. Combining in-line Views and
PL/SQL Functions & Procedures
WITH
procedure camel(p_string in out varchar2)
is
begin
p_string:= initcap(p_string);
end;
function obfuscate(p_string in varchar2)
return varchar2
is
l_string varchar2(100);
begin
l_string:= translate(upper(p_string), 'AEUIO','OIEUA');
camel(l_string);
return l_string;
end;
my_view as (
select obfuscate('Goedemorgen')
from dual
)
select *
from my_view
26. PL/SQL Functions That Run
Faster in SQL
• As of Oracle Database Release 12c, two kinds of PL/SQL functions
might run faster in SQL:
– PL/SQL functions that are defined in the WITH clauses of SQL SELECT
statements, described in Oracle Database SQL Language Reference
– PL/SQL functions that are defined with the "UDF Pragma"
• Pragma UDF means: compile in the ‘SQL way’ as to eliminate SQL
PL/SQL context switch
FUNCTION inc(string VARCHAR2)
RETURN VARCHAR2 IS
PRAGMA UDF;
value number(10):= to_number(string);
BEGIN
if value < 6000
then
return inc(value+100);
else
return to_char(value + 100);
end if;
end;
SQL PLSQL
SQL
plsql plsql
28. SQL Statement Preprocessor
• A mechanism to allow the text of a SQL statement, submitted from a client
program (e.g. via ODBC or JDBC), to be translated by user-supplied code
before it is submitted to the Oracle Database SQL compiler.
– Positioned to allow 3rd party apps to (better) run against Oracle Database
– Additionally, any other use case where it is useful to intervene between the SQL
statement that the client submits and what is actually executed
– Some associations: VPD policies, Data Redaction, MV query rewrite, Advanced
Query Rewrite, Temporal Validity (time slice)
Application
SQLPre
proce
ssor
SQL
engine SQL
29. SQL Translation – set up
AS SYS:
grant create sql translation profile to oow;
grant alter session to oow;
AS Application Owner:
-- Create a Translation Profile
exec dbms_sql_translator.create_profile('HR_PROFILE');
BEGIN
DBMS_SQL_TRANSLATOR.REGISTER_SQL_TRANSLATION(
profile_name => 'HR_PROFILE',
sql_text => 'select ename, job, hiredate
from emp',
translated_text => 'select initcap(ename) as ename
, job, hiredate
from emp where job<>''MANAGER'' ‘
);
END;
select * FROM USER_SQL_TRANSLATION_PROFILES;
select * from USER_SQL_TRANSLATIONS;
30. SQL Translation – in action
-- to enable the profile (usually in logon trigger)
alter session set sql_translation_profile = HR_PROFILE
-- to pretend we are a foreign application, subject to
-- SQL Translation
alter session set events = '10601 trace name context forever, level
32';
-- execute query that is to be translated
select ename, job, hiredate
from emp
-- results are produced as if the translated text had been submitted
SQLPre
proce
ssor
SQL
engine SQL
31. 32
SQL Translation
• Support for bind parameters
• Support for rewriting PL/SQL calls
• Translation of Error Messages (ORA-xxxx to SYB-yyyy or YOURAPP-zzz)
• Out of the box translators for Sybase, SQL Server and some DB2
• Optionally: register a custom translator package
– PROCEDURE TRANSLATE_SQL( sql_text IN CLOB, translated_text OUT CLOB);
33. 35
• Maximum length for VARCHAR2 is
now 32KB (up from 4KB)
• Invisible Columns
• One unified audit trail
• PL/SQL DBMS_UTILITY.EXPAND_SQL_TEXT can be used to uncover the real
SQL executed for a given query
– Expressed only in the underlying base tables, including VPD policies
• Export View as Table with Data Pump – fine grained projection of columns
and rows that will be part of the Dump and subsequent Import
• Creation of multiple indexes on same set of columns is allowed
– Although only one can be live at any one time
• Cross PDB queries
34. API for inspecting the PL/SQL
callstack
• New PL/SQL Package UTL_CALL_STACK provides API for inspecting the
PL/SQL Callstack
– Complements the DBMS_ UTILITY.FORMAT_CALL_STACK that returns a pretty
print human readable overview of the callstack
procedure tell_on_call_stack
is
l_prg_uqn UTL_CALL_STACK.UNIT_QUALIFIED_NAME;
begin
dbms_output.put_line('==== TELL ON CALLSTACK ==== '
||UTL_CALL_STACK.DYNAMIC_DEPTH );
for i in 1..UTL_CALL_STACK.DYNAMIC_DEPTH loop
l_prg_uqn := UTL_CALL_STACK.SUBPROGRAM(i);
dbms_output.put_line( l_prg_uqn(1)
||' line '||UTL_CALL_STACK.UNIT_LINE(i)
||' '
||UTL_Call_Stack.Concatenate_Subprogram
( UTL_Call_Stack.Subprogram(i))
);
end loop;
end tell_on_call_stack;
35. API for inspecting the PL/SQL
callstack
create or replace package body callstack_demo
as
function b( p1 in number, p2 in number) return number is
l number:=1;
begin
tell_on_call_stack;
return l;
end b;
procedure a ( p1 in number, p2 out number) is
begin
tell_on_call_stack;
for i in 1..p1 loop p2:= b(i, p1); end loop;
end a;
function c( p_a in number) return number is
l number;
begin
tell_on_call_stack;
a(p_a, l);
return l;
end c;
end callstack_demo;
36. UTL_CALL_STACK
• Functions for retrieving
– BACKTRACE:
• DEPTH, LINE and UNIT
– ERROR:
• DEPTH, MSG and NUMBER
– OWNER, SUBPROGRAM,
UNIT_LINE
– LEXICAL DEPTH (NESTING LEVEL)
37. DEFAULT
• Default applied (also) when NULL was explicitly specified
• Default based on Sequence
• Identity Column that is automatically assigned generated sequence
number value
• Meta Data Only Defaults
– Data applies to potentially many records but hard takes up any space – only some
meta-data are required to declaratively describe the data
alter table emp
modify (sal number(10,2)
DEFAULT ON NULL 1000
)
alter table emp
modify (empno number(5) NOT NULL
DEFAULT ON NULL EMPNO_SEQ.NEXTVAL
)
38. The Top-3 Earning Employees
• What can you say about the result of this query with respect to the
question: "Who are our top three earning employees?"
A. Correct Answer
B. Sometimes correct
C. Correct if there are never duplicate
salaries
D. Not Correct
40. TOP-N Queries in 12c
• Last part of a query to be evaluated – to fetch only selected rows from
the result set:
– To select the next set of rows (pagination):
select *
from emp
order
by sal desc
FETCH FIRST 3 ROWS ONLY;
select *
from emp
order
by sal desc
OFFSET 3 FETCH NEXT 4 ROWS ONLY;
41. Pagination is just a convenient
syntax…
• OFFSET and FETCH NEXT are replaced by optimizer with Analytic
Functions such as ROW_NUMBER()
c##tkyte%CDB1> select /*+ first_rows(5) */ owner, object_name, object_id
2 from t
3 order by owner, object_name
4 OFFSET 5 ROWS FETCH NEXT 5 ROWS ONLY;
…
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 5 | 1450 | 7 (0)| 00:00:01 |
|* 1 | VIEW | | 5 | 1450 | 7 (0)| 00:00:01 |
|* 2 | WINDOW NOSORT STOPKEY | | 5 | 180 | 7 (0)| 00:00:01 |
| 3 | TABLE ACCESS BY INDEX ROWID| T | 87310 | 3069K| 7 (0)| 00:00:01 |
| 4 | INDEX FULL SCAN | T_IDX | 5 | | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("from$_subquery$_003"."rowlimit_$$_rownumber"<=CASE WHEN (5>=0)
THEN 5 ELSE 0 END +5 AND "from$_subquery$_003"."rowlimit_$$_rownumber">5)
2 - filter(ROW_NUMBER() OVER ( ORDER BY "OWNER","OBJECT_NAME")<=CASE WHEN
(5>=0) THEN 5 ELSE 0 END +5)
42. BOTTOM-N QUERY in 12c
• Return only the last three rows in the ordered result set (in the proper
order)
– or:
select *
from emp
order
by sal desc
OFFSET ((select count(*) from emp)-3) ROWS
FETCH NEXT 3 ROWS ONLY
select *
from ( select *
from emp
order
by sal asc
FETCH FIRST 3 ROWS ONLY
)
order
by sal desc;
43. TOP-n% querying
• To query for a percentage of the result set (rather than an absolute
number of rows)
• And the next batch
select *
from emp
order
by sal desc
FETCH FIRST 30 PERCENT ROWS ONLY;
select *
from emp
order
by sal desc
OFFSET (0.3*(select count(*) from emp)) ROWS
FETCH NEXT (0.3*(select count(*) from emp)) ROWS
ONLY;
44. Apply for joining
• APPLY is used to join with a Collection
• The function employees_in_department
returns a collection (TABLE OF
VARCHAR2 in this case)
• The function takes the DEPTNO value
from the DEPT records as input
• Only when the returned collection is not
empty will the DEPT record be produced
by this join
• Use OUTER APPLY to get a result row
even for an empty collection
SELECT *
FROM DEPT d
CROSS APPLY
employees_in_department(deptno) staff
DDDD
45. OUTER Apply for joining
• APPLY is used to join with a Collection
• With OUTER APPLY, each DEPT record is
produced at least once
– depending on the collection
returned by the function for
the DEPT record, multiple
joins may be produced
SELECT *
FROM DEPT d
OUTER APPLY
employees_in_department(deptno) staff
47. Data Redaction
• At runtime, you can optionally have the query results modified to
reset/scramble/randomize sensitive data
– Through ‘data redaction’ policies associated with tables and view and applied at
query time
• Because the data is masked in real-time, Data Redaction is well suited to
environments in which data is constantly changing.
• You can create the Data Redaction policies in one central location and
easily manage them from there.
SQL
engine SQL
POLICY
POLICY
RESULTS
48. My first Data redaction policy
• Access to DBMS_REDACT package
• Create Data Redaction Policy for SAL column in EMP table – hide salaries
from view
• Find that querying EMP has changed forever…
– Note: the expression can be used to dynamically decide whether or not to apply the policy
grant execute on dbms_redact to scott;
BEGIN
DBMS_REDACT.ADD_POLICY(
object_schema => 'scott',
object_name => 'emp',
column_name => 'sal',
policy_name => 'hide_salary',
function_type => DBMS_REDACT.FULL,
expression => '1=1' );
END;
49. Querying EMP with DATA
REDACTION in place
• Note: drop Redaction Policy
DBMS_REDACT.DROP_POLICY
( object_schema => 'scott'
, object_name => 'emp'
, policy_name => 'hide_salary'
);
53. White List
• A white list of allowed invokers can be defined for a PL/SQL unit
– supports the robust implementation of a module, consisting of a main unit and
helper units, by allowing the helper units to be inaccessible from anywhere
except the unit they are intended to help.
54. accessible by clause
package Helper authid Definer accessible by (Good_Guy, Bad_Guy)
is
procedure p;
end Helper;
package body Good_Guy is
procedure p is
begin
Helper.p();
...
end p;
end Good_Guy;
package body Bad_Guy is
procedure p is
begin
Helper.p();
...
end p;
end Bad_Guy;
PLS-00904: insufficient privilege to access object HELPER
_______
55. View with invoker’s rights
• As of Oracle Database Release 12c, a view can be either
– BEQUEATH DEFINER (the default), which behaves like a Definer’s Rights unit
(functions in the view are executed using the view owner’s rights)
– or BEQUEATH CURRENT_USER, which behaves somewhat like an invoker’s rights
unit (functions in the view are executed using the current user’s rights)
create or replace view managers
( name, sal, deptno, experience)
BEQUEATH CURRENT_USER
as
select ename, sal, deptno, some_function(hiredate)
from emp
where job = ‘MANAGER’
56. 61
(More) Security related
features
• Attach database roles to the program units functions, procedures,
packages, and types.
– The role then becomes enabled during execution of the program unit (but not during
compilation of the program unit).
– This feature enables you to temporarily escalate privileges in the PL/SQL code
without granting the role directly to the user. The benefit of this feature is that it
increases security for applications and helps to enforce the principle of least
privilege.
• An Invoker's Rights Function Can Be Result-Cached
• READ privilege to allow SELECT but no SELECT FOR UPDATE
• A new built-in namespace, SYS_SESSION_ROLES, allows you to
determine if a specified role is enabled for the querying user
GRANT clerk_admin TO procedure scott.process_salaries;
57. SYS_SESSION_ROLES
• A new built-in namespace, SYS_SESSION_ROLES, allows you to
determine if a specified role is enabled for the querying user.
• The following example determines if the DBA role is enabled for user OE:
CONNECT OE/password
SELECT SYS_CONTEXT('SYS_SESSION_ROLES', 'DBA')
FROM DUAL;
SYS_CONTEXT('SYS_SESSION_ROLES','DBA')
--------
FALSE
58. Audit the real privilege
requirements of an application
• Objective: restrict privileges for a role to those that are really required for
using an application
• A privilege capture displays privilege usage for a database according to a
specified condition
– such as privileges to run an application module
– or privileges used in a given user session.
• When a user performs an action and you want to monitor the privileges
that are used for this action, you can create a privilege capture to capture
privilege usage.
• Afterwards, you can view a report that describes the behavior the privilege
capture defined.
– The privilege capture includes both system privileges and object privileges.
• Part of Database Vault option
59. Inherit or not in Invoker rights
program units
• When a user runs an invoker's rights procedure (or program unit), it runs
with the privileges of the invoking user.
• As the procedure runs, the procedure’s owner temporarily has access to
the invoking user's privileges.
• [If the procedure owner has fewer privileges than an invoking user,] the
procedure owner could use the invoking user’s privileges to perform
operations
procedure
owner
Invoker’s rights
invoker
procedure Special_
Table
Tap_
Table
60. 65
New privilege:
INHERIT PRIVILEGES
• To be used for owners of (schemas with) Invoker’s Rights PL/SQL
program units
• Invoker’s rights procedure executions only can run with the privileges of
the invoker if the procedure’s owner has the INHERIT PRIVILEGES
privilege on the invoker (do not stealthily use invoker’s privileges as
owner)
– or if the procedure’s owner has the INHERIT ANY PRIVILEGES privilege
GRANT INHERIT PRIVILEGES
ON USER invoking_user
TO procedure_owner
REVOKE INHERIT PRIVILEGES ON invoking_user
FROM procedure_owner;
62. 67
Raw Data Refinement
based on pattern matching
14,0
16,1
14,1
16,1
16,0
13,1
14,0
16,0
13,1
13,0
14,1
16,0
14,1
13,0
14,1
16,0
13,1
14,0
Processing
• Information
• Conclusion
• Alert
• Recommendation
• Action
63. Who is afraid of Red, Yellow
and blue
• Table Events
– Column Seq number(5)
– Column Payload varchar2(200)
64. Solution using Lead
• With LEAD it is easy to compare a row with its successor(s)
– As long as the pattern is fixed, LEAD will suffice
with look_ahead_events as
( SELECT e.*
, lead(payload) over (order by seq) next_color
, lead(payload,2) over (order by seq) second_next_color
FROM events e
)
select seq
from look_ahead_events
where payload ='red'
and next_color ='yellow'
and second_next_color='blue'
65. Find the pattern red, yellow and
blue
• Using the new 12c Match Recognize operator for finding patterns in
relational data
SELECT *
FROM events
MATCH_RECOGNIZE
(
ORDER BY seq
MEASURES RED.seq AS redseq
, MATCH_NUMBER() AS match_num
ALL ROWS PER MATCH
PATTERN (RED YELLOW BLUE)
DEFINE
RED AS RED.payload ='red',
YELLOW AS YELLOW.payload ='yellow',
BLUE AS BLUE.payload ='blue'
) MR
ORDER
BY MR.redseq
, MR.seq;
66. Match_recognize for finding
patterns in relational data
• The expression MATCH_RECOGNIZE provides native SQL support to
find patterns in sequences of rows
• Match_recognize returns Measures for selected (pattern matched) rows
– Similar to MODEL clause
• Match Conditions are expressed in columns from the Table Source,
aggregate functions and pattern functions FIRST, PREV, NEXT, LAST
• Patterns are regular expressions using match conditions to express a
special sequence of rows satisfying the conditions
Table
Source
&
Where
Match_
Recognize
Process
and Filter
Select &
Order By
67. Did we ever consecutively hire
three employees in the same job?
• Find a string of three subsequent hires where each hire has the same job
• Order by hiredate, pattern is two records that each have the same job as
their predecessor
SELECT *
FROM EMP
MATCH_RECOGNIZE
(
ORDER BY hiredate
MEASURES SAME_JOB.hiredate AS hireday
, MATCH_NUMBER() AS match_num
ALL ROWS PER MATCH
PATTERN (SAME_JOB{3})
DEFINE
SAME_JOB AS SAME_JOB.job = FIRST(SAME_JOB.job)
) MR
68. Pattern clause is a regular
expression
• Supported operators for the pattern clause include:
– * for 0 or more iterations
– + for 1 or more iterations
– ? for 0 or 1 iterations
– { n } for exactly n iterations (n > 0)
– { n, } for n or more iterations (n >= 0)
– { n, m } for between n and m (inclusive) iterations (0 <= n <= m, 0 < m)
– { , m } for between 0 and m (inclusive) iterations (m > 0)
– reluctant qualifiers - *?, +?, ??, {n}?, {n,}?, { n, m }?, {,m}?
– | for alternation (OR)
– grouping using () parentheses
– exclusion using {- and -}
– empty pattern using ()
– ^ and $ for start and end of a partition
69. Two steps forward… and one
step back
• Common expression … common pattern as well?
• Investigate salary evolution
– using Flashback Query and Match_Recognize
match_recognize (
partition by empno
order by the_time
MEASURES MATCH_NUMBER() AS match_num
, classifier() as match_role
ALL ROWS PER MATCH
PATTERN (STRT UP UP DOWN )
DEFINE UP as UP.sal > PREV(UP.SAL)
, DOWN as DOWN.SAL < PREV(DOWN.SAL)
) MR
70. Two steps forward… and one
step backwith sal_history as
( select empno
, ename
, sal
, nvl(versions_starttime, versions_endtime) the_time
from emp versions between timestamp minvalue and maxvalue
)
select *
from sal_history
match_recognize (
partition by empno
order by the_time
MEASURES MATCH_NUMBER() AS match_num
, classifier() as match_role
ALL ROWS PER MATCH
PATTERN (STRT UP UP DOWN )
DEFINE UP as UP.sal > PREV(UP.SAL)
, DOWN as DOWN.SAL < PREV(DOWN.SAL)
) MR
71. Find the longest sequence of
related observations
• Records are ordered
• Each record is qualified: assigned
to a certain category
• Examples:
– Voting records
– Ball possession in football
– Days with or without rain
– Passing vehicles (make and model
or category)
– DNA records
• The challenge: find the longest string
of consecutive observations in
the same category
72. Find the longest sequence of
related observations
SELECT section_category
, section_start
FROM observations
MATCH_RECOGNIZE
(
ORDER BY seq
MEASURES SAME_CATEGORY.category as section_category
, FIRST(SAME_CATEGORY.seq) as section_start
ONE ROW PER MATCH
PATTERN (SAME_CATEGORY* DIFFERENT_CATEGORY) -- as many times as possible
DEFINE
SAME_CATEGORY AS SAME_CATEGORY.category = FIRST(SAME_CATEGORY.category)
, DIFFERENT_CATEGORY AS DIFFERENT_CATEGORY.category !=
NEXT(DIFFERENT_CATEGORY.category)
) MR
order
by rows_in_section desc
)
73. Suppose we allow a single
interruption of a sequence
• One record with a different category
will not end the sequence – it might after
all be a fluke or an incident
• Rewrite the pattern match
to also accept one entry
with a different category
ONE ROW PER MATCH
AFTER MATCH SKIP TO NEXT ROW
-- a next row in the current match may be start of a next string
PATTERN (SAME_CATEGORY* DIFFERENT_CATEGORY{0,1} SAME_CATEGORY* )
DEFINE
SAME_CATEGORY AS SAME_CATEGORY.category = FIRST(SAME_CATEGORY.category)
, DIFFERENT_CATEGORY AS DIFFERENT_CATEGORY.category !=
SAME_CATEGORY.category
74. Find sequence (with one accepted
interruption) from all records
SELECT substr(section_category,1,1) cat
, section_start
, seq
FROM observations
MATCH_RECOGNIZE
( ORDER BY seq
MEASURES SAME_CATEGORY.category as section_category
, FIRST(SAME_CATEGORY.seq) as section_start
, seq as seq
ONE ROW PER MATCH
AFTER MATCH SKIP TO NEXT ROW -- a next row in the current match may be
-- start of a next string
PATTERN (SAME_CATEGORY* DIFFERENT_CATEGORY{0,1} SAME_CATEGORY* )
DEFINE
SAME_CATEGORY AS SAME_CATEGORY.category = FIRST(SAME_CATEGORY.category)
, DIFFERENT_CATEGORY AS DIFFERENT_CATEGORY.category !=
SAME_CATEGORY.category
) MR
order
by rows_in_section desc