diff --git a/pages/pgql-2.0-spec.md b/pages/pgql-2.0-spec.md index b9aa3b43..1b5ceb09 100644 --- a/pages/pgql-2.0-spec.md +++ b/pages/pgql-2.0-spec.md @@ -1409,31 +1409,31 @@ For example:
-SELECT  *  
+SELECT *  
 FROM GRAPH_TABLE (financial_transactions 
   MATCH(n) 
-  COLUMNS(VERTEX_ID(n) as n_id, n.*)) 
+  COLUMNS(n.*)) 
 ORDER BY "number", "name"
 
-SELECT ID(n), n.*
+SELECT n.*
 FROM MATCH (n) ON financial_transactions
 ORDER BY "number", "name"
 
``` -+-----------------------------+ -| label(n) | number | name | -+-----------------------------+ -| Account | 1001 | | -| Account | 2090 | | -| Account | 8021 | | -| Account | 10039 | | -| Person | | Camille | -| Person | | Liam | -| Person | | Nikita | -| Company | | Oracle | -+-----------------------------+ ++------------------+ +| number | name | ++------------------+ +| 1001 | | +| 2090 | | +| 8021 | | +| 10039 | | +| | Camille | +| | Liam | +| | Nikita | +| | Oracle | ++------------------+ ``` Label expressions are taken into account such that only properties are selected that belong to the specified vertex or @@ -1448,27 +1448,27 @@ edge labels: SELECT * FROM GRAPH_TABLE (financial_transactions MATCH(n IS Person) - COLUMNS(VERTEX_ID(n) as n_id, n.*)) -ORDER BY "name" + COLUMNS(n.*)) +ORDER BY "name"
-SELECT ID(n), n.*
+SELECT n.*
 FROM MATCH (n:Person) ON financial_transactions
 ORDER BY "name"
 
