Re: BUG #18962: Type Conversion incorrect when performing UNION of queries.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: sundar(at)mudupalli(dot)com
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18962: Type Conversion incorrect when performing UNION of queries.
Date: 2025-06-19 15:44:03
Message-ID: 1394964.1750347843@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

PG Bug reporting form <noreply(at)postgresql(dot)org> writes:
> create table public.test_table_1 as
> SELECT
> 'AA'::character(2) AS text_type
> UNION ALL
> SELECT
> 'BB'::character(2) AS text_type
> UNION ALL
> SELECT
> 'CC'::TEXT AS text_type
> ;

> Based on the [type conversion rules for
> Union](https://www.postgresql.org/docs/current/typeconv-union-case.html),
> the column `text_type` in `test_table_1` should resolve to the postgres data
> type `TEXT`.

I wonder how you read those rules to arrive at that result. The
relevant step is

5. Select the first non-unknown input type as the candidate type,
then consider each other non-unknown input type, left to right.
If the candidate type can be implicitly converted to the other
type, but not vice-versa, select the other type as the new
candidate type. Then continue considering the remaining
inputs. If, at any stage of this process, a preferred type is
selected, stop considering additional inputs.

So we start with char(2) as the candidate type, and nothing changes
when we consider the second UNION arm. When we consider the third
UNION arm, there are implicit casts in both directions between
char(2) and text, so we will not change the candidate type there
either.

I'd be the first to agree that this behavior sometimes yields
non-intuitive results, but we've been using it for ~25 years.
Changing it now seems out of the question.

regards, tom lane

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message David G. Johnston 2025-06-19 15:46:52 BUG #18962: Type Conversion incorrect when performing UNION of queries.
Previous Message Dilip Kumar 2025-06-19 15:37:57 Re: [EXTERNAL] Re: BUG #18959: Name collisions of expression indexes during parallel Index creations on a pratitioned table.