SELECT
    magicTree.*,
    rawExp.camera,
    rawExp.exp_id,
    diffSkyfile.path_base,
    magicRun.workdir,
    -- convert magic_id into a boolean value (1 or 0)
    -- note that the type stays a 64 bit int
    magicNodeResult.magic_id IS TRUE as done
FROM magicTree
JOIN magicRun USING(magic_id)
JOIN magicInputSkyfile USING(magic_id, node)
JOIN diffSkyfile 
    ON magicInputSkyfile.diff_id = diffSkyfile.diff_id
    AND magicInputSkyfile.node = diffSkyfile.skycell_id
JOIN rawExp USING(exp_id)
LEFT JOIN magicNodeResult
    ON magicTree.magic_id = magicNodeResult.magic_id
    AND magicTree.node = magicNodeResult.node
WHERE
    magicRun.state = 'run'
    AND magicNodeResult.magic_id IS NULL
    AND magicNodeResult.node IS NULL
