The UNION construct is somewhat different in that it must match up possibly dissimilar types to become a single result set.
UNION Evaluation
Check for identical types for all results.
Coerce each result from the UNION clauses to match the type of the first SELECT clause or the target column.
tgl=> SELECT text 'a' AS "Text" UNION SELECT 'b'; Text ------ a b (2 rows)
tgl=> SELECT 1.2 AS "Float8" UNION SELECT 1; Float8 -------- 1 1.2 (2 rows)
The types of the union are forced to match the types of the first/top clause in the union:
tgl=> SELECT 1 AS "All integers" tgl-> UNION SELECT '2.2'::float4 tgl-> UNION SELECT 3.3; All integers -------------- 1 2 3 (3 rows)
An alternate parser strategy could be to choose the "best" type of the bunch, but this is more difficult because of the nice recursion technique used in the parser. However, the "best" type is used when selecting into a table:
tgl=> CREATE TABLE ff (f float); CREATE tgl=> INSERT INTO ff tgl-> SELECT 1 tgl-> UNION SELECT '2.2'::float4 tgl-> UNION SELECT 3.3; INSERT 0 3 tgl=> SELECT f AS "Floating point" from ff; Floating point ------------------ 1 2.20000004768372 3.3 (3 rows)