Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1btERC-0006ku-0F for pgsql-general@arkaria.postgresql.org; Sun, 09 Oct 2016 13:46:46 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1btERA-0007Zn-MG for pgsql-general@arkaria.postgresql.org; Sun, 09 Oct 2016 13:46:44 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1btEOr-0004y4-AT for pgsql-general@postgresql.org; Sun, 09 Oct 2016 13:44:21 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1btEOj-0007FY-Ee for pgsql-general@postgresql.org; Sun, 09 Oct 2016 13:44:19 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id C0ABB206B4; Sun, 9 Oct 2016 09:44:11 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute3.internal (MEProxy); Sun, 09 Oct 2016 09:44:11 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=5Q6R2+k+Mobj0Ook9dOxgjpt/zY=; b=DfKtgH vc7Y8BG8ctae2tm8R8cijHI1TAaTt+B9FWkHvJJuujmicm0gutbkZUhlkMrF1wCj jYCPU1CFq9UP8qYoQQOZvK3zPQc5hy5WuoOwAzDguf8fMBOVYbMWI2TWmIFnCov9 5lo0tAk1LRm7XaP+VX07wYZ3uKnbfWRAS7kHE= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=5Q6R2+k+Mobj0Oo k9dOxgjpt/zY=; b=fxaf8eOBLBSlSD6NNV8Ygb33ZN7qzWdOpv3iqwidERt7GrS ThFev1Wc9TfzdvLNavZXgz8wxAsC/90Lts4jTJmbIsTtRyCXMqAqyqrG1BgZW0h3 BGuWNKutDRTY5l1FQlVR4hSt5f1cfM2laFofYw5dmirvnEDrvfvvkI5+Hl5s= X-Sasl-enc: 8vtHbnHHYHk4CpmPSzbc1nAncHsdyHenozxuFKvRNv2l 1476020651 Received: from [192.168.1.2] (174-21-85-118.tukw.qwest.net [174.21.85.118]) by mail.messagingengine.com (Postfix) with ESMTPA id 47EC6F2985; Sun, 9 Oct 2016 09:44:11 -0400 (EDT) Subject: Re: custom average window function failure To: Seb , pgsql-general@postgresql.org References: <87int2qkat.fsf@gmail.com> From: Adrian Klaver Message-ID: Date: Sun, 9 Oct 2016 06:44:10 -0700 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <87int2qkat.fsf@gmail.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-general Precedence: bulk Sender: pgsql-general-owner@postgresql.org 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