Re: Mysterious ::text::char cast: ascii(chr(32)::char) = 0

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Joel Jacobson" <joel(at)compiler(dot)org>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Mysterious ::text::char cast: ascii(chr(32)::char) = 0
Date: 2021-02-21 06:10:16
Message-ID: 3436457.1613887816@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

"Joel Jacobson" <joel(at)compiler(dot)org> writes:
> When a text string of a single space character is casted to a character,
> I would assume the result to be, a space character,
> but for some reason it's the null character.

This is because of the weird semantics of char(N). chr(32) produces
a TEXT value containing one space, which you then cast to CHAR(1),
making the trailing space semantically insignificant. But the
ascii() function requires a TEXT argument, so we immediately cast
the string back to TEXT, and that cast is defined to strip any
trailing spaces. Thus, what gets delivered to ascii() is an empty
TEXT string, causing it to return 0.

If you'd just done ascii(chr(c)), you'd have gotten c.

regards, tom lane

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Joel Jacobson 2021-02-21 07:13:11 Re: Bizarre behavior of \w in a regular expression bracket construct
Previous Message Amit Kapila 2021-02-21 06:02:29 Re: repeated decoding of prepared transactions