Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
ma_code
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sven-Ove Hänsel
ma_code
Commits
62c7946e
Commit
62c7946e
authored
1 year ago
by
Sven-Ove Hänsel
Browse files
Options
Downloads
Patches
Plain Diff
update pg path query
parent
c8ddf9a2
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
code/infrastructure/eval/query/pg/queries/queries.txt
+31
-29
31 additions, 29 deletions
code/infrastructure/eval/query/pg/queries/queries.txt
with
31 additions
and
29 deletions
code/infrastructure/eval/query/pg/queries/queries.txt
+
31
−
29
View file @
62c7946e
...
@@ -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 e
l
edge_list e
WHERE
WHERE
e
l
.source = '
0CF2BB3E-36B8-11E8-BF66-D9AA8AFF4A69' -- Replace with the UUID of the s
tarting
n
ode
e.source = '
A6A7C956-0132-5506-96D1-2A7DE97CB400' -- S
tarting
N
ode
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
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment