CSE 344: Section 8
NoSQL, SQL++
December 2nd, 2021
Administrivia
Semi-Structured Data
Semi-Structured Data
A data format which does not obey the typical formal structure of data (such as relational data) but also has ‘tags’ to enforce hierarchies (tree structured)
Why Semi-Structured?
JavaScript Object Notation
Why JSON?
Built on two structures:
Takeaways:
AsterixDB
SQL++
SQL++
Why design SQL++ like SQL? Why not some other query language?
Output with Structure
If we wanted the name and a list of orders:
SELECT P.name, P.orders
FROM Person P;
{“name”: “Dan”, “orders”: [ { “date”: 1997,
“product”: “Furby”} ]}
{“name”: “Alvin”, “orders”: [ {date”: 2000,
“product”: “Furby”},
{date”: 2012,
“product”: “Magic8”} ]}
(Implicit) Unnesting
If we wanted the name and each product separately:
SELECT P.name, O.product as product
FROM Person P, P.orders O;
{“name”: “Dan”, “product”: “Furby”}
{“name”: “Alvin”, “product”: “Furby”}
{“name”: “Alvin”, “product”: “Magic8”}
Nesting (Method #1)
If we wanted the name and all products as a list:
SELECT P.name, products
FROM Person P
LET products = (SELECT O.product FROM P.order O);
{“name”: “Dan”, “products”: [“Furby”] }
{“name”: “Alvin”, “products”: [“Furby”, “Magic8”]}
Nesting (Method #2)
If we wanted the name and all products as a list:
SELECT P.name, (SELECT O.product FROM P.order O)� AS products
FROM Person P;
{“name”: “Dan”, “products”: [“Furby”] }
{“name”: “Alvin”, “products”: [“Furby”, “Magic8”]}
Compare!
If we want name and all products as a list:
SELECT P.name, products
FROM Person P
LET products = (SELECT O.product
FROM P.order O);
{“name”: “Dan”, “product”: [“Furby”] }
{“name”: “Alvin”, “product”: [“Furby”, “Magic8”]}
If we want name and each product separately:
SELECT P.name, O.product as product
FROM Person P, P.orders O;
{“name”: “Dan”, “product”: “Furby”}
{“name”: “Alvin”, “product”: “Furby”}
{“name”: “Alvin”, “product”: “Magic8”}
Cases
SELECT y.name, z.`-continent` as continent
FROM geo.world x, x.mondial.country y,
case when is_array(y.encompassed)
then y.encompassed
else [y.encompassed] end z
ORDER BY y.name;
“Find the encompassing continents for each country”
Useful Syntax for HW7
Mondial SQL++ Example
SELECT y.`-car_code` as code,
y.name as name
FROM geo.world x, x.mondial.country y
ORDER BY y.name;
Worksheet Time!