pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Seb <spluque@gmail.com>
To: pgsql-general@postgresql.org
Subject: Re: custom average window function failure
Date: Sun, 9 Oct 2016 06:44:10 -0700
Message-ID: <ff476cf9-beaf-991d-b2ef-cc3aab6d5ac8@aklaver.com> (raw)
In-Reply-To: <87int2qkat.fsf@gmail.com>
References: <87int2qkat.fsf@gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-general>

On 10/08/2016 08:21 PM, Seb wrote:
> Hello,
>
> Until I upgraded to PostgreSQL 9.6, a custom average function was
> working well as a window function.  It's meant to average a composite
> type:
>
> CREATE TYPE public.angle_vectors AS
>    (x double precision,
>     y double precision);
> COMMENT ON TYPE public.angle_vectors
>   IS 'This type holds the x (sine) and y (cosine) components of angle(s).';
>
> The average function:
>
> CREATE OR REPLACE FUNCTION public.angle_vectors_avg(angle_vectors_arr angle_vectors[])
>   RETURNS vector AS
> $BODY$
> DECLARE
> 	x_avg double precision;
> 	y_avg double precision;
> 	magnitude double precision;
> 	angle_avg double precision;
>
> BEGIN
> 	SELECT avg(x) INTO x_avg FROM unnest(angle_vectors_arr) irows;
> 	SELECT avg(y) INTO y_avg FROM unnest(angle_vectors_arr) irows;
> 	magnitude := sqrt((x_avg ^ 2.0) + (y_avg ^ 2.0));
> 	angle_avg := degrees(atan2(x_avg, y_avg));
> 	IF (angle_avg < 0 ) THEN
> 		angle_avg := angle_avg + 360.0;
> 	END IF;
> 	RETURN (angle_avg, magnitude);
> END
> $BODY$
>   LANGUAGE plpgsql STABLE
>   COST 100;
> COMMENT ON FUNCTION public.angle_vectors_avg(angle_vectors[]) IS 'This function computes the average angle from an array of concatenated angle_vectors data type singletons.  It returns vector data type.';
>
> And the aggregate:
>
> CREATE AGGREGATE public.avg(angle_vectors) (
>   SFUNC=array_append,
>   STYPE=angle_vectors[],
>   FINALFUNC=angle_vectors_avg
> );
>
> Query below used to work in PostgreSQL 9.5:
>
> SELECT "time", avg((random(), random())::angle_vectors) over w
> from generate_series('2016-10-08'::timestamp, '2016-10-10'::timestamp, '5 hours') as t("time")
> window w as (partition by date_trunc('day', "time") order by "time");
>
> but is now failing with the following message in 9.6:
>
> ERROR:  input data type is not an array
>
> ********** Error **********
>
> ERROR: input data type is not an array
> SQL state: 42804
>
> Any thoughts on what has changed that is leading to this failure?

Not sure. When I tried using the above(on 9.5) it failed during the 
CREATE OR REPLACE FUNCTION public.angle_vectors_avg stage with:

ERROR:  type "vector" does not exist


So where is that coming from in your setup?

-- 
Adrian Klaver
adrian.klaver@aklaver.com


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



view thread (15+ messages)  latest in thread

Message-ID: <ff476cf9-beaf-991d-b2ef-cc3aab6d5ac8@aklaver.com>
Permalink:  ../ff476cf9-beaf-991d-b2ef-cc3aab6d5ac8@aklaver.com/
Also on:    postgresql.org/message-id/ff476cf9-beaf-991d-b2ef-cc3aab6d5ac8@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, spluque@gmail.com
  Subject: Re: custom average window function failure
  In-Reply-To: <ff476cf9-beaf-991d-b2ef-cc3aab6d5ac8@aklaver.com>
  References: <87int2qkat.fsf@gmail.com> <ff476cf9-beaf-991d-b2ef-cc3aab6d5ac8@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox