Re: strange evaluation Window function and SRF functions?

Lists: pgsql-hackers
From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: strange evaluation Window function and SRF functions?
Date: 2012-07-30 16:19:04
Message-ID: CAFj8pRAzXKJ=bkjMpV4oeJ-u=g1Ldf5gYOGGxBhJS15ueW8Dcg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hello

I seen nice trick based on window function
http://stackoverflow.com/questions/11700930/how-can-i-trim-a-text-array-in-postgresql

but isn't it example of wrong evaluation? Result of row_number is not
correct

Regards

Pavel


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: strange evaluation Window function and SRF functions?
Date: 2012-07-30 16:31:00
Message-ID: 1061.1343665860@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> writes:
> I seen nice trick based on window function
> http://stackoverflow.com/questions/11700930/how-can-i-trim-a-text-array-in-postgresql

> but isn't it example of wrong evaluation? Result of row_number is not
> correct

Sure it is ... or at least, you won't find anything in the SQL spec that
says it isn't. The result of a window function is only dependent on the
state of the input, not on SRFs that might happen to be in sibling
SELECT expressions. (This is one example of why SRFs in SELECT lists
aren't terribly well defined.)

A bigger problem with that query is that there's no guarantee it will
preserve ordering of the elements of the arrays.

regards, tom lane


From: Thom Brown <thom(at)linux(dot)com>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: strange evaluation Window function and SRF functions?
Date: 2012-07-30 16:33:33
Message-ID: CAA-aLv5JQqzOgPrxR0JTp-nctw6jpBKzsKGc+TnqKOpN2mjVDg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 30 July 2012 17:19, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> wrote:

> Hello
>
> I seen nice trick based on window function
> http://stackoverflow.com/questions/11700930/how-can-i-trim-a-text-array-in-postgresql
>
> but isn't it example of wrong evaluation? Result of row_number is not
> correct
>

Looks right to me. I guess the way to get the row_number they're after out
of the result set would involve changing OVER () to OVER (ORDER BY
unnest(myTextArrayColumn))

--
Thom


From: David Johnston <polobo(at)yahoo(dot)com>
To: Thom Brown <thom(at)linux(dot)com>
Cc: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: strange evaluation Window function and SRF functions?
Date: 2012-07-30 16:37:18
Message-ID: EDEC968F-78D5-43ED-8E35-0B9DC08B61B9@yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Jul 30, 2012, at 12:33, Thom Brown <thom(at)linux(dot)com> wrote:

> On 30 July 2012 17:19, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> wrote:
> Hello
>
> I seen nice trick based on window function http://stackoverflow.com/questions/11700930/how-can-i-trim-a-text-array-in-postgresql
>
> but isn't it example of wrong evaluation? Result of row_number is not correct
>
> Looks right to me. I guess the way to get the row_number they're after out of the result set would involve changing OVER () to OVER (ORDER BY unnest(myTextArrayColumn))
>

The better way would be to perform the unnest in a sub-select then attach the row number in the outer select.

David J.


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Thom Brown <thom(at)linux(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: strange evaluation Window function and SRF functions?
Date: 2012-07-30 16:47:45
Message-ID: CAFj8pRC8CTTQCFOrk0q2xpL57huVsi_BKkDV_UxbcWAqw5zofw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/7/30 Thom Brown <thom(at)linux(dot)com>

> On 30 July 2012 17:19, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> wrote:
>
>> Hello
>>
>> I seen nice trick based on window function
>> http://stackoverflow.com/questions/11700930/how-can-i-trim-a-text-array-in-postgresql
>>
>> but isn't it example of wrong evaluation? Result of row_number is not
>> correct
>>
>
> Looks right to me. I guess the way to get the row_number they're after
> out of the result set would involve changing OVER () to OVER (ORDER BY
> unnest(myTextArrayColumn))
>
>
it looks like row_number is evaluated before SRF - this behave is
absolutely undefined - for me - more native behave is different evaluation.

Regards

Pavel

> --
> Thom
>


From: Josh Berkus <josh(at)agliodbs(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: strange evaluation Window function and SRF functions?
Date: 2012-07-30 17:10:08
Message-ID: 5016BFF0.6070802@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


> it looks like row_number is evaluated before SRF - this behave is
> absolutely undefined - for me - more native behave is different evaluation.

SRFs which return multiple rows in the SELECT clause have ALWAYS behaved
oddly when it comes to row evaluation (LIMIT, COUNT(), etc.). This
isn't necessarily desireable, but it is consistent with past releases,
and it's not in any way limited to Windowing functions. In general, if
you care about rows when calling such an SRF, you need to subselect it.

It would be nice to clean that up, but you'd have to start with a
comprehensive definition of what the behavior *should* be in all common
cases. And then you'd be in for a big code overhaul.

--
Josh Berkus
PostgreSQL Experts Inc.
http://pgexperts.com


From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: Thom Brown <thom(at)linux(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: strange evaluation Window function and SRF functions?
Date: 2012-07-30 17:12:15
Message-ID: CAHyXU0yaDvb5j7fVHbMB9GfK1KBEnY4jnqnbTQt--+8k5TZy1w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mon, Jul 30, 2012 at 11:47 AM, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> wrote:
>
>
> 2012/7/30 Thom Brown <thom(at)linux(dot)com>
>>
>> On 30 July 2012 17:19, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> wrote:
>>>
>>> Hello
>>>
>>> I seen nice trick based on window function
>>> http://stackoverflow.com/questions/11700930/how-can-i-trim-a-text-array-in-postgresql
>>>
>>> but isn't it example of wrong evaluation? Result of row_number is not
>>> correct
>>
>>
>> Looks right to me. I guess the way to get the row_number they're after
>> out of the result set would involve changing OVER () to OVER (ORDER BY
>> unnest(myTextArrayColumn))
>>
>
> it looks like row_number is evaluated before SRF - this behave is absolutely
> undefined - for me - more native behave is different evaluation.

If it was me, I'd have expanded the array with generate_series (as
with the undocumented information_schema._pg_expandarray) and stacked
the array with array() not array_agg().

merlin


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Josh Berkus <josh(at)agliodbs(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: strange evaluation Window function and SRF functions?
Date: 2012-07-30 17:18:48
Message-ID: 2116.1343668728@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Josh Berkus <josh(at)agliodbs(dot)com> writes:
>> it looks like row_number is evaluated before SRF - this behave is
>> absolutely undefined - for me - more native behave is different evaluation.

> SRFs which return multiple rows in the SELECT clause have ALWAYS behaved
> oddly when it comes to row evaluation (LIMIT, COUNT(), etc.). This
> isn't necessarily desireable, but it is consistent with past releases,
> and it's not in any way limited to Windowing functions. In general, if
> you care about rows when calling such an SRF, you need to subselect it.

> It would be nice to clean that up, but you'd have to start with a
> comprehensive definition of what the behavior *should* be in all common
> cases. And then you'd be in for a big code overhaul.

And a lot of application code breakage, if you change the semantics at all.

My feeling is that SRFs in targetlists are just fundamentally poorly
defined, and the answer is to avoid them not try to make them cleaner.
Most of the real use-cases for them could be handled in a
better-defined, more standard way with LATERAL ... so what we ought
to be spending time on is getting LATERAL done, not worrying about
putting lipstick on tlist SRFs.

regards, tom lane


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Josh Berkus <josh(at)agliodbs(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: strange evaluation Window function and SRF functions?
Date: 2012-07-30 17:54:13
Message-ID: 5016CA45.9050901@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers


On 07/30/2012 01:18 PM, Tom Lane wrote:

>
> My feeling is that SRFs in targetlists are just fundamentally poorly
> defined, and the answer is to avoid them not try to make them cleaner.
> Most of the real use-cases for them could be handled in a
> better-defined, more standard way with LATERAL ... so what we ought
> to be spending time on is getting LATERAL done, not worrying about
> putting lipstick on tlist SRFs.
>
>

+1

LATERAL would be useful for all sorts of reasons anyway.

cheers

andrew


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Josh Berkus <josh(at)agliodbs(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: strange evaluation Window function and SRF functions?
Date: 2012-07-30 19:24:05
Message-ID: CAFj8pRBmd9rY0PZabYE0wEvMKvjgtMHnnxNZnKedztjY6kMJpQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2012/7/30 Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>

> Josh Berkus <josh(at)agliodbs(dot)com> writes:
> >> it looks like row_number is evaluated before SRF - this behave is
> >> absolutely undefined - for me - more native behave is different
> evaluation.
>
> > SRFs which return multiple rows in the SELECT clause have ALWAYS behaved
> > oddly when it comes to row evaluation (LIMIT, COUNT(), etc.). This
> > isn't necessarily desireable, but it is consistent with past releases,
> > and it's not in any way limited to Windowing functions. In general, if
> > you care about rows when calling such an SRF, you need to subselect it.
>
> > It would be nice to clean that up, but you'd have to start with a
> > comprehensive definition of what the behavior *should* be in all common
> > cases. And then you'd be in for a big code overhaul.
>
> And a lot of application code breakage, if you change the semantics at all.
>
> My feeling is that SRFs in targetlists are just fundamentally poorly
> defined, and the answer is to avoid them not try to make them cleaner.
> Most of the real use-cases for tihem could be handled in a
> better-defined, more standard way with LATERAL ... so what we ought
> to be spending time on is getting LATERAL done, not worrying about
> putting lipstick on tlist SRFs.
>

I don't propose any changes - I would to show interesting/strange usage of
SRF - this is a new use case of old issue - and I agree so we need LATERAL
more and early.

Regards

Pavel

>
> regards, tom lane
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
>