Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WKWmb-0002wA-MZ for pgsql-sql@arkaria.postgresql.org; Mon, 03 Mar 2014 17:36:05 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WKWmb-0000Qu-6m for pgsql-sql@arkaria.postgresql.org; Mon, 03 Mar 2014 17:36:05 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WKWma-0000Qo-FA for pgsql-sql@postgresql.org; Mon, 03 Mar 2014 17:36:04 +0000 Received: from sam.nabble.com ([216.139.236.26]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WKWmX-0002io-BK for pgsql-sql@postgresql.org; Mon, 03 Mar 2014 17:36:03 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1WKWmV-0007Im-St for pgsql-sql@postgresql.org; Mon, 03 Mar 2014 09:35:59 -0800 Date: Mon, 3 Mar 2014 09:35:59 -0800 (PST) From: David Johnston To: pgsql-sql@postgresql.org Message-ID: <1393868159887-5794419.post@n5.nabble.com> In-Reply-To: <87iorvurrd.fsf@net82.ceos.umanitoba.ca> References: <87iorvurrd.fsf@net82.ceos.umanitoba.ca> Subject: Re: creating a new aggregate function MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 4.5 (++++) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Sebastian P. Luque wrote > Hi, > > I'm trying to implement an aggregate function to calculate the average > angle from one or more angles and corresponding magnitudes. So my first > step is to design a function that decomposes the angles and magnitudes > and returns the corresponding x and y vectors, and the following works > does this: > > ---<--------------------cut > here---------------start------------------->--- > CREATE OR REPLACE FUNCTION decompose_angle(IN angle numeric, IN magnitude > numeric, > OUT x numeric, OUT y numeric) RETURNS record AS > $BODY$ > BEGIN > x := sin(radians(angle)) * magnitude; > y := cos(radians(angle)) * magnitude; > END; > $BODY$ > LANGUAGE plpgsql STABLE > COST 100; > ALTER FUNCTION decompose_angle(numeric, numeric) > OWNER TO sluque; > COMMENT ON FUNCTION decompose_angle(numeric, numeric) IS > 'Decompose an angle and magnitude into x and y vectors.'; > ---<--------------------cut > here---------------end--------------------->--- > > Before moving on to writing the full aggregate, I'd appreciate any > suggestions to understand how to go about writing an aggregate for the > above, that would return the average x and y vectors. I would suggest you design custom types that incorporate the angle,magnitude-pair and the x,y-pair and write your functions to operate using those types. The documentation for CREATE AGGREGATE is fairly detailed and can be summarized as: 1) Do something for each input row - you maintain state internally 2) Do something after the last row has been processed - using the state from #1 http://www.postgresql.org/docs/9.3/interactive/sql-createaggregate.html What you do in those two steps depends fully on the algorithm you need which is beyond my immediate knowledge. Note the use of plpgsql in your function is probably undesirable since you are not actually using any procedural logic; an SQL language function is better since it gives the system more optimization options. David J. -- View this message in context: http://postgresql.1045698.n5.nabble.com/creating-a-new-aggregate-function-tp5794414p5794419.html Sent from the PostgreSQL - sql mailing list archive at Nabble.com. -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql