PRAGMA threads=64;

-- Step 1: Load edge list from CSV (assumes no header)
CREATE OR REPLACE TABLE edges AS
SELECT column0 AS node1id, column1 AS node2id
FROM read_csv_auto('/users/zhhong/FlowLog/test/livejournal/Arc.csv');

-- Step 2: Recursive propagation of component IDs
WITH RECURSIVE cc(id, comp) AS (
    SELECT node1id, node1id AS comp FROM edges

    UNION

    SELECT e.node2id AS id, MIN(c.comp)
    FROM cc AS c
    JOIN edges AS e ON e.node1id = c.id
    GROUP BY e.node2id
)

-- Step 4: Deduplicate by selecting minimum comp for each node
SELECT COUNT(*) FROM (
    SELECT id, MIN(comp) AS comp
    FROM cc
    GROUP BY id
) AS deduped;

