11 ms·
CREATE TABLE edges ( source INT, target INT ); INSERT INTO edges (source, target) SELECT generate_series as source, generate_series
by greenavocado 3y ago
CREATE TABLE edges (
source INT,
target INT
);
INSERT INTO edges (source, target)
SELECT generate_series as source, generate_series + 1 as target
FROM generate_series(0, 999);
WITH RECURSIVE path_lengths AS (
-- Base case: Direct paths from edges
SELECT source, target, 1 AS distance
FROM edges
UNION ALL
-- Recursive step: Double the path length by joining on intermediate nodes
SELECT p.source, e.target, p.distance + 1 AS distance
FROM path_lengths AS p
JOIN edges AS e ON p.target = e.source
WHERE p.distance < 256 -- Control the recursion depth (2^8=256 for C8 equivalent)
)
-- Final query to select paths from source 0, similar to the logic program's final goal
, final_paths AS (
SELECT source, target, MIN(distance) AS distance
FROM path_lengths
WHERE source = 0
GROUP BY source, target
)
SELECT * FROM final_paths
ORDER BY source, target;
You can confirm the output matches by pasting it and clicking run here: https://extendsclass.com/postgresql-online.html https://extendsclass.com/postgresql-online.html
- ryanjshaw 3y agoThat's really not bad. I'm looking at CozoDB for a hobby project of mine, but if Logica could produce output like this consistently I'd probably prefer it because I'm much more familiar with maintaining RDBMSs.
- greenavocado 3y agoLogica didn't output that. I posted SQL equivalent to the original Logica.