Re: BUG #5028: CASE returns ELSE value always when type is "char"

From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: <pgsql-bugs(at)postgresql(dot)org>,"Sam Mason" <sam(at)samason(dot)me(dot)uk>
Subject: Re: BUG #5028: CASE returns ELSE value always when type is "char"
Date: 2009-09-04 19:01:07
Message-ID: 4AA11DA3020000250002AADC@gw.wicourts.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Sam Mason <sam(at)samason(dot)me(dot)uk> wrote:

> I fail to see how an error isn't the right thing; if we try with
> some other types let see if you think any of these should succeed.
>
> SELECT CASE WHEN TRUE THEN text 'xxx' ELSE 0 END;
> SELECT CASE WHEN TRUE THEN text 'xxx' ELSE TRUE END;
> SELECT CASE WHEN TRUE THEN text 'xxx' ELSE '{1}'::INT[] END;
> SELECT CASE WHEN TRUE THEN text 'xxx' ELSE array [1] END;
>
> "char" is no different other than, by default, it happens to look a
> lot like any value of text type.

So much so that it has the same name as a text type (wrapped in
quotes) and behaves a lot like one:

test=# SELECT CASE WHEN TRUE THEN text 'xxx' ELSE 'a'::"char" END;
case
------
xxx
(1 row)

test=# select upper('a'::"char");
upper
-------
A
(1 row)

test=# select char_length('a'::"char");
char_length
-------------
1
(1 row)

test=# select substring('a'::"char" from 1 for 1);
substring
-----------
a
(1 row)

Making it behave so much like character-based types and giving it a
name which implies that it is character based and storing a character
in it, but then not treating it like other character types in the
CASE context is bound to cause surprises for people.

> It's a different type (that happens to have some implicit casts to
> confuse things) and hence I can't see why invalid literals should
> not be thrown out.

Only, I guess, because of the name. If it weren't called "char" I
guess I wouldn't be concerned about people expecting it to behave
something like char. If "char" behaved more like char, the 'xxx'
literal wouldn't be taken as input to the type in the above CASE
statement.

-Kevin

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2009-09-04 19:19:10 Re: BUG #5028: CASE returns ELSE value always when type is "char"
Previous Message Tom Lane 2009-09-04 18:04:58 Re: BUG #5035: cast 'text' to 'name' doesnt work in plpgsql function