Skip to content
Snippets Groups Projects
Commit 62c7946e authored by Sven-Ove Hänsel's avatar Sven-Ove Hänsel
Browse files

update pg path query

parent c8ddf9a2
Branches
Tags
No related merge requests found
...@@ -30,46 +30,48 @@ descendant|WITH RECURSIVE DescendantCTE AS ( ...@@ -30,46 +30,48 @@ descendant|WITH RECURSIVE DescendantCTE AS (
) )
SELECT DISTINCT node_no, source, Level FROM DescendantCTE; SELECT DISTINCT node_no, source, Level FROM DescendantCTE;
--- ---
path|WITH RECURSIVE path_cte AS ( path|WITH RECURSIVE search_path(edge_no, path, dest, visited, depth) AS (
-- Anchor member initialization
SELECT SELECT
el.source AS start_node, e.edge_no,
el.dest AS end_node, ARRAY[e.source, e.dest]::VARCHAR[] AS path, -- Cast to VARCHAR[]
ARRAY[el.source::varchar] AS path, -- Cast to varchar to ensure type consistency e.dest,
1 AS depth -- Keep track of the depth to prevent infinite loops ARRAY[e.source]::VARCHAR[] AS visited, -- Cast to VARCHAR[]
1 AS depth
FROM FROM
edge_list el edge_list e
WHERE WHERE
el.source = '0CF2BB3E-36B8-11E8-BF66-D9AA8AFF4A69' -- Replace with the UUID of the starting node e.source = 'A6A7C956-0132-5506-96D1-2A7DE97CB400' -- Starting Node 43C4E3CF-B655-5D90-8F1F-EFB3E7D27AA1
UNION ALL UNION ALL
-- Recursive member definition
SELECT SELECT
p.start_node, e.edge_no,
el.dest, sp.path || e.dest,
p.path || el.dest::varchar, -- Ensure el.dest is cast to varchar e.dest,
p.depth + 1 visited || e.dest::VARCHAR, -- Ensure consistent type
sp.depth + 1
FROM FROM
edge_list el edge_list e, search_path sp
JOIN
path_cte p ON el.source = p.end_node
WHERE WHERE
NOT (el.dest = ANY(p.path)) -- Prevent cycles by ensuring we don't revisit nodes e.source = sp.dest
AND p.depth < 100 -- Example limit to prevent infinite recursion AND NOT (e.dest = ANY(sp.visited)) -- Adjusted to ensure clarity and type consistency
) )
, shortest_paths AS (
-- Final query to select the path SELECT
SELECT distinct path,
start_node, end_node, path, depth depth
FROM FROM
path_cte search_path
WHERE WHERE
end_node = '586C56DA-CB56-745E-96CB-52069E742598' -- Replace with the UUID of the ending node dest = '8DA367BF-36C2-11E8-BF66-D9AA8AFF4A69' -- Destination Node 458F029B-36C2-11E8-BF66-D9AA8AFF4A69
ORDER BY ORDER BY
depth ASC -- Optional: orders by the shortest path depth ASC
--LIMIT 1 -- Optionally limit to one path if multiple paths exist LIMIT 1 -- Adjust based on desired number of shortest paths
; )
SELECT
path
FROM
shortest_paths;
--- ---
k-hop|WITH hop1 AS ( k-hop|WITH hop1 AS (
SELECT DISTINCT e.source, e.dest SELECT DISTINCT e.source, e.dest
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment