Presentation is loading. Please wait.

Presentation is loading. Please wait.

The New and Improved SQL:2016 Standard

Similar presentations


Presentation on theme: "The New and Improved SQL:2016 Standard"— Presentation transcript:

1 The New and Improved SQL:2016 Standard
University of Cyprus EPL646: Advanced Topics in Databases  The New and Improved SQL:2016 Standard The New and Improved SQL:2016 Standard. Jan Michels, Keith Hare, Krishna Kulkarni, Calisto Zuzarte, Zhen Hua Liu, Beda Hammerschmidt, and Fred Zemke The New and Improved SQL: 2016 Standard. SIGMOD Rec. 47, 2 (December 2018), DOI: By: Nectarios Efstathiou

2 Presentation Outline (Indicative)
Introduction of SQL:2016 Support for Java Script Object Notation (JSON) data Polymorphic Table Functions (PTF) Row Pattern Recognition (RPR) Additional Functionality / Future Work Conclusions Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

3 Presentation Outline (Indicative)
Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

4 Support for JSON Data – Querying
Our examples will use the table T as shown to the next slide JSON Functionalities in SQL:2016 IS JSON predicate SQL/JSON path expressions JSON_EXISTS predicate JSON_VALUE function JSON_QUERY function JSON_TABLE function Structural-inspection methods SQL/JSON constructor functions Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

5 ID JCOL 111 { "Name" : "John Smith", "address" : {
"streetAddress": "21 2nd Street", "city": "New York", "state" : "NY", "postalCode" : }, "phoneNumber" : [ { "type" : "home", "number" : " " }, { "type" : "fax", "number" : " " } ] } 222 { "Name" : "Peter Walker", "streetAddress": "111 Main Street", "city": "San Jose", "state" : "CA", "postalCode" : }, { "type" : "home", "number" : " " }, { "type" : "office", "number" : " " } ] } 333 { "Name" : "James Lee" } Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

6 Support for JSON Data – IS JSON
The IS JSON predicate is used to verify that an SQL value contains a syntactically correct JSON value. CREATE TABLE T ( Id INTEGER PRIMARY KEY, Jcol CHARACTER VARYING ( 5000 ) CHECK ( Jcol IS JSON ) ) SELECT * FROM T WHERE Jcol IS JSON Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

7 Support for JSON Data – JSON_EXISTS
JSON_EXISTS is used to determine if an SQL/JSON path expression has any matches in a JSON document. SELECT Id FROM T WHERE JSON_EXISTS ( Jcol, 'strict $.address' ) strict: more strict, returning error if failed lax: not so strict, returning null if failed Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

8 Support for JSON Data – JSON_VALUE
The JSON_VALUE function is used to extract a scalar value from a given JSON value. SELECT Id, JSON_VALUE ( Jcol, 'lax $.phoneNumber[0].number' ) AS Firstphone FROM T SELECT JSON_VALUE ( Jcol, 'lax $.Name' ) AS Name FROM T Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

9 Support for JSON Data – JSON_VALUE
SELECT Id, JSON_VALUE ( Jcol, 'lax $.phoneNumber[0].number') AS Firstphone FROM T ID FIRSTNAME 111 222 333 SELECT Id, JSON_VALUE ( Jcol, 'lax $.phoneNumber ? == "fax" ).number') AS Fax FROM T ID FAX 111 222 333 Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

10 Support for JSON Data – JSON_QUERY
ID ADDRESS 111 { "streetAddress": "21 2nd Street", "city": "New York", "state" : "NY", "postalCode" : } 222 { "streetAddress": "111 Main Street", "city": "San Jose", "state" : "CA", "postalCode" : } 333 The JSON_QUERY function, is used to extract a fragment (i.e., an SQL/JSON object, array, or scalar, possibly wrapped in an SQL/JSON array, if the user specifies this) from a given JSON value. SELECT Id, JSON_QUERY ( Jcol, 'lax $.address' ) AS Address FROM T Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

11 JSON_VALUE vs JSON_QUERY
JSON_VALUE can only extract scalars from a JSON JSON_QUERY can extract a fragment from a JSON Object Array Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

12 Support for JSON Data – SQL/JSON CF
SQL/JSON constructor functions Use values of SQL types and produce JSON (either JSON objects or JSON arrays) The functions are: JSON_OBJECT (scalar functions) JSON_ARRAY (scalar functions) JSON_OBJECTAGG (aggregate functions) JSON_ARRAYAGG (aggregate functions) Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

