cwida / duckpgq-extension

DuckDB extension that adds support for SQL/PGQ
https://duckpgq.notion.site/b8ac652667964f958bfada1c3e53f1bb?v=3b47a8d44bdf4e0c8b503bf23f1b76f2
MIT License
86 stars 7 forks source link

Path finding operator #135

Open Dtenwolde opened 3 months ago

Dtenwolde commented 3 months ago

This PR introduces the path-finding operator developed by Pingan Ren during his MSc thesis.

It doesn't work with the PGQ syntax yet but will be added in future commits.

The operator needs to be enabled with a SET command: set experimental_path_finding_operator=true; Getting the current setting's value: SELECT current_setting('experimental_path_finding_operator');

This PR is limited to unquantified shortest path and path length queries with a * in PGQ.

An example that will trigger the operator:

CREATE TABLE pair(src BIGINT, dst BIGINT); INSERT INTO pair(src, dst) VALUES (0, 1), (1, 2), (2,0);
CREATE TABLE student(id INT); INSERT INTO student(id) VALUES (10), (20), (30), (40);
CREATE TABLE know(src INT, dst INT); INSERT INTO know(src, dst) VALUES (40, 20), (10,30), (10,10), (20,10), (30,10);

WITH shortestpath_cte as (
SELECT src, dst, shortestpathoperator(src, dst, 'pair') as path
FROM pair p
WHERE p.src between (SELECT CSR_OPERATOR(
                             (SELECT count(a.id) as v_size FROM Student a),
                             (SELECT count(k.src) as e_size from know k),
                               a.rowid,
                               c.rowid,
                               k.rowid,
                               t.cnt) FROM Know k
                                           JOIN student a on a.id = k.src
                                           JOIN student c on c.id = k.dst
                                           JOIN (SELECT count(k.src) cnt, a.rowid as a_rowid
                                                   FROM student a
                                                  LEFT JOIN know k ON k.src = a.id
                                                  GROUP BY a.rowid) t
                                              ON t.a_rowid = a.rowid) and p.dst)
 SELECT * FROM shortestpath_cte;

This PR also supports the PGQ syntax:

FROM GRAPH_TABLE (pg
    MATCH p = ANY SHORTEST (a:Person)-[k:Knows]-> *(b:Person) COLUMNS (element_id(p)));

This PR will only support ANY SHORTEST with * (shortest unbounded path patterns). A future PR will add support for shortest path with quantified path patterns and +.