String literal doesn't autocast to text type - Mailing list pgsql-general

From Alex Ignatov
Subject String literal doesn't autocast to text type
Date
Msg-id 56D9C311.7000102@postgrespro.ru
Whole thread Raw
Responses Re: String literal doesn't autocast to text type  (Melvin Davidson <melvin6925@gmail.com>)
Re: String literal doesn't autocast to text type  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
Hello!
Why string literal like 'Hello world!' doesnt automagicaly cast to text
type?

postgres=# select pg_typeof('Hello world');
  pg_typeof
-----------
  unknown
(1 row)

But for example literal like 1.1 automagically cast to numeric( not
float8, float4, whatever)
postgres=# select pg_typeof(1.1);
  pg_typeof
-----------
  numeric
(1 row)

That why we cant do the following without explicit type casting:
postgres=# select t.c||' world' from (select 'Hello' as c) as t;
ERROR:  failed to find conversion function from unknown to text

but that ok:
postgres=# select t.c||' world' from (select 'Hello'::text as c) as t;
   ?column?
-------------
  Hello world
(1 row)

or this is ok too:
postgres=# select t.c::text||' world' from (select 'Hello' as c) as t;
   ?column?
-------------
  Hello world
(1 row)

Sure we can create our cast:
postgres=# create cast (unknown as text) with inout as implicit;
CREATE CAST
and after that we have:
postgres=# select t.c||' world' from (select 'Hello' as c) as t;
   ?column?
-------------
  Hello world
(1 row)

But why we don't have this type cast by default in Postgres? Is there
any fundamental restriction on that or there is some reasons for that?


--
Alex Ignatov
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company



pgsql-general by date:

Previous
From: Alexander Farber
Date:
Subject: Re: Check constraints for varchar[] and varchar[][] columns in a table
Next
From: Melvin Davidson
Date:
Subject: Re: String literal doesn't autocast to text type