If the query is run with ENABLE_EXPR_REWRITES=0; it completes successfully.
The bug is sensitive to the structure of the HAVING clause (it's there that the error seems to get thrown from during analysis). If, for example, we have HAVING (foo < 1) or (foo < 2) the query succeeds. However, I've seen a more elaborate reproduction where the disjuncts were not tautological (basically (foo < X) OR (foo < Y) for X < Y).
Adding extra redundant clauses (like OR FALSE)) often removes the bug as well.