Re: Newbie, Howto access Array-Slots in user defined functions?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: 100(dot)179370(at)germanynet(dot)de (Martin Jacobs)
Cc: pgsql-general(at)hub(dot)org
Subject: Re: Newbie, Howto access Array-Slots in user defined functions?
Date: 2000-10-07 04:27:56
Message-ID: 18185.970892876@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

100(dot)179370(at)germanynet(dot)de (Martin Jacobs) writes:
> CREATE FUNCTION lessbyte (_bytea, _bytea) RETURNS bool AS
> 'SELECT $1[1] < $2[1];' LANGUAGE 'sql';
> ERROR: Unable to identify an operator '<' for types 'bytea' and 'bytea'
> You will have to retype this query using an explicit cast

There is nothing wrong with your syntax --- you've declared a function
that takes two arrays of bytea, selects the first element of each, and
compares 'em. But bytea doesn't support comparison operators ... or
much of anything, actually. There is a get_byte function, so you could
conceivably build what you want starting with

create function lessbyte(bytea, bytea) returns bool as
'select get_byte($1,0) < get_byte($2,0)' language 'sql';

However, I don't see any reasonable way to deal with variable-length
inputs without a loop, and SQL functions don't have looping constructs.

Given the lack of operators, type bytea isn't currently useful for
much except plain storage and retrieval of raw byte sequences.
Have you got a strong reason for using bytea, rather than some
better-supported type like text? Heck, even array of char would
work better:

regression=# CREATE FUNCTION lessbyte(_char, _char) returns bool as
regression-# 'SELECT $1[1] < $2[1];' LANGUAGE 'sql';
CREATE

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Anton Kalauzky 2000-10-07 08:58:42 PostgreSQL encoding question
Previous Message Billy G. Allie 2000-10-07 00:37:55 Re: postgres via shell scripts