Re: Relational Algebra and Aggregate Functions

From: Robert James <srobertjames(at)gmail(dot)com>
To: Sam Mason <sam(at)samason(dot)me(dot)uk>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Relational Algebra and Aggregate Functions
Date: 2009-07-28 15:26:01
Message-ID: e09785e00907280826l72919ef7v7a2265f548be0114@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Jul 28, 2009 at 9:47 AM, Sam Mason <sam(at)samason(dot)me(dot)uk> wrote:

> On Tue, Jul 28, 2009 at 09:14:38AM -0400, Robert James wrote:
> > Many wrote that the functional programming 'fold' is a good model for
> > relational aggregate functions. I have a few difficulties with this:
> > 1. fold doesn't offer any type of GROUP BY, which is an essential
> component
> > of aggregation.
>
> Not sure if I'd agree, a GROUP BY without any aggregate functions looks
> pretty indistinguishable from just a DISTINCT on the same columns to me.
>
> DISTINCT will collapse duplicates, which is not what we want when computing
COUNT, SUM, or AVG - please see below.

> > 3. fold is defined on sequences, not sets. This doesn't seem to be a
> > problem until you think about cases where there a duplicates of the
> > aggregated field. (For instance, there are 10 bags each weighing 5 lbs,
> and
> > you want SUM(weight) - you need to project weight onto a collection which
> > allows for 10 occurences, or define the aggregate function to work on the
> > whole tuple somehow... I know a man named Krug worked out a formal theory
> > for this...)
>
> I don't see why this is a problem at all; could you give a concrete
> example?
>
Relation LUGGAGE = { (name:'ball', weight:3), (name:'bat', weight:3)}
How do we formalize SELECT SUM(weight) FROM LUGGAGE? We could
project_weight(LUGGAGE) and then apply SUM, except that would give us
{(weight:3), (weight:3)}, which is not a set (it has duplicates). We could
define a new operation: project_to_list (allowing duplicates), or we could
define SUM(weight) over the LUGGAGE relation as a whole - either way, we
need to extend the theory a bit.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Andreas Wenk 2009-07-28 15:35:49 Re: Video available for PGDay SJC '09
Previous Message Robert James 2009-07-28 15:20:38 Re: Relational Algebra and Aggregate Functions