13 Support for JSON Data – SQL/JSON CF
SELECT JSON_OBJECT (KEY 'department' VALUE D.Name, KEY 'employees’ VALUE JSON_ARRAYAGG ( JSON_OBJECT ( KEY 'employee’ VALUE E.Name, KEY 'salary’ VALUE E.Salary ) ORDER BY E.Salary ASC ) ) AS Department FROM Departments D, Employees E WHERE D.Dept_id = E.Dept_id GROUP BY D.Name DEPARTMENT { "department" : "Sales", "employees" : [ { "employee" : "James", "salary" : 7000 }, { "employee" : "Rachel", "salary" : 9000 }, { "employee" : "Logan", "salary" : } ] } Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

14 Polymorphic Table Functions
Introduction The SQL standard prior to the 2016 release had only support for monomorphic table functions i.e. Monomorphic The definition of both the output table and the set of input parameters were fixed at function creation time. Polymorphic Have generic table input parameter(s) whose row type(s) may be unknown at creation time. Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

15 Polymorphic Table Functions
When a PTF is invoked in a query, the RDBMS and the PTF interact through a family of one to four SQL-invoked procedures. There are four PTF component procedures: describe (Optional for statically parameters) Called once during compilation of the query that invoke the PTF. The primary task is to determine the row type of the output table. Receives a description of the input tables, their ordering (if any) and, any scalar input arguments that are compile-time constants. Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

16 Polymorphic Table Functions
start (Optional) Called at the start of the execution of the PTF to allocate any resources that the RDBMS does not provide. fulfill (Mandatory) Called during the execution to deliver the output table by “piping” rows to the RDBMS. Reads the contents of the input table(s) and generates the output table. finish (Optional) Called at the end of the execution to deallocate any resources allocated by the PTF on start component procedure. Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

17 PTF – Examples - CSV reader table
Consider a file with a comma-separated list of values (CSV file) The first line of the file contains a list of column names, and subsequent lines of the file contain the actual data. We will create a function ‘CSVreader’ to read a CSV file and provide its data as a table in the FROM clause of a query. Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

18 PTF – Examples - CSV reader table
CREATE FUNCTION CSVreader ( File VARCHAR(1000), Floats DESCRIPTOR DEFAULT NULL, Dates DESCRIPTOR DEFAULT NULL ) RETURNS TABLE NOT DETERMINISTIC CONTAINS SQL PRIVATE DATA ( FileHandle INTEGER ) DESCRIBE WITH PROCEDURE CSVreader_describe START WITH PROCEDURE CSVreader_start FULFILL WITH PROCEDURE CSVreader_fulfill FINISH WITH PROCEDURE CSVreader_finish Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

19 PTF – Examples - CSV reader table
SELECT * FROM TABLE ( CSVreader ( File => 'abc.csv’, Floats => DESCRIPTOR ( "principal", "interest" ) Dates => DESCRIPTOR ( "due_date" ) ) ) AS S Dynamic Variables Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

20 Row Pattern Recognition - Introduction
Row Pattern Recognition (RPR) can be used to search an ordered partition of rows for matches to a regular expression. RPR supports: FROM Clause WINDOW Clause FROM & WINDOW clause uses much the same syntax and semantics. Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

21 Row Pattern Recognition - FROM
RPR in the FROM clause uses the keyword MATCH_RECOGNIZE as a postfix operator on a table, called the row pattern input table. Operates on the row pattern input table and produces the row pattern output table Describing the matches to the pattern that are discovered in the row pattern input table. There are two principal variants of MATCH_RECOGNIZE: ONE ROW PER MATCH: returns a single summary row for each match of the pattern (the default). ALL ROWS PER MATCH: returns one row for each row of each match. Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

22 Row Pattern Recognition - RPR
Ticker Table Symbol Tradeday Price Mapped to XYZ 50 60 A 49 B 40 35 45 C Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

23 Row Pattern Recognition - RPR
SELECT M.Symbol,M.Matchno,M.Startp,M.Bottomp,M.Endp, M.Avgp FROM Ticker MATCH_RECOGNIZE ( PARTITION BY Symbol ORDER BY Tradeday MEASURES MATCH_NUMBER() AS Matchno, A.Price AS Startp, LAST (B.Price) AS Bottomp, LAST (C.Price) AS Endp, AVG (U.Price) AS Avgp ONE ROW PER MATCH AFTER MATCH SKIP PAST LAST ROW PATTERN (A B+ C+) SUBSET U = (A, B, C) DEFINE /* A defaults to True, matches any row */ B AS B.Price < PREV (B.Price), C AS C.Price > PREV (C.Price) ) AS M Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

24 Row Pattern Recognition - RPR
Row Pattern Output Table Symbol Matchno Startp Bottomp Endp Avgp XYZ 1 60 35 45 45.8 Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

25 Additional Functionality
Default values and named arguments for SQL-invoked functions Invoking procedure can provide default value and thus be optional Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

26 Additional Functionality
We will used an example by creating a function (Total_comp) that computes the total compensation as the sum of the base salary and the bonus (where the bonus by default is 1000) CREATE FUNCTION Total_comp ( Base_sal DECIMAL(7,2), Bonus DECIMAL(7,2) DEFAULT ) RETURNS DECIMAL(8,2) LANGUAGE SQL RETURN Base_sal + Bonus; Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

27 Additional Functionality
SQL:2011 allowed for a parameter of an SQL-invoked procedure to have a default value as optional. SQL:2016 enhancement when invoking a procedure using named arguments: Passing all arguments by position: Total_comp( , ) Passing all non-defaulted arguments by position: Total_comp( ) Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

28 Additional Functionality
Passing all arguments by name (in this case the order of the arguments does not need to match the order of the parameters in the function signature): Total_comp(Bonus=> , Base_sal=> ) Passing all non-defaulted arguments by name: Total_comp(Base_sal=> ) Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

29 Additional Functionality - built-in
SQL:2016 adds support for additional scalar mathematical built-in functions including trigonometric and logarithm functions. sine cosine tangent general logarithm function common logarithm function (with the base fixed at 10). LISTAGG is a new aggregate function that allows concatenating character strings over a group of rows. Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

30 Additional Functionality - Future Work
The SQL standards committee is currently working on additional expansions in three areas support for multidimensional arrays, support for streaming data support for property graphs Student Research Presentations, Advanced Topics in Databases, Dept. of Computer Science University of Cyprus

31 Thank you! Q & A


Download ppt "The New and Improved SQL:2016 Standard"

Similar presentations


Ads by Google