SELECT
    distRun.dist_id,
    distRun.label,
    stage,
    stage_id,
    rawImfile.class_id AS component,
    clean,
    rawExp.camera,
    CONCAT_WS('.', outroot, CONVERT(distRun.dist_id, CHAR)) as outdir,
    rawImfile.uri as path_base,         -- change this once rawImfile has path_base
    chipProcessedImfile.path_base as chip_path_base,
    CAST(NULL AS CHAR(255)) AS state,
    CAST(NULL AS CHAR(255)) AS data_state,
    0 as quality,
    distRun.no_magic,
    rawImfile.magicked
FROM distRun
JOIN rawExp ON rawExp.exp_id = stage_id
JOIN rawImfile USING(exp_id)
JOIN chipRun ON chipRun.exp_id = rawExp.exp_id and chipRun.magicked
JOIN chipProcessedImfile
    USING(chip_id, class_id)
LEFT JOIN distComponent 
    ON distRun.dist_id = distComponent.dist_id 
    AND rawImfile.class_id = distComponent.component
WHERE
    distRun.state = 'new'
    AND distRun.clean = 0
    AND distRun.stage = 'raw'
    AND distComponent.dist_id IS NULL
    AND (rawExp.magicked OR distRun.no_magic)
UNION
    -- raw stage clean (dbinfo only)
SELECT
    distRun.dist_id,
    distRun.label,
    stage,
    stage_id,
    'exposure' AS component,
    clean,
    rawExp.camera,
    CONCAT_WS('.', outroot, CONVERT(distRun.dist_id, CHAR)) as outdir,
    CAST(NULL AS CHAR(255)) AS path_base,
    CAST(NULL AS CHAR(255)) AS chip_path_base,
    CAST(NULL AS CHAR(255)) AS state,
    CAST(NULL AS CHAR(255)) AS data_state,
    0 as quality,
    distRun.no_magic,
    rawExp.magicked
FROM distRun
JOIN rawExp ON exp_id = stage_id
LEFT JOIN distComponent 
    ON distRun.dist_id = distComponent.dist_id 
WHERE
    distRun.state = 'new'
    AND distRun.stage = 'raw'
    AND distRun.clean
    AND distComponent.dist_id IS NULL
