ISO SQL:2016 introduced Row Pattern Matching: a feature to apply (limited) regular expressions on table rows and perform analysis on each match. As of writing, this feature is only supported by the Oracle Database 12c.
23. Tolerating Gaps
Example: Comments (new vs. read)
Show comments which…
‣ …are new or
‣ …between two new ones
(show the comment instead of a “load more” button)
Two approaches:
‣ Start-of-group tagging
‣ Row pattern matching
Comments
new commentread comment
52. Top-N Per Group
Example: List 3 most recent comments per topic
Three approaches:
‣ lateral sub-query (requires specific indexing)
‣ Row pattern matching (requires 12c)
‣ row_number() window function
Time
Topic 3
Topic 2
Topic 1
60. Time Intervals (non-overlapping)
Example: Bookings are stored as [begin; end[ intervals
Two problems:
‣ Find free time-slots
‣ Close free time-slots
Time
Busy Busy Busy
86. Endless possibilitesRow Pattern Matching
GROUP BY
➡ ONE ROW PER MATCH
OVER ()
➡ ALL ROWS PER MATCH, FINAL, RUNNING
HAVING, WHERE
➡ PATTERN (unmatched, suppressed {- … -})
Mixing GROUP BY and OVER()
➡ ALL ROWS PER MATCH + all-but-one rows suppressed
Data-driven match length
➡ SUM, COUNT, … in DEFINE
Duplicating rows (to some extend)
➡ ALL ROWS PER MATCH + AFTER MATCH SKIP TO …
87. What if I told you,
you can also
find patterns?
88. Not/Barely covered in this presentationRow Pattern Matching
‣ Reluctant (non-greedy) matching
‣ SHOW/OMIT EMPTY MATCHES
WITH UNMATCHED ROWS
‣ SUBSET (define pattern-vars for use in MEASURES,
DEFINE and AFTER MATCH SKIP TO)
‣ PREV, NEXT, FIRST, LAST (with some nesting!)
‣MATCH_NUMBER()
89. Obstacles and Issues (as of 12r1)Row Pattern Matching
‣ JDBC !!!!!
Tokens ?, {, } have special meaning in JDBC.
You have to escape them using { ... }
https://docs.oracle.com/database/121/JJDBC/apxref.htm#CHECHCJH
‣ ORA-62513: Quantified subpatterns that can have empty matches are not yet supported
PATTERN ( x (a* b*)+ y )
‣ ORA-62512: This aggregate is not yet supported in MATCH_RECOGNIZE clause.
(only COUNT, SUM, AVG, MIN, and MAX)
90. About @MarkusWinand
‣Training for Developers
‣ SQL Performance (Indexing)
‣ Modern SQL
‣ On-Site or Online
‣SQL Tuning
‣ Index-Redesign
‣ Query Improvements
‣ On-Site or Online
http://winand.at/