Re: converting E'C:\\something' to bytea

From: Vlad Romascanu <vromascanu(at)accurev(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Bruce Momjian <bruce(at)momjian(dot)us>, pgsql-general(at)postgresql(dot)org
Subject: Re: converting E'C:\\something' to bytea
Date: 2011-03-16 17:00:18
Message-ID: AANLkTinwFi_6XC=W0Gy9PntssryW_enw-4GNzaEt+kEs@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi, Tom,

Why does:

CREATE CAST (text AS bytea) WITHOUT FUNCTION;
SELECT E'C:\\something'::text::bytea;

work as expected, but (with the original text->bytea cast in place):

CREATE DOMAIN my_varlena AS text;
CREATE CAST (my_varlena AS bytea) WITHOUT FUNCTION;
SELECT E'C:\\something'::my_varlena::bytea;

does not (i.e. appears to cast from the domain's base type) -- is this
related to http://postgresql.1045698.n5.nabble.com/bug-non-working-casts-for-domain-td1944238.html
, should it be documented?

Thx,
V.

On Wed, Mar 16, 2011 at 12:10 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Bruce Momjian <bruce(at)momjian(dot)us> writes:
>> Vlad Romascanu wrote:
>>> Is there any way of casting (reinterpreting) a varchar/text field
>>> containing arbitrary backslashes to bytea without making an escaped
>>> copy of the varchar/text first?
>
>> Well, the '\\' is being converted to '\' because of the single-quotes,
>> and then bytea is saying it doesn't know how to process \something.  It
>> sounds like you want bytea but don't want the ability to use backslash
>> escapes to input the bytea values.  I am unsure how to accomplish that.
>
> A really dangerous way is
>
> CREATE CAST (text AS bytea) WITHOUT FUNCTION;
>
> It's dangerous because it assumes more than it ought to about the
> internal representation of the two types ... but for a one-shot
> conversion I think it'd be all right.
>
>                        regards, tom lane
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Daniele Varrazzo 2011-03-16 17:19:35 Saving bytes in custom data type
Previous Message tv 2011-03-16 16:48:43 Re: query taking much longer since Postgres 8.4 upgrade