Skip to content

Instantly share code, notes, and snippets.

@philzook58
Last active March 13, 2023 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philzook58/b543fc07444fb27bba846b4c4be35a6e to your computer and use it in GitHub Desktop.
Save philzook58/b543fc07444fb27bba846b4c4be35a6e to your computer and use it in GitHub Desktop.
Transitive closure
CREATE TABLE tc(a INTEGER, b INTEGER, PRIMARY KEY (a,b));
INSERT OR IGNORE INTO tc(a,b)
VALUES (1,2),(2,3),(3,4);
-- repeat this query many times for naive iteration
-- path(x,z) :- path(x,y), path(y,z).
INSERT OR IGNORE INTO tc SELECT DISTINCT tc0.a, tc1.b -- the head of the rule gives the insert and select fields
FROM tc as tc0, tc as tc1
WHERE tc0.b = tc1.a; -- The body of the rule gives FROM and WHERE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment