Обсуждение: BUG #17137: Bug with datatypes definition in CTE

Поиск
Список
Период
Сортировка

BUG #17137: Bug with datatypes definition in CTE

От
PG Bug reporting form
Дата:
The following bug has been logged on the website:

Bug reference:      17137
Logged by:          Alex Zhuravlev
Email address:      alexone07@mail.ru
PostgreSQL version: 13.3
Operating system:   Red Hat 8.4.1-1, 64-bit
Description:

SQL expression doesn't execute with error:
SQL Error [42804]: ERROR: recursive query "t" column 1 has type character
varying(20) in non-recursive term but type character varying overall
  Help: Cast the output of the non-recursive term to the correct type.

*But it works with text datatype.

Some repro's:
First:
WITH RECURSIVE t AS (
SELECT a::varchar AS b
  FROM (
       SELECT 'a'::varchar(20) AS a
       ) AS x
UNION ALL
SELECT 'a'::varchar AS b
  FROM t
)
SELECT *
  FROM t;

The Second:
CREATE TABLE tm1(a varchar(20)); SELECT a, a::varchar FROM tm1 \gdesc

The third:
CREATE TABLE t(n) AS SELECT '5.5'::numeric(20, 2); SELECT n::numeric FROM t
\gdesc

https://dbfiddle.uk/?rdbms=postgres_13&fiddle=c3bfe57c351cc783399fad539d5690d0


Re: BUG #17137: Bug with datatypes definition in CTE

От
Bruce Momjian
Дата:
On Fri, Aug  6, 2021 at 06:12:28PM +0000, PG Bug reporting form wrote:
> SQL expression doesn't execute with error:
> SQL Error [42804]: ERROR: recursive query "t" column 1 has type character
> varying(20) in non-recursive term but type character varying overall
>   Help: Cast the output of the non-recursive term to the correct type.
> 
> *But it works with text datatype.
> 
> Some repro's:
> First:
> WITH RECURSIVE t AS (
> SELECT a::varchar AS b
>   FROM (
>        SELECT 'a'::varchar(20) AS a
>        ) AS x
> UNION ALL
> SELECT 'a'::varchar AS b
>   FROM t
> )
> SELECT *
>   FROM t;

Well, this is an interesting report.  First, the failure goes back at
least as far as 9.6, and this isn't complained about often.  However,
someone did complain about it on August 4:

    https://www.postgresql.org/message-id/flat/CABNQVagu3bZGqiTjb31a8D5Od3fUMs7Oh3gmZMQZVHZ%3DuWWWfQ%40mail.gmail.com

and on August 6, Tom Lane patched PG 14 beta and PG master to fix this:

    commit 5c056b0c25
    Author: Tom Lane <tgl@sss.pgh.pa.us>
    Date:   Fri Aug 6 17:32:54 2021 -0400
    
        Don't elide casting to typmod -1.
    
        Casting a value that's already of a type with a specific typmod
        to an unspecified typmod doesn't do anything so far as run-time
        behavior is concerned.  However, it really ought to change the
        exposed type of the expression to match.  Up to now,
        coerce_type_typmod hasn't bothered with that, which creates gotchas
        in contexts such as recursive unions.  If for example one side of
        the union is numeric(18,3), but it needs to be plain numeric to
        match the other side, there's no direct way to express that.
    
        This is easy enough to fix, by inserting a RelabelType to update the
        exposed type of the expression.  However, it's a bit nervous-making
        to change this behavior, because it's stood for a really long time.
        (I strongly suspect that it's like this in part because the logic
        pre-dates the introduction of RelabelType in 7.0.  The commit log
        message for 57b30e8e2 is interesting reading here.)  As a compromise,
        we'll sneak the change into 14beta3, and consider back-patching to
        stable branches if no complaints emerge in the next three months.
    
        Discussion: https://postgr.es/m/CABNQVagu3bZGqiTjb31a8D5Od3fUMs7Oh3gmZMQZVHZ=uWWWfQ@mail.gmail.com

You can read above that we might backpatch this into PG 13 and other
supported releases in the future if there are no complaints in the next
three months.  If you want to, you can apply the patch yourself and test
it.

-- 
  Bruce Momjian  <bruce@momjian.us>        https://momjian.us
  EDB                                      https://enterprisedb.com

  If only the physical world exists, free will is an illusion.