| From: | Andrus <kobruleht2(at)hot(dot)ee> | 
|---|---|
| To: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com> | 
| Cc: | pgsql-general <pgsql-general(at)postgresql(dot)org> | 
| Subject: | Re: How to return argument data type from sql function | 
| Date: | 2022-10-14 20:59:52 | 
| Message-ID: | 12a74d8c-58ac-f14c-39c7-3730362e08e7@hot.ee | 
| Views: | Whole Thread | Raw Message | Download mbox | Resend email | 
| Thread: | |
| Lists: | pgsql-general | 
Hi!
Making separate functions for text and bpchar works for me.
> regression=# select pg_typeof(torus(f1)) from char_tbl;
>   pg_typeof
> -----------
>   character
I tried
create or replace FUNCTION torus(eevarus bpchar) returns bpchar 
immutable AS $f$
select translate( $1, U&'\00f8\00e9', U&'\0451\0439' );
$f$ LANGUAGE SQL ;
create temp table test (
charcol char(10) );
insert into test values ('test');
select torus(charcol)
FROM Test
but it still returns result without trailing spaces. So it is not working.
> Another possibility is to have just one function declared
> to take and return anyelement.  You'd get failures at
> execution if the actual argument type isn't coercible
> to and from text (since translate() deals in text) but
> that might be fine.
I tried
create or replace FUNCTION torus(eevarus anylement ) returns anylement 
immutable AS $f$
select translate( $1, U&'\00f8\00e9', U&'\0451\0439' );
$f$ LANGUAGE SQL ;
but got error
type anyelement does not exists.
Finally I tried
create or replace FUNCTION torus(eevarus text ) returns text immutable 
AS $f$
select translate( $1, U&'\00f8\00e9', U&'\0451\0439' );
$f$ LANGUAGE SQL ;
create or replace function public.ColWidth(p_namespace text, p_table 
text, p_field text)
     returns int as $f$
select atttypmod-4 from pg_namespace n, pg_class c, pg_attribute a
          where n.nspname = p_namespace and
              c.relnamespace = n.oid and
              c.relname = p_table and
              a.attrelid = c.oid and
              a.attname = p_field;
$f$ LANGUAGE SQL ;
create table public.test ( charcol char(10) );
insert into test values ('test');
select rpad ( torus(charcol),  colwidth('public', 'test', 'charcol') )
FROM Test
as Adrian Klaver recommends in
at this worked. In this best solution?
How to remove p_namespace  parameter from colwidth()? ColWidth() should 
return column width in first search_path table just like select ... from 
test finds table test.
Andrus.
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Tom Lane | 2022-10-14 21:24:20 | Re: How to return argument data type from sql function | 
| Previous Message | Dominique Devienne | 2022-10-14 15:05:50 | Re: Number of updated rows with LibPQ |