Improve serialization of CTE & UNION combination

Related to #798