Re: BUG #18962: Type Conversion incorrect when performing UNION of queries. - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #18962: Type Conversion incorrect when performing UNION of queries.
Date
Msg-id 1394964.1750347843@sss.pgh.pa.us
Whole thread Raw
In response to BUG #18962: Type Conversion incorrect when performing UNION of queries.  (PG Bug reporting form <noreply@postgresql.org>)
List pgsql-bugs
PG Bug reporting form <noreply@postgresql.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



pgsql-bugs by date:

Previous
From: Dilip Kumar
Date:
Subject: Re: [EXTERNAL] Re: BUG #18959: Name collisions of expression indexes during parallel Index creations on a pratitioned table.
Next
From: "David G. Johnston"
Date:
Subject: BUG #18962: Type Conversion incorrect when performing UNION of queries.