Assigning it to Sanja because it's a regression introduced with a bugfix for (revno 3574 on maria/5.3 and revno 3526 on maria/5.5).
The following test case
on the affected versions, and
on unaffected versions. The latter is correct.
Reproducible with MyISAM, Aria, InnoDB.
Reproducible starting from revno 3574 on maria/5.3 from revno 3526 on maria/5.5, and up to current ones (3584 and 3544, correspondingly).
Not reproducible if I replace the subquery with a view.
Not reproducible if I replace RIGHT JOIN with LEFT JOIN.
The subquery itself, executed separately, produces the expected result.
Reproducible with the default optimizer switch as well as with optimizer_switch='derived_merge=on' only.
EXPLAIN (with the default optimizer_switch):