pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
type cast about int to bit
4+ messages / 3 participants
[nested] [flat]

* type cast about int to bit
@ 2012-02-06 06:11  zoulx1982 <zoulx1982@163.com>
  0 siblings, 3 replies; 4+ messages in thread

From: zoulx1982 @ 2012-02-06 06:11 UTC (permalink / raw)
  To: pgsql-sql

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 , i want to know the reason.
thank you for your timing.



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: type cast about int to bit
@ 2012-02-06 11:05  Jasen Betts <jasen@xnet.co.nz>
  parent: zoulx1982 <zoulx1982@163.com>
  2 siblings, 0 replies; 4+ messages in thread

From: Jasen Betts @ 2012-02-06 11:05 UTC (permalink / raw)
  To: pgsql-sql

does the mailing list mangle these, or is it just GMANE?

On 2012-02-06, zoulx1982 <zoulx1982@163.com> wrote:
>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 , i want to know the reason.
>thank you for your timing.

possibly postgres doesn't know what size to make the result.

-- 
⚂⚃ 100% natural




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: type cast about int to bit
@ 2012-02-07 01:19  Adrian Klaver <adrian.klaver@gmail.com>
  parent: zoulx1982 <zoulx1982@163.com>
  2 siblings, 0 replies; 4+ messages in thread

From: Adrian Klaver @ 2012-02-07 01:19 UTC (permalink / raw)
  To: pgsql-sql; +Cc: zoulx1982 <zoulx1982@163.com>

On Sunday, February 05, 2012 10:11:12 pm zoulx1982 wrote:
> 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 , i want to know the reason.
> thank you for your timing.

My guess it depends on the  fact that bit types are stored as either char or 
varchar depending on whether they are bit or bit varying.
In the first case you are basically doing an int-->char, for which there is a 
built in cast.
In the second case you are doing int-->varchar for which there is not a cast.


-- 
Adrian Klaver
adrian.klaver@gmail.com



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: type cast about int to bit
@ 2012-02-07 03:28  Adrian Klaver <adrian.klaver@gmail.com>
  parent: zoulx1982 <zoulx1982@163.com>
  2 siblings, 0 replies; 4+ messages in thread

From: Adrian Klaver @ 2012-02-07 03:28 UTC (permalink / raw)
  To: zoulx1982 <zoulx1982@163.com>; +Cc: pgsql-sql

On Monday, February 06, 2012 6:42:45 pm zoulx1982 wrote:
> you mean there is no cast function for int  to varchar ?
> i see sure it is.
> 
That is why I said my guess:) If you want to see what is actually going on take 
a look at:
http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/utils/adt/varbit.c;h=adb0836...

The ERROR though is coming further up, in the parser , if I am following 
correctly.  This because as you have found out there is no direct cast from 
integer to varbit. Why that is for someone else to answer, as I don't know.

Though a little playing around got this, not pretty but it seems to work:

test(5432)aklaver=>SELECT 10::bit(3)::varbit(3);
 varbit 
--------
 010
(1 row)

test(5432)aklaver=>SELECT 10::bit(3)::varbit(4);
 varbit 
--------
 010
(1 row)

test(5432)aklaver=>SELECT 10::bit(4)::varbit(4);
 varbit 
--------
 1010
(1 row)

test(5432)aklaver=>SELECT 10::bit(4)::varbit(3);
 varbit 
--------
 101


-- 
Adrian Klaver
adrian.klaver@gmail.com



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2012-02-07 03:28 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2012-02-06 06:11 type cast about int to bit zoulx1982 <zoulx1982@163.com>
2012-02-06 11:05 ` Jasen Betts <jasen@xnet.co.nz>
2012-02-07 01:19 ` Adrian Klaver <adrian.klaver@gmail.com>
2012-02-07 03:28 ` Adrian Klaver <adrian.klaver@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox