Обсуждение: about type cast

Поиск
Список
Период
Сортировка

about type cast

От
zoulx1982
Дата:
hi,
there is a problem about type cast that i don't understand, follow is my test.
 
postgres=# select 10::bit(3);
 bit
-----
 010
(1 row)
postgres=# select 10::bit varying(3);
ERROR:  cannot cast type integer to bit varying
LINE 1: select 10::bit varying(3);
                 ^
postgres=#
 
my question is why int can cast to bit , but bot for bit varying?
i want to know the reason.
thank you for your timing.


Re: about type cast

От
Robert Haas
Дата:
2012/2/6 zoulx1982 <zoulx1982@163.com>:
> hi,
> there is a problem about type cast that i don't understand, follow is my
> test.
>
> postgres=# select 10::bit(3);
>  bit
> -----
>  010
> (1 row)
> postgres=# select 10::bit varying(3);
> ERROR:  cannot cast type integer to bit varying
> LINE 1: select 10::bit varying(3);
>                  ^
> postgres=#
>
> my question is why int can cast to bit , but bot for bit varying?
> i want to know the reason.
> thank you for your timing.

Off the top of my head, I'm guessing that it's just a case of nobody
having implemented it.  You can do this:

rhaas=# select 10::bit(3)::varbit;varbit
--------010
(1 row)

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


Re: about type cast

От
Tom Lane
Дата:
Robert Haas <robertmhaas@gmail.com> writes:
> 2012/2/6 zoulx1982 <zoulx1982@163.com>:
>> my question is why int can cast to bit , but bot for bit varying?

> Off the top of my head, I'm guessing that it's just a case of nobody
> having implemented it.

I have some vague recollection that we didn't want to be squishy about
the length of the resulting bitstring.  When you cast to bit you're more
or less forced to specify what you want, since the spec-mandated default
length is only 1.  varbit would leave us having to make a decision in
the code.

Anyway, try searching the pgsql-hackers archives if you want some
history.
        regards, tom lane