"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Thu, Mar 26, 2015 at 1:49 PM, Timothy Garnett <tgarnett@panjiva.com>
> wrote:
>> but if that is declared strict then it would take the first non-null value
>> and return A in my second example, if declared non-strict then the initial
>> state would be fed as null rather then the first value. Is there a way to
>> declare the function non-strict (so that null values are passed) but still
>> have it initialize to the first value like it would if it was strict?
> You want NULL to both mean "not initialized" and "unknown value" which is
> impossible and SQL does not provide any other universal literal that means
> one or the other.
Yeah. You need distinct representations for "nothing seen yet" and "saw a
NULL"; the built-in behavior doesn't suffice for this.
One idea is for the state value to be of anyarray type: initially null,
and a one-element array containing the first input value once you've seen
that.
It strikes me though that this aggregate is ill-defined by nature. In
particular, if you're going to treat NULL as being a real data value,
then what're you gonna return when there were no input rows? You won't be
able to distinguish "no input rows" from "first input row had a NULL".
Maybe you should rethink whatever activity you were wanting it for.
regards, tom lane