Re: aggregate function for median calculation

From: "Thalis A(dot) Kalfigopoulos" <thalis(at)cs(dot)pitt(dot)edu>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-general(at)postgresql(dot)org, Peter Eisentraut <peter_e(at)gmx(dot)net>, Alex Pilosov <alex(at)pilosoft(dot)com>
Subject: Re: aggregate function for median calculation
Date: 2001-06-21 16:10:11
Message-ID: Pine.LNX.4.21.0106211151280.24987-100000@aluminum.cs.pitt.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, 21 Jun 2001, Tom Lane wrote:

> "Thalis A. Kalfigopoulos" <thalis(at)cs(dot)pitt(dot)edu> writes:
> > I'm still a bit confused about how to declare the type of the transition state.
> > I have a structure that will hold the current state:
> > struct state {
> > int elem_count; //how many numbers have been assigned so far
> > int *elem_area; //ptr to area holding these integers whose median I'll later calculate
> > }
>
> > My question is: how do I declare this in Pg?
>
> Since you haven't made this a full-fledged Postgres type, you don't.
>
> There's no real *need* to make it a full-fledged type, actually, since
> nobody except your two aggregate functions will ever manipulate the
> value. So I'd suggest cheating. Make it a legal "varlena" value by
> having the first word of the struct contain the total length in bytes
> of the object (including itself). Then pretend in the SQL transition
> function declaration and aggregate declaration that the transition
> datatype is any old varlena type --- text or bytea would do fine.
> Nothing except your code will look at anything except the varlena
> length, so the fact that the body of the value means something unusual
> won't matter.
>
> Or, if you want to be rigidly correct, you could make the transition
> value be a legitimate int4 array (_int4), which is only a field or
> three more overhead. But I don't see any value in it, except possibly
> for manual testing of your transition functions.
>
> regards, tom lane
>

I worked on it so the struct state now has 3 fields: int length, int elem_count, int *elements. Now the issue is that the transition function, which I declare as strict, has a different input and state type (int4 and text respectively), so it requires an INITCOND. What should that be? It has to be the text representation of what the state struct is initially. The state initially has elem_count=0, elements=NULL and length=sizeof(struct state) which is 3*sizeof(int) (the 2 integer fields and a pointer i.e. another integer). How do I explain that?

I tried some scenarios for INITCOD like '\0' and 0 but they all crashed :-/

Any ideas?

tia,
thalis

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2001-06-21 16:47:24 Re: aggregate function for median calculation
Previous Message Stephan Szabo 2001-06-21 15:33:49 Re: Foreign Keys to Non-primary keys?