SELECT DISTINCT
    magicDSRun.magic_ds_id,
    magicRun.magic_id,
    magicRun.exp_id,
    magicDSRun.label,
    camera,
    magicMask.uri as streaks_uri,
    CAST(NULL AS CHAR(255)) AS inv_streaks_uri,
    stage,
    stage_id,
    class_id as component,
    rawImfile.uri as uri,
    CAST(NULL AS CHAR(255)) as path_base,
    magicRun.inverse,
    camProcessedExp.path_base as cam_path_base,
    outroot,
    recoveryroot,
    re_place,
    remove
FROM magicDSRun
JOIN magicMask USING (magic_id)
JOIN magicRun USING (magic_id)
JOIN camProcessedExp USING(cam_id)
JOIN rawImfile ON magicRun.exp_id = rawImfile.exp_id
LEFT JOIN magicDSFile
    ON magicDSRun.magic_ds_id = magicDSFile.magic_ds_id
    AND magicDSFile.component = rawImfile.class_id
WHERE
    magicDSRun.state = 'new'
    AND magicDSRun.stage = 'raw'
    AND magicDSFile.component IS NULL
