domain on bit(N) type produces strange results

Started by Kris Jurkaover 21 years ago3 messagesbugs
Jump to latest
#1Kris Jurka
books@ejurka.com

Creating a domain on bit(N) doesn't seem to work as expected when casting
to the domain type.

CREATE DOMAIN bit4 AS bit(4);

SELECT 7::bit(4), 7::bit4;
bit | bit4
------+------
0111 | 1000
(1 row)

Reported in #postgresql by msw_alt.

Kris Jurka

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Kris Jurka (#1)
Re: domain on bit(N) type produces strange results

Kris Jurka <books@ejurka.com> writes:

Creating a domain on bit(N) doesn't seem to work as expected when casting
to the domain type.

CREATE DOMAIN bit4 AS bit(4);

SELECT 7::bit(4), 7::bit4;
bit | bit4
------+------
0111 | 1000
(1 row)

What's going on here is that "7::bit4" is implemented as
"7::bit::bit(4)", and since 7::bit is taken to mean 7::bit(1),
the result follows.

Bit is the only typmod-using datatype for which casting to the type
with typmod -1 risks discarding information. In a brief look I'm
not sure whether this can easily be fixed without introducing unwanted
side-effects.

regards, tom lane

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tom Lane (#2)
Re: domain on bit(N) type produces strange results

I wrote:

Kris Jurka <books@ejurka.com> writes:

Creating a domain on bit(N) doesn't seem to work as expected when casting
to the domain type.

What's going on here is that "7::bit4" is implemented as
"7::bit::bit(4)", and since 7::bit is taken to mean 7::bit(1),
the result follows.

I've fixed it to collapse this into a direct "7::bit(4)" coercion.
Thanks for the test case.

regards, tom lane