pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Kellerer <spam_eater@gmx.net>
To: pgsql-general@lists.postgresql.org
Subject: Re: lead() with arrays - strange behaviour
Date: Thu, 8 Aug 2019 13:59:50 +0200
Message-ID: <4f2f232b-7f3f-4d69-e448-2944e10682e7@gmx.net> (raw)
In-Reply-To: <CAKJS1f9hLjqQ+q6wEkU3CvuzR7GrvrDopOW6rTpceD9gTfxcUw@mail.gmail.com>
References: <bd1a59b7-0e3d-0a37-fe2d-f60e00dbb050@gmx.net>
	<CAKJS1f9hLjqQ+q6wEkU3CvuzR7GrvrDopOW6rTpceD9gTfxcUw@mail.gmail.com>

David Rowley schrieb am 08.08.2019 um 13:03:
>> The following statement tries to find the overlapping values in id_list between the current row and the next row:
>>
>>     select id,
>>            id_list,
>>            lead(id_list) over (order by id) as next_list,
>>            array(select unnest(id_list) intersect select unnest(lead(id_list) over (order by id))) as common_ids
>>     from sample_data;
>>
>> The above returns:
>>
>>     id | id_list | next_list | common_ids
>>     ---+---------+-----------+-----------
>>      1 | {1,2,3} | {2,3,4}   | {}
>>      2 | {2,3,4} | {4,5,6}   | {}
>>      3 | {4,5,6} |           | {}
>>
>> The empty array for "common_ids" is obviously incorrect.
> 
> I think you're confused with what the SELECT with the empty FROM
> clause does here.  In your subquery "id_list" is just a parameter from
> the outer query. LEAD(id_list) OVER (ORDER BY id) is never going to
> return anything since those are both just effectively scalar values,
> to which there is no "next" value.

id_list is a column in the table and as you can see in the output 
lead(id_list) most definitely returns the array from the next row. 

and "select unnest(some_array)" works just fine as you can see 
when "next_list" is taken from the derived table. 

Thomas







view thread (5+ messages)  latest in thread

Message-ID: <4f2f232b-7f3f-4d69-e448-2944e10682e7@gmx.net>
Permalink:  ../4f2f232b-7f3f-4d69-e448-2944e10682e7@gmx.net/
Also on:    postgresql.org/message-id/4f2f232b-7f3f-4d69-e448-2944e10682e7@gmx.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: spam_eater@gmx.net
  Subject: Re: lead() with arrays - strange behaviour
  In-Reply-To: <4f2f232b-7f3f-4d69-e448-2944e10682e7@gmx.net>
  References: <bd1a59b7-0e3d-0a37-fe2d-f60e00dbb050@gmx.net> <CAKJS1f9hLjqQ+q6wEkU3CvuzR7GrvrDopOW6rTpceD9gTfxcUw@mail.gmail.com> <4f2f232b-7f3f-4d69-e448-2944e10682e7@gmx.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox