pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Philip Carlsen <plcplc@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: valid casts to anyarray
Date: Wed, 27 Sep 2023 16:49:51 +0200
Message-ID: <88bf272f86df0c91a7beb7b229a4a784ff97b9f1.camel@cybertec.at> (raw)
In-Reply-To: <CALkT+D4qBsGL8OruvtZSKL9fXa21HCD-KSDVajujtDs3RV-RDQ@mail.gmail.com>
References: <CALkT+D4qBsGL8OruvtZSKL9fXa21HCD-KSDVajujtDs3RV-RDQ@mail.gmail.com>

On Tue, 2023-09-26 at 21:39 +0200, Philip Carlsen wrote:
> I've been down a rabbit hole today trying to understand what exactly makes a type
> a valid candidate for an ANYARRAY function argument (e.g., something you can 'unnest()').
> 
> My reading has led me across such functions as 'get_promoted_array_type',  'IsTrueArrayType',
> 'can_coerce_type', and 'check_generic_type_consistency', and this has led me to believe
> that any type which has a valid (i.e., non-zero?) pg_type.typelem defined should be applicable.
> 
> However, I cannot seem to be able to call 'unnest' on a 'point': 
> 
> postgres=# select unnest(point(1,2));
> ERROR:  function unnest(point) does not exist
> 
> ... even though according to 'pg_catalog.pg_type' the type 'point' does indeed look
> very array-like (it should be equivalent to an float8 array). The only difference I
> can spot is that it has 'typsubscript=raw_array_subscript_handler', as opposed to
> typsubscript=array_subscript_handler' which is what 'IsTrueArrayType' checks for.
> 
> Can anyone here perhaps enlighten me as to how I can tell if a type is a valid ANYARRAY
> (and bonus points to point out the check I must have missed in the (parser?) source code)?

I'd say that the type has to be an array type...

For example, here is the definition of "cardinality()", which takes "anyarray"
as argument:

Datum
array_cardinality(PG_FUNCTION_ARGS)
{
    AnyArrayType *v = PG_GETARG_ANY_ARRAY_P(0);

    PG_RETURN_INT32(ArrayGetNItems(AARR_NDIM(v), AARR_DIMS(v)));
}

Yours,
Laurenz Albe





view thread (4+ messages)  latest in thread

Message-ID: <88bf272f86df0c91a7beb7b229a4a784ff97b9f1.camel@cybertec.at>
Permalink:  ../88bf272f86df0c91a7beb7b229a4a784ff97b9f1.camel@cybertec.at/
Also on:    postgresql.org/message-id/88bf272f86df0c91a7beb7b229a4a784ff97b9f1.camel@cybertec.at

 · 

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: laurenz.albe@cybertec.at, plcplc@gmail.com
  Subject: Re: valid casts to anyarray
  In-Reply-To: <88bf272f86df0c91a7beb7b229a4a784ff97b9f1.camel@cybertec.at>
  References: <CALkT+D4qBsGL8OruvtZSKL9fXa21HCD-KSDVajujtDs3RV-RDQ@mail.gmail.com> <88bf272f86df0c91a7beb7b229a4a784ff97b9f1.camel@cybertec.at>

* 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