Re: Questionable result from lead(0) IGNORE NULLS

From: Tatsuo Ishii <ishii(at)postgresql(dot)org>
To: ojford(at)gmail(dot)com
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Questionable result from lead(0) IGNORE NULLS
Date: 2025-10-08 00:45:37
Message-ID: 20251008.094537.1069126567025828266.ishii@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Oliver,

I have just pushed a change to WinGetFuncArgInPartition() in
nodeWindowAgg.c to fix Coverity issues. So please update your git
respository.

> The result looks wrong. So I've just tried removing the "&& relpos != 0"
> and I get:
>
> SELECT x, y, lead(x, 0) IGNORE NULLS OVER w FROM g
> WINDOW w AS (ORDER BY y);
> x | y | lead
> ---+---+------
> | 1 |
> | 2 |
> 1 | 3 |
> (3 rows)
>
> Nothing appears for lead at all. So it was doing something but doesn't look
> like it handles the lead(x, 0) case

I think we need to change this:

forward = relpos > 0 ? 1 : -1;
:
:
/*
* Get the next nonnull value in the partition, moving forward or backward
* until we find a value or reach the partition's end.
*/
do
{
int nn_info; /* NOT NULL info */

abs_pos += forward;
if (abs_pos < 0) /* apparently out of partition */
break;

In lead(0, x) case, abs_pos==0 and foward==-1. So it exits the loop
due to out of partition. Probably we need to change
forward = relpos > 0 ? 1 : -1;
to
forward = relpos >= 0 ? 1 : -1;
and change the do..while loop to a for loop?

Best regards,
--
Tatsuo Ishii
SRA OSS K.K.
English: http://www.sraoss.co.jp/index_en/
Japanese:http://www.sraoss.co.jp

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2025-10-08 02:55:04 Re: comment for TwoPhaseGetOldestXidInCommit
Previous Message Thomas Munro 2025-10-08 00:39:14 Re: ReadRecentBuffer() doesn't scale well