pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Kellerer <spam_eater@gmx.net>
To: pgsql-general@lists.postgresql.org
Subject: Arrays, casting and "constrained" data types
Date: Fri, 24 Aug 2018 13:43:15 +0200
Message-ID: <693a9f48-07ce-ce8b-d4e6-c890b36aadcf@gmx.net> (raw)

I stumbled across the following:

Consider the following (simplified) table:

  create table test
  (
    val     numeric(20,0),
    ref_val numeric(20,0)
  );
 
and the following very simple recursive CTE:

    with recursive tree as (
      select val, array[val] as path
      from test
      union all
      select child.val, parent.path||child.val 
      from test child
        join tree parent on parent.val = child.ref_val
    )
    select *
    from tree;

The above fails with: recursive query "tree" column 2 has type numeric(20,0)[] in non-recursive term but type numeric[] overall

However, when casting the array in the non-recursive part, it still doesn't work:

    with recursive tree as (
      select val, array[val]::numeric[] as path
      from test
      union all
      select child.val, parent.path||child.val 
      from test child
        join tree parent on parent.val = child.ref_val
    )
    select *
    from tree;

same error as before. Neither does array[val::numeric] work. 

However, appending the column to an empty array works: 

    with recursive tree as (
      select val, array[]::numeric[] || val as path
      from test
      union all
      select child.val, parent.path||child.val 
      from test child
        join tree parent on parent.val = child.ref_val
    )
    select *
    from tree;


My question is: why isn't "array[val]::numeric[]" enough to create a numeric[] array in the non-recursive part? 

I have seen the same problem with "varchar(x)" 

Thomas








Message-ID: <693a9f48-07ce-ce8b-d4e6-c890b36aadcf@gmx.net>
Permalink:  ../693a9f48-07ce-ce8b-d4e6-c890b36aadcf@gmx.net/
Also on:    postgresql.org/message-id/693a9f48-07ce-ce8b-d4e6-c890b36aadcf@gmx.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: spam_eater@gmx.net
  Subject: Re: Arrays, casting and "constrained" data types
  In-Reply-To: <693a9f48-07ce-ce8b-d4e6-c890b36aadcf@gmx.net>
  References: <693a9f48-07ce-ce8b-d4e6-c890b36aadcf@gmx.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox