Обсуждение: How to overload POSITION

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

How to overload POSITION

От
Troels Arvin
Дата:
Hello,

I'm working on a datatype which I call dnaseq. The basic stuff like
input/output functions, comparison operators, etc., work well.

I have overloaded the CHARACTER_LENGTH function without problems:

CREATE OR REPLACE FUNCTION character_length(dnaseq)
RETURNS integer
AS 'dnaseq','dnaseq_charlen'
LANGUAGE 'C' IMMUTABLE STRICT;

Now, I want to overload the POSITION(lftval IN rgtval) function, but how
do I do that? I've tried

CREATE OR REPLACE FUNCTION position(dnaseq,dnaseq)
RETURNS integer
AS 'dnaseq'
LANGUAGE 'C' IMMUTABLE STRICT;

But now I get:
ERROR:  syntax error at or near "(" at character 36
LINE 1: CREATE OR REPLACE FUNCTION position(dnaseq,dnaseq)

I assume this is because the POSITION function uses "IN" to separate
arguments, in stead of a comma. Is there a way to overload it?

--
Greetings from Troels Arvin, Copenhagen, Denmark


Re: How to overload POSITION

От
Tom Lane
Дата:
Troels Arvin <troels@arvin.dk> writes:
> I assume this is because the POSITION function uses "IN" to separate
> arguments, in stead of a comma. Is there a way to overload it?

Double-quote "position" in the create function command --- it's a
keyword.

Be aware also of the argument order gotcha.  Per gram.y:

            | POSITION '(' position_list ')'
                {
                    /* position(A in B) is converted to position(B, A) */
                    FuncCall *n = makeNode(FuncCall);
                    n->funcname = SystemFuncName("position");
                    n->args = $3;
                    n->agg_star = FALSE;
                    n->agg_distinct = FALSE;
                    $$ = (Node *)n;
                }

Come to think of it, you'll also need to create your function in the
pg_catalog schema, because that's implied by SystemFuncName().  Which
means it won't get dumped by pg_dump.  How wedded are you to being able
to say "IN"?

            regards, tom lane

Re: How to overload POSITION

От
Troels Arvin
Дата:
On Tue, 16 Nov 2004 15:56:00 -0500, Tom Lane wrote:

> [... cut advice ...]

Thanks.

> How wedded are you to being able to say "IN"?

It's only a would-be-nice-to-have.

My dnaseq data type exploits the fact that DNA sequences are made from a
very small alphabet (four characters), so strings can be compressed/packed
4:1 while still being directly usable. A POSITION(dnaseq IN dnaseq) would
mean that the dnaseq-values don't have to be converted to text before
being searched.

But I can live with my existing DNASEQ_POSITION(dnaseq,dnaseq) solution
(which works directly on the dnaseq packed strings).

(I will also try to create a specialized index for long strings, hopefully
using some substring array algorithmics - but that's another story.)

--
Greetings from Troels Arvin, Copenhagen, Denmark