charin(), text_char() should return something else for empty input - Mailing list pgsql-hackers

From Tom Lane
Subject charin(), text_char() should return something else for empty input
Date
Msg-id 13338.991068900@sss.pgh.pa.us
Whole thread Raw
Responses Re: charin(), text_char() should return something else for empty input  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
I have been chasing Domingo Alvarez Duarte's report of funny behavior
when assigning an empty string to a "char" variable in plpgsql.  What
it comes down to is that text-to-char conversion does not behave very
well for zero-length input.  charin() returns a null character, leading
to the following bizarreness:

regression=# select 'z' || (''::"char") || 'q';?column?
----------z
(1 row)

regression=# select length('z' || (''::"char") || 'q');length
--------     3
(1 row)

The concatenation result is 'z\0q', which doesn't print nicely :-(.

text_char() produces a completely random result, eg:

regression=# select ''::text::"char";?column?
----------~
(1 row)

and could even coredump in the worst case, since it tries to fetch the
first character of the text input no matter whether there is one or not.

I propose that both of these operations should return a space character
for an empty input string.  This is by analogy to space-padding as you'd
get with char(1).  Any objections?
        regards, tom lane


pgsql-hackers by date:

Previous
From: Jim Mercer
Date:
Subject: Re: Re: OpenOffice compile
Next
From: Oleg Bartunov
Date:
Subject: Regression tes for R-tree