I think you're on the right track. Note that the error occurs during sql-to-rel. It has passed through validation successfully, and recognized that, in SqlNode format, the two occurrences of case when emp.empno in (3) then 0 else 1 end are structurally identical, as required for them to be grouping expressions. But it looks as if the expressions are translated separately and therefore their RexNode representations look different. IIRC there's some logic in there to track aliased expressions through translation, but if so, it's not working correctly here.