-- Step 1: Load Source nodes
DROP TABLE IF EXISTS Source;
CREATE TABLE Source(id INTEGER);
COPY Source FROM '/hostdata/test/livejournal/Source.csv' (DELIMITER ',', FORMAT csv);

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

-- Step 3: Compute Reachable nodes
WITH RECURSIVE Reach(id) AS (
    -- Base case: start from Source
    SELECT id FROM Source

    UNION

    -- Recursive case: expand along Arc
    SELECT a.y
    FROM Reach r
    JOIN Arc a ON r.id = a.x
)

-- Step 4: Print number of reachable nodes
SELECT COUNT(*) AS num_reachable FROM Reach;
