Search code examples
pipelinedb

Extracting the counter from the AVG aggregate in PipelineDB


I have this continuous view:

=# CREATE CONTINUOUS VIEW v AS
SELECT id::int, count(v::int) AS counter, avg(v) AS average
FROM mystream
GROUP BY id;

So I need the average, and the number of elements used to compute this average (counter). is there a way to extract this count from the AVG data structure, so I don't have to create a counter field ?

=# TABLE v_mrel0;
 id | counter | average
----+---------+---------
  1 |    2    | {2,30}

Solution

  • Unfortunately, there isn't. Internals of transition states are meant to be private. In you case of avg(integer) it's a two-element array, so you could potentially read the first element of the array, however this won't work with avg of other data types where the transition state is a byte array and there's no machinery to interface with it.

    Is there a reason why having a separate count aggregate doesn't work for you?