SELECT DISTINCT
    magicDSRun.magic_ds_id,
    magicDSRun.magic_id,
    chipRun.exp_id,
    magicDSRun.label,
    camera,
    magicMask.uri as streaks_uri,
    CAST(NULL AS CHAR(255)) AS inv_streaks_uri,
    stage,
    stage_id,
    'exposure' as component,
    CAST(NULL AS CHAR(255)) AS uri,
    camProcessedExp.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 camRun ON magicDSRun.stage_id = camRun.cam_id
JOIN camProcessedExp ON camRun.cam_id = camProcessedExp.cam_id
JOIN chipRun USING(chip_id)
JOIN rawExp ON chipRun.exp_id = rawExp.exp_id
LEFT JOIN magicDSFile
    ON magicDSRun.magic_ds_id = magicDSFile.magic_ds_id
WHERE
    magicDSRun.state = 'new'
    AND magicDSRun.stage = 'camera'
    AND camRun.state = 'full'
    AND chipRun.state = 'full'
    AND chipRun.magicked
    AND camProcessedExp.fault = 0
    AND camProcessedExp.quality = 0
    AND magicDSFile.component IS NULL
