-- Step 1: Load NullEdge
DROP TABLE IF EXISTS NullEdge;
CREATE TABLE NullEdge(x INTEGER, y INTEGER);
COPY NullEdge FROM '/hostdata/test/csda-linux/NullEdge.csv' (DELIMITER ',', FORMAT csv);

-- Step 2: Load Edge
DROP TABLE IF EXISTS Edge;
CREATE TABLE Edge(x INTEGER, y INTEGER);
COPY Edge FROM '/hostdata/test/csda-linux/Edge.csv' (DELIMITER ',', FORMAT csv);

-- Step 3: Compute NullNode as reachability from x via Edge, seeded by NullEdge
WITH RECURSIVE NullNode(x, y) AS (
    -- Base case: from NullEdge
    SELECT x, y FROM NullEdge

    UNION

    -- Recursive case: extend path from (x, w) to (x, y) via Edge(w, y)
    SELECT n.x, e.y
    FROM NullNode n
    JOIN Edge e ON n.y = e.x
)

-- Step 4: Output number of NullNode entries
SELECT COUNT(*) AS num_null_nodes FROM NullNode;