``` -+--------------------+ -| label(n) | name | -+--------------------+ -| Person | Camille | -| Person | Liam | -| Person | Nikita | -+--------------------+ ++---------+ +| name | ++---------+ +| Camille | +| Liam | +| Nikita | ++---------+ ``` A `PREFIX` can be specified to avoid duplicate column names in case all properties of multiple vertex or edge variables are selected. -Note: `PREFIX` cannot be used withing the GRAPH_TABLE operator. +Note: `PREFIX` cannot be used within the GRAPH_TABLE operator. For example: @@ -1595,7 +1595,7 @@ PGQL itself does not (yet) provide syntax for specifying a default graph, but Ja - Oracle's in-memory analytics engine PGX has the API `PgxGraph.queryPgql("SELECT ...")` such that the default graph corresponds to `PgxGraph.getName()` such that `ON` clauses can be omitted from queries. - Oracle's PGQL-on-RDBMS provides the API `PgqlConnection.setGraph("myGraph")` for setting the default graph such that the `ON` clauses can be omitted from queries. -Note: graph names has to be explicitly provided for the GRAPH_TABLE operator. +Note: graph names have to be explicitly provided for the GRAPH_TABLE operator. If a default graph is provided then the `ON` clause can be omitted: ```sql @@ -1730,7 +1730,7 @@ ColonOrIsKeyword ::= ':' Colons and `IS` keywords can be used interchangeably. -Note: With GRAPH_TABLE operator only IS is allowed. +Note: With `GRAPH_TABLE` operator only `IS` is allowed. Take the following example: @@ -2537,27 +2537,14 @@ For example: {% include image.html file="example_graphs/financial_transactions.png" %} -
- - -
-SELECT * 
-FROM GRAPH_TABLE(financial_transactions 
-  MATCH (a IS Account) (-[e IS transaction]-> COST e.amount)* (b IS Account) 
-  KEEP ANY CHEAPEST 
-  WHERE a.number = 10039 AND b.number = 2090 
-  COLUMNS(COUNT(EDGE_ID(e)) AS num_hops, 
-          SUM(e.amount) AS total_amount, 
-          ARRAY_AGG(e.amount) AS amounts_along_path))
-
-
-SELECT COUNT(e) AS num_hops
-     , SUM(e.amount) AS total_amount
-     , ARRAY_AGG(e.amount) AS amounts_along_path
-FROM MATCH ANY CHEAPEST (a:Account) (-[e:transaction]-> COST e.amount)* (b:Account)
-       ON financial_transactions
-WHERE a.number = 10039 AND b.number = 2090
-
+```sql +SELECT COUNT(e) AS num_hops + , SUM(e.amount) AS total_amount + , ARRAY_AGG(e.amount) AS amounts_along_path +FROM MATCH ANY CHEAPEST (a:Account) (-[e:transaction]-> COST e.amount)* (b:Account) + ON financial_transactions +WHERE a.number = 10039 AND b.number = 2090 +``` ``` +----------------------------------------------------+ @@ -2571,27 +2558,14 @@ The following example with `CHEAPEST` contains an any-directed edge pattern (`-[ {% include image.html file="example_graphs/financial_transactions.png" %} -
- - -
-SELECT *
-FROM GRAPH_TABLE(financial_transactions 
-  MATCH (a IS Account) (-[e IS transaction]- COST e.amount)* (b IS Account)
-  KEEP ANY CHEAPEST
-  WHERE a.number = 10039 AND b.number = 2090
-  COLUMNS(COUNT(EDGE_ID(e)) AS num_hops
-     , SUM(e.amount) AS total_amount
-     , ARRAY_AGG(e.amount) AS amounts_along_path))
-
-
-SELECT COUNT(e) AS num_hops
-     , SUM(e.amount) AS total_amount
-     , ARRAY_AGG(e.amount) AS amounts_along_path
-FROM MATCH ANY CHEAPEST (a:Account) (-[e:transaction]- COST e.amount)* (b:Account)
-       ON financial_transactions
-WHERE a.number = 10039 AND b.number = 2090
-
+```sql +SELECT COUNT(e) AS num_hops + , SUM(e.amount) AS total_amount + , ARRAY_AGG(e.amount) AS amounts_along_path +FROM MATCH ANY CHEAPEST (a:Account) (-[e:transaction]- COST e.amount)* (b:Account) + ON financial_transactions +WHERE a.number = 10039 AND b.number = 2090 +``` ``` +----------------------------------------------+ @@ -2608,34 +2582,18 @@ The following example has a `CASE` statement that defines a different cost for d {% include image.html file="example_graphs/financial_transactions.png" %} -
- - -
-SELECT * 
-FROM GRAPH_TABLE(financial_transactions 
-  MATCH (p1 IS Person) (-[e IS owner|transaction]- COST CASE 
-                                                          WHEN e.amount IS NULL THEN 1 
-                                                          ELSE e.amount 
-                                                        END)* (p2 IS Person) 
-  KEEP ANY CHEAPEST 
-  WHERE p1.name = 'Nikita' AND p2.name = 'Liam' 
-  COLUMNS(COUNT(EDGE_ID(e)) AS num_hops , 
-          SUM(e.amount) AS total_amount , 
-          ARRAY_AGG(e.amount) AS amounts_along_path))
-
-
-SELECT COUNT(e) AS num_hops
-     , SUM(e.amount) AS total_amount
-     , ARRAY_AGG(e.amount) AS amounts_along_path
-FROM MATCH ANY CHEAPEST (p1:Person) (-[e:owner|transaction]-
-                                    COST CASE
-                                           WHEN e.amount IS NULL THEN 1
-                                           ELSE e.amount
-                                         END)* (p2:Person)
-     ON financial_transactions
-WHERE p1.name = 'Nikita' AND p2.name = 'Liam'
-
+```sql +SELECT COUNT(e) AS num_hops + , SUM(e.amount) AS total_amount + , ARRAY_AGG(e.amount) AS amounts_along_path +FROM MATCH ANY CHEAPEST (p1:Person) (-[e:owner|transaction]- + COST CASE + WHEN e.amount IS NULL THEN 1 + ELSE e.amount + END)* (p2:Person) + ON financial_transactions +WHERE p1.name = 'Nikita' AND p2.name = 'Liam' +``` ``` +----------------------------------------------+ @@ -2669,29 +2627,15 @@ For example, the following query returns the cheapest 3 paths from account 10039 {% include image.html file="example_graphs/financial_transactions.png" %} -
- - -
-SELECT * 
-FROM GRAPH_TABLE(financial_transactions 
-  MATCH (a IS Account) (-[e IS transaction]-> COST e.amount)* (a) 
-  KEEP CHEAPEST 3 PATHS 
-  WHERE a.number = 10039 
-  COLUMNS(COUNT(EDGE_ID(e)) AS num_hops,
-          SUM(e.amount) AS total_amount , 
-          ARRAY_AGG(e.amount) AS amounts_along_path))
-ORDER BY total_amount
-
-
-SELECT COUNT(e) AS num_hops
-     , SUM(e.amount) AS total_amount
-     , ARRAY_AGG(e.amount) AS amounts_along_path
-FROM MATCH CHEAPEST 3 PATHS (a:Account) (-[e:transaction]-> COST e.amount)* (a)
-       ON financial_transactions
-WHERE a.number = 10039
-ORDER BY total_amount
-
+```sql +SELECT COUNT(e) AS num_hops + , SUM(e.amount) AS total_amount + , ARRAY_AGG(e.amount) AS amounts_along_path +FROM MATCH CHEAPEST 3 PATHS (a:Account) (-[e:transaction]-> COST e.amount)* (a) + ON financial_transactions +WHERE a.number = 10039 +ORDER BY total_amount +``` ``` +------------------------------------------------------------+ @@ -2709,41 +2653,21 @@ while `Account` or `Company` vertices contribute `1` to the total cost. {% include image.html file="example_graphs/financial_transactions.png" %} -
- - -
-SELECT * 
-FROM GRAPH_TABLE(financial_transactions 
-  MATCH (a IS Account) 
-          (-[e]- (n_x) COST CASE WHEN n_x IS LABELED Person THEN 3 ELSE 1 END)* 
-            (c IS Company) 
-  KEEP CHEAPEST 4 PATHS 
-  WHERE a.number = 10039 AND c.name = 'Oracle' 
-  COLUMNS(COUNT(EDGE_ID(e)) AS num_hops , 
-          ARRAY_AGG(CASE 
-                      WHEN n_x IS LABELED Person THEN n_x.name 
-                      WHEN n_x IS LABELED Company THEN n_x.name 
-                      WHEN n_x IS LABELED Account THEN CAST(n_x.number AS STRING) 
-                    END ) AS names_or_numbers , 
-          SUM(CASE WHEN n_x IS LABELED Person THEN 8 ELSE 1 END) AS total_cost)) 
-ORDER BY total_cost
-
-
-SELECT COUNT(e) AS num_hops
-     , ARRAY_AGG( CASE label(n_x)
-                    WHEN 'Person' THEN n_x.name
-                    WHEN 'Company' THEN n_x.name
-                    WHEN 'Account' THEN CAST(n_x.number AS STRING)
-                  END ) AS names_or_numbers
-     , SUM( CASE label(n_x) WHEN 'Person' THEN 8 ELSE 1 END ) AS total_cost
-FROM MATCH CHEAPEST 4 PATHS
+```sql
+SELECT COUNT(e) AS num_hops
+     , ARRAY_AGG( CASE label(n_x)
+                    WHEN 'Person' THEN n_x.name
+                    WHEN 'Company' THEN n_x.name
+                    WHEN 'Account' THEN CAST(n_x.number AS STRING)
+                  END ) AS names_or_numbers
+     , SUM( CASE label(n_x) WHEN 'Person' THEN 8 ELSE 1 END ) AS total_cost
+FROM MATCH CHEAPEST 4 PATHS
       (a:Account)
-        (-[e]- (n_x) COST CASE label(n_x) WHEN 'Person' THEN 3 ELSE 1 END)*
-          (c:Company) ON financial_transactions
-WHERE a.number = 10039 AND c.name = 'Oracle'
-ORDER BY total_cost
-
+ (-[e]- (n_x) COST CASE label(n_x) WHEN 'Person' THEN 3 ELSE 1 END)* + (c:Company) ON financial_transactions +WHERE a.number = 10039 AND c.name = 'Oracle' +ORDER BY total_cost +``` ``` +----------------------------------------------+ @@ -3025,25 +2949,13 @@ An example with `WALK` is: {% include image.html file="example_graphs/financial_transactions.png" %} -
- - -
-SELECT * 
-FROM GRAPH_TABLE(financial_transactions 
-  MATCH (a IS account) (-[e IS transaction]-> COST e.amount)* (a) 
-  KEEP CHEAPEST 4 WALK 
-  WHERE a.number = 10039 
-  COLUMNS(LISTAGG(e.amount, ', ') AS amounts_along_path, SUM(e.amount) AS total_cost)) 
-ORDER BY total_cost
-
-
-SELECT LISTAGG(e.amount, ', ') AS amounts_along_path, SUM(e.amount) AS total_cost
-FROM MATCH CHEAPEST 4 WALK (a:account) (-[e:transaction]-> COST e.amount)* (a)
-       ON financial_transactions
-WHERE a.number = 10039
-ORDER BY total_cost
-
+```sql +SELECT LISTAGG(e.amount, ', ') AS amounts_along_path, SUM(e.amount) AS total_cost +FROM MATCH CHEAPEST 4 WALK (a:account) (-[e:transaction]-> COST e.amount)* (a) + ON financial_transactions +WHERE a.number = 10039 +ORDER BY total_cost +``` ``` +-----------------------------------------------------------------------------+ @@ -3950,7 +3862,7 @@ FetchFirstQuantity ::= The `FETCH FIRST` clause is applied after the [OFFSET clause](#offset-clause). If there are fewer solutions than the fetch quantity, all solutions are returned. -For example, in the following query the first solution is pruned from the result (`OFFSET 1`) and the next two solutions are fetched (`FETCH FIRST 2 ROWS ONLY`). +For example, in the following query the first solution is pruned from the result (`OFFSET 1`) and the next one solution is fetched (`FETCH FIRST 1 ROWS ONLY`). {% include image.html file="example_graphs/financial_transactions.png" %} @@ -4013,7 +3925,6 @@ LIMIT 1 | name | +--------+ | Liam | -| Nikita | +--------+ ``` @@ -5443,21 +5354,40 @@ For example, the following query finds the top two people that transacted the mo {% include image.html file="example_graphs/financial_transactions.png" %} -```sql -SELECT p.name, total_transacted, top_transaction -FROM LATERAL ( SELECT p, SUM(t.amount) AS total_transacted - FROM MATCH (p:person) <- (a:account) -[t:transaction]- () - ON financial_transactions - GROUP BY p - ORDER BY total_transacted DESC - FETCH FIRST 2 ROW ONLY ), - LATERAL ( SELECT t.amount AS top_transaction - FROM MATCH (p) <- (a:account) -[t:transaction]- () - ON financial_transactions - ORDER BY t.amount DESC - FETCH FIRST 2 ROW ONLY ) -ORDER BY total_transacted DESC, top_transaction DESC -``` +
+ + +
+SELECT name, total_transacted, top_transaction
+FROM LATERAL ( SELECT p1_id, name, SUM(amount) AS total_transacted
+               FROM GRAPH_TABLE(financial_transactions MATCH (p1 IS person) <- (a1 IS account) -[t1 IS transaction]- ()
+               COLUMNS(VERTEX_ID(p1) AS p1_id, p1.name, t1.amount))
+               GROUP BY p1_id, name
+               ORDER BY total_transacted DESC
+               FETCH FIRST 2 ROW ONLY ),
+     LATERAL ( SELECT amount AS top_transaction
+               FROM GRAPH_TABLE(financial_transactions MATCH (p2 IS Person) <- (a2 IS account) -[t2 IS transaction]- ()
+                      COLUMNS(VERTEX_ID(p2) AS p2_id, t2.amount))
+               WHERE p2_id = p1_id
+               ORDER BY amount DESC
+               FETCH FIRST 2 ROW ONLY )
+ORDER BY total_transacted DESC, top_transaction DESC
+
+
+SELECT p.name, total_transacted, top_transaction
+FROM LATERAL ( SELECT p, SUM(t.amount) AS total_transacted
+               FROM MATCH (p:person) <- (a:account) -[t:transaction]- ()
+                      ON financial_transactions
+               GROUP BY p
+               ORDER BY total_transacted DESC
+               FETCH FIRST 2 ROW ONLY ),
+     LATERAL ( SELECT t.amount AS top_transaction
+               FROM MATCH (p) <- (a:account) -[t:transaction]- ()
+                      ON financial_transactions
+               ORDER BY t.amount DESC
+               FETCH FIRST 2 ROW ONLY )
+ORDER BY total_transacted DESC, top_transaction DESC
+
``` +----------------------------------------------+ @@ -5471,6 +5401,7 @@ ORDER BY total_transacted DESC, top_transaction DESC ``` In the following query, the `LATERAL` subquery projects the two vertices `a` and `p`, while the outer accesses properties of those vertices. +Note: Vertex and edge variables cannot be projected with `GRAPH_TABLE` operator ```sql SELECT p.name, a.number diff --git a/pre-pages/pgql-2.0-spec.md b/pre-pages/pgql-2.0-spec.md index 085eb100..d38ebcf5 100644 --- a/pre-pages/pgql-2.0-spec.md +++ b/pre-pages/pgql-2.0-spec.md @@ -1374,18 +1374,18 @@ ORDER BY "number", "name" ``` ``` -+-----------------------------+ -| label(n) | number | name | -+-----------------------------+ -| Account | 1001 | | -| Account | 2090 | | -| Account | 8021 | | -| Account | 10039 | | -| Person | | Camille | -| Person | | Liam | -| Person | | Nikita | -| Company | | Oracle | -+-----------------------------+ ++------------------+ +| number | name | ++------------------+ +| 1001 | | +| 2090 | | +| 8021 | | +| 10039 | | +| | Camille | +| | Liam | +| | Nikita | +| | Oracle | ++------------------+ ``` Label expressions are taken into account such that only properties are selected that belong to the specified vertex or @@ -1407,13 +1407,13 @@ ORDER BY "name" ``` ``` -+--------------------+ -| label(n) | name | -+--------------------+ -| Person | Camille | -| Person | Liam | -| Person | Nikita | -+--------------------+ ++---------+ +| name | ++---------+ +| Camille | +| Liam | +| Nikita | ++---------+ ``` A `PREFIX` can be specified to avoid duplicate column names in case all properties of multiple vertex or edge variables are selected.