Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pa4Lu-0005JZ-5F for pgsql-general@arkaria.postgresql.org; Thu, 09 Mar 2023 00:41:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pa4Lt-0004v6-0a for pgsql-general@arkaria.postgresql.org; Thu, 09 Mar 2023 00:41:49 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pa4Ls-0004ux-G2 for pgsql-general@lists.postgresql.org; Thu, 09 Mar 2023 00:41:48 +0000 Received: from mail-pg1-x52e.google.com ([2607:f8b0:4864:20::52e]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pa4Lm-0006wH-H8 for pgsql-general@lists.postgresql.org; Thu, 09 Mar 2023 00:41:47 +0000 Received: by mail-pg1-x52e.google.com with SMTP id s17so127839pgv.4 for ; Wed, 08 Mar 2023 16:41:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; t=1678322501; h=to:date:message-id:subject:mime-version:from:from:to:cc:subject :date:message-id:reply-to; bh=NYtecbuBTV1G7O3dVrisTNdT7FYAN17LqQoqp4aFOBo=; b=PnAOB79bYQMIuJyACBGeDF8l75QWVbZ8TMHqjmRpjKKnZB2/mzOjtqix8ordcarkyT eEsRA/KWQHKskEpzgsd7vHfqK0YbW9b8g2w0TIBzG/H+HVXoaZSYU41K2gnbsN5AnC3W pka9vncfmPha/hVUnMp2v6o2t6fwnnqXsJBg8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678322501; h=to:date:message-id:subject:mime-version:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=NYtecbuBTV1G7O3dVrisTNdT7FYAN17LqQoqp4aFOBo=; b=j5QqMyUiQe9NJV8LBrF4Vi7UOYtWR80U2txe8Otx1gJyTMiDc4xjpDJ9BTvB7DmNMQ nqiW1/bfFwHJ3CFXK0OtCPUJRZrzTK+EhxXNF7ngHbHSyR4ZisOYnuwUxrdNNUIOli1E /wvHDKXIuYTbknfde5NTQhb3MvAVV4Wucr+WB8QKRXwPzqKHZ9NefC6zMsoqJrjqsOBU y9yeqx6+ZOvcPQr46LOZ4kyBn1Ltaz8u/ecS4P4XEjEEJrbBfiGEKwFKuy7tXebl3ifp 0Q8ZYCbEHEgvyGX+sAU3nSgLPMM3S6odUGC3tzNctzC91xUMyh6Gh6NpX1VMYV8S9Wal /bMA== X-Gm-Message-State: AO0yUKUddsRX4Ds6VBY0ZscmRScLhEYNJjQ2vy3bIFuAHxUabaRmi1Mu obkgV13/guV0Cg9duT41/aRXyI+lw9Qk1+m5rzA= X-Google-Smtp-Source: AK7set/4qPSrvFW7SbK0R3lOnyHghoauKiHVlLb9oCcyK6ZO0dVLyE9q2ggh3/wJ8pTR7VJptEolWA== X-Received: by 2002:aa7:904f:0:b0:611:a664:8d28 with SMTP id n15-20020aa7904f000000b00611a6648d28mr16692926pfo.4.1678322500880; Wed, 08 Mar 2023 16:41:40 -0800 (PST) Received: from smtpclient.apple ([2601:646:101:7fda:5d84:d5f7:d358:64a5]) by smtp.gmail.com with ESMTPSA id i23-20020aa79097000000b00589a7824703sm9954268pfa.194.2023.03.08.16.41.40 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 08 Mar 2023 16:41:40 -0800 (PST) From: Bryn Llewellyn Content-Type: multipart/alternative; boundary="Apple-Mail=_45807978-C6E3-40F5-899E-D5FD04BDB3F6" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.14\)) Subject: select (17, 42)::s.t2 into... fails with "invalid input syntax" Message-Id: Date: Wed, 8 Mar 2023 16:41:39 -0800 To: pgsql-general list X-Mailer: Apple Mail (2.3654.120.0.1.14) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_45807978-C6E3-40F5-899E-D5FD04BDB3F6 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 I have a horrible feeling that I'm missing the blindingly obvious here. = But I can't spot it. Help! This simple setup produces the expected result: create type s.t1 as (c1 text, c2 text); select ('cat', 'dog')::s.t1; This is the result: (cat,dog) create type s.t2 as (c1 int, c2 int); select (17, 42)::s.t2; This is the result: (17,42) (I know that plsql is doing an under-the-covers typecast to "text" to = display the result. The error (or at least, to me, shock) comes when I = bring PL/pgSQL into the picture: create function s.f() returns table(z text) security definer set search_path =3D pg_catalog, pg_temp language plpgsql as $body$ declare r1 s.t1; r2 s.t2; txt text; begin r1 :=3D (select ('cat', 'dog')::s.t1); z :=3D '1: '||r1.c1||' / '||r1.c2; = return next; select ('cat', 'dog')::s.t1 into r1; z :=3D '2: '||r1.c1||' / '||coalesce(r1.c2, ''); = return next; r2 :=3D (select (17, 42)::s.t2); z :=3D '3: '||(r2.c1)::text||' / '||(r2.c2)::text; = return next; begin select (17, 42)::s.t2 into r2; exception -- invalid input syntax for type integer: "(17,42)" when invalid_text_representation then z :=3D 'invalid_text_representation caught'; = return next; end; select (17, 42)::s.t2 into txt; r2 :=3D txt; z :=3D '4: '||(r2.c1)::text||' / '||(r2.c2)::text; = return next; end; $body$; select s.f(); txttxt It produces this output: 1: cat / dog 2: (cat,dog) / 3: 17 / 42 invalid_text_representation caught 4: 17 / 42 Results #1 and #3, from "UDT-value :=3D (scaler subquery)", are what I = expected. Result #2 tells me what seems to be going on=E2=80=94and it dumbfounds = me. The first text field of my UDT value got "(cat,dog)"; and now that = all input values have been consumed, "c2" got NULL. Do you (all) expect this? And if so, what's the story? This outcome seems to explain the error. The text value "(17,42)" for = "c1", spirited up from "(17, 42)::s.t2", can't be converted to an = integer. Yet more mysterious is why the workaround, go via an intermediate text = value, succeeds: select (17, 42)::s.t2 into txt; r2 :=3D txt; But if I compress it thus: select (((17, 42)::s.t2)::text)::s.t2 into r2; then I'm back to the same 22P02 error: invalid input syntax for type integer: "(17,42)" --Apple-Mail=_45807978-C6E3-40F5-899E-D5FD04BDB3F6 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 I = have a horrible feeling that I'm missing the blindingly obvious here. = But I can't spot it. Help!

This simple setup produces the expected result:

create type s.t1 as (c1 text, c2 text);
select = ('cat', 'dog')::s.t1;

This is the result:

 (cat,dog)

create type = s.t2 as (c1 int,  c2 int);
select (17, = 42)::s.t2;

This is the result:

 (17,42)

(I know that plsql is doing an = under-the-covers typecast to "text" to display the result. The error (or = at least, to me, shock) comes when I bring PL/pgSQL into the = picture:

create function s.f()
  returns table(z = text)
  security definer
  set search_path =3D = pg_catalog, pg_temp
  language plpgsql
as $body$
declare
  r1 s.t1;
  r2 = s.t2;
  txt text;
begin
  r1 :=3D (select = ('cat', 'dog')::s.t1);
  z :=3D '1: '||r1.c1||' / '||r1.c2;  =                     =               return = next;

  select ('cat', 'dog')::s.t1 into = r1;
  z :=3D '2: '||r1.c1||' / '||coalesce(r1.c2, = '<NULL>');              =   return next;

  r2 :=3D (select (17, = 42)::s.t2);
  z :=3D '3: '||(r2.c1)::text||' / = '||(r2.c2)::text;                =     return next;

  begin
  =   select (17, 42)::s.t2 into r2;
  exception
  =   -- invalid input syntax for type integer: = "(17,42)"
    when invalid_text_representation = then
      z :=3D 'invalid_text_representation = caught';                  =     return next;
  end;

  select (17, 42)::s.t2 into txt;
  r2 = :=3D txt;
  z :=3D '4: '||(r2.c1)::text||' / = '||(r2.c2)::text;                =     return next;
end;
$body$;

select = s.f();
txttxt
It = produces this output:

 1: cat / dog
 2: (cat,dog) / <NULL>
 3: 17 / = 42
 invalid_text_representation caught
 4: 17 / 42

Results #1 and #3, from "UDT-value :=3D = (scaler subquery)", are what I expected.

Result #2 tells me what seems to be = going on=E2=80=94and it dumbfounds me. The first text field of my UDT = value got "(cat,dog)"; and now that all input values have been consumed, = "c2" got NULL.

Do you (all) expect this? And if so, what's the = story?

This = outcome seems to explain the error. The text value "(17,42)" for "c1", = spirited up from "(17, 42)::s.t2", can't be converted to an = integer.

Yet = more mysterious is why the workaround, go via an intermediate text = value, succeeds:

select (17, 42)::s.t2 into txt;
r2 :=3D txt;

But if I compress it thus:

select (((17, 42)::s.t2)::text)::s.t2 = into r2;

then I'm = back to the same 22P02 error:

invalid input = syntax for type integer: "(17,42)"

= --Apple-Mail=_45807978-C6E3-40F5-899E-D5FD04BDB3F6--