Fwd: PSQL Help from your biggest fan

From: Evan Stanford <evanstanford1(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org, Grzegorz Jaśkiewicz <gryzman(at)gmail(dot)com>
Subject: Fwd: PSQL Help from your biggest fan
Date: 2012-08-17 22:56:22
Message-ID: CAMEe77Z8QE4DLE1ymvY_4qZPgVv08262zkhzj=gDWV-f0DnJ8w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi Grzegorz and Pgsql-General,
Can you forward this to Scott Bailey? I tried sending it to his old email,
but it seems to be closed.
Or could you answer my question yourself?

Thank you so much,
Evan Stanford

---------- Forwarded message ----------
From: Evan Stanford <evanstanford1(at)gmail(dot)com>
Date: Fri, Aug 17, 2012 at 3:53 PM
Subject: PSQL Help from your biggest fan
To: artacus(at)comcast(dot)net

Hi Scott Bailey,
I am a huge fan of the aggregate function you have posted that I seem to
come across all over the internet.
But I think I found a bug in one of them:

I tried your code in Postgres 8.2:

CREATE OR REPLACE FUNCTION _final_mode(anyarray)

RETURNS anyelement AS

$BODY$

SELECT a

FROM unnest($1) a

GROUP BY 1

ORDER BY COUNT(1) DESC, 1

LIMIT 1;

$BODY$

LANGUAGE 'sql' IMMUTABLE;

DROP AGGREGATE IF EXISTS mode(anyelement);

CREATE AGGREGATE mode(anyelement)
(SFUNC=array_append,STYPE=anyarray,FINALFUNC=_final_mode,INITCOND='{}');

I also added the unnest function (although mine seemed to already have it).

I tested it like this:

sdap=# create table Z as (select 7 as value);

sdap=# select mode(value) from Z;

mode

------

7

(1 row) --WORKS

sdap=# insert into Z values (8);

sdap=# insert into Z values (8);

select mode(value) from Z;

mode

------

8

(1 row) --WORKS

sdap=# insert into Z values (NULL);

sdap=# select mode(value) from Z;

ERROR: null array element where not supported (arrayfuncs.c:872)

Any ideas?

Thank you so much,

Your biggest fan,

Evan Stanford

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Uwe Schroeder 2012-08-18 00:04:03 Re: Views versus user-defined functions: formatting, comments, performance, etc.
Previous Message Merlin Moncure 2012-08-17 22:49:38 Re: Views versus user-defined functions: formatting, comments, performance, etc.